| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tables Triggers |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Triggers; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Tables_Triggers |
| 16 |
* |
| 17 |
* Fires workflows on table record events (new, updated, deleted, cell-changed, |
| 18 |
* and the manual "Trigger Workflow" button click from the shortcode). |
| 19 |
* |
| 20 |
* PHP actions that fire these events: |
| 21 |
* do_action( 'wpbot_table_new_record', $table_id, $table_meta, $record_data, $record_id ) |
| 22 |
* do_action( 'wpbot_table_updated_record', $table_id, $table_meta, $new_data, $row_id, $old_record ) |
| 23 |
* do_action( 'wpbot_table_deleted_record', $table_id, $table_meta, $row_id, $old_record ) |
| 24 |
* do_action( 'wpbot_table_updated_cell', $table_id, $table_meta, $row_id, $col_slug, $old_val, $new_val ) |
| 25 |
* do_action( 'wpbot_table_trigger_button', $table_id, $table_meta, $row_id, $row_data ) |
| 26 |
*/ |
| 27 |
class Tables_Triggers extends Trigger { |
| 28 |
|
| 29 |
public function __construct() { |
| 30 |
$this->id = 'tables'; |
| 31 |
$this->group = __( 'Tables', 'wpbot-automator' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Register WordPress action hooks. |
| 36 |
*/ |
| 37 |
public function register() { |
| 38 |
add_action( 'wpbot_table_new_record', array( $this, 'on_new_record' ), 10, 4 ); |
| 39 |
add_action( 'wpbot_table_updated_record', array( $this, 'on_updated_record' ), 10, 5 ); |
| 40 |
add_action( 'wpbot_table_deleted_record', array( $this, 'on_deleted_record' ), 10, 4 ); |
| 41 |
add_action( 'wpbot_table_updated_cell', array( $this, 'on_updated_cell' ), 10, 6 ); |
| 42 |
add_action( 'wpbot_table_trigger_button', array( $this, 'on_trigger_button' ), 10, 4 ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* New record added to a table. |
| 47 |
* |
| 48 |
* Available tokens: {table_id}, {table_name}, {record_id}, {<column_slug>}, … |
| 49 |
*/ |
| 50 |
public function on_new_record( $table_id, $table_meta, $record_data, $record_id ) { |
| 51 |
$data = array_merge( |
| 52 |
array( |
| 53 |
'table_id' => $table_id, |
| 54 |
'table_name' => $table_meta['name'] ?? '', |
| 55 |
'record_id' => $record_id, |
| 56 |
), |
| 57 |
is_array( $record_data ) ? $record_data : array() |
| 58 |
); |
| 59 |
|
| 60 |
$this->run( array( 'sub_id' => 'new_record', 'data' => $data ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Existing record updated in a table. |
| 65 |
* |
| 66 |
* Available tokens: {table_id}, {table_name}, {record_id}, {<column_slug>} (new value), |
| 67 |
* {old_<column_slug>} (previous value). |
| 68 |
*/ |
| 69 |
public function on_updated_record( $table_id, $table_meta, $new_data, $row_id, $old_record ) { |
| 70 |
$data = array_merge( |
| 71 |
array( |
| 72 |
'table_id' => $table_id, |
| 73 |
'table_name' => $table_meta['name'] ?? '', |
| 74 |
'record_id' => $row_id, |
| 75 |
), |
| 76 |
is_array( $new_data ) ? $new_data : array() |
| 77 |
); |
| 78 |
|
| 79 |
// Expose old values as {old_<slug>} tokens. |
| 80 |
if ( is_array( $old_record ) ) { |
| 81 |
foreach ( $old_record as $k => $v ) { |
| 82 |
$data[ 'old_' . $k ] = $v; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$this->run( array( 'sub_id' => 'updated_record', 'data' => $data ) ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Record deleted from a table. |
| 91 |
* |
| 92 |
* Available tokens: {table_id}, {table_name}, {record_id}, {<column_slug>} (last known values). |
| 93 |
*/ |
| 94 |
public function on_deleted_record( $table_id, $table_meta, $row_id, $old_record ) { |
| 95 |
$data = array_merge( |
| 96 |
array( |
| 97 |
'table_id' => $table_id, |
| 98 |
'table_name' => $table_meta['name'] ?? '', |
| 99 |
'record_id' => $row_id, |
| 100 |
), |
| 101 |
is_array( $old_record ) ? $old_record : array() |
| 102 |
); |
| 103 |
|
| 104 |
$this->run( array( 'sub_id' => 'deleted_record', 'data' => $data ) ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* A specific cell value changed. |
| 109 |
* |
| 110 |
* Available tokens: {table_id}, {table_name}, {record_id}, {column_slug}, {old_value}, {new_value}. |
| 111 |
*/ |
| 112 |
public function on_updated_cell( $table_id, $table_meta, $row_id, $col_slug, $old_val, $new_val ) { |
| 113 |
$this->run( array( |
| 114 |
'sub_id' => 'updated_cell', |
| 115 |
'data' => array( |
| 116 |
'table_id' => $table_id, |
| 117 |
'table_name' => $table_meta['name'] ?? '', |
| 118 |
'record_id' => $row_id, |
| 119 |
'column_slug' => $col_slug, |
| 120 |
'old_value' => $old_val, |
| 121 |
'new_value' => $new_val, |
| 122 |
), |
| 123 |
) ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* "Trigger Workflow" button clicked from the shortcode frontend. |
| 128 |
* |
| 129 |
* Available tokens: {table_id}, {table_name}, {record_id}, {<column_slug>}, … |
| 130 |
*/ |
| 131 |
public function on_trigger_button( $table_id, $table_meta, $row_id, $row_data ) { |
| 132 |
$data = array_merge( |
| 133 |
array( |
| 134 |
'table_id' => $table_id, |
| 135 |
'table_name' => $table_meta['name'] ?? '', |
| 136 |
'record_id' => $row_id, |
| 137 |
), |
| 138 |
is_array( $row_data ) ? $row_data : array() |
| 139 |
); |
| 140 |
|
| 141 |
$this->run( array( 'sub_id' => 'trigger_button', 'data' => $data ) ); |
| 142 |
} |
| 143 |
} |
| 144 |
|