wp清理

<?php
/**
Plugin Name: unify 优化123
Plugin URI: 
Description: 扩展优化wp
Version: 1.0
Author URI: 
*/

/*定义一些常量*/
define('UNIFY_VER', wp_get_theme()->get('Version'));

//define('UNIFY_URL', get_template_directory_uri());//主题模式
//define('UNIFY_URL', get_template_directory_uri());//子主题模式
define('UNIFY_URL', get_template_directory_uri());//插件模式

/**
 * 后台净化
 *
*/
/* ------------------------------------------------------ */
// Remove help and screen context &amp; Options 移除控制台中的【帮助】和【屏幕选项】
// http://wordpress.stackexchange.com/questions/73561/how-to-remove-all-widgets-from-dashboard
add_filter( 'contextual_help', 'wpse_25034_remove_dashboard_help_tab', 999, 3 );
add_filter( 'screen_options_show_screen', 'wpse_25034_remove_help_tab' );
 
function wpse_25034_remove_dashboard_help_tab( $old_help, $screen_id, $screen )
{
    if( 'dashboard' != $screen->base )
    return $old_help;
    $screen->remove_help_tabs();
    return $old_help;
}
 
function wpse_25034_remove_help_tab( $visible )
{
    global $current_screen;
    if( 'dashboard' == $current_screen->base )
    return false;
    return $visible;
}
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
// Disable the Admin Bar.移除 WordPress 网站的管理工具条
// From: http://yoast.com/disable-wp-admin-bar/
add_filter( 'show_admin_bar', '__return_false' );
 
function sp_hide_admin_bar_settings()
{
    ?><style type="text/css">.show-admin-bar {display: none;}</style><?php
}
 
function sp_disable_admin_bar()
{
    add_filter( 'show_admin_bar', '__return_false' );
    add_action( 'admin_print_scripts-profile.php', 'sp_hide_admin_bar_settings' );
}
add_action( 'init', 'sp_disable_admin_bar' , 9 );
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
// Select Performers Admin Button
// This function creates a subtle button on the bottom left of the screen when logged in
// When you click on the button a simple WP admin bar appears
// Note, you should hide the standard WP bar
// You need Font Awesome installed to show the button
/*创建一个简单管理按钮。这个管理按钮,将会在你登录 WordPress 后台之后,出现在你的网站前台的左下角,并不太明显。根据你使用主题的不同,或许你需要对这段代码进行适当的微调。我使用 WordPress Twenty Fourteen 主题测试,效果很好,鼠标划到左下角的时候才能看到这个管理按钮,只有三个链接:退出登录、控制台(仪表盘)、编辑页面。*/
function sp_custom_login()
{
    $current_user = wp_get_current_user();
    $displayName = $current_user->display_name;
    $logout = wp_logout_url("/");
 
    if(is_user_logged_in())
    {
        echo '<style>
        .adminEdit {position:fixed;bottom:20px;left:10px; z-index:9999; display:none;}
        .adminEdit p { background:#dedede; opacity:0.8; padding:10px; border:#ababab 1px solid; font-size:0.9em; border-radius:5px;}
        #admin-menu-show { position:fixed; bottom:-50px; left:-50px; border-radius:50px;width:100px; height:100px; background-color:#333; opacity:0.2; z-index:9999;}
        #admin-menu-show:hover {opacity:0.7;}
        #admin-menu-show:active {opacity:1; background-color:#900;}
        #admin-menu-show i { color:#fff; font-size: 90px;padding-left: 10px;display: block;margin-top: 10px;}
        #admin-menu-show:hover {cursor:pointer;}
        </style>';
        echo "<script>jQuery(document).ready(function ($) { $('#admin-menu-show').click(function() { $('.adminEdit').toggle('fast');});});</script>";
    }
 
    if ( current_user_can('edit_post'))
    {
        edit_post_link("<i class=\"icon-edit\"></i> Edit Page","<div class=\"adminEdit\"><p><i class=\"icon-user\"></i> $displayName logged in | <a href=\"$logout\" title=\"Log Out\"><i class=\"icon-signout\"></i> Log Out</a> | <a href=\"/wp-admin/\" title=\"Go to Dashboard\"><i class=\"icon-cog\"></i> Dashboard</a> | ","</p></div>");
        ?><div id="admin-menu-show" title="Show Admin Bar"><i class="icon-cog"></i></div><?php
    }
    elseif(is_user_logged_in())
    {
        echo "<div class=\"adminEdit\"><p><i class=\"icon-user\"></i> $displayName logged in | <a href=\"$logout\" title=\"Log Out\"><i class=\"icon-signout\"></i> Log Out</a>";
        ?><div id="admin-menu-show" title="Show Admin Bar"><i class="icon-cog"></i></div><?php
    }
}
add_action('wp_footer', 'sp_custom_login');
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
// Remove Dashboard Widgets移除控制台中讨厌的挂件
// http://digwp.com/2010/10/customize-wordpress-dashboard/
 
function disable_default_dashboard_widgets()
{
    // disable default dashboard widgets
    remove_meta_box('dashboard_right_now', 'dashboard', 'core');
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'core');
    remove_meta_box('dashboard_incoming_links', 'dashboard', 'core');
    remove_meta_box('dashboard_plugins', 'dashboard', 'core');
    remove_meta_box('dashboard_recent_drafts', 'dashboard', 'core');
    remove_meta_box('dashboard_quick_press', 'dashboard', 'core');
    remove_meta_box('dashboard_recent_drafts', 'dashboard', 'core');
    remove_meta_box('dashboard_primary', 'dashboard', 'core');
    remove_meta_box('dashboard_secondary', 'dashboard', 'core');
    remove_meta_box('rg_forms_dashboard', 'dashboard', 'normal;');
    remove_meta_box('blc_dashboard_widget', 'dashboard', 'normal;');
    remove_meta_box('powerpress_dashboard_news', 'dashboard', 'normal;');
    // disable Simple:Press dashboard widget
    remove_meta_box('sf_announce', 'dashboard', 'normal');
}
add_action('admin_menu', 'disable_default_dashboard_widgets');
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
// Stop TinyMCE in WordPress 3.x messing up your HTML code在WordPress 3.x+版本中防止TinyMCE编辑器弄乱代码
// http://www.leighton.com/blog/stop-tinymce-in-wordpress-3-x-messing-up-your-html-code
function override_mce_options($initArray)
{
    $opts = '*[*]';
    $initArray['valid_elements'] = $opts;
    $initArray['extended_valid_elements'] = $opts;
    return $initArray;
}
add_filter('tiny_mce_before_init', 'override_mce_options');
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
// Remove Clutter by hiding widgets using CSS
/*移除 W3 Totacl Cache 插件(WordPress缓存插件),Better WP Security插件(WordPress安全插件),以及 Yoast WordPress SEO 插件(SEO著名插件)所生成的无用内容的:*/

add_action('admin_head', 'wp_remove_clutter');
 
function wp_remove_clutter()
{
    echo '<style>
    .toplevel_page_better-wp-security .side, #w3tc-dashboard-widgets, #wpseo_content_top + .postbox-container {display:none;}
    </style>';
}
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
/* Convert absolute URLs in content to site relative ones将你的WordPress网站中的文章和页面的域名,从绝对网址转换为相对网址,代码来自Thisismyurl.com:
Inspired by http://thisismyurl.com/6166/replace-wordpress-static-urls-dynamic-urls/
*/
function sp_clean_static_url($content) {
    $thisURL = get_bloginfo('url');
    $stuff = str_replace(' src=\"'.$thisURL, ' src=\"', $content );
    $stuff = str_replace(' href=\"'.$thisURL, ' href=\"', $stuff );
    return $stuff;
}
add_filter('content_save_pre','sp_clean_static_url','99');
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
// Add confirmation dialogue box when publishing posts/pages发布文章和页面时,添加确认框
// https://gist.github.com/plasticmind/4337952
/* = Add a "molly guard" to the publish button */
 
add_action( 'admin_print_footer_scripts', 'sr_publish_molly_guard' );
function sr_publish_molly_guard() {
    echo "
    <script>
        jQuery(document).ready(function($){
            $('#publishing-action input[name=\"publish\"]').click(function() {
                if(confirm('Are you sure you want to publish this?')) {
                    return true;
                } else {
                    $('#publishing-action .spinner').hide();
                    $('#publishing-action img').hide();
                    $(this).removeClass('button-primary-disabled');
                    return false;
                }
                });
            });
    </script>
    ";
}
/* ------------------------------------------------------ */
// ------------------------------------------------------------------------------
// Include Featured Image &amp; Add Link Back To Original Post添加特色图片,并在RSS feed中链接回原文
// http://www.paulund.co.uk/7-tips-to-improve-your-wordpress-rss-feed
// ------------------------------------------------------------------------------
function feed_copyright_disclaimer($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        $featuredImage = '<p style="text-align:center;"><a href="' . get_permalink() . '">' . get_the_post_thumbnail($post->ID,"medium-large").'</a></p>';
    }
    $content = $featuredImage."<p>".get_the_excerpt()."</p>".'<p><a href="' . get_permalink() . '">Read all of this article</a> on the website.</p>';
    return $content;
}
 
add_filter('the_excerpt_rss','feed_copyright_disclaimer');
add_filter('the_content_feed','feed_copyright_disclaimer');
// ------------------------------------------------------------------------------
/* ------------------------------------------------------ */
// Filter to replace the shortcode text with HTML5 compliant code让WordPress中的图片说明支持HTML5和响应式设计
// http://codex.wordpress.org/Function_Reference/add_filter
function my_img_caption_shortcode_filter($val, $attr, $content = null)
{
    extract(shortcode_atts(array(
    'id' => '',
    'align' => '',
    'width' => '',
    'caption' => ''
    ), $attr));
 
    if ( 1 > (int) $width || empty($caption) )
        return $val;
 
    $capid = '';
    if ( $id ) {
        $id = esc_attr($id);
        $capid = 'id="figcaption_'. $id . '" ';
        $id = 'id="' . $id . '" aria-labelledby="figcaption_' . $id . '" ';
    }
 
    return '<figure ' . $id . 'class="wp-caption ' . esc_attr($align) . '" >'
    . do_shortcode( $content ) . '<figcaption ' . $capid
    . 'class="wp-caption-text">' . $caption . '</figcaption></figure>';
}
 
