Created
December 6, 2019 17:59
-
-
Save wpmudev-sls/d4f2b2b261c347d391059daace0592f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Hummingbird] WP schedule task to clear Assets Cache | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Adds a scheduled task to clear minified assets on daily basis and reports it via email | |
* Author: Alessandro Kaounas @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
die(); | |
} | |
if ( ! class_exists( 'WPMUDEV_Hummingbird_Scheduled_Cache_Clear' ) ) { | |
class WPMUDEV_Hummingbird_Scheduled_Cache_Clear { | |
private $notification = true; | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Hummingbird_Scheduled_Cache_Clear(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
$this->init(); | |
} | |
private function init(){ | |
if ( ! wp_next_scheduled( 'wpmudev_hb_clear_minified_assets' ) ) { | |
wp_schedule_event( time(), 'daily', array( $this, 'wpmudev_hb_clear_minified_assets' ) ); | |
} | |
add_action( 'wpmudev_hb_clear_minified_assets', array( $this, 'wpmudev_hb_clear_assets_cache' ) ); | |
} | |
public function wpmudev_hb_clear_assets_cache() { | |
$request = wp_remote_get( get_rest_url( NULL, 'hummingbird/v1/clear_cache/minify' ) ); | |
$title = "Hummingbird Cron Report [" . get_bloginfo() . "]"; | |
if( is_wp_error( $request ) ) { | |
if( $this->notification ){ | |
wp_mail( get_option('admin_email'), $title, sprintf( __( 'An error occured: %s', 'wpmudev_support' ), $request->get_error_message() ) ); | |
} | |
return false; // Bail early | |
} | |
$response = json_decode( wp_remote_retrieve_body( $request ), true ); | |
if( ! $response['message'] ){ | |
$content = sprintf( __( 'Minified assets cache clear status: %s', 'wpmudev_support' ), ( $response['cache_cleared'] ? '[Success]' : '[Failed]') ); | |
}else{ | |
$content = $response['message']; | |
} | |
if( $this->notification ){ | |
wp_mail( get_option('admin_email'), $title, $content ); | |
} | |
} | |
} | |
if ( ! function_exists( 'wpmudev_hummingbird_scheduled_cache_clear' ) ) { | |
function wpmudev_hummingbird_scheduled_cache_clear() { | |
return WPMUDEV_Hummingbird_Scheduled_Cache_Clear::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_hummingbird_scheduled_cache_clear', 99 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment