| 1 |
<?php |
| 2 |
|
| 3 |
namespace WordProof\SDK\Controllers; |
| 4 |
|
| 5 |
use WordProof\SDK\Helpers\ClassicNoticeHelper; |
| 6 |
use WordProof\SDK\Helpers\PostMetaHelper; |
| 7 |
use WordProof\SDK\Helpers\TimestampHelper; |
| 8 |
use WordProof\SDK\Helpers\TransientHelper; |
| 9 |
|
| 10 |
class TimestampController |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Timestamp an post triggered by custom action. |
| 14 |
* |
| 15 |
* @param integer $postId The post id to be timestamped. |
| 16 |
* @action wordproof_timestamp |
| 17 |
*/ |
| 18 |
public static function timestamp($postId) |
| 19 |
{ |
| 20 |
$post = get_post(intval($postId)); |
| 21 |
|
| 22 |
return TimestampHelper::debounce($post); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Timestamp new posts except those inserted by the API.. |
| 27 |
* |
| 28 |
* @param integer $postId The post id to be timestamped. |
| 29 |
* @param \WP_Post $post The post to be timestamped. |
| 30 |
* @action wp_insert_post |
| 31 |
*/ |
| 32 |
public function timestampAfterPostRequest($postId, $post) |
| 33 |
{ |
| 34 |
if (\defined('REST_REQUEST') && \REST_REQUEST) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$response = TimestampHelper::debounce($post); |
| 39 |
|
| 40 |
ClassicNoticeHelper::addTimestampNotice($response); |
| 41 |
|
| 42 |
return $response; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Timestamp posts inserted by the API. |
| 47 |
* |
| 48 |
* @param \WP_Post $post The post to be timestamped. |
| 49 |
* @action rest_after_insert_post |
| 50 |
*/ |
| 51 |
public function timestampAfterRestApiRequest($post) |
| 52 |
{ |
| 53 |
return TimestampHelper::debounce($post); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Removes action to timestamp post on insert if Elementor is used. |
| 58 |
*/ |
| 59 |
public function beforeElementorSave() |
| 60 |
{ |
| 61 |
remove_action('rest_after_insert_post', [$this, 'timestampAfterRestApiRequest']); |
| 62 |
remove_action('wp_insert_post', [$this, 'timestampAfterPostRequest'], \PHP_INT_MAX); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Syncs timestamp override post meta keys. |
| 67 |
* |
| 68 |
* @param $metaId |
| 69 |
* @param $postId |
| 70 |
* @param $metaKey |
| 71 |
* @param $metaValue |
| 72 |
*/ |
| 73 |
public function syncPostMetaTimestampOverrides($metaId, $postId, $metaKey, $metaValue) |
| 74 |
{ |
| 75 |
$timestampablePostMetaKeys = apply_filters('wordproof_timestamp_post_meta_key_overrides', ['_wordproof_timestamp']); |
| 76 |
|
| 77 |
if (in_array($metaKey, $timestampablePostMetaKeys, true) && count($timestampablePostMetaKeys) > 1) { |
| 78 |
$arrayKey = array_search($metaKey, $timestampablePostMetaKeys, true); |
| 79 |
unset($timestampablePostMetaKeys[$arrayKey]); |
| 80 |
|
| 81 |
TransientHelper::set('wordproof_debounce_post_meta_sync_' . $metaKey . '_' . $postId, true, 5); |
| 82 |
|
| 83 |
foreach ($timestampablePostMetaKeys as $key) { |
| 84 |
TransientHelper::debounce($postId, 'post_meta_sync_' . $key, function () use ($postId, $key, $metaValue) { |
| 85 |
return PostMetaHelper::update($postId, $key, $metaValue); |
| 86 |
}); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|