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/meta-box/includes/actions/update-post-field.php +231 -0 5.7.2 → 6.0.3 View file →
@@ -1,0 +1,231 @@
1 +<?php
2 +/**
3 + * Update Post Field
4 + *
5 + * @package AutomatorWP\Integrations\Meta_Box\Actions\Update_Post_Field
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_Meta_Box_Update_Post_Field extends AutomatorWP_Integration_Action {
13 +
14 + public $integration = 'meta_box';
15 + public $action = 'meta_box_update_post_field';
16 +
17 + /**
18 + * The action result
19 + *
20 + * @since 1.0.0
21 + *
22 + * @var array $result
23 + */
24 + public $result = array();
25 +
26 + /**
27 + * Register the trigger
28 + *
29 + * @since 1.0.0
30 + */
31 + public function register() {
32 +
33 + automatorwp_register_action( $this->action, array(
34 + 'integration' => $this->integration,
35 + 'label' => __( 'Update post field with a value', 'automatorwp' ),
36 + 'select_option' => __( 'Update <strong>post field</strong> with a value', 'automatorwp' ),
37 + /* translators: %1$s: Post. %2$s: Field name. %1$s: Field value.*/
38 + 'edit_label' => sprintf( __( 'Update %1$s %2$s with %3$s', 'automatorwp' ), '{post}', '{field_name}', '{field_value}' ),
39 + /* translators: %1$s: Post. %2$s: Field name. %1$s: Field value.*/
40 + 'log_label' => sprintf( __( 'Update %1$s %2$s with %3$s', 'automatorwp' ), '{post}', '{field_name}', '{field_value}' ),
41 + 'options' => array(
42 + 'post' => array(
43 + 'default' => __( 'any post', 'automatorwp' ),
44 + 'from' => 'post_id',
45 + 'fields' => array(
46 + 'post_id' => automatorwp_utilities_post_field( array(
47 + 'name' => __( 'Post:', 'automatorwp' ),
48 + 'post_type' => 'post',
49 + 'placeholder' => __( 'Select a post', 'automatorwp' ),
50 + 'option_none_label' => __( 'Select a post', 'automatorwp' ),
51 + 'option_custom' => true,
52 + 'option_custom_desc' => __( 'Post ID', 'automatorwp' ),
53 + ) ),
54 + 'post_id_custom' => automatorwp_utilities_custom_field( array(
55 + 'option_custom_desc' => __( 'Post ID', 'automatorwp' ),
56 + ) ),
57 + )),
58 +
59 + 'field_name' => automatorwp_utilities_ajax_selector_option( array(
60 + 'field' => 'field_name',
61 + 'option_default' => __( 'Select a field', 'automatorwp' ),
62 + 'name' => __( 'Field:', 'automatorwp' ),
63 + 'action_cb' => 'automatorwp_meta_box_get_post_fields',
64 + 'options_cb' => 'automatorwp_meta_box_options_cb_post_fields',
65 + 'default' => ''
66 + ) ),
67 +
68 + 'field_value' => array(
69 + 'from' => 'field_value',
70 + 'default' => __( 'value', 'automatorwp' ),
71 + 'fields' => array(
72 + 'field_value' => array(
73 + 'name' => __( 'Value:', 'automatorwp' ),
74 + 'type' => 'text',
75 + 'default' => ''
76 + ),
77 + )
78 + ),
79 + ),
80 + ) );
81 +
82 + }
83 +
84 + /**
85 + * Action execution function
86 + *
87 + * @since 1.0.0
88 + *
89 + * @param stdClass $action The action object
90 + * @param int $user_id The user ID
91 + * @param array $action_options The action's stored options (with tags already passed)
92 + * @param stdClass $automation The action's automation object
93 + */
94 + public function execute( $action, $user_id, $action_options, $automation ) {
95 +
96 + global $wpdb;
97 +
98 + // Shorthand
99 + $field_name = $action_options['field_name'];
100 + $field_value = $action_options['field_value'];
101 + $post_id = absint( $action_options['post_id'] );
102 +
103 + // Bail if field_name is empty
104 + if ( empty ( $field_name ) ){
105 + return;
106 + }
107 +
108 + $this->result = array();
109 +
110 + // Bail if no post
111 + if( empty( $action_options['post_id'] ) ) {
112 + $this->result[] = sprintf( __( 'Post not found by the ID %1$s.', 'automatorwp' ), $post_id );
113 + return;
114 + }
115 +
116 + // Update post meta
117 + update_post_meta( $post_id, $field_name, $field_value );
118 +
119 + $this->result[] = __( 'Post field updated successfully.', 'automatorwp' );
120 +
121 + }
122 +
123 + /**
124 + * Register required hooks
125 + *
126 + * @since 1.0.0
127 + */
128 + public function hooks() {
129 +
130 + // Log meta data
131 + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
132 +
133 + // Log fields
134 + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
135 +
136 + parent::hooks();
137 + }
138 +
139 + /**
140 + * Action custom log meta
141 + *
142 + * @since 1.0.0
143 + *
144 + * @param array $log_meta Log meta data
145 + * @param stdClass $action The action object
146 + * @param int $user_id The user ID
147 + * @param array $action_options The action's stored options (with tags already passed)
148 + * @param stdClass $automation The action's automation object
149 + *
150 + * @return array
151 + */
152 + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
153 +
154 + // Bail if action type don't match this action
155 + if( $action->type !== $this->action ) {
156 + return $log_meta;
157 + }
158 +
159 + // Store post ID
160 + $log_meta['post_id'] = $action_options['post_id'];
161 +
162 + // Store post field
163 + $log_meta['field_name'] = $action_options['field_name'];
164 +
165 + // Store post value field
166 + $log_meta['field_value'] = $action_options['field_value'];
167 +
168 + // Store result
169 + $log_meta['result'] = $this->result;
170 +
171 + return $log_meta;
172 + }
173 +
174 + /**
175 + * Action custom log fields
176 + *
177 + * @since 1.0.0
178 + *
179 + * @param array $log_fields The log fields
180 + * @param stdClass $log The log object
181 + * @param stdClass $object The trigger/action/automation object attached to the log
182 + *
183 + * @return array
184 + */
185 + public function log_fields( $log_fields, $log, $object ) {
186 +
187 + // Bail if log is not assigned to an action
188 + if( $log->type !== 'action' ) {
189 + return $log_fields;
190 + }
191 +
192 + // Bail if action type don't match this action
193 + if( $object->type !== $this->action ) {
194 + return $log_fields;
195 + }
196 +
197 + $log_fields['post_title'] = array(
198 + 'name' => __( 'Post Information', 'automatorwp' ),
199 + 'desc' => __( 'Information about the post updated.', 'automatorwp' ),
200 + 'type' => 'title',
201 + );
202 +
203 + $log_fields['post_id'] = array(
204 + 'name' => __( 'Post ID:', 'automatorwp' ),
205 + 'desc' => __( 'The post\'s ID updated.', 'automatorwp' ),
206 + 'type' => 'text',
207 + );
208 +
209 + $log_fields['field_name'] = array(
210 + 'name' => __( 'Post Field:', 'automatorwp' ),
211 + 'desc' => __( 'The updated field.', 'automatorwp' ),
212 + 'type' => 'text',
213 + );
214 +
215 + $log_fields['field_value'] = array(
216 + 'name' => __( 'Post Field Value:', 'automatorwp' ),
217 + 'desc' => __( 'The updated field value.', 'automatorwp' ),
218 + 'type' => 'text',
219 + );
220 +
221 + $log_fields['result'] = array(
222 + 'name' => __( 'Result:', 'automatorwp' ),
223 + 'type' => 'text',
224 + );
225 +
226 + return $log_fields;
227 + }
228 +
229 +}
230 +
231 +new AutomatorWP_Meta_Box_Update_Post_Field();