• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xwp / stream / 5862

pending completion
5862

push

travis-ci-com

schlessera
Adapt version strings and changelog for hotfix release 3.9.3

3922 of 8537 relevant lines covered (45.94%)

4.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.24
/classes/class-alerts.php
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;
27✔
67

68
                // Register custom post type.
69
                add_action( 'init', array( $this, 'register_post_type' ) );
27✔
70

71
                // Add custom post type to menu.
72
                add_action( 'wp_stream_admin_menu', array( $this, 'register_menu' ) );
27✔
73

74
                // Add scripts to post screens.
75
                add_action(
27✔
76
                        'admin_enqueue_scripts',
27✔
77
                        array(
78
                                $this,
27✔
79
                                'register_scripts',
27✔
80
                        )
81
                );
82

83
                add_action(
27✔
84
                        'network_admin_menu',
27✔
85
                        array(
86
                                $this,
27✔
87
                                'change_menu_link_url',
27✔
88
                        ),
89
                        99
27✔
90
                );
91

92
                add_filter(
27✔
93
                        'wp_stream_record_inserted',
27✔
94
                        array(
95
                                $this,
27✔
96
                                'check_records',
27✔
97
                        ),
98
                        10,
27✔
99
                        2
27✔
100
                );
101

102
                add_action(
27✔
103
                        'wp_ajax_load_alerts_settings',
27✔
104
                        array(
105
                                $this,
27✔
106
                                'load_alerts_settings',
27✔
107
                        )
108
                );
109
                add_action( 'wp_ajax_get_actions', array( $this, 'get_actions' ) );
27✔
110
                add_action(
27✔
111
                        'wp_ajax_save_new_alert',
27✔
112
                        array(
113
                                $this,
27✔
114
                                'save_new_alert',
27✔
115
                        )
116
                );
117
                add_action(
27✔
118
                        'wp_ajax_get_new_alert_triggers_notifications',
27✔
119
                        array(
120
                                $this,
27✔
121
                                'get_new_alert_triggers_notifications',
27✔
122
                        )
123
                );
124

125
                $this->load_alert_types();
27✔
126
                $this->load_alert_triggers();
27✔
127

128
                add_filter(
27✔
129
                        'wp_stream_action_links_posts',
27✔
130
                        array(
131
                                $this,
27✔
132
                                'change_alert_action_links',
27✔
133
                        ),
134
                        11,
27✔
135
                        2
27✔
136
                );
137

138
        }
27✔
139

140
        /**
141
         * Load alert_type classes
142
         *
143
         * @return void
144
         */
