【Hima Art Utility】プラグインの判別関数とフィルターフック一覧

WordPress plugin Hima Art UtilityWordPress Customize Ideas | Personal WP Customization Notes (PWCN)

【Hima Art Utility】プラグインで使用できるようになる判別関数やフィルターフックの一覧です。

判別関数

条件分岐をするために設定している独自関数の一覧です。

赤字の判別関数は変更を可能とするフィルターフックがあります

判別関数は【Hima Art Utility】プラグイン有効化時のみ有効になります。テーマのカスタムコードなどで以下の関数を使用している状況でプラグインを停止すると、エラーでサイトがストップする可能性がありますのでご注意ください(このトラブルを回避する方法は文末の「プラグイン停止時に判別関数やサニタイズで起こるトラブルを回避する方法」を参照ください)

判別関数機能
haup_is_mobile()wp_is_mobile()関数の代替で、タブレットをPCとして判別します
haup_is_single()デフォルトでは「投稿」のみを指定します
haup_is_single_filter()を使用することで、任意の投稿タイプを追加できます
haup_is_single_without_front()フロントページではない固定ページ+haup_is_single()
QA Analytics QA Analytics

フィルターフック

さまざまなところで外部から条件などを変更できるようにするフィルターフックの一覧です。

プラグイン全般に波及するフィルターフック

フック名対象変数内容デフォルト
haup_is_single_filter$posttypeshaup_is_single()の投稿タイプを再指定しますpost「投稿」
haup_related_posts_singular$posttypes関連投稿パターンに含める投稿タイプを再指定しますpost「投稿」
haup_next_prev_link_posttype_filter$posttypes前後ページリンクショートコードに含める投稿タイプを再指定しますpost「投稿」
haup_is_single_query_vars_filter$posttypes「loop-multi-posttype」クラスの付いたクエリーループに含める投稿タイプを再指定しますpost「投稿」
haup_remove_shortcode_paragraph()囲み型ショートコード内のコンテンツに挿入される不要なp、brタグを削除します
※囲み型ショートーコードプログラムの中で使用します
※何も設定のない段落ブロックのpタグも削除されますので注意が必要です
haup_admin_postlist_order_filter$posttypes更新日順にする投稿タイプを指定しなおしますpost,page

機能ごとのフィルターフック一覧

ログイン試行回数制限

フック名対象変数説明デフォルト
haup_change_max_attemps$max_attemptsログイン試行回数を変更します3
haup_change_lockout_time$lock_secロックアウト時間を変更します5
haup_change_locked_out_message$messageロックアウト中のメッセージを変更しますLogin failed. Are you a spammer?

計算問題による認証

フック名対象変数説明デフォルト
haup_math_error_mail_subject_filter$sub_text解答が間違っている場合に送信するメールの件名を変更しますThere may have been an unauthorized login attempt.
haup_math_error_mail_message_filter$message_text解答が間違っている場合に送信するメールの本文を変更しますDid you make a mistake in the answer to the calculation question when logging in? If you do not remember this, there may be an unauthorized login.

セキュリティヘッダー

フック名対象変数説明デフォルト
haup_pagely_frame_ancestors_policy$default_policyContent-Security-Policyの範囲を変更しますすべてのドメインを許可
frame-ancestors ‘self’ https://*

タブ切り替えコンテンツショートコード

フック名対象変数説明デフォルト
haup_tab_content_change_color$variablesタブ各部の色を変更します//タブの文字色
$variables[‘text-color’] = ‘#333’;
//タブの背景色
$variables[‘tab-bg-color’] = ‘#fff’;
//枠線色
$variables[‘tab-border-bolor’] = ‘#ccc’;
//選択時の文字色
$variables[‘tab-selected-text-color’] = ‘#fff’;
//選択時のタブ背景色
$variables[‘tab-selected-bg-color’] = ‘#000’;

パスワード保護コンテンツショートコード

