| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Meta Update |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\WordPress\Triggers\User_Meta_Update |
| 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_User_Meta_Update 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 . '_user_meta_update'; |
| 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 meta gets updated with a value', 'automatorwp' ), |
| 38 |
'select_option' => __( 'User <strong>meta gets updated</strong> with a value', 'automatorwp' ), |
| 39 |
/* translators: %1$s: Key. %2$s: Condition. %3$s: Value. %4$s: Number of times. */ |
| 40 |
'edit_label' => sprintf( __( 'User meta %1$s gets updated with a value %2$s %3$s %4$s time(s)', 'automatorwp' ), '{meta_key}', '{condition}', '{meta_value}', '{times}' ), |
| 41 |
/* translators: %1$s: Key. %2$s: Condition. %3$s: Value. */ |
| 42 |
'log_label' => sprintf( __( 'User meta %1$s gets updated with a value %2$s %3$s', 'automatorwp' ), '{meta_key}', '{condition}', '{meta_value}' ), |
| 43 |
'action' => array( |
| 44 |
'added_user_meta', |
| 45 |
'updated_user_meta' |
| 46 |
), |
| 47 |
'function' => array( $this, 'listener' ), |
| 48 |
'priority' => 10, |
| 49 |
'accepted_args' => 4, |
| 50 |
'options' => array( |
| 51 |
'condition' => automatorwp_utilities_condition_option(), |
| 52 |
'meta_key' => array( |
| 53 |
'from' => 'meta_key', |
| 54 |
/* translators: Refers to meta key */ |
| 55 |
'default' => __( 'with any key', 'automatorwp' ), |
| 56 |
'fields' => array( |
| 57 |
'meta_key' => array( |
| 58 |
'name' => __( 'Meta key:', 'automatorwp' ), |
| 59 |
'type' => 'text', |
| 60 |
'default' => '' |
| 61 |
), |
| 62 |
) |
| 63 |
), |
| 64 |
'meta_value' => array( |
| 65 |
'from' => 'meta_value', |
| 66 |
'default' => __( 'any value', 'automatorwp' ), |
| 67 |
'fields' => array( |
| 68 |
'meta_value' => array( |
| 69 |
'name' => __( 'Meta value:', 'automatorwp' ), |
| 70 |
'type' => 'text', |
| 71 |
'default' => '' |
| 72 |
), |
| 73 |
) |
| 74 |
), |
| 75 |
'times' => automatorwp_utilities_times_option(), |
| 76 |
), |
| 77 |
'tags' => array_merge( |
| 78 |
array( |
| 79 |
'updated_meta_key' => array( |
| 80 |
'label' => __( 'Updated meta key', 'automatorwp' ), |
| 81 |
'type' => 'text', |
| 82 |
'preview' => __( 'Key of the updated meta', 'automatorwp' ), |
| 83 |
), |
| 84 |
'updated_meta_value' => array( |
| 85 |
'label' => __( 'Updated meta value', 'automatorwp' ), |
| 86 |
'type' => 'text', |
| 87 |
'preview' => __( 'Value of the updated meta', 'automatorwp' ), |
| 88 |
), |
| 89 |
), |
| 90 |
automatorwp_utilities_times_tag() |
| 91 |
) |
| 92 |
) ); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Trigger listener |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* |
| 101 |
* @param int $meta_id ID of updated metadata entry. |
| 102 |
* @param int $object_id ID of the object metadata is for. |
| 103 |
* @param string $meta_key Metadata key. |
| 104 |
* @param mixed $meta_value Metadata value. Serialized if non-scalar. |
| 105 |
*/ |
| 106 |
public function listener( $meta_id, $object_id, $meta_key, $meta_value ) { |
| 107 |
|
| 108 |
automatorwp_trigger_event( array( |
| 109 |
'trigger' => $this->trigger, |
| 110 |
'user_id' => $object_id, |
| 111 |
'meta_key' => $meta_key, |
| 112 |
'meta_value' => $meta_value, |
| 113 |
) ); |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* User deserves check |
| 119 |
* |
| 120 |
* @since 1.0.0 |
| 121 |
* |
| 122 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 123 |
* @param stdClass $trigger The trigger object |
| 124 |
* @param int $user_id The user ID |
| 125 |
* @param array $event Event information |
| 126 |
* @param array $trigger_options The trigger's stored options |
| 127 |
* @param stdClass $automation The trigger's automation object |
| 128 |
* |
| 129 |
* @return bool True if user deserves trigger, false otherwise |
| 130 |
*/ |
| 131 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 132 |
|
| 133 |
// Don't deserve if post is not received |
| 134 |
if( ! isset( $event['meta_key'] ) && ! isset( $event['meta_value'] ) ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
// Don't deserve if key doesn't matches with the trigger option |
| 139 |
if( $trigger_options['meta_key'] !== '' && $trigger_options['meta_key'] !== $event['meta_key'] ) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
// Don't deserve if value doesn't matches with the trigger option |
| 144 |
if ( $trigger_options['meta_value'] !== '' && ! automatorwp_condition_matches( $event['meta_value'], $trigger_options['meta_value'], $trigger_options['condition'] ) ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
return $deserves_trigger; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Register the required hooks |
| 154 |
* |
| 155 |
* @since 1.0.0 |
| 156 |
*/ |
| 157 |
public function hooks() { |
| 158 |
|
| 159 |
// Tags replacement |
| 160 |
add_filter( 'automatorwp_get_trigger_tag_replacement', array( $this, 'tags_replacement' ), 10, 6 ); |
| 161 |
|
| 162 |
// Log meta data |
| 163 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 164 |
|
| 165 |
// Log fields |
| 166 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 167 |
|
| 168 |
parent::hooks(); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Trigger custom tags replacement |
| 173 |
* |
| 174 |
* @since 1.0.0 |
| 175 |
* |
| 176 |
* @param string $replacement The tag replacement |
| 177 |
* @param string $tag_name The tag name (without "{}") |
| 178 |
* @param stdClass $trigger The trigger object |
| 179 |
* @param int $user_id The user ID |
| 180 |
* @param string $content The content to parse |
| 181 |
* @param stdClass $log The last trigger log object |
| 182 |
* |
| 183 |
* @return string |
| 184 |
*/ |
| 185 |
function tags_replacement( $replacement, $tag_name, $trigger, $user_id, $content, $log ) { |
| 186 |
|
| 187 |
// Bail if action type don't match this action |
| 188 |
if( $trigger->type !== $this->trigger ) { |
| 189 |
return $replacement; |
| 190 |
} |
| 191 |
|
| 192 |
switch( $tag_name ) { |
| 193 |
case 'updated_meta_key': |
| 194 |
$replacement = automatorwp_get_log_meta( $log->id, 'updated_meta_key', true ); |
| 195 |
break; |
| 196 |
case 'updated_meta_value': |
| 197 |
$replacement = automatorwp_get_log_meta( $log->id, 'updated_meta_value', true ); |
| 198 |
break; |
| 199 |
} |
| 200 |
|
| 201 |
return $replacement; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Trigger custom log meta |
| 207 |
* |
| 208 |
* @since 1.0.0 |
| 209 |
* |
| 210 |
* @param array $log_meta Log meta data |
| 211 |
* @param stdClass $trigger The trigger object |
| 212 |
* @param int $user_id The user ID |
| 213 |
* @param array $event Event information |
| 214 |
* @param array $trigger_options The trigger's stored options |
| 215 |
* @param stdClass $automation The trigger's automation object |
| 216 |
* |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 220 |
|
| 221 |
// Bail if action type don't match this action |
| 222 |
if( $trigger->type !== $this->trigger ) { |
| 223 |
return $log_meta; |
| 224 |
} |
| 225 |
|
| 226 |
$log_meta['updated_meta_key'] = ( isset( $event['updated_meta_key'] ) ? $event['updated_meta_key'] : '' ); |
| 227 |
$log_meta['updated_meta_value'] = ( isset( $event['updated_meta_value'] ) ? $event['updated_meta_value'] : '' ); |
| 228 |
|
| 229 |
return $log_meta; |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Action custom log fields |
| 235 |
* |
| 236 |
* @since 1.0.0 |
| 237 |
* |
| 238 |
* @param array $log_fields The log fields |
| 239 |
* @param stdClass $log The log object |
| 240 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 241 |
* |
| 242 |
* @return array |
| 243 |
*/ |
| 244 |
public function log_fields( $log_fields, $log, $object ) { |
| 245 |
|
| 246 |
// Bail if log is not assigned to an trigger |
| 247 |
if( $log->type !== 'trigger' ) { |
| 248 |
return $log_fields; |
| 249 |
} |
| 250 |
|
| 251 |
// Bail if trigger type don't match this trigger |
| 252 |
if( $object->type !== $this->trigger ) { |
| 253 |
return $log_fields; |
| 254 |
} |
| 255 |
|
| 256 |
$log_fields['updated_meta_key'] = array( |
| 257 |
'name' => __( 'Updated meta key', 'automatorwp' ), |
| 258 |
'desc' => __( 'Key of the updated meta.', 'automatorwp' ), |
| 259 |
'type' => 'text', |
| 260 |
); |
| 261 |
|
| 262 |
$log_fields['updated_meta_value'] = array( |
| 263 |
'name' => __( 'Updated meta value', 'automatorwp' ), |
| 264 |
'desc' => __( 'Value of the updated meta.', 'automatorwp' ), |
| 265 |
'type' => 'text', |
| 266 |
); |
| 267 |
|
| 268 |
return $log_fields; |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
new AutomatorWP_WordPress_User_Meta_Update( 'wordpress' ); |
| 275 |
new AutomatorWP_WordPress_User_Meta_Update( 'users' ); |