Skip to content

Commit

Permalink
Merge pull request #11 from smithfield-studio/upgrade-2.4.8
Browse files Browse the repository at this point in the history
Upgrade 2.4.3 => 2.4.8
  • Loading branch information
mike-sheppard authored Aug 19, 2022
2 parents 15a0938 + 78d5a48 commit 7caa0d8
Show file tree
Hide file tree
Showing 9 changed files with 440 additions and 353 deletions.
4 changes: 2 additions & 2 deletions kinsta-mu-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Kinsta Must-use Plugins
* Plugin URI: https://kinsta.com/knowledgebase/kinsta-mu-plugin/
* Description: The plugin designed to work on Kinsta's managed WordPress hosting platform.
* Version: 2.4.3
* Version: 2.4.8
* Author: Kinsta Team
* Author URI: https://kinsta.com/about-us/
* Text Domain: kinsta-mu-plugins
Expand All @@ -16,7 +16,7 @@
die( 'No script kiddies please!' );
}

define( 'KINSTAMU_VERSION', '2.4.3' );
define( 'KINSTAMU_VERSION', '2.4.8' );
if ( ! defined( 'KINSTAMU_WHITELABEL' ) ) {
define( 'KINSTAMU_WHITELABEL', false );
}
Expand Down
8 changes: 3 additions & 5 deletions kinsta-mu-plugins/cache/cache.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Kinsta Cache
* Kinsta Cache.
*
* Main file to load Kinsta Cache
* Main file to load Kinsta Cache.
*
* @package KinstaMUPlugins
* @subpackage Cache
Expand Down Expand Up @@ -40,7 +40,5 @@

$kinsta_cache = new Cache( $config, $default_settings );

/**
* Backward compatible, WP Rocket plugin's 3.0.1 version caused fatal error without this.
*/
// Backward compatible, WP Rocket plugin version 3.0.1 caused fatal error without this.
$KinstaCache = $kinsta_cache; // phpcs:ignore
132 changes: 111 additions & 21 deletions kinsta-mu-plugins/cache/class-cache-admin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Kinsta Cache Admin classes
* Kinsta Cache Admin classes.
*
* @package KinstaMUPlugins
* @subpackage Cache
Expand All @@ -14,55 +14,77 @@
}

