| @@ -1,0 +1,366 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Integration Filter | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Classes\Integration_Filter | |
| 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_Integration_Filter { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Integration | |
| 16 | + * | |
| 17 | + * @since 1.0.0 | |
| 18 | + * | |
| 19 | + * @var string $integration | |
| 20 | + */ | |
| 21 | + public $integration = ''; | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Filter | |
| 25 | + * | |
| 26 | + * @since 1.0.0 | |
| 27 | + * | |
| 28 | + * @var string $filter | |
| 29 | + */ | |
| 30 | + public $filter = ''; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Filter result | |
| 34 | + * | |
| 35 | + * @since 1.0.0 | |
| 36 | + * | |
| 37 | + * @var string $result | |
| 38 | + */ | |
| 39 | + public $result = ''; | |
| 40 | + | |
| 41 | + public function __construct() { | |
| 42 | + | |
| 43 | + $this->hooks(); | |
| 44 | + | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Register the required hooks | |
| 49 | + * | |
| 50 | + * @since 1.0.0 | |
| 51 | + */ | |
| 52 | + public function hooks() { | |
| 53 | + | |
| 54 | + if ( ! did_action( 'automatorwp_init' ) ) { | |
| 55 | + // Default hook to register | |
| 56 | + add_action('automatorwp_init', array( $this, 'register' ) ); | |
| 57 | + add_action('init', array( $this, 'register_labels' ) ); | |
| 58 | + } else { | |
| 59 | + // Hook for triggers registered from the theme's functions | |
| 60 | + add_action( 'after_setup_theme', array( $this, 'register' ) ); | |
| 61 | + } | |
| 62 | + | |
| 63 | + // Type args | |
| 64 | + add_filter( 'automatorwp_automation_item_type_args', array( $this, 'override_type_args' ), 5, 3 ); | |
| 65 | + | |
| 66 | + // Deserves filter | |
| 67 | + add_filter( 'automatorwp_user_deserves_trigger_filter', array( $this, 'maybe_user_deserves_filter' ), 10, 6 ); | |
| 68 | + add_filter( 'automatorwp_user_deserves_action_filter', array( $this, 'maybe_user_deserves_filter' ), 10, 6 ); | |
| 69 | + | |
| 70 | + // Filter log meta data | |
| 71 | + add_filter( 'automatorwp_user_not_passed_filter_log_meta', array( $this, 'maybe_log_meta' ), 15, 6 ); | |
| 72 | + add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'maybe_log_meta' ), 15, 6 ); | |
| 73 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'maybe_action_log_meta' ), 15, 5 ); | |
| 74 | + | |
| 75 | + // Log fields | |
| 76 | + add_filter( 'automatorwp_log_fields', array( $this, 'maybe_log_fields' ), 10, 5 ); | |
| 77 | + | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Register the trigger | |
| 82 | + * | |
| 83 | + * @since 1.0.0 | |
| 84 | + */ | |
| 85 | + public function register() { | |
| 86 | + // Override | |
| 87 | + } | |
| 88 | + | |
| 89 | + public function register_labels() { | |
| 90 | + $this->register(); | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * Check if object is a filter object | |
| 95 | + * | |
| 96 | + * @since 1.0.0 | |
| 97 | + * | |
| 98 | + * @param stdClass $object The trigger/action object | |
| 99 | + * @param string $item_type The item type (trigger|action) | |
| 100 | + * | |
| 101 | + * @return bool | |
| 102 | + */ | |
| 103 | + public function is_filter_object( $object, $item_type ) { | |
| 104 | + | |
| 105 | + // Bail if not object provided | |
| 106 | + if( ! is_object( $object ) ) { | |
| 107 | + return false; | |
| 108 | + } | |
| 109 | + | |
| 110 | + // Bail if not is a filter | |
| 111 | + if( $object->type !== 'filter' ) { | |
| 112 | + return false; | |
| 113 | + } | |
| 114 | + | |
| 115 | + // Bail if not correct item type provided | |
| 116 | + if( ! in_array( $item_type, array( 'trigger', 'action' ) ) ) { | |
| 117 | + return false; | |
| 118 | + } | |
| 119 | + | |
| 120 | + ct_setup_table( "automatorwp_{$item_type}s" ); | |
| 121 | + $filter = ct_get_object_meta( $object->id, 'filter', true ); | |
| 122 | + ct_reset_setup_table(); | |
| 123 | + | |
| 124 | + // Bail if filter does not matches | |
| 125 | + if( $filter !== $this->filter ) { | |
| 126 | + return false; | |
| 127 | + } | |
| 128 | + | |
| 129 | + return true; | |
| 130 | + | |
| 131 | + } | |
| 132 | + | |
| 133 | + /** | |
| 134 | + * Get the object type args | |
| 135 | + * | |
| 136 | + * @since 1.0.0 | |
| 137 | + * | |
| 138 | + * @param array $type_args The trigger/action args | |
| 139 | + * @param stdClass $object The trigger/action object | |
| 140 | + * @param string $item_type The item type (trigger|action) | |
| 141 | + * | |
| 142 | + * @return array | |
| 143 | + */ | |
| 144 | + public function override_type_args( $type_args, $object, $item_type = '' ) { | |
| 145 | + | |
| 146 | + // Bail if not is a filter | |
| 147 | + if( ! $this->is_filter_object( $object, $item_type ) ) { | |
| 148 | + return $type_args; | |
| 149 | + } | |
| 150 | + | |
| 151 | + $filter_args = automatorwp_get_filter( $this->filter ); | |
| 152 | + | |
| 153 | + // Bail if filter not registered | |
| 154 | + if( ! $filter_args ) { | |
| 155 | + return $type_args; | |
| 156 | + } | |
| 157 | + | |
| 158 | + $type_args['edit_label'] .= ' ' . $filter_args['edit_label']; | |
| 159 | + $type_args['log_label'] .= ' ' . $filter_args['log_label']; | |
| 160 | + $type_args['options'] = array_merge( $type_args['options'], $filter_args['options'] ); | |
| 161 | + | |
| 162 | + return $type_args; | |
| 163 | + | |
| 164 | + } | |
| 165 | + | |
| 166 | + /** | |
| 167 | + * Checks if maybe should call or not to the user_deserves_filter() function | |
| 168 | + * | |
| 169 | + * @since 1.0.0 | |
| 170 | + * | |
| 171 | + * @param bool $deserves_filter True if user deserves filter, false otherwise | |
| 172 | + * @param stdClass $filter The filter object | |
| 173 | + * @param int $user_id The user ID | |
| 174 | + * @param array $event Event information | |
| 175 | + * @param array $filter_options The filter's stored options | |
| 176 | + * @param stdClass $automation The filter's automation object | |
| 177 | + * | |
| 178 | + * @return bool True if user deserves filter, false otherwise | |
| 179 | + */ | |
| 180 | + public function maybe_user_deserves_filter( $deserves_filter, $filter, $user_id, $event, $filter_options, $automation ) { | |
| 181 | + | |
| 182 | + // Bail if filter has not be deserved | |
| 183 | + if( ! $deserves_filter ) { | |
| 184 | + return $deserves_filter; | |
| 185 | + } | |
| 186 | + | |
| 187 | + $item_type = ( current_filter() === 'automatorwp_user_deserves_trigger_filter' ? 'trigger' : 'action' ); | |
| 188 | + | |
| 189 | + // Bail if trigger type don't match this trigger | |
| 190 | + if( ! $this->is_filter_object( $filter, $item_type ) ) { | |
| 191 | + return $deserves_filter; | |
| 192 | + } | |
| 193 | + | |
| 194 | + // Initialize the result | |
| 195 | + $this->result = ''; | |
| 196 | + | |
| 197 | + return $this->user_deserves_filter( $deserves_filter, $filter, $user_id, $event, $filter_options, $automation ); | |
| 198 | + | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * User deserves check | |
| 203 | + * | |
| 204 | + * @since 1.0.0 | |
| 205 | + * | |
| 206 | + * @param bool $deserves_filter True if user deserves filter, false otherwise | |
| 207 | + * @param stdClass $filter The filter object | |
| 208 | + * @param int $user_id The user ID | |
| 209 | + * @param array $event Event information | |
| 210 | + * @param array $filter_options The filter's stored options | |
| 211 | + * @param stdClass $automation The trigger's automation object | |
| 212 | + * | |
| 213 | + * @return bool True if user deserves trigger, false otherwise | |
| 214 | + */ | |
| 215 | + public function user_deserves_filter( $deserves_filter, $filter, $user_id, $event, $filter_options, $automation ) { | |
| 216 | + | |
| 217 | + // Override | |
| 218 | + return $deserves_filter; | |
| 219 | + | |
| 220 | + } | |
| 221 | + | |
| 222 | + /** | |
| 223 | + * Checks if should add custom filter log meta | |
| 224 | + * | |
| 225 | + * @since 1.0.0 | |
| 226 | + * | |
| 227 | + * @param array $log_meta Log meta data | |
| 228 | + * @param stdClass $trigger The trigger object | |
| 229 | + * @param int $user_id The user ID | |
| 230 | + * @param array $event Event information | |
| 231 | + * @param array $trigger_options The trigger's stored option | |
| 232 | + * @param stdClass $automation The trigger's automation object | |
| 233 | + * | |
| 234 | + * @return array | |
| 235 | + */ | |
| 236 | + public function maybe_log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 237 | + | |
| 238 | + $item_type = ''; | |
| 239 | + | |
| 240 | + if( current_filter() === 'automatorwp_user_completed_trigger_log_meta' ) { | |
| 241 | + $item_type = 'trigger'; | |
| 242 | + } else if( current_filter() === 'automatorwp_user_completed_action_log_meta' ) { | |
| 243 | + $item_type = 'action'; | |
| 244 | + } else if( isset( $log_meta['item_type'] ) ) { | |
| 245 | + $item_type = $log_meta['item_type']; | |
| 246 | + } | |
| 247 | + | |
| 248 | + // Bail if not is a filter | |
| 249 | + if( ! $this->is_filter_object( $trigger, $item_type ) ) { | |
| 250 | + return $log_meta; | |
| 251 | + } | |
| 252 | + | |
| 253 | + $log_meta['result'] = $this->result; | |
| 254 | + | |
| 255 | + return $this->log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ); | |
| 256 | + | |
| 257 | + } | |
| 258 | + | |
| 259 | + /** | |
| 260 | + * Checks if should add custom filter log meta | |
| 261 | + * | |
| 262 | + * @since 1.0.0 | |
| 263 | + * | |
| 264 | + * @param array $log_meta Log meta data | |
| 265 | + * @param stdClass $action The action object | |
| 266 | + * @param int $user_id The user ID | |
| 267 | + * @param array $action_options The action's stored option | |
| 268 | + * @param stdClass $automation The action's automation object | |
| 269 | + * | |
| 270 | + * @return array | |
| 271 | + */ | |
| 272 | + public function maybe_action_log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 273 | + | |
| 274 | + global $automatorwp_event; | |
| 275 | + | |
| 276 | + $event = ( is_array( $automatorwp_event ) ? $automatorwp_event : array() ); | |
| 277 | + $item_type = 'action'; | |
| 278 | + | |
| 279 | + // Bail if not is a filter | |
| 280 | + if( ! $this->is_filter_object( $action, $item_type ) ) { | |
| 281 | + return $log_meta; | |
| 282 | + } | |
| 283 | + | |
| 284 | + $log_meta['result'] = $this->result; | |
| 285 | + | |
| 286 | + return $this->log_meta( $log_meta, $action, $user_id, $event, $action_options, $automation ); | |
| 287 | + | |
| 288 | + } | |
| 289 | + | |
| 290 | + /** | |
| 291 | + * Filter custom log meta | |
| 292 | + * | |
| 293 | + * @since 1.0.0 | |
| 294 | + * | |
| 295 | + * @param array $log_meta Log meta data | |
| 296 | + * @param stdClass $trigger The trigger object | |
| 297 | + * @param int $user_id The user ID | |
| 298 | + * @param array $event Event information | |
| 299 | + * @param array $trigger_options The trigger's stored option | |
| 300 | + * @param stdClass $automation The trigger's automation object | |
| 301 | + * | |
| 302 | + * @return array | |
| 303 | + */ | |
| 304 | + public function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 305 | + | |
| 306 | + // Override | |
| 307 | + return $log_meta; | |
| 308 | + | |
| 309 | + } | |
| 310 | + | |
| 311 | + /** | |
| 312 | + * Filter custom log fields | |
| 313 | + * | |
| 314 | + * @since 1.0.0 | |
| 315 | + * | |
| 316 | + * @param array $log_fields The log fields | |
| 317 | + * @param stdClass $log The log object | |
| 318 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 319 | + * | |
| 320 | + * @return array | |
| 321 | + */ | |
| 322 | + public function maybe_log_fields( $log_fields, $log, $object ) { | |
| 323 | + | |
| 324 | + // Bail if log is not assigned to an action | |
| 325 | + if( ! in_array( $log->type, array( 'trigger', 'action', 'filter' ) ) ) { | |
| 326 | + return $log_fields; | |
| 327 | + } | |
| 328 | + | |
| 329 | + if( $log->type === 'filter' ) { | |
| 330 | + $item_type = automatorwp_get_log_meta( $log->id, 'item_type', true ); | |
| 331 | + } else { | |
| 332 | + $item_type = $log->type; | |
| 333 | + } | |
| 334 | + | |
| 335 | + // Bail if not is a filter | |
| 336 | + if( ! $this->is_filter_object( $object, $item_type ) ) { | |
| 337 | + return $log_fields; | |
| 338 | + } | |
| 339 | + | |
| 340 | + $log_fields['result'] = array( | |
| 341 | + 'name' => __( 'Result:', 'automatorwp' ), | |
| 342 | + 'type' => 'text', | |
| 343 | + ); | |
| 344 | + | |
| 345 | + return $this->log_fields( $log_fields, $log, $object ); | |
| 346 | + } | |
| 347 | + | |
| 348 | + /** | |
| 349 | + * Filter custom log fields | |
| 350 | + * | |
| 351 | + * @since 1.0.0 | |
| 352 | + * | |
| 353 | + * @param array $log_fields The log fields | |
| 354 | + * @param stdClass $log The log object | |
| 355 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 356 | + * | |
| 357 | + * @return array | |
| 358 | + */ | |
| 359 | + public function log_fields( $log_fields, $log, $object ) { | |
| 360 | + | |
| 361 | + // Override | |
| 362 | + return $log_fields; | |
| 363 | + | |
| 364 | + } | |
| 365 | + | |
| 366 | +} | |