diff --git a/inc/class-sc-advanced-cache.php b/inc/class-sc-advanced-cache.php index a47de64..d4c6955 100644 --- a/inc/class-sc-advanced-cache.php +++ b/inc/class-sc-advanced-cache.php @@ -252,6 +252,12 @@ public function toggle_caching( $status ) { global $wp_filesystem; + // when calling from WP-CLI $wp_filesystem is not defined. If nothing + // is in $wp_filesystem then instatiate it. + if ( ! $wp_filesystem ) { + WP_Filesystem(); + } + if ( defined( 'WP_CACHE' ) && WP_CACHE === $status ) { return; } diff --git a/inc/class-sc-wp-cli.php b/inc/class-sc-wp-cli.php new file mode 100644 index 0000000..26bc665 --- /dev/null +++ b/inc/class-sc-wp-cli.php @@ -0,0 +1,160 @@ +] + * : (optional) Option to toggle, either 'cache' or 'compression'. + * --- + * default: cache + * options: + * - cache + * - compression + * --- + * + * ## EXAMPLES + * + * wp simple-cache on + * wp simple-cache on --option=compression + * + * @when before_wp_load + */ + function on( $args, $assoc_args ) { + + // get the options array and assosiative args. + $sc_options = get_option( 'sc_simple_cache' ); + $option = $assoc_args['option']; + // an empty string to start as a message. + $message = ''; + switch ( $option ) { + case 'cache': + if ( 1 === $sc_options['enable_page_caching'] ) { + // cache is enabled aready set a message and do nothing. + $message = 'Cache already on'; + } else { + // set the new value and update the options array. + $sc_options['enable_page_caching'] = 1; + $updated = update_option( 'sc_simple_cache', $sc_options ); + SC_Advanced_Cache::factory()->toggle_caching( true ); + $message = 'Cache toggled on'; + } + break; + + case 'compression': + if ( 1 === $sc_options['enable_gzip_compression'] ) { + // compression is enabled aready set a message and do nothing. + $message = 'Compression already on'; + } else { + // set the new value and update the options array. + $sc_options['enable_gzip_compression'] = 1; + $updated = update_option( 'sc_simple_cache', $sc_options ); + $message = 'Compression toggled on'; + } + + break; + + default: + } + // if we have a successful $updated value then success... else error. + if ( isset( $updated ) && $updated ) { + WP_CLI::success( "$message" ); + } else { + WP_CLI::error( "$message" ); + } + + } + + /** + * Used to toggle either the cache or gzip compression options for + * simple-cache to '0' - IE 'OFF'. + * ## OPTIONS + * + * [--option=