Giter Site home page Giter Site logo

gdarko / wp-batch-processing Goto Github PK

View Code? Open in Web Editor NEW
108.0 14.0 26.0 62 KB

๐Ÿšˆ Easily process large batches of data in WordPress. Provide the data, setup the processing procedure, run the batch processor from the admin dashboard. Profit.

JavaScript 8.03% PHP 91.97%

wp-batch-processing's Introduction

WP Batch Processing

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

Installation

There are two ways to install this library:

  1. Install this library as a plugin. It doesn't require anything from below or,

  2. Install it as a composer package (read the notes bellow)

composer require gdarko/wp-batch-processing
WP_Batch_Processor::boot();

Note: The boot() method should be called only if you install the plugin via composer somewhere in your plugin or theme.

Note: If using composer, the library will attempt to find its path, however if you see messed up screen it means that it was unable to find the stylesheet/JS files and you will need to define them manually before boot() method.

// Manually define the path constants to eliminate
// possible errors when resolving the paths and also
// include trailing slash at the end.

if ( ! defined('WP_BP_PATH')) {
    define('WP_BP_PATH', '/path/to/wp-content/plugins/your-plugin/libraries/wp-batch-processing/');
}

if ( ! defined('WP_BP_URL')) {
    define('WP_BP_URL', 'https://site.com/wp-content/plugins/your-plugin/libraries/wp-batch-processing/');
}

WP_Batch_Processor::boot();

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

  • $id - Identifies the batch (must be unique),
  • $title - Shown in the admin area,
  • setup() - Method that you use it to fed your data with WP_Batch_Item instances,
  • process(WP_Batch_Item $item) - Method that will be used to process each next item in the batch
if ( class_exists( 'WP_Batch' ) ) {

	/**
	 * Class MY_Example_Batch
	 */
	class MY_Example_Batch extends WP_Batch {

		/**
		 * Unique identifier of each batch
		 * @var string
		 */
		public $id = 'email_post_authors';

		/**
		 * Describe the batch
		 * @var string
		 */
		public $title = 'Email Post Authors';

		/**
		 * To setup the batch data use the push() method to add WP_Batch_Item instances to the queue.
		 *
		 * Note: If the operation of obtaining data is expensive, cache it to avoid slowdowns.
		 *
		 * @return void
		 */
		public function setup() {

			$users = get_users( array(
				'number' => '40',
				'role'   => 'author',
			) );

			foreach ( $users as $user ) {
				$this->push( new WP_Batch_Item( $user->ID, array( 'author_id' => $user->ID ) ) );
			}
		}

		/**
		 * Handles processing of batch item. One at a time.
		 *
		 * In order to work it correctly you must return values as follows:
		 *
		 * - TRUE - If the item was processed successfully.
		 * - WP_Error instance - If there was an error. Add message to display it in the admin area.
		 *
		 * @param WP_Batch_Item $item
		 *
		 * @return bool|\WP_Error
		 */
		public function process( $item ) {

			// Retrieve the custom data
			$author_id = $item->get_value( 'author_id' );

			// Return WP_Error if the item processing failed (In our case we simply skip author with user id 5)
			if ( $author_id == 5 ) {
				return new WP_Error( 302, "Author skipped" );
			}

			// Do the expensive processing here.
			// ...

			// Return true if the item processing is successful.
			return true;
		}
		
		/**
		 * Called when specific process is finished (all items were processed).
		 * This method can be overriden in the process class.
		 * @return void
		 */
		public function finish() {
			// Do something after process is finished.
			// You have $this->items, etc.
		}
	}
}

After creating the class, class instance needs to be registered in order to be available in the batches list in the admin area.

/**
 * Initialize the batches.
 */
function wp_batch_processing_init() {
    $batch = new MY_Example_Batch();
    WP_Batch_Processor::get_instance()->register( $batch );
}
add_action( 'wp_batch_processing_init', 'wp_batch_processing_init', 15, 1 );

That's it.

Filters and Actions

Set delay between processing items. Default is 0 (no delay)

function wp_bp_my_custom_delay($delay) {
   return 2; // in seconds
}
add_filter('wp_batch_processing_delay', 'wp_bp_my_custom_delay', 10, 1);

Example use cases

The tool can be used in many different ways. For example

  • Importing data
  • Downloading data
  • Emailing
  • Database modifications

Contribute

If you notice a bug or you want to propose improvements feel free to create a pull request!

License

The plugin is licensed under GPL v2

Copyright (C) 2021 Darko Gjorgjijoski (https://darkog.com)

This file is part of WP Batch Processing

WP Batch Processing is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

WP Batch Processing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
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/>.

wp-batch-processing's People

Contributors

d4mation avatar gdarko avatar graham73may avatar kknoer avatar varunsridharan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wp-batch-processing's Issues

Working with post_type => 'shop_order' ?

Hey, I'm greatly interested in your project, since I have orders from a WooCommerce shop to batch process, but I can't make it work...
When defining the setup() method, neither $orders = get_posts([ "post_type" => "shop_order" ]); nor $orders = get_orders(); seem to retrieve anything.
error_log( print_r( $orders ) ); always prints an empty array...
I'm probably doing something wrong, but what? Is there any other info I can give you to help me debug that, if the problem comes from me?
Thanks by advance!

Rerun batch project when finished?

Hello, I am using this plugin and it is really awesome. Congratulations!

I just wonder if it is possible to rerun a batch process after it has finished? For example, I need to calculate some stats from time to time - I did it once using this but I dont know how to rerun them again. I guess I could add something in the finish method to clean the run but not sure how to do it. It'd be nice if that can be done automatically or something like that.

Thanks!

How exactly to use this plugin?

Hi, I am sorry if you think you've given all the instructions in the plugin README.md or in this article; https://medium.com/@gdarko/processing-batch-data-in-wordpress-7cc5ae5720f0

But I am not able to figure out where exactly to place my own Batch class definition? I am placing it in my own custom plugin and it doesn't get recognized when I go under "Wordpress" > "Batches".

I am new to Wordpress let alone this plugin. Please help me understand how to implement this where exactly do I need to put my batch definition and will your plugin recognize my Batch?

My Plugins structure:
myplugin/myplugin.php

myplugin.php code:

if (class_exists('WP_Batch')) {
    class Update_Posts_Status extends WP_Batch {
        public $id = 'update_files_path';
        public $title = 'Updates files path';
        public function setup() {
            $posts = get_posts();
            foreach ($posts as $post) {
                    $this->push(new WP_Batch_Item($post->ID, ['post_id' => $post->ID]));
            }
        }
        public function process($item) {
            $post_id = $item->get_value('post_id');
            $post = get_post($post_id);
            $post_update = [
                'post_id' => $post_id,
                'post_status' => 'publish'
            ];
            wp_update_post( $post_update );
            return true;
        }
        public function finish() {
            //
        }
    }
    function wp_batch_processing_init() {
	$batch = new Update_Posts_Status();
	WP_Batch_Processor::get_instance()->register($batch);
    }
    add_action('wp_batch_processing_init', 'wp_batch_processing_init', 15, 1);
}

Thanks

Cancel a batch

It would be nice to be able to cancel an import and restart even if the current batch is not complete.

Hook into the end of particular the process

Thank you very much for the great plugin - it helps a lot in many cases.

Cann't figure out if there is a way to hook into the end of a particular process - for example to update some options in DB. Could it be done?

If there is no one it would be great to have the ability to do that.

Feature request: Time estimates

Hi,

Really love this plugin, the only thing that stood out to me about the admin interface was it didn't have any time estimate for how long the process may take (it's a feature I guess I'm used to from other large batch tasks like Relevanssi's indexer). How else do I know how many cups of coffee I have time to go and make? :D

image

e.g.

image

Thanks!

How run it with wp cron?

Hi,

I need to run the batch process once daily. How can I add it with a cron job? Is there a hook that trigger running the batch process? If yes, can I trigger that with a cron job?

The Batch keep running over 100%

Hi,

I am trying with simple delete post like this, but the batch keep running even reach 100%...

Please check that...!

public function setup() {

		$posts = get_posts( array(
			'numberposts'      => 10,
			'post_type'   => 'it24-serial-number'
		) );

		foreach ( $posts as $post ) {
			$this->push( new WP_Batch_Item( $post->ID, array( 'post_id' => $post->ID ) ) );
		}
	}

public function process( $item ) {

		// Retrieve the custom data
		$post_id = $item->get_value( 'post_id' );
		

		// Return WP_Error if the item processing failed (In our case we simply skip author with user id 5)
		// if ( $post_id != 5 ) {
		// 	return new WP_Error( 302, "ID".$post_id );
		// }

		// Do the expensive processing here. eg. Sending email.
		// ...
		**wp_trash_post($post_id);**
		
		// Return true if the item processing is successful.
		return true;
	}

	/**
	 * Called when specific process is finished (all items were processed).
	 * This method can be overriden in the process class.
	 * @return void
	 */
	public function finish() {
		// Do something after process is finished.
		// You have $this->items, or other data you can set.
	}

}

Make your script standalone

Your script is working great, thanks for awesome work :)

Just a little suggestion, make your script standalone so it can be used with other plugins. Developer should have option to remove this check is_batch_runner_screen( $action = null ) or can override it completely.

Great Work :-)

Hi There

This is a very great work and thanks for sharing with us.
I would like to know if this can be used as a library instead of using as a plugin

Is there a hook to start a batch [help]

First of all, love your work on this plugin, its been very helpful...

But I was wondering, is there a hook or something that I can use, to start an batch programmatically ?

I want to make an cron job, that runs a batch every night.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.