Нема описа

class-wc-helper-compat.php 5.7KB

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