| 1 |
<?php |
| 2 |
/** |
| 3 |
* Expose the global LearnPress settings that the App needs. |
| 4 |
* |
| 5 |
* Those settings live in wp_options (LP_Settings), not in the post meta of a course, |
| 6 |
* so they cannot be read from the "meta_data" of the courses endpoints. |
| 7 |
* |
| 8 |
* @package LearnPress/JWT/RESTAPI |
| 9 |
*/ |
| 10 |
|
| 11 |
class LP_Jwt_Settings_V1_Controller extends LP_REST_Jwt_Controller { |
| 12 |
protected $namespace = 'learnpress/v1'; |
| 13 |
|
| 14 |
protected $rest_base = 'settings'; |
| 15 |
|
| 16 |
public function register_routes() { |
| 17 |
register_rest_route( |
| 18 |
$this->namespace, |
| 19 |
'/' . $this->rest_base, |
| 20 |
array( |
| 21 |
array( |
| 22 |
'methods' => WP_REST_Server::READABLE, |
| 23 |
'callback' => array( $this, 'get_settings' ), |
| 24 |
'permission_callback' => '__return_true', |
| 25 |
), |
| 26 |
) |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get the global settings. |
| 32 |
* |
| 33 |
* @param WP_REST_Request $request |
| 34 |
* |
| 35 |
* @return WP_REST_Response |
| 36 |
*/ |
| 37 |
public function get_settings( WP_REST_Request $request ) { |
| 38 |
$response = new LP_REST_Response(); |
| 39 |
|
| 40 |
try { |
| 41 |
$response->data->course = (object) $this->get_course_settings(); |
| 42 |
$response->data->currency = (object) $this->get_currency_settings(); |
| 43 |
|
| 44 |
/** |
| 45 |
* Allow add-ons to append their own settings. |
| 46 |
* |
| 47 |
* @param stdClass $data |
| 48 |
* @param WP_REST_Request $request |
| 49 |
*/ |
| 50 |
$response->data = apply_filters( 'lp_jwt_rest_settings_data', $response->data, $request ); |
| 51 |
|
| 52 |
$response->status = 'success'; |
| 53 |
} catch ( Throwable $e ) { |
| 54 |
$response->message = $e->getMessage(); |
| 55 |
} |
| 56 |
|
| 57 |
return rest_ensure_response( $response ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Settings of the courses (LP Settings > Courses). |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
protected function get_course_settings(): array { |
| 66 |
$thumbnail = LP_Settings::get_option( 'course_thumbnail_dimensions', array( 500, 300, 'yes' ) ); |
| 67 |
if ( ! is_array( $thumbnail ) ) { |
| 68 |
$thumbnail = array( 500, 300, 'yes' ); |
| 69 |
} |
| 70 |
|
| 71 |
return array( |
| 72 |
// The layout of the courses archive page: "grid" or "list". |
| 73 |
'archive_layout' => LP_Settings::get_option( 'archive_courses_layout', 'list' ), |
| 74 |
'archive_layouts' => learn_press_courses_layouts(), |
| 75 |
'archive_limit' => absint( LP_Settings::get_option( 'archive_course_limit', 8 ) ), |
| 76 |
'pagination_type' => LP_Settings::get_option( 'course_pagination_type', 'standard' ), |
| 77 |
'layout_single_course' => LP_Settings::get_option( 'layout_single_course', 'classic' ), |
| 78 |
'curriculum_display' => LP_Settings::get_option( 'curriculum_display', 'expand_first_section' ), |
| 79 |
'section_per_page' => (int) LP_Settings::get_option( 'section_per_page', -1 ), |
| 80 |
'course_item_per_page' => (int) LP_Settings::get_option( 'course_item_per_page', -1 ), |
| 81 |
'auto_enroll' => 'yes' === LP_Settings::get_option( 'auto_enroll', 'yes' ), |
| 82 |
'popup_confirm_finish' => 'yes' === LP_Settings::get_option( 'enable_popup_confirm_finish', 'yes' ), |
| 83 |
'courses_of_subcategory' => 'yes' === LP_Settings::get_option( 'get_courses_of_subcategory', 'no' ), |
| 84 |
'thumbnail_dimensions' => array( |
| 85 |
'width' => absint( $thumbnail[0] ?? 500 ), |
| 86 |
'height' => absint( $thumbnail[1] ?? 300 ), |
| 87 |
'crop' => 'yes' === ( $thumbnail[2] ?? 'yes' ), |
| 88 |
), |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Settings used to format a price (LP Settings > General > Currency). |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
protected function get_currency_settings(): array { |
| 98 |
return array( |
| 99 |
'currency' => learn_press_get_currency(), |
| 100 |
'symbol' => html_entity_decode( learn_press_get_currency_symbol() ), |
| 101 |
'position' => LP_Settings::get_option( 'currency_pos', 'left' ), |
| 102 |
'thousands_separator' => LP_Settings::get_option( 'thousands_separator', ',' ), |
| 103 |
'decimals_separator' => LP_Settings::get_option( 'decimals_separator', '.' ), |
| 104 |
'number_of_decimals' => absint( LP_Settings::get_option( 'number_of_decimals', 2 ) ), |
| 105 |
); |
| 106 |
} |
| 107 |
} |
| 108 |
|