update
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Dismissals\Dismissals
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Dismissals
|
||||
* @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\Dismissals;
|
||||
|
||||
use Google\Site_Kit\Context;
|
||||
use Google\Site_Kit\Core\Storage\User_Options;
|
||||
|
||||
/**
|
||||
* Class for handling dismissals.
|
||||
*
|
||||
* @since 1.37.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Dismissals {
|
||||
|
||||
/**
|
||||
* Dismissed_Items instance.
|
||||
*
|
||||
* @since 1.37.0
|
||||
* @var Dismissed_Items
|
||||
*/
|
||||
protected $dismissed_items;
|
||||
|
||||
/**
|
||||
* REST_Dismissals_Controller instance.
|
||||
*
|
||||
* @since 1.37.0
|
||||
* @var REST_Dismissals_Controller
|
||||
*/
|
||||
protected $rest_controller;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.37.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->dismissed_items = new Dismissed_Items( $user_options ?: new User_Options( $context ) );
|
||||
$this->rest_controller = new REST_Dismissals_Controller( $this->dismissed_items );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the reference to the Dismissed_Items instance.
|
||||
*
|
||||
* @since 1.69.0
|
||||
*
|
||||
* @return Dismissed_Items An instance of the Dismissed_Items class.
|
||||
*/
|
||||
public function get_dismissed_items() {
|
||||
return $this->dismissed_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers functionality through WordPress hooks.
|
||||
*
|
||||
* @since 1.37.0
|
||||
*/
|
||||
public function register() {
|
||||
$this->dismissed_items->register();
|
||||
$this->rest_controller->register();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Dismissals\Dismissed_Items
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Dismissals
|
||||
* @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\Dismissals;
|
||||
|
||||
use Closure;
|
||||
use Google\Site_Kit\Core\Storage\User_Setting;
|
||||
|
||||
/**
|
||||
* Class for representing a user's dismissed items.
|
||||
*
|
||||
* @since 1.37.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Dismissed_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_dismissed_items';
|
||||
|
||||
const DISMISS_ITEM_PERMANENTLY = 0;
|
||||
|
||||
/**
|
||||
* Adds one or more items to the list of dismissed items.
|
||||
*
|
||||
* @since 1.37.0
|
||||
*
|
||||
* @param string $item Item to dismiss.
|
||||
* @param int $expires_in_seconds TTL for the item.
|
||||
*/
|
||||
public function add( $item, $expires_in_seconds = self::DISMISS_ITEM_PERMANENTLY ) {
|
||||
$items = $this->get();
|
||||
$items[ $item ] = $expires_in_seconds ? time() + $expires_in_seconds : 0;
|
||||
|
||||
$this->set( $items );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes one or more items from the list of dismissed items.
|
||||
*
|
||||
* @since 1.107.0
|
||||
*
|
||||
* @param string $item Item to remove.
|
||||
*/
|
||||
public function remove( $item ) {
|
||||
$items = $this->get();
|
||||
|
||||
// If the item is not in dismissed 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.37.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.37.0
|
||||
*
|
||||
* @return string The type name.
|
||||
*/
|
||||
protected function get_type() {
|
||||
return 'array';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default value.
|
||||
*
|
||||
* @since 1.37.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.37.0
|
||||
*
|
||||
* @return callable Sanitize callback.
|
||||
*/
|
||||
protected function get_sanitize_callback() {
|
||||
return function ( $items ) {
|
||||
return $this->filter_dismissed_items( $items );
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the item is dismissed.
|
||||
*
|
||||
* @since 1.37.0
|
||||
*
|
||||
* @param string $item The item to check.
|
||||
* @return bool TRUE if item is dismissed, otherwise FALSE.
|
||||
*/
|
||||
public function is_dismissed( $item ) {
|
||||
$items = $this->get();
|
||||
if ( ! array_key_exists( $item, $items ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ttl = $items[ $item ];
|
||||
if ( $ttl > 0 && $ttl < time() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets dismissed items.
|
||||
*
|
||||
* @since 1.37.0
|
||||
*
|
||||
* @return array Dismissed items array.
|
||||
*/
|
||||
public function get_dismissed_items() {
|
||||
$dismissed_items = $this->get();
|
||||
$dismissed_items = $this->filter_dismissed_items( $dismissed_items );
|
||||
|
||||
return array_keys( $dismissed_items );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters dismissed items.
|
||||
*
|
||||
* @since 1.37.0
|
||||
*
|
||||
* @param array $items Dismissed items list.
|
||||
* @return array Filtered dismissed items.
|
||||
*/
|
||||
private function filter_dismissed_items( $items ) {
|
||||
$dismissed = array();
|
||||
|
||||
if ( is_array( $items ) ) {
|
||||
foreach ( $items as $item => $ttl ) {
|
||||
if ( self::DISMISS_ITEM_PERMANENTLY === $ttl || $ttl > time() ) {
|
||||
$dismissed[ $item ] = $ttl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $dismissed;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Dismissals\REST_Dismissals_Controller
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Dismissals
|
||||
* @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\Dismissals;
|
||||
|
||||
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 dismissed items rest routes.
|
||||
*
|
||||
* @since 1.37.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class REST_Dismissals_Controller {
|
||||
|
||||
/**
|
||||
* Dismissed_Items instance.
|
||||
*
|
||||
* @since 1.37.0
|
||||
* @var Dismissed_Items
|
||||
*/
|
||||
protected $dismissed_items;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.37.0
|
||||
*
|
||||
* @param Dismissed_Items $dismissed_items Dismissed items instance.
|
||||
*/
|
||||
public function __construct( Dismissed_Items $dismissed_items ) {
|
||||
$this->dismissed_items = $dismissed_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers functionality through WordPress hooks.
|
||||
*
|
||||
* @since 1.37.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/dismissed-items',
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets REST route instances.
|
||||
*
|
||||
* @since 1.37.0
|
||||
*
|
||||
* @return REST_Route[] List of REST_Route objects.
|
||||
*/
|
||||
protected function get_rest_routes() {
|
||||
$can_dismiss_item = function() {
|
||||
return current_user_can( Permissions::VIEW_SPLASH ) || current_user_can( Permissions::VIEW_DASHBOARD );
|
||||
};
|
||||
|
||||
return array(
|
||||
new REST_Route(
|
||||
'core/user/data/dismissed-items',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => function () {
|
||||
return new WP_REST_Response( $this->dismissed_items->get_dismissed_items() );
|
||||
},
|
||||
'permission_callback' => $can_dismiss_item,
|
||||
)
|
||||
),
|
||||
new REST_Route(
|
||||
'core/user/data/dismiss-item',
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => function ( WP_REST_Request $request ) {
|
||||
$data = $request['data'];
|
||||
|
||||
if ( empty( $data['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 = Dismissed_Items::DISMISS_ITEM_PERMANENTLY;
|
||||
if ( isset( $data['expiration'] ) && intval( $data['expiration'] ) > 0 ) {
|
||||
$expiration = $data['expiration'];
|
||||
}
|
||||
|
||||
$this->dismissed_items->add( $data['slug'], $expiration );
|
||||
|
||||
return new WP_REST_Response( $this->dismissed_items->get_dismissed_items() );
|
||||
},
|
||||
'permission_callback' => $can_dismiss_item,
|
||||
'args' => array(
|
||||
'data' => array(
|
||||
'type' => 'object',
|
||||
'required' => true,
|
||||
),
|
||||
),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user