PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
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
← All changes | integrations/wordpress/triggers/publish-post-category.php +169 -0 5.7.9.2 → 6.0.3 View file →
@@ -1,0 +1,169 @@
1 +<?php
2 +/**
3 + * Publish Post Category
4 + *
5 + * @package AutomatorWP\Integrations\WordPress\Triggers\Publish_Post_Category
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_Category 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_category';
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 category', 'automatorwp' ),
38 + 'select_option' => __( 'User publishes a post in <strong>a category</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_term_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 not is a post
71 + if( $post->post_type !== 'post' ) {
72 + return;
73 + }
74 +
75 + // Bail if post has been already published
76 + if( $old_status === 'publish' ) {
77 + return;
78 + }
79 +
80 + // Bail if post is not published
81 + if( $new_status !== 'publish' ) {
82 + return;
83 + }
84 +
85 + // When inserting a post through the block editor, the post gets updated through rest API
86 + // This is required since on rest saving the post gets updated BEFORE get taxonomies applied
87 + if( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
88 + // Hook the action 'rest_after_insert_{post_type}'
89 + add_action( 'rest_after_insert_post', array( $this, 'rest_listener' ), 10, 3 );
90 + return;
91 + }
92 +
93 + $terms_ids = automatorwp_get_term_ids( $post->ID, 'category' );
94 +
95 + // Bail if post isn't assigned to any category
96 + if( empty( $terms_ids ) ) {
97 + return;
98 + }
99 +
100 + automatorwp_trigger_event( array(
101 + 'trigger' => $this->trigger,
102 + 'user_id' => $post->post_author,
103 + 'post_id' => $post->ID,
104 + 'terms_ids' => $terms_ids,
105 + ) );
106 +
107 + }
108 +
109 + /**
110 + * Trigger rest listener
111 + *
112 + * @since 1.0.0
113 + *
114 + * @param WP_Post $post Inserted or updated post object.
115 + * @param WP_REST_Request $request Request object.
116 + * @param bool $creating True when creating a post, false when updating.
117 + */
118 + public function rest_listener( $post, $request, $creating ) {
119 +
120 + $terms_ids = automatorwp_get_term_ids( $post->ID, 'category' );
121 +
122 + // Bail if post isn't assigned to any category
123 + if( empty( $terms_ids ) ) {
124 + return;
125 + }
126 +
127 + automatorwp_trigger_event( array(
128 + 'trigger' => $this->trigger,
129 + 'user_id' => $post->post_author,
130 + 'post_id' => $post->ID,
131 + 'terms_ids' => $terms_ids,
132 + ) );
133 +
134 + }
135 +
136 + /**
137 + * User deserves check
138 + *
139 + * @since 1.0.0
140 + *
141 + * @param bool $deserves_trigger True if user deserves trigger, false otherwise
142 + * @param stdClass $trigger The trigger object
143 + * @param int $user_id The user ID
144 + * @param array $event Event information
145 + * @param array $trigger_options The trigger's stored options
146 + * @param stdClass $automation The trigger's automation object
147 + *
148 + * @return bool True if user deserves trigger, false otherwise
149 + */
150 + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
151 +
152 + // Don't deserve if post and terms IDs are not received
153 + if( ! isset( $event['post_id'] ) && ! isset( $event['terms_ids'] ) ) {
154 + return false;
155 + }
156 +
157 + // Don't deserve if term doesn't match with the trigger option
158 + if( ! automatorwp_terms_matches( $event['terms_ids'], $trigger_options['term'] ) ) {
159 + return false;
160 + }
161 +
162 + return $deserves_trigger;
163 +
164 + }
165 +
166 +}
167 +
168 +new AutomatorWP_WordPress_Publish_Post_Category( 'wordpress' );
169 +new AutomatorWP_WordPress_Publish_Post_Category( 'posts' );