-
-
Save yipeecaiey/78f29a21e5c4cdbfd77cab37a7c83e7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composer require a5hleyrich/wp-queue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostSaveJob extends \WP_Queue\Job | |
{ | |
protected $postId; | |
public function __construct( $postId ) | |
{ | |
$this->postId = $postId; | |
} | |
public function handle() | |
{ | |
$post = get_post($this->postId ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostSaveJob extends \WP_Queue\Job | |
{ | |
protected $postId; | |
public function __construct( $postId ) | |
{ | |
$this->postId = $postId; | |
} | |
public function handle() { | |
$post = get_post( $this->postId ); | |
if( ! in_array( get_post_type($post), [ 'post', 'page' ]) ){ | |
return; | |
} | |
$controller = new \WP_REST_Posts_Controller( 'post' ); | |
$route = 'wp/v2/posts/' . $this->postId; | |
$request = new \WP_REST_Request('GET', $route); | |
$request->set_param( 'id', $this->postId ); | |
$response = $controller->get_item($request ); | |
$name = sprintf( '%s/posts/%s.json', __DIR__, $post->post_name ); | |
file_put_contents( $name, json_encode( $response ) ); | |
return $name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$fileName = ( new \calderawp\WordPressPlugin\Jobs\PostSaveJob( $postId ) )->handle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostSaveJobTest extends TestCase { | |
public function testSaves(){ | |
$postName = 'foo'; | |
$postId = wp_insert_post( ['post_content' => 'fff', 'post_type' => 'post', 'post_status' => 'publish', 'post_name' => $postName ] ); | |
$fileName = ( new \calderawp\WordPressPlugin\Jobs\PostSaveJob( $postId ) )->handle(); | |
$this->assertTrue( file_exists( $fileName ) ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'save_post', function( $postId ) { | |
wp_queue()->push( new PostSaveJob( $postId ) ); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'save_post', function( $postId ) { | |
wp_queue()->push( new PostSaveJob( $postId ), 600 ); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'plugins_loaded', function(){ | |
wp_queue()->cron(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'wp_queue_default_connection', function() { | |
return 'sync'; | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function workQueue( WP_REST_Request $request ){ | |
//if set up within a class | |
//public static function workQueue( WP_REST_Request $request ){ | |
//set up the parameters | |
$jobs = $request->get_param( 'jobs' ) ?: 10 ; | |
$attemptsPerJob = $request->get_param( 'attemptsPerJob') ?: 3 ; | |
//Get a properly configured queue | |
$queue = wp_queue(); | |
//Get the worker for the queue | |
$worker = $queue->worker($attemptsPerJob); | |
//Loop through jobs in queue until we've done the number passed in $jobs | |
// or no jobs left to run | |
$totalJobs = 0; | |
while( $totalJobs < $jobs && $worker->process() ) { | |
error_log('Processed a WP_Queue Job'); | |
$totalJobs++; | |
} | |
//get pending jobs count (choose the appropriate instance type) | |
$instance = new \WP_Queue\Connections\DatabaseConnection( $GLOBALS['wpdb'] ); | |
//$instance = new \WP_Queue\Connections\RedisConnection(); | |
//$instance = new \WP_Queue\Connections\SyncConnection(); | |
$pendingJobs = $instance->jobs(); | |
//Return something useful | |
return rest_ensure_response( [ | |
'processed_jobs' => $totalJobs, | |
'pending_jobs' => $pendingJobs | |
] ); | |
} | |
add_action( 'rest_api_init', function(){ | |
register_rest_route( 'hi-roy', 'queue/run', [ | |
'methods' => ['POST', 'GET' ], | |
'callback' => 'workQueue' | |
//if set up within a class | |
//'callback' => [ $this, 'workQueue' ] | |
]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment