From c2488979376656afe40061b092464d37c3dafe53 Mon Sep 17 00:00:00 2001 From: Seaton Jiang Date: Thu, 19 Aug 2021 23:10:46 +0800 Subject: [PATCH] refactor: formatting code --- 404.php | 21 ++-- comments.php | 139 +++++++++++++------------ header.php | 123 +++++++++++----------- inc/theme-dogecloud.php | 10 +- inc/theme-setting.php | 5 +- inc/theme-shortcode.php | 100 ++++++++++-------- inc/theme-smtp.php | 3 +- inc/theme-volcengine.php | 10 +- inc/theme-widgets.php | 215 +++++++++++++++++++++------------------ index.php | 41 ++++---- page.php | 12 ++- pages/page-content.php | 52 ++++++---- pages/page-smilies.php | 5 +- pages/page-toolbar.php | 15 +-- 14 files changed, 406 insertions(+), 345 deletions(-) diff --git a/404.php b/404.php index 6ad27bc..bc27b20 100644 --- a/404.php +++ b/404.php @@ -1,9 +1,10 @@ - * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ get_header(); ?> @@ -12,14 +13,14 @@ get_header(); ?>
-
-
+ if (!kratos_option('g_404')) { + $img = ASSET_PATH . '/assets/img/404.jpg'; + } else { + $img = kratos_option('g_404', ASSET_PATH . '/assets/img/404.jpg'); + } + echo $img; ?>"> +
+
diff --git a/comments.php b/comments.php index b8dc6db..80925fb 100644 --- a/comments.php +++ b/comments.php @@ -1,83 +1,88 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ if (isset($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) { - die(); + die(); } require get_template_directory() . '/pages/page-smilies.php'; -if (comments_open()) {?> -
-

-
- -
- -
- -
- 登录 之后才可以评论', 'kratos'), wp_login_url(get_permalink()));?> -
- -
-
- -
-
-
-
- -
- -
-
- -
-
-
- -
- -
-
+if (comments_open()) { ?> +
+

+
+ +
+ +
+ +
+ 登录 之后才可以评论', 'kratos'), wp_login_url(get_permalink())); ?>
- -
- -
-
- -
-
- + + +
+ +
+
+
+
+ +
+ +
+
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+
+ +
+
+
+
+ +
-
- - -
-
-
- - - ID);?> - - + + + ID); ?> + + +
-
- + \ No newline at end of file diff --git a/header.php b/header.php index 2dfd88e..b30cd70 100644 --- a/header.php +++ b/header.php @@ -1,83 +1,90 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ ?> + - <?php wp_title( '-', true, 'right' ); ?> + <?php wp_title('-', true, 'right'); ?> - - ' ?> - + + ' ?> + + -
-
\ No newline at end of file + + + +
+ +
\ No newline at end of file diff --git a/inc/theme-dogecloud.php b/inc/theme-dogecloud.php index 25e6965..dc186b9 100644 --- a/inc/theme-dogecloud.php +++ b/inc/theme-dogecloud.php @@ -1,13 +1,15 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ if (kratos_option('g_cos', false)) { - function dogcloud_upload($object, $file, $mime){ + function dogcloud_upload($object, $file, $mime) + { if (!@file_exists($file)) { return false; } @@ -120,7 +122,7 @@ if (kratos_option('g_cos', false)) { $request = new WP_Http; $result = $request->request($url, array('method' => 'POST', 'body' => $del_file_body, 'headers' => $headers)); - + return $file; } add_action('wp_delete_file', 'dogecloud_delete_remote_file', 100); @@ -148,4 +150,4 @@ if (kratos_option('g_cos', false)) { return $uploads; } add_filter('upload_dir', 'custom_upload_dir'); -} \ No newline at end of file +} diff --git a/inc/theme-setting.php b/inc/theme-setting.php index de077c8..6651f4c 100644 --- a/inc/theme-setting.php +++ b/inc/theme-setting.php @@ -1,9 +1,10 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ // 标题配置 @@ -104,7 +105,7 @@ function mourning() function share_thumbnail_url() { global $post; - if(!is_object($post)) + if (!is_object($post)) return; if (has_post_thumbnail($post->ID)) { $post_thumbnail_id = get_post_thumbnail_id($post); diff --git a/inc/theme-shortcode.php b/inc/theme-shortcode.php index dc05d05..ddbd24d 100644 --- a/inc/theme-shortcode.php +++ b/inc/theme-shortcode.php @@ -1,9 +1,10 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ function h2title($atts, $content = null, $code = "") @@ -177,7 +178,7 @@ add_shortcode('bilibili', 'bilibili'); function reply($atts, $content = null) { - extract(shortcode_atts(array("notice" => ''), $atts)); + extract(shortcode_atts(array("notice" => ''), $atts)); $userEmail = null; $user_ID = (int) wp_get_current_user()->ID; if ($user_ID > 0) { @@ -212,8 +213,9 @@ function reply($atts, $content = null) } add_shortcode('reply', 'reply'); -function accordion($atts, $content=null, $code=""){ - extract(shortcode_atts(array("title"=>__('标题内容','kratos')),$atts)); +function accordion($atts, $content = null, $code = "") +{ + extract(shortcode_atts(array("title" => __('标题内容', 'kratos')), $atts)); $return = '
'; $return .= $title; $return .= '
'; @@ -221,9 +223,10 @@ function accordion($atts, $content=null, $code=""){ $return .= '
'; return $return; } -add_shortcode('accordion','accordion'); +add_shortcode('accordion', 'accordion'); -function dplayer($atts = array(), $content = ''){ +function dplayer($atts = array(), $content = '') +{ static $instance = 0; $instance++; @@ -238,21 +241,25 @@ function dplayer($atts = array(), $content = ''){ 'type' => 'auto', 'mutex' => 'true', 'iconsColor' => '#ffffff' - ), $atts, 'dplayer'); + ), + $atts, + 'dplayer' + ); - $atts['autoplay'] = wp_validate_boolean( $atts['autoplay'] ); - $atts['theme'] = esc_attr( $atts['theme'] ); - $atts['loop'] = wp_validate_boolean( $atts['loop'] ); - $atts['preload'] = esc_attr( $atts['preload'] ); - $atts['src'] = esc_url_raw( $atts['src'] ); - $atts['poster'] = esc_url_raw( $atts['poster'] ); - $atts['type'] = strtolower( esc_attr( $atts['type'] ) ); - $atts['mutex'] = wp_validate_boolean( $atts['mutex'] ); - $atts['iconsColor'] = esc_attr( $atts['iconsColor'] ); + $atts['autoplay'] = wp_validate_boolean($atts['autoplay']); + $atts['theme'] = esc_attr($atts['theme']); + $atts['loop'] = wp_validate_boolean($atts['loop']); + $atts['preload'] = esc_attr($atts['preload']); + $atts['src'] = esc_url_raw($atts['src']); + $atts['poster'] = esc_url_raw($atts['poster']); + $atts['type'] = strtolower(esc_attr($atts['type'])); + $atts['mutex'] = wp_validate_boolean($atts['mutex']); + $atts['iconsColor'] = esc_attr($atts['iconsColor']); - if ( empty( $atts['src'] ) ) return; + if (empty($atts['src'])) return; - $output = sprintf( '', + $output = sprintf( + '', $instance, $instance, $atts['autoplay'], @@ -266,48 +273,50 @@ function dplayer($atts = array(), $content = ''){ $atts['iconsColor'] ); - $html = sprintf( '

', + $html = sprintf( + '

', $instance ); - add_action( 'wp_footer', function () use ( $output ) { + add_action('wp_footer', function () use ($output) { echo ' ' . $output . "\n"; - }, 99999 ); + }, 99999); return $html; } -add_shortcode('dplayer','dplayer'); +add_shortcode('dplayer', 'dplayer'); -function override_wp_video_shortcode( $html = '', $atts = array() ) { - if ( empty( $atts['src'] ) ) { - if (!empty( $atts['mp4'] )) { - $atts['src']=$atts['mp4']; - }elseif(!empty( $atts['m4v'] )) { - $atts['src']=$atts['m4v']; - }elseif(!empty( $atts['webm'] )) { - $atts['src']=$atts['webm']; - }elseif(!empty( $atts['ogv'] )) { - $atts['src']=$atts['ogv']; - }elseif(!empty( $atts['wmv'] )) { - $atts['src']=$atts['wmv']; - }elseif(!empty( $atts['flv'] )) { - $atts['src']=$atts['flv']; +function override_wp_video_shortcode($html = '', $atts = array()) +{ + if (empty($atts['src'])) { + if (!empty($atts['mp4'])) { + $atts['src'] = $atts['mp4']; + } elseif (!empty($atts['m4v'])) { + $atts['src'] = $atts['m4v']; + } elseif (!empty($atts['webm'])) { + $atts['src'] = $atts['webm']; + } elseif (!empty($atts['ogv'])) { + $atts['src'] = $atts['ogv']; + } elseif (!empty($atts['wmv'])) { + $atts['src'] = $atts['wmv']; + } elseif (!empty($atts['flv'])) { + $atts['src'] = $atts['flv']; } }; $video_attr_strings = array(); - foreach ( $atts as $k => $v ) { - if ( $v == '' ) continue; - $video_attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; + foreach ($atts as $k => $v) { + if ($v == '') continue; + $video_attr_strings[] = $k . '="' . esc_attr($v) . '"'; } - $html .= sprintf( '[dplayer %s]', join( ' ', $video_attr_strings ) ); + $html .= sprintf('[dplayer %s]', join(' ', $video_attr_strings)); - return do_shortcode( $html ); + return do_shortcode($html); } -if ( ! is_admin() ){ - add_filter( 'wp_video_shortcode_override' , 'override_wp_video_shortcode', 1, 2 ); +if (!is_admin()) { + add_filter('wp_video_shortcode_override', 'override_wp_video_shortcode', 1, 2); } add_action('init', 'more_button'); @@ -322,12 +331,13 @@ function more_button() } } -function add_more_buttons($buttons) { +function add_more_buttons($buttons) +{ $buttons[] = 'hr'; $buttons[] = 'wp_page'; $buttons[] = 'fontsizeselect'; $buttons[] = 'styleselect'; -return $buttons; + return $buttons; } add_filter("mce_buttons", "add_more_buttons"); diff --git a/inc/theme-smtp.php b/inc/theme-smtp.php index 162877a..f528138 100644 --- a/inc/theme-smtp.php +++ b/inc/theme-smtp.php @@ -1,9 +1,10 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ if (kratos_option('m_smtp', false)) { diff --git a/inc/theme-volcengine.php b/inc/theme-volcengine.php index 73571d7..b34fc79 100644 --- a/inc/theme-volcengine.php +++ b/inc/theme-volcengine.php @@ -1,9 +1,10 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ require_once 'volcengine-imagex/vendor/autoload.php'; @@ -20,7 +21,8 @@ if (kratos_option('g_imgx', false)) { return $imagex_client; } - function imagex_upload($object, $file){ + function imagex_upload($object, $file) + { if (!@file_exists($file)) { return false; } @@ -140,7 +142,7 @@ if (kratos_option('g_imgx', false)) { return $content; } add_filter('the_content', 'imagex_setting_content_ci'); - + function imagex_setting_post_thumbnail_ci($html, $post_id, $post_image_id) { if (has_post_thumbnail()) { @@ -154,4 +156,4 @@ if (kratos_option('g_imgx', false)) { return $html; } add_filter('post_thumbnail_html', 'imagex_setting_post_thumbnail_ci', 10, 3); -} \ No newline at end of file +} diff --git a/inc/theme-widgets.php b/inc/theme-widgets.php index 1a4e447..a03fa1a 100644 --- a/inc/theme-widgets.php +++ b/inc/theme-widgets.php @@ -1,9 +1,10 @@ * @license MIT License - * @version 2021.06.26 + * @version 2021.08.19 */ // 添加小工具 @@ -55,20 +56,22 @@ function widgets_init() add_action('widgets_init', 'widgets_init'); // 分类目录计数 -function cat_count_span( $links ) { - $links = str_replace( ' (', ' / ', $links ); - $links = str_replace( ')', __('篇', 'kratos') . '', $links ); +function cat_count_span($links) +{ + $links = str_replace(' (', ' / ', $links); + $links = str_replace(')', __('篇', 'kratos') . '', $links); return $links; } -add_filter( 'wp_list_categories', 'cat_count_span' ); +add_filter('wp_list_categories', 'cat_count_span'); // 文章归档计数 -function archive_count_span( $links ) { - $links = str_replace( ' (', ' / ', $links ); - $links = str_replace( ')', __('篇', 'kratos') . '', $links ); +function archive_count_span($links) +{ + $links = str_replace(' (', ' / ', $links); + $links = str_replace(')', __('篇', 'kratos') . '', $links); return $links; } -add_filter( 'get_archives_link', 'archive_count_span' ); +add_filter('get_archives_link', 'archive_count_span'); // 小工具文章聚合 - 热点文章 function most_comm_posts($days = 30, $nums = 6) @@ -94,7 +97,8 @@ function most_comm_posts($days = 30, $nums = 6) echo $output; } -function timeago($time) { +function timeago($time) +{ $time = strtotime($time); $dtime = time() - $time; if ($dtime < 1) return __('刚刚', 'kratos'); @@ -113,29 +117,30 @@ function timeago($time) { } } -function string_cut($string, $sublen, $start = 0, $code = 'UTF-8') { - if($code == 'UTF-8') { +function string_cut($string, $sublen, $start = 0, $code = 'UTF-8') +{ + if ($code == 'UTF-8') { $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; preg_match_all($pa, $string, $t_string); - if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)) . "..."; + if (count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)) . "..."; return join('', array_slice($t_string[0], $start, $sublen)); } else { $start = $start * 2; $sublen = $sublen * 2; $strlen = strlen($string); $tmpstr = ''; - for($i = 0; $i < $strlen; $i++) { - if($i >= $start && $i < ($start + $sublen)) { - if(ord(substr($string, $i, 1)) > 129) $tmpstr .= substr($string, $i, 2); + for ($i = 0; $i < $strlen; $i++) { + if ($i >= $start && $i < ($start + $sublen)) { + if (ord(substr($string, $i, 1)) > 129) $tmpstr .= substr($string, $i, 2); else $tmpstr .= substr($string, $i, 1); } - if(ord(substr($string, $i, 1)) > 129) $i++; + if (ord(substr($string, $i, 1)) > 129) $i++; } - return $tmpstr; + return $tmpstr; } } -function latest_comments($list_number=5, $cut_length=50) +function latest_comments($list_number = 5, $cut_length = 50) { global $wpdb, $output; $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_content FROM {$wpdb->comments} LEFT OUTER JOIN {$wpdb->posts} ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID WHERE comment_approved = '1' AND (comment_type = '' OR comment_type = 'comment') AND user_id != '1' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT %d", $list_number)); @@ -157,42 +162,45 @@ function latest_comments($list_number=5, $cut_length=50) class widget_search extends WP_Widget { - public function __construct() { + public function __construct() + { $widget_ops = array( 'classname' => 'widget_search', - 'description' => __( 'A search form for your site.' ), + 'description' => __('A search form for your site.'), 'customize_selective_refresh' => true, ); - parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops ); + parent::__construct('search', _x('Search', 'Search widget'), $widget_ops); } - public function widget( $args, $instance ) { - $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; - $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); + public function widget($args, $instance) + { + $title = !empty($instance['title']) ? $instance['title'] : ''; + $title = apply_filters('widget_title', $title, $instance, $this->id_base); echo '

'. $username .'

' . $introduce . '

'; + echo '

' . $username . '

' . $introduce . '

'; echo '
'; } @@ -322,15 +330,15 @@ class widget_about extends WP_Widget public function form($instance) { $background = !empty($instance['background']) ? $instance['background'] : ASSET_PATH . '/assets/img/about-background.png'; - ?> + ?>

- + - +

- 'px', - 'smallest' => 14, - 'largest' => 14, - 'number' => $number, - 'format' => 'flat', - 'orderby' => 'count', - 'order' => $order, - 'echo' => false, - ) + $tags = wp_tag_cloud( + array( + 'unit' => 'px', + 'smallest' => 14, + 'largest' => 14, + 'number' => $number, + 'format' => 'flat', + 'orderby' => 'count', + 'order' => $order, + 'echo' => false, + ) ); echo '
'; echo '
' . __('标签聚合', 'kratos') . '
'; @@ -382,22 +391,22 @@ class widget_tags extends WP_Widget global $wpdb; $number = !empty($instance['number']) ? $instance['number'] : '8'; $order = !empty($instance['order']) ? $instance['order'] : 'RAND'; - ?> + ?>

- +

- +

- '; - ?> + ?> - '; + '; } public function update($new_instance, $old_instance) @@ -456,7 +469,7 @@ class widget_posts extends WP_Widget $instance['number'] = (!empty($new_instance['number'])) ? $new_instance['number'] : ''; $instance['days'] = (!empty($new_instance['days'])) ? $new_instance['days'] : ''; $instance['order'] = (!empty($new_instance['order'])) ? $new_instance['order'] : ''; - + return $instance; } public function form($instance) @@ -465,26 +478,26 @@ class widget_posts extends WP_Widget $number = !empty($instance['number']) ? $instance['number'] : '6'; $days = !empty($instance['days']) ? $instance['days'] : '30'; $order = !empty($instance['order']) ? $instance['order'] : 'hot'; - ?> + ?>

- +

- +

- +

-
'. $title .'
'; + echo '
' . $title . '
'; echo latest_comments($number, 50); echo '
'; } @@ -516,7 +529,7 @@ class widget_comments extends WP_Widget $instance['number'] = (!empty($new_instance['number'])) ? $new_instance['number'] : ''; $instance['title'] = (!empty($new_instance['title'])) ? $new_instance['title'] : ''; - + return $instance; } public function form($instance) @@ -524,18 +537,18 @@ class widget_comments extends WP_Widget global $wpdb; $number = !empty($instance['number']) ? $instance['number'] : '5'; $title = !empty($instance['title']) ? $instance['title'] : __('最近评论', 'kratos'); - ?> + ?>

- +

- +

- ' . "\n"; $prev_depth = ''; @@ -578,7 +591,7 @@ class widget_toc extends WP_Widget $index .= '' . "\n"; } elseif ($toc_depth > $prev_depth) { $to_depth++; - $index .= '
    ' . "\n"; + $index .= '
      ' . "\n"; } else { $to_depth2 = $to_depth > $prev_depth - $toc_depth ? $prev_depth - $toc_depth : $to_depth; if ($to_depth2) { @@ -590,7 +603,7 @@ class widget_toc extends WP_Widget $index .= ''; } } - $index .= '
    • ' . str_replace(array('[h2title]', '[/h2title]'),array('', ''),$toc_item['text']) . ''; + $index .= '
    • ' . str_replace(array('[h2title]', '[/h2title]'), array('', ''), $toc_item['text']) . ''; $prev_depth = $toc_item['depth']; } for ($i = 0; $i <= $to_depth; $i++) { @@ -599,7 +612,7 @@ class widget_toc extends WP_Widget wp_cache_set(get_the_ID(), $index, 'toc', 360000); $index = '
      ' . "\n" . '
      文章目录
      ' . "\n" . '
      ' . $index . '
      ' . "\n" . '
      '; } - + echo $index; } } diff --git a/index.php b/index.php index bbfee50..3e8e948 100644 --- a/index.php +++ b/index.php @@ -1,9 +1,10 @@ * @license MIT License - * @version 2021.06.26 + * @version 2021.08.19 */ get_header(); ?> @@ -11,32 +12,34 @@ get_header(); ?>
      - +
      -
      +
      + if (have_posts()) { + while (have_posts()) { + the_post(); + get_template_part('/pages/page-content', get_post_format()); + } + } else { ?>
      - +
      -
      diff --git a/page.php b/page.php index deca426..c12ab88 100644 --- a/page.php +++ b/page.php @@ -1,17 +1,19 @@ * @license MIT License - * @version 2021.06.26 + * @version 2021.08.19 */ get_header(); ?>
      -
      - +
      +

      @@ -33,8 +35,8 @@ get_header(); ?> 'before' => '', 'after' => '', 'next_or_number' => 'number', - 'link_before' =>'', - 'link_after'=>'' + 'link_before' => '', + 'link_after' => '' ) ); wp_link_pages( diff --git a/pages/page-content.php b/pages/page-content.php index 3fc659d..7a74d51 100644 --- a/pages/page-content.php +++ b/pages/page-content.php @@ -1,34 +1,39 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ ?>
      - - - ID) >= $article_comment || get_post_meta($post->ID, 'love', true) >= $article_love) { ?> - - - - -
      - - - -
      - -
      + + + ID) >= $article_comment || get_post_meta($post->ID, 'love', true) >= $article_love) { ?> + + + + +
      + + + +
      + +
      term_id) . '">' . $category[0]->cat_name . ''; + echo '' . $category[0]->cat_name . ''; } else { - echo ''. __('页面','kratos') .''; + echo '' . __('页面', 'kratos') . ''; } ?>

      @@ -40,11 +45,18 @@
      - + - - ID, 'love', true)) {echo get_post_meta($post->ID, 'love', true);} else {echo '0';} _e('人点赞', 'kratos'); ?> + + ID, 'love', true)) { + echo get_post_meta($post->ID, 'love', true); + } else { + echo '0'; + } + _e('人点赞', 'kratos'); ?> diff --git a/pages/page-smilies.php b/pages/page-smilies.php index 89d5989..7159465 100644 --- a/pages/page-smilies.php +++ b/pages/page-smilies.php @@ -1,9 +1,10 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ $smilies = ' @@ -26,4 +27,4 @@ $smilies = ' -'; \ No newline at end of file +'; diff --git a/pages/page-toolbar.php b/pages/page-toolbar.php index 66612a4..ace53ef 100644 --- a/pages/page-toolbar.php +++ b/pages/page-toolbar.php @@ -1,21 +1,22 @@ * @license MIT License - * @version 2021.06.25 + * @version 2021.08.19 */ ?>
      -
      +

      -

      +

      \ No newline at end of file