| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post Taxonomy Updated |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\WordPress\Triggers\Post_Taxonomy_Updated |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_WordPress_Post_Taxonomy_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_taxonomy_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 of a taxonomy', 'automatorwp' ), |
| 38 |
'select_option' => __( 'User updates a post of <strong>a taxonomy</strong>', 'automatorwp' ), |
| 39 |
/* translators: %1$s: Term title. %2$s: Number of times. */ |
| 40 |
'edit_label' => sprintf( __( 'User updates a post of %1$s %2$s time(s)', 'automatorwp' ), '{term}', '{times}' ), |
| 41 |
/* translators: %1$s: Term title. */ |
| 42 |
'log_label' => sprintf( __( 'User updates a post of %1$s', 'automatorwp' ), '{term}' ), |
| 43 |
'action' => 'post_updated', |
| 44 |
'function' => array( $this, 'listener' ), |
| 45 |
'priority' => 10, |
| 46 |
'accepted_args' => 3, |
| 47 |
'options' => array( |
| 48 |
'term' => automatorwp_utilities_taxonomy_option(), |
| 49 |
'times' => automatorwp_utilities_times_option(), |
| 50 |
), |
| 51 |
'tags' => array_merge( |
| 52 |
automatorwp_utilities_post_tags(), |
| 53 |
automatorwp_utilities_times_tag() |
| 54 |
) |
| 55 |
) ); |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Trigger listener |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
* @param WP_Post $post_after Post object following the update. |
| 65 |
* @param WP_Post $post_before Post object before the update. |
| 66 |
* @param int $post_ID The post ID |
| 67 |
*/ |
| 68 |
public function listener( $post_ID, $post_after, $post_before ) { |
| 69 |
|
| 70 |
// Check if it is an autosave or a revision. |
| 71 |
if ( wp_is_post_autosave( $post_ID ) || wp_is_post_revision( $post_ID ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
// Bail if is a new post |
| 76 |
if( $post_before->post_status === 'auto-draft' ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ( isset( $_POST['original_post_status'] ) ) { |
| 81 |
if ( !empty( $_POST ) || $_POST['original_post_status'] === 'auto-draft' ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Bail if post is removed |
| 87 |
if ( $post_after->post_status === 'trash' ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$taxonomies = get_object_taxonomies( $post_after->post_type ); |
| 92 |
|
| 93 |
foreach( $taxonomies as $taxonomy ) { |
| 94 |
|
| 95 |
$terms_ids = automatorwp_get_term_ids( $post_after->ID, $taxonomy ); |
| 96 |
|
| 97 |
// Bail if post isn't assigned to any category |
| 98 |
if( empty( $terms_ids ) ) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
/** |
| 102 |
* User ID for post updated triggers |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* |
| 106 |
* @param int $user_id The user ID |
| 107 |
* @param int $post_ID The post ID |
| 108 |
* @param string $trigger The trigger |
| 109 |
*/ |
| 110 |
$user_id = apply_filters( 'automatorwp_post_updated_user_id', $post_after->post_author, $post_ID, $this->trigger ); |
| 111 |
|
| 112 |
automatorwp_trigger_event( array( |
| 113 |
'trigger' => $this->trigger, |
| 114 |
'user_id' => $user_id, |
| 115 |
'post_id' => $post_ID, |
| 116 |
'taxonomy' => $taxonomy, |
| 117 |
'terms_ids' => $terms_ids, |
| 118 |
) ); |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* User deserves check |
| 125 |
* |
| 126 |
* @since 1.0.0 |
| 127 |
* |
| 128 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 129 |
* @param stdClass $trigger The trigger object |
| 130 |
* @param int $user_id The user ID |
| 131 |
* @param array $event Event information |
| 132 |
* @param array $trigger_options The trigger's stored options |
| 133 |
* @param stdClass $automation The trigger's automation object |
| 134 |
* |
| 135 |
* @return bool True if user deserves trigger, false otherwise |
| 136 |
*/ |
| 137 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 138 |
|
| 139 |
// Don't deserve if post, taxonomy and terms IDs are not received |
| 140 |
if( ! isset( $event['post_id'] ) && ! isset( $event['taxonomy'] ) && ! isset( $event['terms_ids'] ) ) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
// Don't deserve if taxonomy doesn't match with the trigger option |
| 145 |
if( $trigger_options['taxonomy'] !== 'any' && $trigger_options['taxonomy'] !== $event['taxonomy'] ) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
// Don't deserve if term doesn't match with the trigger option |
| 150 |
if( ! automatorwp_terms_matches( $event['terms_ids'], $trigger_options['term'] ) ) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
return $deserves_trigger; |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
new AutomatorWP_WordPress_Post_Taxonomy_Updated( 'wordpress' ); |
| 161 |
new AutomatorWP_WordPress_Post_Taxonomy_Updated( 'posts' ); |