Skip to content

Commit

Permalink
Merge branch 'develop' into enhancement/9570-skeleton-loader-wp-dashb…
Browse files Browse the repository at this point in the history
…oard.
  • Loading branch information
ankitrox committed Jan 2, 2025
2 parents 9b92b2e + 3135680 commit 80317c6
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 32 deletions.
11 changes: 3 additions & 8 deletions assets/js/components/settings/SettingsActiveModule/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
import PropTypes from 'prop-types';
import { useHistory, useParams } from 'react-router-dom';
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -42,7 +41,6 @@ import Link from '../../Link';
import { trackEvent } from '../../../util';
import { clearCache } from '../../../googlesitekit/api/cache';
import { CORE_UI } from '../../../googlesitekit/datastore/ui/constants';
import { CORE_USER } from '../../../googlesitekit/datastore/user/constants';
import useViewContext from '../../../hooks/useViewContext';

export default function Footer( props ) {
Expand Down Expand Up @@ -80,12 +78,9 @@ export default function Footer( props ) {
select( CORE_UI ).getValue( isSavingKey )
);

const moduleHomepage = useSelect( ( select ) => {
if ( ! module || isEmpty( module.homepage ) ) {
return undefined;
}
return select( CORE_USER ).getAccountChooserURL( module.homepage );
} );
const moduleHomepage = useSelect( ( select ) =>
select( CORE_MODULES ).getDetailsLinkURL( slug )
);

const { submitChanges } = useDispatch( CORE_MODULES );
const { clearErrors } = useDispatch( module?.storeName ) || {};
Expand Down
40 changes: 40 additions & 0 deletions assets/js/googlesitekit/modules/datastore/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,46 @@ const baseSelectors = {
getRecoveredModules( state ) {
return state.recoveredModules;
},

/**
* Gets the details link URL for a module.
*
* Returns the module homepage by default. This can be overwritten by a
* custom selector of the same name in the module store implementation.
*
* @since n.e.x.t
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(string|null|undefined)} Details link URL; `null` if module is not available, or does not have a homepage. `undefined` if data is still loading.
*/
getDetailsLinkURL: createRegistrySelector(
( select ) => ( state, slug ) => {
const module = select( CORE_MODULES ).getModule( slug );

if ( module === undefined ) {
return undefined;
}

if ( module === null ) {
return null;
}

const storeName = select( CORE_MODULES ).getModuleStoreName( slug );

const { getDetailsLinkURL } = select( storeName ) || {};

if ( typeof getDetailsLinkURL === 'function' ) {
return getDetailsLinkURL();
}

if ( ! module.homepage ) {
return null;
}

return select( CORE_USER ).getAccountChooserURL( module.homepage );
}
),
};

const store = combineStores(
Expand Down
91 changes: 91 additions & 0 deletions assets/js/googlesitekit/modules/datastore/modules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* Internal dependencies
*/
import API from 'googlesitekit-api';
import Modules from 'googlesitekit-modules';
import { combineStores } from 'googlesitekit-data';
import {
createTestRegistry,
muteFetch,
Expand Down Expand Up @@ -2186,5 +2188,94 @@ describe( 'core/modules modules', () => {
await waitForDefaultTimeouts();
} );
} );

describe( 'getDetailsLinkURL', () => {
it( 'should return null if module is not found', () => {
registry.dispatch( CORE_MODULES ).receiveGetModules( FIXTURES );

expect(
registry
.select( CORE_MODULES )
.getDetailsLinkURL( 'unregistered-module' )
).toBeNull();
} );

it( 'should return null if module does not define homepage', () => {
registry.dispatch( CORE_MODULES ).receiveGetModules( [
{
slug: 'search-console',
name: 'Search Console',
active: true,
connected: true,
},
] );

expect(
registry
.select( CORE_MODULES )
.getDetailsLinkURL( 'search-console' )
).toBeNull();
} );

it( 'should return module homepage', () => {
registry.dispatch( CORE_MODULES ).receiveGetModules( [
{
slug: 'search-console',
name: 'Search Console',
homepage: 'https://example.com',
active: true,
connected: true,
},
] );
registry
.dispatch( CORE_USER )
.receiveUserInfo( { email: '[email protected]' } );

expect(
registry
.select( CORE_MODULES )
.getDetailsLinkURL( 'search-console' )
).toBe(
'https://accounts.google.com/accountchooser?continue=https%3A%2F%2Fexample.com&Email=test%40example.com'
);
} );

it( 'should be overridden by module defined selector', () => {
const slug = 'test-module';
const moduleStoreName = `test/${ slug }`;

registry.registerStore(
moduleStoreName,
combineStores(
Modules.createModuleStore( slug, {
storeName: moduleStoreName,
} ),
{
selectors: {
getDetailsLinkURL: () =>
'https://example.com/custom-link',
},
}
)
);

registry
.dispatch( CORE_MODULES )
.registerModule( slug, { storeName: moduleStoreName } );

registry.dispatch( CORE_MODULES ).receiveGetModules( [
{
slug,
name: 'Test Module',
active: true,
connected: true,
},
] );

expect(
registry.select( CORE_MODULES ).getDetailsLinkURL( slug )
).toBe( 'https://example.com/custom-link' );
} );
} );
} );
} );
12 changes: 12 additions & 0 deletions assets/js/modules/adsense/datastore/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ export const selectors = {
return select( MODULES_ADSENSE ).getServiceURL( { path, query } );
}
),

