views
5 months ago
class-wc-helper-admin.php
4 months ago
class-wc-helper-api.php
1 year ago
class-wc-helper-compat.php
5 years ago
class-wc-helper-options.php
3 years ago
class-wc-helper-orders-api.php
2 years ago
class-wc-helper-sanitization.php
1 year ago
class-wc-helper-subscriptions-api.php
4 months ago
class-wc-helper-updater.php
2 months ago
class-wc-helper.php
4 months ago
class-wc-plugin-api-updater.php
1 year ago
class-wc-product-usage-notice.php
1 year ago
class-wc-woo-helper-connection.php
4 months ago
class-wc-woo-update-manager-plugin.php
2 years ago
class-wc-helper-admin.php
302 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Admin Helper - React admin interface |
| 4 | * |
| 5 | * @package WooCommerce\Admin\Helper |
| 6 | */ |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\Admin\Marketplace; |
| 9 | use Automattic\WooCommerce\Admin\PluginsHelper; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * WC_Helper Class |
| 17 | * |
| 18 | * The main entry-point for all things related to the Helper. |
| 19 | * The Helper manages the connection between the store and |
| 20 | * an account on WooCommerce.com. |
| 21 | */ |
| 22 | class WC_Helper_Admin { |
| 23 | /** |
| 24 | * Clear cache tool identifier. |
| 25 | */ |
| 26 | const CACHE_TOOL_ID = 'clear_woocommerce_helper_cache'; |
| 27 | |
| 28 | /** |
| 29 | * Loads the class, runs on init |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public static function load() { |
| 34 | if ( is_admin() ) { |
| 35 | $is_wc_home_or_in_app_marketplace = ( |
| 36 | isset( $_GET['page'] ) && 'wc-admin' === $_GET['page'] //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 37 | ); |
| 38 | |
| 39 | if ( $is_wc_home_or_in_app_marketplace ) { |
| 40 | add_filter( 'woocommerce_admin_shared_settings', array( __CLASS__, 'add_marketplace_settings' ) ); |
| 41 | } |
| 42 | |
| 43 | add_filter( 'woocommerce_debug_tools', array( __CLASS__, 'register_cache_clear_tool' ) ); |
| 44 | } |
| 45 | |
| 46 | add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Pushes settings onto the WooCommerce Admin global settings object (wcSettings). |
| 51 | * |
| 52 | * @param mixed $settings The settings object we're amending. |
| 53 | * |
| 54 | * @return mixed $settings |
| 55 | */ |
| 56 | public static function add_marketplace_settings( $settings ) { |
| 57 | if ( ! WC_Helper::is_site_connected() && isset( $_GET['connect'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 58 | wp_safe_redirect( self::get_connection_url() ); |
| 59 | exit; |
| 60 | } |
| 61 | |
| 62 | $auth_user_data = WC_Helper_Options::get( 'auth_user_data', array() ); |
| 63 | $auth_user_email = isset( $auth_user_data['email'] ) ? $auth_user_data['email'] : ''; |
| 64 | |
| 65 | // Get the all installed themes and plugins. Knowing this will help us decide to show Add to Store button on product cards. |
| 66 | $installed_products = array_merge( WC_Helper::get_local_plugins(), WC_Helper::get_local_themes() ); |
| 67 | $installed_products = array_map( |
| 68 | function ( $product ) { |
| 69 | return $product['slug']; |
| 70 | }, |
| 71 | $installed_products |
| 72 | ); |
| 73 | |
| 74 | $blog_name = get_bloginfo( 'name' ); |
| 75 | |
| 76 | $settings['wccomHelper'] = array( |
| 77 | 'isConnected' => WC_Helper::is_site_connected(), |
| 78 | 'connectURL' => self::get_connection_url(), |
| 79 | 'reConnectURL' => self::get_connection_url( true ), |
| 80 | 'userEmail' => $auth_user_email, |
| 81 | 'userAvatar' => get_avatar_url( $auth_user_email, array( 'size' => '48' ) ), |
| 82 | 'storeCountry' => wc_get_base_location()['country'], |
| 83 | 'storeName' => $blog_name ? $blog_name : '', |
| 84 | 'inAppPurchaseURLParams' => WC_Admin_Addons::get_in_app_purchase_url_params(), |
| 85 | 'installedProducts' => $installed_products, |
| 86 | 'mySubscriptionsTabLoaded' => WC_Helper_Options::get( 'my_subscriptions_tab_loaded' ), |
| 87 | 'wooUpdateManagerInstalled' => WC_Woo_Update_Manager_Plugin::is_plugin_installed(), |
| 88 | 'wooUpdateManagerActive' => WC_Woo_Update_Manager_Plugin::is_plugin_active(), |
| 89 | 'wooUpdateManagerInstallUrl' => WC_Woo_Update_Manager_Plugin::generate_install_url(), |
| 90 | 'wooUpdateManagerPluginSlug' => WC_Woo_Update_Manager_Plugin::WOO_UPDATE_MANAGER_SLUG, |
| 91 | 'dismissNoticeNonce' => wp_create_nonce( 'dismiss_notice' ), |
| 92 | 'trackingAllowed' => 'yes' === get_option( 'woocommerce_allow_tracking' ), |
| 93 | ); |
| 94 | |
| 95 | // This data is only used in the `Extensions` screen, so only populate it there. |
| 96 | // More specifically, it's used in `My Subscriptions`, however, switching tabs doesn't require |
| 97 | // a page reload, so we just check for `path` (/extensions), rather than `tab` (my-subscriptions). |
| 98 | if ( ! empty( $_GET['path'] ) && '/extensions' === $_GET['path'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 99 | $settings['wccomHelper']['wooUpdateCount'] = WC_Helper_Updater::get_updates_count_based_on_site_status(); |
| 100 | $settings['wccomHelper']['connected_notice'] = PluginsHelper::get_wccom_connected_notice( $auth_user_email ); |
| 101 | $settings['wccomHelper']['woocomConnectNoticeType'] = WC_Helper_Updater::get_woo_connect_notice_type(); |
| 102 | |
| 103 | if ( WC_Helper::is_site_connected() ) { |
| 104 | $settings['wccomHelper']['subscription_expired_notice'] = PluginsHelper::get_expired_subscription_notice( false ); |
| 105 | $settings['wccomHelper']['subscription_expiring_notice'] = PluginsHelper::get_expiring_subscription_notice( false ); |
| 106 | $settings['wccomHelper']['subscription_missing_notice'] = PluginsHelper::get_missing_subscription_notice(); |
| 107 | $settings['wccomHelper']['connection_url_notice'] = WC_Woo_Helper_Connection::get_connection_url_notice(); |
| 108 | $settings['wccomHelper']['has_host_plan_orders'] = WC_Woo_Helper_Connection::has_host_plan_orders(); |
| 109 | $settings['wccomHelper']['maybe_deleted_connection'] = WC_Woo_Helper_Connection::get_deleted_connection_notice(); |
| 110 | } else { |
| 111 | $settings['wccomHelper']['disconnected_notice'] = PluginsHelper::get_wccom_disconnected_notice(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return $settings; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Generates the URL for connecting or disconnecting the store to/from WooCommerce.com. |
| 120 | * Approach taken from existing helper code that isn't exposed. |
| 121 | * |
| 122 | * @param bool $reconnect indicate if the site is being reconnected. |
| 123 | * |
| 124 | * @return string |
| 125 | */ |
| 126 | public static function get_connection_url( $reconnect = false ) { |
| 127 | // Default to wc-addons, although this can be changed from the frontend |
| 128 | // in the function `connectUrl()` within marketplace functions.tsx. |
| 129 | $connect_url_args = array( |
| 130 | 'page' => 'wc-addons', |
| 131 | 'section' => 'helper', |
| 132 | ); |
| 133 | |
| 134 | // No active connection. |
| 135 | if ( WC_Helper::is_site_connected() && ! $reconnect ) { |
| 136 | $connect_url_args['wc-helper-disconnect'] = 1; |
| 137 | $connect_url_args['wc-helper-nonce'] = wp_create_nonce( 'disconnect' ); |
| 138 | } else { |
| 139 | $connect_url_args['wc-helper-connect'] = 1; |
| 140 | $connect_url_args['wc-helper-nonce'] = wp_create_nonce( 'connect' ); |
| 141 | } |
| 142 | |
| 143 | if ( ! empty( $_GET['utm_source'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 144 | $connect_url_args['utm_source'] = wc_clean( wp_unslash( $_GET['utm_source'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 145 | } |
| 146 | |
| 147 | if ( ! empty( $_GET['utm_campaign'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 148 | $connect_url_args['utm_campaign'] = wc_clean( wp_unslash( $_GET['utm_campaign'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 149 | } |
| 150 | |
| 151 | return add_query_arg( |
| 152 | $connect_url_args, |
| 153 | admin_url( 'admin.php' ) |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Registers the REST routes for the featured products and product |
| 159 | * previews endpoints. |
| 160 | */ |
| 161 | public static function register_rest_routes() { |
| 162 | /* Used by the WooCommerce > Extensions > Discover page. */ |
| 163 | register_rest_route( |
| 164 | 'wc/v3', |
| 165 | '/marketplace/featured', |
| 166 | array( |
| 167 | 'methods' => 'GET', |
| 168 | 'callback' => array( __CLASS__, 'get_featured' ), |
| 169 | 'permission_callback' => array( __CLASS__, 'get_permission' ), |
| 170 | ) |
| 171 | ); |
| 172 | |
| 173 | /* Used to show previews of products in a modal in in-app marketplace. */ |
| 174 | register_rest_route( |
| 175 | 'wc/v1', |
| 176 | '/marketplace/product-preview', |
| 177 | array( |
| 178 | 'methods' => 'GET', |
| 179 | 'callback' => array( __CLASS__, 'get_product_preview' ), |
| 180 | 'permission_callback' => array( __CLASS__, 'get_permission' ), |
| 181 | ) |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * The Extensions page can only be accessed by users with the manage_woocommerce |
| 187 | * capability. So the API mimics that behavior. |
| 188 | */ |
| 189 | public static function get_permission() { |
| 190 | return current_user_can( 'manage_woocommerce' ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Fetch featured products from WooCommerce.com and serve them |
| 195 | * as JSON. |
| 196 | */ |
| 197 | public static function get_featured() { |
| 198 | $featured = WC_Admin_Addons::fetch_featured(); |
| 199 | |
| 200 | if ( is_wp_error( $featured ) ) { |
| 201 | wp_send_json_error( array( 'message' => $featured->get_error_message() ) ); |
| 202 | } |
| 203 | |
| 204 | wp_send_json( $featured ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Fetch data for product previews from WooCommerce.com. |
| 209 | * |
| 210 | * @param WP_REST_Request $request Request object. |
| 211 | */ |
| 212 | public static function get_product_preview( $request ) { |
| 213 | $product_id = (int) $request->get_param( 'product_id' ); |
| 214 | |
| 215 | if ( ! $product_id ) { |
| 216 | wp_send_json_error( |
| 217 | array( |
| 218 | 'message' => __( 'Missing product ID', 'woocommerce' ), |
| 219 | ), |
| 220 | 400 |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | $product_preview = WC_Admin_Addons::fetch_product_preview( $product_id ); |
| 225 | |
| 226 | if ( ! $product_preview ) { |
| 227 | wp_send_json_error( |
| 228 | array( |
| 229 | 'message' => __( 'We couldn\'t find a preview for this product.', 'woocommerce' ), |
| 230 | ), |
| 231 | 404 |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | if ( is_wp_error( $product_preview ) ) { |
| 236 | wp_send_json_error( |
| 237 | array( |
| 238 | 'message' => $product_preview->get_error_message(), |
| 239 | ) |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | if ( |
| 244 | ! isset( $product_preview['css'] ) |
| 245 | || ! is_string( $product_preview['css'] ) |
| 246 | || ! isset( $product_preview['html'] ) |
| 247 | || ! is_string( $product_preview['html'] ) |
| 248 | ) { |
| 249 | wp_send_json_error( |
| 250 | array( |
| 251 | 'message' => __( |
| 252 | 'API response is missing required elements, or they are in the wrong form.', |
| 253 | 'woocommerce' |
| 254 | ), |
| 255 | ), |
| 256 | 500 |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | $sanitized_product_preview = array( |
| 261 | 'css' => WC_Helper_Sanitization::sanitize_css( $product_preview['css'] ), |
| 262 | 'html' => WC_Helper_Sanitization::sanitize_html( $product_preview['html'] ), |
| 263 | ); |
| 264 | |
| 265 | wp_send_json( $sanitized_product_preview ); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Register the cache clearing tool on the WooCommerce > Status > Tools page. |
| 270 | * |
| 271 | * @param array $debug_tools Available debug tool registrations. |
| 272 | * @return array Filtered debug tool registrations. |
| 273 | */ |
| 274 | public static function register_cache_clear_tool( $debug_tools ) { |
| 275 | $debug_tools[ self::CACHE_TOOL_ID ] = array( |
| 276 | 'name' => __( 'Clear WooCommerce.com cache', 'woocommerce' ), |
| 277 | 'button' => __( 'Clear', 'woocommerce' ), |
| 278 | 'desc' => sprintf( |
| 279 | __( 'This tool will empty the WooCommerce.com data cache, used in WooCommerce Extensions.', 'woocommerce' ), |
| 280 | ), |
| 281 | 'callback' => array( __CLASS__, 'run_clear_cache_tool' ), |
| 282 | ); |
| 283 | |
| 284 | return $debug_tools; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /** |
| 289 | * "Clear" helper cache by invalidating it. |
| 290 | */ |
| 291 | public static function run_clear_cache_tool() { |
| 292 | WC_Helper::_flush_subscriptions_cache(); |
| 293 | WC_Helper::flush_product_usage_notice_rules_cache(); |
| 294 | WC_Helper::flush_connection_data_cache(); |
| 295 | WC_Helper_Updater::flush_updates_cache(); |
| 296 | |
| 297 | return __( 'Helper cache cleared.', 'woocommerce' ); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | WC_Helper_Admin::load(); |
| 302 |