Skip to content

Commit

Permalink
Simplify Parse.ly loader (#5863)
Browse files Browse the repository at this point in the history
Remove the ability to load by option and fix the reporting discrepancy where versions would be reported as `UNKNOWN`

Improve the code readability by switching to `switch` rather than multiple `if/else/elseif` statements.

---------

Co-authored-by: Rebecca Hum <[email protected]>
  • Loading branch information
rinatkhaziev and rebeccahum authored Sep 25, 2024
1 parent 540f97e commit 7a0539d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 176 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/parsely.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,10 @@ jobs:
# Oldest version of the parsely plugin
- { wp: latest, parsely: '3.5', mode: 'filter_enabled', php: '8.1' }
- { wp: latest, parsely: '3.5', mode: 'filter_disabled', php: '8.1' }
- { wp: latest, parsely: '3.5', mode: 'option_enabled', php: '8.1' }
- { wp: latest, parsely: '3.5', mode: 'option_disabled', php: '8.1' }
- { wp: latest, parsely: '3.5', mode: 'filter_and_option_enabled', php: '8.1' }
- { wp: latest, parsely: '3.5', mode: 'filter_and_option_disabled', php: '8.1' }

# Latest version of the parsely plugin
- { wp: latest, mode: 'filter_enabled', php: '8.1' }
- { wp: latest, mode: 'filter_disabled', php: '8.1' }
- { wp: latest, mode: 'option_enabled', php: '8.1' }
- { wp: latest, mode: 'option_disabled', php: '8.1' }
- { wp: latest, mode: 'filter_and_option_enabled', php: '8.1' }
- { wp: latest, mode: 'filter_and_option_disabled', php: '8.1' }
services:
mysql:
image: mysql:8
Expand Down
57 changes: 0 additions & 57 deletions tests/parsely/test-mu-parsely-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,6 @@ public function test_bootstrap_modes_enabled_without_constant() {
);
$this->assertEquals( Parsely_Integration_Type::DISABLED_MUPLUGINS_FILTER, Parsely_Loader_Info::get_integration_type() );
break;
case 'option_enabled':
$this->assertFalse( has_filter( 'wpvip_parsely_load_mu' ) );
$this->assertSame( '1', get_option( '_wpvip_parsely_mu' ) );
$this->assertTrue(
Parsely_Loader_Info::is_active(),
'Expecting wp-parsely plugin to be enabled by the option.'
);
$this->assertEquals( Parsely_Integration_Type::ENABLED_MUPLUGINS_SILENT_OPTION, Parsely_Loader_Info::get_integration_type() );
break;
case 'option_disabled':
$this->assertFalse( has_filter( 'wpvip_parsely_load_mu' ) );
$this->assertSame( '0', get_option( '_wpvip_parsely_mu' ) );
$this->assertFalse(
Parsely_Loader_Info::is_active(),
'Expecting wp-parsely plugin to be disabled by the option.'
);
$this->assertEquals( Parsely_Integration_Type::DISABLED_MUPLUGINS_SILENT_OPTION, Parsely_Loader_Info::get_integration_type() );
break;
case 'filter_and_option_enabled':
$this->assertTrue( has_filter( 'wpvip_parsely_load_mu' ) );
$this->assertSame( '1', get_option( '_wpvip_parsely_mu' ) );
Expand Down Expand Up @@ -238,22 +220,6 @@ public function test_bootstrap_modes_disabled_via_constant() {
$this->assertTrue( has_filter( 'wpvip_parsely_load_mu' ) );
$this->assertFalse( get_option( '_wpvip_parsely_mu' ) );
break;
case 'option_enabled':
$this->assertFalse( has_filter( 'wpvip_parsely_load_mu' ) );
$this->assertSame( '1', get_option( '_wpvip_parsely_mu' ) );
break;
case 'option_disabled':
$this->assertFalse( has_filter( 'wpvip_parsely_load_mu' ) );
$this->assertSame( '0', get_option( '_wpvip_parsely_mu' ) );
break;
case 'filter_and_option_enabled':
$this->assertTrue( has_filter( 'wpvip_parsely_load_mu' ) );
$this->assertSame( '1', get_option( '_wpvip_parsely_mu' ) );
break;
case 'filter_and_option_disabled':
$this->assertTrue( has_filter( 'wpvip_parsely_load_mu' ) );
$this->assertSame( '0', get_option( '_wpvip_parsely_mu' ) );
break;
default:
$this->fail( 'Invalid test mode specified: ' . self::$test_mode );
}
Expand All @@ -262,29 +228,6 @@ public function test_bootstrap_modes_disabled_via_constant() {
$this->assertEquals( Parsely_Integration_Type::DISABLED_CONSTANT, Parsely_Loader_Info::get_integration_type() );
}

public function test_parsely_ui_hooks() {
maybe_load_plugin();

$this->assertFalse( has_action( 'option_parsely', __NAMESPACE__ . '\alter_option_use_repeated_metas' ) );

if ( is_parsely_disabled() ) {
return;
}

\Parsely\parsely_initialize_plugin();
maybe_disable_some_features();

$repeated_metas_expected = 'option_enabled' === self::$test_mode ? 10 : false;
$this->assertSame( $repeated_metas_expected, has_action( 'option_parsely', __NAMESPACE__ . '\alter_option_use_repeated_metas' ) );

$row_actions = new Row_Actions( $GLOBALS['parsely'] );
$row_actions->run();

$row_actions_expected = in_array( self::$test_mode, [ 'filter_enabled', 'filter_and_option_enabled' ] ) ? 10 : false;
$this->assertSame( $row_actions_expected, has_filter( 'page_row_actions', array( $row_actions, 'row_actions_add_parsely_link' ) ) );
$this->assertSame( $row_actions_expected, has_filter( 'post_row_actions', array( $row_actions, 'row_actions_add_parsely_link' ) ) );
}

public function test_default_parsely_configs() {
maybe_load_plugin();

Expand Down
164 changes: 53 additions & 111 deletions wp-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace Automattic\VIP\WP_Parsely_Integration;

use Parsely\Parsely;

/**
* The default version is the first entry in the SUPPORTED_VERSIONS list.
*/
Expand Down Expand Up @@ -174,14 +176,14 @@ public static function get_parsely_options(): array {
}

/**
* Parse.ly options.
* Parse.ly options, plugin may be not loaded at this moment in the runtime, but we want to check the options anyway.
*
* @var array
*/
$parsely_options = array();

if ( isset( $GLOBALS['parsely'] ) && is_a( $GLOBALS['parsely'], 'Parsely\Parsely' ) ) {
$parsely_options = $GLOBALS['parsely']->get_options();
} else {
$parsely_options = get_option( 'parsely', [] );
}

return $parsely_options;
Expand Down Expand Up @@ -240,7 +242,6 @@ function is_queued_for_activation() {
* To enable it on your site, add this line:
* add_filter( 'wpvip_parsely_load_mu', '__return_true' );
*
* We enable it for some sites via the `_wpvip_parsely_mu` blog option.
* To prevent it from loading even when this condition is met, add this line:
* add_filter( 'wpvip_parsely_load_mu', '__return_false' );
*/
Expand All @@ -250,61 +251,46 @@ function maybe_load_plugin() {
return;
}

// Self-managed integration: The plugin exists on the site and is being loaded already.
$plugin_class_exists = class_exists( 'Parsely' ) || class_exists( 'Parsely\Parsely' );
if ( $plugin_class_exists ) {
Parsely_Loader_Info::set_active( true );
Parsely_Loader_Info::set_integration_type( Parsely_Integration_Type::SELF_MANAGED );

$parsely_options = Parsely_Loader_Info::get_parsely_options();
if ( array_key_exists( 'plugin_version', $parsely_options ) ) {
Parsely_Loader_Info::set_version( $parsely_options['plugin_version'] );
}

return;
}

$parsely_enabled_constant = null; // Represents that the site doesn't have parsely enabled / blocked.

if ( defined( 'VIP_PARSELY_ENABLED' ) ) {
$parsely_enabled_constant = constant( 'VIP_PARSELY_ENABLED' );

// Opt out if constant value isn't true.
if ( true !== $parsely_enabled_constant ) {
Parsely_Loader_Info::set_active( false );
Parsely_Loader_Info::set_integration_type( Parsely_Integration_Type::DISABLED_CONSTANT );

return;
}

Parsely_Loader_Info::set_active( true );
Parsely_Loader_Info::set_integration_type( Parsely_Integration_Type::ENABLED_CONSTANT );
}

$option_load_status = get_option( '_wpvip_parsely_mu', null );
$filtered_load_status = apply_filters( 'wpvip_parsely_load_mu', null );

// If plugin isn't enabled via constant then check for filter and option status.
if ( true !== $parsely_enabled_constant ) {
$should_load = true === $filtered_load_status || '1' === $option_load_status;
$should_prevent_loading = false === $filtered_load_status || '0' === $option_load_status;

// No integration: The site has not enabled parsely.
if ( ! $should_load || $should_prevent_loading ) {
Parsely_Loader_Info::set_active( false );

if ( false === $filtered_load_status ) {
Parsely_Loader_Info::set_integration_type( Parsely_Integration_Type::DISABLED_MUPLUGINS_FILTER );
} elseif ( '0' === $option_load_status ) {
Parsely_Loader_Info::set_integration_type( Parsely_Integration_Type::DISABLED_MUPLUGINS_SILENT_OPTION );
switch ( true ) {
// Self-managed
case class_exists( 'Parsely' ) || class_exists( 'Parsely\Parsely' ):
Parsely_Loader_Info::set_active( true );
Parsely_Loader_Info::set_integration_type( Parsely_Integration_Type::SELF_MANAGED );
$parsely_options = Parsely_Loader_Info::get_parsely_options();
if ( array_key_exists( 'plugin_version', $parsely_options ) ) {
Parsely_Loader_Info::set_version( $parsely_options['plugin_version'] );
}
break;
// Integrations-managed
case defined( 'VIP_PARSELY_ENABLED' ):
Parsely_Loader_Info::set_active( true === constant( 'VIP_PARSELY_ENABLED' ) );
Parsely_Loader_Info::set_integration_type(
Parsely_Loader_Info::is_active()
? Parsely_Integration_Type::ENABLED_CONSTANT
: Parsely_Integration_Type::DISABLED_CONSTANT
);
break;
// Filter-managed - enabled
case $filtered_load_status:
Parsely_Loader_Info::set_active( true );
Parsely_Loader_Info::set_integration_type( Parsely_Integration_Type::ENABLED_MUPLUGINS_FILTER );
break;
// Filter-managed - disabled
case false === $filtered_load_status:
Parsely_Loader_Info::set_active( false );
Parsely_Loader_Info::set_integration_type( Parsely_Integration_Type::DISABLED_MUPLUGINS_FILTER );
break;
// Not configured in any way
default:
Parsely_Loader_Info::set_active( false );
Parsely_Loader_Info::set_integration_type( Parsely_Integration_Type::NONE );
break;
}

return;
}

// Enqueuing the disabling of Parse.ly features when the plugin is loaded (after the `plugins_loaded` hook)
// We need priority 0, so it's executed before `widgets_init`.
add_action( 'init', __NAMESPACE__ . '\maybe_disable_some_features', 0 );
if ( ! Parsely_Loader_Info::is_active() || Parsely_Integration_Type::SELF_MANAGED === Parsely_Loader_Info::get_integration_type() ) {
return;
}

$versions_to_try = SUPPORTED_VERSIONS;
Expand Down Expand Up @@ -344,23 +330,16 @@ function maybe_load_plugin() {

// Require the actual wp-parsely plugin.
if ( ! is_readable( $entry_file ) ) {
Parsely_Loader_Info::set_active( false );
return;
}
require_once $entry_file;

// If plugin isn't enabled via constant then set filter or option integration_type.
if ( true !== $parsely_enabled_constant ) {
$integration_type = Parsely_Integration_Type::ENABLED_MUPLUGINS_FILTER;
if ( '1' === $option_load_status && true !== $filtered_load_status ) {
$integration_type = Parsely_Integration_Type::ENABLED_MUPLUGINS_SILENT_OPTION;
}
require_once $entry_file;

Parsely_Loader_Info::set_integration_type( $integration_type );
if ( defined( '\Parsely\PARSELY_VERSION' ) ) {
Parsely_Loader_Info::set_version( constant( '\Parsely\PARSELY_VERSION' ) );
}

Parsely_Loader_Info::set_active( true );
Parsely_Loader_Info::set_version( $version );

// Require VIP's customizations over wp-parsely.
$vip_parsely_plugin = __DIR__ . '/vip-parsely/vip-parsely.php';
if ( is_readable( $vip_parsely_plugin ) ) {
Expand All @@ -369,54 +348,17 @@ function maybe_load_plugin() {
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\maybe_load_plugin', 1 );

/**
* Hides the UI if the plugin is loaded via silent option.
*/
function maybe_disable_some_features() {
if ( ! isset( $GLOBALS['parsely'] ) || ! is_a( $GLOBALS['parsely'], 'Parsely\Parsely' ) ) {
return;
}

$filtered_load_status = apply_filters( 'wpvip_parsely_load_mu', null );
$should_disable_features = apply_filters( 'wpvip_parsely_hide_ui_for_mu', true !== $filtered_load_status );

// If the plugin was not loaded via the filter, hide the UI by default.
if ( $should_disable_features ) {
remove_action( 'init', 'Parsely\parsely_wp_admin_early_register' );
remove_action( 'init', 'Parsely\init_recommendations_block' );
remove_action( 'enqueue_block_editor_assets', 'Parsely\init_content_helper' );
remove_action( 'admin_init', 'Parsely\parsely_admin_init_register' );
remove_action( 'widgets_init', 'Parsely\parsely_recommended_widget_register' );

// Don't show the row action links.
add_filter( 'wp_parsely_enable_row_action_links', '__return_false' );
add_filter( 'wp_parsely_enable_rest_api_support', '__return_false' );
add_filter( 'wp_parsely_enable_related_api_proxy', '__return_false' );

// Default to "repeated metas".
add_filter( 'option_parsely', __NAMESPACE__ . '\alter_option_use_repeated_metas' );

// Remove the Parse.ly Recommended Widget.
unregister_widget( 'Parsely_Recommended_Widget' );
}
}

/**
* Enum which represent all options to integrate `wp-parsely`.
*/
abstract class Parsely_Integration_Type { // phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound, Generic.Classes.OpeningBraceSameLine.ContentAfterBrace
// When parsely is active.
const ENABLED_MUPLUGINS_FILTER = 'ENABLED_MUPLUGINS_FILTER';
const ENABLED_MUPLUGINS_SILENT_OPTION = 'ENABLED_MUPLUGINS_SILENT_OPTION';
const ENABLED_CONSTANT = 'ENABLED_CONSTANT';

const SELF_MANAGED = 'SELF_MANAGED';

// When parsely is not active.
const DISABLED_MUPLUGINS_FILTER = 'DISABLED_MUPLUGINS_FILTER';
const DISABLED_MUPLUGINS_SILENT_OPTION = 'DISABLED_MUPLUGINS_SILENT_OPTION';
const DISABLED_CONSTANT = 'DISABLED_CONSTANT'; // Prevent loading of plugin based on integration meta attribute or customers can also define it.

// When Parse.ly is active.
const ENABLED_MUPLUGINS_FILTER = 'ENABLED_MUPLUGINS_FILTER';
const ENABLED_CONSTANT = 'ENABLED_CONSTANT';
const SELF_MANAGED = 'SELF_MANAGED';
// When Parse.ly is not active.
const DISABLED_MUPLUGINS_FILTER = 'DISABLED_MUPLUGINS_FILTER';
const DISABLED_CONSTANT = 'DISABLED_CONSTANT'; // Prevent loading of plugin based on integration meta attribute or customers can also define it.
// When Parse.ly is not configured in any way.
const NONE = 'NONE';
const NULL = 'NULL';
}

0 comments on commit 7a0539d

Please sign in to comment.