'', 'password_label' => '', 'error_message' => '', 'page_messages' => array(), ); $settings = get_option( ACWPPM_OPTION ); if ( ! is_array( $settings ) ) { $settings = array(); } return wp_parse_args( $settings, $defaults ); } /** * Return the custom message for a given post ID, if any. * * Checks per-page messages first, then falls back to the default message. * * @param int $post_id Post ID. * @return string Empty string when no custom message applies. */ function acwppm_get_message_for_post( $post_id ) { $settings = acwppm_get_settings(); if ( ! empty( $settings['page_messages'] ) && is_array( $settings['page_messages'] ) ) { foreach ( $settings['page_messages'] as $row ) { if ( isset( $row['page_id'], $row['message'] ) && (int) $row['page_id'] === (int) $post_id && '' !== trim( $row['message'] ) ) { return $row['message']; } } } if ( ! empty( $settings['default_message'] ) ) { return $settings['default_message']; } return ''; } /** * Whether the current request follows a failed password attempt. * * WordPress gives no feedback on a wrong password: it stores the hashed * attempt in the wp-postpass cookie and redirects back to the same page. * So a request that still requires a password, carries the cookie and * has the page itself as referer is a failed attempt. * * @param int $post_id Post ID. * @return bool */ function acwppm_is_failed_attempt( $post_id ) { if ( empty( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) { return false; } $referer = wp_get_raw_referer(); if ( ! $referer ) { return false; } return untrailingslashit( $referer ) === untrailingslashit( get_permalink( $post_id ) ); } /** * Build the password form with the custom message and label. * * Replaces the form markup entirely, following the same structure * WordPress core uses, so the custom message and label are rendered * natively instead of being patched into the default output. * * @param string $output Password form HTML. * @return string */ function acwppm_filter_password_form( $output ) { $post_id = get_the_ID(); $settings = acwppm_get_settings(); $message = acwppm_get_message_for_post( $post_id ); $error = ''; if ( ! empty( $settings['error_message'] ) && acwppm_is_failed_attempt( $post_id ) ) { $error = $settings['error_message']; } // Nothing configured: leave the form untouched. if ( '' === $message && '' === $error && empty( $settings['password_label'] ) ) { return $output; } if ( '' === $message ) { $message = __( 'This content is password protected. To view it please enter your password below:', 'acw-password-protected-messages' ); } $label = ! empty( $settings['password_label'] ) ? $settings['password_label'] : __( 'Password:', 'acw-password-protected-messages' ); $field_id = 'acwppm-pwbox-' . ( $post_id ? $post_id : wp_rand() ); $form = ''; if ( '' !== $error ) { $form .= '
' . wp_kses_post( wpautop( $error ) ) . '
'; } $form .= '
' . wp_kses_post( wpautop( $message ) ) . '
'; $form .= '
'; $form .= '

'; $form .= '

'; $form .= '
'; return $form; } add_filter( 'the_password_form', 'acwppm_filter_password_form', 999999 ); /** * Add a "Settings" link on the Plugins screen. * * @param array $links Plugin action links. * @return array */ function acwppm_settings_link( $links ) { $url = admin_url( 'options-general.php?page=acw-password-protected-messages' ); $links[] = '' . esc_html__( 'Settings', 'acw-password-protected-messages' ) . ''; return $links; } add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'acwppm_settings_link' ); /** * Register the settings page under Settings. */ function acwppm_add_settings_page() { add_options_page( __( 'Password Protected Messages', 'acw-password-protected-messages' ), __( 'Password Messages', 'acw-password-protected-messages' ), 'manage_options', 'acw-password-protected-messages', 'acwppm_render_settings_page' ); } add_action( 'admin_menu', 'acwppm_add_settings_page' ); /** * Register the setting. */ function acwppm_register_settings() { register_setting( 'acwppm_settings_group', ACWPPM_OPTION, array( 'type' => 'array', 'sanitize_callback' => 'acwppm_sanitize_settings', ) ); } add_action( 'admin_init', 'acwppm_register_settings' ); /** * Sanitize settings before saving. * * @param array $input Raw input. * @return array */ function acwppm_sanitize_settings( $input ) { $clean = array( 'default_message' => '', 'password_label' => '', 'error_message' => '', 'page_messages' => array(), ); if ( ! is_array( $input ) ) { return $clean; } if ( isset( $input['default_message'] ) ) { $clean['default_message'] = wp_kses_post( $input['default_message'] ); } if ( isset( $input['password_label'] ) ) { $clean['password_label'] = sanitize_text_field( $input['password_label'] ); } if ( isset( $input['error_message'] ) ) { $clean['error_message'] = wp_kses_post( $input['error_message'] ); } if ( isset( $input['page_messages'] ) && is_array( $input['page_messages'] ) ) { $seen = array(); foreach ( $input['page_messages'] as $row ) { if ( ! is_array( $row ) ) { continue; } $page_id = isset( $row['page_id'] ) ? absint( $row['page_id'] ) : 0; $message = isset( $row['message'] ) ? wp_kses_post( $row['message'] ) : ''; // Skip empty rows and duplicate pages. if ( ! $page_id || '' === trim( $message ) || isset( $seen[ $page_id ] ) ) { continue; } $seen[ $page_id ] = true; $clean['page_messages'][] = array( 'page_id' => $page_id, 'message' => $message, ); } } return $clean; } /** * Enqueue admin script on the settings page only. * * @param string $hook Current admin page hook. */ function acwppm_admin_scripts( $hook ) { if ( 'settings_page_acw-password-protected-messages' !== $hook ) { return; } wp_enqueue_script( 'acwppm-admin', plugins_url( 'assets/js/admin.js', __FILE__ ), array( 'jquery' ), ACWPPM_VERSION, true ); } add_action( 'admin_enqueue_scripts', 'acwppm_admin_scripts' ); /** * Get all password protected posts, grouped by post type. * * Only protected content is listed, so the dropdown stays short and * relevant even on large sites. Results are cached per request. * * @return array Map of post type name => array of WP_Post objects. */ function acwppm_get_protected_posts() { static $grouped = null; if ( null !== $grouped ) { return $grouped; } $post_types = get_post_types( array( 'public' => true ) ); unset( $post_types['attachment'] ); $query = new WP_Query( array( 'post_type' => array_values( $post_types ), 'has_password' => true, 'posts_per_page' => 500, 'orderby' => 'title', 'order' => 'ASC', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); $grouped = array(); foreach ( $query->posts as $post ) { $grouped[ $post->post_type ][] = $post; } return $grouped; } /** * Render the dropdown of password protected content, grouped by post type. * * @param string $id Select element ID. * @param string $name Select element name. * @param int $selected Selected post ID. */ function acwppm_render_post_dropdown( $id, $name, $selected = 0 ) { $grouped = acwppm_get_protected_posts(); $selected = (int) $selected; $has_match = false; echo ''; } /** * Render one per-content message row. * * @param int $index Row index. * @param int $page_id Selected post ID. * @param string $message Message text. */ function acwppm_render_page_message_row( $index, $page_id = 0, $message = '' ) { ?>



ACWPPM_OPTION . '[default_message]', 'textarea_rows' => 6, 'media_buttons' => false, 'tinymce' => array( 'toolbar1' => 'bold,italic,underline,forecolor,bullist,numlist,alignleft,aligncenter,alignright,link,unlink', 'toolbar2' => '', ), ) ); ?>