<?php
/*
Plugin Name: 主题多多 WordPress SEO
Description: 这款 SEO 插件可以为您的 WordPress 网站首页、文章页、普通页面、分类目录、标签添加自定义 SEO 设置选项: 标题 (title)、 描述 (description)、关键字 (keywords）。正确使用该SEO插件可以有效地提升您的 WordPress 网站在搜索引擎的排名和索引量。
Version: 2.0
Author: 主题多多
Author URI: https://www.zhutiduoduo.com
License: GPL
Copyright: 主题多多
Text Domain: zhutiduoduo-seo
*/

include dirname(__FILE__) . '/advanced-custom-fields/acf.php';

define( 'ACF_LITE', true );

add_filter('acf/settings/remove_wp_meta_box', '__return_false');

/* ACF Custom Fields for post, page and archives */
if(function_exists("register_field_group"))
{
	register_field_group(array (
		'id' => 'acf_archive_seo',
		'title' => 'SEO信息设置',
		'fields' => array (
			array (
				'key' => 'field_5b6e81c2edbf8',
				'label' => '标题 (title)',
				'name' => 'archive_title',
				'type' => 'text',
				'default_value' => '',
				'placeholder' => '',
				'prepend' => '',
				'append' => '',
				'formatting' => 'html',
				'maxlength' => '',
			),
			array (
				'key' => 'field_5b6e81cdedbf9',
				'label' => '描述 (description)',
				'name' => 'archive_description',
				'type' => 'textarea',
				'default_value' => '',
				'placeholder' => '',
				'maxlength' => '',
				'rows' => '4',
				'formatting' => 'br',
			),
			array (
				'key' => 'field_5b6e81e4edbfa',
				'label' => '关键词 (keywords)',
				'name' => 'archive_keywords',
				'type' => 'text',
				'default_value' => '',
				'placeholder' => '',
				'prepend' => '',
				'append' => '',
				'formatting' => 'html',
				'maxlength' => '',
			),
		),
		'location' => array (
			array (
				array (
					'param' => 'page_type',
					'operator' => '==',
					'value' => 'front_page',
					'order_no' => 0,
					'group_no' => 0,
				),
			),
			array (
				array (
					'param' => 'ef_taxonomy',
					'operator' => '==',
					'value' => 'all',
					'order_no' => 0,
					'group_no' => 1,
				),
			),
		),
		'options' => array (
			'position' => 'normal',
			'layout' => 'default',
			'hide_on_screen' => array (
			),
		),
		'menu_order' => 0,
	));
	register_field_group(array (
		'id' => 'acf_post_seo',
		'title' => 'SEO信息设置',
		'fields' => array (
			array (
				'key' => 'field_5b6e906dda24d',
				'label' => '标题 (title)',
				'name' => 'post_title',
				'type' => 'text',
				'default_value' => '',
				'placeholder' => '',
				'prepend' => '',
				'append' => '',
				'formatting' => 'html',
				'maxlength' => '',
			),
			array (
				'key' => 'field_5b8177751723a',
				'label' => '描述 (description)',
				'name' => 'post_description',
				'type' => 'textarea',
				'default_value' => '',
				'placeholder' => '',
				'maxlength' => '',
				'rows' => 4,
				'formatting' => 'br',
			),
			array (
				'key' => 'field_5b6e9079da24e',
				'label' => '关键词 (keywords)',
				'name' => 'post_keywords',
				'type' => 'text',
				'default_value' => '',
				'placeholder' => '',
				'prepend' => '',
				'append' => '',
				'formatting' => 'html',
				'maxlength' => '',
			),			
		),
		'location' => array (
			array (
				array (
					'param' => 'post_type',
					'operator' => '!=',
					'value' => 'page',
					'order_no' => 0,
					'group_no' => 0,
				),
			),					
		),
		'options' => array (
			'position' => 'normal',
			'layout' => 'default',
			'hide_on_screen' => array (
			),
		),
		'menu_order' => 0,
	));
	register_field_group(array (
		'id' => 'acf_page_seo',
		'title' => '页面SEO设置',
		'fields' => array (
			array (
				'key' => 'field_5b6e8102f5bfd',
				'label' => '标题 (title)',
				'name' => 'page_title',
				'type' => 'text',
				'default_value' => '',
				'placeholder' => '',
				'prepend' => '',
				'append' => '',
				'formatting' => 'html',
				'maxlength' => '',
			),
			array (
				'key' => 'field_5b6e8fa499767',
				'label' => '描述 (description)',
				'name' => 'page_description',
				'type' => 'textarea',
				'default_value' => '',
				'placeholder' => '',
				'maxlength' => '',
				'rows' => '3',
				'formatting' => 'br',
			),
			array (
				'key' => 'field_5b6e8147f5bff',
				'label' => '关键词 (keywords)',
				'name' => 'page_keywords',
				'type' => 'text',
				'default_value' => '',
				'placeholder' => '',
				'prepend' => '',
				'append' => '',
				'formatting' => 'html',
				'maxlength' => '',
			),
		),
		'location' => array (
			array (
				array (
					'param' => 'post_type',
					'operator' => '==',
					'value' => 'page',
					'order_no' => 0,
					'group_no' => 0,
				),
			),
		),
		'options' => array (
			'position' => 'normal',
			'layout' => 'default',
			'hide_on_screen' => array (
			),
		),
		'menu_order' => 0,
	));
}


