PluginProbe
Stream – Activity Log & Audit Trail / 3.2.0
Stream – Activity Log & Audit Trail v3.2.0
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 / classes / class-alerts-list.php

class-alerts-list.php in Stream – Activity Log & Audit Trail 3.2.0, at classes/class-alerts-list.php

379 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Listing of Alerts in the WP Admin.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class Alerts_List
12 *
13 * @package WP_Stream
14 */
15 class Alerts_List {
16 /**
17 * Hold the Plugin class
18 *
19 * @var Plugin
20 */
21 public $plugin;
22
23 /**
24 * Class constructor.
25 *
26 * @param Plugin $plugin The main Plugin class.
27 */
28 public function __construct( $plugin ) {
29 $this->plugin = $plugin;
30
31 add_filter( 'bulk_actions-edit-wp_stream_alerts', array( $this, 'suppress_bulk_actions' ), 10, 1 );
32 add_filter( 'disable_months_dropdown', array( $this, 'suppress_months_dropdown' ), 10, 2 );
33 add_filter( 'post_row_actions', array( $this, 'suppress_quick_edit' ), 10, 1 );
34
35 // @todo Make more specific
36 if ( is_admin() ) {
37 add_filter( 'request', array( $this, 'parse_request' ), 10, 2 );
38 }
39 add_filter( 'views_edit-wp_stream_alerts', array( $this, 'manage_views' ) );
40
41 add_filter( 'manage_wp_stream_alerts_posts_columns', array( $this, 'manage_columns' ) );
42 add_action( 'manage_wp_stream_alerts_posts_custom_column', array( $this, 'column_data' ), 10, 2 );
43
44 add_action( 'quick_edit_custom_box', array( $this, 'display_custom_quick_edit' ), 10, 2 );
45 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
46
47 add_filter( 'wp_insert_post_data', array( $this, 'save_alert_inline_edit' ), 99, 2 );
48 }
49
50 /**
51 * Default to wp_stream_enabled and wp_stream_disabled when querying for alerts
52 *
53 * @filter request
54 *
55 * @param array $query_vars Arguments for query to populate table.
56 * @return array
57 */
58 function parse_request( $query_vars ) {
59 $screen = get_current_screen();
60 if ( 'edit-wp_stream_alerts' === $screen->id && Alerts::POST_TYPE === $query_vars['post_type'] && empty( $query_vars['post_status'] ) ) {
61 $query_vars['post_status'] = array( 'wp_stream_enabled', 'wp_stream_disabled' );
62 }
63 return $query_vars;
64 }
65
66 /**
67 * Manage views on the alerts list view
68 *
69 * @filter views_edit-wp_stream_alerts
70 *
71 * @param array $views View links HTML.
72 * @return array
73 */
74 function manage_views( $views ) {
75
76 if ( array_key_exists( 'trash', $views ) ) {
77 $trash = $views['trash'];
78 unset( $views['trash'] );
79 $views['trash'] = $trash;
80 }
81
82 return $views;
83 }
84
85 /**
86 * Manages columns on the alerts list view
87 *
88 * @filter manage_wp_stream_alerts_posts_columns
89 *
90 * @param array $columns Column id -> title array.
91 * @return array
92 */
93 function manage_columns( $columns ) {
94 $columns = array(
95 'cb' => $columns['cb'],
96 'alert_trigger' => __( 'Trigger', 'stream' ),
97 'alert_type' => __( 'Type', 'stream' ),
98 'alert_status' => __( 'Status', 'stream' ),
99 );
100 return $columns;
101 }
102
103 /**
104 * Fills in column data for custom columns.
105 *
106 * @action manage_wp_stream_alerts_posts_custom_column
107 *
108 * @param string $column_name Column name to show data for.
109 * @param int $post_id The post being processed.
110 * @return mixed
111 */
112 function column_data( $column_name, $post_id ) {
113
114 $alert = $this->plugin->alerts->get_alert( $post_id );
115 if ( ! $alert ) {
116 return;
117 }
118
119 switch ( $column_name ) {
120 case 'alert_trigger' :
121 $values = array();
122 foreach ( $this->plugin->alerts->alert_triggers as $trigger_type => $trigger_obj ) {
123 $value = $trigger_obj->get_display_value( 'list_table', $alert );
124 $values[] = '<span class="alert_trigger_value alert_trigger_' . esc_attr( $trigger_type ) . '">' . esc_html( $value ) . '</span>';
125 }
126 ?>
127 <div><?php echo wp_kses_post( join( '', $values ) ); ?></div>
128 <div class="row-actions wp-stream-show-mobile">
129 <?php echo wp_kses_post( $this->custom_column_actions( $post_id ) ); ?>
130 <button type="button" class="toggle-row"><span class="screen-reader-text"><?php echo esc_html__( 'Show more details', 'stream' ); ?></span></button>
131 </div>
132 <?php
133 if ( ! empty( $alert->alert_meta['trigger_connector'] ) ) {
134 $trigger_connector = $alert->alert_meta['trigger_connector'];
135 } else {
136 $trigger_connector = '';
137 }
138 if ( ! empty( $alert->alert_meta['trigger_context'] ) ) {
139 $trigger_context = $alert->alert_meta['trigger_context'];
140 } else {
141 $trigger_context = '';
142 }
143 if ( ! empty( $alert->alert_meta['trigger_action'] ) ) {
144 $trigger_action = $alert->alert_meta['trigger_action'];
145 } else {
146 $trigger_action = '';
147 }
148 ?>
149 <input type="hidden" name="wp_stream_trigger_connector" value="<?php echo esc_attr( $trigger_connector ); ?>" />
150 <input type="hidden" name="wp_stream_trigger_context" value="<?php echo esc_attr( $trigger_context ); ?>" />
151 <input type="hidden" name="wp_stream_trigger_action" value="<?php echo esc_attr( $trigger_action ); ?>" />
152 <?php
153 echo wp_kses_post( $this->custom_column_actions( $post_id ) );
154 break;
155 case 'alert_type' :
156 $alert_type = $alert->alert_type;
157 if ( ! empty( $this->plugin->alerts->alert_types[ $alert_type ]->name ) ) {
158 $alert_name = $this->plugin->alerts->alert_types[ $alert_type ]->name;
159 } else {
160 $alert_name = 'Untitled Alert';
161 }
162 ?>
163 <input type="hidden" name="wp_stream_alert_type" value="<?php echo esc_attr( $alert->alert_type ); ?>" />
164 <strong class="row-title"><?php echo esc_html( $alert_name ); ?></strong>
165 <?php
166 if ( ! empty( $alert->alert_meta['color'] ) ) {
167 ?>
168 <input type="hidden" name="wp_stream_highlight_color" value="<?php echo esc_attr( $alert->alert_meta['color'] ); ?>" />
169 <?php
170 }
171 if ( ! empty( $alert->alert_meta['email_recipient'] ) ) {
172 ?>
173 <input type="hidden" name="wp_stream_email_recipient" value="<?php echo esc_attr( $alert->alert_meta['email_recipient'] ); ?>" />
174 <?php
175 }
176 if ( ! empty( $alert->alert_meta['email_subject'] ) ) {
177 ?>
178 <input type="hidden" name="wp_stream_email_subject" value="<?php echo esc_attr( $alert->alert_meta['email_subject'] ); ?>" />
179 <?php
180 }
181 if ( ! empty( $alert->alert_meta['event_name'] ) ) {
182 ?>
183 <input type="hidden" name="wp_stream_ifttt_event_name" value="<?php echo esc_attr( $alert->alert_meta['event_name'] ); ?>" />
184 <?php
185 }
186 if ( ! empty( $alert->alert_meta['maker_key'] ) ) {
187 ?>
188 <input type="hidden" name="wp_stream_ifttt_maker_key" value="<?php echo esc_attr( $alert->alert_meta['maker_key'] ); ?>" />
189 <?php
190 }
191 break;
192 case 'alert_status' :
193 $post_status_object = get_post_status_object( get_post_status( $post_id ) );
194 if ( ! empty( $post_status_object ) ) {
195 echo esc_html( $post_status_object->label );
196 }
197 ?>
198 <input type="hidden" name="wp_stream_alert_status" value="<?php echo esc_attr( $post_status_object->name ); ?>" />
199 <?php
200 break;
201 }
202 }
203
204 /**
205 * Remove 'edit' action from bulk actions
206 *
207 * @filter bulk_actions-edit-wp_stream_alerts
208 *
209 * @param array $actions List of bulk actions available.
210 * @return array
211 */
212 public function suppress_bulk_actions( $actions ) {
213 unset( $actions['edit'] );
214 return $actions;
215 }
216
217 /**
218 * Remove quick edit action from inline edit actions
219 *
220 * @filter post_row_actions
221 *
222 * @param array $actions List of inline edit actions available.
223 * @return array
224 */
225 function suppress_quick_edit( $actions ) {
226 if ( Alerts::POST_TYPE !== get_post_type() ) {
227 return $actions;
228 }
229 unset( $actions['edit'] );
230 unset( $actions['view'] );
231 unset( $actions['trash'] );
232 unset( $actions['inline hide-if-no-js'] );
233 return $actions;
234 }
235
236 /**
237 * Remove months dropdown from Alerts list page
238 *
239 * @filter disable_months_dropdown
240 *
241 * @param bool $status Status of months dropdown enabling.
242 * @param string $post_type Post type status is related to.
243 * @return bool
244 */
245 public function suppress_months_dropdown( $status, $post_type ) {
246 if ( Alerts::POST_TYPE === $post_type ) {
247 $status = true;
248 }
249 return $status;
250 }
251
252 /**
253 * Custom column actions for alerts main screen
254 *
255 * @param int $post_id The current post ID.
256 *
257 * @return string
258 */
259 public function custom_column_actions( $post_id ) {
260 $post_status = wp_stream_filter_input( INPUT_GET, 'post_status' );
261 ob_start();
262 if ( 'trash' !== $post_status ) {
263 $bare_url = admin_url( 'post.php?post=' . $post_id . '&action=trash' );
264 $nonce_url = wp_nonce_url( $bare_url, 'trash-post_' . $post_id );
265 ?>
266 <div class="row-actions">
267 <span class="inline hide-if-no-js"><a href="#" class="editinline" aria-label="Quick edit “Hello world!” inline"><?php esc_html_e( 'Edit' ); ?></a>&nbsp;|&nbsp;</span>
268 <span class="trash">
269 <a href="<?php echo esc_url( $nonce_url ); ?>" class="submitdelete"><?php esc_html_e( 'Trash', 'stream' ); ?></a>
270 </span>
271 </div>
272 <?php
273 }
274 return ob_get_clean();
275 }
276
277 /**
278 * Display a custom quick edit form.
279 */
280 public function display_custom_quick_edit() {
281 static $fired = false;
282 if ( false !== $fired ) {
283 return;
284 }
285 $screen = get_current_screen();
286 if ( 'edit-wp_stream_alerts' !== $screen->id ) {
287 return;
288 }
289 wp_nonce_field( plugin_basename( __FILE__ ), Alerts::POST_TYPE . '_edit_nonce' );
290 $box_type = array(
291 'triggers',
292 'notification',
293 'submit',
294 );
295 ?>
296 <legend class="inline-edit-legend"><?php esc_html_e( 'Edit', 'stream' ); ?></legend>
297 <?php
298 foreach ( $box_type as $type ) : // @todo remove inline styles. ?>
299 <fieldset class="inline-edit-col inline-edit-<?php echo esc_attr( Alerts::POST_TYPE ); ?>">
300 <?php
301 $function_name = 'display_' . $type . '_box';
302 $the_post = get_post();
303 call_user_func( array( $this->plugin->alerts, $function_name ), $the_post );
304 ?>
305 </fieldset>
306 <?php
307 endforeach;
308 $fired = true;
309 }
310
311 /**
312 * Enqueue scripts for the alerts list screen.
313 *
314 * @param string $page The current page name.
315 */
316 public function enqueue_scripts( $page ) {
317 $screen = get_current_screen();
318 if ( 'edit-wp_stream_alerts' !== $screen->id ) {
319 return;
320 }
321 wp_register_script( 'wp-stream-alerts-list-js', $this->plugin->locations['url'] . 'ui/js/alerts-list.js', array( 'wp-stream-alerts', 'jquery' ) );
322 wp_enqueue_script( 'wp-stream-alerts-list-js' );
323 wp_register_style( 'wp-stream-alerts-list-css', $this->plugin->locations['url'] . 'ui/css/alerts-list.css' );
324 wp_enqueue_style( 'wp-stream-alerts-list-css' );
325 wp_enqueue_style( 'wp-stream-select2' );
326 }
327
328 /**
329 * Save alert meta after using the inline editor.
330 *
331 * @param array $data Filtered post data.
332 * @param array $postarr Raw post data.
333 *
334 * @return array
335 */
336 function save_alert_inline_edit( $data, $postarr ) {
337 if ( did_action( 'customize_preview_init' ) ) {
338 return $data;
339 }
340
341 $post_id = $postarr['ID'];
342 $post_type = wp_stream_filter_input( INPUT_POST, 'post_type' );
343 if ( Alerts::POST_TYPE !== $post_type ) {
344 return $data;
345 }
346 if ( ! current_user_can( 'edit_post', $post_id ) ) {
347 return $data;
348 }
349
350 $nonce = wp_stream_filter_input( INPUT_POST, Alerts::POST_TYPE . '_edit_nonce' );
351 if ( null === $nonce || ! wp_verify_nonce( $nonce, plugin_basename( __FILE__ ) ) ) {
352 return $data;
353 }
354
355 $trigger_author = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_author' );
356 $trigger_connector_and_context = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_connector_or_context' );
357 $trigger_connector_and_context_split = explode( '-', $trigger_connector_and_context );
358 $trigger_connector = $trigger_connector_and_context_split[0];
359 $trigger_context = $trigger_connector_and_context_split[1];
360
361 $trigger_action = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_action' );
362 $alert_type = wp_stream_filter_input( INPUT_POST, 'wp_stream_alert_type' );
363 $alert_status = wp_stream_filter_input( INPUT_POST, 'wp_stream_alert_status' );
364 $data['post_status'] = $alert_status;
365
366 update_post_meta( $post_id, 'alert_type', $alert_type );
367
368 $alert_meta = array(
369 'trigger_author' => $trigger_author,
370 'trigger_connector' => $trigger_connector,
371 'trigger_action' => $trigger_action,
372 'trigger_context' => $trigger_context,
373 );
374 $alert_meta = apply_filters( 'wp_stream_alerts_save_meta', $alert_meta, $alert_type );
375 update_post_meta( $post_id, 'alert_meta', $alert_meta );
376 return $data;
377 }
378 }
379