Skip to content

Commit

Permalink
Merge branch 'develop' into enhancement/update-tests-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
remyperona committed Sep 21, 2023
2 parents 46c9c0d + ba9ba9d commit 878a23a
Show file tree
Hide file tree
Showing 29 changed files with 737 additions and 296 deletions.
207 changes: 207 additions & 0 deletions inc/Engine/Admin/Metaboxes/PostEditOptionsSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Admin\Metaboxes;

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

class PostEditOptionsSubscriber extends Abstract_Render implements Subscriber_Interface {
/**
* Options instance
*
* @var Options_data
*/
private $options;

/**
* Constructor
*
* @param Options_Data $options Options instance.
* @param string $template_path Path to the views.
*/
public function __construct( Options_Data $options, $template_path ) {
parent::__construct( $template_path );

$this->options = $options;
}

/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'add_meta_boxes' => 'options_metabox',
'save_post' => 'save_metabox_options',
];
}

/**
* Add options metabox on post edit page
*
* @return void
*/
public function options_metabox() {
if ( ! rocket_can_display_options() ) {
return;
}

if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}

$cpts = get_post_types(
[
'public' => true,
],
'objects'
);

unset( $cpts['attachment'] );

/**
* Filters the post types to add the options metabox to
*
* @param array $cpts Array of post types.
*/
$cpts = apply_filters( 'rocket_metabox_options_post_types', $cpts );

foreach ( $cpts as $cpt => $cpt_object ) {
$label = $cpt_object->labels->singular_name;
add_meta_box( 'rocket_post_exclude', sprintf( __( 'WP Rocket Options', 'rocket' ), $label ), [ $this, 'display_metabox' ], $cpt, 'side', 'core' );
}
}

/**
* Displays checkboxes to de/activate some options
*
* @return void
*/
public function display_metabox() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}

global $post, $pagenow;

$excluded_url = false;

if ( 'post-new.php' !== $pagenow ) {
$path = rocket_clean_exclude_file( get_permalink( $post->ID ) );

if ( in_array( $path, $this->options->get( 'cache_reject_uri', [] ), true ) ) {
$excluded_url = true;
}
}

$original_fields = [];

/**
* WP Rocket Metabox fields on post edit page.
*
* @param string[] $original_fields Metaboxes fields.
*/
$fields = apply_filters( 'rocket_meta_boxes_fields', $original_fields );

if ( ! is_array( $fields ) ) {
$fields = $original_fields;
}

$fields_attributes = [];

foreach ( $fields as $field => $label ) {
$disabled = disabled( ! $this->options->get( $field ), true, false );

$fields_attributes[ $field ]['id'] = $field;
$fields_attributes[ $field ]['label'] = $label;
// translators: %s is the name of the option.
$fields_attributes[ $field ]['title'] = $disabled ? ' title="' . esc_attr( sprintf( __( 'Activate first the %s option.', 'rocket' ), $label ) ) . '"' : '';
$fields_attributes[ $field ]['class'] = $disabled ? ' class="rkt-disabled"' : '';
$fields_attributes[ $field ]['checked'] = ! $disabled ? checked( ! get_post_meta( $post->ID, '_rocket_exclude_' . $field, true ), true, false ) : '';
$fields_attributes[ $field ]['disabled'] = $disabled;
}

echo $this->generate( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'post_edit_options',
[
'excluded_url' => $excluded_url, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'fields' => $fields_attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
]
);
}

/**
* Updates the options from the metabox.
*
* @return void
*/
public function save_metabox_options() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}

if ( ! isset( $_POST['post_ID'], $_POST['rocket_post_exclude_hidden'] ) ) {
return;
}

check_admin_referer( 'rocket_box_option', '_rocketnonce' );

// No cache field.
if ( isset( $_POST['post_status'] ) && 'publish' === $_POST['post_status'] ) {
$new_cache_reject_uri = $cache_reject_uri = $this->options->get( 'cache_reject_uri', [] ); // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found
$rejected_uris = array_flip( $cache_reject_uri );
$path = rocket_clean_exclude_file( get_permalink( (int) $_POST['post_ID'] ) );

if ( isset( $_POST['rocket_post_nocache'] ) ) {
if ( ! isset( $rejected_uris[ $path ] ) ) {
array_push( $new_cache_reject_uri, $path );
}
} else {
if ( isset( $rejected_uris[ $path ] ) ) {
unset( $new_cache_reject_uri[ $rejected_uris[ $path ] ] );
}
}

if ( $new_cache_reject_uri !== $cache_reject_uri ) {
// Update the "Never cache the following pages" option.
update_rocket_option( 'cache_reject_uri', $new_cache_reject_uri );

// Update config file.
rocket_generate_config_file();
}
}

$original_fields = [];

/**
* Metaboxes fields.
*
* @param string[] $original_fields Metaboxes fields.
*/
$fields = apply_filters( 'rocket_meta_boxes_fields', $original_fields );

if ( ! is_array( $fields ) ) {
$fields = $original_fields;
}

$fields = array_keys( $fields );

foreach ( $fields as $field ) {
if ( ! isset( $_POST['rocket_post_exclude_hidden'][ $field ] ) ) {
continue;
}

if ( isset( $_POST['rocket_post_exclude'][ $field ] ) ) {
delete_post_meta( (int) $_POST['post_ID'], '_rocket_exclude_' . $field );
continue;
}

if ( $this->options->get( $field ) ) {
update_post_meta( (int) $_POST['post_ID'], '_rocket_exclude_' . $field, true );
}
}
}
}
9 changes: 7 additions & 2 deletions inc/Engine/Admin/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
namespace WP_Rocket\Engine\Admin;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Admin\Deactivation\DeactivationIntent;
use WP_Rocket\Engine\Admin\Deactivation\Subscriber;
use WP_Rocket\Engine\Admin\Deactivation\{DeactivationIntent, Subscriber};
use WP_Rocket\Engine\Admin\Metaboxes\PostEditOptionsSubscriber;
use WP_Rocket\ThirdParty\Plugins\Optimization\Hummingbird;

