Merge pull request #144 from spirit1431007/pr-branch

整合 API
This commit is contained in:
Mashiro 2019-12-03 21:35:58 +08:00 committed by GitHub
commit f301cdcde1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 999 additions and 955 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 55 KiB

View File

@ -1,14 +0,0 @@
<?php
$img_array = glob("gallery/*.{gif,jpg,png}",GLOB_BRACE);
$img = array_rand($img_array);
$imgurl=$img_array[$img];
if($imgurl) {
header("Location: " . $imgurl);
exit();
} else {
exit('error');
}
?>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 486 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 732 KiB

View File

@ -1,14 +0,0 @@
<?php
$img_array = glob("gallery/*.{gif,jpg,png}",GLOB_BRACE);
$img = array_rand($img_array);
$imgurl=$img_array[$img];
if($imgurl) {
header("Location: " . $imgurl);
exit();
} else {
exit('error');
}
?>

View File

@ -1565,29 +1565,15 @@ function html_tag_parser($content) {
if (preg_match_all('/\!\{.*?\)\[.*?\]/i', $content,$matches)){
$i=0;
if ($i<sizeof($matches)) {
$content=str_replace($matches[$i],preg_replace(
'/!\{([^\{\}]+)*\}\('.$url_regex.'\)\['.$url_regex.'\]/i',
'<a data-fancybox="gallery"
data-caption="$1"
class="fancybox"
href="$2"
alt="$1"
title="$1"><img src="$7" target="_blank" rel="nofollow" class="fancybox"></a>',
$matches[$i]),
$content);
$content=str_replace(
$matches[$i],
preg_replace('/!\{([^\{\}]+)*\}\('.$url_regex.'\)\['.$url_regex.'\]/i','<a href="$2"><img src="$7" alt="$1" title="$1"></a>',$matches[$i]),
$content
);
$i++;
}
}
$content=preg_replace(
'/!\{([^\{\}]+)*\}\('.$url_regex.'\)/i',
'<a data-fancybox="gallery"
data-caption="$1"
class="fancybox"
href="$2"
alt="$1"
title="$1"><img src="$2" target="_blank" rel="nofollow" class="fancybox"></a>',
$content
);
$content=preg_replace('/!\{([^\{\}]+)*\}\('.$url_regex.'\)/i','<a href="$2"><img src="$2" alt="$1" title="$1"></a>',$content);
//Github cards
$content=preg_replace(
@ -1645,12 +1631,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 'https://api.mashiro.top/feature/?'.rand(1,1000);
} else {
return akina_option('default_feature_image').'?'.rand(1,1000);
}
return rest_url('sakura/v1/image/feature').'?'.rand(1,1000);
}
//防止设置置顶文章造成的图片同侧bug
@ -1733,4 +1714,31 @@ function allow_more_tag_in_comment() {
$allowedtags['span'] = array('class'=>array());
}
add_action('pre_comment_on_post', 'allow_more_tag_in_comment');
/*
* 随机图
*/
function create_sakura_table(){
global $wpdb;
$sakura_table_name = $wpdb->base_prefix.'sakura';
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta("CREATE TABLE IF NOT EXISTS `" . $sakura_table_name . "` (
`key` varchar(50) COLLATE utf8_bin NOT NULL,
`value` text COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;");
//default data
$manifest = array(
"key" => "manifest_json",
"value" => file_get_contents(get_template_directory()."/manifest/manifest.json")
);
$time = array(
"key" => "json_time",
"value" => date("Y-m-d H:i:s",time())
);
$wpdb->insert($sakura_table_name,$manifest);
$wpdb->insert($sakura_table_name,$time);
}
add_action( 'after_setup_theme', 'create_sakura_table' );
//code end

View File

@ -12,6 +12,18 @@ 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',
));
register_rest_route('sakura/v1', '/image/manifest', array(
'methods' => 'POST',
'callback' => 'update_manifest_json',
));
register_rest_route('sakura/v1', '/qqinfo/json', array(
'methods' => 'GET',
'callback' => 'get_qq_info',
@ -324,3 +336,82 @@ EOS;
return $result;
}
/*
* 随机封面图 rest api
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/image/cover
*/
function cover_gallery() {
global $wpdb;
$img_array = json_decode($wpdb->get_var("SELECT `value` FROM `wp_sakura` WHERE `key`='manifest_json'"), true);
$img = array_rand($img_array);
$imgurl = akina_option('jsdelivr_cdn') . "/manifest/" . $img_array[$img]["webp"][0];
$data = array('cover image');
$response = new WP_REST_Response($data);
$response->set_status(302);
$response->header('Location', $imgurl);
return $response;
}
/*
* 随机文章特色图 rest api
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/image/feature
*/
function feature_gallery() {
global $wpdb;
$img_array = json_decode($wpdb->get_var("SELECT `value` FROM `wp_sakura` WHERE `key`='manifest_json'"), true);
$img = array_rand($img_array);
$imgurl = akina_option('jsdelivr_cdn') . "/manifest/" . $img_array[$img]["webp"][1];
$data = array('cover image');
$response = new WP_REST_Response($data);
$response->set_status(302);
$response->header('Location', $imgurl);
return $response;
}
/*
* update manifest.json rest api
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/image/json
*/
function update_manifest_json() {
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
$user = wp_authenticate($username, $password);
if (is_a($user, 'WP_User')) {
if (in_array('administrator', (array) $user->roles)) {
global $wpdb;
$sakura_table_name = $wpdb->base_prefix.'sakura';
$manifest = array(
"key" => "manifest_json",
"value" => file_get_contents($_FILES["manifest"]["tmp_name"])
);
$time = array(
"key" => "json_time",
"value" => date("Y-m-d H:i:s",time())
);
$wpdb->query("DELETE FROM `wp_sakura` WHERE `key` ='manifest_json'");
$wpdb->query("DELETE FROM `wp_sakura` WHERE `key` ='json_time'");
$wpdb->insert($sakura_table_name,$manifest);
$wpdb->insert($sakura_table_name,$time);
$output = array(
'status' => 200,
'success' => true,
'message' => 'manifest.json has been stored into database'
);
$result = new WP_REST_Response($output, 200);
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
}
} else {
$output = array(
'status' => 401,
'success' => false,
'message' => 'Not Authorized.'
);
$result = new WP_REST_Response($output, 401);
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
}
}

View File

@ -78,15 +78,7 @@ if ( akina_option('toggle-menu') == 'no') { ?>
.comments .comments-hidden {display:none !important;}
<?php } // comments ?>
<?php
if (akina_option('cover_img')) {
$imgurl = akina_option('cover_img');
} else {
$imgurl = get_site_url()."/wp-content/themes/Sakura/cover/index.php";
}
$image_api = 'background-image: url("'.$imgurl.'");';
$image_api = 'background-image: url("'.rest_url('sakura/v1/image/cover').'");';
$bg_style = akina_option('focus_height') ? 'background-position: center center;background-attachment: inherit;' : '';
?>
.centerbg{<?php echo $image_api.$bg_style ?>background-position: center center;background-attachment: inherit;}

View File

@ -71,13 +71,7 @@ mashiro_option.jsdelivr_css_src = "https://cdn.jsdelivr.net/gh/mashirozx/Sakura@
mashiro_option.float_player_on = true;
<?php endif; ?>
<?php
if (akina_option('cover_img')) {
$imgurl = akina_option('cover_img');
} else {
$imgurl = get_site_url()."/wp-content/themes/Sakura/cover/index.php";
} ?>
mashiro_option.cover_api = "<?php echo $imgurl; ?>";
mashiro_option.cover_api = "<?php echo rest_url('sakura/v1/image/cover'); ?>";
/*End of Initial Variables*/
</script>

View File

@ -43,11 +43,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);
} else {
return akina_option('default_feature_image').'?'.rand(1,1000);
}
return rest_url('sakura/v1/image/feature').'?'.rand(1,1000);
}
@ -118,7 +114,10 @@ if(akina_option('classify_display')){
*/
function comment_add_at( $comment_text, $comment = '') {
if( $comment->comment_parent > 0) {
$comment_text = '<a href="#comment-' . $comment->comment_parent . '" class="comment-at">@'.get_comment_author( $comment->comment_parent ) . '</a>&nbsp;' . $comment_text;
if(substr($comment_text, 0, 3) === "<p>")
$comment_text = str_replace(substr($comment_text, 0, 3), '<p><a href="#comment-' . $comment->comment_parent . '" class="comment-at">@'.get_comment_author( $comment->comment_parent ) . '</a>&nbsp;', $comment_text);
else
$comment_text = '<a href="#comment-' . $comment->comment_parent . '" class="comment-at">@'.get_comment_author( $comment->comment_parent ) . '</a>&nbsp;' . $comment_text;
}
return $comment_text;
}

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -262,13 +262,6 @@ function optionsframework_options() {
'right' => __('Right', 'sakura'),/*向右对齐*/
'alternate' => __('Alternate', 'sakura')/*左右交替*/
));
$options[] = array(
'name' => __('Default article feature image', 'sakura'),/*默认文章特色图*/
'desc' => __('The default image displayed without the article feature image set, use random cover if left blank (the image to be displayed is placed in the /wp-content/themes/Sakura/feature/gallery/ directory)', 'sakura'),/*在未设置文章特色图的情况下展示的默认图像,留空则调用本地随机封面(要展示的图片放入 /wp-content/themes/Sakura/feature/gallery/ 目录)*/
'id' => 'default_feature_image',
'std' => 'https://api.mashiro.top/feature/',
'type' => 'text');
$options[] = array(
'name' => __('Comment shrink', 'sakura'),/*评论收缩*/
@ -418,13 +411,6 @@ function optionsframework_options() {
'std' => '',
'type' => 'text');
$options[] = array(
'name' => __('Cover image', 'sakura'),/*封面图*/
'desc' => __('Leave this blank to use the built-in API (put the images that need to be randomly displayed into the /cover/gallery/ directory)', 'sakura'),/*此处留空则使用内置API将需要随机展示的图片放入 /cover/gallery/ 目录)*/
'id' => 'cover_img',
'std' => '',
'type' => 'text');
$options[] = array(
'name' => __('Background image filter', 'sakura'),/*背景图滤镜*/
'id' => 'focus_img_filter',
@ -889,6 +875,13 @@ function optionsframework_options() {
'std' => '',
'type' => 'text');
$options[] = array(
'name' => __('Jsdelivr CDN', 'sakura'),/*图片库*/
'desc' => __('Fill in the cdn 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 <a href = "https: //github.com/mashirozx/Sakura/wiki/options">Wiki </a>', 'sakura'),/*填写 cdn 路径用于随机图片展示最后不用加斜杠https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3 ,更多信息请参考<a href="https://github.com/mashirozx/Sakura/wiki/options">Wiki</a>*/
'id' => 'jsdelivr_cdn',
'std' => 'https://cdn.jsdelivr.net/gh/mashirozx/sakura@3.3.3',
'type' => 'text');
$options[] = array(
'name' => __('Use the front-end library locally (lib.js、lib.css)', 'sakura'),/*本地调用前端库lib.js、lib.css*/
'desc' => __('The front-end library don\'t load from jsDelivr, not recommand', 'sakura'),/*前端库不走 jsDelivr不建议启用*/