From a09689a7693cb8e931c336afb1625d077a777b8f Mon Sep 17 00:00:00 2001 From: William Patton Date: Fri, 3 Nov 2017 14:29:58 +0000 Subject: [PATCH 1/3] Add basic 'on' and 'off' functions to toggle caching via WP-CLI --- inc/class-sc-wp-cli.php | 152 ++++++++++++++++++++++++++++++++++++++++ simple-cache.php | 7 +- 2 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 inc/class-sc-wp-cli.php diff --git a/inc/class-sc-wp-cli.php b/inc/class-sc-wp-cli.php new file mode 100644 index 0000000..0d3a63a --- /dev/null +++ b/inc/class-sc-wp-cli.php @@ -0,0 +1,152 @@ +] + * : (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=