| 1 |
<?php |
| 2 |
/** |
| 3 |
* Trigger on an Action. |
| 4 |
* |
| 5 |
* @package WP_Stream |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Stream; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Alert_Trigger_Action |
| 12 |
* |
| 13 |
* @package WP_Stream |
| 14 |
*/ |
| 15 |
class Alert_Trigger_Action extends Alert_Trigger { |
| 16 |
|
| 17 |
/** |
| 18 |
* Unique identifier |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $slug = 'action'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Meta key used in forms. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $meta_key = 'trigger_action'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Field key used in database |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
public $field_key = 'wp_stream_trigger_action'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Checks if a record matches the criteria from the trigger. |
| 40 |
* |
| 41 |
* @see Alert_Trigger::check_record(). |
| 42 |
* @param bool $success Status of previous checks. |
| 43 |
* @param int $record_id Record ID. |
| 44 |
* @param array $recordarr Record data. |
| 45 |
* @param Alert $alert The Alert being worked on. |
| 46 |
* @return bool False on failure, otherwise should return original value of $success. |
| 47 |
*/ |
| 48 |
public function check_record( $success, $record_id, $recordarr, $alert ) { |
| 49 |
if ( ! empty( $alert->alert_meta['trigger_action'] ) && $recordarr['action'] !== $alert->alert_meta['trigger_action'] ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
return $success; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Adds fields to the trigger form. |
| 57 |
* |
| 58 |
* @see Alert_Trigger::add_fields(). |
| 59 |
* @param Form_Generator $form The Form Object to add to. |
| 60 |
* @param Alert $alert The Alert being worked on. |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function add_fields( $form, $alert = array() ) { |
| 64 |
$value = ''; |
| 65 |
if ( is_object( $alert ) && ! empty( $alert->alert_meta['trigger_action'] ) ) { |
| 66 |
$value = $alert->alert_meta['trigger_action']; |
| 67 |
} |
| 68 |
|
| 69 |
$args = array( |
| 70 |
'name' => esc_attr( $this->field_key ), |
| 71 |
'value' => esc_attr( $value ), |
| 72 |
'options' => $this->get_values(), |
| 73 |
'classes' => 'wp_stream_ajax_forward', |
| 74 |
'data' => array( |
| 75 |
'placeholder' => __( 'Any Action', 'stream' ), |
| 76 |
), |
| 77 |
); |
| 78 |
$form->add_field( 'select2', $args ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Validate and save Alert object |
| 83 |
* |
| 84 |
* @see Alert_Trigger::save_fields(). |
| 85 |
* @param Alert $alert The Alert being worked on. |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function save_fields( $alert ) { |
| 89 |
$input = wp_stream_filter_input( INPUT_POST, $this->field_key ); |
| 90 |
if ( array_key_exists( $input, $this->get_values( true ) ) ) { |
| 91 |
$alert->alert_meta['trigger_action'] = $input; |
| 92 |
} else { |
| 93 |
$alert->alert_meta['trigger_action'] = ''; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Generate array of possible action values |
| 99 |
* |
| 100 |
* @param bool $flat If the array should be multidimensional. |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
public function get_values( $flat = false ) { |
| 104 |
$action_values = array(); |
| 105 |
foreach ( $this->get_terms_labels( 'action' ) as $action_id => $action_data ) { |
| 106 |
if ( ! $flat ) { |
| 107 |
$action_values[] = array( 'id' => $action_id, 'text' => $action_data ); |
| 108 |
} else { |
| 109 |
$action_values[ $action_id ] = $action_data; |
| 110 |
} |
| 111 |
} |
| 112 |
return $action_values; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Function will return all terms labels of given column |
| 117 |
* |
| 118 |
* @todo refactor Settings::get_terms_labels into general utility |
| 119 |
* @param string $column string Name of the column. |
| 120 |
* @return array |
| 121 |
*/ |
| 122 |
public function get_terms_labels( $column ) { |
| 123 |
$return_labels = array(); |
| 124 |
|
| 125 |
if ( isset( $this->plugin->connectors->term_labels[ 'stream_' . $column ] ) ) { |
| 126 |
if ( 'context' === $column && isset( $this->plugin->connectors->term_labels['stream_connector'] ) ) { |
| 127 |
$connectors = $this->plugin->connectors->term_labels['stream_connector']; |
| 128 |
$contexts = $this->plugin->connectors->term_labels['stream_context']; |
| 129 |
|
| 130 |
foreach ( $connectors as $connector => $connector_label ) { |
| 131 |
$return_labels[ $connector ]['label'] = $connector_label; |
| 132 |
foreach ( $contexts as $context => $context_label ) { |
| 133 |
if ( isset( $this->plugin->connectors->contexts[ $connector ] ) && array_key_exists( $context, $this->plugin->connectors->contexts[ $connector ] ) ) { |
| 134 |
$return_labels[ $connector ]['children'][ $context ] = $context_label; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} else { |
| 139 |
$return_labels = $this->plugin->connectors->term_labels[ 'stream_' . $column ]; |
| 140 |
} |
| 141 |
|
| 142 |
ksort( $return_labels ); |
| 143 |
} |
| 144 |
return $return_labels; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the trigger's value for the given alert. |
| 149 |
* |
| 150 |
* @see Alert_Trigger::get_display_value(). |
| 151 |
* @param string $context The location this data will be displayed in. |
| 152 |
* @param Alert $alert Alert being processed. |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
function get_display_value( $context = 'normal', $alert ) { |
| 156 |
$action = ( ! empty( $alert->alert_meta['trigger_action'] ) ) ? $alert->alert_meta['trigger_action'] : null; |
| 157 |
|
| 158 |
if ( 'post_title' === $context ) { |
| 159 |
if ( empty( $action ) ) { |
| 160 |
$action = __( 'performed any action on', 'stream' ); |
| 161 |
} |
| 162 |
} else { |
| 163 |
if ( empty( $action ) ) { |
| 164 |
$action = __( 'Any Action', 'stream' ); |
| 165 |
} else { |
| 166 |
$actions = $this->plugin->connectors->term_labels['stream_action']; |
| 167 |
if ( ! empty( $actions[ $action ] ) ) { |
| 168 |
$action = $actions[ $action ]; |
| 169 |
} |
| 170 |
$action = ucfirst( $action ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return ucfirst( $action ); |
| 175 |
} |
| 176 |
} |
| 177 |
|