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

/* =====================  ここから  ===================== */


/* ============================================================
 * 1. 公開日・最終更新日の表示
 *    鮮度はE-E-A-Tに効くため、更新したことが読者に見える状態にする
 * ============================================================ */
function erabai_show_dates( $content ) {
	if ( is_singular( 'post' ) && in_the_loop() && is_main_query() ) {
		$published = get_the_date( 'Y年n月j日' );
		$modified  = get_the_modified_date( 'Y年n月j日' );

		$html = '<p class="erabai-dates">公開日：' . esc_html( $published );
		if ( $modified !== $published ) {
			$html .= '<span class="erabai-dates-sep">/</span>最終更新日：' . esc_html( $modified );
		}
		$html .= '</p>';

		return $html . $content;
	}
	return $content;
}
add_filter( 'the_content', 'erabai_show_dates', 4 );


/* ============================================================
 * 2. PR表記の自動挿入（景品表示法・ステマ規制対応）
 *    全記事のファーストビューに表示される。本文と同等の視認性を確保すること。
 *    ※priority 5 = 日付表示(4)より後に前置されるので、最終的に最上部へ出る
 * ============================================================ */
function erabai_pr_disclosure( $content ) {
	if ( is_singular( 'post' ) && in_the_loop() && is_main_query() ) {
		$notice = '<p class="erabai-pr">本ページはプロモーション（アフィリエイト広告）が含まれています。</p>';
		return $notice . $content;
	}
	return $content;
}
add_filter( 'the_content', 'erabai_pr_disclosure', 5 );


/* ============================================================
 * 3. 比較表を横スクロール対応にする
 *    ※Cocoonの「スクロール可能な表」機能を使う場合はこのブロックを削除してよい
 * ============================================================ */
function erabai_wrap_tables( $content ) {
	if ( is_singular() && in_the_loop() && is_main_query() ) {
		if ( strpos( $content, '<table' ) !== false ) {
			$content = str_replace( '<table', '<div class="erabai-table-scroll"><table', $content );
			$content = str_replace( '</table>', '</table></div>', $content );
		}
	}
	return $content;
}
add_filter( 'the_content', 'erabai_wrap_tables', 20 );


/* ============================================================
 * 4. ERABAI評価スコア表示ショートコード
 *
 * 【使い方】
 * [erabai_score name="ツール名" output="4" time="5" ja="4" price="3"
 *               ui="4" free="2" support="3" cancel="4" date="2026年9月1日"]
 *
 * 【引数】各軸は 1〜5（小数可）。未検証の軸は書かない（空欄にする）。
 *   output  = 出力品質       (重み25%)
 *   time    = 時短効果       (重み20%)
 *   ja      = 日本語品質     (重み15%)
 *   price   = 料金妥当性     (重み15%)
 *   ui      = 操作性         (重み10%)
 *   free    = 無料版の実用度 (重み 5%)
 *   support = サポート       (重み 5%)
 *   cancel  = 解約しやすさ   (重み 5%)
 *
 * 【仕様】8軸すべてが埋まっているときだけ総合スコアを算出する。
 *         1つでも未検証があれば「検証中」と表示する。
 *         ＝推定値でデータベースを汚さないための安全装置。
 * ============================================================ */
function erabai_score_shortcode( $atts ) {

	$a = shortcode_atts(
		array(
			'name'    => '',
			'output'  => '',
			'time'    => '',
			'ja'      => '',
			'price'   => '',
			'ui'      => '',
			'free'    => '',
			'support' => '',
			'cancel'  => '',
			'date'    => '',
			'link'    => '/verification-method/',
		),
		$atts,
		'erabai_score'
	);

	$axes = array(
		'output'  => array( '出力品質', 25 ),
		'time'    => array( '時短効果', 20 ),
		'ja'      => array( '日本語品質', 15 ),
		'price'   => array( '料金妥当性', 15 ),
		'ui'      => array( '操作性', 10 ),
		'free'    => array( '無料版の実用度', 5 ),
		'support' => array( 'サポート', 5 ),
		'cancel'  => array( '解約しやすさ', 5 ),
	);

	$total      = 0;
	$weight_sum = 0;
	$rows       = '';

	foreach ( $axes as $key => $axis ) {
		$label  = $axis[0];
		$weight = $axis[1];
		$raw    = trim( (string) $a[ $key ] );

		if ( '' === $raw || ! is_numeric( $raw ) ) {
			$rows .= '<tr><th scope="row">' . esc_html( $label ) . '</th>'
				. '<td class="erabai-unverified" colspan="2">未検証</td></tr>';
			continue;
		}

		$score = (float) $raw;
		if ( $score > 5 ) {
			$score = 5;
		}
		if ( $score < 1 ) {
			$score = 1;
		}

		$total      += $score * $weight;
		$weight_sum += $weight;
		$pct         = ( $score / 5 ) * 100;

		$rows .= '<tr><th scope="row">' . esc_html( $label ) . '</th>'
			. '<td class="erabai-bar-cell"><span class="erabai-bar" style="width:' . esc_attr( round( $pct, 1 ) ) . '%"></span></td>'
			. '<td class="erabai-num">' . esc_html( number_format( $score, 1 ) ) . '</td></tr>';
	}

	if ( 100 === $weight_sum ) {
		$overall      = number_format( $total / 100, 1 );
		$overall_html = '<div class="erabai-total">'
			. '<span class="erabai-total-label">総合スコア</span>'
			. '<span class="erabai-total-num">' . esc_html( $overall ) . '</span>'
			. '<span class="erabai-total-max">/ 5.0</span></div>';
	} else {
		$overall_html = '<div class="erabai-total erabai-total-pending">'
			. '<span class="erabai-total-label">総合スコア</span>'
			. '<span class="erabai-total-num">検証中</span></div>';
	}

	$head = '';
	if ( '' !== $a['name'] ) {
		$head = '<div class="erabai-score-name">' . esc_html( $a['name'] ) . '</div>';
	}

	$foot = '';
	if ( '' !== $a['date'] ) {
		$foot = '<p class="erabai-score-date">検証日：' . esc_html( $a['date'] )
			. '<span class="erabai-dates-sep">/</span>'
			. '<a href="' . esc_url( $a['link'] ) . '">評価基準について</a></p>';
	}

	return '<div class="erabai-score">' . $head . $overall_html
		. '<table class="erabai-score-table"><tbody>' . $rows . '</tbody></table>'
		. $foot . '</div>';
}
add_shortcode( 'erabai_score', 'erabai_score_shortcode' );


/* ============================================================
 * 5. 「編集者への確認事項」を本番で表示しないための保険
 *    本文中に <!--erabai-internal--> 以降を書いた場合、公開画面では出力しない。
 *    校正メモを本文に残したまま公開してしまう事故を防ぐ。
 * ============================================================ */
function erabai_strip_internal_notes( $content ) {
	$marker = '<!--erabai-internal-->';
	if ( strpos( $content, $marker ) !== false ) {
		$parts   = explode( $marker, $content );
		$content = $parts[0];
	}
	return $content;
}
add_filter( 'the_content', 'erabai_strip_internal_notes', 1 );


/* =====================  ここまで  ===================== */
