/* Plugin Name: DMM作品カタログ生成 Description: キーワードからDMM作品を取得し、WordPress投稿またはMarkdownを生成するツール Version: 2.3 Author: ご主人様 */ add_action('admin_menu', 'dmm_catalog_menu'); function dmm_catalog_menu() { add_menu_page( 'DMMカタログ生成', 'DMMカタログ生成', 'manage_options', 'dmm-catalog-generator', 'dmm_catalog_page', 'dashicons-video-alt2', 20 ); add_submenu_page( 'dmm-catalog-generator', 'API設定', 'API設定', 'manage_options', 'dmm-catalog-settings', 'dmm_catalog_settings_page' ); } function dmm_catalog_settings_page() { if (isset($_POST['save_api_settings'])) { update_option('dmm_api_id', sanitize_text_field($_POST['dmm_api_id'])); update_option('dmm_affiliate_id', sanitize_text_field($_POST['dmm_affiliate_id'])); echo '

設定を保存しました。

'; } $api_id = get_option('dmm_api_id', ''); $affiliate_id = get_option('dmm_affiliate_id', ''); echo '

DMM API設定

'; echo ''; echo ''; echo ''; echo '
API ID
アフィリエイトID

'; } function dmm_catalog_page() { if (isset($_POST['dmm_confirm'])) { $items = json_decode(stripslashes($_POST['items_json']), true); $order = $_POST['order'] ?? []; $ordered = []; foreach ($order as $index => $rank) { if ($rank !== '' && is_numeric($rank)) { $rank = (int)$rank; $ordered[$rank] = $items[$index]; } } ksort($ordered); $ordered_items = array_values($ordered); $search_keyword = sanitize_text_field($_POST['search_keyword']); $output_type = $_POST['output_type']; if ($output_type === 'markdown') { $markdown = generate_dmm_markdown($search_keyword, $ordered_items); echo "

Markdown出力結果

"; echo ""; } else { $post_id = create_dmm_catalog_post($search_keyword, $ordered_items); echo "

✅ 投稿が作成されました → 編集ページを開く

"; } return; } if (isset($_POST['dmm_submit'])) { $search_keyword = sanitize_text_field($_POST['search_keyword']); $output_type = $_POST['output_type']; $sort_order = sanitize_text_field($_POST['sort_order'] ?? 'date'); $floor_key = sanitize_text_field($_POST['floor'] ?? 'videoa'); $hits = max(1, min((int)($_POST['hits'] ?? 10), 20)); $items = get_dmm_items_by_keyword($search_keyword, $sort_order, $floor_key, $hits); if (!is_array($items)) { echo "

エラー:{$items}

"; echo "

"; return; } echo "

取得結果:おすすめ順を入力してください(1〜{$hits}の中で任意)

"; echo "
"; foreach ($items as $index => $item) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
順番タイトル発売日画像
" . esc_html($item['title']) . "" . esc_html($item['date']) . "
"; echo ""; echo ""; echo ""; echo "

"; return; } echo '

DMMカタログ生成

'; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
フロア
並び順
取得件数
出力形式

'; } function get_dmm_items_by_keyword($keyword, $sort = 'date', $floor_key = 'videoa', $hits = 10) { $api_id = get_option('dmm_api_id'); $affiliate_id = get_option('dmm_affiliate_id'); if (!$api_id || !$affiliate_id) { return 'API IDまたはアフィリエイトIDが未設定です(API設定タブで入力してください)。'; } $floor_map = [ 'videoa' => ['service' => 'digital', 'floor' => 'videoa'], 'videoc' => ['service' => 'digital', 'floor' => 'videoc'], 'anime' => ['service' => 'digital', 'floor' => 'anime'], 'digital_doujin' => ['service' => 'doujin', 'floor' => 'digital_doujin'], 'comic' => ['service' => 'ebook', 'floor' => 'comic'] ]; $service = $floor_map[$floor_key]['service'] ?? 'digital'; $floor = $floor_map[$floor_key]['floor'] ?? 'videoa'; $params = [ 'api_id' => $api_id, 'affiliate_id' => $affiliate_id, 'site' => 'FANZA', 'service' => $service, 'floor' => $floor, 'hits' => $hits, 'sort' => $sort, 'keyword' => $keyword, 'output' => 'json' ]; $url = 'https://api.dmm.com/affiliate/v3/ItemList?' . http_build_query($params); $response = wp_remote_get($url); if (is_wp_error($response)) return 'APIエラー:' . $response->get_error_message(); $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if (empty($data['result']['items'])) return '該当する作品が見つかりませんでした。'; $output = []; foreach ($data['result']['items'] as $item) { $image_data = $item['imageURL'] ?? []; $image_url = $image_data['large'] ?? $image_data['small'] ?? $image_data['list'] ?? ''; $image_url = preg_replace('/^http:/', 'https:', $image_url); $output[] = [ 'title' => $item['title'], 'url' => $item['affiliateURL'], 'image' => $image_url, 'date' => $item['date'], 'review' => $item['review']['average'] ?? null ]; } return $output; } //Markdown function generate_dmm_markdown($actress_name, $items) { $md = "## {$actress_name}の出演作品一覧(おすすめ" . count($items) . "選)\n\n"; $md .= "今回は、AV女優「{$actress_name}」さんのおすすめ出演作品をまとめてご紹介します。\n\n"; $md .= "---\n\n"; foreach ($items as $i => $item) { $md .= "### " . ($i + 1) . ". " . $item['title'] . "\n\n"; if (!empty($item['image'])) { $md .= "![作品画像](" . $item['image'] . ")\n\n"; } $md .= "- 📅 発売日:" . $item['date'] . "\n"; if (!empty($item['review'])) { $md .= "- ⭐ 平均レビュー:" . $item['review'] . "\n"; } else { $md .= "- ⭐ レビューなし\n"; } $md .= "- 👉 [詳細ページはこちら](" . $item['url'] . ")\n\n"; $md .= "---\n\n"; } return $md; } //Post function create_dmm_catalog_post($actress_name, $items) { $title = $actress_name . 'の出演作品カタログ'; $content = "

{$actress_name}の出演作品一覧(おすすめ" . count($items) . "選)

\n"; $content .= "

今回は、AV女優「{$actress_name}」さんのおすすめ出演作品をまとめてご紹介します。

\n
\n"; foreach ($items as $i => $item) { $content .= "
\n"; $content .= "

" . ($i + 1) . ". " . esc_html($item['title']) . "

\n"; $content .= "\n"; if (!empty($item['image'])) { $content .= "\""\n"; } else { $content .= "(画像なし)\n"; } $content .= "\n"; $content .= "

▶ 詳細ページを見る

\n"; $content .= "
\n
\n"; } $post_data = array( 'post_title' => $title, 'post_content' => $content, 'post_status' => 'draft', 'post_author' => get_current_user_id(), 'tags_input' => array($actress_name) ); $post_id = wp_insert_post($post_data); if ($post_id && !is_wp_error($post_id)) { set_post_thumbnail_from_url($post_id, $items[0]['image']); } return $post_id; } function set_post_thumbnail_from_url($post_id, $image_url) { require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); if (empty($image_url)) return; $image_url = preg_replace('/^http:/', 'https:', $image_url); $tmp = download_url($image_url); if (is_wp_error($tmp)) { error_log("画像ダウンロードエラー: " . $tmp->get_error_message()); return; } $file_array = array( 'name' => basename(parse_url($image_url, PHP_URL_PATH)), 'tmp_name' => $tmp ); $id = media_handle_sideload($file_array, $post_id); if (!is_wp_error($id)) { set_post_thumbnail($post_id, $id); } else { error_log("アイキャッチ登録エラー: " . $id->get_error_message()); } }
Warning: Cannot modify header information - headers already sent by (output started at /home/wp604130/imemani.com/public_html/wp-content/plugins/dmm-catalog-plugin_v2/dmm-catalog-plugin.php:1) in /home/wp604130/imemani.com/public_html/wp-includes/pluggable.php on line 1450

Warning: Cannot modify header information - headers already sent by (output started at /home/wp604130/imemani.com/public_html/wp-content/plugins/dmm-catalog-plugin_v2/dmm-catalog-plugin.php:1) in /home/wp604130/imemani.com/public_html/wp-includes/pluggable.php on line 1453