From 21ccef70f1440df19ee4668f860047162baf6d33 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 28 May 2021 21:02:28 +0900 Subject: [PATCH] =?UTF-8?q?WP=20PLUGIN=20MTEE(Meta=20Tag=20etc=20Extend)?= =?UTF-8?q?=20=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・各項目のOFF/ONに対応して設定/HTML出力するようファイルを整理 --- class/mtee-canonical-setting.php | 1 - .../mtee-output-canonical.php | 43 +++- .../mtee-output-noindexnofollow.php | 44 +++- class/mtee-output.php | 207 ------------------ .../mtee_meta_output_keydesc.php | 125 ++++++++++- meta-tag-etc-extend.php | 31 ++- template/index.php | 36 +-- 7 files changed, 248 insertions(+), 239 deletions(-) rename trait/output_canonical.php => class/mtee-output-canonical.php (72%) rename trait/output_noindexnofollow_trait.php => class/mtee-output-noindexnofollow.php (64%) delete mode 100644 class/mtee-output.php rename trait/output_keyword_trait.php => class/mtee_meta_output_keydesc.php (52%) diff --git a/class/mtee-canonical-setting.php b/class/mtee-canonical-setting.php index dfd9391..6341906 100644 --- a/class/mtee-canonical-setting.php +++ b/class/mtee-canonical-setting.php @@ -12,7 +12,6 @@ if (!class_exists('mtee_canonical_setting')) { class mtee_canonical_setting { private $options; - private $tax_place_holder; public function __construct($options) { $this->options = $options; diff --git a/trait/output_canonical.php b/class/mtee-output-canonical.php similarity index 72% rename from trait/output_canonical.php rename to class/mtee-output-canonical.php index 8207140..34d104a 100644 --- a/trait/output_canonical.php +++ b/class/mtee-output-canonical.php @@ -1,10 +1,47 @@ term_id; + $type = 'tax'; + } elseif (is_home() || is_front_page()) { + $type = 'top_page'; + } elseif (is_page() || is_single()) { + $id = $post->ID; + $type = 'posts'; + } + + echo $this->get_canonical_data($id, $type); + + } + public function get_canonical_data($id, $type) { if (empty($type)) { return; @@ -82,6 +119,6 @@ if (!trait_exists('output_canonical')) { } return $canonical; } - } + } } \ No newline at end of file diff --git a/trait/output_noindexnofollow_trait.php b/class/mtee-output-noindexnofollow.php similarity index 64% rename from trait/output_noindexnofollow_trait.php rename to class/mtee-output-noindexnofollow.php index 760b750..71e955d 100644 --- a/trait/output_noindexnofollow_trait.php +++ b/class/mtee-output-noindexnofollow.php @@ -1,10 +1,49 @@ term_id; + $type = 'tax'; + } elseif (is_home() || is_front_page()) { + $type = 'top_page'; + } elseif (is_page() || is_single()) { + $id = $post->ID; + $type = 'posts'; + } + + echo $this->get_noindex_nofollow_data($id, $type); + + } public function get_noindex_nofollow_data($id, $type) { if (empty($type)) { @@ -52,5 +91,4 @@ if (!trait_exists('output_noindexnofollow_trait')) { } } - } \ No newline at end of file diff --git a/class/mtee-output.php b/class/mtee-output.php deleted file mode 100644 index 6da575a..0000000 --- a/class/mtee-output.php +++ /dev/null @@ -1,207 +0,0 @@ -site_name = get_bloginfo('name'); - $this->description = $this->site_name . MTEE_META_DESC_PARTICLE; - add_action('wp_head', array($this, 'post_output_add_metas')); - add_action('wp_head', array($this, 'set_noindex_nofollow')); - add_action('wp_head', array($this, 'set_canonical_url')); - } - - //-------------------------------------------------------------------- - // meta keywords & meta description - //-------------------------------------------------------------------- - public function post_output_add_metas() { - $metas = array(); - 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'; - } - if (isset($type)) { - $metas = $this->post_output_view_metas($type); - } - if (count($metas) == 0) { - $metas = $this->create_default_keywords_description(); - } - echo implode(PHP_EOL, $metas) . PHP_EOL; - } - - public function post_output_view_metas($type): array { - $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'; - } - - $meta_data = $this->post_output_get_meta_data($type); - return $this->create_meta_tags($meta_data); - } - - public function post_output_get_meta_data($type): array { - 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, - ); - } - - public function create_default_keywords_description(): array { - $keywords = $this->create_default_keywords(); - $this->create_default_description(); - $meta_data = array( - MTEE_NAME_KEYWORDS => implode(',', $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); - } - } - - - //-------------------------------------------------------------------- - // noindex, nofollow - //-------------------------------------------------------------------- - public function set_noindex_nofollow() { - - global $post; - $type = ''; - $id = null; - if (is_post_type_archive()) { - $type = 'custom_post_archive'; - } elseif (is_category()) { - $id = get_query_var('cat'); - $type = 'tax'; - } elseif (is_tag()) { - $id = get_query_var('tag_id'); - $type = 'tax'; - } 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'; - } - - echo $this->get_noindex_nofollow_data($id, $type); - - } - - //-------------------------------------------------------------------- - // canonical - //-------------------------------------------------------------------- - public function set_canonical_url() { - global $post; - - $type = ''; - $id = null; - 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'; - } - - echo $this->get_canonical_data($id, $type); - - } - - } -} \ No newline at end of file diff --git a/trait/output_keyword_trait.php b/class/mtee_meta_output_keydesc.php similarity index 52% rename from trait/output_keyword_trait.php rename to class/mtee_meta_output_keydesc.php index c54d7ba..1a7bbd8 100644 --- a/trait/output_keyword_trait.php +++ b/class/mtee_meta_output_keydesc.php @@ -1,10 +1,130 @@ site_name = get_bloginfo('name'); + $this->description = $this->site_name . MTEE_META_DESC_PARTICLE; + add_action('wp_head', array($this, 'post_output_add_metas')); + } + + //-------------------------------------------------------------------- + // meta keywords & meta description + //-------------------------------------------------------------------- + public function post_output_add_metas() { + $metas = array(); + 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'; + } + if (isset($type)) { + $metas = $this->post_output_view_metas($type); + } + if (count($metas) == 0) { + $metas = $this->create_default_keywords_description(); + } + echo implode(PHP_EOL, $metas) . PHP_EOL; + } + + public function post_output_view_metas($type): array { + $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'; + } + + $meta_data = $this->post_output_get_meta_data($type); + return $this->create_meta_tags($meta_data); + } + + public function post_output_get_meta_data($type): array { + 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, + ); + } + + public function create_default_keywords_description(): array { + $keywords = $this->create_default_keywords(); + $this->create_default_description(); + $meta_data = array( + MTEE_NAME_KEYWORDS => implode(',', $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() { $type = ''; @@ -148,5 +268,6 @@ if (!trait_exists('output_keyword_trait')) { } } + } } \ No newline at end of file diff --git a/meta-tag-etc-extend.php b/meta-tag-etc-extend.php index 74bff28..51cbf3e 100644 --- a/meta-tag-etc-extend.php +++ b/meta-tag-etc-extend.php @@ -8,21 +8,17 @@ Version: 1.0 /* Todo:OGPタグ -・canonical設定を追加する ・OGPタグを追加する <各OGP画像の保存> ・投稿のOGP用画像 => post_meta ・各タクソノミーのOGP画像 => term_meta ・トップ、カスタム投稿アーカイブ => wp_options - */ include_once 'config.php'; include_once MTEE_CLASS_DIR . 'mtee.php'; -/** - * CSSを設定 - */ +//admin css/js setting add_action('admin_enqueue_scripts', function () { $plugin_url = plugin_dir_url(__FILE__); wp_enqueue_style('mtee_style', $plugin_url . 'css/mtee.css'); @@ -32,30 +28,45 @@ add_action('admin_enqueue_scripts', function () { $mtee = new MTEE; $options = $mtee->get_options(); -// meta keywords, description設定有効の場合は、各フォームや出力用のクラスを有効にする +// enabled my meta keywords, description if ($mtee->is_enable('enabled')) { require_once MTEE_CLASS_DIR . 'mtee-tax-setting.php'; require_once MTEE_CLASS_DIR . 'mtee-post-setting.php'; + require_once MTEE_CLASS_DIR . 'mtee_meta_output_keydesc.php'; new mtee_tax_setting($options); new mtee_post_setting($options); + new mtee_meta_output_keydesc; } -// canonical設定有効の場合は、フォームを有効にし既存のcanonicalを削除・表示 +// enabled my canonical. caution : default canonical delete if ($mtee->is_enable('canonical_setting')) { remove_action('wp_head', 'rel_canonical'); require_once MTEE_CLASS_DIR . 'mtee-canonical-setting.php'; + require_once MTEE_CLASS_DIR . 'mtee-output-canonical.php'; new mtee_canonical_setting($options); + new mtee_meta_output_canonical(); } -if ($mtee->is_disable('noindex_nofollow')) { +// enabled my OGP tag +if ($mtee->is_enable('ogp_setting')) { + require_once MTEE_CLASS_DIR . 'mtee-ogp-setting.php'; + require_once MTEE_CLASS_DIR . 'mtee-output-ogp.php'; + new mtee_ogp_setting(); + new mtee_meta_output_ogp(); +} + +//enabled my noindex, nofollow +if ($mtee->is_enable('noindex_nofollow')) { require_once MTEE_CLASS_DIR . 'mtee-noindexnofollow-setting.php'; + require_once MTEE_CLASS_DIR . 'mtee-output-noindexnofollow.php'; new mtee_noindexnofolow_setting($options); + new mtee_meta_output_noindexnofollow(); } +//disabled WP version, WP JS/CSS version if ($mtee->is_disable('asset_ver_disabled') || $mtee->is_disable('asset_ver_disabled')) { require_once MTEE_CLASS_DIR . 'mtee-version-setting.php'; new mtee_version_setting($options); } -require_once MTEE_CLASS_DIR . 'mtee-output.php'; -new mtee_meta_output; + diff --git a/template/index.php b/template/index.php index a7de63c..2cde990 100644 --- a/template/index.php +++ b/template/index.php @@ -2,10 +2,11 @@ $register_targets = MTEE::register_target(); $opt = get_option('_mtee'); $enabled = $this->get_key_setting('enabled'); +$noindex_nofollow = $this->get_key_setting('noindex_nofollow'); +$ogp_setting = $this->get_key_setting('ogp_setting'); +$canonical_setting = $this->get_key_setting('canonical_setting'); $wp_ver_disabled = $this->get_key_setting('wp_ver_disabled'); $asset_ver_disabled = $this->get_key_setting('asset_ver_disabled'); -$noindex_nofollow = $this->get_key_setting('noindex_nofollow'); -$canonical_setting = $this->get_key_setting('canonical_setting'); ?>

meta keyword, description / バージョン情報 設定

@@ -193,6 +194,25 @@ $canonical_setting = $this->get_key_setting('canonical_setting'); value="1">有効
+ +
+

OGPタグ出力

+ +
+
+

Canonical URL 出力

+ +

WordPressバージョン情報

-

CSS / JSバージョン情報

+

CSS/JSバージョン情報

-
-

Canonical URL 設定

- -
-