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 / post-field-updated.php

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

184 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Field Updated
4 *
5 * @package AutomatorWP\Integrations\WordPress\Triggers\Post_Field_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_Field_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_field_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 field of a post', 'automatorwp' ),
38 'select_option' => __( 'User updates <strong>a field of a post</strong>', 'automatorwp' ),
39 /* translators: %1$s: Post field. %2$s: Post title. %3$s: Number of times. */
40 'edit_label' => sprintf( __( 'User updates %1$s of %2$s %3$s time(s)', 'automatorwp' ), '{field}', '{post}', '{times}' ),
41 /* translators: %1$s: Post field. %2$s: Post title. */
42 'log_label' => sprintf( __( 'User updates %1$s of %2$s', 'automatorwp' ), '{field}', '{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 'field' => array(
55 'from' => 'post_field',
56 'fields' => array(
57 'post_field' => array(
58 'name' => __( 'Post field:', 'automatorwp' ),
59 'type' => 'select',
60 'classes' => 'automatorwp-selector',
61 'excluded_post_fields' => array( 'ID' ),
62 'options_cb' => 'automatorwp_options_cb_post_fields',
63 'option_none' => true,
64 'default' => 'any'
65 )
66 )
67 ),
68 'times' => automatorwp_utilities_times_option(),
69 ),
70 'tags' => array_merge(
71 automatorwp_utilities_post_tags(),
72 automatorwp_utilities_times_tag()
73 )
74 ) );
75
76 }
77
78
79
80 /**
81 * Trigger listener
82 *
83 * @since 1.0.0
84 *
85 * @param WP_Post $post_after Post object following the update.
86 * @param WP_Post $post_before Post object before the update.
87 * @param int $post_ID The post ID
88 */
89 public function listener( $post_ID, $post_after, $post_before ) {
90
91
92 // Check if it is an autosave or a revision.
93 if ( wp_is_post_autosave( $post_ID ) || wp_is_post_revision( $post_ID ) ) {
94 return;
95 }
96
97 // Bail if is a new post
98 if( $post_before->post_status === 'auto-draft' ) {
99 return;
100 }
101
102 if ( isset( $_POST['original_post_status'] ) ) {
103 if ( !empty( $_POST ) || $_POST['original_post_status'] === 'auto-draft' ) {
104 return;
105 }
106 }
107
108 // Bail if post is removed
109 if ( $post_after->post_status === 'trash' ) {
110 return;
111 }
112
113 /**
114 * User ID for post updated triggers
115 *
116 * @since 1.0.0
117 *
118 * @param int $user_id The user ID
119 * @param int $post_ID The post ID
120 * @param string $trigger The trigger
121 */
122 $user_id = apply_filters( 'automatorwp_post_updated_user_id', $post_after->post_author, $post_ID, $this->trigger );
123
124 automatorwp_trigger_event( array(
125 'trigger' => $this->trigger,
126 'user_id' => $user_id,
127 'post_id' => $post_ID,
128 'post_before' => $post_before,
129 'post_after' => $post_after,
130
131 ) );
132
133 }
134
135 /**
136 * User deserves check
137 *
138 * @since 1.0.0
139 *
140 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
141 * @param stdClass $trigger The trigger object
142 * @param int $user_id The user ID
143 * @param array $event Event information
144 * @param array $trigger_options The trigger's stored options
145 * @param stdClass $automation The trigger's automation object
146 *
147 * @return bool True if user deserves trigger, false otherwise
148 */
149 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
150
151 // Don't deserve if post is not received
152 if( ! isset( $event['post_id'] ) ) {
153 return false;
154 }
155
156 $post = get_post( absint( $event['post_id'] ) );
157
158 // Don't deserve if post doesn't exists
159 if( ! $post ) {
160 return false;
161 }
162
163 $post_field = $trigger_options['post_field'];
164 $old_value = $event['post_before'];
165 $new_value = $event['post_after'];
166
167 // Don't deserve if post doesn't match with the trigger option
168 if( $post_field !== 'any' && $old_value->{$post_field} === $new_value->{$post_field} ) {
169 return false;
170 }
171
172 // Don't deserve if post doesn't match with the trigger option
173 if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
174 return false;
175 }
176
177 return $deserves_trigger;
178
179 }
180
181 }
182
183 new AutomatorWP_WordPress_Post_Field_Updated( 'wordpress' );
184 new AutomatorWP_WordPress_Post_Field_Updated( 'posts' );