| Server IP : 172.67.179.166 / Your IP : 162.159.108.113 Web Server : nginx/1.20.2 System : Linux 172-104-110-161.ip.linodeusercontent.com 3.10.0-1160.36.2.el7.x86_64 #1 SMP Wed Jul 21 11:57:15 UTC 2021 x86_64 User : www ( 1000) PHP Version : 8.1.9 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /www/wwwroot/lenovo-drivers.com/wordpress/wp-content/plugins/lenovo/ |
Upload File : |
<?php
/**
* Plugin Name: Lenovo Article Sync Plugin
* Plugin URI: https://your-wordpress-site.com
* Description: 用于同步联想设备技术文章到WordPress的插件
* Version: 1.0.0
* Author: Your Name
* License: GPL v2 or later
* Text Domain: lenovo-article-sync
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
class LenovoArticleSyncPlugin {
public function __construct() {
add_action('init', array($this, 'init'));
add_action('rest_api_init', array($this, 'register_rest_routes'));
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_init', array($this, 'register_settings'));
}
public function init() {
// 加载文本域
load_plugin_textdomain('lenovo-article-sync', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
public function register_rest_routes() {
// 接收文章同步的API端点
register_rest_route('lenovo-sync/v1', '/receive-article', array(
'methods' => 'POST',
'callback' => array($this, 'receive_article'),
'permission_callback' => array($this, 'verify_api_key'),
));
// 删除文章的API端点
register_rest_route('lenovo-sync/v1', '/delete-article/(?P<id>\d+)', array(
'methods' => 'DELETE',
'callback' => array($this, 'delete_article'),
'permission_callback' => array($this, 'verify_api_key'),
));
// 健康检查端点
register_rest_route('lenovo-sync/v1', '/health', array(
'methods' => 'GET',
'callback' => array($this, 'health_check'),
'permission_callback' => '__return_true',
));
}
public function verify_api_key($request) {
$api_key = $request->get_header('X-API-Key');
$stored_key = get_option('lenovo_sync_api_key');
if (!$stored_key || $api_key !== $stored_key) {
return new WP_Error('rest_forbidden', __('Invalid API key', 'lenovo-article-sync'), array('status' => 403));
}
return true;
}
public function receive_article($request) {
$params = $request->get_json_params();
// 验证必要参数
if (empty($params['title']) || empty($params['content'])) {
return new WP_Error('missing_params', __('Missing required parameters: title, content', 'lenovo-article-sync'), array('status' => 400));
}
try {
// 创建文章
$post_data = array(
'post_title' => sanitize_text_field($params['title']),
'post_content' => wp_kses_post($params['content']),
'post_status' => 'publish',
'post_author' => $this->get_author_id($params['author'] ?? ''),
'post_type' => 'post',
'meta_input' => array(
'lenovo_model_name' => sanitize_text_field($params['model_name'] ?? ''),
'lenovo_source' => 'lenovo_article_sync',
)
);
$post_id = wp_insert_post($post_data);
if (is_wp_error($post_id)) {
return new WP_Error('create_failed', __('Failed to create post', 'lenovo-article-sync'), array('status' => 500));
}
// 添加分类
if (!empty($params['categories'])) {
$this->assign_categories($post_id, $params['categories']);
}
// 添加标签
if (!empty($params['tags'])) {
$this->assign_tags($post_id, $params['tags']);
}
return rest_ensure_response(array(
'success' => true,
'data' => array(
'post_id' => $post_id,
'post_url' => get_permalink($post_id),
'message' => __('Article created successfully', 'lenovo-article-sync')
)
));
} catch (Exception $e) {
return new WP_Error('server_error', $e->getMessage(), array('status' => 500));
}
}
public function delete_article($request) {
$post_id = $request['id'];
// 验证文章是否存在且来自本插件
$post = get_post($post_id);
if (!$post || get_post_meta($post_id, 'lenovo_source', true) !== 'lenovo_article_sync') {
return new WP_Error('not_found', __('Article not found or not from Lenovo sync', 'lenovo-article-sync'), array('status' => 404));
}
$result = wp_delete_post($post_id, true);
if (!$result) {
return new WP_Error('delete_failed', __('Failed to delete article', 'lenovo-article-sync'), array('status' => 500));
}
return rest_ensure_response(array(
'success' => true,
'message' => __('Article deleted successfully', 'lenovo-article-sync')
));
}
public function health_check($request) {
return rest_ensure_response(array(
'success' => true,
'data' => array(
'status' => 'healthy',
'plugin_version' => '1.0.0',
'wordpress_version' => get_bloginfo('version'),
'api_endpoints' => array(
'receive_article' => get_rest_url(null, 'lenovo-sync/v1/receive-article'),
'delete_article' => get_rest_url(null, 'lenovo-sync/v1/delete-article/{id}'),
)
)
));
}
private function get_author_id($author_name) {
if (empty($author_name)) {
return 1; // 默认管理员
}
// 查找或创建作者
$user = get_user_by('login', $author_name);
if ($user) {
return $user->ID;
}
// 如果用户不存在,使用默认管理员
return 1;
}
private function assign_categories($post_id, $categories) {
if (!is_array($categories)) {
$categories = array($categories);
}
$category_ids = array();
foreach ($categories as $category_name) {
$category = get_category_by_slug(sanitize_title($category_name));
if (!$category) {
// 创建新分类
$category_id = wp_create_category($category_name);
if ($category_id) {
$category_ids[] = $category_id;
}
} else {
$category_ids[] = $category->term_id;
}
}
if (!empty($category_ids)) {
wp_set_post_categories($post_id, $category_ids);
}
}
private function assign_tags($post_id, $tags) {
if (!is_array($tags)) {
$tags = array($tags);
}
$tag_ids = array();
foreach ($tags as $tag_name) {
$tag = get_term_by('name', $tag_name, 'post_tag');
if (!$tag) {
// 创建新标签
$tag_info = wp_insert_term($tag_name, 'post_tag');
if (!is_wp_error($tag_info)) {
$tag_ids[] = $tag_info['term_id'];
}
} else {
$tag_ids[] = $tag->term_id;
}
}
if (!empty($tag_ids)) {
wp_set_post_tags($post_id, $tag_ids);
}
}
public function add_admin_menu() {
add_options_page(
__('Lenovo Article Sync Settings', 'lenovo-article-sync'),
__('Lenovo Sync', 'lenovo-article-sync'),
'manage_options',
'lenovo-article-sync',
array($this, 'admin_page')
);
}
public function register_settings() {
register_setting('lenovo_sync_settings', 'lenovo_sync_api_key');
register_setting('lenovo_sync_settings', 'lenovo_sync_default_author');
register_setting('lenovo_sync_settings', 'lenovo_sync_default_category');
}
public function admin_page() {
?>
<div class="wrap">
<h1><?php _e('Lenovo Article Sync Settings', 'lenovo-article-sync'); ?></h1>
<form method="post" action="options.php">
<?php settings_fields('lenovo_sync_settings'); ?>
<?php do_settings_sections('lenovo_sync_settings'); ?>
<table class="form-table">
<tr>
<th scope="row">
<label for="lenovo_sync_api_key"><?php _e('API Key', 'lenovo-article-sync'); ?></label>
</th>
<td>
<input type="text" id="lenovo_sync_api_key" name="lenovo_sync_api_key"
value="<?php echo esc_attr(get_option('lenovo_sync_api_key')); ?>"
class="regular-text" />
<p class="description">
<?php _e('Set this key in your Lenovo article management system for authentication.', 'lenovo-article-sync'); ?>
</p>
</td>
</tr>
<tr>
<th scope="row">
<label for="lenovo_sync_default_author"><?php _e('Default Author', 'lenovo-article-sync'); ?></label>
</th>
<td>
<input type="text" id="lenovo_sync_default_author" name="lenovo_sync_default_author"
value="<?php echo esc_attr(get_option('lenovo_sync_default_author', 'Lenovo Expert')); ?>"
class="regular-text" />
<p class="description">
<?php _e('Default author name for synced articles.', 'lenovo-article-sync'); ?>
</p>
</td>
</tr>
<tr>
<th scope="row">
<label for="lenovo_sync_default_category"><?php _e('Default Category', 'lenovo-article-sync'); ?></label>
</th>
<td>
<input type="text" id="lenovo_sync_default_category" name="lenovo_sync_default_category"
value="<?php echo esc_attr(get_option('lenovo_sync_default_category', 'Lenovo Devices')); ?>"
class="regular-text" />
<p class="description">
<?php _e('Default category for synced articles.', 'lenovo-article-sync'); ?>
</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<div class="card">
<h2><?php _e('API Information', 'lenovo-article-sync'); ?></h2>
<p><strong><?php _e('API Base URL:', 'lenovo-article-sync'); ?></strong> <?php echo get_rest_url(null, 'lenovo-sync/v1'); ?></p>
<p><strong><?php _e('Endpoints:', 'lenovo-article-sync'); ?></strong></p>
<ul>
<li><code>POST /receive-article</code> - <?php _e('Receive new articles', 'lenovo-article-sync'); ?></li>
<li><code>DELETE /delete-article/{id}</code> - <?php _e('Delete articles', 'lenovo-article-sync'); ?></li>
<li><code>GET /health</code> - <?php _e('Health check', 'lenovo-article-sync'); ?></li>
</ul>
</div>
</div>
<?php
}
}
// 初始化插件
new LenovoArticleSyncPlugin();
// 激活插件时的操作
register_activation_hook(__FILE__, 'lenovo_article_sync_activate');
function lenovo_article_sync_activate() {
// 生成默认API密钥
if (!get_option('lenovo_sync_api_key')) {
$api_key = wp_generate_password(32, false);
update_option('lenovo_sync_api_key', $api_key);
}
// 刷新重写规则
flush_rewrite_rules();
}
// 停用插件时的操作
register_deactivation_hook(__FILE__, 'lenovo_article_sync_deactivate');
function lenovo_article_sync_deactivate() {
flush_rewrite_rules();
}