Skip to content

Commit

Permalink
Merge pull request #4236 from Automattic/fix/structure-ordering-cache
Browse files Browse the repository at this point in the history
Fix structure ordering cache
  • Loading branch information
renatho authored Jul 5, 2021
2 parents 99cbb9d + c0fffe1 commit ac64e72
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions includes/class-sensei-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ public function save_lesson_order( $order_string = '', $course_id = 0 ) {
}

/**
* Get course structure.
* Get or filter course structure for lesson ordering.
*
* @param int|array $course_structure Structure array or course ID to get the structure.
* @param null|string $type Optional type to filter the content.
Expand All @@ -1591,7 +1591,7 @@ public function save_lesson_order( $order_string = '', $course_id = 0 ) {
private function get_course_structure( $course_structure = null, $type = null ) {
$course_structure = is_array( $course_structure )
? $course_structure
: Sensei_Course_Structure::instance( $course_structure )->get( 'edit' );
: Sensei_Course_Structure::instance( $course_structure )->get( 'edit', wp_using_ext_object_cache() );

if ( isset( $type ) ) {
$course_structure = array_filter(
Expand Down
28 changes: 26 additions & 2 deletions includes/class-sensei-course-structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ private function __construct( int $course_id ) {
* @see Sensei_Course_Structure::prepare_lesson()
* @see Sensei_Course_Structure::prepare_module()
*
* @param string $context Context that structure is being retrieved for. Possible values: edit, view.
* @param string $context Context that structure is being retrieved for. Possible values: edit, view.
* @param boolean $no_cache Avoid query cache.
*
* @return array {
* An array which has course structure information.
Expand All @@ -70,7 +71,11 @@ private function __construct( int $course_id ) {
* and prepare_module().
* }
*/
public function get( $context = 'view' ) {
public function get( $context = 'view', $no_cache = false ) {
if ( $no_cache ) {
add_filter( 'posts_where', [ $this, 'filter_no_cache_where' ] );
}

$context = in_array( $context, [ 'view', 'edit' ], true ) ? $context : 'view';

$structure = [];
Expand All @@ -97,9 +102,28 @@ public function get( $context = 'view' ) {
$structure[] = $this->prepare_lesson( $lesson );
}

if ( $no_cache ) {
remove_filter( 'posts_where', [ $this, 'filter_no_cache_where' ] );
}

return $structure;
}

/**
* Filter where adding an extra condition to avoid cache.
*
* @access private
*
* @param string $where Current where.
*
* @return string Where with extra condition to avoid cache.
*/
public function filter_no_cache_where( $where ) {
$time = time();

return $where . ' AND ' . $time . ' = ' . $time;
}

/**
* Prepare the result for a module.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function save_course_structure( WP_REST_Request $request ) {
}

$response = new WP_REST_Response();
$response->set_data( $course_structure->get( 'edit' ) );
$response->set_data( $course_structure->get( 'edit', wp_using_ext_object_cache() ) );

return $response;
}
Expand Down

0 comments on commit ac64e72

Please sign in to comment.