diff --git a/class/mtee.php b/class/mtee.php index 30a2aeb..adb49fd 100644 --- a/class/mtee.php +++ b/class/mtee.php @@ -89,5 +89,18 @@ if (!class_exists('MTEE')) { return 0; } } + + public function get_ogp_img($type, $subtype = '') { + if (empty($subtype)) { + $id = get_option('_mtee')[$type]['ogp_img']; + } else { + $id = get_option('_mtee')[$type][$subtype]['ogp_img']; + } + if (!empty($id)) { + return ''; + } + } + + } } diff --git a/class/mtee_meta_output_keydesc.php b/class/mtee_meta_output_keydesc.php index e775ba4..135ea5d 100644 --- a/class/mtee_meta_output_keydesc.php +++ b/class/mtee_meta_output_keydesc.php @@ -6,10 +6,15 @@ if (!defined('ABSPATH')) { if (!class_exists('mtee_meta_output_keydesc')) { + include MTEE_TRAIT_DIR . 'mtee_meta_desc_trait.php'; + class mtee_meta_output_keydesc { + use mtee_meta_desc_trait; + private $site_name; private $description; + private $metas = array(); public function __construct() { $this->site_name = get_bloginfo('name'); @@ -17,11 +22,12 @@ if (!class_exists('mtee_meta_output_keydesc')) { add_action('wp_head', array($this, 'post_output_add_metas')); } - //-------------------------------------------------------------------- - // meta keywords & meta description - //-------------------------------------------------------------------- public function post_output_add_metas() { - $metas = array(); + $this->create_post_output_add_metas(); + echo implode(PHP_EOL, $this->metas) . PHP_EOL; + } + + public function create_post_output_add_metas() { if (is_home() || is_front_page()) { $type = 'top_page'; } elseif (is_post_type_archive()) { @@ -36,12 +42,11 @@ if (!class_exists('mtee_meta_output_keydesc')) { $type = 'post_tag'; } if (isset($type)) { - $metas = $this->post_output_view_metas($type); + $this->metas = $this->post_output_view_metas($type); } - if (count($metas) == 0) { - $metas = $this->create_default_keywords_description(); + if (count($this->metas) == 0) { + $this->metas = $this->create_default_keywords_description(); } - echo implode(PHP_EOL, $metas) . PHP_EOL; } public function post_output_view_metas($type): array { @@ -51,81 +56,38 @@ if (!class_exists('mtee_meta_output_keydesc')) { } elseif (isset($targets['taxonomies']) && array_key_exists($type, $targets['taxonomies'])) { $type = 'custom_tax'; } - - $meta_data = $this->post_output_get_meta_data($type); + $meta_data = array( + MTEE_NAME_KEYWORDS => str_replace('、', ',', $this->post_output_get_meta_keywords($type)), + MTEE_NAME_DESCRIPTION => $this->post_output_get_meta_desc($type), + ); return $this->create_meta_tags($meta_data); } - public function post_output_get_meta_data($type): array { + public function post_output_get_meta_keywords($type) { global $post; if ($type == 'top_page') { $meta_key_words = get_option('_mtee')['top_page']['keywords']; - $meta_description = get_option('_mtee')['top_page']['description']; } elseif ($type == 'custom_post_archive') { $custom_post_name = get_post_type_object(get_post_type())->name; $meta_key_words = get_option('_mtee')['custom_post'][$custom_post_name]['keywords']; - $meta_description = get_option('_mtee')['custom_post'][$custom_post_name]['description']; } elseif ($type == 'custom_tax' || $type == 'category' || $type == 'post_tag') { $queried_object = get_queried_object(); $meta_key_words = get_term_meta($queried_object->term_id, MTEE_NAME_KEYWORDS, true); - $meta_description = get_term_meta($queried_object->term_id, MTEE_NAME_DESCRIPTION, true); } else { $meta_key_words = get_post_meta($post->ID, MTEE_NAME_KEYWORDS, true); - $meta_description = get_post_meta($post->ID, MTEE_NAME_DESCRIPTION, true); } - - return array( - MTEE_NAME_KEYWORDS => str_replace('、', ',', $meta_key_words), - MTEE_NAME_DESCRIPTION => $meta_description, - ); + return $meta_key_words; } public function create_default_keywords_description(): array { - $keywords = $this->create_default_keywords(); - $this->create_default_description(); + $this->description .= $this->create_default_description(); $meta_data = array( - MTEE_NAME_KEYWORDS => implode(',', $keywords), + MTEE_NAME_KEYWORDS => implode(',', $this->create_default_keywords()), MTEE_NAME_DESCRIPTION => $this->description, ); - return $this->create_meta_tags($meta_data); } - public 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(); - $suffix_text = MTEE_META_DESC_SINGLE_BASE; - } elseif (is_category()) { //カテゴリー - $type = 'category'; - $title = single_cat_title('', false); - } elseif (is_tag()) { //タグ - $type = 'tag'; - $title = single_tag_title('', false); - } elseif (is_tax()) { //タクソノミー - $type = 'tax'; - $title = single_term_title('', false); - } elseif (is_post_type_archive()) { //カスタム投稿のアーカイブ - $type = 'custom_post'; - $title = post_type_archive_title('', false); - } - - $replacement = $this->cnv_custom_template($type, $title, $suffix_text); - - if (isset($replacement['org'])) { - $search = array('<###>', '<%%%>'); - $description_noun = MTEE_META_DESC_BEFORE_BRACKETS . '<###>' . MTEE_META_DESC_AFTER_BRACKETS . '<%%%>'; - $this->description .= str_replace($search, $replacement['org'], $description_noun); - } - } - public function create_default_keywords(): array { $type = ''; $title = ''; @@ -253,21 +215,5 @@ if (!class_exists('mtee_meta_output_keydesc')) { return $metas; } - public function cnv_custom_template($type, $title, $suffix_text) { - if ($type == 'custom_post') { - return array('org' => array(post_type_archive_title('', false), $suffix_text)); - } - - $template = get_option('_mtee')['description_tmp']; - if (isset($template[$type]) && !empty($template[$type])) { - $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)); - } - } - - } } \ No newline at end of file diff --git a/class/mtee_meta_output_ogp.php b/class/mtee_meta_output_ogp.php index cff6e84..03054d2 100644 --- a/class/mtee_meta_output_ogp.php +++ b/class/mtee_meta_output_ogp.php @@ -6,11 +6,22 @@ if (!defined('ABSPATH')) { if (!class_exists('mtee_meta_output_ogp')) { - class mtee_meta_output_ogp { + + use mtee_meta_desc_trait; + + public $site_name; + public $ogp_type; + public $ogp_description; + public $ogp_title; + public $ogp_url; + public $ogp_img; + public function __construct() { + $this->site_name = get_bloginfo('name'); //GOPのプレフィックスを出力する add_filter('language_attributes', array($this, 'add_html_ogp_prefix')); + add_action('wp_head', array($this, 'create_ogp_tag')); } /** @@ -26,5 +37,204 @@ if (!class_exists('mtee_meta_output_ogp')) { private function create_ogp_prefix(): string { return ' prefix="og: https://ogp.me/ns#"'; } + + public function create_ogp_tag() { + $this->create_ogp_type(); + $this->create_ogp_title(); + $this->create_ogp_description(); + $this->create_ogp_image(); + $this->create_ogp_url(); + echo $this->set_ogp_tag(); + } + + + public function set_ogp_tag() { + $tags = '' . PHP_EOL; + $tags .= '' . PHP_EOL; + $tags .= '' . PHP_EOL; + $tags .= '' . PHP_EOL; +// $tags .= '' . PHP_EOL; + $tags .= '' . PHP_EOL; +// $tags .= '' . PHP_EOL; +// $tags .= '' . PHP_EOL; + $tags .= '' . "\n"; + return $tags; + } + + public function get_ogp_locale(): string { + $locale = get_locale(); + if (strlen($locale) == 2) { + if ($locale === "ja") + return "ja_JP"; + else + return strtolower($locale) . '_' . strtoupper($locale); + } + } + + public function create_ogp_type() { + if (is_home() || is_front_page()) { + $this->ogp_type = 'website'; + } else { + $this->ogp_type = 'article'; + } + } + + public function create_ogp_title() { + if (is_category()) { + $this->ogp_title = single_cat_title('', false); + } elseif (is_tag()) { + $this->ogp_title = single_cat_title('', false); + } elseif (is_tax()) { + $this->ogp_title = single_term_title('', false); + } elseif (is_post_type_archive()) { + $this->ogp_title = post_type_archive_title('', false); + } elseif (is_single() || is_page()) { + $this->ogp_title = get_the_title(); + } elseif (is_home() || is_front_page()) { + $this->ogp_title = $this->site_name; + } + } + + public function create_ogp_description() { + $this->ogp_description = $this->site_name . MTEE_META_DESC_PARTICLE; + $this->ogp_description .= $this->set_ogp_description(); + } + + public function set_ogp_description() { + $type = ''; + $ogp_desc = ''; + + if (is_home() || is_front_page()) { + $type = 'top_page'; + } elseif (is_post_type_archive()) { + $type = 'custom_post_archive'; + } elseif (is_tax()) { + $type = get_queried_object()->taxonomy; + } elseif (is_page() || is_single()) { + $type = get_post_type(); + } elseif (is_category()) { + $type = 'category'; + } elseif (is_tag()) { + $type = 'post_tag'; + } + + $targets = get_option('_mtee'); + if (isset($targets['custom_posts']) && array_key_exists($type, $targets['custom_posts'])) { + $type = 'custom_post'; + } elseif (isset($targets['taxonomies']) && array_key_exists($type, $targets['taxonomies'])) { + $type = 'custom_tax'; + } + + if (!empty($type)) { + $ogp_desc = $this->post_output_get_meta_desc($type); + } + + if (empty($ogp_desc)) { + $ogp_desc = $this->create_default_description(); + } + return $ogp_desc; + } + + + public function create_ogp_url() { + global $post; + if (is_category()) { + $this->ogp_url = get_category_link(get_query_var('cat')); + } elseif (is_tag()) { + $this->ogp_url = get_tag_link(get_query_var('tag_id')); + } elseif (is_tax()) { + $this->ogp_url = get_term_link((int)get_queried_object()->term_id); + } elseif (is_post_type_archive()) { + $custom_post_name = get_post_type_object(get_post_type())->name; + $this->ogp_url = get_post_type_archive_link($custom_post_name); + } elseif (is_single() || is_page()) { + $this->ogp_url = get_permalink($post->ID); + global $page, $paged; + if ($paged >= 2 || $page >= 2) { + $this->ogp_url .= 'page/' . max($paged, $page) . '/'; + } + } elseif (is_home() || is_front_page()) { + $this->ogp_url = get_bloginfo('url'); + } + } + + public function create_ogp_image() { + global $post; + $id = ''; + $type = ''; + if (is_post_type_archive()) { + $type = 'custom_post_archive'; + } elseif (is_category()) { + $id = get_query_var('cat'); + $type = 'cat'; + } elseif (is_tag()) { + $id = get_query_var('tag_id'); + $type = 'tag'; + } elseif (is_tax()) { + $id = get_queried_object()->term_id; + $type = 'tax'; + } elseif (is_home() || is_front_page()) { + $type = 'top_page'; + } elseif (is_page() || is_single()) { + $id = $post->ID; + $type = 'posts'; + } + return $this->get_ogp_tag_img($id, $type); + } + + public function get_ogp_tag_img($id, $type) { + if ($type == 'top_page') { + $canonical = $this->set_top_page_ogp_img(); + } elseif ($type == 'custom_post_archive') { + $canonical = $this->set_custom_post_archive_ogp_img(); + } elseif ($type == 'posts') { + $canonical = $this->set_post_ogp_img($id); + } elseif ($type == 'cat') { + $canonical = $this->set_category_ogp_img($id); + } elseif ($type == 'tag') { + $canonical = $this->set_tag_ogp_img($id); + } elseif ($type == 'tax') { + $canonical = $this->set_tax_ogp_img($id); + } + } + + public function set_top_page_ogp_img() { + $img = get_option('_mtee')['top_page']['ogp_img']; + if (empty($img)) { + $img = ''; + } + return $img; + } + + public function set_custom_post_archive_ogp_img() { + $custom_post_name = get_post_type_object(get_post_type())->name; + $img = get_option('_mtee')['custom_post'][$custom_post_name]['ogp_img']; + if (empty($img)) { + $img = ''; + } + return $img; + } + + public function set_post_ogp_img($id) { + $img = get_post_meta($id, MTEE_OGP_IMG, true); + + } + + public function set_category_ogp_img($id) { + $img = get_term_meta($id, MTEE_OGP_IMG, true); + + } + + public function set_tag_ogp_img($id) { + $img = get_term_meta($id, MTEE_OGP_IMG, true); + + } + + public function set_tax_ogp_img($id) { + $img = get_term_meta($id, MTEE_OGP_IMG, true); + + } + + } } \ No newline at end of file diff --git a/class/mtee_ogp_post_setting.php b/class/mtee_ogp_post_setting.php index 440db3c..cc70936 100644 --- a/class/mtee_ogp_post_setting.php +++ b/class/mtee_ogp_post_setting.php @@ -67,7 +67,7 @@ placeholder="' . $this->set_default_ogp_desc() . '"/> -
' . $this->set_ogp_thumb($post->ID) . '
+
' . $this->set_ogp_thumb($post->ID) . '
@@ -94,7 +94,7 @@ class="tax-meta-field" /> -
' . $this->set_ogp_thumb($tag->term_id) . '
+
' . $this->set_ogp_thumb($tag->term_id) . '
diff --git a/config.php b/config.php index 2cc6bbc..33a35e6 100644 --- a/config.php +++ b/config.php @@ -1,6 +1,7 @@ '); + }); + }); + + set_media[media_index].open(); + + }); + + /* クリアボタンを押した時の処理 */ + clear_ogp.click(function () { + let clear_index = $('.clear_ogp_img').index(this); + let thumb = $('.ogp_media:eq(' + clear_index + ')'); + $('.' + ogp_img_name + ':eq(' + clear_index + ')').val(''); + thumb.empty(); + }); + + function build_media() { + return wp.media({ title: "画像を選択", /* ライブラリの一覧は画像のみにする */ library: { @@ -46,30 +78,7 @@ jQuery(function ($) { /* 選択できる画像は 1 つだけにする */ multiple: false }); - - custom_uploader.on("select", function () { - let images = custom_uploader.state().get("selection"); - /* file の中に選択された画像の各種情報が入っている */ - images.each(function (file) { - /* テキストフォームと表示されたサムネイル画像があればクリア */ - $('.' + ogp_img_name).val(''); - thumb.empty(); - /* テキストフォームに画像の ID を表示 */ - $('.' + ogp_img_name).val(file.id); - /* プレビュー用に選択されたサムネイル画像を表示 */ - thumb.append(''); - }); - }); - - custom_uploader.open(); - - }); - - /* クリアボタンを押した時の処理 */ - clear_ogp.click(function () { - $('.' + ogp_img_name).val(''); - thumb.empty(); - }); + } } }); \ No newline at end of file diff --git a/meta-tag-etc-extend.php b/meta-tag-etc-extend.php index 5b15c67..e544f29 100644 --- a/meta-tag-etc-extend.php +++ b/meta-tag-etc-extend.php @@ -46,6 +46,7 @@ if ($mtee->is_enable('canonical_setting')) { // enabled my OGP tag if ($mtee->is_enable('ogp_setting')) { + include MTEE_TRAIT_DIR . 'mtee_meta_desc_trait.php'; require_once MTEE_CLASS_DIR . 'mtee_ogp_tax_setting.php'; require_once MTEE_CLASS_DIR . 'mtee_ogp_post_setting.php'; require_once MTEE_CLASS_DIR . 'mtee_meta_output_ogp.php'; diff --git a/template/index.php b/template/index.php index b8753a6..2a46594 100644 --- a/template/index.php +++ b/template/index.php @@ -22,12 +22,19 @@ $rss_disabled = $this->get_key_setting('rss_disabled');
-

キーワード・ディスクリプション

+

メタキーワード・ディスクリプション/OGP

+

OGP出力

+

meta keywords テンプレート設定 @@ -154,15 +161,6 @@ $rss_disabled = $this->get_key_setting('rss_disabled'); value="1">出力しない

-
-

OGPタグ出力

- -

Canonical URL 出力

diff --git a/trait/mtee_meta_desc_trait.php b/trait/mtee_meta_desc_trait.php new file mode 100644 index 0000000..18826db --- /dev/null +++ b/trait/mtee_meta_desc_trait.php @@ -0,0 +1,78 @@ +name; + $meta_description = get_option('_mtee')['custom_post'][$custom_post_name]['description']; + } elseif ($type == 'custom_tax' || $type == 'category' || $type == 'post_tag') { + $queried_object = get_queried_object(); + $meta_description = get_term_meta($queried_object->term_id, MTEE_NAME_DESCRIPTION, true); + } else { + $meta_description = get_post_meta($post->ID, MTEE_NAME_DESCRIPTION, true); + } + return $meta_description; + } + + public function create_default_description() { + $type = ''; + $title = ''; + $suffix_text = MTEE_META_DESC_ARCHIVE_BASE; + + if (is_home() || is_front_page()) { //ページ + return MTEE_META_DESC_TOP_BASE; + } elseif (is_page() || is_single()) { //ページ + $type = 'page'; + $title = get_the_title(); + $suffix_text = MTEE_META_DESC_SINGLE_BASE; + } elseif (is_category()) { //カテゴリー + $type = 'category'; + $title = single_cat_title('', false); + } elseif (is_tag()) { //タグ + $type = 'tag'; + $title = single_tag_title('', false); + } elseif (is_tax()) { //タクソノミー + $type = 'tax'; + $title = single_term_title('', false); + } elseif (is_post_type_archive()) { //カスタム投稿のアーカイブ + $type = 'custom_post'; + $title = post_type_archive_title('', false); + } + + $replacement = $this->cnv_custom_template($type, $title, $suffix_text); + + if (isset($replacement['org'])) { + $search = array('<###>', '<%%%>'); + $description_noun = MTEE_META_DESC_BEFORE_BRACKETS . '<###>' . MTEE_META_DESC_AFTER_BRACKETS . '<%%%>'; + return str_replace($search, $replacement['org'], $description_noun); + } else { + return $replacement; + } + } + + public function cnv_custom_template($type, $title, $suffix_text) { + if ($type == 'custom_post') { + return array('org' => array(post_type_archive_title('', false), $suffix_text)); + } + $template = get_option('_mtee')['description_tmp']; + if (isset($template[$type]) && !empty($template[$type])) { + $search = array('##title##', '##site_name##', '##site_description##'); + $replacement = array($title, get_bloginfo('name'), get_bloginfo('description')); + return str_replace($search, $replacement, $template[$type]); + } else { + return array('org' => array($title, $suffix_text)); + } + } + + } + +} \ No newline at end of file