-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move wpcom/v2/publicize/connections endpoint to publicize package (#4…
…0607) * Move wpcom/v2/publicize/connections endpoint to publicize package * Fix Fatals in Publicize base class on WPCOM * Now we can use v2 for proxy requests * Phan needs a fan because I am not * Update baseline.php * Use publicize for fields for now instead of a new class * Move initialization logic to Publicize_Setup * Clean up * Update phan baseline * Use null for the status field as default * Default external_handle to null * Return null in all cases for external_handle * Make get_username use get_external_handle * Rename rest-endpoints to rest-api * Clean up prepare_item_for_response * Update baseline.php * Use schema caching
- Loading branch information
1 parent
cff42cc
commit f12d3de
Showing
11 changed files
with
463 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ects/packages/publicize/changelog/update-move-wpcom-v2-publicize-connections-to-publicize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
projects/packages/publicize/src/rest-api/class-base-controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ) | ||
); | ||
} | ||
} |
Oops, something went wrong.