| @@ -1,0 +1,410 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Filters | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Filters | |
| 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 | +/** | |
| 13 | + * Registers a new filter | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @param string $filter The filter key | |
| 18 | + * @param array $args The filter arguments | |
| 19 | + */ | |
| 20 | +function automatorwp_register_filter( $filter, $args ) { | |
| 21 | + | |
| 22 | + $args = wp_parse_args( $args, array( | |
| 23 | + 'integration' => '', | |
| 24 | + 'label' => '', | |
| 25 | + 'select_option' => '', | |
| 26 | + 'show_in_selector' => true, | |
| 27 | + 'edit_label' => '', | |
| 28 | + 'log_label' => '', | |
| 29 | + 'options' => array(), | |
| 30 | + ) ); | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Filter to extend registered filter arguments | |
| 34 | + * | |
| 35 | + * @since 1.0.0 | |
| 36 | + * | |
| 37 | + * @param array $args The filter arguments | |
| 38 | + * @param string $filter The filter key | |
| 39 | + * | |
| 40 | + * @return array | |
| 41 | + */ | |
| 42 | + $args = apply_filters( 'automatorwp_register_filter_args', $args, $filter ); | |
| 43 | + | |
| 44 | + // Sanitize options setup | |
| 45 | + foreach( $args['options'] as $option => $option_args ) { | |
| 46 | + | |
| 47 | + if( in_array( $option, array( 'trigger', 'action', 'filter', 'operator', 'nonce', 'id', 'item_type', 'option_name' ) ) ) { | |
| 48 | + _doing_it_wrong( __FUNCTION__, sprintf( __( 'Filter "%s" has the option key "%s" that is not allowed', 'automatorwp' ), $filter, $option ), null ); | |
| 49 | + return; | |
| 50 | + } | |
| 51 | + | |
| 52 | + } | |
| 53 | + | |
| 54 | + $log_duplications = apply_filters( 'automatorwp_log_duplications', false ); | |
| 55 | + | |
| 56 | + if( $log_duplications && isset( AutomatorWP()->filters[$filter] ) ) { | |
| 57 | + error_log( sprintf( __( 'Possible filter duplication with the key "%s"', 'automatorwp' ), $filter ) ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + AutomatorWP()->filters[$filter] = $args; | |
| 61 | + | |
| 62 | +} | |
| 63 | + | |
| 64 | +/** | |
| 65 | + * Get a filter | |
| 66 | + * | |
| 67 | + * @since 1.0.0 | |
| 68 | + * | |
| 69 | + * @param string $filter | |
| 70 | + * | |
| 71 | + * @return array|false | |
| 72 | + */ | |
| 73 | +function automatorwp_get_filter( $filter ) { | |
| 74 | + | |
| 75 | + $filter_args = ( isset( AutomatorWP()->filters[$filter] ) ? AutomatorWP()->filters[$filter] : false ); | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Available filter to override the filter args | |
| 79 | + * | |
| 80 | + * @since 1.0.0 | |
| 81 | + * | |
| 82 | + * @param array|false $filter_args | |
| 83 | + * @param string $filter | |
| 84 | + * | |
| 85 | + * @return array|false | |
| 86 | + */ | |
| 87 | + return apply_filters( 'automatorwp_get_filter', $filter_args, $filter ); | |
| 88 | + | |
| 89 | +} | |
| 90 | + | |
| 91 | +/** | |
| 92 | + * Get a trigger filters | |
| 93 | + * | |
| 94 | + * @since 1.0.0 | |
| 95 | + * | |
| 96 | + * @param stdClass $trigger | |
| 97 | + * | |
| 98 | + * @return array | |
| 99 | + */ | |
| 100 | +function automatorwp_get_trigger_filters( $trigger ) { | |
| 101 | + | |
| 102 | + $triggers = automatorwp_get_automation_triggers( $trigger->automation_id ); | |
| 103 | + $filters = array(); | |
| 104 | + $found = false; | |
| 105 | + | |
| 106 | + foreach ( $triggers as $item ) { | |
| 107 | + | |
| 108 | + // Skip if we found the trigger | |
| 109 | + if( absint( $item->id ) === absint( $trigger->id ) ) { | |
| 110 | + $found = true; | |
| 111 | + continue; | |
| 112 | + } | |
| 113 | + | |
| 114 | + if( $found === false ) { | |
| 115 | + continue; | |
| 116 | + } | |
| 117 | + | |
| 118 | + // Skip if is not a filter | |
| 119 | + if( $item->type !== 'filter' ) { | |
| 120 | + $found = false; | |
| 121 | + continue; | |
| 122 | + } | |
| 123 | + | |
| 124 | + // Add to the filters list | |
| 125 | + $filters[] = $item; | |
| 126 | + | |
| 127 | + } | |
| 128 | + | |
| 129 | + return $filters; | |
| 130 | + | |
| 131 | +} | |
| 132 | + | |
| 133 | +/** | |
| 134 | + * Get an action filters | |
| 135 | + * | |
| 136 | + * @since 1.0.0 | |
| 137 | + * | |
| 138 | + * @param stdClass $action | |
| 139 | + * | |
| 140 | + * @return array | |
| 141 | + */ | |
| 142 | +function automatorwp_get_action_filters( $action ) { | |
| 143 | + | |
| 144 | + $actions = automatorwp_get_automation_actions( $action->automation_id ); | |
| 145 | + $filters = array(); | |
| 146 | + $found = false; | |
| 147 | + | |
| 148 | + foreach ( $actions as $item ) { | |
| 149 | + | |
| 150 | + // Skip if we found the action | |
| 151 | + if( absint( $item->id ) === absint( $action->id ) ) { | |
| 152 | + $found = true; | |
| 153 | + continue; | |
| 154 | + } | |
| 155 | + | |
| 156 | + if( $found === false ) { | |
| 157 | + continue; | |
| 158 | + } | |
| 159 | + | |
| 160 | + // Skip if is not a filter | |
| 161 | + if( $item->type !== 'filter' ) { | |
| 162 | + $found = false; | |
| 163 | + continue; | |
| 164 | + } | |
| 165 | + | |
| 166 | + // Add to the filters list | |
| 167 | + $filters[] = $item; | |
| 168 | + | |
| 169 | + } | |
| 170 | + | |
| 171 | + return $filters; | |
| 172 | + | |
| 173 | +} | |
| 174 | + | |
| 175 | +/** | |
| 176 | + * Get the filter's stored options | |
| 177 | + * | |
| 178 | + * @since 1.0.0 | |
| 179 | + * | |
| 180 | + * @param int $filter_id The filter ID | |
| 181 | + * @param string $item_type The filter item type (trigger/action) | |
| 182 | + * @param bool $single_level The level of options array, if is set to try, only option fields will be returned | |
| 183 | + * | |
| 184 | + * @return array The trigger options. | |
| 185 | + * Stored options format wit $single_level=true: | |
| 186 | + * array( | |
| 187 | + * 'field_id' => 'value' | |
| 188 | + * ) | |
| 189 | + * Stored options format wit $single_level=false: | |
| 190 | + * array( | |
| 191 | + * 'option' => array( | |
| 192 | + * 'field_id' => 'value' | |
| 193 | + * ) | |
| 194 | + * ) | |
| 195 | + */ | |
| 196 | +function automatorwp_get_filter_stored_options( $filter_id, $item_type, $single_level = true ) { | |
| 197 | + | |
| 198 | + ct_setup_table( "automatorwp_{$item_type}s" ); | |
| 199 | + $object = ct_get_object( $filter_id ); | |
| 200 | + ct_reset_setup_table(); | |
| 201 | + | |
| 202 | + if( ! $object ) { | |
| 203 | + return array(); | |
| 204 | + } | |
| 205 | + | |
| 206 | + $filter = automatorwp_automation_item_type_args( $object, $item_type ); | |
| 207 | + | |
| 208 | + if( ! $filter ) { | |
| 209 | + return array(); | |
| 210 | + } | |
| 211 | + | |
| 212 | + ct_setup_table( "automatorwp_{$item_type}s" ); | |
| 213 | + | |
| 214 | + $options = array(); | |
| 215 | + | |
| 216 | + foreach( $filter['options'] as $option => $option_args ) { | |
| 217 | + | |
| 218 | + if( ! isset( $option_args['fields'] ) ) { | |
| 219 | + continue; | |
| 220 | + } | |
| 221 | + | |
| 222 | + if( ! $single_level ) { | |
| 223 | + $options[$option] = array(); | |
| 224 | + } | |
| 225 | + | |
| 226 | + foreach( $option_args['fields'] as $field_id => $field ) { | |
| 227 | + | |
| 228 | + $value = ct_get_object_meta( $object->id, $field_id, true ); | |
| 229 | + | |
| 230 | + if( ( $value === '' || $value === null ) && isset( $field['default'] ) ) { | |
| 231 | + $value = $field['default']; | |
| 232 | + } | |
| 233 | + | |
| 234 | + /** | |
| 235 | + * Filters the option value for replacement on labels | |
| 236 | + * | |
| 237 | + * @since 1.0.0 | |
| 238 | + * | |
| 239 | + * @param string $value The option value | |
| 240 | + * @param stdClass $object The trigger/action object | |
| 241 | + * @param string $item_type The item type (trigger|action) | |
| 242 | + * @param string $option The option name | |
| 243 | + * @param string $context The context this function is executed | |
| 244 | + * | |
| 245 | + * @return string | |
| 246 | + */ | |
| 247 | + $value = apply_filters( 'automatorwp_get_automation_item_option_replacement', $value, $object, $item_type, $option, 'view' ); | |
| 248 | + | |
| 249 | + if( $single_level ) { | |
| 250 | + $options[$field_id] = $value; | |
| 251 | + } else { | |
| 252 | + $options[$option][$field_id] = $value; | |
| 253 | + } | |
| 254 | + | |
| 255 | + | |
| 256 | + } | |
| 257 | + | |
| 258 | + } | |
| 259 | + | |
| 260 | + ct_reset_setup_table(); | |
| 261 | + | |
| 262 | + return $options; | |
| 263 | + | |
| 264 | +} | |
| 265 | + | |
| 266 | +/** | |
| 267 | + * Filters the taxonomy option value for replacement on labels | |
| 268 | + * | |
| 269 | + * @since 1.0.0 | |
| 270 | + * | |
| 271 | + * @param string $value The option value | |
| 272 | + * @param stdClass $object The trigger/action object | |
| 273 | + * @param string $item_type The item type (trigger|action) | |
| 274 | + * @param string $option The option name | |
| 275 | + * @param string $context The context this function is executed | |
| 276 | + * | |
| 277 | + * @return string | |
| 278 | + */ | |
| 279 | +function automatorwp_dynamic_taxonomy_option_replacement( $value, $object, $item_type, $option, $context ) { | |
| 280 | + | |
| 281 | + // Check type args | |
| 282 | + $type_args = automatorwp_automation_item_type_args( $object, $item_type ); | |
| 283 | + | |
| 284 | + if( ! $type_args ) { | |
| 285 | + return $value; | |
| 286 | + } | |
| 287 | + | |
| 288 | + // Bail if this type hasn't any option | |
| 289 | + if( ! isset( $type_args['options'][$option] ) ) { | |
| 290 | + return $value; | |
| 291 | + } | |
| 292 | + | |
| 293 | + $option_args = $type_args['options'][$option]; | |
| 294 | + | |
| 295 | + $field_id = ( isset( $option_args['from'] ) ? $option_args['from'] : '' ); | |
| 296 | + | |
| 297 | + // Check if field id is term | |
| 298 | + if( $field_id !== 'term' ) { | |
| 299 | + return $value; | |
| 300 | + } | |
| 301 | + | |
| 302 | + if( ! isset( $option_args['fields'] ) ) { | |
| 303 | + return $value; | |
| 304 | + } | |
| 305 | + | |
| 306 | + // Check if taxonomy field exists | |
| 307 | + if( ! isset( $option_args['fields']['taxonomy'] ) ) { | |
| 308 | + return $value; | |
| 309 | + } | |
| 310 | + | |
| 311 | + ct_setup_table( "automatorwp_{$item_type}s" ); | |
| 312 | + | |
| 313 | + // Get the custom taxonomy | |
| 314 | + $taxonomy = ct_get_object_meta( $object->id, 'taxonomy', true ); | |
| 315 | + | |
| 316 | + if( $taxonomy !== '' && $taxonomy !== 'any' ) { | |
| 317 | + | |
| 318 | + $term_id = ct_get_object_meta( $object->id, 'term', true ); | |
| 319 | + | |
| 320 | + // Get the term using the taxonomy | |
| 321 | + $term = get_term( $term_id, $taxonomy ); | |
| 322 | + | |
| 323 | + if( $term ) { | |
| 324 | + $value = $term->name; | |
| 325 | + } | |
| 326 | + | |
| 327 | + } else { | |
| 328 | + $value = __( 'any taxonomy', 'automatorwp' ); | |
| 329 | + } | |
| 330 | + | |
| 331 | + ct_reset_setup_table(); | |
| 332 | + | |
| 333 | + return $value; | |
| 334 | +} | |
| 335 | +add_filter( 'automatorwp_get_automation_item_option_replacement', 'automatorwp_dynamic_taxonomy_option_replacement', 10, 5 ); | |
| 336 | + | |
| 337 | +/** | |
| 338 | + * Filters the option custom for replacement on labels | |
| 339 | + * | |
| 340 | + * @since 1.0.0 | |
| 341 | + * | |
| 342 | + * @param string $value The option value | |
| 343 | + * @param stdClass $object The trigger/action object | |
| 344 | + * @param string $item_type The item type (trigger|action) | |
| 345 | + * @param string $option The option name | |
| 346 | + * @param string $context The context this function is executed | |
| 347 | + * | |
| 348 | + * @return string | |
| 349 | + */ | |
| 350 | +function automatorwp_option_custom_replacement( $value, $object, $item_type, $option, $context ) { | |
| 351 | + | |
| 352 | + // Check type args | |
| 353 | + $type_args = automatorwp_automation_item_type_args( $object, $item_type ); | |
| 354 | + | |
| 355 | + if( ! $type_args ) { | |
| 356 | + return $value; | |
| 357 | + } | |
| 358 | + | |
| 359 | + // Bail if this type hasn't any option | |
| 360 | + if( ! isset( $type_args['options'][$option] ) ) { | |
| 361 | + return $value; | |
| 362 | + } | |
| 363 | + | |
| 364 | + $option_args = $type_args['options'][$option]; | |
| 365 | + $field_id = ( isset( $option_args['from'] ) ? $option_args['from'] : '' ); | |
| 366 | + | |
| 367 | + // Check if field exists | |
| 368 | + if( ! isset( $option_args['fields'] ) ) { | |
| 369 | + return $value; | |
| 370 | + } | |
| 371 | + | |
| 372 | + if( ! isset( $option_args['fields'][$field_id] ) ) { | |
| 373 | + return $value; | |
| 374 | + } | |
| 375 | + | |
| 376 | + $field = $option_args['fields'][$field_id]; | |
| 377 | + | |
| 378 | + // Bail if option_custom not enabled on this field | |
| 379 | + if( ! isset( $field['option_custom'] ) ) { | |
| 380 | + return $value; | |
| 381 | + } | |
| 382 | + | |
| 383 | + if( $field['option_custom'] !== true ) { | |
| 384 | + return $value; | |
| 385 | + } | |
| 386 | + | |
| 387 | + // Check if custom field exists | |
| 388 | + if( ! isset( $option_args['fields'][$field_id . '_custom'] ) ) { | |
| 389 | + return $value; | |
| 390 | + } | |
| 391 | + | |
| 392 | + // Get the real value | |
| 393 | + ct_setup_table( "automatorwp_{$item_type}s" ); | |
| 394 | + $field_value = ct_get_object_meta( $object->id, $field_id, true ); | |
| 395 | + ct_reset_setup_table(); | |
| 396 | + | |
| 397 | + // Bail if field is not setup to use the custom value | |
| 398 | + if( $field_value !== $field['option_custom_value'] ) { | |
| 399 | + return $value; | |
| 400 | + } | |
| 401 | + | |
| 402 | + // Get the custom value instead | |
| 403 | + ct_setup_table( "automatorwp_{$item_type}s" ); | |
| 404 | + $value = ct_get_object_meta( $object->id, $field_id . '_custom', true ); | |
| 405 | + ct_reset_setup_table(); | |
| 406 | + | |
| 407 | + return $value; | |
| 408 | + | |
| 409 | +} | |
| 410 | +add_filter( 'automatorwp_get_automation_item_option_replacement', 'automatorwp_option_custom_replacement', 10, 5 ); | |