Skip to content

Commit

Permalink
Merge pull request #8 from smithfield-studio/update-2-4-3
Browse files Browse the repository at this point in the history
  • Loading branch information
retlehs authored Mar 5, 2022
2 parents 2c08dc2 + d78bf29 commit 15a0938
Show file tree
Hide file tree
Showing 25 changed files with 526 additions and 417 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Kinsta Must Use (MU) Plugin

A simple mirror of the official Kinsta Mu Plugin with added Composer/[Bedrock](https://github.com/roots/bedrock) support.

For latest version/info please see the official [Kinsta MU Plugin page](https://kinsta.com/help/kinsta-mu-plugin/).

## Installing with Composer (for Bedrock)

1. Add this plugin repo to `repositories`in your Bedrock `composer.json` file:
```diff
"repositories": {
"wpackagist": {
"type": "composer",
"url": "https://wpackagist.org",
"only": [
"wpackagist-plugin/*",
"wpackagist-theme/*"
]
},
+ "kinsta-mu-plugins": {
+ "type": "vcs",
+ "url": "[email protected]:retlehs/kinsta-mu-plugins"
+ },
```
1. Add the plugin requirement with specific version number (or `*` for latest) to `composer.json`:
```diff
"require": {
"php": ">=7.4",
"composer/installers": "^2.0",
...
+ "kinsta/kinsta-mu-plugins": "*",
...
}
```
1. Run `composer update` from the Bedrock directory.


## White label

Enabling white labeling will change the following elements in the WordPress dashboard:

1. The branded **Kinsta Cache** sidebar link will be changed to an unbranded **Cache Settings** link.
1. The **Thanks for creating with WordPress and hosting with Kinsta** message near the bottom of the dashboard will be replaced with **Thank you for creating with WordPress**.
1. The Kinsta logo on the **Cache Control** page will be removed or replaced with an image of your choice.
1. The links to Kinsta documentation and support will be removed.

```php
define('KINSTAMU_WHITELABEL', true);
define('KINSTAMU_LOGO', 'https://mylogo.com/mylogo.jpg');
```

For more info, refer to the [official docs here](https://kinsta.com/help/white-label-kinsta-wordpress-admin/)


## Bedrock + Kinsta
The following constants may be required to fix issues with CDN paths + shared plugin asset URLs.

```php
/**
* Kinsta CDN fix for Bedrock
*/
define('KINSTA_CDN_USERDIRS', 'app');

/**
* Fix Kinsta MU Plugins URL path with Bedrock
*/
$mu_plugins_url = Config::get('WP_CONTENT_URL') . '/mu-plugins';
define('KINSTAMU_CUSTOM_MUPLUGIN_URL', "{$mu_plugins_url}/kinsta-mu-plugins");
```
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.3.4
* Version: 2.4.3
* 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.3.4' );
define( 'KINSTAMU_VERSION', '2.4.3' );
if ( ! defined( 'KINSTAMU_WHITELABEL' ) ) {
define( 'KINSTAMU_WHITELABEL', false );
}
Expand Down
3 changes: 2 additions & 1 deletion kinsta-mu-plugins/cache/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
'single' => array(),
),
),
'rules' => array(),
);

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

