标签: WordPress建站

  • 【网站】通过模板函数及WP设置文件配置SMTP邮件服务

    为什么我们需要配置邮件服务?

    第一,配置邮件服务可以避免WP中勾选邮件通知但是后台未配置导致的评论发送延迟。第二,可以让找回密码等服务可用。(也就是你要是没有配置服务所谓的密码找回是无效的,当然有自己服务器的就无所谓了,有更加快捷的方法重置密码)

    服务器注意事项

    前提1:服务器端需要开放465端口。

    前提2:防火墙开放465端口。

    采用465端口是因为云服务器的提供商普遍禁25端口,尝试解封了一下没有什么卵用。

    因此我们可以使用465端口进行邮件服务,国内可以直接使用QQ邮箱,在设置中获取授权码、服务器地址等配置基础信息。Hotmail需要配置另外一套验证机制,有点麻烦了,还是建议使用QQ邮箱,响应速度很快。

    配置步骤

    一般分为两个步骤,对wp-config文件和function模板函数进行修改,其实也可以直接配置模板函数,不过理论上通过WP配置文件配置更加安全,因为权限更高。

    WP-Config:

    // SMTP 配置
    define('HOST', 'smtp.qq.com');// smtp服务器
    define('PORT', 465);// 端口
    define('USERNAME', 'email@qq.com'); // 设置邮箱
    define('PASSWORD', 'password'); // 授权码
    define('SECURE', 'ssl'); // ssl
    define('FROM', 'your_qq_email@qq.com'); // 邮箱
    define('FROM_NAME', 'Name'); // 发件人名称

    模板函数:

    function custom_phpmailer_settings($phpmailer) {
    $phpmailer->isSMTP();
    $phpmailer->Host = HOST;
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port =PORT;
    $phpmailer->Username = USERNAME;
    $phpmailer->Password = PASSWORD;
    $phpmailer->SMTPSecure = SECURE;
    $phpmailer->From = FROM;
    $phpmailer->FromName = FROM_NAME;
    }
    add_action('phpmailer_init', 'custom_phpmailer_settings');
  • 【小贴士】从Apache2切换到Nginx的注意事项

    这两天尝试了一下Apache2,感觉还是Nginx的配置更加直观,因此又切换回了Nginx,不过之前删掉了Nginx,俺寻思我Nginx配置那么多次了,重安装还不是轻轻松松,没想到还是遇到了一系列问题。

    1、Nginx及PHP

    重新安装的Nginx默认用户是Nginx,使用PHP-FPM时默认用户要切换成www-data。

    确认PHP端口保持一致。

    2、Cerbot从Apache2切换到Nginx

    删除Apache2的话,Cerbot的配置文件会无法执行操作,我们在重新配置Cerbot时候只需要把其配置文件改为:

    authenticator = nginx
    installer = nginx

    如上即可正确配置证书更新设置。

    3、Fail2Ban之类的conf文件更新

    需要按照Nginx的日志地址进行更新。

    4、读取错误日志进行针对性优化

    这是个很好的习惯!

    总之还是得多上手配置几次,才能知道知识掌握的是否牢固。

  • 【小贴士】WordPress多站点配置错误的解决

    最近配置Wordpress的多站点,初次成功,后续改变了主站点的域名到裸域名,再次配置多站点时候出现五花八门的错误,什么重定向过多,什么连接不上数据库,如果遇到这种情况,那么大概率在重启多站点的时候,你会看到警告:存在已有的多站点数据。

    那么这时候请按照Wordpress的官方多站点配置进行设置和检查,可以很快定位到问题所在。

    如果是使用Apache2进行的网站配置,检查Rewrite模块及模块中的 ‘AllowOverride all’设置。

    重点检查的WP配置文件:wp-config.php

    需要重点检查的数据表:

    wp_blogs

    wp_blogmeta

    wp_blog_versions

    wp_registration_log

    wp_signups

    wp_site

    wp_sitemeta

    其实主要的原因是Wordpress在关闭多站点设置的时候,不会自动对数据库的多站点数据进行清理,所以在重配置的时候会看到警告,也可能会出现花式错误。

    请参考官方链接:

    WP多站点网络创建

    WP多站点网络管理

    WP多站点网络的Debug

    在成功配置多站点后,一定要注意检查网站的固定链接结构!否则在网页中通过固定连接引用的页面会失效。

    如果多站点配置完成出现Cookie的相关错误,或者点击登录、操作需要等待响应等现象,可以配置wp-config.php添加:

    define(‘COOKIE_DOMAIN’, $_SERVER[‘HTTP_HOST’]);
    define(‘COOKIEPATH’, ‘/’);

    刷新站点缓存及本地缓存,重置Cookie后再尝试。

  • 【小贴士】WordPress中锚点注意事项

    开始之前,建议先阅读:WordPress官方关于锚点的介绍

    WordPress在现在的古登堡编辑器中,可以在“侧边栏-高级-HTML锚点”中很方便为页面添加锚点,但是这其中有一些建议和注意事项。

    问题1、锚点的地址设置

    在WP目前的设置中,如果我们在页面中将锚点前加上完整的URL,点击锚点时,(在某些浏览器中)页面跳转会非常突兀,Not elegant。

    示例:

    因此我们在同一页面中只要直接设置“#锚点”,就可以实现顺滑的跳转。

    问题2、锚点的唯一性

    这是接着问题1引申出来的,我们在每个单独页面中肯定是会注意到锚点的唯一性,但是多页面的锚点一旦多起来呢?重复的话会发生什么问题?

    在一般的用户场景这个问题并不显著,但是在WP的合集页面(目录、标签)中,如果我们有多个关于MV的文章,里面每个MV都在各自页面设置了“#MV”的锚点,那么WP在合计页面只能选择第一个文章的“#MV”锚点进行跳转。

    因此,我们可以在结合WP文章的URL结构,在每个锚点单词前加上对应文章URL的数字码,可以一定程度确保每个锚点的唯一性。


    演示区域

    这是一个锚点

  • 【网站】通过nginx为站点启用HTTP2

    启用过程还是比较简单的,但是过程让我学习到,问Ai之前先看官方手册,,,毕竟Ai的知识库不一定是最新的,而且会一本正经的胡说八道!

    过程:

    1、通过nginx -v检查nginx版本,翻阅nginx的官网手册对nginx配置文件进行编辑。

    2、配置文件server部分要注意是:

    server {
    listen 443 ssl;
    http2 on;
    }

    有些教程会告诉你是listen 443 ssl http2;这一语法已经在1.25.1版本之后被弃用了!所以如果你的nginx版本较高,参考官网的语法进行启用!

    3、按照你的需求配置其他选项。

    4、最重要的,编辑完毕使用nginx -t指令测试nginx的配置文件是否通过,然后重启nginx服务即可。

    效果:

    访问网站,检查协议是否为h2。

  • 【Python】使用PIL库进行多格式批量转换WebP并压缩分辨率

    网站将逐步切换到WebP格式图片,今天捣鼓了插件在服务器端替换图片,但WP的媒体库却怎么都搞不定了,媒体库会自动生成很多缩略图用于不同的场景,我不想碰它的缩略图生成效果,因此只写单一的转换代码是没法做出完整的效果的。

    退而求其次使用本地对图片进行处理,该脚本使用PIL库,图片分辨率限制为2560最长/宽,可以处理带透明通道的图片,也可以处理GIF,用下来效果还不错。

    1月8日更新:

    【Python】使用WebP官方库进行WebP转换
    【Python】使用cwebp、gif2webp、exiftool实现保留exif信息的WebP转换

    import tkinter as tk
    from tkinter import filedialog, messagebox
    from PIL import Image, ImageSequence
    import os
    
    def resize_image(img):
        max_size = 2560
        width, height = img.size
    
        if width > max_size or height > max_size:
            if width > height:
                new_width = max_size
                new_height = int((new_width / width) * height)
            else:
                new_height = max_size
                new_width = int((new_height / height) * width)
            
            img = img.resize((new_width, new_height), Image.LANCZOS)
        
        return img
    
    def convert_to_webp(input_path):
        try:
            file_extension = os.path.splitext(input_path)[1].lower()
            output_path = os.path.splitext(input_path)[0] + ".webp"
    
            if not os.path.exists(input_path):
                raise FileNotFoundError(f"文件 {input_path} 不存在,请检查路径。")
    
            if file_extension == '.webp':
                return f"文件 {input_path} 已是 WebP 格式,无需转换。"
    
            with Image.open(input_path) as img:
                if file_extension in ['.gif'] and getattr(img, "is_animated", False):
                    frames = []
                    durations = []
                    for frame in ImageSequence.Iterator(img):
                        # 处理透明度
                        if frame.mode == "P":
                            frame = frame.convert("RGBA")
                        
                        # 转换帧为 RGBA 并存储
                        new_frame = frame.copy()
                        frames.append(new_frame)
                        durations.append(frame.info.get('duration', 100))
    
                    # 重复最后一帧
                    if len(frames) > 1:
                        durations[-1] = max(durations[-1], 100) 
    
                    # 保存为动态 WebP
                    frames[0].save(
                        output_path,
                        format="WEBP",
                        save_all=True,
                        append_images=frames[1:],
                        duration=durations,
                        loop=img.info.get('loop', 0),  # 循环次数
                        transparency=0,  # 确保透明度保留
                        quality=85
                    )
                else:
                    # 静态图片处理
                    if img.mode == "P":
                        if "transparency" in img.info:
                            img = img.convert("RGBA")
                        else:
                            img = img.convert("RGB")
    
                    img = resize_image(img)
    
                    if img.mode != "RGBA":
                        img = img.convert("RGBA")
    
                    img.save(output_path, format="WEBP", quality=85)
    
            return f"图片已转换并保存为 {output_path}"
        except Exception as e:
            return f"处理文件时发生错误: {e}"
    
    
    def select_files():
        file_paths = filedialog.askopenfilenames(
            title="选择图片文件",
            filetypes=[("所有图片格式", "*.jpg;*.jpeg;*.png;*.gif;*.webp;*.bmp;*.tiff"), 
                       ("JPEG 图片", "*.jpg;*.jpeg"),
                       ("PNG 图片", "*.png"),
                       ("GIF 图片", "*.gif"),
                       ("WebP 图片", "*.webp"),
                       ("BMP 图片", "*.bmp"),
                       ("TIFF 图片", "*.tiff")]
        )
        if file_paths:
            for path in file_paths:
                file_listbox.insert(tk.END, path)
    
    def convert_and_save_batch():
        files = file_listbox.get(0, tk.END)
        if not files:
            messagebox.showerror("错误", "请选择至少一个图片文件!")
            return
    
        results = []
        for file_path in files:
            result = convert_to_webp(file_path)
            results.append(result)
    
        messagebox.showinfo("完成", "\n".join(results))
    
    def clear_list():
        file_listbox.delete(0, tk.END)
    
    root = tk.Tk()
    root.title("批量图片转换为 WebP 工具")
    root.geometry("600x400")
    
    frame = tk.Frame(root)
    frame.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)
    
    scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL)
    file_listbox = tk.Listbox(frame, selectmode=tk.EXTENDED, yscrollcommand=scrollbar.set)
    scrollbar.config(command=file_listbox.yview)
    scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
    file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    
    button_frame = tk.Frame(root)
    button_frame.pack(pady=10)
    
    select_button = tk.Button(button_frame, text="选择文件", command=select_files, width=15)
    select_button.grid(row=0, column=0, padx=5)
    
    clear_button = tk.Button(button_frame, text="清空列表", command=clear_list, width=15)
    clear_button.grid(row=0, column=1, padx=5)
    
    convert_button = tk.Button(button_frame, text="批量转换", command=convert_and_save_batch, width=15)
    convert_button.grid(row=0, column=2, padx=5)
    
    root.mainloop()
  • 【WP插件】通过插件形式为WP导入回到顶部功能

    网站此前用的回到顶部按钮是通过插件市场中的插件实现,效果还不错,不过功能比较单一。

    我在其他的博客中看到了一个可以在页面下拉过程中实现当前页面进度的回顶按钮,觉得实用性很棒,因此尝试通过页面脚本的形式导入。

    但是这种方式过于低效,且没法很好自定义,所以便尝试是否可以通过插件的形式导入功能,通过和GPT的一番交流,大致明白了WP插件的制作过程。

    WP的插件可以通过Zip文件导入,其中的结构为:

    backtop/
    ├── assets/
    │ ├── backtop.css
    │ └── backtop.js
    ├── backtop.php
    └── readme.txt (可选)

    其中backtop.php是插件的核心文件,包含了插件的主要功能和初始化代码。

    backtop.css为样式表,js则是JavaScript 文件,写交互逻辑使用。

    参考代码:

    PHP:

    <?php
    /*
    Plugin Name: 写你的插件名称
    Description: 描述
    Version: 版本号
    Author: 作者
    Author URI: https://wanxuefeiyang.cn
    License: GPL2
    */
    
    // Enqueue CSS and JS,注意地址
    function backtop_enqueue_assets() {
        wp_enqueue_style('backtop-style', plugin_dir_url(__FILE__) . 'assets/backtop.css');
        wp_enqueue_script('backtop-script', plugin_dir_url(__FILE__) . 'assets/backtop.js', [], false, true);
    
        $options = get_option('backtop_options');
        wp_localize_script('backtop-script', 'backTopOptions', [
            'position' => $options['position'] ?? 'bottom-right',
            'color' => $options['color'] ?? '#000000',
            'size' => $options['size'] ?? '40px',
            'shape' => $options['shape'] ?? 'circle',
            'margin' => $options['margin'] ?? '20px 20px'
        ]);
    }
    add_action('wp_enqueue_scripts', 'backtop_enqueue_assets');
    
    // 插入 HTML
    function backtop_render_html() {
        echo '
        <div id="backtop-tool">
            <ul>
                <li id="backtop" class="hidden">
                    <span id="backtop-percentage">0%</span>
                </li>
            </ul>
        </div>';
    }
    add_action('wp_footer', 'backtop_render_html');
    
    // 设置页面,可以自定义一些内容
    function backtop_add_settings_page() {
        add_options_page(
            'BackTop Settings',
            'BackTop',
            'manage_options',
            'backtop-settings',
            'backtop_render_settings_page'
        );
    }
    add_action('admin_menu', 'backtop_add_settings_page');
    
    function backtop_render_settings_page() {
        ?>
        <div class="wrap">
            <h1>BackTop Settings</h1>
            <form method="post" action="options.php">
                <?php
                settings_fields('backtop_options_group');
                do_settings_sections('backtop-settings');
                submit_button();
                ?>
            </form>
        </div>
        <?php
    }
    
    function backtop_register_settings() {
        register_setting('backtop_options_group', 'backtop_options', [
            'type' => 'array',
            'sanitize_callback' => 'backtop_sanitize_options',
            'default' => [
                'position' => 'bottom-right',
                'color' => '#000000',
                'size' => '40px',
                'shape' => 'circle',
                'margin' => '20px 20px'
            ],
        ]);
    
        add_settings_section('backtop_main_section', 'Main Settings', null, 'backtop-settings');
    
        add_settings_field('position', 'Position', 'backtop_position_field', 'backtop-settings', 'backtop_main_section');
        add_settings_field('color', 'Background Color', 'backtop_color_field', 'backtop-settings', 'backtop_main_section');
        add_settings_field('size', 'Button Size', 'backtop_size_field', 'backtop-settings', 'backtop_main_section');
        add_settings_field('shape', 'Shape', 'backtop_shape_field', 'backtop-settings', 'backtop_main_section');
        add_settings_field('margin', 'Margin', 'backtop_margin_field', 'backtop-settings', 'backtop_main_section');
    }
    add_action('admin_init', 'backtop_register_settings');
    
    function backtop_position_field() {
        $options = get_option('backtop_options');
        ?>
        <select name="backtop_options[position]">
            <option value="bottom-right" <?php selected($options['position'], 'bottom-right'); ?>>Bottom Right</option>
            <option value="bottom-left" <?php selected($options['position'], 'bottom-left'); ?>>Bottom Left</option>
        </select>
        <?php
    }
    
    function backtop_color_field() {
        $options = get_option('backtop_options');
        ?>
        <input type="color" name="backtop_options[color]" value="<?php echo esc_attr($options['color']); ?>">
        <?php
    }
    
    function backtop_size_field() {
        $options = get_option('backtop_options');
        ?>
        <input type="text" name="backtop_options[size]" value="<?php echo esc_attr($options['size']); ?>" placeholder="e.g., 40px">
        <?php
    }
    
    function backtop_shape_field() {
        $options = get_option('backtop_options');
        ?>
        <select name="backtop_options[shape]">
            <option value="circle" <?php selected($options['shape'], 'circle'); ?>>Circle</option>
            <option value="square" <?php selected($options['shape'], 'square'); ?>>Square</option>
        </select>
        <?php
    }
    
    function backtop_margin_field() {
        $options = get_option('backtop_options');
        ?>
        <input type="text" name="backtop_options[margin]" value="<?php echo esc_attr($options['margin']); ?>" placeholder="e.g., 20px 20px">
        <?php
    }
    
    function backtop_sanitize_options($options) {
        $options['position'] = in_array($options['position'], ['bottom-right', 'bottom-left']) ? $options['position'] : 'bottom-right';
        $options['color'] = sanitize_hex_color($options['color']);
        $options['size'] = preg_match('/^\d+(px|em|%)$/', $options['size']) ? $options['size'] : '40px';
        $options['shape'] = in_array($options['shape'], ['circle', 'square']) ? $options['shape'] : 'circle';
        $options['margin'] = sanitize_text_field($options['margin']);
        return $options;
    }

    JS:

    (function () {
      const backTopTool = document.getElementById("backtop-tool");
      const backTopButton = document.getElementById("backtop");
      const percentageDisplay = document.getElementById("backtop-percentage");
    
      if (backTopTool && backTopButton) {
        const { position, color, size, shape, margin } = backTopOptions || {};
        const [vertical, horizontal] = position.split("-");
        const [marginY, marginX] = margin.split(" ");
        backTopTool.style[vertical] = marginY || "20px";
        backTopTool.style[horizontal] = marginX || "20px";
        backTopButton.style.backgroundColor = color || "#000";
        backTopButton.style.width = size || "40px";
        backTopButton.style.height = size || "40px";
        backTopButton.style.borderRadius = shape === "circle" ? "50%" : "0";
        percentageDisplay.style.fontSize = `${Math.max(parseInt(size) * 0.4, 8)}px`;
      }
    
      const updateScrollProgress = () => {
        const scrollTop = window.scrollY;
        const scrollHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
        const scrollPercentage = Math.round((scrollTop / scrollHeight) * 100);
    
        if (percentageDisplay) {
          percentageDisplay.innerHTML = scrollPercentage >= 95 ? "▲" : `${scrollPercentage}%`;
        }
    
        if (backTopButton) {
          backTopButton.classList.toggle("hidden", scrollTop < 200);
        }
      };
    
      const scrollToTop = () => {
        window.scrollTo({ top: 0, behavior: "smooth" });
      };
    
      backTopButton?.addEventListener("click", scrollToTop);
      window.addEventListener("scroll", updateScrollProgress);
    })();

    CSS:

    #backtop-tool {
      position: fixed;
      z-index: 9999;
    }
    
    #backtop-tool ul {
      list-style: none;
      padding: 0;
      margin: 0;
    }
    
    #backtop-tool .hidden {
      display: none;
    }
    
    #backtop-tool li {
      display: flex; 
      justify-content: center; /* 水平居中 */
      align-items: center; /* 垂直居中 */
      width: var(--size, 40px);
      height: var(--size, 40px);
      background: var(--color, rgba(0, 0, 0, 0.7));
      color: #fff;
      border-radius: var(--shape, 50%);
      cursor: pointer;
      font-size: calc(var(--size, 40px) * 0.4); /* 根据按钮大小动态调整字体 */
      text-align: center; /* 对齐数字文本 */
      overflow: hidden;
      box-sizing: border-box;
    }

    管理界面如下:

    从插件制作到实现的过程,给我的感觉是通过插件对页面、功能进行修改,可以避免我们干扰WP核心文件,让页面中的其他功能不受影响,对灵活性、安全性都有一定的增强,这对于我这种半桶水来说非常利好,即便哪里设置错了直接把插件删了就好嘛,可玩性很高。

  • 【HTML】为网站添加WordPress官网飘雪效果

    WordPress官网最近加上了一个飘雪效果,感觉效果相当不错,经过研究后发现他们页面中使用了两个js,而前端只需要用html进行简单调整就可以自定义,可玩性很强。

    我们也可以在自己的网站中使用这个效果,首先在Wordpress官网下载到is-land及snow-fall这俩js。

    通过模板函数对is-land与snow-fall先后加载:

    // js来源自Wordpress官网
    function enqueue_island_assets() {
        wp_enqueue_script(
            'island-js',
            get_template_directory_uri() . '/island.js', 
            true
        );
    }
    add_action('wp_enqueue_scripts', 'enqueue_island_assets');
    wp_enqueue_script(
        'snow-fall-js',
        get_template_directory_uri() . '/snow-fall.js',
        array('island-js'), // 设置依赖
        true
    );

    前端通过html进行调用,我们通过后面的分析可以发现其为前端预留了多个参数调节,比如我想在页面呈现❄飘落,而不是原版的圆点,只需要调整text的值,写成如下即可:

    <is-land on:media="(prefers-reduced-motion: no-preference)" on:idle="">
        <snow-fall mode="page" text="❄"></snow-fall>
    </is-land>

    2025-1-6更新:如果不需要island里面的一些参数,那么直接引入snow-fall也是可以的:

    // island及snowfall的Js文件来自Wordpress官网
    function enqueue_snowfall() {
        wp_enqueue_script(
            'snow-fall-js',
            get_template_directory_uri() . '/snow-fall.js', 
            true
        );
    }
    add_action('wp_enqueue_scripts', 'enqueue_snowfall');
    <snow-fall mode="page" text="❄"></snow-fall>

    目前网站采用直接引入Snow-fall的方式展示。


    后续是源码及一些可以调用的参数信息:

    其中is-land的源码如下:

    class Island extends HTMLElement {
        static tagName = "is-land";
        static prefix = "is-land--";
        static attr = {
            template: "data-island",
            ready: "ready",
            defer: "defer-hydration"
        };
        static onceCache = new Map;
        static onReady = new Map;
        static fallback = {
            ":not(is-land,:defined,[defer-hydration])": (e, t, a) => {
                let n = document.createElement(a + t.localName);
                for (let e of t.getAttributeNames())
                    n.setAttribute(e, t.getAttribute(e));
                let i = t.shadowRoot;
                if (!i) {
                    let e = t.querySelector(":scope > template:is([shadowrootmode], [shadowroot])");
                    if (e) {
                        let a = e.getAttribute("shadowrootmode") || e.getAttribute("shadowroot") || "closed";
                        i = t.attachShadow({
                            mode: a
                        }),
                        i.appendChild(e.content.cloneNode(!0))
                    }
                }
                return i && n.attachShadow({
                    mode: i.mode
                }).append(...i.childNodes),
                n.append(...t.childNodes),
                t.replaceWith(n),
                e.then(( () => {
                    n.shadowRoot && t.shadowRoot.append(...n.shadowRoot.childNodes),
                    t.append(...n.childNodes),
                    n.replaceWith(t)
                }
                ))
            }
        };
        constructor() {
            super(),
            this.ready = new Promise((e => {
                this.readyResolve = e
            }
            ))
        }
        static getParents(e, t=!1) {
            let a = [];
            for (; e; ) {
                if (e.matches && e.matches(Island.tagName)) {
                    if (t && e === t)
                        break;
                    Conditions.hasConditions(e) && a.push(e)
                }
                e = e.parentNode
            }
            return a
        }
        static async ready(e, t) {
            if (t || (t = Island.getParents(e)),
            0 === t.length)
                return;
            let a = await Promise.all(t.map((e => e.wait())));
            return a.length ? a[0] : void 0
        }
        forceFallback() {
            window.Island && Object.assign(Island.fallback, window.Island.fallback);
            for (let e in Island.fallback) {
                let t = Array.from(this.querySelectorAll(e)).reverse();
                for (let a of t) {
                    if (!a.isConnected)
                        continue;
                    let t = Island.getParents(a);
                    if (1 === t.length) {
                        let n = Island.ready(a, t);
                        Island.fallback[e](n, a, Island.prefix)
                    }
                }
            }
        }
        wait() {
            return this.ready
        }
        async connectedCallback() {
            Conditions.hasConditions(this) && this.forceFallback(),
            await this.hydrate()
        }
        getTemplates() {
            return this.querySelectorAll(`template[${Island.attr.template}]`)
        }
        replaceTemplates(e) {
            for (let t of e) {
                if (Island.getParents(t, this).length > 0)
                    continue;
                let e = t.getAttribute(Island.attr.template);
                if ("replace" === e) {
                    let e = Array.from(this.childNodes);
                    for (let t of e)
                        this.removeChild(t);
                    this.appendChild(t.content);
                    break
                }
                {
                    let a = t.innerHTML;
                    if ("once" === e && a) {
                        if (Island.onceCache.has(a))
                            return void t.remove();
                        Island.onceCache.set(a, !0)
                    }
                    t.replaceWith(t.content)
                }
            }
        }
        async hydrate() {
            let e = [];
            this.parentNode && e.push(Island.ready(this.parentNode));
            let t = Conditions.getConditions(this);
            for (let a in t)
                Conditions.map[a] && e.push(Conditions.map[a](t[a], this));
            await Promise.all(e),
            this.replaceTemplates(this.getTemplates());
            for (let e of Island.onReady.values())
                await e.call(this, Island);
            this.readyResolve(),
            this.setAttribute(Island.attr.ready, ""),
            this.querySelectorAll(`[${Island.attr.defer}]`).forEach((e => e.removeAttribute(Island.attr.defer)))
        }
    }
    class Conditions {
        static map = {
            visible: Conditions.visible,
            idle: Conditions.idle,
            interaction: Conditions.interaction,
            media: Conditions.media,
            "save-data": Conditions.saveData
        };
        static hasConditions(e) {
            return Object.keys(Conditions.getConditions(e)).length > 0
        }
        static getConditions(e) {
            let t = {};
            for (let a of Object.keys(Conditions.map))
                e.hasAttribute(`on:${a}`) && (t[a] = e.getAttribute(`on:${a}`));
            return t
        }
        static visible(e, t) {
            if ("IntersectionObserver"in window)
                return new Promise((e => {
                    let a = new IntersectionObserver((t => {
                        let[n] = t;
                        n.isIntersecting && (a.unobserve(n.target),
                        e())
                    }
                    ));
                    a.observe(t)
                }
                ))
        }
        static idle() {
            let e = new Promise((e => {
                "complete" !== document.readyState ? window.addEventListener("load", ( () => e()), {
                    once: !0
                }) : e()
            }
            ));
            return "requestIdleCallback"in window ? Promise.all([new Promise((e => {
                requestIdleCallback(( () => {
                    e()
                }
                ))
            }
            )), e]) : e
        }
        static interaction(e, t) {
            let a = ["click", "touchstart"];
            return e && (a = (e || "").split(",").map((e => e.trim()))),
            new Promise((e => {
                function n(i) {
                    e();
                    for (let e of a)
                        t.removeEventListener(e, n)
                }
                for (let e of a)
                    t.addEventListener(e, n, {
                        once: !0
                    })
            }
            ))
        }
        static media(e) {
            let t = {
                matches: !0
            };
            if (e && "matchMedia"in window && (t = window.matchMedia(e)),
            !t.matches)
                return new Promise((e => {
                    t.addListener((t => {
                        t.matches && e()
                    }
                    ))
                }
                ))
        }
        static saveData(e) {
            if ("connection"in navigator && navigator.connection.saveData !== ("false" !== e))
                return new Promise(( () => {}
                ))
        }
    }
    "customElements"in window && (window.customElements.define(Island.tagName, Island),
    window.Island = Island);
    export {Island, Island as component};
    export const ready = Island.ready;

    通过前端代码调用:

    <is-land on:media="(prefers-reduced-motion: no-preference)" on:idle="" ready="">
    <snow-fall mode="page"></snow-fall>
    </is-land>

    其中可以定义:

    on:media:当满足媒体查询条件时加载内容。

    on:idle:在浏览器空闲时间或页面完全加载后执行。

    on:visible:当标签内容滚动到视口中时加载。

    on:interaction:在用户交互(如点击、触摸)后加载。

    on:save-data:根据设备的省流量模式决定是否加载内容。

    对于snow-fall.js,源码如下:

    class Snow extends HTMLElement {
        static random(t, e) {
            return t + Math.floor(Math.random() * (e - t) + 1)
        }
        static attrs = {
            count: "count",
            mode: "mode",
            text: "text"
        };
        generateCss(t, e) {
            let n = [];
            n.push('\n:host([mode="element"]) {\n\tdisplay: block;\n\tposition: relative;\n\toverflow: hidden;\n}\n:host([mode="page"]) {\n\tposition: fixed;\n\ttop: 0;\n\tleft: 0;\n\tright: 0;\n}\n:host([mode="page"]),\n:host([mode="element"]) > * {\n\tpointer-events: none;\n}\n:host([mode="element"]) ::slotted(*) {\n\tpointer-events: all;\n}\n* {\n\tposition: absolute;\n}\n:host([text]) * {\n\tfont-size: var(--snow-fall-size, 1em);\n}\n:host(:not([text])) * {\n\twidth: var(--snow-fall-size, 10px);\n\theight: var(--snow-fall-size, 10px);\n\tbackground: var(--snow-fall-color, rgba(255,255,255,.5));\n\tborder-radius: 50%;\n}\n');
            let o = {
                width: 100,
                height: 100
            }
              , a = {
                x: "vw",
                y: "vh"
            };
            "element" === t && (o = {
                width: this.firstElementChild.clientWidth,
                height: this.firstElementChild.clientHeight
            },
            a = {
                x: "px",
                y: "px"
            });
            for (let t = 1; t <= e; t++) {
                let e = Snow.random(1, 100) * o.width / 100
                  , s = Snow.random(-10, 10) * o.width / 100
                  , i = Math.round(Snow.random(30, 100))
                  , l = i * o.height / 100
                  , r = o.height
                  , h = 1e-4 * Snow.random(1, 1e4)
                  , d = Snow.random(10, 30)
                  , m = -1 * Snow.random(0, 30);
                n.push(`\n:nth-child(${t}) {\n\topacity: ${.001 * Snow.random(0, 1e3)};\n\ttransform: translate(${e}${a.x}, -10px) scale(${h});\n\tanimation: fall-${t} ${d}s ${m}s linear infinite;\n}\n\n@keyframes fall-${t} {\n\t${i}% {\n\t\ttransform: translate(${e + s}${a.x}, ${l}${a.y}) scale(${h});\n\t}\n\n\tto {\n\t\ttransform: translate(${e + s / 2}${a.x}, ${r}${a.y}) scale(${h});\n\t}\n}`)
            }
            return n.join("\n")
        }
        connectedCallback() {
            if (this.shadowRoot || !("replaceSync"in CSSStyleSheet.prototype))
                return;
            let t, e = parseInt(this.getAttribute(Snow.attrs.count)) || 100;
            this.hasAttribute(Snow.attrs.mode) ? t = this.getAttribute(Snow.attrs.mode) : (t = this.firstElementChild ? "element" : "page",
            this.setAttribute(Snow.attrs.mode, t));
            let n = new CSSStyleSheet;
            n.replaceSync(this.generateCss(t, e));
            let o = this.attachShadow({
                mode: "open"
            });
            o.adoptedStyleSheets = [n];
            let a = document.createElement("div")
              , s = this.getAttribute(Snow.attrs.text);
            a.innerText = s || "";
            for (let t = 0, n = e; t < n; t++)
                o.appendChild(a.cloneNode(!0));
            o.appendChild(document.createElement("slot"))
        }
    }
    customElements.define("snow-fall", Snow);
    

    其定义了一个自定义 HTML 元素 <snow-fall>,用于实现落雪效果。

    我们可以通过改变js中的一些数值来变更效果,例如

    static attrs = { count: “count”, mode: “mode”, text: “text” };

    count:雪花的数量,默认值是 100。

    mode:两种模式:

    1、element:作用于某个特定元素。

    2、page:覆盖整个页面。

    text:雪花的文本内容(默认为空,显示为圆形点)。

    page模式目前已经满足使用,如果想使用element模式目前似乎会出现firstElementChild 为空或未能正确获取其 clientWidth,导致 generateCss 方法出错。如何正确使用element模式还需要探究。

    通过 generateCss(t, e) { 动态生成CSS,可以定义:

    1、:host([mode=”element”]) 和 :host([mode=”page”]) 用于适配不同模式。

    2、雪花的大小由 –snow-fall-size 决定,颜色由 –snow-fall-color 决定。

    动态生成每个雪花的动画样式:

    1、起始位置:translate(${e}${a.x}, -10px)

    2、随机透明度:opacity

    3、动画时长:animation: fall-${t} ${d}s ${m}s linear infinite;

    4、动画帧 @keyframes: 中间位置:随机 x 偏移,随机 y 偏移。最终位置:完全落到底部。

  • 【网站】WordPress2024年终总结——“传承、创新与社区”

    WordPress的2024年终总结会议如期而至,本次会议在日本·东京举办,那么会上讲了哪些有趣的事情呢?让我们(GPT)简单总结一下:

    主题:

    关于本次会议的主题——传承、创新及社区

    Legacy(传承):

    • 回顾 WordPress 自成立以来的历史和文化传承,特别是其开源精神和全球社区的贡献。例如,日本 WordPress 社区早期本地化的贡献、WapuuWordPress的吉祥物,类似皮卡丘的一个动漫形象 的诞生以及这些文化符号如何在全球传播。

    Innovation(创新):

    • 强调 WordPress 在技术层面的持续进步,例如Gutenberg编辑器区块编辑器,好像很多人不喜欢这个)的改进、新的设计和开发工具、以及 AI 辅助编辑的引入(强调虽然AI的速度飞快,但是依然应该认识到AI不是替代开发者而是增强开发者的能力)
    • 聚焦最新的功能,在下文详述。

    Community(社区):

    • 突出 WordPress 社区在全球范围内的成长和合作,包括教育项目、地方活动(如 WordCamps)和基层组织的努力。其他诸如日本社区的重大贡献和 WordPress 在多语言、多文化生态系统中的扩展。

    内容:

    1. 本次大会相关

    • 大会首次在亚洲(东京)举办,象征 WordPress 的全球化发展再进一步。
    • 东京的传承与创新精神被认为与 WordPress 的品牌理念高度契合。
    • 主题围绕 “感性工程 (kansei engineering)” 展开,强调产品设计的直观体验。

    2. 全球及地区增长

    • 全球市场份额增长至 43.6%,CMS的市场份额达 62.3%。
    • 其中日本市场表现尤为突出,主导 58.5% 的网站,并占有 83% 的 CMS 市场份额。
    • 预计到 2025 年,使用非英语语言的 WordPress 网站,例如德语、日语、阿拉伯语等将超过英语语言网站。

    3. 设计与功能创新

    • 推出改进的沉浸模式、拖拽图片创建画廊的功能,提升写作体验。
    • 引入了 “区块绑定 (Block Bindings)” 和 “数据视图 (Data Views)”,增强了动态内容的管理。
    • 区块API 交互 API提供灵活的设计和开发选项。
    • WordPress Playground (一个Wordpress在线网站建设平台)允许用户直接在浏览器中创建和测试站点。

    4. 插件和主题的生态系统

    • 2024 年上传了 1,700 多个新主题,其中包括 1,000 个块主题。
    • 插件下载量激增至 23.5 亿次,同比增加 20%;插件更新量突破 30 亿次。
    • 插件审查流程得以优化,新工具 “Plugin Check” 大幅提高了审核效率。

    5. 社区工作成就

    • WordPress 社区在全球范围内扩展教育计划和基层活动,如印度的 WP Campus Connect 和拉丁美洲的社区项目。
    • 日本社区的贡献显著,包括 DigitalCube 的上市、插件 Contact Form 7 的成功,以及大量本地活动的举办。
    • 突出贡献者Aki Hamano在核心开发方面的表现尤为亮眼。

    6. 未来与展望

    • 强调内容自由和迁移的便捷性,包括通过 WordPress Playground 实现跨平台内容表达。
    • 展示了 WordPress 在 AI 辅助下建站领域的潜力,并重申其以用户创造力为核心的愿景。
  • 【网站】为WordPress启用持久对象存储(Redis及Memcached)

    一、服务器为Debian、采用PHP,使用Redis:

    1、安装Redis:

    apt install redis-server

    2、检查Redis状态

    systemctl status redis-server

    3、一切无误后,启用插件进行管理,推荐使用插件——Redis Object Cache,配置完成后即可实现启用持久对象存储。

    如果想使用 Object Cache Pro,则需要配置PHP Redis,在此不详述,对一般网站和用户来说Redis Object Cache已经足够。


    二、服务器为Debian、采用PHP,使用Memcached:

    1、安装Memcached:

    apt install memcached libmemcached-tools

    2、安装Memcached PHP 扩展

    apt install php-memcached

    检查扩展是否成功安装:

    php -m | grep memcached 

    检查Memcached状态:

    systemctl status memcached

    检查端口状态:

    netstat -plntu | grep memcached

    3、一切无误后,启用插件进行管理,这是由于PHP版本持续更新,而网上的一些WordPress 对象缓存文件并不能对最新版本的PHP完美兼容,因此推荐使用 W3 Total Cache插件进行启用,配置完成后即可实现启用持久对象存储。