Skip to content

Commit

Permalink
fix: update date-related merge tags to use callable timestamp providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Dartui authored and jakubmikita committed Jul 17, 2024
1 parent 82f7228 commit 05b4fa9
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 69 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions src/Repository/GlobalMergeTagRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ public static function register()
'slug' => 'date',
'name' => __('Trigger execution date', 'notification'),
'hidden' => true,
'timestamp' => time(),
'timestamp' => static function () {
return time();
},
]
)
);
Expand All @@ -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();
},
]
)
);
Expand All @@ -192,7 +196,9 @@ public static function register()
'slug' => 'time',
'name' => __('Trigger execution time', 'notification'),
'hidden' => true,
'timestamp' => time(),
'timestamp' => static function () {
return time();
},
]
)
);
Expand Down
13 changes: 11 additions & 2 deletions src/Repository/MergeTag/DateTime/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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'] .= __(
Expand All @@ -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())};
Expand Down
14 changes: 11 additions & 3 deletions src/Repository/MergeTag/DateTime/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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'] .= __(
Expand All @@ -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())};
Expand Down
14 changes: 11 additions & 3 deletions src/Repository/MergeTag/DateTime/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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'));
}
Expand All @@ -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())};
Expand Down
3 changes: 0 additions & 3 deletions src/Repository/Trigger/Post/PostAdded.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 3 additions & 5 deletions src/Repository/Trigger/Post/PostApproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
},
]
)
);
Expand Down
3 changes: 0 additions & 3 deletions src/Repository/Trigger/Post/PostDrafted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 0 additions & 3 deletions src/Repository/Trigger/Post/PostPending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 3 additions & 5 deletions src/Repository/Trigger/Post/PostPublished.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
},
]
)
);
Expand Down
8 changes: 3 additions & 5 deletions src/Repository/Trigger/Post/PostPublishedPrivately.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
},
]
)
);
Expand Down
8 changes: 3 additions & 5 deletions src/Repository/Trigger/Post/PostScheduled.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
},
]
)
);
Expand Down
3 changes: 0 additions & 3 deletions src/Repository/Trigger/Post/PostTrashed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
64 changes: 41 additions & 23 deletions src/Repository/Trigger/Post/PostTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
},
]
)
);
Expand All @@ -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);
},
]
)
);
Expand Down Expand Up @@ -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);
}
}
3 changes: 0 additions & 3 deletions src/Repository/Trigger/Post/PostUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 05b4fa9

Please sign in to comment.