| @@ -19,28 +19,13 @@ | ||
| 19 | 19 | */ |
| 20 | 20 | const POST_TYPE = 'wp_stream_alerts'; |
| 21 | 21 | |
| 22 | 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 | 23 | * Triggered Alerts meta key for Records |
| 34 | 24 | */ |
| 35 | 25 | const ALERTS_TRIGGERED_META_KEY = 'wp_stream_alerts_triggered'; |
| 36 | 26 | |
| 37 | 27 | /** |
| 38 | - * Capability required to access alerts. | |
| 39 | - */ | |
| 40 | - const CAPABILITY = WP_STREAM_SETTINGS_CAPABILITY; | |
| 41 | - | |
| 42 | - /** | |
| 43 | 28 | * Holds Instance of plugin object |
| 44 | 29 | * |
| 45 | 30 | * @var Plugin |
| 46 | 31 | */ |
| @@ -143,8 +128,9 @@ | ||
| 143 | 128 | ), |
| 144 | 129 | 11, |
| 145 | 130 | 2 |
| 146 | 131 | ); |
| 132 | + | |
| 147 | 133 | } |
| 148 | 134 | |
| 149 | 135 | /** |
| 150 | 136 | * Load alert_type classes |
| @@ -293,12 +279,8 @@ | ||
| 293 | 279 | $alerts = new \WP_Query( $args ); |
| 294 | 280 | foreach ( $alerts->posts as $alert ) { |
| 295 | 281 | $alert = $this->get_alert( $alert->ID ); |
| 296 | 282 | |
| 297 | - if ( false === $alert ) { | |
| 298 | - continue; | |
| 299 | - } | |
| 300 | - | |
| 301 | 283 | $status = $alert->check_record( $record_id, $recordarr ); |
| 302 | 284 | if ( $status ) { |
| 303 | 285 | $alert->send_alert( $record_id, $recordarr ); // @todo send_alert expects int, not array. |
| 304 | 286 | } |
| @@ -304,8 +286,9 @@ | ||
| 304 | 286 | } |
| 305 | 287 | } |
| 306 | 288 | |
| 307 | 289 | return $recordarr; |
| 290 | + | |
| 308 | 291 | } |
| 309 | 292 | |
| 310 | 293 | /** |
| 311 | 294 | * Register scripts for page load |
| @@ -315,24 +298,35 @@ | ||
| 315 | 298 | * @return void |
| 316 | 299 | */ |
| 317 | 300 | public function register_scripts() { |
| 318 | 301 | $screen = get_current_screen(); |
| 319 | - if ( 'edit-wp_stream_alerts' !== $screen->id ) { | |
| 320 | - return; | |
| 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' ); | |
| 321 | 328 | } |
| 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 | 329 | } |
| 336 | 330 | |
| 337 | 331 | /** |
| 338 | 332 | * Register custom post type |
| @@ -363,22 +357,21 @@ | ||
| 363 | 357 | 'labels' => $labels, |
| 364 | 358 | 'description' => __( 'Alerts for Stream.', 'stream' ), |
| 365 | 359 | 'public' => false, |
| 366 | 360 | 'publicly_queryable' => false, |
| 367 | - 'rewrite' => false, | |
| 368 | 361 | 'exclude_from_search' => true, |
| 369 | 362 | 'show_ui' => true, |
| 370 | 363 | 'show_in_menu' => false, // @see modify_admin_menu |
| 371 | 364 | 'supports' => false, |
| 372 | 365 | '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, | |
| 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', | |
| 381 | 374 | ), |
| 382 | 375 | ); |
| 383 | 376 | |
| 384 | 377 | register_post_type( self::POST_TYPE, $args ); |
| @@ -409,21 +402,20 @@ | ||
| 409 | 402 | |
| 410 | 403 | /** |
| 411 | 404 | * Return alert object of the given ID |
| 412 | 405 | * |
| 413 | - * @param string|int $post_id Post ID for the alert. | |
| 406 | + * @param string $post_id Post ID for the alert. | |
| 414 | 407 | * |
| 415 | 408 | * @return Alert |
| 416 | 409 | */ |
| 417 | 410 | public function get_alert( $post_id = '' ) { |
| 418 | 411 | if ( ! $post_id ) { |
| 419 | - return new Alert( null, $this->plugin ); | |
| 412 | + $obj = new Alert( null, $this->plugin ); | |
| 413 | + | |
| 414 | + return $obj; | |
| 420 | 415 | } |
| 421 | 416 | |
| 422 | 417 | $post = get_post( $post_id ); |
| 423 | - if ( ! ( $post instanceof \WP_Post ) ) { | |
| 424 | - return new Alert( null, $this->plugin ); | |
| 425 | - } | |
| 426 | 418 | |
| 427 | 419 | $alert_type = get_post_meta( $post_id, 'alert_type', true ); |
| 428 | 420 | $alert_meta = get_post_meta( $post_id, 'alert_meta', true ); |
| 429 | 421 | |
| @@ -436,37 +428,9 @@ | ||
| 436 | 428 | 'alert_meta' => $alert_meta, |
| 437 | 429 | ); |
| 438 | 430 | |
| 439 | 431 | return new Alert( $obj, $this->plugin ); |
| 440 | - } | |
| 441 | 432 | |
| 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 | 433 | } |
| 470 | 434 | |
| 471 | 435 | /** |
| 472 | 436 | * Add custom post type to menu |
| @@ -479,9 +443,9 @@ | ||
| 479 | 443 | add_submenu_page( |
| 480 | 444 | $this->plugin->admin->records_page_slug, |
| 481 | 445 | __( 'Alerts', 'stream' ), |
| 482 | 446 | __( 'Alerts', 'stream' ), |
| 483 | - self::CAPABILITY, | |
| 447 | + 'manage_options', | |
| 484 | 448 | 'edit.php?post_type=wp_stream_alerts' |
| 485 | 449 | ); |
| 486 | 450 | } |
| 487 | 451 | |
| @@ -544,20 +508,16 @@ | ||
| 544 | 508 | * |
| 545 | 509 | * @return void |
| 546 | 510 | */ |
| 547 | 511 | public function display_notification_box( $post = array() ) { |
| 548 | - $alert = null; | |
| 549 | 512 | $alert_type = 'none'; |
| 550 | 513 | if ( is_object( $post ) ) { |
| 551 | - $alert = $this->get_alert( $post->ID ); | |
| 552 | - if ( false !== $alert ) { | |
| 553 | - $alert_type = $alert->alert_type; | |
| 554 | - } | |
| 514 | + $alert = $this->get_alert( $post->ID ); | |
| 515 | + $alert_type = $alert->alert_type; | |
| 555 | 516 | } |
| 556 | 517 | $form = new Form_Generator(); |
| 557 | 518 | |
| 558 | - echo '<label>' . esc_html__( 'Alert me by', 'stream' ) . '</label>'; | |
| 559 | - $form->render_field( | |
| 519 | + $field_html = $form->render_field( | |
| 560 | 520 | 'select', |
| 561 | 521 | array( |
| 562 | 522 | 'id' => 'wp_stream_alert_type', |
| 563 | 523 | 'name' => 'wp_stream_alert_type', |
| @@ -567,8 +527,11 @@ | ||
| 567 | 527 | 'title' => 'Alert Type:', |
| 568 | 528 | ) |
| 569 | 529 | ); |
| 570 | 530 | |
| 531 | + echo '<label>' . esc_html__( 'Alert me by', 'stream' ) . '</label>'; | |
| 532 | + echo $field_html; // Xss ok. | |
| 533 | + | |
| 571 | 534 | echo '<div id="wp_stream_alert_type_form">'; |
| 572 | 535 | if ( is_object( $alert ) ) { |
| 573 | 536 | $alert->get_alert_type_obj()->display_fields( $alert ); |
| 574 | 537 | } else { |
| @@ -585,20 +548,13 @@ | ||
| 585 | 548 | * |
| 586 | 549 | * @return void |
| 587 | 550 | */ |
| 588 | 551 | 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 | 552 | $alert = array(); |
| 597 | 553 | $post_id = wp_stream_filter_input( INPUT_POST, 'post_id' ); |
| 598 | - if ( ! empty( $post_id ) && 'new' !== $post_id ) { | |
| 554 | + if ( ! empty( $post_id ) ) { | |
| 599 | 555 | $alert = $this->get_alert( $post_id ); |
| 600 | - if ( false === $alert ) { | |
| 556 | + if ( ! $alert ) { | |
| 601 | 557 | wp_send_json_error( |
| 602 | 558 | array( |
| 603 | 559 | 'message' => 'Could not find alert.', |
| 604 | 560 | ) |
| @@ -636,40 +592,19 @@ | ||
| 636 | 592 | * |
| 637 | 593 | * @return void |
| 638 | 594 | */ |
| 639 | 595 | public function display_triggers_box( $post = array() ) { |
| 640 | - $alert = false; | |
| 641 | 596 | if ( is_object( $post ) ) { |
| 642 | 597 | $alert = $this->get_alert( $post->ID ); |
| 643 | - } | |
| 644 | - if ( false === $alert ) { | |
| 598 | + } else { | |
| 645 | 599 | $alert = array(); |
| 646 | 600 | } |
| 647 | - | |
| 648 | 601 | $form = new Form_Generator(); |
| 649 | 602 | do_action( 'wp_stream_alert_trigger_form_display', $form, $alert ); |
| 650 | 603 | // @TODO use human readable text. |
| 651 | 604 | echo '<label>' . esc_html__( 'Alert me when', 'stream' ) . '</label>'; |
| 652 | - $form->render_fields(); | |
| 605 | + echo $form->render_fields(); // Xss ok. | |
| 653 | 606 | 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 | 607 | } |
| 673 | 608 | |
| 674 | 609 | /** |
| 675 | 610 | * Display Submit Box |
| @@ -722,9 +657,9 @@ | ||
| 722 | 657 | ?> |
| 723 | 658 | </div> |
| 724 | 659 | <div id="publishing-action"> |
| 725 | 660 | <span class="spinner"></span> |
| 726 | - <?php submit_button( __( 'Save', 'stream' ), 'primary button-large', 'publish', false ); ?> | |
| 661 | + <?php submit_button( __( 'Save' ), 'primary button-large', 'publish', false ); ?> | |
| 727 | 662 | </div> |
| 728 | 663 | <div class="clear"></div> |
| 729 | 664 | </div> |
| 730 | 665 | </div> |
| @@ -775,18 +710,8 @@ | ||
| 775 | 710 | /** |
| 776 | 711 | * Update actions dropdown options based on the connector selected. |
| 777 | 712 | */ |
| 778 | 713 | 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 | 714 | $connector_name = wp_stream_filter_input( INPUT_POST, 'connector' ); |
| 790 | 715 | $stream_connectors = wp_stream_get_instance()->connectors; |
| 791 | 716 | if ( ! empty( $connector_name ) ) { |
| 792 | 717 | if ( isset( $stream_connectors->connectors[ $connector_name ] ) ) { |
| @@ -806,15 +731,8 @@ | ||
| 806 | 731 | * Save a new alert |
| 807 | 732 | */ |
| 808 | 733 | public function save_new_alert() { |
| 809 | 734 | 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 | 735 | $trigger_author = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_author' ); |
| 818 | 736 | $trigger_connector_and_context = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_context' ); |
| 819 | 737 | if ( false !== strpos( $trigger_connector_and_context, '-' ) ) { |
| 820 | 738 | // This is a connector with a context such as posts-post. |
| @@ -820,16 +738,18 @@ | ||
| 820 | 738 | // This is a connector with a context such as posts-post. |
| 821 | 739 | $trigger_connector_and_context_split = explode( '-', $trigger_connector_and_context ); |
| 822 | 740 | $trigger_connector = $trigger_connector_and_context_split[0]; |
| 823 | 741 | $trigger_context = $trigger_connector_and_context_split[1]; |
| 824 | - } elseif ( ! empty( $trigger_connector_and_context ) ) { | |
| 742 | + } else { | |
| 743 | + if ( ! empty( $trigger_connector_and_context ) ) { | |
| 825 | 744 | // This is a parent connector with no dash such as posts. |
| 826 | 745 | $trigger_connector = $trigger_connector_and_context; |
| 827 | 746 | $trigger_context = ''; |
| 828 | - } else { | |
| 829 | - // There is no connector or context. | |
| 830 | - $trigger_connector = ''; | |
| 831 | - $trigger_context = ''; | |
| 747 | + } else { | |
| 748 | + // There is no connector or context. | |
| 749 | + $trigger_connector = ''; | |
| 750 | + $trigger_context = ''; | |
| 751 | + } | |
| 832 | 752 | } |
| 833 | 753 | |
| 834 | 754 | $trigger_action = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_action' ); |
| 835 | 755 | $alert_type = wp_stream_filter_input( INPUT_POST, 'wp_stream_alert_type' ); |
| @@ -878,14 +798,8 @@ | ||
| 878 | 798 | /** |
| 879 | 799 | * Return HTML string of the Alert page controls. |
| 880 | 800 | */ |
| 881 | 801 | 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 | 802 | ob_start(); |
| 889 | 803 | ?> |
| 890 | 804 | <fieldset class="inline-edit-col inline-edit-wp_stream_alerts inline-edit-add-new-triggers"> |
| 891 | 805 | <legend class="inline-edit-legend">Add New</legend> |