Reading. * * @package CHRNX */ /** * Sanitize the configured subscribe Page ID. * * @param mixed $value Submitted option value. * @return int Editable Page ID or zero. */ function chrnx_sanitize_subscribe_page_id( $value ) { $page_id = absint( $value ); $page = get_post( $page_id ); if ( ! $page instanceof WP_Post || 'page' !== $page->post_type || ! current_user_can( 'edit_post', $page_id ) ) { return 0; } return $page_id; } /** Render the subscribe Page dropdown. */ function chrnx_render_subscribe_page_field() { wp_dropdown_pages( array( 'name' => 'chrnx_subscribe_page_id', 'selected' => absint( get_option( 'chrnx_subscribe_page_id', 0 ) ), 'show_option_none' => esc_html__( '— Select a page —', 'chrnx' ), 'option_none_value' => '0', ) ); echo '

' . esc_html__( 'Choose the published page used by subscription calls to action.', 'chrnx' ) . '

'; } /** Register the subscribe Page setting and Reading field. */ function chrnx_register_subscribe_setting() { register_setting( 'reading', 'chrnx_subscribe_page_id', array( 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'chrnx_sanitize_subscribe_page_id', ) ); add_settings_field( 'chrnx_subscribe_page_id', esc_html__( 'Subscribe page', 'chrnx' ), 'chrnx_render_subscribe_page_field', 'reading' ); } add_action( 'admin_init', 'chrnx_register_subscribe_setting' ); /** * Return the configured public subscription URL. * * @return string Published Page permalink or empty string. */ function chrnx_get_subscribe_url(): string { $page_id = absint( get_option( 'chrnx_subscribe_page_id', 0 ) ); if ( 0 === $page_id ) { return ''; } $page = get_post( $page_id ); if ( ! $page instanceof WP_Post || 'page' !== $page->post_type || 'publish' !== $page->post_status ) { return ''; } $url = get_permalink( $page ); return is_string( $url ) ? $url : ''; } /** * Sanitize the subscription form endpoint and require HTTPS. * * @param mixed $value Submitted option value. * @return string Valid HTTPS URL or an empty string. */ function chrnx_sanitize_subscribe_endpoint( $value ) { return chrnx_sanitize_https_url( $value ); } /** * Sanitize the provider's email field name. * * @param mixed $value Submitted option value. * @return string Safe field name, defaulting to email. */ function chrnx_sanitize_subscribe_field( $value ) { if ( ! is_string( $value ) ) { return 'email'; } $name = trim( $value ); // Providers use names like email, EMAIL, or fields[email]. return 1 === preg_match( '/^[A-Za-z0-9_\[\]-]{1,64}$/', $name ) ? $name : 'email'; } /** Render the subscription endpoint field. */ function chrnx_render_subscribe_endpoint_field() { printf( '', esc_attr( (string) get_option( 'chrnx_subscribe_endpoint', '' ) ) ); echo '

' . esc_html__( 'HTTPS form endpoint from your email provider. Leave empty to hide the form.', 'chrnx' ) . '

'; } /** Render the subscription email field-name input. */ function chrnx_render_subscribe_field_field() { printf( '', esc_attr( chrnx_get_subscribe_field_name() ) ); echo '

' . esc_html__( 'Name of the email input your provider expects, for example email or EMAIL.', 'chrnx' ) . '

'; } /** Register the subscription form settings and Reading fields. */ function chrnx_register_subscribe_form_settings() { register_setting( 'reading', 'chrnx_subscribe_endpoint', array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'chrnx_sanitize_subscribe_endpoint', ) ); register_setting( 'reading', 'chrnx_subscribe_field', array( 'type' => 'string', 'default' => 'email', 'sanitize_callback' => 'chrnx_sanitize_subscribe_field', ) ); add_settings_field( 'chrnx_subscribe_endpoint', esc_html__( 'Subscribe form endpoint', 'chrnx' ), 'chrnx_render_subscribe_endpoint_field', 'reading' ); add_settings_field( 'chrnx_subscribe_field', esc_html__( 'Subscribe email field name', 'chrnx' ), 'chrnx_render_subscribe_field_field', 'reading' ); } add_action( 'admin_init', 'chrnx_register_subscribe_form_settings' ); /** * Return the configured subscription form endpoint. * * @return string Valid HTTPS endpoint or an empty string. */ function chrnx_get_subscribe_endpoint(): string { return chrnx_sanitize_https_url( get_option( 'chrnx_subscribe_endpoint', '' ) ); } /** * Return the configured email field name. * * @return string Safe field name. */ function chrnx_get_subscribe_field_name(): string { return chrnx_sanitize_subscribe_field( get_option( 'chrnx_subscribe_field', 'email' ) ); }