-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7941 from google/7940-km-new-badge
Add key metrics setup new badge
- Loading branch information
Showing
7 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Site Kit by Google, Copyright 2023 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 Data from 'googlesitekit-data'; | ||
import { CORE_SITE } from '../../googlesitekit/datastore/site/constants'; | ||
import Badge from '../Badge'; | ||
const { useSelect } = Data; | ||
|
||
export default function KeyMetricsNewBadge() { | ||
// This is necessary to conditionally render the badge | ||
// as this component is used in a context where `select` is not in scope. | ||
const isNew = useSelect( ( select ) => | ||
select( CORE_SITE ).getKeyMetricsSetupNew() | ||
); | ||
|
||
return ( | ||
isNew && ( | ||
<Badge | ||
className="googlesitekit-new-badge" | ||
label={ __( 'New', 'google-site-kit' ) } | ||
/> | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/** | ||
* Class Google\Site_Kit\Core\Key_Metrics\Key_Metrics_Setup_New | ||
* | ||
* @package Google\Site_Kit\Core\Key_Metrics | ||
* @copyright 2023 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\Key_Metrics; | ||
|
||
use Google\Site_Kit\Core\Storage\Transients; | ||
use Google\Site_Kit\Core\Util\Method_Proxy_Trait; | ||
|
||
/** | ||
* Class for handling Key_Metrics_Setup_New state. | ||
* | ||
* @since 1.115.0 | ||
* @access private | ||
* @ignore | ||
*/ | ||
class Key_Metrics_Setup_New { | ||
|
||
use Method_Proxy_Trait; | ||
|
||
const TRANSIENT = 'googlesitekit_key_metrics_setup_new'; | ||
|
||
/** | ||
* Transients instance. | ||
* | ||
* @var Transients | ||
*/ | ||
private $transients; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since 1.115.0 | ||
* | ||
* @param Transients $transients Transients instance. | ||
*/ | ||
public function __construct( Transients $transients ) { | ||
$this->transients = $transients; | ||
} | ||
|
||
/** | ||
* Registers functionality through WordPress hooks. | ||
* | ||
* @since 1.115.0 | ||
*/ | ||
public function register() { | ||
add_action( | ||
'add_option_' . Key_Metrics_Setup_Completed_By::OPTION, | ||
$this->get_method_proxy( 'mark_setup_completed' ), | ||
10, | ||
2 | ||
); | ||
|
||
add_filter( 'googlesitekit_inline_base_data', $this->get_method_proxy( 'inline_js_base_data' ) ); | ||
} | ||
|
||
/** | ||
* Marks Key Metrics setup as just completed for a limited period of time. | ||
* | ||
* @since 1.115.0 | ||
* | ||
* @param string $option Key_Metrics_Setup_Completed_By option name. | ||
* @param mixed $value Option value added. | ||
*/ | ||
protected function mark_setup_completed( $option, $value ) { | ||
if ( $value ) { | ||
$this->transients->set( self::TRANSIENT, true, 2 * WEEK_IN_SECONDS ); | ||
} | ||
} | ||
|
||
/** | ||
* Extends base data with setup new state. | ||
* | ||
* @since 1.115.0 | ||
* | ||
* @param array $data Inline base data. | ||
* @return array | ||
*/ | ||
protected function inline_js_base_data( $data ) { | ||
$data['keyMetricsSetupNew'] = (bool) $this->transients->get( self::TRANSIENT ); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters