/**
Theme Name: JIN:R child
Template: jinr
Author: CROOVER Inc.
Author URI: https://croover.co.jp
Version: 1.00
*/


//---------------------------------------------------------------------------
//	 H2 adsense
//---------------------------------------------------------------------------


function add_ad_before_h2_for_3times($the_content) {

	
$ad = <<< EOF

<p class="has-text-align-center u-mb-ctrl u-mb-0">スポンサーリンク</p>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 資格の門 - 記事中（PC・スマホ） -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-3377778616314459"
     data-ad-slot="6450604129"
     data-ad-format="rectangle"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>


EOF;

	
  if (  is_single() ) {//投稿ページ
    $h2 = '/^<h2.*?>.+?<\/h2>$/im';//H3見出しのパターン
    if ( preg_match_all( $h2, $the_content, $h2s )) {//H2見出しが本文中にあるかどうか
      if ( $h2s[0] ) {//チェックは不要と思うけど一応

        if ( $h2s[0][0] ) {//1番目のH2見出し手前に広告を挿入
          $the_content  = str_replace($h2s[0][0], $ad.$h2s[0][0], $the_content);
        }

      }
    }
  }
  return $the_content;
}
add_filter('the_content','add_ad_before_h2_for_3times');

//---------------------------------------------------------------------------
//	 H2 adsense 基本情報の下
//---------------------------------------------------------------------------


function add_ad_before_h2_for_4times($the_content) {

	
$ad = <<< EOF

<p class="has-text-align-center u-mb-ctrl u-mb-0">スポンサーリンク</p>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 資格の門 - 基本情報の下 -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-3377778616314459"
     data-ad-slot="5789776591"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>


EOF;

	
  if (  is_single() ) {//投稿ページ
    $h2 = '/^<h2.*?>.+?<\/h2>$/im';//H3見出しのパターン
    if ( preg_match_all( $h2, $the_content, $h2s )) {//H2見出しが本文中にあるかどうか
      if ( $h2s[0] ) {//チェックは不要と思うけど一応

        if ( $h2s[0][1] ) {//1番目のH2見出し手前に広告を挿入
          $the_content  = str_replace($h2s[0][1], $ad.$h2s[0][1], $the_content);
        }

      }
    }
  }
  return $the_content;
}
add_filter('the_content','add_ad_before_h2_for_4times');


/**
* Rinkerのクレジットを削除します
*/
function yyi_rinker_delete_credit_html_data( $meta_datas ) {
	$meta_datas[ 'credit' ] = '';
	return $meta_datas;
}
add_filter( 'yyi_rinker_meta_data_update',  'yyi_rinker_delete_credit_html_data', 200 );


function yyi_rinker_custom_shop_labels( $attr ) {
	if ( $attr[ 'alabel' ] === '' ) {
		$attr[ 'alabel' ]	= 'Amazonで見る';
	}
	if ( $attr[ 'rlabel' ] === '' ) {
		$attr[ 'rlabel' ]	= '楽天ブックスで見る';
	}
	if ( $attr[ 'ylabel' ] === '' ) {
		$attr[ 'ylabel' ]	= 'Yahooショップで見る';
	}
	if ( $attr[ 'klabel' ] === '' ) {
		$attr[ 'klabel' ]	= 'Kindleで見る';
	}
	return $attr;
}
add_action( 'yyi_rinker_update_attribute', 'yyi_rinker_custom_shop_labels' );

