Created
June 29, 2019 17:23
-
-
Save herewithme/28370640a62ae1d78e50ff5181c97b05 to your computer and use it in GitHub Desktop.
A WP-CLI command for refresh existing export (trigger|processing) from WP All Export Pro plugin
This file contains hidden or 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
<?php | |
/* | |
Plugin Name: WP-CLI - WP All Export Pro | |
Plugin URI: http://www.beapi.fr | |
Description: Add a WP-CLI command for refresh existing export (trigger|processing) from WP All Export Pro plugin | |
Author: Be API | |
Author URI: http://www.beapi.fr | |
Version: 0.1 | |
---- | |
Copyright 2019 - Amaury Balmer ([email protected]) | |
---- | |
*/ | |
if ( ! defined( 'WP_CLI' ) ) { | |
return false; | |
} | |
class WP_CLI_WP_All_Export_Pro extends WP_CLI_Command { | |
private function get_exports() { | |
global $wpdb; | |
$table_name = PMXE_Plugin::getInstance()->getTablePrefix() . 'exports'; | |
return $wpdb->get_col( 'SELECT id FROM ' . $table_name ); | |
} | |
/** | |
* Trigger existing export | |
*/ | |
public function trigger() { | |
$this->refresh( 'trigger' ); | |
} | |
/** | |
* Processing existing export | |
*/ | |
public function processing() { | |
$this->refresh( 'processing' ); | |
} | |
/** | |
* Generic method for existing export | |
* | |
* @param string $action trigger|processing | |
* | |
* @throws \WP_CLI\ExitException | |
*/ | |
private function refresh( $action = 'trigger' ) { | |
if ( ! class_exists( 'PMXE_Plugin' ) ) { | |
WP_CLI::error( 'WP All Export Pro plugin are not actived on this website.' ); | |
} | |
$cron_job_key = PMXE_Plugin::getInstance()->getOption( 'cron_job_key' ); | |
$exports = $this->get_exports(); | |
if ( empty( $exports ) ) { | |
WP_CLI::success( 'No export to refresh on this website.' ); | |
} | |
foreach ( $exports as $export_id ) { | |
$target_url = home_url( '/wp-load.php?export_key=' . $cron_job_key . '&export_id=' . $export_id . '&action=' . $action ); | |
WP_CLI::debug( $target_url ); | |
wp_remote_get( | |
$target_url, | |
array( | |
'timeout' => 120, | |
'blocking' => false, // ASYNC Call! | |
) | |
); | |
} | |
WP_CLI::success( sprintf( 'The %s action was successfully performed on the exports of this site!', $action ) ); | |
} | |
} | |
WP_CLI::add_command( 'wpaep', 'WP_CLI_WP_All_Export_Pro' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CRON install :
0 2 * * * wp wpaep trigger >/dev/null 2>&1
*/5 * * * * wp wpaep processing >/dev/null 2>&1
The first CRON runs once a day, the second every 5 minutes.
For multisite :
0 2 * * * wp site list --field=url --url="NETWORK" | xargs -I {} wp wpaep trigger --url={} >/dev/null 2>&1
*/5 * * * * wp site list --field=url --url="NETWORK" | xargs -I {} wp wpaep processing --url={} >/dev/null 2>&1