PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.2.13
Adminify – White Label, Admin Menu Editor, Login Customizer v4.2.13
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Inc / Classes / Tweaks.php

Tweaks.php in Adminify – White Label, Admin Menu Editor, Login Customizer 4.2.13, at Inc/Classes/Tweaks.php

955 lines 28.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PXLBSAdminify\Inc\Classes;
4
5 use PXLBSAdminify\Inc\Utils;
6 use PXLBSAdminify\Inc\Admin\AdminSettings;
7 use PXLBSAdminify\Inc\Admin\AdminSettingsModel;
8
9 // no direct access allowed
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 /**
15 * @package WP Adminify
16 * @author: Jewel Theme<support@jeweltheme.com>
17 */
18
19 class Tweaks extends AdminSettingsModel
20 {
21
22 public $redirect_status = 301;
23 private $performance;
24 private $security_head;
25 private $security_feed;
26 private $security_rest_api;
27 private $security_posts;
28 private $security_media;
29 private $custom_gravatar;
30
31 public function __construct()
32 {
33 $this->options = (array) AdminSettings::get_instance()->get();
34 $this->performance = $this->options['performance'];
35 $this->security_head = $this->options['security_head'];
36 $this->security_feed = $this->options['security_feed'];
37 $this->security_rest_api = $this->options['security_rest_api'];
38 $this->security_posts = $this->options['post_archives'];
39 $this->security_media = $this->options['media_attachments'];
40 $this->custom_gravatar = $this->options['custom_gravatar'];
41
42
43
44 if (!empty($this->security_head['enable_security_head'])) {
45 $this->security_head();
46 }
47
48 $this->security_feed();
49
50 if (!empty($this->security_rest_api['security_rest_api_enable'])) {
51 $this->security_rest_api();
52 }
53
54 if (!empty($this->security_posts['post_archives_enable'])) {
55 $this->security_post_archives();
56 }
57
58 $this->security_attachments();
59
60 if(!empty($this->performance['performance_enable'])){
61 $this->performances();
62 }
63
64 // Add Custom Default Gravatar Image
65 if (!empty($this->custom_gravatar) && array_key_exists('enable', $this->custom_gravatar)) {
66 // Add Custom Default Gravatar Image
67 add_filter('avatar_defaults', [$this, 'add_custom_gravatar_image'], 99);
68 }
69
70 // If Admin bar Editor Plugin not Installed
71 if (! class_exists('\JewelTheme\AdminBarEditor\AdminBarEditor')) {
72 if (!empty($this->options['white_label']['wordpress']['remove_howdy_msg']) || !empty($this->options['white_label']['wordpress']['change_howdy_text'])) {
73 // Change Howdy Text
74 // add_action('admin_bar_menu', [$this, 'remove_from_admin_bar'], 999);
75 // add_action('admin_bar_menu', [$this, 'clear_node_title'], 999);
76 add_filter('admin_bar_menu', [$this, 'change_howdy_text'], 9999);
77 }
78 }
79
80
81 // Remove Admin Bar Logo
82 if(!empty($this->options['white_label']['wordpress']['admin_bar_cleanup'])){
83 add_action("wp_before_admin_bar_render", [$this, "admin_bar_cleanup"], 0);
84 }
85
86 }
87
88
89 /**
90 * Security: Post & Archives
91 *
92 * @return void
93 */
94 public function security_post_archives(){
95 $post_archives_data = $this->security_posts['post_archives_data'];
96
97 // tag and category hooks
98 if (!empty($post_archives_data) && in_array('last_modified_date', $post_archives_data)) {
99 add_filter('the_content', [$this, 'last_updated_date'], 10, 1);
100 add_filter('wp_head', [$this, 'last_updated_date_style']);
101 }
102
103 /** Remove Capital P Dangit */
104 if (!empty($post_archives_data) && in_array('capital_p_dangit', $post_archives_data)) {
105 remove_filter('the_title', 'capital_P_dangit', 11);
106 remove_filter('the_content', 'capital_P_dangit', 11);
107 remove_filter('comment_text', 'capital_P_dangit', 31);
108 }
109
110 // Remove date archives
111 if (!empty($post_archives_data) && (
112 in_array('archives_date', $post_archives_data) ||
113 in_array('archives_author', $post_archives_data) ||
114 in_array('archives_tag', $post_archives_data) ||
115 in_array('archives_category', $post_archives_data) ||
116 in_array('archives_postformat', $post_archives_data) ||
117 in_array('archives_search', $post_archives_data)
118 )) {
119 add_action('template_redirect', [$this, 'redirect_to_home']);
120 }
121
122 // // Remove Author archives
123 // if (!empty($post_archives_data) && in_array('archives_author', $post_archives_data)) {
124 // if (is_author()) {
125 // add_action('template_redirect', [$this, 'redirect']);
126 // }
127 // }
128
129
130 // // Remove tag archives
131 // if (!empty($post_archives_data) && in_array('archives_tag', $post_archives_data)) {
132 // if (is_tag()) {
133 // add_action('template_redirect', [$this, 'redirect']);
134 // }
135 // }
136
137 // // Remove category archives
138 // if (!empty($post_archives_data) && in_array('archives_category', $post_archives_data)) {
139 // if (is_category()) {
140 // add_action('template_redirect', [$this, 'redirect']);
141 // }
142 // }
143
144 // // Remove archives post formats
145 // if (!empty($post_archives_data) && in_array('archives_postformat', $post_archives_data)) {
146 // if (is_tax('post_format')) {
147 // add_action('template_redirect', [$this, 'redirect']);
148 // }
149 // }
150
151 // // Remove search page
152 // if (!empty($post_archives_data) && in_array('archives_search', $post_archives_data)) {
153 // if (is_search()) {
154 // add_action('template_redirect', [$this, 'redirect']);
155 // }
156 // }
157
158 }
159
160 public function url_redirection(){
161 global $wp_query;
162 $wp_query->set_404();
163 wp_safe_redirect(esc_url(home_url()), 301);
164 exit;
165 }
166
167 public function redirect_to_home(){
168 $post_archives_data = $this->security_posts['post_archives_data'];
169
170 // Remove Author archives
171 if (!empty($post_archives_data) && in_array('archives_author', $post_archives_data)) {
172 if (is_author()) {
173 $this->url_redirection();
174 }
175 }
176
177
178 // Remove tag archives
179 if (!empty($post_archives_data) && in_array('archives_tag', $post_archives_data)) {
180 if (is_tag()) {
181 $this->url_redirection();
182 }
183 }
184
185 // Remove category archives
186 if (!empty($post_archives_data) && in_array('archives_category', $post_archives_data)) {
187 if (is_category()) {
188 $this->url_redirection();
189 }
190 }
191
192 // Remove archives post formats
193 if (!empty($post_archives_data) && in_array('archives_postformat', $post_archives_data)) {
194 if (is_tax('post_format')) {
195 $this->url_redirection();
196 }
197 }
198
199 // Remove search page
200 if (!empty($post_archives_data) && in_array('archives_search', $post_archives_data)) {
201 if (is_search()) {
202 $this->url_redirection();
203 }
204 }
205 }
206
207
208 /**
209 * Security: Attachments
210 *
211 * @return void
212 */
213 public function security_attachments(){
214
215 // Add Featured Image or Post Thumbnail to RSS Feed
216 if (!empty($this->security_media['thumbnails_rss_feed'])) {
217 add_filter('the_excerpt_rss', [$this, 'rss_post_thumbnail']);
218 add_filter('the_content_feed', [$this, 'rss_post_thumbnail']);
219 }
220
221 }
222
223 /**
224 * Disable REST API
225 *
226 * @return void
227 */
228 public function security_rest_api(){
229 $this->security_rest_api = $this->security_rest_api['security_rest_api_data'];
230
231 /** Disable REST API */
232 if (!empty($this->security_rest_api) && in_array('rest_api', $this->security_rest_api)) {
233
234 // All REST API Hooks
235 if (version_compare(get_bloginfo('version'), '4.7', '>=')) {
236 add_filter('rest_authentication_errors', [$this, 'disable_rest_api']);
237 } else {
238 // Filters for WP-API version 1.x
239 add_filter('json_enabled', '__return_false');
240 add_filter('json_jsonp_enabled', '__return_false');
241
242 // Filters for WP-API version 2.x
243 add_filter('rest_enabled', '__return_false');
244 add_filter('rest_jsonp_enabled', '__return_false');
245 }
246
247 remove_action('wp_head', 'rest_output_link_wp_head', 10); // Disable REST API links in the HTML <head> tag
248 remove_action('wp_head', 'wp_oembed_add_discovery_links'); // Remove oEmbed discovery links.
249 remove_action('template_redirect', 'rest_output_link_header', 11, 0); // Disable the REST API link in HTTP headers.
250 remove_action('xmlrpc_rsd_apis', 'rest_output_rsd'); // Remove the REST API URL from the WordPress RSD endpoint.
251 }
252
253
254 // Remove X-Powered-By
255 if (!empty($this->security_rest_api) && in_array('powered', $this->security_rest_api)) {
256 // add_action('wp', [$this, 'remove_powered']); // OLD way
257
258 // Hook into 'send_headers' to remove the "X-Powered-By" header
259 add_action('send_headers', [$this, 'remove_powered']);
260 }
261 }
262
263
264 public function disable_rest_api(){
265 if (!is_user_logged_in()) {
266 return new \WP_Error(
267 'rest_api_authentication_required',
268 __('The REST API has been restricted to authenticated users.', 'adminify'),
269 [
270 'status' => rest_authorization_required_code()
271 ]
272 );
273 }
274 }
275
276
277 /**
278 * Security Head
279 *
280 * @return void
281 */
282 public function security_head(){
283
284 $this->security_head = $this->security_head['security_head_data'];
285
286 // Disable XML-RPC
287 if (!empty($this->security_head) && in_array('xmlrpc', $this->security_head)) {
288 // add_filter('xmlrpc_enabled', '__return_false');
289 add_filter('wp_xmlrpc_server_class', [$this, 'adminify_disable_wp_xmlrpc']);
290 }
291
292 // Remove WordPress Version Number
293 if (!empty($this->security_head) && in_array('generator_wp_version', $this->security_head)) {
294 remove_action('wp_head', 'wp_generator'); // Remove WordPress Generator Version
295 add_filter('the_generator', '__return_false'); // Remove Generator Name From Rss Feeds.
296 }
297
298 // Remove Revolution Slider generator version
299 if (!empty($this->security_head) && in_array('revslider_generator', $this->security_head)) {
300 add_filter('revslider_meta_generator', '__return_empty_string');
301 }
302
303 // Remove woocommerce generator version
304 if (!empty($this->security_head) && in_array('wc_generator', $this->security_head)) {
305 remove_action('wp_head', 'wc_generator_tag');
306 }
307
308 // Remove wpml meta generator tag
309 if (!empty($this->security_head) && in_array('wpml_generator', $this->security_head)) {
310 add_action('wp_head', [$this, 'remove_wpml_generator'], 0);
311 }
312
313 // Remove wpbakery visual_composer meta generator tag
314 if (!empty($this->security_head) && in_array('js_composer_generator', $this->security_head)) {
315 if (class_exists('Vc_Manager')) {
316 remove_action('wp_head', [visual_composer(), 'addMetaData'], 1);
317 }
318 }
319
320 // Remove Yoast SEO meta generator tag
321 if (!empty($this->security_head) && in_array('yoast_generator', $this->security_head)) {
322 add_filter('wpseo_debug_markers', '__return_false');
323 }
324
325 // REMOVE wlwmanifest.xml.
326 if (!empty($this->security_head) && in_array('wlwmanifest', $this->security_head)) {
327 remove_action('wp_head', 'wlwmanifest_link');
328 }
329
330 // Remove Really Simple Discovery Link.
331 if (!empty($this->security_head) && in_array('rsd', $this->security_head)) {
332 remove_action('wp_head', 'rsd_link');
333 }
334
335 // Remove Shortlink Url
336 if (!empty($this->security_head) && in_array('shortlink', $this->security_head)) {
337 remove_action('wp_head', 'wp_shortlink_wp_head');
338 remove_action('template_redirect', 'wp_shortlink_header', 100, 0);
339 }
340
341 /** Remove Version Query Strings from Scripts/Styles */
342 if (!empty($this->security_head) && in_array('canonical', $this->security_head)) {
343 remove_action('embed_head', 'rel_canonical');
344
345 // Function to remove the canonical URL
346 //
347
348 // Hook the function to wp_head
349 add_action('wp_head', [$this, 'remove_wp_canonical_url'] , 1);
350 // Yoast SEO Canonical URL
351 if (Utils::is_plugin_active('wordpress-seo/wp-seo.php')) {
352 add_filter('wpseo_canonical', '__return_false');
353 }
354 }
355
356
357 /* Disable Self Pings */
358 if (!empty($this->security_head) && in_array('self_ping', $this->security_head)) {
359 add_action('pre_ping', [$this, 'disable_self_ping']);
360 }
361
362 }
363
364 /**
365 * Disable XMLRPC file
366 *
367 * @return void
368 */
369 public function adminify_disable_wp_xmlrpc($data)
370 {
371 http_response_code(403);
372 }
373
374 /**
375 * Security Feed Links
376 *
377 * @return void
378 */
379 public function security_feed(){
380
381 // Remove Feed Links
382 if (!empty($this->security_feed)) {
383 remove_action('wp_head', 'feed_links_extra', 3); // Remove Every Extra Links to Rss Feeds.
384 remove_action('wp_head', 'feed_links', 2); // Remove Head link <head/>
385 remove_action('do_feed_rdf', 'do_feed_rdf', 10, 0);
386 remove_action('do_feed_rss', 'do_feed_rss', 10, 0);
387 remove_action('do_feed_rss2', 'do_feed_rss2', 10, 1);
388 remove_action('do_feed_atom', 'do_feed_atom', 10, 1);
389
390 // Redirect to Home
391 add_action('do_feed', [$this, 'redirect'], 1);
392 add_action('do_feed_rdf', [$this, 'redirect'], 1);
393 add_action('do_feed_rss', [$this, 'redirect'], 1);
394 add_action('do_feed_rss2', [$this, 'redirect'], 1);
395 add_action('do_feed_atom', [$this, 'redirect'], 1);
396
397 }
398 }
399
400
401
402 /*
403 * Change Howdy Text
404 */
405 public function change_howdy_text($wp_admin_bar)
406 {
407 // Remove Howdy Message entirely
408 if (!empty($this->options['white_label']['wordpress']['remove_howdy_msg'])) {
409
410 // Remove the entire "My Account" section and rebuild it later.
411 remove_action('admin_bar_menu', 'wp_admin_bar_my_account_item', 7);
412
413 $current_user = wp_get_current_user();
414 $user_id = get_current_user_id();
415 $profile_url = get_edit_profile_url($user_id);
416
417 $avatar = get_avatar($user_id, 26); // size 26x26 pixels
418 $display_name = $current_user->display_name;
419 $class = $avatar ? 'with-avatar' : 'no-avatar';
420 $wp_admin_bar->add_menu(array(
421 'id' => 'my-account',
422 'parent' => 'top-secondary',
423 'title' => $display_name . $avatar,
424 'href' => $profile_url,
425 'meta' => array(
426 'class' => $class,
427 ),
428 ));
429 }
430
431 // Change Howdy Text
432 if (!empty($this->options['white_label']['wordpress']['change_howdy_text'])) {
433
434 // Get the current user information
435 $my_account = $wp_admin_bar->get_node('my-account');
436
437 if (isset($my_account->title)) {
438
439 // Replace the "Howdy" text with "Welcome"
440 $changed_howdy_text = str_replace('Howdy', Utils::kses_custom($this->options['white_label']['wordpress']['change_howdy_text']), $my_account->title);
441
442 // Update the node with the new title
443 $wp_admin_bar->add_node([
444 'id' => 'my-account',
445 'parent' => 'top-secondary',
446 'title' => $changed_howdy_text,
447 ]);
448 }
449
450 }
451 }
452
453 /**
454 * Remove Canonical URL
455 *
456 * @return void
457 */
458 public function remove_wp_canonical_url()
459 {
460 remove_action('wp_head', 'rel_canonical');
461 }
462
463 /**
464 * Removes admin bar logo from Admin Toolbar
465 */
466 public function admin_bar_cleanup()
467 {
468 global $wp_admin_bar;
469 //TODO: Check for Network wide site "my-sites"
470
471 if (in_array('wp_logo', $this->options['white_label']['wordpress']['admin_bar_cleanup'])) {
472 $wp_admin_bar->remove_menu('wp-logo');
473 }
474 if (in_array('site_name', $this->options['white_label']['wordpress']['admin_bar_cleanup'])) {
475 $wp_admin_bar->remove_menu('site-name');
476 }
477 if (in_array('comments', $this->options['white_label']['wordpress']['admin_bar_cleanup'])) {
478 $wp_admin_bar->remove_menu('comments');
479 }
480 // if (in_array('menu_toggle', $this->options['white_label']['wordpress']['admin_bar_cleanup'])) {
481 // $wp_admin_bar->remove_menu('menu-toggle');
482 // }
483 }
484
485
486
487 // Clear the node titles
488 // This will only work if the node is using a :before element for the icon
489 public function clear_node_title($wp_admin_bar)
490 {
491 $all_toolbar_nodes = $wp_admin_bar->get_nodes();
492 // Create an array of node ID's we'd like to remove
493 $clear_titles = [
494 'site-name',
495 'customize',
496 ];
497
498 foreach ($all_toolbar_nodes as $node) {
499
500 // Run an if check to see if a node is in the array to clear_titles
501 if (in_array($node->id, $clear_titles)) {
502 // use the same node's properties
503 $args = $node;
504
505 // make the node title a blank string
506 $args->title = '';
507
508 // update the Toolbar node
509 $wp_admin_bar->add_node($args);
510 }
511 }
512 }
513
514 // Remove items from the admin bar
515 public function remove_from_admin_bar($wp_admin_bar)
516 {
517 /*
518 * Placing items in here will only remove them from admin bar
519 * when viewing the fronte end of the site
520 */
521 if (!is_admin()) {
522 // Example of removing item generated by plugin. Full ID is #wp-admin-bar-si_menu
523 $wp_admin_bar->remove_node('si_menu');
524
525 // WordPress Core Items (uncomment to remove)
526 $wp_admin_bar->remove_node('updates');
527 $wp_admin_bar->remove_node('comments');
528 $wp_admin_bar->remove_node('new-content');
529 // $wp_admin_bar->remove_node('wp-logo');
530 // $wp_admin_bar->remove_node('site-name');
531 $wp_admin_bar->remove_node('my-account');
532 // $wp_admin_bar->remove_node('search');
533 // $wp_admin_bar->remove_node('customize');
534 }
535
536 /*
537 * Items placed outside the if statement will remove it from both the frontend
538 * and backend of the site
539 */
540 $wp_admin_bar->remove_node('wp-logo');
541 }
542
543 // Custom Avatars
544 public function add_custom_gravatar_image($avatar_defaults)
545 {
546 // Register the avatar images configured by the administrator in the plugin settings.
547 if ( ! empty( $this->custom_gravatar['image'] ) && is_array( $this->custom_gravatar['image'] ) ) {
548 foreach ( $this->custom_gravatar['image'] as $value ) {
549 if ( empty( $value['avatar_image']['url'] ) ) {
550 continue;
551 }
552 $avatar_url = esc_url_raw( $value['avatar_image']['url'] );
553 $avatar_name = ! empty( $value['avatar_name'] ) ? sanitize_text_field( $value['avatar_name'] ) : __( 'Custom Gravatar', 'adminify' );
554 $avatar_defaults[ $avatar_url ] = $avatar_name;
555 }
556 }
557
558 return $avatar_defaults;
559 }
560
561 // Check Last Login Column
562 public function last_login_column_info( $user_login )
563 {
564 $user = get_user_by( 'login', $user_login );
565 // by username
566 update_user_meta( $user->ID, 'adminify_last_login_on', time() );
567 }
568
569
570 // Remove WPML Generator
571 public function remove_wpml_generator()
572 {
573 if (!empty($GLOBALS['sitepress'])) {
574 remove_action(current_filter(), [$GLOBALS['sitepress'], 'meta_generator_tag']);
575 }
576 }
577
578
579 /**
580 * Disable WP emojicons from TinyMCE
581 *
582 * @access public
583 * @since 1.0.0
584 * @return void
585 */
586 public function disable_emojicons_tinymce($plugins)
587 {
588 if (is_array($plugins)) {
589 return array_diff($plugins, ['wpemoji']);
590 } else {
591 return [];
592 }
593 }
594
595 /**
596 * Disable WP emojicons from TinyMCE
597 *
598 * @access public
599 * @since 1.0.0
600 * @return void
601 */
602 public function remove_dns_prefetch( $urls, $relation_type ) {
603 if ('dns-prefetch' == $relation_type) {
604 // Strip out any URLs referencing the WordPress.org emoji location
605 $emoji_svg_url_base = 'https://s.w.org/images/core/emoji/';
606 foreach ($urls as $key => $url) {
607 if (is_string($url) && false !== strpos($url, $emoji_svg_url_base)) {
608 unset($urls[$key]);
609 }
610 }
611 }
612 return $urls;
613 }
614
615 public function remove_powered()
616 {
617 if (function_exists('header_remove')) {
618 header_remove('X-Powered-By');
619 }
620 }
621
622
623
624 /**
625 * Remove all rewrite rules related to embeds.
626 *
627 * @param array $rules WordPress rewrite rules.
628 * @return array Rewrite rules without embeds rules.
629 */
630 public function disable_embeds_rewrites($rules)
631 {
632 foreach ($rules as $rule => $rewrite) {
633 if (false !== strpos($rewrite, 'embed=true')) {
634 unset($rules[$rule]);
635 }
636 }
637
638 return $rules;
639 }
640
641 // Redirect 403
642 public function redirect_to_403($url = false)
643 {
644 status_header(403); // Send an HTTP 403 Forbidden status header
645 die('403 Forbidden'); // End execution and display a 403 Forbidden message
646 }
647
648 // Redirect function
649 public function redirect($url = false)
650 {
651 if ($url) {
652 $target = $url;
653 } else {
654 $target = esc_url(home_url());
655 }
656
657 $target = apply_filters('pxlbsadminify_redirect_target', $target);
658 $status = apply_filters('pxlbsadminify_redirect_status', $this->redirect_status);
659
660 wp_safe_redirect($target, $status);
661 exit;
662 }
663
664
665 /* Add Featured Image or Post Thumbnail to RSS Feed */
666 public function rss_post_thumbnail($content)
667 {
668 global $post;
669 if (has_post_thumbnail($post->ID)) {
670 $content = '<p>' . get_the_post_thumbnail($post->ID) .
671 '</p>' . get_the_content();
672 }
673 return $content;
674 }
675
676 // Display Last Updated Date of Your Posts
677 public function last_updated_date($content)
678 {
679 if (!is_single()) {
680 return $content;
681 }
682
683 $u_time = get_the_time('U');
684 $u_modified_time = get_the_modified_time('U');
685
686 if ($u_modified_time >= $u_time + 86400) {
687 $custom_content = sprintf(
688 '<div class="wp-adminify-last-updated"><strong><span>%1$s</span></strong><span>%2$s %3$s</span></div>',
689 esc_html__('Last Updated on', 'adminify'),
690 esc_html(get_the_modified_time('F jS, Y')),
691 esc_html(get_the_modified_time('h:i a'))
692 );
693 return $custom_content . $content;
694 }
695
696 return $content;
697 }
698
699 public function last_updated_date_style()
700 {
701 echo '<style>.wp-adminify-last-updated{ border: 1px dashed red; padding: 5px 10px;}</style>';
702 }
703
704
705 // Remove Default Image Links typeremove_image_link
706 public function security_attachment_imagelink()
707 {
708 $image_set = get_option('image_default_link_type');
709
710 if ($image_set !== 'none') {
711 update_option('image_default_link_type', 'none');
712 }
713 }
714
715
716 /* Disable Self Pings */
717 public function disable_self_ping(&$links)
718 {
719 $home = esc_url( home_url() );
720
721 // Additional URLs and explode into an array.
722 $extra_urls = (!empty($this->options['self_ping_sites'])) ? $this->options['self_ping_sites'] : '';
723 $extra_urls = explode(PHP_EOL, $extra_urls);
724
725 if ( is_array( $extra_urls ) ) {
726 $url_array = $extra_urls;
727 } else {
728 $url_array = array();
729 }
730
731 foreach ( $links as $l => $link ) {
732 if ( 0 === strpos( $link, $home ) ) {
733 unset( $links[ $l ] );
734 }
735 foreach ( $url_array as $url ) {
736 $url = trim( $url );
737 if ( 0 === strpos( $link, $url ) && '' !== $url ) {
738 unset( $links[ $l ] );
739 }
740 }
741 }
742
743 }
744
745
746 /** Remove Dashicons from Admin Bar for non logged in users **/
747 public function remove_dashicons()
748 {
749 global $pagenow;
750
751 if (!is_user_logged_in()) {
752
753 // This retrieves the /path/file.php?param=val part of the URL
754 $current_request_uri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '';
755
756 // for homepage
757 if (empty($current_request_uri)) {
758 wp_dequeue_style('dashicons');
759 wp_deregister_style('dashicons');
760 } else {
761 // Exclude the login page, where Dashicons assets are required for proper styling.
762 if (false !== strpos($current_request_uri, 'wp-login.php') || 'wp-login.php' === $pagenow) {
763 // Do nothing on wp-login.php
764 } elseif (false !== strpos($current_request_uri, 'protected-page=view')) {
765 // Exclude the password protection form.
766 // Do nothing on protected-page=view.
767 } else {
768 // Dequeue Dashicons from contents e.g., yoursite.com/content-url/. Not for wp-login.php,
769 wp_dequeue_style('dashicons');
770 wp_deregister_style('dashicons');
771 }
772 }
773 }
774 }
775
776
777 /** Control Interval Heartbeat API **/
778 public function control_heartbeat_api($settings)
779 {
780 $settings['interval'] = 60;
781 return $settings;
782 }
783
784 /** Remove Query Strings from Scripts/Styles **/
785 public function remove_script_versions($src)
786 {
787 if( !empty($src) ) {
788 $parts = explode('?ver', $src);
789 return $parts[0];
790 }
791 }
792
793 /** Browser Cache Expires & GZIP Compression **/
794 public function htaccess()
795 {
796 // We get the main WordPress .htaccess filepath.
797 $ruta_htaccess = get_home_path() . '.htaccess'; // https://codex.wordpress.org/Function_Reference/get_home_path !
798
799 $lineas = [];
800 $lineas[] = '<IfModule mod_expires.c>';
801 $lineas[] = '# Activar caducidad de contenido';
802 $lineas[] = 'ExpiresActive On';
803 $lineas[] = '# Directiva de caducidad por defecto';
804 $lineas[] = 'ExpiresDefault "access plus 1 month"';
805 $lineas[] = '# Para el favicon';
806 $lineas[] = 'ExpiresByType image/x-icon "access plus 1 year"';
807 $lineas[] = '# Imagenes';
808 $lineas[] = 'ExpiresByType image/gif "access plus 1 month"';
809 $lineas[] = 'ExpiresByType image/png "access plus 1 month"';
810 $lineas[] = 'ExpiresByType image/jpg "access plus 1 month"';
811 $lineas[] = 'ExpiresByType image/jpeg "access plus 1 month"';
812 $lineas[] = '# CSS';
813 $lineas[] = 'ExpiresByType text/css "access 1 month"';
814 $lineas[] = '# Javascript';
815 $lineas[] = 'ExpiresByType application/javascript "access plus 1 year"';
816 $lineas[] = '</IfModule>';
817 $lineas[] = '<IfModule mod_deflate.c>';
818 $lineas[] = '# Activar compresión de contenidos estáticos';
819 $lineas[] = 'AddOutputFilterByType DEFLATE text/plain text/html';
820 $lineas[] = 'AddOutputFilterByType DEFLATE text/xml application/xml application/xhtml+xml application/xml-dtd';
821 $lineas[] = 'AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml image/svg+xml';
822 $lineas[] = 'AddOutputFilterByType DEFLATE text/css text/javascript application/javascript application/x-javascript';
823 $lineas[] = 'AddOutputFilterByType DEFLATE font/otf font/opentype application/font-otf application/x-font-otf';
824 $lineas[] = 'AddOutputFilterByType DEFLATE font/ttf font/truetype application/font-ttf application/x-font-ttf';
825 $lineas[] = '</IfModule>';
826
827 insert_with_markers($ruta_htaccess, 'Adminify by Jewel Theme', $lineas); // https://developer.wordpress.org/reference/functions/insert_with_markers/ !
828 }
829
830
831 /**
832 * Performance Cleanup
833 *
834 * @access public
835 * @since 1.0.0
836 * @return void
837 */
838 public function performances()
839 {
840 $performance_data = $this->performance['performance_data'];
841
842 // Remove jQuery Migrate
843
844 // Remove Dashicons from Frontend
845 if (!empty($performance_data) && in_array('dashicons', $performance_data)) {
846 add_action('wp_print_styles', [$this, 'remove_dashicons'], 100);
847 }
848
849 /** Remove Version Query Strings from Scripts/Styles */
850 if (!empty($performance_data) && in_array('version_strings', $performance_data)) {
851 if(!is_admin()){
852 add_filter('script_loader_src', [$this, 'remove_script_versions'], 15, 1);
853 add_filter('style_loader_src', [$this, 'remove_script_versions'], 15, 1);
854 }
855 }
856
857
858 // Remove Emoji Styles and Scripts
859 if (!empty($performance_data) && in_array('emoji', $performance_data)) {
860 remove_action('wp_head', 'print_emoji_detection_script', 7); // Remove Emoji's Styles and Scripts.
861 remove_action('embed_head', 'print_emoji_detection_script');
862 remove_action('embeded_head', 'print_emoji_detection_script');
863 remove_action('admin_print_scripts', 'print_emoji_detection_script'); // Remove Emoji's Styles and Scripts.
864 remove_action('wp_print_styles', 'print_emoji_styles'); // Remove Emoji's Styles and Scripts.
865 remove_action('admin_print_styles', 'print_emoji_styles'); // Remove Emoji's Styles and Scripts.
866 remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
867 remove_filter('the_content_feed', 'wp_staticize_emoji');
868 remove_filter('comment_text_rss', 'wp_staticize_emoji');
869 add_filter('option_use_smilies', '__return_false');
870 add_filter('emoji_svg_url', '__return_false');
871 add_action('admin_init', [$this, 'remove_admin_emojis']);
872 add_filter('tiny_mce_plugins', [$this, 'disable_emojicons_tinymce']);
873 add_filter('wp_resource_hints', [$this, 'remove_dns_prefetch'], 10, 2);
874 }
875
876
877
878 add_action('wp_default_scripts', [$this, 'remove_wp_default_scripts'], 9999);
879
880 /** Secure method for Defer Parsing of JavaScript moving ALL JS from Header to Footer */
881 if (in_array('defer_parsing_js_footer', $performance_data)) {
882 add_filter('script_loader_tag', [$this, 'defer_parsing_of_js'], 10, 2);
883 }
884
885 /** Browser Cache Expires & GZIP Compression */
886 if (in_array('cache_gzip_compression', $performance_data)) {
887 register_activation_hook(__FILE__, [$this, 'htaccess']);
888 }
889
890
891 // Remove Gravatar Query Strings
892 if (!empty($performance_data) && in_array('gravatar_query_strings', $performance_data)) {
893 add_filter('get_avatar_url', [$this, 'remove_avatar_query_string']);
894 }
895 }
896
897 /* Remove Gravatar Query Strings */
898 public function remove_avatar_query_string($url)
899 {
900 $url_parts = explode('?', $url);
901 return $url_parts[0];
902 }
903
904
905 public function remove_admin_emojis()
906 {
907 remove_action('admin_print_scripts', 'print_emoji_detection_script');
908 remove_action('admin_print_styles', 'print_emoji_styles');
909 }
910
911
912 /** Secure method for Defer Parsing of JavaScript moving ALL JS from Header to Footer **/
913 public function defer_parsing_of_js($tag, $handle)
914 {
915 $skip = apply_filters('pxlbsadminify_defer_skip', false, $tag, $handle);
916
917 if ($skip) {
918 return $tag;
919 }
920
921 if (is_admin()) {
922 return $tag;
923 }
924 if (strpos($tag, '/wp-includes/js/jquery/jquery')) {
925 return $tag;
926 }
927 if (isset($_SERVER['HTTP_USER_AGENT']) && strpos(sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])), 'MSIE 9.') !== false) {
928 return $tag;
929 } else {
930 return str_replace(' src', ' defer src', $tag);
931 }
932 }
933
934 /**
935 * Remove jQuery Migrate Script
936 *
937 * @param [type] $scripts
938 *
939 * @return void
940 */
941 public function remove_wp_default_scripts($scripts)
942 {
943 // Frontend
944 if (!is_admin() && in_array('jquery_migrate_front', $this->performance['performance_data'])) {
945 $scripts->registered["jquery"]->deps = array_diff($scripts->registered["jquery"]->deps, ["jquery-migrate"]);
946 }
947
948 // Backend
949 if (is_admin() && in_array('jquery_migrate_back', $this->performance['performance_data'])) {
950 $scripts->registered["jquery"]->deps = array_diff($scripts->registered["jquery"]->deps, ["jquery-migrate"]);
951 }
952
953 }
954 }
955