'', 'enabled' => 1, 'logged_in_only' => 0, 'hide_button' => 0, 'open_selector' => '', 'secret_hash_key' => '', 'identifier_source' => 'email', ); $options = get_option( RELEASEBIRD_OPTION, array() ); if ( ! is_array( $options ) ) { $options = array(); } // Up to 0.2.0 the selector was stored as "mount_in" (it had no effect there). if ( ! isset( $options['open_selector'] ) && ! empty( $options['mount_in'] ) ) { $options['open_selector'] = $options['mount_in']; } return wp_parse_args( $options, $defaults ); } /** * The secret hash key: a constant in wp-config.php wins over the saved setting. * * @param array $o Options. * @return string */ private function get_secret( $o ) { if ( defined( 'RELEASEBIRD_SECRET_HASH_KEY' ) && is_string( RELEASEBIRD_SECRET_HASH_KEY ) && '' !== RELEASEBIRD_SECRET_HASH_KEY ) { return RELEASEBIRD_SECRET_HASH_KEY; } return (string) $o['secret_hash_key']; } /* -------------------------------------------------------------------- */ /* Settings page */ /* -------------------------------------------------------------------- */ public function register_settings_page() { add_options_page( __( 'Releasebird', 'releasebird' ), __( 'Releasebird', 'releasebird' ), 'manage_options', 'releasebird', array( $this, 'render_settings_page' ) ); } public function register_settings() { register_setting( 'releasebird_group', RELEASEBIRD_OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'default' => array(), ) ); add_settings_section( 'releasebird_main', __( 'Widget', 'releasebird' ), '__return_false', 'releasebird' ); $this->add_field( 'api_key', __( 'API key', 'releasebird' ), 'field_api_key' ); $this->add_field( 'enabled', __( 'Enable widget', 'releasebird' ), 'field_enabled' ); $this->add_field( 'logged_in_only', __( 'Logged-in users only', 'releasebird' ), 'field_logged_in_only' ); add_settings_section( 'releasebird_launcher', __( 'Launcher (optional)', 'releasebird' ), array( $this, 'render_launcher_section_intro' ), 'releasebird' ); $this->add_field( 'open_selector', __( 'Open the widget from', 'releasebird' ), 'field_open_selector', 'releasebird_launcher' ); $this->add_field( 'hide_button', __( 'Floating button', 'releasebird' ), 'field_hide_button', 'releasebird_launcher' ); add_settings_section( 'releasebird_identify', __( 'User verification (optional)', 'releasebird' ), array( $this, 'render_identify_section_intro' ), 'releasebird' ); $this->add_field( 'secret_hash_key', __( 'Secret hash key', 'releasebird' ), 'field_secret_hash_key', 'releasebird_identify' ); $this->add_field( 'identifier_source', __( 'Identify logged-in users by', 'releasebird' ), 'field_identifier_source', 'releasebird_identify' ); } private function add_field( $id, $label, $callback, $section = 'releasebird_main' ) { add_settings_field( 'releasebird_' . $id, $label, array( $this, $callback ), 'releasebird', $section ); } public function render_launcher_section_intro() { echo '

' . esc_html__( 'By default the widget shows a floating button. You can also open it from your own button or menu item.', 'releasebird' ) . '

'; } public function render_identify_section_intro() { echo '

' . esc_html__( 'Verify the identity of logged-in WordPress users so they appear as verified in Releasebird. The secret hash key is used only on the server to compute the signature and is never exposed to the browser.', 'releasebird' ) . '

'; } /** * @param mixed $input * @return array */ public function sanitize_settings( $input ) { $input = is_array( $input ) ? $input : array(); $current = $this->get_options(); $api_key = isset( $input['api_key'] ) ? trim( sanitize_text_field( $input['api_key'] ) ) : ''; if ( 0 === strpos( $api_key, 'rb_live_' ) ) { add_settings_error( RELEASEBIRD_OPTION, 'releasebird_wrong_key', __( 'That is an API key for the CLI (rb_live_…). The widget needs the public key from Releasebird → Settings → Widgets. Your previous key was kept.', 'releasebird' ) ); $api_key = $current['api_key']; } elseif ( '' !== $api_key && ! preg_match( '/^[A-Za-z0-9_-]{8,128}$/', $api_key ) ) { add_settings_error( RELEASEBIRD_OPTION, 'releasebird_invalid_key', __( 'The API key contains unexpected characters. Copy it again from Releasebird → Settings → Widgets. Your previous key was kept.', 'releasebird' ) ); $api_key = $current['api_key']; } // The saved secret is never printed into the form. An empty field keeps it; the checkbox removes it. $secret = isset( $input['secret_hash_key'] ) ? trim( sanitize_text_field( $input['secret_hash_key'] ) ) : ''; if ( ! empty( $input['secret_hash_key_remove'] ) ) { $secret = ''; } elseif ( '' === $secret ) { $secret = $current['secret_hash_key']; } return array( 'api_key' => $api_key, 'enabled' => empty( $input['enabled'] ) ? 0 : 1, 'logged_in_only' => empty( $input['logged_in_only'] ) ? 0 : 1, 'hide_button' => empty( $input['hide_button'] ) ? 0 : 1, 'open_selector' => isset( $input['open_selector'] ) ? trim( sanitize_text_field( $input['open_selector'] ) ) : '', 'secret_hash_key' => $secret, 'identifier_source' => isset( $input['identifier_source'] ) && 'wp_id' === $input['identifier_source'] ? 'wp_id' : 'email', ); } public function field_api_key() { $o = $this->get_options(); printf( '', esc_attr( RELEASEBIRD_OPTION ), esc_attr( $o['api_key'] ) ); echo '

