一键修改优化代码脚本 | 支持Zibll任意版本-zibll教程分享社区-zibll子比主题-WordPress主题模板-zibll子比主题

一键修改优化代码脚本 | 支持Zibll任意版本

相关阅读

优化新窗口打开相关问题 优化移动端体验教程-WordPress主题模板-zibll子比主题

杂谈

之前分享了几期优化的教程,但是每次子比更新之后,行数都会有所调整,导致很多热心网友,修改起来非常费劲,再加上本人技术水平有限,也不会写插件😂

所以,我们想到一个更加有趣的方式,借助ChatGPT编写了一个替换代码的快捷脚本,一方面可以做到自动备份修改前的文件为*_bak.php,同时源码的基础上修改不会对性能造成任何影响!

 

使用方法

打包版运行

你如果使用打包版程序,请将auto.exe放入zibll主题的根目录中

image-22

运行auto.exe

image-23

 

源码运行

您如果会使用Python可以将源码放置到主题的路径下

image-19

使用Python运行auto.py

image-20

按照需求,选择1-5,如果一键全部修改,您可以选择5然后回车

image-21

工具下载

Python源码

auto.py

# -*- coding: utf-8 -*-
import re
import shutil
import os


def replace_code(src_file: str, bak_file: str, pattern: str, repl: str) -> None:
    # 备份原代码文件,仅在第一次调用时备份
    if not os.path.exists(bak_file):
        shutil.copy(src_file, bak_file)

    # 读取WordPress代码文件,使用utf-8编码
    with open(src_file, 'r', encoding='utf-8') as f:
        code = f.read()

    # 使用正则表达式搜索要被替换的代码
    code = re.sub(pattern, repl, code)

    # 写入替换后的代码文件,使用utf-8编码
    with open(src_file, 'w', encoding='utf-8') as f:
        f.write(code)


