update
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Notifications\Notification
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Notifications
|
||||
* @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\Notifications;
|
||||
|
||||
/**
|
||||
* Class for representing a notification.
|
||||
*
|
||||
* @since 1.4.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Notification {
|
||||
/**
|
||||
* Unique notification slug.
|
||||
*
|
||||
* @since 1.4.0
|
||||
* @var string
|
||||
*/
|
||||
private $slug;
|
||||
|
||||
/**
|
||||
* Notification arguments.
|
||||
*
|
||||
* @since 1.4.0
|
||||
* @var array
|
||||
*/
|
||||
private $args;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param string $slug Unique notification slug.
|
||||
* @param array $args {
|
||||
* Associative array of notification arguments.
|
||||
*
|
||||
* @type string $title Required notification title.
|
||||
* @type string $content Required notification content. May contain inline HTML tags.
|
||||
* @type string $cta_url Call to action URL.
|
||||
* @type string $cta_label Call to action anchor text.
|
||||
* @type string $cta_target Call to action anchor target.
|
||||
* @type string $learn_more_url Learn more URL.
|
||||
* @type string $learn_more_label Learn more anchor text.
|
||||
* @type bool $dismissible Whether the notice should be dismissible. Default false.
|
||||
* @type string $dismiss_label Dismiss anchor text.
|
||||
* }
|
||||
*/
|
||||
public function __construct( $slug, array $args ) {
|
||||
$this->slug = (string) $slug;
|
||||
$this->args = array_merge(
|
||||
array(
|
||||
'title' => '',
|
||||
'content' => '',
|
||||
'cta_url' => '',
|
||||
'cta_label' => '',
|
||||
'cta_target' => '',
|
||||
'learn_more_url' => '',
|
||||
'learn_more_label' => '',
|
||||
'dismissible' => false,
|
||||
'dismiss_label' => __( 'Dismiss', 'google-site-kit' ),
|
||||
),
|
||||
$args
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the notification's slug.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @return string Unique notification slug.
|
||||
*/
|
||||
public function get_slug() {
|
||||
return $this->slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the JS representation of the Notification.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function prepare_for_js() {
|
||||
return array(
|
||||
'id' => $this->get_slug(),
|
||||
'title' => $this->args['title'],
|
||||
'content' => $this->args['content'],
|
||||
'ctaURL' => $this->args['cta_url'],
|
||||
'ctaLabel' => $this->args['cta_label'],
|
||||
'ctaTarget' => $this->args['cta_target'],
|
||||
'learnMoreURL' => $this->args['learn_more_url'],
|
||||
'learnMoreLabel' => $this->args['learn_more_label'],
|
||||
'dismissible' => $this->args['dismissible'],
|
||||
'dismissLabel' => $this->args['dismiss_label'],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Notifications\Notifications.php
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Notifications
|
||||
* @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\Notifications;
|
||||
|
||||
use Exception;
|
||||
use Google\Site_Kit\Context;
|
||||
use Google\Site_Kit\Core\Authentication\Credentials;
|
||||
use Google\Site_Kit\Core\Authentication\Google_Proxy;
|
||||
use Google\Site_Kit\Core\Authentication\Authentication;
|
||||
use Google\Site_Kit\Core\Permissions\Permissions;
|
||||
use Google\Site_Kit\Core\REST_API\REST_Route;
|
||||
use Google\Site_Kit\Core\Storage\Encrypted_Options;
|
||||
use Google\Site_Kit\Core\Storage\Options;
|
||||
use WP_Error;
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
use WP_REST_Server;
|
||||
|
||||
/**
|
||||
* Class for managing core notifications.
|
||||
*
|
||||
* @since 1.4.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Notifications {
|
||||
/**
|
||||
* Context instance.
|
||||
*
|
||||
* @since 1.4.0
|
||||
* @var Context
|
||||
*/
|
||||
private $context;
|
||||
|
||||
/**
|
||||
* Options instance.
|
||||
*
|
||||
* @since 1.4.0
|
||||
* @var Options
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Authentication instance.
|
||||
*
|
||||
* @since 1.8.0
|
||||
* @var Authentication
|
||||
*/
|
||||
private $authentication;
|
||||
|
||||
/**
|
||||
* Google_Proxy instance.
|
||||
*
|
||||
* @since 1.4.0
|
||||
* @var Google_Proxy
|
||||
*/
|
||||
private $google_proxy;
|
||||
|
||||
/**
|
||||
* Credentials instance.
|
||||
*
|
||||
* @since 1.4.0
|
||||
* @var Credentials
|
||||
*/
|
||||
private $credentials;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param Context $context Context instance.
|
||||
* @param Options $options Options instance.
|
||||
* @param Authentication $authentication Authentication instance.
|
||||
*/
|
||||
public function __construct( Context $context, Options $options = null, Authentication $authentication = null ) {
|
||||
$this->context = $context;
|
||||
$this->options = $options ?: new Options( $context );
|
||||
$this->google_proxy = new Google_Proxy( $this->context );
|
||||
$this->authentication = $authentication ?: new Authentication( $this->context );
|
||||
$this->credentials = $this->authentication->credentials();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers core notifications.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public function register() {
|
||||
add_filter(
|
||||
'googlesitekit_rest_routes',
|
||||
function ( $routes ) {
|
||||
return array_merge( $routes, $this->get_rest_routes() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets related REST routes.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @return array List of REST_Route objects.
|
||||
*/
|
||||
private function get_rest_routes() {
|
||||
$can_use_notifications = function () {
|
||||
return current_user_can( Permissions::SETUP ) && $this->credentials->has();
|
||||
};
|
||||
|
||||
return array(
|
||||
new REST_Route(
|
||||
'core/site/data/notifications',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => function ( WP_REST_Request $request ) {
|
||||
$endpoint = add_query_arg(
|
||||
array(
|
||||
'site_id' => $this->credentials->get()['oauth2_client_id'],
|
||||
),
|
||||
$this->google_proxy->url( '/notifications/' )
|
||||
);
|
||||
|
||||
// Return an empty array of notifications if the user isn't using the proxy.
|
||||
if ( ! $this->credentials->using_proxy() ) {
|
||||
return new WP_REST_Response( array() );
|
||||
}
|
||||
|
||||
$response = wp_remote_get( $endpoint ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->parse_response( $response );
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'exception', $e->getMessage() );
|
||||
}
|
||||
|
||||
$data = array_map(
|
||||
function ( Notification $notification ) {
|
||||
return $notification->prepare_for_js();
|
||||
},
|
||||
$this->map_response_to_notifications( $response )
|
||||
);
|
||||
|
||||
return new WP_REST_Response( $data );
|
||||
},
|
||||
'permission_callback' => $can_use_notifications,
|
||||
),
|
||||
)
|
||||
),
|
||||
new REST_Route(
|
||||
'core/site/data/mark-notification',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => function ( WP_REST_Request $request ) {
|
||||
$data = $request['data'];
|
||||
|
||||
if ( empty( $data['notificationID'] ) ) {
|
||||
return $this->missing_required_param( 'data.notificationID' );
|
||||
}
|
||||
if ( empty( $data['notificationState'] ) ) {
|
||||
return $this->missing_required_param( 'data.notificationState' );
|
||||
}
|
||||
|
||||
$credentials = $this->credentials->get();
|
||||
$response = wp_remote_post(
|
||||
$this->google_proxy->url( '/notifications/mark/' ),
|
||||
array(
|
||||
'body' => array(
|
||||
'site_id' => $credentials['oauth2_client_id'],
|
||||
'site_secret' => $credentials['oauth2_client_secret'],
|
||||
'notification_id' => $data['notificationID'],
|
||||
'notification_state' => $data['notificationState'],
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->parse_response( $response );
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'exception', $e->getMessage() );
|
||||
}
|
||||
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'success' => isset( $response['success'] ) ? (bool) $response['success'] : false,
|
||||
)
|
||||
);
|
||||
},
|
||||
'args' => array(
|
||||
'data' => array(
|
||||
'required' => true,
|
||||
'type' => 'object',
|
||||
),
|
||||
),
|
||||
'permission_callback' => $can_use_notifications,
|
||||
),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and parses the given JSON response into an array.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param array $response HTTP response array.
|
||||
* @return mixed JSON decoded response.
|
||||
* @throws Exception Throws exception if response cannot be parsed or if an error is returned.
|
||||
*/
|
||||
private function parse_response( $response ) {
|
||||
$body = wp_remote_retrieve_body( $response );
|
||||
$decoded = json_decode( $body, true );
|
||||
|
||||
if ( json_last_error() ) {
|
||||
throw new Exception( 'Error while decoding response: ' . json_last_error() );
|
||||
}
|
||||
|
||||
if ( ! empty( $decoded['error'] ) ) {
|
||||
throw new Exception( $decoded['error'] );
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the response objects into Notification objects.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param array $response Array of notification objects from API.
|
||||
* @return Notification[] Array of Notification objects.
|
||||
*/
|
||||
private function map_response_to_notifications( array $response ) {
|
||||
return array_map(
|
||||
function ( $notification ) {
|
||||
return new Notification(
|
||||
$notification['id'],
|
||||
array(
|
||||
'title' => $notification['title'],
|
||||
'content' => $notification['content'],
|
||||
'cta_url' => $notification['ctaURL'],
|
||||
'cta_label' => $notification['ctaLabel'],
|
||||
'cta_target' => $notification['ctaTarget'],
|
||||
'learn_more_url' => $notification['learnMoreURL'],
|
||||
'learn_more_label' => $notification['learnMoreLabel'],
|
||||
'dismissible' => $notification['dismissible'],
|
||||
'dismiss_label' => $notification['dismissLabel'],
|
||||
)
|
||||
);
|
||||
},
|
||||
$response
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a WP_Error instance for the given missing required parameter.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param string $param Missing required parameter.
|
||||
* @return WP_Error
|
||||
*/
|
||||
private function missing_required_param( $param ) {
|
||||
return new WP_Error(
|
||||
'missing_required_param',
|
||||
/* translators: %s: Missing parameter name */
|
||||
sprintf( __( 'Request parameter is empty: %s.', 'google-site-kit' ), $param ),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user