first commit
This commit is contained in:
43
includes/admin-options.php
Normal file
43
includes/admin-options.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
die;
|
||||
}
|
||||
|
||||
if (class_exists('CSF'))
|
||||
{
|
||||
$prefix = 'zib_plugin_demo';
|
||||
|
||||
//开始构建
|
||||
CSF::createOptions($prefix, array(
|
||||
'menu_title' => '演示插件',
|
||||
'menu_slug' => 'zib_plugin_demo',
|
||||
'framework_title' => '演示插件',
|
||||
'show_in_customizer' => true,
|
||||
'footer_text' => '由李初一开发的演示插件',
|
||||
'footer_credit' => '<i class="fa fa-fw fa-heart-o" aria-hidden="true"></i> ',
|
||||
'theme' => 'light',
|
||||
));
|
||||
|
||||
CSF::createSection($prefix, array(
|
||||
'id' => 'add',
|
||||
'title' => '测试功能',
|
||||
'icon' => 'fa fa-gitlab',
|
||||
'fields' => array(
|
||||
array(
|
||||
'title' => ' ',
|
||||
'id' => 'demo_func',
|
||||
'default' => false,
|
||||
'subtitle' => __('网站全局变灰', 'zib_language'),
|
||||
'type' => 'switcher',
|
||||
),
|
||||
array(
|
||||
'dependency' => array('demo_func', '!=', ''),
|
||||
'content' => '测试保存和功能拦截,就算已保存删除链接后同样不生效',
|
||||
'style' => 'danger',
|
||||
'type' => 'submessage',
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
}
|
||||
254
includes/functions.php
Normal file
254
includes/functions.php
Normal file
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
// 启用 Link Manager(如果未启用)
|
||||
if (!get_option('link_manager_enabled')) {
|
||||
add_option('link_manager_enabled', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测是否存在指定链接
|
||||
*
|
||||
* 该函数用于检测目标 URL 是否已经存在于友情链接列表中,并且是非私密状态。
|
||||
*
|
||||
* @param string $target_url 要检测的目标链接 URL,默认为 'https://www.vxras.com'
|
||||
* @return bool 如果目标链接存在且非私密状态返回 true,否则返回 false
|
||||
*/
|
||||
function zib_has_link($target_url = 'https://www.vxras.com') {
|
||||
// 获取所有友情链接,包括私密和待审核状态的链接
|
||||
$links = get_bookmarks(array(
|
||||
'orderby' => 'id',
|
||||
'order' => 'ASC',
|
||||
'hide_invisible' => false, // 显示所有状态的链接
|
||||
));
|
||||
|
||||
// 遍历所有友情链接
|
||||
foreach ($links as $link) {
|
||||
// 检查链接是否以目标 URL 开头,并且是公开状态(非私密)
|
||||
if (strpos($link->link_url, $target_url) === 0 && $link->link_visible === 'Y') {
|
||||
return true; // 目标链接存在且非私密状态
|
||||
}
|
||||
}
|
||||
|
||||
return false; // 目标链接不存在或为私密状态
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测链接是否为私密状态
|
||||
*
|
||||
* 该函数用于检测目标 URL 是否存在于友情链接列表中,并且是私密状态。
|
||||
*
|
||||
* @param string $target_url 要检测的目标链接 URL,默认为 'https://www.vxras.com'
|
||||
* @return bool 如果目标链接为私密状态返回 true,否则返回 false
|
||||
*/
|
||||
function zib_is_link_private($target_url = 'https://www.vxras.com') {
|
||||
// 获取所有友情链接,包括私密和待审核状态的链接
|
||||
$links = get_bookmarks(array(
|
||||
'orderby' => 'id',
|
||||
'order' => 'ASC',
|
||||
'hide_invisible' => false, // 显示所有状态的链接
|
||||
));
|
||||
|
||||
// 遍历所有友情链接
|
||||
foreach ($links as $link) {
|
||||
// 检查链接是否以目标 URL 开头,并且是私密状态
|
||||
if (strpos($link->link_url, $target_url) === 0 && $link->link_visible === 'N') {
|
||||
return true; // 目标链接为私密状态
|
||||
}
|
||||
}
|
||||
|
||||
return false; // 目标链接不存在或为非私密状态
|
||||
}
|
||||
|
||||
/**
|
||||
* 在后台显示提示信息和一键添加按钮
|
||||
*
|
||||
* 该函数会根据当前友情链接的状态,在 WordPress 后台显示相应的提示信息和操作按钮。
|
||||
* 如果链接为私密状态,则提示解除私密;如果链接不存在,则提示添加链接。
|
||||
*/
|
||||
function zib_check_and_prompt_link() {
|
||||
$target_url = 'https://www.vxras.com';
|
||||
|
||||
// 检查是否已存在该链接
|
||||
if (zib_is_link_private($target_url)) {
|
||||
// 如果链接为私密状态,显示提示信息
|
||||
echo '<div class="notice notice-warning is-dismissible">';
|
||||
echo '<h2><i class="dashicons-before dashicons-lock"></i> 友情链接为私密状态</h2>';
|
||||
echo '<p>您已添加的友情链接 <strong>' . esc_html($target_url) . '</strong> 当前为私密状态,插件功能将不可用。请解除私密状态,或点击下方按钮解除私密。</p>';
|
||||
echo '<ajaxform class="ajax-form" ajax-url="' . admin_url('admin-ajax.php') . '">';
|
||||
echo '<a href="javascript:;" class="ajax-submit but jb-green" style="margin-bottom: 20px;">解除私密</a>';
|
||||
echo '<div class="ajax-notice mt6"></div>';
|
||||
echo '<input type="hidden" ajax-name="action" value="zib_make_link_public">';
|
||||
echo '</ajaxform>';
|
||||
echo '</div>';
|
||||
return; // 直接返回,避免继续执行后续逻辑
|
||||
} elseif (!zib_has_link($target_url)) {
|
||||
// 输出提示信息和一键添加按钮
|
||||
echo '<div class="notice notice-error is-dismissible">';
|
||||
echo '<h2 style="color:#f73d3f;"><i class="dashicons-before dashicons-admin-settings"></i> 请添加友情链接</h2>';
|
||||
echo '<p>您还未将初一小盏添加到友情链接,插件功能将不可用,插件后台也将无法保存,请及时将初一小盏添加到您的友情链接列表,或点击下方按钮一键添加。</p>';
|
||||
echo '<ajaxform class="ajax-form" ajax-url="' . admin_url('admin-ajax.php') . '">';
|
||||
echo '<a href="javascript:;" class="ajax-submit but jb-blue" style="margin-bottom: 20px;">一键添加</a>';
|
||||
echo '<div class="ajax-notice mt6"></div>';
|
||||
echo '<input type="hidden" ajax-name="action" value="zib_add_custom_link">';
|
||||
echo '</ajaxform>';
|
||||
echo '</div>';
|
||||
return;
|
||||
}
|
||||
}
|
||||
add_action('admin_notices', 'zib_check_and_prompt_link');
|
||||
|
||||
/**
|
||||
* 通过 AJAX 处理一键添加逻辑
|
||||
*
|
||||
* 该函数通过 AJAX 请求实现一键添加友情链接的功能。
|
||||
* 它会检查分类情况,优先将链接添加到链接最多的分类中。如果没有分类,则自动创建一个名为“友情链接”的分类。
|
||||
*/
|
||||
function zib_handle_add_custom_link() {
|
||||
// 定义要添加的链接信息
|
||||
$link_name = '初一小盏';
|
||||
$link_url = 'https://www.vxras.com';
|
||||
$link_description = '初一小盏致力于推广网络教学知识,打造同行业最具影响力的交流平台,为广大网络知识爱好者分享网站源码,教程,工具,技巧等网络相关文章与视频!';
|
||||
$link_image = 'https://www.vxras.com/favicon.ico'; // Logo 图像地址
|
||||
$link_rss = ''; // RSS 地址
|
||||
$link_notes = ''; // 备注
|
||||
$link_rating = 10; // 评级
|
||||
$link_visible = 'Y'; // 可见性
|
||||
|
||||
// 检查是否已经存在该链接
|
||||
if (zib_has_link($link_url)) {
|
||||
zib_send_json_error('链接已存在,无需重复添加!');
|
||||
}
|
||||
|
||||
// 获取所有链接分类及其链接数量
|
||||
$categories = get_terms(array(
|
||||
'taxonomy' => 'link_category',
|
||||
'hide_empty' => false, // 包括空分类
|
||||
));
|
||||
|
||||
$max_links_count = 0;
|
||||
$category_id = null;
|
||||
|
||||
if (!empty($categories) && !is_wp_error($categories)) {
|
||||
// 遍历分类,找到链接最多的分类
|
||||
foreach ($categories as $category) {
|
||||
$links_count = count(get_bookmarks(array(
|
||||
'category' => $category->term_id,
|
||||
'hide_invisible' => false, // 包括私密和待审核链接
|
||||
)));
|
||||
|
||||
if ($links_count > $max_links_count) {
|
||||
$max_links_count = $links_count;
|
||||
$category_id = $category->term_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有分类或所有分类都没有链接,创建一个新的分类
|
||||
if (is_null($category_id)) {
|
||||
$new_category = wp_insert_term('友情链接', 'link_category');
|
||||
if (is_wp_error($new_category)) {
|
||||
zib_send_json_error('创建分类失败,请稍后重试!');
|
||||
}
|
||||
$category_id = $new_category['term_id'];
|
||||
}
|
||||
|
||||
// 插入新的友情链接
|
||||
$link_data = array(
|
||||
'link_name' => $link_name,
|
||||
'link_url' => $link_url,
|
||||
'link_description' => $link_description,
|
||||
'link_image' => $link_image,
|
||||
'link_rss' => $link_rss,
|
||||
'link_notes' => $link_notes,
|
||||
'link_rating' => $link_rating,
|
||||
'link_category' => array($category_id),
|
||||
'link_visible' => $link_visible,
|
||||
);
|
||||
|
||||
$result = wp_insert_link($link_data);
|
||||
|
||||
if ($result) {
|
||||
zib_send_json_success('链接已成功添加!');
|
||||
} else {
|
||||
zib_send_json_error('链接添加失败,请稍后重试!');
|
||||
}
|
||||
}
|
||||
add_action('wp_ajax_zib_add_custom_link', 'zib_handle_add_custom_link');
|
||||
|
||||
/**
|
||||
* 通过 AJAX 处理解除私密逻辑
|
||||
*
|
||||
* 该函数通过 AJAX 请求实现解除友情链接私密状态的功能。
|
||||
* 它会查找目标链接并将其可见性设置为公开状态。
|
||||
*/
|
||||
function zib_make_link_public() {
|
||||
$target_url = 'https://www.vxras.com';
|
||||
|
||||
$links = get_bookmarks(array(
|
||||
'orderby' => 'id',
|
||||
'order' => 'ASC',
|
||||
'hide_invisible' => false, // 显示所有状态的链接
|
||||
));
|
||||
|
||||
foreach ($links as $link) {
|
||||
if (strpos($link->link_url, $target_url) === 0 && $link->link_visible === 'N') {
|
||||
// 更新链接为可见状态
|
||||
$link_data = array(
|
||||
'link_id' => $link->link_id,
|
||||
'link_visible' => 'Y',
|
||||
);
|
||||
|
||||
$result = wp_update_link($link_data);
|
||||
|
||||
if ($result) {
|
||||
zib_send_json_success('链接已解除私密状态!');
|
||||
} else {
|
||||
zib_send_json_error('解除私密状态失败,请稍后重试!');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
zib_send_json_error('未找到需要解除私密状态的链接!');
|
||||
}
|
||||
add_action('wp_ajax_zib_make_link_public', 'zib_make_link_public');
|
||||
|
||||
/**
|
||||
* 演示功能
|
||||
*
|
||||
* 该函数会在前端页面应用灰度滤镜效果,模拟演示模式。
|
||||
* 它会检测是否启用了演示功能,并在符合条件时应用样式。
|
||||
*/
|
||||
function demo_func() {
|
||||
// 检测链接,如果没有友情链接,则直接返回
|
||||
if (!zib_has_link()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查后台开关是否开启
|
||||
if (!zib_plugin_demo('demo_func')) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo '<style>body > .header, body > .container, body > .footer { -webkit-filter: grayscale(100%); filter: grayscale(100%); }</style>';
|
||||
}
|
||||
add_action('wp_head', 'demo_func');
|
||||
|
||||
/**
|
||||
* 拦截 CSF 菜单保存
|
||||
*
|
||||
* 该函数用于拦截 CSF(Codestar Framework)菜单保存操作。
|
||||
* 如果友情链接为私密状态或不存在,则阻止保存并显示提示信息。
|
||||
*
|
||||
* @param array $data 传入的表单数据
|
||||
* @return array 返回修改后的表单数据
|
||||
*/
|
||||
function zib_csf_save_before($data)
|
||||
{
|
||||
if (zib_is_link_private()) {
|
||||
wp_send_json_success(array('errors' => array(), "notice" => "<i class='fa fa-info-circle fa-fw'></i> 请先解除私密状态!"));
|
||||
} elseif (!zib_has_link()) {
|
||||
wp_send_json_success(array('errors' => array(), "notice" => "<i class='fa fa-info-circle fa-fw'></i> 请先添加友情链接!"));
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
add_action('csf_zib_plugin_demo_save', 'zib_csf_save_before');
|
||||
35
index.php
Normal file
35
index.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/*
|
||||
Plugin Name: 子比主题·演示插件
|
||||
Plugin URI: https://www.vxras.com/
|
||||
Description: 子比主题定制插件
|
||||
Version: 1.0.0
|
||||
Author: 李初一
|
||||
Author URI: https://www.vxras.com/
|
||||
*/
|
||||
|
||||
// 定义插件常量
|
||||
define('zib_plugin_demo_url', plugins_url('', __FILE__));
|
||||
define('zib_plugin_demo_path', plugin_dir_path(__FILE__));
|
||||
|
||||
// 获取插件菜单设置
|
||||
if (!function_exists('zib_plugin_demo')) {
|
||||
function zib_plugin_demo($option = '', $default = null) {
|
||||
$options = get_option('zib_plugin_demo');
|
||||
return (isset($options[$option])) ? $options[$option] : $default;
|
||||
}
|
||||
}
|
||||
|
||||
// 在主题启用后引入文件
|
||||
function zib_plugin_demo_init() {
|
||||
|
||||
$require_once = array(
|
||||
'includes/functions.php',
|
||||
'includes/admin-options.php',
|
||||
);
|
||||
|
||||
foreach ( $require_once as $require ) {
|
||||
require_once plugin_dir_path( __FILE__ ) . $require;
|
||||
}
|
||||
}
|
||||
add_action( 'after_setup_theme', 'zib_plugin_demo_init' );
|
||||
Reference in New Issue
Block a user