@charset "UTF-8";
/*
    Template: swell
    Theme Name: SWELL CHILD
    Theme URI: https://swell-theme.com/
    Description: SWELLの子テーマ
    Version: 1.0.0
    Author: LOOS WEB STUDIO
    Author URI: https://loos-web-studio.com/

    License: GNU General Public License
    License URI: http://www.gnu.org/licenses/gpl.html
*/
/**
 * FANZAレビューサイト SEO強化コード（SWELL用）
 * functions.php の末尾に追加してください
 */

// ========================================
// 1. meta description の自動生成
// ========================================
function fanza_meta_description() {
    if ( is_single() || is_page() ) {
        if ( has_excerpt() ) {
            $description = wp_strip_all_tags( get_the_excerpt() );
        } else {
            $description = wp_trim_words( wp_strip_all_tags( get_the_content() ), 30, '' );
        }
    } elseif ( is_category() ) {
        $description = wp_strip_all_tags( category_description() );
        if ( empty( $description ) ) {
            $description = single_cat_title( '', false ) . 'の人気エロ動画一覧。評価・レビュー順で厳選した作品をご紹介。';
        }
    } elseif ( is_tag() ) {
        $description = single_tag_title( '', false ) . 'タグのエロ動画一覧。FANZAの人気作品を評価順で掲載。';
    } else {
        $description = 'FANZAの人気エロ動画をレビュー評価順で紹介！毎日更新の最新ランキングで、今話題の作品をチェック。';
    }

    $description = esc_attr( mb_substr( trim( $description ), 0, 120 ) );
    echo '<meta name="description" content="' . $description . '">' . "\n";
}
add_action( 'wp_head', 'fanza_meta_description', 1 );


// ========================================
// 2. OGP タグ（SNSシェア対応）
// ========================================
function fanza_ogp_tags() {
    global $post;
    $site_name = get_bloginfo( 'name' );
    $site_url  = home_url();

    if ( is_single() || is_page() ) {
        $title       = get_the_title();
        $url         = get_permalink();
        $description = has_excerpt()
            ? wp_strip_all_tags( get_the_excerpt() )
            : wp_trim_words( wp_strip_all_tags( get_the_content() ), 30, '' );
        $description = esc_attr( mb_substr( trim( $description ), 0, 120 ) );
        $image       = has_post_thumbnail()
            ? get_the_post_thumbnail_url( $post->ID, 'large' )
            : $site_url . '/wp-content/uploads/ogp-default.jpg';
        $type        = 'article';
    } else {
        $title       = get_bloginfo( 'name' );
        $url         = $site_url . '/';
        $description = 'FANZAの人気エロ動画をレビュー評価順で紹介！毎日更新の最新ランキングで、今話題の作品をチェック。';
        $image       = $site_url . '/wp-content/uploads/ogp-default.jpg';
        $type        = 'website';
    }

    echo '<meta property="og:type"        content="' . esc_attr( $type )        . '">' . "\n";
    echo '<meta property="og:title"       content="' . esc_attr( $title )       . '">' . "\n";
    echo '<meta property="og:description" content="' . esc_attr( $description ) . '">' . "\n";
    echo '<meta property="og:url"         content="' . esc_url( $url )          . '">' . "\n";
    echo '<meta property="og:image"       content="' . esc_url( $image )        . '">' . "\n";
    echo '<meta property="og:site_name"   content="' . esc_attr( $site_name )   . '">' . "\n";
    echo '<meta name="twitter:card"       content="summary_large_image">'               . "\n";
}
add_action( 'wp_head', 'fanza_ogp_tags', 2 );


// ========================================
// 3. canonical URL の設定
// ========================================
function fanza_canonical_url() {
    if ( is_singular() ) {
        $url = get_permalink();
    } elseif ( is_category() ) {
        $url = get_category_link( get_queried_object_id() );
    } elseif ( is_tag() ) {
        $url = get_tag_link( get_queried_object_id() );
    } elseif ( is_home() || is_front_page() ) {
        $url = home_url( '/' );
    } else {
        return;
    }
    echo '<link rel="canonical" href="' . esc_url( $url ) . '">' . "\n";
}
add_action( 'wp_head', 'fanza_canonical_url', 3 );


