0, 'default_post' => 0, 'double_optin' => 1, 'from_name' => '', 'from_email' => '', 'consent_text' => __( 'I agree to receive email notifications about this discussion. I can unsubscribe at any time.', 'replyping' ), 'notify_subject' => __( '[{site_name}] New comment on "{post_title}"', 'replyping' ), 'notify_body' => __( "{comment_author} wrote:\n\n{comment_excerpt}\n\nRead and reply: {comment_link}\n\nUnsubscribe: {unsubscribe_link}", 'replyping' ), 'confirm_subject' => __( '[{site_name}] Please confirm your subscription', 'replyping' ), 'confirm_body' => __( "Please confirm that you want to receive comment notifications for \"{post_title}\":\n\n{confirm_link}\n\nIf you did not request this, ignore this email or unsubscribe: {unsubscribe_link}", 'replyping' ), ); } /** * Get all settings merged with defaults. * * @return array */ public static function all() { $saved = get_option( self::OPTION, array() ); return wp_parse_args( is_array( $saved ) ? $saved : array(), self::defaults() ); } /** * Get one setting. * * @param string $key Key. * @return mixed */ public static function get( $key ) { $all = self::all(); return isset( $all[ $key ] ) ? $all[ $key ] : ''; } /** * Add the settings page. */ public function admin_menu() { add_options_page( __( 'ReplyPing', 'replyping' ), __( 'ReplyPing', 'replyping' ), 'manage_options', 'replyping', array( $this, 'render_page' ) ); } /** * Register setting, sections and fields. */ public function register() { register_setting( 'replyping', self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize' ), 'default' => self::defaults(), ) ); add_settings_section( 'replyping_general', __( 'General', 'replyping' ), '__return_false', 'replyping' ); add_settings_section( 'replyping_emails', __( 'Emails', 'replyping' ), array( $this, 'emails_intro' ), 'replyping' ); $fields = array( array( 'default_replies', __( 'Pre-check "replies" box', 'replyping' ), 'checkbox', 'replyping_general' ), array( 'default_post', __( 'Pre-check "all comments" box', 'replyping' ), 'checkbox', 'replyping_general' ), array( 'double_optin', __( 'Double opt-in for logged-in users', 'replyping' ), 'checkbox', 'replyping_general' ), array( 'consent_text', __( 'Consent text', 'replyping' ), 'textarea', 'replyping_general' ), array( 'from_name', __( 'From name', 'replyping' ), 'text', 'replyping_emails' ), array( 'from_email', __( 'From email', 'replyping' ), 'email', 'replyping_emails' ), array( 'notify_subject', __( 'Notification subject', 'replyping' ), 'text', 'replyping_emails' ), array( 'notify_body', __( 'Notification body', 'replyping' ), 'textarea', 'replyping_emails' ), array( 'confirm_subject', __( 'Confirmation subject', 'replyping' ), 'text', 'replyping_emails' ), array( 'confirm_body', __( 'Confirmation body', 'replyping' ), 'textarea', 'replyping_emails' ), ); foreach ( $fields as $f ) { add_settings_field( 'replyping_' . $f[0], $f[1], array( $this, 'render_field' ), 'replyping', $f[3], array( 'key' => $f[0], 'type' => $f[2], 'label_for' => 'replyping_' . $f[0], ) ); } } /** * Intro for the emails section. */ public function emails_intro() { echo '

' . esc_html__( 'Placeholders: {post_title}, {comment_author}, {comment_excerpt}, {comment_link}, {unsubscribe_link}, {site_name}. The confirmation email also supports {confirm_link}.', 'replyping' ) . '

'; } /** * Sanitize all settings. * * @param mixed $input Raw input. * @return array */ public function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $defaults = self::defaults(); $out = array(); foreach ( array( 'default_replies', 'default_post', 'double_optin' ) as $k ) { $out[ $k ] = empty( $input[ $k ] ) ? 0 : 1; } $out['from_name'] = isset( $input['from_name'] ) ? sanitize_text_field( $input['from_name'] ) : ''; $out['from_email'] = isset( $input['from_email'] ) ? sanitize_email( $input['from_email'] ) : ''; foreach ( array( 'notify_subject', 'confirm_subject' ) as $k ) { $v = isset( $input[ $k ] ) ? sanitize_text_field( $input[ $k ] ) : ''; $out[ $k ] = '' === $v ? $defaults[ $k ] : $v; } foreach ( array( 'consent_text', 'notify_body', 'confirm_body' ) as $k ) { $v = isset( $input[ $k ] ) ? sanitize_textarea_field( $input[ $k ] ) : ''; $out[ $k ] = '' === $v ? $defaults[ $k ] : $v; } return $out; } /** * Render one field. * * @param array $args Field args. */ public function render_field( $args ) { $key = $args['key']; $value = self::get( $key ); $id = 'replyping_' . $key; $name = self::OPTION . '[' . $key . ']'; switch ( $args['type'] ) { case 'checkbox': printf( '', esc_attr( $id ), esc_attr( $name ), checked( 1, (int) $value, false ) ); if ( 'double_optin' === $key ) { echo '

' . esc_html__( 'Guests must always confirm by email. When this is off, logged-in users subscribing with their own account email are activated immediately.', 'replyping' ) . '

'; } break; case 'textarea': printf( '', esc_attr( $id ), esc_attr( $name ), esc_textarea( $value ) ); break; default: printf( '', 'email' === $args['type'] ? 'email' : 'text', esc_attr( $id ), esc_attr( $name ), esc_attr( $value ) ); } } /** * Render the settings page. */ public function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } echo '

' . esc_html( get_admin_page_title() ) . '

'; echo '
'; settings_fields( 'replyping' ); do_settings_sections( 'replyping' ); submit_button(); echo '
'; } }