From 8775a619c7d887cdda6a2c36f5938c13a6bd15ef Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 26 May 2021 20:20:13 +0900 Subject: [PATCH 1/3] =?UTF-8?q?WP=20PLUGIN=20MTEE(Meta=20Tag=20etc=20Exten?= =?UTF-8?q?d)=20canonical=E8=A8=AD=E5=AE=9A=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・トップ、カスタム分類アーカイブ、ページ、投稿、カスタム投稿のcanonical設定の追加 ・メソッドを整理 --- class/mtee-canonical-setting.php | 75 +++++++++++++++++++++++++++ class/mtee-output.php | 69 ++++++++++++++++++++++++- class/mtee.php | 62 +++-------------------- config.php | 1 + css/mtee.css | 7 ++- meta-tag-etc-extend.php | 21 +++++--- template/index.php | 87 +++++++++++++++++++++++++++----- 7 files changed, 248 insertions(+), 74 deletions(-) create mode 100644 class/mtee-canonical-setting.php diff --git a/class/mtee-canonical-setting.php b/class/mtee-canonical-setting.php new file mode 100644 index 0000000..282e814 --- /dev/null +++ b/class/mtee-canonical-setting.php @@ -0,0 +1,75 @@ +options = $options; + if ($this->options['enabled'] == 1) { + add_action('admin_menu', array($this, 'add_meta_fields')); + add_action('save_post', array($this, 'save_meta_fields')); + } + } + + public function add_meta_fields() { + //add_meta_box(表示される入力ボックスのHTMLのID, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法) + $default_built_ins = array( + 'posts' => array('page', 'post',), + ); + + foreach ($default_built_ins['posts'] as $built_in) { + add_meta_box( + 'canonical_setting', + 'Canonical設定', + array($this, 'insert_meta_fields'), + $built_in, + 'normal' + ); + } + + if (isset($this->options['custom_posts'])) { + foreach ($this->options['custom_posts'] as $custom_post => $post_chk) { + if ($post_chk == 1) { + add_meta_box( + 'canonical_setting', + 'Canonical設定', + array($this, 'insert_meta_fields'), + $custom_post, 'normal' + ); + } + } + } + } + + // カスタムフィールドの入力エリア + public function insert_meta_fields() { + global $post; + echo '
+ +
+ +
+
'; + } + + // カスタムフィールドの値を保存 + public function save_meta_fields($post_id) { + if (!empty($_POST[MTEE_CANONICAL_URL])) { //meta_keywordsが入力されている場合 + update_post_meta($post_id, MTEE_CANONICAL_URL, $_POST[MTEE_CANONICAL_URL]); //値を保存 + } else { //未入力の場合は値を削除 + delete_post_meta($post_id, MTEE_CANONICAL_URL); + } + } + } + +} \ No newline at end of file diff --git a/class/mtee-output.php b/class/mtee-output.php index 418ab97..99dbdb1 100644 --- a/class/mtee-output.php +++ b/class/mtee-output.php @@ -23,6 +23,8 @@ if (!class_exists('mtee_meta_output')) { $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')); + } //-------------------------------------------------------------------- @@ -102,7 +104,6 @@ if (!class_exists('mtee_meta_output')) { 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, @@ -112,6 +113,8 @@ if (!class_exists('mtee_meta_output')) { } private function create_default_keywords() { + $type = ''; + $title = ''; if (is_category()) { $type = 'category'; $title = single_cat_title('', false); @@ -349,5 +352,69 @@ if (!class_exists('mtee_meta_output')) { ); } + //-------------------------------------------------------------------- + // 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 = '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_canonical_data($id, $type); + + } + + private function get_canonical_data($id, $type) { + if (empty($type)) { + return; + } + $canonical = ''; + + if ($type == 'top_page') { + $canonical = get_option('_mtee')['top_page']['canonical']; + } elseif ($type == 'custom_post_archive') { + $custom_post_name = get_post_type_object(get_post_type())->name; + $canonical = get_option('_mtee')['custom_post'][$custom_post_name]['canonical']; + } elseif ($type == 'posts') { + $canonical = get_post_meta($id, MTEE_CANONICAL_URL, true); + if (empty($canonical)) { + $canonical = get_permalink(); + global $page, $paged; + if ($paged >= 2 || $page >= 2) { + $canonical .= 'page/' . max($paged, $page) . '/'; + } + } + } + +// $noindex = get_post_meta($id, MTEE_NAME_NOINDEX, true); +// $nofollow = get_post_meta($id, MTEE_NAME_NOFOLLOW, true); +// } elseif ($type == 'tax') { +// $noindex = get_term_meta($id, MTEE_NAME_NOINDEX, true); +// $nofollow = get_term_meta($id, MTEE_NAME_NOFOLLOW, true); +// } + + if ($canonical) { + return '' . PHP_EOL; + } + } + } } \ No newline at end of file diff --git a/class/mtee.php b/class/mtee.php index 023b9f8..c9352be 100644 --- a/class/mtee.php +++ b/class/mtee.php @@ -64,26 +64,17 @@ if (!class_exists('MTEE')) { return get_option('_mtee'); } - public function is_enable(): bool { - if (isset($this->get_options()['enabled']) - && $this->get_options()['enabled'] == 1) { + public function is_enable($type): bool { + if (isset($this->get_options()[$type]) + && $this->get_options()[$type] == 1) { return true; } return false; } - public function is_wp_ver_disable(): bool { - if (isset($this->get_options()['wp_ver_disabled']) - && $this->get_options()['wp_ver_disabled'] == 1) { - return true; - } - - return false; - } - - public function is_asset_ver_disable(): bool { - if (isset($this->get_options()['asset_ver_disabled']) + public function is_disable($type): bool { + if (isset($this->get_options()[$type]) && $this->get_options()['asset_ver_disabled'] == 1) { return true; } @@ -91,50 +82,13 @@ if (!class_exists('MTEE')) { return false; } - public function is_noindex_nofollow_disable(): bool { - if (isset($this->get_options()['noindex_nofollow']) - && $this->get_options()['noindex_nofollow'] == '1') { - return true; - } - - return false; - } - - public function get_key_desc_status(): int { - if (isset($this->get_options()['enabled']) - && $this->get_options()['enabled'] == '1') { + public function get_key_setting($type): int { + if (isset($this->get_options()[$type]) + && $this->get_options()[$type] == '1') { return 1; } else { return 0; } } - - public function get_wp_ver_disabled(): int { - if (isset($this->get_options()['wp_ver_disabled']) - && $this->get_options()['wp_ver_disabled'] == '1') { - return 1; - } else { - return 0; - } - } - - public function get_asset_ver_disabled(): int { - if (isset($this->get_options()['asset_ver_disabled']) - && $this->get_options()['asset_ver_disabled'] == '1') { - return 1; - } else { - return 0; - } - } - - public function get_noindex_nofollow(): int { - if (isset($this->get_options()['noindex_nofollow']) - && $this->get_options()['noindex_nofollow'] == '1') { - return 1; - } else { - return 0; - } - } - } } diff --git a/config.php b/config.php index 029ff39..318a558 100644 --- a/config.php +++ b/config.php @@ -6,6 +6,7 @@ const MTEE_NAME_KEYWORDS = 'mtee_meta_keywords'; const MTEE_NAME_DESCRIPTION = 'mtee_meta_description'; const MTEE_NAME_NOINDEX = '_mtee_robots_noindex'; const MTEE_NAME_NOFOLLOW = '_mtee_robots_nofollow'; +const MTEE_CANONICAL_URL = 'mtee_canonical_url'; const MTEE_META_DESC_TOP_BASE = 'トップページです。'; const MTEE_META_DESC_SINGLE_BASE = 'ページです。'; diff --git a/css/mtee.css b/css/mtee.css index 658621c..64058fe 100644 --- a/css/mtee.css +++ b/css/mtee.css @@ -40,7 +40,6 @@ h2 { display: block; } - .mtee_box_border { border-top: 1px solid #ccc; } @@ -111,6 +110,12 @@ h2 { font-style: italic; } +.disabled_status { + display: block; + font-size: .8em; + color: #f35; +} + @media screen and (max-width: 768px) { .mtee_description_tmp_list { display: block; diff --git a/meta-tag-etc-extend.php b/meta-tag-etc-extend.php index 67894da..74bff28 100644 --- a/meta-tag-etc-extend.php +++ b/meta-tag-etc-extend.php @@ -8,6 +8,7 @@ Version: 1.0 /* Todo:OGPタグ +・canonical設定を追加する ・OGPタグを追加する <各OGP画像の保存> ・投稿のOGP用画像 => post_meta @@ -31,22 +32,30 @@ add_action('admin_enqueue_scripts', function () { $mtee = new MTEE; $options = $mtee->get_options(); -// 設定が有効の場合は、各フォームや出力用のクラスを有効にする -if ($mtee->is_enable()) { +// 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-output.php'; new mtee_tax_setting($options); new mtee_post_setting($options); - new mtee_meta_output; } -if ($mtee->is_noindex_nofollow_disable()) { +// canonical設定有効の場合は、フォームを有効にし既存のcanonicalを削除・表示 +if ($mtee->is_enable('canonical_setting')) { + remove_action('wp_head', 'rel_canonical'); + require_once MTEE_CLASS_DIR . 'mtee-canonical-setting.php'; + new mtee_canonical_setting($options); +} + +if ($mtee->is_disable('noindex_nofollow')) { require_once MTEE_CLASS_DIR . 'mtee-noindexnofollow-setting.php'; new mtee_noindexnofolow_setting($options); } -if ($mtee->is_wp_ver_disable() || $mtee->is_asset_ver_disable()) { +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 6de865a..a7de63c 100644 --- a/template/index.php +++ b/template/index.php @@ -1,10 +1,11 @@ get_key_desc_status(); -$wp_ver_disabled = $this->get_wp_ver_disabled(); -$asset_ver_disabled = $this->get_asset_ver_disabled(); -$noindex_nofollow = $this->get_noindex_nofollow(); +$enabled = $this->get_key_setting('enabled'); +$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 / バージョン情報 設定

