Skip to content

Commit 1ea1971

Browse files
authored
Merge pull request #1108 from google/enhance/1067-admin-bar-module-awareness
Introduce Module_With_Admin_Bar
2 parents dcf1bde + 35d64b8 commit 1ea1971

File tree

5 files changed

+171
-61
lines changed

5 files changed

+171
-61
lines changed

includes/Core/Admin_Bar/Admin_Bar.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace Google\Site_Kit\Core\Admin_Bar;
1212

1313
use Google\Site_Kit\Context;
14+
use Google\Site_Kit\Core\Modules\Module_With_Admin_Bar;
15+
use Google\Site_Kit\Core\Modules\Modules;
1416
use Google\Site_Kit\Core\Permissions\Permissions;
1517
use Google\Site_Kit\Core\Assets\Assets;
1618

@@ -39,21 +41,31 @@ final class Admin_Bar {
3941
*/
4042
private $assets;
4143

44+
/**
45+
* Modules instance.
46+
*
47+
* @since n.e.x.t
48+
* @var Modules
49+
*/
50+
private $modules;
51+
4252
/**
4353
* Constructor.
4454
*
4555
* @since 1.0.0
4656
*
4757
* @param Context $context Plugin context.
4858
* @param Assets $assets Optional. Assets API instance. Default is a new instance.
59+
* @param Modules $modules Optional. Modules instance. Default is a new instance.
4960
*/
50-
public function __construct( Context $context, Assets $assets = null ) {
61+
public function __construct(
62+
Context $context,
63+
Assets $assets = null,
64+
Modules $modules = null
65+
) {
5166
$this->context = $context;
52-
53-
if ( ! $assets ) {
54-
$assets = new Assets( $this->context );
55-
}
56-
$this->assets = $assets;
67+
$this->assets = $assets ?: new Assets( $this->context );
68+
$this->modules = $modules ?: new Modules( $this->context );
5769
}
5870

5971
/**
@@ -192,6 +204,14 @@ public function is_active() {
192204
}
193205
}
194206

207+
$display = false;
208+
foreach ( $this->modules->get_active_modules() as $module ) {
209+
if ( $module instanceof Module_With_Admin_Bar && $module->is_active_in_admin_bar( $current_url ) ) {
210+
$display = true;
211+
break;
212+
}
213+
}
214+
195215
/**
196216
* Filters whether the Site Kit admin bar menu should be displayed.
197217
*
@@ -204,7 +224,7 @@ public function is_active() {
204224
* @param bool $display Whether to display the admin bar menu.
205225
* @param string $current_url The URL of the current request.
206226
*/
207-
return apply_filters( 'googlesitekit_show_admin_bar_menu', true, $current_url );
227+
return apply_filters( 'googlesitekit_show_admin_bar_menu', $display, $current_url );
208228
}
209229

210230
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Class Google\Site_Kit\Core\Modules\Module_With_Admin_Bar
4+
*
5+
* @package Google\Site_Kit\Core\Modules
6+
* @copyright 2020 Google LLC
7+
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
8+
* @link https://sitekit.withgoogle.com
9+
*/
10+
11+
namespace Google\Site_Kit\Core\Modules;
12+
13+
interface Module_With_Admin_Bar {
14+
15+
/**
16+
* Checks if the module is active in the admin bar for the given URL.
17+
*
18+
* @since n.e.x.t
19+
*
20+
* @param string $url URL to determine active state for.
21+
* @return bool
22+
*/
23+
public function is_active_in_admin_bar( $url );
24+
}

includes/Modules/Analytics.php

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Google\Site_Kit\Core\Modules\Module;
1414
use Google\Site_Kit\Core\Modules\Module_Settings;
15+
use Google\Site_Kit\Core\Modules\Module_With_Admin_Bar;
1516
use Google\Site_Kit\Core\Modules\Module_With_Screen;
1617
use Google\Site_Kit\Core\Modules\Module_With_Screen_Trait;
1718
use Google\Site_Kit\Core\Modules\Module_With_Scopes;
@@ -21,6 +22,10 @@
2122
use Google\Site_Kit\Core\Authentication\Clients\Google_Site_Kit_Client;
2223
use Google\Site_Kit\Core\REST_API\Data_Request;
2324
use Google\Site_Kit\Modules\Analytics\Settings;
25+
use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_DateRangeValues;
26+
use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_GetReportsResponse;
27+
use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_Report;
28+
use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_ReportData;
2429
use Google\Site_Kit_Dependencies\Google_Service_Exception;
2530
use Google\Site_Kit_Dependencies\Google_Service_Analytics;
2631
use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting;
@@ -48,7 +53,8 @@
4853
* @access private
4954
* @ignore
5055
*/
51-
final class Analytics extends Module implements Module_With_Screen, Module_With_Scopes, Module_With_Settings {
56+
final class Analytics extends Module
57+
implements Module_With_Screen, Module_With_Scopes, Module_With_Settings, Module_With_Admin_Bar {
5258
use Module_With_Screen_Trait, Module_With_Scopes_Trait, Module_With_Settings_Trait;
5359

5460
/**
@@ -200,6 +206,22 @@ public function on_deactivation() {
200206
$this->options->delete( 'googlesitekit_analytics_adsense_linked' );
201207
}
202208

209+
/**
210+
* Checks if the module is active in the admin bar for the given URL.
211+
*
212+
* @since n.e.x.t
213+
*
214+
* @param string $url URL to determine active state for.
215+
* @return bool
216+
*/
217+
public function is_active_in_admin_bar( $url ) {
218+
if ( ! $this->is_connected() ) {
219+
return false;
220+
}
221+
222+
return $this->has_data_for_url( $url );
223+
}
224+
203225
/**
204226
* Outputs gtag snippet.
205227
*
@@ -1037,6 +1059,7 @@ function ( Google_Service_Analytics_Account $account ) {
10371059

10381060
return $response;
10391061
case 'GET:report':
1062+
/* @var Google_Service_AnalyticsReporting_GetReportsResponse $response Response object. */
10401063
if ( $this->is_adsense_request( $data ) ) {
10411064
$is_linked = empty( $response->error );
10421065
$this->get_settings()->merge( array( 'adsenseLinked' => $is_linked ) );
@@ -1232,4 +1255,60 @@ private function is_adsense_request( $data ) {
12321255
protected function setup_settings() {
12331256
return new Settings( $this->options );
12341257
}
1258+
1259+
/**
1260+
* Checks whether Analytics data exists for the given URL.
1261+
*
1262+
* @since n.e.x.t
1263+
*
1264+
* @param string $url The url to check data for.
1265+
* @return bool
1266+
*/
1267+
protected function has_data_for_url( $url ) {
1268+
if ( ! $url ) {
1269+
return false;
1270+
}
1271+
1272+
$transient_key = 'googlesitekit_analytics_has_data_' . md5( $url );
1273+
$has_data = get_transient( $transient_key );
1274+
1275+
if ( false === $has_data ) {
1276+
/* @var Google_Service_AnalyticsReporting_Report[]|WP_Error $reports Array of reporting report instances. */
1277+
$reports = $this->get_data(
1278+
'report',
1279+
array(
1280+
'url' => $url,
1281+
'metrics' => array(
1282+
array( 'expression' => 'ga:users' ),
1283+
array( 'expression' => 'ga:sessions' ),
1284+
),
1285+
)
1286+
);
1287+
1288+
if ( is_wp_error( $reports ) ) {
1289+
$reports = array(); // Bypass data check and cache.
1290+
}
1291+
1292+
foreach ( $reports as $report ) {
1293+
/* @var Google_Service_AnalyticsReporting_Report $report Report instance. */
1294+
$report_data = $report->getData();
1295+
/* @var Google_Service_AnalyticsReporting_ReportData $report_data Report data instance. */
1296+
foreach ( $report_data->getTotals() as $date_range_values ) {
1297+
/* @var Google_Service_AnalyticsReporting_DateRangeValues $date_range_values Values instance. */
1298+
if (
1299+
isset( $date_range_values[0], $date_range_values[1] )
1300+
&& ( 0 < $date_range_values[0] || 0 < $date_range_values[1] )
1301+
) {
1302+
$has_data = true;
1303+
break 2;
1304+
}
1305+
}
1306+
}
1307+
1308+
// Cache "data found" status for one day, "no data" status for one hour.
1309+
set_transient( $transient_key, (int) $has_data, $has_data ? DAY_IN_SECONDS : HOUR_IN_SECONDS );
1310+
}
1311+
1312+
return (bool) $has_data;
1313+
}
12351314
}

includes/Modules/Search_Console.php

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Google\Site_Kit\Core\Modules\Module;
1414
use Google\Site_Kit\Core\Modules\Module_Settings;
15+
use Google\Site_Kit\Core\Modules\Module_With_Admin_Bar;
1516
use Google\Site_Kit\Core\Modules\Module_With_Screen;
1617
use Google\Site_Kit\Core\Modules\Module_With_Screen_Trait;
1718
use Google\Site_Kit\Core\Modules\Module_With_Scopes;
@@ -23,6 +24,8 @@
2324
use Google\Site_Kit\Modules\Search_Console\Settings;
2425
use Google\Site_Kit_Dependencies\Google_Service_Exception;
2526
use Google\Site_Kit_Dependencies\Google_Service_Webmasters;
27+
use Google\Site_Kit_Dependencies\Google_Service_Webmasters_ApiDataRow;
28+
use Google\Site_Kit_Dependencies\Google_Service_Webmasters_SearchAnalyticsQueryResponse;
2629
use Google\Site_Kit_Dependencies\Google_Service_Webmasters_SitesListResponse;
2730
use Google\Site_Kit_Dependencies\Google_Service_Webmasters_WmxSite;
2831
use Google\Site_Kit_Dependencies\Google_Service_Webmasters_SearchAnalyticsQueryRequest;
@@ -39,7 +42,8 @@
3942
* @access private
4043
* @ignore
4144
*/
42-
final class Search_Console extends Module implements Module_With_Screen, Module_With_Scopes, Module_With_Settings {
45+
final class Search_Console extends Module
46+
implements Module_With_Screen, Module_With_Scopes, Module_With_Settings, Module_With_Admin_Bar {
4347
use Module_With_Screen_Trait, Module_With_Scopes_Trait, Module_With_Settings_Trait;
4448

4549
/**
@@ -90,23 +94,6 @@ function ( $data ) {
9094
},
9195
11
9296
);
93-
94-
add_filter(
95-
'googlesitekit_show_admin_bar_menu',
96-
function( $display, $current_url ) {
97-
if ( ! $this->get_property_id() ) {
98-
return false;
99-
}
100-
101-
if ( ! $this->has_data_for_url( $current_url ) ) {
102-
return false;
103-
}
104-
105-
return $display;
106-
},
107-
10,
108-
2
109-
);
11097
}
11198

11299
/**
@@ -122,6 +109,22 @@ public function get_scopes() {
122109
);
123110
}
124111

112+
/**
113+
* Checks if the module is active in the admin bar for the given URL.
114+
*
115+
* @since n.e.x.t
116+
*
117+
* @param string $url URL to determine active state for.
118+
* @return bool
119+
*/
120+
public function is_active_in_admin_bar( $url ) {
121+
if ( ! $this->get_property_id() ) {
122+
return false;
123+
}
124+
125+
return $this->has_data_for_url( $url );
126+
}
127+
125128
/**
126129
* Returns the mapping between available datapoints and their services.
127130
*
@@ -348,57 +351,41 @@ protected function create_search_analytics_data_request( array $args = array() )
348351
}
349352

350353
/**
351-
* Checks whether Search Console data exists for the given post.
354+
* Checks whether Search Console data exists for the given URL.
352355
*
353356
* The result of this query is stored in a transient.
354357
*
355358
* @since 1.0.0
356359
*
357-
* @param string $current_url The current url.
360+
* @param string $url The url to check data for.
358361
* @return bool True if Search Console data exists, false otherwise.
359362
*/
360-
protected function has_data_for_url( $current_url ) {
361-
if ( ! $current_url ) {
363+
protected function has_data_for_url( $url ) {
364+
if ( ! $url ) {
362365
return false;
363366
}
364367

365-
$transient_key = 'googlesitekit_sc_data_' . md5( $current_url );
368+
$transient_key = 'googlesitekit_sc_data_' . md5( $url );
366369
$has_data = get_transient( $transient_key );
367-
if ( false === $has_data ) {
368370

369-
// Check search console for data.
370-
$datasets = array(
371+
if ( false === $has_data ) {
372+
/* @var Google_Service_Webmasters_ApiDataRow[]|WP_Error $response_rows Array of data rows. */
373+
$response_rows = $this->get_data(
374+
'searchanalytics',
371375
array(
372-
'identifier' => $this->slug,
373-
'key' => 'sc-site-analytics',
374-
'datapoint' => 'searchanalytics',
375-
'data' => array(
376-
'url' => $current_url,
377-
'dateRange' => 'last-90-days',
378-
'dimensions' => 'date',
379-
'compareDateRanges' => true,
380-
),
381-
),
382-
);
383-
384-
$responses = $this->get_batch_data(
385-
array_map(
386-
function( $dataset ) {
387-
return (object) $dataset;
388-
},
389-
$datasets
376+
'url' => $url,
377+
'dimensions' => 'date',
378+
'compareDateRanges' => true,
390379
)
391380
);
392381

393-
$has_data = false;
394-
395-
// Go thru results, any impressions means the URL has data.
396-
foreach ( $responses['sc-site-analytics'] as $key => $response ) {
397-
if ( is_wp_error( $response ) || empty( $response ) || ! isset( $response->impressions ) ) {
398-
continue;
399-
}
382+
if ( is_wp_error( $response_rows ) ) {
383+
$response_rows = array(); // Bypass data check and cache.
384+
}
400385

401-
if ( $response->impressions > 0 ) {
386+
foreach ( $response_rows as $data_row ) {
387+
/* @var Google_Service_Webmasters_ApiDataRow $data_row Data row instance. */
388+
if ( 0 < $data_row->getImpressions() ) {
402389
$has_data = true;
403390
break;
404391
}

includes/Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function() use ( $options, $transients, $assets ) {
151151
( new Core\Permissions\Permissions( $this->context, $authentication ) )->register();
152152
( new Core\Util\Tracking( $this->context, $authentication ) )->register();
153153
( new Core\REST_API\REST_Routes( $this->context, $authentication, $modules ) )->register();
154-
( new Core\Admin_Bar\Admin_Bar( $this->context, $assets ) )->register();
154+
( new Core\Admin_Bar\Admin_Bar( $this->context, $assets, $modules ) )->register();
155155
( new Core\Admin\Screens( $this->context, $assets ) )->register();
156156
( new Core\Admin\Notices() )->register();
157157
( new Core\Admin\Dashboard( $this->context, $assets ) )->register();

0 commit comments

Comments
 (0)