'', 'identity_secret' => '', 'visibility' => 'all', // all | include | exclude. 'page_ids' => array(), 'home' => 0, 'recognise' => 1, 'woo' => 1, 'consent' => 0, 'help_slug' => '', ); } /** * Current settings merged with defaults. * * @return array */ function hivenza_settings() { $saved = get_option( HIVENZA_OPTION, array() ); return wp_parse_args( is_array( $saved ) ? $saved : array(), hivenza_defaults() ); } /** * The identity secret: a constant in wp-config.php wins over the stored option. * * @return string */ function hivenza_identity_secret() { if ( defined( 'HIVENZA_SECRET' ) && is_string( HIVENZA_SECRET ) && '' !== HIVENZA_SECRET ) { return HIVENZA_SECRET; } $settings = hivenza_settings(); return (string) $settings['identity_secret']; } /** * Keep only a help centre address: accepts "my-brand" or a pasted https://hivenza.com/help/my-brand link. * * @param string $value Raw value. * @return string */ function hivenza_clean_slug( $value ) { $value = trim( (string) $value ); if ( preg_match( '#/help/([^/?\#]+)#i', $value, $match ) ) { $value = $match[1]; } $value = strtolower( $value ); return preg_match( '/^[a-z0-9][a-z0-9-]{0,80}$/', $value ) ? $value : ''; } /** * Sanitize the whole option on save. * * @param mixed $input Submitted values. * @return array */ function hivenza_sanitize_settings( $input ) { $input = is_array( $input ) ? $input : array(); $old = hivenza_settings(); $out = hivenza_defaults(); $key = isset( $input['chat_key'] ) ? trim( sanitize_text_field( wp_unslash( $input['chat_key'] ) ) ) : ''; // A chat key is letters, digits, dashes and underscores. $out['chat_key'] = preg_match( '/^[A-Za-z0-9_-]{6,100}$/', $key ) ? $key : ''; if ( '' === $out['chat_key'] && '' !== $key ) { $out['chat_key'] = (string) $old['chat_key']; // Keep the working key. add_settings_error( 'hivenza_settings', 'hivenza_key', __( 'That does not look like a chat key. Copy it again from Hivenza (Website chat, Install).', 'hivenza' ) ); } // The secret is never printed back: leave the field empty to keep it. $secret = isset( $input['identity_secret'] ) ? trim( sanitize_text_field( wp_unslash( $input['identity_secret'] ) ) ) : ''; if ( ! empty( $input['remove_secret'] ) ) { $out['identity_secret'] = ''; } elseif ( '' !== $secret ) { $out['identity_secret'] = $secret; } else { $out['identity_secret'] = (string) $old['identity_secret']; } $visibility = isset( $input['visibility'] ) ? sanitize_key( $input['visibility'] ) : 'all'; $out['visibility'] = in_array( $visibility, array( 'all', 'include', 'exclude' ), true ) ? $visibility : 'all'; $out['page_ids'] = isset( $input['page_ids'] ) && is_array( $input['page_ids'] ) ? array_values( array_unique( array_filter( array_map( 'absint', $input['page_ids'] ) ) ) ) : array(); $out['home'] = empty( $input['home'] ) ? 0 : 1; $out['recognise'] = empty( $input['recognise'] ) ? 0 : 1; $out['woo'] = empty( $input['woo'] ) ? 0 : 1; $out['consent'] = empty( $input['consent'] ) ? 0 : 1; $out['help_slug'] = hivenza_clean_slug( isset( $input['help_slug'] ) ? wp_unslash( $input['help_slug'] ) : '' ); return $out; } /** * Register the option with the Settings API. */ function hivenza_register_settings() { register_setting( 'hivenza_settings_group', HIVENZA_OPTION, array( 'type' => 'array', 'sanitize_callback' => 'hivenza_sanitize_settings', 'default' => hivenza_defaults(), 'show_in_rest' => false, ) ); } add_action( 'admin_init', 'hivenza_register_settings' );