PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 5.8.6
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v5.8.6
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / integrations / wordpress / triggers / publish-post-taxonomy.php

publish-post-taxonomy.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 5.8.6, at integrations/wordpress/triggers/publish-post-taxonomy.php

183 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Publish Post Taxonomy
4 *
5 * @package AutomatorWP\Integrations\WordPress\Triggers\Publish_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_Publish_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 . '_publish_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 publishes a post in a taxonomy', 'automatorwp' ),
38 'select_option' => __( 'User publishes a post in <strong>a taxonomy</strong>', 'automatorwp' ),
39 /* translators: %1$s: Term title. %2$s: Number of times. */
40 'edit_label' => sprintf( __( 'User publishes a post in %1$s %2$s time(s)', 'automatorwp' ), '{term}', '{times}' ),
41 /* translators: %1$s: Term title. */
42 'log_label' => sprintf( __( 'User publishes a post in %1$s', 'automatorwp' ), '{term}' ),
43 'action' => 'transition_post_status',
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 string $new_status The new post status
65 * @param string $old_status The old post status
66 * @param WP_Post $post The post
67 */
68 public function listener( $new_status, $old_status, $post ) {
69
70 // Bail if post has been already published
71 if( $old_status === 'publish' ) {
72 return;
73 }
74
75 // Bail if post is not published
76 if( $new_status !== 'publish' ) {
77 return;
78 }
79
80 // When inserting a post through the block editor, the post gets updated through rest API
81 // This is required since on rest saving the post gets updated BEFORE get taxonomies applied
82 if( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
83 // Hook the action 'rest_after_insert_{post_type}'
84 add_action( "rest_after_insert_{$post->post_type}", array( $this, 'rest_listener' ), 10, 3 );
85 return;
86 }
87
88 $taxonomies = get_object_taxonomies( $post->post_type );
89
90 foreach( $taxonomies as $taxonomy ) {
91
92 $terms_ids = automatorwp_get_term_ids( $post->ID, $taxonomy );
93
94 // Bail if post isn't assigned to any term
95 if( empty( $terms_ids ) ) {
96 continue;
97 }
98
99 automatorwp_trigger_event( array(
100 'trigger' => $this->trigger,
101 'user_id' => $post->post_author,
102 'post_id' => $post->ID,
103 'taxonomy' => $taxonomy,
104 'terms_ids' => $terms_ids,
105 ) );
106
107 }
108
109 }
110
111 /**
112 * Trigger rest listener
113 *
114 * @since 1.0.0
115 *
116 * @param WP_Post $post Inserted or updated post object.
117 * @param WP_REST_Request $request Request object.
118 * @param bool $creating True when creating a post, false when updating.
119 */
120 public function rest_listener( $post, $request, $creating ) {
121
122 $taxonomies = get_object_taxonomies( $post->post_type );
123
124 foreach( $taxonomies as $taxonomy ) {
125
126 $terms_ids = automatorwp_get_term_ids( $post->ID, $taxonomy );
127
128 // Bail if post isn't assigned to any term
129 if( empty( $terms_ids ) ) {
130 continue;
131 }
132
133 automatorwp_trigger_event( array(
134 'trigger' => $this->trigger,
135 'user_id' => $post->post_author,
136 'post_id' => $post->ID,
137 'taxonomy' => $taxonomy,
138 'terms_ids' => $terms_ids,
139 ) );
140
141 }
142
143 }
144
145 /**
146 * User deserves check
147 *
148 * @since 1.0.0
149 *
150 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
151 * @param stdClass $trigger The trigger object
152 * @param int $user_id The user ID
153 * @param array $event Event information
154 * @param array $trigger_options The trigger's stored options
155 * @param stdClass $automation The trigger's automation object
156 *
157 * @return bool True if user deserves trigger, false otherwise
158 */
159 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
160
161 // Don't deserve if post and terms IDs are not received
162 if( ! isset( $event['post_id'] ) && ! isset( $event['taxonomy'] ) && ! isset( $event['terms_ids'] ) ) {
163 return false;
164 }
165
166 // Don't deserve if taxonomy doesn't match with the trigger option
167 if( $trigger_options['taxonomy'] !== 'any' && $trigger_options['taxonomy'] !== $event['taxonomy'] ) {
168 return false;
169 }
170
171 // Don't deserve if term doesn't match with the trigger option
172 if( ! automatorwp_terms_matches( $event['terms_ids'], $trigger_options['term'] ) ) {
173 return false;
174 }
175
176 return $deserves_trigger;
177
178 }
179
180 }
181
182 new AutomatorWP_WordPress_Publish_Post_Taxonomy( 'wordpress' );
183 new AutomatorWP_WordPress_Publish_Post_Taxonomy( 'posts' );