@@ -77,7 +78,11 @@ $noindex_nofollow = $this->get_noindex_nofollow();
-

meta keywords テンプレート設定

+

meta keywords テンプレート設定 + is_enable('enabled')): ?> + ※現在無効です + +

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

get_noindex_nofollow();
-

meta description テンプレート設定

+

meta description テンプレート設定 + is_enable('enabled')): ?> + ※現在無効です +

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

get_noindex_nofollow(); value="1">削除
+
+

Canonical URL 設定

+ +
+

@@ -212,25 +230,38 @@ $noindex_nofollow = $this->get_noindex_nofollow(); $description = $opt['top_page']['description'] ?? ''; $noindex = $opt['top_page']['noindex'] ?? '0'; $nofollow = $opt['top_page']['nofollow'] ?? '0'; + $canonical_url = $opt['top_page']['canonical'] ?? ''; ?>
- + - + - + + + + +
meta keywordsmeta keywords + is_enable('enabled')): ?> + ※現在無効です + +
キーワードはカンマ(,)区切りで入力してください
meta descriptionmeta description + is_enable('enabled')): ?> + ※現在無効です + +
noindex nofollownoindex nofollow + is_enable('noindex_nofollow')): ?> + ※現在無効です + +
Canonical URL + is_enable('canonical_setting')): ?> + ※現在無効です + +
0): ?> @@ -260,6 +302,7 @@ $noindex_nofollow = $this->get_noindex_nofollow(); $description = $opt['custom_post'][$custom_post]['description'] ?? ''; $noindex = $opt['custom_post'][$custom_post]['noindex'] ?? '0'; $nofollow = $opt['custom_post'][$custom_post]['nofollow'] ?? '0'; + $canonical_url = $opt['custom_post'][$custom_post]['canonical'] ?? '0'; ?>
@@ -269,7 +312,10 @@ $noindex_nofollow = $this->get_noindex_nofollow(); - + - + - + + + + +
meta keywordsmeta keywords + is_enable('enabled')): ?> + ※現在無効です + @@ -277,7 +323,10 @@ $noindex_nofollow = $this->get_noindex_nofollow();
meta descriptionmeta description + is_enable('enabled')): ?> + ※現在無効です + get_noindex_nofollow();
noindex nofollownoindex nofollow + is_enable('noindex_nofollow')): ?> + ※現在無効です +
Canonical URL + is_enable('canonical_setting')): ?> + ※現在無効です + +
From 0870f128259283076a92c9a46d918d56c53763fe Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 27 May 2021 15:00:42 +0900 Subject: [PATCH 2/3] =?UTF-8?q?WP=20PLUGIN=20MTEE(Meta=20Tag=20etc=20Exten?= =?UTF-8?q?d)=20=E3=82=AB=E3=82=B9=E3=82=BF=E3=83=A0=E5=88=86=E9=A1=9Enoin?= =?UTF-8?q?dex=E3=80=81nofollow=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・メソッドを整理 ・カスタム分類にnoindex、nofollowのメタボックスが表示されていなかったので修正 --- class/mtee-noindexnofollow-setting.php | 206 ++++++++++----------- class/mtee-output.php | 237 ++----------------------- class/mtee-tax-setting.php | 21 --- class/mtee.php | 1 - config.php | 1 + trait/output_keyword_trait.php | 152 ++++++++++++++++ trait/output_noindexnofollow_trait.php | 56 ++++++ 7 files changed, 325 insertions(+), 349 deletions(-) create mode 100644 trait/output_keyword_trait.php create mode 100644 trait/output_noindexnofollow_trait.php diff --git a/class/mtee-noindexnofollow-setting.php b/class/mtee-noindexnofollow-setting.php index 912918d..80d398d 100644 --- a/class/mtee-noindexnofollow-setting.php +++ b/class/mtee-noindexnofollow-setting.php @@ -1,152 +1,154 @@ set_tax_meta_box(); //各投稿画面 - add_action( 'create_term', array( $this, 'save_terms' ) ); //新規追加用フック - add_action( 'edit_terms', array( $this, 'save_terms' ) ); //編集ページ用フック + //Taxonomy + $this->set_tax_meta_box(); //各投稿画面 + add_action('create_term', array($this, 'save_terms')); //新規追加用フック + add_action('edit_terms', array($this, 'save_terms')); //編集ページ用フック - } + } - public function add_meta_fields() { - $target_posts = $this->set_post_types(); - foreach ( $target_posts as $target_post ) { - add_meta_box( 'noindex_nofollow_setting', 'noindex nofollow 設定', array( - $this, - 'insert_meta_fields' - ), $target_post, 'normal' ); - } - } + public function add_meta_fields() { + $target_posts = $this->set_post_types(); + foreach ($target_posts as $target_post) { + add_meta_box('noindex_nofollow_setting', 'noindex nofollow 設定', array( + $this, + 'insert_meta_fields' + ), $target_post, 'normal'); + } + } - private function set_post_types(): array { - $custom_posts = array_values( get_post_types( array( 'public' => true, '_builtin' => false ) ) ); + private function set_post_types(): array { + $custom_posts = array_values(get_post_types(array('public' => true, '_builtin' => false))); - return array_merge( array( 'page', 'post', ), $custom_posts ); - } + return array_merge(array('page', 'post',), $custom_posts); + } - //------------------------------------------------------------------------------------------- - // 投稿タイプのカスタムフィールド設定 - //------------------------------------------------------------------------------------------- - // カスタムフィールドの入力エリア - public function insert_meta_fields() { - global $post; - $noindex_value = get_post_meta( $post->ID, MTEE_NAME_NOINDEX, true ); - $nofollow_value = get_post_meta( $post->ID, MTEE_NAME_NOFOLLOW, true ); - echo ' + //------------------------------------------------------------------------------------------- + // 投稿タイプのカスタムフィールド設定 + //------------------------------------------------------------------------------------------- + // カスタムフィールドの入力エリア + public function insert_meta_fields() { + global $post; + $noindex_value = get_post_meta($post->ID, MTEE_NAME_NOINDEX, true); + $nofollow_value = get_post_meta($post->ID, MTEE_NAME_NOFOLLOW, true); + echo '
+ checked($noindex_value, 1); + echo ' />
+ checked($nofollow_value, 1); + echo '. />
'; - } + } - // カスタムフィールドの値を保存 - public function save_meta_fields( $post_id ) { - update_post_meta( $post_id, MTEE_NAME_NOINDEX, $_POST[ MTEE_NAME_NOINDEX ] ); - update_post_meta( $post_id, MTEE_NAME_NOFOLLOW, $_POST[ MTEE_NAME_NOFOLLOW ] ); - } + // カスタムフィールドの値を保存 + public function save_meta_fields($post_id) { + update_post_meta($post_id, MTEE_NAME_NOINDEX, $_POST[MTEE_NAME_NOINDEX] ?? ''); + update_post_meta($post_id, MTEE_NAME_NOFOLLOW, $_POST[MTEE_NAME_NOFOLLOW] ?? ''); + } - //------------------------------------------------------------------------------------------- - // タクソノミーのカスタムフィールド設定 - //------------------------------------------------------------------------------------------- - public function set_tax_meta_box() { - $taxs = $this->set_tax_types(); - foreach ( $taxs as $tax ) { - add_action( $tax . '_add_form_fields', array( $this, 'add_tax_term_fields' ) ); - add_action( $tax . '_edit_form_fields', array( $this, 'edit_tax_term_fields' ) ); - } - } + //------------------------------------------------------------------------------------------- + // タクソノミーのカスタムフィールド設定 + //------------------------------------------------------------------------------------------- + public function set_tax_meta_box() { + $taxs = $this->set_tax_types(); + foreach ($taxs as $tax) { + add_action($tax . '_add_form_fields', array($this, 'add_tax_term_fields')); + add_action($tax . '_edit_form_fields', array($this, 'edit_tax_term_fields')); + } + } - private function set_tax_types() { - $custom_tax = get_taxonomies( array( 'public' => true, '_builtin' => false ) ); + public function set_tax_types(): array { + $custom_tax = get_taxonomies(array('public' => true, '_builtin' => false)); + $tax_array = array_merge(array('category', 'post_tag'), $custom_tax); + $add_tax = filter_input(INPUT_GET, 'taxonomy'); + if (!empty($add_tax)) { + return array_merge($tax_array, array($add_tax)); + } + return $tax_array; + } - return array_merge( array( 'category', 'post_tag' ), $custom_tax ); - } + public function add_tax_term_fields($tag) { + $form = '
##CONTENTS##
'; + echo $this->insert_term_meta_fields($tag, $form); + } - public function add_tax_term_fields( $tag ) { - $form = '
##CONTENTS##
'; - - echo $this->insert_term_meta_fields( $tag, $form ); - } - - public function edit_tax_term_fields( $tag ) { - $form = ' + public function edit_tax_term_fields($tag) { + $form = ' noindex / nofollow ##CONTENTS## '; - echo $this->insert_term_meta_fields( $tag, $form ); - } + echo $this->insert_term_meta_fields($tag, $form); + } - private function insert_term_meta_fields( $tag, $form ) { - $noindex_value = ''; - $nofollow_value = ''; - if ( isset( $tag->term_id ) ) { - $noindex_value = get_term_meta( $tag->term_id, MTEE_NAME_NOINDEX, true ); - $nofollow_value = get_term_meta( $tag->term_id, MTEE_NAME_NOFOLLOW, true ); - } - $add_meta = ' + private function insert_term_meta_fields($tag, $form) { + $noindex_value = ''; + $nofollow_value = ''; + if (isset($tag->term_id)) { + $noindex_value = get_term_meta($tag->term_id, MTEE_NAME_NOINDEX, true); + $nofollow_value = get_term_meta($tag->term_id, MTEE_NAME_NOFOLLOW, true); + } + $add_meta = '
'; - return str_replace( '##CONTENTS##', $add_meta, $form ); + return str_replace('##CONTENTS##', $add_meta, $form); - } + } - private static function create_checked( $current, $value ) { - if ( $current == $value ) { - return ' checked="checked"'; - } + private static function create_checked($current, $value) { + if ($current == $value) { + return ' checked="checked"'; + } - return; - } + return; + } - public function save_terms( $term_id ) { - if ( array_key_exists( MTEE_NAME_NOINDEX, $_POST ) ) { - update_term_meta( $term_id, MTEE_NAME_NOINDEX, $_POST[ MTEE_NAME_NOINDEX ] ); - } - if ( array_key_exists( MTEE_NAME_NOFOLLOW, $_POST ) ) { - update_term_meta( $term_id, MTEE_NAME_NOFOLLOW, $_POST[ MTEE_NAME_NOFOLLOW ] ); - } - } + public function save_terms($term_id) { + if (array_key_exists(MTEE_NAME_NOINDEX, $_POST)) { + update_term_meta($term_id, MTEE_NAME_NOINDEX, $_POST[MTEE_NAME_NOINDEX]); + } + if (array_key_exists(MTEE_NAME_NOFOLLOW, $_POST)) { + update_term_meta($term_id, MTEE_NAME_NOFOLLOW, $_POST[MTEE_NAME_NOFOLLOW]); + } + } + } - } - -} \ No newline at end of file +} diff --git a/class/mtee-output.php b/class/mtee-output.php index 99dbdb1..bb1d9b4 100644 --- a/class/mtee-output.php +++ b/class/mtee-output.php @@ -13,8 +13,15 @@ if (!class_exists('mtee_meta_output')) { * Class mtee_meta_output * */ + + include_once MTEE_TRAIT_DIR . 'output_keyword_trait.php'; + include_once MTEE_TRAIT_DIR . 'output_noindexnofollow_trait.php'; + include_once MTEE_TRAIT_DIR . 'output_canonical.php'; + class mtee_meta_output { + use output_keyword_trait, output_noindexnofollow_trait, output_canonical; + private $site_name; private $description; @@ -23,8 +30,7 @@ if (!class_exists('mtee_meta_output')) { $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')); - + //add_action('wp_head', array($this, 'set_canonical_url')); } //-------------------------------------------------------------------- @@ -54,7 +60,7 @@ if (!class_exists('mtee_meta_output')) { echo implode(PHP_EOL, $metas) . PHP_EOL; } - private function post_output_view_metas($type): array { + 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'; @@ -66,7 +72,7 @@ if (!class_exists('mtee_meta_output')) { return $this->create_meta_tags($meta_data); } - private function post_output_get_meta_data($type): array { + public function post_output_get_meta_data($type): array { global $post; if ($type == 'top_page') { $meta_key_words = get_option('_mtee')['top_page']['keywords']; @@ -90,17 +96,6 @@ if (!class_exists('mtee_meta_output')) { ); } - private function create_meta_tags($meta_data): array { - $metas = array(); - if (!empty($meta_data[MTEE_NAME_KEYWORDS])) { - $metas[] = str_replace('##KEYWORDS##', $meta_data[MTEE_NAME_KEYWORDS], ''); - } - if (!empty($meta_data[MTEE_NAME_DESCRIPTION])) { - $metas[] = str_replace('##DISCRIPTION##', $meta_data[MTEE_NAME_DESCRIPTION], ''); - } - return $metas; - } - public function create_default_keywords_description(): array { $keywords = $this->create_default_keywords(); $this->create_default_description(); @@ -112,123 +107,7 @@ if (!class_exists('mtee_meta_output')) { return $this->create_meta_tags($meta_data); } - private function create_default_keywords() { - $type = ''; - $title = ''; - if (is_category()) { - $type = 'category'; - $title = single_cat_title('', false); - } elseif (is_tag()) { - $type = 'tag'; - $title = single_cat_title('', false); - } elseif (is_tax()) { - $type = 'tax'; - $title = single_term_title('', false); - } elseif (is_post_type_archive()) { - $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[] = $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 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() { + public function create_default_description() { $type = ''; $title = ''; $suffix_text = MTEE_META_DESC_ARCHIVE_BASE; @@ -263,25 +142,12 @@ if (!class_exists('mtee_meta_output')) { } } - private 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)); - } - } //-------------------------------------------------------------------- // noindex, nofollow //-------------------------------------------------------------------- public function set_noindex_nofollow() { + global $post; $type = ''; $id = null; @@ -307,51 +173,6 @@ if (!class_exists('mtee_meta_output')) { } - private function get_noindex_nofollow_data($id, $type) { - if (empty($type)) { - return; - } - - $status = $this->get_noindex_nofollow_stauts($type, $id); - - if ($status['noindex'] == '0' && $status['nofollow'] == '0') { - return; - } - if ($status['noindex'] == '1' && $status['nofollow'] == '1') { - return ''; - } - if ($status['noindex'] == '1' && $status['nofollow'] == '0') { - return ''; - } - if ($status['noindex'] == '0' && $status['nofollow'] == '1') { - return ''; - } - } - - private function get_noindex_nofollow_stauts($type, $id): array { - $noindex = 0; - $nofollow = 0; - if ($type == 'top_page') { - $noindex = get_option('_mtee')['top_page']['noindex']; - $nofollow = get_option('_mtee')['top_page']['nofollow']; - } elseif ($type == 'custom_post_archive') { - $custom_post_name = get_post_type_object(get_post_type())->name; - $noindex = get_option('_mtee')['custom_post'][$custom_post_name]['noindex']; - $nofollow = get_option('_mtee')['custom_post'][$custom_post_name]['nofollow']; - } elseif ($type == 'posts') { - $noindex = get_post_meta($id, MTEE_NAME_NOINDEX, true); - $nofollow = get_post_meta($id, MTEE_NAME_NOFOLLOW, true); - } elseif ($type == 'tax') { - $noindex = get_term_meta($id, MTEE_NAME_NOINDEX, true); - $nofollow = get_term_meta($id, MTEE_NAME_NOFOLLOW, true); - } - - return array( - 'noindex' => $noindex, - 'nofollow' => $nofollow, - ); - } - //-------------------------------------------------------------------- // canonical //-------------------------------------------------------------------- @@ -382,39 +203,5 @@ if (!class_exists('mtee_meta_output')) { } - private function get_canonical_data($id, $type) { - if (empty($type)) { - return; - } - $canonical = ''; - - if ($type == 'top_page') { - $canonical = get_option('_mtee')['top_page']['canonical']; - } elseif ($type == 'custom_post_archive') { - $custom_post_name = get_post_type_object(get_post_type())->name; - $canonical = get_option('_mtee')['custom_post'][$custom_post_name]['canonical']; - } elseif ($type == 'posts') { - $canonical = get_post_meta($id, MTEE_CANONICAL_URL, true); - if (empty($canonical)) { - $canonical = get_permalink(); - global $page, $paged; - if ($paged >= 2 || $page >= 2) { - $canonical .= 'page/' . max($paged, $page) . '/'; - } - } - } - -// $noindex = get_post_meta($id, MTEE_NAME_NOINDEX, true); -// $nofollow = get_post_meta($id, MTEE_NAME_NOFOLLOW, true); -// } elseif ($type == 'tax') { -// $noindex = get_term_meta($id, MTEE_NAME_NOINDEX, true); -// $nofollow = get_term_meta($id, MTEE_NAME_NOFOLLOW, true); -// } - - if ($canonical) { - return '' . PHP_EOL; - } - } - } } \ No newline at end of file diff --git a/class/mtee-tax-setting.php b/class/mtee-tax-setting.php index 4fabec7..6b04a18 100644 --- a/class/mtee-tax-setting.php +++ b/class/mtee-tax-setting.php @@ -40,27 +40,6 @@ if ( ! class_exists( 'mtee_tax_setting' ) ) { } } - // カスタムフィールドの入力エリア - function term_meta_fields( $tag ) { - $key_value = ''; - $desc_value = ''; - if ( isset( $tag->term_id ) ) { - $key_value = get_term_meta( $tag->term_id, MTEE_NAME_KEYWORDS, true ); - $desc_value = get_term_meta( $tag->term_id, MTEE_MTEE_NAME_DESCRIPTION, true ); - } - echo ' -
-meta keywords
- -
-
-meta description
- -
-'; - } - - function insert_term_meta_add_fields( $tag ) { $key_value = ''; $desc_value = ''; diff --git a/class/mtee.php b/class/mtee.php index c9352be..1caba8a 100644 --- a/class/mtee.php +++ b/class/mtee.php @@ -53,7 +53,6 @@ if (!class_exists('MTEE')) { public static function register_target(): array { $custom_posts = get_post_types(array('public' => true, '_builtin' => false)); $taxonomies = get_taxonomies(array('public' => true, '_builtin' => false)); - return [ 'custom_posts' => $custom_posts, 'taxonomies' => $taxonomies, diff --git a/config.php b/config.php index 318a558..481ad2a 100644 --- a/config.php +++ b/config.php @@ -1,6 +1,7 @@ get_keywords_data($type, $title); + } + + return $this->get_custom_template_keywords_data($custom_keywords_temp, $type, $title); + + } + + public 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[] = $keyword; + } + return array_reverse($keywords); + } + + public 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; + } + + public 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); + } + } + } + } + + public 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); + } + } + } + + public function get_single_keyword_tax_data(): array { + global $post; + return array_reverse(get_object_taxonomies(get_post_type($post->ID))); + } + + public function get_custom_keywords_terms(&$keywords, $id, $target) { + $terms = get_the_terms($id, $target); + if ($terms) { + foreach ($terms as $term) { + $keywords[] = $term->name; + } + } + } + + public function create_meta_tags($meta_data): array { + $metas = array(); + if (!empty($meta_data[MTEE_NAME_KEYWORDS])) { + $metas[] = str_replace('##KEYWORDS##', $meta_data[MTEE_NAME_KEYWORDS], ''); + } + if (!empty($meta_data[MTEE_NAME_DESCRIPTION])) { + $metas[] = str_replace('##DISCRIPTION##', $meta_data[MTEE_NAME_DESCRIPTION], ''); + } + 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/trait/output_noindexnofollow_trait.php b/trait/output_noindexnofollow_trait.php new file mode 100644 index 0000000..760b750 --- /dev/null +++ b/trait/output_noindexnofollow_trait.php @@ -0,0 +1,56 @@ +get_noindex_nofollow_stauts($type, $id); + + if ($status['noindex'] == '0' && $status['nofollow'] == '0') { + return; + } + if ($status['noindex'] == '1' && $status['nofollow'] == '1') { + return ''; + } + if ($status['noindex'] == '1' && $status['nofollow'] == '0') { + return ''; + } + if ($status['noindex'] == '0' && $status['nofollow'] == '1') { + return ''; + } + } + + public function get_noindex_nofollow_stauts($type, $id): array { + $noindex = 0; + $nofollow = 0; + if ($type == 'top_page') { + $noindex = get_option('_mtee')['top_page']['noindex']; + $nofollow = get_option('_mtee')['top_page']['nofollow']; + } elseif ($type == 'custom_post_archive') { + $custom_post_name = get_post_type_object(get_post_type())->name; + $noindex = get_option('_mtee')['custom_post'][$custom_post_name]['noindex']; + $nofollow = get_option('_mtee')['custom_post'][$custom_post_name]['nofollow']; + } elseif ($type == 'posts') { + $noindex = get_post_meta($id, MTEE_NAME_NOINDEX, true); + $nofollow = get_post_meta($id, MTEE_NAME_NOFOLLOW, true); + } elseif ($type == 'tax') { + $noindex = get_term_meta($id, MTEE_NAME_NOINDEX, true); + $nofollow = get_term_meta($id, MTEE_NAME_NOFOLLOW, true); + } + + return array( + 'noindex' => $noindex, + 'nofollow' => $nofollow, + ); + } + + } + +} \ No newline at end of file From ee28571645cfddb022be5e61f0bc64e857fe9766 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 27 May 2021 16:10:58 +0900 Subject: [PATCH 3/3] =?UTF-8?q?WP=20PLUGIN=20MTEE(Meta=20Tag=20etc=20Exten?= =?UTF-8?q?d)=20=E3=82=AB=E3=83=86=E3=82=B4=E3=83=AA=E3=83=BC=E3=80=81?= =?UTF-8?q?=E3=82=BF=E3=82=B0=E3=80=81=E3=82=AB=E3=82=B9=E3=82=BF=E3=83=A0?= =?UTF-8?q?=E5=88=86=E9=A1=9E=E3=81=AEcanonical=E8=A8=AD=E5=AE=9A=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・カテゴリー、タグ、カスタム分類にcanonical urlの入力設定メタボックス追加 ・デフォルトのcanonical urlを追加 --- class/mtee-canonical-setting.php | 68 ++++++++++++++++++++++++- class/mtee-output.php | 6 +-- trait/output_canonical.php | 87 ++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 5 deletions(-) create mode 100644 trait/output_canonical.php diff --git a/class/mtee-canonical-setting.php b/class/mtee-canonical-setting.php index 282e814..dfd9391 100644 --- a/class/mtee-canonical-setting.php +++ b/class/mtee-canonical-setting.php @@ -12,15 +12,26 @@ if (!class_exists('mtee_canonical_setting')) { class mtee_canonical_setting { private $options; + private $tax_place_holder; public function __construct($options) { $this->options = $options; - if ($this->options['enabled'] == 1) { + if ($this->options['canonical_setting'] == 1) { + //Posts add_action('admin_menu', array($this, 'add_meta_fields')); add_action('save_post', array($this, 'save_meta_fields')); + + //Taxonomy + $this->set_tax_meta_box(); //各投稿画面 + add_action('create_term', array($this, 'save_terms')); //新規追加用フック + add_action('edit_terms', array($this, 'save_terms')); //編集ページ用フック + } } + //------------------------------------------------------------------------------------------- + // 投稿、ページのカスタムフィールド設定 + //------------------------------------------------------------------------------------------- public function add_meta_fields() { //add_meta_box(表示される入力ボックスのHTMLのID, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法) $default_built_ins = array( @@ -57,7 +68,7 @@ if (!class_exists('mtee_canonical_setting')) { echo '
- +
'; } @@ -70,6 +81,59 @@ if (!class_exists('mtee_canonical_setting')) { delete_post_meta($post_id, MTEE_CANONICAL_URL); } } + + //------------------------------------------------------------------------------------------- + // タクソノミーのカスタムフィールド設定 + //------------------------------------------------------------------------------------------- + public function set_tax_meta_box() { + $taxs = $this->set_tax_types(); + foreach ($taxs as $tax) { + add_action($tax . '_add_form_fields', array($this, 'add_tax_term_fields')); + add_action($tax . '_edit_form_fields', array($this, 'edit_tax_term_fields')); + } + } + + public function set_tax_types(): array { + $custom_tax = get_taxonomies(array('public' => true, '_builtin' => false)); + $tax_array = array_merge(array('category', 'post_tag'), $custom_tax); + $add_tax = filter_input(INPUT_GET, 'taxonomy'); + + if (!empty($add_tax)) { + return array_merge($tax_array, array($add_tax)); + } + return $tax_array; + } + + public function add_tax_term_fields($tag) { + $form = '
Canonical URL
##CONTENTS##
'; + + echo $this->insert_term_meta_fields($tag, $form); + } + + public function edit_tax_term_fields($tag) { + $form = ' + Canonical URL + ##CONTENTS## + '; + + echo $this->insert_term_meta_fields($tag, $form); + } + + public function insert_term_meta_fields($tag, $form) { + $canonical_url = ''; + if (isset($tag->term_id)) { + $canonical_url = get_term_meta($tag->term_id, MTEE_CANONICAL_URL, true); + } + $add_meta = ''; + return str_replace('##CONTENTS##', $add_meta, $form); + } + + public function save_terms($term_id) { + if (array_key_exists(MTEE_CANONICAL_URL, $_POST)) { + update_term_meta($term_id, MTEE_CANONICAL_URL, $_POST[MTEE_CANONICAL_URL]); + } + } + } } \ No newline at end of file diff --git a/class/mtee-output.php b/class/mtee-output.php index bb1d9b4..6da575a 100644 --- a/class/mtee-output.php +++ b/class/mtee-output.php @@ -30,7 +30,7 @@ if (!class_exists('mtee_meta_output')) { $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')); + add_action('wp_head', array($this, 'set_canonical_url')); } //-------------------------------------------------------------------- @@ -185,10 +185,10 @@ if (!class_exists('mtee_meta_output')) { $type = 'custom_post_archive'; } elseif (is_category()) { $id = get_query_var('cat'); - $type = 'tax'; + $type = 'cat'; } elseif (is_tag()) { $id = get_query_var('tag_id'); - $type = 'tax'; + $type = 'tag'; } elseif (is_tax()) { $id = get_queried_object()->term_id; $type = 'tax'; diff --git a/trait/output_canonical.php b/trait/output_canonical.php new file mode 100644 index 0000000..8207140 --- /dev/null +++ b/trait/output_canonical.php @@ -0,0 +1,87 @@ +set_top_page_canonical(); + } elseif ($type == 'custom_post_archive') { + $canonical = $this->set_custom_post_archive_canonical(); + } elseif ($type == 'posts') { + $canonical = $this->set_post_canonical($id); + } elseif ($type == 'cat') { + $canonical = $this->set_category_canonical($id); + } elseif ($type == 'tag') { + $canonical = $this->set_tag_canonical($id); + } elseif ($type == 'tax') { + $canonical = $this->set_tax_canonical($id); + } + + if ($canonical) { + return '' . PHP_EOL; + } + } + + public function set_top_page_canonical() { + $canonical = get_option('_mtee')['top_page']['canonical']; + if (empty($canonical)) { + $canonical = get_bloginfo('url'); + } + return $canonical; + } + + public function set_custom_post_archive_canonical() { + $custom_post_name = get_post_type_object(get_post_type())->name; + $canonical = get_option('_mtee')['custom_post'][$custom_post_name]['canonical']; + if (empty($canonical)) { + $canonical = get_post_type_archive_link($custom_post_name); + } + return $canonical; + } + + public function set_post_canonical($id) { + $canonical = get_post_meta($id, MTEE_CANONICAL_URL, true); + if (empty($canonical)) { + $canonical = get_permalink($id); + global $page, $paged; + if ($paged >= 2 || $page >= 2) { + $canonical .= 'page/' . max($paged, $page) . '/'; + } + } + return $canonical; + } + + public function set_category_canonical($id) { + $canonical = get_term_meta($id, MTEE_CANONICAL_URL, true); + if (empty($canonical)) { + $canonical = get_category_link($id); + } + return $canonical; + } + + public function set_tag_canonical($id) { + $canonical = get_term_meta($id, MTEE_CANONICAL_URL, true); + if (empty($canonical)) { + $canonical = get_tag_link($id); + } + return $canonical; + } + + public function set_tax_canonical($id) { + $canonical = get_term_meta($id, MTEE_CANONICAL_URL, true); + if (empty($canonical)) { + $canonical = get_term_link($id); + } + return $canonical; + } + } + +} \ No newline at end of file