'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 '