| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post Field Updated |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Advance_Custom_Fields\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_Advanced_Custom_Fields_Post_Field_Updated extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'advanced_custom_fields'; |
| 15 |
public $trigger = 'advanced_custom_fields_post_field_updated'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_trigger( $this->trigger, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'User updates post field with a value', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User updates <strong>post field</strong> with a value', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Tag. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User updates post %1$s with %2$s %3$s time(s)', 'automatorwp' ), '{field_name}', '{field_value}', '{times}' ), |
| 30 |
/* translators: %1$s: Tag. */ |
| 31 |
'log_label' => sprintf( __( 'User updates post %1$s with %2$s', 'automatorwp' ), '{field_name}', '{field_value}' ), |
| 32 |
'action' => 'updated_post_meta', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 4, |
| 36 |
'options' => array( |
| 37 |
'field_name' => array( |
| 38 |
'from' => 'field_name', |
| 39 |
'default' => __( 'field', 'automatorwp' ), |
| 40 |
'fields' => array( |
| 41 |
'field_name' => array( |
| 42 |
'name' => __( 'Field:', 'automatorwp' ), |
| 43 |
'type' => 'select', |
| 44 |
'classes' => 'automatorwp-selector', |
| 45 |
'options_cb' => 'automatorwp_advanced_custom_fields_options_cb_fields_posts', |
| 46 |
'default' => 'any' |
| 47 |
) |
| 48 |
) |
| 49 |
), |
| 50 |
'field_value' => array( |
| 51 |
'from' => 'field_value', |
| 52 |
'default' => __( 'any value', 'automatorwp' ), |
| 53 |
'fields' => array( |
| 54 |
'field_value' => array( |
| 55 |
'name' => __( 'Value:', 'automatorwp' ), |
| 56 |
'type' => 'text', |
| 57 |
'default' => '' |
| 58 |
), |
| 59 |
) |
| 60 |
), |
| 61 |
'times' => automatorwp_utilities_times_option(), |
| 62 |
), |
| 63 |
'tags' => array_merge( |
| 64 |
automatorwp_advanced_custom_fields_get_tags(), |
| 65 |
automatorwp_utilities_post_tags(), |
| 66 |
automatorwp_utilities_times_tag() |
| 67 |
) |
| 68 |
) ); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Trigger listener |
| 74 |
* |
| 75 |
* @since 1.0.0 |
| 76 |
* |
| 77 |
* @param int $meta_id ID of updated metadata entry. |
| 78 |
* @param int $object_id ID of the object metadata is for. |
| 79 |
* @param string $meta_key Metadata key. |
| 80 |
* @param mixed $meta_value Metadata value. Serialized if non-scalar. |
| 81 |
*/ |
| 82 |
public function listener( $meta_id, $object_id, $meta_key, $meta_value ) { |
| 83 |
|
| 84 |
$post = get_post( $object_id ); |
| 85 |
|
| 86 |
automatorwp_trigger_event( array( |
| 87 |
'trigger' => $this->trigger, |
| 88 |
'user_id' => $post->post_author, |
| 89 |
'post_id' => $object_id, |
| 90 |
'meta_key' => $meta_key, |
| 91 |
'meta_value' => $meta_value, |
| 92 |
) ); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* User deserves check |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* |
| 101 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 102 |
* @param stdClass $trigger The trigger object |
| 103 |
* @param int $user_id The user ID |
| 104 |
* @param array $event Event information |
| 105 |
* @param array $trigger_options The trigger's stored options |
| 106 |
* @param stdClass $automation The trigger's automation object |
| 107 |
* |
| 108 |
* @return bool True if user deserves trigger, false otherwise |
| 109 |
*/ |
| 110 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 111 |
|
| 112 |
// Don't deserve if post is not received |
| 113 |
if( ! isset( $event['meta_key'] ) ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
$fields_allowed = automatorwp_advanced_custom_fields_options_cb_fields_posts(); |
| 118 |
|
| 119 |
// Don't deserve if meta_key is not allowed |
| 120 |
if ( ! array_key_exists( $event['meta_key'], $fields_allowed ) ) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
$post = get_post( absint( $event['post_id'] ) ); |
| 125 |
|
| 126 |
// Don't deserve if post doesn't exists |
| 127 |
if( ! $post ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
// Don't deserve if post doesn't match with the trigger option |
| 132 |
if( $trigger_options['field_name'] !== 'any' && $trigger_options['field_name'] !== $event['meta_key'] ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
// Don't deserve if value doesn't matches with the trigger option |
| 137 |
if( $trigger_options['field_value'] !== '' && $trigger_options['field_value'] !== $event['meta_value'] ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
return $deserves_trigger; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Register the required hooks |
| 147 |
* |
| 148 |
* @since 1.0.0 |
| 149 |
*/ |
| 150 |
public function hooks() { |
| 151 |
|
| 152 |
// Log meta data |
| 153 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 154 |
|
| 155 |
// Log fields |
| 156 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 157 |
|
| 158 |
parent::hooks(); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Trigger custom log meta |
| 163 |
* |
| 164 |
* @since 1.0.0 |
| 165 |
* |
| 166 |
* @param array $log_meta Log meta data |
| 167 |
* @param stdClass $trigger The trigger object |
| 168 |
* @param int $user_id The user ID |
| 169 |
* @param array $event Event information |
| 170 |
* @param array $trigger_options The trigger's stored options |
| 171 |
* @param stdClass $automation The trigger's automation object |
| 172 |
* |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 176 |
|
| 177 |
// Bail if action type don't match this action |
| 178 |
if( $trigger->type !== $this->trigger ) { |
| 179 |
return $log_meta; |
| 180 |
} |
| 181 |
|
| 182 |
$log_meta['updated_meta_key'] = ( isset( $event['meta_key'] ) ? $event['meta_key'] : '' ); |
| 183 |
$log_meta['updated_meta_value'] = ( isset( $event['meta_value'] ) ? $event['meta_value'] : '' ); |
| 184 |
|
| 185 |
return $log_meta; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Action custom log fields |
| 191 |
* |
| 192 |
* @since 1.0.0 |
| 193 |
* |
| 194 |
* @param array $log_fields The log fields |
| 195 |
* @param stdClass $log The log object |
| 196 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 197 |
* |
| 198 |
* @return array |
| 199 |
*/ |
| 200 |
public function log_fields( $log_fields, $log, $object ) { |
| 201 |
|
| 202 |
// Bail if log is not assigned to an trigger |
| 203 |
if( $log->type !== 'trigger' ) { |
| 204 |
return $log_fields; |
| 205 |
} |
| 206 |
|
| 207 |
// Bail if trigger type don't match this trigger |
| 208 |
if( $object->type !== $this->trigger ) { |
| 209 |
return $log_fields; |
| 210 |
} |
| 211 |
|
| 212 |
$log_fields['updated_meta_key'] = array( |
| 213 |
'name' => __( 'Updated field', 'automatorwp' ), |
| 214 |
'desc' => __( 'Key of the updated field.', 'automatorwp' ), |
| 215 |
'type' => 'text', |
| 216 |
); |
| 217 |
|
| 218 |
$log_fields['updated_meta_value'] = array( |
| 219 |
'name' => __( 'Updated value', 'automatorwp' ), |
| 220 |
'desc' => __( 'Value of the updated field.', 'automatorwp' ), |
| 221 |
'type' => 'text', |
| 222 |
); |
| 223 |
|
| 224 |
return $log_fields; |
| 225 |
|
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
new AutomatorWP_Advanced_Custom_Fields_Post_Field_Updated(); |