Created
December 11, 2019 13:40
-
-
Save aiiddqd/f3f148442d2a13765d5f1e2a7a480e72 to your computer and use it in GitHub Desktop.
Optimizatron - Оптимизатрон Фронтэнд - оптимизирует фронтэнд, отключая лишние плагины, снижает нагрузку, ускоряет TTFB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Optimizatron | |
Description: Оптимизатрон Фронтэнд - оптимизирует фронтэнд, отключая лишние плагины, снижает нагрузку, ускоряет TTFB | |
Version: 1.0 | |
*/ | |
class Optimizatron | |
{ | |
public static $request_uri = ''; | |
public static function init(){ | |
if ( is_admin() ) { | |
return; | |
} | |
self::$request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); | |
add_filter('option_active_plugins', [__CLASS__, 'disable_plugin_for_all_front']); | |
add_filter('option_active_plugins', [__CLASS__, 'disable_some_plugins_by_condition']); | |
add_filter('option_active_plugins', [__CLASS__, 'front_disable_plugins_except_rest']); | |
add_filter('option_active_plugins', [__CLASS__, 'rest_api_whitle_list']); | |
add_filter('override_load_textdomain', [__CLASS__, 'rest_api_override_load_textdomain']); | |
} | |
/** | |
* disable translate api for fastest rest api | |
*/ | |
public static function rest_api_override_load_textdomain() | |
{ | |
return true; | |
} | |
/** | |
* rest_api add | |
* whitle_list plugins | |
* | |
* @param [type] $plugins | |
* @return void | |
*/ | |
public static function rest_api_whitle_list($plugins) | |
{ | |
if( ! self::is_rest()){ | |
return $plugins; | |
} | |
// перечень разрешенных в REST плагинов | |
$allowed = [ | |
'meta-box/meta-box.php', | |
'cyr3lat/cyr-to-lat.php', | |
'jetpack/jetpack.php' | |
]; | |
// включаем только их, остальные запрещаем | |
$plugins = array_intersect($plugins, $allowed); | |
return $plugins; | |
} | |
/** | |
* disable plugins on front | |
* except rest api | |
* | |
* @param [type] $plugins | |
* @return void | |
*/ | |
public static function front_disable_plugins_except_rest($plugins) | |
{ | |
if(self::is_rest()){ | |
return $plugins; | |
} | |
$disallowed = [ | |
'notification/notification.php', | |
]; | |
$plugins = array_diff($plugins, $disallowed); | |
return $plugins; | |
} | |
/** | |
* condition tag is rest | |
* | |
* @return boolean | |
*/ | |
public static function is_rest(){ | |
return strpos(self::$request_uri, '/wp-json/') !== false; | |
} | |
public static function disable_some_plugins_by_condition($plugins) | |
{ | |
$is_contact_page = strpos(self::$request_uri, '/editor/'); | |
$is_post = strpos(self::$request_uri, '/blog/'); | |
$is_bk = strpos(self::$request_uri, '/bk/'); | |
$frontEditorPlugin = "wp-user-frontend/wpuf.php"; | |
$tocPlugin = "luckywp-table-of-contents/luckywp-table-of-contents.php"; | |
$a = array_search($frontEditorPlugin, $plugins); | |
$b = array_search($tocPlugin, $plugins); | |
if (false !== $a && false === $is_contact_page) { | |
unset($plugins[$a]); | |
} | |
if (false !== $b && false === $is_bk && false === $is_post) { | |
unset($plugins[$b]); | |
} | |
return $plugins; | |
} | |
/** | |
* Отключючает не нужные во фронтэнде плагины | |
* | |
* @param array $plugins список плагинов | |
* | |
* @return array | |
*/ | |
public static function disable_plugin_for_all_front($plugins){ | |
$disallowed = [ | |
'ari-adminer/ari-adminer.php', | |
'meta-box/meta-box.php', | |
'youtube-videos-to-wordpress-posts/youtube-videos-to-wordpress-posts.php', | |
'simple-page-ordering/simple-page-ordering.php', | |
'mb-admin-columns/mb-admin-columns.php', | |
// 'notification/notification.php', | |
]; | |
$plugins = array_diff($plugins, $disallowed); | |
return $plugins; | |
} | |
} | |
Optimizatron::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment