From 6f711d0f30ef0d167817b5b39256105c070d4b1c Mon Sep 17 00:00:00 2001 From: Kevin Behrens <43488774+agapetry@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:24:50 -0500 Subject: [PATCH] Extend Notifications API for Revision Status notifications --- lib/Notifications/Shortcodes.php | 4 +- .../Workflow/Step/Receiver/ParentAuthor.php | 111 ++++++++++++++++++ .../Workflow/WorkflowsController.php | 2 +- .../improved-notifications.php | 14 ++- modules/notifications/notifications.php | 15 ++- 5 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 lib/Notifications/Workflow/Step/Receiver/ParentAuthor.php diff --git a/lib/Notifications/Shortcodes.php b/lib/Notifications/Shortcodes.php index de31c13ad..244186db6 100644 --- a/lib/Notifications/Shortcodes.php +++ b/lib/Notifications/Shortcodes.php @@ -438,9 +438,11 @@ private function get_post_field($post, $field, $attrs) case 'old_status': case 'new_status': + $status_name = apply_filters('publishpress_notifications_status', $this->event_args['params'][$field], $post); + $status = $publishpress->getPostStatusBy( 'slug', - $this->event_args['params'][$field] + $status_name ); if (empty($status) || 'WP_Error' === get_class($status)) { diff --git a/lib/Notifications/Workflow/Step/Receiver/ParentAuthor.php b/lib/Notifications/Workflow/Step/Receiver/ParentAuthor.php new file mode 100644 index 000000000..10f314bb5 --- /dev/null +++ b/lib/Notifications/Workflow/Step/Receiver/ParentAuthor.php @@ -0,0 +1,111 @@ + + * @copyright Copyright (c) 2022 PublishPress. All rights reserved. + * @license GPLv2 or later + * @since 1.0.0 + */ + +namespace PublishPress\Notifications\Workflow\Step\Receiver; + +class ParentAuthor extends Simple_Checkbox implements Receiver_Interface +{ + const META_KEY = '_psppno_toauthor'; + + const META_VALUE = 'parent_author'; + + protected $option = 'receiver_parent_author'; + + /** + * The constructor + */ + public function __construct() + { + $this->name = 'parent_author'; + $this->label = __('Authors of the parent page', 'publishpress'); + $this->option_name = 'receiver_parent_author'; + + parent::__construct(); + } + + /** + * Filters the list of receivers for the workflow. Returns the list of IDs. + * + * @param array $receivers + * @param WP_Post $workflow + * @param array $args + * + * @return array + */ + public function filter_workflow_receivers($receivers, $workflow, $args) + { + // If checked, add the authors to the list of receivers + if ($this->is_selected($workflow->ID)) { + $post = get_post($args['params']['post_id']); + + if (function_exists('rvy_in_revision_workflow') && rvy_in_revision_workflow($post->ID)) { + $parent_post_id = rvy_post_id($post->ID); + } else { + $parent_post_id = $post->post_parent; + } + + if ($parent_post_id) { + $parent_post = get_post($parent_post_id); + } + + if (empty($parent_post) || empty($posparent_postt_id)) { + return; + } + + /** + * @param int $post_author + * @param int $post_id + * + * @return int|array + */ + $post_authors = apply_filters( + 'publishpress_notifications_receiver_post_parent_authors', + [$post->post_author], + $workflow->ID, + $args + ); + + if (!is_array($post_authors)) { + $post_authors = [$post_authors]; + } + + foreach ($post_authors as $post_author) { + $receiverData = [ + 'receiver' => $post_author, + 'group' => self::META_VALUE + ]; + + if (!is_numeric($post_author) && substr_count($post_author, '@') > 0) { + $receiverData['channel'] = 'email'; + } + + $receivers[] = $receiverData; + } + } + + return $receivers; + } + + /** + * Add the respective value to the column in the workflow list + * + * @param array $values + * @param int $post_id + * + * @return array + */ + public function filter_receivers_column_value($values, $post_id) + { + if ($this->is_selected($post_id)) { + $values[] = __('Parent Page Authors', 'publishpress'); + } + + return $values; + } +} diff --git a/lib/Notifications/Workflow/WorkflowsController.php b/lib/Notifications/Workflow/WorkflowsController.php index 3c6225c50..aeb8ee174 100644 --- a/lib/Notifications/Workflow/WorkflowsController.php +++ b/lib/Notifications/Workflow/WorkflowsController.php @@ -51,7 +51,7 @@ public function trigger_workflows($params) $query = $this->get_workflows_filter_query($params); if (!empty($query->posts)) { - foreach ($query->posts as $workflowPost) { + foreach (apply_filters('publishpress_post_notification_trigger_workflows', $query->posts) as $workflowPost) { $workflow = new Workflow($workflowPost); $workflow->run($params); } diff --git a/modules/improved-notifications/improved-notifications.php b/modules/improved-notifications/improved-notifications.php index fc437099a..40cf687d7 100644 --- a/modules/improved-notifications/improved-notifications.php +++ b/modules/improved-notifications/improved-notifications.php @@ -625,11 +625,13 @@ public function action_transition_post_status($postId, $post, $update, $postBefo $oldStatus = 'new'; if (is_object($postBefore)) { - $oldStatus = $postBefore->post_status; + $oldStatus = apply_filters('publishpress_notifications_status', $postBefore->post_status, $postBefore); } + $newStatus = apply_filters('publishpress_notifications_status', $post->post_status, $post); + // Ignores if it is saved with the same status, avoiding multiple notifications on some situations. - if ($oldStatus === $post->post_status) { + if ($oldStatus === $newStatus) { return; } @@ -639,7 +641,7 @@ public function action_transition_post_status($postId, $post, $update, $postBefo 'user_id' => get_current_user_id(), 'params' => [ 'post_id' => (int)$postId, - 'new_status' => $post->post_status, + 'new_status' => $newStatus, 'old_status' => $oldStatus, ], ]; @@ -677,16 +679,18 @@ public function action_update_post($postId, $post, $update, $postBefore) $oldStatus = 'new'; if (is_object($postBefore)) { - $oldStatus = $postBefore->post_status; + $oldStatus = apply_filters('publishpress_notifications_status', $postBefore->post_status, $postBefore); } + $newStatus = apply_filters('publishpress_notifications_status', $post->post_status, $post); + // Go ahead and do the action to run workflows $params = [ 'event' => Post_Update::EVENT_NAME, 'user_id' => get_current_user_id(), 'params' => [ 'post_id' => (int)$postId, - 'new_status' => $post->post_status, + 'new_status' => $newStatus, 'old_status' => $oldStatus, ], ]; diff --git a/modules/notifications/notifications.php b/modules/notifications/notifications.php index a1d5dd129..73d0fc294 100644 --- a/modules/notifications/notifications.php +++ b/modules/notifications/notifications.php @@ -781,17 +781,19 @@ protected function get_workflows_related_to_post($post) { $workflows_controller = $this->get_service('workflows_controller'); + $post_status = apply_filters('publishpress_notifications_status', $post->post_status, $post); + $args = [ 'event' => '', 'params' => [ 'post_id' => $post->ID, - 'new_status' => $post->post_status, - 'old_status' => $post->post_status, + 'new_status' => $post_status, + 'old_status' => $post_status, 'ignore_event' => true, ], ]; - return $workflows_controller->get_filtered_workflows($args); + return apply_filters('publishpress_post_notification_get_workflows', $workflows_controller->get_filtered_workflows($args), $post); } public function action_save_post($postId) @@ -1904,9 +1906,12 @@ private function get_scheduled_datetime($post) public function send_notification_status_update($args) { - $new_status = $args['new_status']; - $old_status = $args['old_status']; $post = $args['post']; + $new_status = apply_filters('publishpress_notifications_status', $args['new_status'], $post); + $old_status = apply_filters('publishpress_notifications_status', $args['old_status'], $post); + + // Get current user + $current_user = wp_get_current_user(); // Get current user $current_user = wp_get_current_user();