22
33namespace Dwnload \WpRestApi \WpAdmin ;
44
5- use function Dwnload \WpRestApi \Helpers \filter_var_int ;
65use Dwnload \WpRestApi \RestApi \CacheApiTrait ;
76use Dwnload \WpRestApi \RestApi \RestDispatch ;
87use Dwnload \WpRestApi \WpRestApiCache ;
@@ -21,6 +20,7 @@ class Admin implements WpHooksInterface
2120
2221 const ACTION_REQUEST_FLUSH_CACHE = WpRestApiCache::FILTER_PREFIX . 'request_flush_cache ' ;
2322 const ADMIN_ACTION = WpRestApiCache::FILTER_PREFIX . 'flush ' ;
23+ const CAPABILITY = 'manage_wp_rest_api_cache ' ;
2424 const FILTER_SHOW_ADMIN = WpRestApiCache::FILTER_PREFIX . 'show_admin ' ;
2525 const FILTER_SHOW_ADMIN_BAR_MENU = WpRestApiCache::FILTER_PREFIX . 'show_admin_bar_menu ' ;
2626 const FILTER_SHOW_ADMIN_MENU = WpRestApiCache::FILTER_PREFIX . 'show_admin_menu ' ;
@@ -59,11 +59,33 @@ public function addHooks()
5959 $ this ->addAction ('admin_action_ ' . self ::ADMIN_ACTION , [$ this , 'adminAction ' ]);
6060 $ this ->addAction ('admin_notices ' , [$ this , 'adminNotices ' ]);
6161 }
62-
6362 if ($ this ->showAdminMenuBar ()) {
6463 $ this ->addAction ('admin_bar_menu ' , [$ this , 'adminBarMenu ' ], 999 );
6564 }
65+ if ($ this ->showAdminMenu () || $ this ->showAdminMenuBar ()) {
66+ $ this ->addFilter ('map_meta_cap ' , [$ this , 'mapMetaCap ' ], 10 , 2 );
67+ }
68+ }
69+ }
70+
71+
72+ /**
73+ * Map `self::CAPABILITY` capability.
74+ *
75+ * @param array $caps Returns the user's actual capabilities.
76+ * @param string $cap Capability name.
77+ * @return array
78+ */
79+ protected function mapMetaCap (array $ caps , string $ cap ) : array
80+ {
81+ // Map single-site cap check to 'manage_options'
82+ if ($ cap === self ::CAPABILITY ) {
83+ if (! \is_multisite ()) {
84+ $ caps = ['delete_users ' ];
85+ }
6686 }
87+
88+ return $ caps ;
6789 }
6890
6991 /**
@@ -75,7 +97,7 @@ protected function adminMenu()
7597 'options-general.php ' ,
7698 \esc_html__ ('WP REST API Cache ' , 'wp-rest-api-cache ' ),
7799 \esc_html__ ('REST API Cache ' , 'wp-rest-api-cache ' ),
78- ' delete_users ' ,
100+ self :: CAPABILITY ,
79101 self ::MENU_SLUG ,
80102 function () {
81103 $ this ->renderPage ();
@@ -90,7 +112,7 @@ function () {
90112 */
91113 protected function adminBarMenu (WP_Admin_Bar $ wp_admin_bar )
92114 {
93- if (! is_user_logged_in () || ! current_user_can (' delete_users ' ) || ! is_admin_bar_showing ()) {
115+ if (! \ is_user_logged_in () || ! \ current_user_can (self :: CAPABILITY ) || ! \ is_admin_bar_showing ()) {
94116 return ;
95117 }
96118
@@ -103,6 +125,9 @@ protected function adminBarMenu(WP_Admin_Bar $wp_admin_bar)
103125 'id ' => self ::MENU_ID ,
104126 'title ' => \esc_html__ ('Empty all cache ' , 'wp-rest-api-cache ' ),
105127 'href ' => \esc_url ($ this ->getEmptyCacheUrl ()),
128+ 'meta ' => [
129+ 'onclick ' => 'return confirm("This will clear ALL cache, continue?") '
130+ ]
106131 ]);
107132 }
108133
@@ -130,7 +155,7 @@ protected function adminAction()
130155 protected function adminNotices ()
131156 {
132157 if (! empty ($ _GET [self ::NOTICE ]) &&
133- filter_var_int ($ _GET [self ::NOTICE ]) === 1
158+ \filter_var ($ _GET [self ::NOTICE ], FILTER_VALIDATE_INT ) === 1
134159 ) {
135160 $ message = \esc_html__ ('The cache has been successfully cleared. ' , 'wp-rest-api-cache ' );
136161 echo "<div class='notice updated is-dismissible'><p> {$ message }</p></div> " ; // PHPCS: XSS OK.
@@ -178,7 +203,7 @@ private function requestCallback()
178203 \wp_verify_nonce ($ _REQUEST [self ::NONCE_NAME ], 'rest_cache_options ' ) !== false
179204 ) {
180205 if (! empty ($ _GET ['rest_cache_empty ' ]) &&
181- filter_var_int ($ _GET ['rest_cache_empty ' ]) === 1
206+ \filter_var ($ _GET ['rest_cache_empty ' ], FILTER_VALIDATE_INT ) === 1
182207 ) {
183208 if ($ this ->wpCacheFlush ()) {
184209 $ type = 'updated ' ;
@@ -196,7 +221,7 @@ private function requestCallback()
196221 */
197222 \do_action (self ::ACTION_REQUEST_FLUSH_CACHE , $ message , $ type , \wp_get_current_user ());
198223 } elseif (! empty ($ _POST [self ::OPTION_KEY ])) {
199- if ($ this ->updateOptions ($ _POST [' rest_cache_options ' ])) {
224+ if ($ this ->updateOptions ($ _POST [self :: OPTION_KEY ])) {
200225 $ type = 'updated ' ;
201226 $ message = \esc_html__ ('The cache time has been updated ' , 'wp-rest-api-cache ' );
202227 } else {
@@ -280,6 +305,6 @@ private function showAdminMenu() : bool
280305 */
281306 private function showAdminMenuBar () : bool
282307 {
283- return \apply_filters (self ::FILTER_SHOW_ADMIN_BAR_MENU , true ) === true ;
308+ return \apply_filters (self ::FILTER_SHOW_ADMIN_BAR_MENU , \is_admin_bar_showing () ) === true ;
284309 }
285310}
0 commit comments