Skip to content

Commit

Permalink
Implement llms posts restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
eri-trabiccolo committed Feb 11, 2020
1 parent f7e5cd4 commit debe8b5
Show file tree
Hide file tree
Showing 8 changed files with 1,133 additions and 81 deletions.
513 changes: 479 additions & 34 deletions includes/abstracts/class-llms-rest-posts-controller.php

Large diffs are not rendered by default.

120 changes: 110 additions & 10 deletions includes/server/class-llms-rest-courses-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
* Removed `create_llms_post()` and `get_object()` methods, now abstracted in `LLMS_REST_Posts_Controller` class.
* `llms_rest_course_filters_removed_for_response` filter hook added.
* Added `llms_rest_course_item_schema`, `llms_rest_pre_insert_course`, `llms_rest_prepare_course_object_response`, `llms_rest_course_links` filter hooks.
* @since [version] Added `page_restricted()` and `maybe_restrict_object_data()` methods override.
* Properly define and handle `audio_embed` and `video_embed` properties thar are now composed of sub-properties.
*/
class LLMS_REST_Courses_Controller extends LLMS_REST_Posts_Controller {

Expand Down Expand Up @@ -164,6 +166,8 @@ protected function get_object_id( $object ) {
* Add missing quotes in enrollment/access default messages shortcodes.
* @since 1.0.0-beta.9 Make sure instructors list is either not empty and composed by real user ids.
* Added `llms_rest_course_item_schema` filter hook.
* @since [version] `audio_embed` and `video_embed` properties are now composed of sub-properties: `raw`, `rendered`, `restricted`.
*
* @return array
*/
public function get_item_schema() {
Expand Down Expand Up @@ -224,20 +228,63 @@ public function get_item_schema() {
),
'audio_embed' => array(
'description' => __( 'URL to an oEmbed enable audio URL.', 'lifterlms' ),
'type' => 'string',
'type' => 'object',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'arg_options' => array(
'sanitize_callback' => 'esc_url_raw',
'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
),
'properties' => array(
'raw' => array(
'description' => __( 'Raw URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
'format' => 'uri',
),
'rendered' => array(
'description' => __( 'Rendered URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'readonly' => true,
),
'restricted' => array(
'description' => __( 'Whether the audio embed URL is restricted to the current user.', 'lifterlms' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'video_embed' => array(
'description' => __( 'URL to an oEmbed enable video URL.', 'lifterlms' ),
'type' => 'string',
'type' => 'object',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'arg_options' => array(
'sanitize_callback' => 'esc_url_raw',
'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
),
'properties' => array(
'raw' => array(
'description' => __( 'Raw URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
'format' => 'uri',
),
'rendered' => array(
'description' => __( 'Rendered URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'readonly' => true,
),
'restricted' => array(
'description' => __( 'Whether the video embed URL is restricted to the current user.', 'lifterlms' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'capacity_enabled' => array(
Expand Down Expand Up @@ -314,7 +361,7 @@ public function get_item_schema() {
),
),
'restricted_message' => array(
'description' => __( 'Message displayed when non-enrolled visitors try to access restricted course content (lessons, quizzes, etc..) directly.', 'lifterlms' ),
'description' => __( 'Message displayed when non-enrolled visitors try to access restricted course content (lessons, quizzes, etc...) directly.', 'lifterlms' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
Expand Down Expand Up @@ -536,6 +583,7 @@ public function get_item_schema() {
* Always return `sales_page_url` and `sales_page_page_id` when in `edit` context.
* @since 1.0.0-beta.9 Fixed `sales_page_url` not returned in `edit` context.
* Added `llms_rest_prepare_course_object_response` filter hook.
* @since [version] Properly handle `audio_embed` and `video_embed` properties thar are now composed of sub-properties.
*
* @param LLMS_Course $course Course object.
* @param WP_REST_Request $request Full details about the request.
Expand Down Expand Up @@ -580,10 +628,14 @@ protected function prepare_object_for_response( $course, $request ) {
$data['instructors'] = $instructors;

// Audio Embed.
$data['audio_embed'] = $course->get( 'audio_embed' );
$data['audio_embed']['raw'] = $course->get( 'audio_embed' );
$data['audio_embed']['rendered'] = $data['audio_embed']['raw'];
$data['audio_embed']['restricted'] = false; // default.

// Video Embed.
$data['video_embed'] = $course->get( 'video_embed' );
$data['video_embed']['raw'] = $course->get( 'video_embed' );
$data['video_embed']['rendered'] = $data['video_embed']['raw'];
$data['video_embed']['restricted'] = false; // default.

// Video tile.
$data['video_tile'] = 'yes' === $course->get( 'tile_featured_video' );
Expand Down Expand Up @@ -682,6 +734,7 @@ protected function prepare_object_for_response( $course, $request ) {
* Made `access_opens_date`, `access_closes_date`, `enrollment_opens_date`, `enrollment_closes_date` nullable.
* @since 1.0.0-beta.8 Renamed `sales_page_page_type` and `sales_page_page_url` properties, respectively to `sales_page_type` and `sales_page_url` according to the specs.
* @since 1.0.0-beta.9 Added `llms_rest_pre_insert_course` filter hook.
* @since [version] Properly handle `audio_embed` and `video_embed` properties thar are now composed of sub-properties.
*
* @param WP_REST_Request $request Request object.
* @return array|WP_Error Array of llms post args or WP_Error.
Expand All @@ -693,12 +746,20 @@ protected function prepare_item_for_database( $request ) {

// Course Audio embed URL.
if ( ! empty( $schema['properties']['audio_embed'] ) && isset( $request['audio_embed'] ) ) {
$prepared_item['audio_embed'] = $request['audio_embed'];
if ( is_string( $request['audio_embed'] ) ) {
$prepared_item['audio_embed'] = esc_url_raw( $request['audio_embed'] );
} elseif ( isset( $request['audio_embed']['raw'] ) ) {
$prepared_item['audio_embed'] = esc_url_raw( $request['audio_embed']['raw'] );
}
}

// Course Video embed URL.
if ( ! empty( $schema['properties']['video_embed'] ) && isset( $request['video_embed'] ) ) {
$prepared_item['video_embed'] = $request['video_embed'];
if ( is_string( $request['video_embed'] ) ) {
$prepared_item['video_embed'] = esc_url_raw( $request['video_embed'] );
} elseif ( isset( $request['video_embed']['raw'] ) ) {
$prepared_item['video_embed'] = esc_url_raw( $request['video_embed']['raw'] );
}
}

// Video tile.
Expand Down Expand Up @@ -1241,4 +1302,43 @@ public function get_course_content_items( $request ) {

}

/**
* Determine if course content should be restricted for the current user.
*
* @since [version]
*
* @param LLMS_Course $course LLMS_Course instance.
* @return array Restriction check result data.
*/
protected function page_restricted( $course ) {

if ( 'none' === $course->get( 'sales_page_content_type' ) ) {
return array();
}

return llms_page_restricted( $course->get( 'id' ) );

}

/**
* Maybe apply restrictions on object's data properties.
*
* @since [version]
*
* @param array $data Array of object data.
* @param LLMS_Course $course LLMS_Course instance.
* @return array Array of object data.
*/
protected function maybe_restrict_object_data( $data, $course ) {

$data = parent::maybe_restrict_object_data( $data, $course );

// when displaying custom content audio and video embed are not restricted.
if ( 'content' === $course->get( 'sales_page_content_type' ) ) {
$data['audio_embed']['restricted'] = false;
$data['video_embed']['restricted'] = false;
}

return $data;
}
}
81 changes: 70 additions & 11 deletions includes/server/class-llms-rest-lessons-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* so to instruct it to return a WP_Error on failure.
* @since 1.0.0-beta.9 Removed `create_llms_post()` and `get_object()` methods, now abstracted in `LLMS_REST_Posts_Controller` class.
* `llms_rest_lesson_filters_removed_for_response` filter hook added.
* @since [version] Properly define and handle `audio_embed` and `video_embed` properties thar are now composed of sub-properties.
*/
class LLMS_REST_Lessons_Controller extends LLMS_REST_Posts_Controller {

Expand Down Expand Up @@ -103,6 +104,7 @@ public function get_parent_id() {
* Prepares a single lesson for create or update.
*
* @since 1.0.0-beta.7
* @since [version] Properly handle `audio_embed` and `video_embed` properties thar are now composed of sub-properties.
*
* @param WP_REST_Request $request Request object.
* @return array|WP_Error Array of lesson args or WP_Error.
Expand All @@ -114,12 +116,20 @@ protected function prepare_item_for_database( $request ) {

// Lesson's audio embed URL.
if ( ! empty( $schema['properties']['audio_embed'] ) && isset( $request['audio_embed'] ) ) {
$prepared_item['audio_embed'] = $request['audio_embed'];
if ( is_string( $request['audio_embed'] ) ) {
$prepared_item['audio_embed'] = esc_url_raw( $request['audio_embed'] );
} elseif ( isset( $request['audio_embed']['raw'] ) ) {
$prepared_item['audio_embed'] = esc_url_raw( $request['audio_embed']['raw'] );
}
}

// Lesson's video embed URL.
if ( ! empty( $schema['properties']['video_embed'] ) && isset( $request['video_embed'] ) ) {
$prepared_item['video_embed'] = $request['video_embed'];
if ( is_string( $request['video_embed'] ) ) {
$prepared_item['video_embed'] = esc_url_raw( $request['video_embed'] );
} elseif ( isset( $request['video_embed']['raw'] ) ) {
$prepared_item['video_embed'] = esc_url_raw( $request['video_embed']['raw'] );
}
}

// Parent (section) id.
Expand Down Expand Up @@ -288,6 +298,7 @@ protected function update_additional_object_fields( $lesson, $request, $schema,
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 Added the following properties: drip_date, drip_days, drip_method, public, quiz.
* Added `llms_rest_lesson_item_schema` filter hook.
* @since [version] `audio_embed` and `video_embed` properties are now composed of sub-properties: `raw`, `rendered`, `restricted`.
*
* @return array Item schema data.
*/
Expand Down Expand Up @@ -339,22 +350,65 @@ public function get_item_schema() {
'sanitize_callback' => 'absint',
),
),
'audio_embed' => array(
'audio_embed' => array(
'description' => __( 'URL to an oEmbed enable audio URL.', 'lifterlms' ),
'type' => 'string',
'type' => 'object',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'arg_options' => array(
'sanitize_callback' => 'esc_url_raw',
'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
),
'properties' => array(
'raw' => array(
'description' => __( 'Raw URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
'format' => 'uri',
),
'rendered' => array(
'description' => __( 'Rendered URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'readonly' => true,
),
'restricted' => array(
'description' => __( 'Whether the audio embed URL is restricted to the current user.', 'lifterlms' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'video_embed' => array(
'video_embed' => array(
'description' => __( 'URL to an oEmbed enable video URL.', 'lifterlms' ),
'type' => 'string',
'type' => 'object',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'arg_options' => array(
'sanitize_callback' => 'esc_url_raw',
'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
),
'properties' => array(
'raw' => array(
'description' => __( 'Raw URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'edit' ),
'format' => 'uri',
),
'rendered' => array(
'description' => __( 'Rendered URL.', 'lifterlms' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'format' => 'uri',
'readonly' => true,
),
'restricted' => array(
'description' => __( 'Whether the video embed URL is restricted to the current user.', 'lifterlms' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'drip_date' => array(
Expand Down Expand Up @@ -503,6 +557,7 @@ public function build_collection_params() {
* @since 1.0.0-beta.7 Added following properties to the response object:
* public, points, quiz, drip_method, drip_days, drip_date, prerequisite, audio_embed, video_embed.
* Added `llms_rest_prepare_lesson_object_response` filter hook.
* @since [version] `audio_embed` and `video_embed` properties are now composed of sub-properties that need to be properly set.
*
* @param LLMS_Lesson $lesson Lesson object.
* @param WP_REST_Request $request Full details about the request.
Expand All @@ -513,10 +568,14 @@ protected function prepare_object_for_response( $lesson, $request ) {
$data = parent::prepare_object_for_response( $lesson, $request );

// Audio Embed.
$data['audio_embed'] = $lesson->get( 'audio_embed' );
$data['audio_embed']['raw'] = $lesson->get( 'audio_embed' );
$data['audio_embed']['rendered'] = $data['audio_embed']['raw'];
$data['audio_embed']['restricted'] = false; // default.

// Video Embed.
$data['video_embed'] = $lesson->get( 'video_embed' );
$data['video_embed']['raw'] = $lesson->get( 'video_embed' );
$data['video_embed']['rendered'] = $data['video_embed']['raw'];
$data['video_embed']['restricted'] = false; // default.

// Parent section.
$data['parent_id'] = $lesson->get_parent_section();
Expand Down
23 changes: 21 additions & 2 deletions includes/server/class-llms-rest-memberships-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* @package LifterLMS_REST/Classes/Controllers
*
* @since 1.0.0-beta.9
* @version 1.0.0-beta.9
* @since 1.0.0-beta.9
* @version [version]
*/

defined( 'ABSPATH' ) || exit;
Expand All @@ -14,6 +14,7 @@
* LLMS_REST_Memberships_Controller class.
*
* @since 1.0.0-beta.9
* @since [version] Added `page_restricted()` method override.
*/
class LLMS_REST_Memberships_Controller extends LLMS_REST_Posts_Controller {
/**
Expand Down Expand Up @@ -666,4 +667,22 @@ protected function update_additional_object_fields( $membership, $request, $sche

return ! empty( $to_set );
}

/**
* Determine if membership content should be restricted for the current user.
*
* @since [version]
*
* @param LLMS_Membership $membership LLMS_Membership instance.
* @return array Restriction check result data.
*/
protected function page_restricted( $membership ) {

if ( 'none' === $membership->get( 'sales_page_content_type' ) ) {
return array();
}

return llms_page_restricted( $membership->get( 'id' ) );

}
}
Loading

0 comments on commit debe8b5

Please sign in to comment.