Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions inc/compatibilities/jetpack_photon_compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* Class Optml_jetpack_photon_compatibility.
*
* @reason Register Jetpack as a conflicting plugin when its image Site Accelerator is active.
*/
class Optml_jetpack_photon_compatibility extends Optml_compatibility {
/**
* Jetpack conflict key.
*/
const CONFLICT_KEY = 'jetpack_Photon';

/**
* Jetpack plugin file.
*/
const PLUGIN_FILE = 'jetpack/jetpack.php';

/**
* Check whether Jetpack's image Site Accelerator is active.
*
* @return bool Whether to load the compatibility.
*/
public function should_load() {
return class_exists( '\Jetpack', false ) && \Jetpack::is_module_active( 'photon' );
}

/**
* Register the Jetpack conflict integration.
*
* @return void
*/
public function register() {
add_filter( 'optml_conflicting_defined_plugins', [ $this, 'add_conflicting_plugin' ] );
}

/**
* Add Jetpack to the conflicting plugin definitions.
*
* @param array<string, string> $plugins Conflicting plugin definitions.
* @return array<string, string>
*/
public function add_conflicting_plugin( $plugins ) {
$plugins[ self::CONFLICT_KEY ] = self::PLUGIN_FILE;

return $plugins;
}

/**
* Register before the generic conflict notice is evaluated.
*
* @return bool
*/
public function should_load_early() {
return true;
}
}
3 changes: 2 additions & 1 deletion inc/conflicts/conflicting_plugins.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* The Conflicting Plugins class, documents and displays dashboard notice for conflicting plugins.
*
Expand Down Expand Up @@ -54,7 +55,6 @@ private function defined_plugins() {
'litespeed' => 'litespeed-cache/litespeed-cache.php',
'autoptimize' => 'autoptimize/autoptimize.php',
'perfmatters' => 'perfmatters/perfmatters.php',
'jetpack_Photon' => 'jetpack/jetpack.php',
// 'plugin-slug' => 'plugin-folder/plugin-file.php'
];

Expand All @@ -73,6 +73,7 @@ private function get_active_plugins() {

$conflicting_plugins = $this->defined_plugins();
$conflicting_plugins = array_filter( $conflicting_plugins, 'is_plugin_active' );

return apply_filters( 'optml_conflicting_active_plugins', $conflicting_plugins );
}

Expand Down
10 changes: 2 additions & 8 deletions inc/conflicts/jetpack_photon.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,8 @@ public function define_message() {
* @access public
*/
public function is_conflict_valid() {
$compatibility = new Optml_jetpack_photon_compatibility();

if ( ! is_plugin_active( 'jetpack/jetpack.php' ) ) {
return false;
}
if ( ! class_exists( 'Jetpack', false ) ) {
return false;
}

return Jetpack::is_module_active( 'photon' );
return $compatibility->should_load();
}
}
1 change: 1 addition & 0 deletions inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ final class Optml_Manager {
'wpsp',
'jetengine',
'jetpack',
'jetpack_photon_compatibility',
'wp_rocket',
'wp_super_cache',
'breeze',
Expand Down
101 changes: 101 additions & 0 deletions tests/test-jetpack-conflicts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* Jetpack conflict tests.
*
* @package Optimole-WP
*/

/**
* Class Test_Jetpack_Conflicts.
*/
class Test_Jetpack_Conflicts extends WP_UnitTestCase {
/**
* Active plugins before each test.
*
* @var array
*/
private $active_plugins;

/**
* Jetpack Photon compatibility.
*
* @var Optml_jetpack_photon_compatibility
*/
private $compatibility;

/**
* Set up the test.
*/
public function set_up() {
parent::set_up();

$this->active_plugins = get_option( 'active_plugins', [] );
$this->compatibility = new Optml_jetpack_photon_compatibility();
update_option(
'active_plugins',
array_merge( $this->active_plugins, [ 'jetpack/jetpack.php' ] )
);
}

/**
* Restore active plugins after each test.
*/
public function tear_down() {
remove_filter( 'optml_conflicting_defined_plugins', [ $this->compatibility, 'add_conflicting_plugin' ] );
Jetpack::$photon_active = false;
update_option( 'active_plugins', $this->active_plugins );

parent::tear_down();
}

/**
* Jetpack without Photon should not be a generic conflict.
*/
public function test_jetpack_without_photon_is_not_a_generic_conflict() {
$conflicts = new Optml_Conflicting_Plugins();
$conflict = new Optml_Jetpack_Photon();

$this->assertFalse( $this->compatibility->should_load() );
$this->assertFalse( $conflict->is_conflict_valid() );
$this->assertNotContains( 'jetpack/jetpack.php', $conflicts->get_conflicting_plugins( true ) );
}

/**
* Jetpack with Photon should remain a generic conflict.
*/
public function test_jetpack_with_photon_is_a_generic_conflict() {
Jetpack::$photon_active = true;
$this->compatibility->register();
$conflicts = new Optml_Conflicting_Plugins();
$conflict = new Optml_Jetpack_Photon();

$this->assertTrue( $this->compatibility->should_load() );
$this->assertTrue( $conflict->is_conflict_valid() );
$this->assertContains( 'jetpack/jetpack.php', $conflicts->get_conflicting_plugins( true ) );
}
}

if ( ! class_exists( 'Jetpack', false ) ) {
/**
* Minimal Jetpack test double.
*/
class Jetpack {
/**
* Whether Photon is active.
*
* @var bool
*/
public static $photon_active = false;

/**
* Check whether a module is active.
*
* @param string $module Module slug.
* @return bool
*/
public static function is_module_active( $module ) {
return 'photon' === $module && self::$photon_active;
}
}
}
Loading