isset( $saved['site_id'] ) && is_string( $saved['site_id'] ) ? $saved['site_id'] : '', 'enabled' => ! empty( $saved['enabled'] ), ); } /** * The tag is printed only when both a site ID and the explicit opt-in are saved. * * @return bool */ function tryftly_is_active() { $settings = tryftly_get_settings(); return '' !== $settings['site_id'] && $settings['enabled']; } /** * Front end: load the Tryftly SDK from the Tryftly service. Nothing is loaded until * tryftly_is_active() is true. */ function tryftly_enqueue_sdk() { if ( ! tryftly_is_active() || is_customize_preview() ) { return; } $settings = tryftly_get_settings(); $src = add_query_arg( 'site', rawurlencode( $settings['site_id'] ), TRYFTLY_SDK_URL ); // The version is null on purpose: the file is served by the Tryftly service, which // versions it itself, and the URL must stay exactly `sdk.js?site=`. wp_enqueue_script( TRYFTLY_SCRIPT_HANDLE, $src, array(), null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion array( 'strategy' => 'async', 'in_footer' => false, ) ); } add_action( 'wp_enqueue_scripts', 'tryftly_enqueue_sdk' ); /** * Add data-site-id to the SDK tag (the SDK reads it as a fallback for the `site` query). * * @param array $attributes Script tag attributes. * @return array */ function tryftly_script_attributes( $attributes ) { if ( isset( $attributes['id'] ) && TRYFTLY_SCRIPT_HANDLE . '-js' === $attributes['id'] ) { $settings = tryftly_get_settings(); $attributes['data-site-id'] = $settings['site_id']; } return $attributes; } add_filter( 'wp_script_attributes', 'tryftly_script_attributes' ); /** * Settings → Tryftly. */ function tryftly_admin_menu() { add_options_page( __( 'Tryftly', 'tryftly' ), __( 'Tryftly', 'tryftly' ), 'manage_options', 'tryftly', 'tryftly_render_settings_page' ); } add_action( 'admin_menu', 'tryftly_admin_menu' ); /** * Register the single option that holds the settings. */ function tryftly_register_settings() { register_setting( 'tryftly', TRYFTLY_OPTION, array( 'type' => 'array', 'sanitize_callback' => 'tryftly_sanitize_settings', 'default' => array( 'site_id' => '', 'enabled' => false, ), ) ); } add_action( 'admin_init', 'tryftly_register_settings' ); /** * Accept a bare site ID or a whole pasted tag, and keep only the ID. * * @param mixed $input Raw form input. * @return array{site_id: string, enabled: bool} */ function tryftly_sanitize_settings( $input ) { $current = tryftly_get_settings(); $input = is_array( $input ) ? $input : array(); $raw = isset( $input['site_id'] ) && is_string( $input['site_id'] ) ? trim( wp_unslash( $input['site_id'] ) ) : ''; $site_id = ''; if ( '' !== $raw ) { if ( preg_match( TRYFTLY_SITE_ID_PATTERN, $raw, $matches ) ) { $site_id = strtolower( $matches[0] ); } else { $site_id = $current['site_id']; add_settings_error( TRYFTLY_OPTION, 'tryftly_invalid_site_id', __( 'That does not look like a Tryftly tag or site ID. In Tryftly, copy the tag from Site settings → Measurement → Tracking snippet.', 'tryftly' ) ); } } return array( 'site_id' => $site_id, 'enabled' => ! empty( $input['enabled'] ), ); } /** * "Settings" link in the plugin list row. * * @param string[] $links Existing action links. * @return string[] */ function tryftly_action_links( $links ) { $url = admin_url( 'options-general.php?page=tryftly' ); array_unshift( $links, '' . esc_html__( 'Settings', 'tryftly' ) . '' ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'tryftly_action_links' ); /** * Render Settings → Tryftly. */ function tryftly_render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = tryftly_get_settings(); $links = array( 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array(), ), ); ?>