From 8ac7819d8ffbb46f472410bbcecad1d65ca13761 Mon Sep 17 00:00:00 2001 From: Chad Riston Denaux Date: Mon, 21 Dec 2020 13:14:39 -0500 Subject: [PATCH 1/2] fix: add support for custom post type move to trash hook. --- includes/admin/class-bnfw-notification.php | 2436 +++++++++++++------- 1 file changed, 1608 insertions(+), 828 deletions(-) diff --git a/includes/admin/class-bnfw-notification.php b/includes/admin/class-bnfw-notification.php index 462188c..e8ac76f 100755 --- a/includes/admin/class-bnfw-notification.php +++ b/includes/admin/class-bnfw-notification.php @@ -1,857 +1,1637 @@ array( - 'name' => __( 'Notifications', 'bnfw' ), - 'singular_name' => __( 'Notification', 'bnfw' ), - 'add_new' => __( 'Add New', 'bnfw' ), - 'menu_name' => __( 'Notifications', 'bnfw' ), - 'name_admin_bar' => __( 'Notifications', 'bnfw' ), - 'add_new_item' => __( 'Add New Notification', 'bnfw' ), - 'edit_item' => __( 'Edit Notification', 'bnfw' ), - 'new_item' => __( 'New Notification', 'bnfw' ), - 'view_item' => __( 'View Notification', 'bnfw' ), - 'search_items' => __( 'Search Notifications', 'bnfw' ), - 'not_found' => __( 'No Notifications found', 'bnfw' ), - 'not_found_in_trash' => __( 'No Notifications found in trash', 'bnfw' ), - 'all_items' => __( 'All Notifications', 'bnfw' ) - ), - 'public' => false, - 'show_in_nav_menus' => true, - 'show_in_admin_bar' => true, - 'has_archive' => false, - 'show_ui' => true, - 'show_in_menu' => true, - 'menu_icon' => 'dashicons-email-alt', - 'menu_position' => 100, - 'rewrite' => false, - 'map_meta_cap' => false, - 'capabilities' => array( - - // meta caps (don't assign these to roles) - 'edit_post' => 'manage_options', - 'read_post' => 'manage_options', - 'delete_post' => 'manage_options', - - // primitive/meta caps - 'create_posts' => 'manage_options', - - // primitive caps used outside of map_meta_cap() - 'edit_posts' => 'manage_options', - 'edit_others_posts' => 'manage_options', - 'publish_posts' => 'manage_options', - 'read_private_posts' => 'manage_options', - - // primitive caps used inside of map_meta_cap() - 'read' => 'manage_options', - 'delete_posts' => 'manage_options', - 'delete_private_posts' => 'manage_options', - 'delete_published_posts' => 'manage_options', - 'delete_others_posts' => 'manage_options', - 'edit_private_posts' => 'manage_options', - 'edit_published_posts' => 'manage_options', - ), - - // What features the post type supports. - 'supports' => array( - 'title', - ), - ) ); - } - - /** - * Remove unwanted meta boxes. - * - * @since 1.0 - */ - public function remove_meta_boxes() { - remove_meta_box( 'submitdiv', self::POST_TYPE, 'side' ); - } - - /** - * Add meta box to the post editor screen. - * - * @since 1.0 - */ - public function add_meta_boxes() { - add_meta_box( - 'bnfw-post-notification', // Unique ID - esc_html__( 'Notification Settings', 'bnfw' ), // Title - array( $this, 'render_settings_meta_box' ), // Callback function - self::POST_TYPE, // Admin page (or post type) - 'normal' // Context - ); - - add_meta_box( - 'bnfw_submitdiv', - __( 'Save Notification', 'bnfw' ), - array( $this, 'render_submitdiv' ), - self::POST_TYPE, - 'side', - 'core' - ); - } - - /** - * Render the settings meta box. - * - * @since 1.0 - * @param unknown $post - */ - public function render_settings_meta_box( $post ) { - wp_nonce_field( - // Action - self::POST_TYPE, - // Name. - self::POST_TYPE . '_nonce' - ); - - $setting = $this->read_settings( $post->ID ); -?> + const POST_TYPE = 'bnfw_notification'; + const META_KEY_PREFIX = 'bnfw_'; + const TEST_MAIL_ARG = 'test-mail'; + + /** + * + */ + public function __construct() { + add_action( 'init', array( $this, 'register_post_type' ) ); + add_action( 'do_meta_boxes', array( $this, 'remove_meta_boxes' ) ); + add_action( 'add_meta_boxes_' . self::POST_TYPE, array( $this, 'add_meta_boxes' ) ); + add_action( 'save_post', array( $this, 'save_meta_data' ) ); + add_action( 'edit_form_top', array( $this, 'admin_notices' ) ); + add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) ); + + add_filter( 'use_block_editor_for_post_type', array( $this, 'disable_gutenberg_for_notification' ), 10, 2 ); + + add_filter( 'bulk_actions-edit-bnfw_notification', array( $this, 'add_custom_edit_action' ) ); + add_filter( 'handle_bulk_actions-edit-bnfw_notification', array( $this, 'handle_custom_edit_action' ), 10, 3 ); + + // Custom row actions. + add_filter( 'post_row_actions', array( $this, 'custom_row_actions' ), 10, 2 ); + add_action( 'admin_init', array( $this, 'handle_actions' ) ); + + // Custom columns + add_filter( sprintf( 'manage_%s_posts_columns', self::POST_TYPE ), array( $this, 'columns_header' ) ); + add_action( sprintf( 'manage_%s_posts_custom_column', self::POST_TYPE ), array( $this, 'custom_column_row' ), 10, 2 ); + + // Enqueue scripts/styles and disables autosave for this post type. + add_action( 'admin_enqueue_scripts', array( $this, 'is_assets_needed' ) ); + + add_action( 'admin_notices', array( $this, 'show_help_notice' ) ); + } + + /** + * Register bnfw_notification custom post type. + * + * @since 1.0 + */ + public function register_post_type() { + register_post_type( self::POST_TYPE, array( + 'labels' => array( + 'name' => esc_html__( 'Notifications', 'bnfw' ), + 'singular_name' => esc_html__( 'Notification', 'bnfw' ), + 'add_new' => esc_html__( 'Add New', 'bnfw' ), + 'menu_name' => esc_html__( 'Notifications', 'bnfw' ), + 'name_admin_bar' => esc_html__( 'Notifications', 'bnfw' ), + 'add_new_item' => esc_html__( 'Add New Notification', 'bnfw' ), + 'edit_item' => esc_html__( 'Edit Notification', 'bnfw' ), + 'new_item' => esc_html__( 'New Notification', 'bnfw' ), + 'view_item' => esc_html__( 'View Notification', 'bnfw' ), + 'search_items' => esc_html__( 'Search Notifications', 'bnfw' ), + 'not_found' => esc_html__( 'No Notifications found', 'bnfw' ), + 'not_found_in_trash' => esc_html__( 'No Notifications found in trash', 'bnfw' ), + 'all_items' => esc_html__( 'All Notifications', 'bnfw' ), + ), + 'public' => false, + 'show_in_nav_menus' => true, + 'show_in_admin_bar' => true, + 'has_archive' => false, + 'show_ui' => true, + 'show_in_menu' => true, + 'menu_icon' => 'dashicons-email-alt', + 'menu_position' => 101, + 'rewrite' => false, + 'map_meta_cap' => false, + 'capabilities' => array( + + // meta caps (don't assign these to roles) + 'edit_post' => 'bnfw', + 'read_post' => 'bnfw', + 'delete_post' => 'bnfw', + + // primitive/meta caps + 'create_posts' => 'bnfw', + + // primitive caps used outside of map_meta_cap() + 'edit_posts' => 'bnfw', + 'edit_others_posts' => 'bnfw', + 'publish_posts' => 'bnfw', + 'read_private_posts' => 'bnfw', + + // primitive caps used inside of map_meta_cap() + 'read' => 'bnfw', + 'delete_posts' => 'bnfw', + 'delete_private_posts' => 'bnfw', + 'delete_published_posts' => 'bnfw', + 'delete_others_posts' => 'bnfw', + 'edit_private_posts' => 'bnfw', + 'edit_published_posts' => 'bnfw', + ), + + // What features the post type supports. + 'supports' => array( + 'title', + ), + ) ); + } + + /** + * Remove unwanted meta boxes. + * + * @since 1.0 + */ + public function remove_meta_boxes() { + remove_meta_box( 'submitdiv', self::POST_TYPE, 'side' ); + remove_meta_box( 'slugdiv', self::POST_TYPE, 'normal' ); + } + + /** + * Add meta box to the post editor screen. + * + * @since 1.0 + */ + public function add_meta_boxes() { + global $post; + + add_meta_box( + 'bnfw-post-notification', // Unique ID + esc_html__( 'Notification Settings', 'bnfw' ), // Title + array( $this, 'render_settings_meta_box' ), // Callback function + self::POST_TYPE, // Admin page (or post type) + 'normal', // Context + 'default' + ); + + add_meta_box( + 'bnfw_submitdiv', + __( 'Save Notification', 'bnfw' ), + array( $this, 'render_submitdiv' ), + self::POST_TYPE, + 'side', + 'core' + ); + + if ( self::POST_TYPE !== get_post_type( $post ) ) { + return; + } + + do_action( 'bnfw_after_metaboxes', $this->read_settings( $post->ID ) ); + } + + /** + * Disable Gutenberg for notifications. + * + * @param bool $is_enabled Is Gutenberg enabled? + * @param string $post_type Post Type. + * + * @return bool Should Gutenberg be enabled? + */ + public function disable_gutenberg_for_notification( $is_enabled, $post_type ) { + if ( self::POST_TYPE === $post_type ) { + return false; + } + + return $is_enabled; + } + + /** + * Render the settings meta box. + * + * @since 1.0 + * + * @param WP_Post $post + */ + public function render_settings_meta_box( $post ) { + global $wp_version; + + wp_nonce_field( self::POST_TYPE, self::POST_TYPE . '_nonce' ); + + $setting = $this->read_settings( $post->ID ); + ?> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
  -
-

-
-
- - - - - -
- - - > - -
- - - - -
- - - -
- - - -
- - - -
- -
- - - -
- - - false ) ); ?> -

-
- - -get_names(); + + + + +

+ + + + + + + + + +   + +
+

+
+ + + + + + +

+ + + + + + + + + + + + + +

+ + + > + + + + + + + + +

+ + + + + + + + + + + +

+ + + + + + + + + + +

+ + + + + + + + + + +

+ + + + + + + + + + + + +

+ + + + + + + + + + + +   +

+ + + + + + + + + + + +

+ + + + + + + + + +
+

+ +

+
+ + + + + + + should_show_users_count_msg( $setting ) ) { + $display = 'table-row'; + } + ?> + +   + +
+

+ +

+
+ + + + + + + + + + + + + + + + + + + + + + + +
+

+
+
+
+
+ +

+ $value ) { - update_post_meta( $post_id, self::META_KEY_PREFIX . $key, $value ); - } - } - - /** - * Read settings from post meta. - * - * @since 1.0 - * @param unknown $post_id - * @return unknown - */ - public function read_settings( $post_id ) { - $setting = array(); - $default = array( - 'notification' => '', - 'from-name' => '', - 'from-email' => '', - 'cc' => array(), - 'bcc' => array(), - 'users' => array(), - 'subject' => '', - 'email-formatting' => 'html', - 'message' => '', - 'show-fields' => 'false', - 'disable-current-user' => 'false', - 'disabled' => 'false', - ); - - foreach ( $default as $key => $default_value ) { - $value = get_post_meta( $post_id, self::META_KEY_PREFIX . $key, true ); - if ( ! empty( $value ) ) { - $setting[ $key ] = $value; - } else { - $setting[ $key ] = $default_value; - } - } - - // compatibility code. This will be removed subsequently - $user_roles = get_post_meta( $post_id, self::META_KEY_PREFIX . 'user-roles', true ); - if ( ! empty( $user_roles ) && is_array( $user_roles ) ) { - foreach ( $user_roles as $role ) { - $setting['users'][] = 'role-' . $role; - } - - update_post_meta( $post_id, self::META_KEY_PREFIX . 'users', $setting['users'] ); - delete_post_meta( $post_id, self::META_KEY_PREFIX . 'user-roles' ); - } - - return $setting; - } - - /** - * Change the post updated message for notification post type. - * - * @since 1.0 - * @param unknown $messages - * @return unknown - */ - public function post_updated_messages( $messages ) { - $messages[ self::POST_TYPE ] = array_fill( 0, 11, __( 'Notification saved.', 'bnfw' ) ); - - return $messages; - } - - /** - * Render submit div meta box. - * - * @since 1.0 - * @param unknown $post - */ - public function render_submitdiv( $post ) { - global $post; -?> -
- - -
- -
+ ?> +

+ +

+

+ +

+

+ +

+
+ + + true ) ); ?> +

 

+
+ +
+ + + + + + enqueue_assets(); + + do_action( 'bnfw_after_enqueue_scripts' ); + } + } + + /** + * Enqueue assets. + * + * @since 1.4 + */ + public function enqueue_assets() { + wp_deregister_script( 'select2' ); + wp_dequeue_script( 'select2' ); + wp_deregister_style( 'select2' ); + wp_dequeue_style( 'select2' ); + + // Ultimate Member plugin is giving us problems. They should upgrade + wp_deregister_script( 'um_minified' ); + wp_dequeue_script( 'um_minified' ); + wp_deregister_script( 'um_admin_scripts' ); + wp_dequeue_script( 'um_admin_scripts' ); + + wp_enqueue_style( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css', array(), '4.0.3' ); + wp_enqueue_script( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.full.min.js', array( 'jquery' ), '4.0.3', true ); + + wp_enqueue_script( 'bnfw', plugins_url( '../assets/js/bnfw.js', dirname( __FILE__ ) ), array( 'select2' ), '0.1', true ); + wp_enqueue_style( 'bnfw', plugins_url( '../assets/css/bnfw.css', dirname( __FILE__ ) ), array( 'dashicons', 'select2' ), '0.1' ); + + $strings = array( + 'validation_element' => apply_filters( 'bnfw_validation_element', '#users-select' ), + 'empty_user' => esc_html__( 'You must choose at least one User or User Role to send the notification to before you can save', 'bnfw' ), + 'enableTags' => false, + ); + + /** + * Filter the localized array that is sent to scripts. + * + * @since 1.7.0 + */ + $strings = apply_filters( 'bnfw_localize_script', $strings ); + + wp_localize_script( 'bnfw', 'BNFW', $strings ); + } + + /** + * Save the meta box's post metadata. + * + * @since 1.0 + * + * @param int $post_id The ID of the post being saved. + */ + public function save_meta_data( $post_id ) { + if ( self::POST_TYPE !== get_post_type( $post_id ) ) { + return; + } + + // Check nonce. + if ( empty( $_POST[ self::POST_TYPE . '_nonce' ] ) ) { + return; + } + + // Verify nonce. + if ( ! wp_verify_nonce( $_POST[ self::POST_TYPE . '_nonce' ], self::POST_TYPE ) ) { + return; + } + + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { + return; + } + + if ( ! current_user_can( 'bnfw' ) ) { + return; + } + + if ( isset( $_POST['digest-interval'] ) && 'no' != $_POST['digest-interval']) { + $subject = $_POST['subject']; + }else{ + $subject = sanitize_text_field( $_POST['subject'] ); + } + + $setting = array( + 'notification' => sanitize_text_field( $_POST['notification'] ), + 'subject' => $subject, + 'message' => $_POST['notification_message'], + 'disabled' => isset( $_POST['disabled'] ) ? sanitize_text_field( $_POST['disabled'] ) : 'false', + 'email-formatting' => isset( $_POST['email-formatting'] ) ? sanitize_text_field( $_POST['email-formatting'] ) : 'html', + 'disable-current-user' => isset( $_POST['disable-current-user'] ) ? sanitize_text_field( $_POST['disable-current-user'] ) : 'false', + 'disable-autop' => isset( $_POST['disable-autop'] ) ? sanitize_text_field( $_POST['disable-autop'] ) : 'false', + 'only-post-author' => isset( $_POST['only-post-author'] ) ? sanitize_text_field( $_POST['only-post-author'] ) : 'false', + 'users' => array(), + 'exclude-users' => array(), + ); + + if ( isset( $_POST['users'] ) ) { + $setting['users'] = array_map( 'sanitize_text_field', $_POST['users'] ); + } + + if ( isset( $_POST['exclude-users'] ) ) { + $setting['exclude-users'] = array_map( 'sanitize_text_field', $_POST['exclude-users'] ); + } + + if ( isset( $_POST['show-fields'] ) && 'true' == $_POST['show-fields'] ) { + $setting['show-fields'] = 'true'; + $setting['from-name'] = sanitize_text_field( $_POST['from-name'] ); + $setting['from-email'] = sanitize_text_field( $_POST['from-email'] ); + $setting['reply-name'] = sanitize_text_field( $_POST['reply-name'] ); + $setting['reply-email'] = sanitize_text_field( $_POST['reply-email'] ); + $setting['cc'] = isset( $_POST['cc'] ) ? array_map( 'sanitize_text_field', $_POST['cc'] ) : ''; + $setting['bcc'] = isset( $_POST['bcc'] ) ? array_map( 'sanitize_text_field', $_POST['bcc'] ) : ''; + } else { + $setting['show-fields'] = 'false'; + } + + $setting = apply_filters( 'bnfw_notification_setting', $setting, $_POST ); + + $this->save_settings( $post_id, $setting ); + + if ( isset( $_POST['send-test-email'] ) ) { + if ( 'true' == sanitize_text_field( $_POST['send-test-email'] ) ) { + BNFW::factory()->engine->send_test_email( $setting ); + add_filter( 'redirect_post_location', array( $this, 'test_mail_sent' ) ); + } + } + } + + /** + * Add a query parameter to url if test email was sent. + * + * @since 1.3 + */ + public function test_mail_sent( $loc ) { + return add_query_arg( self::TEST_MAIL_ARG, 1, $loc ); + } + + /** + * Add a notification if a test email was sent. + * + * @since 1.3 + */ + public function admin_notices() { + if ( isset( $_GET[ self::TEST_MAIL_ARG ] ) ) { + $screen = get_current_screen(); + if ( in_array( $screen->post_type, array( self::POST_TYPE ) ) ) { + ?> +
+

+
+ $value ) { + update_post_meta( $post_id, self::META_KEY_PREFIX . $key, $value ); + } + } + + /** + * Read settings from post meta. + * + * @since 1.0 + * + * @param int $post_id + * + * @return array + */ + public function read_settings( $post_id ) { + $setting = array(); + $default = array( + 'notification' => '', + 'from-name' => '', + 'from-email' => '', + 'reply-name' => '', + 'reply-email' => '', + 'cc' => array(), + 'bcc' => array(), + 'users' => array(), + 'exclude-users' => array(), + 'subject' => '', + 'email-formatting' => get_option( 'bnfw_email_format', 'html' ), + 'message' => '', + 'show-fields' => 'false', + 'disable-current-user' => 'false', + 'disable-autop' => 'false', + 'only-post-author' => 'false', + 'disabled' => 'false', + ); + + $default = apply_filters( 'bnfw_notification_setting_fields', $default ); + + foreach ( $default as $key => $default_value ) { + $value = get_post_meta( $post_id, self::META_KEY_PREFIX . $key, true ); + if ( ! empty( $value ) ) { + $setting[ $key ] = $value; + } else { + $setting[ $key ] = $default_value; + } + } + + // compatibility code. This will be removed subsequently + $user_roles = get_post_meta( $post_id, self::META_KEY_PREFIX . 'user-roles', true ); + if ( ! empty( $user_roles ) && is_array( $user_roles ) ) { + foreach ( $user_roles as $role ) { + $setting['users'][] = 'role-' . $role; + } + + update_post_meta( $post_id, self::META_KEY_PREFIX . 'users', $setting['users'] ); + delete_post_meta( $post_id, self::META_KEY_PREFIX . 'user-roles' ); + } + + $setting['id'] = $post_id; + + return $setting; + } + + /** + * Change the post updated message for notification post type. + * + * @since 1.0 + * + * @param $messages + * + * @return mixed + */ + public function post_updated_messages( $messages ) { + $messages[ self::POST_TYPE ] = array_fill( 0, 11, esc_html__( 'Notification saved.', 'bnfw' ) ); + + return $messages; + } + + /** + * Render submit div meta box. + * + * @since 1.0 + * + * @param $post + */ + public function render_submitdiv( $post ) { + global $post; + ?> +
+ + +
+ +
+ + +
+
+ + +

+ + read_settings( $post->ID ); + ?> + + +
+ + +
- -
- +
+
-read_settings( $post->ID ); -?> - > -
-
+ post_status ) { ?> +
+ + -post_status ) { ?> - - - +

+
+ -
+
-
+
- - + +
- - - + + +
+
+ + +
- + + self::POST_TYPE, + ); + + $meta_query = array(); + + if ( ! empty( $types ) ) { + $meta_query[] = array( + 'key' => self::META_KEY_PREFIX . 'notification', + 'value' => $types, + 'compare' => 'IN', + ); + } + + if ( $exclude_disabled ) { + $meta_query[] = array( + 'key' => self::META_KEY_PREFIX . 'disabled', + 'value' => 'true', + 'compare' => '!=', + ); + } + + if ( ! empty( $meta_query ) ) { + $args['meta_query'] = $meta_query; + } + + $args['posts_per_page'] = -1; + $args['nopagging'] = true; + + $args = apply_filters( 'bnfw_get_notifications_args', $args, $types, $exclude_disabled ); + + $wp_query = new WP_Query(); + $posts = $wp_query->query( $args ); + + $posts = apply_filters( 'bnfw_get_notifications_posts', $posts, $args, $types, $exclude_disabled ); + + return $posts; + } + + /** + * Are there any disabled notifications for a particular notification type. + * + * @param string $type Notification type. + * + * @return bool True if disabled, False otherwise. + */ + public function is_notification_disabled( $type ) { + $args = array( + 'post_type' => self::POST_TYPE, + 'posts_per_page' => - 1, + 'nopagging' => true, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => self::META_KEY_PREFIX . 'notification', + 'value' => $type, + ), + array( + 'key' => self::META_KEY_PREFIX . 'disabled', + 'value' => 'true', + ), + ) + ); + + $args = apply_filters( 'bnfw_is_notification_disabled_args', $args, $type ); + + $wp_query = new WP_Query(); + $posts = $wp_query->query( $args ); + + $posts = apply_filters( 'bnfw_is_notification_disabled_posts', $posts, $args, $type ); + + return count( $posts ) > 0; + } + + /** + * Does a particular type of notification exists or not. + * + * @since 1.1 + * + * @param string $type Notification Type. + * @param bool $exclude_disabled (optional) Whether to exclude disabled notifications or not. True by default. + * + * @return bool True if present, False otherwise + */ + public function notification_exists( $type, $exclude_disabled = true ) { + $notifications = $this->get_notifications( $type, $exclude_disabled ); + + if ( count( $notifications ) > 0 ) { + return true; + } else { + return false; + } + } + + /** + * Custom columns for this post type. + * + * @since 1.0 + * @filter manage_{post_type}_posts_columns + * + * @param array $columns + * + * @return array + */ + public function columns_header( $columns ) { + $columns['type'] = esc_html__( 'Notification Type', 'bnfw' ); + $columns['disabled'] = esc_html__( 'Enabled?', 'bnfw' ); + $columns['subject'] = esc_html__( 'Subject', 'bnfw' ); + $columns['users'] = esc_html__( 'User Roles / Users', 'bnfw' ); + $columns['excluded'] = esc_html__( 'Excluded User Roles / Users', 'bnfw' ); + + return $columns; + } + + /** + * Custom column appears in each row. + * + * @since 1.0 + * @action manage_{post_type}_posts_custom_column + * + * @param string $column Column name + * @param int $post_id Post ID + */ + public function custom_column_row( $column, $post_id ) { + $setting = $this->read_settings( $post_id ); + switch ( $column ) { + case 'disabled': + if ( 'true' != $setting['disabled'] ) { + printf( '' ); + } + break; + case 'type': + echo $this->get_notification_name( $setting['notification'] ); + break; + case 'subject': + echo ! empty( $setting['subject'] ) ? $setting['subject'] : ''; + break; + case 'users': + $users = $this->get_names_from_users( $setting['users'] ); + if (!empty($users)) { + echo implode( ', ', $users ); + } + else { + if(isset($setting['new-user-role'])){ + $users = $this->get_names_from_users( $setting['new-user-role'] ); + echo implode( ', ', $users ); + } + } -
-
- - self::POST_TYPE, - 'meta_query' => array( - array( - 'key' => self::META_KEY_PREFIX . 'notification', - 'value' => $type, - ), - array( - 'key' => self::META_KEY_PREFIX . 'disabled', - 'value' => 'true', - 'compare' => '!=', - ), - ), - ); - - $wp_query = new WP_Query(); - $posts = $wp_query->query( $args ); - return $posts; - } - - /** - * Does a particular type of notification exists or not. - * - * @since 1.1 - * - * @param string $type Notification Type. - * @return bool True if present, False otherwise - */ - public function notification_exists( $type ) { - $notifications = $this->get_notifications( $type ); - - if ( count( $notifications ) > 0 ) { - return true; - } else { - return false; - } - } - - /** - * Custom columns for this post type. - * - * @since 1.0 - * @filter manage_{post_type}_posts_columns - * @param array $columns - * @return array - */ - public function columns_header( $columns ) { - $columns['type'] = __( 'Notification Type', 'bnfw' ); - $columns['disabled'] = __( 'Enabled?', 'bnfw' ); - $columns['subject'] = __( 'Subject', 'bnfw' ); - $columns['users'] = __( 'User Roles/Users', 'bnfw' ); - - return $columns; - } - - /** - * Custom column appears in each row. - * - * - * @since 1.0 - * @action manage_{post_type}_posts_custom_column - * @param string $column Column name - * @param int $post_id Post ID - */ - public function custom_column_row( $column, $post_id ) { - $setting = $this->read_settings( $post_id ); - switch ( $column ) { - case 'disabled': - if ( 'true' != $setting['disabled'] ) { - printf( '', plugins_url( '../assets/images/notification-enabled.png', dirname( __FILE__ ) ) ); - } - break; - case 'type': - echo $this->get_notifications_name( $setting['notification'] ); - break; - case 'subject': - echo ! empty( $setting['subject'] ) ? $setting['subject'] : ''; - break; - case 'users': - $users = $this->get_names_from_users( $setting['users'] ); - echo implode( ', ', $users ); - break; - } - } - - /** - * Get names from users. - * - * @since 1.2 - */ - private function get_names_from_users( $users ) { - $email_list = array(); - $user_ids = array(); - $user_roles = array(); - $names_from_user_ids = array(); - - foreach ( $users as $user ) { - if ( $this->starts_with( $user, 'role-' ) ) { - $user_roles[] = str_replace( 'role-', '', $user ); - } else { - $user_ids[] = absint( $user ); - } - } - - if ( ! empty( $user_ids ) ) { - $user_query = new WP_User_Query( array( 'include' => $user_ids ) ); - foreach ( $user_query->results as $user ) { - $names_from_user_ids[] = $user->user_login; - } - } - - return array_merge( $user_roles, $names_from_user_ids ); - } - - /** - * Get name of the notification based on slug. - * - * @param mixed $slug - * @return unknown - */ - private function get_notifications_name( $slug ) { - switch ( $slug ) { - case 'new-comment': - return __( 'New Comment', 'bnfw' ); - break; - case 'new-trackback': - return __( 'New Trackback', 'bnfw' ); - break; - case 'new-pingback': - return __( 'New Pingback', 'bnfw' ); - break; - case 'reply-comment': - return __( 'Comment Reply', 'bnfw' ); - break; - case 'user-password': - return __( 'Lost Password - For User', 'bnfw' ); - break; - case 'admin-password': - return __( 'Lost Password - For Admin', 'bnfw' ); - break; - case 'new-user': - return __( 'User Registration - For User', 'bnfw' ); - break; - case 'welcome-email': - return __( 'New User - Welcome email', 'bnfw' ); - break; - case 'admin-user': - return __( 'User Registration - For Admin', 'bnfw' ); - break; - case 'new-post': - return __( 'New Post Published', 'bnfw' ); - break; - case 'update-post': - return __( 'Post Updated', 'bnfw' ); - break; - case 'pending-post': - return __( 'Post Pending Review', 'bnfw' ); - break; - case 'future-post': - return __( 'Post Scheduled', 'bnfw' ); - break; - case 'newterm-category': - return __( 'New Category', 'bnfw' ); - break; - case 'newterm-post_tag': - return __( 'New Tag', 'bnfw' ); - break; - default: - $splited = explode( '-', $slug ); - $label = $splited[1]; - $post_obj = get_post_type_object( $splited[1] ); - - if ( null != $post_obj ) { - $label = $post_obj->labels->singular_name; - } - - switch ( $splited[0] ) { - case 'new': - return __( 'New ', 'bnfw' ) . $label; - break; - case 'update': - return __( 'Updated ', 'bnfw' ) . $label; - break; - case 'pending': - return $label . __( ' Pending Review', 'bnfw' ); - break; - case 'future': - return $label . __( ' Scheduled', 'bnfw' ); - break; - case 'comment': - return $label . __( ' Comment', 'bnfw' ); - break; - case 'newterm': - return __( 'New term in ', 'bnfw' ) . $splited[1]; - break; - } - break; - } - } - - /** - * Custom row actions for this post type. - * - * @since 1.0 - * @filter post_row_actions - * @param array $actions - * @return array - */ - public function custom_row_actions( $actions ) { - $post = get_post(); - - if ( self::POST_TYPE === get_post_type( $post ) ) { - unset( $actions['inline hide-if-no-js'] ); - unset( $actions['view'] ); - } - - return $actions; - } - - /** - * Find if a string starts with another string. - * - * @since 1.2 - */ - private function starts_with( $haystack, $needle ) { - // search backwards starting from haystack length characters from the end - return $needle === '' || strrpos( $haystack, $needle, -strlen( $haystack ) ) !== false; - } -} + if ( 'true' === $setting['only-post-author'] ) { + echo esc_html__( ', Post Author', 'bnfw' ); + } + + break; + case 'excluded': + $excluded_users = $this->get_names_from_users( $setting['exclude-users'] ); + echo implode( ', ', $excluded_users ); + + break; + } + + /** + * Invoked while displaying a custom column in notification table. + * + * @since 1.3.9 + * + * @param string $column Column name + * @param int $post_id Post ID + */ + do_action( 'bnfw_notification_table_column', $column, $post_id ); + } + + /** + * Get names from users. + * + * @since 1.2 + */ + private function get_names_from_users( $users ) { + $user_ids = array(); + $user_roles = array(); + $emails = array(); + $names_from_user_ids = array(); + + if ( is_array( $users ) ) { + foreach ( $users as $user ) { + if ( $this->starts_with( $user, 'role-' ) ) { + $user_roles[] = ucfirst( str_replace( 'role-', '', $user ) ); + } elseif ( strpos( $user, '@' ) !== false ) { + $emails[] = $user; + } elseif ( absint( $user ) > 0 ) { + $user_ids[] = absint( $user ); + } else { + $emails[] = $user; + } + } + } + else { + // User Roles not associated with a To/CC/BCC field + $role = get_role( $users ); + + if ( !empty( $role ) ) { + $user_roles = array( $role->name ); + } + } + + if ( ! empty( $user_ids ) ) { + $user_query = new WP_User_Query( array( 'include' => $user_ids ) ); + foreach ( $user_query->results as $user ) { + $names_from_user_ids[] = $user->user_login; + } + } + + return array_merge( $user_roles, $names_from_user_ids, $emails ); + } + + /** + * Get name of the notification based on slug. + * + * @param string $slug Notification Slug. + * + * @return string Notification Name. + */ + private function get_notification_name( $slug ) { + $name = ''; + switch ( $slug ) { + case 'new-comment': + $name = esc_html__( 'New Comment', 'bnfw' ); + break; + case 'approve-post-comment': + $name = esc_html__( 'Post - Comment Approved', 'bnfw' ); + break; + case 'moderate-comment': + $name = esc_html__( 'New Comment Awaiting Moderation', 'bnfw' ); + break; + case 'new-trackback': + $name = esc_html__( 'New Trackback', 'bnfw' ); + break; + case 'new-pingback': + $name = esc_html__( 'New Pingback', 'bnfw' ); + break; + case 'reply-comment': + $name = esc_html__( 'Comment Reply', 'bnfw' ); + break; + case 'user-password': + $name = esc_html__( 'User Lost Password - For User', 'bnfw' ); + break; + case 'admin-password': + $name = esc_html__( 'User Lost Password - For Admin', 'bnfw' ); + break; + case 'admin-password-changed': + $name = esc_html__( 'Password Changed - For Admin', 'bnfw' ); + break; + case 'admin-email-changed': + $name = esc_html__( 'User Email Changed - For Admin', 'bnfw' ); + break; + case 'password-changed': + $name = esc_html__( 'Password Changed - For User', 'bnfw' ); + break; + case 'email-changing': + $name = esc_html__( 'User Email Changed Confirmation - For User', 'bnfw' ); + break; + case 'email-changed': + $name = esc_html__( 'User Email Changed - For User', 'bnfw' ); + break; + case 'core-updated': + $name = esc_html__( 'WordPress Core Automatic Background Updates', 'bnfw' ); + break; + case 'new-user': + $name = esc_html__( 'New User Registration - For User', 'bnfw' ); + break; + case 'user-login': + $name = esc_html__( 'User Logged In - For User', 'bnfw' ); + break; + case 'admin-user-login': + $name = esc_html__( 'User Logged In - For Admin', 'bnfw' ); + break; + case 'welcome-email': + $name = esc_html__( 'New User - Post-registration Email', 'bnfw' ); + break; + case 'admin-user': + $name = esc_html__( 'New User Registration - For Admin', 'bnfw' ); + break; + case 'user-role': + $name = esc_html__( 'User Role Changed - For User', 'bnfw' ); + break; + case 'admin-role': + $name = esc_html__( 'User Role Changed - For Admin', 'bnfw' ); + break; + case 'new-post': + $name = esc_html__( 'New Post Published', 'bnfw' ); + break; + case 'update-post': + $name = esc_html__( 'Post Updated', 'bnfw' ); + break; + case 'pending-post': + $name = esc_html__( 'Post Pending Review', 'bnfw' ); + break; + case 'private-post': + $name = esc_html__( 'New Private Post', 'bnfw' ); + break; + case 'future-post': + $name = esc_html__( 'Post Scheduled', 'bnfw' ); + break; + case 'trash-post': + $name = esc_html__( 'Published Post Moved to Trash', 'bnfw' ); + break; + case 'new-page': + $name = esc_html__( 'New Page Published', 'bnfw' ); + break; + case 'newterm-category': + $name = esc_html__( 'New Category', 'bnfw' ); + break; + case 'newterm-post_tag': + $name = esc_html__( 'New Tag', 'bnfw' ); + break; + case 'ca-export-data': + $name = esc_html__( 'Privacy – Confirm Action: Export Data Request – For User', 'bnfw' ); + break; + case 'ca-erase-data': + $name = esc_html__( 'Privacy – Confirm Action: Erase Data Request – For User', 'bnfw' ); + break; + case 'uc-export-data': + $name = esc_html__( 'Privacy - Confirm Action: Export Data Request - For Admin', 'bnfw' ); + break; + case 'uc-erase-data': + $name = esc_html__( 'Privacy - Confirm Action: Erase Data Request - For Admin', 'bnfw' ); + break; + case 'data-export': + $name = esc_html__( 'Privacy - Data Export - For User', 'bnfw' ); + break; + case 'data-erased': + $name = esc_html__( 'Privacy - Data Erased - For User', 'bnfw' ); + break; + case 'new-media': + $name = esc_html__( 'New Media Published', 'bnfw' ); + break; + case 'update-media': + $name = esc_html__( 'Media Updated', 'bnfw' ); + break; + case 'comment-attachment': + $name = esc_html__( 'Media - New Comment', 'bnfw' ); + break; + case 'approve-page-comment': + $name = esc_html__( 'Page - Comment Approved', 'bnfw' ); + break; + case 'approve-attachment-comment': + $name = esc_html__( 'Media - Comment Approved', 'bnfw' ); + break; + case 'moderate-attachment-comment': + $name = esc_html__( 'Media - New Comment Awaiting Moderation', 'bnfw' ); + break; + case 'commentreply-attachment': + $name = esc_html__( 'Media - Comment Reply', 'bnfw' ); + break; + + + default: + $splited = explode( '-', $slug ); + $label = $splited[1]; + $post_obj = get_post_type_object( $splited[1] ); + + if ( null != $post_obj ) { + $label = $post_obj->labels->singular_name; + } + + switch ( $splited[0] ) { + case 'new': + $name = esc_html__( 'New ', 'bnfw' ) . $label . ' ' . esc_html__( 'Published', 'bnfw' ); + break; + case 'update': + $name = esc_html__( 'Updated ', 'bnfw' ) . $label; + break; + case 'pending': + $name = $label . esc_html__( ' Pending Review', 'bnfw' ); + break; + case 'trash': + $name = $label . esc_html__( ' Moved To Trash', 'bnfw' ); + break; + case 'future': + $name = $label . esc_html__( ' Scheduled', 'bnfw' ); + break; + case 'private': + $name = esc_html__( 'New Private ', 'bnfw' ) . $label; + break; + case 'comment': + $name = $label . esc_html__( ' Comment', 'bnfw' ); + break; + case 'moderate': + $name = $label . ' - ' . esc_html__( 'New Comment Awaiting Moderation', 'bnfw' ); + break; + case 'commentreply': + $name = $label . esc_html__( ' Comment Reply', 'bnfw' ); + break; + case 'approve': + $name = $label . esc_html__( ' Comment Approved', 'bnfw' ); + break; + case 'newterm': + $tax = get_taxonomy( $splited[1] ); + if ( ! $tax ) { + $name = esc_html__( 'New Term', 'bnfw' ); + } else { + $name = esc_html__( 'New Term in ', 'bnfw' ) . $tax->labels->name; + } + break; + } + break; + } + + $name = apply_filters( 'bnfw_notification_name', $name, $slug ); + + return $name; + } + + /** + * Add additional custom edit actions for enabling and disabling notifications in bulk. + * + * @param array $bulk_actions Bulk Actions. + * + * @return array Modified list of Bulk Actions. + */ + public function add_custom_edit_action( $bulk_actions ) { + $bulk_actions['enable_notifications'] = __( 'Enable Notifications', 'bnfw' ); + $bulk_actions['disable_notifications'] = __( 'Disable Notifications', 'bnfw' ); + + return $bulk_actions; + } + + /** + * Handle custom edit actions. + * + * @param $redirect_to + * @param $doaction + * @param $post_ids + * + * @return string + */ + public function handle_custom_edit_action( $redirect_to, $doaction, $post_ids ) { + if ( 'enable_notifications' !== $doaction && 'disable_notifications' !== $doaction ) { + return $redirect_to; + } + + $redirect_to = remove_query_arg( array( 'bulk_enable_notifications', 'bulk_disable_notifications', 'bnfw_action' ), $redirect_to ); + + $meta_value = 'true'; + + if ( 'enable_notifications' === $doaction ) { + $meta_value = 'false'; + } + + foreach ( $post_ids as $post_id ) { + update_post_meta( $post_id, self::META_KEY_PREFIX . 'disabled', $meta_value ); + } + + $redirect_to = add_query_arg( 'bulk_' . $doaction, count( $post_ids ), $redirect_to ); + + return $redirect_to; + } + + /** + * Custom row actions for this post type. + * + * @since 1.0 + * @filter post_row_actions + * + * @param array $actions + * @param \WP_Post $post + * + * @return array + */ + public function custom_row_actions( $actions, $post ) { + if ( self::POST_TYPE === get_post_type( $post ) ) { + unset( $actions['inline hide-if-no-js'] ); + unset( $actions['view'] ); + + $notification_disabled = get_post_meta( $post->ID, self::META_KEY_PREFIX . 'disabled', true ); + + if ( 'true' === $notification_disabled ) { + $url = add_query_arg( + array( + 'notification_id' => $post->ID, + 'bnfw_action' => 'enable_notification', + ) + ); + $actions['enable_notification'] = '' . __( 'Enable Notification', 'bnfw' ) . ''; + } else { + $url = add_query_arg( + array( + 'notification_id' => $post->ID, + 'bnfw_action' => 'disable_notification', + ) + ); + $actions['disable_notification'] = '' . __( 'Disable Notification', 'bnfw' ) . ''; + } + } + + return $actions; + } + + /** + * Handle custom actions. + */ + public function handle_actions() { + if ( ! isset( $_GET['bnfw_action'] ) || ! isset( $_GET['notification_id'] ) ) { + return; + } + + $post_id = absint( $_GET['notification_id'] ); + if ( 0 === $post_id ) { + return; + } + + $action = sanitize_text_field( $_GET['bnfw_action'] ); + + if ( 'enable_notification' === $action ) { + update_post_meta( $post_id, self::META_KEY_PREFIX . 'disabled', 'false' ); + } + + if ( 'disable_notification' === $action ) { + update_post_meta( $post_id, self::META_KEY_PREFIX . 'disabled', 'true' ); + } + } + + /** + * Find if a string starts with another string. + * + * @since 1.2 + * + * @param $haystack + * @param $needle + * + * @return bool + */ + public function starts_with( $haystack, $needle ) { + // search backwards starting from haystack length characters from the end + return '' === $needle || strrpos( $haystack, $needle, - strlen( $haystack ) ) !== false; + } + + /** + * Display a help notice. + * + * @since 1.7 + */ + public function show_help_notice() { + $screen = get_current_screen(); + if ( ! in_array( $screen->post_type, array( self::POST_TYPE ) ) ) { + return; + } + + if ( ! empty( $_REQUEST['bnfw_action'] ) && 'enable_notification' === $_REQUEST['bnfw_action'] ) { + echo '

' . __( 'Enabled 1 Notification.', 'bnfw' ) . '

'; + } + + if ( ! empty( $_REQUEST['bnfw_action'] ) && 'disable_notification' === $_REQUEST['bnfw_action'] ) { + echo '

' . __( 'Disabled 1 Notification.', 'bnfw' ) . '

'; + } + + if ( ! empty( $_REQUEST['bulk_enable_notifications'] ) ) { + $enabled_count = intval( $_REQUEST['bulk_enable_notifications'] ); + printf( '

' . + _n( 'Enabled %s Notification.', + 'Enabled %s Notifications.', + $enabled_count, + 'bnfw' + ) . '

', $enabled_count ); + } + + if ( ! empty( $_REQUEST['bulk_disable_notifications'] ) ) { + $disabled_count = intval( $_REQUEST['bulk_disable_notifications'] ); + printf( '

' . + _n( 'Disabled %s Notification.', + 'Disabled %s Notifications.', + $disabled_count, + 'bnfw' + ) . '

', $disabled_count ); + } + + if ( ! PAnD::is_admin_notice_active( 'disable-bnfw-help-notice-forever' ) ) { + return; + } + + ?> +
+

+ improve email deliverability. I recommend using Post SMTP as it\'s easy to set-up or Email Log to just log and view emails that are sent.', 'bnfw' ); ?> +

+
+ 200 ) { + return true; + } + + $emails = BNFW::factory()->engine->get_emails_from_users( $users ); + + if ( count( $emails ) > 200 ) { + return true; + } + + return false; + } +} \ No newline at end of file From 1ed423794d5470b6da0eaf9a1d7fb25e81846c12 Mon Sep 17 00:00:00 2001 From: Chad Riston Denaux Date: Mon, 21 Dec 2020 13:22:52 -0500 Subject: [PATCH 2/2] chore: minor cleanup on initial push --- includes/admin/class-bnfw-notification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/class-bnfw-notification.php b/includes/admin/class-bnfw-notification.php index e8ac76f..155d762 100755 --- a/includes/admin/class-bnfw-notification.php +++ b/includes/admin/class-bnfw-notification.php @@ -1,10 +1,10 @@