From 47a9534cabcd16c7af93565a0132fe6d8b31cc55 Mon Sep 17 00:00:00 2001 From: Ashley Rich Date: Tue, 12 Jan 2016 13:06:40 +0000 Subject: [PATCH] First commit --- README.md | 0 async-requests/class-example-request.php | 25 +++ .../class-example-process.php | 45 +++++ class-logger.php | 41 +++++ example-plugin.php | 154 ++++++++++++++++++ 5 files changed, 265 insertions(+) create mode 100644 README.md create mode 100644 async-requests/class-example-request.php create mode 100644 background-processes/class-example-process.php create mode 100644 class-logger.php create mode 100644 example-plugin.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/async-requests/class-example-request.php b/async-requests/class-example-request.php new file mode 100644 index 0000000..b602cfd --- /dev/null +++ b/async-requests/class-example-request.php @@ -0,0 +1,25 @@ +get_message( $_POST['name'] ); + + $this->really_long_running_task(); + $this->log( $message ); + } + +} \ No newline at end of file diff --git a/background-processes/class-example-process.php b/background-processes/class-example-process.php new file mode 100644 index 0000000..e2c083a --- /dev/null +++ b/background-processes/class-example-process.php @@ -0,0 +1,45 @@ +get_message( $item ); + + $this->really_long_running_task(); + $this->log( $message ); + + return false; + } + + /** + * Complete + * + * Override if applicable, but ensure that the below actions are + * performed, or, call parent::complete(). + */ + protected function complete() { + parent::complete(); + + // Show notice to user or perform some other arbitrary task... + } + +} \ No newline at end of file diff --git a/class-logger.php b/class-logger.php new file mode 100644 index 0000000..efc2d8b --- /dev/null +++ b/class-logger.php @@ -0,0 +1,41 @@ +process_single = new WP_Example_Request(); + $this->process_all = new WP_Example_Process(); + } + + /** + * Admin bar + * + * @param WP_Admin_Bar $wp_admin_bar + */ + public function admin_bar( $wp_admin_bar ) { + if ( ! current_user_can( 'manage_options' ) ) { + return; + } + + $wp_admin_bar->add_menu( array( + 'id' => 'example-plugin', + 'title' => __( 'Process', 'example-plugin' ), + 'href' => '#', + ) ); + + $wp_admin_bar->add_menu( array( + 'parent' => 'example-plugin', + 'id' => 'example-plugin-single', + 'title' => __( 'Single User', 'example-plugin' ), + 'href' => wp_nonce_url( admin_url( '?process=single'), 'process' ), + ) ); + + $wp_admin_bar->add_menu( array( + 'parent' => 'example-plugin', + 'id' => 'example-plugin-all', + 'title' => __( 'All Users', 'example-plugin' ), + 'href' => wp_nonce_url( admin_url( '?process=all'), 'process' ), + ) ); + } + + /** + * Process handler + */ + public function process_handler() { + if ( ! isset( $_GET['process'] ) || ! isset( $_GET['_wpnonce'] ) ) { + return; + } + + if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'process') ) { + return; + } + + if ( 'single' === $_GET['process'] ) { + $this->handle_single(); + } + + if ( 'all' === $_GET['process'] ) { + $this->handle_all(); + } + } + + /** + * Handle single + */ + protected function handle_single() { + $names = $this->get_names(); + $rand = array_rand( $names, 1 ); + $name = $names[ $rand ]; + + $this->process_single->data( array( 'name' => $name ) )->dispatch(); + } + + /** + * Handle all + */ + protected function handle_all() { + $names = $this->get_names(); + + foreach ( $names as $name ) { + $this->process_all->push_to_queue( $name ); + } + + $this->process_all->save()->dispatch(); + } + + /** + * Get names + * + * @return array + */ + protected function get_names() { + return array( + 'Grant Buel', + 'Bryon Pennywell', + 'Jarred Mccuiston', + 'Reynaldo Azcona', + 'Jarrett Pelc', + 'Blake Terrill', + 'Romeo Tiernan', + 'Marion Buckle', + 'Theodore Barley', + 'Carmine Hopple', + 'Suzi Rodrique', + 'Fran Velez', + 'Sherly Bolten', + 'Rossana Ohalloran', + 'Sonya Water', + 'Marget Bejarano', + 'Leslee Mans', + 'Fernanda Eldred', + 'Terina Calvo', + 'Dawn Partridge', + ); + } + +} + +new Example_Background_Processing(); \ No newline at end of file