yoone-snow/yoone-snow.php

131 lines
4.7 KiB
PHP

<?php
/*
Plugin Name: Yoone Snow
Description: 首页 canvas 雪花效果
Version: 1.1.0
Author: Yoone
*/
if (!defined('ABSPATH')) { exit; }
function yoone_snow_is_enabled() {
return function_exists('is_front_page') ? is_front_page() : false;
}
function yoone_snow_enqueue_assets() {
if (!yoone_snow_is_enabled()) { return; }
$style_handle = 'yoone-snow-style';
$style_src = plugins_url('css/snow.css', __FILE__);
wp_register_style($style_handle, $style_src, array(), '1.1.0', 'all');
wp_enqueue_style($style_handle);
$script_handle = 'yoone-snow-script';
$script_src = plugins_url('js/snow-canvas.js', __FILE__);
wp_register_script($script_handle, $script_src, array(), '1.1.0', true);
wp_enqueue_script($script_handle);
// 将后端设置传递到前端脚本 变量名称为 YooneSnowSettings
$shape = get_option('yoone_snow_shape', 'dot');
if (!in_array($shape, array('dot', 'flake', 'mixed'), true)) {
// 如果选项值不合法则回退到默认值 dot
$shape = 'dot';
}
wp_localize_script($script_handle, 'YooneSnowSettings', array(
'shape' => $shape,
));
}
function yoone_snow_render_overlay() {
if (!yoone_snow_is_enabled()) { return; }
static $yoone_snow_rendered = false;
if ($yoone_snow_rendered) { return; }
$yoone_snow_rendered = true;
echo '<canvas id="effectiveAppsSnow" aria-hidden="true"></canvas>';
}
add_action('wp_enqueue_scripts', 'yoone_snow_enqueue_assets');
add_action('wp_body_open', 'yoone_snow_render_overlay');
add_action('wp_footer', 'yoone_snow_render_overlay', 100);
// 注册设置页面和设置项 用于选择雪花形状
function yoone_snow_register_settings() {
// 注册设置项 选项名称为 yoone_snow_shape 默认值为 dot
register_setting('yoone_snow_options', 'yoone_snow_shape', array(
'type' => 'string',
'sanitize_callback' => function($value) {
// 对提交的值进行校验 只允许 dot flake mixed 三种
$allowed = array('dot', 'flake', 'mixed');
if (!is_string($value)) { return 'dot'; }
return in_array($value, $allowed, true) ? $value : 'dot';
},
'default' => 'dot',
));
// 添加设置分区 标题为 Snow Settings
add_settings_section(
'yoone_snow_section',
'Snow Settings',
function() {
// 输出分区描述 使用英文标点保证兼容
echo '<p>Configure snow appearance</p>';
},
'yoone_snow'
);
// 添加设置字段 下拉选择雪花形状
add_settings_field(
'yoone_snow_shape',
'Snow Shape',
function() {
// 渲染选择框 选项值 dot flake mixed
$current = get_option('yoone_snow_shape', 'dot');
echo '<select name="yoone_snow_shape" id="yoone_snow_shape">';
echo '<option value="dot"' . selected($current, 'dot', false) . '>Dot</option>';
echo '<option value="flake"' . selected($current, 'flake', false) . '>Snowflake</option>';
echo '<option value="mixed"' . selected($current, 'mixed', false) . '>Mixed</option>';
echo '</select>';
echo '<p class="description">Choose dot snowflake or mixed</p>';
},
'yoone_snow',
'yoone_snow_section'
);
}
// 添加设置页面到后台菜单 条目在设置菜单下
function yoone_snow_add_settings_page() {
add_options_page(
'Yoone Snow',
'Yoone Snow',
'manage_options',
'yoone_snow',
function() {
// 渲染设置页面 表单提交到 options.php
echo '<div class="wrap">';
echo '<h1>Yoone Snow</h1>';
echo '<form method="post" action="options.php">';
settings_fields('yoone_snow_options');
do_settings_sections('yoone_snow');
submit_button();
echo '</form>';
echo '</div>';
}
);
}
// 在 admin 初始化时注册设置 在 admin 菜单挂载页面
add_action('admin_init', 'yoone_snow_register_settings');
add_action('admin_menu', 'yoone_snow_add_settings_page');
// 在插件列表行添加 Settings 链接 指向设置页面
function yoone_snow_plugin_action_links($links) {
// 构造设置页面链接 使用 admin_url 保证后台路径正确
$settingsUrl = admin_url('options-general.php?page=yoone_snow');
$settingsLink = '<a href="' . esc_url($settingsUrl) . '">Settings</a>';
// 将设置链接插入到最前面 便于用户点击
array_unshift($links, $settingsLink);
return $links;
}
// 绑定到当前插件的 action links 钩子 使用 plugin_basename 计算插件标识
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'yoone_snow_plugin_action_links');