PluginProbe
Stream – Activity Log & Audit Trail / 4.0.0
Stream – Activity Log & Audit Trail v4.0.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.php

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

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