PluginProbe
Customify / 2.5.3
Customify v2.5.3
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.5.3, at includes/lib/class-customify-design-assets.php

259 lines 7.2 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( self::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 // We don't force skip the cache for AJAX requests for performance reasons.
109 if ( ! wp_doing_ajax() && defined('CUSTOMIFY_SM_ALWAYS_FETCH_DESIGN_ASSETS' ) && true === CUSTOMIFY_SM_ALWAYS_FETCH_DESIGN_ASSETS ) {
110 $skip_cache = true;
111 }
112
113 $expire_timestamp = false;
114
115 // Only try to get the expire timestamp if we really need to.
116 if ( true !== $skip_cache && false !== $data ) {
117 // Get the cache data expiration timestamp.
118 $expire_timestamp = get_option( self::get_cache_key() . '_timestamp' );
119 }
120
121 // The data isn't set, is expired or we were instructed to skip the cache; we need to fetch fresh data.
122 if ( true === $skip_cache || false === $data || false === $expire_timestamp || $expire_timestamp < time() ) {
123 // Fetch the design assets from the cloud.
124 $fetched_data = $this->cloud_api->fetch_design_assets();
125 // Bail in case of failure to retrieve data.
126 // We will return the data already available.
127 if ( false === $fetched_data ) {
128 return $data;
129 }
130
131 $data = $fetched_data;
132
133 if ( true !== $skip_cache ) {
134 // Cache the data in an option for 6 hours
135 update_option( self::get_cache_key(), $data, true );
136 update_option( self::get_cache_key() . '_timestamp', time() + 6 * HOUR_IN_SECONDS, true );
137 }
138 }
139
140 return apply_filters( 'customify_style_manager_maybe_fetch_design_assets', $data );
141 }
142
143 /**
144 * Get the design assets cache key.
145 *
146 * @since 1.7.4
147 *
148 * @return string
149 */
150 private static function get_cache_key() {
151 return 'customify_style_manager_design_assets';
152 }
153
154 public static function invalidate_cache() {
155 update_option( self::get_cache_key() . '_timestamp' , time() - 24 * HOUR_IN_SECONDS, true );
156 }
157
158 /**
159 * Include the customify "external" config file in the theme root and overwrite the existing theme configs.
160 *
161 * @since 1.7.4
162 *
163 * @param array $design_assets
164 *
165 * @return array
166 */
167 protected function maybe_load_theme_config_from_theme_root( $design_assets ) {
168 $file_name = 'customify_theme_root.php';
169
170 // First gather details about the current (parent) theme.
171 $theme = wp_get_theme( get_template() );
172 // Bail if for some strange reason we couldn't find the theme.
173 if ( ! $theme->exists() ) {
174 return $design_assets;
175 }
176
177 $file = trailingslashit( $theme->get_template_directory() ) . $file_name;
178 if ( ! file_exists( $file ) ) {
179 return $design_assets;
180 }
181
182 // We expect to get from the file include a $config variable with the entire Customify (partial) config.
183 include $file;
184
185 if ( ! isset( $config ) || ! is_array( $config ) || empty( $config['sections'] ) ) {
186 // Alert the developers that things are not alright.
187 _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 );
188
189 return $design_assets;
190 }
191
192 // Construct the pseudo-external theme config.
193 // Start with a clean slate.
194 $design_assets['theme_configs'] = array();
195
196 $design_assets['theme_configs']['theme_root'] = array(
197 'id' => 1,
198 'name' => $theme->get( 'Name' ),
199 'slug' => $theme->get_stylesheet(),
200 'txtd' => $theme->get( 'TextDomain' ),
201 'loose_match' => true,
202 'config' => $config,
203 'created' => date('Y-m-d H:i:s'),
204 'last_modified' => date('Y-m-d H:i:s'),
205 'hashid' => 'theme_root',
206 );
207
208 return $design_assets;
209 }
210
211 /**
212 * Main Customify_Design_Assets Instance
213 *
214 * Ensures only one instance of Customify_Design_Assets is loaded or can be loaded.
215 *
216 * @since 1.7.4
217 * @static
218 *
219 * @return Customify_Design_Assets Main Customify_Design_Assets instance
220 */
221 public static function instance() {
222
223 if ( is_null( self::$_instance ) ) {
224 self::$_instance = new self();
225 }
226
227 return self::$_instance;
228 } // End instance ()
229
230 /**
231 * Cloning is forbidden.
232 *
233 * @since 1.7.4
234 */
235 public function __clone() {
236
237 _doing_it_wrong( __FUNCTION__,esc_html__( 'You should not do that!', 'customify' ), null );
238 }
239
240 /**
241 * Unserializing instances of this class is forbidden.
242 *
243 * @since 1.7.4
244 */
245 public function __wakeup() {
246
247 _doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null );
248 }
249 }
250
251 // Handle various cache invalidations, in a proactive manner
252 // We need to do the hooking here becuse by the time the class might be instantiated it might be too late.
253 add_action( 'activated_plugin', array( 'Customify_Design_Assets', 'invalidate_cache' ), 1 );
254 add_action( 'deactivated_plugin', array( 'Customify_Design_Assets', 'invalidate_cache' ), 1 );
255 add_action( 'after_switch_theme', array( 'Customify_Design_Assets', 'invalidate_cache' ), 1 );
256 add_action( 'upgrader_process_complete', array( 'Customify_Design_Assets', 'invalidate_cache' ), 1 );
257
258 }
259