Created
February 27, 2016 17:22
-
-
Save weaming/8ae15514d5a46e7924e3 to your computer and use it in GitHub Desktop.
wordpress plugin source code
This file contains 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: Useso take over Google | |
Plugin URI: http://www.brunoxu.com/useso-take-over-google.html | |
Description: 用360前端公共库Useso接管Google字体库和Google公共库,无需设置,插件安装激活后即刻生效。 | |
Author: Bruno Xu | |
Author URI: http://www.brunoxu.com/ | |
Version: 1.6.4 | |
License: GNU General Public License v2 or later | |
License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
define('USESO_TAKE_OVER_GOOGLE_PLUGIN_URL', plugin_dir_url( __FILE__ )); | |
define('USESO_TAKE_OVER_GOOGLE_PLUGIN_DIR', plugin_dir_path( __FILE__ )); | |
function useso_take_over_google_is_login_page() { | |
return in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')); | |
} | |
if (is_admin()) {// init -> wp_loaded -> admin_menu -> admin_init -> wp -> admin_enqueue_scripts -> admin_head | |
$action = 'admin_init'; // OK | |
//$action = 'wp'; // NG | |
} elseif (useso_take_over_google_is_login_page()) { | |
$action = 'wp_loaded'; // OK | |
//$action = 'wp'; // NG | |
} else { // init -> wp_loaded -> wp -> template_redirect -> get_header -> wp_enqueue_scripts -> wp_head | |
$action = 'template_redirect'; // OK | |
//$action = 'get_header';// NG in theme 'pinnacle'(use redux framework) | |
} | |
add_action($action, 'useso_take_over_google_obstart'); | |
function useso_take_over_google_obstart() { | |
ob_start('useso_take_over_google_obend'); | |
} | |
function useso_take_over_google_obend($content) { | |
return useso_take_over_google_filter($content); | |
} | |
function useso_take_over_google_filter($content) | |
{ | |
$content = apply_filters('useso_take_over_google_content_filter_before', $content); | |
/* | |
<link rel="stylesheet" id="open-sans-css" href="//fonts.googleapis.com/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&subset=latin%2Clatin-ext&ver=3.9.2" type="text/css" media="all"> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script> | |
*/ | |
$regexp = "/<(link|script)([^<>]+)>/i"; | |
$content = preg_replace_callback( | |
$regexp, | |
"useso_take_over_google_str_handler", | |
$content | |
); | |
/* | |
@import url(http://fonts.googleapis.com/css?family=Roboto+Condensed:regular); | |
@import url(http://fonts.googleapis.com/css?family=Merriweather:300,300italic,700,700italic); | |
*/ | |
$regexp = "/@import\s+url\([^\(\)]+\);?/i"; | |
$content = preg_replace_callback( | |
$regexp, | |
"useso_take_over_google_str_handler", | |
$content | |
); | |
/* | |
google fonts imported by 'Web Font Loader' | |
*/ | |
$webfont_js = USESO_TAKE_OVER_GOOGLE_PLUGIN_URL.'webfont_v1.5.3.js'; | |
if (is_ssl()) { | |
$webfont_js = USESO_TAKE_OVER_GOOGLE_PLUGIN_URL.'webfont_https_v1.5.3.js'; | |
} | |
//$content = str_ireplace('//ajax.googleapis'.'.com/ajax/libs/webfont/1/webfont.js', substr($webfont_js, strpos($webfont_js,'//')), $content); | |
$content = preg_replace('|//ajax.googleapis'.'.com/ajax/libs/webfont/[\d\.]+/webfont.js|i', substr($webfont_js, strpos($webfont_js,'//')), $content); | |
/* | |
gravatar imgs: | |
<img src="http://1.gravatar.com/avatar/11fee321889526d1df2393655f48bd0c?s=26&d=retro&r=g"> | |
<img src="https://secure.gravatar.com/avatar/06a2950d128ec9faf155e28d9e889baa?s=120"> | |
*/ | |
/* v1 bak | |
$regexp = "/(\d+|www|secure|cn).gravatar.com\/avatar/i"; | |
$content = preg_replace($regexp, 'sdn.geekzu.org/avatar', $content); | |
*/ | |
$regexp = "/<img([^<>]+)>/i"; | |
$content = preg_replace_callback( | |
$regexp, | |
"useso_take_over_google_str_handler2", | |
$content | |
); | |
return apply_filters('useso_take_over_google_content_filter_after', $content); | |
} | |
function useso_take_over_google_str_handler($matches) | |
{ | |
$str = $matches[0]; | |
if (!is_ssl()) { | |
$str = str_ireplace('//fonts.googleapis'.'.com/', '//fonts.useso.com/', $str); | |
$str = str_ireplace('//ajax.googleapis'.'.com/', '//ajax.useso.com/', $str); | |
//$str = str_ireplace('//fonts.googleapis'.'.com/', '//fonts.geekzu.org/', $str); | |
//$str = str_ireplace('//ajax.googleapis'.'.com/', '//fdn.geekzu.org/ajax/', $str); | |
} else { | |
//$str = str_ireplace('//fonts.googleapis'.'.com/', '//fonts.lug.ustc.edu.cn/', $str); | |
//$str = str_ireplace('//ajax.googleapis'.'.com/', '//ajax.lug.ustc.edu.cn/', $str); | |
$str = str_ireplace('//fonts.googleapis'.'.com/', '//fonts.geekzu.org/', $str); | |
$str = str_ireplace('//ajax.googleapis'.'.com/', '//sdn.geekzu.org/ajax/', $str); | |
} | |
/* | |
fix below references: | |
https://fonts.useso.com/.... | |
https://ajax.useso.com/.... | |
*/ | |
if (!is_ssl()) { | |
$str = str_ireplace('https://fonts.useso.com/', 'http://fonts.useso.com/', $str); | |
$str = str_ireplace('https://ajax.useso.com/', 'http://ajax.useso.com/', $str); | |
$str = str_ireplace('https://fonts.geekzu.org/', 'http://fonts.geekzu.org/', $str); | |
$str = str_ireplace('https://fdn.geekzu.org/ajax/', 'http://fdn.geekzu.org/ajax/', $str); | |
} else { | |
//... | |
} | |
return $str; | |
} | |
function useso_take_over_google_str_handler2($matches) | |
{ | |
$str = $matches[0]; | |
$regexp = "/(\d+|www|secure|cn|s).gravatar.com\/avatar/i"; | |
$str = preg_replace($regexp, 'sdn.geekzu.org/avatar', $str); | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment