$src ) { foreach ( (array) $src['hosts'] as $h ) { if ( '' !== $host && ( $host === $h || substr( $host, - ( strlen( $h ) + 1 ) ) === '.' . $h ) ) { return $id; } } } if ( '' !== $utm ) { foreach ( self::sources() as $id => $src ) { if ( in_array( $utm, (array) $src['utm'], true ) ) { return $id; } } } return ''; } /** * Enqueue the beacon. */ public static function enqueue() { if ( is_admin() || is_preview() || is_customize_preview() ) { return; } wp_register_script( 'aeoora-ref', AEOORA_URL . 'assets/js/beacon.js', array(), AEOORA_VERSION, array( 'in_footer' => true, 'strategy' => 'defer', ) ); $data = array( 'u' => admin_url( 'admin-ajax.php?action=aeoora_ref' ), 'id' => is_singular() ? (int) get_queried_object_id() : 0, ); // The default host map ships inside the cached beacon.js. Only send it // inline when a filter has changed the list of sources (only then is // it worth building both maps on a page view). if ( has_filter( 'aeoora_ai_referrers' ) ) { $map = self::beacon_map( self::sources() ); if ( $map !== self::beacon_map( self::default_sources() ) ) { $data['h'] = $map['h']; $data['t'] = $map['t']; } } wp_add_inline_script( 'aeoora-ref', 'window.aeooraRef=' . wp_json_encode( $data ) . ';', 'before' ); wp_enqueue_script( 'aeoora-ref' ); } /** * Unfiltered source list from the data file. * * @return array */ private static function default_sources() { $out = array(); foreach ( (array) include AEOORA_DIR . 'data/referrers.php' as $src ) { $out[ $src['id'] ] = $src; } return $out; } /** * Host / utm lookup map used by the beacon (same shape as tools/make-beacon.php). * * @param array $sources Sources keyed by id. * @return array */ private static function beacon_map( array $sources ) { $hosts = array(); $utms = array(); foreach ( $sources as $id => $src ) { foreach ( (array) $src['hosts'] as $h ) { $hosts[ $h ] = (string) $id; } foreach ( (array) $src['utm'] as $u ) { $utms[ $u ] = (string) $id; } } return array( 'h' => $hosts, 't' => $utms, ); } /** * Exclude from JS delay / defer optimizations. * * @param array $list Exclusions. * @return array */ public static function exclude_from_delay( $list ) { $list = (array) $list; $list[] = 'aeoora'; $list[] = 'aeooraRef'; return $list; } /** * Keep Cloudflare Rocket Loader away from the beacon. * * @param string $tag Tag. * @param string $handle Handle. * @return string */ public static function script_attrs( $tag, $handle ) { if ( 'aeoora-ref' === $handle && false === strpos( $tag, 'data-cfasync' ) ) { $tag = str_replace( '