/* Custom functions for homepage SEO */
/* Custom functions for homepage SEO */
new zhutubaba_seo_general_setting();

class zhutubaba_seo_general_setting {

    public function __construct() {
        add_action( 'admin_init', array( $this, 'zhutubaba_seo_register_fields' ) );
    }

    public function zhutubaba_seo_register_fields() {

        register_setting( 'general', 'custom_site_title', 'sanitize_text_field' );
        register_setting( 'general', 'custom_site_description', 'sanitize_textarea_field' );
        register_setting( 'general', 'custom_site_keywords', 'sanitize_textarea_field' );
        register_setting( 'general', 'custom_sep', 'sanitize_text_field' );

        add_settings_field(
            'custom_site_title',
            '<label for="custom_site_title">首页标题 (title)</label>',
            array( $this, 'field_title' ),
            'general'
        );

        add_settings_field(
            'custom_site_description',
            '<label for="custom_site_description">首页描述 (description)</label>',
            array( $this, 'field_description' ),
            'general'
        );

        add_settings_field(
            'custom_site_keywords',
            '<label for="custom_site_keywords">首页关键词 (keywords)</label>',
            array( $this, 'field_keywords' ),
            'general'
        );

        add_settings_field(
            'custom_sep',
            '<label for="custom_sep">标题分隔符</label>',
            array( $this, 'field_sep' ),
            'general'
        );
    }

    public function field_title() {
        echo '<input type="text" id="custom_site_title" name="custom_site_title" class="regular-text" value="' . esc_attr( get_option( 'custom_site_title', '' ) ) . '" />';
    }

    public function field_description() {
        echo '<textarea id="custom_site_description" name="custom_site_description" class="regular-text" rows="5">' . esc_textarea( get_option( 'custom_site_description', '' ) ) . '</textarea>';
    }

    public function field_keywords() {
        echo '<textarea id="custom_site_keywords" name="custom_site_keywords" class="regular-text" rows="3">' . esc_textarea( get_option( 'custom_site_keywords', '' ) ) . '</textarea>';
    }

    public function field_sep() {
        echo '<input type="text" id="custom_sep" name="custom_sep" class="small-text" value="' . esc_attr( get_option( 'custom_sep', '-' ) ) . '" />';
    }
}


/* Generate custom title, description and keywords in header */
add_action('wp_head', 'zhutiduoduo_header_seo_code');

function zhutiduoduo_archive_title( $title ) {
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    } elseif ( is_tag() ) {
        $title = single_tag_title( '', false );
    } elseif ( is_author() ) {
        $title = get_the_author();
    } elseif ( is_post_type_archive() ) {
        $title = post_type_archive_title( '', false );
    } elseif ( is_tax() ) {
        $title = single_term_title( '', false );
    } elseif ( is_year() ) {
        $title  = get_the_date( _x( 'Y', 'yearly archives date format' ) );
    } elseif ( is_month() ) {
        $title  = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
    } elseif ( is_day() ) {
        $title  = get_the_date( _x( 'F j, Y', 'daily archives date format' ) );
	}
  
    return $title;
}
 
add_filter( 'get_the_archive_title', 'zhutiduoduo_archive_title' );

