WP PLUGIN MTEE(Meta Tag etc Extend) ファイル整理

・各項目のOFF/ONに対応して設定/HTML出力するようファイルを整理
This commit is contained in:
2021-05-28 21:02:28 +09:00
parent 3c381f8295
commit 21ccef70f1
7 changed files with 248 additions and 239 deletions
-1
View File
@@ -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;
@@ -1,10 +1,47 @@
<?php
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
if (!trait_exists('output_canonical')) {
trait output_canonical {
if (!class_exists('mtee_meta_output_canonical')) {
/**
* Class mtee_meta_output_canonical
*/
class mtee_meta_output_canonical {
public function __construct() {
add_action('wp_head', array($this, 'set_canonical_url'));
}
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);
}
public function get_canonical_data($id, $type) {
if (empty($type)) {
return;
@@ -82,6 +119,6 @@ if (!trait_exists('output_canonical')) {
}
return $canonical;
}
}
}
}
@@ -1,10 +1,49 @@
<?php
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
if (!trait_exists('output_noindexnofollow_trait')) {
trait output_noindexnofollow_trait {
if (!class_exists('mtee_meta_output_noindexnofollow')) {
/**
* Class mtee_meta_output_noindexnofollow
*/
class mtee_meta_output_noindexnofollow {
public function __construct() {
add_action('wp_head', array($this, 'set_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);
}
public function get_noindex_nofollow_data($id, $type) {
if (empty($type)) {
@@ -52,5 +91,4 @@ if (!trait_exists('output_noindexnofollow_trait')) {
}
}
}
-207
View File
@@ -1,207 +0,0 @@
<?php
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
if (!class_exists('mtee_meta_output')) {
/**
* HTML出力
* 設定が有効でキーワード・ディスクリプションのカスタムフィールドに登録があればそれを出力する。
* 登録が無い場合は、デフォルトのキーワード・ディスクリプションを出力する。
* 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;
public function __construct() {
$this->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);
}
}
}
@@ -1,10 +1,130 @@
<?php
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
if (!trait_exists('output_keyword_trait')) {
trait output_keyword_trait {
if (!class_exists('mtee_meta_output_keydesc')) {
class mtee_meta_output_keydesc {
private $site_name;
private $description;
public function __construct() {
$this->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')) {
}
}
}
}
+21 -10
View File
@@ -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;
+23 -13
View File
@@ -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');
?>
<div class="wrap">
<h2>meta keyword, description / バージョン情報 設定</h2>
@@ -193,6 +194,25 @@ $canonical_setting = $this->get_key_setting('canonical_setting');
value="1">有効
</label>
</div>
<div class="mtee-form-box mtee_box_border">
<h3>OGPタグ出力</h3>
<label>
<input type="hidden" name="_mtee[ogp_setting]" value="0">
<input type="checkbox"
name="_mtee[ogp_setting]" <?php checked($ogp_setting, 1); ?>
value="1">有効
</label>
</div>
<div class="mtee-form-box mtee_box_border">
<h3>Canonical URL 出力</h3>
<label>
<input type="hidden" name="_mtee[canonical_setting]" value="0">
<input type="checkbox"
name="_mtee[canonical_setting]" <?php checked($canonical_setting, 1); ?>
value="1">有効
</label>
</div>
<div class="mtee-form-box mtee_box_border">
<h3>WordPressバージョン情報</h3>
<label>
@@ -203,7 +223,7 @@ $canonical_setting = $this->get_key_setting('canonical_setting');
</label>
</div>
<div class="mtee-form-box mtee_box_border">
<h3>CSS / JSバージョン情報</h3>
<h3>CSS/JSバージョン情報</h3>
<label>
<input type="hidden" name="_mtee[asset_ver_disabled]" value="0">
<input type="checkbox"
@@ -211,16 +231,6 @@ $canonical_setting = $this->get_key_setting('canonical_setting');
value="1">削除
</label>
</div>
<div class="mtee-form-box mtee_box_border">
<h3>Canonical URL 設定</h3>
<label>
<input type="hidden" name="_mtee[canonical_setting]" value="0">
<input type="checkbox"
name="_mtee[canonical_setting]" <?php checked($canonical_setting, 1); ?>
value="1">有効
</label>
</div>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/></p>
</div>
<div class="mtee_tab_box">