diff --git a/class/mtee_canonical_setting.php b/class/mtee_canonical_setting.php index 6730c28..09f0efe 100644 --- a/class/mtee_canonical_setting.php +++ b/class/mtee_canonical_setting.php @@ -55,7 +55,9 @@ if (!class_exists('mtee_canonical_setting')) { echo '
- +
'; } @@ -69,6 +71,15 @@ if (!class_exists('mtee_canonical_setting')) { } } + public function set_default_post_canonical_url($id) { + $canonical = get_permalink($id); + global $page, $paged; + if ($paged >= 2 || $page >= 2) { + $canonical .= 'page/' . max($paged, $page) . '/'; + } + return $canonical; + } + //------------------------------------------------------------------------------------------- // タクソノミーのカスタムフィールド設定 //------------------------------------------------------------------------------------------- @@ -108,13 +119,29 @@ if (!class_exists('mtee_canonical_setting')) { public function insert_term_meta_fields($tag, $form) { $canonical_url = ''; + $placeholder = ''; if (isset($tag->term_id)) { $canonical_url = get_term_meta($tag->term_id, MTEE_CANONICAL_URL, true); } - $add_meta = ''; + if (isset($tag->term_id) && empty($canonical_url)) { + $placeholder = $this->set_default_tax_canonical_url($tag->term_id, $tag->taxonomy); + } + $add_meta = ''; return str_replace('##CONTENTS##', $add_meta, $form); } + public function set_default_tax_canonical_url($id, $taxonomy) { + if ($taxonomy == 'category') { + $placeholder = get_category_link($id); + } elseif ($taxonomy == 'post_tag') { + $placeholder = get_tag_link($id); + } else { + $placeholder = get_term_link((int)$id); + } + return $placeholder; + } + 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]); diff --git a/class/mtee_meta_output_canonical.php b/class/mtee_meta_output_canonical.php index 34d104a..e5c7116 100644 --- a/class/mtee_meta_output_canonical.php +++ b/class/mtee_meta_output_canonical.php @@ -115,7 +115,7 @@ if (!class_exists('mtee_meta_output_canonical')) { public function set_tax_canonical($id) { $canonical = get_term_meta($id, MTEE_CANONICAL_URL, true); if (empty($canonical)) { - $canonical = get_term_link($id); + $canonical = get_term_link((int)$id); } return $canonical; } diff --git a/class/mtee_meta_output_keydesc.php b/class/mtee_meta_output_keydesc.php index 1a7bbd8..e775ba4 100644 --- a/class/mtee_meta_output_keydesc.php +++ b/class/mtee_meta_output_keydesc.php @@ -126,7 +126,7 @@ if (!class_exists('mtee_meta_output_keydesc')) { } } - public function create_default_keywords() { + public function create_default_keywords(): array { $type = ''; $title = ''; if (is_category()) { @@ -158,7 +158,7 @@ if (!class_exists('mtee_meta_output_keydesc')) { } - public function get_keywords_data($type, $keyword) { + public function get_keywords_data($type, $keyword): array { $keywords = array(); $keywords[] = $this->site_name; //サイトタイトル if (!empty(get_bloginfo('description'))) { @@ -172,7 +172,7 @@ if (!class_exists('mtee_meta_output_keydesc')) { return array_reverse($keywords); } - public function get_custom_template_keywords_data($custom_keywords_temp, $type, $title) { + public function get_custom_template_keywords_data($custom_keywords_temp, $type, $title): array { $targets = explode(',', $custom_keywords_temp[$type]); $keywords = array(); foreach ($targets as $target) { 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_post_setting.php b/class/mtee_post_setting.php index 89e26ca..ca30761 100644 --- a/class/mtee_post_setting.php +++ b/class/mtee_post_setting.php @@ -1,29 +1,28 @@ 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 __construct($options) { + $this->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, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法) - $custom_posts = array_values(get_post_types(array('public' => true, '_builtin' => false))); - $target_posts = array_merge(array('page', 'post',), $custom_posts); + public function add_meta_fields() { + //add_meta_box(表示される入力ボックスのHTMLのID, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法) + $target_posts = $this->get_all_post_type(); foreach ($target_posts as $target_post) { add_meta_box( 'meta_setting', @@ -35,34 +34,66 @@ if ( ! class_exists( 'mtee_post_setting' ) ) { 'normal' ); } - } + } - // カスタムフィールドの入力エリア - public function insert_meta_fields() { - global $post; - echo '
+ public function get_all_post_type(): 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; + echo '
-
+
'; - 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 ); - } - } - } + // カスタムフィールドの値を保存 + 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); + } + } + + public function set_default_keywords() { + global $post_type; + if ($post_type == 'page') { + if (!empty($this->options['keywords_tmp']['page'])) { + return $this->options['keywords_tmp']['page']; + } + return '##title##,##description##,##site_name##'; + } else { + if (!empty($this->options['keywords_tmp']['post'])) { + return $this->options['keywords_tmp']['post']; + } + return '##title##,##category##,##tag##,##custom_tax##,##description##,##site_name##'; + } + } + + public function set_default_description() { + if (!empty($this->options['description_tmp']['page'])) { + return $this->options['description_tmp']['page']; + } + return '##site_name##の' . MTEE_META_DESC_BEFORE_BRACKETS . '##title##' . MTEE_META_DESC_AFTER_BRACKETS . MTEE_META_DESC_SINGLE_BASE; + } + + } } \ No newline at end of file diff --git a/class/mtee_tax_setting.php b/class/mtee_tax_setting.php index 0aeecbd..6d0511c 100644 --- a/class/mtee_tax_setting.php +++ b/class/mtee_tax_setting.php @@ -39,43 +39,95 @@ if (!class_exists('mtee_tax_setting')) { } 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); - } + $input_values = $this->set_input_values($tag); 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); - } + $input_values = $this->set_input_values($tag); echo ' -meta keywords - - - -meta description - +meta keywords
meta description + + + + '; } + function set_input_values($tag): array { + $key_value = ''; + $desc_value = ''; + $keyword_placeholder = ''; + $desc_placeholder = ''; + 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); + $keyword_placeholder = $this->set_default_keywords($tag); + $desc_placeholder = $this->set_default_description($tag); + } + return array( + 'keywords' => $key_value, + 'desc' => $desc_value, + 'keyword_pl' => $keyword_placeholder, + 'desc_pl' => $desc_placeholder, + ); + } + + public function set_default_keywords($tag) { + if ($tag->taxonomy == 'category') { + $keywords = $this->options['keywords_tmp']['category']; + } elseif ($tag->taxonomy == 'post_tag') { + $keywords = $this->options['keywords_tmp']['tag']; + } else { + $keywords = $this->options['keywords_tmp']['tax']; + } + if (empty($keywords)) { + $keywords = '##title##,##description##,##site_name##'; + } + return $keywords; + } + + public function set_default_description($tag) { + if ($tag->taxonomy == 'category') { + $description = $this->options['description_tmp']['category']; + } elseif ($tag->taxonomy == 'post_tag') { + $description = $this->options['description_tmp']['tag']; + } else { + $description = $this->options['description_tmp']['tax']; + } + if (empty($description)) { + $description = '##site_name##の' . MTEE_META_DESC_BEFORE_BRACKETS . '##title##' . MTEE_META_DESC_AFTER_BRACKETS . MTEE_META_DESC_SINGLE_BASE; + } + return $description; + } function save_terms($term_id) { if (array_key_exists(MTEE_NAME_KEYWORDS, $_POST)) { diff --git a/meta-tag-etc-extend.php b/meta-tag-etc-extend.php index d8ec747..2e51111 100644 --- a/meta-tag-etc-extend.php +++ b/meta-tag-etc-extend.php @@ -35,7 +35,7 @@ if ($mtee->is_enable('enabled')) { 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; + new mtee_meta_output_keydesc(); } // enabled my canonical. caution : default canonical delete @@ -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(); }