145
        public function load_alert_types() {
146
                $alert_types = array(
147
                        'none',
27✔
148
                        'highlight',
149
                        'email',
150
                        'ifttt',
151
                        'slack',
152
                );
153

154
                $classes = array();
27✔
155
                foreach ( $alert_types as $alert_type ) {
27✔
156
                        $file_location = $this->plugin->locations['dir'] . '/alerts/class-alert-type-' . $alert_type . '.php';
27✔
157
                        if ( file_exists( $file_location ) ) {
27✔
158
                                include_once $file_location;
27✔
159
                                $class_name = sprintf( '\WP_Stream\Alert_Type_%s', str_replace( '-', '_', $alert_type ) );
27✔
160
                                if ( ! class_exists( $class_name ) ) {
27✔
161
                                        continue;
×
162
                                }
163
                                $class = new $class_name( $this->plugin );
27✔
164
                                if ( ! property_exists( $class, 'slug' ) ) {
27✔
165
                                        continue;
×
166
                                }
167
                                $classes[ $class->slug ] = $class;
27✔
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 );
27✔
177

178
                // Ensure that all alert_types extend Notifier.
179
                foreach ( $this->alert_types as $key => $alert_type ) {
27✔
180
                        if ( ! $this->is_valid_alert_type( $alert_type ) ) {
27✔
181
                                unset( $this->alert_types[ $key ] );
1✔
182
                        }
183
                }
184
        }
27✔
185

186
        /**
187
         * Load alert_type classes
188
         *
189
         * @return void
190
         */
191
        public function load_alert_triggers() {
192
                $alert_triggers = array(
193
                        'author',
27✔
194
                        'context',
195
                        'action',
196
                );
197

198
                $classes = array();
27✔
199
                foreach ( $alert_triggers as $alert_trigger ) {
27✔
200
                        $file_location = $this->plugin->locations['dir'] . '/alerts/class-alert-trigger-' . $alert_trigger . '.php';
27✔
201
                        if ( file_exists( $file_location ) ) {
27✔
202
                                include_once $file_location;
27✔
203
                                $class_name = sprintf( '\WP_Stream\Alert_Trigger_%s', str_replace( '-', '_', $alert_trigger ) );
27✔
204
                                if ( ! class_exists( $class_name ) ) {
27✔
205
                                        continue;
×
206
                                }
207
                                $class = new $class_name( $this->plugin );
27✔
208
                                if ( ! property_exists( $class, 'slug' ) ) {
27✔
209
                                        continue;
×
210
                                }
211
                                $classes[ $class->slug ] = $class;
27✔
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 );
27✔
221

222
                // Ensure that all alert_triggers extend Notifier.
223
                foreach ( $this->alert_triggers as $key => $alert_trigger ) {
27✔
224
                        if ( ! $this->is_valid_alert_trigger( $alert_trigger ) ) {
27✔
225
                                unset( $this->alert_triggers[ $key ] );
1✔
226
                        }
227
                }
228
        }
27✔
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' ) ) {
27✔
239
                        return false;
2✔
240
                }
241

242
                if ( ! method_exists( $alert_type, 'is_dependency_satisfied' ) || ! $alert_type->is_dependency_satisfied() ) {
27✔
243
                        return false;
×
244
                }
245

246
                return true;
27✔
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' ) ) {
27✔
258
                        return false;
2✔
259
                }
260

261
                if ( ! method_exists( $alert_trigger, 'is_dependency_satisfied' ) || ! $alert_trigger->is_dependency_satisfied() ) {
27✔
262
                        return false;
×
263
                }
264

265
                return true;
27✔
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,
103✔
281
                        'post_status' => 'wp_stream_enabled',
103✔
282
                );
283

284
                $alerts = new \WP_Query( $args );
103✔
285
                foreach ( $alerts->posts as $alert ) {
103✔
286
                        $alert = $this->get_alert( $alert->ID );
18✔
287

288
                        if ( false === $alert ) {
18✔
289
                                continue;
×
290
                        }
291

292
                        $status = $alert->check_record( $record_id, $recordarr );
18✔
293
                        if ( $status ) {
18✔
294
                                $alert->send_alert( $record_id, $recordarr ); // @todo send_alert expects int, not array.
10✔
295
                        }
296
                }
297

298
                return $recordarr;
103✔
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' ),
1✔
350
                        'singular_name'      => _x( 'Alert', 'post type singular name', 'stream' ),
1✔
351
                        'menu_name'          => _x( 'Alerts', 'admin menu', 'stream' ),
1✔
352
                        'name_admin_bar'     => _x( 'Alert', 'add new on admin bar', 'stream' ),
1✔
353
                        'add_new'            => _x( 'Add New', 'book', 'stream' ),
1✔
354
                        'add_new_item'       => __( 'Add New Alert', 'stream' ),
1✔
355
                        'new_item'           => __( 'New Alert', 'stream' ),
1✔
356
                        'edit_item'          => __( 'Edit Alert', 'stream' ),
1✔
357
                        'view_item'          => __( 'View Alert', 'stream' ),
1✔
358
                        'all_items'          => __( 'Alerts', 'stream' ),
1✔
359
                        'search_items'       => __( 'Search Alerts', 'stream' ),
1✔
360
                        'parent_item_colon'  => __( 'Parent Alerts:', 'stream' ),
