PluginProbe
WP Ultimate Post Grid / 4.1.1
WP Ultimate Post Grid v4.1.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / includes / public / class-wpupg-template-manager.php

class-wpupg-template-manager.php in WP Ultimate Post Grid 4.1.1, at includes/public/class-wpupg-template-manager.php

359 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Responsible for the grid template.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 3.0.0
7 *
8 * @package WP_Ultimate_Post_Grid
9 * @subpackage WP_Ultimate_Post_Grid/includes/public
10 */
11
12 /**
13 * Responsible for the grid template.
14 *
15 * @since 3.0.0
16 * @package WP_Ultimate_Post_Grid
17 * @subpackage WP_Ultimate_Post_Grid/includes/public
18 * @author Brecht Vandersmissen <brecht@bootstrapped.ventures>
19 */
20 class WPUPG_Template_Manager {
21 /**
22 * Cached version of all the available templates.
23 *
24 * @since 3.0.0
25 * @access private
26 * @var array $templates Array containing all templates that have been loaded.
27 */
28 private static $templates = array();
29
30 /**
31 * Templates used in the output.
32 *
33 * @since 3.0.0
34 * @access private
35 * @var array $used_templates Array containing all templates that have been used in the output.
36 */
37 private static $used_templates = array();
38
39 /**
40 * Register actions and filters.
41 *
42 * @since 3.0.0
43 */
44 public static function init() {
45 add_action( 'wp_footer', array( __CLASS__, 'templates_css' ), 99 );
46 }
47
48 /**
49 * Add CSS to footer for all grids on this page.
50 *
51 * @since 3.0.0
52 */
53 public static function templates_css() {
54 if ( count( self::$used_templates ) ) {
55 $style = '';
56
57 foreach ( self::$used_templates as $slug => $template ) {
58 $style .= self::get_template_css( $template );
59 }
60
61 if ( $style ) {
62 echo '<style>' . $style . '</style>';
63 }
64 }
65 }
66
67 /**
68 * Add template as being used on this page to output CSS for later.
69 *
70 * @since 3.0.0
71 */
72 public static function add_used_template( $template ) {
73 if ( ! array_key_exists( $template['slug'], self::$used_templates ) ) {
74 self::$used_templates[ $template['slug'] ] = $template;
75 }
76 }
77
78 /**
79 * Get template output.
80 *
81 * @since 3.0.0
82 * @param mixed $item Item to output the template for.
83 * @param mixed $slug Slug of the template we want.
84 */
85 public static function get_template( $item, $slug = false ) {
86 $template = self::get_template_by_slug( $slug );
87
88 // Add template to array of used templats.
89 self::add_used_template( $template );
90
91 // Get HTML.
92 WPUPG_Template_Shortcodes::set_current_item( $item );
93 $html = self::replace_placeholders( $template['html'], $item );
94 $output = do_shortcode( $html );
95 WPUPG_Template_Shortcodes::set_current_item( false );
96
97 return $output;
98 }
99
100 /**
101 * Replace placeholders in template HTML.
102 *
103 * @since 3.8.0
104 * @param mixed $html Template HTML.
105 * @param mixed $item Item to output the template for.
106 */
107 public static function replace_placeholders( $html, $item = false ) {
108 if ( $item ) {
109 $html = str_ireplace( '%wpupg_id%', $item->id(), $html );
110 }
111
112 return $html;
113 }
114
115 /**
116 * Get template by slug.
117 *
118 * @since 3.0.0
119 * @param mixed $slug Slug of the template we want.
120 */
121 public static function get_template_by_slug( $slug = false ) {
122 $templates = self::get_templates();
123
124 // Default to "Simple" if template is not found.
125 return isset( $templates[ $slug ] ) ? $templates[ $slug ] : $templates[ 'simple' ];
126 }
127
128 /**
129 * Get CSS for a specific template.
130 *
131 * @since 3.0.0
132 * @param object $template_or_slug Template to get the CSS for.
133 */
134 public static function get_template_css( $template_or_slug ) {
135 $css = '';
136
137 if ( is_array( $template_or_slug ) ) {
138 $template = $template_or_slug;
139 } else {
140 $template = self::get_template_by_slug( $template_or_slug );
141 }
142
143 if ( ! $template ) {
144 return $css;
145 }
146
147 if ( 'file' === $template['location'] ) {
148
149 if ( ! $template['custom'] ) {
150 // Get default CSS.
151 ob_start();
152 include( WPUPG_DIR . 'templates/grid/default.css' );
153 $css .= ob_get_contents();
154 ob_end_clean();
155
156 // Replace default classic with template specific one.
157 $css = preg_replace( '/\.wpupg-template(\[|:|\s|\{)/im', '.wpupg-template-' . $template['slug'] . '$1', $css );
158 }
159
160 // Get CSS from stylesheet.
161 if ( $template['stylesheet'] ) {
162 ob_start();
163 include( $template['dir'] . '/' . $template['stylesheet'] );
164 $css .= ob_get_contents();
165 ob_end_clean();
166 }
167 } else {
168 // Get virtual CSS.
169 $css = $template['css'];
170 }
171
172 return $css;
173 }
174
175 /**
176 * Save a template.
177 *
178 * @since 3.0.0
179 * @param mixed $template Template to save.
180 */
181 public static function save_template( $template ) {
182 $templates = self::get_templates();
183 $slug = isset( $template['slug'] ) ? sanitize_title( $template['slug'] ) : false;
184 $old_slug = isset( $template['oldSlug'] ) ? sanitize_title( $template['oldSlug'] ) : $slug;
185
186 // New slug needed.
187 if ( ! $slug || ( array_key_exists( $slug, $templates ) && 'file' === $templates[ $slug ]['location'] ) ) {
188 $slug_base = sanitize_title( $template['name'], 'template' );
189
190 $slug = $slug_base;
191 $i = 2;
192 while ( array_key_exists( $slug, $templates ) ) {
193 $slug = $slug_base . '-' . $i;
194 $i++;
195 }
196
197 if ( $old_slug ) {
198 // Need to update CSS and HTML classes.
199 $template['css'] = str_ireplace( '.wpupg-template-' . $old_slug, '.wpupg-template-' . $slug, $template['css'] );
200 $template['html'] = str_ireplace( 'wpupg-template-' . $old_slug, 'wpupg-template-' . $slug, $template['html'] );
201 }
202 }
203
204 // Sanitize template.
205 $sanitized_template['location'] = 'database';
206 $sanitized_template['custom'] = true;
207 $sanitized_template['dir'] = false;
208 $sanitized_template['url'] = false;
209 $sanitized_template['stylesheet'] = false;
210 $sanitized_template['screenshot'] = false;
211
212 $sanitized_template['premium'] = (bool) $template['premium'];
213 $sanitized_template['slug'] = $slug;
214 $sanitized_template['name'] = sanitize_text_field( $template['name'] );
215 $sanitized_template['css'] = trim( $template['css'] );
216 $sanitized_template['html'] = trim( $template['html'] );
217
218 // Make sure list of templates is up to date.
219 $templates = get_option( 'wpupg_templates', array() );
220 if ( ! in_array( $slug, $templates ) ) {
221 $templates[] = $slug;
222 update_option( 'wpupg_templates', $templates );
223 }
224
225 // Save template in cache and database.
226 self::$templates[$slug] = $sanitized_template;
227 update_option( 'wpupg_template_' . $slug, $sanitized_template );
228
229 return $sanitized_template;
230 }
231
232 /**
233 * Delete a template.
234 *
235 * @since 3.0.0
236 * @param mixed $slug Slug of the template to delete.
237 */
238 public static function delete_template( $slug ) {
239 $slug = sanitize_title( $slug );
240
241 // Make sure list of templates is up to date.
242 $templates = get_option( 'wpupg_templates', array() );
243 if ( false !== ( $index = array_search( $slug, $templates ) ) ) {
244 unset( $templates[ $index ] );
245 }
246 update_option( 'wpupg_templates', $templates );
247 delete_option( 'wpupg_template_' . $slug );
248
249 return $slug;
250 }
251
252 /**
253 * Get all available templates.
254 *
255 * @since 3.0.0
256 */
257 public static function get_templates() {
258 if ( empty( self::$templates ) ) {
259 self::load_templates();
260 }
261
262 return self::$templates;
263 }
264
265 /**
266 * Load all available templates.
267 *
268 * @since 3.0.0
269 */
270 private static function load_templates() {
271 $templates = array();
272
273 $dirs = array_filter( glob( WPUPG_DIR . 'templates/grid/*' ), 'is_dir' );
274 $url = WPUPG_URL . 'templates/grid/';
275
276 foreach ( $dirs as $dir ) {
277 $template = self::load_template( $dir, $url, false );
278 $templates[ $template['slug'] ] = $template;
279 }
280
281 // Load custom templates from parent theme.
282 $theme_dir = get_template_directory();
283
284 if ( file_exists( $theme_dir . '/wpupg-templates' ) && file_exists( $theme_dir . '/wpupg-templates/grid' ) ) {
285 $url = get_template_directory_uri() . '/wpupg-templates/grid/';
286
287 $dirs = array_filter( glob( $theme_dir . '/wpupg-templates/grid/*' ), 'is_dir' );
288
289 foreach ( $dirs as $dir ) {
290 $template = self::load_template( $dir, $url, true );
291 $templates[ $template['slug'] ] = $template;
292 }
293 }
294
295 // Load custom templates from child theme (if present).
296 if ( get_stylesheet_directory() !== $theme_dir ) {
297 $theme_dir = get_stylesheet_directory();
298
299 if ( file_exists( $theme_dir . '/wpupg-templates' ) && file_exists( $theme_dir . '/wpupg-templates/grid' ) ) {
300 $url = get_stylesheet_directory_uri() . '/wpupg-templates/grid/';
301
302 $dirs = array_filter( glob( $theme_dir . '/wpupg-templates/grid/*' ), 'is_dir' );
303
304 foreach ( $dirs as $dir ) {
305 $template = self::load_template( $dir, $url, true );
306 $templates[ $template['slug'] ] = $template;
307 }
308 }
309 }
310
311 // Load templates from database.
312 $db_templates = get_option( 'wpupg_templates', array() );
313
314 foreach ( $db_templates as $slug ) {
315 $template = get_option( 'wpupg_template_' . $slug, false );
316
317 if ( $template ) {
318 $templates[ $slug ] = $template;
319 }
320 }
321
322 self::$templates = $templates;
323 }
324
325 /**
326 * Load template from directory.
327 *
328 * @since 3.0.0
329 * @param mixed $dir Directory to load the template from.
330 * @param mixed $url URL to load the template from.
331 * @param boolean $custom Wether or not this is a custom template included by the user.
332 * @param boolean $premium Wether or not this is a premium template.
333 */
334 private static function load_template( $dir, $url, $custom = false, $premium = false ) {
335 $slug = basename( $dir );
336 $name = ucwords( str_replace( '-', ' ', $slug ) );
337
338 // Allow both .min.css and .css as extension.
339 $stylesheet = file_exists( $dir . '/' . $slug . '.min.css' ) ? $slug . '.min.css' : $slug . '.css';
340
341 // Check for HTML file.
342 $html = file_exists( $dir . '/' . $slug . '.html' ) ? trim( file_get_contents( $dir . '/' . $slug . '.html' ) ) : false;
343
344 return array(
345 'location' => 'file',
346 'custom' => $custom,
347 'premium' => $premium,
348 'name' => $name,
349 'slug' => $slug,
350 'dir' => $dir,
351 'url' => $url . $slug,
352 'stylesheet' => $stylesheet,
353 'html' => $html,
354 );
355 }
356 }
357
358 WPUPG_Template_Manager::init();
359