PluginProbe
Stream – Activity Log & Audit Trail / 3.9.0
Stream – Activity Log & Audit Trail v3.9.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 3.9.0, at classes/class-alerts.php

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