Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show RRM buttons on the edit post page for non-Site Kit users. #10434

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Site Kit by Google, Copyright 2025 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 { registerBlockType } from '@wordpress-core/blocks';
import { useBlockProps, InspectorControls } from '@wordpress-core/block-editor';
import { Notice } from '@wordpress-core/components';
import { Fragment } from '@wordpress-core/element';
import { select } from '@wordpress-core/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { CORE_EDIT_SITE } from '../common/constants';
import { EditorButton } from '../common';
import metadata from './block.json';

function Edit() {
const blockProps = useBlockProps();

return (
<Fragment>
<InspectorControls>
<div className="block-editor-block-card">
<Notice status="warning" isDismissible={ false }>
{ __(
'This block can only be configured by Site Kit users. Please contact your administrator.',
'google-site-kit'
) }
</Notice>
</div>
</InspectorControls>
<div { ...blockProps }>
<div className="googlesitekit-blocks-reader-revenue-manager">
<EditorButton disabled>
{
/* translators: Button label for Contribute with Google. See: https://github.com/subscriptions-project/swg-js/blob/05af2d45cfcaf831a6b4d35c28f2c7b5c2e39308/src/i18n/swg-strings.ts#L58-L91 (please refer to the latest version of the file) */
__( 'Contribute with Google', 'google-site-kit' )
}
</EditorButton>
</div>
</div>
</Fragment>
);
}

function registerBlock() {
const isSiteEditor = !! select( CORE_EDIT_SITE );

registerBlockType( metadata.name, {
edit() {
// Don't render the block in the site editor. Site editor support will be added in a future issue.
if ( isSiteEditor ) {
return null;
}

return <Edit />;
},
supports: {
// Don't allow the block to be inserted in the site editor.
inserter: ! isSiteEditor,
},
} );
}

registerBlock();
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Site Kit by Google, Copyright 2025 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 { registerBlockType } from '@wordpress-core/blocks';
import { useBlockProps, InspectorControls } from '@wordpress-core/block-editor';
import { Notice } from '@wordpress-core/components';
import { Fragment } from '@wordpress-core/element';
import { select } from '@wordpress-core/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { CORE_EDIT_SITE } from '../common/constants';
import { EditorButton } from '../common';
import metadata from './block.json';

function Edit() {
const blockProps = useBlockProps();

return (
<Fragment>
<InspectorControls>
<div className="block-editor-block-card">
<Notice status="warning" isDismissible={ false }>
{ __(
'This block can only be configured by Site Kit users. Please contact your administrator.',
'google-site-kit'
) }
</Notice>
</div>
</InspectorControls>
<div { ...blockProps }>
<div className="googlesitekit-blocks-reader-revenue-manager">
<EditorButton disabled>
{
/* translators: Button label for Subscribe with Google. See: https://github.com/subscriptions-project/swg-js/blob/05af2d45cfcaf831a6b4d35c28f2c7b5c2e39308/src/i18n/swg-strings.ts#L24-L57 (please refer to the latest version of the file) */
__( 'Subscribe with Google', 'google-site-kit' )
}
</EditorButton>
</div>
</div>
</Fragment>
);
}

function registerBlock() {
const isSiteEditor = !! select( CORE_EDIT_SITE );

registerBlockType( metadata.name, {
edit() {
// Don't render the block in the site editor. Site editor support will be added in a future issue.
if ( isSiteEditor ) {
return null;
}

return <Edit />;
},
supports: {
// Don't allow the block to be inserted in the site editor.
inserter: ! isSiteEditor,
},
} );
}

registerBlock();
4 changes: 4 additions & 0 deletions assets/webpack/gutenbergBlocks.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ module.exports = ( mode ) => ( {
// Reader Revenue Manager blocks.
'reader-revenue-manager/contribute-with-google/index':
'./blocks/reader-revenue-manager/contribute-with-google/index.js',
'reader-revenue-manager/contribute-with-google/non-site-kit-user':
'./blocks/reader-revenue-manager/contribute-with-google/non-site-kit-user.js',
'reader-revenue-manager/subscribe-with-google/index':
'./blocks/reader-revenue-manager/subscribe-with-google/index.js',
'reader-revenue-manager/subscribe-with-google/non-site-kit-user':
'./blocks/reader-revenue-manager/subscribe-with-google/non-site-kit-user.js',
'reader-revenue-manager/common/editor-styles':
'./blocks/reader-revenue-manager/common/editor-styles.scss',
// Sign in with Google block.
Expand Down
57 changes: 57 additions & 0 deletions includes/Modules/Reader_Revenue_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Google\Site_Kit\Core\Modules\Module_With_Settings_Trait;
use Google\Site_Kit\Core\Modules\Module_With_Tag;
use Google\Site_Kit\Core\Modules\Module_With_Tag_Trait;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\REST_API\Data_Request;
use Google\Site_Kit\Core\REST_API\Exception\Missing_Required_Param_Exception;
use Google\Site_Kit\Core\Site_Health\Debug_Data;
Expand All @@ -42,6 +43,7 @@
use Google\Site_Kit\Core\Tags\Guards\Tag_Verify_Guard;
use Google\Site_Kit\Core\Util\Block_Support;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
use Google\Site_Kit\Core\Util\URL;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Admin_Post_List;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Contribute_With_Google_Block;
Expand Down Expand Up @@ -69,6 +71,7 @@ final class Reader_Revenue_Manager extends Module implements Module_With_Scopes,
use Module_With_Scopes_Trait;
use Module_With_Settings_Trait;
use Module_With_Tag_Trait;
use Method_Proxy_Trait;

/**
* Module slug name.
Expand Down Expand Up @@ -169,6 +172,7 @@ public function register() {
}
}

add_action( 'enqueue_block_editor_assets', $this->get_method_proxy( 'enqueue_assets_for_non_sitekit_user_on_block_editor_page' ), 40 );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should only hook this action when the rrmModuleV2 feature flag is enabled.

add_action( 'load-toplevel_page_googlesitekit-dashboard', array( $synchronize_publication, 'maybe_schedule_synchronize_publication' ) );
add_action( 'load-toplevel_page_googlesitekit-settings', array( $synchronize_publication, 'maybe_schedule_synchronize_publication' ) );

Expand Down Expand Up @@ -551,6 +555,30 @@ protected function setup_assets() {
)
);

if ( $this->is_non_sitekit_user() ) {
$assets[] = new Script(
'blocks-contribute-with-google-non-sitekit-user',
array(
'src' => $base_url . 'js/blocks/reader-revenue-manager/contribute-with-google/non-site-kit-user.js',
'dependencies' => array(
'googlesitekit-i18n',
),
'load_contexts' => array( Asset::CONTEXT_ADMIN_POST_EDITOR ),
'execution' => 'defer',
)
);

$assets[] = new Script(
'blocks-subscribe-with-google-non-sitekit-user',
array(
'src' => $base_url . 'js/blocks/reader-revenue-manager/subscribe-with-google/non-site-kit-user.js',
'dependencies' => array( 'googlesitekit-i18n' ),
'load_contexts' => array( Asset::CONTEXT_ADMIN_POST_EDITOR ),
'execution' => 'defer',
)
);
}

$assets[] = new Stylesheet(
'blocks-reader-revenue-manager-common-editor-styles',
array(
Expand Down Expand Up @@ -627,6 +655,35 @@ public function register_tag() {
$tag->register();
}

/**
* Checks if the current user is a non-Site Kit user.
*
* @since n.e.x.t
*
* @return bool True if the current user is a non-Site Kit user, false otherwise.
*/
private function is_non_sitekit_user() {
return ! ( current_user_can( Permissions::VIEW_SPLASH ) || current_user_can( Permissions::VIEW_DASHBOARD ) );
}

/**
* Enqueues assets for non-Site Kit users on block editor pages.
*
* @since n.e.x.t
*
* @return void
*/
private function enqueue_assets_for_non_sitekit_user_on_block_editor_page() {
Comment on lines +669 to +676
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're at it, I feel we can improve the naming of this method. WDYT?

Suggested change
/**
* Enqueues assets for non-Site Kit users on block editor pages.
*
* @since n.e.x.t
*
* @return void
*/
private function enqueue_assets_for_non_sitekit_user_on_block_editor_page() {
/**
* Enqueues block editor assets for non-Site Kit users.
*
* @since n.e.x.t
*
* @return void
*/
private function enqueue_block_editor_assets_for_non_sitekit_user() {

if ( $this->is_non_sitekit_user() ) {
// Enqueue styles.
$this->assets->enqueue_asset( 'blocks-reader-revenue-manager-common-editor-styles' );

// Enqueue scripts.
$this->assets->enqueue_asset( 'blocks-contribute-with-google-non-sitekit-user' );
$this->assets->enqueue_asset( 'blocks-subscribe-with-google-non-sitekit-user' );
}
}

/**
* Gets an array of debug field definitions.
*
Expand Down
89 changes: 86 additions & 3 deletions tests/phpunit/integration/Modules/Reader_Revenue_ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Google\Site_Kit\Core\Modules\Module;
use Google\Site_Kit\Core\Modules\Module_With_Service_Entity;
use Google\Site_Kit\Core\Modules\Module_With_Settings;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\REST_API\Exception\Missing_Required_Param_Exception;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
Expand Down Expand Up @@ -66,6 +67,13 @@ class Reader_Revenue_ManagerTest extends TestCase {
*/
private $options;

/**
* User_Options object.
*
* @var User_Options
*/
private $user_options;

/**
* Reader_Revenue_Manager object.
*
Expand All @@ -79,9 +87,9 @@ public function set_up() {
$this->context = new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE );
$this->options = new Options( $this->context );
$user = $this->factory()->user->create_and_get( array( 'role' => 'administrator' ) );
$user_options = new User_Options( $this->context, $user->ID );
$this->authentication = new Authentication( $this->context, $this->options, $user_options );
$this->reader_revenue_manager = new Reader_Revenue_Manager( $this->context, $this->options, $user_options, $this->authentication );
$this->user_options = new User_Options( $this->context, $user->ID );
$this->authentication = new Authentication( $this->context, $this->options, $this->user_options );
$this->reader_revenue_manager = new Reader_Revenue_Manager( $this->context, $this->options, $this->user_options, $this->authentication );
}

public function test_register() {
Expand Down Expand Up @@ -806,6 +814,81 @@ function ( $asset ) {
);
}

public function test_non_sk_user_scripts_not_enqueued_for_sk_users() {
$this->enable_feature( 'rrmModuleV2' );

// Create a user.
$user = $this->factory()->user->create_and_get( array( 'role' => 'editor' ) );
$user->add_cap( Permissions::VIEW_SPLASH );
wp_set_current_user( $user->ID );

$dismissed_items = new \Google\Site_Kit\Core\Dismissals\Dismissed_Items( $this->user_options );
$dismissed_items->add( 'shared_dashboard_splash' );

$user->add_role( 'editor' );
Comment on lines +820 to +828
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to my tests, a lot of the above doesn't seem to be necessary.

Suggested change
// Create a user.
$user = $this->factory()->user->create_and_get( array( 'role' => 'editor' ) );
$user->add_cap( Permissions::VIEW_SPLASH );
wp_set_current_user( $user->ID );
$dismissed_items = new \Google\Site_Kit\Core\Dismissals\Dismissed_Items( $this->user_options );
$dismissed_items->add( 'shared_dashboard_splash' );
$user->add_role( 'editor' );
// Create a user.
$user = $this->factory()->user->create_and_get( array( 'role' => 'editor' ) );
wp_set_current_user( $user->ID );


//simulate the setting of the module sharing setting.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is scope to improve the comment here.

Suggested change
//simulate the setting of the module sharing setting.
// Simulate view-only access to Site Kit for the user.

$module_sharing_settings = new \Google\Site_Kit\Core\Modules\Module_Sharing_Settings( $this->options );
$module_sharing_settings->set(
array(
'analytics-4' => array(
'sharedRoles' => array( 'editor' ),
'management' => 'owner',
),
)
);

do_action( 'enqueue_block_editor_assets' );

$registerable_asset_handles = array_map(
function ( $asset ) {
return $asset->get_handle();
},
$this->reader_revenue_manager->get_assets()
);

$non_sk_users_block_editor_scripts = array(
'blocks-contribute-with-google-non-sitekit-user',
'blocks-subscribe-with-google-non-sitekit-user',
);

$present_handles = array_intersect( $non_sk_users_block_editor_scripts, $registerable_asset_handles );

$this->assertEmpty(
$present_handles,
'The following block editor handles should not be present: ' . implode( ', ', $present_handles )
);
}

public function test_non_sk_user_scripts_enqueued() {
if ( version_compare( get_bloginfo( 'version' ), '5.8', '<' ) ) {
$this->markTestSkipped( 'This test only runs on WordPress 5.8 and above.' );
}

$this->enable_feature( 'rrmModuleV2' );

do_action( 'enqueue_block_editor_assets' );

$registerable_asset_handles = array_map(
function ( $asset ) {
return $asset->get_handle();
},
$this->reader_revenue_manager->get_assets()
);

$non_sk_users_block_editor_scripts = array(
'blocks-contribute-with-google-non-sitekit-user',
'blocks-subscribe-with-google-non-sitekit-user',
);

$present_handles = array_intersect( $non_sk_users_block_editor_scripts, $registerable_asset_handles );

$this->assertEquals(
$non_sk_users_block_editor_scripts,
$present_handles
);
}

public function data_block_editor_script_not_enqueued() {
return array(
'feature flag disabled' => array(
Expand Down
Loading