-- ' . __( 'Article individual settings', AAIH__TEXT_DOMAIN ) . ' -->';
}
/**
* メタボックスの表示
*
* add_meta_boxesフックで呼び出している add_meta_box のコールバック関数
* 編集画面セクションに HTML 出力する関数。
*
* @param WP_Post $post 現在編集している $post オブジェクト(が引数として設定できる)
* @return void
*
* 補足
* nonceフィールドを使用しセキュリティ確保する必要あり
*/
function aaih__meta_box_callback( $post ) {
/* ------------------------------------
* nonceフィールドを追加して後でチェックする
* wp_nonce_field( $action, $name, $referer, $echo )
* ------------------------------------
* $action :(文字列)アクションの名前。実行中のコンテキストを与える
* $name :(文字列)nonce の名前。作成される hidden フィールドの name 属性。
* $referer :(真偽値)wp_referer_field() 関数を使って、リファラーを表す hidden フィールドを生成するかどうか。初期値: true
* $echo :(真偽値)hidden フィールドを表示する(true)か値として返す(false)か
*
* 例)フォーム内でアクションと nonce に名前をつける。1 番目と 2 番目の引数に値を入れて、必要な hidden フィールドを表示
*
* ----------------------------------- */
wp_nonce_field( AAIH__NONCE_ACTION__META_BOX , AAIH__NONCE_NAME__META_BOX );
/* ------------------------------------
* プラグイン設定値をゲット
* カスタムフィールドの値をDBから取得
* ----------------------------------- */
$settings = aaih__get_item();
$meta_settings = aaih__get_meta_data( $post->ID );
/* ------------------------------------
* 記事ステータスチェック
* (公開 publish 記事はメッセージ表示)
* ----------------------------------- */
aaih__show_meta__publish_warning();
/* ------------------------------------
* カスタムフィールド表示
* ----------------------------------- */
?>
:
:
:
$str_shortcode ) {
// Adxxに設定されている 広告ユニットを取得
$ad_unit_select = $settings[ $ad_nth ]['ad_unit_select'];
// Adxxに設定されている memo(名前の入力)を取得
$ad_name = $settings[ $ad_nth ]['name'];
if ( '' === $ad_name ) {
$ad_name = __( '---', AAIH__TEXT_DOMAIN ); // なし表示;
}
// ショートコード表示
?>
$value ) {
update_post_meta( $post_id, $key , $value );
}
}
/**
* メタデータ全取得
*
* アクションフック save_post を使用。
* コールバック関数の引数として、保存する記事の $post_id が受け取れる。
*
* @param int $post_id :保存する記事の ID
* @return array $meta_args :メタフィールド内の設定値全て
*/
function aaih__get_meta_data( $post_id ) {
/*
* $meta_names = array(
* 'meta__ad_off' => 'off', // 広告の表示を全てoff(3つの設定を強制OFF)
* 'meta__target_h_tag' => 'same', // 対象とするHタグ
* 'meta__ad_space_change' => 'same', // 広告の間隔を変更するかのon/off
* 'meta__ad_space' => '', // int: 広告の間隔(文字数指定)
* 'meta__ad_show_num' => '-1', // int: 記事内広告の上限( -1: 設定値に同じ、0:無制限)
* 'meta__debug_mode_onoff' => 'same', // int: 記事内広告の上限( -1: 設定値に同じ、0:無制限)
* );
*/
$meta_settings = AAIH__META_DEFAULT;
foreach( array_keys( $meta_settings ) as $key ) {
$meta_value = get_post_meta( $post_id, $key, true );
// 空文字(一度も保存されてない場合)はデフォルト値
// 広告の間隔だけは、デフォルトとして設定値をセット
if ( 'meta__ad_space' === $key && '' === $meta_value ) {
$meta_settings[ $key ] = aaih__get_item( 'ad_space' );
}elseif ( '' !== $meta_value ) {
// 空文字でない場合(一度でも保存された場合)
$meta_settings[ $key ] = $meta_value;
}
}
//データ検証
$meta_settings = aaih__item_validation( $meta_settings );
return $meta_settings;
}
/**
* メタデータによる設定変更確認
*
* 設定値に対してカスタムフィールド内で設定変更がされたか確認し、
* 変更されていればその値を優先的に反映する。
*
* @param int $post_id : 保存する記事の ID
* @param array $settings : 設定値全体
* @return array $settings : カスタムフィールドの設定値を反映した設定値全体
*/
function aaih__meta_data_check( $settings ) {
/* 記事に設定されているメタデータをすべて取得
* オプション:広告非表示
* オプション:対象Hタグ
* オプション:広告の間隔
* オプション:記事内広告の上限の数
*/
// post id をゲット
$post_id = aaih__get_post_id();
// カスタムフィールド設定値を全て取得
$meta_settings = aaih__get_meta_data( $post_id );
foreach ( $meta_settings as $key => $value ) {
switch ( $key ) {
case 'meta__ad_off': // on / off
if ( 'on' === $value) {
// 広告非表示がonの場合には、以下3つの設定は全てoffにする
// ショートコードには影響しない
$settings['h_tag_ad_onoff'] = 'off'; // Hタグ前Ad
$settings['first_h_tag_ad_onoff'] = 'off'; // 最初のHタグ前Ad
$settings['after_content_ad_onoff'] = 'off'; // 記事下Ad
}
break;
case 'meta__target_h_tag': // same / H_tag_all / H2_only
if ( 'same' !== $value) {
$settings['target_h_tag'] = $value; // 対象とするHタグ
}
break;
case 'meta__ad_space_change': // same / change
// 特に何もしない( 以下の 'meta__ad_space' でまとめてチェック)
break;
case 'meta__ad_space':
if ( 'change' === $meta_settings['meta__ad_space_change'] && '' !== $value ) {
$settings['ad_space'] = $value; // 広告の間隔(文字数指定)
}
break;
case 'meta__ad_show_num':
if ( -1 !== (int) $value) { // -1: 設定値に同じ
// 設定値に同じではない場合は 設定値をカスタムフィールドの値に書き換え
$settings['ad_show_num'] = (int) $value; // 記事内広告の上限
}
break;
case 'meta__debug_mode_onoff': // same / on / off
if ( 'same' !== $value) {
$settings['debug_mode_onoff'] = $value; // デバッグモードの on/off
}
break;
default:
$alert_msg = 'aaih__meta_data_check 無効な値 : key:'. $key . ', value:' . $value ;
aaih__popup_alert( $alert_msg );
}
}
return $settings;
}
?>