Merged in feature/117-dev-dev01 (pull request #8)
auto-patch 117-dev-dev01-2023-12-15T16_09_06 * auto-patch 117-dev-dev01-2023-12-15T16_09_06
This commit is contained in:
@@ -14,7 +14,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
*
|
||||
* The main entry-point for all things related to the Helper.
|
||||
* The Helper manages the connection between the store and
|
||||
* an account on WooCommerce.com.
|
||||
* an account on Woo.com.
|
||||
*/
|
||||
class WC_Helper_Admin {
|
||||
|
||||
@@ -52,38 +52,36 @@ class WC_Helper_Admin {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the URL for connecting or disconnecting the store to/from WooCommerce.com.
|
||||
* Generates the URL for connecting or disconnecting the store to/from Woo.com.
|
||||
* Approach taken from existing helper code that isn't exposed.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_connection_url() {
|
||||
// No active connection.
|
||||
if ( ! WC_Helper::is_site_connected() ) {
|
||||
$connect_url = add_query_arg(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'wc-helper-connect' => 1,
|
||||
'wc-helper-nonce' => wp_create_nonce( 'connect' ),
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
global $current_screen;
|
||||
|
||||
return $connect_url;
|
||||
}
|
||||
|
||||
$connect_url = add_query_arg(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'wc-helper-disconnect' => 1,
|
||||
'wc-helper-nonce' => wp_create_nonce( 'disconnect' ),
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
$connect_url_args = array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
);
|
||||
|
||||
return $connect_url;
|
||||
// No active connection.
|
||||
if ( WC_Helper::is_site_connected() ) {
|
||||
$connect_url_args['wc-helper-disconnect'] = 1;
|
||||
$connect_url_args['wc-helper-nonce'] = wp_create_nonce( 'disconnect' );
|
||||
} else {
|
||||
$connect_url_args['wc-helper-connect'] = 1;
|
||||
$connect_url_args['wc-helper-nonce'] = wp_create_nonce( 'connect' );
|
||||
}
|
||||
|
||||
if ( isset( $current_screen->id ) && 'woocommerce_page_wc-admin' === $current_screen->id ) {
|
||||
$connect_url_args['redirect-to-wc-admin'] = 1;
|
||||
}
|
||||
|
||||
return add_query_arg(
|
||||
$connect_url_args,
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,7 +110,7 @@ class WC_Helper_Admin {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch featured procucts from WooCommerce.com and serve them
|
||||
* Fetch featured products from Woo.com and serve them
|
||||
* as JSON.
|
||||
*/
|
||||
public static function get_featured() {
|
||||
@@ -124,7 +122,6 @@ class WC_Helper_Admin {
|
||||
|
||||
wp_send_json( $featured );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
WC_Helper_Admin::load();
|
||||
|
||||
@@ -12,7 +12,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
/**
|
||||
* WC_Helper_API Class
|
||||
*
|
||||
* Provides a communication interface with the WooCommerce.com Helper API.
|
||||
* Provides a communication interface with the Woo.com Helper API.
|
||||
*/
|
||||
class WC_Helper_API {
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
/**
|
||||
* WC_Helper_Plugin_Info Class
|
||||
*
|
||||
* Provides the "View Information" core modals with data for WooCommerce.com
|
||||
* Provides the "View Information" core modals with data for Woo.com
|
||||
* hosted extensions.
|
||||
*/
|
||||
class WC_Helper_Plugin_Info {
|
||||
@@ -22,6 +22,7 @@ class WC_Helper_Plugin_Info {
|
||||
*/
|
||||
public static function load() {
|
||||
add_filter( 'plugins_api', array( __CLASS__, 'plugins_api' ), 20, 3 );
|
||||
add_filter( 'themes_api', array( __CLASS__, 'themes_api' ), 20, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,7 +38,31 @@ class WC_Helper_Plugin_Info {
|
||||
if ( 'plugin_information' !== $action ) {
|
||||
return $response;
|
||||
}
|
||||
return self::maybe_override_products_api( $response, $action, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Theme information callback for Woo themes.
|
||||
*
|
||||
* @param object $response The response core needs to display the modal.
|
||||
* @param string $action The requested themes_api() action.
|
||||
* @param object $args Arguments passed to themes_api().
|
||||
*/
|
||||
public static function themes_api( $response, $action, $args ) {
|
||||
if ( 'theme_information' !== $action ) {
|
||||
return $response;
|
||||
}
|
||||
return self::maybe_override_products_api( $response, $action, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the products API to fetch data from the Helper API if it's a Woo product.
|
||||
*
|
||||
* @param object $response The response core needs to display the modal.
|
||||
* @param string $action The requested action.
|
||||
* @param object $args Arguments passed to the API.
|
||||
*/
|
||||
public static function maybe_override_products_api( $response, $action, $args ) {
|
||||
if ( empty( $args->slug ) ) {
|
||||
return $response;
|
||||
}
|
||||
@@ -74,6 +99,11 @@ class WC_Helper_Plugin_Info {
|
||||
$results = json_decode( wp_remote_retrieve_body( $request ), true );
|
||||
if ( ! empty( $results ) ) {
|
||||
$response = (object) $results;
|
||||
|
||||
$product = array_shift( $products );
|
||||
if ( isset( $product['package'] ) ) {
|
||||
$response->download_link = $product['package'];
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
<?php
|
||||
/**
|
||||
* WooCommerce Admin Helper - React admin interface
|
||||
*
|
||||
* @package WooCommerce\Admin\Helper
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WC_Helper_Subscriptions_API
|
||||
*
|
||||
* The main entry-point for all things related to the Marketplace Subscriptions API.
|
||||
* The Subscriptions API manages WooCommerce.com Subscriptions.
|
||||
*/
|
||||
class WC_Helper_Subscriptions_API {
|
||||
|
||||
/**
|
||||
* Loads the class, runs on init
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function load() {
|
||||
add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the REST routes for the Marketplace Subscriptions API.
|
||||
* These endpoints are used by the Marketplace Subscriptions React UI.
|
||||
*/
|
||||
public static function register_rest_routes() {
|
||||
register_rest_route(
|
||||
'wc/v3',
|
||||
'/marketplace/refresh',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( __CLASS__, 'refresh' ),
|
||||
'permission_callback' => array( __CLASS__, 'get_permission' ),
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
'wc/v3',
|
||||
'/marketplace/subscriptions',
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( __CLASS__, 'get_subscriptions' ),
|
||||
'permission_callback' => array( __CLASS__, 'get_permission' ),
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
'wc/v3',
|
||||
'/marketplace/subscriptions/connect',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( __CLASS__, 'connect' ),
|
||||
'permission_callback' => array( __CLASS__, 'get_permission' ),
|
||||
'args' => array(
|
||||
'product_key' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
'wc/v3',
|
||||
'/marketplace/subscriptions/disconnect',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( __CLASS__, 'disconnect' ),
|
||||
'permission_callback' => array( __CLASS__, 'get_permission' ),
|
||||
'args' => array(
|
||||
'product_key' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
'wc/v3',
|
||||
'/marketplace/subscriptions/activate',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( __CLASS__, 'activate' ),
|
||||
'permission_callback' => array( __CLASS__, 'get_permission' ),
|
||||
'args' => array(
|
||||
'product_key' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The Extensions page can only be accessed by users with the manage_woocommerce
|
||||
* capability. So the API mimics that behavior.
|
||||
*/
|
||||
public static function get_permission() {
|
||||
return current_user_can( 'manage_woocommerce' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch subscriptions from WooCommerce.com and serve them
|
||||
* as JSON.
|
||||
*/
|
||||
public static function get_subscriptions() {
|
||||
$subscriptions = WC_Helper::get_subscription_list_data();
|
||||
wp_send_json(
|
||||
array_values(
|
||||
$subscriptions
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh account and subscriptions from WooCommerce.com and serve subscriptions
|
||||
* as JSON.
|
||||
*/
|
||||
public static function refresh() {
|
||||
WC_Helper::refresh_helper_subscriptions();
|
||||
self::get_subscriptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect a WooCommerce.com subscription.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
public static function connect( $request ) {
|
||||
$product_key = $request->get_param( 'product_key' );
|
||||
try {
|
||||
$success = WC_Helper::activate_helper_subscription( $product_key );
|
||||
} catch ( Exception $e ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'message' => $e->getMessage(),
|
||||
),
|
||||
400
|
||||
);
|
||||
}
|
||||
if ( $success ) {
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'message' => __( 'Your subscription has been connected.', 'woocommerce' ),
|
||||
)
|
||||
);
|
||||
} else {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'message' => __( 'There was an error connecting your subscription. Please try again.', 'woocommerce' ),
|
||||
),
|
||||
400
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect a WooCommerce.com subscription.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
public static function disconnect( $request ) {
|
||||
$product_key = $request->get_param( 'product_key' );
|
||||
try {
|
||||
$success = WC_Helper::deactivate_helper_subscription( $product_key );
|
||||
} catch ( Exception $e ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'message' => $e->getMessage(),
|
||||
),
|
||||
400
|
||||
);
|
||||
}
|
||||
if ( $success ) {
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'message' => __( 'Your subscription has been disconnected.', 'woocommerce' ),
|
||||
)
|
||||
);
|
||||
} else {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'message' => __( 'There was an error disconnecting your subscription. Please try again.', 'woocommerce' ),
|
||||
),
|
||||
400
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate a WooCommerce.com product.
|
||||
* This activates the plugin/theme on the site.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
public static function activate( $request ) {
|
||||
$product_key = $request->get_param( 'product_key' );
|
||||
$subscription = WC_Helper::get_subscription( $product_key );
|
||||
|
||||
if ( ! $subscription ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'message' => __( 'We couldn\'t find a subscription for this product.', 'woocommerce' ),
|
||||
),
|
||||
400
|
||||
);
|
||||
}
|
||||
|
||||
if ( true !== $subscription['local']['installed'] || ! isset( $subscription['local']['active'] ) ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'message' => __( 'This product is not installed.', 'woocommerce' ),
|
||||
),
|
||||
400
|
||||
);
|
||||
}
|
||||
|
||||
if ( true === $subscription['local']['active'] ) {
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'message' => __( 'This product is already active.', 'woocommerce' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if ( 'plugin' === $subscription['product_type'] ) {
|
||||
$success = activate_plugin( $subscription['local']['path'] );
|
||||
if ( is_wp_error( $success ) ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'message' => __( 'There was an error activating this plugin.', 'woocommerce' ),
|
||||
),
|
||||
400
|
||||
);
|
||||
}
|
||||
} elseif ( 'theme' === $subscription['product_type'] ) {
|
||||
switch_theme( $subscription['local']['slug'] );
|
||||
$theme = wp_get_theme();
|
||||
if ( $subscription['local']['slug'] !== $theme->get_stylesheet() ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'message' => __( 'There was an error activating this theme.', 'woocommerce' ),
|
||||
),
|
||||
400
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'message' => __( 'This product has been activated.', 'woocommerce' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
WC_Helper_Subscriptions_API::load();
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* The update helper for WooCommerce.com plugins.
|
||||
* The update helper for Woo.com plugins.
|
||||
*
|
||||
* @class WC_Helper_Updater
|
||||
* @package WooCommerce\Admin\Helper
|
||||
@@ -14,7 +14,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
* WC_Helper_Updater Class
|
||||
*
|
||||
* Contains the logic to fetch available updates and hook into Core's update
|
||||
* routines to serve WooCommerce.com-provided packages.
|
||||
* routines to serve Woo.com-provided packages.
|
||||
*/
|
||||
class WC_Helper_Updater {
|
||||
|
||||
@@ -89,7 +89,7 @@ class WC_Helper_Updater {
|
||||
|
||||
/**
|
||||
* Runs on pre_set_site_transient_update_themes, provides custom
|
||||
* packages for WooCommerce.com-hosted extensions.
|
||||
* packages for Woo.com-hosted extensions.
|
||||
*
|
||||
* @param object $transient The update_themes transient object.
|
||||
*
|
||||
@@ -516,7 +516,7 @@ class WC_Helper_Updater {
|
||||
return new WP_Error(
|
||||
'woocommerce_subscription_expired',
|
||||
sprintf(
|
||||
// translators: %s: URL of WooCommerce.com subscriptions tab.
|
||||
// translators: %s: URL of Woo.com subscriptions tab.
|
||||
__( 'Please visit the <a href="%s" target="_blank">subscriptions page</a> and renew to continue receiving updates.', 'woocommerce' ),
|
||||
esc_url( admin_url( 'admin.php?page=wc-addons§ion=helper' ) )
|
||||
)
|
||||
|
||||
@@ -59,6 +59,7 @@ class WC_Helper {
|
||||
include_once dirname( __FILE__ ) . '/class-wc-helper-plugin-info.php';
|
||||
include_once dirname( __FILE__ ) . '/class-wc-helper-compat.php';
|
||||
include_once dirname( __FILE__ ) . '/class-wc-helper-admin.php';
|
||||
include_once dirname( __FILE__ ) . '/class-wc-helper-subscriptions-api.php';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,14 +113,17 @@ class WC_Helper {
|
||||
$woo_plugins = self::get_local_woo_plugins();
|
||||
$woo_themes = self::get_local_woo_themes();
|
||||
|
||||
$site_id = absint( $auth['site_id'] );
|
||||
$subscriptions = self::get_subscriptions();
|
||||
$subscriptions_list_data = self::get_subscription_list_data();
|
||||
$subscriptions = array_filter(
|
||||
$subscriptions_list_data,
|
||||
function( $subscription ) {
|
||||
return ! empty( $subscription['product_key'] );
|
||||
}
|
||||
);
|
||||
$updates = WC_Helper_Updater::get_update_data();
|
||||
$subscriptions_product_ids = wp_list_pluck( $subscriptions, 'product_id' );
|
||||
|
||||
foreach ( $subscriptions as &$subscription ) {
|
||||
$subscription['active'] = in_array( $site_id, $subscription['connections'] );
|
||||
|
||||
$subscription['activate_url'] = add_query_arg(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
@@ -146,48 +150,24 @@ class WC_Helper {
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
|
||||
$subscription['local'] = array(
|
||||
'installed' => false,
|
||||
'active' => false,
|
||||
'version' => null,
|
||||
);
|
||||
|
||||
$subscription['update_url'] = admin_url( 'update-core.php' );
|
||||
|
||||
$local = wp_list_filter( array_merge( $woo_plugins, $woo_themes ), array( '_product_id' => $subscription['product_id'] ) );
|
||||
|
||||
if ( ! empty( $local ) ) {
|
||||
$local = array_shift( $local );
|
||||
$subscription['local']['installed'] = true;
|
||||
$subscription['local']['version'] = $local['Version'];
|
||||
|
||||
if ( 'plugin' == $local['_type'] ) {
|
||||
if ( is_plugin_active( $local['_filename'] ) ) {
|
||||
$subscription['local']['active'] = true;
|
||||
} elseif ( is_multisite() && is_plugin_active_for_network( $local['_filename'] ) ) {
|
||||
$subscription['local']['active'] = true;
|
||||
}
|
||||
|
||||
$local = array_shift( $local );
|
||||
if ( 'plugin' === $local['_type'] ) {
|
||||
// A magic update_url.
|
||||
$subscription['update_url'] = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $local['_filename'], 'upgrade-plugin_' . $local['_filename'] );
|
||||
|
||||
} elseif ( 'theme' == $local['_type'] ) {
|
||||
if ( in_array( $local['_stylesheet'], array( get_stylesheet(), get_template() ) ) ) {
|
||||
$subscription['local']['active'] = true;
|
||||
}
|
||||
|
||||
} elseif ( 'theme' === $local['_type'] ) {
|
||||
// Another magic update_url.
|
||||
$subscription['update_url'] = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-theme&theme=' . $local['_stylesheet'] ), 'upgrade-theme_' . $local['_stylesheet'] );
|
||||
}
|
||||
}
|
||||
|
||||
$subscription['has_update'] = false;
|
||||
if ( $subscription['local']['installed'] && ! empty( $updates[ $subscription['product_id'] ] ) ) {
|
||||
$subscription['has_update'] = version_compare( $updates[ $subscription['product_id'] ]['version'], $subscription['local']['version'], '>' );
|
||||
}
|
||||
|
||||
$subscription['download_primary'] = true;
|
||||
$subscription['download_url'] = 'https://woocommerce.com/my-account/downloads/';
|
||||
$subscription['download_url'] = 'https://woo.com/my-account/downloads/';
|
||||
if ( ! $subscription['local']['installed'] && ! empty( $updates[ $subscription['product_id'] ] ) ) {
|
||||
$subscription['download_url'] = $updates[ $subscription['product_id'] ]['package'];
|
||||
}
|
||||
@@ -243,7 +223,7 @@ class WC_Helper {
|
||||
'message' => sprintf( __( 'This subscription has expired. Please <strong>renew</strong> to receive updates and support.', 'woocommerce' ) ),
|
||||
'button_label' => __( 'Renew', 'woocommerce' ),
|
||||
'button_url' => self::add_utm_params_to_url_for_subscription_link(
|
||||
'https://woocommerce.com/my-account/my-subscriptions/',
|
||||
'https://woo.com/my-account/my-subscriptions/',
|
||||
'renew'
|
||||
),
|
||||
'status' => 'expired',
|
||||
@@ -258,7 +238,7 @@ class WC_Helper {
|
||||
'message' => __( 'Subscription is <strong>expiring</strong> soon.', 'woocommerce' ),
|
||||
'button_label' => __( 'Enable auto-renew', 'woocommerce' ),
|
||||
'button_url' => self::add_utm_params_to_url_for_subscription_link(
|
||||
'https://woocommerce.com/my-account/my-subscriptions/',
|
||||
'https://woo.com/my-account/my-subscriptions/',
|
||||
'auto-renew'
|
||||
),
|
||||
'status' => 'expired',
|
||||
@@ -272,7 +252,7 @@ class WC_Helper {
|
||||
'message' => sprintf( __( 'This subscription is expiring soon. Please <strong>renew</strong> to continue receiving updates and support.', 'woocommerce' ) ),
|
||||
'button_label' => __( 'Renew', 'woocommerce' ),
|
||||
'button_url' => self::add_utm_params_to_url_for_subscription_link(
|
||||
'https://woocommerce.com/my-account/my-subscriptions/',
|
||||
'https://woo.com/my-account/my-subscriptions/',
|
||||
'renew'
|
||||
),
|
||||
'status' => 'expired',
|
||||
@@ -334,7 +314,7 @@ class WC_Helper {
|
||||
} else {
|
||||
$action = array(
|
||||
/* translators: 1: subscriptions docs 2: subscriptions docs */
|
||||
'message' => sprintf( __( 'To receive updates and support for this extension, you need to <strong>purchase</strong> a new subscription or consolidate your extensions to one connected account by <strong><a href="%1$s" title="Sharing Docs">sharing</a> or <a href="%2$s" title="Transferring Docs">transferring</a></strong> this extension to this connected account.', 'woocommerce' ), 'https://docs.woocommerce.com/document/managing-woocommerce-com-subscriptions/#section-10', 'https://docs.woocommerce.com/document/managing-woocommerce-com-subscriptions/#section-5' ),
|
||||
'message' => sprintf( __( 'To receive updates and support for this extension, you need to <strong>purchase</strong> a new subscription or consolidate your extensions to one connected account by <strong><a href="%1$s" title="Sharing Docs">sharing</a> or <a href="%2$s" title="Transferring Docs">transferring</a></strong> this extension to this connected account.', 'woocommerce' ), 'https://woo.com/document/managing-woocommerce-com-subscriptions/#section-10', 'https://woo.com/document/managing-woocommerce-com-subscriptions/#section-5' ),
|
||||
'button_label' => __( 'Purchase', 'woocommerce' ),
|
||||
'button_url' => self::add_utm_params_to_url_for_subscription_link(
|
||||
$data['_product_url'],
|
||||
@@ -372,7 +352,7 @@ class WC_Helper {
|
||||
/**
|
||||
* Add tracking parameters to buttons (Renew, Purchase, etc.) on subscriptions page
|
||||
*
|
||||
* @param string $url URL to product page or to https://woocommerce.com/my-account/my-subscriptions/
|
||||
* @param string $url URL to product page or to https://woo.com/my-account/my-subscriptions/.
|
||||
* @param string $utm_content value of utm_content query parameter used for tracking
|
||||
*
|
||||
* @return string URL including utm parameters for tracking
|
||||
@@ -633,14 +613,14 @@ class WC_Helper {
|
||||
|
||||
case 'helper-connected':
|
||||
$notices[] = array(
|
||||
'message' => __( 'You have successfully connected your store to WooCommerce.com', 'woocommerce' ),
|
||||
'message' => __( 'You have successfully connected your store to Woo.com', 'woocommerce' ),
|
||||
'type' => 'updated',
|
||||
);
|
||||
break;
|
||||
|
||||
case 'helper-disconnected':
|
||||
$notices[] = array(
|
||||
'message' => __( 'You have successfully disconnected your store from WooCommerce.com', 'woocommerce' ),
|
||||
'message' => __( 'You have successfully disconnected your store from Woo.com', 'woocommerce' ),
|
||||
'type' => 'updated',
|
||||
);
|
||||
break;
|
||||
@@ -693,7 +673,7 @@ class WC_Helper {
|
||||
}
|
||||
|
||||
if ( ! empty( $_GET['wc-helper-deactivate'] ) ) {
|
||||
return self::_helper_subscription_deactivate();
|
||||
return self::helper_subscription_deactivate();
|
||||
}
|
||||
|
||||
if ( ! empty( $_GET['wc-helper-deactivate-plugin'] ) ) {
|
||||
@@ -701,6 +681,32 @@ class WC_Helper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get helper redirect URL.
|
||||
*
|
||||
* @param array $args Query args.
|
||||
* @param bool $redirect_to_wc_admin Whether to redirect to WC Admin.
|
||||
* @return string
|
||||
*/
|
||||
private static function get_helper_redirect_url( $args = array(), $redirect_to_wc_admin = false ) {
|
||||
global $current_screen;
|
||||
if ( true === $redirect_to_wc_admin && 'woocommerce_page_wc-addons' === $current_screen->id ) {
|
||||
return add_query_arg(
|
||||
array(
|
||||
'page' => 'wc-admin',
|
||||
'tab' => 'my-subscriptions',
|
||||
'path' => rawurlencode( '/extensions' ),
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
}
|
||||
|
||||
return add_query_arg(
|
||||
$args,
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate a new OAuth connection.
|
||||
*/
|
||||
@@ -710,13 +716,19 @@ class WC_Helper {
|
||||
wp_die( 'Could not verify nonce' );
|
||||
}
|
||||
|
||||
$redirect_url_args = array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'wc-helper-return' => 1,
|
||||
'wc-helper-nonce' => wp_create_nonce( 'connect' ),
|
||||
);
|
||||
|
||||
if ( isset( $_GET['redirect-to-wc-admin'] ) ) {
|
||||
$redirect_url_args['redirect-to-wc-admin'] = 1;
|
||||
}
|
||||
|
||||
$redirect_uri = add_query_arg(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'wc-helper-return' => 1,
|
||||
'wc-helper-nonce' => wp_create_nonce( 'connect' ),
|
||||
),
|
||||
$redirect_url_args,
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
|
||||
@@ -762,7 +774,7 @@ class WC_Helper {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return from WooCommerce.com OAuth flow.
|
||||
* Return from Woo.com OAuth flow.
|
||||
*/
|
||||
private static function _helper_auth_return() {
|
||||
if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'connect' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
@@ -776,7 +788,16 @@ class WC_Helper {
|
||||
* Fires when the Helper connection process is denied/cancelled.
|
||||
*/
|
||||
do_action( 'woocommerce_helper_denied' );
|
||||
wp_safe_redirect( admin_url( 'admin.php?page=wc-addons§ion=helper' ) );
|
||||
|
||||
wp_safe_redirect(
|
||||
self::get_helper_redirect_url(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
),
|
||||
isset( $_GET['redirect-to-wc-admin'] )
|
||||
)
|
||||
);
|
||||
die();
|
||||
}
|
||||
|
||||
@@ -823,7 +844,7 @@ class WC_Helper {
|
||||
WC_Tracker::send_tracking_data( true );
|
||||
}
|
||||
|
||||
// If connecting through in-app purchase, redirects back to WooCommerce.com
|
||||
// If connecting through in-app purchase, redirects back to Woo.com
|
||||
// for product installation.
|
||||
if ( ! empty( $_GET['wccom-install-url'] ) ) {
|
||||
wp_redirect( wp_unslash( $_GET['wccom-install-url'] ) );
|
||||
@@ -831,20 +852,20 @@ class WC_Helper {
|
||||
}
|
||||
|
||||
wp_safe_redirect(
|
||||
add_query_arg(
|
||||
self::get_helper_redirect_url(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'wc-helper-status' => 'helper-connected',
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
isset( $_GET['redirect-to-wc-admin'] )
|
||||
)
|
||||
);
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect from WooCommerce.com, clear OAuth tokens.
|
||||
* Disconnect from Woo.com, clear OAuth tokens.
|
||||
*/
|
||||
private static function _helper_auth_disconnect() {
|
||||
if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'disconnect' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
@@ -857,13 +878,13 @@ class WC_Helper {
|
||||
*/
|
||||
do_action( 'woocommerce_helper_disconnected' );
|
||||
|
||||
$redirect_uri = add_query_arg(
|
||||
$redirect_uri = self::get_helper_redirect_url(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'wc-helper-status' => 'helper-disconnected',
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
isset( $_GET['redirect-to-wc-admin'] )
|
||||
);
|
||||
|
||||
self::disconnect();
|
||||
@@ -881,27 +902,36 @@ class WC_Helper {
|
||||
wp_die( 'Could not verify nonce' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires when Helper subscriptions are refreshed.
|
||||
*/
|
||||
do_action( 'woocommerce_helper_subscriptions_refresh' );
|
||||
self::refresh_helper_subscriptions();
|
||||
|
||||
$redirect_uri = add_query_arg(
|
||||
$redirect_uri = self::get_helper_redirect_url(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'filter' => self::get_current_filter(),
|
||||
'wc-helper-status' => 'helper-refreshed',
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
isset( $_GET['redirect-to-wc-admin'] )
|
||||
);
|
||||
|
||||
wp_safe_redirect( $redirect_uri );
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush helper authentication cache.
|
||||
*/
|
||||
public static function refresh_helper_subscriptions() {
|
||||
/**
|
||||
* Fires when Helper subscriptions are refreshed.
|
||||
*
|
||||
* @since 8.3.0
|
||||
*/
|
||||
do_action( 'woocommerce_helper_subscriptions_refresh' );
|
||||
|
||||
self::_flush_authentication_cache();
|
||||
self::_flush_subscriptions_cache();
|
||||
self::_flush_updates_cache();
|
||||
|
||||
wp_safe_redirect( $redirect_uri );
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -916,6 +946,41 @@ class WC_Helper {
|
||||
wp_die( 'Could not verify nonce' );
|
||||
}
|
||||
|
||||
try {
|
||||
$activated = self::activate_helper_subscription( $product_key, $product_id );
|
||||
} catch ( Exception $e ) {
|
||||
$activated = false;
|
||||
}
|
||||
|
||||
$redirect_uri = add_query_arg(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'filter' => self::get_current_filter(),
|
||||
'wc-helper-status' => $activated ? 'activate-success' : 'activate-error',
|
||||
'wc-helper-product-id' => $product_id,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
|
||||
wp_safe_redirect( $redirect_uri );
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate helper subscription.
|
||||
*
|
||||
* @throws Exception If the subscription could not be activated or found.
|
||||
* @param string $product_key Subscription product key.
|
||||
* @return bool True if activated, false otherwise.
|
||||
*/
|
||||
public static function activate_helper_subscription( $product_key ) {
|
||||
$subscription = self::get_subscription( $product_key );
|
||||
if ( ! $subscription ) {
|
||||
throw new Exception( __( 'Subscription not found', 'woocommerce' ) );
|
||||
}
|
||||
$product_id = $subscription['product_id'];
|
||||
|
||||
// Activate subscription.
|
||||
$activation_response = WC_Helper_API::post(
|
||||
'activate',
|
||||
@@ -954,6 +1019,7 @@ class WC_Helper {
|
||||
* @param array $activation_response The response object from wp_safe_remote_request().
|
||||
*/
|
||||
do_action( 'woocommerce_helper_subscription_activate_error', $product_id, $product_key, $activation_response );
|
||||
throw new Exception( $body['message'] ?? __( 'Unknown error', 'woocommerce' ) );
|
||||
}
|
||||
|
||||
// Attempt to activate this plugin.
|
||||
@@ -965,12 +1031,33 @@ class WC_Helper {
|
||||
self::_flush_subscriptions_cache();
|
||||
self::_flush_updates_cache();
|
||||
|
||||
return $activated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate a product subscription.
|
||||
*/
|
||||
private static function helper_subscription_deactivate() {
|
||||
$product_key = isset( $_GET['wc-helper-product-key'] ) ? wc_clean( wp_unslash( $_GET['wc-helper-product-key'] ) ) : '';
|
||||
$product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0;
|
||||
|
||||
if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'deactivate:' . $product_key ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
self::log( 'Could not verify nonce in helper_subscription_deactivate' );
|
||||
wp_die( 'Could not verify nonce' );
|
||||
}
|
||||
|
||||
try {
|
||||
$deactivated = self::deactivate_helper_subscription( $product_key );
|
||||
} catch ( Exception $e ) {
|
||||
$deactivated = false;
|
||||
}
|
||||
|
||||
$redirect_uri = add_query_arg(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'filter' => self::get_current_filter(),
|
||||
'wc-helper-status' => $activated ? 'activate-success' : 'activate-error',
|
||||
'wc-helper-status' => $deactivated ? 'deactivate-success' : 'deactivate-error',
|
||||
'wc-helper-product-id' => $product_id,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
@@ -982,15 +1069,17 @@ class WC_Helper {
|
||||
|
||||
/**
|
||||
* Deactivate a product subscription.
|
||||
*
|
||||
* @throws Exception If the subscription could not be deactivated or found.
|
||||
* @param string $product_key Subscription product key.
|
||||
* @return bool True if deactivated, false otherwise.
|
||||
*/
|
||||
private static function _helper_subscription_deactivate() {
|
||||
$product_key = isset( $_GET['wc-helper-product-key'] ) ? wc_clean( wp_unslash( $_GET['wc-helper-product-key'] ) ) : '';
|
||||
$product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0;
|
||||
|
||||
if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'deactivate:' . $product_key ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
self::log( 'Could not verify nonce in _helper_subscription_deactivate' );
|
||||
wp_die( 'Could not verify nonce' );
|
||||
public static function deactivate_helper_subscription( $product_key ) {
|
||||
$subscription = self::get_subscription( $product_key );
|
||||
if ( ! $subscription ) {
|
||||
throw new Exception( __( 'Subscription not found', 'woocommerce' ) );
|
||||
}
|
||||
$product_id = $subscription['product_id'];
|
||||
|
||||
$deactivation_response = WC_Helper_API::post(
|
||||
'deactivate',
|
||||
@@ -1027,23 +1116,14 @@ class WC_Helper {
|
||||
* @param array $deactivation_response The response object from wp_safe_remote_request().
|
||||
*/
|
||||
do_action( 'woocommerce_helper_subscription_deactivate_error', $product_id, $product_key, $deactivation_response );
|
||||
|
||||
$body = json_decode( wp_remote_retrieve_body( $deactivation_response ), true );
|
||||
throw new Exception( $body['message'] ?? __( 'Unknown error', 'woocommerce' ) );
|
||||
}
|
||||
|
||||
self::_flush_subscriptions_cache();
|
||||
|
||||
$redirect_uri = add_query_arg(
|
||||
array(
|
||||
'page' => 'wc-addons',
|
||||
'section' => 'helper',
|
||||
'filter' => self::get_current_filter(),
|
||||
'wc-helper-status' => $deactivated ? 'deactivate-success' : 'deactivate-error',
|
||||
'wc-helper-product-id' => $product_id,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
|
||||
wp_safe_redirect( $redirect_uri );
|
||||
die();
|
||||
return $deactivated;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1148,10 +1228,63 @@ class WC_Helper {
|
||||
if ( ! empty( $subscriptions ) ) {
|
||||
return $single ? array_shift( $subscriptions ) : $subscriptions;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get locally installed plugins
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_local_plugins() {
|
||||
if ( ! function_exists( 'get_plugins' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
$plugins = get_plugins();
|
||||
|
||||
$output_plugins = array();
|
||||
foreach ( $plugins as $filename => $data ) {
|
||||
array_push(
|
||||
$output_plugins,
|
||||
array(
|
||||
'_filename' => $filename,
|
||||
'_type' => 'plugin',
|
||||
'slug' => dirname( $filename ),
|
||||
'Version' => $data['Version'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $output_plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get locally installed themes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_local_themes() {
|
||||
if ( ! function_exists( 'wp_get_themes' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/theme.php';
|
||||
}
|
||||
$themes = wp_get_themes();
|
||||
|
||||
$output_themes = array();
|
||||
foreach ( $themes as $theme ) {
|
||||
array_push(
|
||||
$output_themes,
|
||||
array(
|
||||
'_filename' => $theme->get_stylesheet() . '/style.css',
|
||||
'_stylesheet' => $theme->get_stylesheet(),
|
||||
'_type' => 'theme',
|
||||
'slug' => $theme->get_stylesheet(),
|
||||
'Version' => $theme->get( 'Version' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
return $output_themes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a list of data about locally installed Woo extensions.
|
||||
*/
|
||||
@@ -1256,6 +1389,7 @@ class WC_Helper {
|
||||
'_product_id' => absint( $product_id ),
|
||||
'_file_id' => $file_id,
|
||||
'_type' => 'theme',
|
||||
'slug' => dirname( $theme->get_stylesheet() ),
|
||||
);
|
||||
|
||||
$woo_themes[ $data['_filename'] ] = $data;
|
||||
@@ -1313,6 +1447,256 @@ class WC_Helper {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription data for a given product key.
|
||||
*
|
||||
* @param string $product_key Subscription product key.
|
||||
* @return array|bool The array containing sub data or false.
|
||||
*/
|
||||
public static function get_subscription( $product_key ) {
|
||||
$subscriptions = wp_list_filter(
|
||||
self::get_subscriptions(),
|
||||
array( 'product_key' => $product_key )
|
||||
);
|
||||
|
||||
if ( empty( $subscriptions ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$subscription = array_shift( $subscriptions );
|
||||
$subscription['local'] = self::get_subscription_local_data( $subscription );
|
||||
return $subscription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the connected user's subscription list data.
|
||||
* This is used by the My Subscriptions page.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscription_list_data() {
|
||||
$subscriptions = self::get_subscriptions();
|
||||
|
||||
// Installed plugins and themes, with or without an active subscription.
|
||||
$woo_plugins = self::get_local_woo_plugins();
|
||||
$woo_themes = self::get_local_woo_themes();
|
||||
|
||||
$subscriptions_product_ids = wp_list_pluck( $subscriptions, 'product_id' );
|
||||
|
||||
$auth = WC_Helper_Options::get( 'auth' );
|
||||
$site_id = isset( $auth['site_id'] ) ? absint( $auth['site_id'] ) : 0;
|
||||
|
||||
// Installed products without a subscription.
|
||||
foreach ( array_merge( $woo_plugins, $woo_themes ) as $filename => $data ) {
|
||||
if ( in_array( $data['_product_id'], $subscriptions_product_ids, true ) ) {
|
||||
continue;
|
||||
}
|
||||
$subscriptions[] = array(
|
||||
'product_key' => '',
|
||||
'product_id' => $data['_product_id'],
|
||||
'product_name' => $data['Name'],
|
||||
'product_url' => $data['PluginURI'] ?? '',
|
||||
'zip_slug' => $data['slug'],
|
||||
'documentation_url' => '',
|
||||
'key_type' => '',
|
||||
'key_type_label' => '',
|
||||
'lifetime' => false,
|
||||
'product_status' => 'publish',
|
||||
'connections' => array(),
|
||||
'expires' => 0,
|
||||
'expired' => true,
|
||||
'expiring' => false,
|
||||
'sites_max' => 0,
|
||||
'sites_active' => 0,
|
||||
'autorenew' => false,
|
||||
'maxed' => false,
|
||||
);
|
||||
}
|
||||
|
||||
foreach ( $subscriptions as &$subscription ) {
|
||||
$subscription['active'] = in_array( $site_id, $subscription['connections'], true );
|
||||
|
||||
$updates = WC_Helper_Updater::get_update_data();
|
||||
|
||||
$subscription['local'] = self::get_subscription_local_data( $subscription );
|
||||
|
||||
$subscription['has_update'] = false;
|
||||
if ( $subscription['local']['installed'] && ! empty( $updates[ $subscription['product_id'] ] ) ) {
|
||||
$subscription['has_update'] = version_compare( $updates[ $subscription['product_id'] ]['version'], $subscription['local']['version'], '>' );
|
||||
}
|
||||
|
||||
if ( ! empty( $updates[ $subscription['product_id'] ] ) ) {
|
||||
$subscription['version'] = $updates[ $subscription['product_id'] ]['version'];
|
||||
}
|
||||
}
|
||||
|
||||
// Sort subscriptions by name and expiration date.
|
||||
usort(
|
||||
$subscriptions,
|
||||
function( $a, $b ) {
|
||||
$compare_value = strcasecmp( $a['product_name'], $b['product_name'] );
|
||||
if ( 0 === $compare_value ) {
|
||||
return strcasecmp( $a['expires'], $b['expires'] );
|
||||
}
|
||||
return $compare_value;
|
||||
}
|
||||
);
|
||||
|
||||
// Add subscription install flags after the active and local data is set.
|
||||
foreach ( $subscriptions as &$subscription ) {
|
||||
$subscription['subscription_available'] = self::is_subscription_available( $subscription, $subscriptions );
|
||||
$subscription['subscription_installed'] = self::is_subscription_installed( $subscription, $subscriptions );
|
||||
}
|
||||
|
||||
// Break the by-ref.
|
||||
unset( $subscription );
|
||||
|
||||
return $subscriptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a subscription is available to use.
|
||||
* That is, is not already active and hasn't expired, and there are no other subscriptions
|
||||
* for this product already active on this site.
|
||||
*
|
||||
* @param array $subscription The subscription we're checking.
|
||||
* @param array $subscriptions The list of all the user's subscriptions.
|
||||
* @return bool True if multiple licenses exist, false otherwise.
|
||||
*/
|
||||
public static function is_subscription_available( $subscription, $subscriptions ) {
|
||||
if ( true === $subscription['active'] ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( true === $subscription['expired'] ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$product_subscriptions = wp_list_filter(
|
||||
$subscriptions,
|
||||
array(
|
||||
'product_id' => $subscription['product_id'],
|
||||
'active' => true,
|
||||
)
|
||||
);
|
||||
|
||||
// If there are no subscriptions for this product already active on this site, then it's available.
|
||||
if ( empty( $product_subscriptions ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if product relating to a subscription is installed.
|
||||
* This method will return true if the product is installed, but will exclude subscriptions for the same product that are not in use.
|
||||
* If a product is installed and inactive, this will ensure that one subscription is marked as installed.
|
||||
*
|
||||
* @param array $subscription The subscription we're checking.
|
||||
* @param array $subscriptions The list of all the user's subscriptions.
|
||||
* @return bool True if installed, false otherwise.
|
||||
*/
|
||||
public static function is_subscription_installed( $subscription, $subscriptions ) {
|
||||
if ( false === $subscription['local']['installed'] ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the subscription is active, then it's installed.
|
||||
if ( true === $subscription['active'] ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$product_subscriptions = wp_list_filter(
|
||||
$subscriptions,
|
||||
array(
|
||||
'product_id' => $subscription['product_id'],
|
||||
)
|
||||
);
|
||||
if ( empty( $product_subscriptions ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If there are no other subscriptions for this product, then it's installed.
|
||||
if ( 1 === count( $product_subscriptions ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$active_subscription = wp_list_filter(
|
||||
$product_subscriptions,
|
||||
array(
|
||||
'active' => true,
|
||||
)
|
||||
);
|
||||
|
||||
// If there is another active subscription, this subscription is not installed.
|
||||
// If the current subscription is active, it would already return true above.
|
||||
if ( ! empty( $active_subscription ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If there are multiple subscriptions, but no active subscriptions, then mark the first one as installed.
|
||||
$product_subscription = array_shift( $product_subscriptions );
|
||||
if ( $product_subscription['product_key'] === $subscription['product_key'] ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add local data to a subscription.
|
||||
*
|
||||
* @param array $subscription The subscription data.
|
||||
* @return array The subscription data with local data added.
|
||||
*/
|
||||
public static function get_subscription_local_data( array $subscription ) {
|
||||
$local_plugins = self::get_local_plugins();
|
||||
$local_themes = self::get_local_themes();
|
||||
|
||||
$installed_product = wp_list_filter(
|
||||
array_merge( $local_plugins, $local_themes ),
|
||||
array( 'slug' => $subscription['zip_slug'] )
|
||||
);
|
||||
$installed_product = array_shift( $installed_product );
|
||||
|
||||
if ( empty( $installed_product ) ) {
|
||||
return array(
|
||||
'installed' => false,
|
||||
'active' => false,
|
||||
'version' => null,
|
||||
'type' => null,
|
||||
'slug' => null,
|
||||
'path' => null,
|
||||
);
|
||||
}
|
||||
|
||||
$local_data = array(
|
||||
'installed' => true,
|
||||
'active' => false,
|
||||
'version' => $installed_product['Version'],
|
||||
'type' => $installed_product['_type'],
|
||||
'slug' => null,
|
||||
'path' => $installed_product['_filename'],
|
||||
);
|
||||
|
||||
if ( 'plugin' === $installed_product['_type'] ) {
|
||||
$local_data['slug'] = $installed_product['slug'];
|
||||
if ( is_plugin_active( $installed_product['_filename'] ) ) {
|
||||
$local_data['active'] = true;
|
||||
} elseif ( is_multisite() && is_plugin_active_for_network( $installed_product['_filename'] ) ) {
|
||||
$local_data['active'] = true;
|
||||
}
|
||||
} elseif ( 'theme' === $installed_product['_type'] ) {
|
||||
$local_data['slug'] = $installed_product['_stylesheet'];
|
||||
if ( in_array( $installed_product['_stylesheet'], array( get_stylesheet(), get_template() ), true ) ) {
|
||||
$local_data['active'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $local_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs when any plugin is activated.
|
||||
*
|
||||
|
||||
@@ -21,9 +21,9 @@
|
||||
<?php
|
||||
printf(
|
||||
wp_kses(
|
||||
/* translators: Introduction to list of WooCommerce.com extensions the merchant has subscriptions for. */
|
||||
/* translators: Introduction to list of Woo.com extensions the merchant has subscriptions for. */
|
||||
__(
|
||||
'Below is a list of extensions available on your WooCommerce.com account. To receive extension updates please make sure the extension is installed, and its subscription activated and connected to your WooCommerce.com account. Extensions can be activated from the <a href="%s">Plugins</a> screen.',
|
||||
'Below is a list of extensions available on your Woo.com account. To receive extension updates please make sure the extension is installed, and its subscription activated and connected to your Woo.com account. Extensions can be activated from the <a href="%s">Plugins</a> screen.',
|
||||
'woocommerce'
|
||||
),
|
||||
array(
|
||||
@@ -135,7 +135,7 @@
|
||||
</td>
|
||||
<td class="wp-list-table__ext-actions">
|
||||
<?php if ( ! $subscription['active'] && $subscription['maxed'] ) : ?>
|
||||
<a class="button" href="https://woocommerce.com/my-account/my-subscriptions/" target="_blank"><?php esc_html_e( 'Upgrade', 'woocommerce' ); ?></a>
|
||||
<a class="button" href="https://woo.com/my-account/my-subscriptions/" target="_blank"><?php esc_html_e( 'Upgrade', 'woocommerce' ); ?></a>
|
||||
<?php elseif ( ! $subscription['local']['installed'] && ! $subscription['expired'] ) : ?>
|
||||
<a class="button <?php echo empty( $subscription['download_primary'] ) ? 'button-secondary' : ''; ?>" href="<?php echo esc_url( $subscription['download_url'] ); ?>" target="_blank"><?php esc_html_e( 'Download', 'woocommerce' ); ?></a>
|
||||
<?php elseif ( $subscription['active'] ) : ?>
|
||||
@@ -190,7 +190,7 @@
|
||||
<?php endforeach; ?>
|
||||
<?php else : ?>
|
||||
<tr>
|
||||
<td colspan="3"><em><?php esc_html_e( 'Could not find any subscriptions on your WooCommerce.com account', 'woocommerce' ); ?></td>
|
||||
<td colspan="3"><em><?php esc_html_e( 'Could not find any subscriptions on your Woo.com account', 'woocommerce' ); ?></td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
@@ -198,7 +198,7 @@
|
||||
|
||||
<?php if ( ! empty( $no_subscriptions ) ) : ?>
|
||||
<h2><?php esc_html_e( 'Installed Extensions without a Subscription', 'woocommerce' ); ?></h2>
|
||||
<p>Below is a list of WooCommerce.com products available on your site - but are either out-dated or do not have a valid subscription.</p>
|
||||
<p>Below is a list of Woo.com products available on your site - but are either out-dated or do not have a valid subscription.</p>
|
||||
|
||||
<table class="wp-list-table widefat fixed striped">
|
||||
<?php /* Extensions without a subscription. */ ?>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Admin -> WooCommerce -> Extensions -> WooCommerce.com Subscriptions main page.
|
||||
* Admin -> WooCommerce -> Extensions -> Woo.com Subscriptions main page.
|
||||
*
|
||||
* @package WooCommerce\Views
|
||||
*/
|
||||
@@ -23,7 +23,7 @@ defined( 'ABSPATH' ) || exit();
|
||||
<?php endif; ?>
|
||||
|
||||
<h2><?php esc_html_e( 'Manage your subscriptions, get important product notifications, and updates, all from the convenience of your WooCommerce dashboard', 'woocommerce' ); ?></h2>
|
||||
<p><?php esc_html_e( 'Once connected, your WooCommerce.com purchases will be listed here.', 'woocommerce' ); ?></p>
|
||||
<p><?php esc_html_e( 'Once connected, your Woo.com purchases will be listed here.', 'woocommerce' ); ?></p>
|
||||
<p><a class="button button-primary button-helper-connect" href="<?php echo esc_url( $connect_url ); ?>"><?php esc_html_e( 'Connect', 'woocommerce' ); ?></a></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
<?php defined( 'ABSPATH' ) or exit(); ?>
|
||||
|
||||
<a class="button button-update" href="<?php echo esc_url( $refresh_url ); ?>"><span class="dashicons dashicons-image-rotate"></span> <?php _e( 'Update', 'woocommerce' ); ?></a>
|
||||
<a class="button button-update" href="<?php echo esc_url( $refresh_url ); ?>"><span class="dashicons dashicons-image-rotate"></span> <?php esc_html_e( 'Update', 'woocommerce' ); ?></a>
|
||||
<div class="user-info">
|
||||
<header>
|
||||
<p><?php printf( __( 'Connected to WooCommerce.com', 'woocommerce' ) ); ?> <span class="chevron dashicons dashicons-arrow-down-alt2"></span></p>
|
||||
<p><?php esc_html_e( 'Connected to Woo.com', 'woocommerce' ); ?> <span class="chevron dashicons dashicons-arrow-down-alt2"></span></p>
|
||||
</header>
|
||||
<section>
|
||||
<p><?php echo get_avatar( $auth_user_data['email'], 48 ); ?> <?php echo esc_html( $auth_user_data['email'] ); ?></p>
|
||||
<div class="actions">
|
||||
<a class="" href="https://woocommerce.com/my-account/my-subscriptions/" target="_blank"><span class="dashicons dashicons-admin-generic"></span> <?php _e( 'My Subscriptions', 'woocommerce' ); ?></a>
|
||||
<a class="" href="<?php echo esc_url( $disconnect_url ); ?>"><span class="dashicons dashicons-no"></span> <?php _e( 'Disconnect', 'woocommerce' ); ?></a>
|
||||
<a class="" href="https://woo.com/my-account/my-subscriptions/" target="_blank"><span class="dashicons dashicons-admin-generic"></span> <?php esc_html_e( 'My Subscriptions', 'woocommerce' ); ?></a>
|
||||
<a class="" href="<?php echo esc_url( $disconnect_url ); ?>"><span class="dashicons dashicons-no"></span> <?php esc_html_e( 'Disconnect', 'woocommerce' ); ?></a>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
@@ -21,7 +21,7 @@ defined( 'ABSPATH' ) || exit(); ?>
|
||||
|
||||
<?php
|
||||
$count_html = WC_Helper_Updater::get_updates_count_html();
|
||||
/* translators: %s: WooCommerce.com Subscriptions tab count HTML. */
|
||||
/* translators: %s: Woo.com Subscriptions tab count HTML. */
|
||||
$menu_title = sprintf( __( 'My Subscriptions %s', 'woocommerce' ), $count_html );
|
||||
?>
|
||||
<a href="<?php echo esc_url( admin_url( 'admin.php?page=wc-addons§ion=helper' ) ); ?>" class="nav-tab nav-tab-active"><?php echo wp_kses_post( $menu_title ); ?></a>
|
||||
|
||||
Reference in New Issue
Block a user