Skip to content

Commit

Permalink
Add composer support
Browse files Browse the repository at this point in the history
  • Loading branch information
gdarko committed Oct 23, 2021
1 parent 29a4a30 commit e1c25e4
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
vendor/*
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 <https://www.gnu.org/licenses/>.
```
```
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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/"
]
}
}
20 changes: 20 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions includes/class-batch-processor-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -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();
21 changes: 13 additions & 8 deletions wp-batch-processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -31,4 +36,4 @@
require_once 'includes/class-batch-processor-admin.php';

// Examples
// require_once 'examples/class-example-batch.php';
// require_once 'examples/class-example-batch.php';

0 comments on commit e1c25e4

Please sign in to comment.