@charset "UTF-8";

/*!
Theme Name: Cocoon Child
Description: Cocoon専用の子テーマ
Theme URI: https://wp-cocoon.com/
Author: わいひら
Author URI: https://nelog.jp/
Template:   cocoon-master
Version:    1.1.3
*/

/************************************
** 子テーマ用のスタイルを書く
************************************/
/*必要ならばここにコードを書く*/

/************************************
** レスポンシブデザイン用のメディアクエリ
************************************/
/*1023px以下*/
@media screen and (max-width: 1023px){
  /*必要ならばここにコードを書く*/
}

/*834px以下*/
@media screen and (max-width: 834px){
  /*必要ならばここにコードを書く*/
}

/*480px以下*/
@media screen and (max-width: 480px){
  /*必要ならばここにコードを書く*/
}
/* ==================================================
 管理者除外・外部アクセスのみ・流入元別PVカウンター
 管理者ログイン時だけ画面右下に表示
================================================== */

/* PVカウント */
function ps_external_referrer_counter() {

    if (is_admin()) return;
    if (!is_singular()) return;

    // 管理者ログイン中の自分のアクセスは除外
    if (current_user_can('administrator')) return;

    global $post;
    if (!$post || empty($post->ID)) return;

    $post_id = $post->ID;

    // 流入元判定
    $source = 'direct';

    if (isset($_GET['utm_source']) && $_GET['utm_source'] !== '') {
        $source = sanitize_key($_GET['utm_source']);
    } elseif (isset($_GET['from']) && $_GET['from'] !== '') {
        $source = sanitize_key($_GET['from']);
    } elseif (!empty($_SERVER['HTTP_REFERER'])) {
        $ref = esc_url_raw($_SERVER['HTTP_REFERER']);

        if (strpos($ref, 'instagram.com') !== false) {
            $source = 'instagram';
        } elseif (strpos($ref, 'line.me') !== false || strpos($ref, 'lin.ee') !== false) {
            $source = 'line';
        } elseif (strpos($ref, 'pinterest.') !== false) {
            $source = 'pinterest';
        } elseif (strpos($ref, 'google.') !== false) {
            $source = 'google';
        } elseif (strpos($ref, 'yahoo.') !== false) {
            $source = 'yahoo';
        } elseif (strpos($ref, 'bing.') !== false) {
            $source = 'bing';
        } else {
            $source = 'other';
        }
    }

    // 合計PV
    $total_key = '_ps_total_external_views';
    $total = (int) get_post_meta($post_id, $total_key, true);
    update_post_meta($post_id, $total_key, $total + 1);

    // 流入元別PV
    $source_key = '_ps_source_views_' . $source;
    $source_count = (int) get_post_meta($post_id, $source_key, true);
    update_post_meta($post_id, $source_key, $source_count + 1);
}
add_action('wp', 'ps_external_referrer_counter');


/* 管理者だけにPV表示 */
function ps_admin_only_counter_display() {

    if (!is_singular()) return;
    if (!current_user_can('administrator')) return;

    global $post;
    if (!$post || empty($post->ID)) return;

    $post_id = $post->ID;

    $sources = array(
        'instagram' => 'Instagram',
        'line'      => 'LINE',
        'pinterest' => 'Pinterest',
        'google'    => 'Google',
        'yahoo'     => 'Yahoo',
        'bing'      => 'Bing',
        'direct'    => '直接',
        'other'     => 'その他'
    );

    $total = (int) get_post_meta($post_id, '_ps_total_external_views', true);

    echo '<div style="
        position:fixed;
        right:16px;
        bottom:16px;
        background:#111827;
        color:#ffffff;
        padding:14px 16px;
        border-radius:10px;
        font-size:14px;
        line-height:1.7;
        z-index:999999;
        box-shadow:0 4px 12px rgba(0,0,0,0.25);
        max-width:260px;
    ">';

    echo '<strong>外部PV：' . esc_html($total) . '</strong><br>';

    foreach ($sources as $key => $label) {
        $count = (int) get_post_meta($post_id, '_ps_source_views_' . $key, true);
        echo esc_html($label) . '：' . esc_html($count) . '<br>';
    }

    echo '</div>';
}
add_action('wp_footer', 'ps_admin_only_counter_display');