'; echo esc_html__( 'The public widget key from Releasebird → Settings → Widgets (shown in the embed snippet). Not the rb_live_ key under Settings → API Keys.', 'releasebird' ); printf( ' %s', esc_url( RELEASEBIRD_DASHBOARD_URL ), esc_html__( 'Open Releasebird', 'releasebird' ) ); echo '

'; } public function field_enabled() { $o = $this->get_options(); printf( '', esc_attr( RELEASEBIRD_OPTION ), checked( 1, $o['enabled'], false ), esc_html__( 'Show the widget on the site', 'releasebird' ) ); } public function field_logged_in_only() { $o = $this->get_options(); printf( '', esc_attr( RELEASEBIRD_OPTION ), checked( 1, $o['logged_in_only'], false ), esc_html__( 'Only load the widget for logged-in users', 'releasebird' ) ); } public function field_open_selector() { $o = $this->get_options(); printf( '', esc_attr( RELEASEBIRD_OPTION ), esc_attr( $o['open_selector'] ) ); echo '

' . esc_html__( 'CSS selector of your own button, link or menu item, for example .open-releasebird or #whats-new. A click on it opens the widget.', 'releasebird' ) . '

'; } public function field_hide_button() { $o = $this->get_options(); printf( '', esc_attr( RELEASEBIRD_OPTION ), checked( 1, $o['hide_button'], false ), esc_html__( 'Hide the floating button (use your own element above instead)', 'releasebird' ) ); } public function field_secret_hash_key() { $o = $this->get_options(); if ( defined( 'RELEASEBIRD_SECRET_HASH_KEY' ) && '' !== (string) RELEASEBIRD_SECRET_HASH_KEY ) { echo '

' . esc_html__( 'Set in wp-config.php (RELEASEBIRD_SECRET_HASH_KEY).', 'releasebird' ) . '

'; return; } $saved = '' !== (string) $o['secret_hash_key']; printf( '', esc_attr( RELEASEBIRD_OPTION ), esc_attr( $saved ? __( 'Saved — enter a new key to replace it', 'releasebird' ) : '' ) ); if ( $saved ) { printf( '

', esc_attr( RELEASEBIRD_OPTION ), esc_html__( 'Remove the saved key', 'releasebird' ) ); } echo '

' . esc_html__( 'From Releasebird → Settings → Widgets → “User verification”. Leave empty to load the widget anonymously. You can also define RELEASEBIRD_SECRET_HASH_KEY in wp-config.php instead of saving it here.', 'releasebird' ) . '

'; } public function field_identifier_source() { $o = $this->get_options(); $current = $o['identifier_source']; printf( ''; echo '

' . esc_html__( 'Which value identifies the user in Releasebird. It must stay stable across sessions; the signature is computed over exactly this value. If users can change their email address, choose the WordPress user ID.', 'releasebird' ) . '

'; } public function render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $o = $this->get_options(); ?>

' . esc_html__( 'Settings', 'releasebird' ) . ''; array_unshift( $links, $link ); return $links; } /** * Suggested text for the site's privacy policy (Settings → Privacy). */ public function add_privacy_policy_content() { if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { return; } $content = '

' . esc_html__( 'This site uses the Releasebird widget for product updates, feedback and support. The widget is loaded from Releasebird’s servers; your browser therefore connects to them and transmits technical data such as your IP address. Messages, feedback and contact details you enter in the widget are processed by Releasebird on our behalf. If you are logged in, your name and email address may be passed to the widget so we can recognise you.', 'releasebird' ) . '

'; wp_add_privacy_policy_content( 'Releasebird', wp_kses_post( $content ) ); } /* -------------------------------------------------------------------- */ /* Front-end output */ /* -------------------------------------------------------------------- */ /** * Whether the widget loads on the current request. * * @param array $o Options. * @return bool */ private function should_load( $o ) { $load = ! empty( $o['enabled'] ) && '' !== (string) $o['api_key']; if ( $load && ! empty( $o['logged_in_only'] ) && ! is_user_logged_in() ) { $load = false; } // Not inside the Customizer preview or on feeds. if ( $load && ( is_customize_preview() || is_feed() ) ) { $load = false; } /** * Filters whether the Releasebird widget loads on the current page, e.g. to skip * landing pages or checkout: add_filter( 'releasebird_load_widget', fn( $load ) => $load && ! is_page( 'checkout' ) ); * * @param bool $load Whether to load the widget. */ return (bool) apply_filters( 'releasebird_load_widget', $load ); } /** * Enqueues one small inline script in the footer. It loads the hosted widget script * asynchronously and starts the widget once it is there — so the page never waits for it. * The embed key is public, so it appears in the markup. */ public function enqueue_widget() { $o = $this->get_options(); if ( ! $this->should_load( $o ) ) { return; } $config = array( 'key' => (string) $o['api_key'], 'src' => esc_url_raw( RELEASEBIRD_WIDGET_SRC ), 'hideButton' => ! empty( $o['hide_button'] ), 'openSelector' => (string) $o['open_selector'], ); $identity = $this->build_identity( $o ); if ( $identity ) { $config['identity'] = $identity['identity']; $config['hash'] = $identity['hash']; } elseif ( '' !== $this->get_secret( $o ) ) { // Verification is on and nobody is logged in: sign a previously recognised user out of the widget. $config['logout'] = true; } // JSON_HEX_* keeps values safe to embed directly inside a