plugin updates
This commit is contained in:
@@ -101,6 +101,14 @@ class WC_Helper_Admin {
|
||||
$connect_url_args['wc-helper-nonce'] = wp_create_nonce( 'connect' );
|
||||
}
|
||||
|
||||
if ( ! empty( $_GET['utm_source'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
$connect_url_args['utm_source'] = wc_clean( wp_unslash( $_GET['utm_source'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
}
|
||||
|
||||
if ( ! empty( $_GET['utm_campaign'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
$connect_url_args['utm_campaign'] = wc_clean( wp_unslash( $_GET['utm_campaign'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
}
|
||||
|
||||
return add_query_arg(
|
||||
$connect_url_args,
|
||||
admin_url( 'admin.php' )
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
* @package WooCommerce\Admin\Helper
|
||||
*/
|
||||
|
||||
use Automattic\WooCommerce\Admin\PluginsHelper;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
@@ -26,7 +28,7 @@ class WC_Helper_Updater {
|
||||
add_action( 'pre_set_site_transient_update_themes', array( __CLASS__, 'transient_update_themes' ), 21, 1 );
|
||||
add_action( 'upgrader_process_complete', array( __CLASS__, 'upgrader_process_complete' ) );
|
||||
add_action( 'upgrader_pre_download', array( __CLASS__, 'block_expired_updates' ), 10, 2 );
|
||||
add_action( 'plugins_loaded', array( __CLASS__, 'add_hook_for_modifying_update_notices' ) );
|
||||
add_action( 'admin_init', array( __CLASS__, 'add_hook_for_modifying_update_notices' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,9 +188,11 @@ class WC_Helper_Updater {
|
||||
public static function add_connect_woocom_plugin_message() {
|
||||
$connect_page_url = add_query_arg(
|
||||
array(
|
||||
'page' => 'wc-admin',
|
||||
'tab' => 'my-subscriptions',
|
||||
'path' => rawurlencode( '/extensions' ),
|
||||
'page' => 'wc-admin',
|
||||
'tab' => 'my-subscriptions',
|
||||
'path' => rawurlencode( '/extensions' ),
|
||||
'utm_source' => 'pu',
|
||||
'utm_campaign' => 'pu_plugin_screen_connect',
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
@@ -252,41 +256,68 @@ class WC_Helper_Updater {
|
||||
* @return void.
|
||||
*/
|
||||
public static function display_notice_for_expired_and_expiring_subscriptions( $plugin_data, $response ) {
|
||||
|
||||
// Extract product ID from the response.
|
||||
$product_id = preg_replace( '/[^0-9]/', '', $response->id );
|
||||
|
||||
// Get the subscription details based on product ID.
|
||||
$subscription = current(
|
||||
wp_list_filter(
|
||||
WC_Helper::get_subscriptions(),
|
||||
array( 'product_id' => $product_id )
|
||||
)
|
||||
);
|
||||
|
||||
// Check if subscription is empty.
|
||||
if ( empty( $subscription ) ) {
|
||||
// Product subscriptions.
|
||||
$subscriptions = wp_list_filter( WC_Helper::get_installed_subscriptions(), array( 'product_id' => $product_id ) );
|
||||
if ( empty( $subscriptions ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$expired_subscription = current(
|
||||
array_filter(
|
||||
$subscriptions,
|
||||
function ( $subscription ) {
|
||||
return ! empty( $subscription['expired'] ) && ! $subscription['lifetime'];
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
$expiring_subscription = current(
|
||||
array_filter(
|
||||
$subscriptions,
|
||||
function ( $subscription ) {
|
||||
return ! empty( $subscription['expiring'] ) && ! $subscription['autorenew'];
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// Prepare the expiry notice based on subscription status.
|
||||
$expiry_notice = '';
|
||||
if ( ! empty( $subscription['expired'] ) && ! $subscription['lifetime'] ) {
|
||||
if ( ! empty( $expired_subscription ) ) {
|
||||
|
||||
$renew_link = add_query_arg(
|
||||
array(
|
||||
'utm_source' => 'pu',
|
||||
'utm_campaign' => 'pu_plugin_screen_renew',
|
||||
),
|
||||
PluginsHelper::WOO_SUBSCRIPTION_PAGE_URL
|
||||
);
|
||||
|
||||
/* translators: 1: Product regular price */
|
||||
$product_price = ! empty( $subscription['product_regular_price'] ) ? sprintf( __( 'for %s ', 'woocommerce' ), esc_html( $subscription['product_regular_price'] ) ) : '';
|
||||
$product_price = ! empty( $expired_subscription['product_regular_price'] ) ? sprintf( __( 'for %s ', 'woocommerce' ), esc_html( $expired_subscription['product_regular_price'] ) ) : '';
|
||||
|
||||
$expiry_notice = sprintf(
|
||||
/* translators: 1: URL to My Subscriptions page 2: Product price */
|
||||
__( ' Your subscription expired, <a href="%1$s" class="woocommerce-renew-subscription">renew %2$s</a>to update.', 'woocommerce' ),
|
||||
esc_url( 'https://woocommerce.com/my-account/my-subscriptions/' ),
|
||||
esc_url( $renew_link ),
|
||||
$product_price
|
||||
);
|
||||
} elseif ( ! empty( $subscription['expiring'] ) && ! $subscription['autorenew'] ) {
|
||||
} elseif ( ! empty( $expiring_subscription ) ) {
|
||||
$renew_link = add_query_arg(
|
||||
array(
|
||||
'utm_source' => 'pu',
|
||||
'utm_campaign' => 'pu_plugin_screen_enable_autorenew',
|
||||
),
|
||||
PluginsHelper::WOO_SUBSCRIPTION_PAGE_URL
|
||||
);
|
||||
|
||||
$expiry_notice = sprintf(
|
||||
/* translators: 1: Expiry date 1: URL to My Subscriptions page */
|
||||
__( ' Your subscription expires on %1$s, <a href="%2$s" class="woocommerce-enable-autorenew">enable auto-renew</a> to continue receiving updates.', 'woocommerce' ),
|
||||
date_i18n( 'F jS', $subscription['expires'] ),
|
||||
esc_url( 'https://woocommerce.com/my-account/my-subscriptions/' )
|
||||
date_i18n( 'F jS', $expiring_subscription['expires'] ),
|
||||
esc_url( $renew_link )
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,7 @@ class WC_Helper {
|
||||
include_once __DIR__ . '/class-wc-helper-admin.php';
|
||||
include_once __DIR__ . '/class-wc-helper-subscriptions-api.php';
|
||||
include_once __DIR__ . '/class-wc-helper-orders-api.php';
|
||||
include_once __DIR__ . '/class-wc-product-usage-notice.php';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -785,6 +786,14 @@ class WC_Helper {
|
||||
$redirect_url_args['install'] = sanitize_text_field( wp_unslash( $_GET['install'] ) );
|
||||
}
|
||||
|
||||
if ( isset( $_GET['utm_source'] ) ) {
|
||||
$redirect_url_args['utm_source'] = wc_clean( wp_unslash( $_GET['utm_source'] ) );
|
||||
}
|
||||
|
||||
if ( isset( $_GET['utm_campaign'] ) ) {
|
||||
$redirect_url_args['utm_campaign'] = wc_clean( wp_unslash( $_GET['utm_campaign'] ) );
|
||||
}
|
||||
|
||||
$redirect_uri = add_query_arg(
|
||||
$redirect_url_args,
|
||||
admin_url( 'admin.php' )
|
||||
@@ -1296,6 +1305,60 @@ class WC_Helper {
|
||||
return ! empty( $subscription );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's connected subscriptions that are installed on the current
|
||||
* site.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_installed_subscriptions() {
|
||||
static $installed_subscriptions = null;
|
||||
|
||||
// Cache installed_subscriptions in the current request.
|
||||
if ( is_null( $installed_subscriptions ) ) {
|
||||
$auth = WC_Helper_Options::get( 'auth' );
|
||||
$site_id = isset( $auth['site_id'] ) ? absint( $auth['site_id'] ) : 0;
|
||||
if ( 0 === $site_id ) {
|
||||
$installed_subscriptions = array();
|
||||
return $installed_subscriptions;
|
||||
}
|
||||
|
||||
$installed_subscriptions = array_filter(
|
||||
self::get_subscriptions(),
|
||||
function ( $subscription ) use ( $site_id ) {
|
||||
return in_array( $site_id, $subscription['connections'], true );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return $installed_subscriptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription state of a given product ID.
|
||||
*
|
||||
* @since TBD
|
||||
*
|
||||
* @param int $product_id The product id.
|
||||
*
|
||||
* @return array Array of state_name => (bool) state
|
||||
*/
|
||||
public static function get_product_subscription_state( $product_id ) {
|
||||
$product_subscriptions = wp_list_filter( self::get_installed_subscriptions(), array( 'product_id' => $product_id ) );
|
||||
|
||||
$subscription = ! empty( $product_subscriptions )
|
||||
? array_shift( $product_subscriptions )
|
||||
: array();
|
||||
|
||||
return array(
|
||||
'unregistered' => empty( $subscription ),
|
||||
'expired' => ( isset( $subscription['expired'] ) && $subscription['expired'] ),
|
||||
'expiring' => ( isset( $subscription['expiring'] ) && $subscription['expiring'] ),
|
||||
'key' => $subscription['product_key'] ?? '',
|
||||
'order_id' => $subscription['order_id'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a subscription entry from product_id. If multiple subscriptions are
|
||||
* found with the same product id and $single is set to true, will return the
|
||||
@@ -1482,6 +1545,41 @@ class WC_Helper {
|
||||
return $woo_themes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rules for displaying notice regarding marketplace product usage.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_product_usage_notice_rules() {
|
||||
$cache_key = '_woocommerce_helper_product_usage_notice_rules';
|
||||
$data = get_transient( $cache_key );
|
||||
if ( false !== $data ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$request = WC_Helper_API::get(
|
||||
'product-usage-notice-rules',
|
||||
array(
|
||||
'authenticated' => false,
|
||||
)
|
||||
);
|
||||
|
||||
// Retry in 15 minutes for non-200 response.
|
||||
if ( wp_remote_retrieve_response_code( $request ) !== 200 ) {
|
||||
set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS );
|
||||
return array();
|
||||
}
|
||||
|
||||
$data = json_decode( wp_remote_retrieve_body( $request ), true );
|
||||
if ( empty( $data ) || ! is_array( $data ) ) {
|
||||
$data = array();
|
||||
}
|
||||
|
||||
set_transient( $cache_key, $data, 1 * HOUR_IN_SECONDS );
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the connected user's subscriptions.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,384 @@
|
||||
<?php
|
||||
/**
|
||||
* WooCommerce Product Usage Notice.
|
||||
*
|
||||
* @package WooCommerce\Admin\Helper
|
||||
*/
|
||||
|
||||
declare( strict_types = 1 );
|
||||
|
||||
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Product usage notice class.
|
||||
*/
|
||||
class WC_Product_Usage_Notice {
|
||||
/**
|
||||
* User meta key prefix to store dismiss counts per product. Product ID is
|
||||
* the suffix part.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const DISMISSED_COUNT_META_PREFIX = '_woocommerce_product_usage_notice_dismissed_count_';
|
||||
|
||||
/**
|
||||
* User meta key prefix to store timestamp of last dismissed product usage notice.
|
||||
* Product ID is the suffix part.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const DISMISSED_TIMESTAMP_META_PREFIX = '_woocommerce_product_usage_notice_dismissed_timestamp_';
|
||||
|
||||
/**
|
||||
* User meta key prefix to store timestamp of last clicked remind later from
|
||||
* product usage notice. Product ID is the suffix part.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const REMIND_LATER_TIMESTAMP_META_PREFIX = '_woocommerce_product_usage_notice_remind_later_timestamp_';
|
||||
|
||||
/**
|
||||
* User meta key to store timestamp of last dismissed of any product usage
|
||||
* notices. There's no product ID in the meta key.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const LAST_DISMISSED_TIMESTAMP_META = '_woocommerce_product_usage_notice_last_dismissed_timestamp';
|
||||
|
||||
/**
|
||||
* Array of product usage notice rules from helper API.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $product_usage_notice_rules = array();
|
||||
|
||||
/**
|
||||
* Current product usage notice rule applied to the current admin screen.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $current_notice_rule = array();
|
||||
|
||||
/**
|
||||
* Loads the class, runs on init.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function load() {
|
||||
add_action( 'current_screen', array( __CLASS__, 'maybe_show_product_usage_notice' ) );
|
||||
|
||||
add_action( 'wp_ajax_woocommerce_dismiss_product_usage_notice', array( __CLASS__, 'ajax_dismiss' ) );
|
||||
add_action( 'wp_ajax_woocommerce_remind_later_product_usage_notice', array( __CLASS__, 'ajax_remind_later' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe show product usage notice in a given screen object.
|
||||
*
|
||||
* @param \WP_Screen $screen Current \WP_Screen object.
|
||||
*/
|
||||
public static function maybe_show_product_usage_notice( $screen ) {
|
||||
$user_id = get_current_user_id();
|
||||
if ( ! $user_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! WC_Helper::is_site_connected() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
self::$product_usage_notice_rules = WC_Helper::get_product_usage_notice_rules();
|
||||
if ( empty( self::$product_usage_notice_rules ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
self::$current_notice_rule = self::get_current_notice_rule( $screen );
|
||||
if ( empty( self::$current_notice_rule ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$product_id = self::$current_notice_rule['id'];
|
||||
|
||||
if ( self::is_notice_throttled( $user_id, $product_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_product_usage_notice_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the user clicked "remind later" recently.
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
* @param int $product_id Product ID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function is_remind_later_clicked_recently( int $user_id, int $product_id ): bool {
|
||||
$last_remind_later_ts = absint(
|
||||
get_user_meta(
|
||||
$user_id,
|
||||
self::REMIND_LATER_TIMESTAMP_META_PREFIX . $product_id,
|
||||
true
|
||||
)
|
||||
);
|
||||
if ( 0 === $last_remind_later_ts ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$seconds_since_clicked_remind_later = time() - $last_remind_later_ts;
|
||||
|
||||
$wait_after_remind_later = self::$current_notice_rule['wait_in_seconds_after_remind_later'];
|
||||
|
||||
return $seconds_since_clicked_remind_later < $wait_after_remind_later;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the user has reached max dismissals of product usage notice.
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
* @param int $product_id Product ID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function has_reached_max_dismissals( int $user_id, int $product_id ): bool {
|
||||
$dismiss_count = absint(
|
||||
get_user_meta(
|
||||
$user_id,
|
||||
self::DISMISSED_COUNT_META_PREFIX . $product_id,
|
||||
true
|
||||
)
|
||||
);
|
||||
|
||||
$max_dismissals = self::$current_notice_rule['max_dismissals'];
|
||||
|
||||
return $dismiss_count >= $max_dismissals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the user dismissed any product usage notices recently.
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function is_any_notices_dismissed_recently( int $user_id ): bool {
|
||||
$global_last_dismissed_ts = absint(
|
||||
get_user_meta(
|
||||
$user_id,
|
||||
self::LAST_DISMISSED_TIMESTAMP_META,
|
||||
true
|
||||
)
|
||||
);
|
||||
if ( 0 === $global_last_dismissed_ts ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$seconds_since_dismissed = time() - $global_last_dismissed_ts;
|
||||
|
||||
$wait_after_any_dismisses = self::$product_usage_notice_rules['wait_in_seconds_after_any_dismisses'];
|
||||
|
||||
return $seconds_since_dismissed < $wait_after_any_dismisses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the user dismissed given product usage notice recently.
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
* @param int $product_id Product ID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function is_product_notice_dismissed_recently( int $user_id, int $product_id ): bool {
|
||||
$last_dismissed_ts = absint(
|
||||
get_user_meta(
|
||||
$user_id,
|
||||
self::DISMISSED_TIMESTAMP_META_PREFIX . $product_id,
|
||||
true
|
||||
)
|
||||
);
|
||||
if ( 0 === $last_dismissed_ts ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$seconds_since_dismissed = time() - $last_dismissed_ts;
|
||||
|
||||
$wait_after_dismiss = self::$current_notice_rule['wait_in_seconds_after_dismiss'];
|
||||
|
||||
return $seconds_since_dismissed < $wait_after_dismiss;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether current notice is throttled for the user and product.
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
* @param int $product_id Product ID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function is_notice_throttled( int $user_id, int $product_id ): bool {
|
||||
return self::is_remind_later_clicked_recently( $user_id, $product_id ) ||
|
||||
self::has_reached_max_dismissals( $user_id, $product_id ) ||
|
||||
self::is_any_notices_dismissed_recently( $user_id ) ||
|
||||
self::is_product_notice_dismissed_recently( $user_id, $product_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts needed to display product usage notice (or modal).
|
||||
*/
|
||||
public static function enqueue_product_usage_notice_scripts() {
|
||||
WCAdminAssets::register_style( 'woo-product-usage-notice', 'style', array( 'wp-components' ) );
|
||||
WCAdminAssets::register_script( 'wp-admin-scripts', 'woo-product-usage-notice', true );
|
||||
|
||||
$subscribe_url = add_query_arg(
|
||||
array(
|
||||
'add-to-cart' => self::$current_notice_rule['id'],
|
||||
'utm_source' => 'pu',
|
||||
'utm_medium' => 'product',
|
||||
'utm_campaign' => 'pu_modal_subscribe',
|
||||
),
|
||||
'https://woocommerce.com/cart/'
|
||||
);
|
||||
|
||||
$renew_url = add_query_arg(
|
||||
array(
|
||||
'renew_product' => self::$current_notice_rule['id'],
|
||||
'product_key' => self::$current_notice_rule['state']['key'],
|
||||
'order_id' => self::$current_notice_rule['state']['order_id'],
|
||||
'utm_source' => 'pu',
|
||||
'utm_medium' => 'product',
|
||||
'utm_campaign' => 'pu_modal_renew',
|
||||
),
|
||||
'https://woocommerce.com/cart/'
|
||||
);
|
||||
|
||||
wp_localize_script(
|
||||
'wc-admin-woo-product-usage-notice',
|
||||
'wooProductUsageNotice',
|
||||
array(
|
||||
'subscribeUrl' => $subscribe_url,
|
||||
'renewUrl' => $renew_url,
|
||||
'dismissAction' => 'woocommerce_dismiss_product_usage_notice',
|
||||
'remindLaterAction' => 'woocommerce_remind_later_product_usage_notice',
|
||||
'productId' => self::$current_notice_rule['id'],
|
||||
'productName' => self::$current_notice_rule['name'],
|
||||
'productRegularPrice' => self::$current_notice_rule['regular_price'],
|
||||
'dismissNonce' => wp_create_nonce( 'dismiss_product_usage_notice' ),
|
||||
'remindLaterNonce' => wp_create_nonce( 'remind_later_product_usage_notice' ),
|
||||
'showAs' => self::$current_notice_rule['show_as'],
|
||||
'colorScheme' => self::$current_notice_rule['color_scheme'],
|
||||
'subscriptionState' => self::$current_notice_rule['state'],
|
||||
'screenId' => get_current_screen()->id,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get product usage notice rule from a given WP_Screen object.
|
||||
*
|
||||
* @param \WP_Screen $screen Current \WP_Screen object.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function get_current_notice_rule( $screen ) {
|
||||
foreach ( self::$product_usage_notice_rules['products'] as $product_id => $rule ) {
|
||||
if ( ! isset( $rule['screens'][ $screen->id ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check query strings.
|
||||
if ( ! self::query_string_matches( $screen, $rule ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$product_id = absint( $product_id );
|
||||
$state = WC_Helper::get_product_subscription_state( $product_id );
|
||||
if ( $state['expired'] || $state['unregistered'] ) {
|
||||
$rule['id'] = $product_id;
|
||||
$rule['state'] = $state;
|
||||
return $rule;
|
||||
}
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the screen and GET parameter matches a given rule.
|
||||
*
|
||||
* @param \WP_Screen $screen Current \WP_Screen object.
|
||||
* @param array $rule Product usage notice rule.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function query_string_matches( $screen, $rule ) {
|
||||
if ( empty( $rule['screens'][ $screen->id ]['qs'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$qs = $rule['screens'][ $screen->id ]['qs'];
|
||||
foreach ( $qs as $key => $val ) {
|
||||
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
if ( empty( $_GET[ $key ] ) || $_GET[ $key ] !== $val ) {
|
||||
return false;
|
||||
}
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX handler for dismiss action of product usage notice.
|
||||
*/
|
||||
public static function ajax_dismiss() {
|
||||
if ( ! check_ajax_referer( 'dismiss_product_usage_notice' ) ) {
|
||||
wp_die( -1 );
|
||||
}
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
if ( ! $user_id ) {
|
||||
wp_die( -1 );
|
||||
}
|
||||
|
||||
$product_id = absint( $_GET['product_id'] ?? 0 );
|
||||
if ( ! $product_id ) {
|
||||
wp_die( -1 );
|
||||
}
|
||||
|
||||
$dismiss_count = absint( get_user_meta( $user_id, self::DISMISSED_COUNT_META_PREFIX . $product_id, true ) );
|
||||
update_user_meta( $user_id, self::DISMISSED_COUNT_META_PREFIX . $product_id, $dismiss_count + 1 );
|
||||
|
||||
update_user_meta( $user_id, self::DISMISSED_TIMESTAMP_META_PREFIX . $product_id, time() );
|
||||
update_user_meta( $user_id, self::LAST_DISMISSED_TIMESTAMP_META, time() );
|
||||
|
||||
wp_die( 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX handler for "remind later" action of product usage notice.
|
||||
*/
|
||||
public static function ajax_remind_later() {
|
||||
if ( ! check_ajax_referer( 'remind_later_product_usage_notice' ) ) {
|
||||
wp_die( -1 );
|
||||
}
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
if ( ! $user_id ) {
|
||||
wp_die( -1 );
|
||||
}
|
||||
|
||||
$product_id = absint( $_GET['product_id'] ?? 0 );
|
||||
if ( ! $product_id ) {
|
||||
wp_die( -1 );
|
||||
}
|
||||
|
||||
update_user_meta( $user_id, self::REMIND_LATER_TIMESTAMP_META_PREFIX . $product_id, time() );
|
||||
|
||||
wp_die( 1 );
|
||||
}
|
||||
}
|
||||
|
||||
WC_Product_Usage_Notice::load();
|
||||
Reference in New Issue
Block a user