'string', 'sanitize_callback' => 'convertlyft_sanitize_key', 'default' => '', ) ); add_settings_section( 'convertlyft_main', '', '__return_false', 'convertlyft' ); add_settings_field( CONVERTLYFT_OPTION, esc_html__( 'Site key', 'convertlyft' ), 'convertlyft_render_key_field', 'convertlyft', 'convertlyft_main', array( 'label_for' => CONVERTLYFT_OPTION ) ); } add_action( 'admin_init', 'convertlyft_register_settings' ); /** * Render the site key input. */ function convertlyft_render_key_field() { printf( '', esc_attr( CONVERTLYFT_OPTION ), esc_attr( get_option( CONVERTLYFT_OPTION, '' ) ) ); echo '

'; printf( /* translators: %s: link to the Convertlyft sign-in page. */ esc_html__( 'Your site key starts with pk_. Find it in the install snippet in your %s workspace. Leave the field empty to remove the tag.', 'convertlyft' ), '' . esc_html__( 'Convertlyft', 'convertlyft' ) . '' ); echo '

'; } /** * Add the page under Settings. */ function convertlyft_add_settings_page() { add_options_page( esc_html__( 'Convertlyft', 'convertlyft' ), esc_html__( 'Convertlyft', 'convertlyft' ), 'manage_options', 'convertlyft', 'convertlyft_render_settings_page' ); } add_action( 'admin_menu', 'convertlyft_add_settings_page' ); /** * Render the settings page. settings_fields() prints the nonce; options.php checks it and manage_options. */ function convertlyft_render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $key = convertlyft_get_key(); ?>

' . esc_html__( 'Settings', 'convertlyft' ) . '' ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'convertlyft_action_links' ); /** * Enqueue the tag on the front end, only when a valid key is set. */ function convertlyft_enqueue_tag() { if ( is_admin() || '' === convertlyft_get_key() ) { return; } wp_enqueue_script( 'convertlyft', CONVERTLYFT_TAG_SRC, array(), null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Remote tag versioned by convertlyft.com; the URL stays exactly as in the install snippet. array( 'strategy' => 'defer', 'in_footer' => false, ) ); } add_action( 'wp_enqueue_scripts', 'convertlyft_enqueue_tag' ); /** * Add data-workspace-key (and defer, for WordPress before 6.3) to the tag. * * @param string $tag The script tag HTML. * @param string $handle Script handle. * @return string */ function convertlyft_script_tag( $tag, $handle ) { if ( 'convertlyft' !== $handle ) { return $tag; } $key = convertlyft_get_key(); if ( '' === $key ) { return $tag; } $extra = ' data-workspace-key="' . esc_attr( $key ) . '"'; if ( false === strpos( $tag, ' defer' ) ) { $extra .= ' defer'; } return preg_replace( '/