array, 'negate' => bool], or null for no segment. * @throws ApiException When the segment doesn't exist or doesn't apply to the report. */ public static function resolve( Profile $site, $param, array $allowed ) { $param = (string) $param; if ( '' === $param ) { return null; } $negate = '!' === $param[0]; $id = ltrim( $param, '!' ); foreach ( $site->segments as $segment ) { if ( $segment['id'] === $id ) { if ( ! in_array( $segment['type'], $allowed, true ) ) { throw new ApiException( __( 'That segment doesn\'t apply to this part of the report.', 'turtlereport' ), ApiException::INVALID ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Caught by the plugin; escaped where the message is shown. } return array( 'segment' => $segment, 'negate' => $negate, ); } } throw new ApiException( __( 'That segment no longer exists. Reload the page.', 'turtlereport' ), ApiException::INVALID ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Caught by the plugin; escaped where the message is shown. } /** * Search Console dimensionFilterGroups for a resolved segment. * * @param array $resolved Result of resolve(). * @return array */ public static function gsc( array $resolved ) { return array( array( 'filters' => array( array( 'dimension' => $resolved['segment']['type'], 'operator' => $resolved['negate'] ? 'excludingRegex' : 'includingRegex', 'expression' => '(?i)' . Segments::pattern( $resolved['segment'] ), ), ), ), ); } /** * GA4 filter on landing page for a resolved page segment. * * @param array $resolved Result of resolve(). * @return array */ public static function ga( array $resolved ) { $filter = Filters::regex( 'landingPage', Segments::pattern( $resolved['segment'] ) ); return $resolved['negate'] ? array( 'notExpression' => $filter ) : $filter; } }