LLMS_REST_Access_Plans_Controller::update_additional_object_fields( LLMS_Access_Plan $access_plan, WP_REST_Request $request, array $schema, array $prepared_item, bool $creating = true )

Updates an existing single LLMS_Access_Plan in the database.


Description Description

This method should be used for access plan properties that require the access plan id in order to be saved in the database.


Top ↑

Parameters Parameters

$access_plan

(LLMS_Access_Plan) (Required) LLMS Access Plan instance.

$request

(WP_REST_Request) (Required) Full details about the request.

$schema

(array) (Required) The item schema.

$prepared_item

(array) (Required) Array.

$creating

(bool) (Optional) Whether we're in creation or update phase. Default true (create).

Default value: true


Top ↑

Return Return

(bool|WP_Error) True on success or false if nothing to update, WP_Error object if something went wrong during the update.


Top ↑

Source Source

File: libraries/lifterlms-rest/includes/server/class-llms-rest-access-plans-controller.php

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
    $to_set = array();
 
    // Access expiration.
    if ( ! empty( $schema['properties']['access_expiration'] ) && isset( $request['access_expiration'] ) ) {
        $to_set['access_expiration'] = $request['access_expiration'];
    }
 
    // Access expires.
    if ( ! empty( $schema['properties']['access_expires'] ) && isset( $request['access_expires'] ) ) {
        $access_expires           = rest_parse_date( $request['access_expires'] );
        $to_set['access_expires'] = empty( $access_expires ) ? '' : date_i18n( 'Y-m-d H:i:s', $access_expires );
    }
 
    // Access length.
    if ( ! empty( $schema['properties']['access_length'] ) && isset( $request['access_length'] ) ) {
        $to_set['access_length'] = $request['access_length'];
    }
 
    // Access period.
    if ( ! empty( $schema['properties']['access_period'] ) && isset( $request['access_period'] ) ) {
        $to_set['access_period'] = $request['access_period'];
    }
 
    // Redirect.
    if ( ! empty( $schema['properties']['redirect_type'] ) && isset( $request['redirect_type'] ) ) {
        $to_set['checkout_redirect_type'] = $request['redirect_type'];
    }
 
    // Redirect page.
    if ( ! empty( $schema['properties']['redirect_page'] ) && isset( $request['redirect_page'] ) ) {
        $redirect_page = get_post( $request['redirect_page'] );
        if ( $redirect_page && is_a( $redirect_page, 'WP_Post' ) ) {
            $to_set['checkout_redirect_page'] = $request['redirect_page']; // maybe allow only published pages?
        }
    }
 
    // Redirect url.
    if ( ! empty( $schema['properties']['redirect_url'] ) && isset( $request['redirect_url'] ) ) {
        $to_set['checkout_redirect_url'] = $request['redirect_url'];
    }
 
    // Price.
    if ( ! empty( $schema['properties']['price'] ) && isset( $request['price'] ) ) {
        $to_set['price'] = $request['price'];
    }
 
    // Sale enabled.
    if ( ! empty( $schema['properties']['sale_enabled'] ) && isset( $request['sale_enabled'] ) ) {
        $to_set['on_sale'] = $request['sale_enabled'] ? 'yes' : 'no';
    }
 
    // Sale dates.
    if ( ! empty( $schema['properties']['sale_date_start'] ) && isset( $request['sale_date_start'] ) ) {
        $sale_date_start      = rest_parse_date( $request['sale_date_start'] );
        $to_set['sale_start'] = empty( $sale_date_start ) ? '' : date_i18n( 'Y-m-d H:i:s', $sale_date_start );
    }
 
    if ( ! empty( $schema['properties']['sale_date_end'] ) && isset( $request['sale_date_end'] ) ) {
        $sale_date_end      = rest_parse_date( $request['sale_date_end'] );
        $to_set['sale_end'] = empty( $sale_date_end ) ? '' : date_i18n( 'Y-m-d H:i:s', $sale_date_end );
    }
    // Sale price.
    if ( ! empty( $schema['properties']['sale_price'] ) && isset( $request['sale_price'] ) ) {
        $to_set['sale_price'] = $request['sale_price'];
    }
 
    // Trial enabled.
    if ( ! empty( $schema['properties']['trial_enabled'] ) && isset( $request['trial_enabled'] ) ) {
        $to_set['trial_offer'] = $request['trial_enabled'] ? 'yes' : 'no';
    }
 
    // Trial Length.
    if ( ! empty( $schema['properties']['trial_length'] ) && isset( $request['trial_length'] ) ) {
        $to_set['trial_length'] = $request['trial_length'];
    }
    // Trial Period.
    if ( ! empty( $schema['properties']['trial_period'] ) && isset( $request['trial_period'] ) ) {
        $to_set['trial_period'] = $request['trial_period'];
    }
    // Trial price.
    if ( ! empty( $schema['properties']['trial_price'] ) && isset( $request['trial_price'] ) ) {
        $to_set['trial_price'] = $request['trial_price'];
    }
 
    // Availability restrictions.
    // If access plan related post type is not a course, set availability to 'open' and clean the `availability_restrictions` array.
    if ( 'course' !== $access_plan->get_product_type() ) {
        $to_set['availability']              = 'open';
        $to_set['availability_restrictions'] = array();
    } elseif ( ! empty( $schema['properties']['availability_restrictions'] ) && isset( $request['availability_restrictions'] ) ) {
        $to_set['availability_restrictions'] = $request['availability_restrictions'];
        // If availability restrictions supplied is not empty, set `availability` to 'members'.
        $to_set['availability'] = ! empty( $to_set['availability_restrictions'] ) ? 'members' : 'open';
    }
 
    // Redirect forced.
    if ( ! empty( $schema['properties']['redirect_forced'] ) && isset( $request['redirect_forced'] ) ) {
        $to_set['checkout_redirect_forced'] = $request['redirect_forced'] ? 'yes' : 'no';
    }
 
    // Frequency.
    if ( ! empty( $schema['properties']['frequency'] ) && isset( $request['frequency'] ) ) {
        $to_set['frequency'] = $request['frequency'];
    }
 
    // Length.
    if ( ! empty( $schema['properties']['length'] ) && isset( $request['length'] ) ) {
        $to_set['length'] = $request['length'];
    }
    // Period.
    if ( ! empty( $schema['properties']['period'] ) && isset( $request['period'] ) ) {
        $to_set['period'] = $request['period'];
    }
 
    $this->handle_props_interdependency( $to_set, $access_plan, $creating );
 
    // Visibility.
    if ( ! empty( $schema['properties']['visibility'] ) && isset( $request['visibility'] ) ) {
        $visibility = $access_plan->set_visibility( $request['visibility'] );
        if ( is_wp_error( $visibility ) ) {
            return $visibility;
        }
    }
 
    // Set bulk.
    if ( ! empty( $to_set ) ) {
        $update = $access_plan->set_bulk( $to_set, true, true );
        if ( is_wp_error( $update ) ) {
            $error = $update;
        }
    }
 
    if ( $error->errors ) {
        return $error;
    }
 
    return ! empty( $to_set ) || ! empty( $visibility );
}
 
/**
 * Handle properties interdependency
 *
 * @since 1.0.0-beta.18


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0-beta.25 Allow updating meta with the same value as the stored one.
1.0.0-beta.18
1.0.0-beta-24 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.