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

PoC: Block editor elements for RRM v2 #9883

Draft
wants to merge 13 commits into
base: develop
Choose a base branch
from
144 changes: 144 additions & 0 deletions assets/js/googlesitekit-reader-revenue-manager-block-editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* Reader Revenue Manager module's block editor entrypoint.
*
* 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 { __ } from '@wordpress/i18n';
import { Fragment } from '@wordpress-core/element';
import { PanelBody, SelectControl } from '@wordpress-core/components';
import {
PluginDocumentSettingPanel,
PluginSidebarMoreMenuItem,
PluginSidebar,
} from '@wordpress-core/edit-post'; // The @wordpress-core/editor package should've been used here as these packages are deprecated in WP 6.6 and upwards. However, since we'll have to support upto WP 5.2, we should still use the @wordpress-core/edit-post package.
import { registerPlugin } from '@wordpress-core/plugins';

/**
* Internal dependencies
*/
import { select } from 'googlesitekit-data';
import { CORE_MODULES } from './googlesitekit/modules/datastore/constants';
import GoogleLogoIcon from '../svg/graphics/logo-g.svg';

function SettingsForm() {
return (
<Fragment>
<SelectControl
label={ __( 'Product ID Override', 'google-site-kit' ) }
options={ [
{
label: __( 'No change', 'google-site-kit' ),
value: '',
},
{
label: __( 'Off', 'google-site-kit' ),
value: 'none',
},
{
label: __( 'Open access', 'google-site-kit' ),
value: 'openaccess',
},
{
label: __( 'Product ID A', 'google-site-kit' ),
value: 'product-id-a',
},
{
label: __( 'Product ID B', 'google-site-kit' ),
value: 'product-id-b',
},
] }
help={ __(
'The snippet configuration will be inherited from a parent taxonomy term, or Site Kit settings.',
'google-site-kit'
) }
__next40pxDefaultSize
__nextHasNoMarginBottom
/>
</Fragment>
);
}

function PanelSection( { children, title } ) {
return (
<section>
<h3>{ title }</h3>
{ children }
</section>
);
}

function SiteKitSettingPanel() {
const isRRMConnected = select( CORE_MODULES ).isModuleConnected(
'reader-revenue-manager'
);

if ( ! isRRMConnected ) {
return null;
}

const isDocumentSettingPanelAvailable =
typeof wp.editPost?.PluginDocumentSettingPanel === 'function';

return (
<Fragment>
<PluginSidebarMoreMenuItem
target="google-site-kit"
icon={ <GoogleLogoIcon height="16" width="16" /> }
>
{ __( 'Google Site Kit', 'google-site-kit' ) }
</PluginSidebarMoreMenuItem>
<PluginSidebar
name="google-site-kit"
title={ __( 'Google Site Kit', 'google-site-kit' ) }
icon={ <GoogleLogoIcon height="16" width="16" /> }
>
<PanelBody
title={ __( 'Reader Revenue Manager', 'google-site-kit' ) }
>
<SettingsForm />
</PanelBody>
</PluginSidebar>
{ isDocumentSettingPanelAvailable && (
<PluginDocumentSettingPanel
name="google-site-kit"
title={ __( 'Google Site Kit', 'google-site-kit' ) }
icon={ <GoogleLogoIcon height="16" width="16" /> }
>
<PanelSection
title={ __(
'Reader Revenue Manager',
'google-site-kit'
) }
>
<SettingsForm />
</PanelSection>
</PluginDocumentSettingPanel>
) }
</Fragment>
);
}

// Since we need to check if the module is connected, we need to resolve the modules first.
// There is probably a better way to do this, but this should suffice for the PoC.
select( CORE_MODULES ).getModules();

registerPlugin( 'google-site-kit', {
render: SiteKitSettingPanel,
icon: <GoogleLogoIcon height="16" width="16" />,
} );
28 changes: 17 additions & 11 deletions includes/Core/Storage/Post_Meta_Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
*/
abstract class Post_Meta_Setting {

/**
* The post meta key for this setting.
* Override in a sub-class.
*/
const META_KEY = '';

/**
* Post_Meta_Interface implementation.
*
Expand Down Expand Up @@ -52,7 +46,7 @@ public function __construct( Post_Meta_Interface $post_meta ) {
public function register() {
register_meta(
'post',
static::META_KEY,
$this->get_meta_key(),
array(
'type' => $this->get_type(),
'sanitize_callback' => $this->get_sanitize_callback(),
Expand All @@ -62,6 +56,18 @@ public function register() {
);
}

/**
* The post meta key for this setting.
* Override in a sub-class.
*
* @since n.e.x.t
*
* @return string The meta key.
*/
protected function get_meta_key() {
return '';
}

/**
* Gets the expected value type.
*
Expand Down Expand Up @@ -127,7 +133,7 @@ protected function get_show_in_rest() {
* @return bool True if the meta key exists, otherwise false.
*/
public function has( $post_id ) {
return metadata_exists( 'post', $post_id, static::META_KEY );
return metadata_exists( 'post', $post_id, $this->get_meta_key() );
}

/**
Expand All @@ -143,7 +149,7 @@ public function get( $post_id ) {
return $this->get_default();
}

return $this->post_meta->get( $post_id, static::META_KEY, true );
return $this->post_meta->get( $post_id, $this->get_meta_key(), true );
}

/**
Expand All @@ -156,7 +162,7 @@ public function get( $post_id ) {
* @return bool TRUE on success, otherwise FALSE.
*/
public function set( $post_id, $value ) {
return $this->post_meta->update( $post_id, static::META_KEY, $value );
return $this->post_meta->update( $post_id, $this->get_meta_key(), $value );
}

/**
Expand All @@ -168,6 +174,6 @@ public function set( $post_id, $value ) {
* @return bool TRUE on success, otherwise FALSE.
*/
public function delete( $post_id ) {
return $this->post_meta->delete( $post_id, static::META_KEY );
return $this->post_meta->delete( $post_id, $this->get_meta_key() );
}
}
97 changes: 97 additions & 0 deletions includes/Modules/Reader_Revenue_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Google\Site_Kit\Modules;

