From 5dd3c25bb6d10e5ffc1531219a9c48b642852268 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 14 May 2021 19:11:30 +0900 Subject: [PATCH] WP PLUGIN MTEE(Meta Tag etc Extend) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WordPressに下記を追加する ・meta keywords, description設定 ・noindex, nofollowの表示/非表示設定 ・WPバージョン表示/非表示設定 ・CSS/JSバージョン表示/非表示設定 --- class/mtee-noindexnofollow-setting.php | 152 +++++++++++++++++++ class/mtee-output.php | 202 +++++++++++++++++++++++++ class/mtee-post-setting.php | 74 +++++++++ class/mtee-tax-setting.php | 113 ++++++++++++++ class/mtee-version-setting.php | 36 +++++ class/mtee.php | 121 +++++++++++++++ config.php | 15 ++ css/mtee.css | 44 ++++++ meta-tag-etc-extend.php | 41 +++++ template/success.php | 1 + template/top.php | 121 +++++++++++++++ uninstall.php | 20 +++ 12 files changed, 940 insertions(+) create mode 100644 class/mtee-noindexnofollow-setting.php create mode 100644 class/mtee-output.php create mode 100644 class/mtee-post-setting.php create mode 100644 class/mtee-tax-setting.php create mode 100644 class/mtee-version-setting.php create mode 100644 class/mtee.php create mode 100644 config.php create mode 100644 css/mtee.css create mode 100644 meta-tag-etc-extend.php create mode 100644 template/success.php create mode 100644 template/top.php create mode 100644 uninstall.php diff --git a/class/mtee-noindexnofollow-setting.php b/class/mtee-noindexnofollow-setting.php new file mode 100644 index 0000000..912918d --- /dev/null +++ b/class/mtee-noindexnofollow-setting.php @@ -0,0 +1,152 @@ +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' ); + } + } + + 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 ); + } + + //------------------------------------------------------------------------------------------- + // 投稿タイプのカスタムフィールド設定 + //------------------------------------------------------------------------------------------- + // カスタムフィールドの入力エリア + 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 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' ) ); + } + } + + private function set_tax_types() { + $custom_tax = get_taxonomies( array( 'public' => true, '_builtin' => false ) ); + + 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 edit_tax_term_fields( $tag ) { + $form = ' + noindex / nofollow + ##CONTENTS## + '; + + 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 = ' +
+ + +
'; + + return str_replace( '##CONTENTS##', $add_meta, $form ); + + } + + private static function create_checked( $current, $value ) { + if ( $current == $value ) { + return ' checked="checked"'; + } + + 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 ] ); + } + } + + + } + +} \ No newline at end of file diff --git a/class/mtee-output.php b/class/mtee-output.php new file mode 100644 index 0000000..ca9907c --- /dev/null +++ b/class/mtee-output.php @@ -0,0 +1,202 @@ +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' ) ); + } + + //-------------------------------------------------------------------- + // meta keywords & meta description + //-------------------------------------------------------------------- + public function post_output_add_metas() { + $metas = []; + if ( is_tax() ) { + $metas = $this->post_output_view_metas( get_queried_object()->taxonomy ); + } elseif ( is_page() || is_single() ) { + $metas = $this->post_output_view_metas( get_post_type() ); + } elseif ( is_category() ) { + $metas = $this->post_output_view_metas( 'category' ); + } elseif ( is_tag() ) { + $metas = $this->post_output_view_metas( 'post_tag' ); + } + if ( count( $metas ) == 0 ) { + $metas = $this->create_default_keywords_description(); + } + echo implode( PHP_EOL, $metas ) . PHP_EOL; + } + + private 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 ); + } + + private function post_output_get_meta_data( $type ): array { + global $post; + if ( $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 [ + MTEE_NAME_KEYWORDS => $meta_key_words, + MTEE_NAME_DESCRIPTION => $meta_description, + ]; + } + + private function create_meta_tags( $meta_data ): array { + $metas = []; + 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->site_name; //サイトタイトル + if ( ! empty( get_bloginfo( 'description' ) ) ) { + $keywords[] = get_bloginfo( 'description' ); //サイトタイトル + } + + $this->createKeywords( $keywords ); + $this->createDescription(); + + $meta_data = [ + MTEE_NAME_KEYWORDS => implode( ',', array_reverse( $keywords ) ), + MTEE_NAME_DESCRIPTION => $this->description, + ]; + + return $this->create_meta_tags( $meta_data ); + } + + private function createKeywords( &$keywords ) { + if ( is_category() || is_tag() ) { + $keywords[] = single_cat_title( '', false ); + } elseif ( is_tax() ) { + $keywords[] = single_term_title( '', false ); + } elseif ( is_post_type_archive() ) { + $keywords[] = post_type_archive_title( '', false ); + } else { + $keywords[] = get_the_title(); + } + } + + private function createDescription() { + if ( is_home() || is_front_page() ) { //ページ + $this->description .= MTEE_META_DESC_TOP_BASE; + } + + if ( is_page() || is_single() ) { //ページ + $replacement = array( get_the_title(), MTEE_META_DESC_SINGLE_BASE ); + } elseif ( is_category() ) { //カテゴリー + $replacement = array( single_cat_title( '', false ), MTEE_META_DESC_ARCHIVE_BASE ); + } elseif ( is_tag() ) { //タグ + $replacement = array( single_tag_title( '', false ), MTEE_META_DESC_ARCHIVE_BASE ); + } elseif ( is_tax() ) { //タクソノミー + $replacement = array( single_term_title( '', false ), MTEE_META_DESC_ARCHIVE_BASE ); + } elseif ( is_post_type_archive() ) { //カスタム投稿のアーカイブ + $replacement = array( post_type_archive_title( '', false ), MTEE_META_DESC_ARCHIVE_BASE ); + } + + if ( isset( $replacement ) ) { + $search = array( '<###>', '<%%%>' ); + $description_noun = MTEE_META_DESC_BEFORE_BRACKETS . '<###>' . MTEE_META_DESC_AFTER_BRACKETS . '<%%%>'; + $this->description .= str_replace( $search, $replacement, $description_noun ); + } + } + + //-------------------------------------------------------------------- + // noindex, nofollow + //-------------------------------------------------------------------- + public function set_noindex_nofollow() { + global $post; + $type = ''; + if ( 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() || is_page() || is_single() ) { + $id = $post->ID; + $type = 'posts'; + } + + echo $this->get_noindex_nofollow_data( $id, $type ); + + } + + 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 ) { + if ( $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 diff --git a/class/mtee-post-setting.php b/class/mtee-post-setting.php new file mode 100644 index 0000000..d450ea0 --- /dev/null +++ b/class/mtee-post-setting.php @@ -0,0 +1,74 @@ +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( 'meta_setting', 'meta設定', 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( 'meta_setting', 'meta設定', array( + $this, + 'insert_meta_fields' + ), $custom_post, 'normal' ); + } + } + } + } + + // カスタムフィールドの入力エリア + public function insert_meta_fields() { + global $post; + echo '
+ +
+
'; + echo ' +
+
+
'; + } + + // カスタムフィールドの値を保存 + public function save_meta_fields( $post_id ) { + if ( ! empty( $_POST[ MTEE_NAME_KEYWORDS ] ) ) { //meta_keywordsが入力されている場合 + update_post_meta( $post_id, MTEE_NAME_KEYWORDS, $_POST[ MTEE_NAME_KEYWORDS ] ); //値を保存 + } else { //未入力の場合は値を削除 + delete_post_meta( $post_id, MTEE_NAME_KEYWORDS ); + } + if ( ! empty( $_POST[ MTEE_NAME_DESCRIPTION ] ) ) { + update_post_meta( $post_id, MTEE_NAME_DESCRIPTION, $_POST[ MTEE_NAME_DESCRIPTION ] ); + } else { + delete_post_meta( $post_id, MTEE_NAME_DESCRIPTION ); + } + } + } + +} \ No newline at end of file diff --git a/class/mtee-tax-setting.php b/class/mtee-tax-setting.php new file mode 100644 index 0000000..4fabec7 --- /dev/null +++ b/class/mtee-tax-setting.php @@ -0,0 +1,113 @@ +options = $options; + $this->set_fields(); + add_action( 'create_term', array( $this, 'save_terms' ) ); //新規追加用フック + add_action( 'edit_terms', array( $this, 'save_terms' ) ); //編集ページ用フック + } + + public function set_fields() { + $built_ins = array( + 'term' => array( 'category', 'post_tag' ), + ); + + foreach ( $built_ins['term'] as $built_in ) { + add_action( $built_in . '_add_form_fields', array( $this, 'insert_term_meta_add_fields' ) ); + add_action( $built_in . '_edit_form_fields', array( $this, 'insert_term_meta_edit_fields' ) ); + } + + if ( isset( $this->options['taxonomies'] ) ) { + foreach ( $this->options['taxonomies'] as $tax_key => $tax_chk ) { + if ( $tax_chk == 1 ) { + add_action( $tax_key . '_add_form_fields', array( $this, 'insert_term_meta_add_fields' ) ); + add_action( $tax_key . '_edit_form_fields', array( $this, 'insert_term_meta_edit_fields' ) ); + } + } + } + } + + // カスタムフィールドの入力エリア + 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 = ''; + 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_edit_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_NAME_DESCRIPTION, true ); + } + echo ' + +meta keywords + + + +meta description + + +'; + } + + + function save_terms( $term_id ) { + if ( array_key_exists( MTEE_NAME_KEYWORDS, $_POST ) ) { + update_term_meta( $term_id, MTEE_NAME_KEYWORDS, $_POST[ MTEE_NAME_KEYWORDS ] ); + } + if ( array_key_exists( MTEE_NAME_DESCRIPTION, $_POST ) ) { + update_term_meta( $term_id, MTEE_NAME_DESCRIPTION, $_POST[ MTEE_NAME_DESCRIPTION ] ); + } + } + } + +} \ No newline at end of file diff --git a/class/mtee-version-setting.php b/class/mtee-version-setting.php new file mode 100644 index 0000000..6a1c46d --- /dev/null +++ b/class/mtee-version-setting.php @@ -0,0 +1,36 @@ +options = $options; + $this->change_wp_version_view(); + $this->change_asset_version_view(); + } + + public function change_wp_version_view() { + if ( $this->options['wp_ver_disabled'] == 1 ) { + remove_action( 'wp_head', 'wp_generator' ); + } + } + + public function change_asset_version_view() { + if ( $this->options['asset_ver_disabled'] == 1 ) { + add_action( 'wp_default_scripts', array( $this, 'remove_src_wp_ver' ) ); + add_action( 'wp_default_styles', array( $this, 'remove_src_wp_ver' ) ); + } + } + + function remove_src_wp_ver( $dep ) { + $dep->default_version = ''; + } + + + } +} \ No newline at end of file diff --git a/class/mtee.php b/class/mtee.php new file mode 100644 index 0000000..544ba14 --- /dev/null +++ b/class/mtee.php @@ -0,0 +1,121 @@ + true, '_builtin' => false ) ); + $taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ) ); + + return [ + 'custom_posts' => $custom_posts, + 'taxonomies' => $taxonomies, + ]; + } + + public function get_options() { + return get_option( '_mtee' ); + } + + public function is_enable(): bool { + if ( $this->get_options()['enabled'] == 1 ) { + return true; + } + + return false; + } + + public function is_wp_ver_disable(): bool { + if ( $this->get_options()['wp_ver_disabled'] == 1 ) { + return true; + } + + return false; + } + + public function is_asset_ver_disable(): bool { + if ( $this->get_options()['asset_ver_disabled'] == 1 ) { + return true; + } + + return false; + } + + public function is_noindex_nofollow_disable(): bool { + if ( $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' ) { + 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 new file mode 100644 index 0000000..c79846f --- /dev/null +++ b/config.php @@ -0,0 +1,15 @@ +get_options(); + +// 設定が有効の場合は、各フォームや出力用のクラスを有効にする +if ( $mtee->is_enable() ) { + 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() ) { + 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() ) { + require_once MTEE_CLASS_DIR . 'mtee-version-setting.php'; + new mtee_version_setting( $options ); +} diff --git a/template/success.php b/template/success.php new file mode 100644 index 0000000..e97c95a --- /dev/null +++ b/template/success.php @@ -0,0 +1 @@ +

設定を保存しました

\ No newline at end of file diff --git a/template/top.php b/template/top.php new file mode 100644 index 0000000..c48789e --- /dev/null +++ b/template/top.php @@ -0,0 +1,121 @@ +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(); +?> +
+

+

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

+
+ +
+

キーワード・ディスクリプション

+ +
+ 0 || count( $register_targets['taxonomies'] ) > 0 ): ?> + + 0 ): ?> + + + + + + 0 ): ?> + + + + + +
カスタム投稿 +
    + +
  • + +
  • + +
+
カスタム分類 +
    + +
  • + +
  • + +
+
+ + +
+

個別設定 NOINDEX/NOFOLLOW

+ +
+
+

WordPressバージョン情報

+ +
+
+

CSS / JSバージョン情報

+ +
+

+
+ +
\ No newline at end of file diff --git a/uninstall.php b/uninstall.php new file mode 100644 index 0000000..c6e46c3 --- /dev/null +++ b/uninstall.php @@ -0,0 +1,20 @@ +