From 0b3570445ed6ee25ee31219830b2c81d70441a6b Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Wed, 17 Jul 2024 16:39:59 +0200 Subject: [PATCH] fix: update date-related merge tags to use callable timestamp providers --- readme.txt | 1 + src/Repository/GlobalMergeTagRepository.php | 12 +++- src/Repository/MergeTag/DateTime/Date.php | 13 +++- src/Repository/MergeTag/DateTime/DateTime.php | 14 +++- src/Repository/MergeTag/DateTime/Time.php | 14 +++- src/Repository/Trigger/Post/PostAdded.php | 3 - src/Repository/Trigger/Post/PostApproved.php | 8 +-- src/Repository/Trigger/Post/PostDrafted.php | 3 - src/Repository/Trigger/Post/PostPending.php | 3 - src/Repository/Trigger/Post/PostPublished.php | 8 +-- .../Trigger/Post/PostPublishedPrivately.php | 8 +-- src/Repository/Trigger/Post/PostScheduled.php | 8 +-- src/Repository/Trigger/Post/PostTrashed.php | 3 - src/Repository/Trigger/Post/PostTrigger.php | 64 ++++++++++++------- src/Repository/Trigger/Post/PostUpdated.php | 3 - 15 files changed, 96 insertions(+), 69 deletions(-) diff --git a/readme.txt b/readme.txt index 26e89071..1bb605d5 100644 --- a/readme.txt +++ b/readme.txt @@ -377,6 +377,7 @@ Removed deprecated hooks: * [Changed] Namespace `BracketSpace\Notification\Defaults\` to `BracketSpace\Notification\Repository\`. * [Changed] Runtime components are now referenced by FQCN (Fully Qualified Class Name), instead of the name. * [Changed] Abstract classes are now renamed BaseSomething convention and placed in Repository dir. +* [Changed] Date-related merge tags (`Date`, `DateTime` and `Time`) now requires `timestamp` argument to be callable. * [Fixed] Shortcodes being uncorrectly stripped leaving closing "]" behind. * [Fixed] PHP 8.2 deprecations. * [Fixed] Stripping shortcodes in carrier fields. diff --git a/src/Repository/GlobalMergeTagRepository.php b/src/Repository/GlobalMergeTagRepository.php index daeb9f27..59db4062 100644 --- a/src/Repository/GlobalMergeTagRepository.php +++ b/src/Repository/GlobalMergeTagRepository.php @@ -170,7 +170,9 @@ public static function register() 'slug' => 'date', 'name' => __('Trigger execution date', 'notification'), 'hidden' => true, - 'timestamp' => time(), + 'timestamp' => static function () { + return time(); + }, ] ) ); @@ -181,7 +183,9 @@ public static function register() 'slug' => 'date_time', 'name' => __('Trigger execution date and time', 'notification'), 'hidden' => true, - 'timestamp' => time(), + 'timestamp' => static function () { + return time(); + }, ] ) ); @@ -192,7 +196,9 @@ public static function register() 'slug' => 'time', 'name' => __('Trigger execution time', 'notification'), 'hidden' => true, - 'timestamp' => time(), + 'timestamp' => static function () { + return time(); + }, ] ) ); diff --git a/src/Repository/MergeTag/DateTime/Date.php b/src/Repository/MergeTag/DateTime/Date.php index 682c470f..78849910 100644 --- a/src/Repository/MergeTag/DateTime/Date.php +++ b/src/Repository/MergeTag/DateTime/Date.php @@ -5,7 +5,7 @@ * * Requirements: * - Trigger property of the merge tag slug with timestamp - * - or 'timestamp' parameter in arguments with timestamp + * - or 'timestamp' parameter in arguments with callback to resolve value * * @package notification */ @@ -45,6 +45,10 @@ public function __construct($params = []) ] ); + if (isset($args['timestamp']) && !is_callable($args['timestamp'])) { + _deprecated_argument(__METHOD__, '[Next]', '"timestamp" option must be callable.'); + } + if (!isset($args['description'])) { $args['description'] = wp_date($args['date_format']) . '. '; $args['description'] .= __( @@ -55,7 +59,12 @@ public function __construct($params = []) if (!isset($args['resolver'])) { $args['resolver'] = function ($trigger) use ($args) { - if (isset($args['timestamp'])) { + if (isset($args['timestamp']) && is_callable($args['timestamp'])) { + $timestamp = call_user_func($args['timestamp'], $trigger); + } elseif (isset($args['timestamp']) && !is_callable($args['timestamp'])) { + /** + * @deprecated [Next] "timestamp" option must be callable. + */ $timestamp = $args['timestamp']; } elseif (isset($trigger->{CaseHelper::toCamel($this->getSlug())})) { $timestamp = $trigger->{CaseHelper::toCamel($this->getSlug())}; diff --git a/src/Repository/MergeTag/DateTime/DateTime.php b/src/Repository/MergeTag/DateTime/DateTime.php index 3737daf7..7812f337 100644 --- a/src/Repository/MergeTag/DateTime/DateTime.php +++ b/src/Repository/MergeTag/DateTime/DateTime.php @@ -5,7 +5,7 @@ * * Requirements: * - Trigger property of the merge tag slug with timestamp - * - or 'timestamp' parameter in arguments with timestamp + * - or 'timestamp' parameter in arguments with callback to resolve value * * @package notification */ @@ -46,6 +46,10 @@ public function __construct($params = []) ] ); + if (isset($args['timestamp']) && !is_callable($args['timestamp'])) { + _deprecated_argument(__METHOD__, '[Next]', '"timestamp" option must be callable.'); + } + if (!isset($args['description'])) { $args['description'] = wp_date($args['date_format'] . ' ' . $args['time_format']) . '. '; $args['description'] .= __( @@ -56,8 +60,12 @@ public function __construct($params = []) if (!isset($args['resolver'])) { $args['resolver'] = function ($trigger) use ($args) { - - if (isset($args['timestamp'])) { + if (isset($args['timestamp']) && is_callable($args['timestamp'])) { + $timestamp = call_user_func($args['timestamp'], $trigger); + } elseif (isset($args['timestamp']) && !is_callable($args['timestamp'])) { + /** + * @deprecated [Next] "timestamp" option must be callable. + */ $timestamp = $args['timestamp']; } elseif (isset($trigger->{CaseHelper::toCamel($this->getSlug())})) { $timestamp = $trigger->{CaseHelper::toCamel($this->getSlug())}; diff --git a/src/Repository/MergeTag/DateTime/Time.php b/src/Repository/MergeTag/DateTime/Time.php index 720290cd..0d0e3bab 100644 --- a/src/Repository/MergeTag/DateTime/Time.php +++ b/src/Repository/MergeTag/DateTime/Time.php @@ -5,7 +5,7 @@ * * Requirements: * - Trigger property of the merge tag slug with timestamp - * - or 'timestamp' parameter in arguments with timestamp + * - or 'timestamp' parameter in arguments with callback to resolve value * * @package notification */ @@ -45,6 +45,10 @@ public function __construct($params = []) ] ); + if (isset($args['timestamp']) && !is_callable($args['timestamp'])) { + _deprecated_argument(__METHOD__, '[Next]', '"timestamp" option must be callable.'); + } + if (!isset($args['group'])) { $this->setGroup(__('Date', 'notification')); } @@ -59,8 +63,12 @@ public function __construct($params = []) if (!isset($args['resolver'])) { $args['resolver'] = function ($trigger) use ($args) { - - if (isset($args['timestamp'])) { + if (isset($args['timestamp']) && is_callable($args['timestamp'])) { + $timestamp = call_user_func($args['timestamp'], $trigger); + } elseif (isset($args['timestamp']) && !is_callable($args['timestamp'])) { + /** + * @deprecated [Next] "timestamp" option must be callable. + */ $timestamp = $args['timestamp']; } elseif (isset($trigger->{CaseHelper::toCamel($this->getSlug())})) { $timestamp = $trigger->{CaseHelper::toCamel($this->getSlug())}; diff --git a/src/Repository/Trigger/Post/PostAdded.php b/src/Repository/Trigger/Post/PostAdded.php index a089e807..d652ed74 100644 --- a/src/Repository/Trigger/Post/PostAdded.php +++ b/src/Repository/Trigger/Post/PostAdded.php @@ -113,8 +113,5 @@ public function context($postId, $post, $update) $this->author = get_userdata((int)$this->post->post_author); $this->lastEditor = get_userdata((int)get_post_meta($this->post->ID, '_edit_last', true)); $this->publishingUser = get_userdata(get_current_user_id()); - - $this->postCreationDatetime = strtotime($this->post->post_date_gmt); - $this->postModificationDatetime = strtotime($this->post->post_modified_gmt); } } diff --git a/src/Repository/Trigger/Post/PostApproved.php b/src/Repository/Trigger/Post/PostApproved.php index 964d8470..d224465b 100644 --- a/src/Repository/Trigger/Post/PostApproved.php +++ b/src/Repository/Trigger/Post/PostApproved.php @@ -89,10 +89,6 @@ public function context($post) $this->author = get_userdata((int)$this->post->post_author); $this->lastEditor = get_userdata((int)get_post_meta($this->post->ID, '_edit_last', true)); $this->approvingUser = get_userdata(get_current_user_id()); - - $this->postCreationDatetime = strtotime($this->post->post_date_gmt); - $this->postPublicationDatetime = strtotime($this->post->post_date_gmt); - $this->postModificationDatetime = strtotime($this->post->post_modified_gmt); } /** @@ -266,7 +262,9 @@ public function mergeTags() ), // translators: singular post name. 'name' => sprintf(__('%s approving date and time', 'notification'), $postTypeName), - 'timestamp' => $this->postPublicationDatetime, + 'timestamp' => static function ($trigger) { + return strtotime($trigger->post->post_date_gmt); + }, ] ) ); diff --git a/src/Repository/Trigger/Post/PostDrafted.php b/src/Repository/Trigger/Post/PostDrafted.php index 1f6ee69c..e0172812 100644 --- a/src/Repository/Trigger/Post/PostDrafted.php +++ b/src/Repository/Trigger/Post/PostDrafted.php @@ -98,8 +98,5 @@ public function context($newStatus, $oldStatus, $post) $this->author = get_userdata((int)$this->post->post_author); $this->lastEditor = get_userdata((int)get_post_meta($this->post->ID, '_edit_last', true)); $this->publishingUser = get_userdata(get_current_user_id()); - - $this->postCreationDatetime = strtotime($this->post->post_date_gmt); - $this->postModificationDatetime = strtotime($this->post->post_modified_gmt); } } diff --git a/src/Repository/Trigger/Post/PostPending.php b/src/Repository/Trigger/Post/PostPending.php index 1c2fd1d0..9f84c91f 100644 --- a/src/Repository/Trigger/Post/PostPending.php +++ b/src/Repository/Trigger/Post/PostPending.php @@ -86,8 +86,5 @@ public function context($newStatus, $oldStatus, $post) $this->author = get_userdata((int)$this->post->post_author); $this->lastEditor = get_userdata((int)get_post_meta($this->post->ID, '_edit_last', true)); - - $this->postCreationDatetime = strtotime($this->post->post_date_gmt); - $this->postModificationDatetime = strtotime($this->post->post_modified_gmt); } } diff --git a/src/Repository/Trigger/Post/PostPublished.php b/src/Repository/Trigger/Post/PostPublished.php index 287aeb39..1ecbc137 100644 --- a/src/Repository/Trigger/Post/PostPublished.php +++ b/src/Repository/Trigger/Post/PostPublished.php @@ -102,10 +102,6 @@ public function context($newStatus, $oldStatus, $post) $this->author = get_userdata((int)$this->post->post_author); $this->lastEditor = get_userdata((int)get_post_meta($this->post->ID, '_edit_last', true)); $this->publishingUser = get_userdata(get_current_user_id()); - - $this->postCreationDatetime = strtotime($this->post->post_date_gmt); - $this->postPublicationDatetime = strtotime($this->post->post_date_gmt); - $this->postModificationDatetime = strtotime($this->post->post_modified_gmt); } /** @@ -279,7 +275,9 @@ public function mergeTags() ), // translators: singular post name. 'name' => sprintf(__('%s publication date and time', 'notification'), $postTypeName), - 'timestamp' => $this->postPublicationDatetime, + 'timestamp' => static function ($trigger) { + return strtotime($trigger->post->post_date_gmt); + }, ] ) ); diff --git a/src/Repository/Trigger/Post/PostPublishedPrivately.php b/src/Repository/Trigger/Post/PostPublishedPrivately.php index 1438e7f7..039cc0b9 100644 --- a/src/Repository/Trigger/Post/PostPublishedPrivately.php +++ b/src/Repository/Trigger/Post/PostPublishedPrivately.php @@ -102,10 +102,6 @@ public function context($newStatus, $oldStatus, $post) $this->author = get_userdata((int)$this->post->post_author); $this->lastEditor = get_userdata((int)get_post_meta($this->post->ID, '_edit_last', true)); $this->publishingUser = get_userdata(get_current_user_id()); - - $this->postCreationDatetime = strtotime($this->post->post_date_gmt); - $this->postPublicationDatetime = strtotime($this->post->post_date_gmt); - $this->postModificationDatetime = strtotime($this->post->post_modified_gmt); } /** @@ -279,7 +275,9 @@ public function mergeTags() ), // translators: singular post name. 'name' => sprintf(__('%s publication date and time', 'notification'), $postTypeName), - 'timestamp' => $this->postPublicationDatetime, + 'timestamp' => static function ($trigger) { + return strtotime($trigger->post->post_date_gmt); + }, ] ) ); diff --git a/src/Repository/Trigger/Post/PostScheduled.php b/src/Repository/Trigger/Post/PostScheduled.php index 4836b44e..f02cfb6b 100644 --- a/src/Repository/Trigger/Post/PostScheduled.php +++ b/src/Repository/Trigger/Post/PostScheduled.php @@ -97,10 +97,6 @@ public function context($newStatus, $oldStatus, $post) $this->author = get_userdata((int)$this->post->post_author); $this->lastEditor = get_userdata((int)get_post_meta($this->post->ID, '_edit_last', true)); $this->schedulingUser = get_userdata($schedulingUserId); - - $this->postCreationDatetime = strtotime($this->post->post_date_gmt); - $this->postPublicationDatetime = strtotime($this->post->post_date_gmt); - $this->postModificationDatetime = strtotime($this->post->post_modified_gmt); } /** @@ -123,7 +119,9 @@ public function mergeTags() ), // translators: singular post name. 'name' => sprintf(__('%s publication date and time', 'notification'), $postTypeName), - 'timestamp' => $this->postPublicationDatetime, + 'timestamp' => static function ($trigger) { + return strtotime($trigger->post->post_date_gmt); + }, ] ) ); diff --git a/src/Repository/Trigger/Post/PostTrashed.php b/src/Repository/Trigger/Post/PostTrashed.php index 7563f167..0119a677 100644 --- a/src/Repository/Trigger/Post/PostTrashed.php +++ b/src/Repository/Trigger/Post/PostTrashed.php @@ -90,9 +90,6 @@ public function context($postId, $post) $this->author = get_userdata((int)$this->post->post_author); $this->lastEditor = get_userdata((int)get_post_meta($this->post->ID, '_edit_last', true)); $this->trashingUser = get_userdata(get_current_user_id()); - - $this->postCreationDatetime = strtotime($this->post->post_date_gmt); - $this->postModificationDatetime = strtotime($this->post->post_modified_gmt); } /** diff --git a/src/Repository/Trigger/Post/PostTrigger.php b/src/Repository/Trigger/Post/PostTrigger.php index 8739e65c..f05a115e 100644 --- a/src/Repository/Trigger/Post/PostTrigger.php +++ b/src/Repository/Trigger/Post/PostTrigger.php @@ -47,27 +47,6 @@ abstract class PostTrigger extends BaseTrigger */ public $post; - /** - * Post creation timestamp. - * - * @var int|false - */ - public $postCreationDatetime; - - /** - * Post publication timestamp. - * - * @var int|false - */ - public $postPublicationDatetime; - - /** - * Post modification timestamp. - * - * @var int|false - */ - public $postModificationDatetime; - /** * Constructor * @@ -257,7 +236,9 @@ public function mergeTags() // translators: singular post name. 'name' => sprintf(__('%s creation date and time', 'notification'), $postTypeName), - 'timestamp' => $this->postCreationDatetime, + 'timestamp' => static function ($trigger) { + return strtotime($trigger->post->post_date_gmt); + }, ] ) ); @@ -272,7 +253,9 @@ public function mergeTags() // translators: singular post name. 'name' => sprintf(__('%s modification date and time', 'notification'), $postTypeName), - 'timestamp' => $this->postModificationDatetime, + 'timestamp' => static function ($trigger) { + return strtotime($trigger->post->post_modified_gmt); + }, ] ) ); @@ -599,4 +582,39 @@ public function mergeTags() ) ); } + + /** + * Gets the value of deprecated properties. + * + * @param string $property + * @return mixed + */ + public function __get($property) + { + $propertyMap = [ + 'postCreationDatetime' => function () { + return strtotime($this->post->post_date_gmt); + }, + 'postPublicationDatetime' => function () { + return strtotime($this->post->post_date_gmt); + }, + 'postModificationDatetime' => function () { + return strtotime($this->post->post_modified_gmt); + }, + ]; + + if (in_array($property, array_keys($propertyMap), true)) { + wp_trigger_error( + static::class, + sprintf( + 'Property `%s` is deprecated since [Next], use `post` property instead.', + $property + ) + ); + + return call_user_func($propertyMap[$property]); + } + + trigger_error('Undefined property: ' . static::class . '::$' . $property, E_USER_WARNING); + } } diff --git a/src/Repository/Trigger/Post/PostUpdated.php b/src/Repository/Trigger/Post/PostUpdated.php index 9da62041..9501693d 100644 --- a/src/Repository/Trigger/Post/PostUpdated.php +++ b/src/Repository/Trigger/Post/PostUpdated.php @@ -112,9 +112,6 @@ public function context($postId, $post, $postBefore) $this->author = get_userdata((int)$this->post->post_author); $this->lastEditor = get_userdata((int)get_post_meta($this->post->ID, '_edit_last', true)); $this->updatingUser = get_userdata($updatingUserId); - - $this->postCreationDatetime = strtotime($this->post->post_date_gmt); - $this->postModificationDatetime = strtotime($this->post->post_modified_gmt); } /**