WP PLUGIN MTEE(Meta Tag etc Extend) カスタム分類noindex、nofollowの修正
・メソッドを整理 ・カスタム分類にnoindex、nofollowのメタボックスが表示されていなかったので修正
This commit is contained in:
@@ -1,152 +1,154 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
} // Exit if accessed directly
|
||||
|
||||
if ( ! class_exists( 'mtee_noindexnofolow_setting' ) ) {
|
||||
if (!class_exists('mtee_noindexnofolow_setting')) {
|
||||
|
||||
/**
|
||||
* noindex,nofollowの設定フォーム
|
||||
* Class mtee_noindexnofolow_setting
|
||||
*/
|
||||
class mtee_noindexnofolow_setting {
|
||||
/**
|
||||
* noindex,nofollowの設定フォーム
|
||||
* Class mtee_noindexnofolow_setting
|
||||
*/
|
||||
class mtee_noindexnofolow_setting {
|
||||
|
||||
public function __construct( $options ) {
|
||||
//posts
|
||||
add_action( 'admin_menu', array( $this, 'add_meta_fields' ) );
|
||||
add_action( 'save_post', array( $this, 'save_meta_fields' ) );
|
||||
public function __construct($options) {
|
||||
//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' ) ); //編集ページ用フック
|
||||
//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 '
|
||||
<div class="meta_noindex_nofollow_box">
|
||||
<div>
|
||||
<label style="font-weight: bold;">noindex</label>
|
||||
<input type="hidden" name="' . MTEE_NAME_NOINDEX . '" value="0" />
|
||||
<input type="checkbox" name="' . MTEE_NAME_NOINDEX . '" value="1"';
|
||||
checked( $noindex_value, 1 );
|
||||
echo ' /></div>
|
||||
checked($noindex_value, 1);
|
||||
echo ' /></div>
|
||||
<div>
|
||||
<label style="font-weight: bold;">nofollow</label>
|
||||
<input type="hidden" name="' . MTEE_NAME_NOFOLLOW . '" value="0" />
|
||||
<input type="checkbox" name="' . MTEE_NAME_NOFOLLOW . '" value="1"';
|
||||
checked( $nofollow_value, 1 );
|
||||
echo '. />
|
||||
checked($nofollow_value, 1);
|
||||
echo '. />
|
||||
</div>
|
||||
</div>';
|
||||
}
|
||||
}
|
||||
|
||||
// カスタムフィールドの値を保存
|
||||
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 = '<div class="meta_noindex_nofollow_outer">##CONTENTS##</div>';
|
||||
|
||||
echo $this->insert_term_meta_fields($tag, $form);
|
||||
}
|
||||
|
||||
public function add_tax_term_fields( $tag ) {
|
||||
$form = '<div class="meta_noindex_nofollow_outer">##CONTENTS##</div>';
|
||||
|
||||
echo $this->insert_term_meta_fields( $tag, $form );
|
||||
}
|
||||
|
||||
public function edit_tax_term_fields( $tag ) {
|
||||
$form = '<tr class="form-field">
|
||||
public function edit_tax_term_fields($tag) {
|
||||
$form = '<tr class="form-field">
|
||||
<th scope="row">noindex / nofollow</th>
|
||||
<td>##CONTENTS##</td>
|
||||
</tr>';
|
||||
|
||||
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 = '
|
||||
<div class="meta_noindex_nofollow_box">
|
||||
<label>
|
||||
<input type="hidden" name="' . MTEE_NAME_NOINDEX . '" value="0" />
|
||||
<input type="checkbox" name="' . MTEE_NAME_NOINDEX . '" value="1" ' . self::create_checked( 1, $noindex_value ) . ' />
|
||||
<input type="checkbox" name="' . MTEE_NAME_NOINDEX . '" value="1" ' . self::create_checked(1, $noindex_value) . ' />
|
||||
noindex
|
||||
</label>
|
||||
<label>
|
||||
<input type="hidden" name="' . MTEE_NAME_NOFOLLOW . '" value="0" />
|
||||
<input type="checkbox" name="' . MTEE_NAME_NOFOLLOW . '" value="1" ' . self::create_checked( 1, $nofollow_value ) . ' />
|
||||
<input type="checkbox" name="' . MTEE_NAME_NOFOLLOW . '" value="1" ' . self::create_checked(1, $nofollow_value) . ' />
|
||||
nofollow
|
||||
</label>
|
||||
</div>';
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+12
-225
@@ -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], '<meta name="keywords" content="##KEYWORDS##" />');
|
||||
}
|
||||
if (!empty($meta_data[MTEE_NAME_DESCRIPTION])) {
|
||||
$metas[] = str_replace('##DISCRIPTION##', $meta_data[MTEE_NAME_DESCRIPTION], '<meta name="description" content="##DISCRIPTION##" />');
|
||||
}
|
||||
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 '<meta name="robots" content="noindex,nofollow" />';
|
||||
}
|
||||
if ($status['noindex'] == '1' && $status['nofollow'] == '0') {
|
||||
return '<meta name="robots" content="noindex,follow" />';
|
||||
}
|
||||
if ($status['noindex'] == '0' && $status['nofollow'] == '1') {
|
||||
return '<meta name="robots" content="index,nofollow" />';
|
||||
}
|
||||
}
|
||||
|
||||
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 '<link rel="canonical" href="' . $canonical . '">' . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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 '
|
||||
<div class="form-field">
|
||||
meta keywords<br>
|
||||
<input type="text" name="' . MTEE_NAME_KEYWORDS . '" value="' . $key_value . '" class="tax-meta-field" />
|
||||
</div>
|
||||
<div class="form-field">
|
||||
meta description<br>
|
||||
<input type="text" name="' . MTEE_NAME_DESCRIPTION . '" value="' . $desc_value . '" class="tax-meta-field" />
|
||||
</div>
|
||||
';
|
||||
}
|
||||
|
||||
|
||||
function insert_term_meta_add_fields( $tag ) {
|
||||
$key_value = '';
|
||||
$desc_value = '';
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
const MTEE_CLASS_DIR = __DIR__ . '/class/';
|
||||
const MTEE_TRAIT_DIR = __DIR__ . '/trait/';
|
||||
const MTEE_TEMPLATE_DIR = __DIR__ . '/template/';
|
||||
const MTEE_NAME_KEYWORDS = 'mtee_meta_keywords';
|
||||
const MTEE_NAME_DESCRIPTION = 'mtee_meta_description';
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
} // Exit if accessed directly
|
||||
if (!trait_exists('output_keyword_trait')) {
|
||||
|
||||
trait output_keyword_trait {
|
||||
|
||||
public 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);
|
||||
|
||||
}
|
||||
|
||||
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], '<meta name="keywords" content="##KEYWORDS##" />');
|
||||
}
|
||||
if (!empty($meta_data[MTEE_NAME_DESCRIPTION])) {
|
||||
$metas[] = str_replace('##DISCRIPTION##', $meta_data[MTEE_NAME_DESCRIPTION], '<meta name="description" content="##DISCRIPTION##" />');
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
} // Exit if accessed directly
|
||||
if (!trait_exists('output_noindexnofollow_trait')) {
|
||||
|
||||
trait output_noindexnofollow_trait {
|
||||
|
||||
public 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 '<meta name="robots" content="noindex,nofollow" />';
|
||||
}
|
||||
if ($status['noindex'] == '1' && $status['nofollow'] == '0') {
|
||||
return '<meta name="robots" content="noindex,follow" />';
|
||||
}
|
||||
if ($status['noindex'] == '0' && $status['nofollow'] == '1') {
|
||||
return '<meta name="robots" content="index,nofollow" />';
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user