use Exception;
use Google\Site_Kit\Core\Assets\Asset;
use Google\Site_Kit\Core\Assets\Script;
use Google\Site_Kit\Core\Authentication\Clients\Google_Site_Kit_Client;
use Google\Site_Kit\Core\Modules\Module;
Expand All @@ -30,9 +31,11 @@
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;
use Google\Site_Kit\Core\Storage\Post_Meta;
use Google\Site_Kit\Core\Tags\Guards\Tag_Environment_Type_Guard;
use Google\Site_Kit\Core\Tags\Guards\Tag_Verify_Guard;
use Google\Site_Kit\Core\Util\URL;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Post_Product_ID;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Settings;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Synchronize_OnboardingState;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Tag_Guard;
Expand All @@ -56,6 +59,13 @@ final class Reader_Revenue_Manager extends Module implements Module_With_Scopes,
use Module_With_Settings_Trait;
use Module_With_Tag_Trait;

/**
* Post_Product_ID instance.
*
* @var Post_Product_ID
*/
protected $post_product_id;

/**
* Module slug name.
*/
Expand All @@ -80,6 +90,27 @@ public function register() {

// Reader Revenue Manager tag placement logic.
add_action( 'template_redirect', array( $this, 'register_tag' ) );

$taxonomies = get_taxonomies();

foreach ( $taxonomies as $taxonomy ) {
add_action(
"{$taxonomy}_add_form_fields",
array( $this, 'add_create_taxonomy_fields' )
);
add_action(
"{$taxonomy}_edit_form_fields",
array( $this, 'add_edit_taxonomy_fields' )
);
}

$post_meta = new Post_Meta();
$publication_id = $this->get_settings()->get()['publicationID'];
$this->post_product_id = new Post_Product_ID(
$post_meta,
$publication_id
);
$this->post_product_id->register();
}

/**
Expand Down Expand Up @@ -400,6 +431,22 @@ protected function setup_assets() {
),
)
),
new Script(
'googlesitekit-reader-revenue-manager-block-editor.js',
array(
'src' => $base_url . 'js/googlesitekit-reader-revenue-manager-block-editor.js',
'dependencies' => array(
'googlesitekit-data',
'googlesitekit-i18n',
'googlesitekit-modules',
'wp-components',
'wp-editor',
'wp-element',
'wp-plugins',
),
'load_contexts' => array( Asset::CONTEXT_ADMIN_POST_EDITOR ),
)
),
);
}

Expand Down Expand Up @@ -463,4 +510,54 @@ public function get_debug_fields() {
),
);
}

/**
* Adds fields to the taxonomy creation form.
*
* @since n.e.x.t
*
* @return void
*/
public function add_create_taxonomy_fields() {
?>
<div class="form-field">
<label for="googlesitekit_rrm_product_id">Site Kit: Reader Revenue Manager Product ID Override</label>
<select name="googlesitekit_rrm_product_id" id="googlesitekit_rrm_product_id">
<option value="">No change</option>
<option value="none">Off</option>
<option value="openaccess">Open access</option>
<option value="product-id-a">Product ID A</option>
<option value="product-id-b">Product ID B</option>
</select>
<p class="description">The snippet configuration will be inherited from the individual post, or Site Kit settings.</p>
</div>
<?php
}

/**
* Adds fields to the taxonomy edit form.
*
* @since n.e.x.t
*
* @return void
*/
public function add_edit_taxonomy_fields() {
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="googlesitekit_rrm_product_id">Site Kit: Reader Revenue Manager Product ID Override</label>
</th>
<td>
<select name="googlesitekit_rrm_product_id" id="googlesitekit_rrm_product_id">
<option value="">No change</option>
<option value="none">Off</option>
<option value="openaccess">Open access</option>
<option value="product-id-a">Product ID A</option>
<option value="product-id-b">Product ID B</option>
</select>
<p class="description">The snippet configuration will be inherited from the individual post, or Site Kit settings.</p>
</td>
</tr>
<?php
}
}
Loading
Loading