add_filter('img_caption_shortcode', 'my_img_caption_shortcode_filter',10,3);
/* ------------------------------------------------------ */
add_filter('content_save_pre', 'auto_save_image');
function auto_save_image($content) {
    $upload_path = '';
    $upload_url_path = get_bloginfo('url');
    //上传目录
    if (($var = get_option('upload_path')) !=''){
        $upload_path = $var;
    } else {
        $upload_path = 'wp-content/uploads';
    }
    if(get_option('uploads_use_yearmonth_folders')) {
        $upload_path .= '/'.date("Y",time()).'/'.date("m",time());
    }
    //文件地址
    if(($var = get_option('upload_url_path')) != '') {
        $upload_url_path = $var;
    } else {
        $upload_url_path = bloginfo('url');
    }
    if(get_option('uploads_use_yearmonth_folders')) {
        $upload_url_path .= '/'.date("Y",time()).'/'.date("m",time());
    }
    require_once ("../wp-includes/class-snoopy.php");
    $snoopy_Auto_Save_Image = new Snoopy;
    $img = array();
    //以文章的标题作为图片的标题
    if ( !empty( $_REQUEST['post_title'] ) )
        $post_title = wp_specialchars( stripslashes( $_REQUEST['post_title'] ));
    $text = stripslashes($content);
    if (get_magic_quotes_gpc()) $text = stripslashes($text);
    preg_match_all("/ src=(\"|\'){0,}(http:\/\/(.+?))(\"|\'|\s)/is",$text,$img);
    $img = array_unique(dhtmlspecialchars($img[2]));
    foreach ($img as $key => $value){
        set_time_limit(180); //每个图片最长允许下载时间,秒
        if(str_replace(get_bloginfo('url'),"",$value)==$value&&str_replace(get_bloginfo('home'),"",$value)==$value){
            //判断是否是本地图片,如果不是,则保存到服务器
            $fileext = substr(strrchr($value,'.'),1);
            $fileext = strtolower($fileext);
            if($fileext==""||strlen($fileext)>4)
                $fileext = "jpg";
            $savefiletype = array('jpg','gif','png','bmp');
            if (in_array($fileext, $savefiletype)){
                if($snoopy_Auto_Save_Image->fetch($value)){
                    $get_file = $snoopy_Auto_Save_Image->results;
                }else{
                    echo "error fetching file: ".$snoopy_Auto_Save_Image->error."<br>";
                    echo "error url: ".$value;
                    die();
                }
                $filetime = time();
                $filepath = "/".$upload_path;//图片保存的路径目录
                !is_dir("..".$filepath) ? mkdirs("..".$filepath) : null;
                //$filename = date("His",$filetime).random(3);
                $filename = substr($value,strrpos($value,'/'),strrpos($value,'.')-strrpos($value,'/'));
                //$e = '../'.$filepath.$filename.'.'.$fileext;
                //if(!is_file($e)) {
                // copy(htmlspecialchars_decode($value),$e);
                //}
                $fp = @fopen("..".$filepath.$filename.".".$fileext,"w");
                @fwrite($fp,$get_file);
                fclose($fp);
                $wp_filetype = wp_check_filetype( $filename.".".$fileext, false );
                $type = $wp_filetype['type'];
                $post_id = (int)$_POST['temp_ID2'];
                $title = $post_title;
                $url = $upload_url_path.$filename.".".$fileext;
                $file = $_SERVER['DOCUMENT_ROOT'].$filepath.$filename.".".$fileext;
                //添加数据库记录
                $attachment = array(
                    'post_type' => 'attachment',
                    'post_mime_type' => $type,
                    'guid' => $url,
                    'post_parent' => $post_id,
                    'post_title' => $title,
                    'post_content' => '',
                );
                $id = wp_insert_attachment($attachment, $file, $post_parent);
                $text = str_replace($value,$url,$text); //替换文章里面的图片地址
            }
        }
    }0
    $content = AddSlashes($text);
    remove_filter('content_save_pre', 'auto_save_image');
    return $content;
}
function mkdirs($dir){
    if(!is_dir($dir)){
        mkdirs(dirname($dir));
        mkdir($dir);
    }
    return ;
}
function dhtmlspecialchars($string) {
    if(is_array($string)) {
        foreach($string as $key => $val) {
            $string[$key] = dhtmlspecialchars($val);
        }
    }else{
        $string = str_replace('&', '&', $string);
        $string = str_replace('"', '"', $string);
        $string = str_replace('<', '<', $string);
        $string = str_replace('>', '>', $string);
        $string = preg_replace('/&(#\d;)/', '&\1', $string);
    }
    return $string;
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值