-
Notifications
You must be signed in to change notification settings - Fork 58
fix(parse.ly): update meta_type default #4724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+199
−1
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f1b2614
fix(parse.ly): update meta_type default
chickenn00dle d490b6e
fix: add migrator
chickenn00dle 35964b7
Merge remote-tracking branch 'origin/release' into fix/update-default…
adekbadek 117704c
fix(parse.ly): migrate absent meta_type key, add tests
chickenn00dle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
| /** | ||
| * Parse.ly integration class. | ||
| * | ||
| * @package Newspack | ||
| */ | ||
|
|
||
| namespace Newspack; | ||
|
|
||
| defined( 'ABSPATH' ) || exit; | ||
|
|
||
| /** | ||
| * Parse.ly integration: migrates existing installs off the legacy `json_ld` | ||
| * meta_type default. | ||
| */ | ||
| class Parsely { | ||
| /** | ||
| * Name of the option that records whether the meta_type migration has run. | ||
| * | ||
| * @var string | ||
| */ | ||
| const META_TYPE_MIGRATION_OPTION = 'newspack_parsely_meta_type_migrated'; | ||
|
|
||
| /** | ||
| * Register hooks. | ||
| */ | ||
| public static function init() { | ||
| add_action( 'admin_init', [ __CLASS__, 'migrate_meta_type' ] ); | ||
| } | ||
|
|
||
| /** | ||
| * Migrate existing Parse.ly installs from the legacy `json_ld` meta_type | ||
| * default to `repeated_metas`, which avoids conflicts with Yoast SEO's own | ||
| * JSON-LD output. | ||
| * | ||
| * The switch is intentional and unconditional for any site still rendering | ||
| * `json_ld`: because Parse.ly's own default for an absent `meta_type` is | ||
| * also `json_ld`, a missing key is treated as the legacy default and | ||
| * rewritten too. This means a deliberate publisher `json_ld` choice is | ||
| * indistinguishable from the old default and gets switched as well — which | ||
| * is desired, since the Yoast conflict applies regardless of intent. | ||
| * | ||
| * Runs on every `admin_init` until wp-parsely is active, then migrates the | ||
| * site once and records completion. On sites that never activate Parse.ly | ||
| * the migration simply stays pending (two cached option reads per request). | ||
| */ | ||
| public static function migrate_meta_type() { | ||
| if ( get_option( self::META_TYPE_MIGRATION_OPTION ) ) { | ||
|
chickenn00dle marked this conversation as resolved.
chickenn00dle marked this conversation as resolved.
|
||
| return; | ||
| } | ||
|
|
||
| if ( ! is_plugin_active( 'wp-parsely/wp-parsely.php' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $parsely_settings = get_option( 'parsely', [] ); | ||
| if ( ! is_array( $parsely_settings ) ) { | ||
| $parsely_settings = []; | ||
| } | ||
|
|
||
| // Treat an absent key as the legacy default: wp-parsely merges its own | ||
| // `json_ld` default at read time, so a missing `meta_type` renders the | ||
| // same conflicting JSON-LD output we're migrating away from. | ||
| $meta_type = $parsely_settings['meta_type'] ?? 'json_ld'; | ||
| if ( 'json_ld' === $meta_type ) { | ||
| $parsely_settings['meta_type'] = 'repeated_metas'; | ||
| update_option( 'parsely', $parsely_settings ); | ||
| } | ||
|
|
||
| update_option( self::META_TYPE_MIGRATION_OPTION, true ); | ||
| } | ||
| } | ||
| Parsely::init(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| <?php | ||
| /** | ||
| * Tests the Parse.ly meta_type migration. | ||
| * | ||
| * @package Newspack\Tests | ||
| */ | ||
|
|
||
| use Newspack\Parsely; | ||
|
|
||
| /** | ||
| * Tests the Parse.ly meta_type migration. | ||
| */ | ||
| class Newspack_Test_Parsely extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Reset migration state and active plugins before each test. | ||
| */ | ||
| public function set_up() { | ||
| parent::set_up(); | ||
| delete_option( Parsely::META_TYPE_MIGRATION_OPTION ); | ||
| delete_option( 'parsely' ); | ||
| update_option( 'active_plugins', [] ); | ||
| } | ||
|
|
||
| /** | ||
| * Mark wp-parsely as active. | ||
| */ | ||
| private function activate_parsely() { | ||
| update_option( 'active_plugins', [ 'wp-parsely/wp-parsely.php' ] ); | ||
| } | ||
|
|
||
| /** | ||
| * A stored `json_ld` meta_type is switched to `repeated_metas`. | ||
| */ | ||
| public function test_migrates_explicit_json_ld() { | ||
| $this->activate_parsely(); | ||
| update_option( | ||
| 'parsely', | ||
| [ | ||
| 'apikey' => 'example.com', | ||
| 'meta_type' => 'json_ld', | ||
| ] | ||
| ); | ||
|
|
||
| Parsely::migrate_meta_type(); | ||
|
|
||
| $settings = get_option( 'parsely' ); | ||
| $this->assertEquals( 'repeated_metas', $settings['meta_type'] ); | ||
| $this->assertEquals( 'example.com', $settings['apikey'] ); | ||
| $this->assertNotEmpty( get_option( Parsely::META_TYPE_MIGRATION_OPTION ) ); | ||
| } | ||
|
|
||
| /** | ||
| * A missing meta_type key is treated as the legacy `json_ld` default and rewritten. | ||
| */ | ||
| public function test_migrates_absent_meta_type() { | ||
| $this->activate_parsely(); | ||
| update_option( 'parsely', [ 'apikey' => 'example.com' ] ); | ||
|
|
||
| Parsely::migrate_meta_type(); | ||
|
|
||
| $settings = get_option( 'parsely' ); | ||
| $this->assertEquals( 'repeated_metas', $settings['meta_type'] ); | ||
| $this->assertEquals( 'example.com', $settings['apikey'] ); | ||
| } | ||
|
|
||
| /** | ||
| * An empty stored option array is migrated (absent key === legacy default). | ||
| */ | ||
| public function test_migrates_empty_option() { | ||
| $this->activate_parsely(); | ||
| update_option( 'parsely', [] ); | ||
|
|
||
| Parsely::migrate_meta_type(); | ||
|
|
||
| $settings = get_option( 'parsely' ); | ||
| $this->assertEquals( 'repeated_metas', $settings['meta_type'] ); | ||
| } | ||
|
|
||
| /** | ||
| * A non-`json_ld` meta_type is left untouched. | ||
| */ | ||
| public function test_leaves_other_meta_type_untouched() { | ||
| $this->activate_parsely(); | ||
| update_option( 'parsely', [ 'meta_type' => 'repeated_metas' ] ); | ||
|
|
||
| Parsely::migrate_meta_type(); | ||
|
|
||
| $settings = get_option( 'parsely' ); | ||
| $this->assertEquals( 'repeated_metas', $settings['meta_type'] ); | ||
| $this->assertNotEmpty( get_option( Parsely::META_TYPE_MIGRATION_OPTION ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Nothing happens — and completion is not recorded — when wp-parsely is inactive, | ||
| * so the migration stays pending until the plugin is activated. | ||
| */ | ||
| public function test_skips_and_stays_pending_when_parsely_inactive() { | ||
| update_option( 'parsely', [ 'meta_type' => 'json_ld' ] ); | ||
|
|
||
| Parsely::migrate_meta_type(); | ||
|
|
||
| $settings = get_option( 'parsely' ); | ||
| $this->assertEquals( 'json_ld', $settings['meta_type'] ); | ||
| $this->assertFalse( get_option( Parsely::META_TYPE_MIGRATION_OPTION ) ); | ||
| } | ||
|
|
||
| /** | ||
| * The migration is idempotent: once recorded it does not run again, even if | ||
| * the stored meta_type is later reset to `json_ld`. | ||
| */ | ||
| public function test_is_idempotent() { | ||
| $this->activate_parsely(); | ||
| update_option( 'parsely', [ 'meta_type' => 'json_ld' ] ); | ||
|
|
||
| Parsely::migrate_meta_type(); | ||
| $this->assertEquals( 'repeated_metas', get_option( 'parsely' )['meta_type'] ); | ||
|
|
||
| // Simulate a later manual change back to json_ld; the migration should not re-run. | ||
| update_option( 'parsely', [ 'meta_type' => 'json_ld' ] ); | ||
| Parsely::migrate_meta_type(); | ||
| $this->assertEquals( 'json_ld', get_option( 'parsely' )['meta_type'] ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.