Skip to content

Commit

Permalink
Merge branch 'develop' into enhancement/4145-adsense-cta-widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvinb committed Nov 16, 2021
2 parents 2a779f6 + 42aec98 commit 88e1478
Show file tree
Hide file tree
Showing 24 changed files with 346 additions and 118 deletions.
2 changes: 2 additions & 0 deletions assets/js/feature-tours/index.js
Expand Up @@ -31,6 +31,7 @@ import { UI_ALL_TRAFFIC_LOADED } from '../modules/analytics/datastore/constants'
import helpVisibility from './help-visibility';
import ideaHubModule from './idea-hub-module';
import ideaHubPostsTable from './idea-hub-posts-table';
import pagespeedInsightsForceActive from './pagespeed-insights-force-active';

const allTrafficWidget = {
slug: 'allTrafficWidget',
Expand Down Expand Up @@ -104,4 +105,5 @@ export default [
helpVisibility,
ideaHubModule,
ideaHubPostsTable,
pagespeedInsightsForceActive,
];
85 changes: 85 additions & 0 deletions assets/js/feature-tours/pagespeed-insights-force-active.js
@@ -0,0 +1,85 @@
/**
* Page Speed Insight Force Active tour for the dashboard.
*
* Site Kit by Google, Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/*
* Internal dependencies
*/
import {
VIEW_CONTEXT_DASHBOARD,
VIEW_CONTEXT_PAGE_DASHBOARD,
} from '../googlesitekit/constants';
import { CORE_SITE } from '../googlesitekit/datastore/site/constants';
import {
MODULES_PAGESPEED_INSIGHTS,
STRATEGY_DESKTOP,
STRATEGY_MOBILE,
} from '../modules/pagespeed-insights/datastore/constants';

const pagespeedInsightsForceActive = {
slug: 'pagespeedInsightsForceActive',
contexts: [ VIEW_CONTEXT_DASHBOARD, VIEW_CONTEXT_PAGE_DASHBOARD ],
version: '1.46.0',
gaEventCategory: ( viewContext ) =>
`${ viewContext }_pagespeed-widget-force-active`,
checkRequirements: ( {
select,
__experimentalResolveSelect: resolveSelect,
} ) => {
if ( ! global._googlesitekitBaseData.showPSIForceActiveTour ) {
return false;
}

const referenceURL = select( CORE_SITE ).getCurrentReferenceURL();
const { getReport: asyncGetReport } = resolveSelect(
MODULES_PAGESPEED_INSIGHTS
);

// Only show the tour if reports are available within .5 sec.
return Promise.race( [
// If both reports finish first, the tour will be shown.
Promise.all( [
asyncGetReport( referenceURL, STRATEGY_DESKTOP ),
asyncGetReport( referenceURL, STRATEGY_MOBILE ),
] ).then( () => true ),
// If the timeout resolves first, the tour will not be shown.
new Promise( ( resolve ) => setTimeout( resolve, 500 ) ).then(
() => false
),
] );
},
steps: [
{
target: '.googlesitekit-widget-area--dashboardSpeed',
title: __(
'PageSpeed Insights is now active for everyone',
'google-site-kit'
),
content: __(
'Keep track of how your pages are doing in terms of speed and user experience and see recommendations on what to improve.',
'google-site-kit'
),
},
],
};

export default pagespeedInsightsForceActive;
Expand Up @@ -20,23 +20,12 @@
* Internal dependencies
*/
import DashboardPageSpeed from './DashboardPageSpeed';
import whenActive from '../../../../util/when-active';

function DashboardPageSpeedWidget( { Widget } ) {
export default function DashboardPageSpeedWidget( { Widget } ) {
// Pass class to omit regular widget padding and legacy widget class to use original styles.
return (
<Widget className="googlesitekit-pagespeed-widget" noPadding>
<DashboardPageSpeed />
</Widget>
);
}

export default whenActive( {
moduleName: 'pagespeed-insights',
FallbackComponent: ( { WidgetActivateModuleCTA } ) => (
<WidgetActivateModuleCTA moduleSlug="pagespeed-insights" />
),
IncompleteComponent: ( { WidgetCompleteModuleActivationCTA } ) => (
<WidgetCompleteModuleActivationCTA moduleSlug="pagespeed-insights" />
),
} )( DashboardPageSpeedWidget );
1 change: 0 additions & 1 deletion includes/Core/Assets/Assets.php
Expand Up @@ -12,7 +12,6 @@

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Storage\Cache;
use Google\Site_Kit\Core\Util\BC_Functions;
use Google\Site_Kit\Core\Util\Feature_Flags;
use WP_Dependencies;
Expand Down
17 changes: 16 additions & 1 deletion includes/Core/Modules/Modules.php
Expand Up @@ -20,6 +20,7 @@
use Google\Site_Kit\Core\Authentication\Authentication;
use Google\Site_Kit\Core\REST_API\Exception\Invalid_Datapoint_Exception;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Core\Util\Migration_1_46_0;
use Google\Site_Kit\Modules\AdSense;
use Google\Site_Kit\Modules\Analytics;
use Google\Site_Kit\Modules\Analytics_4;
Expand Down Expand Up @@ -256,7 +257,8 @@ function( Module $module ) {
}
);

$data['activeModules'] = array_keys( $non_internal_active_modules );
$data['activeModules'] = array_keys( $non_internal_active_modules );
$data['showPSIForceActiveTour'] = ! $this->manually_enabled( 'pagespeed-insights' ) && $this->options->get( Migration_1_46_0::OPTION_KEY_PSI_UPDATED );

return $data;
}
Expand Down Expand Up @@ -468,6 +470,19 @@ public function activate_module( $slug ) {
return true;
}

/**
* Checks whether the module identified by the given slug is enabled by the option.
*
* @since n.e.x.t
*
* @param string $slug Unique module slug.
* @return bool True if module has been manually enabled, false otherwise.
*/
private function manually_enabled( $slug ) {
$option = $this->get_active_modules_option();
return in_array( $slug, $option, true );
}

/**
* Deactivates the module identified by the given slug.
*
Expand Down
120 changes: 120 additions & 0 deletions includes/Core/Util/Migration_1_46_0.php
@@ -0,0 +1,120 @@
<?php
/**
* Migration for 1.46.0
*
* @package Google\Site_Kit\Core\Util
* @copyright 2021 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Core\Util;

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Authentication\Authentication;
use Google\Site_Kit\Core\Authentication\Google_Proxy;
use Google\Site_Kit\Core\Authentication\Profile;
use Google\Site_Kit\Core\Authentication\Verification_File;
use Google\Site_Kit\Core\Authentication\Verification_Meta;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use WP_User;
use WP_Error;

/**
* Class Migration_1_46_0
*
* @since 1.46.0
* @access private
* @ignore
*/
class Migration_1_46_0 {
/**
* Target DB version.
*/
const DB_VERSION = '1.46.0';

const OPTION_KEY_PSI_UPDATED = 'googlesitekit_psi_updated';

/**
* Context instance.
*
* @since 1.46.0
* @var Context
*/
protected $context;

/**
* Options instance.
*
* @since 1.46.0
* @var Options
*/
protected $options;

/**
* User_Options instance.
*
* @since 1.46.0
* @var User_Options
*/
protected $user_options;

/**
* Authentication instance.
*
* @since 1.46.0
* @var Authentication
*/
protected $authentication;

/**
* Constructor.
*
* @since 1.46.0
*
* @param Context $context Plugin context instance.
* @param Options $options Optional. Options instance.
* @param Authentication $authentication Optional. Authentication instance. Default is a new instance.
*/
public function __construct(
Context $context,
Options $options = null,
Authentication $authentication = null
) {
$this->context = $context;
$this->options = $options ?: new Options( $this->context );
$this->authentication = $authentication ?: new Authentication( $this->context, $this->options, $this->user_options );
}

/**
* Registers hooks.
*
* @since 1.46.0
*/
public function register() {
add_action( 'admin_init', array( $this, 'migrate' ) );
}

/**
* Migrates the DB.
*
* @since 1.46.0
*/
public function migrate() {
$db_version = $this->options->get( 'googlesitekit_db_version' );

// Do not run if database version already updated.
if ( $db_version && version_compare( $db_version, self::DB_VERSION, '>=' ) ) {
return;
}

if ( $this->authentication->is_setup_completed() ) {
$this->options->set( self::OPTION_KEY_PSI_UPDATED, true );
}

// Update database version.
$this->options->set( 'googlesitekit_db_version', self::DB_VERSION );
}
}
11 changes: 6 additions & 5 deletions includes/Modules/PageSpeed_Insights.php
Expand Up @@ -162,11 +162,12 @@ protected function setup_assets() {
*/
protected function setup_info() {
return array(
'slug' => 'pagespeed-insights',
'name' => _x( 'PageSpeed Insights', 'Service name', 'google-site-kit' ),
'description' => __( 'Google PageSpeed Insights gives you metrics about performance, accessibility, SEO and PWA', 'google-site-kit' ),
'order' => 4,
'homepage' => __( 'https://developers.google.com/speed/pagespeed/insights/', 'google-site-kit' ),
'slug' => 'pagespeed-insights',
'name' => _x( 'PageSpeed Insights', 'Service name', 'google-site-kit' ),
'description' => __( 'Google PageSpeed Insights gives you metrics about performance, accessibility, SEO and PWA', 'google-site-kit' ),
'order' => 4,
'homepage' => __( 'https://developers.google.com/speed/pagespeed/insights/', 'google-site-kit' ),
'force_active' => true,
);
}

Expand Down
1 change: 1 addition & 0 deletions includes/Plugin.php
Expand Up @@ -193,6 +193,7 @@ function() use ( $options, $activation_flag ) {
( new Core\User_Surveys\REST_User_Surveys_Controller( $authentication ) )->register();
( new Core\Util\Migration_1_3_0( $this->context, $options, $user_options ) )->register();
( new Core\Util\Migration_1_8_1( $this->context, $options, $user_options, $authentication ) )->register();
( new Core\Util\Migration_1_46_0( $this->context, $options, $authentication ) )->register();

// If a login is happening (runs after 'init'), update current user in dependency chain.
add_action(
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/specs/admin-tracking.test.js
Expand Up @@ -163,6 +163,14 @@ describe( 'initialization on load for Site Kit screens', () => {
)
) {
request.respond( { status: 200 } );
} else if (
request
.url()
.match(
'google-site-kit/v1/modules/pagespeed-insights/data/pagespeed'
)
) {
request.respond( { status: 200, body: JSON.stringify( {} ) } );
} else if (
request
.url()
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/specs/auth/gcp-flow-admin-2.test.js
Expand Up @@ -47,6 +47,14 @@ describe( 'the set up flow for the second administrator', () => {
)
) {
request.respond( { status: 200, body: JSON.stringify( {} ) } );
} else if (
request
.url()
.match(
'google-site-kit/v1/modules/pagespeed-insights/data/pagespeed'
)
) {
request.respond( { status: 200, body: JSON.stringify( {} ) } );
} else {
request.continue();
}
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/specs/auth/gcp-flow.test.js
Expand Up @@ -40,6 +40,14 @@ function handleRequest( request ) {
)
) {
request.respond( { status: 200, body: JSON.stringify( {} ) } );
} else if (
request
.url()
.match(
'google-site-kit/v1/modules/pagespeed-insights/data/pagespeed'
)
) {
request.respond( { status: 200, body: JSON.stringify( {} ) } );
} else if (
request
.url()
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/specs/auth/gcp-setup-search-console.test.js
Expand Up @@ -43,6 +43,14 @@ describe( 'Site Kit set up flow for the first time with search console setup', (
)
) {
request.respond( { status: 200, body: JSON.stringify( {} ) } );
} else if (
request
.url()
.match(
'google-site-kit/v1/modules/pagespeed-insights/data/pagespeed'
)
) {
request.respond( { status: 200, body: JSON.stringify( {} ) } );
} else {
request.continue();
}
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/specs/auth/gcp-setup-verification.test.js
Expand Up @@ -43,6 +43,14 @@ describe( 'Site Kit set up flow for the first time with site verification', () =
)
) {
request.respond( { status: 200, body: JSON.stringify( {} ) } );
} else if (
request
.url()
.match(
'google-site-kit/v1/modules/pagespeed-insights/data/pagespeed'
)
) {
request.respond( { status: 200, body: JSON.stringify( {} ) } );
} else {
request.continue();
}
Expand Down

0 comments on commit 88e1478

Please sign in to comment.