update
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Expirables\Expirable_Items
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Expirables
|
||||
* @copyright 2024 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\Expirables;
|
||||
|
||||
use Google\Site_Kit\Core\Storage\User_Setting;
|
||||
|
||||
/**
|
||||
* Class for handling expirables items.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Expirable_Items extends User_Setting {
|
||||
/**
|
||||
* The user option name for this setting.
|
||||
*
|
||||
* @note This option is prefixed differently so that it will persist across disconnect/reset.
|
||||
*/
|
||||
const OPTION = 'googlesitekitpersistent_expirable_items';
|
||||
|
||||
/**
|
||||
* Adds one or more items to the list of expired items.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @param string $item Item to set expiration for.
|
||||
* @param int $expires_in_seconds TTL for the item.
|
||||
*/
|
||||
public function add( $item, $expires_in_seconds ) {
|
||||
$items = $this->get();
|
||||
$items[ $item ] = time() + $expires_in_seconds;
|
||||
|
||||
$this->set( $items );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes one or more items from the list of expirable items.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @param string $item Item to remove.
|
||||
*/
|
||||
public function remove( $item ) {
|
||||
$items = $this->get();
|
||||
|
||||
// If the item is not in expirable items, there's nothing to do.
|
||||
if ( ! array_key_exists( $item, $items ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
unset( $items[ $item ] );
|
||||
|
||||
$this->set( $items );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the setting.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @return array Value set for the option, or default if not set.
|
||||
*/
|
||||
public function get() {
|
||||
$value = parent::get();
|
||||
return is_array( $value ) ? $value : $this->get_default();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the expected value type.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @return string The type name.
|
||||
*/
|
||||
protected function get_type() {
|
||||
return 'array';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default value.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @return array The default value.
|
||||
*/
|
||||
protected function get_default() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the callback for sanitizing the setting's value before saving.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @return callable Sanitize callback.
|
||||
*/
|
||||
protected function get_sanitize_callback() {
|
||||
return function ( $items ) {
|
||||
return $this->filter_expirable_items( $items );
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters expirable items.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @param array $items Expirable items list.
|
||||
* @return array Filtered expirable items.
|
||||
*/
|
||||
private function filter_expirable_items( $items ) {
|
||||
$expirables = array();
|
||||
|
||||
if ( is_array( $items ) ) {
|
||||
foreach ( $items as $item => $ttl ) {
|
||||
if ( is_integer( $ttl ) ) {
|
||||
$expirables[ $item ] = $ttl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $expirables;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Expirables\Expirables
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Expirables
|
||||
* @copyright 2024 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\Expirables;
|
||||
|
||||
use Google\Site_Kit\Core\Expirables\Expirable_Items;
|
||||
use Google\Site_Kit\Core\Expirables\REST_Expirable_Items_Controller;
|
||||
use Google\Site_Kit\Context;
|
||||
use Google\Site_Kit\Core\Storage\User_Options;
|
||||
|
||||
/**
|
||||
* Class for handling expirables.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Expirables {
|
||||
|
||||
/**
|
||||
* Expirable_Items instance.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @var Expirable_Items
|
||||
*/
|
||||
protected $expirable_items;
|
||||
|
||||
/**
|
||||
* REST_Expirable_Items_Controller instance.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @var REST_Expirable_Items_Controller
|
||||
*/
|
||||
protected $rest_controller;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @param Context $context Plugin context.
|
||||
* @param User_Options $user_options Optional. User option API. Default is a new instance.
|
||||
*/
|
||||
public function __construct( Context $context, User_Options $user_options = null ) {
|
||||
$this->expirable_items = new Expirable_Items( $user_options ?: new User_Options( $context ) );
|
||||
$this->rest_controller = new REST_Expirable_Items_Controller( $this->expirable_items );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the reference to the Expirable_Items instance.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @return Expirable_Items An instance of the Expirable_Items class.
|
||||
*/
|
||||
public function get_expirable_items() {
|
||||
return $this->expirable_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers functionality through WordPress hooks.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*/
|
||||
public function register() {
|
||||
$this->expirable_items->register();
|
||||
$this->rest_controller->register();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Expirables\REST_Expirable_Items_Controller
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Expirables
|
||||
* @copyright 2024 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\Expirables;
|
||||
|
||||
use Google\Site_Kit\Core\Expirables\Expirable_Items;
|
||||
use Google\Site_Kit\Core\Permissions\Permissions;
|
||||
use Google\Site_Kit\Core\REST_API\REST_Route;
|
||||
use Google\Site_Kit\Core\REST_API\REST_Routes;
|
||||
use WP_Error;
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
use WP_REST_Server;
|
||||
|
||||
/**
|
||||
* Class for handling expirable items rest routes.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class REST_Expirable_Items_Controller {
|
||||
|
||||
/**
|
||||
* Expirable_Items instance.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @var Expirable_Items
|
||||
*/
|
||||
protected $expirable_items;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @param Expirable_Items $expirable_items Expirable items instance.
|
||||
*/
|
||||
public function __construct( Expirable_Items $expirable_items ) {
|
||||
$this->expirable_items = $expirable_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers functionality through WordPress hooks.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*/
|
||||
public function register() {
|
||||
add_filter(
|
||||
'googlesitekit_rest_routes',
|
||||
function ( $routes ) {
|
||||
return array_merge( $routes, $this->get_rest_routes() );
|
||||
}
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'googlesitekit_apifetch_preload_paths',
|
||||
function ( $paths ) {
|
||||
return array_merge(
|
||||
$paths,
|
||||
array(
|
||||
'/' . REST_Routes::REST_ROOT . '/core/user/data/expirable-items',
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets REST route instances.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @return REST_Route[] List of REST_Route objects.
|
||||
*/
|
||||
protected function get_rest_routes() {
|
||||
$can_manage_expirable_item = function() {
|
||||
return current_user_can( Permissions::VIEW_DASHBOARD );
|
||||
};
|
||||
|
||||
return array(
|
||||
new REST_Route(
|
||||
'core/user/data/expirable-items',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => function () {
|
||||
return new WP_REST_Response( $this->expirable_items->get() );
|
||||
},
|
||||
'permission_callback' => $can_manage_expirable_item,
|
||||
)
|
||||
),
|
||||
new REST_Route(
|
||||
'core/user/data/set-expirable-item-timers',
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => function ( WP_REST_Request $request ) {
|
||||
$data = $request['data'];
|
||||
|
||||
if ( empty( $data ) || ! is_array( $data ) ) {
|
||||
return new WP_Error(
|
||||
'missing_required_param',
|
||||
/* translators: %s: Missing parameter name */
|
||||
sprintf( __( 'Request parameter is empty: %s.', 'google-site-kit' ), 'items' ),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
|
||||
foreach ( $data as $datum ) {
|
||||
if ( empty( $datum['slug'] ) ) {
|
||||
return new WP_Error(
|
||||
'missing_required_param',
|
||||
/* translators: %s: Missing parameter name */
|
||||
sprintf( __( 'Request parameter is empty: %s.', 'google-site-kit' ), 'slug' ),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
|
||||
$expiration = null;
|
||||
if ( isset( $datum['expiration'] ) && intval( $datum['expiration'] ) > 0 ) {
|
||||
$expiration = $datum['expiration'];
|
||||
}
|
||||
|
||||
if ( ! $expiration ) {
|
||||
return new WP_Error(
|
||||
'missing_required_param',
|
||||
/* translators: %s: Missing parameter name */
|
||||
sprintf( __( 'Request parameter is invalid: %s.', 'google-site-kit' ), 'expiration' ),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
|
||||
$this->expirable_items->add( $datum['slug'], $expiration );
|
||||
}
|
||||
|
||||
return new WP_REST_Response( $this->expirable_items->get() );
|
||||
},
|
||||
'permission_callback' => $can_manage_expirable_item,
|
||||
'args' => array(
|
||||
'data' => array(
|
||||
'type' => 'array',
|
||||
'required' => true,
|
||||
'items' => array(
|
||||
'type' => 'object',
|
||||
'additionalProperties' => false,
|
||||
'properties' => array(
|
||||
'slug' => array(
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
),
|
||||
'expiration' => array(
|
||||
'type' => 'integer',
|
||||
'required' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user