From 5197e50c2e476462c9ba0144cd81a727da3abb08 Mon Sep 17 00:00:00 2001 From: spirit Date: Wed, 27 Nov 2019 17:17:54 +0800 Subject: [PATCH] rest api for random images --- functions.php | 28 +++++++--------------------- inc/api.php | 36 ++++++++++++++++++++++++++++++++++++ inc/decorate.php | 2 +- inc/swicher.php | 2 +- inc/theme_plus.php | 2 +- 5 files changed, 46 insertions(+), 24 deletions(-) diff --git a/functions.php b/functions.php index dfab6c8..8b3a693 100644 --- a/functions.php +++ b/functions.php @@ -1565,29 +1565,15 @@ function html_tag_parser($content) { if (preg_match_all('/\!\{.*?\)\[.*?\]/i', $content,$matches)){ $i=0; if ($i', - $matches[$i]), - $content); + $content=str_replace( + $matches[$i], + preg_replace('/!\{([^\{\}]+)*\}\('.$url_regex.'\)\['.$url_regex.'\]/i','$1',$matches[$i]), + $content + ); $i++; } } - $content=preg_replace( - '/!\{([^\{\}]+)*\}\('.$url_regex.'\)/i', - '', - $content - ); + $content=preg_replace('/!\{([^\{\}]+)*\}\('.$url_regex.'\)/i','$1',$content); //Github cards $content=preg_replace( @@ -1646,7 +1632,7 @@ function change_avatar($avatar){ // default feature image function DEFAULT_FEATURE_IMAGE() { if ( empty( akina_option('default_feature_image' )) ) { - return get_template_directory_uri().'/feature/index.php?'.rand(1,1000); + return rest_url('sakura/v1/image/feature').'?'.rand(1,1000); //return 'https://api.mashiro.top/feature/?'.rand(1,1000); } else { return akina_option('default_feature_image').'?'.rand(1,1000); diff --git a/inc/api.php b/inc/api.php index 9cb0ca8..a175108 100644 --- a/inc/api.php +++ b/inc/api.php @@ -12,6 +12,14 @@ add_action('rest_api_init', function () { 'methods' => 'GET', 'callback' => 'cache_search_json', )); + register_rest_route('sakura/v1', '/image/cover', array( + 'methods' => 'GET', + 'callback' => 'cover_gallery', + )); + register_rest_route('sakura/v1', '/image/feature', array( + 'methods' => 'GET', + 'callback' => 'feature_gallery', + )); }); /** @@ -262,3 +270,31 @@ EOS; return $result; } + +/* + * 随机封面图 rest api + * @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/image/cover + */ +function cover_gallery() { + $img_array = glob(get_wp_root_path(). "/themes/Sakura/cover/gallery/*.{gif,jpg,png}",GLOB_BRACE); + $img = array_rand($img_array); + $imgurl = $img_array[$img]; + $mimetype = end(getimagesize($imgurl)); + $imgdata = file_get_contents($imgurl); + header("Content-type: ".$mimetype); + echo $imgdata; +} + +/* + * 随机文章特色图 rest api + * @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/image/feature + */ +function feature_gallery() { + $img_array = glob(get_wp_root_path(). "/themes/Sakura/feature/gallery/*.{gif,jpg,png}",GLOB_BRACE); + $img = array_rand($img_array); + $imgurl = $img_array[$img]; + $mimetype = end(getimagesize($imgurl)); + $imgdata = file_get_contents($imgurl); + header("Content-type: ".$mimetype); + echo $imgdata; +} \ No newline at end of file diff --git a/inc/decorate.php b/inc/decorate.php index 6389b26..538293f 100644 --- a/inc/decorate.php +++ b/inc/decorate.php @@ -82,7 +82,7 @@ if ( akina_option('toggle-menu') == 'no') { ?> if (akina_option('cover_img')) { $imgurl = akina_option('cover_img'); } else { - $imgurl = get_site_url()."/wp-content/themes/Sakura/cover/index.php"; + $imgurl = rest_url('sakura/v1/image/cover'); } diff --git a/inc/swicher.php b/inc/swicher.php index f2ec4d0..258eb3f 100644 --- a/inc/swicher.php +++ b/inc/swicher.php @@ -75,7 +75,7 @@ mashiro_option.float_player_on = true; if (akina_option('cover_img')) { $imgurl = akina_option('cover_img'); } else { - $imgurl = get_site_url()."/wp-content/themes/Sakura/cover/index.php"; + $imgurl = rest_url('sakura/v1/image/cover'); } ?> mashiro_option.cover_api = ""; diff --git a/inc/theme_plus.php b/inc/theme_plus.php index c554094..115b200 100644 --- a/inc/theme_plus.php +++ b/inc/theme_plus.php @@ -44,7 +44,7 @@ function get_avatar_profile_url(){ */ function get_random_bg_url(){ if ( empty( akina_option('default_feature_image' )) ) { - return get_template_directory_uri().'/feature/index.php?'.rand(1,1000); + return rest_url('sakura/v1/image/feature').'?'.rand(1,1000); } else { return akina_option('default_feature_image').'?'.rand(1,1000); }