| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post Updated |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\WordPress\Triggers\Post_Updated |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_WordPress_Post_Updated extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
/** |
| 15 |
* Initialize the trigger |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
public function __construct( $integration ) { |
| 20 |
|
| 21 |
$this->integration = $integration; |
| 22 |
$this->trigger = $integration . '_post_updated'; |
| 23 |
|
| 24 |
parent::__construct(); |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register the trigger |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public function register() { |
| 34 |
|
| 35 |
automatorwp_register_trigger( $this->trigger, array( |
| 36 |
'integration' => $this->integration, |
| 37 |
'label' => __( 'User updates a post', 'automatorwp' ), |
| 38 |
'select_option' => __( 'User updates <strong>a post</strong>', 'automatorwp' ), |
| 39 |
/* translators: %1$s: Post title. %2$s: Number of times. */ |
| 40 |
'edit_label' => sprintf( __( 'User updates %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ), |
| 41 |
/* translators: %1$s: Post title. */ |
| 42 |
'log_label' => sprintf( __( 'User updates %1$s', 'automatorwp' ), '{post}' ), |
| 43 |
'action' => 'post_updated', |
| 44 |
'function' => array( $this, 'listener' ), |
| 45 |
'priority' => 10, |
| 46 |
'accepted_args' => 3, |
| 47 |
'options' => array( |
| 48 |
'post' => automatorwp_utilities_post_option( array( |
| 49 |
'post_type' => 'any', |
| 50 |
'option_none_label' => __( 'any post', 'automatorwp' ), |
| 51 |
'option_custom' => true, |
| 52 |
'option_custom_desc' => __( 'Post ID', 'automatorwp' ), |
| 53 |
) ), |
| 54 |
'times' => automatorwp_utilities_times_option(), |
| 55 |
), |
| 56 |
'tags' => array_merge( |
| 57 |
automatorwp_utilities_post_tags(), |
| 58 |
automatorwp_utilities_times_tag() |
| 59 |
) |
| 60 |
) ); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Trigger listener |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
* |
| 69 |
* @param WP_Post $post_after Post object following the update. |
| 70 |
* @param WP_Post $post_before Post object before the update. |
| 71 |
* @param int $post_ID The post ID |
| 72 |
*/ |
| 73 |
public function listener( $post_ID, $post_after, $post_before ) { |
| 74 |
|
| 75 |
// Check if it is an autosave or a revision. |
| 76 |
if ( wp_is_post_autosave( $post_ID ) || wp_is_post_revision( $post_ID ) ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
// Bail if is a new post |
| 81 |
if( $post_before->post_status === 'auto-draft' ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if ( isset( $_POST['original_post_status'] ) ) { |
| 86 |
if ( !empty( $_POST ) || $_POST['original_post_status'] === 'auto-draft' ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// Bail if post is removed |
| 92 |
if ( $post_after->post_status === 'trash' ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* User ID for post updated triggers |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* |
| 101 |
* @param int $user_id The user ID |
| 102 |
* @param int $post_ID The post ID |
| 103 |
* @param string $trigger The trigger |
| 104 |
*/ |
| 105 |
$user_id = apply_filters( 'automatorwp_post_updated_user_id', $post_after->post_author, $post_ID, $this->trigger ); |
| 106 |
|
| 107 |
automatorwp_trigger_event( array( |
| 108 |
'trigger' => $this->trigger, |
| 109 |
'user_id' => $user_id, |
| 110 |
'post_id' => $post_ID, |
| 111 |
) ); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* User deserves check |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
* |
| 120 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 121 |
* @param stdClass $trigger The trigger object |
| 122 |
* @param int $user_id The user ID |
| 123 |
* @param array $event Event information |
| 124 |
* @param array $trigger_options The trigger's stored options |
| 125 |
* @param stdClass $automation The trigger's automation object |
| 126 |
* |
| 127 |
* @return bool True if user deserves trigger, false otherwise |
| 128 |
*/ |
| 129 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 130 |
|
| 131 |
// Don't deserve if post is not received |
| 132 |
if( ! isset( $event['post_id'] ) ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
// Don't deserve if post doesn't match with the trigger option |
| 137 |
if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
return $deserves_trigger; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
new AutomatorWP_WordPress_Post_Updated( 'wordpress' ); |
| 148 |
new AutomatorWP_WordPress_Post_Updated( 'posts' ); |