| 1 |
<?php |
| 2 |
/** |
| 3 |
* Docket Cache. |
| 4 |
* |
| 5 |
* @author Nawawi Jamili |
| 6 |
* @license MIT |
| 7 |
* |
| 8 |
* @see https://github.com/nawawi/docket-cache |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Nawawi\DocketCache; |
| 12 |
|
| 13 |
\defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
final class LimitBulkedit |
| 16 |
{ |
| 17 |
private $limit = 100; |
| 18 |
private $notice_id = 'docket-cache-notice-bulkedit'; |
| 19 |
|
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
$limit = (int) nwdcx_constval('LIMITBULKEDIT_LIMIT'); |
| 23 |
if ($limit > 20) { |
| 24 |
$this->limit = $limit; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
public function register() |
| 29 |
{ |
| 30 |
// min 20. |
| 31 |
if ($this->limit > 20) { |
| 32 |
add_action('wp_loaded', [$this, 'limit_bulk_edit_for_registered_post_types']); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public function limit_bulk_edit_for_registered_post_types() |
| 37 |
{ |
| 38 |
$types = get_post_types([ |
| 39 |
'show_ui' => true, |
| 40 |
]); |
| 41 |
|
| 42 |
foreach ($types as $type) { |
| 43 |
add_filter('bulk_actions-edit-'.$type, [$this, 'limit_bulk_edit']); |
| 44 |
add_action('admin_notices', [$this, 'bulk_edit_admin_notice']); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
private function cleanup_dismissed_pointers() |
| 49 |
{ |
| 50 |
$id = $this->notice_id; |
| 51 |
$user_id = get_current_user_id(); |
| 52 |
$pointers = array_filter(explode(',', (string) get_user_meta($user_id, 'dismissed_wp_pointers', true))); |
| 53 |
if (\in_array($id, $pointers, true)) { |
| 54 |
$index = array_search($id, $pointers); |
| 55 |
unset($pointers[$index]); |
| 56 |
$pointers = implode(',', $pointers); |
| 57 |
update_user_meta($user_id, 'dismissed_wp_pointers', $pointers); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
private function bulk_editing_is_limited() |
| 62 |
{ |
| 63 |
if (isset($GLOBALS['wp_query']) && ($GLOBALS['wp_query'] instanceof WP_Query)) { |
| 64 |
$total_posts = $GLOBALS['wp_query']->found_posts; |
| 65 |
|
| 66 |
if (isset($total_posts) && $this->limit > $total_posts) { |
| 67 |
$this->cleanup_dismissed_pointers(); |
| 68 |
|
| 69 |
return false; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Get default per page. |
| 74 |
if (!empty($_GET['post_type'])) { |
| 75 |
$post_type = sanitize_key($_GET['post_type']); |
| 76 |
// See wp-admin/includes/post.php -> wp_edit_posts_query(). |
| 77 |
$option = 'edit_'.$post_type.'_per_page'; |
| 78 |
$per_page = (int) get_user_option($option); |
| 79 |
if (empty($per_page) || $per_page < 1) { |
| 80 |
$per_page = 20; |
| 81 |
} |
| 82 |
} else { |
| 83 |
$per_page = get_query_var('posts_per_page'); |
| 84 |
} |
| 85 |
|
| 86 |
// Get per page when use wp_list_table. |
| 87 |
if (isset($GLOBALS['wp_list_table']) && ($GLOBALS['wp_list_table'] instanceof WP_Posts_List_Table)) { |
| 88 |
$per_page = isset($GLOBALS['wp_list_table']->_pagination_args['per_page']) ? $GLOBALS['wp_list_table']->_pagination_args['per_page'] : $per_page; |
| 89 |
} |
| 90 |
|
| 91 |
if (-1 === $per_page || $per_page > $this->limit) { |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
$this->cleanup_dismissed_pointers(); |
| 96 |
|
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
public function limit_bulk_edit($bulk_actions) |
| 101 |
{ |
| 102 |
if ($this->bulk_editing_is_limited()) { |
| 103 |
$bulk_actions = []; |
| 104 |
} |
| 105 |
|
| 106 |
return $bulk_actions; |
| 107 |
} |
| 108 |
|
| 109 |
public function bulk_edit_admin_notice() |
| 110 |
{ |
| 111 |
if (!$this->bulk_editing_is_limited()) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$id = $this->notice_id; |
| 116 |
|
| 117 |
$dismissed_pointers = array_filter(explode(',', (string) get_user_meta(get_current_user_id(), 'dismissed_wp_pointers', true))); |
| 118 |
if (\in_array($id, $dismissed_pointers, true)) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$msg = sprintf( |
| 123 |
/* translators: %d = number of items */ |
| 124 |
__('<strong>Docket Cache:</strong> 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'), |
| 125 |
(int) $this->limit |
| 126 |
); |
| 127 |
$js_id = esc_js($id); |
| 128 |
$code = '<script data-cfasync="false" data-noptimize="1" data-no-minify="1">'; |
| 129 |
$code .= 'jQuery(document).ready(function($){$( "#'.$js_id.'" ).on( "remove",function(){$.ajax({url: ajaxurl,type:"POST",xhrFields:{withCredentials:true},data:{action:"dismiss-wp-pointer",pointer:"'.$js_id.'"}});})});'; |
| 130 |
$code .= '</script>'; |
| 131 |
|
| 132 |
echo Resc::boxmsg(['id' => $id, 'text' => $msg, 'extra_after' => $code], 'error', true, false, false); |
| 133 |
} |
| 134 |
} |
| 135 |
|