@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){
  /*必要ならばここにコードを書く*/
}


/**
 * 特定のポイントサイトから還元額を抽出するショートコード
 * [point_val site="サイト名"] の形式で使用可能
 */
function get_mizuho_rakuten_point($atts) {
    // パラメータのデフォルト設定
    $a = shortcode_atts(array(
        'site' => 'モッピー',
    ), $atts);

    $cache_key = 'mizuho_rakuten_rate_' . md5($a['site']);
    $cached_data = get_transient($cache_key);

    // キャッシュがあればそれを返す（12時間有効）
    if ($cached_data !== false) {
        return $cached_data;
    }

    $url = 'https://yurui-okozukai.com/42974/mizuho-rakutencard/';
    $response = wp_remote_get($url, array('timeout' => 10));

    if (is_wp_error($response)) {
        return '取得エラー';
    }

    $html = wp_remote_retrieve_body($response);
    
    // サイト名とセットになっている「10,000円」のような数値を抽出
    // 例: 「モッピー 10,000円」という並びを探す
    $pattern = '/' . preg_quote($a['site'], '/') . '\s*([\d,]+)円/u';

    if (preg_match($pattern, $html, $matches)) {
        $price = $matches[1] . '円';
        // 12時間キャッシュ（相手サイトへの負荷軽減）
        set_transient($cache_key, $price, 12 * HOUR_IN_SECONDS);
        return $price;
    }

    return '確認中';
}
add_shortcode('point_val', 'get_mizuho_rakuten_point');