Last active
February 26, 2016 23:25
-
-
Save soderlind/a27527c3ed6545f594d7 to your computer and use it in GitHub Desktop.
Testing WP CLI and WP Gears. WP Gears integrates Gearman with WordPress for asynchronous task running. WP Gears is available at https://github.com/10up/WP-Gears
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: WP CLI GEARS | |
Version: 0.0.2 | |
Description: Testing WP CLI and WP Gears | |
Author: Per Soderlind | |
Author URI: https://soderlind.no | |
Plugin URI: https://gist.github.com/soderlind/a27527c3ed6545f594d7 | |
License: GPL | |
*/ | |
function wp_cli_gears_plugin_init() { | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
/** | |
* Test WP Gears using WP CLI | |
*/ | |
class WP_Gears_Command extends WP_CLI_Command { | |
/** | |
* Test wp cli and wp gears | |
* ## OPTIONS | |
* | |
* <file> | |
* : Path to output file | |
* | |
* @synopsis <file> | |
*/ | |
function mytest( $args, $assoc_args ) { | |
list( $file ) = $args; | |
wp_async_task_add( 'wp_cli_gears_worker', array( 'file' => $file ) ); | |
WP_CLI::success( 'Done!' ); | |
} | |
} | |
WP_CLI::add_command( 'wp-gears-test', 'WP_Gears_Command' ); | |
} | |
} | |
if ( defined( 'WP_CLI' ) ) { | |
add_action( 'plugins_loaded', 'wp_cli_gears_plugin_init' ); | |
} | |
/** | |
* Worker Class, NOTE, this class is must be outside "if ( defined('WP_CLI') && WP_CLI ) {}" | |
*/ | |
class WP_Gears_Command_Worker { | |
/** | |
* Worker callback function | |
* | |
* @param array $args parameters sent to the function. added using wp_async_task_add() | |
*/ | |
static function wp_cli_gears_worker_callback( $args ) { | |
//must have write access to $args['file'] | |
file_put_contents( $args['file'], 'Time: ' . time() . "\n", FILE_APPEND ); | |
} | |
} | |
add_action( 'wp_cli_gears_worker', array( 'WP_Gears_Command_Worker', 'wp_cli_gears_worker_callback' ) ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment