Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions assets/js/googlesitekit-public-dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Dashboard component.
*
* Site Kit by Google, Copyright 2024 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 domReady from '@wordpress/dom-ready';
import { render } from '@wordpress/element';

/**
* Internal dependencies
*/
import { clearCache } from './googlesitekit/api/cache';
import Root from './components/Root';
import { VIEW_CONTEXT_MAIN_DASHBOARD_VIEW_ONLY } from './googlesitekit/constants';
import DashboardEntryPoint from './components/DashboardEntryPoint';

// Initialize the app once the DOM is ready.
domReady( async () => {
if ( global._googlesitekitLegacyData.admin.resetSession ) {
await clearCache();
}

const renderTarget = document.getElementById(
'js-googlesitekit-public-dashboard'
);

if ( renderTarget ) {
render(
<Root viewContext={ VIEW_CONTEXT_MAIN_DASHBOARD_VIEW_ONLY }>
<DashboardEntryPoint />
</Root>,
renderTarget
);
}
} );
2 changes: 2 additions & 0 deletions assets/webpack/modules.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ module.exports = ( mode, rules, ANALYZE ) => {
'./js/googlesitekit-main-dashboard.js',
'googlesitekit-entity-dashboard':
'./js/googlesitekit-entity-dashboard.js',
'googlesitekit-public-dashboard':
'./js/googlesitekit-public-dashboard.js',
'googlesitekit-splash': './js/googlesitekit-splash.js',
'googlesitekit-wp-dashboard': './js/googlesitekit-wp-dashboard.js',
},
Expand Down
3 changes: 2 additions & 1 deletion feature-flags.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"gtagUserData",
"privacySandboxModule",
"proactiveUserEngagement",
"setupFlowRefresh"
"setupFlowRefresh",
"publicDashboard"
]
10 changes: 10 additions & 0 deletions includes/Core/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,16 @@ private function get_assets() {
),
);

if ( Feature_Flags::enabled( 'publicDashboard' ) ) {
$assets[] = new Script(
'googlesitekit-public-dashboard',
array(
'src' => $base_url . 'js/googlesitekit-public-dashboard.js',
'dependencies' => $this->get_asset_dependencies(),
)
);
}

/**
* Filters the list of assets that Site Kit should register.
*
Expand Down
191 changes: 191 additions & 0 deletions includes/Core/Util/Public_Dashboard/Public_Dashboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php
/**
* Class Google\Site_Kit\Core\Util\Public_Dashboard
*
* @package Google\Site_Kit
* @copyright 2025 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\Core\Assets\Assets;
use Google\Site_Kit\Core\Modules\Modules;
use Google\Site_Kit\Core\Permissions\Permissions;

/**
* Class to handle Public Dashboard functionality.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Public_Dashboard {

use Method_Proxy_Trait;

/**
* Assets instance.
*
* @since n.e.x.t
* @var Assets
*/
protected $assets;

/**
* Modules instance.
*
* @since n.e.x.t
* @var Modules
*/
protected $modules;

// Capabilities required to view the public dashboard.
const PUBLIC_DASHBOARD_CAPABILITIES = array(
Permissions::VIEW_DASHBOARD,
Permissions::VIEW_POSTS_INSIGHTS,
Permissions::READ_SHARED_MODULE_DATA,
);

/**
* Constructor.
*
* @since n.e.x.t
*
* @param Assets $assets Assets instance.
* @param Modules $modules Modules instance.
*/
public function __construct( Assets $assets, Modules $modules ) {
$this->assets = $assets;
$this->modules = $modules;
}

/**
* Registers functionality through WordPress hooks.
*
* @since n.e.x.t
*/
public function register() {
add_action( 'init', $this->get_method_proxy( 'register_public_dashboard' ) );
add_filter( 'query_vars', $this->get_method_proxy( 'add_query_vars' ) );
add_filter( 'template_include', $this->get_method_proxy( 'load_template' ) );

add_filter(
'map_meta_cap',
$this->get_method_proxy( 'map_meta_capabilities' ),
20,
2
);

add_filter(
'user_has_cap',
$this->get_method_proxy( 'grant_capabilities' ),
20,
1
);

add_action( 'wp_enqueue_scripts', $this->get_method_proxy( 'enqueue_assets' ) );
}

/**
* Registers the public dashboard rewrite rule.
*
* @since n.e.x.t
*/
protected function register_public_dashboard() {
add_rewrite_rule(
'^google-site-kit/?',
'index.php?custom_page=google-site-kit',
'top'
);
}

/**
* Adds custom query variables.
*
* @since n.e.x.t
*
* @param array $vars Existing query variables.
* @return array Modified query variables.
*/
protected function add_query_vars( $vars ) {
$vars[] = 'custom_page';

return $vars;
}

/**
* Loads the public dashboard template.
*
* @since n.e.x.t
*
* @param string $template The path to the template to load.
* @return string The path to the template to load.
*/
protected function load_template( $template ) {
if ( get_query_var( 'custom_page' ) === 'google-site-kit' ) {
$template_path = GOOGLESITEKIT_PLUGIN_DIR_PATH . 'includes/Core/Util/Public_Dashboard/page-templates/page-public-dashboard.php';

if ( file_exists( $template_path ) ) {
return $template_path;
}
}

return $template;
}

/**
* Maps meta capabilities for the public dashboard.
*
* @since n.e.x.t
*
* @param array $caps Capabilities.
* @param string $cap Capability to map.
* @return array Mapped capabilities.
*/
protected function map_meta_capabilities( $caps, $cap ) {
if ( is_user_logged_in() ) {
return $caps;
}

if ( in_array( $cap, self::PUBLIC_DASHBOARD_CAPABILITIES, true ) ) {
return array( $cap );
}

return $caps;
}

/**
* Grants capabilities for the public dashboard to non-logged-in users.
*
* @since n.e.x.t
*
* @param array $allcaps All capabilities.
* @return array Modified capabilities.
*/
protected function grant_capabilities( $allcaps ) {
if ( is_user_logged_in() ) {
return $allcaps;
}

foreach ( self::PUBLIC_DASHBOARD_CAPABILITIES as $capability ) {
$allcaps[ $capability ] = true;
}

return $allcaps;
}

/**
* Enqueues assets for the public dashboard.
*
* @since n.e.x.t
*/
protected function enqueue_assets() {
if ( get_query_var( 'custom_page' ) === 'google-site-kit' ) {
$this->assets->enqueue_asset( 'googlesitekit-public-dashboard' );
$this->assets->enqueue_asset( 'googlesitekit-admin-css' );
$this->modules->enqueue_assets();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Template Name: Site Kit Public Dashboard
*
* @package Google\Site_Kit
* @copyright 2025 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

// Load necessary WordPress core files and functions.
require_once ABSPATH . 'wp-load.php';
require_once ABSPATH . WPINC . '/template-loader.php';

// Output the necessary parts of the header.
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div id="content">

<div class="googlesitekit-plugin">
<div id="js-googlesitekit-public-dashboard" data-view-only="true"></div>
</div>

<?php
// Output the necessary parts of the footer.
?>
</div> <!-- End of #content -->
<?php wp_footer(); ?>
</body>
</html>
6 changes: 6 additions & 0 deletions includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Google\Site_Kit\Core\Remote_Features\Remote_Features_Provider;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Core\Util\Public_Dashboard;

/**
* Main class for the plugin.
Expand Down Expand Up @@ -184,6 +185,11 @@ function () use ( $options, $activation_flag ) {
$permissions = new Core\Permissions\Permissions( $this->context, $authentication, $modules, $user_options, $dismissed_items );
$permissions->register();

if ( Feature_Flags::enabled( 'publicDashboard' ) ) {
$public_dashboard = new Public_Dashboard( $assets, $modules );
$public_dashboard->register();
}

$nonces = new Core\Nonces\Nonces( $this->context );
$nonces->register();

Expand Down
Loading