/**
* Plugin Name: WP文章自动刷新插件
* Plugin URI: https://www.imwpweb.com/
* Description: 真正意义上的文章翻新插件,模拟手工重新发布,让老旧文章重新获得搜索引擎青睐,提升网站流量
* Version: 1.0.0
* Author: IMWPWEB
* Author URI: https://www.imwpweb.com/
* Text Domain: wp-article-refresh
* Domain Path: /languages
* License: GPL v2 or later
*/
if (!defined('ABSPATH')) {
exit;
}
// 插件常量定义
define('WP_ARTICLE_REFRESH_VERSION', '1.0.0');
define('WP_ARTICLE_REFRESH_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WP_ARTICLE_REFRESH_PLUGIN_URL', plugin_dir_url(__FILE__));
/**
* 主插件类
*/
class WP_Article_Refresh {
/**
* 单例实例
*/
private static $instance = null;
/**
* 获取单例实例
*/
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* 构造函数
*/
private function __construct() {
// 注册激活/停用钩子
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
// 初始化插件
add_action('init', array($this, 'init'));
// 添加定时任务钩子
add_action('wp_article_refresh_event', array($this, 'do_refresh'));
}
/**
* 插件激活
*/
public function activate() {
// 设置默认选项
$default_options = array(
'enabled' => 1,
'refresh_interval' => 60, // 分钟
'days_range' => 365, // 1年内
'daily_limit' => 10, // 每天最多翻新10篇
'time_window_start' => '00:00',
'time_window_end' => '23:59',
'categories' => array(), // 空数组表示所有分类
'exclude_categories' => array(),
'refresh_mode' => 'today', // today:今天, random:随机前几天, gradual:逐渐递增
'refreshed_today' => 0,
'last_refresh_date' => date('Y-m-d'),
'total_refreshed' => 0
);
if (!get_option('wp_article_refresh_options')) {
add_option('wp_article_refresh_options', $default_options);
}
// 设置定时任务
if (!wp_next_scheduled('wp_article_refresh_event')) {
wp_schedule_event(time(), 'wp_article_refresh_interval', 'wp_article_refresh_event');
}
// 刷新重写规则
flush_rewrite_rules();
}
/**
* 插件停用
*/
public function deactivate() {
// 清除定时任务
wp_clear_scheduled_hook('wp_article_refresh_event');
// 刷新重写规则
flush_rewrite_rules();
}
/**
* 初始化插件
*/
public function init() {
// 加载文本域
load_plugin_textdomain('wp-article-refresh', false, dirname(plugin_basename(__FILE__)) . '/languages');
// 添加自定义定时间隔
add_filter('cron_schedules', array($this, 'add_cron_interval'));
// 添加管理菜单
add_action('admin_menu', array($this, 'add_admin_menu'));
// 注册AJAX处理
add_action('wp_ajax_wp_article_refresh', array($this, 'handle_ajax'));
// 添加设置链接
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_settings_link'));
// 加载前端资源(仅在插件页面加载)
add_action('admin_enqueue_scripts', array($this, 'enqueue_assets'));
}
/**
* 添加自定义Cron间隔
*/
public function add_cron_interval($schedules) {
$options = get_option('wp_article_refresh_options');
$interval = isset($options['refresh_interval']) ? $options['refresh_interval'] : 60;
$schedules['wp_article_refresh_interval'] = array(
'interval' => $interval * 60,
'display' => sprintf(__('每 %d 分钟', 'wp-article-refresh'), $interval)
);
return $schedules;
}
/**
* 添加管理菜单
*/
public function add_admin_menu() {
add_menu_page(
__('文章自动刷新', 'wp-article-refresh'),
__('文章刷新', 'wp-article_refresh'),
'manage_options',
'wp-article-refresh',
array($this, 'render_admin_page'),
'dashicons-update',
30
);
}
/**
* 渲染管理页面
*/
public function render_admin_page() {
// 处理表单提交
if (isset($_POST['wp_article_refresh_nonce']) && wp_verify_nonce($_POST['wp_article_refresh_nonce'], 'wp_article_refresh_save')) {
$this->save_options();
echo '
' . __('设置已保存!', 'wp-article-refresh') . '
';
}
// 处理手动刷新
if (isset($_POST['wp_article_refresh_manual']) && wp_verify_nonce($_POST['wp_article_refresh_manual_nonce'], 'wp_article_refresh_manual')) {
$result = $this->do_refresh();
if ($result) {
echo '' . sprintf(__('成功刷新了 %d 篇文章!', 'wp-article-refresh'), $result) . '
';
} else {
echo '' . __('没有找到符合条件的文章或已达今日上限!', 'wp-article-refresh') . '
';
}
}
$options = get_option('wp_article_refresh_options');
$stats = $this->get_statistics();
$categories = get_categories(array('hide_empty' => false));
include WP_ARTICLE_REFRESH_PLUGIN_DIR . 'templates/admin-page.php';
}
/**
* 保存设置选项
*/
private function save_options() {
$options = array(
'enabled' => isset($_POST['enabled']) ? 1 : 0,
'refresh_interval' => absint($_POST['refresh_interval']),
'days_range' => absint($_POST['days_range']),
'daily_limit' => absint($_POST['daily_limit']),
'time_window_start' => sanitize_text_field($_POST['time_window_start']),
'time_window_end' => sanitize_text_field($_POST['time_window_end']),
'categories' => isset($_POST['categories']) ? array_map('absint', $_POST['categories']) : array(),
'exclude_categories' => isset($_POST['exclude_categories']) ? array_map('absint', $_POST['exclude_categories']) : array(),
'refresh_mode' => sanitize_text_field($_POST['refresh_mode']),
'refreshed_today' => isset($_POST['reset_count']) ? 0 : get_option('wp_article_refresh_options', array())
);
// 获取之前的设置
$prev_options = get_option('wp_article_refresh_options', array());
$options['refreshed_today'] = isset($prev_options['refreshed_today']) ? $prev_options['refreshed_today'] : 0;
$options['last_refresh_date'] = isset($prev_options['last_refresh_date']) ? $prev_options['last_refresh_date'] : date('Y-m-d');
$options['total_refreshed'] = isset($prev_options['total_refreshed']) ? $prev_options['total_refreshed'] : 0;
update_option('wp_article_refresh_options', $options);
// 重新设置定时任务
wp_clear_scheduled_hook('wp_article_refresh_event');
if ($options['enabled']) {
wp_schedule_event(time(), 'wp_article_refresh_interval', 'wp_article_refresh_event');
}
}
/**
* 执行文章刷新
*/
public function do_refresh() {
$options = get_option('wp_article_refresh_options');
// 检查插件是否启用
if (!$options['enabled']) {
return 0;
}
// 检查时间窗口
$current_time = current_time('H:i');
if ($current_time < $options['time_window_start'] || $current_time > $options['time_window_end']) {
return 0;
}
// 重置每日计数
$today = date('Y-m-d');
if ($options['last_refresh_date'] !== $today) {
$options['refreshed_today'] = 0;
$options['last_refresh_date'] = $today;
}
// 检查每日限制
if ($options['refreshed_today'] >= $options['daily_limit']) {
return 0;
}
// 获取符合条件的文章
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $options['daily_limit'] - $options['refreshed_today'],
'date_query' => array(
'before' => date('Y-m-d H:i:s', strtotime('-1 day')),
'after' => date('Y-m-d H:i:s', strtotime('-' . $options['days_range'] . ' days'))
),
'orderby' => 'rand',
'no_found_rows' => true
);
// 分类筛选
if (!empty($options['categories'])) {
$args['category__in'] = $options['categories'];
}
if (!empty($options['exclude_categories'])) {
$args['category__not_in'] = $options['exclude_categories'];
}
$query = new WP_Query($args);
$refreshed = 0;
if ($query->have_posts()) {
foreach ($query->posts as $post) {
// 更新文章日期
$new_date = $this->calculate_new_date($post->post_date, $options['refresh_mode']);
wp_update_post(array(
'ID' => $post->ID,
'post_date' => $new_date,
'post_date_gmt' => get_gmt_from_date($new_date),
'post_modified' => current_time('mysql'),
'post_modified_gmt' => current_time('mysql', 1)
));
// 清理缓存
clean_post_cache($post->ID);
$refreshed++;
}
wp_reset_postdata();
}
// 更新统计
$options['refreshed_today'] += $refreshed;
$options['total_refreshed'] += $refreshed;
update_option('wp_article_refresh_options', $options);
return $refreshed;
}
/**
* 计算新日期
*/
private function calculate_new_date($original_date, $mode) {
$original = strtotime($original_date);
$now = time();
switch ($mode) {
case 'today':
// 设置为今天
return date('Y-m-d H:i:s', time());
case 'random':
// 随机1-7天前
$random_days = wp_rand(1, 7);
$new_time = strtotime("-{$random_days} days");
return date('Y-m-d H:i:s', $new_time);
case 'gradual':
// 逐渐递增(基于原日期递减)
$days_diff = floor(($now - $original) / 86400);
if ($days_diff > 7) {
$new_time = strtotime('-7 days');
} else {
$new_time = $original + 86400; // 往后推一天
}
return date('Y-m-d H:i:s', $new_time);
default:
return date('Y-m-d H:i:s', time());
}
}
/**
* 获取统计信息
*/
public function get_statistics() {
$options = get_option('wp_article_refresh_options');
// 统计文章总数
$total_posts = wp_count_posts('post');
// 统计符合条件的文章数量
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'date_query' => array(
'before' => date('Y-m-d H:i:s', strtotime('-1 day')),
'after' => date('Y-m-d H:i:s', strtotime('-' . $options['days_range'] . ' days'))
),
'no_found_rows' => true
);
if (!empty($options['categories'])) {
$args['category__in'] = $options['categories'];
}
if (!empty($options['exclude_categories'])) {
$args['category__not_in'] = $options['exclude_categories'];
}
$query = new WP_Query($args);
$eligible_posts = $query->found_posts;
return array(
'total_posts' => $total_posts->publish,
'eligible_posts' => $eligible_posts,
'refreshed_today' => $options['refreshed_today'],
'daily_limit' => $options['daily_limit'],
'total_refreshed' => $options['total_refreshed'],
'is_enabled' => $options['enabled'],
'next_refresh' => wp_next_scheduled('wp_article_refresh_event')
? date('Y-m-d H:i:s', wp_next_scheduled('wp_article_refresh_event'))
: __('未设置', 'wp-article-refresh')
);
}
/**
* 处理AJAX请求
*/
public function handle_ajax() {
if (!current_user_can('manage_options') || !wp_verify_nonce($_POST['nonce'], 'wp_article_refresh_ajax')) {
wp_die(__('无权限', 'wp-article-refresh'));
}
$action = sanitize_text_field($_POST['sub_action']);
switch ($action) {
case 'get_stats':
$stats = $this->get_statistics();
wp_send_json_success($stats);
break;
case 'refresh_now':
$result = $this->do_refresh();
wp_send_json_success(array('refreshed' => $result));
break;
default:
wp_send_json_error(__('未知操作', 'wp-article-refresh'));
}
}
/**
* 添加设置链接
*/
public function add_settings_link($links) {
$settings_link = '' . __('设置', 'wp-article-refresh') . ' ';
array_unshift($links, $settings_link);
return $links;
}
/**
* 加载资源文件
*/
public function enqueue_assets($hook) {
if ('toplevel_page_wp-article-refresh' !== $hook) {
return;
}
wp_enqueue_style('wp-article-refresh-admin', WP_ARTICLE_REFRESH_PLUGIN_URL . 'assets/css/admin.css', array(), WP_ARTICLE_REFRESH_VERSION);
wp_enqueue_script('wp-article-refresh-admin', WP_ARTICLE_REFRESH_PLUGIN_URL . 'assets/js/admin.js', array('jquery'), WP_ARTICLE_REFRESH_VERSION, true);
wp_localize_script('wp-article-refresh-admin', 'wpArticleRefresh', array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('wp_article_refresh_ajax'),
'strings' => array(
'confirm' => __('确定要手动刷新吗?', 'wp-article-refresh'),
'refreshing' => __('刷新中...', 'wp-article-refresh'),
'success' => __('刷新成功!', 'wp-article-refresh'),
'error' => __('刷新失败!', 'wp-article-refresh')
)
));
}
}
// 初始化插件
WP_Article_Refresh::get_instance();
// 创建模板目录
function wp_article_refresh_create_templates_dir() {
$dir = WP_ARTICLE_REFRESH_PLUGIN_DIR . 'templates';
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
}
add_action('admin_init', 'wp_article_refresh_create_templates_dir');
Python模块安装失败提示TypeError: cannot use a bytes pattern on a string-like object怎么办?_创想鸟
创想鸟 首页用户投稿
Python模块安装失败提示TypeError: cannot use a bytes pattern on a string-like object怎么办?
程序猿
•
2025年12月13日 21:29:03
•
用户投稿 •
阅读 0
Python模块安装失败,报错“TypeError: cannot use a bytes pattern on a string-like object”的解决方案
很多Python开发者在安装第三方库时,都会遇到恼人的TypeError: cannot use a bytes pattern on a string-like object错误。此错误通常表示Python解释器在安装过程中无法正确处理字符串和字节对象间的转换。本文将结合实际案例,提供有效的解决方法 。
一位用户反馈:其电脑 之前能正常安装Python模块,但最近却频繁出现此错误,无论使用PyCharm还是命令行,都无法安装成功,甚至运行程序也会报错。
问题根源可能多样,但常见原因是系统环境或Python解释器本身存在问题,导致字符串编码处理异常。直接套用网上解决方案往往无效,因为根本问题未解决。
立即学习“Python免费学习笔记(深入)”;
推荐的解决方法:创建虚拟环境 。 使用venv模块创建一个独立的虚拟环境,并在该环境中重新安装所需的模块。这能有效隔离系统环境中可能存在的冲突或问题。
创建虚拟环境是解决TypeError: cannot use a bytes pattern on a string-like object错误的有效手段,因为它提供了一个干净的Python环境,避免了系统环境配置问题对模块安装的影响。 这通常意味着系统某些组件可能未更新或存在冲突,而虚拟环境巧妙地规避了这些问题。
以上就是Python模块安装失败提示TypeError: cannot use a bytes pattern on a string-like object怎么办?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1358034.html
赞 (0)
打赏
微信扫一扫
支付宝扫一扫
Python文件编码声明:多种写法是否有效?
上一篇
2025年12月13日 21:28:57
Redis更新键值的同时如何保持其生存时间不变?
下一篇
2025年12月13日 21:29:12
相关推荐
如何跨越localhost使用本地图片? 问题: 在本地使用mask js库时,引入本地图片会报跨域错误。 解决方案: 要解决此问题,需要使用本地服务器启动文件,以http或https协议访问图片,而不是使用file://协议。例如: python -m http.server 8000 然后,可以…
跨域疑难:如何解决 mask 引入本地图片产生的跨域问题? 在使用 mask 导入本地图片时,你可能会遇到令人沮丧的跨域错误。为什么会出现跨域问题呢?让我们深入了解一下: mask 框架假设你以 http(s) 协议加载你的 html 文件,而当使用 file:// 协议打开本地文件时,就会产生跨域…
正则表达式助力文本输入验证 在文本输入框的验证中,经常遇到需要限定输入内容的情况。例如,输入框只能输入整数,第一位可以为负号。对于不会使用正则表达式的人来说,这可能是个难题。下面我们将提供三种正则表达式,分别满足不同的验证要求。 1. 可选负号,任意数量数字 如果输入框中允许第一位为负号,后面可输入…
作为一个刚刚完成中学教育的女孩和一个精通技术并热衷于解决问题的人,几周前我开始了我的编程之旅。我的名字是OKESANJO FATHIA OPEYEMI。我很高兴能分享我在编码世界中的经验和发现。拥有计算机科学背景的我一直对编程提供的无限可能性着迷。在这篇文章中,我将反思我在学习编程的第一周中获得的关…
在全栈和平均栈开发方面工作了 6 年多,我可以告诉您,虽然这两种方法都是流行且有效的方法,但它们满足不同的需求,并且有自己的优点和缺点。这两个堆栈都可以帮助您创建 Web 应用程序,但它们的实现方式却截然不同。如果您在两者之间难以选择,我希望我在两者之间的经验能给您一些有用的见解。 在这篇文章中,我…
本教程演示如何在新项目中从头开始配置 django 和 tailwindcss。 django 设置 创建一个名为 .venv 的新虚拟环境。 # windows$ python -m venv .venv$ .venvscriptsactivate.ps1(.venv) $# macos/linu…
→ Python → JavaScript → Java → C# → 红宝石 → 斯威夫特 → 科特林 → C++ → PHP → 出发 → R → 打字稿 []https://x.com/e_opore/status/1811567830594388315?t=_j4nncuiy2wfbm7ic…
粘性定位为什么会失效?原因及解决方法 一、引言在前端开发中,粘性定位(sticky position)是一种常见的布局方式。通过设置元素的定位属性为sticky,可以实现在指定的滚动范围内,元素在页面上的位置保持固定不变,直到达到指定的偏移量。然而,有时候我们会发现粘性定位失效的情况,本文将探讨其原…
绝对定位故障的原因分析及解决方法 概述:绝对定位是前端开发中常见的一种布局方式,它可以让元素在页面中精确地定位。但是,在实际的开发过程中,我们可能会遇到绝对定位出现故障的情况。本文将分析绝对定位故障的原因,并提供解决方法,同时附上具体的代码示例。 一、原因分析: 定位元素和参照元素的父元素未设置定位…
解析CSS主框架偏移的原因及解决方法,需要具体代码示例 标题:CSS主框架偏移问题的分析与解决方案 引言:随着Web开发的不断发展,CSS作为前端开发的重要工具之一,被广泛应用于页面布局和样式设计。然而,在实际开发中,我们可能会遇到CSS主框架偏移的问题,即页面元素无法按预期位置显示。本文将深入分析…
css如何解决bug?相信有很多刚刚接触css中ie浏览器的朋友都会有这样的疑问。本章就给大家介绍css中ie浏览器最基本的一些bug以及解决方法。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。 一、IE6双倍边距bug 当页面上的元素使用float浮动时,不管是向左还是向右浮动;…
HTML5无法直接导出video标签内容,需借助Canvas捕获帧并结合MediaRecorder API、FFmpeg.wasm或服务端协同实现。MediaRecorder适用于WebM格式前端录制;FFmpeg.wasm支持MP4等格式及精细编码控制;服务端方案适合高负载场景。 如果您希望在网页…
要查看HTML文件的浏览器渲染效果,需确保文件以.html为扩展名保存、用浏览器直接打开、利用开发者工具调试、必要时启用本地HTTP服务器、或使用编辑器实时预览插件。 如果您编写了HTML代码,但无法直观看到其在浏览器中的实际渲染效果,则可能是由于文件未正确保存、未使用浏览器打开或文件扩展名设置错误…
应通过 HTTP 服务运行打包后的 HTML5 页面,而非双击打开:一、Webpack 配 webpack-dev-server 启动本地服务;二、Gulp 配 BrowserSync 提供实时重载;三、用 Python/Node.js 轻量 HTTP 工具托管 dist 目录;四、仅当必须双击运行…
首先检查文件扩展名和编码格式,确保为.html且使用UTF-8编码;接着验证HTML5结构完整性,包含及正确闭合的标签;然后排查外部资源路径是否正确,利用开发者工具查看404错误;排除浏览器兼容性问题,优先在现代浏览器中测试并避免未广泛支持的API;检查JavaScript语法错误与执行顺序,确保脚…
可在HTML5中用iframe或object标签嵌入PDF,需设宽高及可访问路径;Word文档需借OneDrive等第三方服务代理渲染;须处理跨域限制并提供下载降级方案。 如果您希望在HTML5页面中嵌入PDF或Word文档并直接显示,可以使用或标签实现。以下是几种可行的嵌入方法: 一、使用ifra…
HTML5图标显示异常可因路径错误、引用不当或字体未加载,解决方法包括:一、用iconfont类名引用;二、用Unicode字符引用;三、用img标签引用位图;四、内联SVG图标;五、预加载字体文件。 如果您在HTML5页面中需要显示图标,但图标无法正常加载或显示效果不符合预期,则可能是由于图标文件…
HTML代码需保存为.html文件并用浏览器打开才能正确显示;若含AJAX或外部资源则需本地服务器;临时测试可用开发者工具;在线编辑器支持即时预览。 如果您编写了一段HTML代码,但无法在浏览器中正确显示效果,则可能是由于文件未以正确的格式保存或未通过浏览器打开。以下是运行HTML代码的具体步骤: …
Safari中正确渲染HTML5内容需采用file://协议、禁用本地限制、启用HTTP服务器或更新版本并开启实验性功能。具体包括:一、用file:///绝对路径打开本地HTML文件;二、勾选高级设置中的“显示开发菜单”并禁用本地文件限制;三、用Python启动本地HTTP服务,通过http://l…
拖拽HTML文件到浏览器可直接加载页面;2. 通过菜单“打开文件”或快捷键Ctrl+O选择文件;3. 地址栏输入file:///加路径访问,注意斜杠格式;4. 双击文件用默认浏览器打开,推荐新手使用拖拽或Ctrl+O方式。 要让浏览器运行HTML文件,关键是正确打开并加载本地的HTML文件路径。操作…