| @@ -1,0 +1,91 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Triggers | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Custom_Tables\Triggers | |
| 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 | + * Custom Table Labels | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @return array | |
| 18 | + */ | |
| 19 | +function automatorwp_triggers_labels() { | |
| 20 | + | |
| 21 | + return array( | |
| 22 | + 'singular' => __( 'Trigger', 'automatorwp' ), | |
| 23 | + 'plural' => __( 'Triggers', 'automatorwp' ), | |
| 24 | + ); | |
| 25 | + | |
| 26 | +} | |
| 27 | +add_filter( 'ct_automatorwp_triggers_labels', 'automatorwp_triggers_labels' ); | |
| 28 | + | |
| 29 | +/** | |
| 30 | + * Parse query args for triggers | |
| 31 | + * | |
| 32 | + * @since 1.0.0 | |
| 33 | + * | |
| 34 | + * @param string $where | |
| 35 | + * @param CT_Query $ct_query | |
| 36 | + * | |
| 37 | + * @return string | |
| 38 | + */ | |
| 39 | +function automatorwp_triggers_query_where( $where, $ct_query ) { | |
| 40 | + | |
| 41 | + global $ct_table; | |
| 42 | + | |
| 43 | + if( $ct_table->name !== 'automatorwp_triggers' ) { | |
| 44 | + return $where; | |
| 45 | + } | |
| 46 | + | |
| 47 | + $table_name = $ct_table->db->table_name; | |
| 48 | + | |
| 49 | + // Shorthand | |
| 50 | + $qv = $ct_query->query_vars; | |
| 51 | + | |
| 52 | + // Type | |
| 53 | + $where .= automatorwp_custom_table_where( $qv, 'type', 'type', 'string' ); | |
| 54 | + | |
| 55 | + // Automation ID | |
| 56 | + $where .= automatorwp_custom_table_where( $qv, 'automation_id', 'automation_id', 'integer' ); | |
| 57 | + | |
| 58 | + return $where; | |
| 59 | +} | |
| 60 | +add_filter( 'ct_query_where', 'automatorwp_triggers_query_where', 10, 2 ); | |
| 61 | + | |
| 62 | +/** | |
| 63 | + * On delete a trigger | |
| 64 | + * | |
| 65 | + * @since 1.0.0 | |
| 66 | + * | |
| 67 | + * @param int $object_id | |
| 68 | + */ | |
| 69 | +function automatorwp_triggers_delete_object( $object_id ) { | |
| 70 | + | |
| 71 | + global $wpdb, $ct_table; | |
| 72 | + | |
| 73 | + if( ! ( $ct_table instanceof CT_Table ) ) { | |
| 74 | + return; | |
| 75 | + } | |
| 76 | + | |
| 77 | + if( $ct_table->name !== 'automatorwp_triggers' ) { | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + | |
| 81 | + $logs = AutomatorWP()->db->logs; | |
| 82 | + $logs_meta = AutomatorWP()->db->logs_meta; | |
| 83 | + | |
| 84 | + // Delete all logs assigned to this action | |
| 85 | + $wpdb->query( "DELETE l FROM {$logs} AS l WHERE l.object_id = {$object_id} AND l.type = 'trigger'" ); | |
| 86 | + | |
| 87 | + // Delete orphaned log metas | |
| 88 | + $wpdb->query( "DELETE lm FROM {$logs_meta} lm LEFT JOIN {$logs} l ON l.id = lm.id WHERE l.id IS NULL" ); | |
| 89 | + | |
| 90 | +} | |
| 91 | +add_action( 'delete_object', 'automatorwp_triggers_delete_object' ); | |