/**
* Cache_Admin class
* Cache_Admin class.
*
* @since 1.0.0
*/
class Cache_Admin {

/**
* Kinsta/Cache Object
* Kinsta/Cache Object.
*
* @var object
*/
public $kinsta_cache;

/**
* Kinsta/Cache_Purge Object
* Kinsta/Cache_Purge Object.
*
* @var object
*/
public $kinsta_cache_purge;

/**
* The role or capability to view and use cache options
* The role or capability to view and use cache options.
*
* @var string
*/
private $view_role_or_capability;

/**
* Constructor class
* Constructor class.
*
* @param object $kinsta_cache Kinsta Cache Object.
*/
public function __construct( $kinsta_cache ) {
public function __construct( $kinsta_cache = false ) {
if ( false === $kinsta_cache ) {
return;
}

// Set our class variables.
$this->kinsta_cache = $kinsta_cache;
$this->kinsta_cache_purge = $this->kinsta_cache->kinsta_cache_purge;
$this->view_role_or_capability = $this->set_view_role_or_capability();

// Admin Menu and Admin Toolbar.
add_action( 'admin_menu', array( $this, 'admin_menu_item' ) );
add_action( 'admin_bar_menu', array( $this, 'admin_bar_item' ), 100 );

// Custom styling.
if ( KINSTAMU_WHITELABEL === false ) {
add_action( 'admin_head', array( $this, 'menu_icon_style' ) );
}
add_action( 'admin_bar_menu', array( $this, 'admin_bar_item' ), 100 );

// Notice for successful cache clear.
add_action( 'admin_notices', array( $this, 'cleared_cache_notice' ) );

// Ajax actions for cache exclusion path management.
add_action( 'wp_ajax_kinsta_save_custom_path', array( $this, 'action_kinsta_save_custom_path' ) );
add_action( 'wp_ajax_kinsta_remove_custom_path', array( $this, 'action_kinsta_remove_custom_path' ) );
}

/**
* Add main Kinsta Tools menu item.
*/
public function admin_menu_item() {
/**
* Filters whether or not Admin Menu item/page is visible.
*
* @param bool True to hide the Admin Menu item/page, false to show. Default is false.
*/
if ( apply_filters( 'kinsta_admin_disabled', false ) ) {
return;
}

$icon = ( KINSTAMU_WHITELABEL === false ) ? 'none' : 'dashicons-admin-generic';
$title = ( KINSTAMU_WHITELABEL === false ) ? __( 'Kinsta Cache', 'kinsta-mu-plugins' ) : __( 'Cache Settings', 'kinsta-mu-plugins' );
Expand All @@ -79,18 +101,23 @@ public function admin_menu_item() {
}

/**
* Add Admin bar menu
* Add Admin Toolbar menu.
*
* @param object $wp_admin_bar WP_Admin_Bar object.
*
* @return void
*/
public function admin_bar_item( $wp_admin_bar ) {
if ( ! current_user_can( $this->view_role_or_capability ) ) {
/**
* Filters whether or not Admin Menu item/page is visible.
*
* @param bool True to hide the Admin Menu item/page, false to show. Default is false.
*/
if ( apply_filters( 'kinsta_admin_disabled', false ) || ! current_user_can( $this->view_role_or_capability ) ) {
return;
}

if ( $this->kinsta_cache->has_object_cache ) {

$wp_admin_bar->add_node(
array(
'id' => 'kinsta-cache',
Expand Down Expand Up @@ -148,16 +175,11 @@ public function admin_bar_item( $wp_admin_bar ) {
* @return void
*/
public function admin_menu_page() {

// TODO: Revisit & test the $_POST.
if ( ! empty( $_POST ) ) { // WPCS: CSRF ok.
$this->kinsta_cache->save_plugin_options();
}
include plugin_dir_path( __FILE__ ) . 'pages/pages.php';
}

/**
* Show Kinsta menu icon
* Show Kinsta menu icon.
*
* @return void
*/
Expand All @@ -177,16 +199,84 @@ public function menu_icon_style() {
}

/**
* Sets the required capability to view and use the cache purging options
* Notice that shows for successful cache clear.
* Query var: kinsta-cache-cleared.
*
* @return string the required capability or role
* @since 1.0.0
*
* @return void
*/
private function set_view_role_or_capability() {
public function cleared_cache_notice() {
if ( ! empty( $_GET['kinsta-cache-cleared'] ) && 'true' === $_GET['kinsta-cache-cleared'] ) :
?>
<div class="notice notice-success is-dismissible">
<p><?php esc_html_e( 'Cache cleared successfully', 'kinsta-mu-plugins' ); ?></p>
</div>
<?php
endif;
}

/**
* AJAX Action to save custom path
*
* @return void
*/
public function action_kinsta_save_custom_path() {
check_ajax_referer( 'save_plugin_options', 'kinsta_nonce' );

$paths = get_option( 'kinsta-cache-additional-paths' );
if ( empty( $paths ) ) {
$paths = array();
}

$paths[] = array(
'path' => sanitize_text_field( $_POST['path'] ),
'type' => sanitize_text_field( wp_unslash( $_POST['type'] ) ),
);
$paths = array_values( $paths );

update_option( 'kinsta-cache-additional-paths', $paths );

die();
}

/**
* AJAX action to remove custom path.
*
* @return void
*/
public function action_kinsta_remove_custom_path() {
check_ajax_referer( 'save_plugin_options', 'kinsta_nonce' );
if ( ! isset( $_POST['index'] ) || ( isset( $_POST['index'] ) && is_int( $_POST['index'] ) ) ) {
return;
}

$index = sanitize_text_field( wp_unslash( $_POST['index'] ) );
$paths = get_option( 'kinsta-cache-additional-paths' );
if ( ! empty( $paths[ $index ] ) ) {
unset( $paths[ $index ] );
}

if ( count( $paths ) === 0 ) {
delete_option( 'kinsta-cache-additional-paths' );
} else {
$paths = array_values( $paths );
update_option( 'kinsta-cache-additional-paths', $paths );
}

die();
}

/**
* Sets the required capability to view and use the cache purging options.
*
* @return string the required capability
*/
private function set_view_role_or_capability() {
if ( defined( 'KINSTAMU_ROLE' ) && is_string( KINSTAMU_ROLE ) ) {
return esc_attr( KINSTAMU_ROLE );
}

return 'manage_options';
}

}
Loading

0 comments on commit 7caa0d8

Please sign in to comment.