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