/**
Expand All @@ -27,6 +27,7 @@ class ServiceProvider extends AbstractServiceProvider {
'deactivation_intent_subscriber',
'hummingbird_subscriber',
'actionscheduler_admin_subscriber',
'post_edit_options_subscriber',
];

/**
Expand All @@ -48,5 +49,9 @@ public function register() {
->addArgument( $options )
->addTag( 'admin_subscriber' );
$this->getContainer()->share( 'actionscheduler_admin_subscriber', ActionSchedulerSubscriber::class );
$this->getContainer()->share( 'post_edit_options_subscriber', PostEditOptionsSubscriber::class )
->addArgument( $options )
->addArgument( $this->getContainer()->get( 'template_path' ) . '/metaboxes' )
->addTag( 'admin_subscriber' );
}
}
32 changes: 32 additions & 0 deletions inc/Engine/CDN/Admin/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\CDN\Admin;

use WP_Rocket\Event_Management\Subscriber_Interface;

class Subscriber implements Subscriber_Interface {
/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_meta_boxes_fields' => [ 'add_meta_box', 9 ],
];
}

/**
* Add the field to the WP Rocket metabox on the post edit page.
*
* @param string[] $fields Metaboxes fields.
*
* @return string[]
*/
public function add_meta_box( array $fields ) {
$fields['cdn'] = __( 'CDN', 'rocket' );

return $fields;
}
}
4 changes: 4 additions & 0 deletions inc/Engine/CDN/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace WP_Rocket\Engine\CDN;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\CDN\Admin\Subscriber as AdminSubscriber;

/**
* Service provider for WP Rocket CDN
Expand All @@ -21,6 +22,7 @@ class ServiceProvider extends AbstractServiceProvider {
protected $provides = [
'cdn',
'cdn_subscriber',
'cdn_admin_subscriber',
];

/**
Expand All @@ -37,5 +39,7 @@ public function register() {
->addArgument( $options )
->addArgument( $this->getContainer()->get( 'cdn' ) )
->addTag( 'common_subscriber' );
$this->getContainer()->share( 'cdn_admin_subscriber', AdminSubscriber::class )
->addTag( 'admin_subscriber' );
}
}
16 changes: 15 additions & 1 deletion inc/Engine/Cache/PurgeActionsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function get_subscribed_events() {
],
'rocket_rucss_complete_job_status' => [ 'purge_url_cache', 100 ],
'rocket_rucss_after_clearing_usedcss' => 'purge_url_cache',
'rocket_after_save_dynamic_lists' => 'purge_cache',
'rocket_after_save_dynamic_lists' => 'purge_cache_after_saving_dynamic_lists',
'update_option_' . $slug => [ 'purge_cache_reject_uri_partially', 10, 2 ],
'update_option_blog_public' => 'purge_cache',
];
Expand Down Expand Up @@ -179,4 +179,18 @@ public function purge_cache() {
public function purge_cache_reject_uri_partially( array $old_value, array $value ): void {
$this->purge->purge_cache_reject_uri_partially( $old_value, $value );
}

/**
* Purge cache after saving dynamic lists.
*
* @param bool $should_purge Should purge or not.
*
* @return void
*/
public function purge_cache_after_saving_dynamic_lists( $should_purge = true ) {
if ( ! $should_purge ) {
return;
}
$this->purge_cache();
}
}
15 changes: 15 additions & 0 deletions inc/Engine/CriticalPath/Admin/Subscriber.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\CriticalPath\Admin;

Expand Down Expand Up @@ -59,6 +60,7 @@ public static function get_subscribed_events() {
[ 'enqueue_admin_cpcss_heartbeat_script' ],
],
'rocket_admin_bar_items' => 'add_regenerate_menu_item',
'rocket_meta_boxes_fields' => [ 'add_meta_box', 3 ],
];
}

Expand Down Expand Up @@ -186,4 +188,17 @@ public function enqueue_admin_cpcss_heartbeat_script() {
public function add_regenerate_menu_item( $wp_admin_bar ) {
$this->admin->add_regenerate_menu_item( $wp_admin_bar );
}

/**
* Add the field to the WP Rocket metabox on the post edit page.
*
* @param string[] $fields Metaboxes fields.
*
* @return string[]
*/
public function add_meta_box( array $fields ) {
$fields['async_css'] = __( 'Load CSS asynchronously', 'rocket' );

return $fields;
}
}
16 changes: 16 additions & 0 deletions inc/Engine/Media/Lazyload/AdminSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Lazyload;

Expand All @@ -14,6 +15,7 @@ public static function get_subscribed_events(): array {
return [
'rocket_first_install_options' => [ 'add_option', 15 ],
'rocket_input_sanitize' => 'sanitize_exclude_lazyload',
'rocket_meta_boxes_fields' => [ 'add_meta_box', 7 ],
];
}

Expand Down Expand Up @@ -48,4 +50,18 @@ public function sanitize_exclude_lazyload( array $input ): array {

return $input;
}

/**
* Add the field to the WP Rocket metabox on the post edit page.
*
* @param string[] $fields Metaboxes fields.
*
* @return string[]
*/
public function add_meta_box( array $fields ) {
$fields['lazyload'] = __( 'LazyLoad for images', 'rocket' );
$fields['lazyload_iframes'] = __( 'LazyLoad for iframes/videos', 'rocket' );

return $fields;
}
}
Loading

0 comments on commit 878a23a

Please sign in to comment.