Skip to content

Commit

Permalink
Merge pull request #6 from wp-media/branch-2.0.3
Browse files Browse the repository at this point in the history
v2.0.3: update, bugfixes, new stuff
  • Loading branch information
arunbasillal authored Apr 13, 2018
2 parents 232a32e + a3c4128 commit 4a89228
Show file tree
Hide file tree
Showing 15 changed files with 327 additions and 26 deletions.
28 changes: 28 additions & 0 deletions _experimental/wp-rocket-cache-purge-cron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# NOT A PLUGIN! DON’T INSTALL THIS IN YOUR WORDPRESS!
_ZIP file not provided on purpose._

Cron script to clear the cache and run a cache preload.

🚧 **ADVANCED CUSTOMIZATION, HANDLE WITH CARE!**
📝 **Manual code edit required before use!**

Edit `WPROCKETHELPERS_WP_LOAD_FILE` to reflect the absolute path to wp-load.php from wherever on your server you run this script.

This script does the following:

- Checks WP Rocket is active on the website. If so:
- Clears the cache.
- Checks if sitemap preload is configured.
- If so, runs a sitemap preload; otherwise calls the preload bot.
- Checks if the website is a multisite network.
- If so, iterates over each site in the network, and performs the steps above.

Documentation:
* [How to clear cache via cron job](http://docs.wp-rocket.me/article/494-how-to-clear-cache-via-cron-job)

To be used with:
* any setup where the cache should be cleared and preloaded with a custom cron job rather than wp-cron

Last tested with:
* WP Rocket 2.11.x
* WordPress 4.9.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* *********************************************************************
* THIS IS NOT A PLUGIN! DON’T INSTALL THIS IN YOUR WORDPRESS!
* *********************************************************************
* Read the documentation:
* http://docs.wp-rocket.me/article/494-how-to-clear-cache-via-cron-job
* *********************************************************************
* URI: https://github.com/wp-media/wp-rocket-helpers/tree/master/cache/wp-rocket-cache-purge-cron/
* Author: WP Rocket Support Team
* Author URI: http://wp-rocket.me/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright SAS WP MEDIA 2018
*/

namespace WP_Rocket\Helpers\cache\cron\purge_preload;

// EDIT THIS TO REFLECT THE ABSOLUTE PATH TO wp-load.php ON YOUR SERVER:

define( 'WPROCKETHELPERS_WP_LOAD_FILE', '/path/to/wp-load.php' );

// STOP EDITING.

// If we are in WordPress at this point, something is not right, so nope.
if ( defined( 'ABSPATH' ) ) {
die( 'This is not a WordPress plugin! Read the <a href="http://docs.wp-rocket.me/article/494-how-to-clear-cache-via-cron-job">documentation</a>.' );
}

// Check if wp-load.php exists at this path.
file_exists( WPROCKETHELPERS_WP_LOAD_FILE ) or die( 'Please enter the absolute path to wp-load.php.' );

// Load WordPress.
require( WPROCKETHELPERS_WP_LOAD_FILE );

/**
* Clear cache and run preload.
* In a multisite network, this applies only to the main site.
*/
clear_cache_run_preload();


// Stop here if this is a regular single WordPress site, not a multisite.
if ( ! is_multisite() ) {
return;
}

/**
* Clear cache and run preload on each sub-site in the network.
*/
$subsites = get_sites();
foreach( $subsites as $subsite ) {

// Check if WP Rocket is active.
if ( wp_rocket_maybe_active() ) {

// Clear cache, run preload.
clear_cache_run_preload();
}
}

/**
* Checks functions from WP Rocket are available.
*
* @author Caspar Hübinger
* @return bool True if specified functions exist, else false
*/
function wp_rocket_maybe_active() {
return function_exists( '\rocket_clean_domain' ) && function_exists( '\run_rocket_preload_cache' );
}

/**
* Checks if WP Rocket is active; if so, clears cache, and runs preload.
*
* @author Caspar Hübinger
* @uses wp_rocket_maybe_active
* @return bool False if WP Rocket is not active, else true
*/
function clear_cache_run_preload() {

if ( ! wp_rocket_maybe_active() ) {
return false;
}

// Clear global cache.
\rocket_clean_domain();

// Run preload (bot + sitemap as configured).
\run_rocket_preload_cache( 'cache-preload' );

return true;
}
13 changes: 13 additions & 0 deletions _experimental/wp-rocket-compat-wc-optimize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# WP Rocket | Optimize WooCommerce Cart, Checkout, Account

Disables page caching for WooCommerce cart, checkout, and account pages, but keeps other optimization features applicable.

🚧&#160;&#160;**ADVANCED CUSTOMIZATION, HANDLE WITH CARE!**

To be used with:
* WooCommerce

Last tested with:
* WooCommerce 3.x
* WP Rocket 3.x
* WordPress 4.9.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Plugin Name: WP Rocket | Optimize WooCommerce Cart, Checkout, Account
* Description: Disables page caching for WooCommerce cart, checkout, and account pages, but keeps other optimization features applicable.
* Author: WP Rocket Support Team
* Author URI: http://wp-rocket.me/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright SAS WP MEDIA 2018
*/

// Standard plugin security, keep this line in place.
defined( 'ABSPATH' ) or die();

/**
* Remove WP Rocket core behaviour.
*
* @see wp-rocket/inc/3rd-party/plugins/ecommerce/woocommerce.php#L6
*/
add_action( 'wp_rocket_loaded', function () {
remove_filter( 'rocket_cache_reject_uri', 'rocket_exclude_woocommerce_pages' );
});

/**
* Alters WP Rocket’s core behaviours so that page caching is still disabled on
* cart, checkout, and account pages, but other optimization features (LazyLoad,
* Minify, Optimize CSS, deferred JS etc.) are applied.
*
* @return [type] [description]
*/
add_action( 'template_redirect', function () {

if ( ! function_exists( 'rocket_exclude_woocommerce_pages' ) ) {
return;
}

if ( ! class_exists( 'WooCommerce' ) ) {
return;
}

if ( ! function_exists( 'is_cart' ) || ! function_exists( 'is_checkout' ) || ! function_exists( 'is_account_page' ) ) {
return;
}

/**
* Disable page caching on dedicated pages.
*
* @see https://docs.woocommerce.com/document/conditional-tags/
*/
if ( is_cart() || is_checkout() || is_account_page() ) {
// var_dump( 'HELLO');
add_filter( 'rocket_override_donotcachepage', '__return_true', PHP_INT_MAX );
add_filter( 'do_rocket_generate_caching_files', '__return_false' );
}
});
Binary file not shown.
16 changes: 16 additions & 0 deletions cache/wp-rocket-cache-purge-toolbar-link/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# WP Rocket | Toolbar Clear Cache Link

Adds a “Clear cache” link to the toolbar for users who can publish posts.

You can edit `WPROCKETHELPERS_CACHE_PURGE_TOOLBAR_LINK_CAPACITY` to define the user role you want the new toolbar link to be displayed for. Default: `publish_posts` (Author)

Requires:
* PHP 5.4 or greater
* WP Rocket 3.0 or greater

To be used with:
* any setup where a “Clear cache” link should be available to non-admin users

Last tested with:
* WP Rocket 3.0.x
* WordPress 4.9.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Plugin Name: WP Rocket | Toolbar Clear Cache Link
* Description: Adds a “Clear cache” link to the toolbar for users who can publish posts.
* Plugin URI: https://github.com/wp-media/wp-rocket-helpers/tree/master/cache/wp-rocket-cache-purge-toolbar-link/
* Author: WP Rocket Support Team
* Author URI: http://wp-rocket.me/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright SAS WP MEDIA 2018
*/

namespace WP_Rocket\Helpers\cache\clear_cache_toolbar_link;

// Standard plugin security, keep this line in place.
defined( 'ABSPATH' ) or die();

/**
* The minimum capability for users to see the new Clear cache button in the
* toolbar is `publish_posts` which translates into the user role of “Author”.
*
* This makes sense in most cases, since a person who can’t publish new content
* usually won’t have to clear the cache.
*
* However, if a different capability or user role is required in your case,
* feel free to edit the following constant.
*/

// EDIT THIS TO CUSTOMIZE THE MINIMUM CAPACITY FOR THE NEW TOOLBAR LINK:
define( 'WPROCKETHELPERS_CACHE_PURGE_TOOLBAR_LINK_CAPACITY', 'publish_posts' );
// STOP EDITING.

/**
* Renders a “Clear cache” button in the toolbar.
*
* @author Caspar Hübinger
* @link https://developer.wordpress.org/reference/classes/wp_admin_bar/
* @param object $wp_admin_bar WP_Admin_Bar object
* @return object WP_Admin_Bar object
*/
function render( $wp_admin_bar ) {

$icon_html = '<span class="ab-icon dashicons-before dashicons-trash" style="top:2px;" aria-hidden="true"></span>';

$wp_admin_bar->add_menu([
'id' => 'wp-rocket-clear-cache-button',
'href' => wp_nonce_url( admin_url( 'admin-post.php?action=purge_cache&type=all' ), 'purge_cache_all' ),
// 'parent' => 'top-secondary', // uncomment to place next to user menu in secondary toolbar
'title' => $icon_html . esc_html__( 'Clear cache', 'rocket' ),
]);

return $wp_admin_bar;
}

/**
* Adds toolbar link for users with sufficient capabilities.
*
* @author Caspar Hübinger
*/
function maybe_add() {

// Not for WP Rocket admins since those already have a full toolbar menu.
if ( current_user_can( apply_filters( 'rocket_capacity', 'manage_options' ) ) ) {
return;
}

// Only for users with sufficient capabilities.
if ( ! current_user_can( WPROCKETHELPERS_CACHE_PURGE_TOOLBAR_LINK_CAPACITY ) ) {
return;
}

add_action( 'admin_bar_menu', __NAMESPACE__ . '\render',
200 // editing this priority will move the button within the toolbar
);
}
add_action( 'wp_rocket_loaded', __NAMESPACE__ . '\maybe_add' );
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function minify_compatibility( $url ) {
*/
function render_admin_notice() {

printf( '<div class="notice notice-error"><p></p></div>',
printf( '<div class="notice notice-error"><p>%s</p></div>',
__( '<strong>WP Rocket for DreamPress</strong> requires <strong>WP Rocket</strong> to be active. Please activate WP Rocket before activating this plugin.', 'wp-rocket-for-dreampress' )
);

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ function render_rewrite_rules( $marker ) {
add_filter( 'before_rocket_htaccess_rules', __NAMESPACE__ . '\render_rewrite_rules' );

/**
* Updates .htaccess, and regenerates config file.
* Updates .htaccess, regenerates WP Rocket config file.
*
* @return bool
* @author Caspar Hübinger
*/
function wp_rocket_htaccess_redirect_www_to_nonwww__housekeeping() {
function flush_wp_rocket() {

if ( ! function_exists( 'flush_rocket_htaccess' )
|| ! function_exists( 'rocket_generate_config_file' ) ) {
Expand All @@ -57,23 +57,20 @@ function wp_rocket_htaccess_redirect_www_to_nonwww__housekeeping() {

// Regenerate WP Rocket config file.
rocket_generate_config_file();

// Return a value for testing.
return true;
}
register_activation_hook( __FILE__, 'wp_rocket_htaccess_redirect_www_to_nonwww__housekeeping' );
register_activation_hook( __FILE__, __NAMESPACE__ . '\flush_wp_rocket' );

/**
* Removes plugin additions, updates .htaccess, and regenerates config file.
* Removes customizations, updates .htaccess, regenerates config file.
*
* @return bool
* @author Caspar Hübinger
*/
function wp_rocket_htaccess_redirect_www_to_nonwww__deactivate() {
function deactivate() {

// We don’t want .htaccess rules added upon deactivation. Remove!
remove_filter( 'before_rocket_htaccess_rules', 'wp_rocket_htaccess_redirect_www_to_nonwww' );
// Remove all functionality added above.
remove_filter( 'before_rocket_htaccess_rules', __NAMESPACE__ . '\render_rewrite_rules' );

// Flush .htaccess rules and regenerate WP Rocket config file.
wp_rocket_htaccess_redirect_www_to_nonwww__housekeeping();
// Flush .htaccess rules, and regenerate WP Rocket config file.
flush_wp_rocket();
}
register_deactivation_hook( __FILE__, 'wp_rocket_htaccess_redirect_www_to_nonwww__deactivate' );
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\deactivate' );
Binary file not shown.
11 changes: 4 additions & 7 deletions various/wp-rocket-custom-preload-intervals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ Adds a custom set of sitemap preload intervals in WP Rocket’s Preload settings

📝&#160;&#160;**Manual code edit required before use!**

## Add a custom intervals for sitemap preload
As an example we have added additional values of 3,4, and 5 second intervals. Edit the example, or add your own applying this scheme:
## Add a custom interval for sitemap preload

```
(integer) milliseconds => (string) human-readable label
```
Edit `WPROCKETHELPERS_PRELOAD_INTERVAL_IN_SECONDS` to reflect the number of seconds you wish to apply as your preload interval. (No decimals allowed!)

To be used with:
* any setup

Last tested with:
* WP Rocket 2.8.x
* WordPress 4.6.x
* WP Rocket 3.0.x
* WordPress 4.9.x
Loading

0 comments on commit 4a89228

Please sign in to comment.