2019-02-22 19:34:18 +00:00
|
|
|
<?php
|
2019-02-25 21:07:42 +00:00
|
|
|
/**
|
|
|
|
* Marketplace suggestions updater
|
|
|
|
*
|
|
|
|
* Uses WC_Queue to ensure marketplace suggestions data is up to date and cached locally.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Classes
|
|
|
|
* @since 3.6.0
|
|
|
|
*/
|
|
|
|
|
2019-02-22 19:34:18 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marketplace Suggestions Updater
|
|
|
|
*/
|
|
|
|
class WC_Marketplace_Updater {
|
|
|
|
|
2019-02-25 21:07:42 +00:00
|
|
|
/**
|
|
|
|
* Setup.
|
|
|
|
*/
|
2019-02-22 19:34:18 +00:00
|
|
|
public static function load() {
|
|
|
|
add_action( 'init', array( __CLASS__, 'init' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule events and hook appropriate actions.
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
add_action( 'woocommerce_update_marketplace_suggestions', array( __CLASS__, 'update_marketplace_suggestions' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches new marketplace data, updates wc_marketplace_suggestions.
|
|
|
|
*/
|
|
|
|
public static function update_marketplace_suggestions() {
|
2019-02-25 21:07:42 +00:00
|
|
|
$data = get_option(
|
|
|
|
'woocommerce_marketplace_suggestions',
|
|
|
|
array(
|
|
|
|
'suggestions' => array(),
|
|
|
|
'updated' => time(),
|
|
|
|
)
|
|
|
|
);
|
2019-02-22 19:34:18 +00:00
|
|
|
|
|
|
|
$data['updated'] = time();
|
|
|
|
|
2019-02-27 09:26:14 +00:00
|
|
|
$url = 'https://woocommerce.com/wp-json/wccom/marketplace-suggestions/1.0/suggestions.json';
|
2019-02-25 21:07:42 +00:00
|
|
|
$request = wp_safe_remote_get( $url );
|
2019-02-22 19:34:18 +00:00
|
|
|
|
|
|
|
if ( is_wp_error( $request ) ) {
|
|
|
|
self::retry();
|
|
|
|
return update_option( 'woocommerce_marketplace_suggestions', $data, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
$body = wp_remote_retrieve_body( $request );
|
|
|
|
if ( empty( $body ) ) {
|
|
|
|
self::retry();
|
|
|
|
return update_option( 'woocommerce_marketplace_suggestions', $data, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
$body = json_decode( $body, true );
|
|
|
|
if ( empty( $body ) || ! is_array( $body ) ) {
|
|
|
|
self::retry();
|
|
|
|
return update_option( 'woocommerce_marketplace_suggestions', $data, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['suggestions'] = $body;
|
|
|
|
return update_option( 'woocommerce_marketplace_suggestions', $data, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used when an error has occured when fetching suggestions.
|
|
|
|
* Re-schedules the job earlier than the main weekly one.
|
|
|
|
*/
|
|
|
|
public static function retry() {
|
2019-04-24 15:28:28 +00:00
|
|
|
WC()->queue()->cancel_all( 'woocommerce_update_marketplace_suggestions' );
|
2019-02-22 19:34:18 +00:00
|
|
|
WC()->queue()->schedule_single( time() + DAY_IN_SECONDS, 'woocommerce_update_marketplace_suggestions' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WC_Marketplace_Updater::load();
|