PluginProbe
Customify / 2.0.2
Customify v2.0.2
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / includes / lib / class-customify-design-assets.php

class-customify-design-assets.php in Customify 2.0.2, at includes/lib/class-customify-design-assets.php

241 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This is the class that handles the overall logic for design assets.
4 *
5 * @see https://pixelgrade.com
6 * @author Pixelgrade
7 * @since 1.7.4
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly
12 }
13
14 if ( ! class_exists( 'Customify_Design_Assets' ) ) :
15
16 class Customify_Design_Assets {
17
18 /**
19 * Holds the only instance of this class.
20 * @var null|Customify_Design_Assets
21 * @access protected
22 * @since 1.7.4
23 */
24 protected static $_instance = null;
25
26 /**
27 * The current design assets config.
28 * @var array
29 * @access public
30 * @since 1.7.4
31 */
32 protected $design_assets = null;
33
34 /**
35 * The cloud API object used to communicate with the cloud.
36 * @var Customify_Cloud_Api
37 * @access public
38 * @since 1.7.4
39 */
40 protected $cloud_api = null;
41
42 /**
43 * Constructor.
44 *
45 * @since 1.7.4
46 */
47 private function __construct() {
48 $this->init();
49 }
50
51 /**
52 * Initialize this module.
53 *
54 * @since 1.7.4
55 */
56 public function init() {
57 /**
58 * Initialize the Cloud API logic.
59 */
60 require_once 'class-customify-cloud-api.php';
61 $this->cloud_api = new Customify_Cloud_Api();
62 }
63
64 /**
65 * Get the design assets configuration.
66 *
67 * @since 1.7.4
68 *
69 * @param bool $skip_cache Optional. Whether to use the cached config or fetch a new one.
70 *
71 * @return array
72 */
73 public function get( $skip_cache = false ) {
74 if ( ! is_null( $this->design_assets ) && false === $skip_cache ) {
75 return $this->design_assets;
76 }
77
78 $this->design_assets = $this->maybe_fetch( $skip_cache );
79
80 // Determine if we should use the config in the theme root and skip the external config entirely.
81 if ( defined('CUSTOMIFY_SM_LOAD_THEME_ROOT_CONFIG') && true === CUSTOMIFY_SM_LOAD_THEME_ROOT_CONFIG ) {
82 $this->design_assets = $this->maybe_load_theme_config_from_theme_root( $this->design_assets );
83 }
84
85 return apply_filters( 'customify_style_manager_get_design_assets', $this->design_assets );
86 }
87
88 /**
89 * Fetch the design assets data from the Pixelgrade Cloud.
90 *
91 * Caches the data for 12 hours. Use local defaults if not available.
92 *
93 * @since 1.7.4
94 *
95 * @param bool $skip_cache Optional. Whether to use the cached data or fetch a new one.
96 *
97 * @return array|false
98 */
99 protected function maybe_fetch( $skip_cache = false ) {
100 // First try and get the cached data
101 $data = get_option( $this->_get_cache_key() );
102
103 // For performance reasons, we will ONLY fetch remotely when in the WP ADMIN area or via an ADMIN AJAX call, regardless of settings.
104 if ( ! is_admin() ) {
105 return $data;
106 }
107
108 // Get the cache data expiration timestamp.
109 $expire_timestamp = get_option( $this->_get_cache_key() . '_timestamp' );
110
111 // We don't force skip the cache for AJAX requests for performance reasons.
112 if ( ! wp_doing_ajax() && defined('CUSTOMIFY_SM_ALWAYS_FETCH_DESIGN_ASSETS' ) && true === CUSTOMIFY_SM_ALWAYS_FETCH_DESIGN_ASSETS ) {
113 $skip_cache = true;
114 }
115
116 // The data isn't set, is expired or we were instructed to skip the cache; we need to fetch fresh data.
117 if ( true === $skip_cache || false === $data || false === $expire_timestamp || $expire_timestamp < time() ) {
118 // Fetch the design assets from the cloud.
119 $fetched_data = $this->cloud_api->fetch_design_assets();
120 // Bail in case of failure to retrieve data.
121 // We will return the data already available.
122 if ( false === $fetched_data ) {
123 return $data;
124 }
125
126 $data = $fetched_data;
127
128 // Cache the data in an option for 6 hours
129 update_option( $this->_get_cache_key() , $data, true );
130 update_option( $this->_get_cache_key() . '_timestamp' , time() + 6 * HOUR_IN_SECONDS, true );
131 }
132
133 return apply_filters( 'customify_style_manager_maybe_fetch_design_assets', $data );
134 }
135
136 /**
137 * Get the design assets cache key.
138 *
139 * @since 1.7.4
140 *
141 * @return string
142 */
143 private function _get_cache_key() {
144 return 'customify_style_manager_design_assets';
145 }
146
147 /**
148 * Include the customify "external" config file in the theme root and overwrite the existing theme configs.
149 *
150 * @since 1.7.4
151 *
152 * @param array $design_assets
153 *
154 * @return array
155 */
156 protected function maybe_load_theme_config_from_theme_root( $design_assets ) {
157 $file_name = 'customify_theme_root.php';
158
159 // First gather details about the current (parent) theme.
160 $theme = wp_get_theme( get_template() );
161 // Bail if for some strange reason we couldn't find the theme.
162 if ( ! $theme->exists() ) {
163 return $design_assets;
164 }
165
166 $file = trailingslashit( $theme->get_template_directory() ) . $file_name;
167 if ( ! file_exists( $file ) ) {
168 return $design_assets;
169 }
170
171 // We expect to get from the file include a $config variable with the entire Customify (partial) config.
172 include $file;
173
174 if ( ! isset( $config ) || ! is_array( $config ) || empty( $config['sections'] ) ) {
175 // Alert the developers that things are not alright.
176 _doing_it_wrong( __METHOD__, 'The Customify theme root config is not good - the `sections` entry is missing. Please check it! We will not apply it.', null );
177
178 return $design_assets;
179 }
180
181 // Construct the pseudo-external theme config.
182 // Start with a clean slate.
183 $design_assets['theme_configs'] = array();
184
185 $design_assets['theme_configs']['theme_root'] = array(
186 'id' => 1,
187 'name' => $theme->get( 'Name' ),
188 'slug' => $theme->get_stylesheet(),
189 'txtd' => $theme->get( 'TextDomain' ),
190 'loose_match' => true,
191 'config' => $config,
192 'created' => date('Y-m-d H:i:s'),
193 'last_modified' => date('Y-m-d H:i:s'),
194 'hashid' => 'theme_root',
195 );
196
197 return $design_assets;
198 }
199
200 /**
201 * Main Customify_Design_Assets Instance
202 *
203 * Ensures only one instance of Customify_Design_Assets is loaded or can be loaded.
204 *
205 * @since 1.7.4
206 * @static
207 *
208 * @return Customify_Design_Assets Main Customify_Design_Assets instance
209 */
210 public static function instance() {
211
212 if ( is_null( self::$_instance ) ) {
213 self::$_instance = new self();
214 }
215
216 return self::$_instance;
217 } // End instance ()
218
219 /**
220 * Cloning is forbidden.
221 *
222 * @since 1.7.4
223 */
224 public function __clone() {
225
226 _doing_it_wrong( __FUNCTION__,esc_html( __( 'Cheatin&#8217; huh?' ) ), null );
227 } // End __clone ()
228
229 /**
230 * Unserializing instances of this class is forbidden.
231 *
232 * @since 1.7.4
233 */
234 public function __wakeup() {
235
236 _doing_it_wrong( __FUNCTION__, esc_html( __( 'Cheatin&#8217; huh?' ) ), null );
237 } // End __wakeup ()
238 }
239
240 endif;
241