Sin descripción

DeprecatedShortcodeNotice.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace MailPoet\Util\Notices;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Util\Helpers;
  5. use MailPoet\WP\Functions as WPFunctions;
  6. use MailPoet\WP\Notice;
  7. /**
  8. * This can be removed after 2021-08-01
  9. */
  10. class DeprecatedShortcodeNotice {
  11. const DISMISS_NOTICE_TIMEOUT_SECONDS = 15552000; // 6 months
  12. const OPTION_NAME = 'dismissed-deprecated-shortcode-notice';
  13. /** @var WPFunctions */
  14. private $wp;
  15. public function __construct(
  16. WPFunctions $wp
  17. ) {
  18. $this->wp = $wp;
  19. }
  20. public function init($shouldDisplay) {
  21. if ($shouldDisplay && !$this->wp->getTransient(self::OPTION_NAME) && $this->isUsingDeprecatedShortcode()) {
  22. return $this->display();
  23. }
  24. return null;
  25. }
  26. public function isUsingDeprecatedShortcode() {
  27. global $wp_filter;// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  28. $hook = 'mailpoet_newsletter_shortcode';
  29. if (empty($wp_filter[$hook])) return false;// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  30. $callbacks = $wp_filter[$hook];// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  31. if (empty($callbacks->callbacks) || !is_array($callbacks->callbacks)) return false;
  32. foreach ($callbacks->callbacks as $callbackByPriority) {
  33. if (empty($callbackByPriority) || !is_array($callbackByPriority)) continue;
  34. foreach ($callbackByPriority as $callback) {
  35. if (!is_array($callback)) return true;// this is not our callback
  36. if (empty($callback['function']) || !is_array($callback['function'])) return true; // not our callback
  37. if (isset($callback['function'][1]) && $callback['function'][1] === 'handleOrderTotalShortcode') continue;
  38. if (isset($callback['function'][1]) && $callback['function'][1] === 'handleOrderDateShortcode') continue;
  39. return true;
  40. }
  41. }
  42. }
  43. public function display() {
  44. $errorString = __('MailPoet recently changed how custom email shortcodes work, you may need to update your custom shortcodes.', 'mailpoet');
  45. $linkString = __('[link]See the documentation for necessary changes[/link]', 'mailpoet');
  46. $error = $errorString . '<br><br>' . Helpers::replaceLinkTags($linkString, 'https://kb.mailpoet.com/article/160-create-a-custom-shortcode', [
  47. 'target' => '_blank',
  48. 'data-beacon-article' => '581f6faac697914aa838044f',
  49. 'class' => 'mailpoet-button button-primary',
  50. ]);
  51. $extraClasses = 'mailpoet-dismissible-notice is-dismissible';
  52. return Notice::displayWarning($error, $extraClasses, self::OPTION_NAME);
  53. }
  54. public function disable() {
  55. $this->wp->setTransient(self::OPTION_NAME, true, self::DISMISS_NOTICE_TIMEOUT_SECONDS);
  56. }
  57. }