function zhutiduoduo_header_seo_code() {

	$sep = get_option( 'custom_sep','-');

	if ( is_paged() ) {
		$page = $sep . '第' . get_query_var('paged') . '页';
	} else {
		$page = '';
	}
	$title = "";
	$description = "";
	$keywords = "";

	if ( function_exists('get_field') && is_single() ) {
		// Post SEO
		if (get_field('post_title')) {
			$title = get_field('post_title');
		} else {
			$title = get_the_title() . $sep . get_bloginfo('name');
		}

		if (get_field('post_description')) {
			$description = get_field('post_description');
		} else {

			while ( have_posts() ) : the_post();
				$description = get_the_excerpt();
			endwhile; 

		}

		if (get_field('post_keywords')) {
			$keywords = get_field('post_keywords');
		} else {

			$posttags = get_the_tags();
			$i = 1;
			
			if(!empty($posttags)) {
				
				foreach ($posttags as $tag) {
				    if ($i == 1) {
				        $keywords .= $tag->name;
				    } else {
				    	$keywords .= ',' . $tag->name;
				    }
				    $i++;
				}			
			}
		}
	}

	if ( function_exists('get_field') && is_page() ) {
		// Page SEO
		if (get_field('page_title')) {
			$title = get_field('page_title');
		} else {
			$title = get_the_title() . $sep . get_bloginfo('name');
		}
		$description = get_field('page_description');
		$keywords = get_field('page_keywords');
	}

	if ( function_exists('get_field') && is_archive() ) {
		// Archive SEO
		$term = get_queried_object();

		if (get_field('archive_title', $term)) {
			$title = get_field('archive_title', $term) . $page;
		} else {
			$title = get_the_archive_title() . $page . $sep . get_bloginfo('name');
		}			
		$description = get_field('archive_description', $term);
		$keywords = get_field('archive_keywords', $term);	
	}	

	if ( function_exists('get_field') && is_tag() ) {
		// Tag SEO
		$term = get_queried_object();

		if (get_field('archive_title', $term)) {
			$title = get_field('archive_title', $term) . $page;
		} else {
			$title = get_the_archive_title() . $page . $sep . get_bloginfo('name');
		}

		if (get_field('archive_description', $term)) {
			$description = get_field('archive_description', $term);
		} else {
			$description =  '这是和' . get_the_archive_title() . '相关联的文章';
		}					

		if (get_field('archive_keywords', $term)) {
			$keywords = get_field('archive_keywords', $term);
		} else {
			$keywords = get_the_archive_title();
		}	
	}

	if ( function_exists('get_field') && is_author() ) {
		// Author SEO
		$term = get_queried_object();

		if (get_field('archive_title', $term)) {
			$title = get_field('archive_title', $term). $page;
		} else {
			$title = get_the_archive_title() . $page . $sep . get_bloginfo('name');
		}

		if (get_field('archive_description', $term)) {
			$description = get_field('archive_description', $term);
		} else {
			$description =  '这是作者' . get_the_archive_title() . '的所有文章';
		}					

		if (get_field('archive_keywords', $term)) {
			$keywords = get_field('archive_keywords', $term);
		} else {
			$keywords = get_the_archive_title();
		}	
	}

	if ( function_exists('get_field') && is_category() ) {
		// Category SEO
		$term = get_queried_object();

		if (get_field('archive_title', $term)) {
			$title = get_field('archive_title', $term) . $page;
		} else {
			$title = get_the_archive_title() . $page . $sep . get_bloginfo('name');
		}

		if (get_field('archive_description', $term)) {
			$description = get_field('archive_description', $term);
		} else {
			$description =  '这是' . get_the_archive_title() . '分类目录下的所有文章';
		}					

		if (get_field('archive_keywords', $term)) {
			$keywords = get_field('archive_keywords', $term);
		} else {
			$keywords = get_the_archive_title();
		}	
	}					

	if (is_home()) {
		// Home SEO
		if (get_option( 'custom_site_title')) {
			$title = get_option('custom_site_title') . $page;
		} else {
			$title = get_bloginfo('name') . $sep . get_bloginfo('description') . $page;
		}			
		$description = get_option( 'custom_site_description');
		$keywords = get_option( 'custom_site_keywords' );
	}

	if (is_search()) {
		// Search SEO
		$title = get_search_query() . '相关文章' . $page . $sep . get_bloginfo('name');
	}

	echo '<title>' . $title . '</title>';
	echo "\n";
	echo '<meta name="description" content="' . $description .'" />';
	echo "\n";
	echo '<meta name="keywords" content="' . $keywords . '" />';
	echo "\n";

};

/* Remove default title tag generated by WordPress */
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
