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