Files
Convert_Protection_Text/convert-protection-text.php
T
nobu 622d8dc3c8 WP_PLUGIN パスワード保護ページのテキスト変更
・サニタイズとエスケープ
・完了メッセージの閉じるボタン追加
2021-06-18 19:20:02 +09:00

138 lines
5.4 KiB
PHP

<?php
/*
Plugin Name: Convert Protection Text
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: 1.0
Author: nobu
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
class CNV_Protection_Text {
const ALERT_TEXT = 'このコンテンツはパスワードで保護されています。閲覧するには以下にパスワードを入力してください。';
const BTN_LABEL = 'パスワード';
const BTN_TEXT = '確定';
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_pages' ) );
add_filter( 'protected_title_format', array( $this, 'edit_protected_word' ) );
add_filter( 'the_password_form', array( $this, 'my_password_form' ) );
}
public function add_pages() {
add_menu_page( 'アクセス権限ページ設定', 'アクセス権限ページ設定', 'level_8', __FILE__, array(
$this,
'show_text_option_page'
), '', 90 );
}
public function show_text_option_page() {
$update_option = filter_input( INPUT_POST, '_cnv_protect_options', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
if ( ! empty( $update_option ) ) {
check_admin_referer( 'cnv_options' );
update_option( '_cnv_protect_options', $update_option );
?>
<div id="settings_updated" class="updated notice is-dismissible">
<p><strong>設定を保存しました</strong></p>
</div><?php
}
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br/></div>
<h2>アクセス権限ページ設定</h2>
<form action="" method="post">
<?php
wp_nonce_field( 'cnv_options' );
$opt = get_option( '_cnv_protect_options' );
$show_radio = $opt['flag'] ?? 1;
$show_text = $opt['text'] ?? self::ALERT_TEXT;
$show_label = $opt['label'] ?? self::BTN_LABEL;
$show_btn = $opt['btn'] ?? self::BTN_TEXT;
?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="inputtext">保護中</label></th>
<td>
<?php if ( $show_radio == 2 ) : ?>
<label>
<input type="radio" name="_cnv_protect_options[flag]" value="1">表示
</label>
<label>
<input type="radio" name="_cnv_protect_options[flag]" value="2" checked>非表示
</label>
<?php elseif ( $show_radio == 1 ): ?>
<label>
<input type="radio" name="_cnv_protect_options[flag]" value="1" checked>表示
</label>
<label>
<input type="radio" name="_cnv_protect_options[flag]" value="2">非表示
</label>
<?php endif; ?>
</td>
</tr>
<tr valign="top">
<th scope="row">メッセージ</th>
<td>
<textarea name="_cnv_protect_options[text]" rows="4"
cols="60"><?php echo esc_html( $show_text ); ?></textarea>
</td>
</tr>
<tr valign="top">
<th scope="row">ボタンラベル</th>
<td>
<input type="text" name="_cnv_protect_options[label]"
value="<?php echo esc_html( $show_label ); ?>">
</td>
</tr>
<tr valign="top">
<th scope="row">ボタン名</th>
<td>
<input type="text" name="_cnv_protect_options[btn]"
value="<?php echo esc_html( $show_btn ); ?>">
</td>
</tr>
</table>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/></p>
</form>
<!-- /.wrap --></div>
<?php
}
public function edit_protected_word( $text ) {
$opt = get_option( '_cnv_protect_options' );
if ( isset( $opt['flag'] ) && $opt['flag'] == 2 ) {
return '%s';
}
return $text;
}
//パスワード保護時のメッセージ
public function my_password_form( $form_text ): string {
$opt = get_option( '_cnv_protect_options' );
if ( isset( $opt['text'] ) && ! empty( $opt['text'] ) ) {
$alert_text = $opt['text'];
} else {
$alert_text = self::ALERT_TEXT;
}
if ( isset( $opt['label'] ) && ! empty( $opt['label'] ) ) {
$btn_label = $opt['label'] . ' : ';
} else {
$btn_label = self::BTN_LABEL . ' : ';
}
if ( isset( $opt['btn'] ) && ! empty( $opt['btn'] ) ) {
$btn_text = $opt['btn'];
} else {
$btn_text = self::BTN_TEXT;
}
return '<form action="' . home_url() . '/wp-login.php?action=postpass" class="post-password-form" method="post">
<p>' . nl2br( esc_html( $alert_text ) ) . '</p>
<p><label for="pwbox">' . esc_html( $btn_label ) . '<input name="post_password" id="pwbox" type="password" size="20"></label> <input type="submit" name="Submit" value="' . esc_html( $btn_text ) . '"></p></form>';
}
}
new CNV_Protection_Text;