20) { $this->limit = $limit; } } public function register() { // min 20. if ($this->limit > 20) { add_action('wp_loaded', [$this, 'limit_bulk_edit_for_registered_post_types']); } } public function limit_bulk_edit_for_registered_post_types() { $types = get_post_types([ 'show_ui' => true, ]); foreach ($types as $type) { add_filter('bulk_actions-edit-'.$type, [$this, 'limit_bulk_edit']); add_action('admin_notices', [$this, 'bulk_edit_admin_notice']); } } private function cleanup_dismissed_pointers() { $id = $this->notice_id; $user_id = get_current_user_id(); $pointers = array_filter(explode(',', (string) get_user_meta($user_id, 'dismissed_wp_pointers', true))); if (\in_array($id, $pointers, true)) { $index = array_search($id, $pointers); unset($pointers[$index]); $pointers = implode(',', $pointers); update_user_meta($user_id, 'dismissed_wp_pointers', $pointers); } } private function bulk_editing_is_limited() { if (isset($GLOBALS['wp_query']) && ($GLOBALS['wp_query'] instanceof WP_Query)) { $total_posts = $GLOBALS['wp_query']->found_posts; if (isset($total_posts) && $this->limit > $total_posts) { $this->cleanup_dismissed_pointers(); return false; } } // Get default per page. if (!empty($_GET['post_type'])) { $post_type = sanitize_key($_GET['post_type']); // See wp-admin/includes/post.php -> wp_edit_posts_query(). $option = 'edit_'.$post_type.'_per_page'; $per_page = (int) get_user_option($option); if (empty($per_page) || $per_page < 1) { $per_page = 20; } } else { $per_page = get_query_var('posts_per_page'); } // Get per page when use wp_list_table. if (isset($GLOBALS['wp_list_table']) && ($GLOBALS['wp_list_table'] instanceof WP_Posts_List_Table)) { $per_page = isset($GLOBALS['wp_list_table']->_pagination_args['per_page']) ? $GLOBALS['wp_list_table']->_pagination_args['per_page'] : $per_page; } if (-1 === $per_page || $per_page > $this->limit) { return true; } $this->cleanup_dismissed_pointers(); return false; } public function limit_bulk_edit($bulk_actions) { if ($this->bulk_editing_is_limited()) { $bulk_actions = []; } return $bulk_actions; } public function bulk_edit_admin_notice() { if (!$this->bulk_editing_is_limited()) { return; } $id = $this->notice_id; $dismissed_pointers = array_filter(explode(',', (string) get_user_meta(get_current_user_id(), 'dismissed_wp_pointers', true))); if (\in_array($id, $dismissed_pointers, true)) { return; } $msg = sprintf( /* translators: %d = number of items */ __('Docket Cache: Bulk actions are disabled because more than %d items have been listed. To re-enable bulk editing, please adjust the "Number of items" setting under "Screen Options".', 'docket-cache'), (int) $this->limit ); $js_id = esc_js($id); $code = ''; echo Resc::boxmsg(['id' => $id, 'text' => $msg, 'extra_after' => $code], 'error', true, false, false); } }