Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions inc/classes/everest-forms/class-everest-forms-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_forms' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => $this->get_collection_params(),
),
),
Expand All @@ -49,7 +49,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_form' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => array_merge(
$this->get_collection_params(), // Default collection params.
array(
Expand Down Expand Up @@ -117,9 +117,8 @@ public function get_forms( $request ) {
$formatted_forms = array_map(
function ( $form ) {
return array(
'id' => $form->ID,
'title' => $form->post_title,
'description' => $form->post_excerpt,
'id' => $form->ID,
'title' => $form->post_title,
);
},
$forms
Expand Down
4 changes: 2 additions & 2 deletions inc/classes/ninja-forms/class-ninja-forms-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_ninja_forms' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => $this->get_collection_params(),
),
),
Expand All @@ -50,7 +50,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_ninja_form' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => array_merge(
$this->get_collection_params(), // Default collection params.
array(
Expand Down
13 changes: 13 additions & 0 deletions inc/classes/rest-api/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ public function register_rest_routes() {
*/
abstract public function get_rest_routes();

/**
* Permission check for routes only the Media Editor calls.
*
* The capability the Media Editor page itself requires.
*
* @since 2.5.1
*
* @return bool
*/
public function forms_permissions_check() {
return current_user_can( 'upload_files' );
}

/**
* Sets up the proper HTTP status code for authorization.
*
Expand Down
2 changes: 1 addition & 1 deletion inc/classes/rest-api/class-cf7.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_cf7_form' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => array_merge(
$this->get_collection_params(), // Default collection params.
array(
Expand Down
4 changes: 2 additions & 2 deletions inc/classes/rest-api/class-fluent-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_fluent_forms' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => $this->get_collection_params(),
),
),
Expand All @@ -40,7 +40,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_fluent_form' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => array_merge(
$this->get_collection_params(), // Default collection params.
array(
Expand Down
10 changes: 4 additions & 6 deletions inc/classes/rest-api/class-forminator-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function get_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_forminator_forms' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => $this->get_collection_params(),
),
),
Expand All @@ -74,7 +74,7 @@ public function get_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_forminator_form' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => array_merge(
$this->get_collection_params(), // Default collection params.
array(
Expand Down Expand Up @@ -126,10 +126,8 @@ public function get_forminator_forms( WP_REST_Request $request ) {
foreach ( $forms as $form ) {
// Prepare the form data.
$all_forms[] = array(
'id' => $form->id,
'name' => $form->name,
'client_id' => $form->client_id,
'status' => $form->status,
'id' => $form->id,
'name' => $form->name,
);
}
++$paged;
Expand Down
9 changes: 0 additions & 9 deletions inc/classes/rest-api/class-gf.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ class GF extends Base {
*/
public const ALLOWED_FORM_FIELDS = array( 'id', 'title', 'description' );

/**
* Permission check — same capability the Video Editor page requires.
*
* @return bool
*/
public function forms_permissions_check() {
return current_user_can( 'upload_files' );
}

/**
* Get REST routes.
*/
Expand Down
26 changes: 22 additions & 4 deletions inc/classes/rest-api/class-jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_jetpack_form' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => array_merge(
$this->get_collection_params(),
array(
Expand Down Expand Up @@ -302,7 +302,10 @@ public function get_jetpack_form( $request ) {

// Get the post content.
$post = get_post( $post_id ); // godam-coverage-ignore -- get_jetpack_form(): $post_id is the host post embedding the Jetpack form block, not an attachment ID.
if ( ! $post ) {

// A post visitors cannot see is previewed only for a user who can edit it.
$can_preview = $post && ( self::is_host_post_public( $post ) || current_user_can( 'edit_post', $post_id ) ); // godam-coverage-ignore -- get_jetpack_form(): $post_id is the host post embedding the Jetpack form block, not an attachment ID.
if ( ! $can_preview ) {
return new \WP_Error( 'post_not_found', __( 'Post not found.', 'godam' ), array( 'status' => 404 ) );
}

Expand Down Expand Up @@ -409,6 +412,21 @@ private function render_jetpack_form_directly( $form_block, $post_id ) {
}
}

/**
* Whether a post that holds a Jetpack form is one visitors can see.
*
* Forms are shown in a video, and submitted from it, only when the post that
* holds them is publicly viewable and has no password.
*
* @since 2.5.1
*
* @param \WP_Post|null $post The post that holds the form.
* @return bool
*/
public static function is_host_post_public( $post ) {
return $post instanceof \WP_Post && is_post_publicly_viewable( $post ) && empty( $post->post_password );
}

/**
* Static helper method to get rendered form HTML (for use in templates).
*
Expand All @@ -432,7 +450,7 @@ public static function get_rendered_form_html_static( $form_id ) {

// Get the post content.
$post = get_post( $post_id ); // godam-coverage-ignore -- get_rendered_form_html_static(): $post_id is the host post embedding the Jetpack form block, not an attachment ID.
if ( ! $post ) {
if ( ! $post || ! self::is_host_post_public( $post ) ) {
return false;
}

Expand Down Expand Up @@ -766,7 +784,7 @@ private function map_field_names( $frontend_fields, $original_form ) {
private function load_form_into_memory_with_origin( $form_id, $form_hash, $origin_post_id ) {
// Get the origin post.
$origin_post = get_post( $origin_post_id ); // godam-coverage-ignore -- load_form_into_memory_with_origin(): $origin_post_id is the host post that originated the form submission (REST "origin-post-id" param or the same form-ID parsing as get_jetpack_form), not an attachment ID.
if ( ! $origin_post ) {
if ( ! $origin_post || ! self::is_host_post_public( $origin_post ) ) {
return false;
}

Expand Down
10 changes: 4 additions & 6 deletions inc/classes/rest-api/class-sureforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function get_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_sureforms' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => $this->get_collection_params(),
),
),
Expand All @@ -73,7 +73,7 @@ public function get_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_sureform' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => array_merge(
$this->get_collection_params(), // Default collection params.
array(
Expand Down Expand Up @@ -134,10 +134,8 @@ public function get_sureforms( $request ) {

foreach ( $forms as $form ) {
$sure_forms[] = array(
'id' => $form->ID,
'title' => $form->post_title,
'description' => $form->post_excerpt,
'content' => $form->post_content,
'id' => $form->ID,
'title' => $form->post_title,
);
}

Expand Down
9 changes: 4 additions & 5 deletions inc/classes/rest-api/class-wpforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_gforms' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => $this->get_collection_params(),
),
),
Expand All @@ -42,7 +42,7 @@ public function get_rest_routes() {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_wpforms_form' ),
'permission_callback' => '__return_true',
'permission_callback' => array( $this, 'forms_permissions_check' ),
'args' => array_merge(
$this->get_collection_params(), // Default collection params.
array(
Expand Down Expand Up @@ -108,9 +108,8 @@ public function get_gforms( $request ) {
if ( ! empty( $forms ) && ! is_wp_error( $forms ) ) {
foreach ( $forms as $form ) {
$wpforms[] = array(
'id' => $form->ID,
'title' => $form->post_title,
'description' => $form->post_excerpt,
'id' => $form->ID,
'title' => $form->post_title,
);
}
}
Expand Down
22 changes: 19 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,22 @@ function wp_parse_url( $url, $component = -1 ) {
}
}

// Empty stand-in for the WP core base so Video_Editor's parent chain resolves
// Stand-in for the WP core base so Video_Editor's parent chain resolves
// without a WordPress install; the constructor is never run (tests build the
// object with ReflectionClass::newInstanceWithoutConstructor()).
// object with ReflectionClass::newInstanceWithoutConstructor()). The route
// tables call get_collection_params() for their `args`; the params themselves
// are not what those tests look at.
if ( ! class_exists( 'WP_REST_Controller' ) ) {
class WP_REST_Controller {} // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
// phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed, Generic.Files.OneObjectStructurePerFile.MultipleFound -- test stub.
class WP_REST_Controller {

/**
* @return array
*/
public function get_collection_params() {
return array();
}
}
}

/*
Expand Down Expand Up @@ -295,6 +306,11 @@ public function get_data() {
public function get_status() {
return $this->status;
}

/** @param int $status HTTP status code. */
public function set_status( $status ) {
$this->status = $status;
}
}
}

Expand Down
Loading
Loading