From 742f5680e3cc230ce27b5a208a160c1dec310a4d Mon Sep 17 00:00:00 2001 From: Spirit Date: Fri, 27 Dec 2019 21:00:34 +0800 Subject: [PATCH] feat: add cover images api options --- functions.php | 10 +++------- inc/api.php | 54 ++++++++++++++++++++++++++++++++++----------------- options.php | 17 ++++++++++++++-- 3 files changed, 54 insertions(+), 27 deletions(-) diff --git a/functions.php b/functions.php index 17a602b..4013efb 100644 --- a/functions.php +++ b/functions.php @@ -1626,8 +1626,7 @@ function html_tag_parser($content) //With Thumbnail: !{alt}(url)[th_url] if (preg_match_all('/\!\{.*?\)\[.*?\]/i', $content, $matches)) { - $i = 0; - if ($i < sizeof($matches)) { + for ($i = 0; $i < sizeof($matches); $i++) { $content = str_replace($matches[$i], preg_replace( '/!\{([^\{\}]+)*\}\(' . $url_regex . '\)\[' . $url_regex . '\]/i', '', $matches[$i]), $content); - $i++; } } @@ -1671,14 +1669,12 @@ function html_tag_parser($content) //Fancybox $url_regex = '((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))'; if (preg_match_all('/\!\{.*?\)\[.*?\]/i', $content, $matches)) { - $i = 0; - if ($i < sizeof($matches)) { + for ($i = 0; $i < sizeof($matches); $i++) { $content = str_replace( $matches[$i], preg_replace('/!\{([^\{\}]+)*\}\(' . $url_regex . '\)\[' . $url_regex . '\]/i', '$1', $matches[$i]), $content ); - $i++; } } $content = preg_replace('/!\{([^\{\}]+)*\}\(' . $url_regex . '\)/i', '$1', $content); @@ -1864,7 +1860,7 @@ function create_sakura_table() if (!$wpdb->get_var("SELECT COUNT(*) FROM $sakura_table_name WHERE mate_key = 'manifest_json'")) { $manifest = array( "mate_key" => "manifest_json", - "mate_value" => file_get_contents(get_template_directory() . "/manifest/manifest.json"), + "mate_value" => file_get_contents(get_stylesheet_directory() . "/manifest/manifest.json"), ); $wpdb->insert($sakura_table_name, $manifest); } diff --git a/inc/api.php b/inc/api.php index 0c1f8fd..e652731 100644 --- a/inc/api.php +++ b/inc/api.php @@ -325,14 +325,23 @@ EOS; * @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/image/cover */ function cover_gallery() { - global $wpdb,$sakura_image_array; - $img_array = json_decode($sakura_image_array, true); - $img = array_rand($img_array); - $img_domain = akina_option('cover_cdn') ? akina_option('cover_cdn') : get_template_directory_uri(); - if(strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) { - $imgurl = $img_domain . "/manifest/" . $img_array[$img]["webp"][0]; - } else { - $imgurl = $img_domain . "/manifest/" . $img_array[$img]["jpeg"][0]; + if(akina_option('cover_cdn_options')=="type_2"){ + $img_array = glob(get_template_directory() . "/manifest/gallary/*.{gif,jpg,png}",GLOB_BRACE); + $img = array_rand($img_array); + $imgurl = trim($img_array[$img]); + $imgurl = str_replace(get_template_directory(), get_template_directory_uri(), $imgurl); + }elseif(akina_option('cover_cdn_options')=="type_3"){ + $imgurl = akina_option('cover_cdn'); + }else{ + global $sakura_image_array; + $img_array = json_decode($sakura_image_array, true); + $img = array_rand($img_array); + $img_domain = akina_option('cover_cdn') ? akina_option('cover_cdn') : get_template_directory_uri(); + if(strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) { + $imgurl = $img_domain . "/manifest/" . $img_array[$img]["webp"][0]; + } else { + $imgurl = $img_domain . "/manifest/" . $img_array[$img]["jpeg"][0]; + } } $data = array('cover image'); $response = new WP_REST_Response($data); @@ -346,14 +355,23 @@ function cover_gallery() { * @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/image/feature */ function feature_gallery() { - global $wpdb,$sakura_image_array; - $img_array = json_decode($sakura_image_array, true); - $img = array_rand($img_array); - $img_domain = akina_option('cover_cdn') ? akina_option('cover_cdn') : get_template_directory_uri(); - if(strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) { - $imgurl = $img_domain . "/manifest/" . $img_array[$img]["webp"][0]; - } else { - $imgurl = $img_domain . "/manifest/" . $img_array[$img]["jpeg"][0]; + if(akina_option('cover_cdn_options')=="type_2"){ + $img_array = glob(get_template_directory() . "/manifest/gallery/*.{gif,jpg,png}",GLOB_BRACE); + $img = array_rand($img_array); + $imgurl = trim($img_array[$img]); + $imgurl = str_replace(get_template_directory(), get_template_directory_uri(), $imgurl); + }elseif(akina_option('cover_cdn_options')=="type_3"){ + $imgurl = akina_option('cover_cdn'); + }else{ + global $sakura_image_array; + $img_array = json_decode($sakura_image_array, true); + $img = array_rand($img_array); + $img_domain = akina_option('cover_cdn') ? akina_option('cover_cdn') : get_template_directory_uri(); + if(strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) { + $imgurl = $img_domain . "/manifest/" . $img_array[$img]["webp"][0]; + } else { + $imgurl = $img_domain . "/manifest/" . $img_array[$img]["jpeg"][0]; + } } $data = array('cover image'); $response = new WP_REST_Response($data); @@ -369,7 +387,7 @@ function feature_gallery() { function update_database() { global $wpdb; $sakura_table_name = $wpdb->base_prefix.'sakura'; - $img_domain = akina_option('cover_cdn') ? akina_option('cover_cdn') : get_template_directory_uri(); + $img_domain = akina_option('cover_cdn') ? akina_option('cover_cdn') : get_template_directory(); $manifest = file_get_contents($img_domain . "/manifest/manifest.json"); if($manifest) { $manifest = array( @@ -387,7 +405,7 @@ function update_database() { $wpdb->insert($sakura_table_name,$time); $output = "manifest.json has been stored into database."; }else{ - $output = "manifest.json not found, please ensure your url is corrent."; + $output = "manifest.json not found, please ensure your url ($img_domain) is corrent."; } $result = new WP_REST_Response($output, 200); return $result; diff --git a/options.php b/options.php index 96e5187..456217a 100644 --- a/options.php +++ b/options.php @@ -374,8 +374,21 @@ function optionsframework_options() )); $options[] = array( - 'name' => __('Cover manifest', 'sakura'), /*封面图片库*/ - 'desc' => sprintf(__('Fill in the manifest path for random picture display, without adding a slash at the end, for example: https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3, please refer to Wiki . Click here to update manifest', 'sakura'), rest_url('sakura/v1/database/update')), /*填写 manifest 路径 ,用于随机图片展示,最后不用加斜杠,例:https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3 ,更多信息请参考Wiki*/ + 'name' => __('Cover manifest', 'sakura'), /*封面图片库选项*/ + 'desc' => __('Select how to call the cover random image', 'sakura'), /*选择封面随机图的调用方式*/ + 'id' => 'cover_cdn_options', + 'std' => "type_1", + 'type' => "select", + 'options' => array( + 'type_1' => __('webp images (optimization)', 'sakura'), /*webp优化随机图*/ + 'type_2' => __('built-in api (easiest)', 'sakura'), /*内置原图随机图*/ + 'type_3' => __('custom api (advanced)', 'sakura'), /*外部随机图API*/ + ) + ); + + $options[] = array( + 'name' => __('Cover images url', 'sakura'), /*图片库url*/ + 'desc' => sprintf(__('Fill in the manifest path for random picture display, please refer to Wiki . If you select webp images above, click here to update manifest', 'sakura'), rest_url('sakura/v1/database/update')), /*填写 manifest 路径,更多信息请参考Wiki,,如果你在上面选择了webp优化,点击这里更新 manifest*/ 'id' => 'cover_cdn', 'std' => 'https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3', 'type' => 'text');