'helpdot' ), $args ), admin_url( 'options-general.php' ) ); } add_action( 'wp_enqueue_scripts', function () { if ( get_option( 'helpdot_bot_id' ) && get_option( 'helpdot_enabled', '1' ) === '1' ) { wp_enqueue_script( 'helpdot', HELPDOT_URL . '/w.js', array(), HELPDOT_VERSION, array( 'strategy' => 'async', 'in_footer' => true ) ); } } ); add_filter( 'script_loader_tag', function ( $tag, $handle ) { if ( 'helpdot' !== $handle ) { return $tag; } return str_replace( ' src=', ' data-bot="' . esc_attr( get_option( 'helpdot_bot_id' ) ) . '" src=', $tag ); }, 10, 2 ); add_action( 'admin_menu', function () { add_options_page( 'Helpdot', 'Helpdot', 'manage_options', 'helpdot', 'helpdot_settings_page' ); } ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function ( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'helpdot' ) . '' ); return $links; } ); // Saves the bot sent back by helpdot.app, only if it answers our own request add_action( 'admin_init', function () { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- the one-time state is checked instead. if ( ! isset( $_GET['page'], $_GET['helpdot_bot'], $_GET['helpdot_state'] ) || 'helpdot' !== $_GET['page'] || ! current_user_can( 'manage_options' ) ) { return; } $bot = sanitize_text_field( wp_unslash( $_GET['helpdot_bot'] ) ); $state = sanitize_text_field( wp_unslash( $_GET['helpdot_state'] ) ); // phpcs:enable $key = 'helpdot_state_' . get_current_user_id(); $expected = get_transient( $key ); $ok = $expected && hash_equals( $expected, $state ) && helpdot_is_bot_id( $bot ); if ( $ok ) { delete_transient( $key ); update_option( 'helpdot_bot_id', $bot ); update_option( 'helpdot_enabled', '1' ); } wp_safe_redirect( helpdot_page_url( array( 'helpdot_notice' => $ok ? 'connected' : 'failed' ) ) ); exit; } ); // Each handler also calls check_admin_referer itself, so code checkers can see it function helpdot_check_user() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'helpdot' ) ); } } function helpdot_back( $notice ) { wp_safe_redirect( helpdot_page_url( array( 'helpdot_notice' => $notice ) ) ); exit; } add_action( 'admin_post_helpdot_connect', function () { helpdot_check_user(); check_admin_referer( 'helpdot_connect' ); $state = wp_generate_password( 32, false ); set_transient( 'helpdot_state_' . get_current_user_id(), $state, HOUR_IN_SECONDS ); $url = HELPDOT_URL . '/connect/wordpress?' . http_build_query( array( 'site' => home_url(), 'return' => helpdot_page_url(), 'state' => $state, ), '', '&' ); wp_redirect( $url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- our own service. exit; } ); add_action( 'admin_post_helpdot_save', function () { helpdot_check_user(); check_admin_referer( 'helpdot_save' ); update_option( 'helpdot_enabled', isset( $_POST['helpdot_enabled'] ) ? '1' : '0' ); helpdot_back( 'saved' ); } ); add_action( 'admin_post_helpdot_manual', function () { helpdot_check_user(); check_admin_referer( 'helpdot_manual' ); $bot = isset( $_POST['helpdot_bot_id'] ) ? strtolower( trim( sanitize_text_field( wp_unslash( $_POST['helpdot_bot_id'] ) ) ) ) : ''; if ( ! helpdot_is_bot_id( $bot ) ) { helpdot_back( 'bad_id' ); } update_option( 'helpdot_bot_id', $bot ); update_option( 'helpdot_enabled', '1' ); helpdot_back( 'connected' ); } ); add_action( 'admin_post_helpdot_disconnect', function () { helpdot_check_user(); check_admin_referer( 'helpdot_disconnect' ); delete_option( 'helpdot_bot_id' ); delete_option( 'helpdot_enabled' ); helpdot_back( 'disconnected' ); } ); function helpdot_form_start( $action ) { echo '
'; echo ''; wp_nonce_field( $action ); } function helpdot_settings_page() { $bot = get_option( 'helpdot_bot_id' ); $notices = array( 'connected' => array( 'success', __( 'Connected. Open your site to see the bubble. If you use a cache plugin, clear its cache first.', 'helpdot' ) ), 'saved' => array( 'success', __( 'Saved.', 'helpdot' ) ), 'disconnected' => array( 'success', __( 'Disconnected. The bubble is off your site.', 'helpdot' ) ), 'failed' => array( 'error', __( 'We could not connect. Click Connect and try again.', 'helpdot' ) ), 'bad_id' => array( 'error', __( 'That is not a bot ID. Copy the value of data-bot from the Install page in Helpdot.', 'helpdot' ) ), ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only picks a message. $notice = isset( $_GET['helpdot_notice'] ) ? sanitize_key( wp_unslash( $_GET['helpdot_notice'] ) ) : ''; echo '

Helpdot

'; if ( isset( $notices[ $notice ] ) ) { echo '

' . esc_html( $notices[ $notice ][1] ) . '

'; } if ( $bot ) { echo '

' . esc_html__( 'Your site is connected to your bot.', 'helpdot' ) . '

'; echo '

' . esc_html__( 'Open my bot in Helpdot', 'helpdot' ) . '

'; helpdot_form_start( 'helpdot_save' ); echo '

'; submit_button( __( 'Save', 'helpdot' ), 'secondary', 'submit', false ); echo ''; helpdot_form_start( 'helpdot_disconnect' ); echo '

'; } else { echo '

' . esc_html__( 'Connect your site to show a chat bubble that answers your visitors. The free plan needs no card.', 'helpdot' ) . '

'; echo '

' . esc_html__( 'You need a free Helpdot account. Click Connect to open helpdot.app, then sign up or log in. It receives your site and admin page addresses.', 'helpdot' ) . '

'; helpdot_form_start( 'helpdot_connect' ); submit_button( __( 'Connect', 'helpdot' ), 'primary', 'submit', false ); echo ''; echo '
' . esc_html__( 'Connect with a bot ID instead', 'helpdot' ) . ''; echo '

' . esc_html__( 'Find it in the line of code on the Install page: the value of data-bot.', 'helpdot' ) . '

'; helpdot_form_start( 'helpdot_manual' ); echo ' '; submit_button( __( 'Save', 'helpdot' ), 'secondary', 'submit', false ); echo '
'; } echo '
'; }