Initial commit xmarticaims
This commit is contained in:
+140
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists;
|
||||
|
||||
use WP_Error;
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
|
||||
abstract class AbstractAPIClient {
|
||||
/**
|
||||
* API URL.
|
||||
*/
|
||||
const API_URL = 'https://b.rucss.wp-rocket.me/api/';
|
||||
|
||||
/**
|
||||
* Response Code.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $response_code = 200;
|
||||
|
||||
/**
|
||||
* Error message.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $error_message = '';
|
||||
|
||||
/**
|
||||
* Response Body.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $response_body = '';
|
||||
|
||||
/**
|
||||
* Plugin options instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Instantiate the class.
|
||||
*
|
||||
* @param Options_Data $options Options instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options ) {
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify API endpoint path.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function get_api_path();
|
||||
|
||||
/**
|
||||
* Get exclusions list.
|
||||
*
|
||||
* @param string $hash Hash of lists content to compare.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_exclusions_list( $hash ) {
|
||||
$args = [
|
||||
'body' => [
|
||||
'hash' => $hash,
|
||||
],
|
||||
'timeout' => 5,
|
||||
];
|
||||
|
||||
if ( ! $this->handle_request( $args ) ) {
|
||||
return [
|
||||
'code' => $this->response_code,
|
||||
'message' => $this->error_message,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => $this->response_code,
|
||||
'body' => $this->response_body,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the request.
|
||||
*
|
||||
* @param array $args Passed arguments.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function handle_request( array $args ) {
|
||||
$api_url = rocket_get_constant( 'WP_ROCKET_EXCLUSIONS_API_URL', false )
|
||||
? rocket_get_constant( 'WP_ROCKET_EXCLUSIONS_API_URL', false )
|
||||
: self::API_URL;
|
||||
|
||||
if ( empty( $args['body'] ) ) {
|
||||
$args['body'] = [];
|
||||
}
|
||||
|
||||
$args['body']['credentials'] = [
|
||||
'wpr_email' => $this->options->get( 'consumer_email', '' ),
|
||||
'wpr_key' => $this->options->get( 'consumer_key', '' ),
|
||||
];
|
||||
|
||||
$response = wp_remote_get(
|
||||
$api_url . $this->get_api_path(),
|
||||
$args
|
||||
);
|
||||
|
||||
return $this->check_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SaaS request error.
|
||||
*
|
||||
* @param array|WP_Error $response WP Remote request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function check_response( $response ): bool {
|
||||
$this->response_code = is_array( $response )
|
||||
? wp_remote_retrieve_response_code( $response )
|
||||
: $response->get_error_code();
|
||||
|
||||
if ( 200 !== $this->response_code && 206 !== $this->response_code ) {
|
||||
$this->error_message = is_array( $response )
|
||||
? wp_remote_retrieve_response_message( $response )
|
||||
: $response->get_error_message();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->response_body = wp_remote_retrieve_body( $response );
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists;
|
||||
|
||||
use WP_Filesystem_Direct;
|
||||
use StdClass;
|
||||
|
||||
abstract class AbstractDataManager {
|
||||
/**
|
||||
* Filesystem instance
|
||||
*
|
||||
* @var WP_Filesystem_Direct
|
||||
*/
|
||||
private $filesystem;
|
||||
|
||||
/**
|
||||
* Cache ttl.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $cache_duration = WEEK_IN_SECONDS;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
|
||||
*/
|
||||
public function __construct( $filesystem = null ) {
|
||||
$this->filesystem = is_null( $filesystem ) ? rocket_direct_filesystem() : $filesystem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache transient name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function get_cache_transient_name();
|
||||
|
||||
/**
|
||||
* Get lists json filename.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function get_json_filename();
|
||||
|
||||
/**
|
||||
* Gets the lists content
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function get_lists() {
|
||||
$transient = get_transient( $this->get_cache_transient_name() );
|
||||
|
||||
if ( false !== $transient ) {
|
||||
return $transient;
|
||||
}
|
||||
|
||||
$json = $this->get_lists_from_file();
|
||||
|
||||
$lists = json_decode( $json );
|
||||
|
||||
if ( empty( $lists ) ) {
|
||||
return new StdClass();
|
||||
}
|
||||
|
||||
$this->set_lists_cache( $lists );
|
||||
|
||||
return $lists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hash of the current JSON
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_lists_hash() {
|
||||
return md5( $this->get_lists_from_file() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save dynamic lists on file & transient
|
||||
*
|
||||
* @param string $content Lists content.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function save_dynamic_lists( string $content ) {
|
||||
$result = $this->put_lists_to_file( $content );
|
||||
|
||||
$lists = json_decode( $content );
|
||||
|
||||
$this->set_lists_cache( $lists );
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the path to the dynamic lists JSON file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_json_filepath(): string {
|
||||
return rocket_get_constant( 'WP_ROCKET_CONFIG_PATH', '' ) . $this->get_json_filename() . '.json';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets lists JSON content from file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_lists_from_file(): string {
|
||||
$content = '';
|
||||
$lists_filepath = $this->get_json_filepath();
|
||||
|
||||
if ( $this->filesystem->exists( $lists_filepath ) ) {
|
||||
$content = $this->filesystem->get_contents( $lists_filepath );
|
||||
}
|
||||
|
||||
if ( ! empty( $content ) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$fallback_filepath = rocket_get_constant( 'WP_ROCKET_PATH', '' ) . $this->get_json_filename() . '.json';
|
||||
|
||||
if ( $this->filesystem->exists( $fallback_filepath ) ) {
|
||||
$content = $this->filesystem->get_contents( $fallback_filepath );
|
||||
}
|
||||
|
||||
if ( ! empty( $content ) ) {
|
||||
$this->put_lists_to_file( $content );
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write lists content to JSON file
|
||||
*
|
||||
* @param string $content JSON content.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function put_lists_to_file( string $content ): bool {
|
||||
return $this->filesystem->put_contents( $this->get_json_filepath(), $content, rocket_get_filesystem_perms( 'file' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets transient for lists content
|
||||
*
|
||||
* @param object $content Lists content.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function set_lists_cache( $content ) {
|
||||
set_transient( $this->get_cache_transient_name(), $content, $this->cache_duration );
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists\DefaultLists;
|
||||
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\AbstractAPIClient;
|
||||
|
||||
class APIClient extends AbstractAPIClient {
|
||||
|
||||
/**
|
||||
* Specify API endpoint path.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_api_path() {
|
||||
return 'exclusions/list';
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists\DefaultLists;
|
||||
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\AbstractDataManager;
|
||||
|
||||
class DataManager extends AbstractDataManager {
|
||||
|
||||
/**
|
||||
* Get cache transient name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_cache_transient_name() {
|
||||
return 'wpr_dynamic_lists';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get lists json filename.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_json_filename() {
|
||||
return 'dynamic-lists';
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists\DelayJSLists;
|
||||
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\AbstractAPIClient;
|
||||
|
||||
class APIClient extends AbstractAPIClient {
|
||||
|
||||
/**
|
||||
* Specify API endpoint path.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_api_path() {
|
||||
return 'delay-js-exclusions/list';
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists\DelayJSLists;
|
||||
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\AbstractDataManager;
|
||||
|
||||
class DataManager extends AbstractDataManager {
|
||||
|
||||
/**
|
||||
* Get cache transient name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_cache_transient_name() {
|
||||
return 'wpr_dynamic_lists_delayjs';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get lists json filename.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_json_filename() {
|
||||
return 'dynamic-lists-delayjs';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists;
|
||||
|
||||
use WP_Rocket\Abstract_Render;
|
||||
use WP_Rocket\Engine\Admin\Beacon\Beacon;
|
||||
use WP_Rocket\Engine\License\API\User;
|
||||
use WP_REST_Response;
|
||||
use WP_Error;
|
||||
|
||||
class DynamicLists extends Abstract_Render {
|
||||
|
||||
/**
|
||||
* Providers array.
|
||||
* Array of objects with keys: api_client and data_manager.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $providers;
|
||||
|
||||
/**
|
||||
* User instance
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* Beacon instance
|
||||
*
|
||||
* @var Beacon
|
||||
*/
|
||||
private $beacon;
|
||||
|
||||
/**
|
||||
* Route Rest API namespace.
|
||||
*/
|
||||
const ROUTE_NAMESPACE = 'wp-rocket/v1';
|
||||
|
||||
/**
|
||||
* Instantiate the class.
|
||||
*
|
||||
* @param array $providers Lists providers.
|
||||
* @param User $user User instance.
|
||||
* @param string $template_path Path to views.
|
||||
* @param Beacon $beacon Beacon instance.
|
||||
*/
|
||||
public function __construct( array $providers, User $user, $template_path, Beacon $beacon ) {
|
||||
parent::__construct( $template_path );
|
||||
|
||||
$this->providers = $providers;
|
||||
$this->user = $user;
|
||||
$this->beacon = $beacon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the dynamic lists update route
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_rest_route() {
|
||||
register_rest_route(
|
||||
self::ROUTE_NAMESPACE,
|
||||
'dynamic_lists/update',
|
||||
[
|
||||
'methods' => 'PUT',
|
||||
'callback' => [ $this, 'rest_update_response' ],
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
]
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Checks user's permissions. This is a callback registered to REST route's "permission_callback" parameter.
|
||||
*
|
||||
* @return bool true if the user has permission; else false.
|
||||
*/
|
||||
public function check_permissions() {
|
||||
return current_user_can( 'rocket_manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the update response
|
||||
*
|
||||
* @return WP_REST_Response|WP_Error
|
||||
*/
|
||||
public function rest_update_response() {
|
||||
return rest_ensure_response( $this->update_lists_from_remote() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the lists from remote
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function update_lists_from_remote() {
|
||||
if ( $this->user->is_license_expired() ) {
|
||||
return [
|
||||
'success' => false,
|
||||
'data' => '',
|
||||
'message' => __( 'You need an active license to get the latest version of the lists from our server.', 'rocket' ),
|
||||
];
|
||||
}
|
||||
|
||||
$response = [];
|
||||
$success = false;
|
||||
$should_purge = false;
|
||||
|
||||
foreach ( $this->providers as $provider ) {
|
||||
$result = $provider->api_client->get_exclusions_list( $provider->data_manager->get_lists_hash() );
|
||||
|
||||
if ( empty( $result['code'] ) || empty( $result['body'] ) ) {
|
||||
$response[ $provider->title ] = [
|
||||
'success' => false,
|
||||
'data' => '',
|
||||
'message' => __( 'Could not get updated lists from server.', 'rocket' ),
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 206 === $result['code'] ) {
|
||||
$response[ $provider->title ] = [
|
||||
'success' => true,
|
||||
'data' => '',
|
||||
'message' => __( 'Lists are up to date.', 'rocket' ),
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! $provider->data_manager->save_dynamic_lists( $result['body'] ) ) {
|
||||
$response[ $provider->title ] = [
|
||||
'success' => false,
|
||||
'data' => '',
|
||||
'message' => __( 'Could not update lists.', 'rocket' ),
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
$success = true;
|
||||
$response[ $provider->title ] = [
|
||||
'success' => true,
|
||||
'data' => '',
|
||||
'message' => __( 'Lists are successfully updated.', 'rocket' ),
|
||||
];
|
||||
|
||||
$should_purge |= $provider->clear_cache ?? true;
|
||||
}
|
||||
|
||||
if ( $success ) {
|
||||
/**
|
||||
* Fires after saving all dynamic lists files.
|
||||
*
|
||||
* @since 3.12.1
|
||||
*
|
||||
* @param bool $should_purge Should purge status based on the updated providers.
|
||||
*/
|
||||
do_action( 'rocket_after_save_dynamic_lists', $should_purge );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule cron to update dynamic lists weekly.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function schedule_lists_update() {
|
||||
if ( ! wp_next_scheduled( 'rocket_update_dynamic_lists' ) ) {
|
||||
wp_schedule_event( time(), 'weekly', 'rocket_update_dynamic_lists' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear dynamic lists update event.
|
||||
*/
|
||||
public function clear_schedule_lists_update() {
|
||||
wp_clear_scheduled_hook( 'rocket_update_dynamic_lists' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the dynamic lists update section on tools tab
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function display_update_lists_section() {
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'beacon' => $this->beacon->get_suggest( 'dynamic_lists' ),
|
||||
];
|
||||
|
||||
echo $this->generate( 'settings/dynamic-lists-update', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cached ignored parameters
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_cache_ignored_parameters(): array {
|
||||
$lists = $this->providers['defaultlists']->data_manager->get_lists();
|
||||
|
||||
return isset( $lists->cache_ignored_parameters ) ? array_flip( $lists->cache_ignored_parameters ) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JS minify excluded external paths
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_js_minify_excluded_external(): array {
|
||||
$lists = $this->providers['defaultlists']->data_manager->get_lists();
|
||||
|
||||
return isset( $lists->js_minify_external ) ? $lists->js_minify_external : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the patterns to move after the combine JS file
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_js_move_after_combine(): array {
|
||||
$lists = $this->providers['defaultlists']->data_manager->get_lists();
|
||||
|
||||
return isset( $lists->js_move_after_combine ) ? $lists->js_move_after_combine : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the inline JS excluded from combine JS
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_combine_js_excluded_inline(): array {
|
||||
$lists = $this->providers['defaultlists']->data_manager->get_lists();
|
||||
|
||||
return isset( $lists->js_excluded_inline ) ? $lists->js_excluded_inline : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the preload exclusions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_preload_exclusions(): array {
|
||||
$lists = $this->providers['defaultlists']->data_manager->get_lists();
|
||||
|
||||
return isset( $lists->preload_exclusions ) ? $lists->preload_exclusions : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Delay JS dynamic list.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_delayjs_list() {
|
||||
return $this->providers['delayjslists']->data_manager->get_lists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JS minify excluded files
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_js_exclude_files(): array {
|
||||
$lists = $this->providers['defaultlists']->data_manager->get_lists();
|
||||
|
||||
return isset( $lists->exclude_js_files ) ? $lists->exclude_js_files : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the incompatible plugins list
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_incompatible_plugins() {
|
||||
$lists = $this->providers['incompatible_plugins']->data_manager->get_plugins_list();
|
||||
|
||||
return isset( $lists ) ? $lists : [];
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists\IncompatiblePluginsLists;
|
||||
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\AbstractAPIClient;
|
||||
|
||||
class APIClient extends AbstractAPIClient {
|
||||
|
||||
/**
|
||||
* Specify API endpoint path.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_api_path() {
|
||||
return 'incompatible-plugins/list';
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists\IncompatiblePluginsLists;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\AbstractDataManager;
|
||||
|
||||
class DataManager extends AbstractDataManager {
|
||||
|
||||
/**
|
||||
* Plugin options instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Instantiate the class.
|
||||
*
|
||||
* @param Options_Data $options Options instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options ) {
|
||||
parent::__construct();
|
||||
$this->options = $options;
|
||||
}
|
||||
/**
|
||||
* Get cache transient name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_cache_transient_name() {
|
||||
return 'wpr_dynamic_lists_incompatible_plugins';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get lists json filename.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_json_filename() {
|
||||
return 'dynamic-lists-incompatible-plugins';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the plugins list content
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_plugins_list() {
|
||||
$lists = [];
|
||||
$list_from_json = $this->get_lists();
|
||||
foreach ( $list_from_json as $conditions => $list ) {
|
||||
if ( $this->meet_conditions( $conditions ) ) {
|
||||
$list = array_column( $list, 'file', 'slug' );
|
||||
$lists = array_merge( $lists, $list );
|
||||
}
|
||||
}
|
||||
return $lists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the condition is meet based on plugin option and condition string.
|
||||
* If $conditions contain "||" split and treat it like or
|
||||
*
|
||||
* @param string $conditions condition.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function meet_conditions( $conditions = '' ) {
|
||||
if ( empty( $conditions ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$conditions = explode( '||', $conditions );
|
||||
|
||||
foreach ( $conditions as $condition ) {
|
||||
if ( $this->options->get( trim( $condition ), false ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists;
|
||||
|
||||
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\DefaultLists\APIClient as DefaultListsAPIClient;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\DefaultLists\DataManager as DefaultListsDataManager;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\DelayJSLists\APIClient as DelayJSListsAPIClient;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\DelayJSLists\DataManager as DelayJSListsDataManager;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\IncompatiblePluginsLists\DataManager as IncompatiblePluginsListsDataManager;
|
||||
use WP_Rocket\Engine\Optimization\DynamicLists\IncompatiblePluginsLists\APIClient as IncompatiblePluginsListsAPIClient;
|
||||
|
||||
/**
|
||||
* Service provider for the WP Rocket DynamicLists
|
||||
*/
|
||||
class ServiceProvider extends AbstractServiceProvider {
|
||||
|
||||
/**
|
||||
* The provides array is a way to let the container
|
||||
* know that a service is provided by this service
|
||||
* provider. Every service that is registered via
|
||||
* this service provider must have an alias added
|
||||
* to this array or it will be ignored.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $provides = [
|
||||
'dynamic_lists_defaultlists_data_manager',
|
||||
'dynamic_lists_defaultlists_api_client',
|
||||
'dynamic_lists_delayjslists_data_manager',
|
||||
'dynamic_lists_delayjslists_api_client',
|
||||
'dynamic_lists_incompatible_plugins_lists_data_manager',
|
||||
'dynamic_lists_incompatible_plugins_lists_api_client',
|
||||
'dynamic_lists',
|
||||
'dynamic_lists_subscriber',
|
||||
];
|
||||
|
||||
/**
|
||||
* Registers the option array in the container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
$this->getContainer()->add( 'dynamic_lists_defaultlists_data_manager', DefaultListsDataManager::class );
|
||||
$this->getContainer()->add( 'dynamic_lists_defaultlists_api_client', DefaultListsAPIClient::class )
|
||||
->addArgument( $this->getContainer()->get( 'options' ) );
|
||||
$this->getContainer()->add( 'dynamic_lists_delayjslists_data_manager', DelayJSListsDataManager::class );
|
||||
$this->getContainer()->add( 'dynamic_lists_delayjslists_api_client', DelayJSListsAPIClient::class )
|
||||
->addArgument( $this->getContainer()->get( 'options' ) );
|
||||
$this->getContainer()->add( 'dynamic_lists_incompatible_plugins_lists_data_manager', IncompatiblePluginsListsDataManager::class )
|
||||
->addArgument( $this->getContainer()->get( 'options' ) );
|
||||
$this->getContainer()->add( 'dynamic_lists_incompatible_plugins_lists_api_client', IncompatiblePluginsListsAPIClient::class )
|
||||
->addArgument( $this->getContainer()->get( 'options' ) );
|
||||
|
||||
$providers = [
|
||||
'defaultlists' =>
|
||||
(object) [
|
||||
'api_client' => $this->getContainer()->get( 'dynamic_lists_defaultlists_api_client' ),
|
||||
'data_manager' => $this->getContainer()->get( 'dynamic_lists_defaultlists_data_manager' ),
|
||||
'title' => __( 'Default Lists', 'rocket' ),
|
||||
],
|
||||
'delayjslists' =>
|
||||
(object) [
|
||||
'api_client' => $this->getContainer()->get( 'dynamic_lists_delayjslists_api_client' ),
|
||||
'data_manager' => $this->getContainer()->get( 'dynamic_lists_delayjslists_data_manager' ),
|
||||
'title' => __( 'Delay JavaScript Execution Exclusion Lists', 'rocket' ),
|
||||
],
|
||||
'incompatible_plugins' =>
|
||||
(object) [
|
||||
'api_client' => $this->getContainer()->get( 'dynamic_lists_incompatible_plugins_lists_api_client' ),
|
||||
'data_manager' => $this->getContainer()->get( 'dynamic_lists_incompatible_plugins_lists_data_manager' ),
|
||||
'title' => __( 'Incompatible plugins Lists', 'rocket' ),
|
||||
'clear_cache' => false,
|
||||
],
|
||||
];
|
||||
|
||||
$this->getContainer()->add( 'dynamic_lists', DynamicLists::class )
|
||||
->addArgument( $providers )
|
||||
->addArgument( $this->getContainer()->get( 'user' ) )
|
||||
->addArgument( $this->getContainer()->get( 'template_path' ) )
|
||||
->addArgument( $this->getContainer()->get( 'beacon' ) );
|
||||
|
||||
$this->getContainer()->share( 'dynamic_lists_subscriber', Subscriber::class )
|
||||
->addArgument( $this->getContainer()->get( 'dynamic_lists' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DynamicLists;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
/**
|
||||
* DynamicLists instance
|
||||
*
|
||||
* @var DynamicLists
|
||||
*/
|
||||
private $dynamic_lists;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param DynamicLists $dynamic_lists DynamicLists instance.
|
||||
*/
|
||||
public function __construct( DynamicLists $dynamic_lists ) {
|
||||
$this->dynamic_lists = $dynamic_lists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Events this subscriber listens to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rest_api_init' => 'register_rest_route',
|
||||
'rocket_localize_admin_script' => [ 'add_dynamic_lists_script', 11 ],
|
||||
'init' => 'schedule_lists_update',
|
||||
'rocket_update_dynamic_lists' => 'update_lists',
|
||||
'rocket_deactivation' => 'clear_schedule_lists_update',
|
||||
'rocket_settings_tools_content' => 'display_update_lists_section',
|
||||
'rocket_cache_ignored_parameters' => 'add_cache_ignored_parameters',
|
||||
'rocket_minify_excluded_external_js' => 'add_minify_excluded_external_js',
|
||||
'rocket_move_after_combine_js' => 'add_move_after_combine_js',
|
||||
'rocket_excluded_inline_js_content' => 'add_combine_js_excluded_inline',
|
||||
'rocket_preload_exclude_urls' => 'add_preload_exclusions',
|
||||
'rocket_exclude_js' => 'add_js_exclude_files',
|
||||
'rocket_plugins_to_deactivate' => 'add_incompatible_plugins_to_deactivate',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the REST dynamic lists update route
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_rest_route() {
|
||||
$this->dynamic_lists->register_rest_route();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add REST data to our localize script data.
|
||||
*
|
||||
* @param array $data Localize script data.
|
||||
* @return array
|
||||
*/
|
||||
public function add_dynamic_lists_script( $data ) {
|
||||
$data['rest_url'] = rest_url( 'wp-rocket/v1/dynamic_lists/update/' );
|
||||
$data['rest_nonce'] = wp_create_nonce( 'wp_rest' );
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scheduling the dynamic lists update cron event.
|
||||
*/
|
||||
public function schedule_lists_update() {
|
||||
$this->dynamic_lists->schedule_lists_update();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the dynamic lists update cron event.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear_schedule_lists_update() {
|
||||
$this->dynamic_lists->clear_schedule_lists_update();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update dynamic lists from API.
|
||||
*
|
||||
* * @return void
|
||||
*/
|
||||
public function update_lists() {
|
||||
$this->dynamic_lists->update_lists_from_remote();
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the dynamic lists update section on tools tab
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function display_update_lists_section() {
|
||||
$this->dynamic_lists->display_update_lists_section();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the cached ignored parameters to the array
|
||||
*
|
||||
* @param string $params Array of ignored parameters.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_cache_ignored_parameters( $params = [] ): array {
|
||||
if ( ! is_array( $params ) ) {
|
||||
$params = (array) $params;
|
||||
}
|
||||
|
||||
return array_merge( $params, $this->dynamic_lists->get_cache_ignored_parameters() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the excluded external JS patterns to the array
|
||||
*
|
||||
* @param string $excluded Array of excluded patterns.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_minify_excluded_external_js( $excluded = [] ): array {
|
||||
if ( ! is_array( $excluded ) ) {
|
||||
$excluded = (array) $excluded;
|
||||
}
|
||||
|
||||
return array_merge( $excluded, $this->dynamic_lists->get_js_minify_excluded_external() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the JS patterns to move after the combine JS file to the array
|
||||
*
|
||||
* @param string $excluded Array of patterns to move.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_move_after_combine_js( $excluded = [] ): array {
|
||||
if ( ! is_array( $excluded ) ) {
|
||||
$excluded = (array) $excluded;
|
||||
}
|
||||
|
||||
return array_merge( $excluded, $this->dynamic_lists->get_js_move_after_combine() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the excluded inline JS patterns to the array
|
||||
*
|
||||
* @param string $excluded Array of excluded patterns.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_combine_js_excluded_inline( $excluded = [] ): array {
|
||||
if ( ! is_array( $excluded ) ) {
|
||||
$excluded = (array) $excluded;
|
||||
}
|
||||
|
||||
return array_merge( $excluded, $this->dynamic_lists->get_combine_js_excluded_inline() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the preload exclusions to the array
|
||||
*
|
||||
* @param array $excluded Array of ignored URL regex.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_preload_exclusions( $excluded = [] ): array {
|
||||
if ( ! is_array( $excluded ) ) {
|
||||
$excluded = (array) $excluded;
|
||||
}
|
||||
|
||||
return array_merge( $excluded, $this->dynamic_lists->get_preload_exclusions() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the js files exclusions to the array
|
||||
*
|
||||
* @param array $js_files Array of files.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_js_exclude_files( $js_files = [] ): array {
|
||||
if ( ! is_array( $js_files ) ) {
|
||||
$js_files = (array) $js_files;
|
||||
}
|
||||
|
||||
return array_merge( $js_files, $this->dynamic_lists->get_js_exclude_files() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add incompatible plugins to the array
|
||||
*
|
||||
* @param array $plugins Array of $plugins.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_incompatible_plugins_to_deactivate( $plugins = [] ): array {
|
||||
return array_merge( (array) $plugins, $this->dynamic_lists->get_incompatible_plugins() );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user