class-wc-helper-compat.php 5.6 KB

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