class-wc-helper.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * WC_Helper Class
  7. *
  8. * The main entry-point for all things related to the Helper.
  9. */
  10. class WC_Helper {
  11. /**
  12. * A log object returned by wc_get_logger().
  13. */
  14. public static $log;
  15. /**
  16. * Get an absolute path to the requested helper view.
  17. *
  18. * @param string $view The requested view file.
  19. *
  20. * @return string The absolute path to the view file.
  21. */
  22. public static function get_view_filename( $view ) {
  23. return __DIR__ . "/views/$view";
  24. }
  25. /**
  26. * Loads the helper class, runs on init.
  27. */
  28. public static function load() {
  29. add_action( 'current_screen', array( __CLASS__, 'current_screen' ) );
  30. add_action( 'woocommerce_helper_output', array( __CLASS__, 'render_helper_output' ) );
  31. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
  32. add_filter( 'extra_plugin_headers', array( __CLASS__, 'extra_headers' ) );
  33. add_filter( 'extra_theme_headers', array( __CLASS__, 'extra_headers' ) );
  34. // Attempt to toggle subscription state upon plugin activation/deactivation
  35. add_action( 'activated_plugin', array( __CLASS__, 'activated_plugin' ) );
  36. add_action( 'deactivated_plugin', array( __CLASS__, 'deactivated_plugin' ) );
  37. // Add some nags about extension updates
  38. add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
  39. do_action( 'woocommerce_helper_loaded' );
  40. }
  41. /**
  42. * Render the helper section content based on context.
  43. */
  44. public static function render_helper_output() {
  45. $auth = WC_Helper_Options::get( 'auth' );
  46. $auth_user_data = WC_Helper_Options::get( 'auth_user_data' );
  47. // Return success/error notices.
  48. $notices = self::_get_return_notices();
  49. // No active connection.
  50. if ( empty( $auth['access_token'] ) ) {
  51. $connect_url = add_query_arg(
  52. array(
  53. 'page' => 'wc-addons',
  54. 'section' => 'helper',
  55. 'wc-helper-connect' => 1,
  56. 'wc-helper-nonce' => wp_create_nonce( 'connect' ),
  57. ), admin_url( 'admin.php' )
  58. );
  59. include self::get_view_filename( 'html-oauth-start.php' );
  60. return;
  61. }
  62. $disconnect_url = add_query_arg(
  63. array(
  64. 'page' => 'wc-addons',
  65. 'section' => 'helper',
  66. 'wc-helper-disconnect' => 1,
  67. 'wc-helper-nonce' => wp_create_nonce( 'disconnect' ),
  68. ), admin_url( 'admin.php' )
  69. );
  70. $current_filter = self::get_current_filter();
  71. $refresh_url = add_query_arg(
  72. array(
  73. 'page' => 'wc-addons',
  74. 'section' => 'helper',
  75. 'filter' => $current_filter,
  76. 'wc-helper-refresh' => 1,
  77. 'wc-helper-nonce' => wp_create_nonce( 'refresh' ),
  78. ), admin_url( 'admin.php' )
  79. );
  80. // Installed plugins and themes, with or without an active subscription.
  81. $woo_plugins = self::get_local_woo_plugins();
  82. $woo_themes = self::get_local_woo_themes();
  83. $site_id = absint( $auth['site_id'] );
  84. $subscriptions = self::get_subscriptions();
  85. $updates = WC_Helper_Updater::get_update_data();
  86. $subscriptions_product_ids = wp_list_pluck( $subscriptions, 'product_id' );
  87. foreach ( $subscriptions as &$subscription ) {
  88. $subscription['active'] = in_array( $site_id, $subscription['connections'] );
  89. $subscription['activate_url'] = add_query_arg(
  90. array(
  91. 'page' => 'wc-addons',
  92. 'section' => 'helper',
  93. 'filter' => $current_filter,
  94. 'wc-helper-activate' => 1,
  95. 'wc-helper-product-key' => $subscription['product_key'],
  96. 'wc-helper-product-id' => $subscription['product_id'],
  97. 'wc-helper-nonce' => wp_create_nonce( 'activate:' . $subscription['product_key'] ),
  98. ), admin_url( 'admin.php' )
  99. );
  100. $subscription['deactivate_url'] = add_query_arg(
  101. array(
  102. 'page' => 'wc-addons',
  103. 'section' => 'helper',
  104. 'filter' => $current_filter,
  105. 'wc-helper-deactivate' => 1,
  106. 'wc-helper-product-key' => $subscription['product_key'],
  107. 'wc-helper-product-id' => $subscription['product_id'],
  108. 'wc-helper-nonce' => wp_create_nonce( 'deactivate:' . $subscription['product_key'] ),
  109. ), admin_url( 'admin.php' )
  110. );
  111. $subscription['local'] = array(
  112. 'installed' => false,
  113. 'active' => false,
  114. 'version' => null,
  115. );
  116. $subscription['update_url'] = admin_url( 'update-core.php' );
  117. $local = wp_list_filter( array_merge( $woo_plugins, $woo_themes ), array( '_product_id' => $subscription['product_id'] ) );
  118. if ( ! empty( $local ) ) {
  119. $local = array_shift( $local );
  120. $subscription['local']['installed'] = true;
  121. $subscription['local']['version'] = $local['Version'];
  122. if ( 'plugin' == $local['_type'] ) {
  123. if ( is_plugin_active( $local['_filename'] ) ) {
  124. $subscription['local']['active'] = true;
  125. } elseif ( is_multisite() && is_plugin_active_for_network( $local['_filename'] ) ) {
  126. $subscription['local']['active'] = true;
  127. }
  128. // A magic update_url.
  129. $subscription['update_url'] = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $local['_filename'], 'upgrade-plugin_' . $local['_filename'] );
  130. } elseif ( 'theme' == $local['_type'] ) {
  131. if ( in_array( $local['_stylesheet'], array( get_stylesheet(), get_template() ) ) ) {
  132. $subscription['local']['active'] = true;
  133. }
  134. // Another magic update_url.
  135. $subscription['update_url'] = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-theme&theme=' . $local['_stylesheet'] ), 'upgrade-theme_' . $local['_stylesheet'] );
  136. }
  137. }
  138. $subscription['has_update'] = false;
  139. if ( $subscription['local']['installed'] && ! empty( $updates[ $subscription['product_id'] ] ) ) {
  140. $subscription['has_update'] = version_compare( $updates[ $subscription['product_id'] ]['version'], $subscription['local']['version'], '>' );
  141. }
  142. $subscription['download_primary'] = true;
  143. $subscription['download_url'] = $subscription['product_url'];
  144. if ( ! $subscription['local']['installed'] && ! empty( $updates[ $subscription['product_id'] ] ) ) {
  145. $subscription['download_url'] = $updates[ $subscription['product_id'] ]['package'];
  146. }
  147. $subscription['actions'] = array();
  148. if ( $subscription['has_update'] && ! $subscription['expired'] ) {
  149. $action = array(
  150. /* translators: %s: version number */
  151. 'message' => sprintf( __( 'Version %s is <strong>available</strong>.', 'woocommerce' ), esc_html( $updates[ $subscription['product_id'] ]['version'] ) ),
  152. 'button_label' => __( 'Update', 'woocommerce' ),
  153. 'button_url' => $subscription['update_url'],
  154. 'status' => 'update-available',
  155. 'icon' => 'dashicons-update',
  156. );
  157. // Subscription is not active on this site.
  158. if ( ! $subscription['active'] ) {
  159. $action['message'] .= ' ' . __( 'To enable this update you need to <strong>activate</strong> this subscription.', 'woocommerce' );
  160. $action['button_label'] = null;
  161. $action['button_url'] = null;
  162. }
  163. $subscription['actions'][] = $action;
  164. }
  165. if ( $subscription['has_update'] && $subscription['expired'] ) {
  166. $action = array(
  167. /* translators: %s: version number */
  168. 'message' => sprintf( __( 'Version %s is <strong>available</strong>.', 'woocommerce' ), esc_html( $updates[ $subscription['product_id'] ]['version'] ) ),
  169. 'status' => 'expired',
  170. 'icon' => 'dashicons-info',
  171. );
  172. $action['message'] .= ' ' . __( 'To enable this update you need to <strong>purchase</strong> a new subscription.', 'woocommerce' );
  173. $action['button_label'] = __( 'Purchase', 'woocommerce' );
  174. $action['button_url'] = $subscription['product_url'];
  175. $subscription['actions'][] = $action;
  176. } elseif ( $subscription['expired'] && ! empty( $subscription['master_user_email'] ) ) {
  177. $action = array(
  178. 'message' => sprintf( __( 'This subscription has expired. Contact the owner to <strong>renew</strong> the subscription to receive updates and support.', 'woocommerce' ) ),
  179. 'status' => 'expired',
  180. 'icon' => 'dashicons-info',
  181. );
  182. $subscription['actions'][] = $action;
  183. } elseif ( $subscription['expired'] ) {
  184. $action = array(
  185. 'message' => sprintf( __( 'This subscription has expired. Please <strong>renew</strong> to receive updates and support.', 'woocommerce' ) ),
  186. 'button_label' => __( 'Renew', 'woocommerce' ),
  187. 'button_url' => 'https://woocommerce.com/my-account/my-subscriptions/',
  188. 'status' => 'expired',
  189. 'icon' => 'dashicons-info',
  190. );
  191. $subscription['actions'][] = $action;
  192. }
  193. if ( $subscription['expiring'] && ! $subscription['autorenew'] ) {
  194. $action = array(
  195. 'message' => __( 'Subscription is <strong>expiring</strong> soon.', 'woocommerce' ),
  196. 'button_label' => __( 'Enable auto-renew', 'woocommerce' ),
  197. 'button_url' => 'https://woocommerce.com/my-account/my-subscriptions/',
  198. 'status' => 'expired',
  199. 'icon' => 'dashicons-info',
  200. );
  201. $subscription['download_primary'] = false;
  202. $subscription['actions'][] = $action;
  203. } elseif ( $subscription['expiring'] ) {
  204. $action = array(
  205. 'message' => sprintf( __( 'This subscription is expiring soon. Please <strong>renew</strong> to continue receiving updates and support.', 'woocommerce' ) ),
  206. 'button_label' => __( 'Renew', 'woocommerce' ),
  207. 'button_url' => 'https://woocommerce.com/my-account/my-subscriptions/',
  208. 'status' => 'expired',
  209. 'icon' => 'dashicons-info',
  210. );
  211. $subscription['download_primary'] = false;
  212. $subscription['actions'][] = $action;
  213. }
  214. // Mark the first action primary.
  215. foreach ( $subscription['actions'] as $key => $action ) {
  216. if ( ! empty( $action['button_label'] ) ) {
  217. $subscription['actions'][ $key ]['primary'] = true;
  218. break;
  219. }
  220. }
  221. }
  222. // Break the by-ref.
  223. unset( $subscription );
  224. // Installed products without a subscription.
  225. $no_subscriptions = array();
  226. foreach ( array_merge( $woo_plugins, $woo_themes ) as $filename => $data ) {
  227. if ( in_array( $data['_product_id'], $subscriptions_product_ids ) ) {
  228. continue;
  229. }
  230. $data['_product_url'] = '#';
  231. $data['_has_update'] = false;
  232. if ( ! empty( $updates[ $data['_product_id'] ] ) ) {
  233. $data['_has_update'] = version_compare( $updates[ $data['_product_id'] ]['version'], $data['Version'], '>' );
  234. if ( ! empty( $updates[ $data['_product_id'] ]['url'] ) ) {
  235. $data['_product_url'] = $updates[ $data['_product_id'] ]['url'];
  236. } elseif ( ! empty( $data['PluginURI'] ) ) {
  237. $data['_product_url'] = $data['PluginURI'];
  238. }
  239. }
  240. $data['_actions'] = array();
  241. if ( $data['_has_update'] ) {
  242. $action = array(
  243. 'message' => sprintf( __( 'Version %s is <strong>available</strong>. To enable this update you need to <strong>purchase</strong> a new subscription.', 'woocommerce' ), esc_html( $updates[ $data['_product_id'] ]['version'] ) ),
  244. 'button_label' => __( 'Purchase', 'woocommerce' ),
  245. 'button_url' => $data['_product_url'],
  246. 'status' => 'expired',
  247. 'icon' => 'dashicons-info',
  248. );
  249. $data['_actions'][] = $action;
  250. } else {
  251. $action = array(
  252. /* translators: 1: subscriptions docs 2: subscriptions docs */
  253. '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' ),
  254. 'button_label' => __( 'Purchase', 'woocommerce' ),
  255. 'button_url' => $data['_product_url'],
  256. 'status' => 'expired',
  257. 'icon' => 'dashicons-info',
  258. );
  259. $data['_actions'][] = $action;
  260. }
  261. $no_subscriptions[ $filename ] = $data;
  262. }
  263. // Update the user id if it came from a migrated connection.
  264. if ( empty( $auth['user_id'] ) ) {
  265. $auth['user_id'] = get_current_user_id();
  266. WC_Helper_Options::update( 'auth', $auth );
  267. }
  268. // Sort alphabetically
  269. uasort( $subscriptions, array( __CLASS__, '_sort_by_product_name' ) );
  270. uasort( $no_subscriptions, array( __CLASS__, '_sort_by_name' ) );
  271. // Filters
  272. self::get_filters_counts( $subscriptions ); // Warm it up.
  273. self::_filter( $subscriptions, self::get_current_filter() );
  274. // We have an active connection.
  275. include self::get_view_filename( 'html-main.php' );
  276. return;
  277. }
  278. /**
  279. * Get available subscriptions filters.
  280. *
  281. * @return array An array of filter keys and labels.
  282. */
  283. public static function get_filters() {
  284. $filters = array(
  285. 'all' => __( 'All', 'woocommerce' ),
  286. 'active' => __( 'Active', 'woocommerce' ),
  287. 'inactive' => __( 'Inactive', 'woocommerce' ),
  288. 'installed' => __( 'Installed', 'woocommerce' ),
  289. 'update-available' => __( 'Update Available', 'woocommerce' ),
  290. 'expiring' => __( 'Expiring Soon', 'woocommerce' ),
  291. 'expired' => __( 'Expired', 'woocommerce' ),
  292. 'download' => __( 'Download', 'woocommerce' ),
  293. );
  294. return $filters;
  295. }
  296. /**
  297. * Get counts data for the filters array.
  298. *
  299. * @param array $subscriptions The array of all available subscriptions.
  300. *
  301. * @return array Filter counts (filter => count)
  302. */
  303. public static function get_filters_counts( $subscriptions = null ) {
  304. static $filters;
  305. if ( isset( $filters ) ) {
  306. return $filters;
  307. }
  308. $filters = array_fill_keys( array_keys( self::get_filters() ), 0 );
  309. if ( empty( $subscriptions ) ) {
  310. return array();
  311. }
  312. foreach ( $filters as $key => $count ) {
  313. $_subs = $subscriptions;
  314. self::_filter( $_subs, $key );
  315. $filters[ $key ] = count( $_subs );
  316. }
  317. return $filters;
  318. }
  319. /**
  320. * Get current filter.
  321. *
  322. * @return string The current filter.
  323. */
  324. public static function get_current_filter() {
  325. $current_filter = 'all';
  326. $valid_filters = array_keys( self::get_filters() );
  327. if ( ! empty( $_GET['filter'] ) && in_array( $_GET['filter'], $valid_filters ) ) {
  328. $current_filter = $_GET['filter'];
  329. }
  330. return $current_filter;
  331. }
  332. /**
  333. * Filter an array of subscriptions by $filter.
  334. *
  335. * @param array $subscriptions The subscriptions array, passed by ref.
  336. * @param string $filter The filter.
  337. */
  338. private static function _filter( &$subscriptions, $filter ) {
  339. switch ( $filter ) {
  340. case 'active':
  341. $subscriptions = wp_list_filter( $subscriptions, array( 'active' => true ) );
  342. break;
  343. case 'inactive':
  344. $subscriptions = wp_list_filter( $subscriptions, array( 'active' => false ) );
  345. break;
  346. case 'installed':
  347. foreach ( $subscriptions as $key => $subscription ) {
  348. if ( empty( $subscription['local']['installed'] ) ) {
  349. unset( $subscriptions[ $key ] );
  350. }
  351. }
  352. break;
  353. case 'update-available':
  354. $subscriptions = wp_list_filter( $subscriptions, array( 'has_update' => true ) );
  355. break;
  356. case 'expiring':
  357. $subscriptions = wp_list_filter( $subscriptions, array( 'expiring' => true ) );
  358. break;
  359. case 'expired':
  360. $subscriptions = wp_list_filter( $subscriptions, array( 'expired' => true ) );
  361. break;
  362. case 'download':
  363. foreach ( $subscriptions as $key => $subscription ) {
  364. if ( $subscription['local']['installed'] || $subscription['expired'] ) {
  365. unset( $subscriptions[ $key ] );
  366. }
  367. }
  368. break;
  369. }
  370. }
  371. /**
  372. * Enqueue admin scripts and styles.
  373. */
  374. public static function admin_enqueue_scripts() {
  375. $screen = get_current_screen();
  376. $screen_id = $screen ? $screen->id : '';
  377. $wc_screen_id = sanitize_title( __( 'WooCommerce', 'woocommerce' ) );
  378. if ( $wc_screen_id . '_page_wc-addons' === $screen_id && isset( $_GET['section'] ) && 'helper' === $_GET['section'] ) {
  379. wp_enqueue_style( 'woocommerce-helper', WC()->plugin_url() . '/assets/css/helper.css', array(), WC_VERSION );
  380. }
  381. }
  382. /**
  383. * Various success/error notices.
  384. *
  385. * Runs during admin page render, so no headers/redirects here.
  386. *
  387. * @return array Array pairs of message/type strings with notices.
  388. */
  389. private static function _get_return_notices() {
  390. $return_status = isset( $_GET['wc-helper-status'] ) ? $_GET['wc-helper-status'] : null;
  391. $notices = array();
  392. switch ( $return_status ) {
  393. case 'activate-success':
  394. $subscription = self::_get_subscriptions_from_product_id( absint( $_GET['wc-helper-product-id'] ) );
  395. $notices[] = array(
  396. 'type' => 'updated',
  397. /* translators: %s: product name */
  398. 'message' => sprintf(
  399. __( '%s activated successfully. You will now receive updates for this product.', 'woocommerce' ),
  400. '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>'
  401. ),
  402. );
  403. break;
  404. case 'activate-error':
  405. $subscription = self::_get_subscriptions_from_product_id( absint( $_GET['wc-helper-product-id'] ) );
  406. $notices[] = array(
  407. 'type' => 'error',
  408. /* translators: %s: product name */
  409. 'message' => sprintf(
  410. __( 'An error has occurred when activating %s. Please try again later.', 'woocommerce' ),
  411. '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>'
  412. ),
  413. );
  414. break;
  415. case 'deactivate-success':
  416. $subscription = self::_get_subscriptions_from_product_id( absint( $_GET['wc-helper-product-id'] ) );
  417. $local = self::_get_local_from_product_id( absint( $_GET['wc-helper-product-id'] ) );
  418. /* translators: %s: product name */
  419. $message = sprintf(
  420. __( 'Subscription for %s deactivated successfully. You will no longer receive updates for this product.', 'woocommerce' ),
  421. '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>'
  422. );
  423. if ( $local && is_plugin_active( $local['_filename'] ) && current_user_can( 'activate_plugins' ) ) {
  424. $deactivate_plugin_url = add_query_arg(
  425. array(
  426. 'page' => 'wc-addons',
  427. 'section' => 'helper',
  428. 'filter' => self::get_current_filter(),
  429. 'wc-helper-deactivate-plugin' => 1,
  430. 'wc-helper-product-id' => $subscription['product_id'],
  431. 'wc-helper-nonce' => wp_create_nonce( 'deactivate-plugin:' . $subscription['product_id'] ),
  432. ), admin_url( 'admin.php' )
  433. );
  434. /* translators: %1$s: product name, %2$s: deactivate url */
  435. $message = sprintf(
  436. __( 'Subscription for %1$s deactivated successfully. You will no longer receive updates for this product. <a href="%2$s">Click here</a> if you wish to deactivate the plugin as well.', 'woocommerce' ),
  437. '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>', esc_url( $deactivate_plugin_url )
  438. );
  439. }
  440. $notices[] = array(
  441. 'message' => $message,
  442. 'type' => 'updated',
  443. );
  444. break;
  445. case 'deactivate-error':
  446. $subscription = self::_get_subscriptions_from_product_id( absint( $_GET['wc-helper-product-id'] ) );
  447. $notices[] = array(
  448. 'type' => 'error',
  449. /* translators: %s: product name */
  450. 'message' => sprintf(
  451. __( 'An error has occurred when deactivating the subscription for %s. Please try again later.', 'woocommerce' ),
  452. '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>'
  453. ),
  454. );
  455. break;
  456. case 'deactivate-plugin-success':
  457. $subscription = self::_get_subscriptions_from_product_id( absint( $_GET['wc-helper-product-id'] ) );
  458. $notices[] = array(
  459. 'type' => 'updated',
  460. /* translators: %s: product name */
  461. 'message' => sprintf(
  462. __( 'The extension %s has been deactivated successfully.', 'woocommerce' ),
  463. '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>'
  464. ),
  465. );
  466. break;
  467. case 'deactivate-plugin-error':
  468. $subscription = self::_get_subscriptions_from_product_id( absint( $_GET['wc-helper-product-id'] ) );
  469. $notices[] = array(
  470. 'type' => 'error',
  471. /* translators: %1$s: product name, %2$s: plugins screen url */
  472. 'message' => sprintf(
  473. __( 'An error has occurred when deactivating the extension %1$s. Please proceed to the <a href="%2$s">Plugins screen</a> to deactivate it manually.', 'woocommerce' ),
  474. '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>', admin_url( 'plugins.php' )
  475. ),
  476. );
  477. break;
  478. case 'helper-connected':
  479. $notices[] = array(
  480. 'message' => __( 'You have successfully connected your store to WooCommerce.com', 'woocommerce' ),
  481. 'type' => 'updated',
  482. );
  483. break;
  484. case 'helper-disconnected':
  485. $notices[] = array(
  486. 'message' => __( 'You have successfully disconnected your store from WooCommerce.com', 'woocommerce' ),
  487. 'type' => 'updated',
  488. );
  489. break;
  490. case 'helper-refreshed':
  491. $notices[] = array(
  492. 'message' => __( 'Authentication and subscription caches refreshed successfully.', 'woocommerce' ),
  493. 'type' => 'updated',
  494. );
  495. break;
  496. }
  497. return $notices;
  498. }
  499. /**
  500. * Various early-phase actions with possible redirects.
  501. */
  502. public static function current_screen( $screen ) {
  503. $wc_screen_id = sanitize_title( __( 'WooCommerce', 'woocommerce' ) );
  504. if ( $wc_screen_id . '_page_wc-addons' !== $screen->id ) {
  505. return;
  506. }
  507. if ( empty( $_GET['section'] ) || 'helper' !== $_GET['section'] ) {
  508. return;
  509. }
  510. if ( ! empty( $_GET['wc-helper-connect'] ) ) {
  511. return self::_helper_auth_connect();
  512. }
  513. if ( ! empty( $_GET['wc-helper-return'] ) ) {
  514. return self::_helper_auth_return();
  515. }
  516. if ( ! empty( $_GET['wc-helper-disconnect'] ) ) {
  517. return self::_helper_auth_disconnect();
  518. }
  519. if ( ! empty( $_GET['wc-helper-refresh'] ) ) {
  520. return self::_helper_auth_refresh();
  521. }
  522. if ( ! empty( $_GET['wc-helper-activate'] ) ) {
  523. return self::_helper_subscription_activate();
  524. }
  525. if ( ! empty( $_GET['wc-helper-deactivate'] ) ) {
  526. return self::_helper_subscription_deactivate();
  527. }
  528. if ( ! empty( $_GET['wc-helper-deactivate-plugin'] ) ) {
  529. return self::_helper_plugin_deactivate();
  530. }
  531. }
  532. /**
  533. * Initiate a new OAuth connection.
  534. */
  535. private static function _helper_auth_connect() {
  536. if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( $_GET['wc-helper-nonce'], 'connect' ) ) {
  537. self::log( 'Could not verify nonce in _helper_auth_connect' );
  538. wp_die( 'Could not verify nonce' );
  539. }
  540. $redirect_uri = add_query_arg(
  541. array(
  542. 'page' => 'wc-addons',
  543. 'section' => 'helper',
  544. 'wc-helper-return' => 1,
  545. 'wc-helper-nonce' => wp_create_nonce( 'connect' ),
  546. ), admin_url( 'admin.php' )
  547. );
  548. $request = WC_Helper_API::post(
  549. 'oauth/request_token', array(
  550. 'body' => array(
  551. 'home_url' => home_url(),
  552. 'redirect_uri' => $redirect_uri,
  553. ),
  554. )
  555. );
  556. $code = wp_remote_retrieve_response_code( $request );
  557. if ( 200 !== $code ) {
  558. self::log( sprintf( 'Call to oauth/request_token returned a non-200 response code (%d)', $code ) );
  559. wp_die( 'Something went wrong' );
  560. }
  561. $secret = json_decode( wp_remote_retrieve_body( $request ) );
  562. if ( empty( $secret ) ) {
  563. self::log( sprintf( 'Call to oauth/request_token returned an invalid body: %s', wp_remote_retrieve_body( $request ) ) );
  564. wp_die( 'Something went wrong' );
  565. }
  566. $connect_url = add_query_arg(
  567. array(
  568. 'home_url' => rawurlencode( home_url() ),
  569. 'redirect_uri' => rawurlencode( $redirect_uri ),
  570. 'secret' => rawurlencode( $secret ),
  571. ), WC_Helper_API::url( 'oauth/authorize' )
  572. );
  573. wp_redirect( esc_url_raw( $connect_url ) );
  574. die();
  575. }
  576. /**
  577. * Return from WooCommerce.com OAuth flow.
  578. */
  579. private static function _helper_auth_return() {
  580. if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( $_GET['wc-helper-nonce'], 'connect' ) ) {
  581. self::log( 'Could not verify nonce in _helper_auth_return' );
  582. wp_die( 'Something went wrong' );
  583. }
  584. // Bail if the user clicked deny.
  585. if ( ! empty( $_GET['deny'] ) ) {
  586. wp_safe_redirect( admin_url( 'admin.php?page=wc-addons&section=helper' ) );
  587. die();
  588. }
  589. // We do need a request token...
  590. if ( empty( $_GET['request_token'] ) ) {
  591. self::log( 'Request token not found in _helper_auth_return' );
  592. wp_die( 'Something went wrong' );
  593. }
  594. // Obtain an access token.
  595. $request = WC_Helper_API::post(
  596. 'oauth/access_token', array(
  597. 'body' => array(
  598. 'request_token' => $_GET['request_token'],
  599. 'home_url' => home_url(),
  600. ),
  601. )
  602. );
  603. $code = wp_remote_retrieve_response_code( $request );
  604. if ( 200 !== $code ) {
  605. self::log( sprintf( 'Call to oauth/access_token returned a non-200 response code (%d)', $code ) );
  606. wp_die( 'Something went wrong' );
  607. }
  608. $access_token = json_decode( wp_remote_retrieve_body( $request ), true );
  609. if ( ! $access_token ) {
  610. self::log( sprintf( 'Call to oauth/access_token returned an invalid body: %s', wp_remote_retrieve_body( $request ) ) );
  611. wp_die( 'Something went wrong' );
  612. }
  613. WC_Helper_Options::update(
  614. 'auth', array(
  615. 'access_token' => $access_token['access_token'],
  616. 'access_token_secret' => $access_token['access_token_secret'],
  617. 'site_id' => $access_token['site_id'],
  618. 'user_id' => get_current_user_id(),
  619. 'updated' => time(),
  620. )
  621. );
  622. // Obtain the connected user info.
  623. if ( ! self::_flush_authentication_cache() ) {
  624. self::log( 'Could not obtain connected user info in _helper_auth_return' );
  625. WC_Helper_Options::update( 'auth', array() );
  626. wp_die( 'Something went wrong.' );
  627. }
  628. self::_flush_subscriptions_cache();
  629. self::_flush_updates_cache();
  630. // Enable tracking when connected.
  631. if ( class_exists( 'WC_Tracker' ) ) {
  632. update_option( 'woocommerce_allow_tracking', 'yes' );
  633. WC_Tracker::send_tracking_data( true );
  634. }
  635. wp_safe_redirect(
  636. add_query_arg(
  637. array(
  638. 'page' => 'wc-addons',
  639. 'section' => 'helper',
  640. 'wc-helper-status' => 'helper-connected',
  641. ), admin_url( 'admin.php' )
  642. )
  643. );
  644. die();
  645. }
  646. /**
  647. * Disconnect from WooCommerce.com, clear OAuth tokens.
  648. */
  649. private static function _helper_auth_disconnect() {
  650. if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( $_GET['wc-helper-nonce'], 'disconnect' ) ) {
  651. self::log( 'Could not verify nonce in _helper_auth_disconnect' );
  652. wp_die( 'Could not verify nonce' );
  653. }
  654. $redirect_uri = add_query_arg(
  655. array(
  656. 'page' => 'wc-addons',
  657. 'section' => 'helper',
  658. 'wc-helper-status' => 'helper-disconnected',
  659. ), admin_url( 'admin.php' )
  660. );
  661. WC_Helper_API::post(
  662. 'oauth/invalidate_token', array(
  663. 'authenticated' => true,
  664. )
  665. );
  666. WC_Helper_Options::update( 'auth', array() );
  667. WC_Helper_Options::update( 'auth_user_data', array() );
  668. self::_flush_subscriptions_cache();
  669. self::_flush_updates_cache();
  670. // Disable tracking when disconnected.
  671. update_option( 'woocommerce_allow_tracking', 'no' );
  672. wp_safe_redirect( $redirect_uri );
  673. die();
  674. }
  675. /**
  676. * User hit the Refresh button, clear all caches.
  677. */
  678. private static function _helper_auth_refresh() {
  679. if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( $_GET['wc-helper-nonce'], 'refresh' ) ) {
  680. self::log( 'Could not verify nonce in _helper_auth_refresh' );
  681. wp_die( 'Could not verify nonce' );
  682. }
  683. $redirect_uri = add_query_arg(
  684. array(
  685. 'page' => 'wc-addons',
  686. 'section' => 'helper',
  687. 'filter' => self::get_current_filter(),
  688. 'wc-helper-status' => 'helper-refreshed',
  689. ), admin_url( 'admin.php' )
  690. );
  691. self::_flush_authentication_cache();
  692. self::_flush_subscriptions_cache();
  693. self::_flush_updates_cache();
  694. wp_safe_redirect( $redirect_uri );
  695. die();
  696. }
  697. /**
  698. * Active a product subscription.
  699. */
  700. private static function _helper_subscription_activate() {
  701. $product_key = $_GET['wc-helper-product-key'];
  702. $product_id = absint( $_GET['wc-helper-product-id'] );
  703. if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( $_GET['wc-helper-nonce'], 'activate:' . $product_key ) ) {
  704. self::log( 'Could not verify nonce in _helper_subscription_activate' );
  705. wp_die( 'Could not verify nonce' );
  706. }
  707. $request = WC_Helper_API::post(
  708. 'activate', array(
  709. 'authenticated' => true,
  710. 'body' => json_encode(
  711. array(
  712. 'product_key' => $product_key,
  713. )
  714. ),
  715. )
  716. );
  717. $activated = wp_remote_retrieve_response_code( $request ) === 200;
  718. $body = json_decode( wp_remote_retrieve_body( $request ), true );
  719. if ( ! $activated && ! empty( $body['code'] ) && 'already_connected' == $body['code'] ) {
  720. $activated = true;
  721. }
  722. // Attempt to activate this plugin.
  723. $local = self::_get_local_from_product_id( $product_id );
  724. if ( $local && 'plugin' == $local['_type'] && current_user_can( 'activate_plugins' ) && ! is_plugin_active( $local['_filename'] ) ) {
  725. activate_plugin( $local['_filename'] );
  726. }
  727. self::_flush_subscriptions_cache();
  728. self::_flush_updates_cache();
  729. $redirect_uri = add_query_arg(
  730. array(
  731. 'page' => 'wc-addons',
  732. 'section' => 'helper',
  733. 'filter' => self::get_current_filter(),
  734. 'wc-helper-status' => $activated ? 'activate-success' : 'activate-error',
  735. 'wc-helper-product-id' => $product_id,
  736. ), admin_url( 'admin.php' )
  737. );
  738. wp_safe_redirect( $redirect_uri );
  739. die();
  740. }
  741. /**
  742. * Deactivate a product subscription.
  743. */
  744. private static function _helper_subscription_deactivate() {
  745. $product_key = $_GET['wc-helper-product-key'];
  746. $product_id = absint( $_GET['wc-helper-product-id'] );
  747. if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( $_GET['wc-helper-nonce'], 'deactivate:' . $product_key ) ) {
  748. self::log( 'Could not verify nonce in _helper_subscription_deactivate' );
  749. wp_die( 'Could not verify nonce' );
  750. }
  751. $request = WC_Helper_API::post(
  752. 'deactivate', array(
  753. 'authenticated' => true,
  754. 'body' => json_encode(
  755. array(
  756. 'product_key' => $product_key,
  757. )
  758. ),
  759. )
  760. );
  761. $code = wp_remote_retrieve_response_code( $request );
  762. $deactivated = 200 === $code;
  763. if ( ! $deactivated ) {
  764. self::log( sprintf( 'Deactivate API call returned a non-200 response code (%d)', $code ) );
  765. }
  766. self::_flush_subscriptions_cache();
  767. $redirect_uri = add_query_arg(
  768. array(
  769. 'page' => 'wc-addons',
  770. 'section' => 'helper',
  771. 'filter' => self::get_current_filter(),
  772. 'wc-helper-status' => $deactivated ? 'deactivate-success' : 'deactivate-error',
  773. 'wc-helper-product-id' => $product_id,
  774. ), admin_url( 'admin.php' )
  775. );
  776. wp_safe_redirect( $redirect_uri );
  777. die();
  778. }
  779. /**
  780. * Deactivate a plugin.
  781. */
  782. private static function _helper_plugin_deactivate() {
  783. $product_id = absint( $_GET['wc-helper-product-id'] );
  784. $deactivated = false;
  785. if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( $_GET['wc-helper-nonce'], 'deactivate-plugin:' . $product_id ) ) {
  786. self::log( 'Could not verify nonce in _helper_plugin_deactivate' );
  787. wp_die( 'Could not verify nonce' );
  788. }
  789. if ( ! current_user_can( 'activate_plugins' ) ) {
  790. wp_die( 'You are not allowed to manage plugins on this site.' );
  791. }
  792. $local = wp_list_filter(
  793. array_merge(
  794. self::get_local_woo_plugins(),
  795. self::get_local_woo_themes()
  796. ), array( '_product_id' => $product_id )
  797. );
  798. // Attempt to deactivate this plugin or theme.
  799. if ( ! empty( $local ) ) {
  800. $local = array_shift( $local );
  801. if ( is_plugin_active( $local['_filename'] ) ) {
  802. deactivate_plugins( $local['_filename'] );
  803. }
  804. $deactivated = ! is_plugin_active( $local['_filename'] );
  805. }
  806. $redirect_uri = add_query_arg(
  807. array(
  808. 'page' => 'wc-addons',
  809. 'section' => 'helper',
  810. 'filter' => self::get_current_filter(),
  811. 'wc-helper-status' => $deactivated ? 'deactivate-plugin-success' : 'deactivate-plugin-error',
  812. 'wc-helper-product-id' => $product_id,
  813. ), admin_url( 'admin.php' )
  814. );
  815. wp_safe_redirect( $redirect_uri );
  816. die();
  817. }
  818. /**
  819. * Get a local plugin/theme entry from product_id.
  820. *
  821. * @param int $product_id The product id.
  822. *
  823. * @return array|bool The array containing the local plugin/theme data or false.
  824. */
  825. private static function _get_local_from_product_id( $product_id ) {
  826. $local = wp_list_filter(
  827. array_merge(
  828. self::get_local_woo_plugins(),
  829. self::get_local_woo_themes()
  830. ), array( '_product_id' => $product_id )
  831. );
  832. if ( ! empty( $local ) ) {
  833. return array_shift( $local );
  834. }
  835. return false;
  836. }
  837. /**
  838. * Get a subscription entry from product_id. If multiple subscriptions are
  839. * found with the same product id and $single is set to true, will return the
  840. * first one in the list, so you can use this method to get things like extension
  841. * name, version, etc.
  842. *
  843. * @param int $product_id The product id.
  844. * @param bool $single Whether to return a single subscription or all matching a product id.
  845. *
  846. * @return array|bool The array containing sub data or false.
  847. */
  848. private static function _get_subscriptions_from_product_id( $product_id, $single = true ) {
  849. $subscriptions = wp_list_filter( self::get_subscriptions(), array( 'product_id' => $product_id ) );
  850. if ( ! empty( $subscriptions ) ) {
  851. return $single ? array_shift( $subscriptions ) : $subscriptions;
  852. }
  853. return false;
  854. }
  855. /**
  856. * Additional theme style.css and plugin file headers.
  857. *
  858. * Format: Woo: product_id:file_id
  859. */
  860. public static function extra_headers( $headers ) {
  861. $headers[] = 'Woo';
  862. return $headers;
  863. }
  864. /**
  865. * Obtain a list of locally installed Woo extensions.
  866. */
  867. public static function get_local_woo_plugins() {
  868. if ( ! function_exists( 'get_plugins' ) ) {
  869. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  870. }
  871. $plugins = get_plugins();
  872. $woo_plugins = array();
  873. // Backwards compatibility for woothemes_queue_update().
  874. $_compat = array();
  875. if ( ! empty( $GLOBALS['woothemes_queued_updates'] ) ) {
  876. foreach ( $GLOBALS['woothemes_queued_updates'] as $_compat_plugin ) {
  877. $_compat[ $_compat_plugin->file ] = array(
  878. 'product_id' => $_compat_plugin->product_id,
  879. 'file_id' => $_compat_plugin->file_id,
  880. );
  881. }
  882. }
  883. foreach ( $plugins as $filename => $data ) {
  884. if ( empty( $data['Woo'] ) && ! empty( $_compat[ $filename ] ) ) {
  885. $data['Woo'] = sprintf( '%d:%s', $_compat[ $filename ]['product_id'], $_compat[ $filename ]['file_id'] );
  886. }
  887. if ( empty( $data['Woo'] ) ) {
  888. continue;
  889. }
  890. list( $product_id, $file_id ) = explode( ':', $data['Woo'] );
  891. if ( empty( $product_id ) || empty( $file_id ) ) {
  892. continue;
  893. }
  894. $data['_filename'] = $filename;
  895. $data['_product_id'] = absint( $product_id );
  896. $data['_file_id'] = $file_id;
  897. $data['_type'] = 'plugin';
  898. $woo_plugins[ $filename ] = $data;
  899. }
  900. return $woo_plugins;
  901. }
  902. /**
  903. * Get locally installed Woo themes.
  904. */
  905. public static function get_local_woo_themes() {
  906. $themes = wp_get_themes();
  907. $woo_themes = array();
  908. foreach ( $themes as $theme ) {
  909. $header = $theme->get( 'Woo' );
  910. // Backwards compatibility for theme_info.txt
  911. if ( ! $header ) {
  912. $txt = $theme->get_stylesheet_directory() . '/theme_info.txt';
  913. if ( is_readable( $txt ) ) {
  914. $txt = file_get_contents( $txt );
  915. $txt = preg_split( '#\s#', $txt );
  916. if ( count( $txt ) >= 2 ) {
  917. $header = sprintf( '%d:%s', $txt[0], $txt[1] );
  918. }
  919. }
  920. }
  921. if ( empty( $header ) ) {
  922. continue;
  923. }
  924. list( $product_id, $file_id ) = explode( ':', $header );
  925. if ( empty( $product_id ) || empty( $file_id ) ) {
  926. continue;
  927. }
  928. $data = array(
  929. 'Name' => $theme->get( 'Name' ),
  930. 'Version' => $theme->get( 'Version' ),
  931. 'Woo' => $header,
  932. '_filename' => $theme->get_stylesheet() . '/style.css',
  933. '_stylesheet' => $theme->get_stylesheet(),
  934. '_product_id' => absint( $product_id ),
  935. '_file_id' => $file_id,
  936. '_type' => 'theme',
  937. );
  938. $woo_themes[ $data['_filename'] ] = $data;
  939. }
  940. return $woo_themes;
  941. }
  942. /**
  943. * Get the connected user's subscriptions.
  944. *
  945. * @return array
  946. */
  947. public static function get_subscriptions() {
  948. $cache_key = '_woocommerce_helper_subscriptions';
  949. if ( false !== ( $data = get_transient( $cache_key ) ) ) {
  950. return $data;
  951. }
  952. // Obtain the connected user info.
  953. $request = WC_Helper_API::get(
  954. 'subscriptions', array(
  955. 'authenticated' => true,
  956. )
  957. );
  958. if ( wp_remote_retrieve_response_code( $request ) !== 200 ) {
  959. set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS );
  960. return array();
  961. }
  962. $data = json_decode( wp_remote_retrieve_body( $request ), true );
  963. if ( empty( $data ) || ! is_array( $data ) ) {
  964. $data = array();
  965. }
  966. set_transient( $cache_key, $data, 1 * HOUR_IN_SECONDS );
  967. return $data;
  968. }
  969. /**
  970. * Runs when any plugin is activated.
  971. *
  972. * Depending on the activated plugin attempts to look through available
  973. * subscriptions and auto-activate one if possible, so the user does not
  974. * need to visit the Helper UI at all after installing a new extension.
  975. *
  976. * @param string $filename The filename of the activated plugin.
  977. */
  978. public static function activated_plugin( $filename ) {
  979. $plugins = self::get_local_woo_plugins();
  980. // Not a local woo plugin
  981. if ( empty( $plugins[ $filename ] ) ) {
  982. return;
  983. }
  984. // Make sure we have a connection.
  985. $auth = WC_Helper_Options::get( 'auth' );
  986. if ( empty( $auth ) ) {
  987. return;
  988. }
  989. $plugin = $plugins[ $filename ];
  990. $subscriptions = self::_get_subscriptions_from_product_id( $plugin['_product_id'], false );
  991. // No valid subscriptions for this product
  992. if ( empty( $subscriptions ) ) {
  993. return;
  994. }
  995. $subscription = null;
  996. foreach ( $subscriptions as $_sub ) {
  997. // Don't attempt to activate expired subscriptions.
  998. if ( $_sub['expired'] ) {
  999. continue;
  1000. }
  1001. // No more sites available in this subscription.
  1002. if ( $_sub['sites_max'] && $_sub['sites_active'] >= $_sub['sites_max'] ) {
  1003. continue;
  1004. }
  1005. // Looks good.
  1006. $subscription = $_sub;
  1007. break;
  1008. }
  1009. // No valid subscription found.
  1010. if ( ! $subscription ) {
  1011. return;
  1012. }
  1013. $request = WC_Helper_API::post(
  1014. 'activate', array(
  1015. 'authenticated' => true,
  1016. 'body' => json_encode(
  1017. array(
  1018. 'product_key' => $subscription['product_key'],
  1019. )
  1020. ),
  1021. )
  1022. );
  1023. $activated = wp_remote_retrieve_response_code( $request ) === 200;
  1024. $body = json_decode( wp_remote_retrieve_body( $request ), true );
  1025. if ( ! $activated && ! empty( $body['code'] ) && 'already_connected' == $body['code'] ) {
  1026. $activated = true;
  1027. }
  1028. if ( ! $activated ) {
  1029. self::log( 'Could not activate a subscription upon plugin activation: ' . $filename );
  1030. return;
  1031. }
  1032. self::log( 'Auto-activated a subscription for ' . $filename );
  1033. self::_flush_subscriptions_cache();
  1034. self::_flush_updates_cache();
  1035. }
  1036. /**
  1037. * Runs when any plugin is deactivated.
  1038. *
  1039. * When a user deactivates a plugin, attempt to deactivate any subscriptions
  1040. * associated with the extension.
  1041. *
  1042. * @param string $filename The filename of the deactivated plugin.
  1043. */
  1044. public static function deactivated_plugin( $filename ) {
  1045. $plugins = self::get_local_woo_plugins();
  1046. // Not a local woo plugin
  1047. if ( empty( $plugins[ $filename ] ) ) {
  1048. return;
  1049. }
  1050. // Make sure we have a connection.
  1051. $auth = WC_Helper_Options::get( 'auth' );
  1052. if ( empty( $auth ) ) {
  1053. return;
  1054. }
  1055. $plugin = $plugins[ $filename ];
  1056. $subscriptions = self::_get_subscriptions_from_product_id( $plugin['_product_id'], false );
  1057. $site_id = absint( $auth['site_id'] );
  1058. // No valid subscriptions for this product
  1059. if ( empty( $subscriptions ) ) {
  1060. return;
  1061. }
  1062. $deactivated = 0;
  1063. foreach ( $subscriptions as $subscription ) {
  1064. // Don't touch subscriptions that aren't activated on this site.
  1065. if ( ! in_array( $site_id, $subscription['connections'] ) ) {
  1066. continue;
  1067. }
  1068. $request = WC_Helper_API::post(
  1069. 'deactivate', array(
  1070. 'authenticated' => true,
  1071. 'body' => json_encode(
  1072. array(
  1073. 'product_key' => $subscription['product_key'],
  1074. )
  1075. ),
  1076. )
  1077. );
  1078. if ( wp_remote_retrieve_response_code( $request ) === 200 ) {
  1079. $deactivated++;
  1080. }
  1081. }
  1082. if ( $deactivated ) {
  1083. self::log( sprintf( 'Auto-deactivated %d subscription(s) for %s', $deactivated, $filename ) );
  1084. self::_flush_subscriptions_cache();
  1085. self::_flush_updates_cache();
  1086. }
  1087. }
  1088. /**
  1089. * Various Helper-related admin notices.
  1090. */
  1091. public static function admin_notices() {
  1092. if ( apply_filters( 'woocommerce_helper_suppress_admin_notices', false ) ) {
  1093. return;
  1094. }
  1095. $screen = get_current_screen();
  1096. $screen_id = $screen ? $screen->id : '';
  1097. self::_prompt_helper_connect( $screen_id );
  1098. if ( 'update-core' !== $screen_id ) {
  1099. return;
  1100. }
  1101. // Don't nag if Woo doesn't have an update available.
  1102. if ( ! self::_woo_core_update_available() ) {
  1103. return;
  1104. }
  1105. // Add a note about available extension updates if Woo core has an update available.
  1106. $notice = self::_get_extensions_update_notice();
  1107. if ( ! empty( $notice ) ) {
  1108. echo '<div class="updated woocommerce-message"><p>' . $notice . '</p></div>';
  1109. }
  1110. }
  1111. /**
  1112. * Prompt a Helper connection if the user has WooCommerce.com extensions.
  1113. *
  1114. * @param string $screen_id Current screen ID.
  1115. */
  1116. private static function _prompt_helper_connect( $screen_id ) {
  1117. if ( apply_filters( 'woocommerce_helper_suppress_connect_notice', false ) ) {
  1118. return;
  1119. }
  1120. $screens = wc_get_screen_ids();
  1121. $screens[] = 'plugins';
  1122. if ( ! in_array( $screen_id, $screens, true ) ) {
  1123. return;
  1124. }
  1125. // Don't show the notice on the Helper screens.
  1126. $screen_addons = sanitize_title( __( 'WooCommerce', 'woocommerce' ) ) . '_page_wc-addons';
  1127. if ( $screen_addons === $screen_id && ! empty( $_REQUEST['section'] ) && 'helper' === $_REQUEST['section'] ) {
  1128. return;
  1129. }
  1130. // We believe we have an active connection.
  1131. $auth = WC_Helper_Options::get( 'auth' );
  1132. if ( ! empty( $auth['access_token'] ) ) {
  1133. return;
  1134. }
  1135. $active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
  1136. if ( empty( $active_plugins ) ) {
  1137. return;
  1138. }
  1139. $woo_plugins = self::get_local_woo_plugins();
  1140. if ( empty( $woo_plugins ) ) {
  1141. return;
  1142. }
  1143. $active_woo_plugins = array_intersect_key( $woo_plugins, array_flip( $active_plugins ) );
  1144. if ( count( $active_woo_plugins ) > 0 ) {
  1145. /* translators: %s: helper screen url */
  1146. $notice = __( '<a href="%s">Connect your store</a> to WooCommerce.com to receive extensions updates and support.', 'woocommerce' );
  1147. $notice = sprintf( $notice, admin_url( 'admin.php?page=wc-addons&section=helper' ) );
  1148. echo '<div class="updated woocommerce-message"><p>' . $notice . '</p></div>';
  1149. }
  1150. }
  1151. /**
  1152. * Get an update notice if one or more Woo extensions has an update available.
  1153. *
  1154. * @return string|null The update notice or null if everything is up to date.
  1155. */
  1156. private static function _get_extensions_update_notice() {
  1157. $plugins = self::get_local_woo_plugins();
  1158. $updates = WC_Helper_Updater::get_update_data();
  1159. $available = 0;
  1160. foreach ( $plugins as $data ) {
  1161. if ( empty( $updates[ $data['_product_id'] ] ) ) {
  1162. continue;
  1163. }
  1164. $product_id = $data['_product_id'];
  1165. if ( version_compare( $updates[ $product_id ]['version'], $data['Version'], '>' ) ) {
  1166. $available++;
  1167. }
  1168. }
  1169. if ( ! $available ) {
  1170. return;
  1171. }
  1172. /* translators: %1$s: helper url, %2$d: number of extensions */
  1173. return sprintf(
  1174. _n( 'Note: You currently have <a href="%1$s">%2$d paid extension</a> which should be updated first before updating WooCommerce.', 'Note: You currently have <a href="%1$s">%2$d paid extensions</a> which should be updated first before updating WooCommerce.', $available, 'woocommerce' ),
  1175. admin_url( 'admin.php?page=wc-addons&section=helper' ), $available
  1176. );
  1177. }
  1178. /**
  1179. * Whether WooCommerce has an update available.
  1180. *
  1181. * @return bool True if a Woo core update is available.
  1182. */
  1183. private static function _woo_core_update_available() {
  1184. $updates = get_site_transient( 'update_plugins' );
  1185. if ( empty( $updates->response ) ) {
  1186. return false;
  1187. }
  1188. if ( empty( $updates->response['woocommerce/woocommerce.php'] ) ) {
  1189. return false;
  1190. }
  1191. $data = $updates->response['woocommerce/woocommerce.php'];
  1192. if ( version_compare( WC_VERSION, $data->new_version, '>=' ) ) {
  1193. return false;
  1194. }
  1195. return true;
  1196. }
  1197. /**
  1198. * Flush subscriptions cache.
  1199. */
  1200. private static function _flush_subscriptions_cache() {
  1201. delete_transient( '_woocommerce_helper_subscriptions' );
  1202. }
  1203. /**
  1204. * Flush auth cache.
  1205. *
  1206. * @access private
  1207. */
  1208. public static function _flush_authentication_cache() {
  1209. $request = WC_Helper_API::get(
  1210. 'oauth/me', array(
  1211. 'authenticated' => true,
  1212. )
  1213. );
  1214. if ( wp_remote_retrieve_response_code( $request ) !== 200 ) {
  1215. return false;
  1216. }
  1217. $user_data = json_decode( wp_remote_retrieve_body( $request ), true );
  1218. if ( ! $user_data ) {
  1219. return false;
  1220. }
  1221. WC_Helper_Options::update(
  1222. 'auth_user_data', array(
  1223. 'name' => $user_data['name'],
  1224. 'email' => $user_data['email'],
  1225. )
  1226. );
  1227. return true;
  1228. }
  1229. /**
  1230. * Flush updates cache.
  1231. */
  1232. private static function _flush_updates_cache() {
  1233. WC_Helper_Updater::flush_updates_cache();
  1234. }
  1235. /**
  1236. * Sort subscriptions by the product_name.
  1237. *
  1238. * @param array $a Subscription array
  1239. * @param array $b Subscription array
  1240. *
  1241. * @return int
  1242. */
  1243. public static function _sort_by_product_name( $a, $b ) {
  1244. return strcmp( $a['product_name'], $b['product_name'] );
  1245. }
  1246. /**
  1247. * Sort subscriptions by the Name.
  1248. *
  1249. * @param array $a Product array
  1250. * @param array $b Product array
  1251. *
  1252. * @return int
  1253. */
  1254. public static function _sort_by_name( $a, $b ) {
  1255. return strcmp( $a['Name'], $b['Name'] );
  1256. }
  1257. /**
  1258. * Log a helper event.
  1259. *
  1260. * @param string $message Log message.
  1261. * @param string $level Optional, defaults to info, valid levels:
  1262. * emergency|alert|critical|error|warning|notice|info|debug
  1263. */
  1264. public static function log( $message, $level = 'info' ) {
  1265. if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
  1266. return;
  1267. }
  1268. if ( ! isset( self::$log ) ) {
  1269. self::$log = wc_get_logger();
  1270. }
  1271. self::$log->log( $level, $message, array( 'source' => 'helper' ) );
  1272. }
  1273. }
  1274. WC_Helper::load();