PluginProbe
Stream – Activity Log & Audit Trail / 3.8.2
Stream – Activity Log & Audit Trail v3.8.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / alerts / class-alert-trigger-action.php

class-alert-trigger-action.php in Stream – Activity Log & Audit Trail 3.8.2, at alerts/class-alert-trigger-action.php

194 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 *
43 * @param bool $success Status of previous checks.
44 * @param int $record_id Record ID.
45 * @param array $recordarr Record data.
46 * @param Alert $alert The Alert being worked on.
47 *
48 * @return bool False on failure, otherwise should return original value of $success.
49 */
50 public function check_record( $success, $record_id, $recordarr, $alert ) {
51 if ( ! empty( $alert->alert_meta['trigger_action'] ) && $recordarr['action'] !== $alert->alert_meta['trigger_action'] ) {
52 return false;
53 }
54
55 return $success;
56 }
57
58 /**
59 * Adds fields to the trigger form.
60 *
61 * @see Alert_Trigger::add_fields().
62 *
63 * @param Form_Generator $form The Form Object to add to.
64 * @param Alert $alert The Alert being worked on.
65 *
66 * @return void
67 */
68 public function add_fields( $form, $alert = array() ) {
69 $value = '';
70 if ( is_object( $alert ) && ! empty( $alert->alert_meta['trigger_action'] ) ) {
71 $value = $alert->alert_meta['trigger_action'];
72 }
73
74 $args = array(
75 'name' => esc_attr( $this->field_key ),
76 'value' => esc_attr( $value ),
77 'options' => $this->get_values(),
78 'classes' => 'wp_stream_ajax_forward',
79 'data' => array(
80 'placeholder' => __( 'Any Action', 'stream' ),
81 ),
82 );
83 $form->add_field( 'select2', $args );
84 }
85
86 /**
87 * Validate and save Alert object
88 *
89 * @see Alert_Trigger::save_fields().
90 *
91 * @param Alert $alert The Alert being worked on.
92 *
93 * @return void
94 */
95 public function save_fields( $alert ) {
96 $input = wp_stream_filter_input( INPUT_POST, $this->field_key );
97 if ( array_key_exists( $input, $this->get_values( true ) ) ) {
98 $alert->alert_meta['trigger_action'] = $input;
99 } else {
100 $alert->alert_meta['trigger_action'] = '';
101 }
102 }
103
104 /**
105 * Generate array of possible action values
106 *
107 * @param bool $flat If the array should be multidimensional.
108 *
109 * @return array
110 */
111 public function get_values( $flat = false ) {
112 $action_values = array();
113 foreach ( $this->get_terms_labels( 'action' ) as $action_id => $action_data ) {
114 if ( ! $flat ) {
115 $action_values[] = array(
116 'id' => $action_id,
117 'text' => $action_data,
118 );
119 } else {
120 $action_values[ $action_id ] = $action_data;
121 }
122 }
123
124 return $action_values;
125 }
126
127 /**
128 * Function will return all terms labels of given column
129 *
130 * @todo refactor Settings::get_terms_labels into general utility
131 *
132 * @param string $column string Name of the column.
133 *
134 * @return array
135 */
136 public function get_terms_labels( $column ) {
137 $return_labels = array();
138
139 if ( isset( $this->plugin->connectors->term_labels[ 'stream_' . $column ] ) ) {
140 if ( 'context' === $column && isset( $this->plugin->connectors->term_labels['stream_connector'] ) ) {
141 $connectors = $this->plugin->connectors->term_labels['stream_connector'];
142 $contexts = $this->plugin->connectors->term_labels['stream_context'];
143
144 foreach ( $connectors as $connector => $connector_label ) {
145 $return_labels[ $connector ]['label'] = $connector_label;
146 foreach ( $contexts as $context => $context_label ) {
147 if ( isset( $this->plugin->connectors->contexts[ $connector ] ) && array_key_exists( $context, $this->plugin->connectors->contexts[ $connector ] ) ) {
148 $return_labels[ $connector ]['children'][ $context ] = $context_label;
149 }
150 }
151 }
152 } else {
153 $return_labels = $this->plugin->connectors->term_labels[ 'stream_' . $column ];
154 }
155
156 ksort( $return_labels );
157 }
158
159 return $return_labels;
160 }
161
162 /**
163 * Returns the trigger's value for the given alert.
164 *
165 * @see Alert_Trigger::get_display_value().
166 *
167 * @param string $context The location this data will be displayed in.
168 * @param Alert|null $alert Alert being processed.
169 *
170 * @return string
171 */
172 public function get_display_value( $context = 'normal', $alert = null ) {
173 $action = ( ! empty( $alert->alert_meta['trigger_action'] ) ) ? $alert->alert_meta['trigger_action'] : null;
174
175 if ( 'post_title' === $context ) {
176 if ( empty( $action ) ) {
177 $action = __( 'performed any action on', 'stream' );
178 }
179 } else {
180 if ( empty( $action ) ) {
181 $action = __( 'Any Action', 'stream' );
182 } else {
183 $actions = $this->plugin->connectors->term_labels['stream_action'];
184 if ( ! empty( $actions[ $action ] ) ) {
185 $action = $actions[ $action ];
186 }
187 $action = ucfirst( $action );
188 }
189 }
190
191 return ucfirst( $action );
192 }
193 }
194