From 0a00caef2144aba031091325f62ac46eb577d24f Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 30 May 2021 19:00:09 +0900 Subject: [PATCH] =?UTF-8?q?WP=20PLUGIN=20MTEE(Meta=20Tag=20etc=20Extend)?= =?UTF-8?q?=20OGP=E3=82=BF=E3=82=B0=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・タクソノミーのOGPタグ設定 ・画像アップロード機能の追加 --- class/mtee_ogp_post_setting.php | 89 ++++++++++++- class/mtee_ogp_tax_setting.php | 213 ++++++++++++++++++++++++++++++++ class/mtee_tax_setting.php | 20 +-- meta-tag-etc-extend.php | 2 + 4 files changed, 312 insertions(+), 12 deletions(-) create mode 100644 class/mtee_ogp_tax_setting.php diff --git a/class/mtee_ogp_post_setting.php b/class/mtee_ogp_post_setting.php index 5e3b8d4..7179419 100644 --- a/class/mtee_ogp_post_setting.php +++ b/class/mtee_ogp_post_setting.php @@ -15,7 +15,7 @@ if (!class_exists('mtee_ogp_post_setting')) { public function __construct($options) { $this->options = $options; add_action('admin_menu', array($this, 'add_meta_fields')); - //画像をアップする場合は、multipart/form-dataの設定が必要なので、post_edit_form_tagをフックしてformタグに追加 + //post_edit_form_tagをフックしてformタグにmultipart/form-data属性を追加 add_action('post_edit_form_tag', array($this, 'custom_meta_box_edit_form_tag')); add_action('save_post', array($this, 'save_meta_fields')); } @@ -47,23 +47,48 @@ if (!class_exists('mtee_ogp_post_setting')) { echo '
- +
- +
- + +

' . $this->set_ogp_thumb($post->ID) . '

'; } + + public function set_default_ogp_title() { + return get_the_title(); + } + + public function set_default_ogp_desc(): string { + return get_bloginfo('name') . 'の' . $this->set_default_ogp_title() . 'です。'; + } + + + public function set_ogp_thumb($id): string { + $ogp_thumb = get_post_meta($id, MTEE_OGP_IMG, true); + if (!empty($ogp_thumb)) { + return ''; + } + return ''; + } + // カスタムフィールドの値を保存 public function save_meta_fields($post_id) { if (!empty($_POST[MTEE_OGP_TITLE])) { //meta_keywordsが入力されている場合 @@ -76,6 +101,62 @@ if (!class_exists('mtee_ogp_post_setting')) { } else { delete_post_meta($post_id, MTEE_OGP_DESC); } + if (isset($_FILES[MTEE_OGP_IMG]) && $_FILES[MTEE_OGP_IMG]['size'] !== 0) { + $this->save_ogp_image('post', $post_id); + } + } + + public function save_ogp_image($type, $id) { + $file_name = $this->set_ogp_filename(); + $wp_upload_dir = wp_upload_dir(); //現在のuploadディクレトリのパスとURLを入れた配列 + $upload_file = $_FILES[MTEE_OGP_IMG]['tmp_name']; + $upload_path = $wp_upload_dir['path'] . '/' . $file_name; + //画像ファイルをuploadディクレトリに移動させる + move_uploaded_file($upload_file, $upload_path); + + $this->upload_ogp_file($type, $upload_path, $wp_upload_dir, $file_name, $id); + } + + public function set_ogp_filename() { + $file_name = basename($_FILES[MTEE_OGP_IMG]['name']); + $file_name = trim($file_name); + return str_replace(' ', '-', $file_name); + } + + public function upload_ogp_file($type, $upload_path, $wp_upload_dir, $file_name, $id) { + if (file_exists($upload_path)) { + //添付ファイルを追加 + $attachment = $this->set_upload_file_param($wp_upload_dir, $file_name, $upload_path); + $attach_id = wp_insert_attachment($attachment, $upload_path, $id); + if (!function_exists('wp_generate_attachment_metadata')) { + require_once(ABSPATH . "wp-admin" . '/includes/image.php'); + } + //添付ファイルのメタデータを生成し保存 + $this->save_ogp_file($type, $attach_id, $upload_path, $id); + } else { + //保存失敗 + echo '画像保存に失敗しました'; + exit; + } + } + + public function set_upload_file_param($wp_upload_dir, $file_name, $upload_path): array { + return array( + 'guid' => $wp_upload_dir['url'] . '/' . basename($file_name), + 'post_mime_type' => $_FILES[MTEE_OGP_IMG]['type'], + //正規表現で拡張子なしのスラッグ名を生成 + 'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload_path)), + 'post_content' => '', + 'post_status' => 'inherit' + ); + } + + public function save_ogp_file($type, $attach_id, $upload_path, $id) { + $attach_data = wp_generate_attachment_metadata($attach_id, $upload_path); + wp_update_attachment_metadata($attach_id, $attach_data); + if ($type == 'post') { + update_post_meta($id, MTEE_OGP_IMG, $attach_id); + } } } diff --git a/class/mtee_ogp_tax_setting.php b/class/mtee_ogp_tax_setting.php new file mode 100644 index 0000000..ce0af87 --- /dev/null +++ b/class/mtee_ogp_tax_setting.php @@ -0,0 +1,213 @@ +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() { + $taxs = $this->set_tax_types(); + foreach ($taxs as $tax) { + add_action($tax . '_add_form_fields', array($this, 'insert_term_meta_add_fields')); + add_action($tax . '_edit_form_fields', array($this, 'insert_term_meta_edit_fields')); + //formタグにmultipart/form-data属性を追加 + add_action($tax . '_term_edit_form_tag', array($this, 'custom_meta_box_edit_form_tag')); + add_action($tax . '_add_form', array($this, 'create_form_tag')); + } + } + + function create_form_tag() { + ?> + + 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; + } + + function insert_term_meta_add_fields($tag) { + $input_values = $this->set_input_values($tag); + echo ' +
+OGP title
+ +
+
+OGP description
+ + +
+'; + } + + function insert_term_meta_edit_fields($tag) { + $input_values = $this->set_input_values($tag); + echo ' + +OGP + + + + +

' . $this->set_ogp_thumb($tag->term_id) . '

+ + +'; + } + + function set_input_values($tag): array { + $title_value = ''; + $desc_value = ''; + $title_placeholder = ''; + $desc_placeholder = ''; + if (isset($tag->term_id)) { + $title_value = get_term_meta($tag->term_id, MTEE_OGP_TITLE, true); + $desc_value = get_term_meta($tag->term_id, MTEE_OGP_DESC, true); + $title_placeholder = $this->set_default_title($tag); + $desc_placeholder = $this->set_default_description($title_placeholder); + } + return array( + 'title' => $title_value, + 'desc' => $desc_value, + 'title_pl' => $title_placeholder, + 'desc_pl' => $desc_placeholder, + ); + } + + public function set_default_title($tag) { + return $tag->name; + } + + public function set_default_description($title): string { + return get_bloginfo('name') . 'の' . $title . 'です。'; + } + + public function set_ogp_thumb($id): string { + $ogp_thumb = get_term_meta($id, MTEE_OGP_IMG, true); + if (!empty($ogp_thumb)) { + return ''; + } + return ''; + } + + function save_terms($term_id) { + if (array_key_exists(MTEE_OGP_TITLE, $_POST)) { + update_term_meta($term_id, MTEE_OGP_TITLE, $_POST[MTEE_OGP_TITLE]); + } + if (array_key_exists(MTEE_OGP_DESC, $_POST)) { + update_term_meta($term_id, MTEE_OGP_DESC, $_POST[MTEE_OGP_DESC]); + } + if (isset($_FILES[MTEE_OGP_IMG]) && $_FILES[MTEE_OGP_IMG]['size'] !== 0) { + $this->save_ogp_image('term', $term_id); + } + + } + + public function save_ogp_image($type, $id) { + $file_name = $this->set_ogp_filename(); + $wp_upload_dir = wp_upload_dir(); //現在のuploadディクレトリのパスとURLを入れた配列 + $upload_file = $_FILES[MTEE_OGP_IMG]['tmp_name']; + $upload_path = $wp_upload_dir['path'] . '/' . $file_name; + //画像ファイルをuploadディクレトリに移動させる + move_uploaded_file($upload_file, $upload_path); + + $this->upload_ogp_file($type, $upload_path, $wp_upload_dir, $file_name, $id); + } + + public function set_ogp_filename() { + $file_name = basename($_FILES[MTEE_OGP_IMG]['name']); + $file_name = trim($file_name); + return str_replace(' ', '-', $file_name); + } + + public function upload_ogp_file($type, $upload_path, $wp_upload_dir, $file_name, $id) { + if (file_exists($upload_path)) { + //添付ファイルを追加 + $attachment = $this->set_upload_file_param($wp_upload_dir, $file_name, $upload_path); + $attach_id = wp_insert_attachment($attachment, $upload_path, $id); + if (!function_exists('wp_generate_attachment_metadata')) { + require_once(ABSPATH . "wp-admin" . '/includes/image.php'); + } + //添付ファイルのメタデータを生成し保存 + $this->save_ogp_file($type, $attach_id, $upload_path, $id); + } else { + //保存失敗 + echo '画像保存に失敗しました'; + exit; + } + } + + public function set_upload_file_param($wp_upload_dir, $file_name, $upload_path): array { + return array( + 'guid' => $wp_upload_dir['url'] . '/' . basename($file_name), + 'post_mime_type' => $_FILES[MTEE_OGP_IMG]['type'], + //正規表現で拡張子なしのスラッグ名を生成 + 'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload_path)), + 'post_content' => '', + 'post_status' => 'inherit' + ); + } + + public function save_ogp_file($type, $attach_id, $upload_path, $id) { + $attach_data = wp_generate_attachment_metadata($attach_id, $upload_path); + wp_update_attachment_metadata($attach_id, $attach_data); + if ($type == 'post') { + update_post_meta($id, MTEE_OGP_IMG, $attach_id); + } elseif ($type == 'term') { + update_term_meta($id, MTEE_OGP_IMG, $attach_id); + } + } + + } + +} \ No newline at end of file diff --git a/class/mtee_tax_setting.php b/class/mtee_tax_setting.php index 531d996..6d0511c 100644 --- a/class/mtee_tax_setting.php +++ b/class/mtee_tax_setting.php @@ -62,18 +62,22 @@ class="tax-meta-field" /> $input_values = $this->set_input_values($tag); echo ' -meta keywords -meta keywords
meta description + + + + '; } diff --git a/meta-tag-etc-extend.php b/meta-tag-etc-extend.php index 06de3b6..2e51111 100644 --- a/meta-tag-etc-extend.php +++ b/meta-tag-etc-extend.php @@ -49,8 +49,10 @@ if ($mtee->is_enable('canonical_setting')) { // enabled my OGP tag if ($mtee->is_enable('ogp_setting')) { + require_once MTEE_CLASS_DIR . 'mtee_ogp_tax_setting.php'; require_once MTEE_CLASS_DIR . 'mtee_ogp_post_setting.php'; require_once MTEE_CLASS_DIR . 'mtee_meta_output_ogp.php'; + new mtee_ogp_tax_setting($options); new mtee_ogp_post_setting($options); new mtee_meta_output_ogp(); }