Skip to content

Commit

Permalink
qa/address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
al-esc committed Jun 6, 2024
1 parent 2cc0466 commit 8c4798f
Show file tree
Hide file tree
Showing 35 changed files with 966 additions and 734 deletions.
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
"pantheon-systems/wpunit-helpers": true
}
},
"autoload": {
"classmap": [
"inc/compatibility/"
]
},
"scripts": {
"chmod-scripts": [
"chmod +x bin/*.sh"
Expand Down
86 changes: 39 additions & 47 deletions inc/compatibility/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,28 @@ abstract class Base {
*/
protected $run_fix_everytime = false;

public function __construct() {
// Register the plugin deactivation hooks.
/**
* Base constructor registering deactivation hook and conditionally executing activate method.
*/
public function __construct( $slug ) {
static::$plugin_slug = $slug;
register_deactivation_hook( WP_PLUGIN_DIR . '/' . static::$plugin_slug, [ $this, 'deactivate' ] );
if ( $this->is_plugin_active() || $this->is_any_plugin_active() ) {
if ( $this->is_plugin_active() ) {
$this->activate();
}
}

/**
* Check if the plugin is active.
* Check if the plugin is active by $plugin_slug parameter.
*
* @return bool
*/
protected function is_plugin_active() {
return ( static::$plugin_slug && is_plugin_active( static::$plugin_slug ) );
}

protected function is_any_plugin_active() {
if ( ! property_exists( $this, 'plugin_slugs' ) ) {
return false;
}

foreach ( static::$plugin_slugs as $plugin_slug ) {
if ( is_plugin_active( $plugin_slug ) ) {
return true;
}
}

return false;
}

/**
* Activate the plugin.
* Set up compatibility hooks and persists compatibility status.
*
* @return void
*/
Expand Down Expand Up @@ -134,12 +123,17 @@ protected function activate() {
$this->persist_data( $plugin_methods );
}

/**
* Trigger compatibility layer on plugin activation.
*
* @return void
*/
protected function run_on_plugin_activation() {
$this->apply_fix();
}

/**
* Apply the fix to the plugin.
* Apply the compatibility fix.
*
* @return mixed
*/
Expand All @@ -159,34 +153,53 @@ protected function add_action_after_plugin_activation() {
}, PHP_INT_MAX);
}

/**
* Trigger compatibility layer after plugin activation.
*
* @return void
*/
protected function run_after_plugin_activation() {
$this->apply_fix();
}

/**
* Trigger compatibility layer on WP Dashboard.
*
* @return void
*/
protected function run_fix_on_dashboard_only() {
$this->apply_fix();
}

/**
* Trigger compatibility layer on WP frontend.
*
* @return void
*/
protected function run_fix_on_frontend_only() {
$this->apply_fix();
}

/**
* Trigger compatibility layer on each request.
*
* @return void
*/
protected function run_fix_everytime() {
$this->apply_fix();
}

/**
* Persist the plugin's data to the database.
* Persist the compatibility layer data to the database.
*
* @return void
*/
protected function persist_data( array $plugin_methods = [] ) {
$pantheon_applied_fixes = get_option( 'pantheon_applied_fixes' );
$pantheon_applied_fixes = get_option( 'pantheon_applied_fixes' ) ?: [];
$old = $pantheon_applied_fixes[ static::$plugin_slug ] ?? [];
$pantheon_applied_fixes[ static::$plugin_slug ] = [
'plugin_slug' => static::$plugin_slug,
'plugin_name' => static::$plugin_name,
'plugin_version' => $this->get_plugin_version(),
'plugin_status' => $plugin_methods ? 'automated' : 'waiting',
'plugin_message' => 'Manual fixes can be safely removed.',
'plugin_class' => static::class,
Expand All @@ -199,17 +212,6 @@ protected function persist_data( array $plugin_methods = [] ) {
}
}

/**
* Get the version of the plugin.
*
* @return mixed
*/
protected function get_plugin_version() {
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . static::$plugin_slug );

return $plugin_data['Version'];
}

/**
* Check if the plugin is installed.
*
Expand All @@ -220,7 +222,7 @@ public function is_plugin_installed() {
}

/**
* Deactivate the plugin.
* Rollback the compatibility layer.
*
* @return void
*/
Expand All @@ -230,14 +232,14 @@ public function deactivate() {
}

/**
* Remove the fix from the plugin.
* Remove the compatibility layer fix.
*
* @return mixed
*/
abstract public function remove_fix();

/**
* Remove the plugin's data from the persisted fixes.
* Remove the compatibility layer's persisted data.
*
* @return void
*/
Expand All @@ -254,14 +256,4 @@ protected function remove_persisted_data() {
update_option( 'pantheon_applied_fixes', $pantheon_applied_fixes );
}
}

/**
* Register the plugin activation hooks.
*
* @return void
*/
protected function register_plugin_activation_hooks() {
register_activation_hook( WP_PLUGIN_DIR . '/' . static::$plugin_slug, [ $this, 'activate' ] );
register_deactivation_hook( WP_PLUGIN_DIR . '/' . static::$plugin_slug, [ $this, 'deactivate' ] );
}
}
17 changes: 8 additions & 9 deletions inc/compatibility/class-acceleratedmobilepages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* Accelerated Mobile Pages compatibility fix.
*
* @link https://docs.pantheon.io/plugins-known-issues#amp-for-wp--accelerated-mobile-pages
* @package Pantheon\Compatibility
*/

Expand All @@ -14,14 +15,6 @@
*/
class AcceleratedMobilePages extends Base {


/**
* The plugin slug.
*
* @var string
*/
public static $plugin_slug = 'accelerated-mobile-pages/accelerated-moblie-pages.php';

/**
* The plugin name.
*
Expand All @@ -30,16 +23,22 @@ class AcceleratedMobilePages extends Base {
public static $plugin_name = 'AMP for WP – Accelerated Mobile Pages';

/**
* Run fix everytime either frontend or dashboard.
* Run fix on every request.
*
* @var bool
*/
protected $run_fix_everytime = true;

/**
* @return void
*/
public function apply_fix() {
AcceleratedMobilePagesFix::apply();
}

/**
* @return void
*/
public function remove_fix() {
AcceleratedMobilePagesFix::remove();
}
Expand Down
17 changes: 9 additions & 8 deletions inc/compatibility/class-auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/**
* Auth0 Compatibility
*
* @package Pantheon
* @link https://docs.pantheon.io/plugins-known-issues#auth0
* @package Pantheon\Compatibility
*/

namespace Pantheon\Compatibility;
Expand All @@ -15,29 +16,29 @@
class Auth0 extends Base {


/**
* Plugin slug
*
* @var string
*/
public static $plugin_slug = 'auth0/WP_Auth0.php';
/**
* The plugin name.
*
* @var string
*/
public static $plugin_name = 'Auth0';
/**
* Run fix everytime either frontend or dashboard.
* Run fix on each request.
*
* @var bool
*/
protected $run_fix_everytime = true;

/**
* @return void
*/
public function apply_fix() {
Auth0Fix::apply();
}

/**
* @return void
*/
public function remove_fix() {
Auth0Fix::remove();
}
Expand Down
17 changes: 9 additions & 8 deletions inc/compatibility/class-autoptimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/**
* Autoptimize Compatibility
*
* @package Pantheon
* @link https://docs.pantheon.io/plugins-known-issues#autoptimize
* @package Pantheon\Compatibility
*/

namespace Pantheon\Compatibility;
Expand All @@ -15,29 +16,29 @@
class Autoptimize extends Base {


/**
* Plugin slug
*
* @var string
*/
public static $plugin_slug = 'autoptimize/autoptimize.php';
/**
* The plugin name.
*
* @var string
*/
public static $plugin_name = 'Autoptimize';
/**
* Run fix everytime either frontend or dashboard.
* Run fix on each request.
*
* @var bool
*/
protected $run_fix_everytime = true;

/**
* @return void
*/
public function apply_fix() {
AutoptimizeFix::apply();
}

/**
* @return void
*/
public function remove_fix() {
AutoptimizeFix::remove();
}
Expand Down
15 changes: 8 additions & 7 deletions inc/compatibility/class-bettersearchreplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* Better Search Replace compatibility fix.
*
* @link https://docs.pantheon.io/plugins-known-issues#better-search-and-replace
* @package Pantheon\Compatibility
*/

Expand All @@ -15,31 +16,31 @@
class BetterSearchReplace extends Base {


/**
* The plugin slug.
*
* @var string
*/
public static $plugin_slug = 'better-search-replace/better-search-replace.php';
/**
* The plugin name.
*
* @var string
*/
public static $plugin_name = 'Better Search Replace';
/**
* Run fix everytime either frontend or dashboard.
* Run fix on each request.
*
* @var bool
*/
protected $run_fix_everytime = true;

/**
* @return void
*/
public function apply_fix() {
AddFilterFix::apply('bsr_capability', function () {
return 'manage_options';
});
}

/**
* @return void
*/
public function remove_fix() {
AddFilterFix::remove('bsr_capability', function () {
return 'manage_options';
Expand Down
Loading

0 comments on commit 8c4798f

Please sign in to comment.