@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){
  /*必要ならばここにコードを書く*/
}
// 往年のWeb拍手ショートコード [web_clap]（ページ移動なし・記事別カウント）
add_shortcode('web_clap', function() {
  if ( !is_single() ) {
    return '';
  }
  $post_id = get_the_ID();
  $count   = (int) get_post_meta($post_id, '_hamidashi_clap_count', true);
  $nonce   = wp_create_nonce('hamidashi_clap_' . $post_id);
  $ajaxurl = admin_url('admin-ajax.php');

  ob_start();
  ?>
  <div class="web-clap-box" id="web-clap-<?php echo esc_attr($post_id); ?>">
    <button type="button" class="web-clap-btn" data-id="<?php echo esc_attr($post_id); ?>" data-nonce="<?php echo esc_attr($nonce); ?>">
      Web拍手を送る（<span class="web-clap-num"><?php echo esc_html($count); ?></span>）
    </button>
    <div class="web-clap-msg" aria-live="polite"></div>
  </div>
  <script>
  (function(){
    var box = document.getElementById('web-clap-<?php echo (int) $post_id; ?>');
    if (!box) return;
    var btn = box.querySelector('.web-clap-btn');
    var num = box.querySelector('.web-clap-num');
    var msg = box.querySelector('.web-clap-msg');
    btn.addEventListener('click', function(){
      if (btn.disabled) return;
      btn.disabled = true;
      var body = new URLSearchParams();
      body.append('action', 'hamidashi_send_clap');
      body.append('post_id', btn.getAttribute('data-id'));
      body.append('nonce', btn.getAttribute('data-nonce'));
      fetch('<?php echo esc_url($ajaxurl); ?>', {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: body.toString()
      })
      .then(function(r){ return r.json(); })
      .then(function(res){
        if (res && res.success) {
          num.textContent = res.data.count;
          msg.textContent = res.data.message;
        }
        btn.disabled = false;
      })
      .catch(function(){ btn.disabled = false; });
    });
  })();
  </script>
  <?php
  return ob_get_clean();
});

add_action('wp_ajax_hamidashi_send_clap', 'hamidashi_handle_send_clap');
add_action('wp_ajax_nopriv_hamidashi_send_clap', 'hamidashi_handle_send_clap');
function hamidashi_handle_send_clap() {
  $post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0;
  $nonce   = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
  if ( !$post_id || !wp_verify_nonce($nonce, 'hamidashi_clap_' . $post_id) ) {
    wp_send_json_error();
  }
  $count = (int) get_post_meta($post_id, '_hamidashi_clap_count', true) + 1;
  update_post_meta($post_id, '_hamidashi_clap_count', $count);

  $messages = array(
    'パチパチ！拍手ありがとうございます。',
    '読んでくださってありがとうございます！',
    '励みになります。ゆっくりしていってくださいね。',
  );
  $msg = $messages[array_rand($messages)];
  wp_send_json_success(array('count' => $count, 'message' => $msg));
}