PageRenderTime 44ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/packages/woocommerce-admin/src/Notes/WooSubscriptionsNotes.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 455 lines | 287 code | 63 blank | 105 comment | 35 complexity | 04b455c45451f849cdd70352cf00e47b MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Admin (Dashboard) WooCommerce.com Extension Subscriptions Note Provider.
  4. *
  5. * Adds notes to the merchant's inbox concerning WooCommerce.com extension subscriptions.
  6. */
  7. namespace Automattic\WooCommerce\Admin\Notes;
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * Woo_Subscriptions_Notes
  11. */
  12. class WooSubscriptionsNotes {
  13. const LAST_REFRESH_OPTION_KEY = 'woocommerce_admin-wc-helper-last-refresh';
  14. const CONNECTION_NOTE_NAME = 'wc-admin-wc-helper-connection';
  15. const SUBSCRIPTION_NOTE_NAME = 'wc-admin-wc-helper-subscription';
  16. const NOTIFY_WHEN_DAYS_LEFT = 60;
  17. /**
  18. * We want to bubble up expiration notices when they cross certain age
  19. * thresholds. PHP 5.2 doesn't support constant arrays, so we do this.
  20. *
  21. * @return array
  22. */
  23. private function get_bump_thresholds() {
  24. return array( 60, 45, 20, 7, 1 ); // days.
  25. }
  26. /**
  27. * Hook all the things.
  28. */
  29. public function __construct() {
  30. add_action( 'admin_init', array( $this, 'admin_init' ) );
  31. add_action( 'update_option_woocommerce_helper_data', array( $this, 'update_option_woocommerce_helper_data' ), 10, 2 );
  32. }
  33. /**
  34. * Reacts to changes in the helper option.
  35. *
  36. * @param array $old_value The previous value of the option.
  37. * @param array $value The new value of the option.
  38. */
  39. public function update_option_woocommerce_helper_data( $old_value, $value ) {
  40. if ( ! is_array( $old_value ) ) {
  41. $old_value = array();
  42. }
  43. if ( ! is_array( $value ) ) {
  44. $value = array();
  45. }
  46. $old_auth = array_key_exists( 'auth', $old_value ) ? $old_value['auth'] : array();
  47. $new_auth = array_key_exists( 'auth', $value ) ? $value['auth'] : array();
  48. $old_token = array_key_exists( 'access_token', $old_auth ) ? $old_auth['access_token'] : '';
  49. $new_token = array_key_exists( 'access_token', $new_auth ) ? $new_auth['access_token'] : '';
  50. // The site just disconnected.
  51. if ( ! empty( $old_token ) && empty( $new_token ) ) {
  52. $this->remove_notes();
  53. $this->add_no_connection_note();
  54. return;
  55. }
  56. // The site is connected.
  57. if ( $this->is_connected() ) {
  58. $this->remove_notes();
  59. $this->refresh_subscription_notes();
  60. return;
  61. }
  62. }
  63. /**
  64. * Things to do on admin_init.
  65. */
  66. public function admin_init() {
  67. $this->check_connection();
  68. if ( $this->is_connected() ) {
  69. $refresh_notes = false;
  70. // Did the user just do something on the helper page?.
  71. if ( isset( $_GET['wc-helper-status'] ) ) { // @codingStandardsIgnoreLine.
  72. $refresh_notes = true;
  73. }
  74. // Has it been more than a day since we last checked?
  75. // Note: We do it this way and not wp_scheduled_task since WC_Helper_Options is not loaded for cron.
  76. $time_now_gmt = current_time( 'timestamp', 0 );
  77. $last_refresh = intval( get_option( self::LAST_REFRESH_OPTION_KEY, 0 ) );
  78. if ( $last_refresh + DAY_IN_SECONDS <= $time_now_gmt ) {
  79. update_option( self::LAST_REFRESH_OPTION_KEY, $time_now_gmt );
  80. $refresh_notes = true;
  81. }
  82. if ( $refresh_notes ) {
  83. $this->refresh_subscription_notes();
  84. }
  85. }
  86. }
  87. /**
  88. * Checks the connection. Adds a note (as necessary) if there is no connection.
  89. */
  90. public function check_connection() {
  91. if ( ! $this->is_connected() ) {
  92. $data_store = Notes::load_data_store();
  93. $note_ids = $data_store->get_notes_with_name( self::CONNECTION_NOTE_NAME );
  94. if ( ! empty( $note_ids ) ) {
  95. // We already have a connection note. Exit early.
  96. return;
  97. }
  98. $this->remove_notes();
  99. $this->add_no_connection_note();
  100. }
  101. }
  102. /**
  103. * Whether or not we think the site is currently connected to WooCommerce.com.
  104. *
  105. * @return bool
  106. */
  107. public function is_connected() {
  108. $auth = \WC_Helper_Options::get( 'auth' );
  109. return ( ! empty( $auth['access_token'] ) );
  110. }
  111. /**
  112. * Returns the WooCommerce.com provided site ID for this site.
  113. *
  114. * @return int|false
  115. */
  116. public function get_connected_site_id() {
  117. if ( ! $this->is_connected() ) {
  118. return false;
  119. }
  120. $auth = \WC_Helper_Options::get( 'auth' );
  121. return absint( $auth['site_id'] );
  122. }
  123. /**
  124. * Returns an array of product_ids whose subscriptions are active on this site.
  125. *
  126. * @return array
  127. */
  128. public function get_subscription_active_product_ids() {
  129. $site_id = $this->get_connected_site_id();
  130. if ( ! $site_id ) {
  131. return array();
  132. }
  133. $product_ids = array();
  134. if ( $this->is_connected() ) {
  135. $subscriptions = \WC_Helper::get_subscriptions();
  136. foreach ( (array) $subscriptions as $subscription ) {
  137. if ( in_array( $site_id, $subscription['connections'], true ) ) {
  138. $product_ids[] = $subscription['product_id'];
  139. }
  140. }
  141. }
  142. return $product_ids;
  143. }
  144. /**
  145. * Clears all connection or subscription notes.
  146. */
  147. public function remove_notes() {
  148. Notes::delete_notes_with_name( self::CONNECTION_NOTE_NAME );
  149. Notes::delete_notes_with_name( self::SUBSCRIPTION_NOTE_NAME );
  150. }
  151. /**
  152. * Adds a note prompting to connect to WooCommerce.com.
  153. */
  154. public function add_no_connection_note() {
  155. $note = new Note();
  156. $note->set_title( __( 'Connect to WooCommerce.com', 'woocommerce' ) );
  157. $note->set_content( __( 'Connect to get important product notifications and updates.', 'woocommerce' ) );
  158. $note->set_content_data( (object) array() );
  159. $note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
  160. $note->set_name( self::CONNECTION_NOTE_NAME );
  161. $note->set_source( 'woocommerce-admin' );
  162. $note->add_action(
  163. 'connect',
  164. __( 'Connect', 'woocommerce' ),
  165. '?page=wc-addons&section=helper',
  166. Note::E_WC_ADMIN_NOTE_UNACTIONED
  167. );
  168. $note->save();
  169. }
  170. /**
  171. * Gets the product_id (if any) associated with a note.
  172. *
  173. * @param Note $note The note object to interrogate.
  174. * @return int|false
  175. */
  176. public function get_product_id_from_subscription_note( &$note ) {
  177. $content_data = $note->get_content_data();
  178. if ( property_exists( $content_data, 'product_id' ) ) {
  179. return intval( $content_data->product_id );
  180. }
  181. return false;
  182. }
  183. /**
  184. * Removes notes for product_ids no longer active on this site.
  185. */
  186. public function prune_inactive_subscription_notes() {
  187. $active_product_ids = $this->get_subscription_active_product_ids();
  188. $data_store = Notes::load_data_store();
  189. $note_ids = $data_store->get_notes_with_name( self::SUBSCRIPTION_NOTE_NAME );
  190. foreach ( (array) $note_ids as $note_id ) {
  191. $note = Notes::get_note( $note_id );
  192. $product_id = $this->get_product_id_from_subscription_note( $note );
  193. if ( ! empty( $product_id ) ) {
  194. if ( ! in_array( $product_id, $active_product_ids, true ) ) {
  195. $note->delete();
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * Finds a note for a given product ID, if the note exists at all.
  202. *
  203. * @param int $product_id The product ID to search for.
  204. * @return Note|false
  205. */
  206. public function find_note_for_product_id( $product_id ) {
  207. $product_id = intval( $product_id );
  208. $data_store = Notes::load_data_store();
  209. $note_ids = $data_store->get_notes_with_name( self::SUBSCRIPTION_NOTE_NAME );
  210. foreach ( (array) $note_ids as $note_id ) {
  211. $note = Notes::get_note( $note_id );
  212. $found_product_id = $this->get_product_id_from_subscription_note( $note );
  213. if ( $product_id === $found_product_id ) {
  214. return $note;
  215. }
  216. }
  217. return false;
  218. }
  219. /**
  220. * Deletes a note for a given product ID, if the note exists at all.
  221. *
  222. * @param int $product_id The product ID to search for.
  223. */
  224. public function delete_any_note_for_product_id( $product_id ) {
  225. $product_id = intval( $product_id );
  226. $note = $this->find_note_for_product_id( $product_id );
  227. if ( $note ) {
  228. $note->delete();
  229. }
  230. }
  231. /**
  232. * Adds or updates a note for an expiring subscription.
  233. *
  234. * @param array $subscription The subscription to work with.
  235. */
  236. public function add_or_update_subscription_expiring( $subscription ) {
  237. $product_id = $subscription['product_id'];
  238. $product_name = $subscription['product_name'];
  239. $expires = intval( $subscription['expires'] );
  240. $time_now_gmt = current_time( 'timestamp', 0 );
  241. $days_until_expiration = intval( ceil( ( $expires - $time_now_gmt ) / DAY_IN_SECONDS ) );
  242. $note = $this->find_note_for_product_id( $product_id );
  243. if ( $note ) {
  244. $content_data = $note->get_content_data();
  245. if ( property_exists( $content_data, 'days_until_expiration' ) ) {
  246. // Note: There is no reason this property should not exist. This is just defensive programming.
  247. $note_days_until_expiration = intval( $content_data->days_until_expiration );
  248. if ( $days_until_expiration === $note_days_until_expiration ) {
  249. // Note is already up to date. Bail.
  250. return;
  251. }
  252. // If we have a note and we are at or have crossed a threshold, we should delete
  253. // the old note and create a new one, thereby "bumping" the note to the top of the inbox.
  254. $bump_thresholds = $this->get_bump_thresholds();
  255. $crossing_threshold = false;
  256. foreach ( (array) $bump_thresholds as $bump_threshold ) {
  257. if ( ( $note_days_until_expiration > $bump_threshold ) && ( $days_until_expiration <= $bump_threshold ) ) {
  258. $note->delete();
  259. $note = false;
  260. continue;
  261. }
  262. }
  263. }
  264. }
  265. $note_title = sprintf(
  266. /* translators: name of the extension subscription expiring soon */
  267. __( '%s subscription expiring soon', 'woocommerce' ),
  268. $product_name
  269. );
  270. $note_content = sprintf(
  271. /* translators: number of days until the subscription expires */
  272. __( 'Your subscription expires in %d days. Enable autorenew to avoid losing updates and access to support.', 'woocommerce' ),
  273. $days_until_expiration
  274. );
  275. $note_content_data = (object) array(
  276. 'product_id' => $product_id,
  277. 'product_name' => $product_name,
  278. 'expired' => false,
  279. 'days_until_expiration' => $days_until_expiration,
  280. );
  281. if ( ! $note ) {
  282. $note = new Note();
  283. }
  284. // Reset everything in case we are repurposing an expired note as an expiring note.
  285. $note->set_title( $note_title );
  286. $note->set_type( Note::E_WC_ADMIN_NOTE_WARNING );
  287. $note->set_name( self::SUBSCRIPTION_NOTE_NAME );
  288. $note->set_source( 'woocommerce-admin' );
  289. $note->clear_actions();
  290. $note->add_action(
  291. 'enable-autorenew',
  292. __( 'Enable Autorenew', 'woocommerce' ),
  293. 'https://woocommerce.com/my-account/my-subscriptions/?utm_medium=product'
  294. );
  295. $note->set_content( $note_content );
  296. $note->set_content_data( $note_content_data );
  297. $note->save();
  298. }
  299. /**
  300. * Adds a note for an expired subscription, or updates an expiring note to expired.
  301. *
  302. * @param array $subscription The subscription to work with.
  303. */
  304. public function add_or_update_subscription_expired( $subscription ) {
  305. $product_id = $subscription['product_id'];
  306. $product_name = $subscription['product_name'];
  307. $product_page = $subscription['product_url'];
  308. $expires = intval( $subscription['expires'] );
  309. $expires_date = gmdate( 'F jS', $expires );
  310. $note = $this->find_note_for_product_id( $product_id );
  311. if ( $note ) {
  312. $note_content_data = $note->get_content_data();
  313. if ( $note_content_data->expired ) {
  314. // We've already got a full fledged expired note for this. Bail.
  315. // Expired notes' content don't change with time.
  316. return;
  317. }
  318. }
  319. $note_title = sprintf(
  320. /* translators: name of the extension subscription that expired */
  321. __( '%s subscription expired', 'woocommerce' ),
  322. $product_name
  323. );
  324. $note_content = sprintf(
  325. /* translators: date the subscription expired, e.g. Jun 7th 2018 */
  326. __( 'Your subscription expired on %s. Get a new subscription to continue receiving updates and access to support.', 'woocommerce' ),
  327. $expires_date
  328. );
  329. $note_content_data = (object) array(
  330. 'product_id' => $product_id,
  331. 'product_name' => $product_name,
  332. 'expired' => true,
  333. 'expires' => $expires,
  334. 'expires_date' => $expires_date,
  335. );
  336. if ( ! $note ) {
  337. $note = new Note();
  338. }
  339. $note->set_title( $note_title );
  340. $note->set_content( $note_content );
  341. $note->set_content_data( $note_content_data );
  342. $note->set_type( Note::E_WC_ADMIN_NOTE_WARNING );
  343. $note->set_name( self::SUBSCRIPTION_NOTE_NAME );
  344. $note->set_source( 'woocommerce-admin' );
  345. $note->clear_actions();
  346. $note->add_action(
  347. 'renew-subscription',
  348. __( 'Renew Subscription', 'woocommerce' ),
  349. $product_page
  350. );
  351. $note->save();
  352. }
  353. /**
  354. * For each active subscription on this site, checks the expiration date and creates/updates/deletes notes.
  355. */
  356. public function refresh_subscription_notes() {
  357. if ( ! $this->is_connected() ) {
  358. return;
  359. }
  360. $this->prune_inactive_subscription_notes();
  361. $subscriptions = \WC_Helper::get_subscriptions();
  362. $active_product_ids = $this->get_subscription_active_product_ids();
  363. foreach ( (array) $subscriptions as $subscription ) {
  364. // Only concern ourselves with active products.
  365. $product_id = $subscription['product_id'];
  366. if ( ! in_array( $product_id, $active_product_ids, true ) ) {
  367. continue;
  368. }
  369. // If the subscription will auto-renew, clean up and exit.
  370. if ( $subscription['autorenew'] ) {
  371. $this->delete_any_note_for_product_id( $product_id );
  372. continue;
  373. }
  374. // If the subscription is not expiring by the first threshold, clean up and exit.
  375. $bump_thresholds = $this->get_bump_thresholds();
  376. $first_threshold = DAY_IN_SECONDS * $bump_thresholds[0];
  377. $expires = intval( $subscription['expires'] );
  378. $time_now_gmt = current_time( 'timestamp', 0 );
  379. if ( $expires > $time_now_gmt + $first_threshold ) {
  380. $this->delete_any_note_for_product_id( $product_id );
  381. continue;
  382. }
  383. // Otherwise, if the subscription can still have auto-renew enabled, let them know that now.
  384. if ( $expires > $time_now_gmt ) {
  385. $this->add_or_update_subscription_expiring( $subscription );
  386. continue;
  387. }
  388. // If we got this far, the subscription has completely expired, let them know.
  389. $this->add_or_update_subscription_expired( $subscription );
  390. }
  391. }
  392. }