
// === CABABOOST 文脈CTA 位置制御 ここから ===
// 記事メタ cababoost_cta_pos で、文脈CTAの挿入位置を記事ごとに選べるようにする。
//   ''（既定・全記事）… 従来どおり本文の直後（＝ページ最下部）。挙動は一切変わらない
//   'h3:3' / 'h2:2' / 'h:4' … その見出しの直前に挿入する（h は h1-h6 のいずれか）
// Elementor記事では見出しがウィジェットdivに包まれているため、直近の
// <div class="elementor-element ..."> まで遡って「ウィジェットの直前」に差し込む
// （＝完全な要素の境界に入れるのでDOMの入れ子は壊れない）。
// 位置を解決できない場合は必ず末尾へフォールバックする（CTAを落とさない）。
add_action( 'init', function () {
	register_post_meta( 'post', 'cababoost_cta_pos', array(
		'show_in_rest'      => true,
		'single'            => true,
		'type'              => 'string',
		'default'           => '',
		'sanitize_callback' => 'cababoost_cta_pos_sanitize',
		'auth_callback'     => function ( $allowed, $meta_key, $post_id ) {
			return current_user_can( 'edit_post', $post_id );
		},
	) );
} );

function cababoost_cta_pos_sanitize( $value ) {
	$value = is_string( $value ) ? trim( $value ) : '';
	if ( '' === $value ) {
		return '';
	}
	return preg_match( '/^h[1-6]?:[1-9][0-9]?$/', $value ) ? $value : '';
}

function cababoost_cta_place( $content, $cta, $post_id ) {
	$pos = cababoost_cta_pos_sanitize( get_post_meta( $post_id, 'cababoost_cta_pos', true ) );
	if ( '' === $pos ) {
		return $content . "\n" . $cta;
	}
	$parts = explode( ':', $pos );
	$tag   = $parts[0];
	$nth   = (int) $parts[1];
	$pattern = ( 'h' === $tag ) ? '/<h[1-6]\b[^>]*>/i' : '/<' . $tag . '\b[^>]*>/i';
	if ( ! preg_match_all( $pattern, $content, $m, PREG_OFFSET_CAPTURE ) ) {
		return $content . "\n" . $cta;
	}
	if ( count( $m[0] ) < $nth ) {
		return $content . "\n" . $cta;
	}
	$at   = $m[0][ $nth - 1 ][1];
	$wrap = strrpos( substr( $content, 0, $at ), '<div class="elementor-element' );
	if ( false !== $wrap && ( $at - $wrap ) <= 1500 ) {
		$at = $wrap;
	}
	return substr( $content, 0, $at ) . "\n" . $cta . "\n" . substr( $content, $at );
}
// === CABABOOST 文脈CTA 位置制御 ここまで ===
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://cabapost.co.jp/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://cabapost.co.jp/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://cabapost.co.jp/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://cabapost.co.jp/wp-sitemap-posts-ufaq-1.xml</loc></sitemap><sitemap><loc>https://cabapost.co.jp/wp-sitemap-posts-comparison-1.xml</loc></sitemap><sitemap><loc>https://cabapost.co.jp/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://cabapost.co.jp/wp-sitemap-taxonomies-post_tag-1.xml</loc></sitemap><sitemap><loc>https://cabapost.co.jp/wp-sitemap-taxonomies-ufaq-category-1.xml</loc></sitemap></sitemapindex>
