HeadlineSearchPos = 0; $this->largeHeadLine = 0; $this->numberType = ''; $this->numberCaughtType = ''; $this->arrayNumberTypeChara = []; $this->numberCaughtTypeChara = ''; //配列の中身を作成する。 for ( $i=1; $i <= HEADLINE_LARGE_NUMBER_SEARCH_MAX; $i++ ) { $this->arrayNumber = ["Number$i"]; } //配列の初期化 for ( $i=1; $i <= HEADLINE_LARGE_NUMBER_SEARCH_MAX; $i++ ) { $this->arrayNumber["Number$i"]=0; } /***** フック *****/ /*設定ページを追加する*/ add_action( 'admin_menu', array( $this, "add_plugin_admin_menu" ) ); /*見出しに数字を追加する*/ add_filter( 'the_content', array( $this, 'add_number_to_headLine' ) ); } /*設定メニューのサブメニューにメニューを追加*/ public function add_plugin_admin_menu(){ /* プラグインページを登録 */ add_submenu_page( 'options-general.php', 'AddHeadlineNumber', 'AddHeadlineNumber', 'administrator', __FILE__, array( $this, 'display_plugin_admin_page' )); /*数字の種類用*/ register_setting( 'headline-number-group', // option_group 'headline_number_type', // option_name array( $this,'headline_number_type_validation')); // sanitize_callback /*数字間の種類用*/ register_setting( 'headline-number-group', // option_group 'headline_number_Caught_type', // option_name array( $this,'headline_number_caught_validation')); // sanitize_callback } /*保存する数字のタイプの検証*/ public function headline_number_type_validation($input){ foreach ( ARRAY_HEAD_LINE_NUMBER_TYPE as $type ) { if($type == $input){ return $input; } } add_settings_error( 'headline_number_type', 'HeadlineNumberTypeValidationError', __( 'illegal data', 'AddHeadlineNumber' ), 'error' ); } /*保存する数字間の文字の検証*/ public function headline_number_caught_validation($input){ foreach ( ARRAY_HEAD_LINE_NUMBER_CAUGHT_TYPE as $type ) { if( $type == $input ){ return $input; } } add_settings_error( 'headline_number_caught_type', 'HeadlineNumberTypeCaughtValidationError', __( 'illegal data', 'AddHeadlineNumber' ), 'error' ); } public function display_plugin_admin_page() { /*スタイルシート読み込み*/ wp_enqueue_style( 'Add-Headline-Number' ,plugins_url( 'add-number-to-headline.css', __FILE__ )); /*保存情報を取得する*/ $this->numberType = get_site_option( 'headline_number_type', HEADLINE_NUMBER_TYPE_NORMAL); //数字の種類 $this->numberCaughtType = get_site_option('headline_number_Caught_type',HEADLINE_NUMBER_CAUGHT_TYPE_HYPHEN); //数字間の文字の種類 ?>


'; echo ''; } ?>
'; echo 'numberType == $type ) { echo "checked"; } echo " />$type"; echo ''.ARRAY_HEAD_LINE_NUMBER_TYPE_STR[ $type ][0].ARRAY_HEAD_LINE_NUMBER_CAUGHT_TYPE_STR[ $this->numberCaughtType ].ARRAY_HEAD_LINE_NUMBER_TYPE_STR[ $type ][1].ARRAY_HEAD_LINE_NUMBER_CAUGHT_TYPE_STR[ $this->numberCaughtType ].ARRAY_HEAD_LINE_NUMBER_TYPE_STR[ $type ][2]; echo '