/**
* Overrides the details link URL for this module.
*
* @since n.e.x.t
*
* @return {(string|undefined)} AdSense account sites list URL (or `undefined` if not loaded).
*/
getDetailsLinkURL: createRegistrySelector(
( select ) => () =>
select( MODULES_ADSENSE ).getServiceAccountManageSitesURL()
),
};

const store = {
Expand Down
65 changes: 52 additions & 13 deletions fpm/measurement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @copyright 2024 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*
* @version 57f0a63
* @version 07651f2
*
* NOTICE: This file has been modified from its original version in accordance with the Apache License, Version 2.0.
*/
Expand Down Expand Up @@ -73,17 +73,15 @@ public function run()

$fpsUrl = 'https://' . $tagId . '.fps.goog/' . self::FPS_PATH . $path;

if (self::isScriptRequest($path)) {
$response = $this->helper->sendRequest($fpsUrl);
$response = $this->helper->sendRequest($fpsUrl);
if (self::isScriptResponse($response['headers'])) {
$response['body'] = str_replace(
'/' . self::FPS_PATH . '/',
$redirectorFile . self::TAG_ID_QUERY . $tagId . self::PATH_QUERY,
$response['body']
);
return $response;
} else {
return $this->helper->sendRequest($fpsUrl);
}
return $response;
}

private static function appendRequestIP($path)
Expand All @@ -107,13 +105,40 @@ private static function appendRequestIP($path)
}
}

/**
* Use best effort for determing if a request path is a script request.
*
* @param string $requestPath
*/
private static function isScriptRequest($requestPath)
{
return substr($requestPath, 0, 7) === "/gtm.js"
|| substr($requestPath, 0, 8) === "/gtag.js"
|| substr($requestPath, 0, 8) === "/gtag/js";
}

/**
* @param string[] $headers
*/
private static function isScriptResponse($headers)
{
if (empty($headers)) {
return false;
}

foreach ($headers as $header) {
if (empty($headers)) {
continue;
}

$normalizedHeader = strtolower(str_replace(' ', '', $header));
if (strpos($normalizedHeader, 'content-type:application/javascript') === 0) {
return true;
}
}
return false;
}


private static function extractParameters()
{
Expand Down Expand Up @@ -169,7 +194,7 @@ public function invalidRequest($statsCode): void
/**
* Set the headers from a headers array.
*
* @param array<string, string> $headers
* @param string[] $headers
*/
public function setHeaders($headers): void
{
Expand All @@ -186,7 +211,7 @@ public function setHeaders($headers): void
* @param string $url
* @return array{
* body: string,
* headers: array<string, string>,
* headers: string[],
* statusCode: int,
* }
*/
Expand All @@ -204,7 +229,7 @@ public function sendRequest($url): array
* @param string $url
* @return array{
* body: string,
* headers: array<string, string>,
* headers: string[],
* statusCode: int,
* }
*/
Expand All @@ -222,6 +247,7 @@ protected function sendCurlRequest($url): array
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headersString = substr($result, 0, $headerSize);
$headers = explode("\r\n", $headersString);
$headers = $this->normalizeHeaders($headers);

$body = substr($result, $headerSize);

Expand All @@ -238,17 +264,17 @@ protected function sendCurlRequest($url): array
* @param string $url
* @return array{
* body: string,
* headers: array<string, string>,
* headers: string[],
* statusCode: int,
* }
*/
protected function sendFileGetContents($url): array
{
$streamContext = array(
$streamContext = stream_context_create(array(
"http" => array(
"method" => "GET",
)
);
));

// Calling file_get_contents will set the variable $http_response_header
// within the local scope.
Expand All @@ -264,8 +290,8 @@ protected function sendFileGetContents($url): array
// value from the headers.
preg_match('/HTTP\/\d\.\d\s+(\d+)/', $headers[0], $statusHeader);
$statusCode = intval($statusHeader[1]) ?? 200;
array_shift($headers);
}
$headers = $this->normalizeHeaders($headers);

return array(
'body' => $result,
Expand All @@ -278,6 +304,19 @@ protected function isCurlInstalled(): bool
{
return extension_loaded('curl');
}

/** @param string[] $headers */
protected function normalizeHeaders($headers): array
{
if (empty($headers)) {
return $headers;
}

// The first element in the headers array will be the HTTP version
// and status code used, this value is not needed in the headers.
array_shift($headers);
return $headers;
}
}
// REQUEST_HELPER_END

Expand Down
13 changes: 2 additions & 11 deletions includes/Modules/Ads/Web_Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Google\Site_Kit\Core\Modules\Tags\Module_Web_Tag;
use Google\Site_Kit\Core\Tags\GTag;
use Google\Site_Kit\Core\Tags\Tag_With_Linker_Interface;
use Google\Site_Kit\Core\Tags\Tag_With_Linker_Trait;
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;

/**
Expand All @@ -25,17 +26,7 @@
class Web_Tag extends Module_Web_Tag implements Tag_With_Linker_Interface {

use Method_Proxy_Trait;

/**
* Sets the current home domain.
*
* @since 1.125.0
*
* @param string $domain Domain name.
*/
public function set_home_domain( $domain ) {
$this->home_domain = $domain;
}
use Tag_With_Linker_Trait;

/**
* Registers tag hooks.
Expand Down

0 comments on commit 80317c6

Please sign in to comment.