Skip to content

Commit 4a1d3ea

Browse files
authored
Add Yoast SEO integration for media pages site health check (#2136)
1 parent 058b6ad commit 4a1d3ea

File tree

4 files changed

+228
-0
lines changed

4 files changed

+228
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Add Yoast SEO integration for media pages site health check

integration/class-yoast-seo.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Yoast SEO integration file.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Integration;
9+
10+
/**
11+
* Yoast SEO integration class.
12+
*/
13+
class Yoast_Seo {
14+
15+
/**
16+
* Initialize the class, registering WordPress hooks.
17+
*/
18+
public static function init() {
19+
\add_filter( 'site_status_tests', array( self::class, 'add_site_health_tests' ) );
20+
}
21+
22+
/**
23+
* Add Yoast-specific site health tests.
24+
*
25+
* @param array $tests The site health tests array.
26+
*
27+
* @return array The modified tests array.
28+
*/
29+
public static function add_site_health_tests( $tests ) {
30+
// Only add the test if attachment post type is supported by ActivityPub.
31+
if ( self::is_attachment_supported() ) {
32+
$tests['direct']['activitypub_yoast_seo_media_pages'] = array(
33+
'label' => \__( 'Yoast SEO Media Pages Test', 'activitypub' ),
34+
'test' => array( self::class, 'test_yoast_seo_media_pages' ),
35+
);
36+
}
37+
38+
return $tests;
39+
}
40+
41+
/**
42+
* Test if Yoast's "Enable media pages" setting is properly configured.
43+
*
44+
* @return array The test result.
45+
*/
46+
public static function test_yoast_seo_media_pages() {
47+
$result = array(
48+
'label' => \__( 'Yoast SEO media pages are enabled', 'activitypub' ),
49+
'status' => 'good',
50+
'badge' => array(
51+
'label' => \__( 'ActivityPub', 'activitypub' ),
52+
'color' => 'green',
53+
),
54+
'description' => \sprintf(
55+
'<p>%s</p>',
56+
\__( 'Media pages are enabled in Yoast SEO, which allows media attachments to be federated and interacted with through ActivityPub.', 'activitypub' )
57+
),
58+
'actions' => '',
59+
'test' => 'test_yoast_seo_media_pages',
60+
);
61+
62+
if ( self::is_media_pages_disabled() ) {
63+
$result['status'] = 'recommended';
64+
$result['label'] = \__( 'Yoast SEO media pages should be enabled', 'activitypub' );
65+
$result['badge']['color'] = 'orange';
66+
$result['description'] = \sprintf(
67+
'<p>%s</p>',
68+
\__( 'Yoast SEO&#8217;s &#8220;Enable media pages&#8221; setting is currently disabled. Since you have media attachments configured to be federated through ActivityPub, you should enable media pages so that media can be properly accessed and interacted with by ActivityPub clients and other federated platforms.', 'activitypub' )
69+
);
70+
$result['actions'] = \sprintf(
71+
'<p>%s</p>',
72+
\sprintf(
73+
// translators: %s: Yoast SEO settings URL.
74+
\__( 'You can enable media pages in <a href="%s">Yoast SEO > Settings > Advanced > Media pages</a>.', 'activitypub' ),
75+
\esc_url( \admin_url( 'admin.php?page=wpseo_page_settings#/media-pages' ) )
76+
)
77+
);
78+
}
79+
80+
return $result;
81+
}
82+
83+
/**
84+
* Check if Yoast SEO media pages are disabled.
85+
*
86+
* @return bool True if media pages are disabled, false otherwise.
87+
*/
88+
public static function is_media_pages_disabled() {
89+
// Get Yoast SEO options.
90+
$yoast_options = \get_option( 'wpseo_titles' );
91+
92+
if ( ! is_array( $yoast_options ) ) {
93+
return false;
94+
}
95+
96+
// Check if disable-attachment is set to true (media pages disabled).
97+
return isset( $yoast_options['disable-attachment'] ) && true === $yoast_options['disable-attachment'];
98+
}
99+
100+
/**
101+
* Check if attachment post type is supported by ActivityPub.
102+
*
103+
* @return bool True if attachment is supported, false otherwise.
104+
*/
105+
private static function is_attachment_supported() {
106+
$supported_post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) );
107+
return in_array( 'attachment', $supported_post_types, true );
108+
}
109+
}

integration/load.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ function ( $transformer, $data, $object_class ) {
127127
WP_Rest_Cache::init();
128128
}
129129

