From e1c25e40d7f418fffc6295070407a5f81091ac7c Mon Sep 17 00:00:00 2001 From: Darko Gjorgjijoski Date: Sat, 23 Oct 2021 09:00:43 +0000 Subject: [PATCH] Add composer support --- .gitignore | 2 ++ README.md | 37 +++++++++++++++++++++--- composer.json | 20 +++++++++++++ composer.lock | 20 +++++++++++++ includes/class-batch-processor-admin.php | 28 ++++++++++++++++-- wp-batch-processing.php | 21 +++++++++----- 6 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..508a6b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor/ +vendor/* diff --git a/README.md b/README.md index dec5d18..200b9ef 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,38 @@ # WP Batch Processing -WP Batch Processing is WordPress plugin for creating batches of data and processing the the data items one by one. It allows you to define a batch and to process the queued batch items one by one. There is also option to resume/continue later in case your internet connection goes down. +WP Batch Processing is WordPress plugin for creating batches of data and processing the data items one by one. It allows you to define a batch and to process the queued batch items one by one. There is also option to resume/continue later in case your internet connection goes down. ![Example](examples/processing.gif) +## Installation + +You can install this library as a plugin + +or via composer: + +``` +composer require gdarko/wp-batch-processing +``` + +**Note/Optional**: The plugin should be able to find its public path, however if you see messed up screen it means that it was unable to find the stylesheet/JS file and you will need to define them manually before including composer's autoload.php file. + +```php +if ( ! defined('WP_BP_PATH')) { + define('WP_BP_PATH', plugin_dir_path(__FILE__)); +} + +if ( ! defined('WP_BP_URL')) { + define('WP_BP_URL', plugin_dir_url(__FILE__)); +} + +// Before this statement. +require_once 'vendor/autoload.php'; + +``` + + +## How it works + To define a batch you just need to extend the class `WP_Batch` and later register it. Follow the examples below to learn how. The class provides the following attributes and methods @@ -111,7 +140,7 @@ add_action( 'wp_batch_processing_init', 'wp_batch_processing_init', 15, 1 ); That's it. -## Hooks +## Filters and Actions Set delay between processing items. Default is 0 (no delay) ```php @@ -141,7 +170,7 @@ If you notice a bug or you want to propose improvements feel free to create a pu The plugin is licensed under GPL v2 ``` -Copyright (C) 2019 Darko Gjorgjijoski (https://darkog.com) +Copyright (C) 2021 Darko Gjorgjijoski (https://darkog.com) This file is part of WP Batch Processing @@ -157,4 +186,4 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with WP Batch Processing. If not, see . -``` \ No newline at end of file +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d4b3b6c --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "gdarko/wp-batch-processing", + "description": "Easily process large batches of data in WordPress. Provide the data, setup the processing procedure, run the batch processor from the admin dashboard. Profit.", + "type": "library", + "require": { + "php": ">=5.3" + }, + "license": "GPL-2.0-or-later", + "authors": [ + { + "name": "Darko Gjorgjijoski", + "email": "dg@darkog.com" + } + ], + "autoload": { + "classmap": [ + "includes/" + ] + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..45e3c1e --- /dev/null +++ b/composer.lock @@ -0,0 +1,20 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "fac3bfb16f48815eb401dd55eefbde96", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.3" + }, + "platform-dev": [], + "plugin-api-version": "2.1.0" +} diff --git a/includes/class-batch-processor-admin.php b/includes/class-batch-processor-admin.php index 38aae4f..41e9127 100644 --- a/includes/class-batch-processor-admin.php +++ b/includes/class-batch-processor-admin.php @@ -35,6 +35,9 @@ class WP_Batch_Processor_Admin { * Kick-in the class */ protected function init() { + + $this->load_paths(); + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'init', array( $this, 'setup' ), 0 ); @@ -71,7 +74,8 @@ public function enqueue_scripts() { 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'wp-batch-processing' ), 'batch_id' => isset( $_GET['id'] ) ? $_GET['id'] : 0, - 'delay' => apply_filters( 'wp_batch_processing_delay', 0 ), // Set delay in seconds before processing the next item. Default 0. No delay. + 'delay' => apply_filters( 'wp_batch_processing_delay', 0 ), + // Set delay in seconds before processing the next item. Default 0. No delay. 'text' => array( 'processing' => __( 'Processing...', 'wp-batch-processing' ), 'start' => __( 'Start', 'wp-batch-processing' ), @@ -138,11 +142,31 @@ private function is_batch_runner_ajax() { private function is_batch_runner_screen( $action = null ) { $is_main_screen = isset( $_GET['page'] ) && $_GET['page'] === 'dg-batches'; if ( ! is_null( $action ) ) { - $is_main_screen = $is_main_screen && isset($_GET['action']) && $_GET['action'] === $action; + $is_main_screen = $is_main_screen && isset( $_GET['action'] ) && $_GET['action'] === $action; } return $is_main_screen; } + + /** + * Determine the library URL. + * Note: This won't work if the library is outside of the wp-content directory + * and also contains multiple 'wp-content' words in the path. + */ + private function load_paths() { + if ( ! defined( 'WP_BP_PATH' ) || ! defined( 'WP_BP_URL' ) ) { + $path = trailingslashit( dirname( __FILE__ ) ); + $content_dir = basename( untrailingslashit( WP_CONTENT_DIR ) ); + $library_uri = substr( strstr( trailingslashit( dirname( $path ) ), $content_dir ), strlen( $content_dir ) ); + $url = untrailingslashit( WP_CONTENT_URL ) . $library_uri; + if ( ! defined( 'WP_BP_PATH' ) ) { + define( 'WP_BP_PATH', $path ); + } + if ( ! defined( 'WP_BP_URL' ) ) { + define( 'WP_BP_URL', trailingslashit( $url ) ); + } + } + } } WP_Batch_Processor_Admin::get_instance(); diff --git a/wp-batch-processing.php b/wp-batch-processing.php index 9efc468..5b3fd01 100644 --- a/wp-batch-processing.php +++ b/wp-batch-processing.php @@ -3,23 +3,28 @@ Plugin Name: WP Batch Processing Plugin URI: https://github.com/gdarko/wp-batch-processing Description: Batch Processing for WordPress. Imagine you have to send custom emails to a lots of users based on some kind of logic. This plugin makes batch tasks easy. -Version: 1.0.1 +Version: 1.1.0 Author: Darko Gjorgjijoski Author URI: https://darkog.com License: GPL-2+ License URI: http://www.gnu.org/licenses/gpl-2.0.txt */ -if ( ! defined( 'ABSPATH' ) ) { - die; +if ( ! defined('ABSPATH')) { + die; } -if ( ! is_admin() ) { - return; +if ( ! is_admin()) { + return; } -define( 'WP_BP_PATH', plugin_dir_path( __FILE__ ) ); -define( 'WP_BP_URL', plugin_dir_url( __FILE__ ) ); +if ( ! defined('WP_BP_PATH')) { + define('WP_BP_PATH', plugin_dir_path(__FILE__)); +} + +if ( ! defined('WP_BP_URL')) { + define('WP_BP_URL', plugin_dir_url(__FILE__)); +} require_once 'includes/class-bp-helper.php'; require_once 'includes/class-bp-singleton.php'; @@ -31,4 +36,4 @@ require_once 'includes/class-batch-processor-admin.php'; // Examples -// require_once 'examples/class-example-batch.php'; \ No newline at end of file +// require_once 'examples/class-example-batch.php';