PluginProbe
Stream – Activity Log & Audit Trail / trunk
Stream – Activity Log & Audit Trail vtrunk
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.php

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

937 lines 24.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Alerts feature class.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class Alerts
12 *
13 * @package WP_Stream
14 */
15 class Alerts {
16
17 /**
18 * Alerts post type slug
19 */
20 const POST_TYPE = 'wp_stream_alerts';
21
22 /**
23 * Enabled alert post status slug.
24 */
25 const STATUS_ENABLED = 'wp_stream_enabled';
26
27 /**
28 * Disabled alert post status slug.
29 */
30 const STATUS_DISABLED = 'wp_stream_disabled';
31
32 /**
33 * Triggered Alerts meta key for Records
34 */
35 const ALERTS_TRIGGERED_META_KEY = 'wp_stream_alerts_triggered';
36
37 /**
38 * Capability required to access alerts.
39 */
40 const CAPABILITY = WP_STREAM_SETTINGS_CAPABILITY;
41
42 /**
43 * Holds Instance of plugin object
44 *
45 * @var Plugin
46 */
47 public $plugin;
48
49 /**
50 * Post meta prefix
51 *
52 * @var string
53 */
54 public $meta_prefix = 'wp_stream';
55
56 /**
57 * Alert Types
58 *
59 * @var array
60 */
61 public $alert_types = array();
62
63 /**
64 * Alert Triggers
65 *
66 * @var array
67 */
68 public $alert_triggers = array();
69
70 /**
71 * Class constructor.
72 *
73 * @param Plugin $plugin Instance of plugin object.
74 */
75 public function __construct( $plugin ) {
76 $this->plugin = $plugin;
77
78 // Register custom post type.
79 add_action( 'init', array( $this, 'register_post_type' ) );
80
81 // Add custom post type to menu.
82 add_action( 'wp_stream_admin_menu', array( $this, 'register_menu' ) );
83
84 // Add scripts to post screens.
85 add_action(
86 'admin_enqueue_scripts',
87 array(
88 $this,
89 'register_scripts',
90 )
91 );
92
93 add_action(
94 'network_admin_menu',
95 array(
96 $this,
97 'change_menu_link_url',
98 ),
99 99
100 );
101
102 add_filter(
103 'wp_stream_record_inserted',
104 array(
105 $this,
106 'check_records',
107 ),
108 10,
109 2
110 );
111
112 add_action(
113 'wp_ajax_load_alerts_settings',
114 array(
115 $this,
116 'load_alerts_settings',
117 )
118 );
119 add_action( 'wp_ajax_get_actions', array( $this, 'get_actions' ) );
120 add_action(
121 'wp_ajax_save_new_alert',
122 array(
123 $this,
124 'save_new_alert',
125 )
126 );
127 add_action(
128 'wp_ajax_get_new_alert_triggers_notifications',
129 array(
130 $this,
131 'get_new_alert_triggers_notifications',
132 )
133 );
134
135 $this->load_alert_types();
136 $this->load_alert_triggers();
137
138 add_filter(
139 'wp_stream_action_links_posts',
140 array(
141 $this,
142 'change_alert_action_links',
143 ),
144 11,
145 2
146 );
147 }
148
149 /**
150 * Load alert_type classes
151 *
152 * @return void
153 */
154 public function load_alert_types() {
155 $alert_types = array(
156 'none',
157 'highlight',
158 'email',
159 'ifttt',
160 'slack',
161 );
162
163 $classes = array();
164 foreach ( $alert_types as $alert_type ) {
165 $file_location = $this->plugin->locations['dir'] . '/alerts/class-alert-type-' . $alert_type . '.php';
166 if ( file_exists( $file_location ) ) {
167 include_once $file_location;
168 $class_name = sprintf( '\WP_Stream\Alert_Type_%s', str_replace( '-', '_', $alert_type ) );
169 if ( ! class_exists( $class_name ) ) {
170 continue;
171 }
172 $class = new $class_name( $this->plugin );
173 if ( ! property_exists( $class, 'slug' ) ) {
174 continue;
175 }
176 $classes[ $class->slug ] = $class;
177 }
178 }
179
180 /**
181 * Allows for adding additional alert_types via classes that extend Notifier.
182 *
183 * @param array $classes An array of Notifier objects. In the format alert_type_slug => Notifier_Class()
184 */
185 $this->alert_types = apply_filters( 'wp_stream_alert_types', $classes );
186
187 // Ensure that all alert_types extend Notifier.
188 foreach ( $this->alert_types as $key => $alert_type ) {
189 if ( ! $this->is_valid_alert_type( $alert_type ) ) {
190 unset( $this->alert_types[ $key ] );
191 }
192 }
193 }
194
195 /**
196 * Load alert_type classes
197 *
198 * @return void
199 */
200 public function load_alert_triggers() {
201 $alert_triggers = array(
202 'author',
203 'context',
204 'action',
205 );
206
207 $classes = array();
208 foreach ( $alert_triggers as $alert_trigger ) {
209 $file_location = $this->plugin->locations['dir'] . '/alerts/class-alert-trigger-' . $alert_trigger . '.php';
210 if ( file_exists( $file_location ) ) {
211 include_once $file_location;
212 $class_name = sprintf( '\WP_Stream\Alert_Trigger_%s', str_replace( '-', '_', $alert_trigger ) );
213 if ( ! class_exists( $class_name ) ) {
214 continue;
215 }
216 $class = new $class_name( $this->plugin );
217 if ( ! property_exists( $class, 'slug' ) ) {
218 continue;
219 }
220 $classes[ $class->slug ] = $class;
221 }
222 }
223
224 /**
225 * Allows for adding additional alert_triggers via classes that extend Notifier.
226 *
227 * @param array $classes An array of Notifier objects. In the format alert_trigger_slug => Notifier_Class()
228 */
229 $this->alert_triggers = apply_filters( 'wp_stream_alert_triggers', $classes );
230
231 // Ensure that all alert_triggers extend Notifier.
232 foreach ( $this->alert_triggers as $key => $alert_trigger ) {
233 if ( ! $this->is_valid_alert_trigger( $alert_trigger ) ) {
234 unset( $this->alert_triggers[ $key ] );
235 }
236 }
237 }
238
239 /**
240 * Checks whether a Alert Type class is valid
241 *
242 * @param Alert_Type $alert_type The class to check.
243 *
244 * @return bool
245 */
246 public function is_valid_alert_type( $alert_type ) {
247 if ( ! is_a( $alert_type, 'WP_Stream\Alert_Type' ) ) {
248 return false;
249 }
250
251 if ( ! method_exists( $alert_type, 'is_dependency_satisfied' ) || ! $alert_type->is_dependency_satisfied() ) {
252 return false;
253 }
254
255 return true;
256 }
257
258 /**
259 * Checks whether a Alert Trigger class is valid
260 *
261 * @param Alert_Trigger $alert_trigger The class to check.
262 *
263 * @return bool
264 */
265 public function is_valid_alert_trigger( $alert_trigger ) {
266 if ( ! is_a( $alert_trigger, 'WP_Stream\Alert_Trigger' ) ) {
267 return false;
268 }
269
270 if ( ! method_exists( $alert_trigger, 'is_dependency_satisfied' ) || ! $alert_trigger->is_dependency_satisfied() ) {
271 return false;
272 }
273
274 return true;
275 }
276
277 /**
278 * Checks record being processed against active alerts.
279 *
280 * @filter wp_stream_record_inserted
281 *
282 * @param int $record_id The record being processed.
283 * @param array $recordarr Record data.
284 *
285 * @return array
286 */
287 public function check_records( $record_id, $recordarr ) {
288 $args = array(
289 'post_type' => self::POST_TYPE,
290 'post_status' => 'wp_stream_enabled',
291 );
292
293 $alerts = new \WP_Query( $args );
294 foreach ( $alerts->posts as $alert ) {
295 $alert = $this->get_alert( $alert->ID );
296
297 if ( false === $alert ) {
298 continue;
299 }
300
301 $status = $alert->check_record( $record_id, $recordarr );
302 if ( $status ) {
303 $alert->send_alert( $record_id, $recordarr ); // @todo send_alert expects int, not array.
304 }
305 }
306
307 return $recordarr;
308 }
309
310 /**
311 * Register scripts for page load
312 *
313 * @action admin_enqueue_scripts
314 *
315 * @return void
316 */
317 public function register_scripts() {
318 $screen = get_current_screen();
319 if ( 'edit-wp_stream_alerts' !== $screen->id ) {
320 return;
321 }
322
323 $this->plugin->enqueue_asset(
324 'alerts',
325 array(
326 $this->plugin->with_select2(),
327 'inline-edit-post',
328 ),
329 array(
330 'any' => __( 'Any', 'stream' ),
331 'anyContext' => __( 'Any Context', 'stream' ),
332 'getActionsNonce' => wp_create_nonce( 'stream_get_actions' ),
333 )
334 );
335 }
336
337 /**
338 * Register custom post type
339 *
340 * @action init
341 *
342 * @return void
343 */
344 public function register_post_type() {
345 $labels = array(
346 'name' => _x( 'Alerts', 'post type general name', 'stream' ),
347 'singular_name' => _x( 'Alert', 'post type singular name', 'stream' ),
348 'menu_name' => _x( 'Alerts', 'admin menu', 'stream' ),
349 'name_admin_bar' => _x( 'Alert', 'add new on admin bar', 'stream' ),
350 'add_new' => _x( 'Add New', 'book', 'stream' ),
351 'add_new_item' => __( 'Add New Alert', 'stream' ),
352 'new_item' => __( 'New Alert', 'stream' ),
353 'edit_item' => __( 'Edit Alert', 'stream' ),
354 'view_item' => __( 'View Alert', 'stream' ),
355 'all_items' => __( 'Alerts', 'stream' ),
356 'search_items' => __( 'Search Alerts', 'stream' ),
357 'parent_item_colon' => __( 'Parent Alerts:', 'stream' ),
358 'not_found' => __( 'No alerts found.', 'stream' ),
359 'not_found_in_trash' => __( 'No alerts found in Trash.', 'stream' ),
360 );
361
362 $args = array(
363 'labels' => $labels,
364 'description' => __( 'Alerts for Stream.', 'stream' ),
365 'public' => false,
366 'publicly_queryable' => false,
367 'rewrite' => false,
368 'exclude_from_search' => true,
369 'show_ui' => true,
370 'show_in_menu' => false, // @see modify_admin_menu
371 'supports' => false,
372 'capabilities' => array(
373 'publish_posts' => self::CAPABILITY,
374 'edit_others_posts' => self::CAPABILITY,
375 'delete_posts' => self::CAPABILITY,
376 'delete_others_posts' => self::CAPABILITY,
377 'read_private_posts' => self::CAPABILITY,
378 'edit_post' => self::CAPABILITY,
379 'delete_post' => self::CAPABILITY,
380 'read_post' => self::CAPABILITY,
381 ),
382 );
383
384 register_post_type( self::POST_TYPE, $args );
385
386 $args = array(
387 'label' => _x( 'Enabled', 'alert', 'stream' ),
388 'public' => false,
389 'show_in_admin_all_list' => true,
390 'show_in_admin_status_list' => true,
391 /* translators: %s: a number of items (e.g. "42") */
392 'label_count' => _n_noop( 'Enabled <span class="count">(%s)</span>', 'Enabled <span class="count">(%s)</span>', 'stream' ),
393 );
394
395 register_post_status( 'wp_stream_enabled', $args );
396
397 $args = array(
398 'label' => _x( 'Disabled', 'alert', 'stream' ),
399 'public' => false,
400 'internal' => false,
401 'show_in_admin_all_list' => true,
402 'show_in_admin_status_list' => true,
403 /* translators: %s: a number of items (e.g. "42") */
404 'label_count' => _n_noop( 'Disabled <span class="count">(%s)</span>', 'Disabled <span class="count">(%s)</span>', 'stream' ),
405 );
406
407 register_post_status( 'wp_stream_disabled', $args );
408 }
409
410 /**
411 * Return alert object of the given ID
412 *
413 * @param string|int $post_id Post ID for the alert.
414 *
415 * @return Alert
416 */
417 public function get_alert( $post_id = '' ) {
418 if ( ! $post_id ) {
419 return new Alert( null, $this->plugin );
420 }
421
422 $post = get_post( $post_id );
423 if ( ! ( $post instanceof \WP_Post ) ) {
424 return new Alert( null, $this->plugin );
425 }
426
427 $alert_type = get_post_meta( $post_id, 'alert_type', true );
428 $alert_meta = get_post_meta( $post_id, 'alert_meta', true );
429
430 $obj = (object) array(
431 'ID' => $post->ID,
432 'status' => $post->post_status,
433 'date' => $post->post_date,
434 'author' => $post->post_author,
435 'alert_type' => $alert_type,
436 'alert_meta' => $alert_meta,
437 );
438
439 return new Alert( $obj, $this->plugin );
440 }
441
442 /**
443 * Return a list of alerts, optionally filtered by post status.
444 *
445 * @param array $statuses Optional list of alert post statuses to include.
446 * Defaults to enabled + disabled.
447 *
448 * @return Alert[]
449 */
450 public function get_alerts( array $statuses = array() ) {
451 if ( empty( $statuses ) ) {
452 $statuses = array( self::STATUS_ENABLED, self::STATUS_DISABLED );
453 }
454
455 $posts = get_posts(
456 array(
457 'post_type' => self::POST_TYPE,
458 'post_status' => $statuses,
459 'posts_per_page' => -1, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
460 )
461 );
462
463 $alerts = array();
464 foreach ( $posts as $post ) {
465 $alerts[] = $this->get_alert( $post->ID );
466 }
467
468 return $alerts;
469 }
470
471 /**
472 * Add custom post type to menu
473 *
474 * @action admin_menu
475 *
476 * @return void
477 */
478 public function register_menu() {
479 add_submenu_page(
480 $this->plugin->admin->records_page_slug,
481 __( 'Alerts', 'stream' ),
482 __( 'Alerts', 'stream' ),
483 self::CAPABILITY,
484 'edit.php?post_type=wp_stream_alerts'
485 );
486 }
487
488 /**
489 * Modify the Stream > Alerts Network Admin Menu link.
490 *
491 * In self::register_menu(), the Alerts submenu item
492 * is essentially set to go to the Site's admin area.
493 *
494 * However, on the Network admin, we need to redirect
495 * it to the first site in the network, as this is
496 * where the true Network Alerts settings page is located.
497 *
498 * @action network_admin_menu
499 * @return bool
500 */
501 public function change_menu_link_url() {
502 global $submenu;
503
504 $parent = 'wp_stream';
505 $page = 'edit.php?post_type=wp_stream_alerts';
506
507 // If we're not on the Stream menu item, return.
508 if ( ! isset( $submenu[ $parent ] ) ) {
509 return false;
510 }
511
512 // Get the first existing Site in the Network.
513 $sites = wp_stream_get_sites(
514 array(
515 'limit' => 5, // Limit the size of the query.
516 )
517 );
518
519 $site_id = '1';
520
521 // Function wp_get_sites() can return an empty array if the network is too large.
522 if ( ! empty( $sites ) && ! empty( $sites[0]->blog_id ) ) {
523 $site_id = $sites[0]->blog_id;
524 }
525
526 $new_url = get_admin_url( $site_id, $page );
527
528 foreach ( $submenu[ $parent ] as $key => $value ) {
529 // Set correct URL for the menu item.
530 if ( $page === $value[2] ) {
531 // This hack is not kosher, see the docblock for an explanation.
532 $submenu[ $parent ][ $key ][2] = $new_url; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
533 break;
534 }
535 }
536
537 return true;
538 }
539
540 /**
541 * Display Alert Type Meta Box
542 *
543 * @param \WP_Post|array $post Post object for current alert.
544 *
545 * @return void
546 */
547 public function display_notification_box( $post = array() ) {
548 $alert = null;
549 $alert_type = 'none';
550 if ( is_object( $post ) ) {
551 $alert = $this->get_alert( $post->ID );
552 if ( false !== $alert ) {
553 $alert_type = $alert->alert_type;
554 }
555 }
556 $form = new Form_Generator();
557
558 echo '<label>' . esc_html__( 'Alert me by', 'stream' ) . '</label>';
559 $form->render_field(
560 'select',
561 array(
562 'id' => 'wp_stream_alert_type',
563 'name' => 'wp_stream_alert_type',
564 'value' => $alert_type,
565 'options' => $this->get_notification_values(),
566 'placeholder' => __( 'No Alert', 'stream' ),
567 'title' => 'Alert Type:',
568 )
569 );
570
571 echo '<div id="wp_stream_alert_type_form">';
572 if ( is_object( $alert ) ) {
573 $alert->get_alert_type_obj()->display_fields( $alert );
574 } else {
575 $this->plugin->alerts->alert_types['none']->display_fields( array() );
576 }
577
578 echo '</div>';
579 }
580
581 /**
582 * Returns settings form HTML for AJAX use
583 *
584 * @action wp_ajax_load_alerts_settings
585 *
586 * @return void
587 */
588 public function load_alerts_settings() {
589 if ( ! current_user_can( self::CAPABILITY ) ) {
590 wp_send_json_error(
591 array(
592 'message' => 'You do not have permission to do this.',
593 )
594 );
595 }
596 $alert = array();
597 $post_id = wp_stream_filter_input( INPUT_POST, 'post_id' );
598 if ( ! empty( $post_id ) && 'new' !== $post_id ) {
599 $alert = $this->get_alert( $post_id );
600 if ( false === $alert ) {
601 wp_send_json_error(
602 array(
603 'message' => 'Could not find alert.',
604 )
605 );
606 }
607 }
608
609 $alert_type = wp_stream_filter_input( INPUT_POST, 'alert_type' );
610 if ( empty( $alert_type ) ) {
611 $alert_type = 'none';
612 }
613 if ( ! array_key_exists( $alert_type, $this->alert_types ) ) {
614 wp_send_json_error(
615 array(
616 'message' => 'Could not find alert type.',
617 )
618 );
619 }
620
621 ob_start();
622 $this->alert_types[ $alert_type ]->display_fields( $alert );
623 $output = ob_get_contents();
624 ob_end_clean();
625
626 $data = array(
627 'html' => $output,
628 );
629 wp_send_json_success( $data );
630 }
631
632 /**
633 * Display Trigger Meta Box
634 *
635 * @param \WP_Post|array $post Post object for current alert.
636 *
637 * @return void
638 */
639 public function display_triggers_box( $post = array() ) {
640 $alert = false;
641 if ( is_object( $post ) ) {
642 $alert = $this->get_alert( $post->ID );
643 }
644 if ( false === $alert ) {
645 $alert = array();
646 }
647
648 $form = new Form_Generator();
649 do_action( 'wp_stream_alert_trigger_form_display', $form, $alert );
650 // @TODO use human readable text.
651 echo '<label>' . esc_html__( 'Alert me when', 'stream' ) . '</label>';
652 $form->render_fields();
653 wp_nonce_field( 'save_alert', 'wp_stream_alerts_nonce' );
654
655 if ( $post instanceof \WP_Post ) :
656 /**
657 * These fields are required for the post to be saved, as the Admin AJAX inline_save action is fired.
658 *
659 * @see get_inline_data()
660 * @see wp_ajax_inline_save()
661 */
662 ?>
663 <input type="hidden" name="_status" value="<?php echo esc_attr( get_post_status( $post->ID ) ); ?>" />
664 <input type="hidden" name="jj" value="<?php echo esc_attr( mysql2date( 'd', $post->post_date, false ) ); ?>" />
665 <input type="hidden" name="mm" value="<?php echo esc_attr( mysql2date( 'm', $post->post_date, false ) ); ?>" />
666 <input type="hidden" name="aa" value="<?php echo esc_attr( mysql2date( 'Y', $post->post_date, false ) ); ?>" />
667 <input type="hidden" name="hh" value="<?php echo esc_attr( mysql2date( 'H', $post->post_date, false ) ); ?>" />
668 <input type="hidden" name="mn" value="<?php echo esc_attr( mysql2date( 'i', $post->post_date, false ) ); ?>" />
669 <input type="hidden" name="ss" value="<?php echo esc_attr( mysql2date( 's', $post->post_date, false ) ); ?>" />
670 <?php
671 endif;
672 }
673
674 /**
675 * Display Submit Box
676 *
677 * @param \WP_Post $post Post object for current alert.
678 *
679 * @return void
680 */
681 public function display_submit_box( $post ) {
682 if ( empty( $post ) ) {
683 return;
684 }
685
686 $post_status = $post->post_status;
687 if ( 'auto-draft' === $post_status ) {
688 $post_status = 'wp_stream_enabled';
689 }
690 ?>
691 <div class="submitbox" id="submitpost">
692 <div id="minor-publishing">
693 <div id="misc-publishing-actions">
694 <div class="misc-pub-section misc-pub-post-status">
695 <label for="wp_stream_alert_status"><?php esc_html_e( 'Status', 'stream' ); ?></label>
696 <select name='wp_stream_alert_status' id='wp_stream_alert_status'>
697 <option<?php selected( $post_status, 'wp_stream_enabled' ); ?>
698 value='wp_stream_enabled'><?php esc_html_e( 'Enabled', 'stream' ); ?></option>
699 <option<?php selected( $post_status, 'wp_stream_disabled' ); ?>
700 value='wp_stream_disabled'><?php esc_html_e( 'Disabled', 'stream' ); ?></option>
701 </select>
702 </div>
703 </div>
704 <div class="clear"></div>
705 </div>
706
707 <div id="major-publishing-actions">
708 <div id="delete-action">
709 <?php
710 if ( current_user_can( 'delete_post', $post->ID ) ) {
711 if ( ! EMPTY_TRASH_DAYS ) {
712 $delete_text = __( 'Delete Permanently', 'stream' );
713 } else {
714 $delete_text = __( 'Move to Trash', 'stream' );
715 }
716 ?>
717 <a class="submitdelete deletion" href="<?php echo get_delete_post_link( $post->ID ); ?>">
718 <?php esc_html( $delete_text ); ?>
719 </a>
720 <?php
721 }
722 ?>
723 </div>
724 <div id="publishing-action">
725 <span class="spinner"></span>
726 <?php submit_button( __( 'Save', 'stream' ), 'primary button-large', 'publish', false ); ?>
727 </div>
728 <div class="clear"></div>
729 </div>
730 </div>
731 <?php
732 }
733
734 /**
735 * Display Status Box
736 *
737 * @return void
738 */
739 public function display_status_box() {
740 ?>
741 <div id="minor-publishing">
742 <div id="misc-publishing-actions">
743 <div class="misc-pub-section misc-pub-post-status">
744 <label for="wp_stream_alert_status">
745 <span class="title"><?php esc_html_e( 'Status:', 'stream' ); ?></span>
746 <span class="input-text-wrap">
747 <select name='wp_stream_alert_status' id='wp_stream_alert_status'>
748 <option selected value='wp_stream_enabled'><?php esc_html_e( 'Enabled', 'stream' ); ?></option>
749 <option value='wp_stream_disabled'><?php esc_html_e( 'Disabled', 'stream' ); ?></option>
750 </select>
751 </span>
752 </label>
753 </div>
754 </div>
755 <div class="clear"></div>
756 </div>
757 <?php
758 }
759
760 /**
761 * Return all notification values
762 *
763 * @return array
764 */
765 public function get_notification_values() {
766 $result = array();
767 $names = wp_list_pluck( $this->alert_types, 'name', 'slug' );
768 foreach ( $names as $slug => $name ) {
769 $result[ $slug ] = $name;
770 }
771
772 return $result;
773 }
774
775 /**
776 * Update actions dropdown options based on the connector selected.
777 */
778 public function get_actions() {
779 if ( ! current_user_can( self::CAPABILITY ) ) {
780 wp_send_json_error(
781 array(
782 'message' => 'You do not have permission to do this.',
783 )
784 );
785 }
786
787 check_ajax_referer( 'stream_get_actions', 'nonce' );
788
789 $connector_name = wp_stream_filter_input( INPUT_POST, 'connector' );
790 $stream_connectors = wp_stream_get_instance()->connectors;
791 if ( ! empty( $connector_name ) ) {
792 if ( isset( $stream_connectors->connectors[ $connector_name ] ) ) {
793 $connector = $stream_connectors->connectors[ $connector_name ];
794 if ( method_exists( $connector, 'get_action_labels' ) ) {
795 $actions = $connector->get_action_labels();
796 }
797 }
798 } else {
799 $actions = $stream_connectors->term_labels['stream_action'];
800 }
801 ksort( $actions );
802 wp_send_json_success( $actions );
803 }
804
805 /**
806 * Save a new alert
807 */
808 public function save_new_alert() {
809 check_ajax_referer( 'save_alert', 'wp_stream_alerts_nonce' );
810
811 if ( ! current_user_can( $this->plugin->admin->settings_cap ) ) {
812 wp_die(
813 esc_html__( "You don't have sufficient privileges to do this action.", 'stream' )
814 );
815 }
816
817 $trigger_author = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_author' );
818 $trigger_connector_and_context = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_context' );
819 if ( false !== strpos( $trigger_connector_and_context, '-' ) ) {
820 // This is a connector with a context such as posts-post.
821 $trigger_connector_and_context_split = explode( '-', $trigger_connector_and_context );
822 $trigger_connector = $trigger_connector_and_context_split[0];
823 $trigger_context = $trigger_connector_and_context_split[1];
824 } elseif ( ! empty( $trigger_connector_and_context ) ) {
825 // This is a parent connector with no dash such as posts.
826 $trigger_connector = $trigger_connector_and_context;
827 $trigger_context = '';
828 } else {
829 // There is no connector or context.
830 $trigger_connector = '';
831 $trigger_context = '';
832 }
833
834 $trigger_action = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_action' );
835 $alert_type = wp_stream_filter_input( INPUT_POST, 'wp_stream_alert_type' );
836 $alert_status = wp_stream_filter_input( INPUT_POST, 'wp_stream_alert_status' );
837
838 // Insert the post into the database.
839 $item = (object) array(
840 'alert_type' => $alert_type,
841 'alert_meta' => array(
842 'trigger_author' => $trigger_author,
843 'trigger_connector' => $trigger_connector,
844 'trigger_action' => $trigger_action,
845 'trigger_context' => $trigger_context,
846 ),
847 'alert_status' => $alert_status,
848 );
849 $alert = new Alert( $item, $this->plugin );
850 $title = $alert->get_title();
851 $post_id = wp_insert_post(
852 array(
853 'post_status' => $alert_status,
854 'post_type' => 'wp_stream_alerts',
855 'post_title' => $title,
856 )
857 );
858 if ( empty( $post_id ) ) {
859 wp_send_json_error();
860 }
861 add_post_meta( $post_id, 'alert_type', $alert_type );
862
863 $alert_meta = array(
864 'trigger_author' => $trigger_author,
865 'trigger_connector' => $trigger_connector,
866 'trigger_action' => $trigger_action,
867 'trigger_context' => $trigger_context,
868 );
869 $alert_meta = apply_filters( 'wp_stream_alerts_save_meta', $alert_meta, $alert_type );
870 add_post_meta( $post_id, 'alert_meta', $alert_meta );
871 wp_send_json_success(
872 array(
873 'success' => true,
874 )
875 );
876 }
877
878 /**
879 * Return HTML string of the Alert page controls.
880 */
881 public function get_new_alert_triggers_notifications() {
882 if ( ! current_user_can( $this->plugin->admin->settings_cap ) ) {
883 wp_die(
884 esc_html__( "You don't have sufficient privileges to do this action.", 'stream' )
885 );
886 }
887
888 ob_start();
889 ?>
890 <fieldset class="inline-edit-col inline-edit-wp_stream_alerts inline-edit-add-new-triggers">
891 <legend class="inline-edit-legend">Add New</legend>
892 <?php $GLOBALS['wp_stream']->alerts->display_triggers_box(); ?>
893 </fieldset>
894 <fieldset class="inline-edit-col inline-edit-wp_stream_alerts inline-edit-add-new-notifications">
895 <?php $GLOBALS['wp_stream']->alerts->display_notification_box(); ?>
896 </fieldset>
897 <fieldset class="inline-edit-col inline-edit-wp_stream_alerts inline-edit-add-new-status">
898 <?php $GLOBALS['wp_stream']->alerts->display_status_box(); ?>
899 </fieldset>
900 <?php
901 $html = ob_get_clean();
902 wp_send_json_success(
903 array(
904 'success' => true,
905 'html' => $html,
906 )
907 );
908 }
909
910 /**
911 * Add action links to Stream drop row in admin list screen
912 *
913 * @filter wp_stream_action_links_{connector}
914 *
915 * @param array $links Previous links registered.
916 * @param Record $record Stream record.
917 *
918 * @return array Action links
919 */
920 public function change_alert_action_links( $links, $record ) {
921 $post = get_post( $record->object_id );
922
923 if ( $post && self::POST_TYPE === $post->post_type && $post->post_status === $record->get_meta( 'new_status', true ) ) {
924 if ( 'trash' !== $post->post_status ) {
925 $connector_posts = new \WP_Stream\Connector_Posts();
926 $post_type_name = $connector_posts->get_post_type_name( get_post_type( $post->ID ) );
927
928 /* translators: %s: the post type singular name (e.g. "Post") */
929 $links[ sprintf( esc_html_x( 'Edit %s', 'Post type singular name', 'stream' ), $post_type_name ) ] = admin_url( 'edit.php?post_type=wp_stream_alerts#post-' . $post->ID );
930 unset( $links[ esc_html__( 'View', 'stream' ) ] );
931 }
932 }
933
934 return $links;
935 }
936 }
937