add_node( [ 'id' => self::POST_TYPE, 'title' => __( 'New notification', 'admin-user-control' ), 'href' => get_dashboard_url(), ] ); } public function hide_notification_bar() { echo ""; } public function delete_read_notification_with_post( $post_id ) { $deleted_post = get_post( $post_id ); if ( $deleted_post->post_type === self::POST_TYPE ) { global $wpdb; $wpdb->delete( 'wp_usermeta', [ 'meta_key' => 'auc_read_notification', 'meta_value' => $post_id ] ); } } public function add_post_type_notifications() { register_post_type( self::POST_TYPE, [ 'labels' => [ 'name' => __( 'Notification', 'admin-user-control' ), 'add_new_item' => __( 'Add New Notification', 'admin-user-control' ), 'edit_item' => __( 'Edit Notification', 'admin-user-control' ), ], 'capability_type' => self::POST_TYPE, 'map_meta_cap' => true, 'public' => true, 'has_archive' => true, ] ); } public function admin_ajax_read() { $result = add_user_meta( get_current_user_id(), 'auc_read_notification', $_POST['notificationId'] ); wp_send_json( [ 'result' => $result ? true : false ] ); } public function add_dashboard_notifications_widget() { wp_add_dashboard_widget( 'notifications_widget', __( 'Latest notifications', 'admin-user-control' ), [ $this, 'add_notifications_to_widget' ] ); } public function add_notifications_to_widget() { $notifications = new WP_Query( [ 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => apply_filters( 'auc_notification_per_page', 10 ), ] ); if ( $notifications->have_posts() ) { $widget = '

' . __( 'Click a title to display a detail. If you read it, mark it as read.', 'admin-user-control' ) . '

'; $read_notification_ids = get_user_meta( get_current_user_id(), 'auc_read_notification' ); $widget .= ''; } else { $widget = '

' . __( 'There is no notifications', 'admin-user-control' ) . '

'; } echo $widget; } public function add_notification_capabilities() { $notification_caps = [ 'edit_post' => 'edit_' . self::POST_TYPE, 'read_post' => 'read_' . self::POST_TYPE, 'delete_post' => 'delete_' . self::POST_TYPE, 'edit_posts' => 'edit_' . self::POST_TYPE . 's', 'edit_others_posts' => 'edit_others_' . self::POST_TYPE . 's', 'delete_posts' => 'delete_' . self::POST_TYPE . 's', 'publish_posts' => 'publish_' . self::POST_TYPE . 's', 'read_private_posts' => 'read_private_' . self::POST_TYPE . 's', 'delete_private_posts' => 'delete_private_' . self::POST_TYPE . 's', 'delete_published_posts' => 'delete_published_' . self::POST_TYPE . 's', 'delete_others_posts' => 'delete_others_' . self::POST_TYPE . 's', 'edit_private_posts' => 'edit_private_' . self::POST_TYPE . 's', 'edit_published_posts' => 'edit_published_' . self::POST_TYPE . 's', 'create_posts' => 'edit_' . self::POST_TYPE . 's', ]; foreach ( $notification_caps as $notification_cap ) { $role = get_role( 'administrator' ); $role->add_cap( $notification_cap ); } } public function create_response() { $user_id = get_current_user_id(); $read_notification_ids = get_user_meta( $user_id, 'auc_read_notification' ); $get_from_days = apply_filters( 'auc_notification_get_from_days', 30 ); $unread_notifications = new WP_Query( [ 'post_type' => 'notifications', 'post_status' => 'publish', 'post__not_in' => $read_notification_ids, 'date_query' => [ [ 'after' => date_i18n( 'Ymd', strtotime( "-{$get_from_days} days" ) ) ], ], 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 1, ] ); $is_exist_notification = false; if ( $unread_notifications->found_posts !== 0 ) { $is_exist_notification = true; } return [ 'admin-notification' => [ 'is_exist_notification' => $is_exist_notification ] ]; } }