1✔
361
                        'not_found'          => __( 'No alerts found.', 'stream' ),
1✔
362
                        'not_found_in_trash' => __( 'No alerts found in Trash.', 'stream' ),
1✔
363
                );
364

365
                $args = array(
366
                        'labels'              => $labels,
1✔
367
                        'description'         => __( 'Alerts for Stream.', 'stream' ),
1✔
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,
1✔
376
                                'edit_others_posts'   => self::CAPABILITY,
1✔
377
                                'delete_posts'        => self::CAPABILITY,
1✔
378
                                'delete_others_posts' => self::CAPABILITY,
1✔
379
                                'read_private_posts'  => self::CAPABILITY,
1✔
380
                                'edit_post'           => self::CAPABILITY,
1✔
381
                                'delete_post'         => self::CAPABILITY,
1✔
382
                                'read_post'           => self::CAPABILITY,
1✔
383
                        ),
384
                );
385

386
                register_post_type( self::POST_TYPE, $args );
1✔
387

388
                $args = array(
389
                        'label'                     => _x( 'Enabled', 'alert', 'stream' ),
1✔
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' ),
1✔
395
                );
396

397
                register_post_status( 'wp_stream_enabled', $args );
1✔
398

399
                $args = array(
400
                        'label'                     => _x( 'Disabled', 'alert', 'stream' ),
1✔
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' ),
1✔
407
                );
408

409
                register_post_status( 'wp_stream_disabled', $args );
1✔
410
        }
1✔
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 ) {
37✔
421
                        $obj = new Alert( null, $this->plugin );
19✔
422

423
                        return $obj;
19✔
424
                }
425

426
                $post = get_post( $post_id );
18✔
427

428
                $alert_type = get_post_meta( $post_id, 'alert_type', true );
18✔
429
                $alert_meta = get_post_meta( $post_id, 'alert_meta', true );
18✔
430

431
                $obj = (object) array(
432
                        'ID'         => $post->ID,
18✔
433
                        'status'     => $post->post_status,
18✔
434
                        'date'       => $post->post_date,
18✔
435
                        'author'     => $post->post_author,
18✔
436
                        'alert_type' => $alert_type,
18✔
437
                        'alert_meta' => $alert_meta,
18✔
438
                );
439

440
                return new Alert( $obj, $this->plugin );
18✔
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(
1✔
453
                        $this->plugin->admin->records_page_slug,
1✔
454
                        __( 'Alerts', 'stream' ),
1✔
455
                        __( 'Alerts', 'stream' ),
1✔
456
                        self::CAPABILITY,
1✔
457
                        'edit.php?post_type=wp_stream_alerts'
1✔
458
                );
459
        }
1✔
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;
2✔
522
                $alert_type = 'none';
2✔
523
                if ( is_object( $post ) ) {
2✔
524
                        $alert = $this->get_alert( $post->ID );
1✔
525
                        if ( false !== $alert ) {
1✔
526
                                $alert_type = $alert->alert_type;
1✔
527
                        }
528
                }
529
                $form = new Form_Generator();
2✔
530

531
                $field_html = $form->render_field(
2✔
532
                        'select',
2✔
533
                        array(
534
                                'id'          => 'wp_stream_alert_type',
2✔
535
                                'name'        => 'wp_stream_alert_type',
2✔
536
                                'value'       => $alert_type,
2✔
537
                                'options'     => $this->get_notification_values(),
2✔
538
                                'placeholder' => __( 'No Alert', 'stream' ),
2✔
539
                                'title'       => 'Alert Type:',
2✔
540
                        )
541
                );
542

543
                echo '<label>' . esc_html__( 'Alert me by', 'stream' ) . '</label>';
2✔
544
                echo $field_html; // Xss ok.
2✔
545

546
                echo '<div id="wp_stream_alert_type_form">';
2✔
547
                if ( is_object( $alert ) ) {
2✔
548
                        $alert->get_alert_type_obj()->display_fields( $alert );
1✔
549
                } else {
550
                        $this->plugin->alerts->alert_types['none']->display_fields( array() );
1✔
551
                }
552

553
                echo '</div>';
2✔
554
        }
2✔
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 ) ) {
3✔
565
                        wp_send_json_error(
1✔
566
                                array(
567
                                        'message' => 'You do not have permission to do this.',
1✔
568
                                )
569
                        );
570
                }
571
                $alert   = array();
2✔
572
                $post_id = wp_stream_filter_input( INPUT_POST, 'post_id' );
2✔
573
                if ( ! empty( $post_id ) ) {
2✔
574
                        $alert = $this->get_alert( $post_id );
2✔
575
                        if ( false === $alert ) {
2✔
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' );
2✔
585
                if ( empty( $alert_type ) ) {
2✔
586
                        $alert_type = 'none';
1✔
587
                }
588
                if ( ! array_key_exists( $alert_type, $this->alert_types ) ) {
2✔
589
                        wp_send_json_error(
×
590
                                array(
591
                                        'message' => 'Could not find alert type.',
×
592
                                )
593
                        );
594
                }
595

596
                ob_start();
2✔
597
                $this->alert_types[ $alert_type ]->display_fields( $alert );
2✔
598
                $output = ob_get_contents();
2✔
599
                ob_end_clean();
2✔
600

601
                $data = array(
602
                        'html' => $output,
2✔
603
                );
604
                wp_send_json_success( $data );
2✔
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;
2✔
616
                if ( is_object( $post ) ) {
2✔
617
                        $alert = $this->get_alert( $post->ID );
1✔
618
                }
619
                if ( false === $alert ) {
2✔
620
                        $alert = array();
1✔
621
                }
622

623
                $form = new Form_Generator();
2✔
624
                do_action( 'wp_stream_alert_trigger_form_display', $form, $alert );
2✔
625
                // @TODO use human readable text.
626
                echo '<label>' . esc_html__( 'Alert me when', 'stream' ) . '</label>';
2✔
627
                echo $form->render_fields(); // Xss ok.
2✔
628
                wp_nonce_field( 'save_alert', 'wp_stream_alerts_nonce' );
2✔
629
        }
2✔
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 ) ) {
1✔
640
                        return;
×
641
                }
642

643
                $post_status = $post->post_status;
1✔
644
                if ( 'auto-draft' === $post_status ) {
1✔
645
                        $post_status = 'wp_stream_enabled';
×
646
                }
647
                ?>
648
                <div class="submitbox" id="submitpost">
1✔
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 ) ) {
1✔
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>
1✔
681
                                <div id="publishing-action">
682
                                        <span class="spinner"></span>
683
                                        <?php submit_button( __( 'Save' ), 'primary button-large', 'publish', false ); ?>
684
                                </div>
1✔
685
                                <div class="clear"></div>
686
                        </div>
687
                </div>
688
                <?php
689
        }
1✔
690

691
        /**
692
         * Display Status Box
693
         *
694
         * @return void
695
         */
696
        public function display_status_box() {
697
                ?>
698
                <div id="minor-publishing">
1✔
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
        }
1✔
716

717
        /**
718
         * Return all notification values
719
         *
720
         * @return array
721
         */
722
        public function get_notification_values() {
723
                $result = array();
3✔
724
                $names  = wp_list_pluck( $this->alert_types, 'name', 'slug' );
3✔
725
                foreach ( $names as $slug => $name ) {
3✔
726
                        $result[ $slug ] = $name;
3✔
727
                }
728

729
                return $result;
3✔
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' );
1✔
737
                $stream_connectors = wp_stream_get_instance()->connectors;
1✔
738
                if ( ! empty( $connector_name ) ) {
1✔
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'];
1✔
747
                }
748
                ksort( $actions );
1✔
749
                wp_send_json_success( $actions );
1✔
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' );
6✔
757

758
                if ( ! current_user_can( $this->plugin->admin->settings_cap ) ) {
3✔
759
                        wp_die(
1✔
760
                                esc_html__( "You don't have sufficient privileges to do this action.", 'stream' )
1✔
761
                        );
762
                }
763

764
                $trigger_author                = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_author' );
2✔
765
                $trigger_connector_and_context = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_context' );
2✔
766
                if ( false !== strpos( $trigger_connector_and_context, '-' ) ) {
2✔
767
                        // This is a connector with a context such as posts-post.
768
                        $trigger_connector_and_context_split = explode( '-', $trigger_connector_and_context );
1✔
769
                        $trigger_connector                   = $trigger_connector_and_context_split[0];
1✔
770
                        $trigger_context                     = $trigger_connector_and_context_split[1];
1✔
771
                } else {
772
                        if ( ! empty( $trigger_connector_and_context ) ) {
1✔
773
                                // This is a parent connector with no dash such as posts.
774
                                $trigger_connector = $trigger_connector_and_context;
1✔
775
                                $trigger_context   = '';
1✔
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' );
2✔
784
                $alert_type     = wp_stream_filter_input( INPUT_POST, 'wp_stream_alert_type' );
2✔
785
                $alert_status   = wp_stream_filter_input( INPUT_POST, 'wp_stream_alert_status' );
2✔
786

787
                // Insert the post into the database.
788
                $item    = (object) array(
789
                        'alert_type'   => $alert_type,
2✔
790
                        'alert_meta'   => array(
791
                                'trigger_author'    => $trigger_author,
2✔
792
                                'trigger_connector' => $trigger_connector,
2✔
793
                                'trigger_action'    => $trigger_action,
2✔
794
                                'trigger_context'   => $trigger_context,
2✔
795
                        ),
796
                        'alert_status' => $alert_status,
2✔
797
                );
798
                $alert   = new Alert( $item, $this->plugin );
2✔
799
                $title   = $alert->get_title();
2✔
800
                $post_id = wp_insert_post(
2✔
801
                        array(
802
                                'post_status' => $alert_status,
2✔
803
                                'post_type'   => 'wp_stream_alerts',
2✔
804
                                'post_title'  => $title,
2✔
805
                        )
806
                );
807
                if ( empty( $post_id ) ) {
2✔
808
                        wp_send_json_error();
×
809
                }
810
                add_post_meta( $post_id, 'alert_type', $alert_type );
2✔
811

812
                $alert_meta = array(
813
                        'trigger_author'    => $trigger_author,
2✔
814
                        'trigger_connector' => $trigger_connector,
2✔
815
                        'trigger_action'    => $trigger_action,
2✔
816
                        'trigger_context'   => $trigger_context,
2✔
817
                );
818
                $alert_meta = apply_filters( 'wp_stream_alerts_save_meta', $alert_meta, $alert_type );
2✔
819
                add_post_meta( $post_id, 'alert_meta', $alert_meta );
2✔
820
                wp_send_json_success(
2✔
821
                        array(
822
                                'success' => true,
2✔
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 ) ) {
2✔
832
                        wp_die(
1✔
833
                                esc_html__( "You don't have sufficient privileges to do this action.", 'stream' )
1✔
834
                        );
835
                }
836

837
                ob_start();
1✔
838
                ?>
839
                <fieldset class="inline-edit-col inline-edit-wp_stream_alerts inline-edit-add-new-triggers">
1✔
840
                        <legend class="inline-edit-legend">Add New</legend>
841
                        <?php $GLOBALS['wp_stream']->alerts->display_triggers_box(); ?>
842
                </fieldset>
1✔
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>
1✔
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>
1✔
849
                <?php
850
                $html = ob_get_clean();
1✔
851
                wp_send_json_success(
1✔
852
                        array(
853
                                'success' => true,
1✔
854
                                'html'    => $html,
1✔
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 );
1✔
871

872
                if ( $post && self::POST_TYPE === $post->post_type && $post->post_status === $record->get_meta( 'new_status', true ) ) {
1✔
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;
1✔
884
        }
885
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc