From f1826aabe27fca7072c15e62dd943d6b0b618e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=8B=E4=BA=BAa=E4=B8=B6?= <1747111677@qq.com> Date: Tue, 30 Aug 2022 10:54:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +- admin/preload.php | 2 +- admin/render.php | 76 +++--- assets/admin.js | 1 + class/compress.php | 6 +- class/local.php | 6 +- config.php | 2 +- nicen-localize-image.php | 45 ++-- readme.txt | 9 +- response/response.php | 508 ++++++++++++++++++++------------------- 10 files changed, 351 insertions(+), 312 deletions(-) diff --git a/README.md b/README.md index 3554ca1..8fdb2d6 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Plugin Name: nicen-localize-image Plugin URI:https://nicen.cn/2893.html Description: 用于本地化文章的外部图片的插件,支持文章发布前通过编辑器插件本地化、文章发布时自动本地化、定时发布文章时自动本地化、已发布的文章批量本地化。 -Version: 1.3.1 +Version: 1.3.3 Author: 友人a丶 Author URI: https://nicen.cn Text Domain: nicen-localize-image @@ -22,6 +22,12 @@ nicen-localize-image,是一款用于本地化文章的外部图片的插件, # 更新日志: +## v1.3.3 + +1. 修改代码适配wordpress插件商店规范; +2. 图片压缩完成后自动刷新显示的目录; +3. 修改网络请求超时时间为120s; + ## v1.3.1 beta 1. 新增批量本地化时,可以指定文章分类,指定文章发布时间范围; diff --git a/admin/preload.php b/admin/preload.php index 1aec60f..bb55675 100644 --- a/admin/preload.php +++ b/admin/preload.php @@ -24,7 +24,7 @@ } -const NICEN_VERSION='1.3.2'; //插件版本 +const NICEN_VERSION='1.3.3'; //插件版本 /* * 定时任务接口 * */ diff --git a/admin/render.php b/admin/render.php index f63e331..4054fd4 100644 --- a/admin/render.php +++ b/admin/render.php @@ -10,33 +10,35 @@ /* * 输出本地化日志 * */ -function nicen_plugin_local_log() { +function nicen_plugin_local_log() +{ - $logs = ( ( Nicen_log::getInstance() )->get_logs() ); + $logs = ((Nicen_log::getInstance())->get_logs()); - echo ''; - echo '清空插件日志'; - echo ''; + echo ''; + echo '清空插件日志'; + echo ''; - echo ' + echo ' '; - echo ''; + echo ''; } -/* +/** * 插件升级 - * */ -function nicen_plugin_update() { + */ +function nicen_plugin_update() +{ - echo ' + echo ' 当前版本(' . NICEN_VERSION . ')/ 最新版本({{version}}) @@ -94,22 +96,23 @@ function nicen_plugin_update() { } -/* +/** * 文章批量本地化 - * */ -function Nicen_form_batch() { + */ +function Nicen_form_batch() +{ - $count = wp_count_posts();//文章总数 - $last = get_option( 'nicen_last_batch' ); //上次本地化的ID + $count = wp_count_posts();//文章总数 + $last = get_option('nicen_last_batch'); //上次本地化的ID - /* - * 获取上次的ID - * */ - if ( ! empty( $last ) ) { - $last = '上次批量本地化的文章ID为:' . $last . ','; - } + /* + * 获取上次的ID + * */ + if (!empty($last)) { + $last = '上次批量本地化的文章ID为:' . $last . ','; + } - echo ' + echo '
按照指定的文章ID范围批量进行图片本地化,点击开始后任务自动运行,运行过程中可以随时暂停,关闭网页表示强制暂停!运行过程中将会展示实时日志!