// ========================================
// 4. 構造化データ（JSON-LD）
// ========================================
function fanza_structured_data() {
    if ( is_single() ) {
        // パンくずリスト
        $categories = get_the_category();
        $items = array(
            array( '@type' => 'ListItem', 'position' => 1, 'name' => 'ホーム', 'item' => home_url('/') ),
        );
        if ( ! empty( $categories ) ) {
            $items[] = array(
                '@type'    => 'ListItem',
                'position' => 2,
                'name'     => esc_html( $categories[0]->name ),
                'item'     => esc_url( get_category_link( $categories[0]->term_id ) ),
            );
            $items[] = array(
                '@type'    => 'ListItem',
                'position' => 3,
                'name'     => esc_html( get_the_title() ),
                'item'     => esc_url( get_permalink() ),
            );
        }
        $breadcrumb = array(
            '@context'        => 'https://schema.org',
            '@type'           => 'BreadcrumbList',
            'itemListElement' => $items,
        );
        echo '<script type="application/ld+json">' . wp_json_encode( $breadcrumb, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) . '</script>' . "\n";

        // レビュー記事の構造化データ
        $review = array(
            '@context'    => 'https://schema.org',
            '@type'       => 'Review',
            'name'        => esc_html( get_the_title() ),
            'url'         => esc_url( get_permalink() ),
            'datePublished' => get_the_date( 'c' ),
            'dateModified'  => get_the_modified_date( 'c' ),
            'author'      => array(
                '@type' => 'Organization',
                'name'  => esc_html( get_bloginfo( 'name' ) ),
            ),
        );
        echo '<script type="application/ld+json">' . wp_json_encode( $review, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) . '</script>' . "\n";
    }
}
add_action( 'wp_head', 'fanza_structured_data', 4 );


// ========================================
// 5. 自動タイトル最適化（SWELL用）
// ========================================
function fanza_optimize_title( $title ) {
    if ( is_category() ) {
        $title['title'] = single_cat_title( '', false ) . '動画一覧 | ' . get_bloginfo( 'name' );
    } elseif ( is_tag() ) {
        $title['title'] = single_tag_title( '', false ) . 'のエロ動画 | ' . get_bloginfo( 'name' );
    }
    return $title;
}
add_filter( 'document_title_parts', 'fanza_optimize_title' );
/**
 * FANZAアフィリエイト ボタンショートコード＆テンプレート
 * functions.php の末尾に追加してください
 *
 * ★ YOUR-AFFILIATE-ID の部分を自分のアフィリエイトIDに変更してください
 *   例: yourname-990
 */

if ( ! defined( 'FANZA_AFFILIATE_ID' ) ) {
    define( 'FANZA_AFFILIATE_ID', 'sloby-990' );
}

// ========================================
// ショートコード: [fanza_btn url="商品URL"]
// 使い方: 記事内に [fanza_btn url="https://www.dmm.co.jp/..."] と書くだけ
// ========================================
if ( ! function_exists( 'fanza_button_shortcode' ) ) :
function fanza_button_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'url'   => '',
        'label' => '▶ FANZAで視聴する',
    ), $atts );

    if ( empty( $atts['url'] ) ) {
        return '';
    }

    $affiliate_url = 'https://al.dmm.co.jp/?lurl=' . urlencode( $atts['url'] )
        . '&af_id=' . FANZA_AFFILIATE_ID
        . '&ch=toolbar&ch_id=link';

    return '<div class="fanza-btn-wrap">
        <a href="' . esc_url( $affiliate_url ) . '" target="_blank" rel="nofollow noopener" class="fanza-btn">
            ' . esc_html( $atts['label'] ) . '
        </a>
    </div>';
}
add_shortcode( 'fanza_btn', 'fanza_button_shortcode' );
endif;


// ========================================
// ボタンのCSS（追加CSSに入れても可）
// ========================================
if ( ! function_exists( 'fanza_button_style' ) ) :
function fanza_button_style() {
    echo '<style>
    .fanza-btn-wrap {
        text-align: center;
        margin: 20px 0;
    }
    .fanza-btn {
        display: inline-block;
        background: linear-gradient(135deg, #e94560, #c0392b);
        color: #ffffff !important;
        font-size: 1rem;
        font-weight: 700;
        padding: 14px 40px;
        border-radius: 50px;
        text-decoration: none !important;
        letter-spacing: 0.05em;
        box-shadow: 0 4px 12px rgba(233,69,96,0.4);
        transition: transform 0.2s, box-shadow 0.2s;
    }
    .fanza-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 18px rgba(233,69,96,0.5);
        color: #ffffff !important;
    }
    </style>' . "\n";
}
add_action( 'wp_head', 'fanza_button_style' );
endif;
