← All changes
|
integrations/advanced-custom-fields/includes/actions/update-post-field.php
+231
-0
5.7.9
→
6.0.3
View file →
| @@ -1,0 +1,231 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Update Post Field | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\Advance_Custom_Fields\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_Advanced_Custom_Fields_Update_Post_Field extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + public $integration = 'advanced_custom_fields'; | |
| 15 | + public $action = 'advanced_custom_fields_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: User. */ | |
| 38 | + 'edit_label' => sprintf( __( 'Update %1$s %2$s with %3$s', 'automatorwp' ), '{post}', '{field_name}', '{field_value}' ), | |
| 39 | + /* translators: %1$s: User. */ | |
| 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' => __( 'any 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' => array( | |
| 60 | + 'from' => 'field_name', | |
| 61 | + 'default' => __( 'field', 'automatorwp' ), | |
| 62 | + 'fields' => array( | |
| 63 | + 'field_name' => array( | |
| 64 | + 'name' => __( 'Field:', 'automatorwp' ), | |
| 65 | + 'type' => 'select', | |
| 66 | + 'classes' => 'automatorwp-selector', | |
| 67 | + 'options_cb' => 'automatorwp_advanced_custom_fields_options_cb_fields_posts', | |
| 68 | + 'default' => '' | |
| 69 | + ) | |
| 70 | + ) | |
| 71 | + ), | |
| 72 | + | |
| 73 | + 'field_value' => array( | |
| 74 | + 'from' => 'field_value', | |
| 75 | + 'default' => __( 'value', 'automatorwp' ), | |
| 76 | + 'fields' => array( | |
| 77 | + 'field_value' => array( | |
| 78 | + 'name' => __( 'Value:', 'automatorwp' ), | |
| 79 | + 'type' => 'text', | |
| 80 | + 'default' => '' | |
| 81 | + ), | |
| 82 | + ) | |
| 83 | + ), | |
| 84 | + ), | |
| 85 | + ) ); | |
| 86 | + | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Action execution function | |
| 91 | + * | |
| 92 | + * @since 1.0.0 | |
| 93 | + * | |
| 94 | + * @param stdClass $action The action object | |
| 95 | + * @param int $user_id The user ID | |
| 96 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 97 | + * @param stdClass $automation The action's automation object | |
| 98 | + */ | |
| 99 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 100 | + | |
| 101 | + global $wpdb; | |
| 102 | + | |
| 103 | + // Shorthand | |
| 104 | + $field_name = $action_options['field_name']; | |
| 105 | + $field_value = $action_options['field_value']; | |
| 106 | + $post_id = absint( $action_options['post_id'] ); | |
| 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_Advanced_Custom_Fields_Update_Post_Field(); | |