if __name__ == '__main__':
    print("""
    ********************************************************************

    ███████╗ █████╗ ███╗   ██╗ ██████╗██╗   ██╗██████╗ ██╗ ██████╗ 
    ██╔════╝██╔══██╗████╗  ██║██╔════╝╚██╗ ██╔╝██╔══██╗██║██╔════╝ 
    █████╗  ███████║██╔██╗ ██║██║      ╚████╔╝ ██████╔╝██║██║  ███╗
    ██╔══╝  ██╔══██║██║╚██╗██║██║       ╚██╔╝  ██╔═══╝ ██║██║   ██║
    ██║     ██║  ██║██║ ╚████║╚██████╗   ██║   ██║     ██║╚██████╔╝
    ╚═╝     ╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝   ╚═╝   ╚═╝     ╚═╝ ╚═════╝ 

    工具来源:FancyPig's blog
    网站地址:https://www.iculture.cc
    ********************************************************************
    1 【一键修改代码,支持自动打卡功能】
    2 【一键修改代码,优化刷新按钮功能】
    3 【一键修改代码,优化打开页面窗口】 
    4 【一键修改代码,支持论坛虚拟阅读】
    5 【一键修改代码,执行上述全部优化】
        """)

    selection=int(input('请输入数字:'))
    if selection==1:
        src_file = 'template/comments.php'
        bak_file = 'template/comments_bak.php'
        pattern = r'if\s*\(_pz\(\'comment_smilie\'\)\)\s*{\s*echo\s*zib_get_input_expand_but\(\'smilie\'\);\s*}'
        repl = '									//用户自动打卡\n									if (_pz(\'comment_signin\')) {\n									echo zib_get_input_expand_but(\'signin\');\n									}\n									if (_pz(\'comment_smilie\')) {\n									echo zib_get_input_expand_but(\'smilie\');\n									}'
        replace_code(src_file, bak_file, pattern, repl)

        src_file = 'inc/functions/bbs/inc/comment.php'
        bak_file = 'inc/functions/bbs/inc/comment_bak.php'
        pattern = r'if\s*\(_pz\(\'bbs_comment_smilie\'\,\s*true\)\)\s*{\s*\$html\s*\.=\s*zib_get_input_expand_but\(\'smilie\'\);\s*}'
        repl = '    //用户快速打卡\n    if (_pz(\'bbs_comment_signin\', true)) {\n        $html .= zib_get_input_expand_but(\'signin\');\n    }\n    if (_pz(\'bbs_comment_smilie\', true)) {\n        $html .= zib_get_input_expand_but(\'smilie\');\n    }'
        replace_code(src_file, bak_file, pattern, repl)

        src_file = 'inc/options/admin-options.php'
        bak_file = 'inc/options/admin-options_bak.php'

        replace_code(
                'inc/options/admin-options.php',
                'inc/options/admin-options_bak.php',
                'array\(\s*\'dependency\'\s*=>\s*array\(\s*\'close_comments\'\s*,\s*\'==\'\s*,\s*\'\'\s*,\s*\'\'\s*,\s*\'visible\'\s*\)\s*,\s*\'id\'\s*=>\s*\'comment_smilie\'\s*,\s*\'help\'\s*=>\s*\'[^\']*\'\s*,\s*\'type\'\s*=>\s*\'switcher\'\s*,\s*\'default\'\s*=>\s*true\s*,\s*\'title\'\s*=>\s*__\(\'允许插入表情\'\s*,\s*\'zib_language\'\)\s*,\s*\)\s*,\s*',
                'array(\n                \'dependency\' => array(\'close_comments\', \'==\', \'\', \'\', \'visible\'),\n                \'id\'         => \'comment_smilie\',\n                \'help\'       => \'为了防止恶意评论,建议在后台-设置-讨论:开启"用户必须登录后才能发表评论"\',\n                \'type\'       => \'switcher\',\n                \'default\'    => true,\n                \'title\'      => __(\'允许插入表情\', \'zib_language\'),\n            ),\n            // 允许打卡\n            array(\n                \'dependency\' => array(\'close_comments\', \'==\', \'\', \'\', \'visible\'),\n                \'id\'         => \'comment_signin\',\n                \'help\'       => \'允许打卡,懒人必备\',\n                \'type\'       => \'switcher\',\n                \'default\'    => true,\n                \'title\'      => __(\'允许用户快速打卡\', \'zib_language\'),\n            ),\n')

        src_file = 'inc/functions/bbs/admin/option.php'
        bak_file = 'inc/functions/bbs/admin/option_bak.php'

        pattern = r"\n\s*array\(\n\s*'id'\s*=>\s*'bbs_comment_smilie',\n\s*'type'\s*=>\s*'switcher',\n\s*'default'\s*=>\s*true,\n\s*'title'\s*=>\s*__\('允许插入表情',\s*'zib_language'\),\n\s*\),\n"
        repl = "\n            array(\n                'id'      => 'bbs_comment_smilie',\n                'type'    => 'switcher',\n                'default' => true,\n                'title'   => __('允许插入表情', 'zib_language'),\n            ),\n            array(\n                'id'      => 'bbs_comment_signin',\n                'type'    => 'switcher',\n                'default' => true,\n                'title'   => __('允许用户快速打卡', 'zib_language'),\n            ),\n"

        replace_code(src_file, bak_file, pattern, repl)

        replace_code("inc/functions/functions.php", "inc/functions/functions_bak.php",
                     r"if\s*\(\s*!is_user_logged_in\(\)\s*\)\s*\{\s*\$upload\s*=\s*false\s*;\s*\}",
                     "if (!is_user_logged_in()) {\n        $upload = false;\n    }\n    //打卡\n    if ('signin' == $type) {\n        $but = '<a class=\"but input-signin mr6\" href=\"javascript:fancypig.simple.daka();\"><i class=\"fa fa-check-square-o\"></i><span class=\"hide-sm\">打卡</span></a>';\n    }")


        print("自动打卡,已修改完毕!")

    elif selection == 2:
        replace_code('inc/functions/zib-footer.php', 'inc/functions/zib-footer_bak.php',
                     r'(\$btn \.= \'<\' \. \$tag \. \$style \. \' class="\' \. \$class \. \'"\' \. \$target \. \$title \. \' href=")(.*)(">\' \. \$icon \. \$hover \. \'</\' \. \$tag \. \'>\';)',
                     r'\g<1>javascript:location.reload();\g<3>')
        print("优化刷新按钮,已修改完毕!")

    elif selection == 3:
        src_file = "inc/functions/zib-theme.php"
        bak_file = "inc/functions/zib-theme_bak.php"
        pattern = r'function _post_target_blank\(\)\n{\n\s*return _pz\(\'target_blank\'\) \? \' target="_blank"\' \: \'\';\n}'
        repl = '''function _post_target_blank()
        {
            if (!wp_is_mobile()) {
                return _pz('target_blank') ? ' target="_blank"' : '';
            }

        }'''

        replace_code(src_file, bak_file, pattern, repl)

        src_file = "inc/functions/bbs/inc/plate.php"
        bak_file = "inc/functions/bbs/inc/plate_bak.php"
        pattern = r'\$target_blank\s+=\s+_pz\(\'plate_target_blank\'\) \? \' target="_blank"\' \: \'\';'
        repl = '''    if (!wp_is_mobile()) {
                $target_blank = _pz('plate_target_blank') ? ' target="_blank"' : '';
            }'''

        replace_code(src_file, bak_file, pattern, repl)

        src_file = "inc/functions/bbs/inc/posts.php"
        bak_file = "inc/functions/bbs/inc/posts_bak.php"
        pattern = r'\$target_blank\s+=\s+_pz\(\'posts_target_blank\'\) \&\& \$post_status \!== \'trash\' \? \' target="_blank"\' \: \'\';'
        repl = '''    if (!wp_is_mobile()) {
                $target_blank = _pz('posts_target_blank') && $post_status !== 'trash' ? ' target="_blank"' : '';
            }'''

        replace_code(src_file, bak_file, pattern, repl)
        print("优化打开页面,已修改完毕!")


    elif selection == 4:
        src_file = "inc/functions/bbs/inc/posts.php"
        bak_file = "inc/functions/bbs/inc/posts_bak.php"
        pattern1 = r"\$views\s+=\s+_cut_count\(get_post_meta\(\$posts_id,\s+'views',\s+true\)\);\s+//查看数量"
        repl1 = r"$views = rand(500,999) + (int)(_cut_count(get_post_meta($posts_id, 'views', true))); //查看数量"
        pattern2 = r"\$views\s+=\s+get_post_meta\(\$posts_id,\s+'views',\s+true\);\s+//查看"
        repl2 = r"$views = rand(500,999) + (int)get_post_meta($posts_id, 'views', true); //查看"
        replace_code(src_file, bak_file, pattern1, repl1)
        replace_code(src_file, bak_file, pattern2, repl2)

        src_file = "inc/functions/bbs/inc/single.php"
        bak_file = "inc/functions/bbs/inc/single_bak.php"
        pattern = r"\$views\s+=\s+get_post_meta\(\$posts_id,\s+'views',\s+true\);\s+//查看"
        repl = r"$views = rand(500,999) + (int)get_post_meta($posts_id, 'views', true); //查看"

        replace_code(src_file, bak_file, pattern, repl)
        print("虚拟阅读,已修改完毕!")

    elif selection == 5:
        # 支持自动打卡,开始
        src_file = 'template/comments.php'
        bak_file = 'template/comments_bak.php'
        pattern = r'if\s*\(_pz\(\'comment_smilie\'\)\)\s*{\s*echo\s*zib_get_input_expand_but\(\'smilie\'\);\s*}'
        repl = '									//用户自动打卡\n									if (_pz(\'comment_signin\')) {\n									echo zib_get_input_expand_but(\'signin\');\n									}\n									if (_pz(\'comment_smilie\')) {\n									echo zib_get_input_expand_but(\'smilie\');\n									}'
        replace_code(src_file, bak_file, pattern, repl)

        src_file = 'inc/functions/bbs/inc/comment.php'
        bak_file = 'inc/functions/bbs/inc/comment_bak.php'
        pattern = r'if\s*\(_pz\(\'bbs_comment_smilie\'\,\s*true\)\)\s*{\s*\$html\s*\.=\s*zib_get_input_expand_but\(\'smilie\'\);\s*}'
        repl = '    //用户快速打卡\n    if (_pz(\'bbs_comment_signin\', true)) {\n        $html .= zib_get_input_expand_but(\'signin\');\n    }\n    if (_pz(\'bbs_comment_smilie\', true)) {\n        $html .= zib_get_input_expand_but(\'smilie\');\n    }'
        replace_code(src_file, bak_file, pattern, repl)

        src_file = 'inc/options/admin-options.php'
        bak_file = 'inc/options/admin-options_bak.php'

        replace_code(
                'inc/options/admin-options.php',
                'inc/options/admin-options_bak.php',
                'array\(\s*\'dependency\'\s*=>\s*array\(\s*\'close_comments\'\s*,\s*\'==\'\s*,\s*\'\'\s*,\s*\'\'\s*,\s*\'visible\'\s*\)\s*,\s*\'id\'\s*=>\s*\'comment_smilie\'\s*,\s*\'help\'\s*=>\s*\'[^\']*\'\s*,\s*\'type\'\s*=>\s*\'switcher\'\s*,\s*\'default\'\s*=>\s*true\s*,\s*\'title\'\s*=>\s*__\(\'允许插入表情\'\s*,\s*\'zib_language\'\)\s*,\s*\)\s*,\s*',
                'array(\n                \'dependency\' => array(\'close_comments\', \'==\', \'\', \'\', \'visible\'),\n                \'id\'         => \'comment_smilie\',\n                \'help\'       => \'为了防止恶意评论,建议在后台-设置-讨论:开启"用户必须登录后才能发表评论"\',\n                \'type\'       => \'switcher\',\n                \'default\'    => true,\n                \'title\'      => __(\'允许插入表情\', \'zib_language\'),\n            ),\n            // 允许打卡\n            array(\n                \'dependency\' => array(\'close_comments\', \'==\', \'\', \'\', \'visible\'),\n                \'id\'         => \'comment_signin\',\n                \'help\'       => \'允许打卡,懒人必备\',\n                \'type\'       => \'switcher\',\n                \'default\'    => true,\n                \'title\'      => __(\'允许用户快速打卡\', \'zib_language\'),\n            ),\n')

        src_file = 'inc/functions/bbs/admin/option.php'
        bak_file = 'inc/functions/bbs/admin/option_bak.php'

        pattern = r"\n\s*array\(\n\s*'id'\s*=>\s*'bbs_comment_smilie',\n\s*'type'\s*=>\s*'switcher',\n\s*'default'\s*=>\s*true,\n\s*'title'\s*=>\s*__\('允许插入表情',\s*'zib_language'\),\n\s*\),\n"
        repl = "\n            array(\n                'id'      => 'bbs_comment_smilie',\n                'type'    => 'switcher',\n                'default' => true,\n                'title'   => __('允许插入表情', 'zib_language'),\n            ),\n            array(\n                'id'      => 'bbs_comment_signin',\n                'type'    => 'switcher',\n                'default' => true,\n                'title'   => __('允许用户快速打卡', 'zib_language'),\n            ),\n"

        replace_code(src_file, bak_file, pattern, repl)

        replace_code("inc/functions/functions.php", "inc/functions/functions_bak.php",
                     r"if\s*\(\s*!is_user_logged_in\(\)\s*\)\s*\{\s*\$upload\s*=\s*false\s*;\s*\}",
                     "if (!is_user_logged_in()) {\n        $upload = false;\n    }\n    //打卡\n    if ('signin' == $type) {\n        $but = '<a class=\"but input-signin mr6\" href=\"javascript:fancypig.simple.daka();\"><i class=\"fa fa-check-square-o\"></i><span class=\"hide-sm\">打卡</span></a>';\n    }")

        print("自动打卡,已修改完毕!")
        #支持自动打卡,结束

        #优化刷新按钮,开始
        replace_code('inc/functions/zib-footer.php', 'inc/functions/zib-footer_bak.php',
                     r'(\$btn \.= \'<\' \. \$tag \. \$style \. \' class="\' \. \$class \. \'"\' \. \$target \. \$title \. \' href=")(.*)(">\' \. \$icon \. \$hover \. \'</\' \. \$tag \. \'>\';)',
                     r'\g<1>javascript:location.reload();\g<3>')
        print("优化刷新按钮,已修改完毕!")
        #优化刷新按钮,结束

        #优化打开页面,开始
        src_file = "inc/functions/zib-theme.php"
        bak_file = "inc/functions/zib-theme_bak.php"
        pattern = r'function _post_target_blank\(\)\n{\n\s*return _pz\(\'target_blank\'\) \? \' target="_blank"\' \: \'\';\n}'
        repl = '''function _post_target_blank()
        {
            if (!wp_is_mobile()) {
                return _pz('target_blank') ? ' target="_blank"' : '';
            }

        }'''

        replace_code(src_file, bak_file, pattern, repl)

        src_file = "inc/functions/bbs/inc/plate.php"
        bak_file = "inc/functions/bbs/inc/plate_bak.php"
        pattern = r'\$target_blank\s+=\s+_pz\(\'plate_target_blank\'\) \? \' target="_blank"\' \: \'\';'
        repl = '''    if (!wp_is_mobile()) {
                $target_blank = _pz('plate_target_blank') ? ' target="_blank"' : '';
            }'''

        replace_code(src_file, bak_file, pattern, repl)

        src_file = "inc/functions/bbs/inc/posts.php"
        bak_file = "inc/functions/bbs/inc/posts_bak.php"
        pattern = r'\$target_blank\s+=\s+_pz\(\'posts_target_blank\'\) \&\& \$post_status \!== \'trash\' \? \' target="_blank"\' \: \'\';'
        repl = '''    if (!wp_is_mobile()) {
                $target_blank = _pz('posts_target_blank') && $post_status !== 'trash' ? ' target="_blank"' : '';
            }'''

        replace_code(src_file, bak_file, pattern, repl)
        print("优化打开页面,已修改完毕!")
        #优化打开页面,结束

        #论坛虚拟阅读,开始
        src_file = "inc/functions/bbs/inc/posts.php"
        bak_file = "inc/functions/bbs/inc/posts_bak.php"
        pattern1 = r"\$views\s+=\s+_cut_count\(get_post_meta\(\$posts_id,\s+'views',\s+true\)\);\s+//查看数量"
        repl1 = r"$views = rand(500,999) + (int)(_cut_count(get_post_meta($posts_id, 'views', true))); //查看数量"
        pattern2 = r"\$views\s+=\s+get_post_meta\(\$posts_id,\s+'views',\s+true\);\s+//查看"
        repl2 = r"$views = rand(500,999) + (int)get_post_meta($posts_id, 'views', true); //查看"
        replace_code(src_file, bak_file, pattern1, repl1)
        replace_code(src_file, bak_file, pattern2, repl2)

        src_file = "inc/functions/bbs/inc/single.php"
        bak_file = "inc/functions/bbs/inc/single.bak.php"
        pattern = r"\$views\s+=\s+get_post_meta\(\$posts_id,\s+'views',\s+true\);\s+//查看"
        repl = r"$views = rand(500,999) + (int)get_post_meta($posts_id, 'views', true); //查看"

        replace_code(src_file, bak_file, pattern, repl)

        print("论坛虚拟阅读,已修改完毕!")
        #论坛虚拟阅读,结束




        # src_file = "inc/functions/zib-footer.php"
        # bak_file = "inc/functions/zib-footer_bak.php"
        # pattern = r"case 'theme_mode':[\s\S]+?break;"
        # repl = r"case 'theme_mode':\n                $btn .= '<a' . $style . ' class=\"float-btn toggle-theme hover-show\"' . $tooltip . ' data-placement=\"left\" title=\"切换主题\" href=\"javascript:;\"><i class=\"fa fa-toggle-theme\"></i>\n                </a>';\n                break;\n            case 'chatwidget':\n                $btn .= '<a' . $style . ' class=\"float-btn chat-button\"' . $tooltip . ' data-placement=\"left\" title=\"在线沟通\"><i class=\"fa fa-handshake-o\"></i></a>';\n                break;"
        #
        # replace_code(src_file, bak_file, pattern, repl)

 

请登录后发表评论