Skip to content

Commit

Permalink
Extend Notifications API for Revision Status notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
agapetry committed Dec 10, 2024
1 parent 1aaa98a commit 6f711d0
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/Notifications/Shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
111 changes: 111 additions & 0 deletions lib/Notifications/Workflow/Step/Receiver/ParentAuthor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* @package PublishPress\Notifications
* @author PublishPress <[email protected]>
* @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;
}
}
2 changes: 1 addition & 1 deletion lib/Notifications/Workflow/WorkflowsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 9 additions & 5 deletions modules/improved-notifications/improved-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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,
],
];
Expand Down Expand Up @@ -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,
],
];
Expand Down
15 changes: 10 additions & 5 deletions modules/notifications/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6f711d0

Please sign in to comment.