'defer', 'in_footer' => false ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion } add_filter( 'script_loader_tag', 'larvabot_script_tag', 10, 2 ); /** * The snippet reads its project from data-key. * * @param string $tag The script tag. * @param string $handle Script handle. * @return string */ function larvabot_script_tag( $tag, $handle ) { if ( 'larvabot' !== $handle ) { return $tag; } return preg_replace( '/ 'string', 'sanitize_callback' => 'larvabot_sanitize_key', 'default' => '', ) ); // Back from Larvabot's Connect page with the chosen project's key. The state proves this admin // started the connection here (it's kept per user for 30 minutes), so a crafted link can't set it. // phpcs:disable WordPress.Security.NonceVerification.Recommended -- the one-time state below is the nonce. if ( ! isset( $_GET['page'], $_GET['larvabot_key'], $_GET['larvabot_state'] ) || 'larvabot' !== $_GET['page'] ) { return; } if ( ! current_user_can( 'manage_options' ) ) { return; } $state = sanitize_text_field( wp_unslash( $_GET['larvabot_state'] ) ); $key = sanitize_text_field( wp_unslash( $_GET['larvabot_key'] ) ); $project = isset( $_GET['larvabot_project'] ) ? sanitize_text_field( wp_unslash( $_GET['larvabot_project'] ) ) : ''; $expected = get_transient( 'larvabot_state_' . get_current_user_id() ); // phpcs:enable $result = 'failed'; if ( is_string( $expected ) && '' !== $expected && hash_equals( $expected, $state ) && preg_match( '/^[a-z0-9]{8,40}$/', $key ) ) { delete_transient( 'larvabot_state_' . get_current_user_id() ); update_option( 'larvabot_tracking_key', $key ); update_option( 'larvabot_project', $project ); $result = 'connected'; } wp_safe_redirect( admin_url( 'options-general.php?page=larvabot&larvabot=' . $result ) ); exit; } /** * The Connect URL, with a fresh one-time state for this admin. * * @return string */ function larvabot_connect_url() { $state = wp_generate_password( 32, false ); set_transient( 'larvabot_state_' . get_current_user_id(), $state, 30 * MINUTE_IN_SECONDS ); return add_query_arg( array( 'site' => rawurlencode( home_url( '/' ) ), 'return' => rawurlencode( admin_url( 'options-general.php?page=larvabot' ) ), 'state' => $state, ), LARVABOT_APP . '/connect/wordpress' ); } add_action( 'admin_enqueue_scripts', 'larvabot_admin_assets' ); /** * Larvabot's own look, on its settings screen only. * * @param string $hook Current admin page. */ function larvabot_admin_assets( $hook ) { if ( 'settings_page_larvabot' === $hook ) { wp_enqueue_style( 'larvabot-admin', plugins_url( 'assets/admin.css', __FILE__ ), array(), LARVABOT_VERSION ); } } add_action( 'admin_post_larvabot_disconnect', 'larvabot_disconnect' ); /** * Disconnect: removes the key, so the snippet leaves every page. */ function larvabot_disconnect() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'larvabot' ), 403 ); } check_admin_referer( 'larvabot_disconnect' ); delete_option( 'larvabot_tracking_key' ); delete_option( 'larvabot_project' ); wp_safe_redirect( admin_url( 'options-general.php?page=larvabot&larvabot=disconnected' ) ); exit; } /** * The settings page. */ function larvabot_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $key = larvabot_key(); $project = (string) get_option( 'larvabot_project', '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. $result = isset( $_GET['larvabot'] ) ? sanitize_key( wp_unslash( $_GET['larvabot'] ) ) : ''; $notices = array( 'connected' => array( 'ok', __( 'Connected. The Larvabot snippet is now on every page of your site.', 'larvabot' ) ), 'disconnected' => array( 'info', __( 'Disconnected. The snippet has been removed from your site.', 'larvabot' ) ), 'failed' => array( 'bad', __( "That connection expired or didn't start here. Click Connect to Larvabot again.", 'larvabot' ) ), ); $assets = plugins_url( 'assets/', __FILE__ ); ?>


larvabot ↗

' . esc_html( $project ) . '' ); } else { esc_html_e( 'Tracking visits on every page', 'larvabot' ); } ?>

' . esc_html__( 'Settings', 'larvabot' ) . '' ); return $links; } add_action( 'admin_notices', 'larvabot_setup_notice' ); /** * Until connected, a reminder on the Plugins screen only. */ function larvabot_setup_notice() { $screen = get_current_screen(); if ( larvabot_key() || ! $screen || 'plugins' !== $screen->id || ! current_user_can( 'manage_options' ) ) { return; } printf( '

%s %s

', esc_html__( 'Larvabot is installed. Connect it to your project to start seeing which emails and posts send you visitors.', 'larvabot' ), esc_url( admin_url( 'options-general.php?page=larvabot' ) ), esc_html__( 'Connect now', 'larvabot' ) ); }