PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.2
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.2
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 5.6.2 All 33 releases
automatorwp / integrations / wordpress / actions / post-meta.php

post-meta.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.2, at integrations/wordpress/actions/post-meta.php

326 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Meta
4 *
5 * @package AutomatorWP\Integrations\WordPress\Actions\Post_Meta
6 * @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 class AutomatorWP_WordPress_Post_Meta extends AutomatorWP_Integration_Action {
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->action = $integration . '_post_meta';
23
24 parent::__construct();
25
26 }
27
28 /**
29 * The post ID where meta has been applied
30 *
31 * @since 1.0.0
32 *
33 * @var int|WP_Error $post_id
34 */
35 public $post_id = 0;
36
37 /**
38 * Register the trigger
39 *
40 * @since 1.0.0
41 */
42 public function register() {
43
44 automatorwp_register_action( $this->action, array(
45 'integration' => $this->integration,
46 'label' => __( 'Update post meta', 'automatorwp' ),
47 'select_option' => __( 'Update <strong>post meta</strong>', 'automatorwp' ),
48 /* translators: %1$s: Operation (Set, insert, increment or decrement). %2$s: Post ID. %3$s: Meta value. %4$s: Meta key. */
49 'edit_label' => sprintf( __( '%1$s post %2$s meta value %3$s for meta key %4$s', 'automatorwp' ), '{operation}', '{post}', '{meta_value}', '{meta_key}' ),
50 /* translators: %1$s: Operation (Set, insert, increment or decrement). %2$s: Post ID. %3$s: Meta value. %4$s: Meta key. */
51 'log_label' => sprintf( __( '%1$s post %2$s meta value %3$s for meta key %4$s', 'automatorwp' ), '{operation}', '{post}', '{meta_value}', '{meta_key}' ),
52 'options' => array(
53 'operation' => array(
54 'from' => 'operation',
55 'fields' => array(
56 'operation' => array(
57 'name' => __( 'Operation:', 'automatorwp' ),
58 'desc' => __( 'Operation defines how the meta value will be applied. The available options are:', 'automatorwp' )
59 . '<br><br>' . __( '<strong>Set:</strong> The new value will be set as the meta value.', 'automatorwp' )
60 . '<br>' . __( 'Example: If old value is "Word" and new value is "Press", the final meta value will be "Press".', 'automatorwp' )
61 . '<br><br>' . __( '<strong>Insert:</strong> The new value will be inserted to the current meta value (for arrays, the new value will be inserted at the end, for other types, the new value will be appended).', 'automatorwp' )
62 . '<br>' . __( 'Example for arrays: If old value is array( "Word" ) and new value is "Press", the final meta value will be array( "Word", "Press" ).', 'automatorwp' )
63 . '<br>' . __( 'Example for other types: If old value is "Word" and new value is "Press", the final meta value will be "WordPress".', 'automatorwp' )
64 . '<br><br>' . __( '<strong>Increment:</strong> For numeric values, the current meta value will be incremented the same amount as the new value.', 'automatorwp' )
65 . '<br>' . __( 'Example: If old value is "5" and new value is "1", the final meta value will be "6".', 'automatorwp' )
66 . '<br><br>' . __( '<strong>Decrement:</strong> For numeric values, the current meta value will be decremented the same amount as the new value.', 'automatorwp' )
67 . '<br>' . __( 'Example: If old value is "5" and new value is "1", the final meta value will be "4".', 'automatorwp' ),
68 'type' => 'select',
69 'options' => array(
70 'set' => __( 'Set', 'automatorwp' ),
71 'insert' => __( 'Insert', 'automatorwp' ),
72 'increment' => __( 'Increment', 'automatorwp' ),
73 'decrement' => __( 'Decrement', 'automatorwp' ),
74 ),
75 'default' => 'set'
76 ),
77 )
78 ),
79 'post' => array(
80 'from' => 'post',
81 'default' => __( '(Post ID)', 'automatorwp' ),
82 'fields' => array(
83 'post' => array(
84 'name' => __( 'Post ID:', 'automatorwp' ),
85 'type' => 'text',
86 'default' => ''
87 ),
88 )
89 ),
90 'meta_value' => array(
91 'from' => 'meta_value',
92 'default' => __( 'value', 'automatorwp' ),
93 'fields' => array(
94 'meta_value' => array(
95 'name' => __( 'Meta value:', 'automatorwp' ),
96 'type' => 'text',
97 'default' => ''
98 ),
99 )
100 ),
101 'meta_key' => array(
102 'from' => 'meta_key',
103 /* translators: Refers to meta key */
104 'default' => __( 'key', 'automatorwp' ),
105 'fields' => array(
106 'meta_key' => array(
107 'name' => __( 'Meta key:', 'automatorwp' ),
108 'type' => 'text',
109 'default' => ''
110 ),
111 )
112 )
113 ),
114 'tags' => automatorwp_utilities_post_tags(),
115 ) );
116
117 }
118
119 /**
120 * Register required hooks
121 *
122 * @since 1.0.0
123 */
124 public function hooks() {
125
126 // Dynamic edit and log labels
127 add_filter( 'automatorwp_parse_automation_item_edit_label', array( $this, 'dynamic_label' ), 10, 5 );
128 add_filter( 'automatorwp_parse_automation_item_log_label', array( $this, 'dynamic_label' ), 10, 5 );
129
130 // Log post ID
131 add_filter( 'automatorwp_user_completed_action_post_id', array( $this, 'post_id' ), 10, 6 );
132
133 // Log meta data
134 add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
135
136 parent::hooks();
137
138 }
139
140 /**
141 * Custom edit/log label
142 *
143 * @since 1.0.0
144 *
145 * @param string $label The edit label
146 * @param stdClass $object The trigger/action object
147 * @param string $item_type The item type (trigger|action)
148 * @param string $context The context this function is executed
149 * @param array $type_args The type parameters
150 *
151 * @return string
152 */
153 public function dynamic_label( $label, $object, $item_type, $context, $type_args ) {
154
155 // Bail if action type don't match this action
156 if( $object->type !== $this->action ) {
157 return $label;
158 }
159
160 // Get the operation value
161 ct_setup_table( "automatorwp_{$item_type}s" );
162 $operation = ct_get_object_meta( $object->id, 'operation', true );
163 ct_reset_setup_table();
164
165 // Update the edit and log labels
166 if( in_array( $operation, array( 'increment', 'decrement' ) ) ) {
167 /* translators: %1$s: Operation (Set, insert, increment or decrement). %2$s: Post ID. %3$s: Meta value. %4$s: Meta key. */
168 return sprintf( __( '%1$s post %2$s meta value by %3$s for meta key %4$s', 'automatorwp' ), '{operation}', '{post}', '{meta_value}', '{meta_key}' );
169 }
170
171 return $label;
172
173 }
174
175 /**
176 * Action execution function
177 *
178 * @since 1.0.0
179 *
180 * @param stdClass $action The action object
181 * @param int $user_id The user ID
182 * @param array $action_options The action's stored options (with tags already passed)
183 * @param stdClass $automation The action's automation object
184 */
185 public function execute( $action, $user_id, $action_options, $automation ) {
186
187 // Shorthand
188 $operation = $action_options['operation'];
189 $post_id = $action_options['post'];
190 $meta_key = sanitize_title( $action_options['meta_key'] );
191 $meta_value = sanitize_text_field( $action_options['meta_value'] );
192
193 // Bail if empty meta key
194 if( empty( $meta_key ) ) {
195 return;
196 }
197
198 $post = get_post( $post_id );
199
200 // Bail if post doesn't exists
201 if( ! $post ) {
202 return;
203 }
204
205 $this->post_id = $post_id;
206
207 // Get the current meta value
208 $value = get_post_meta( $post_id, $meta_key, true );
209
210 // For increment and decrement, is required to turn values into a numeric value
211 if( in_array( $operation, array( 'increment', 'decrement' ) ) ) {
212
213 if( strpos( $meta_value, '.' ) !== false ) {
214 // Treat values as float
215 $value = (float) $value;
216 $meta_value = (float) $meta_value;
217 } else {
218 // Treat values as int
219 $value = (int) $value;
220 $meta_value = (int) $meta_value;
221 }
222
223 }
224
225 switch ( $operation ) {
226 case 'set':
227 // Override old meta value
228 $value = $meta_value;
229 break;
230 case 'insert':
231 if( is_array( $value ) ) {
232 // If value is an array, append the new value
233 $value[] = $meta_value;
234 } else {
235 // If not, concat the new value
236 $value .= $meta_value;
237 }
238 break;
239 case 'increment':
240 // Increase meta value
241 $value += $meta_value;
242 break;
243 case 'decrement':
244 // Decrease meta value
245 $value -= $meta_value;
246 break;
247 }
248
249 // Update the user meta value
250 update_post_meta( $post_id, $meta_key, $value );
251
252 }
253
254 /**
255 * Filter to assign a custom post ID to this action
256 *
257 * @since 1.0.0
258 *
259 * @param int $post_id The post ID, by default 0
260 * @param stdClass $action The action object
261 * @param int $user_id The user ID
262 * @param array $event Event information
263 * @param array $action_options The action's stored options (with tags already passed)
264 * @param stdClass $automation The action's automation object
265 *
266 * @return int
267 */
268 public function post_id( $post_id, $action, $user_id, $event, $action_options, $automation ) {
269
270 // Bail if action type don't match this action
271 if( $action->type !== $this->action ) {
272 return $post_id;
273 }
274
275 return $this->post_id;
276 }
277
278 /**
279 * Action custom log meta
280 *
281 * @since 1.0.0
282 *
283 * @param array $log_meta Log meta data
284 * @param stdClass $action The action object
285 * @param int $user_id The user ID
286 * @param array $action_options The action's stored options (with tags already passed)
287 * @param stdClass $automation The action's automation object
288 *
289 * @return array
290 */
291 public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
292
293 // Bail if action type don't match this action
294 if( $action->type !== $this->action ) {
295 return $log_meta;
296 }
297
298 // Store post fields
299 $post_fields = array(
300 'post_title',
301 'post_name',
302 'post_type',
303 'post_status',
304 'post_date',
305 'post_author',
306 'post_content',
307 'post_excerpt',
308 'post_parent',
309 'menu_order',
310 'post_password',
311 );
312
313 foreach( $post_fields as $post_field ) {
314 $log_meta[$post_field] = $action_options[$post_field];
315 }
316
317 // Store post meta
318 $log_meta['post_meta'] = $this->post_meta;
319
320 return $log_meta;
321 }
322
323 }
324
325 new AutomatorWP_WordPress_Post_Meta( 'wordpress' );
326 new AutomatorWP_WordPress_Post_Meta( 'posts' );