| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | class Meow_MGL_Rest |
| 4 | 4 | { |
| 5 | - private $core; | |
| 5 | + private $core; | |
| 6 | 6 | private $namespace = 'meow-gallery/v1'; |
| 7 | 7 | |
| 8 | 8 | public function __construct( $core ) { |
| 9 | 9 | $this->core = $core; |
| @@ -35,16 +35,16 @@ | ||
| 35 | 35 | 'callback' => array( $this, 'rest_all_settings' ) |
| 36 | 36 | ) ); |
| 37 | 37 | |
| 38 | 38 | // Gutenberg Block |
| 39 | - register_rest_route( $this->namespace, '/preview', array( | |
| 39 | + register_rest_route( $this->namespace, '/preview', array( | |
| 40 | 40 | 'methods' => 'POST', |
| 41 | 41 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 42 | 42 | 'callback' => array( $this, 'preview' ), |
| 43 | 43 | ) ); |
| 44 | - } | |
| 45 | - | |
| 46 | - function preview( WP_REST_Request $request ) { | |
| 44 | + } | |
| 45 | + | |
| 46 | + function preview( WP_REST_Request $request ) { | |
| 47 | 47 | $params = $request->get_body(); |
| 48 | 48 | $params = json_decode( $params ); |
| 49 | 49 | $params->ids = implode( ',', $params->ids ); |
| 50 | 50 | $atts = (array) $params; |
| @@ -52,21 +52,90 @@ | ||
| 52 | 52 | return new WP_REST_Response( [ 'success' => true, 'data' => $html ], 200 ); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | function rest_all_settings() { |
| 56 | - return new WP_REST_Response( [ 'success' => true, 'data' => $this->core->get_all_options() ], 200 ); | |
| 56 | + return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 ); | |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | + function create_default_googlemaps_style( $force = false ) { | |
| 60 | + $style = get_option( 'mgl_googlemaps_style', "" ); | |
| 61 | + if ( $force || empty( $style ) ) { | |
| 62 | + $style = '[]'; | |
| 63 | + update_option( 'mgl_googlemaps_style', $style ); | |
| 64 | + } | |
| 65 | + return $style; | |
| 66 | + } | |
| 67 | + | |
| 68 | + function create_default_mapbox_style( $force = false ) { | |
| 69 | + $style = get_option( 'mgl_mapbox_style', "" ); | |
| 70 | + if ( $force || empty( $style ) ) { | |
| 71 | + $style = '{"username":"", "style_id":""}'; | |
| 72 | + update_option( 'mgl_mapbox_style', $style ); | |
| 73 | + } | |
| 74 | + return $style; | |
| 75 | + } | |
| 76 | + | |
| 77 | + function get_all_options() { | |
| 78 | + $options = $this->list_options(); | |
| 79 | + $current_options = array(); | |
| 80 | + foreach ( $options as $option => $default ) { | |
| 81 | + $current_options[$option] = get_option( $option, $default ); | |
| 82 | + } | |
| 83 | + return $current_options; | |
| 84 | + } | |
| 85 | + | |
| 86 | + // List all the options with their default values. | |
| 87 | + function list_options() { | |
| 88 | + return array( | |
| 89 | + 'mgl_layout' => 'tiles', | |
| 90 | + 'mgl_captions' => 'none', | |
| 91 | + 'mgl_animation' => false, | |
| 92 | + 'mgl_image_size' => 'srcset', | |
| 93 | + 'mgl_infinite' => false, | |
| 94 | + 'mgl_tiles_gutter' => 5, | |
| 95 | + 'mgl_tiles_gutter_tablet' => 5, | |
| 96 | + 'mgl_tiles_gutter_mobile' => 5, | |
| 97 | + 'mgl_tiles_density' => 'high', | |
| 98 | + 'mgl_tiles_density_tablet' => 'medium', | |
| 99 | + 'mgl_tiles_density_mobile' => 'low', | |
| 100 | + 'mgl_masonry_gutter' => 5, | |
| 101 | + 'mgl_masonry_columns' => 3, | |
| 102 | + 'mgl_justified_gutter' => 5, | |
| 103 | + 'mgl_justified_row_height' => 200, | |
| 104 | + 'mgl_square_gutter' => 5, | |
| 105 | + 'mgl_square_columns' => 5, | |
| 106 | + 'mgl_cascade_gutter' => 5, | |
| 107 | + 'mgl_carousel_gutter' => 5, | |
| 108 | + 'mgl_carousel_image_height' => 500, | |
| 109 | + 'mgl_carousel_arrow_nav_enabled' => true, | |
| 110 | + 'mgl_carousel_dot_nav_enabled' => true, | |
| 111 | + 'mgl_map_engine' => '', | |
| 112 | + 'mgl_map_height' => 400, | |
| 113 | + 'mgl_googlemaps_token' => '', | |
| 114 | + 'mgl_googlemaps_style' => $this->create_default_googlemaps_style(), | |
| 115 | + 'mgl_mapbox_token' => '', | |
| 116 | + 'mgl_mapbox_style' => $this->create_default_mapbox_style(), | |
| 117 | + 'mgl_maptiler_token' => '', | |
| 118 | + 'mgl_right_click' => false | |
| 119 | + ); | |
| 120 | + } | |
| 121 | + | |
| 59 | 122 | function rest_update_option( $request ) { |
| 123 | + $params = $request->get_json_params(); | |
| 60 | 124 | try { |
| 61 | - $params = $request->get_json_params(); | |
| 62 | - $value = $params['options']; | |
| 63 | - $options = $this->core->update_options( $value ); | |
| 64 | - $success = !!$options; | |
| 65 | - $message = __( $success ? 'OK' : "Could not update options.", MGL_DOMAIN ); | |
| 66 | - return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $success ? $options : null ], 200 ); | |
| 67 | - } | |
| 68 | - catch ( Exception $e ) { | |
| 125 | + $name = $params['name']; | |
| 126 | + $options = $this->list_options(); | |
| 127 | + if ( !array_key_exists( $name, $options ) ) { | |
| 128 | + return new WP_REST_Response([ 'success' => false, 'message' => 'This option does not exist.' ], 200 ); | |
| 129 | + } | |
| 130 | + $value = is_bool( $params['value'] ) ? ( $params['value'] ? '1' : '' ) : $params['value']; | |
| 131 | + $success = update_option( $name, $value ); | |
| 132 | + if ( !$success ) { | |
| 133 | + return new WP_REST_Response([ 'success' => false, 'message' => 'Could not update option.' ], 200 ); | |
| 134 | + } | |
| 135 | + return new WP_REST_Response([ 'success' => true, 'data' => $value ], 200 ); | |
| 136 | + } | |
| 137 | + catch (Exception $e) { | |
| 69 | 138 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 70 | 139 | } |
| 71 | 140 | } |
| 72 | 141 | |