update
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Prompts\Dismissed_Prompts
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Prompts
|
||||
* @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\Prompts;
|
||||
|
||||
use Google\Site_Kit\Core\Storage\User_Setting;
|
||||
|
||||
/**
|
||||
* Class for representing a user's dismissed prompts.
|
||||
*
|
||||
* @since 1.121.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Dismissed_Prompts 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_prompts';
|
||||
|
||||
const DISMISS_PROMPT_PERMANENTLY = 0;
|
||||
|
||||
/**
|
||||
* Adds one prompt to the list of dismissed prompts or updates the triggered count.
|
||||
*
|
||||
* @since 1.121.0
|
||||
*
|
||||
* @param string $prompt Prompt to dismiss.
|
||||
* @param int $expires_in_seconds TTL for the prompt.
|
||||
*/
|
||||
public function add( $prompt, $expires_in_seconds = self::DISMISS_PROMPT_PERMANENTLY ) {
|
||||
$prompts = $this->get();
|
||||
|
||||
if ( array_key_exists( $prompt, $prompts ) ) {
|
||||
$prompts[ $prompt ]['expires'] = $expires_in_seconds ? time() + $expires_in_seconds : 0;
|
||||
$prompts[ $prompt ]['count'] = $prompts[ $prompt ]['count'] + 1;
|
||||
} else {
|
||||
$prompts[ $prompt ] = array(
|
||||
'expires' => $expires_in_seconds ? time() + $expires_in_seconds : 0,
|
||||
'count' => 1,
|
||||
);
|
||||
}
|
||||
|
||||
$this->set( $prompts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes one or more prompts from the list of dismissed prompts.
|
||||
*
|
||||
* @since 1.121.0
|
||||
*
|
||||
* @param string $prompt Item to remove.
|
||||
*/
|
||||
public function remove( $prompt ) {
|
||||
$prompts = $this->get();
|
||||
|
||||
// If the prompt is not in dismissed prompts, there's nothing to do.
|
||||
if ( ! array_key_exists( $prompt, $prompts ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
unset( $prompts[ $prompt ] );
|
||||
|
||||
$this->set( $prompts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the setting.
|
||||
*
|
||||
* @since 1.121.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.121.0
|
||||
*
|
||||
* @return string The type name.
|
||||
*/
|
||||
protected function get_type() {
|
||||
return 'array';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default value.
|
||||
*
|
||||
* @since 1.121.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.121.0
|
||||
*
|
||||
* @return callable Sanitize callback.
|
||||
*/
|
||||
protected function get_sanitize_callback() {
|
||||
return function ( $prompts ) {
|
||||
if ( ! is_array( $prompts ) ) {
|
||||
return $this->get_default();
|
||||
}
|
||||
|
||||
$sanitized_prompts = array();
|
||||
|
||||
foreach ( $prompts as $prompt => $data ) {
|
||||
if ( is_array( $data ) && isset( $data['expires'], $data['count'] ) && is_numeric( $data['expires'] ) && is_numeric( $data['count'] ) ) {
|
||||
$sanitized_prompts[ $prompt ] = array(
|
||||
'expires' => $data['expires'],
|
||||
'count' => $data['count'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $sanitized_prompts;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Prompts\Prompts
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Prompts
|
||||
* @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\Prompts;
|
||||
|
||||
use Google\Site_Kit\Context;
|
||||
use Google\Site_Kit\Core\Storage\User_Options;
|
||||
|
||||
/**
|
||||
* Class for handling prompts.
|
||||
*
|
||||
* @since 1.121.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Prompts {
|
||||
|
||||
/**
|
||||
* Dismissed_Prompts instance.
|
||||
*
|
||||
* @since 1.121.0
|
||||
* @var Dismissed_Prompts
|
||||
*/
|
||||
protected $dismissed_prompts;
|
||||
|
||||
/**
|
||||
* REST_Prompts_Controller instance.
|
||||
*
|
||||
* @since 1.121.0
|
||||
* @var REST_Prompts_Controller
|
||||
*/
|
||||
protected $rest_controller;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.121.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_prompts = new Dismissed_Prompts( $user_options ?: new User_Options( $context ) );
|
||||
$this->rest_controller = new REST_Prompts_Controller( $this->dismissed_prompts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the reference to the Dismissed_Prompts instance.
|
||||
*
|
||||
* @since 1.121.0
|
||||
*
|
||||
* @return Dismissed_Prompts An instance of the Dismissed_Prompts class.
|
||||
*/
|
||||
public function get_dismissed_prompts() {
|
||||
return $this->dismissed_prompts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers functionality through WordPress hooks.
|
||||
*
|
||||
* @since 1.121.0
|
||||
*/
|
||||
public function register() {
|
||||
$this->dismissed_prompts->register();
|
||||
$this->rest_controller->register();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Prompts\REST_Prompts_Controller
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Prompts
|
||||
* @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\Prompts;
|
||||
|
||||
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 prompts rest routes.
|
||||
*
|
||||
* @since 1.121.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class REST_Prompts_Controller {
|
||||
|
||||
/**
|
||||
* Dismissed_Prompts instance.
|
||||
*
|
||||
* @since 1.121.0
|
||||
* @var Dismissed_Prompts
|
||||
*/
|
||||
protected $dismissed_prompts;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.121.0
|
||||
*
|
||||
* @param Dismissed_Prompts $dismissed_prompts Dismissed prompts instance.
|
||||
*/
|
||||
public function __construct( Dismissed_Prompts $dismissed_prompts ) {
|
||||
$this->dismissed_prompts = $dismissed_prompts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers functionality through WordPress hooks.
|
||||
*
|
||||
* @since 1.121.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-prompts',
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets REST route instances.
|
||||
*
|
||||
* @since 1.121.0
|
||||
*
|
||||
* @return REST_Route[] List of REST_Route objects.
|
||||
*/
|
||||
protected function get_rest_routes() {
|
||||
$can_dismiss_prompt = function() {
|
||||
return current_user_can( Permissions::VIEW_SPLASH ) || current_user_can( Permissions::VIEW_DASHBOARD );
|
||||
};
|
||||
|
||||
return array(
|
||||
new REST_Route(
|
||||
'core/user/data/dismissed-prompts',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => function () {
|
||||
return new WP_REST_Response( $this->dismissed_prompts->get() );
|
||||
},
|
||||
'permission_callback' => $can_dismiss_prompt,
|
||||
)
|
||||
),
|
||||
new REST_Route(
|
||||
'core/user/data/dismiss-prompt',
|
||||
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_Prompts::DISMISS_PROMPT_PERMANENTLY;
|
||||
if ( isset( $data['expiration'] ) && intval( $data['expiration'] ) > 0 ) {
|
||||
$expiration = $data['expiration'];
|
||||
}
|
||||
|
||||
$this->dismissed_prompts->add( $data['slug'], $expiration );
|
||||
|
||||
return new WP_REST_Response( $this->dismissed_prompts->get() );
|
||||
},
|
||||
'permission_callback' => $can_dismiss_prompt,
|
||||
'args' => array(
|
||||
'data' => array(
|
||||
'type' => 'object',
|
||||
'required' => true,
|
||||
),
|
||||
),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user