Skip to content

Commit

Permalink
use Jetpack\Connection\Manager to get site ID, remove unneeded code a…
Browse files Browse the repository at this point in the history
…nd is_wpcom var
  • Loading branch information
CGastrell committed Feb 23, 2023
1 parent 2738848 commit d12459e
Showing 1 changed file with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Automattic\Jetpack\Forms;

use Jetpack_Options;
use Automattic\Jetpack\Connection\Manager;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Server;
Expand All @@ -17,19 +17,10 @@
* Handles the REST routes for Form Responses, aka Feedback.
*/
class WPCOM_REST_API_V2_Endpoint_Forms extends WP_REST_Controller {
/**
* Is WPCOM or not
*
* @var bool
*/
protected $is_wpcom;

/**
* Constructor.
*/
public function __construct() {
$this->is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;

$this->namespace = 'wpcom/v2';
$this->rest_base = 'forms';

Expand All @@ -53,25 +44,25 @@ public function register_rest_routes() {
'permissions_check' => array( $this, 'get_responses_permission_check' ),
'args' => array(
'limit' => array(
'default' => 20,
'type' => 'integer',
'required' => false,
'minimum' => 1,
'default' => 20,
'type' => 'integer',
'required' => false,
'minimum' => 1,
),
'offset' => array(
'default' => 0,
'type' => 'integer',
'required' => false,
'minimum' => 0,
'default' => 0,
'type' => 'integer',
'required' => false,
'minimum' => 0,
),
'form_id' => array(
'type' => 'integer',
'required' => false,
'minimum' => 1,
'type' => 'integer',
'required' => false,
'minimum' => 1,
),
'search' => array(
'type' => 'string',
'required' => false,
'type' => 'string',
'required' => false,
),
),
)
Expand Down Expand Up @@ -154,7 +145,12 @@ function ( $response ) {
* @return true|WP_Error Returns true if the user has the required capability, else a WP_Error object.
*/
public function get_responses_permission_check() {
if ( ! is_user_member_of_blog( get_current_user_id(), $this->get_blog_id() ) ) {
$site_id = Manager::get_site_id();
if ( is_wp_error( $site_id ) ) {
return $site_id;
}

if ( ! is_user_member_of_blog( get_current_user_id(), $site_id ) ) {
return new WP_Error(
'invalid_user_permission_jetpack_form_responses',
'unauthorized',
Expand All @@ -164,13 +160,6 @@ public function get_responses_permission_check() {

return true;
}

/**
* Get blog ID selectively depending on IS_WPCOM or not
*/
protected function get_blog_id() {
return $this->is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' );
}
}

if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
Expand Down

0 comments on commit d12459e

Please sign in to comment.