Skip to content

Commit

Permalink
Closes #6367 Add theme resolver for compatibility classes (#6390)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabrisrp authored and wordpressfan committed Mar 15, 2024
1 parent 3e4a773 commit b3dd96e
Show file tree
Hide file tree
Showing 37 changed files with 567 additions and 659 deletions.
13 changes: 2 additions & 11 deletions inc/ThirdParty/Themes/Avada.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
namespace WP_Rocket\ThirdParty\Themes;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;

/**
* Compatibility class for Avada theme
*/
class Avada extends ThirdpartyTheme {
/**
* Theme name
*
* @var string
*/
protected static $theme_name = 'avada';

class Avada implements Subscriber_Interface {
/**
* Options instance
*
Expand All @@ -29,9 +23,6 @@ class Avada extends ThirdpartyTheme {
* @return array
*/
public static function get_subscribed_events() {
if ( ! self::is_current_theme() ) {
return [];
}
return [
'avada_clear_dynamic_css_cache' => 'clean_domain',
'rocket_exclude_defer_js' => 'exclude_defer_js',
Expand Down
14 changes: 2 additions & 12 deletions inc/ThirdParty/Themes/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
namespace WP_Rocket\ThirdParty\Themes;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\ThirdParty\ReturnTypesTrait;

class Bridge extends ThirdpartyTheme {
class Bridge implements Subscriber_Interface {
use ReturnTypesTrait;

/**
* Theme name
*
* @var string
*/
protected static $theme_name = 'bridge';

/**
* Options instance
*
Expand All @@ -29,10 +23,6 @@ class Bridge extends ThirdpartyTheme {
* @return array
*/
public static function get_subscribed_events() {
if ( ! self::is_current_theme() ) {
return [];
}

return [
'rocket_lazyload_background_images' => 'return_false',
'update_option_qode_options_proya' => [ 'maybe_clear_cache', 10, 2 ],
Expand Down
28 changes: 4 additions & 24 deletions inc/ThirdParty/Themes/Divi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@
use WP_Rocket\Admin\{Options, Options_Data};
use WP_Rocket\Engine\Optimization\DelayJS\HTML;
use WP_Rocket\Engine\Optimization\RUCSS\Controller\UsedCSS;
use WP_Rocket\Event_Management\Subscriber_Interface;

class Divi extends ThirdpartyTheme {
/**
* Theme name
*
* @var string
*/
protected static $theme_name = 'divi';

class Divi implements Subscriber_Interface {
/**
* Options API instance.
*
Expand Down Expand Up @@ -64,13 +58,10 @@ public function __construct( Options $options_api, Options_Data $options, HTML $
*/
public static function get_subscribed_events() {
$events = [
'switch_theme' => [ 'maybe_disable_youtube_preview', PHP_INT_MAX, 2 ],
'after_switch_theme' => [ 'maybe_disable_youtube_preview', PHP_INT_MAX ],
'rocket_specify_dimension_images' => 'disable_image_dimensions_height_percentage',
];

if ( ! self::is_current_theme() ) {
return $events;
}
$events['rocket_exclude_js'] = 'exclude_js';
$events['rocket_maybe_disable_youtube_lazyload_helper'] = 'add_divi_to_description';

Expand Down Expand Up @@ -116,16 +107,9 @@ public function exclude_js( $excluded_js ) {
*
* @since 3.6.3
*
* @param string $name Name of the new theme.
* @param WP_Theme $theme instance of the new theme.
*
* @return void
*/
public function maybe_disable_youtube_preview( $name, $theme ) {
if ( ! self::is_current_theme( $theme ) ) {
return;
}

public function maybe_disable_youtube_preview() {
$this->options->set( 'lazyload_youtube', 0 );
$this->options_api->set( 'settings', $this->options->get_options() );
}
Expand All @@ -140,10 +124,6 @@ public function maybe_disable_youtube_preview( $name, $theme ) {
* @return array
*/
public function add_divi_to_description( $disable_youtube_lazyload ) {
if ( ! self::is_current_theme() ) {
return $disable_youtube_lazyload;
}

$disable_youtube_lazyload[] = 'Divi';

return $disable_youtube_lazyload;
Expand Down
17 changes: 5 additions & 12 deletions inc/ThirdParty/Themes/Flatsome.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@

namespace WP_Rocket\ThirdParty\Themes;

class Flatsome extends ThirdpartyTheme {
/**
* Theme name
*
* @var string
*/
protected static $theme_name = 'flatsome';
use WP_Rocket\Event_Management\Subscriber_Interface;

class Flatsome implements Subscriber_Interface {
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
if ( ! self::is_current_theme() ) {
return [];
}

return [ 'rocket_rucss_inline_content_exclusions' => 'preserve_patterns' ];
return [
'rocket_rucss_inline_content_exclusions' => 'preserve_patterns',
];
}

/**
Expand Down
12 changes: 2 additions & 10 deletions inc/ThirdParty/Themes/Jevelin.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
<?php
namespace WP_Rocket\ThirdParty\Themes;

class Jevelin extends ThirdpartyTheme {
/**
* Theme name
*
* @var string
*/
protected static $theme_name = 'jevelin';
use WP_Rocket\Event_Management\Subscriber_Interface;

class Jevelin implements Subscriber_Interface {
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
if ( ! self::is_current_theme() ) {
return [];
}
return [
'rocket_rucss_inline_content_exclusions' => 'preserve_patterns',
];
Expand Down
13 changes: 2 additions & 11 deletions inc/ThirdParty/Themes/MinimalistBlogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@

namespace WP_Rocket\ThirdParty\Themes;

class MinimalistBlogger extends ThirdpartyTheme {
/**
* Theme name
*
* @var string
*/
protected static $theme_name = 'minimalistblogger';
use WP_Rocket\Event_Management\Subscriber_Interface;

class MinimalistBlogger implements Subscriber_Interface {
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
if ( ! self::is_current_theme() ) {
return [];
}

return [
'rocket_delay_js_exclusions' => 'exclude_jquery_from_delay_js',
];
Expand Down
23 changes: 2 additions & 21 deletions inc/ThirdParty/Themes/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@

namespace WP_Rocket\ThirdParty\Themes;

class Polygon extends ThirdpartyTheme {
/**
* Theme name
*
* @var string
*/
protected static $theme_name = 'polygon';
use WP_Rocket\Event_Management\Subscriber_Interface;

class Polygon implements Subscriber_Interface {
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
if ( ! self::is_current_theme() ) {
return [];
}

return [
'rocket_rucss_inline_content_exclusions' => 'add_rucss_content_excluded',
];
Expand All @@ -35,14 +26,4 @@ public function add_rucss_content_excluded( $excluded ) {
$excluded [] = '.expanding_bar_';
return $excluded;
}

/**
* Checks if the current theme (or parent) is Polygone
*
* @param WP_Theme $theme Instance of the theme.
*/
private static function is_polygon( $theme = null ) {
$theme = $theme instanceof WP_Theme ? $theme : wp_get_theme();
return ( 'polygon' === strtolower( $theme->get( 'Name' ) ) || 'polygon' === $theme->get_template() );
}
}
85 changes: 33 additions & 52 deletions inc/ThirdParty/Themes/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace WP_Rocket\ThirdParty\Themes;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\{AbstractServiceProvider, BootableServiceProviderInterface};

class ServiceProvider extends AbstractServiceProvider {
class ServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
Expand All @@ -15,65 +15,46 @@ class ServiceProvider extends AbstractServiceProvider {
*
* @var array
*/
protected $provides = [
'avada_subscriber',
'bridge_subscriber',
'divi',
'flatsome',
'jevelin',
'minimalist_blogger',
'polygon',
'uncode',
'xstore',
'themify',
'shoptimizer',
];
protected $provides = [];

/**
* Register the service in the provider array
*
* @return void
*/
public function boot() {
$theme = ThemeResolver::get_current_theme();

if ( ! empty( $theme ) ) {
$this->provides[] = $theme;
}
}

/**
* Registers the subscribers in the container
*
* @return void
*/
public function register() {
$options = $this->getContainer()->get( 'options' );
$theme = ThemeResolver::get_current_theme();

if ( ! empty( $theme ) ) {
$factory = new SubscriberFactory();

$theme_data = $factory->get_subscriber();
$arguments = [];

$this->getContainer()
->share( 'avada_subscriber', Avada::class )
->addArgument( $options )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'bridge_subscriber', Bridge::class )
->addArgument( $options )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'divi', Divi::class )
->addArgument( $this->getContainer()->get( 'options_api' ) )
->addArgument( $options )
->addArgument( $this->getContainer()->get( 'delay_js_html' ) )
->addArgument( $this->getContainer()->get( 'rucss_used_css_controller' ) )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'flatsome', Flatsome::class )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'jevelin', Jevelin::class )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'minimalist_blogger', MinimalistBlogger::class )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'polygon', Polygon::class )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'uncode', Uncode::class )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'xstore', Xstore::class )
->addTag( 'common_subscriber' );
if ( empty( $theme_data ) ) {
return;
}

$this->getContainer()->share( 'themify', Themify::class )
->addArgument( $options );
foreach ( $theme_data['arguments'] as $arg ) {
$arguments[] = $this->getContainer()->get( $arg );
}

$this->getContainer()->share( 'shoptimizer', Shoptimizer::class );
$this->getContainer()
->share( $theme, $theme_data['class'] )
->addArguments( $arguments );
}
}
}

0 comments on commit b3dd96e

Please sign in to comment.