2019-02-20 23:37:10 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-02-25 21:07:42 +00:00
|
|
|
* Marketplace suggestions
|
2019-02-20 23:37:10 +00:00
|
|
|
*
|
2019-02-25 21:07:42 +00:00
|
|
|
* Behaviour for displaying in-context suggestions for marketplace extensions.
|
2019-02-20 23:37:10 +00:00
|
|
|
*
|
|
|
|
* @package WooCommerce\Classes
|
|
|
|
* @since 3.6.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
2019-02-25 21:07:42 +00:00
|
|
|
* Marketplace suggestions core behaviour.
|
2019-02-20 23:37:10 +00:00
|
|
|
*/
|
|
|
|
class WC_Marketplace_Suggestions {
|
|
|
|
|
|
|
|
/**
|
2019-02-21 01:38:04 +00:00
|
|
|
* Initialise.
|
2019-02-20 23:37:10 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
2019-02-21 02:04:38 +00:00
|
|
|
if ( ! self::allow_suggestions() ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-21 02:56:26 +00:00
|
|
|
|
2019-02-22 08:52:17 +00:00
|
|
|
// Add suggestions to the product tabs.
|
|
|
|
add_action( 'woocommerce_product_data_tabs', array( __CLASS__, 'product_data_tabs' ) );
|
|
|
|
add_action( 'woocommerce_product_data_panels', array( __CLASS__, 'product_data_panels' ) );
|
|
|
|
|
2019-02-21 01:38:04 +00:00
|
|
|
// Register ajax api handlers.
|
2019-02-21 02:56:26 +00:00
|
|
|
add_action( 'wp_ajax_woocommerce_add_dismissed_marketplace_suggestion', array( __CLASS__, 'post_add_dismissed_suggestion_handler' ) );
|
2019-02-21 01:38:04 +00:00
|
|
|
|
|
|
|
// Register hooks for rendering suggestions container markup.
|
|
|
|
add_action( 'wc_marketplace_suggestions_products_empty_state', array( __CLASS__, 'render_products_list_empty_state' ) );
|
2019-02-22 03:13:37 +00:00
|
|
|
add_action( 'wc_marketplace_suggestions_orders_empty_state', array( __CLASS__, 'render_orders_list_empty_state' ) );
|
2019-02-20 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 08:52:17 +00:00
|
|
|
/**
|
|
|
|
* Product data tabs filter
|
|
|
|
*
|
|
|
|
* Adds a new Extensions tab to the product data meta box.
|
|
|
|
*
|
2019-02-25 03:12:24 +00:00
|
|
|
* @param array $tabs Existing tabs.
|
2019-02-22 08:52:17 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function product_data_tabs( $tabs ) {
|
|
|
|
$tabs['marketplace-suggestions'] = array(
|
2019-02-25 03:12:24 +00:00
|
|
|
'label' => _x( 'Get more options', 'Marketplace suggestions', 'woocommerce' ),
|
|
|
|
'target' => 'marketplace_suggestions',
|
|
|
|
'class' => array(),
|
2019-02-22 08:52:17 +00:00
|
|
|
'priority' => 1000,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $tabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-25 17:06:30 +00:00
|
|
|
* Render additional panels in the product data metabox.
|
2019-02-22 08:52:17 +00:00
|
|
|
*/
|
|
|
|
public static function product_data_panels() {
|
2019-02-25 03:12:24 +00:00
|
|
|
include dirname( __FILE__ ) . '/templates/html-product-data-extensions.php';
|
2019-02-22 08:52:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 23:37:10 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2019-02-21 01:38:04 +00:00
|
|
|
/**
|
|
|
|
* Render suggestions containers in products list empty state.
|
|
|
|
*/
|
|
|
|
public static function render_products_list_empty_state() {
|
|
|
|
self::render_suggestions_container( 'products-list-empty-header' );
|
|
|
|
self::render_suggestions_container( 'products-list-empty-body' );
|
|
|
|
self::render_suggestions_container( 'products-list-empty-footer' );
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:13:37 +00:00
|
|
|
/**
|
|
|
|
* Render suggestions containers in orders list empty state.
|
|
|
|
*/
|
|
|
|
public static function render_orders_list_empty_state() {
|
|
|
|
self::render_suggestions_container( 'orders-list-empty-header' );
|
|
|
|
self::render_suggestions_container( 'orders-list-empty-body' );
|
|
|
|
self::render_suggestions_container( 'orders-list-empty-footer' );
|
|
|
|
}
|
|
|
|
|
2019-02-21 01:38:04 +00:00
|
|
|
/**
|
|
|
|
* Render a suggestions container element, with the specified context.
|
|
|
|
*
|
|
|
|
* @param string $context Suggestion context name (rendered as a css class).
|
|
|
|
*/
|
|
|
|
public static function render_suggestions_container( $context ) {
|
2019-02-23 20:37:33 +00:00
|
|
|
include dirname( __FILE__ ) . '/views/container.php';
|
2019-02-21 01:38:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 03:11:34 +00:00
|
|
|
/**
|
|
|
|
* Should suggestions be displayed?
|
|
|
|
*
|
|
|
|
* @param string $screen_id The current admin screen.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function show_suggestions_for_screen( $screen_id ) {
|
|
|
|
// We only show suggestions on certain admin screens.
|
2019-02-22 08:52:17 +00:00
|
|
|
if ( ! in_array( $screen_id, array( 'edit-product', 'edit-shop_order', 'product' ), true ) ) {
|
2019-02-22 03:11:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::allow_suggestions();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-21 02:04:38 +00:00
|
|
|
/**
|
2019-02-21 02:20:16 +00:00
|
|
|
* Should suggestions be displayed?
|
|
|
|
*
|
|
|
|
* @return bool
|
2019-02-21 02:04:38 +00:00
|
|
|
*/
|
|
|
|
public static function allow_suggestions() {
|
2019-02-21 23:31:02 +00:00
|
|
|
// We currently only support English suggestions.
|
2019-02-21 02:20:16 +00:00
|
|
|
$locale = get_locale();
|
|
|
|
$suggestion_locales = array(
|
|
|
|
'en_AU',
|
|
|
|
'en_CA',
|
|
|
|
'en_GB',
|
|
|
|
'en_NZ',
|
|
|
|
'en_US',
|
|
|
|
'en_ZA',
|
|
|
|
);
|
2019-02-21 23:31:02 +00:00
|
|
|
if ( ! in_array( $locale, $suggestion_locales, true ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suggestions are only displayed if user can install plugins.
|
|
|
|
if ( ! current_user_can( 'install_plugins' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-21 02:20:16 +00:00
|
|
|
|
2019-04-05 20:38:30 +00:00
|
|
|
// Suggestions may be disabled via a setting under Accounts & Privacy.
|
|
|
|
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:31:02 +00:00
|
|
|
// User can disabled all suggestions via filter.
|
|
|
|
return apply_filters( 'woocommerce_allow_marketplace_suggestions', true );
|
2019-02-21 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 23:37:10 +00:00
|
|
|
/**
|
2019-04-24 12:00:16 +00:00
|
|
|
* Pull suggestion data from options. This is retrieved from a remote endpoint.
|
2019-02-20 23:37:10 +00:00
|
|
|
*
|
|
|
|
* @return array of json API data
|
|
|
|
*/
|
2019-02-21 22:23:09 +00:00
|
|
|
public static function get_suggestions_api_data() {
|
2019-02-22 19:34:18 +00:00
|
|
|
$data = get_option( 'woocommerce_marketplace_suggestions', array() );
|
2019-04-24 12:00:16 +00:00
|
|
|
|
|
|
|
// If the options have never been updated, or were updated over a week ago, queue update.
|
|
|
|
if ( empty( $data['updated'] ) || ( time() - WEEK_IN_SECONDS ) > $data['updated'] ) {
|
|
|
|
$next = WC()->queue()->get_next( 'woocommerce_update_marketplace_suggestions' );
|
|
|
|
if ( ! $next ) {
|
2019-04-24 15:28:28 +00:00
|
|
|
WC()->queue()->cancel_all( 'woocommerce_update_marketplace_suggestions' );
|
2019-04-24 12:00:16 +00:00
|
|
|
WC()->queue()->schedule_single( time(), 'woocommerce_update_marketplace_suggestions' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 19:34:18 +00:00
|
|
|
return ! empty( $data['suggestions'] ) ? $data['suggestions'] : array();
|
2019-02-20 23:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WC_Marketplace_Suggestions::init();
|
2019-02-22 03:06:18 +00:00
|
|
|
|