From 3918214727d36fea32fc70c0ee5440c204d8bc30 Mon Sep 17 00:00:00 2001 From: Alex Standiford Date: Tue, 23 Nov 2021 08:21:27 -0600 Subject: [PATCH] Corrects composer.json --- batch-tasks.php | 67 ----------------- bootstrap.php | 72 +++++++++++++++++++ composer.json | 3 +- lib/{abstracts => Abstracts}/Batch_Task.php | 22 +++--- .../Batch_Task_Instance.php | 4 +- lib/{loaders => Loaders}/Batch_Tasks.php | 13 ++-- templates/batch/notice.php | 2 +- 7 files changed, 96 insertions(+), 87 deletions(-) delete mode 100644 batch-tasks.php create mode 100644 bootstrap.php rename lib/{abstracts => Abstracts}/Batch_Task.php (93%) rename lib/{factories => Factories}/Batch_Task_Instance.php (93%) rename lib/{loaders => Loaders}/Batch_Tasks.php (78%) diff --git a/batch-tasks.php b/batch-tasks.php deleted file mode 100644 index 15a39fe..0000000 --- a/batch-tasks.php +++ /dev/null @@ -1,67 +0,0 @@ - function ( Underpin $plugin ) { - $class = get_class( $plugin ); - - if ( ! defined( 'UNDERPIN_BATCH_TASKS_ROOT_DIR' ) ) { - define( 'UNDERPIN_BATCH_TASKS_ROOT_DIR', plugin_dir_path( __FILE__ ) ); - } - - require_once( UNDERPIN_BATCH_TASKS_ROOT_DIR . 'lib/loaders/Batch_Tasks.php' ); - require_once( UNDERPIN_BATCH_TASKS_ROOT_DIR . 'lib/abstracts/Batch_Task.php' ); - require_once( UNDERPIN_BATCH_TASKS_ROOT_DIR . 'lib/factories/Batch_Task_Instance.php' ); - - // Register the loader - $plugin->loaders()->add( 'batch_tasks', [ - 'class' => 'Underpin_Batch_Tasks\Loaders\Batch_Tasks', - ] ); - - // Register core-specific items. - if ( 'Underpin\Underpin' === $class ) { - $dir_url = plugin_dir_url( __FILE__ ); - - // Register the batch JS - $plugin->scripts()->add( 'batch', [ - 'class' => 'Underpin_Scripts\Factories\Script_Instance', - 'args' => [ - [ - 'handle' => 'underpin_batch', - 'deps' => [ 'jquery' ], - 'description' => 'Script that handles batch tasks.', - 'name' => "Batch Task Runner Script", - 'in_footer' => true, - 'src' => $dir_url . 'assets/js/build/batch.min.js', - 'version' => '1.0.0', - ], - ], - ] ); - - // Localize the ajax URL on the batch JS, if it was registered successfully. - if ( ! is_wp_error( underpin()->scripts()->get( 'batch' ) ) ) { - underpin()->scripts()->get( 'batch' )->set_param( 'ajaxUrl', admin_url( 'admin-ajax.php' ) ); - } - - // Register the batch stylesheet - $plugin->styles()->add( 'batch', [ - 'class' => 'Underpin_Styles\Factories\Style_Instance', - 'args' => [ - [ - 'handle' => 'underpin_batch', - 'description' => 'Styles for batch tasks.', - 'name' => "Batch Task Runner Styles", - 'src' => $dir_url . 'assets/css/build/batchStyle.min.css', - ], - ], - ] ); - } - }, -] ) ); \ No newline at end of file diff --git a/bootstrap.php b/bootstrap.php new file mode 100644 index 0000000..e599689 --- /dev/null +++ b/bootstrap.php @@ -0,0 +1,72 @@ + 'Underpin\Batch_Tasks', + 'text_domain' => 'underpin-batch-tasks', + 'minimum_php_version' => '7.0', + 'minimum_wp_version' => '5.1', + 'version' => '1.0.0', + ] )->get( __FILE__ ); +} + +//set up handler +batch_task_handler(); + +// Enqueue scripts and styles to batch task. +Underpin::attach( 'setup', new \Underpin\Factories\Observer( 'batch_tasks', [ + 'update' => function ( Underpin $plugin ) { + + // Register core-specific items. + if ( $plugin->file() === __FILE__ ) { + $dir_url = plugin_dir_url( __FILE__ ); + + // Register the batch JS + $plugin->scripts()->add( 'batch', [ + 'class' => 'Underpin\Scripts\Factories\Script_Instance', + 'args' => [ + [ + 'handle' => 'underpin_batch', + 'deps' => [ 'jquery' ], + 'description' => 'Script that handles batch tasks.', + 'name' => "Batch Task Runner Script", + 'in_footer' => true, + 'src' => $plugin->url() . 'assets/js/build/batch.min.js', + 'version' => '1.0.0', + ], + ], + ] ); + + // Localize the ajax URL on the batch JS, if it was registered successfully. + if ( ! is_wp_error( $plugin->scripts()->get( 'batch' ) ) ) { + $plugin->scripts()->get( 'batch' )->set_param( 'ajaxUrl', admin_url( 'admin-ajax.php' ) ); + } + + // Register the batch stylesheet + $plugin->styles()->add( 'batch', [ + 'class' => 'Underpin\Styles\Factories\Style_Instance', + 'args' => [ + [ + 'handle' => 'underpin_batch', + 'description' => 'Styles for batch tasks.', + 'name' => "Batch Task Runner Styles", + 'src' => $dir_url . 'assets/css/build/batchStyle.min.css', + ], + ], + ] ); + } + } +] ) ); + +// Add this loader. +Underpin::attach( 'setup', new \Underpin\Factories\Observers\Loader( 'batch_tasks', [ + 'class' => 'Underpin\Batch_Tasks\Loaders\Batch_Tasks', +] ) ); diff --git a/composer.json b/composer.json index 1cb46fb..bd7eee7 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,9 @@ "underpin/style-loader": "^1.0" }, "autoload": { + "psr-4": {"Underpin\\Batch_Tasks\\": "lib/"}, "files": [ - "batch-tasks.php" + "bootstrap.php" ] } } diff --git a/lib/abstracts/Batch_Task.php b/lib/Abstracts/Batch_Task.php similarity index 93% rename from lib/abstracts/Batch_Task.php rename to lib/Abstracts/Batch_Task.php index 7ad76e4..b6ab74a 100644 --- a/lib/abstracts/Batch_Task.php +++ b/lib/Abstracts/Batch_Task.php @@ -7,12 +7,14 @@ */ -namespace Underpin_Batch_Tasks\Abstracts; +namespace Underpin\Batch_Tasks\Abstracts; +use Underpin\Loaders\Logger; use Underpin\Traits\Templates; use WP_Error; -use function Underpin\underpin; +use function Underpin\Batch_Tasks\batch_task_handler; + if ( ! defined( 'ABSPATH' ) ) { exit; @@ -180,7 +182,7 @@ public function run( $current_tally ) { $this->finish_process( $current_tally ); - underpin()->logger()->log( + Logger::log( 'notice', 'batch_action_complete', 'The batch action called ' . $this->name . ' is complete.', @@ -193,12 +195,12 @@ public function run( $current_tally ) { $status = $this->task( $current_tally, $i ); if ( is_wp_error( $status ) ) { - underpin()->logger()->log_wp_error( 'batch_error', $status ); + Logger::log_wp_error( 'batch_error', $status ); // Bail early if we're supposed to stop when an error occurs. if ( true === $this->stop_on_error ) { - underpin()->logger()->log( + Logger::log( 'warning', 'batch_action_stopped_early', 'The batch action called ' . $this->name . ' stopped early because of an error.', @@ -240,7 +242,7 @@ protected function is_done( $current_tally ) { protected function is_valid() { if ( ! current_user_can( $this->capability ) ) { - return underpin()->logger()->log_as_error( + return Logger::log_as_error( 'batch_error', 'batch_task_invalid_user_permissions', 'The specified user does not have the permission to run this task' @@ -280,16 +282,16 @@ public function render_callback() { $batch_params = [ 'total_items' => $this->total_items ]; - underpin()->scripts()->set_param( 'batch', $this->batch_id, $batch_params ); - underpin()->scripts()->enqueue( 'batch' ); - underpin()->styles()->enqueue( 'batch' ); + batch_task_handler()->scripts()->set_param( 'batch', $this->batch_id, $batch_params ); + batch_task_handler()->scripts()->enqueue( 'batch' ); + batch_task_handler()->styles()->enqueue( 'batch' ); echo $this->get_template( 'notice', [ 'batch_id' => $this->batch_id, 'message' => $this->notice_message, 'button_text' => $this->button_text, ] ); - underpin()->logger()->log( + Logger::log( 'notice', 'batch_task_enqueued', 'A batch task was enqueued.', diff --git a/lib/factories/Batch_Task_Instance.php b/lib/Factories/Batch_Task_Instance.php similarity index 93% rename from lib/factories/Batch_Task_Instance.php rename to lib/Factories/Batch_Task_Instance.php index ac1e579..b395256 100644 --- a/lib/factories/Batch_Task_Instance.php +++ b/lib/Factories/Batch_Task_Instance.php @@ -7,10 +7,10 @@ */ -namespace Underpin_Batch_Tasks\Factories; +namespace Underpin\Batch_Tasks\Factories; -use Underpin_Batch_Tasks\Abstracts\Batch_Task; +use Underpin\Batch_Tasks\Abstracts\Batch_Task; use Underpin\Traits\Instance_Setter; if ( ! defined( 'ABSPATH' ) ) { diff --git a/lib/loaders/Batch_Tasks.php b/lib/Loaders/Batch_Tasks.php similarity index 78% rename from lib/loaders/Batch_Tasks.php rename to lib/Loaders/Batch_Tasks.php index 7ff8c42..92fb50b 100644 --- a/lib/loaders/Batch_Tasks.php +++ b/lib/Loaders/Batch_Tasks.php @@ -7,12 +7,13 @@ */ -namespace Underpin_Batch_Tasks\Loaders; +namespace Underpin\Batch_Tasks\Loaders; use Underpin\Abstracts\Registries\Object_Registry; -use Underpin_Batch_Tasks\Abstracts\Batch_Task; +use Underpin\Loaders\Logger; +use Underpin\Batch_Tasks\Abstracts\Batch_Task; use WP_Error; -use function Underpin\underpin; + if ( ! defined( 'ABSPATH' ) ) { exit; @@ -30,9 +31,9 @@ class Batch_Tasks extends Object_Registry { /** * @inheritDoc */ - protected $abstraction_class = 'Underpin_Batch_Tasks\Abstracts\Batch_Task'; + protected $abstraction_class = 'Underpin\Batch_Tasks\Abstracts\Batch_Task'; - protected $default_factory = 'Underpin_Batch_Tasks\Factories\Batch_Task_Instance'; + protected $default_factory = 'Underpin\Batch_Tasks\Factories\Batch_Task_Instance'; /** * @inheritDoc @@ -63,7 +64,7 @@ public function enqueue( $key ) { $batch_task = $this->get( $key ); if ( is_wp_error( $batch_task ) ) { - underpin()->logger()->log_wp_error( 'error', $batch_task ); + Logger::log_wp_error( 'error', $batch_task ); return $batch_task; } diff --git a/templates/batch/notice.php b/templates/batch/notice.php index a6b3317..4ca43b1 100644 --- a/templates/batch/notice.php +++ b/templates/batch/notice.php @@ -11,7 +11,7 @@ exit; } -if ( ! isset( $template ) || ! $template instanceof Underpin_Batch_Tasks\Abstracts\Batch_Task ) { +if ( ! isset( $template ) || ! $template instanceof Underpin\Batch_Tasks\Abstracts\Batch_Task ) { return; }