Skip to content

Instantly share code, notes, and snippets.

@nerrad
Last active May 10, 2016 14:16
Show Gist options
  • Save nerrad/b1c651bbe60466c064805216a9045756 to your computer and use it in GitHub Desktop.
Save nerrad/b1c651bbe60466c064805216a9045756 to your computer and use it in GitHub Desktop.
Demonstration of how a single action allowing for client code to send in actual callbacks for job execution.
<?php
/*
Plugin Name: WPGears unique bug proof of concept
Plugin URI:
Description: Hit any page with a `?do_job_test=1` param in the url and then check the options table. If you DON'T see
"random string maybe not" in the option then that proves the bug.
Author: Darren Ethier
Version: 1.0
*/
/**
* Demonstration class that wraps WPGears implementation so that client code can send actual callback to be used.
*/
class WPGearsAdapter {
public function add( $callback, $args ) {
$args['job_callback'] = $callback;
//$args['make_unique'] = true;
wp_async_task_add( 'wpgears_bug_test_callback', $args );
}
public static function doJob( $jobData ) {
//grab the registered callback from the jobData
if (
isset( $jobData['job_callback'] )
&& is_callable( $jobData['job_callback'] )
) {
//remove callback from jobData so it isn't sent to the callback as an argument.
$callback = $jobData['job_callback'];
unset( $jobData['job_callback'] );
return (bool) call_user_func_array( $callback, $jobData );
}
return false;
}
}
//set action for wpgears async request
add_action( 'wpgears_bug_test_callback', array( 'WPGearsAdapter', 'doJob' ) );
add_action( 'plugins_loaded', function() {
//sample jobs getting added in test kicked off by get
if ( isset( $_GET['do_job_test'] ) ) {
$adapter = new WPGearsAdapter();
$adapter->add( 'gears_option_update_test', array( 'value_to_use' => 'random string' ) );
//add another job for different callback and different values.
$adapter->add( 'gears_option_second_update_test', array( 'value_to_use' => 'maybe not' ) );
}
} );
function gears_option_update_test( $value_to_use ) {
$existing_value = get_option( 'gears_option_test', '' );
return update_option( 'gears_option_test', $existing_value . ' ' . $value_to_use );
}
function gears_option_second_update_test( $value_to_use ) {
$existing_value = get_option( 'gears_option_test', '' );
return update_option( 'gears_option_test', $existing_value . ' ' . $value_to_use );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment