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.5 1.3.8.6 1.3.8.7 1.3.8.8 1.3.8.9 1.3.9.0 1.3.9.1 1.3.9.2 1.3.9.3 1.3.9.4 1.3.9.5 1.3.9.6 1.3.9.7 1.3.9.8 1.3.9.9 1.4 1.4.0.1 1.4.0.2 1.4.0.3 1.4.0.4
wp-asset-clean-up / classes / Menu.php
wp-asset-clean-up / classes Last commit date
Admin 1 week ago OptimiseAssets 1 week ago ThirdParty 1 year ago AdminBar.php 1 week ago AssetsManager.php 1 week ago BulkChanges.php 1 week ago CleanUp.php 2 months ago Debug.php 1 week ago DebugException.php 1 week ago FileSystem.php 1 year ago HardcodedAssets.php 1 week ago LoadExceptions.php 1 week ago Main.php 1 week ago MainFront.php 1 week ago Maintenance.php 1 week ago Menu.php 1 week ago MetaBoxes.php 1 week ago Misc.php 1 week ago MiscArray.php 1 week ago ObjectCache.php 2 months ago OwnAssets.php 1 week ago PluginTracking.php 1 week ago Preloads.php 1 week ago Regex.php 1 week ago Settings.php 1 week ago Tips.php 1 year ago Update.php 1 week ago
Menu.php
369 lines
1 <?php
2 /** @noinspection MultipleReturnStatementsInspection */
3
4 namespace WpAssetCleanUp;
5
6 use WpAssetCleanUp\Admin\AssetsManagerAdmin;
7 use WpAssetCleanUp\Admin\Info;
8 use WpAssetCleanUp\Admin\Overview;
9 use WpAssetCleanUp\Admin\PluginsManagerAdmin;
10 use WpAssetCleanUp\Admin\SettingsAdmin;
11 use WpAssetCleanUp\Admin\Tools;
12
13 /**
14 * Class Menu
15 * @package WpAssetCleanUp
16 */
17 class Menu
18 {
19 /**
20 * @var string
21 */
22 public static $defaultAccessRole = 'administrator';
23
24 /**
25 * This capability is assigned to non-admin users (the admins already have the "administrator" role that takes priority)
26 * so they would get access to the plugin's area
27 *
28 * @var string
29 */
30 public static $pluginAccessCap = 'assetcleanup_manager';
31
32 /**
33 * Menu constructor.
34 */
35 public function __construct()
36 {
37 add_action('admin_menu', array($this, 'activeMenu'));
38
39 // Whenever the following option is on: "Settings" - "Plugin Usage Preferences" - "Visibility" - "Hide it from the left sidebar within the Dashboard"
40 // Make sure that on any plugin page that is visited the following sidebar Dashboard menu item will be visible: "Settings" - "Asset CleanUp Pro"
41 if (self::isPluginPage() && Main::instance()->settings['hide_from_side_bar']) {
42 self::makeSidebarSettingsPluginLinkVisible();
43 add_filter('admin_body_class', array($this, 'filterAdminBodyClass'), PHP_INT_MAX);
44 }
45
46 add_filter( 'post_row_actions', array($this, 'editPostRowActions'), 10, 2 );
47 add_filter( 'page_row_actions', array($this, 'editPostRowActions'), 10, 2 );
48
49 add_action('admin_page_access_denied', array($this, 'pluginPagesAccessDenied'));
50 }
51
52 /**
53 * @return string[]
54 */
55 public static function getAllMenuPages()
56 {
57 $menuPages = array(
58 WPACU_PLUGIN_ID . '_getting_started',
59 WPACU_PLUGIN_ID . '_settings',
60 WPACU_PLUGIN_ID . '_assets_manager',
61 WPACU_PLUGIN_ID . '_plugins_manager',
62 WPACU_PLUGIN_ID . '_bulk_unloads',
63 WPACU_PLUGIN_ID . '_overview',
64 WPACU_PLUGIN_ID . '_tools',
65 WPACU_PLUGIN_ID . '_license',
66 WPACU_PLUGIN_ID . '_get_help'
67 );
68
69 return apply_filters('wpacu_internal_menu_all_pages', $menuPages);
70 }
71
72 /**
73 * @param $classes
74 *
75 * @return mixed
76 */
77 public function filterAdminBodyClass($classes)
78 {
79 $sanitizedData = apply_filters('wpacu_internal_admin_body_class_plugin_page_prefix', 'asset-cleanup');
80
81 $classes .= ' '.$sanitizedData.'_page_'.sanitize_title($_GET['page']).' ';
82
83 return $classes;
84 }
85
86 /**
87 * @noinspection NestedAssignmentsUsageInspection
88 */
89 public function activeMenu()
90 {
91 // User should be of 'administrator' role and allowed to activate plugins
92 if (! self::userCanAccessPlugin()) {
93 return;
94 }
95
96 $slug = $parentSlug = WPACU_PLUGIN_ID . '_getting_started'; // default
97
98 if (Main::instance()->settings['hide_from_side_bar']) {
99 $parentSlug = '';
100 }
101
102 add_menu_page(
103 WPACU_PLUGIN_TITLE,
104 WPACU_PLUGIN_TITLE,
105 self::getAccessCapability(),
106 $slug,
107 array(new Info, 'gettingStarted'),
108 WPACU_PLUGIN_URL.'/assets/icons/icon-asset-cleanup.png'
109 );
110
111 add_submenu_page(
112 $parentSlug,
113 __('Getting Started', 'wp-asset-clean-up'),
114 __('Getting Started', 'wp-asset-clean-up'),
115 self::getAccessCapability(),
116 $parentSlug
117 );
118
119 add_submenu_page(
120 $parentSlug,
121 __('Settings', 'wp-asset-clean-up'),
122 __('Settings', 'wp-asset-clean-up'),
123 self::getAccessCapability(),
124 WPACU_PLUGIN_ID . '_settings',
125 array(new SettingsAdmin, 'settingsPage')
126 );
127
128 add_submenu_page(
129 $parentSlug,
130 __('CSS/JS Manager', 'wp-asset-clean-up'),
131 __('CSS/JS Manager', 'wp-asset-clean-up'),
132 self::getAccessCapability(),
133 WPACU_PLUGIN_ID . '_assets_manager',
134 array(new AssetsManagerAdmin, 'renderPage')
135 );
136
137 add_submenu_page(
138 $parentSlug,
139 __('Plugins Manager', 'wp-asset-clean-up'),
140 __('Plugins Manager', 'wp-asset-clean-up'),
141 self::getAccessCapability(),
142 WPACU_PLUGIN_ID . '_plugins_manager',
143 array(new PluginsManagerAdmin, 'page')
144 );
145
146 add_submenu_page(
147 $parentSlug,
148 __('Bulk Changes', 'wp-asset-clean-up'),
149 __('Bulk Changes', 'wp-asset-clean-up'),
150 self::getAccessCapability(),
151 WPACU_PLUGIN_ID . '_bulk_unloads',
152 array(new BulkChanges, 'pageBulkUnloads')
153 );
154
155 add_submenu_page(
156 $parentSlug,
157 __('Overview', 'wp-asset-clean-up'),
158 __('Overview', 'wp-asset-clean-up'),
159 self::getAccessCapability(),
160 WPACU_PLUGIN_ID . '_overview',
161 array(new Overview, 'pageOverview')
162 );
163
164 add_submenu_page(
165 $parentSlug,
166 __('Tools', 'wp-asset-clean-up'),
167 __('Tools', 'wp-asset-clean-up'),
168 self::getAccessCapability(),
169 WPACU_PLUGIN_ID . '_tools',
170 array(new Tools, 'toolsPage')
171 );
172
173 // License Page
174 add_submenu_page(
175 $parentSlug,
176 __('License', 'wp-asset-clean-up'),
177 apply_filters('wpacu_internal_license_submenu_label', __('License', 'wp-asset-clean-up')),
178 self::getAccessCapability(),
179 WPACU_PLUGIN_ID . '_license',
180 apply_filters('wpacu_internal_license_submenu_callback', '__return_null')
181 );
182
183 // Get Help | Support Page
184 add_submenu_page(
185 $parentSlug,
186 __('Help', 'wp-asset-clean-up'),
187 __('Help', 'wp-asset-clean-up'),
188 self::getAccessCapability(),
189 WPACU_PLUGIN_ID . '_get_help',
190 array(new Info, 'help')
191 );
192
193 do_action('wpacu_internal_admin_menu_after_get_help', $parentSlug);
194
195 // Add plugin settings link to the main "Settings" menu within the Dashboard, for easier navigation
196 add_options_page(
197 WPACU_PLUGIN_TITLE,
198 WPACU_PLUGIN_TITLE,
199 self::getAccessCapability(),
200 admin_url('admin.php?page=' . WPACU_PLUGIN_ID . '_settings')
201 );
202
203 }
204
205 /**
206 * @return void
207 */
208 public static function makeSidebarSettingsPluginLinkVisible()
209 {
210 add_action('wp_loaded', static function() {
211 ob_start(static function($htmlSource) {
212 $htmlSource = preg_replace(
213 '#<li class="wp-has-submenu wp-not-current-submenu (.*?)" id="menu-settings"#',
214 '<li class="wp-has-submenu wp-has-current-submenu \\1" id="menu-settings"',
215 $htmlSource
216 );
217
218 $adminUrl = admin_url('admin.php?page='.WPACU_PLUGIN_ID.'_settings');
219
220 $reps = array(
221 '<a href=\'options-general.php\' class="wp-has-submenu wp-not-current-submenu' =>
222 '<a href=\'options-general.php\' class="wp-has-submenu wp-has-current-submenu wp-menu-open',
223
224 '<li><a href=\''.$adminUrl.'\'>' . WPACU_PLUGIN_TITLE . '</a></li>' =>
225 '<li class="current"><a class="current" aria-current="page" href=\''.$adminUrl.'\'>' . WPACU_PLUGIN_TITLE . '</a></li>'
226 );
227
228 return str_replace(array_keys($reps), array_values($reps), $htmlSource);
229 });
230 }, 0);
231 }
232
233 /**
234 *
235 * @return bool
236 */
237 public static function userCanAccessPlugin()
238 {
239 if (is_super_admin()) {
240 return true; // For security reasons, super admins will always be able to access the plugin's settings
241 }
242
243 if (current_user_can(self::$defaultAccessRole) || current_user_can(self::$pluginAccessCap)) {
244 return true;
245 }
246
247 return false;
248 }
249
250 /**
251 * @return false|string
252 *
253 * If the page belongs to the plugin, it will return the actual page without the prefix which is: WPACU_PLUGIN_ID . '_'
254 */
255 public static function isPluginPage()
256 {
257 return isset($_GET['page']) && is_string($_GET['page']) && in_array($_GET['page'], self::getAllMenuPages())
258 ? str_replace(WPACU_PLUGIN_ID . '_', '', sanitize_text_field($_GET['page']))
259 : false;
260 }
261
262 /**
263 * @return string
264 */
265 public static function getAccessCapability()
266 {
267 // You can be an admin, and have a user registered that has a 'subscriber' role with limited access to other sensitive parts of the website
268 // You can give him/her access to Asset CleanUp (e.g. he/she can be a developer that needs access to the plugin's settings to optimize the website)
269 // Anyone with $_plugin_access_capability capability could access the plugin
270 if (current_user_can(self::$pluginAccessCap)) {
271 return self::$pluginAccessCap;
272 }
273
274 // Those with 'administrator' role will always be able to access it
275 return self::$defaultAccessRole;
276 }
277
278 /**
279 * @param $actions
280 * @param $post
281 *
282 * @return mixed
283 */
284 public function editPostRowActions($actions, $post)
285 {
286 // Check for your post type.
287 if ( $post->post_type === 'post' ) {
288 $wpacuFor = 'posts';
289 } elseif ( $post->post_type === 'page' ) {
290 $wpacuFor = 'pages';
291 } elseif ( $post->post_type === 'attachment' ) {
292 $wpacuFor = 'media_attachment';
293 } else {
294 $wpacuFor = 'custom_post_types';
295 }
296
297 $postTypeObject = get_post_type_object($post->post_type);
298
299 if ( ! (isset($postTypeObject->public) && $postTypeObject->public == 1) ) {
300 return $actions;
301 }
302
303 if ( ! in_array(get_post_status($post), array('publish', 'private')) ) {
304 return $actions;
305 }
306
307 // Do not show the management link to specific post types that are marked as "public", but not relevant such as "ct_template" from Oxygen Builder
308 if (in_array($post->post_type, MetaBoxes::$noMetaBoxesForPostTypes)) {
309 return $actions;
310 }
311
312 // Build your links URL.
313 $url = esc_url(admin_url( 'admin.php?page=wpassetcleanup_assets_manager' ));
314
315 // Maybe put in some extra arguments based on the post status.
316 $edit_link = add_query_arg(
317 array(
318 'wpacu_for' => $wpacuFor,
319 'wpacu_post_id' => $post->ID
320 ), $url
321 );
322
323 // Only show it to the user that has "administrator" access, and it's in the following list (if a certain list of admins is provided)
324 // "Settings" -> "Plugin Usage Preferences" -> "Allow managing assets to:"
325 if (self::userCanAccessPlugin() && AssetsManager::currentUserCanViewAssetsList()) {
326 /*
327 * You can reset the default $actions with your own array, or simply merge them
328 * here I want to rewrite my Edit link, remove the Quick-link, and introduce a
329 * new link 'Copy'
330 */
331 $actions['wpacu_manage_assets'] = sprintf( '<a href="%1$s">%2$s</a>',
332 esc_url( $edit_link ),
333 esc_html( __( 'Manage CSS &amp; JS', 'wp-asset-clean-up' ) )
334 );
335 }
336
337 return $actions;
338 }
339
340 /**
341 * Message to show if the user tries to access a plugin's page without having any right to do so
342 */
343 public function pluginPagesAccessDenied()
344 {
345 if ( ! self::isPluginPage() ) {
346 // Not an Asset CleanUp page
347 return;
348 }
349
350 $userMeta = get_userdata(get_current_user_id());
351 $userRoles = $userMeta->roles;
352
353 $accessDeniedMsg = __('Sorry, you are not allowed to access this page.').'<br /><br />'.
354 sprintf(
355 __('By default, for security reasons, %s can be accesed within the Dashboard by <strong>Super Admins</strong> (somebody with access to the site network administration features and all other features) and <strong>Administrators</strong> (somebody who has access to all the administration features within a single site).', 'wp-asset-clean-up'),
356 WPACU_PLUGIN_TITLE
357 ) . '<br /><br />';
358
359
360 $accessDeniedMsg .= sprintf(__('Your current role(s): <strong>%s</strong>', 'wp-asset-clean-up'), implode(', ', $userRoles)) . '<br /><br />';
361
362 $accessDeniedMsg .= __('Please reach out to the administrator of this website if you believe you have the right to access this page.', 'wp-asset-clean-up').'<br /><br />';
363
364 $accessDeniedMsg .= '<div>Read more about WordPress user roles: <a target="_blank" href="https://wordpress.org/support/article/roles-and-capabilities/#summary-of-roles">https://wordpress.org/support/article/roles-and-capabilities/#summary-of-roles</a></div>';
365
366 wp_die( $accessDeniedMsg, 403 );
367 }
368 }
369