Skip to content

Commit

Permalink
Adding option to force transient purge and related CLI command. fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
norcross committed Mar 5, 2017
1 parent 9518487 commit 1a13825
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### Version 0.2.3 - 2017/03/05
* Adding option to force transient purge
* Adding `clean` action to WP-CLI functions

#### Version 0.2.2 - 2016/12/07
* Adding WP-CLI support. props @markjaquith

Expand Down
30 changes: 22 additions & 8 deletions airplane-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Control loading of external files when developing locally
* Author: Andrew Norcross
* Author URI: http://andrewnorcross.com/
* Version: 0.2.2
* Version: 0.2.3
* Text Domain: airplane-mode
* Requires WP: 4.4
* Domain Path: languages
Expand Down Expand Up @@ -49,7 +49,7 @@

// Set our version if not already defined.
if ( ! defined( 'AIRMDE_VER' ) ) {
define( 'AIRMDE_VER', '0.2.2' );
define( 'AIRMDE_VER', '0.2.3' );
}

// Load our WP-CLI helper if that is defined and available.
Expand Down Expand Up @@ -542,6 +542,8 @@ public function toggle_css() {
* @return bool Whether the setting changed.
*/
public function set_mode( $mode = 'on' ) {

// Check what mode we're currently in, with "on" as a fallback.
if ( ! in_array( $mode, array( 'on', 'off' ) ) ) {
$mode = 'on';
}
Expand All @@ -552,6 +554,7 @@ public function set_mode( $mode = 'on' ) {
// Fire action to allow for functions to run on status change.
do_action( 'airplane_mode_status_change', $mode );

// And return the status we just set.
return $return;
}

Expand Down Expand Up @@ -728,19 +731,21 @@ public function prevent_auto_updates( $caps, $cap ) {

/**
* Check the new status after airplane mode has been enabled or
* disabled and purge related transients
* disabled and purge related transients.
*
* @param boolean $force Whether to force the purge.
*
* @return void
*/
public function purge_transients() {
public function purge_transients( $force = false ) {

// First check for the filter to avoid this action overall.
if ( false === $clear = apply_filters( 'airplane_mode_purge_transients', true ) ) {
if ( empty( $force ) && false === $clear = apply_filters( 'airplane_mode_purge_transients', true ) ) {
return;
}

// Purge the transients related to updates when disabled.
if ( ! $this->enabled() ) {
// Purge the transients related to updates when disabled or the force is called.
if ( ! $this->enabled() || ! empty( $force ) ) {
delete_site_transient( 'update_core' );
delete_site_transient( 'update_plugins' );
delete_site_transient( 'update_themes' );
Expand Down Expand Up @@ -1193,17 +1198,26 @@ public function count_http_requests() {
// End class.
}

} //end class_exists
} // End class_exists.

if ( ! class_exists( 'Airplane_Mode_WP_Error' ) ) {

/**
* Extend the WP_Error class to include our own.
*/
class Airplane_Mode_WP_Error extends WP_Error {

/**
* Get our error data and return it.
*
* @return string
*/
public function __tostring() {
$data = $this->get_error_data();
return $data['return'];
}

// End class.
}

}
Expand Down
14 changes: 14 additions & 0 deletions inc/wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ function status() {
$on = 'on' === get_site_option( 'airplane-mode' );
WP_CLI::success( $on ? __( 'Airplane mode is enabled', 'airplane-mode' ) : __( 'Airplane mode is disabled', 'airplane-mode' ) );
}

/**
* Purge the transients set from airplane mode.
*
* ## EXAMPLES
*
* wp airplane-mode clean
*
* @when after_wp_load
*/
function clean() {
Airplane_Mode_Core::getInstance()->purge_transients( true );
WP_CLI::success( __( 'Transients have been cleared', 'airplane-mode' ) );
}
}

WP_CLI::add_command( 'airplane-mode', 'Airplane_Mode_Command' );
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Donate link: https://andrewnorcross.com/donate
Tags: external calls, HTTP
Requires at least: 4.4
Tested up to: 4.7
Stable tag: 0.2.2
Stable tag: 0.2.3
License: MIT
License URI: http://norcross.mit-license.org/

Expand Down Expand Up @@ -45,6 +45,10 @@ Because you are a jet set developer who needs to work without internet.

== Changelog ==

= 0.2.3 - 2017/03/05
* Adding option to force transient purge
* Adding `clean` action to WP-CLI functions

= 0.2.2 - 2016/12/07
* Adding WP-CLI support. props @markjaquith

Expand Down

0 comments on commit 1a13825

Please sign in to comment.