File: /www/wwwroot/www.scdc-marine.com/wp-content/themes/heku-cms/HekuClass/View/SEO.php
<?php
namespace Heku\HekuClass\View;
/**
* @package Heku\HekuClass\View
* @subpackage Classes/SEO
* @author Heku Daleconan <http://www.daleconan.com>
* @copyright Copyright (c) 2022-2023, Heku Daleconan
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
class SEO {
/**
* 类实现单例模式,类的独生子女
*/
private static $instance;
/**
* 依赖注入的接口 Instance 方法
* 目标是 保证 init() 初始化方法 只执行一次
*
* @return self 唯一实例化的本类
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self;
self::$instance->init();
}
return self::$instance;
}
private function init() {
add_action( 'seo_head_meta', array( $this, 'head_meta_html' ), 10 );
}
public function head_meta_html(){
$name = get_bloginfo('name');
if(is_home() || is_front_page() || is_404()){
$meta = self::get_home_meta();
echo '<title>'.$meta['title'].'</title>
<meta name="keywords" content="'.$meta['keywords'].'" />
<meta name="description" content="'.$meta['description'].'" />
<meta property="og:locale" content="'.get_locale().'" />
<meta property="og:type" content="article" />
<meta property="og:site_name" content="'.$name.'" />
<meta property="og:title" content="'.$meta['title'].'" />
<meta property="og:description" content="'.$meta['description'].'" />
<meta property="og:image" content="'.$meta['image'].'" />
<meta property="og:url" content="'.$meta['url'].'" />
<link rel="canonical" href="'.$meta['url'].'" />';
}elseif(is_singular()){
$meta = self::get_single_meta();
echo '<title>'.$meta['title'].'</title>
<meta name="keywords" content="'.$meta['keywords'].'" />
<meta name="description" content="'.$meta['description'].'" />
<meta property="og:locale" content="'.get_locale().'" />
<meta property="og:type" content="article" />
<meta property="og:site_name" content="'.$name.'" />
<meta property="og:title" content="'.$meta['title'].'" />
<meta property="og:description" content="'.$meta['description'].'" />
<meta property="og:image" content="'.$meta['image'].'" />
<meta property="og:updated_time" content="'.$meta['updated_time'].'" />
<meta property="og:url" content="'.$meta['url'].'" />
<link rel="canonical" href="'.$meta['url'].'" />';
}elseif(is_archive()){
$term = get_queried_object();
if(isset($term->term_id)){
$meta = self::get_archive_meta($term->term_id);
//$img = self::get_term_meta($term->term_id,'b2_tax_img');
echo '<title>'.$meta['title'].'</title>
<meta name="keywords" content="'.$meta['keywords'].'" />
<meta name="description" content="'.$meta['description'].'" />
<meta property="og:locale" content="'.get_locale().'" />
<meta property="og:type" content="article" />
<meta property="og:site_name" content="'.$name.'" />
<meta property="og:title" content="'.$meta['title'].'" />
<meta property="og:description" content="'.$meta['description'].'" />
<meta property="og:image" content="'.$meta['image'].'" />
<meta property="og:url" content="'.$meta['url'].'" />
<link rel="canonical" href="'.$meta['url'].'" />';
}else{
echo '<title>'.get_the_title().'</title>
<meta property="og:locale" content="'.get_locale().'" />
<meta property="og:type" content="article" />
<meta property="og:site_name" content="'.$name.'" />
<meta property="og:title" content="'.get_the_title().'" />
<meta property="og:url" content="'.self::get_seo_canonical_url().'" />
<link rel="canonical" href="'.self::get_seo_canonical_url().'" />';
}
}else{
echo '<title>'.get_the_title().'</title>
<meta property="og:locale" content="'.get_locale().'" />
<meta property="og:type" content="article" />
<meta property="og:site_name" content="'.$name.'" />
<meta property="og:title" content="'. get_the_title().'" />
<meta property="og:url" content="'.self::get_seo_canonical_url().'" />
<link rel="canonical" href="'.self::get_seo_canonical_url().'" />';
}
}
public static function get_home_meta(){
$seo_title = heku_get_option('seo_options','global_seo_title');
$seo_des = heku_get_option('seo_options','global_seo_description');
$seo_keywords = heku_get_option('seo_options','global_seo_keywords');
$seo_img_logo = heku_get_option('seo_options','global_seo_img');
$seo_img_logo = home_url(wp_parse_url($seo_img_logo)['path']);
$seo_url = self::get_seo_canonical_url();
//var_dump(wp_parse_url($seo_img_logo ));
return array(
'title' => $seo_title ? $seo_title : wp_get_document_title(),
'description' => $seo_des ? $seo_des : get_bloginfo('blogdescription'),
'keywords' => $seo_keywords ? $seo_keywords : get_bloginfo('name'),
'image' => $seo_img_logo,
'url' => $seo_url
);
}
public static function get_single_meta($post_id = 0){
if(!$post_id){
global $post;
$post_id = $post->ID;
}
//$author = esc_attr(get_post_field('post_author',$post_id));
$seo_title = self::get_post_meta($post_id,'heku_seo_title');
$seo_title = $seo_title ? $seo_title : get_the_title($post_id);
//$thumb_url = Post::get_post_thumb($post_id);
$seo_desc = self::get_post_meta($post_id,'heku_seo_description');
$seo_desc = $seo_desc ? $seo_desc : mb_strimwidth(preg_replace("/(\s|\ \;| |\xc2\xa0)/", "", strip_tags(apply_filters('the_content', get_post($post_id)->post_title))), 0, 60,"");
$seo_keywords = implode(',', array_column( wp_get_post_tags($post_id), 'name'));
$seo_keywords = $seo_keywords ? $seo_keywords : implode(',', array_column( get_the_category(), 'name'));
$thumb_url = get_the_post_thumbnail_url($post_id);
$thumb_url = $thumb_url ? $thumb_url : heku_get_option('seo_options','global_seo_img');
$seo_img = self::get_post_meta($post_id,'heku_seo_img');
$seo_img = $seo_img ? $seo_img : $thumb_url;
$seo_img = home_url(wp_parse_url($seo_img)['path']);
$seo_url = self::get_seo_canonical_url();
return array(
'id' => $post_id,
'title' => esc_attr($seo_title),
'keywords' => $seo_keywords,
'description' => $seo_desc,
'image' => $seo_img,
'url' => $seo_url,
'updated_time' => get_the_modified_date('c',$post_id),
//'author' => get_author_posts_url($author)
);
}
public static function get_archive_meta($term_id = 0){
if(!$term_id){
$term = get_queried_object();
$term_id = $term->term_id;
}
trim(strip_tags(get_the_archive_description()));
//$author = esc_attr(get_post_field('post_author',$post_id));
$seo_title = self::get_term_meta($term_id,'heku_seo_title');
$seo_title = $seo_title ? $seo_title : get_the_title($term_id);
//$thumb_url = Post::get_post_thumb($post_id);
$seo_desc = self::get_term_meta($term_id,'heku_seo_description');
//$seo_desc = $seo_desc ? $seo_desc : mb_strimwidth(preg_replace("/(\s|\ \;| |\xc2\xa0)/", "", strip_tags(apply_filters('the_content', get_post($term_id)->post_title))), 0, 60,"");
$seo_keywords = implode(',', array_column( wp_get_post_tags($term_id), 'name'));
$seo_keywords = $seo_keywords ? $seo_keywords : implode(',', array_column( get_the_category(), 'name'));
$seo_url = self::get_seo_canonical_url();
$seo_img = self::get_term_meta($term_id,'heku_seo_img');
$seo_img = $seo_img ? $seo_img : heku_get_option('seo_options','global_seo_img');
$seo_img = home_url(wp_parse_url($seo_img)['path']);
return array(
'id' => $term_id,
'title' => esc_attr($seo_title),
'keywords' => $seo_keywords,
'description' => $seo_desc,
'image' => $seo_img,
'url' => $seo_url,
'updated_time' => get_the_modified_date('c',$term_id),
//'author' => get_author_posts_url($author)
);
}
public static function get_term_meta($term_id,$key) {
$value = get_term_meta($term_id,$key,true);
$value = esc_attr($value);
return $value;
}
public static function get_post_meta($post_id,$key) {
$value = get_post_meta($post_id,$key,true);
$value = esc_attr($value);
return $value;
}
public static function get_seo_canonical_url(){
global $wp;
$current_url = home_url(add_query_arg( array(), $wp->request ));
//var_dump(wp_parse_url($current_url));
if(is_singular()){
$current_url = home_url(wp_parse_url(wp_get_canonical_url())['path']);
}
return $current_url;
}
public static function get_seo_title(){
if(is_singular()){
$meta = self::get_single_meta();
return $meta['title'];
}
if(is_archive()){
$meta = self::get_archive_meta();
return $meta['title'];
}
$meta = self::get_home_meta();
return $meta['title'];
}
}