フック名対象変数説明デフォルト
haup_pw_form_change_pass_error_message$error_message認証エラー時のメッセージを変更しますPlease enter the correct password
haup_pw_form_change_heading_text$heading_text見出し文字列を変更しますA password is required to view the content
haup_pw_form_change_message$message説明文の文字列を変更しますPlease enter your password to view the content
haup_pw_form_change_pass_text$label_textフォームラベルを変更しますPassword
haup_pw_form_change_submit_text$submit_text送信ボタンの文字列を変更しますSubmit

最近更新した投稿ダッシュボードウィジェット

フック名対象変数説明デフォルト
haup_updated_posts_dwidget_posttype_filter$posttypesリストに含める投稿タイプを指定しますpost(投稿)
haup_updated_posts_dwidget_num_filter$numberリストの表示数を変更します10

サニタイズ(データの無害化)関数

フォームなどで入力されたデータをサニタイズ(無害化)するためにプラグイン独自で追加している関数の一覧です。

判別関数は【Hima Art Utility】プラグイン有効化時のみ有効になります。テーマのカスタムコードなどで以下の関数を使用している状況でプラグインを停止すると、エラーでサイトがストップする可能性がありますのでご注意ください(このトラブルを回避する方法は文末の「プラグイン停止時に判別関数やサニタイズで起こるトラブルを回避する方法」を参照ください)

関数名内容
hau_sanitize_checkbox()チェックボックスで(checked)以外の値を指定できないようにします
hau_sanitize_image()画像(image)以外を指定できないようにします
hau_sanitize_css()スタイルコード以外を指定できないようにします
hau_sanitize_js_encode()jsコードをエンコード(記号化)します
※変換するだけなので厳密にはサニタイズではありません
hau_sanitize_js_decode()記号化したjsコードを元に戻します
※変換するだけなので厳密にはサニタイズではありません
ha_sanitize_html_tags()入力を許可するHTMLタグを指定しつつ、HTMLタグのみを許可します

プラグイン停止時に判別関数やサニタイズで起こるトラブルを回避する方法

【Hima Art Utility】を有効化すると、テーマのカスタムコードでも判別関数やサニタイズ関数に記載している関数を使って条件分岐やデータのサニタイズ(消毒)を行うことができます。

しかしながら、カスタムコードにこの関数を使用した状態でプラグインを停止した場合、サイトがストップし、エラー表示されるようになります。

これは判別関数やサニタイズ関数が存在しないことで起こる現象で、プラグインの不具合ではありません。

判別関数のトラブル回避策

判別関数を使ってテーマでカスタムコードを記述する場合は、以下のコードを有効化しているテーマのfunctions.phpへ追加してください。

/*** モバイル判定 ***/
if ( !function_exists( 'haup_is_mobile' ) ){
//スマホ表示分岐
function haup_is_mobile(){
if (!isset($_SERVER['HTTP_USER_AGENT'])) {
	return false;
}
$useragents = array(
	'iPhone', // iPhone
	'iPod', // iPod touch
	'Android.*Mobile', // 1.5+ Android *** Only mobile
	'Windows.*Phone', // *** Windows Phone
	'dream', // Pre 1.5 Android
	'CUPCAKE', // 1.5+ Android
	'blackberry9500', // Storm
	'blackberry9530', // Storm
	'blackberry9520', // Storm v2
	'blackberry9550', // Storm v2
	'blackberry9800', // Torch
	'webOS', // Palm Pre Experimental
	'incognito', // Other iPhone browser
	'webmate' ,// Other iPhone browser
	'Mobile.*Firefox', // Firefox OS
	'Opera Mini', // Opera Mini Browser
	'BB10', // BlackBerry 10
);
$pattern = '/'.implode('|', $useragents).'/i';
return preg_match($pattern, $_SERVER['HTTP_USER_AGENT']);
}
}


/*** Hima Art Utilityで追加した投稿タイプ判定関数 ***/
if ( !function_exists( 'haup_is_single' ) ){
function haup_is_single(){
	$posttypes ='post';
	$posttypes = apply_filters('pwcn_is_single_filter',$posttypes);
	$post_types_array = explode(',', $posttypes);//「,」で投稿タイプを分割
	$result = in_array(get_post_type(), $post_types_array);

	// pageを除外する
	if (get_post_type() === 'page') {
		$result = false;
	}

	return $result;
	
}
}

