diff --git a/assets/blocks/reader-revenue-manager/contribute-with-google/non-site-kit-user.js b/assets/blocks/reader-revenue-manager/contribute-with-google/non-site-kit-user.js new file mode 100644 index 00000000000..c58501cd6cd --- /dev/null +++ b/assets/blocks/reader-revenue-manager/contribute-with-google/non-site-kit-user.js @@ -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 ( + + +
+ + { __( + 'This block can only be configured by Site Kit users. Please contact your administrator.', + 'google-site-kit' + ) } + +
+
+
+
+ + { + /* 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' ) + } + +
+
+
+ ); +} + +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 ; + }, + supports: { + // Don't allow the block to be inserted in the site editor. + inserter: ! isSiteEditor, + }, + } ); +} + +registerBlock(); diff --git a/assets/blocks/reader-revenue-manager/subscribe-with-google/non-site-kit-user.js b/assets/blocks/reader-revenue-manager/subscribe-with-google/non-site-kit-user.js new file mode 100644 index 00000000000..b82cd6acffa --- /dev/null +++ b/assets/blocks/reader-revenue-manager/subscribe-with-google/non-site-kit-user.js @@ -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 ( + + +
+ + { __( + 'This block can only be configured by Site Kit users. Please contact your administrator.', + 'google-site-kit' + ) } + +
+
+
+
+ + { + /* 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' ) + } + +
+
+
+ ); +} + +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 ; + }, + supports: { + // Don't allow the block to be inserted in the site editor. + inserter: ! isSiteEditor, + }, + } ); +} + +registerBlock(); diff --git a/assets/webpack/gutenbergBlocks.config.js b/assets/webpack/gutenbergBlocks.config.js index 99fa0e572c8..e2113f6b4b8 100644 --- a/assets/webpack/gutenbergBlocks.config.js +++ b/assets/webpack/gutenbergBlocks.config.js @@ -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. diff --git a/includes/Modules/Reader_Revenue_Manager.php b/includes/Modules/Reader_Revenue_Manager.php index be7983945ed..db13a4da2f1 100644 --- a/includes/Modules/Reader_Revenue_Manager.php +++ b/includes/Modules/Reader_Revenue_Manager.php @@ -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; @@ -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; @@ -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. @@ -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 ); 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' ) ); @@ -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( @@ -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() { + 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. * diff --git a/tests/phpunit/integration/Modules/Reader_Revenue_ManagerTest.php b/tests/phpunit/integration/Modules/Reader_Revenue_ManagerTest.php index 5348f58e3d4..25c3ac7f422 100644 --- a/tests/phpunit/integration/Modules/Reader_Revenue_ManagerTest.php +++ b/tests/phpunit/integration/Modules/Reader_Revenue_ManagerTest.php @@ -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; @@ -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. * @@ -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() { @@ -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' ); + + //simulate the setting of the module sharing setting. + $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(