1. Go to this page and download the library: Download moderntribe/square1-queues library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
moderntribe / square1-queues example snippets
class Example_Class {
private $queue;
public function __construct( \Tribe\Libs\Queues\Contracts\Queue $queue ) {
$this->queue = $queue;
}
}
declare(strict_types=1);
namespace Tribe\Project\Queues\Tasks;
use Tribe\Libs\Queues\Contracts\Task;
use Tribe\Project\Posts\Post_Fetcher;
/**
* An example of Queue Task using dependency injection
*/
class Cache_Slow_Query implements Task {
public const OPTION = 'tribe_latest_post_cache';
/**
* Example dependency injection: This object will be
* the service responsible for handling complex logic
* for this task. Moving the logic to a service object
* allows that functionality to be shared outside
* this task in case something else needs to consume
* it.
*/
private Post_Fetcher $post_fetcher;
/**
* @param \Tribe\Project\Posts\Post_Fetcher $post_fetcher This is a concrete class. PHP-DI knows to automatically
* inject the instance without any needed configuration.
*/
public function __construct( Post_Fetcher $post_fetcher ) {
$this->post_fetcher = $post_fetcher;
}
/**
* @param array $args This variable populated with the dynamic data you set
* when the Queue Task is dispatched.
*
* @example $queue->dispatch( Tribe\Project\Queues\Tasks\Cache_Slow_Query::class, [ 'my_custom_post_type' ] );
*
* @return bool
*/
public function handle( array $args ): bool {
// Create the $post_type variable via unpacking of $args[0]
[ $post_type ] = $args;
/**
* Fetch the posts from some very long and intensive query.
*
* @var \WP_Query $query
*/
$query = $this->post_fetcher->get_latest_posts( $post_type );
/*
* There are no posts, so return true to avoid placing this back in the queue to run
* again until posts are found.
*
* In this scenario, we'd rather just check that the next time this task is dispatched
* to the queue.
*/
if ( empty( $query->posts ) ) {
return true;
}
/*
* If for some reason our option update doesn't work, it'll automatically be placed back in the
* queue to try again.
*
* Some other service will query this option to display the posts instead of running the
* massive query above in real time.
*/
return update_option( self::OPTION, $query->posts, false );
}
}