PluginProbe
Microthemer Lite – Visual Editor to Customize CSS / trunk
Microthemer Lite – Visual Editor to Customize CSS vtrunk
trunk 5.0.0.2
microthemer / src / Helper.php

Helper.php in Microthemer Lite – Visual Editor to Customize CSS trunk, at src/Helper.php

303 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Microthemer;
4
5 /*
6 * Helper
7 *
8 * A collection of static helper functions that are used in the admin, frontend, logic (inc stand alone)
9 */
10
11 class Helper {
12
13 public static $doDebug = false;
14 public static $debugOutput = '';
15
16 public static function debug($message, $data = false, $die = false){
17
18 if (Helper::$doDebug){
19
20 $output = $message;
21
22 if ($data){
23 $output.= ':<pre>' . print_r($data, 1) . '</pre>';
24 }
25
26 if (!$data){
27 $output.= "\n\n";
28 }
29
30 if ($die){
31 wp_die(wp_kses_post($output)); // debug output only
32 } else {
33 if (Helper::$doDebug !== 'silent'){
34 //echo $output;
35 error_log($output . "\n\n");
36 }
37 //Helper::$debugOutput.= $output;
38 }
39 }
40
41 }
42
43 public static function isBlockAdminPage($id = null){
44
45 global $pagenow;
46 $postId = null;
47 $isBlockAdmin = false;
48
49 //$regular = $pagenow === 'post.php';
50
51 if (!is_admin()){
52 return false;
53 }
54
55 if ($pagenow === 'post-new.php'){
56 $postId = -1;
57 $isBlockAdmin = true;
58 } elseif ($pagenow === 'post.php' && isset($_GET['post'])){
59 $postId = intval($_GET["post"]);
60 $isBlockAdmin = true;
61 } elseif ($pagenow === 'site-editor.php'){
62 if (isset($_GET['postId'])){
63 $postId = sanitize_text_field(wp_unslash($_GET["postId"]));
64 } else {
65 $postId = Helper::homePageData()['id'];
66 }
67 $isBlockAdmin = true;
68 }
69
70 return $isBlockAdmin && ($id === 'global' || $id == $postId);
71 }
72
73 // get the template file WordPress is loading e.g. page, single, single-with-sidebar
74 // this is duplicate code of method with same name in Common class, which may not be present
75 public static function getCurrentTemplateSlug(){
76
77 return !empty(Logic::$cache['settings']['currentTemplate'])
78 ? Logic::$cache['settings']['currentTemplate']
79 : '';
80
81 }
82
83 // determine what type of FSE page we're editing
84 public static function getFSEType($postType, $categoryType){
85 return $postType === 'wp_block' || $categoryType === 'pattern'
86 ? 'wp_pattern'
87 : $postType;
88 }
89
90 // determine what type of FSE page we're editing
91 public static function getTemplateFromPostId($postId, $post){
92
93 if (Common::$live_template !== false){
94 $currentTemplate = Common::$live_template;
95 //echo 'Retrieved from Common::$live_template: ' . $currentTemplate . '<br>';
96 } else {
97 $currentTemplate = get_post_meta($postId, '_wp_page_template', true);
98 //echo 'Retrieved from get_post_meta: ' . $currentTemplate . '<br>';
99 }
100
101 if (!$currentTemplate){
102 $currentTemplate = $post
103 ? ($post->post_type === 'post'
104 ? 'single'
105 : $post->post_type)
106 : 'page';
107 }
108 return $currentTemplate;
109 }
110
111 // if it's a front page fallback on the FSE admin page
112 public static function isFrontOrFallback($orDirectView = true){
113
114 global $pagenow;
115
116 $fse = $pagenow === 'site-editor.php';
117 $regular = $pagenow === 'post.php';
118 $post = isset($_GET["post"]) ? intval($_GET["post"]) : null;
119 $postId = isset($_GET["postId"]) ? sanitize_text_field(wp_unslash($_GET["postId"])) : null;
120 $homePage = Helper::homePageData();
121 $fseFallback = $fse && $postId === null;
122 $fseDirect = $fse && $postId === $homePage['id'];
123 // The request ID is cast above, so normalize the option value before the strict comparison too.
124 $regularDirect = $regular && $post === (int) $homePage['id'];
125 $frontDirect = !is_admin() && (
126 is_front_page() || ( $homePage['id'] > 0 && is_page($homePage['id']) )
127 );
128
129 return $fseFallback || ($orDirectView && ($fseDirect || $regularDirect || $frontDirect));
130 }
131
132 public static function getMicroRoot($wp_content_dir, $wp_content_url){
133
134 global $blog_id;
135
136 if (is_multisite()) {
137
138 $filename = $wp_content_dir . "/blogs.dir/";
139
140 if (file_exists($filename)){
141 if ($blog_id == '1') {
142 $dir = $wp_content_dir . '/blogs.dir/micro-themes/';
143 $url = $wp_content_url . '/blogs.dir/micro-themes/';
144 } else {
145 $dir = $wp_content_dir . '/blogs.dir/' . $blog_id . '/micro-themes/';
146 $url = $wp_content_url . '/blogs.dir/' . $blog_id . '/micro-themes/';
147 }
148 } else {
149 if ($blog_id == '1') {
150 $dir = $wp_content_dir . '/uploads/sites/micro-themes/';
151 $url = $wp_content_url . '/uploads/sites/micro-themes/';
152 } else {
153 $dir = $wp_content_dir . '/uploads/sites/' . $blog_id . '/micro-themes/';
154 $url = $wp_content_url . '/uploads/sites/' . $blog_id . '/micro-themes/';
155 }
156 }
157 } else {
158 $dir = $wp_content_dir . '/micro-themes/';
159 $url = $wp_content_url . '/micro-themes/';
160 }
161
162 return array(
163 'dir' => $dir,
164 'url' => $url,
165 );
166 }
167
168 public static function getTemplateMap($themeSlug = '', &$cache = array()){
169
170 $themeSlug = $themeSlug ?: get_stylesheet();
171
172 // return cache if available
173 if (isset($cache['templateMap'][$themeSlug])){
174 return $cache['templateMap'][$themeSlug];
175 }
176
177 // else read the file
178 $map = null;
179 $dir = Helper::getMicroRoot(wp_normalize_path(WP_CONTENT_DIR), content_url())['dir'];
180 $templateMapFile = $dir . 'mt/cache/themes/'.$themeSlug.'/'.Helper::templateMapName().'.json';
181
182 if (file_exists($templateMapFile)){
183
184 $mapString = file_get_contents($templateMapFile);
185
186 if ($mapString){
187 $map = json_decode($mapString, true);
188 }
189 }
190
191 // cache to save reading the file again
192 $cache['templateMap'][$themeSlug] = $map;
193
194 return $map;
195 }
196
197 public static function isLiveContentTest(){
198 return isset($_POST['live_post_content']);
199 }
200
201 public static function getFSEPermalink($permalink){
202
203 $themeSlug = get_stylesheet();
204
205 // Get parameters in case we are in the FSE view
206 extract(Helper::extractUrlParams($themeSlug));
207
208 if ($postType === 'page' && $postId !== null){
209 $permalink = get_permalink($postId);
210 }
211
212 return $permalink;
213 }
214
215 public static function extractUrlParams($themeSlug){
216 $postType = isset($_GET["postType"]) ? sanitize_key(wp_unslash($_GET["postType"])) : null;
217 $postId = isset($_GET['postId'])
218 ? Helper::removeRedundantThemePrefix(
219 Helper::maybeMakeNumber(sanitize_text_field(wp_unslash($_GET["postId"]))), $themeSlug
220 )
221 : null;
222 $categoryType = isset($_GET["categoryType"]) ? sanitize_key(wp_unslash($_GET["categoryType"])) : null;
223 $fseType = Helper::getFSEType($postType, $categoryType);
224
225 return array(
226 'postType' => $postType,
227 'postId' => $postId,
228 'categoryType' => $categoryType,
229 'fseType' => $fseType,
230 );
231
232 }
233
234 public static function templateMapName(){
235 return Helper::isLiveContentTest()
236 ? 'template-map-live'
237 : 'template-map';
238 }
239
240 public static function extractSyncedPatterns($content){
241
242 $pattern = '/<!-- wp:(block|navigation) {"ref":(\d+)}/';
243
244 preg_match_all($pattern, $content,$matches);
245
246 return isset($matches[2])
247 ? $matches
248 : array('', '');
249
250 }
251
252 // remove redundant theme slug if it matches the name of the file we are working with
253 // this is a duplicate of a method in BlockTrait.php - todo create HelpersTrait
254 public static function removeRedundantThemePrefix($slug, $themeSlug){
255
256 $redundantPrefix = '/^'.$themeSlug . '\/\/?(.+)$/';
257
258 if ($slug && preg_match($redundantPrefix, $slug, $matches)){
259 $slug = $matches[1];
260 }
261
262 return $slug;
263 }
264
265 // Set numeric values to inter or floats
266 public static function maybeMakeNumber($value){
267
268 return is_numeric($value)
269 ? (strpos($value, '.') !== false
270 ? (float) $value
271 : (int) $value
272 )
273 : $value;
274 }
275
276 public static function homePageData(){
277
278 $id = -1;
279
280 // Blog posts on home
281 if (('posts' === get_option( 'show_on_front' ))){
282 $title = esc_html__('Blog home', 'microthemer');
283 $logic = '\Microthemer\is_post_or_page("front")';
284 }
285
286 // Static page on home
287 else {
288 $id = get_option('page_on_front');
289 $post = get_post($id);
290 $title = $post->post_title;
291 $logic = '\Microthemer\is_post_or_page("'.$id.'")';
292 }
293
294 return array(
295 'id' => $id,
296 'title' => $title,
297 'logic' => $logic
298 );
299
300 }
301
302 }
303