| 1 |
<?php |
| 2 |
/** |
| 3 |
* Trigger on Context. |
| 4 |
* |
| 5 |
* @package WP_Stream |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Stream; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Alert_Trigger_Context |
| 12 |
* |
| 13 |
* @package WP_Stream |
| 14 |
*/ |
| 15 |
class Alert_Trigger_Context extends Alert_Trigger { |
| 16 |
|
| 17 |
/** |
| 18 |
* Unique identifier |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $slug = 'context'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Field key used in database |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $field_key = 'wp_stream_trigger_context'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Checks if a record matches the criteria from the trigger. |
| 33 |
* |
| 34 |
* @see Alert_Trigger::check_record(). |
| 35 |
* |
| 36 |
* @param bool $success Status of previous checks. |
| 37 |
* @param int $record_id Record ID. |
| 38 |
* @param array $recordarr Record data. |
| 39 |
* @param Alert $alert The Alert being worked on. |
| 40 |
* |
| 41 |
* @return bool False on failure, otherwise should return original value of $success. |
| 42 |
*/ |
| 43 |
public function check_record( $success, $record_id, $recordarr, $alert ) { |
| 44 |
if ( ! empty( $alert->alert_meta['trigger_connector'] ) && $recordarr['connector'] !== $alert->alert_meta['trigger_connector'] ) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
if ( ! empty( $alert->alert_meta['trigger_context'] ) && $recordarr['context'] !== $alert->alert_meta['trigger_context'] ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
return $success; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Adds fields to the trigger form. |
| 56 |
* |
| 57 |
* @see Alert_Trigger::add_fields(). |
| 58 |
* |
| 59 |
* @param Form_Generator $form The Form Object to add to. |
| 60 |
* @param Alert $alert The Alert being worked on. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function add_fields( $form, $alert = array() ) { |
| 65 |
$connector = ''; |
| 66 |
if ( is_object( $alert ) && ! empty( $alert->alert_meta['trigger_connector'] ) ) { |
| 67 |
$connector = $alert->alert_meta['trigger_connector']; |
| 68 |
} |
| 69 |
|
| 70 |
$context = ''; |
| 71 |
if ( is_object( $alert ) && ! empty( $alert->alert_meta['trigger_context'] ) ) { |
| 72 |
$context = $alert->alert_meta['trigger_context']; |
| 73 |
} |
| 74 |
|
| 75 |
// Context dropdown menu. |
| 76 |
$context_values = array(); |
| 77 |
|
| 78 |
$form->add_field( |
| 79 |
'select2', |
| 80 |
array( |
| 81 |
'name' => 'wp_stream_trigger_connector_or_context', |
| 82 |
'options' => $this->get_values(), |
| 83 |
'classes' => 'wp_stream_ajax_forward connector_or_context', |
| 84 |
'data' => array( |
| 85 |
'placeholder' => __( 'Any Context', 'stream' ), |
| 86 |
), |
| 87 |
) |
| 88 |
); |
| 89 |
|
| 90 |
$form->add_field( |
| 91 |
'hidden', |
| 92 |
array( |
| 93 |
'name' => 'wp_stream_trigger_connector', |
| 94 |
'value' => $connector, |
| 95 |
'classes' => 'connector wp_stream_ajax_forward', |
| 96 |
) |
| 97 |
); |
| 98 |
|
| 99 |
$form->add_field( |
| 100 |
'hidden', |
| 101 |
array( |
| 102 |
'name' => 'wp_stream_trigger_context', |
| 103 |
'value' => $context, |
| 104 |
'classes' => 'context wp_stream_ajax_forward', |
| 105 |
) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Validate and save Alert object |
| 111 |
* |
| 112 |
* @see Alert_Trigger::save_fields(). |
| 113 |
* |
| 114 |
* @param Alert $alert The Alert being worked on. |
| 115 |
* |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function save_fields( $alert ) { |
| 119 |
$alert->alert_meta['trigger_connector'] = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_connector' ); |
| 120 |
$alert->alert_meta['trigger_context'] = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_context' ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Generate array of possible action values |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
public function get_values() { |
| 129 |
$context_values = array(); |
| 130 |
|
| 131 |
foreach ( $this->get_terms_labels( 'context' ) as $context_id => $context_data ) { |
| 132 |
if ( is_array( $context_data ) ) { |
| 133 |
$child_values = array(); |
| 134 |
if ( isset( $context_data['children'] ) ) { |
| 135 |
$child_values = array(); |
| 136 |
foreach ( $context_data['children'] as $child_id => $child_value ) { |
| 137 |
$child_values[] = array( |
| 138 |
'value' => $context_id . '-' . $child_id, |
| 139 |
'id' => $context_id . '-' . $child_id, |
| 140 |
'text' => $child_value, |
| 141 |
'parent' => $context_id, |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
if ( isset( $context_data['label'] ) ) { |
| 146 |
$context_values[] = array( |
| 147 |
'value' => $context_id, |
| 148 |
'id' => $context_id, |
| 149 |
'text' => $context_data['label'], |
| 150 |
'children' => $child_values, |
| 151 |
); |
| 152 |
} |
| 153 |
} else { |
| 154 |
$context_values[] = array( |
| 155 |
'value' => $context_id, |
| 156 |
'id' => $context_id, |
| 157 |
'text' => $context_data, |
| 158 |
); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return $context_values; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Function will return all terms labels of given column |
| 167 |
* |
| 168 |
* @todo refactor Settings::get_terms_labels into general utility |
| 169 |
* |
| 170 |
* @param string $column string Name of the column. |
| 171 |
* |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
public function get_terms_labels( $column ) { |
| 175 |
$return_labels = array(); |
| 176 |
|
| 177 |
if ( isset( $this->plugin->connectors->term_labels[ 'stream_' . $column ] ) ) { |
| 178 |
if ( 'context' === $column && isset( $this->plugin->connectors->term_labels['stream_connector'] ) ) { |
| 179 |
$connectors = $this->plugin->connectors->term_labels['stream_connector']; |
| 180 |
$contexts = $this->plugin->connectors->term_labels['stream_context']; |
| 181 |
|
| 182 |
foreach ( $connectors as $connector => $connector_label ) { |
| 183 |
$return_labels[ $connector ]['label'] = $connector_label; |
| 184 |
foreach ( $contexts as $context => $context_label ) { |
| 185 |
if ( isset( $this->plugin->connectors->contexts[ $connector ] ) && array_key_exists( $context, $this->plugin->connectors->contexts[ $connector ] ) ) { |
| 186 |
$return_labels[ $connector ]['children'][ $context ] = $context_label; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} else { |
| 191 |
$return_labels = $this->plugin->connectors->term_labels[ 'stream_' . $column ]; |
| 192 |
} |
| 193 |
|
| 194 |
ksort( $return_labels ); |
| 195 |
} |
| 196 |
|
| 197 |
return $return_labels; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Returns the trigger's value for the given alert. |
| 202 |
* |
| 203 |
* @see Alert_Trigger::get_display_value(). |
| 204 |
* |
| 205 |
* @param string $context The location this data will be displayed in. |
| 206 |
* @param Alert|null $alert Alert being processed. |
| 207 |
* |
| 208 |
* @return string |
| 209 |
*/ |
| 210 |
public function get_display_value( $context = 'normal', $alert = null ) { |
| 211 |
$context = ( ! empty( $alert->alert_meta['trigger_context'] ) ) ? $alert->alert_meta['trigger_context'] : null; |
| 212 |
$connector = ( ! empty( $alert->alert_meta['trigger_connector'] ) ) ? $alert->alert_meta['trigger_connector'] : null; |
| 213 |
if ( empty( $context ) && empty( $connector ) ) { |
| 214 |
$context = __( 'Any Context', 'stream' ); |
| 215 |
} else { |
| 216 |
$term_labels = $this->get_terms_labels( 'context' ); |
| 217 |
if ( ! empty( $term_labels[ $connector ]['children'][ $context ] ) ) { |
| 218 |
$context = $term_labels[ $connector ]['children'][ $context ]; |
| 219 |
} else { |
| 220 |
if ( ! empty( $term_labels[ $connector ]['label'] ) ) { |
| 221 |
$context = $term_labels[ $connector ]['label']; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return ucfirst( $context ); |
| 227 |
} |
| 228 |
} |
| 229 |
|