tidy up main marketplace suggestions module, make it a real class +
+ require it for admin only + fix issue with retrieving dismissed suggestions user meta value
This commit is contained in:
parent
5c1e20c7d1
commit
71250c30a2
|
@ -438,7 +438,7 @@ if ( ! class_exists( 'WC_Admin_Assets', false ) ) :
|
|||
array(
|
||||
'dismiss_suggestion_nonce' => wp_create_nonce( 'add_dismissed_marketplace_suggestion' ),
|
||||
'installed_woo_plugins' => $woo_plugin_slugs,
|
||||
'dismissed_suggestions' => wc_marketplace_suggestions_get_dismissed(),
|
||||
'dismissed_suggestions' => WC_Marketplace_Suggestions::get_dismissed_suggestions(),
|
||||
)
|
||||
);
|
||||
wp_enqueue_script( 'marketplace-suggestions' );
|
||||
|
|
|
@ -378,17 +378,17 @@ final class WooCommerce {
|
|||
*/
|
||||
include_once WC_ABSPATH . 'includes/libraries/action-scheduler/action-scheduler.php';
|
||||
|
||||
/**
|
||||
* Marketplace suggestions
|
||||
*/
|
||||
include_once WC_ABSPATH . 'includes/marketplace-suggestions/class-wc-marketplace-suggestions-api.php';
|
||||
|
||||
if ( defined( 'WP_CLI' ) && WP_CLI ) {
|
||||
include_once WC_ABSPATH . 'includes/class-wc-cli.php';
|
||||
}
|
||||
|
||||
if ( $this->is_request( 'admin' ) ) {
|
||||
include_once WC_ABSPATH . 'includes/admin/class-wc-admin.php';
|
||||
|
||||
/**
|
||||
* Marketplace suggestions & related REST API.
|
||||
*/
|
||||
include_once WC_ABSPATH . 'includes/marketplace-suggestions/class-wc-marketplace-suggestions.php';
|
||||
}
|
||||
|
||||
if ( $this->is_request( 'frontend' ) ) {
|
||||
|
|
|
@ -1,107 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Marketplace suggestions API
|
||||
*
|
||||
* Handles requests for marketplace suggestions data
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @since 3.6.0
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Pull suggestion data from remote endpoint & cache in a transient.
|
||||
*
|
||||
* @return array of json API data
|
||||
*/
|
||||
function wc_marketplace_suggestions_get_api_data() {
|
||||
$suggestion_data = get_transient( 'wc_marketplace_suggestions' );
|
||||
if ( false !== $suggestion_data ) {
|
||||
return $suggestion_data;
|
||||
}
|
||||
|
||||
$suggestion_data_url = 'https://d3t0oesq8995hv.cloudfront.net/add-ons/marketplace-suggestions.json';
|
||||
$raw_suggestions = wp_remote_get(
|
||||
$suggestion_data_url,
|
||||
array( 'user-agent' => 'WooCommerce Marketplace Suggestions' )
|
||||
);
|
||||
|
||||
// Parse the data to check for any errors.
|
||||
// If it's valid, store structure in transient.
|
||||
if ( ! is_wp_error( $raw_suggestions ) ) {
|
||||
$suggestions = json_decode( wp_remote_retrieve_body( $raw_suggestions ) );
|
||||
if ( $suggestions && is_array( $suggestions ) ) {
|
||||
set_transient( 'wc_marketplace_suggestions', $suggestions, WEEK_IN_SECONDS );
|
||||
return $suggestions;
|
||||
}
|
||||
}
|
||||
|
||||
// Cache empty suggestions data to reduce requests if there are any issues with API.
|
||||
set_transient( 'wc_marketplace_suggestions', '[]', DAY_IN_SECONDS );
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Suggestion data GET handler.
|
||||
*/
|
||||
function wc_marketplace_suggestions_ajax_handler() {
|
||||
$suggestion_data = wc_marketplace_suggestions_get_api_data();
|
||||
wp_send_json_success( $suggestion_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of suggestions the user has dismissed.
|
||||
*/
|
||||
function wc_marketplace_suggestions_get_dismissed() {
|
||||
$dismissed_suggestions = array();
|
||||
|
||||
$dismissed_suggestions_data = get_user_meta( 'wc_marketplace_suggestions_dismissed_suggestions', get_current_user_id() );
|
||||
if ( $dismissed_suggestions_data ) {
|
||||
$dismissed_suggestions = $dismissed_suggestions_data;
|
||||
if ( ! is_array( $dismissed_suggestions ) ) {
|
||||
$dismissed_suggestions = array();
|
||||
}
|
||||
}
|
||||
|
||||
return $dismissed_suggestions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Suggestion data GET handler.
|
||||
*/
|
||||
function wc_marketplace_suggestions_dismiss_handler() {
|
||||
if ( ! check_ajax_referer( 'add_dismissed_marketplace_suggestion' ) ) {
|
||||
wp_die();
|
||||
}
|
||||
|
||||
$post_data = wp_unslash( $_POST );
|
||||
$suggestion_slug = sanitize_text_field( $post_data['slug'] );
|
||||
if ( ! $suggestion_slug ) {
|
||||
wp_die();
|
||||
}
|
||||
|
||||
$dismissed_suggestions = wc_marketplace_suggestions_get_dismissed();
|
||||
|
||||
if ( in_array( $suggestion_slug, $dismissed_suggestions, true ) ) {
|
||||
wp_die();
|
||||
}
|
||||
|
||||
$dismissed_suggestions[] = $suggestion_slug;
|
||||
update_user_meta(
|
||||
get_current_user_id(),
|
||||
'wc_marketplace_suggestions_dismissed_suggestions',
|
||||
$dismissed_suggestions
|
||||
);
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise
|
||||
*/
|
||||
function wc_marketplace_suggestions_api_init() {
|
||||
add_action( 'wp_ajax_marketplace_suggestions', 'wc_marketplace_suggestions_ajax_handler' );
|
||||
add_action( 'wp_ajax_add_dismissed_marketplace_suggestion', 'wc_marketplace_suggestions_dismiss_handler' );
|
||||
}
|
||||
wc_marketplace_suggestions_api_init();
|
|
@ -0,0 +1,115 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Marketplace suggestions API
|
||||
*
|
||||
* Handles requests for marketplace suggestions data & rendering
|
||||
* templates for suggestion DOM content.
|
||||
*
|
||||
* @package WooCommerce\Classes
|
||||
* @since 3.6.0
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* REST API Marketplace suggestions API and related logic.
|
||||
*/
|
||||
class WC_Marketplace_Suggestions {
|
||||
|
||||
/**
|
||||
* Initialise – register ajax api handlers.
|
||||
*/
|
||||
public static function init() {
|
||||
add_action( 'wp_ajax_marketplace_suggestions', array( __CLASS__, 'get_suggestion_json_data_handler' ) );
|
||||
add_action( 'wp_ajax_add_dismissed_marketplace_suggestion', array( __CLASS__, 'post_add_dismissed_suggestion_handler' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Suggestion data GET handler.
|
||||
*/
|
||||
public static function get_suggestion_json_data_handler() {
|
||||
$suggestion_data = self::get_suggestions_api_data();
|
||||
wp_send_json_success( $suggestion_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of suggestions the user has dismissed.
|
||||
*/
|
||||
public static function get_dismissed_suggestions() {
|
||||
$dismissed_suggestions = array();
|
||||
|
||||
$dismissed_suggestions_data = get_user_meta( get_current_user_id(), 'wc_marketplace_suggestions_dismissed_suggestions', true );
|
||||
if ( $dismissed_suggestions_data ) {
|
||||
$dismissed_suggestions = $dismissed_suggestions_data;
|
||||
if ( ! is_array( $dismissed_suggestions ) ) {
|
||||
$dismissed_suggestions = array();
|
||||
}
|
||||
}
|
||||
|
||||
return $dismissed_suggestions;
|
||||
}
|
||||
|
||||
/**
|
||||
* POST handler for adding a dismissed suggestion.
|
||||
*/
|
||||
public static function post_add_dismissed_suggestion_handler() {
|
||||
if ( ! check_ajax_referer( 'add_dismissed_marketplace_suggestion' ) ) {
|
||||
wp_die();
|
||||
}
|
||||
|
||||
$post_data = wp_unslash( $_POST );
|
||||
$suggestion_slug = sanitize_text_field( $post_data['slug'] );
|
||||
if ( ! $suggestion_slug ) {
|
||||
wp_die();
|
||||
}
|
||||
|
||||
$dismissed_suggestions = self::get_dismissed_suggestions();
|
||||
|
||||
if ( in_array( $suggestion_slug, $dismissed_suggestions, true ) ) {
|
||||
wp_die();
|
||||
}
|
||||
|
||||
$dismissed_suggestions[] = $suggestion_slug;
|
||||
update_user_meta(
|
||||
get_current_user_id(),
|
||||
'wc_marketplace_suggestions_dismissed_suggestions',
|
||||
$dismissed_suggestions
|
||||
);
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pull suggestion data from remote endpoint & cache in a transient.
|
||||
*
|
||||
* @return array of json API data
|
||||
*/
|
||||
private static function get_suggestions_api_data() {
|
||||
$suggestion_data = get_transient( 'wc_marketplace_suggestions' );
|
||||
if ( false !== $suggestion_data ) {
|
||||
return $suggestion_data;
|
||||
}
|
||||
|
||||
$suggestion_data_url = 'https://d3t0oesq8995hv.cloudfront.net/add-ons/marketplace-suggestions.json';
|
||||
$raw_suggestions = wp_remote_get(
|
||||
$suggestion_data_url,
|
||||
array( 'user-agent' => 'WooCommerce Marketplace Suggestions' )
|
||||
);
|
||||
|
||||
// Parse the data to check for any errors.
|
||||
// If it's valid, store structure in transient.
|
||||
if ( ! is_wp_error( $raw_suggestions ) ) {
|
||||
$suggestions = json_decode( wp_remote_retrieve_body( $raw_suggestions ) );
|
||||
if ( $suggestions && is_array( $suggestions ) ) {
|
||||
set_transient( 'wc_marketplace_suggestions', $suggestions, WEEK_IN_SECONDS );
|
||||
return $suggestions;
|
||||
}
|
||||
}
|
||||
|
||||
// Cache empty suggestions data to reduce requests if there are any issues with API.
|
||||
set_transient( 'wc_marketplace_suggestions', '[]', DAY_IN_SECONDS );
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
WC_Marketplace_Suggestions::init();
|
Loading…
Reference in New Issue