'; echo ''; } ?>
"; echo 'numberCaughtType == $type ) { echo "checked"; } echo " />$type"; echo ''.ARRAY_HEAD_LINE_NUMBER_TYPE_STR[ $this->numberType ][0].ARRAY_HEAD_LINE_NUMBER_CAUGHT_TYPE_STR[ $type ].ARRAY_HEAD_LINE_NUMBER_TYPE_STR[ $this->numberType ][1].ARRAY_HEAD_LINE_NUMBER_CAUGHT_TYPE_STR[ $type ].ARRAY_HEAD_LINE_NUMBER_TYPE_STR[ $this->numberType ][2]; echo '
numberType = get_site_option( 'headline_number_type', HEADLINE_NUMBER_TYPE_NORMAL); $this->numberCaughtType = get_site_option('headline_number_Caught_type',HEADLINE_NUMBER_CAUGHT_TYPE_HYPHEN); /*見出しに付与する情報を取得する。*/ $this->arrayNumberTypeChara = ARRAY_HEAD_LINE_NUMBER_TYPE_STR[ $this->numberType ]; $this->numberCaughtTypeChara = ARRAY_HEAD_LINE_NUMBER_CAUGHT_TYPE_STR[ $this->numberCaughtType ]; //リセット for ( $i=1; $i <= HEADLINE_LARGE_NUMBER_SEARCH_MAX; $i++ ) { $this->arrayNumber["Number$i"]=0; } $this->HeadlineSearchPos = 0; /*記事中にある一番大きい見出しを取得する 見出しが見つからなかったら何もしない。*/ if( $this->largeHeadLine = $this->search_large_number($the_content) ){ /*見出しが見つからなくなるまで処理を繰り返す*/ while(1) { //次の見出しを検索する。 if( -1 == ($headLine = $this->search_next($the_content)) ){ //見出しが見つからないので処理が終了する break; } //見出しに付与する文字列を作成する $numberStr = $this->create_number_str($headLine); //見出しに文字列を付与する $the_content = $this->add_number($numberStr,$the_content); } //endwhile; } // endif; } //endif; return $the_content; } // end AddHeadLineNumber /*記事の中から一番大きい見出しを調べる。*/ public function search_large_number($the_content){ for ($i=1; $i <= HEADLINE_LARGE_NUMBER_SEARCH_MAX ; $i++) { $searchStr = '//i'; if( preg_match($searchStr,$the_content) ){ return $i; } } return 0; } // end HeadLineLargeNumberSearch /*次の見出しが何か検索する。*/ public function search_next($the_content){ //見出しが見つかるまで $count = 0; while (1) { $this->HeadlineSearchPos = strpos($the_content, 'HeadlineSearchPos); if(false === ($endTagPos = strpos($the_content, '>', $this->HeadlineSearchPos))){ //終了タグが見つからなかったら return -1; } //見つかった終了タグが見出しのものか調べる。 $startTagPos = strpos($the_content,'<',$this->HeadlineSearchPos+1); if(($endTagPos > $startTagPos) && (false !== $startTagPos)){ //終了タグの前に開始タグがあったらその終了タグは見出しの終了タグではないと判断。 //終了タグ以降から見出しを探すようにする。 $this->HeadlineSearchPos = $endTagPos; continue; } if(false === $this->HeadlineSearchPos){ //見出しが見つからなかったら return -1; } //HeadlineSearchPos), $matches, PREG_OFFSET_CAPTURE); //記事の中で一番大きい見出しか? if( 2 == $matches[0][1] ){ if( $this->largeHeadLine == intval( $matches[0][0] ) ){ if($this->arrayNumber['Number1'] <= HEADLINE_LARGE_NUMBER_SEARCH_MAX){ $this->arrayNumber['Number1']++; } //順位②番目以降の見出しは付与する値をリセットする。 for ( $i=2; $i <= HEADLINE_LARGE_NUMBER_SEARCH_MAX; $i++ ) { $this->arrayNumber['Number'.$i] = 0; } } else { if( $this->arrayNumber['Number'.((intval($matches[0][0])) - ($this->largeHeadLine) + 1)] <= HEADLINE_LARGE_NUMBER_SEARCH_MAX ){ $this->arrayNumber['Number'.((intval($matches[0][0])) - ($this->largeHeadLine) + 1)] += 1; } } return intval($matches[0][0]); //見つかった数字をヘッダーの番号として返却する。 } $this->HeadlineSearchPos++; //次の位置から検索していく。 } } // end SearchNextHeadline /*見出しに付与する数字の文字列を作成する。*/ public function create_number_str($HeadLine){ $baseCount = $this->largeHeadLine; $insertCount = 1; $numberStr = ''; //1-2-3のような見出しに付与する文字列を作成する。 if($insertCount <= HEADLINE_LARGE_NUMBER_SEARCH_MAX + 1){ for ( ;$baseCount <= $HeadLine;$baseCount++ ) { if( $insertCount > 1 ){ $numberStr .= $this->numberCaughtTypeChara; } $numberStr .= $this->arrayNumberTypeChara[( $this->arrayNumber['Number'.$insertCount] == 0 ) ? 0 : ( $this->arrayNumber['Number'.$insertCount] - 1 )]; $insertCount++; } } return $numberStr.' '; } // end CreateHeadlineNumber /*見出しに数字を付与する*/ public function add_number($NumberStr,$the_content){ $this->HeadlineSearchPos = strpos($the_content,'>',$this->HeadlineSearchPos); $the_content = substr_replace($the_content, $NumberStr, $this->HeadlineSearchPos+1, 0); $this->HeadlineSearchPos++; return $the_content; }//end AddNumberHeadline //プラグイン停止時の処理 public function headline_number_deactivate() { $this->remove_user_setting(); } //ユーザー設定削除 private static function remove_user_setting() { delete_option('headline_number_type'); delete_option('headline_number_Caught_type'); } } //class end /*実態を作成する*/ $AHLNClass = new KOSUKE_cAddNumberToHeadLine(); register_deactivation_hook( __FILE__, array( $AHLNClass, 'headline_number_deactivate' ) ); ?>