views
7 months ago
class-wc-helper-admin.php
6 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 weeks ago
class-wc-helper-updater.php
2 weeks ago
class-wc-helper.php
2 weeks 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
6 months ago
class-wc-woo-update-manager-plugin.php
2 years ago
class-wc-helper-compat.php
205 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Admin Helper Compat |
| 4 | * |
| 5 | * @package WooCommerce\Admin\Helper |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * WC_Helper_Compat Class |
| 14 | * |
| 15 | * Some level of compatibility with the legacy WooCommerce Helper plugin. |
| 16 | */ |
| 17 | class WC_Helper_Compat { |
| 18 | |
| 19 | /** |
| 20 | * Loads the class, runs on init. |
| 21 | */ |
| 22 | public static function load() { |
| 23 | add_action( 'woocommerce_helper_loaded', array( __CLASS__, 'helper_loaded' ) ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Runs during woocommerce_helper_loaded |
| 28 | */ |
| 29 | public static function helper_loaded() { |
| 30 | // Stop the nagging about WooThemes Updater |
| 31 | remove_action( 'admin_notices', 'woothemes_updater_notice' ); |
| 32 | |
| 33 | // A placeholder dashboard menu for legacy helper users. |
| 34 | add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) ); |
| 35 | |
| 36 | if ( empty( $GLOBALS['woothemes_updater'] ) ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | self::remove_actions(); |
| 41 | self::migrate_connection(); |
| 42 | self::deactivate_plugin(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Remove legacy helper actions (notices, menus, etc.) |
| 47 | */ |
| 48 | public static function remove_actions() { |
| 49 | // Remove WooThemes Updater notices |
| 50 | remove_action( 'network_admin_notices', array( $GLOBALS['woothemes_updater']->admin, 'maybe_display_activation_notice' ) ); |
| 51 | remove_action( 'admin_notices', array( $GLOBALS['woothemes_updater']->admin, 'maybe_display_activation_notice' ) ); |
| 52 | remove_action( 'network_admin_menu', array( $GLOBALS['woothemes_updater']->admin, 'register_settings_screen' ) ); |
| 53 | remove_action( 'admin_menu', array( $GLOBALS['woothemes_updater']->admin, 'register_settings_screen' ) ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Attempt to migrate a legacy connection to a new one. |
| 58 | */ |
| 59 | public static function migrate_connection() { |
| 60 | // Don't attempt to migrate if attempted before. |
| 61 | if ( WC_Helper_Options::get( 'did-migrate' ) ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | $auth = WC_Helper_Options::get( 'auth' ); |
| 66 | if ( ! empty( $auth ) ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | WC_Helper::log( 'Attempting oauth/migrate' ); |
| 71 | WC_Helper_Options::update( 'did-migrate', true ); |
| 72 | |
| 73 | $master_key = get_option( 'woothemes_helper_master_key' ); |
| 74 | if ( empty( $master_key ) ) { |
| 75 | WC_Helper::log( 'Master key not found, aborting' ); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | $request = WC_Helper_API::post( |
| 80 | 'oauth/migrate', |
| 81 | array( |
| 82 | 'body' => array( |
| 83 | 'home_url' => home_url(), |
| 84 | 'master_key' => $master_key, |
| 85 | ), |
| 86 | ) |
| 87 | ); |
| 88 | |
| 89 | if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) !== 200 ) { |
| 90 | WC_Helper::log( 'Call to oauth/migrate returned a non-200 response code' ); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | $request_token = json_decode( wp_remote_retrieve_body( $request ) ); |
| 95 | if ( empty( $request_token ) ) { |
| 96 | WC_Helper::log( 'Call to oauth/migrate returned an empty token' ); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Obtain an access token. |
| 101 | $request = WC_Helper_API::post( |
| 102 | 'oauth/access_token', |
| 103 | array( |
| 104 | 'body' => array( |
| 105 | 'request_token' => $request_token, |
| 106 | 'home_url' => home_url(), |
| 107 | 'migrate' => true, |
| 108 | ), |
| 109 | ) |
| 110 | ); |
| 111 | |
| 112 | if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) !== 200 ) { |
| 113 | WC_Helper::log( 'Call to oauth/access_token returned a non-200 response code' ); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | $access_token = json_decode( wp_remote_retrieve_body( $request ), true ); |
| 118 | if ( empty( $access_token ) ) { |
| 119 | WC_Helper::log( 'Call to oauth/access_token returned an invalid token' ); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | WC_Helper_Options::update( |
| 124 | 'auth', |
| 125 | array( |
| 126 | 'access_token' => $access_token['access_token'], |
| 127 | 'access_token_secret' => $access_token['access_token_secret'], |
| 128 | 'site_id' => $access_token['site_id'], |
| 129 | 'user_id' => null, // Set this later |
| 130 | 'updated' => time(), |
| 131 | ) |
| 132 | ); |
| 133 | |
| 134 | // Obtain the connected user info. |
| 135 | if ( ! WC_Helper::_flush_authentication_cache() ) { |
| 136 | WC_Helper::log( 'Could not obtain connected user info in migrate_connection' ); |
| 137 | WC_Helper_Options::update( 'auth', array() ); |
| 138 | return; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Attempt to deactivate the legacy helper plugin. |
| 144 | */ |
| 145 | public static function deactivate_plugin() { |
| 146 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 147 | if ( ! function_exists( 'deactivate_plugins' ) ) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | if ( is_plugin_active( 'woothemes-updater/woothemes-updater.php' ) ) { |
| 152 | deactivate_plugins( 'woothemes-updater/woothemes-updater.php' ); |
| 153 | |
| 154 | // Notify the user when the plugin is deactivated. |
| 155 | add_action( 'pre_current_active_plugins', array( __CLASS__, 'plugin_deactivation_notice' ) ); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Display admin notice directing the user where to go. |
| 161 | */ |
| 162 | public static function plugin_deactivation_notice() { |
| 163 | ?> |
| 164 | <div id="message" class="error is-dismissible"> |
| 165 | <p><?php printf( __( 'The WooCommerce Helper plugin is no longer needed. <a href="%s">Manage subscriptions</a> from the extensions tab instead.', 'woocommerce' ), esc_url( admin_url( 'admin.php?page=wc-addons§ion=helper' ) ) ); ?></p> |
| 166 | </div> |
| 167 | <?php |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Register menu item. |
| 172 | */ |
| 173 | public static function admin_menu() { |
| 174 | // No additional menu items for users who did not have a connected helper before. |
| 175 | $master_key = get_option( 'woothemes_helper_master_key' ); |
| 176 | if ( empty( $master_key ) ) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | // Do not show the menu item if user has already seen the new screen. |
| 181 | $auth = WC_Helper_Options::get( 'auth' ); |
| 182 | if ( ! empty( $auth['user_id'] ) ) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | add_dashboard_page( __( 'WooCommerce Helper', 'woocommerce' ), __( 'WooCommerce Helper', 'woocommerce' ), 'manage_options', 'woothemes-helper', array( __CLASS__, 'render_compat_menu' ) ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Render the legacy helper compat view. |
| 191 | */ |
| 192 | public static function render_compat_menu() { |
| 193 | $helper_url = add_query_arg( |
| 194 | array( |
| 195 | 'page' => 'wc-addons', |
| 196 | 'section' => 'helper', |
| 197 | ), |
| 198 | admin_url( 'admin.php' ) |
| 199 | ); |
| 200 | include WC_Helper::get_view_filename( 'html-helper-compat.php' ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | WC_Helper_Compat::load(); |
| 205 |