← All changes
|
integrations/wordpress/triggers/delete-post-taxonomy.php
+151
-0
5.7.3
→
6.0.3
View file →
| @@ -1,0 +1,151 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Delete Post Taxonomy | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WordPress\Triggers\Delete_Post_Taxonomy | |
| 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_Delete_Post_Taxonomy 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 . '_delete_post_taxonomy'; | |
| 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 deletes a post in a taxonomy', 'automatorwp' ), | |
| 38 | + 'select_option' => __( 'User deletes a post in <strong>a taxonomy</strong>', 'automatorwp' ), | |
| 39 | + /* translators: %1$s: Term title. %2$s: Number of times. */ | |
| 40 | + 'edit_label' => sprintf( __( 'User deletes a post in %1$s %2$s time(s)', 'automatorwp' ), '{term}', '{times}' ), | |
| 41 | + /* translators: %1$s: Term title. */ | |
| 42 | + 'log_label' => sprintf( __( 'User deletes a post in %1$s', 'automatorwp' ), '{term}' ), | |
| 43 | + 'action' => array( | |
| 44 | + 'trashed_post', | |
| 45 | + 'before_delete_post' | |
| 46 | + ), | |
| 47 | + 'function' => array( $this, 'listener' ), | |
| 48 | + 'priority' => 10, | |
| 49 | + 'accepted_args' => 2, | |
| 50 | + 'options' => array( | |
| 51 | + 'term' => automatorwp_utilities_taxonomy_option(), | |
| 52 | + 'times' => automatorwp_utilities_times_option(), | |
| 53 | + ), | |
| 54 | + 'tags' => array_merge( | |
| 55 | + automatorwp_utilities_post_tags(), | |
| 56 | + automatorwp_utilities_times_tag() | |
| 57 | + ) | |
| 58 | + ) ); | |
| 59 | + | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Trigger listener | |
| 64 | + * | |
| 65 | + * @since 1.0.0 | |
| 66 | + * | |
| 67 | + * @param int $post_id The post ID | |
| 68 | + * @param object|string $post_data | |
| 69 | + */ | |
| 70 | + public function listener( $post_id = 0, $post_data = null ) { | |
| 71 | + | |
| 72 | + $post = get_post( $post_id ); | |
| 73 | + | |
| 74 | + if ( ! $post ) { | |
| 75 | + return; | |
| 76 | + } | |
| 77 | + | |
| 78 | + if ( $post_data === null ) { | |
| 79 | + return; | |
| 80 | + } | |
| 81 | + | |
| 82 | + // trashed_post hook returns string with previous status | |
| 83 | + // before_delete_post hook returns WP_post with eliminated post data | |
| 84 | + $post_status = ( is_object( $post_data ) ) ? $post_data->post_status : $post_data; | |
| 85 | + | |
| 86 | + if( ! in_array( $post_status, array( 'publish', 'private' ) ) ) { | |
| 87 | + return; | |
| 88 | + } | |
| 89 | + | |
| 90 | + $taxonomies = get_object_taxonomies( $post->post_type ); | |
| 91 | + | |
| 92 | + foreach( $taxonomies as $taxonomy ) { | |
| 93 | + | |
| 94 | + $terms_ids = automatorwp_get_term_ids( $post->ID, $taxonomy ); | |
| 95 | + | |
| 96 | + // Bail if post isn't assigned to any term | |
| 97 | + if( empty( $terms_ids ) ) { | |
| 98 | + continue; | |
| 99 | + } | |
| 100 | + | |
| 101 | + automatorwp_trigger_event( array( | |
| 102 | + 'trigger' => $this->trigger, | |
| 103 | + 'user_id' => $post->post_author, | |
| 104 | + 'post_id' => $post->ID, | |
| 105 | + 'taxonomy' => $taxonomy, | |
| 106 | + 'terms_ids' => $terms_ids, | |
| 107 | + ) ); | |
| 108 | + | |
| 109 | + } | |
| 110 | + | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * User deserves check | |
| 115 | + * | |
| 116 | + * @since 1.0.0 | |
| 117 | + * | |
| 118 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 119 | + * @param stdClass $trigger The trigger object | |
| 120 | + * @param int $user_id The user ID | |
| 121 | + * @param array $event Event information | |
| 122 | + * @param array $trigger_options The trigger's stored options | |
| 123 | + * @param stdClass $automation The trigger's automation object | |
| 124 | + * | |
| 125 | + * @return bool True if user deserves trigger, false otherwise | |
| 126 | + */ | |
| 127 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 128 | + | |
| 129 | + // Don't deserve if post and terms IDs are not received | |
| 130 | + if( ! isset( $event['post_id'] ) && ! isset( $event['taxonomy'] ) && ! isset( $event['terms_ids'] ) ) { | |
| 131 | + return false; | |
| 132 | + } | |
| 133 | + | |
| 134 | + // Don't deserve if taxonomy doesn't match with the trigger option | |
| 135 | + if( $trigger_options['taxonomy'] !== 'any' && $trigger_options['taxonomy'] !== $event['taxonomy'] ) { | |
| 136 | + return false; | |
| 137 | + } | |
| 138 | + | |
| 139 | + // Don't deserve if term doesn't match with the trigger option | |
| 140 | + if( ! automatorwp_terms_matches( $event['terms_ids'], $trigger_options['term'] ) ) { | |
| 141 | + return false; | |
| 142 | + } | |
| 143 | + | |
| 144 | + return $deserves_trigger; | |
| 145 | + | |
| 146 | + } | |
| 147 | + | |
| 148 | +} | |
| 149 | + | |
| 150 | +new AutomatorWP_WordPress_Delete_Post_Taxonomy( 'wordpress' ); | |
| 151 | +new AutomatorWP_WordPress_Delete_Post_Taxonomy( 'posts' ); | |