PluginProbe ʕ •ᴥ•ʔ
ShopPress – Shop Builder for Elementor and WooCommerce / trunk
ShopPress – Shop Builder for Elementor and WooCommerce vtrunk
shop-press / Admin / API / Options.php
shop-press / Admin / API Last commit date
Export.php 2 years ago GetFields.php 4 months ago Import.php 2 years ago Options.php 1 week ago Pages.php 2 years ago Posts.php 4 months ago Services.php 1 year ago
Options.php
110 lines
1 <?php
2 /**
3 * Options.
4 *
5 * @package ShopPress
6 */
7
8 namespace ShopPress\Admin\API;
9
10 defined( 'ABSPATH' ) || exit;
11
12 class Options {
13 /**
14 * Instance of this class.
15 *
16 * @since 1.0.0
17 */
18 public static $instance;
19
20 /**
21 * Provides access to a single instance of a module using the singleton pattern.
22 *
23 * @since 1.0.0
24 *
25 * @return object
26 */
27 public static function instance() {
28 if ( self::$instance === null ) {
29 self::$instance = new self();
30 }
31 return self::$instance;
32 }
33
34 /**
35 * Update options.
36 *
37 * @since 1.0.0
38 */
39 private function update_options( $options ) {
40
41 $options = apply_filters( 'shoppress_update_sp_admin_settings', $options );
42
43 $success = update_option( 'sp_admin', json_encode( $options ) );
44
45 update_option( 'sp_need_rewrite_rules', 'yes' );
46
47 return $success;
48 }
49
50 /**
51 * Get.
52 *
53 * @since 1.0.0
54 */
55 public function get() {
56 $sp_admin = json_decode( get_option( 'sp_admin' ), true );
57
58 // Guarantee the response always exposes the full options structure
59 // ( templates, modules, addons, general ). When the stored option is
60 // missing, partial, or corrupted - e.g. on a fresh setup where the
61 // activation hook never ran, or after an upgrade - the admin React app
62 // would otherwise receive an undefined "templates"/"modules" set and
63 // crash while rendering the Templates and Modules screens.
64 $defaults = \ShopPress\Admin\DefaultOptions\Options::get_default_options();
65
66 if ( ! is_array( $sp_admin ) ) {
67 $sp_admin = array();
68 }
69
70 foreach ( $defaults as $group_key => $group_defaults ) {
71
72 if ( ! isset( $sp_admin[ $group_key ] ) || ! is_array( $sp_admin[ $group_key ] ) ) {
73
74 $sp_admin[ $group_key ] = $group_defaults;
75 } elseif ( is_array( $group_defaults ) ) {
76
77 // Keep saved values, add any keys introduced by newer versions.
78 $sp_admin[ $group_key ] = $sp_admin[ $group_key ] + $group_defaults;
79 }
80 }
81
82 $sp_admin = apply_filters( 'shoppress_get_sp_admin_settings', $sp_admin );
83
84 return new \WP_REST_Response(
85 $sp_admin,
86 200
87 );
88 }
89
90 /**
91 * Update.
92 *
93 * @since 1.0.0
94 */
95 public function update( $request ) {
96 $options = $request->get_json_params();
97
98 if ( ! empty( $options ) || is_array( $options ) ) {
99
100 $updated = $this->update_options( $options );
101
102 if ( ! is_wp_error( $updated ) ) {
103 return new \WP_REST_Response( true, 200 );
104 }
105 }
106
107 return false;
108 }
109 }
110