@@ -127,15 +130,15 @@ function Nicen_form_batch() { '; - echo ' + echo ' '; - echo " + echo " "; - echo ''; - echo ' + echo ''; + echo ' {{batch.loading?"正在运行,点击取消运行...":"开始运行"}} 取消运行 '; - echo ''; + echo ''; } @@ -158,19 +161,20 @@ function Nicen_form_batch() { /* * 文章批量本地化 * */ -function Nicen_form_compress() { +function Nicen_form_compress() +{ - echo '选择指定的目录或者图片进行压缩,默认根目录为 /wp-content/uploads,压缩前请先点击加载图片目录,然后再选中目录或图片进行压缩 '; + echo '选择指定的目录或者图片进行压缩,默认根目录为 wordpress媒体文件存放目录,压缩前请先点击加载图片目录,然后再选中目录或图片进行压缩 '; - echo ''; - echo ' + echo ''; + echo ' {{tree.loading?"正在压缩第"+tree.count+"张图片,点击取消压缩...":"开始压缩"}} 取消压缩 加载图片目录 '; - echo ''; + echo ''; - echo " + echo " $headers, - 'sslverify' => false + 'sslverify' => false, + 'timeout' => 120, ] ); return wp_remote_retrieve_body( $res ); @@ -137,7 +138,7 @@ function post( $file ) { 'files' => new \CURLFile( $file ) ]; - $Http = new WP_Http_Curl; + $Http = new WP_Http_Curl(); /* * 修改请求配置 @@ -146,6 +147,7 @@ function post( $file ) { 'method' => 'POST', 'sslverify' => false, 'body' => $post_data, + 'timeout' => 120, 'httpversion' => '1.1' ); diff --git a/class/local.php b/class/local.php index f1bdbbd..ddb0d74 100644 --- a/class/local.php +++ b/class/local.php @@ -39,7 +39,8 @@ public static function getInstance() { * */ function getHttpcode( $url ) { $response = wp_remote_get( $url, [ - 'sslverify' => false + 'sslverify' => false, + 'timeout' => 60, ] ); $http_code = wp_remote_retrieve_response_code( $response ); @@ -69,7 +70,8 @@ function getImage( $url ) { * */ $res = wp_remote_get( $url, [ 'headers' => $headers, - 'sslverify' => false + 'sslverify' => false, + 'timeout' => 120, ] ); return wp_remote_retrieve_body( $res ); diff --git a/config.php b/config.php index 49ba532..369893c 100644 --- a/config.php +++ b/config.php @@ -264,7 +264,7 @@ 'nicen_make_plugin_save_result' => '', //临时保存本地化结果 'nicen_make_plugin_alt' => '1', //自动新增alt 'nicen_make_plugin_alt_type' => '1', //alt增加的类型 - 'nicen_make_plugin_path' => '/wp-content/uploads/replace', //资源保存的路径 + 'nicen_make_plugin_path' => NICEN_SITE_ROOT.'/uploads/replace', //资源保存的路径 'nicen_make_plugin_add_domain' => '0', //链接是否增加域名 'nicen_make_save_type'=>'png', diff --git a/nicen-localize-image.php b/nicen-localize-image.php index 339be87..78b862e 100644 --- a/nicen-localize-image.php +++ b/nicen-localize-image.php @@ -3,7 +3,7 @@ * Plugin Name: nicen-localize-image * Plugin URI:https://nicen.cn/2893.html * Description: 用于本地化文章的外部图片的插件,支持文章发布前通过编辑器插件本地化、文章发布时自动本地化、定时发布文章时自动本地化、已发布的文章批量本地化。 - * Version: 1.3.2 + * Version: 1.3.3 * Author: 友人a丶 * Author URI: https://nicen.cn * Text Domain: nicen-localize-image @@ -11,28 +11,39 @@ */ - -define( 'nicen_make_PATH', plugin_dir_path( __FILE__ ) ); //插件目录 -define( 'nicen_make_URL', plugin_dir_url( __FILE__ ) ); //插件URL +define('nicen_make_PATH', plugin_dir_path(__FILE__)); //插件目录 +define('nicen_make_URL', plugin_dir_url(__FILE__)); //插件URL //this is the root of website's path. call $_SERVER['DOCUMENT_ROOT'],I think it's safe; //Think's -define( 'NICEN_ROOT', $_SERVER['DOCUMENT_ROOT'] ); -var_dump(realpath(WP_CONTENT_DIR)); -var_dump(DIRECTORY_SEPARATOR); -var_dump(NICEN_ROOT); -exit(); +/*文件系统根目录*/ +define('NICEN_ROOT', $_SERVER['DOCUMENT_ROOT']); + +/*站点根目录*/ +define('NICEN_SITE_ROOT', + str_replace( + str_replace( + '\\', + '/', + $_SERVER['DOCUMENT_ROOT']), + "", + str_replace( + '\\', + '/', + WP_CONTENT_DIR) + )); + -date_default_timezone_set( get_option( 'timezone_string' ) ); //设置时区 +date_default_timezone_set(get_option('timezone_string')); //设置时区 include_once nicen_make_PATH . '/admin/preload.php'; //加载插件配置 include_once nicen_make_PATH . '/config.php'; //加载插件配置 include_once nicen_make_PATH . '/admin/install.php'; //安装时触发 -register_activation_hook( __FILE__, "nicen_make_install" );//初始化插件 -register_deactivation_hook( __FILE__, 'nicen_make_end' ); //卸载插件 +register_activation_hook(__FILE__, "nicen_make_install");//初始化插件 +register_deactivation_hook(__FILE__, 'nicen_make_end'); //卸载插件 /*导入类库*/ include_once nicen_make_PATH . '/class/log.php'; //错误日志封装类 @@ -49,11 +60,11 @@ /* * 只在后台才触发 * */ -if ( is_admin() ) { - include_once nicen_make_PATH . '/admin/load.php'; //加载后台插件资源 - include_once nicen_make_PATH . '/admin/form.php'; //加载后台设置表单 - include_once nicen_make_PATH . '/admin/setting.php';//渲染表单 - include_once nicen_make_PATH . '/admin/initialize.php'; //初始化插件功能 +if (is_admin()) { + include_once nicen_make_PATH . '/admin/load.php'; //加载后台插件资源 + include_once nicen_make_PATH . '/admin/form.php'; //加载后台设置表单 + include_once nicen_make_PATH . '/admin/setting.php';//渲染表单 + include_once nicen_make_PATH . '/admin/initialize.php'; //初始化插件功能 } diff --git a/readme.txt b/readme.txt index c11034c..9c4fc22 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Donate link: https://nicen.cn/donate Requires at least: 5.6 Tested up to: 6.0.1 Requires PHP: 7.3 -Stable tag: 1.3.2 +Stable tag: 1.3.3 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html @@ -27,6 +27,13 @@ Github:https://github.com/friend-nicen/nicen-localize-image Gitee:https://gitee.com/friend-nicen/nicen-localize-image == Changelog == + +v1.3.3 + +1. 修改代码适配wordpress插件商店规范; +2. 图片压缩完成后自动刷新显示的目录; +3. 修改网络请求超时时间为120s; + v1.3.1 beta 1. 新增批量本地化时,可以指定文章分类,指定文章发布时间范围; diff --git a/response/response.php b/response/response.php index 6f98965..e2857e8 100644 --- a/response/response.php +++ b/response/response.php @@ -10,278 +10,284 @@ * 链接提交 * */ -class Nicen_response { - - private $private; - private static $self; - - private function __construct() { - $this->private = get_option( "nicen_make_plugin_private" ); - } - - - /** - * 获取单例 - * */ - public static function getInstance() { - /*如果实例不存在*/ - if ( ! self::$self ) { - self::$self = new self(); - } - - return self::$self; - } - - /** - * 验证接口权限 - * */ - public function auth() { - - if ( empty( $_GET['private'] ) && empty( $_POST['private'] ) ) { - exit( json_encode( [ - 'code' => 0, - 'result' => "密钥为空" - ] ) ); - } - - if ( ( $_GET['private'] ?? "" ) != $this->private && ( $_POST['private'] ?? "" ) != $this->private ) { - exit( json_encode( [ - 'code' => 0, - 'result' => "密钥有误" - ] ) ); - } - } - - /* - * 添加一个间隔时间 - * */ - public function add_schedules( $schedules ) { - $schedules['nicen_crontab'] = array( - 'interval' => get_option( 'nicen_make_plugin_interval' ), //获取设置的间隔时间 - 'display' => '定时发布草稿文章' - ); - - return $schedules; - } - - /** - * 接收响应 - * */ - public function response() { - - /* - * 接手响应 - * */ - - try { - - /* - * 本地化图片的请求 - * */ - if ( isset( $_GET['nicen_make_replace'] ) ) { - $this->auth(); //权限验证 - ( Nicen_local::getInstance() )->localImage( sanitize_url( $_POST['img'] ) ); - } - - /* - * 清空日志 - * */ - if ( isset( $_GET['nicen_make_clear_log'] ) ) { - $this->auth(); //权限验证 - exit( json_encode( [ - 'code' => 1, - 'result' => "清除成功!" - ] ) ); - } - - /** - * 批量本地化 - * */ - if ( isset( $_GET['nicen_make_batch'] ) ) { - - - $this->auth(); //权限验证 - global $wpdb; //数据库操作 - - $json = json_decode( file_get_contents( 'php://input' ), true ); - - /* - * 判断参数完整性 - * 有一个没填那就获取所有文章 - * */ - - $condition = [ - '`post_type` = "post"', //指定文章类型 - '(`post_status` = "publish" or `post_status` = "draft")' //指定草稿和已发布(爬出自动草稿) - ]; - - /* - * ID范围 - * */ - if ( ! empty( $json['start'] ) && ! empty( $json['end'] ) ) { - $condition[] = '(`ID` >= ' . $json['start'] . ' and `ID` <= ' . $json['end'] . ')'; - } - - - /* - * 时间范围 - * */ - if ( ! empty( $json['range'] ) ) { - $condition[] = '(`post_date` >= "' . $json['range'][0] . '" and `post_date` <= "' . $json['range'][1] . '")'; - } - - /* - * 分类范围 - * */ - if ( ! empty( $json['category'] ) ) { - $condition[] = '`ID` in (select DISTINCT `object_id` from `wp_term_relationships` where `term_taxonomy_id` in (' . join( ',', $json['category'] ) . '))'; - } - - $sql = 'select `ID` from `wp_posts` where ' . join( ' and ', $condition ) . ' order by `post_date`'; - - $result = $wpdb->get_results( $sql ); - - /* - * 判断本地化结果 - * */ - if ( empty( $result ) ) { - exit( json_encode( [ - 'code' => 0, - 'errMsg' => "没有符合条件的文章或草稿!" - ] ) ); - } else { - exit( json_encode( [ - 'code' => 1, - 'errMsg' => "查询成功!", - 'data' => $result - ] ) ); - } - - } - - - /** - * 开始本地化 - * */ - if ( isset( $_GET['nicen_make_local_batch'] ) && isset( $_GET['batch_id'] ) ) { - - $this->auth(); //权限验证 - $ID = $_GET['batch_id']; //文章ID - $post = get_post( $ID ); //获取文章 - $log = nicen_make_when_save_post( $ID, false ); //开始本地化 - - update_option( 'nicen_last_batch', $ID ); //记录本地化 - /* - * 返回结果 - * */ - exit( json_encode( [ - 'code' => 1, - 'errMsg' => empty( $log ) ? "文章【" . $post->post_title . '】没有检测到外部图片' : "文章【" . $post->post_title . '】' . $log - ] ) ); - } - - - /** - * 是否修改了定时任务的执行状态 - * */ - if ( isset( $_POST['nicen_make_plugin_auto_publish'] ) ) { - - /* - * 对比 - * */ - $list = [ - 'nicen_make_plugin_order', - 'nicen_make_plugin_auto_publish', - 'nicen_make_plugin_interval', - 'nicen_make_plugin_publish_local', - 'nicen_make_publish_date' - ]; +class Nicen_response +{ + + private $private; + private static $self; + + private function __construct() + { + $this->private = get_option("nicen_make_plugin_private"); + } + + + /** + * 获取单例 + * */ + public static function getInstance() + { + /*如果实例不存在*/ + if (!self::$self) { + self::$self = new self(); + } + + return self::$self; + } + + /** + * 验证接口权限 + * */ + public function auth() + { + + if (empty($_GET['private']) && empty($_POST['private'])) { + exit(json_encode([ + 'code' => 0, + 'result' => "密钥为空" + ])); + } + + if (($_GET['private'] ?? "") != $this->private && ($_POST['private'] ?? "") != $this->private) { + exit(json_encode([ + 'code' => 0, + 'result' => "密钥有误" + ])); + } + } + + /* + * 添加一个间隔时间 + * */ + public function add_schedules($schedules) + { + $schedules['nicen_crontab'] = array( + 'interval' => get_option('nicen_make_plugin_interval'), //获取设置的间隔时间 + 'display' => '定时发布草稿文章' + ); + + return $schedules; + } + + /** + * 接收响应 + * */ + public function response() + { + + /* + * 接手响应 + * */ + + try { + + /* + * 本地化图片的请求 + * */ + if (isset($_GET['nicen_make_replace'])) { + $this->auth(); //权限验证 + (Nicen_local::getInstance())->localImage(sanitize_url($_POST['img'])); + } + + /* + * 清空日志 + * */ + if (isset($_GET['nicen_make_clear_log'])) { + $this->auth(); //权限验证 + exit(json_encode([ + 'code' => 1, + 'result' => "清除成功!" + ])); + } + + /** + * 批量本地化 + * */ + if (isset($_GET['nicen_make_batch'])) { + + + $this->auth(); //权限验证 + global $wpdb; //数据库操作 + + $json = json_decode(file_get_contents('php://input'), true); + + /* + * 判断参数完整性 + * 有一个没填那就获取所有文章 + * */ + + $condition = [ + '`post_type` = "post"', //指定文章类型 + '(`post_status` = "publish" or `post_status` = "draft")' //指定草稿和已发布(爬出自动草稿) + ]; + + /* + * ID范围 + * */ + if (!empty($json['start']) && !empty($json['end'])) { + $condition[] = '(`ID` >= ' . $json['start'] . ' and `ID` <= ' . $json['end'] . ')'; + } + + + /* + * 时间范围 + * */ + if (!empty($json['range'])) { + $condition[] = '(`post_date` >= "' . $json['range'][0] . '" and `post_date` <= "' . $json['range'][1] . '")'; + } + + /* + * 分类范围 + * */ + if (!empty($json['category'])) { + $condition[] = '`ID` in (select DISTINCT `object_id` from `wp_term_relationships` where `term_taxonomy_id` in (' . join(',', $json['category']) . '))'; + } + + $sql = 'select `ID` from `wp_posts` where ' . join(' and ', $condition) . ' order by `post_date`'; + + $result = $wpdb->get_results($sql); + + /* + * 判断本地化结果 + * */ + if (empty($result)) { + exit(json_encode([ + 'code' => 0, + 'errMsg' => "没有符合条件的文章或草稿!" + ])); + } else { + exit(json_encode([ + 'code' => 1, + 'errMsg' => "查询成功!", + 'data' => $result + ])); + } + + } + + + /** + * 开始本地化 + * */ + if (isset($_GET['nicen_make_local_batch']) && isset($_GET['batch_id'])) { + + $this->auth(); //权限验证 + $ID = $_GET['batch_id']; //文章ID + $post = get_post($ID); //获取文章 + $log = nicen_make_when_save_post($ID, false); //开始本地化 + + update_option('nicen_last_batch', $ID); //记录本地化 + /* + * 返回结果 + * */ + exit(json_encode([ + 'code' => 1, + 'errMsg' => empty($log) ? "文章【" . $post->post_title . '】没有检测到外部图片' : "文章【" . $post->post_title . '】' . $log + ])); + } + + + /** + * 是否修改了定时任务的执行状态 + * */ + if (isset($_POST['nicen_make_plugin_auto_publish'])) { + + /* + * 对比 + * */ + $list = [ + 'nicen_make_plugin_order', + 'nicen_make_plugin_auto_publish', + 'nicen_make_plugin_interval', + 'nicen_make_plugin_publish_local', + 'nicen_make_publish_date' + ]; - /* - * 表单值是否有了变化 - * */ - - $hasChange = false; //变化 - - foreach ( $list as $value ) { - if ( nicen_make_config( $value ) != $_POST[ $value ] ) { - $hasChange = true; - break; - } - } - - - /* - * 配置是否发生改变 - * */ - if ( $hasChange ) { - - $current = sanitize_option( $_POST['nicen_make_plugin_auto_publish'] ); //修改的状态 + /* + * 表单值是否有了变化 + * */ + + $hasChange = false; //变化 + + foreach ($list as $value) { + if (nicen_make_config($value) != $_POST[$value]) { + $hasChange = true; + break; + } + } + + + /* + * 配置是否发生改变 + * */ + if ($hasChange) { + + $current = sanitize_option($_POST['nicen_make_plugin_auto_publish']); //修改的状态 - /** - * 如果是开启 - * */ - if ( $current ) { - /*重新初始化钩子*/ - add_filter( 'cron_schedules', array( $this, 'add_schedules' ) ); //自定义间隔时间 - wp_clear_scheduled_hook( 'nicen_plugin_auto_publish' ); //清除任务 - wp_schedule_event( time(), 'nicen_crontab', 'nicen_plugin_auto_publish' ); - } else { - update_option( 'nicen_last_auto_publish', "任务已关闭" ); - wp_clear_scheduled_hook( 'nicen_plugin_auto_publish' ); //清除任务 - } + /** + * 如果是开启 + * */ + if ($current) { + /*重新初始化钩子*/ + add_filter('cron_schedules', array($this, 'add_schedules')); //自定义间隔时间 + wp_clear_scheduled_hook('nicen_plugin_auto_publish'); //清除任务 + wp_schedule_event(time(), 'nicen_crontab', 'nicen_plugin_auto_publish'); + } else { + update_option('nicen_last_auto_publish', "任务已关闭"); + wp_clear_scheduled_hook('nicen_plugin_auto_publish'); //清除任务 + } - } + } - } + } - /** - * 图片压缩 - * */ - if ( isset( $_GET['nicen_make_compress'] ) ) { - $this->auth(); //权限验证 + /** + * 图片压缩 + * */ + if (isset($_GET['nicen_make_compress'])) { + $this->auth(); //权限验证 - $json = json_decode( file_get_contents( 'php://input' ), true ); + $json = json_decode(file_get_contents('php://input'), true); - if ( isset( $json['file'] ) ) { - exit( json_encode( ( Nicen_comress::getInstance() )->getCompress( $json['file'] ) ) ); - } - } + if (isset($json['file'])) { + exit(json_encode((Nicen_comress::getInstance())->getCompress($json['file']))); + } + } - /** - * 加载目录 - * */ - if ( isset( $_GET['nicen_make_files'] ) ) { - $this->auth(); //权限验证 + /** + * 加载目录 + * */ + if (isset($_GET['nicen_make_files'])) { + $this->auth(); //权限验证 - $lists = ( Nicen_comress::getInstance() )->readDirs( '/wp-content/uploads' ); + $lists = (Nicen_comress::getInstance())->readDirs(NICEN_SITE_ROOT . '/uploads'); - $result = json_encode( [ - 'code' => 1, - 'data' => $lists, - 'errMsg' => "加载成功!" - ] ); + $result = json_encode([ + 'code' => 1, + 'data' => $lists, + 'errMsg' => "加载成功!" + ]); - exit( $result ); + exit($result); - } + } - } catch ( \Throwable $e ) { - exit( json_encode( [ - 'code' => 0, - 'errMsg' => $e->getMessage() - ] ) ); - } + } catch (\Throwable $e) { + exit(json_encode([ + 'code' => 0, + 'errMsg' => $e->getMessage() + ])); + } - } + } } -( Nicen_response::getInstance() )->response(); //接收请求 \ No newline at end of file +(Nicen_response::getInstance())->response(); //接收请求 \ No newline at end of file