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-editor.php

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

191 lines 4.8 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 editor.
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 editor.
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_Editor {
21 /**
22 * Register actions and filters.
23 *
24 * @since 3.0.0
25 */
26 public static function init() {
27 add_action( 'admin_menu', array( __CLASS__, 'add_submenu_page' ), 20 );
28 }
29
30 /**
31 * Add the template editor submenu to the WPUPG menu.
32 *
33 * @since 3.0.0
34 */
35 public static function add_submenu_page() {
36 add_submenu_page( 'wpultimatepostgrid', __( 'WPUPG Template Editor', 'wp-ultimate-post-grid' ), __( 'Template Editor', 'wp-ultimate-post-grid' ), 'manage_options', 'wpupg_template_editor', array( __CLASS__, 'template_editor_page_template' ) );
37 }
38
39 /**
40 * Get the template for the template editor page.
41 *
42 * @since 3.0.0
43 */
44 public static function template_editor_page_template() {
45 self::localize_admin_template();
46 echo '<div id="wpupg-template" class="wrap">Loading...</div>';
47 }
48
49 /**
50 * Localize JS for the template editor page.
51 *
52 * @since 5.8.0
53 */
54 public static function localize_admin_template() {
55 // Get all modern templates.
56 $modern_templates = array();
57 $templates = WPUPG_Template_Manager::get_templates();
58
59 foreach ( $templates as $template ) {
60 $modern_templates[ $template['slug'] ] = self::prepare_template_for_editor( $template );
61 }
62
63 wp_localize_script( 'wpupg-admin-template', 'wpupg_admin_template', array(
64 'templates' => $modern_templates,
65 'shortcodes' => WPUPG_Template_Shortcodes::get_shortcodes(),
66 'icons' => WPUPG_Icon::get_all(),
67 'thumbnail_sizes' => get_intermediate_image_sizes(),
68 'preview_item' => self::get_default_preview_item(),
69 ) );
70 }
71
72 /**
73 * Get the default preview item for the template editor.
74 *
75 * @since 3.4.0
76 */
77 private static function get_default_preview_item() {
78 $preview_item = false;
79
80 // Get the latest published post that as a featured image set.
81 $args = array(
82 'post_type' => 'post',
83 'post_status' => 'publish',
84 'posts_per_page' => 1,
85 'ignore_sticky_posts' => true,
86 'order_by' => 'date',
87 'order' => 'DESC',
88 'meta_query' => array(
89 array(
90 'key' => '_thumbnail_id',
91 'value' => '0',
92 'compare' => '>'
93 ),
94 ),
95 );
96
97 $query = new WP_Query( $args );
98 $posts = $query->have_posts() ? $query->posts : array();
99
100 $post = isset( $posts[0] ) ? $posts[0] : false;
101
102 if ( $post ) {
103 $item = WPUPG_Item_Manager::get_item( $post->ID );
104
105 if ( $item ) {
106 $post_type = get_post_type_object( $post->post_type );
107
108 $preview_item = array(
109 'value' => $post->ID,
110 'label' => $post_type->labels->singular_name . ' - ' . $post->ID . ' - ' . $post->post_title,
111 'classes' => $item->classes(),
112 );
113 }
114 }
115
116 return $preview_item;
117 }
118
119 /**
120 * Prepare a template for the template editor.
121 *
122 * @since 3.0.0
123 * @param mixed $template Template to prepare.
124 */
125 public static function prepare_template_for_editor( $template ) {
126 $template['style'] = self::extract_style_with_properties( $template );
127 return $template;
128 }
129
130 /**
131 * Extract the style and optional properties from a template stylesheet.
132 *
133 * @since 3.0.0
134 * @param mixed $template Template to extract from.
135 */
136 private static function extract_style_with_properties( $template ) {
137 $css = WPUPG_Template_Manager::get_template_css( $template );
138
139 // Find properties in CSS.
140 $properties = array();
141
142 preg_match_all( "/:([^:;]+);\s*\/\*([^*]*)\*+([^\/*][^*]*\*+)*\//im", $css, $matches );
143 foreach ( $matches[2] as $index => $comment ) {
144 $value = trim( $matches[1][ $index ] );
145 $comment = trim( $comment );
146
147 // Check if it's one of our comments.
148 if ( 'wpupg_' === substr( $comment, 0, 6 ) ) {
149 $parts = explode( ' ', $comment );
150
151 // First part should be variable name.
152 $id = substr( $parts[0], 6 );
153 unset( $parts[0] );
154
155 if ( $id ) {
156 $property = array(
157 'id' => $id,
158 'name' => ucwords( str_replace( '_', ' ', $id ) ),
159 'default' => $value,
160 'value' => $value,
161 );
162
163 // Check if there are any parts left.
164 foreach ( $parts as $part ) {
165 $pieces = explode( '=', $part );
166
167 if ( 2 === count( $pieces ) ) {
168 if ( ! array_key_exists( $pieces[0], $property ) ) {
169 $property[ $pieces[0] ] = $pieces[1];
170 }
171 }
172 }
173
174 // Add to properties.
175 $properties[ $id ] = $property;
176
177 // Replace with variable in CSS.
178 $css = str_ireplace( $matches[0][ $index ], ': %wpupg_' . $id .'%;', $css );
179 }
180 }
181 }
182
183 return array(
184 'properties' => $properties,
185 'css' => $css,
186 );
187 }
188 }
189
190 WPUPG_Template_Editor::init();
191