-
Notifications
You must be signed in to change notification settings - Fork 812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move wpcom/v2/publicize/connections endpoint to publicize package #40607
Changes from all commits
64dae1c
8c7af2b
645afa3
d284e58
b6adf61
b2e8359
6f8d281
fb4d36f
9e9705c
7925061
ac3f287
d88b9a3
8c4a00e
b327a58
f28680e
7fa7069
d70c2b9
39330e3
e6273ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: changed | ||
|
||
Moved wpcom/v2/publicize/connections endpoint to publicize package |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ | |
|
||
namespace Automattic\Jetpack\Publicize; | ||
|
||
use Automattic\Jetpack\Publicize\REST_API\Connections_Controller; | ||
use Automattic\Jetpack\Status\Host; | ||
|
||
/** | ||
* The class to configure and initialize the publicize package. | ||
*/ | ||
|
@@ -26,6 +29,30 @@ public static function configure() { | |
add_action( 'jetpack_feature_publicize_enabled', array( __CLASS__, 'on_jetpack_feature_publicize_enabled' ) ); | ||
} | ||
|
||
/** | ||
* Initialization of publicize logic that should always be loaded. | ||
*/ | ||
public static function pre_initialization() { | ||
|
||
$is_wpcom = ( new Host() )->is_wpcom_simple(); | ||
|
||
// Assets are to be loaded in all cases. | ||
Publicize_Assets::configure(); | ||
|
||
$rest_controllers = array( | ||
Connections_Controller::class, | ||
); | ||
|
||
// Load the REST controllers. | ||
foreach ( $rest_controllers as $controller ) { | ||
if ( $is_wpcom ) { | ||
wpcom_rest_api_v2_load_plugin( $controller ); | ||
} else { | ||
new $controller(); | ||
} | ||
} | ||
Comment on lines
+37
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should really be part of We could move this in there and then call What do you think about calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let us improve that initialization in a follow up PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have created a task for it and added it to our backlog. |
||
} | ||
|
||
manzoorwanijk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* To configure the publicize package, when called via the Config package. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* Base Controller class. | ||
* | ||
* @package automattic/jetpack-publicize | ||
*/ | ||
|
||
namespace Automattic\Jetpack\Publicize\REST_API; | ||
|
||
use Automattic\Jetpack\Status\Host; | ||
use WP_Error; | ||
use WP_REST_Controller; | ||
use WP_REST_Request; | ||
use WP_REST_Response; | ||
|
||
/** | ||
* Base controller for Publicize endpoints. | ||
*/ | ||
abstract class Base_Controller extends WP_REST_Controller { | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() { | ||
$this->wpcom_is_wpcom_only_endpoint = true; | ||
} | ||
|
||
/** | ||
* Check if we are on WPCOM. | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_wpcom() { | ||
return ( new Host() )->is_wpcom_simple(); | ||
} | ||
|
||
/** | ||
* Filters out data based on ?_fields= request parameter | ||
* | ||
* @param array $item Item to prepare. | ||
* @param WP_REST_Request $request Full details about the request. | ||
* | ||
* @return WP_REST_Response filtered item | ||
*/ | ||
public function prepare_item_for_response( $item, $request ) { | ||
|
||
$fields = $this->get_fields_for_response( $request ); | ||
|
||
$response_data = array(); | ||
foreach ( $item as $field => $value ) { | ||
if ( rest_is_field_included( $field, $fields ) ) { | ||
$response_data[ $field ] = $value; | ||
} | ||
} | ||
|
||
return rest_ensure_response( $response_data ); | ||
} | ||
|
||
/** | ||
* Verify that user can access Publicize data | ||
* | ||
* @return true|WP_Error | ||
*/ | ||
public function get_items_permission_check() { | ||
global $publicize; | ||
|
||
if ( ! $publicize ) { | ||
return new WP_Error( | ||
'publicize_not_available', | ||
__( 'Sorry, Jetpack Social is not available on your site right now.', 'jetpack-publicize-pkg' ), | ||
array( 'status' => rest_authorization_required_code() ) | ||
); | ||
} | ||
|
||
if ( $publicize->current_user_can_access_publicize_data() ) { | ||
return true; | ||
} | ||
|
||
return new WP_Error( | ||
'invalid_user_permission_publicize', | ||
__( 'Sorry, you are not allowed to access Jetpack Social data on this site.', 'jetpack-publicize-pkg' ), | ||
array( 'status' => rest_authorization_required_code() ) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happier with this approach as I can see a number of the other packages doing it in the same way, but I wonder if we should restrict this to just WPCOM, similar to the Connection package
jetpack/projects/packages/connection/actions.php
Line 8 in 38efec6
It might again be something we do in a follow-up but this means we're circumventing the Config mechanisms. I'll add another comment in class-publicize-setup.php to explain what I mean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we can use that approach. We can do that in a follow up to unblock other tasks here.