サニタイズ関数のトラブル防止策

サニタイズ関数を使ってテーマでカスタムコードを記述する場合は、以下のコードを有効化しているテーマのfunctions.phpへ追加してください。

/***** サニタイズ関数 *****/
/* チェックボックス */
if ( !function_exists( 'hau_sanitize_checkbox' ) ){
function hau_sanitize_checkbox( $checked ) {
	// Boolean check.
	return ( ( isset( $checked ) && true == $checked ) ? true : false );
}
}

/* 画像アップロード(URL) */
if ( !function_exists( 'hau_sanitize_image' ) ){
function hau_sanitize_image( $input ) {
	$filetype = wp_check_filetype( $input );
	if ( $filetype['ext'] && wp_ext2type( $filetype['ext'] ) === 'image' ) {
		return esc_url( $input );
	}
	return '';
}
}

/* CSS */
if ( !function_exists( 'hau_sanitize_css' ) ){
function hau_sanitize_css( $input ) {
	return wp_strip_all_tags( $input );
}
}

/* JS他 エンコード・デコードするだけなので完全ではない */
/* 入力 */
if ( !function_exists( 'hau_sanitize_js_encode' ) ){
function hau_sanitize_js_encode($input){
	return base64_encode($input);
}
}

/* 出力 */
if ( !function_exists( 'hau_sanitize_js_decode' ) ){
function hau_sanitize_js_decode($input){
	return base64_decode($input);
}
}

/* HTMLタグ */
if ( !function_exists( 'ha_sanitize_html_tags' ) ){
function ha_sanitize_html_tags( $input ){
$allowed_tags = array(
	'a' => array(
		'class' => array(),
		'href'  => array(),
		'rel'   => array(),
		'title' => array(),
	),
	'aside' => array(
		'class' => array(),
		'style' => array(),
	),
	'b' => array(),
	'blockquote' => array(
		'cite'  => array(),
	),
	'br' => array(),
	'button' => array(
		'id' => array(),
		'class'  => array(),
		'style' => array(),
	),
	'cite' => array(
		'title' => array(),
	),
	'code' => array(),
	'del' => array(
		'datetime' => array(),
		'title' => array(),
	),
	'dd' => array(),
	'div' => array(
		'id' => array(),
		'class' => array(),
		'title' => array(),
		'style' => array(),
	),
	'dl' => array(),
	'dt' => array(),
	'em' => array(),
	'h1' => array(
		'class' => array(),
		'style' => array(),
	),
	'h2' => array(
		'class' => array(),
		'style' => array(),
	),
	'h3' => array(
		'class' => array(),
		'style' => array(),
	),
	'h4' => array(
		'class' => array(),
		'style' => array(),
	),
	'h5' => array(
		'class' => array(),
		'style' => array(),
	),
	'h6' => array(
		'class' => array(),
		'style' => array(),
	),
	'i' => array(),
	'img' => array(
		'alt'    => array(),
		'class'  => array(),
		'height' => array(),
		'src'    => array(),
		'width'  => array(),
	),
	'li' => array(
		'class' => array(),
		'style' => array(),
	),
	'nav' => array(
		'id' => array(),
		'class' => array(),
		'style' => array(),
	),
	'ol' => array(
		'class' => array(),
		'style' => array(),
	),
	'p' => array(
		'class' => array(),
		'style' => array(),
	),
	'q' => array(
		'cite' => array(),
		'title' => array(),
	),
	'section' => array(
		'id' => array(),
		'class' => array(),
		'style' => array(),
	),
	'span' => array(
		'class' => array(),
		'title' => array(),
		'style' => array(),
	),
	'strong' => array(),
	'ul' => array(
		'class' => array(),
		'style' => array(),
	),
);

	return wp_kses($input, $allowed_tags);
}
}