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

Add term meta infra implementation. #10026

Merged
merged 6 commits into from
Jan 22, 2025
Merged
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
73 changes: 73 additions & 0 deletions includes/Core/Storage/Meta_Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Interface Google\Site_Kit\Core\Storage\Meta_Interface
*
* @package Google\Site_Kit\Core\Storage
* @copyright 2021 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\Storage;

/**
* Interface for object meta implementations.
*
* @since 1.33.0
* @since n.e.x.t Renamed from Post_Meta_Interface to Meta_Interface.
*
* @access private
* @ignore
*/
interface Meta_Interface {

/**
* Gets object meta.
*
* @since 1.33.0
*
* @param int $object_id Object ID.
* @param string $key Metadata key.
* @param bool $single Whether to return a single value.
* @return mixed Object meta value.
*/
public function get( $object_id, $key, $single = false );

/**
* Updates an object meta field based on the given object ID.
*
* @since 1.33.0
*
* @param int $object_id Object ID.
* @param string $key Metadata key.
* @param mixed $value Metadata value.
* @param mixed $prev_value Previous value to check before updating. If specified, only update existing metadata entries with this value. Otherwise, update all entries.
* @return bool TRUE on success, otherwise FALSE.
*/
public function update( $object_id, $key, $value, $prev_value = '' );

/**
* Adds a meta field to the given object.
*
* @since 1.33.0
*
* @param int $object_id Object ID.
* @param string $key Metadata key.
* @param mixed $value Metadata value.
* @param bool $unique Whether the same key should not be added.
* @return int|bool Meta id on success, otherwise FALSE.
*/
public function add( $object_id, $key, $value, $unique = false );

/**
* Deletes an object meta field for the given object ID.
*
* @since 1.33.0
*
* @param int $object_id Object ID.
* @param string $key Metadata key.
* @param mixed $value Metadata value. If provided, rows will only be removed that match the value.
* @return bool TRUE on success, otherwise FALSE.
*/
public function delete( $object_id, $key, $value = '' );
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Class Google\Site_Kit\Core\Storage\Post_Meta_Setting
* Class Google\Site_Kit\Core\Storage\Meta_Setting_Trait
*
* @package Google\Site_Kit\Core\Storage
* @copyright 2021 Google LLC
Expand All @@ -11,50 +11,49 @@
namespace Google\Site_Kit\Core\Storage;

/**
* Base class for a single post meta setting.
* Base class for a single object meta setting.
*
* @since 1.33.0
* @since n.e.x.t Changed from Post_Meta_Setting to Meta_Setting_Trait.
*
* @access private
* @ignore
*/
abstract class Post_Meta_Setting {

trait Meta_Setting_Trait {
/**
* Post_Meta_Interface implementation.
* Meta_Interface implementation.
*
* @since 1.33.0
* @var Post_Meta_Interface
* @var Meta_Interface
*/
protected $post_meta;
protected $meta;

/**
* Post_Meta_Setting constructor.
* Gets the meta key for the setting.
*
* @since 1.33.0
* @since n.e.x.t
*
* @param Post_Meta_Interface $post_meta Post_Meta_Interface instance.
* @return string Meta key.
*/
public function __construct( Post_Meta_Interface $post_meta ) {
$this->post_meta = $post_meta;
}
abstract protected function get_meta_key(): string;

/**
* Gets the meta key for the setting.
* Gets the object type like `post`, `term`, etc.
*
* @since n.e.x.t
*
* @return string Meta key.
* @return string Object type.
*/
abstract protected function get_meta_key(): string;
abstract protected function get_object_type(): string;

/**
* Registers the post setting in WordPress.
* Registers the object setting in WordPress.
*
* @since 1.33.0
*/
public function register() {
register_meta(
'post',
$this->get_object_type(),
$this->get_meta_key(),
array(
'type' => $this->get_type(),
Expand Down Expand Up @@ -111,7 +110,7 @@ protected function get_sanitize_callback() {
}

/**
* Gets the `show_in_rest` value for this postmeta setting value.
* Gets the `show_in_rest` value for this meta setting value.
*
* @since 1.37.0
*
Expand All @@ -122,55 +121,59 @@ protected function get_show_in_rest() {
}

/**
* Checks whether a post meta exists or not.
* Checks whether meta exists for a given object or not.
*
* @since 1.33.0
* @since n.e.x.t Changed `$post_id` parameter to `$object_id`.
*
* @param int $post_id Post ID.
* @param int $object_id Object ID.
* @return bool True if the meta key exists, otherwise false.
*/
public function has( $post_id ) {
return metadata_exists( 'post', $post_id, $this->get_meta_key() );
public function has( $object_id ) {
return metadata_exists( $this->get_object_type(), $object_id, $this->get_meta_key() );
}

/**
* Gets the value of the setting.
*
* @since 1.33.0
* @since n.e.x.t Changed `$post_id` parameter to `$object_id`.
*
* @param int $post_id Post ID.
* @param int $object_id Object ID.
* @return mixed Value set for the setting, or default if not set.
*/
public function get( $post_id ) {
if ( ! $this->has( $post_id ) ) {
public function get( $object_id ) {
if ( ! $this->has( $object_id ) ) {
return $this->get_default();
}

return $this->post_meta->get( $post_id, $this->get_meta_key(), true );
return $this->meta->get( $object_id, $this->get_meta_key(), true );
}

/**
* Updates the post setting for the given post ID.
* Updates the setting for the given object ID.
*
* @since 1.33.0
* @since n.e.x.t Changed `$post_id` parameter to `$object_id`.
*
* @param int $post_id Post ID.
* @param mixed $value Metadata value.
* @param int $object_id Object ID.
* @param mixed $value Metadata value.
* @return bool TRUE on success, otherwise FALSE.
*/
public function set( $post_id, $value ) {
return $this->post_meta->update( $post_id, $this->get_meta_key(), $value );
public function set( $object_id, $value ) {
return $this->meta->update( $object_id, $this->get_meta_key(), $value );
}

/**
* Deletes the post setting for the given post ID.
* Deletes the setting for the given object ID.
*
* @since 1.33.0
* @since n.e.x.t Changed `$post_id` parameter to `$object_id`.
*
* @param int $post_id Post ID.
* @param int $object_id Object ID.
* @return bool TRUE on success, otherwise FALSE.
*/
public function delete( $post_id ) {
return $this->post_meta->delete( $post_id, $this->get_meta_key() );
public function delete( $object_id ) {
return $this->meta->delete( $object_id, $this->get_meta_key() );
}
}
2 changes: 1 addition & 1 deletion includes/Core/Storage/Post_Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @access private
* @ignore
*/
final class Post_Meta implements Post_Meta_Interface {
final class Post_Meta implements Meta_Interface {

/**
* Gets post meta.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,71 +1,79 @@
<?php
/**
* Interface Google\Site_Kit\Core\Storage\Post_Meta_Interface
* Class Google\Site_Kit\Core\Storage\Term_Meta
*
* @package Google\Site_Kit\Core\Storage
* @copyright 2021 Google LLC
* @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\Storage;

/**
* Interface for Post_Meta implementations.
* Term metadata storage class.
*
* @since 1.33.0
* @since n.e.x.t
* @access private
* @ignore
*/
interface Post_Meta_Interface {
final class Term_Meta implements Meta_Interface {

/**
* Gets post meta.
* Gets term meta.
*
* @since 1.33.0
* @since n.e.x.t
*
* @param int $post_id Post ID.
* @param int $term_id Term ID.
* @param string $key Metadata key.
* @param bool $single Whether to return a single value.
* @return mixed Post meta value.
* @return mixed Term meta value.
*/
public function get( $post_id, $key, $single = false );
public function get( $term_id, $key, $single = false ) {
return get_term_meta( $term_id, $key, $single );
}

/**
* Updates a post meta field based on the given post ID.
* Updates a term meta field based on the given term ID.
*
* @since 1.33.0
* @since n.e.x.t
*
* @param int $post_id Post ID.
* @param int $term_id Term ID.
* @param string $key Metadata key.
* @param mixed $value Metadata value.
* @param mixed $prev_value Previous value to check before updating. If specified, only update existing metadata entries with this value. Otherwise, update all entries.
* @return bool TRUE on success, otherwise FALSE.
*/
public function update( $post_id, $key, $value, $prev_value = '' );
public function update( $term_id, $key, $value, $prev_value = '' ) {
return update_term_meta( $term_id, $key, $value, $prev_value );
}

/**
* Adds a meta field to the given post.
* Adds a meta field to the given term.
*
* @since 1.33.0
* @since n.e.x.t
*
* @param int $post_id Post ID.
* @param int $term_id Term ID.
* @param string $key Metadata key.
* @param mixed $value Metadata value.
* @param bool $unique Whether the same key should not be added.
* @return int|bool Meta id on success, otherwise FALSE.
*/
public function add( $post_id, $key, $value, $unique = false );
public function add( $term_id, $key, $value, $unique = false ) {
return add_term_meta( $term_id, $key, $value, $unique );
}

/**
* Deletes a post meta field for the given post ID.
* Deletes a term meta field for the given term ID.
*
* @since 1.33.0
* @since n.e.x.t
*
* @param int $post_id Post ID.
* @param int $term_id Term ID.
* @param string $key Metadata key.
* @param mixed $value Metadata value. If provided, rows will only be removed that match the value.
* @return bool TRUE on success, otherwise FALSE.
*/
public function delete( $post_id, $key, $value = '' );
public function delete( $term_id, $key, $value = '' ) {
return delete_term_meta( $term_id, $key, $value );
}
}
21 changes: 17 additions & 4 deletions includes/Modules/Reader_Revenue_Manager/Post_Product_ID.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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;
use Google\Site_Kit\Core\Storage\Meta_Setting_Trait;

/**
* Class for associating product ID to post meta.
Expand All @@ -20,7 +20,10 @@
* @access private
* @ignore
*/
class Post_Product_ID extends Post_Meta_Setting {
class Post_Product_ID {

use Meta_Setting_Trait;

/**
* Publication ID.
*
Expand All @@ -39,8 +42,7 @@ class Post_Product_ID extends Post_Meta_Setting {
* @param string $publication_id Publication ID.
*/
public function __construct( Post_Meta $post_meta, string $publication_id ) {
parent::__construct( $post_meta );

$this->meta = $post_meta;
$this->publication_id = $publication_id;
}

Expand All @@ -55,6 +57,17 @@ protected function get_meta_key(): string {
return 'googlesitekit_rrm_' . $this->publication_id . ':productID';
}

/**
* Returns the object type.
*
* @since n.e.x.t
*
* @return string Object type.
*/
protected function get_object_type(): string {
return 'post';
}

/**
* Gets the `show_in_rest` value for this postmeta setting value.
*
Expand Down
Loading
Loading