Skip to content

Commit

Permalink
Merge branch 'trunk' into social/unified-connections-management
Browse files Browse the repository at this point in the history
  • Loading branch information
manzoorwanijk committed Jan 15, 2025
2 parents c27ef5c + 270e71d commit 4fbe10b
Show file tree
Hide file tree
Showing 24 changed files with 202 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Moved WPCOM_REST_API_Proxy_Request trait to the connection package
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
<?php
/**
* Trait WPCOM_REST_API_Proxy_Request_Trait
* Trait WPCOM_REST_API_Proxy_Request
*
* Used to proxy requests to wpcom servers.
*
* @package automattic/jetpack
* @package automattic/jetpack-connection
*/

namespace Automattic\Jetpack\Connection\Traits;

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Status\Visitor;
use WP_Error;
use WP_REST_Request;

trait WPCOM_REST_API_Proxy_Request {

trait WPCOM_REST_API_Proxy_Request_Trait {
/**
* Base path for the API.
*
* @var string
*/
protected $base_api_path;

/**
* Version of the API.
*
* @var string
*/
protected $version;

/**
* The base of the controller's route.
*
* @var string
*/
protected $rest_base;

/**
* Proxy request to wpcom servers on behalf of a user or using the Site-level Connection (blog token).
*
* @param WP_Rest_Request $request Request to proxy.
* @param WP_REST_Request $request Request to proxy.
* @param string $path Path to append to the rest base.
* @param string $context Whether the request should be proxied on behalf of the current user or using the Site-level Connection, aka 'blog' token. Can be Either 'user' or 'blog'. Defaults to 'user'.
* @param bool $allow_fallback_to_blog If the $context is 'user', whether we should fallback to using the Site-level Connection in case the current user is not connected.
* @param array $request_options Request options to pass to wp_remote_request.
*
* @return mixed|WP_Error Response from wpcom servers or an error.
*/
public function proxy_request_to_wpcom( $request, $path = '', $context = 'user', $allow_fallback_to_blog = false ) {
public function proxy_request_to_wpcom( $request, $path = '', $context = 'user', $allow_fallback_to_blog = false, $request_options = array() ) {
$blog_id = \Jetpack_Options::get_option( 'id' );
$path = '/sites/' . rawurldecode( $blog_id ) . '/' . rawurldecode( ltrim( $this->rest_base, '/' ) ) . ( $path ? '/' . rawurldecode( ltrim( $path, '/' ) ) : '' );
$query_params = $request->get_query_params();
Expand All @@ -40,20 +66,23 @@ public function proxy_request_to_wpcom( $request, $path = '', $context = 'user',
}
$api_url = add_query_arg( $query_params, $path );

$request_options = array(
'headers' => array(
'Content-Type' => 'application/json',
'X-Forwarded-For' => ( new Visitor() )->get_ip( true ),
$request_options = array_replace_recursive(
array(
'headers' => array(
'Content-Type' => 'application/json',
'X-Forwarded-For' => ( new Visitor() )->get_ip( true ),
),
'method' => $request->get_method(),
),
'method' => $request->get_method(),
$request_options
);

// If no body is present, passing it as $request->get_body() will cause an error.
$body = $request->get_body() ? $request->get_body() : null;

$response = new WP_Error(
'rest_unauthorized',
__( 'Please connect your user account to WordPress.com', 'jetpack' ),
__( 'Please connect your user account to WordPress.com', 'jetpack-connection' ),
array( 'status' => rest_authorization_required_code() )
);

Expand Down Expand Up @@ -85,8 +114,8 @@ public function proxy_request_to_wpcom( $request, $path = '', $context = 'user',
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( $response_status >= 400 ) {
$code = isset( $response_body['code'] ) ? $response_body['code'] : 'unknown_error';
$message = isset( $response_body['message'] ) ? $response_body['message'] : __( 'An unknown error occurred.', 'jetpack' );
$code = $response_body['code'] ?? 'unknown_error';
$message = $response_body['message'] ?? __( 'An unknown error occurred.', 'jetpack-connection' );

return new WP_Error( $code, $message, array( 'status' => $response_status ) );
}
Expand All @@ -97,24 +126,26 @@ public function proxy_request_to_wpcom( $request, $path = '', $context = 'user',
/**
* Proxy request to wpcom servers on behalf of a user.
*
* @param WP_Rest_Request $request Request to proxy.
* @param WP_REST_Request $request Request to proxy.
* @param string $path Path to append to the rest base.
* @param array $request_options Request options to pass to wp_remote_request.
*
* @return mixed|WP_Error Response from wpcom servers or an error.
*/
public function proxy_request_to_wpcom_as_user( $request, $path = '' ) {
return $this->proxy_request_to_wpcom( $request, $path, 'user' );
public function proxy_request_to_wpcom_as_user( $request, $path = '', $request_options = array() ) {
return $this->proxy_request_to_wpcom( $request, $path, 'user', false, $request_options );
}

/**
* Proxy request to wpcom servers using the Site-level Connection (blog token).
*
* @param WP_Rest_Request $request Request to proxy.
* @param WP_REST_Request $request Request to proxy.
* @param string $path Path to append to the rest base.
* @param array $request_options Request options to pass to wp_remote_request.
*
* @return mixed|WP_Error Response from wpcom servers or an error.
*/
public function proxy_request_to_wpcom_as_blog( $request, $path = '' ) {
return $this->proxy_request_to_wpcom( $request, $path, 'blog' );
public function proxy_request_to_wpcom_as_blog( $request, $path = '', $request_options = array() ) {
return $this->proxy_request_to_wpcom( $request, $path, 'blog', false, $request_options );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Import Media: Introduce the Import Media page
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Removed no longer used code


Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public static function load_wpcom_user_features() {
require_once __DIR__ . '/features/wpcom-command-palette/wpcom-command-palette.php';
require_once __DIR__ . '/features/wpcom-dashboard-widgets/wpcom-dashboard-widgets.php';
require_once __DIR__ . '/features/wpcom-locale/sync-locale-from-calypso-to-atomic.php';
require_once __DIR__ . '/features/wpcom-media/wpcom-external-media-import.php';
require_once __DIR__ . '/features/wpcom-plugins/wpcom-plugins.php';
require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-link-to-wpcom.php';
require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-notices.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,7 @@ const Notice = () => {
},
};

const icons = {
'edit.php': verse,
'edit.php?post_type=page': pages,
'edit.php?post_type=jetpack-portfolio': archive,
'edit.php?post_type=jetpack-testimonial': commentContent,
'edit-comments.php': postComments,
'edit-tags.php?taxonomy=category': category,
'edit-tags.php?taxonomy=post_tag': tag,
};

if ( ! Object.keys( icons ).includes( removedCalypsoScreenNoticeConfig.screen ) ) {
if ( ! Object.keys( config ).includes( removedCalypsoScreenNoticeConfig.screen ) ) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log( 'Hello, Import Media Page' ); // eslint-disable-line no-console
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* WordPress.com media import page.
*
* Adds WordPress.com-specific external media page to WordPress Media > Import.
*
* @package automattic/jetpack-mu-wpcom
*/

if ( empty( $_GET['wpcom_external_media_import_page'] ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
return;
}

/**
* Register the WordPress.com-specific external media page to Media > Import.
*/
function add_wpcom_external_media_import_page() {
$wpcom_external_media_import_page_hook = add_submenu_page(
'upload.php',
__( 'Import Media', 'jetpack-mu-wpcom' ),
__( 'Import Media', 'jetpack-mu-wpcom' ),
'upload_files',
'wpcom_external_media_import_page',
'render_wpcom_external_media_import_page'
);

add_action( "load-$wpcom_external_media_import_page_hook", 'enqueue_wpcom_external_media_import_page' );
}
add_action( 'admin_menu', 'add_wpcom_external_media_import_page' );

/**
* Enqueue the assets of the wpcom external media page.
*/
function enqueue_wpcom_external_media_import_page() {
jetpack_mu_wpcom_enqueue_assets( 'wpcom-external-media-import-page', array( 'js' ) );
}

/**
* Render the container of the wpcom external media page.
*/
function render_wpcom_external_media_import_page() {
$title = __( 'Import Media', 'jetpack-mu-wpcom' );
$description = __( 'WordPress.com allows you to import media from various platforms directly into the Media Library. To begin, select a platform from the options below:', 'jetpack-mu-wpcom' );
$external_media_sources = array(
array(
'id' => 'google_photos',
'name' => __( 'Google Photos', 'jetpack-mu-wpcom' ),
'description' => __( 'Import media from your Google Photos account.', 'jetpack-mu-wpcom' ),
),
array(
'id' => 'pexels',
'name' => __( 'Pexels free photos', 'jetpack-mu-wpcom' ),
'description' => __( 'Free stock photos, royalty free images shared by creators.', 'jetpack-mu-wpcom' ),
),
array(
'id' => 'openverse',
'name' => __( 'Openverse', 'jetpack-mu-wpcom' ),
'description' => __( 'Explore more than 800 million creative works.', 'jetpack-mu-wpcom' ),
),
);

?>
<div class="wrap">
<h1><?php echo esc_html( $title ); ?></h1>
<p><?php echo esc_html( $description ); ?></p>
<table class="widefat importers striped">
<?php
foreach ( $external_media_sources as $external_media_source ) {
$id = $external_media_source['id'];
$name = $external_media_source['name'];
$description = $external_media_source['description'];
$action = sprintf(
'<a id="%1$s" aria-label="%2$s">%3$s</a>',
esc_attr( $id ),
/* translators: %s: The name of the external media source. */
esc_attr( sprintf( __( 'Import %s', 'jetpack-mu-wpcom' ), $name ) ),
__( 'Import now', 'jetpack-mu-wpcom' )
);

?>
<tr class='importer-item'>
<td class='import-system'>
<span class='importer-title'><?php echo esc_html( $name ); ?></span>
<span class='importer-action'>
<?php echo $action; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we escape things above. ?>
</span>
</td>
<td class='desc'>
<span class='importer-desc'><?php echo esc_html( $description ); ?></span>
</td>
</tr>
<?php
}
?>
</table>
</div>
<?php
}
2 changes: 2 additions & 0 deletions projects/packages/jetpack-mu-wpcom/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ module.exports = [
'./src/features/wpcom-global-styles/wpcom-global-styles-view.js',
'wpcom-documentation-links':
'./src/features/wpcom-documentation-links/wpcom-documentation-links.ts',
'wpcom-external-media-import-page':
'./src/features/wpcom-media/wpcom-external-media-import.js',
'wpcom-plugins-banner': './src/features/wpcom-plugins/js/banner.js',
'wpcom-plugins-banner-style': './src/features/wpcom-plugins/css/banner.css',
'wpcom-profile-settings-link-to-wpcom':
Expand Down
1 change: 0 additions & 1 deletion projects/plugins/jetpack/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
'_inc/lib/core-api/wpcom-endpoints/publicize-connections.php' => ['PhanParamSignatureMismatch', 'PhanTypeMismatchArgument'],
'_inc/lib/core-api/wpcom-endpoints/publicize-services.php' => ['PhanParamSignatureMismatch', 'PhanPluginMixedKeyNoKey', 'PhanTypeMismatchArgument'],
'_inc/lib/core-api/wpcom-endpoints/service-api-keys.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeArraySuspicious', 'PhanTypeMismatchReturnProbablyReal'],
'_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanUndeclaredProperty'],
'_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMissingReturn'],
'_inc/lib/debugger/class-jetpack-cxn-test-base.php' => ['PhanDeprecatedFunctionInternal', 'PhanTypeArraySuspiciousNullable', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchReturn'],
'_inc/lib/debugger/class-jetpack-cxn-tests.php' => ['PhanPluginSimplifyExpressionBool'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
use Automattic\Jetpack\Status\Host;

require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';
require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';

/**
Expand All @@ -19,7 +19,7 @@
*/
class WPCOM_REST_API_V2_Endpoint_Email_Preview extends WP_REST_Controller {

use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
* @since 12.6
*/

use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
use Automattic\Jetpack\Status\Host;

require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';

/**
* Class WPCOM_REST_API_V2_Endpoint_Following
*/
class WPCOM_REST_API_V2_Endpoint_Newsletter_Categories_List extends WP_REST_Controller {
use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
* @since 12.6
*/

use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
use Automattic\Jetpack\Status\Host;

require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';

/**
* Class WPCOM_REST_API_V2_Endpoint_Newsletter_Categories_Subscriptions_Count
*/
class WPCOM_REST_API_V2_Endpoint_Newsletter_Categories_Subscriptions_Count extends WP_REST_Controller {
use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
*/

use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
use Automattic\Jetpack\Status\Host;

require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';

/**
* Class WPCOM_REST_API_V2_Endpoint_Send_Email_Preview
* Handles the sending of email previews via the WordPress.com REST API
*/
class WPCOM_REST_API_V2_Endpoint_Send_Email_Preview extends WP_REST_Controller {

use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
* @package automattic/jetpack
*/

// Ensure WPCOM_REST_API_Proxy_Request_Trait is present.
require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';
use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;

/**
* REST API endpoint wpcom/v3/sites/%s/blogging-prompts.
*/
class WPCOM_REST_API_V3_Endpoint_Blogging_Prompts extends WP_REST_Posts_Controller {

use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

const TEMPLATE_BLOG_ID = 205876834;

Expand Down
Loading

0 comments on commit 4fbe10b

Please sign in to comment.