/**
* Backward compatible, WP Rocket plugin's 3.0.1 version caused fatal error without this.
*/
$KinstaCache = $kinsta_cache; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCase
$KinstaCache = $kinsta_cache; // phpcs:ignore
13 changes: 8 additions & 5 deletions kinsta-mu-plugins/cache/class-cache-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ public function admin_bar_item( $wp_admin_bar ) {
array(
'id' => 'kinsta-cache',
'title' => __( 'Clear Cache', 'kinsta-mu-plugins' ),
'meta' => array( 'title' => __( 'Clear Cache', 'kinsta-mu-plugins' ) ),
'meta' => array(
'title' => __( 'Clear Cache', 'kinsta-mu-plugins' ),
'tabindex' => 0,
),
'parent' => 'top-secondary',
)
);
Expand All @@ -104,7 +107,7 @@ public function admin_bar_item( $wp_admin_bar ) {
array(
'id' => 'kinsta-cache-all',
'title' => 'Clear All Caches',
'href' => wp_nonce_url( admin_url( 'admin-ajax.php?action=kinsta_clear_cache_all&source=adminbar' ), 'kinsta-clear-cache-all', 'kinsta_nonce' ),
'href' => wp_nonce_url( admin_url( 'admin.php?page=kinsta-tools&clear-cache=kinsta-clear-cache-all' ), 'kinsta-clear-cache-admin-bar', 'kinsta_nonce' ),
'parent' => 'kinsta-cache',
)
);
Expand All @@ -113,7 +116,7 @@ public function admin_bar_item( $wp_admin_bar ) {
array(
'id' => 'kinsta-cache-full-page',
'title' => 'Clear Full Page Cache',
'href' => wp_nonce_url( admin_url( 'admin-ajax.php?action=kinsta_clear_cache_full_page&source=adminbar' ), 'kinsta-clear-cache-full-page', 'kinsta_nonce' ),
'href' => wp_nonce_url( admin_url( 'admin.php?page=kinsta-tools&clear-cache=kinsta-clear-cache-full-page' ), 'kinsta-clear-cache-admin-bar', 'kinsta_nonce' ),
'parent' => 'kinsta-cache',
)
);
Expand All @@ -122,7 +125,7 @@ public function admin_bar_item( $wp_admin_bar ) {
array(
'id' => 'kinsta-cache-object',
'title' => 'Clear Object Cache',
'href' => wp_nonce_url( admin_url( 'admin-ajax.php?action=kinsta_clear_cache_object&source=adminbar' ), 'kinsta-clear-cache-object', 'kinsta_nonce' ),
'href' => wp_nonce_url( admin_url( 'admin.php?page=kinsta-tools&clear-cache=kinsta-clear-cache-object' ), 'kinsta-clear-cache-admin-bar', 'kinsta_nonce' ),
'parent' => 'kinsta-cache',
)
);
Expand All @@ -131,7 +134,7 @@ public function admin_bar_item( $wp_admin_bar ) {
array(
'id' => 'kinsta-cache',
'title' => __( 'Clear Cache', 'kinsta-mu-plugins' ),
'href' => wp_nonce_url( admin_url( 'admin-ajax.php?action=kinsta_clear_cache_full_page&source=adminbar' ), 'kinsta-clear-cache-full-page', 'kinsta_nonce' ),
'href' => wp_nonce_url( admin_url( 'admin.php?page=kinsta-tools&clear-cache=kinsta-clear-cache-full-page' ), 'kinsta-clear-cache-admin-bar', 'kinsta_nonce' ),
'meta' => array( 'title' => __( 'Clear Cache', 'kinsta-mu-plugins' ) ),
'parent' => 'top-secondary',
)
Expand Down
54 changes: 33 additions & 21 deletions kinsta-mu-plugins/cache/class-cache-purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Cache_Purge {
*/
public function __construct( $kinsta_cache ) {
global $wp_rewrite;
global $wp_version;

$this->kinsta_cache = $kinsta_cache;
$this->posts_page_id = get_option( 'page_for_posts' );
Expand All @@ -82,14 +83,15 @@ public function __construct( $kinsta_cache ) {
$this->purge_single_happened = false;
$this->purge_all_happened = false;

add_action( 'transition_post_status', array( $this, 'post_published' ), 10, 3 );
add_action( 'edit_comment', array( $this, 'comment_edit_actions' ), 10, 2 );
add_action( 'pre_post_update', array( $this, 'post_unpublished' ), 10, 2 );
add_action( 'post_updated', array( $this, 'post_updated' ), 10, 3 );
add_action( 'wp_trash_post', array( $this, 'post_trashed' ), 10 );

add_action( 'wp_insert_comment', array( $this, 'comment_insert_actions' ), 10, 2 );
add_action( 'edit_comment', array( $this, 'comment_edit_actions' ), 10, 2 );
add_action( 'transition_comment_status', array( $this, 'comment_transition_actions' ), 10, 3 );
add_action( 'transition_post_status', array( $this, 'post_published' ), 10, 3 );

add_action( 'wp_insert_comment', array( $this, 'comment_insert_actions' ), 10, 2 );
add_action( 'wp_insert_post', array( $this, 'post_updated' ), 10, 3 );
add_action( 'wp_trash_post', array( $this, 'post_trashed' ), 10 );
add_action( 'wp_update_nav_menu', array( $this, 'purge_complete_caches' ) );
}

Expand All @@ -102,6 +104,7 @@ public function __construct( $kinsta_cache ) {
* @return void
*/
public function post_published( $new_status, $old_status, $post ) {

if ( $new_status === $old_status || $this->purge_single_happened ) {
return;
}
Expand Down Expand Up @@ -140,20 +143,21 @@ public function post_unpublished( $post_ID, $updated ) {
/**
* Figures out which published post is updated and initiates a cache purge with that post.
*
* @param int $post_ID The post ID.
* @param int $post_id The post ID.
* @param WP_Post $post_after Post object following the update.
* @param WP_Post $post_before Post object following the update.
* @param bool $update Whether this is an existing post being updated.
* @return void
*/
public function post_updated( $post_ID, $post_after, $post_before ) {
if ( $this->purge_single_happened || wp_is_post_autosave( $post_ID ) || wp_is_post_revision( $post_ID ) ) {
public function post_updated( $post_id, $post, $update ) {

if ( $this->purge_single_happened || wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
return;
}

// Clear cache when the post updated, and only when it's already / still published.
if ( 'publish' === $post_after->post_status && 'publish' === $post_before->post_status ) {
if ( true === $update && 'publish' === get_post_status( $post_id ) ) {
$this->purge_single_happened = true;
$this->initiate_purge( $post_ID, 'post' );
$this->initiate_purge( $post_id, 'post' );
}
}

Expand Down Expand Up @@ -278,7 +282,13 @@ public function initiate_purge( $post_id, $event ) {

$archives = $this->get_post_archives_list( $post );

$purge_list['throttled'] = $archives;
$purge_list = array(
'throttled' => $archives,
'immediate' => array(
'single' => array(),
'group' => array(),
),
);

// Immediately remove first three pages of archives.
foreach ( $archives['group'] as $key => $url ) {
Expand All @@ -298,7 +308,7 @@ public function initiate_purge( $post_id, $event ) {
$purge_list['immediate']['single'][ 'blog_page_' . $i ] = $this->posts_page_url . '/page/' . $i . '/';
}

$purge_list['throttled']['group']['blog_page'] = $purge_list['single']['home_page'] . '/page/';
$purge_list['throttled']['group']['blog_page'] = $purge_list['immediate']['single']['home_page'] . '/page/';
} else {
$purge_list['immediate']['single']['home_blog_page'] = home_url() . '/';
for ( $i = 2; $i <= $this->immediate_depth; $i++ ) {
Expand Down Expand Up @@ -350,11 +360,9 @@ public function initiate_purge( $post_id, $event ) {

/**
* Filters applied.
*
* TODO Rewrite the filter name to follow WordPress standard, and remove the rule exclusion in the phpcs.xml.
*/
$purge_request['immediate'] = apply_filters( 'KinstaCache/purgeImmediate', $purge_request['immediate'] );
$purge_request['throttled'] = apply_filters( 'KinstaCache/purgeThrottled', $purge_request['throttled'] );
$purge_request['immediate'] = apply_filters( 'KinstaCache/purgeImmediate', $purge_request['immediate'] ); // phpcs:ignore
$purge_request['throttled'] = apply_filters( 'KinstaCache/purgeThrottled', $purge_request['throttled'] ); // phpcs:ignore

$result['requests'] = $purge_request;

Expand Down Expand Up @@ -429,6 +437,11 @@ public function get_post_archives_list( $post ) {
$taxonomies = array_values( $taxonomies );
$terms = wp_get_object_terms( $post->ID, $taxonomies );

$purge = array(
'group' => array(),
'single' => array(),
);

// Author Archive.
$purge['group']['author'] = get_author_posts_url( $post->post_author );

Expand All @@ -439,10 +452,9 @@ public function get_post_archives_list( $post ) {
}
}

$time = strtotime( $post->post_date );
$year = date( 'Y', $time );
$month = date( 'm', $time );
$day = date( 'd', $time );
$year = get_the_date( 'Y', $post );
$month = get_the_date( 'm', $post );
$day = get_the_date( 'd', $post );

$purge['single']['year'] = get_year_link( $year );
$purge['single']['month'] = get_month_link( $year, $month );
Expand Down
50 changes: 37 additions & 13 deletions kinsta-mu-plugins/cache/class-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Cache {
*
* @var Cache_Purge
*/
public $KinstaCachePurge; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase
public $KinstaCachePurge; // phpcs:ignore

/**
* The cache configuration
Expand Down Expand Up @@ -102,6 +102,8 @@ public function __construct( $config, $default_settings ) {
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_action( 'admin_init', array( $this, 'clear_cache_admin_bar' ) );

// Removing other cache systems.
add_filter( 'do_rocket_generate_caching_files', '__return_false', 999 ); // Disable WP rocket caching.
}
Expand All @@ -118,8 +120,7 @@ public function init_cache() {
$this->kinsta_cache_purge = new Cache_Purge( $this );
$this->kinsta_cache_admin = new Cache_Admin( $this );

// phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
$this->KinstaCachePurge = $this->kinsta_cache_purge;
$this->KinstaCachePurge = $this->kinsta_cache_purge; // phpcs:ignore

do_action( 'kinsta_cache_init', $this );
}
Expand All @@ -146,7 +147,7 @@ public function set_has_object_cache() {
* @return void
*/
public function cleared_cache_notice() {
if ( ! empty( $_GET['kinsta-cache-cleared'] ) && 'true' == $_GET['kinsta-cache-cleared'] ) : // WPCS: CSRF ok, loose comparison ok.
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>
Expand Down Expand Up @@ -243,6 +244,38 @@ public function save_plugin_options() {
update_option( $this->config['option_name'], $this->settings );
}

/**
* * Function to handle Admin Bar cache clear requests.
* *
* * @return void
*/
public function clear_cache_admin_bar() {
if ( empty( $_GET['page'] ) || empty( $_GET['clear-cache'] ) || ( ! empty( $_GET['page'] ) && 'kinsta-tools' !== $_GET['page'] ) ) {
return;
}
check_admin_referer( 'kinsta-clear-cache-admin-bar', 'kinsta_nonce' );
if ( 'kinsta-clear-cache-all' === $_GET['clear-cache'] ) {
$this->kinsta_cache_purge->purge_complete_caches();
} elseif ( 'kinsta-clear-cache-object' === $_GET['clear-cache'] ) {
$this->kinsta_cache_purge->purge_complete_object_cache();
} elseif ( 'kinsta-clear-cache-full-page' === $_GET['clear-cache'] ) {
$this->kinsta_cache_purge->purge_complete_full_page_cache();
} else {
return;
}
if ( empty( wp_get_referer() ) ) {
$query_vars = array(
'page' => 'kinsta-tools',
'kinsta-cache-cleared' => 'true',
);
$redirect_url = add_query_arg( $query_vars, admin_url( 'admin.php' ) );
} else {
$redirect_url = add_query_arg( 'kinsta-cache-cleared', 'true', wp_get_referer() );
}
wp_safe_redirect( $redirect_url );
exit;
}

/**
* AJAX Action to clear all cache
*
Expand All @@ -252,9 +285,6 @@ public function action_kinsta_clear_cache_all() {

check_ajax_referer( 'kinsta-clear-cache-all', 'kinsta_nonce' );
$this->kinsta_cache_purge->purge_complete_caches();
if ( isset( $_GET ) && isset( $_GET['source'] ) && 'adminbar' == $_GET['source'] ) { // WPCS: loose comparison ok.
header( 'Location: ' . add_query_arg( 'kinsta-cache-cleared', 'true', $_SERVER['HTTP_REFERER'] ) );
}
die();
}

Expand All @@ -267,9 +297,6 @@ public function action_kinsta_clear_cache_full_page() {

check_ajax_referer( 'kinsta-clear-cache-full-page', 'kinsta_nonce' );
$this->kinsta_cache_purge->purge_complete_full_page_cache();
if ( isset( $_GET ) && isset( $_GET['source'] ) && 'adminbar' == $_GET['source'] ) { // WPCS: CSRF ok, loose comparison ok.
header( 'Location: ' . add_query_arg( 'kinsta-cache-cleared', 'true', $_SERVER['HTTP_REFERER'] ) );
}
die();
}

Expand All @@ -282,9 +309,6 @@ public function action_kinsta_clear_cache_object() {

check_ajax_referer( 'kinsta-clear-cache-object', 'kinsta_nonce' );
$this->kinsta_cache_purge->purge_complete_object_cache();
if ( 'adminbar' == $_GET['source'] ) { // WPCS: CSRF ok, loose comparison ok.
header( 'Location: ' . add_query_arg( 'kinsta-cache-cleared', 'true', $_SERVER['HTTP_REFERER'] ) );
}
die();
}

Expand Down
4 changes: 2 additions & 2 deletions kinsta-mu-plugins/cache/pages/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
<?php
echo wp_kses(
$warning_msg,
[
array(
'strong' => true,
'code' => true,
]
)
);
?>
</div>
Expand Down
Loading

0 comments on commit 15a0938

Please sign in to comment.