130+
/**
131+
* Adds Yoast SEO support.
132+
*
133+
* This class handles the compatibility with Yoast SEO.
134+
*
135+
* @see https://wordpress.org/plugins/wordpress-seo/
136+
*/
137+
if ( \defined( 'WPSEO_VERSION' ) ) {
138+
Yoast_Seo::init();
139+
}
140+
130141
/**
131142
* Load the Surge integration.
132143
*
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Test Yoast SEO integration.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Tests\Integration;
9+
10+
use Activitypub\Integration\Yoast_Seo;
11+
12+
/**
13+
* Test the Yoast integration.
14+
*
15+
* @group integration
16+
* @coversDefaultClass \Activitypub\Integration\Yoast_Seo
17+
*/
18+
class Test_Yoast_Seo extends \WP_UnitTestCase {
19+
20+
/**
21+
* Test that Yoast SEO integration adds site health tests when attachment is supported.
22+
*/
23+
public function test_yoast_seo_site_health_test_registration_with_attachment_support() {
24+
// Simulate Yoast being active.
25+
if ( ! defined( 'WPSEO_VERSION' ) ) {
26+
define( 'WPSEO_VERSION', '20.0' );
27+
}
28+
29+
// Add attachment to supported post types.
30+
\update_option( 'activitypub_support_post_types', array( 'post', 'attachment' ) );
31+
32+
// Initialize the Yoast integration.
33+
Yoast_Seo::init();
34+
35+
// Get site health tests.
36+
$tests = \apply_filters( 'site_status_tests', array() );
37+
38+
// Check if our test is registered.
39+
$this->assertArrayHasKey( 'direct', $tests );
40+
$this->assertArrayHasKey( 'activitypub_yoast_seo_media_pages', $tests['direct'] );
41+
$this->assertEquals( 'Yoast SEO Media Pages Test', $tests['direct']['activitypub_yoast_seo_media_pages']['label'] );
42+
}
43+
44+
/**
45+
* Test that Yoast SEO integration does not add site health tests when attachment is not supported.
46+
*/
47+
public function test_yoast_seo_site_health_test_not_registered_without_attachment_support() {
48+
// Simulate Yoast being active.
49+
if ( ! defined( 'WPSEO_VERSION' ) ) {
50+
define( 'WPSEO_VERSION', '20.0' );
51+
}
52+
53+
// Remove attachment from supported post types.
54+
\update_option( 'activitypub_support_post_types', array( 'post' ) );
55+
56+
// Initialize the Yoast integration.
57+
Yoast_Seo::init();
58+
59+
// Get site health tests.
60+
$tests = \apply_filters( 'site_status_tests', array( 'direct' => array() ) );
61+
62+
// Check if our test is NOT registered.
63+
$this->assertArrayNotHasKey( 'activitypub_yoast_seo_media_pages', $tests['direct'] );
64+
}
65+
66+
/**
67+
* Test media pages disabled check.
68+
*/
69+
public function test_is_media_pages_disabled() {
70+
// Test with media pages enabled (default WordPress behavior).
71+
\update_option( 'wpseo_titles', array( 'disable-attachment' => false ) );
72+
$this->assertFalse( Yoast_Seo::is_media_pages_disabled() );
73+
74+
// Test with media pages disabled (recommended setting).
75+
\update_option( 'wpseo_titles', array( 'disable-attachment' => true ) );
76+
$this->assertTrue( Yoast_Seo::is_media_pages_disabled() );
77+
78+
// Test with no Yoast options.
79+
\delete_option( 'wpseo_titles' );
80+
$this->assertFalse( Yoast_Seo::is_media_pages_disabled() );
81+
}
82+
83+
/**
84+
* Test the site health test execution.
85+
*/
86+
public function test_yoast_media_pages_site_health_test() {
87+
// Test when media pages are properly enabled (good status for ActivityPub).
88+
\update_option( 'wpseo_titles', array( 'disable-attachment' => false ) );
89+
$result = Yoast_Seo::test_yoast_seo_media_pages();
90+
91+
$this->assertEquals( 'good', $result['status'] );
92+
$this->assertEquals( 'Yoast SEO media pages are enabled', $result['label'] );
93+
$this->assertEquals( 'green', $result['badge']['color'] );
94+
95+
// Test when media pages are disabled (recommended status to enable them).
96+
\update_option( 'wpseo_titles', array( 'disable-attachment' => true ) );
97+
$result = Yoast_Seo::test_yoast_seo_media_pages();
98+
99+
$this->assertEquals( 'recommended', $result['status'] );
100+
$this->assertEquals( 'Yoast SEO media pages should be enabled', $result['label'] );
101+
$this->assertEquals( 'orange', $result['badge']['color'] );
102+
$this->assertStringContainsString( 'Yoast SEO > Settings', $result['actions'] );
103+
}
104+
}

0 commit comments

Comments
 (0)