| 1 |
<?php |
| 2 |
/** |
| 3 |
* Triggers |
| 4 |
* |
| 5 |
* @package AutomatorWP\Triggers |
| 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 |
/** |
| 13 |
* Registers a new trigger |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @param string $trigger The trigger key |
| 18 |
* @param array $args The trigger arguments |
| 19 |
*/ |
| 20 |
function automatorwp_register_trigger( $trigger, $args ) { |
| 21 |
|
| 22 |
$args = wp_parse_args( $args, array( |
| 23 |
'integration' => '', |
| 24 |
'anonymous' => false, |
| 25 |
'label' => '', |
| 26 |
'select_option' => '', |
| 27 |
'show_in_selector' => true, |
| 28 |
'edit_label' => '', |
| 29 |
'log_label' => '', |
| 30 |
'action' => '', |
| 31 |
'filter' => '', |
| 32 |
'function' => '', |
| 33 |
'priority' => 10, |
| 34 |
'accepted_args' => 1, |
| 35 |
'options' => array(), |
| 36 |
'tags' => array(), |
| 37 |
) ); |
| 38 |
|
| 39 |
/** |
| 40 |
* Filter to extend registered trigger arguments |
| 41 |
* |
| 42 |
* @since 1.0.0 |
| 43 |
* |
| 44 |
* @param array $args The trigger arguments |
| 45 |
* @param string $trigger The trigger key |
| 46 |
* |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
$args = apply_filters( 'automatorwp_register_trigger_args', $args, $trigger ); |
| 50 |
|
| 51 |
// Sanitize options setup |
| 52 |
foreach( $args['options'] as $option => $option_args ) { |
| 53 |
|
| 54 |
if( in_array( $option, array( 'action', 'nonce', 'id', 'item_type', 'option_name' ) ) ) { |
| 55 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Trigger "%s" has the option key "%s" that is not allowed', 'automatorwp' ), $trigger, $option ), null ); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
$log_duplications = apply_filters( 'automatorwp_log_duplications', false ); |
| 62 |
|
| 63 |
if( $log_duplications && isset( AutomatorWP()->triggers[$trigger] ) ) { |
| 64 |
error_log( sprintf( __( 'Possible trigger duplication with the key "%s"', 'automatorwp' ), $trigger ) ); |
| 65 |
} |
| 66 |
|
| 67 |
AutomatorWP()->triggers[$trigger] = $args; |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get registered triggers |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
function automatorwp_get_triggers() { |
| 79 |
|
| 80 |
return AutomatorWP()->triggers; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get a trigger |
| 86 |
* |
| 87 |
* @since 1.0.0 |
| 88 |
* |
| 89 |
* @param string $trigger |
| 90 |
* |
| 91 |
* @return array|false |
| 92 |
*/ |
| 93 |
function automatorwp_get_trigger( $trigger ) { |
| 94 |
|
| 95 |
$trigger_args = ( isset( AutomatorWP()->triggers[$trigger] ) ? AutomatorWP()->triggers[$trigger] : false ); |
| 96 |
|
| 97 |
/** |
| 98 |
* Available filter to override the trigger args |
| 99 |
* |
| 100 |
* @since 1.0.0 |
| 101 |
* |
| 102 |
* @param array|false $trigger_args |
| 103 |
* @param string $trigger |
| 104 |
* |
| 105 |
* @return array|false |
| 106 |
*/ |
| 107 |
return apply_filters( 'automatorwp_get_trigger', $trigger_args, $trigger ); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get an integration triggers |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
* |
| 116 |
* @param string $integration The integration key |
| 117 |
* @param array $filters Filters to filter triggers by args |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
function automatorwp_get_integration_triggers( $integration, $filters = array() ) { |
| 122 |
|
| 123 |
$triggers = array(); |
| 124 |
|
| 125 |
foreach( AutomatorWP()->triggers as $trigger => $args ) { |
| 126 |
|
| 127 |
if( $args['integration'] !== $integration ) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
// If filters defined, apply them |
| 132 |
if( is_array( $filters ) && ! empty( $filters ) ) { |
| 133 |
|
| 134 |
$pass_filters = true; |
| 135 |
|
| 136 |
foreach( $filters as $filter_key => $filter_value ) { |
| 137 |
|
| 138 |
// Check if argument exists |
| 139 |
if( ! isset( $args[$filter_key] ) ) { |
| 140 |
$pass_filters = false; |
| 141 |
} |
| 142 |
|
| 143 |
// Check if argument value matches |
| 144 |
if( $args[$filter_key] !== $filter_value ) { |
| 145 |
$pass_filters = false; |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
// Skip this item if filters not passed |
| 151 |
if( ! $pass_filters ) { |
| 152 |
continue; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
$triggers[$trigger] = $args; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Available filter to extend integration triggers |
| 162 |
* |
| 163 |
* @since 1.0.0 |
| 164 |
* |
| 165 |
* @param array $triggers |
| 166 |
* @param string $integration |
| 167 |
* |
| 168 |
* @return array |
| 169 |
*/ |
| 170 |
return apply_filters( 'automatorwp_get_integration_triggers', $triggers, $integration ); |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get the trigger object data |
| 176 |
* |
| 177 |
* @param int $trigger_id The trigger ID |
| 178 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
| 179 |
* a object, an associative array, or a numeric array, respectively. Default OBJECT. |
| 180 |
* @return array|stdClass|null |
| 181 |
*/ |
| 182 |
function automatorwp_get_trigger_object( $trigger_id, $output = OBJECT ) { |
| 183 |
|
| 184 |
ct_setup_table( 'automatorwp_triggers' ); |
| 185 |
|
| 186 |
$trigger = ct_get_object( $trigger_id ); |
| 187 |
|
| 188 |
ct_reset_setup_table(); |
| 189 |
|
| 190 |
return $trigger; |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get the trigger object data |
| 196 |
* |
| 197 |
* @param int $trigger_id The trigger ID |
| 198 |
* @param string $meta_key Optional. The meta key to retrieve. By default, returns |
| 199 |
* data for all keys. Default empty. |
| 200 |
* @param bool $single Optional. Whether to return a single value. Default false. |
| 201 |
* |
| 202 |
* @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
| 203 |
*/ |
| 204 |
function automatorwp_get_trigger_meta( $trigger_id, $meta_key = '', $single = false ) { |
| 205 |
|
| 206 |
ct_setup_table( 'automatorwp_triggers' ); |
| 207 |
|
| 208 |
$meta_value = ct_get_object_meta( $trigger_id, $meta_key, $single ); |
| 209 |
|
| 210 |
ct_reset_setup_table(); |
| 211 |
|
| 212 |
return $meta_value; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Get the trigger's automation object |
| 218 |
* |
| 219 |
* @since 1.0.0 |
| 220 |
* |
| 221 |
* @param int $trigger_id The trigger ID |
| 222 |
* |
| 223 |
* @return stdClass|false The automation object or false |
| 224 |
*/ |
| 225 |
function automatorwp_get_trigger_automation( $trigger_id ) { |
| 226 |
|
| 227 |
$trigger = automatorwp_get_trigger_object( $trigger_id ); |
| 228 |
|
| 229 |
if( ! $trigger ) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
$automation = automatorwp_get_automation_object( $trigger->automation_id ); |
| 234 |
|
| 235 |
return $automation; |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get the trigger's stored options |
| 241 |
* |
| 242 |
* @since 1.0.0 |
| 243 |
* |
| 244 |
* @param int $trigger_id The trigger ID |
| 245 |
* @param bool $single_level The level of options array, if is set to try, only option fields will be returned |
| 246 |
* |
| 247 |
* @return array The trigger options. |
| 248 |
* Stored options format wit $single_level=true: |
| 249 |
* array( |
| 250 |
* 'field_id' => 'value' |
| 251 |
* ) |
| 252 |
* Stored options format wit $single_level=false: |
| 253 |
* array( |
| 254 |
* 'option' => array( |
| 255 |
* 'field_id' => 'value' |
| 256 |
* ) |
| 257 |
* ) |
| 258 |
*/ |
| 259 |
function automatorwp_get_trigger_stored_options( $trigger_id, $single_level = true ) { |
| 260 |
|
| 261 |
$object = automatorwp_get_trigger_object( $trigger_id ); |
| 262 |
|
| 263 |
if( ! $object ) { |
| 264 |
return array(); |
| 265 |
} |
| 266 |
|
| 267 |
$trigger = automatorwp_get_trigger( $object->type ); |
| 268 |
|
| 269 |
if( ! $trigger ) { |
| 270 |
return array(); |
| 271 |
} |
| 272 |
|
| 273 |
ct_setup_table( 'automatorwp_triggers' ); |
| 274 |
|
| 275 |
$options = array(); |
| 276 |
|
| 277 |
foreach( $trigger['options'] as $option => $option_args ) { |
| 278 |
|
| 279 |
if( ! isset( $option_args['fields'] ) ) { |
| 280 |
continue; |
| 281 |
} |
| 282 |
|
| 283 |
if( ! $single_level ) { |
| 284 |
$options[$option] = array(); |
| 285 |
} |
| 286 |
|
| 287 |
foreach( $option_args['fields'] as $field_id => $field ) { |
| 288 |
|
| 289 |
$value = ct_get_object_meta( $object->id, $field_id, true ); |
| 290 |
|
| 291 |
if( ( $value === '' || $value === null ) && isset( $field['default'] ) ) { |
| 292 |
$value = $field['default']; |
| 293 |
} |
| 294 |
|
| 295 |
if( $single_level ) { |
| 296 |
$options[$field_id] = $value; |
| 297 |
} else { |
| 298 |
$options[$option][$field_id] = $value; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
ct_reset_setup_table(); |
| 307 |
|
| 308 |
return $options; |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Check if trigger is in use |
| 314 |
* |
| 315 |
* @since 1.3.0 |
| 316 |
* |
| 317 |
* @param string $trigger |
| 318 |
* |
| 319 |
* @return bool |
| 320 |
*/ |
| 321 |
function automatorwp_is_trigger_in_use( $trigger ) { |
| 322 |
|
| 323 |
$triggers_in_user = automatorwp_get_triggers_in_use(); |
| 324 |
|
| 325 |
// Check if this trigger is not in use |
| 326 |
return (bool) in_array( $trigger, $triggers_in_user ); |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Get all triggers in use |
| 332 |
* |
| 333 |
* @since 1.0.0 |
| 334 |
* |
| 335 |
* @return array |
| 336 |
*/ |
| 337 |
function automatorwp_get_triggers_in_use() { |
| 338 |
|
| 339 |
global $wpdb; |
| 340 |
|
| 341 |
$ct_table = ct_setup_table( 'automatorwp_triggers' ); |
| 342 |
|
| 343 |
// Check if table exists, just to avoid issues on first install |
| 344 |
if( ! $ct_table->db->exists() ) { |
| 345 |
ct_reset_setup_table(); |
| 346 |
return array(); |
| 347 |
} |
| 348 |
|
| 349 |
$cache = automatorwp_get_cache( 'triggers_in_use', false, false ); |
| 350 |
|
| 351 |
// If result already cached, return it |
| 352 |
if( is_array( $cache ) ) { |
| 353 |
ct_reset_setup_table(); |
| 354 |
return $cache; |
| 355 |
} |
| 356 |
|
| 357 |
$cache = get_transient( 'automatorwp_triggers_in_use' ); |
| 358 |
|
| 359 |
// If result is in a transient, return it |
| 360 |
if( is_array( $cache ) && count( $cache ) ) { |
| 361 |
ct_reset_setup_table(); |
| 362 |
return $cache; |
| 363 |
} |
| 364 |
|
| 365 |
$triggers_in_use = array(); |
| 366 |
$results = $wpdb->get_results( "SELECT t.type FROM {$ct_table->db->table_name} AS t GROUP BY t.type" ); |
| 367 |
|
| 368 |
ct_reset_setup_table(); |
| 369 |
|
| 370 |
if( is_array( $results ) && count( $results ) ) { |
| 371 |
$triggers_in_use = wp_list_pluck( $results, 'type' ); |
| 372 |
} |
| 373 |
|
| 374 |
// Cache function result |
| 375 |
automatorwp_set_cache( 'triggers_in_use', $triggers_in_use ); |
| 376 |
|
| 377 |
// Store the result for a day |
| 378 |
set_transient( 'automatorwp_triggers_in_use', $triggers_in_use, HOUR_IN_SECONDS * 24 ); |
| 379 |
|
| 380 |
return $triggers_in_use; |
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Get a trigger required times |
| 386 |
* |
| 387 |
* @since 1.0.0 |
| 388 |
* |
| 389 |
* @param int $trigger_id The trigger ID |
| 390 |
* |
| 391 |
* @return int |
| 392 |
*/ |
| 393 |
function automatorwp_get_trigger_required_times( $trigger_id ) { |
| 394 |
|
| 395 |
$trigger_id = absint( $trigger_id ); |
| 396 |
|
| 397 |
// Check the trigger ID |
| 398 |
if( $trigger_id === 0 ) { |
| 399 |
return 0; |
| 400 |
} |
| 401 |
|
| 402 |
$times = absint( automatorwp_get_trigger_meta( $trigger_id, 'times', true ) ); |
| 403 |
|
| 404 |
// Ensure to always require triggers at least 1 time |
| 405 |
if( $times === 0 ) { |
| 406 |
$times = 1; |
| 407 |
} |
| 408 |
|
| 409 |
return $times; |
| 410 |
|
| 411 |
} |