Skip to content

Commit

Permalink
Merge pull request #9999 from google/enhancement/9955-post-meta-setti…
Browse files Browse the repository at this point in the history
…ngs-class
  • Loading branch information
nfmohit authored Jan 14, 2025
2 parents f1c9abc + 53437b8 commit d5100bd
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 11 deletions.
25 changes: 14 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 All @@ -44,6 +38,15 @@ public function __construct( Post_Meta_Interface $post_meta ) {
$this->post_meta = $post_meta;
}

/**
* Gets the meta key for the setting.
*
* @since n.e.x.t
*
* @return string Meta key.
*/
abstract protected function get_meta_key(): string;

/**
* Registers the post setting in WordPress.
*
Expand All @@ -52,7 +55,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 Down Expand Up @@ -127,7 +130,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 +146,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 +159,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 +171,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() );
}
}
9 changes: 9 additions & 0 deletions includes/Modules/Reader_Revenue_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
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\Feature_Flags;
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 Down Expand Up @@ -77,6 +79,13 @@ public function register() {
);
$synchronize_onboarding_state->register();

if ( Feature_Flags::enabled( 'rrmModuleV2' ) && $this->is_connected() ) {
$post_meta = new Post_Meta();
$publication_id = $this->get_settings()->get()['publicationID'];
$post_product_id = new Post_Product_ID( $post_meta, $publication_id );
$post_product_id->register();
}

add_action( 'load-toplevel_page_googlesitekit-dashboard', array( $synchronize_onboarding_state, 'maybe_schedule_synchronize_onboarding_state' ) );
add_action( 'load-toplevel_page_googlesitekit-settings', array( $synchronize_onboarding_state, 'maybe_schedule_synchronize_onboarding_state' ) );

Expand Down
68 changes: 68 additions & 0 deletions includes/Modules/Reader_Revenue_Manager/Post_Product_ID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Class Google\Site_Kit\Modules\Reader_Revenue_Manager\Post_Product_ID
*
* @package Google\Site_Kit\Modules\Reader_Revenue_Manager
* @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\Modules\Reader_Revenue_Manager;

use Google\Site_Kit\Core\Storage\Post_Meta;
use Google\Site_Kit\Core\Storage\Post_Meta_Setting;

/**
* Class for associating product ID to post meta.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Post_Product_ID extends Post_Meta_Setting {
/**
* Publication ID.
*
* @since n.e.x.t
*
* @var string
*/
private string $publication_id;

/**
* Post_Product_ID constructor.
*
* @since n.e.x.t
*
* @param Post_Meta $post_meta Post_Meta instance.
* @param string $publication_id Publication ID.
*/
public function __construct( Post_Meta $post_meta, string $publication_id ) {
parent::__construct( $post_meta );

$this->publication_id = $publication_id;
}

/**
* Gets the meta key for the setting.
*
* @since n.e.x.t
*
* @return string Meta key.
*/
protected function get_meta_key(): string {
return 'googlesitekit_rrm_' . $this->publication_id . ':productID';
}

/**
* Gets the `show_in_rest` value for this postmeta setting value.
*
* @since n.e.x.t
*
* @return bool|Array Any valid value for the `show_in_rest`
*/
protected function get_show_in_rest() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Class Google\Site_Kit\Tests\Modules\Reader_Revenue_Manager\Post_Product_ID_Test
*
* @package Google\Site_Kit\Tests\Modules\Reader_Revenue_Manager
* @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\Tests\Modules\Reader_Revenue_Manager;

use Google\Site_Kit\Core\Storage\Post_Meta;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Post_Product_ID;
use Google\Site_Kit\Tests\TestCase;

class Post_Product_ID_Test extends TestCase {

/**
* @var Post_Product_ID
*/
private $setting;

public function set_up(): void {
parent::set_up();

$post_meta = new Post_Meta();
$this->setting = new Post_Product_ID( $post_meta, 'test_publication_id' );
$this->setting->register();
}

public function test_product_id_meta_registered() {
$registered = registered_meta_key_exists( 'post', 'googlesitekit_rrm_test_publication_id:productID' );

$this->assertTrue( $registered );
}

public function test_show_in_rest() {
$meta_key = 'googlesitekit_rrm_test_publication_id:productID';
$show_in_rest = get_registered_meta_keys( 'post' )[ $meta_key ]['show_in_rest'];

$this->assertTrue( $show_in_rest );
}
}
50 changes: 50 additions & 0 deletions tests/phpunit/integration/Modules/Reader_Revenue_ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,56 @@ public function test_check_service_entity_access_no_access_unavailable_publicati
$this->assertEquals( false, $access );
}

public function test_product_id_setting_registered() {
$publication_id = 'ABCDEFGH';
$this->enable_feature( 'rrmModuleV2' );

$this->reader_revenue_manager->get_settings()->set(
array(
'publicationID' => $publication_id,
)
);

$this->reader_revenue_manager->register();

$registered = registered_meta_key_exists( 'post', 'googlesitekit_rrm_' . $publication_id . ':productID' );

$this->assertTrue( $registered );
}

public function test_publication_id_empty_product_id_setting_not_registered() {
$publication_id = 'ABCDEFGH';
$this->enable_feature( 'rrmModuleV2' );

$this->reader_revenue_manager->get_settings()->set(
array(
'publicationID' => '',
)
);

$this->reader_revenue_manager->register();

$registered = registered_meta_key_exists( 'post', 'googlesitekit_rrm_' . $publication_id . ':productID' );

$this->assertFalse( $registered );
}

public function test_feature_disabled_product_id_setting_not_registered() {
$publication_id = 'ABCDEFGH';

$this->reader_revenue_manager->get_settings()->set(
array(
'publicationID' => $publication_id,
)
);

$this->reader_revenue_manager->register();

$registered = registered_meta_key_exists( 'post', 'googlesitekit_rrm_' . $publication_id . ':productID' );

$this->assertFalse( $registered );
}

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

Expand Down

0 comments on commit d5100bd

Please sign in to comment.