diff --git a/class/mtee-output.php b/class/mtee-output.php index e58b13d..418ab97 100644 --- a/class/mtee-output.php +++ b/class/mtee-output.php @@ -100,41 +100,139 @@ if (!class_exists('mtee_meta_output')) { } public function create_default_keywords_description(): array { - $keywords[] = $this->site_name; //サイトタイトル - if (!empty(get_bloginfo('description'))) { - $keywords[] = get_bloginfo('description'); //サイトタイトル - } - - $this->createKeywords($keywords); - $this->createDescription(); + $keywords = $this->create_default_keywords(); + $this->create_default_description(); $meta_data = array( - MTEE_NAME_KEYWORDS => implode(',', array_reverse($keywords)), + MTEE_NAME_KEYWORDS => implode(',', $keywords), MTEE_NAME_DESCRIPTION => $this->description, ); return $this->create_meta_tags($meta_data); } - private function createKeywords(&$keywords) { - if (is_category() || is_tag()) { - $keywords[] = single_cat_title('', false); + private function create_default_keywords() { + if (is_category()) { + $type = 'category'; + $title = single_cat_title('', false); + } elseif (is_tag()) { + $type = 'tag'; + $title = single_cat_title('', false); } elseif (is_tax()) { - $keywords[] = single_term_title('', false); + $type = 'tax'; + $title = single_term_title('', false); } elseif (is_post_type_archive()) { - $keywords[] = post_type_archive_title('', false); + $type = 'custom_post_archive'; + $title = post_type_archive_title('', false); + } elseif (is_single()) { + $type = 'post'; + $title = get_the_title(); + } elseif (is_page()) { + $type = 'page'; + $title = get_the_title(); + } + + $custom_keywords_temp = get_option('_mtee')['keywords_tmp']; + if (empty($custom_keywords_temp[$type])) { + return $this->get_keywords_data($type, $title); + } + + return $this->get_custom_template_keywords_data($custom_keywords_temp, $type, $title); + + } + + private function get_keywords_data($type, $keyword) { + $keywords = array(); + $keywords[] = $this->site_name; //サイトタイトル + if (!empty(get_bloginfo('description'))) { + $keywords[] = get_bloginfo('description'); //サイトタイトル + } + if ($type == 'post') { + $this->get_single_keywords_tax($keywords); } else { - $keywords[] = get_the_title(); + $keywords[] = $keyword; + } + return array_reverse($keywords); + } + + private function get_custom_template_keywords_data($custom_keywords_temp, $type, $title) { + $targets = explode(',', $custom_keywords_temp[$type]); + $keywords = array(); + foreach ($targets as $target) { + switch ($target) { + case '##title##': + $keywords[] = $title; + break; + case '##site_name##': + $keywords[] = $this->site_name; + break; + case '##site_description##': + $keywords[] = get_bloginfo('description'); + break; + case '##category##': + $this->get_custom_template_single_keywords($keywords, 'category'); + break; + case '##tag##': + $this->get_custom_template_single_keywords($keywords, 'post_tag'); + break; + case '##custom_tax##': + $this->get_custom_template_single_keywords($keywords, 'custom_tax'); + break; + } + } + return $keywords; + } + + private function get_custom_template_single_keywords(&$keywords, $type) { + global $post; + $taxonomies = $this->get_single_keyword_tax_data(); + if ($type == 'custom_tax') { + foreach ($taxonomies as $target) { + if ($target != 'post_format' && $target != 'category' && $target != 'post_tag') { + $this->get_custom_keywords_terms($keywords, $post->ID, $target); + } + } + } else { + foreach ($taxonomies as $target) { + if ($target == $type) { + $this->get_custom_keywords_terms($keywords, $post->ID, $target); + } + } } } - private function createDescription() { + private function get_single_keywords_tax(&$keywords) { + global $post; + $taxonomies = $this->get_single_keyword_tax_data(); + foreach ($taxonomies as $target) { + if ($target != 'post_format') { + $this->get_custom_keywords_terms($keywords, $post->ID, $target); + } + } + } + + private function get_single_keyword_tax_data(): array { + global $post; + return array_reverse(get_object_taxonomies(get_post_type($post->ID))); + } + + private function get_custom_keywords_terms(&$keywords, $id, $target) { + $terms = get_the_terms($id, $target); + if ($terms) { + foreach ($terms as $term) { + $keywords[] = $term->name; + } + } + } + + private function create_default_description() { $type = ''; $title = ''; $suffix_text = MTEE_META_DESC_ARCHIVE_BASE; if (is_home() || is_front_page()) { //ページ $this->description .= MTEE_META_DESC_TOP_BASE; + return; } elseif (is_page() || is_single()) { //ページ $type = 'page'; $title = get_the_title(); @@ -158,7 +256,7 @@ if (!class_exists('mtee_meta_output')) { if (isset($replacement['org'])) { $search = array('<###>', '<%%%>'); $description_noun = MTEE_META_DESC_BEFORE_BRACKETS . '<###>' . MTEE_META_DESC_AFTER_BRACKETS . '<%%%>'; - $this->description .= str_replace($search, $replacement, $description_noun); + $this->description .= str_replace($search, $replacement['org'], $description_noun); } } @@ -169,8 +267,8 @@ if (!class_exists('mtee_meta_output')) { $template = get_option('_mtee')['description_tmp']; if (isset($template[$type]) && !empty($template[$type])) { - $search = array('##title##', '##site_name##'); - $replacement = array($title, get_bloginfo('name')); + $search = array('##title##', '##site_name##', '##site_description##'); + $replacement = array($title, get_bloginfo('name'), get_bloginfo('description')); $this->description = str_replace($search, $replacement, $template[$type]); } else { return array('org' => array($title, $suffix_text)); diff --git a/css/mtee.css b/css/mtee.css index 779195a..658621c 100644 --- a/css/mtee.css +++ b/css/mtee.css @@ -63,19 +63,6 @@ h2 { margin: 0 } -.mtee-example { - width: 95%; - margin-bottom: 2em; - padding: 1em; - background: #f9f9f9; - border: 1px solid #666; - border-radius: 6px; -} - -.mtee-example h4 { - margin: 0 0 .5em; -} - .meta_noindex_nofollow_box { display: flex; margin-bottom: 2em; @@ -114,7 +101,8 @@ h2 { margin-left: 0; } -.mtee_description_tmp { +.mtee_description_tmp, +.mtee_keywords_tmp { width: 100%; } diff --git a/meta-tag-etc-extend.php b/meta-tag-etc-extend.php index ad5cf99..67894da 100644 --- a/meta-tag-etc-extend.php +++ b/meta-tag-etc-extend.php @@ -7,9 +7,6 @@ Version: 1.0 */ /* -Todo:meta keywords & description -・キーワードの雛形設定 - Todo:OGPタグ ・OGPタグを追加する <各OGP画像の保存> diff --git a/template/index.php b/template/index.php index f121bad..6de865a 100644 --- a/template/index.php +++ b/template/index.php @@ -76,18 +76,65 @@ $noindex_nofollow = $this->get_noindex_nofollow(); - +

meta description テンプレート設定

個別設定していない投稿やカテゴリー等は、このテンプレートを適用します。

@@ -125,7 +172,8 @@ $noindex_nofollow = $this->get_noindex_nofollow();