PluginProbe
Post Grid Gutenberg Blocks – PostX / 4.0.3
Post Grid Gutenberg Blocks – PostX v4.0.3
5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 2.1.2 All 237 releases
ultimate-post / classes / Styles.php

Styles.php in Post Grid Gutenberg Blocks – PostX 4.0.3, at classes/Styles.php

661 lines 22.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Styles Add and Style REST API Action.
4 *
5 * @package ULTP\Styles
6 * @since v.1.0.0
7 */
8 namespace ULTP;
9
10 defined('ABSPATH') || exit;
11
12 /**
13 * Styles class.
14 */
15 class Styles {
16
17 /**
18 * Setup class.
19 *
20 * @since v.1.0.0
21 */
22 public function __construct() {
23 $this->require_block_css();
24 add_action( 'rest_api_init', array( $this, 'save_block_css_callback' ) );
25 add_action( 'wp_ajax_disable_google_font', array( $this, 'disable_google_font_callback' ) );
26 add_action( 'after_delete_post', array( $this, 'ultp_delete_post_callback' ), 10, 2 ); // Delete Plugin Data CSS file delete Action
27 }
28
29 /**
30 * Delete Plugin Data CSS file delete Action
31 *
32 * * @since v.2.9.8
33 * @return STRING
34 */
35 public function ultp_delete_post_callback( $post_id, $post ) {
36 $upload = wp_upload_dir();
37 $upload_dir = $upload['basedir'];
38 $upload_dir_path = $upload_dir . "/ultimate-post/ultp-css-{$post_id}.css";
39 if ( file_exists( $upload_dir_path ) ) {
40 wp_delete_file( $upload_dir_path );
41 }
42 }
43
44 /**
45 * Disable Google Font Callback
46 *
47 * * @since v.2.8.1
48 * @return STRING
49 */
50 public function disable_google_font_callback() {
51 if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) {
52 return ;
53 }
54 if(!ultimate_post()->permission_check_for_restapi()){
55 return;
56 }
57
58 global $wp_filesystem;
59 if ( ! $wp_filesystem ) {
60 require_once( ABSPATH . 'wp-admin/includes/file.php' );
61 WP_Filesystem();
62 }
63
64 $upload_dir_url = wp_upload_dir();
65 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'ultimate-post/';
66 $css_dir = glob( $dir . '*.css' );
67 $exclude_typo = implode( '|', ['Arial','Tahoma','Verdana','Helvetica','Times New Roman','Trebuchet MS','Georgia'] );
68
69 if ( count( $css_dir ) > 0 ) {
70 foreach ( $css_dir as $key => $value ) {
71 $css = $wp_filesystem->get_contents( $value );
72 $filter_css = preg_replace( '/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $css ); // Remove Import Font
73 $final_css = preg_replace( '/(font-family:)((?!'.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css ); // Font Replace Except Default Font
74 $wp_filesystem->put_contents( $value, $final_css ); // Update CSS File
75 }
76 }
77
78 global $wpdb;
79 $results = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE `meta_key`='_ultp_css'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
80 if ( ! empty( $results ) ) {
81 foreach ( $results as $key => $value ) {
82 $filter_css = preg_replace('/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $value->meta_value); // Remove Import Font
83 $final_css = preg_replace('/(font-family:)((?!'.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css); // Font Replace Except Default Font
84 update_option($value->meta_key, $final_css);
85 }
86 }
87
88 return wp_send_json_success(__('CSS Updated!', 'ultimate-post'));
89 }
90
91 /**
92 * REST API Action
93 *
94 * @since v.1.0.0
95 * @return NULL
96 */
97 public function save_block_css_callback() {
98 register_rest_route(
99 'ultp/v1',
100 '/save_block_css/',
101 array(
102 array(
103 'methods' => 'POST',
104 'callback' => array( $this, 'save_block_content_css'),
105 'permission_callback' => function () {
106 return current_user_can( 'publish_posts' );
107 },
108 'args' => array()
109 )
110 )
111 );
112 register_rest_route(
113 'ultp/v1',
114 '/get_reusable_posts/',
115 array(
116 array(
117 'methods' => 'POST',
118 'callback' => array($this, 'get_reusable_posts_call'),
119 'permission_callback' => function () {
120 return current_user_can('edit_posts');
121 },
122 'args' => array()
123 )
124 )
125 );
126 register_rest_route(
127 'ultp/v1',
128 '/appened_css/',
129 array(
130 array(
131 'methods' => 'POST',
132 'callback' => array($this, 'appened_css_call'),
133 'permission_callback' => function () {
134 return current_user_can('publish_posts');
135 },
136 'args' => array()
137 )
138 )
139 );
140 register_rest_route(
141 'ultp/v1',
142 '/action_option/',
143 array(
144 array(
145 'methods' => 'POST',
146 'callback' => array($this, 'global_settings_action'),
147 'permission_callback' => function () {
148 return current_user_can('edit_posts');
149 },
150 'args' => array()
151 )
152 )
153 );
154 register_rest_route(
155 'ultp/v1',
156 '/postx_presets/',
157 array(
158 array(
159 'methods' => 'POST',
160 'callback' => array($this, 'postx_presets_callback'),
161 'permission_callback' => function () {
162 return current_user_can('edit_posts');
163 },
164 'args' => array()
165 )
166 )
167 );
168 }
169
170 /**
171 * Get and Set PostX Presets Settings
172 *
173 * @since v.2.4.24
174 * @param OBJECT | Request Param of the REST API
175 * @return ARRAY | Array of the Custom Message
176 */
177 public function postx_presets_callback($server) {
178 $post = $server->get_params();
179 $type = isset($post['type']) ? $post['type'] : '';
180 $key = isset($post['key']) ? $post['key'] : '';
181 $data = isset($post['data']) ? $post['data'] : '';
182
183 if ($type) {
184 if ($type == 'set') {
185 update_option($key, $data);
186 return ['success' => true];
187 } else {
188 return ['success' => true, 'data' => get_option($key, [])];
189 }
190 } else {
191 return ['success' => false];
192 }
193 }
194 /**
195 * Get and Set PostX Global Settings
196 *
197 * @since v.2.4.24
198 * @param OBJECT | Request Param of the REST API
199 * @return ARRAY | Array of the Custom Message
200 */
201 public function global_settings_action($server) {
202 $post = $server->get_params();
203 $post_type = isset($post['type'])?ultimate_post()->ultp_rest_sanitize_params($post['type']):'';
204 if ($post_type && ultimate_post()->permission_check_for_restapi()) {
205 if ($post_type == 'set') {
206 update_option('postx_global', ultimate_post()->ultp_rest_sanitize_params( $post['data']));
207 return ['success' => true];
208 } else {
209 return ['success' => true, 'data' => get_option('postx_global', [])];
210 }
211 } else {
212 return ['success' => false];
213 }
214 }
215
216 /**
217 * Save Import CSS in the top of the File
218 *
219 * @since v.1.0.0
220 * @param OBJECT | Request Param of the REST API
221 * @return ARRAY/Exception | Array of the Custom Message
222 */
223 public function appened_css_call($server) {
224 global $wp_filesystem;
225 if ( ! $wp_filesystem ) {
226 require_once( ABSPATH . 'wp-admin/includes/file.php' );
227 }
228 $post = $server->get_params();
229 $css = $post['inner_css'];
230 $post_id = isset($post['post_id'])? (int) ultimate_post()->ultp_rest_sanitize_params($post['post_id']):0;
231 if ( $post_id && 'wp_block' === get_post_type($post_id) ) {
232
233 $upload_dir_url = wp_upload_dir();
234 $filename = "ultp-css-{$post_id}.css";
235 $dir = trailingslashit($upload_dir_url['basedir']).'ultimate-post/';
236 update_post_meta($post_id, '_ultp_css', $css);
237 WP_Filesystem( false, $upload_dir_url['basedir'], true );
238 if ( ! $wp_filesystem->is_dir( $dir ) ) {
239 $wp_filesystem->mkdir( $dir );
240 }
241 if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
242 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post' )); //phpcs:ignore
243 }
244 wp_send_json_success(array('success' => true, 'message' => __('Data retrive done', 'ultimate-post')));
245 } else {
246 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
247 }
248 }
249
250
251 /**
252 * Save Import CSS in the top of the File
253 *
254 * @since v.1.0.0
255 * @param OBJECT | Request Param of the REST API
256 * @return ARRAY/Exception | Array of the Custom Message
257 */
258 public function get_reusable_posts_call($server) {
259 $post = $server->get_params();
260 $post_id = isset($post['postId'])? ultimate_post()->ultp_rest_sanitize_params($post['postId']):'';
261 if ( $post_id && ( ultimate_post()->permission_check_for_restapi($post_id) || 'wp_block'===get_post_type($post_id) )) {
262 return array('success' => true, 'data'=> get_post($post_id)->post_content, 'message' => __('Data retrive done', 'ultimate-post'));
263 } else {
264 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
265 }
266 }
267
268
269 /**
270 * Save Import CSS in the top of the File
271 *
272 * @since v.1.0.0
273 * @param OBJECT | Request Param of the REST API
274 * @return ARRAY/Exception | Array of the Custom Message
275 */
276 public function save_block_content_css($request) {
277
278 $params = $request->get_params();
279 $post_id = isset($params['post_id'])? ultimate_post()->ultp_rest_sanitize_params($params['post_id']):'';
280
281 if(!ultimate_post()->permission_check_for_restapi(is_numeric($post_id)?$post_id:false)) {
282 return;
283 }
284
285 try{
286 global $wp_filesystem;
287 if ( ! $wp_filesystem ) {
288 require_once( ABSPATH . 'wp-admin/includes/file.php' );
289 }
290
291 $params['has_block'] = isset($params['has_block'])?rest_sanitize_boolean($params['has_block']):'';
292 $params['preview'] = isset($params['preview'])?rest_sanitize_boolean($params['preview']):'';
293
294 if ( $post_id == 'ultp-widget' && $params['has_block'] ) { // Only Editor and upwards role user can save widget css
295 update_option($post_id, $params['block_css']);
296 return ['success' => true, 'message' => __('Widget CSS Saved', 'ultimate-post')];
297 }
298
299 $upload_dir_url = wp_upload_dir();
300 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultimate-post/';
301
302 if ( strpos($post_id, '//') !== false ) {
303 $data = explode('//', $post_id);
304
305
306 if ( $data[2] == 'wp_template_part' ) {
307 $filename = "ultp-css-{$data[0]}__{$data[3]}.css";
308 $template_css = get_option($data[0].'__'.$data[3], '');
309 $reg = '/(\/\*TEMPLATE_START_'.$data[1].')[^"]+?(TEMPLATE_CLOSE_'.$data[1].'\*\/)/m';
310
311 $ultp_block_css = '';
312 if ( strpos($template_css, 'TEMPLATE_START_'.$data[1]) !== false ) {
313 $ultp_block_css = preg_replace($reg, '/*TEMPLATE_START_'.$data[1].'*/' . $params['block_css'] . '/*TEMPLATE_CLOSE_'.$data[1].'*/', $template_css);
314 } else {
315 $ultp_block_css = $template_css . '/*TEMPLATE_START_'.$data[1].'*/' . $params['block_css'] . '/*TEMPLATE_CLOSE_'.$data[1].'*/';
316 }
317 $ultp_block_css = $this->set_top_css($ultp_block_css);
318 WP_Filesystem( false, $upload_dir_url['basedir'], true );
319 if ( ! $wp_filesystem->is_dir( $dir ) ) {
320 $wp_filesystem->mkdir( $dir );
321 }
322
323 $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css );
324
325 update_option($data[0].'__'.$data[3], $ultp_block_css);
326 } else if ( $data[2] == 'wp_template' ) {
327 $filename = "ultp-css-{$data[0]}__{$data[1]}.css";
328 $ultp_block_css = $this->set_top_css($params['block_css']);
329 WP_Filesystem( false, $upload_dir_url['basedir'], true );
330 if ( ! $wp_filesystem->is_dir( $dir ) ) {
331 $wp_filesystem->mkdir( $dir );
332 }
333 $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css );
334
335 update_option($data[0].'__'.$data[1], $ultp_block_css);
336 }
337
338 return ['success' => true, 'message' => __('Template & Template Part CSS Saved.', 'ultimate-post')];
339 }
340
341 $post_id = (int) $post_id;
342 $filename = "ultp-css-{$post_id}.css";
343
344 if ( $params['has_block'] ) {
345 // Set Saving ID for Clean Cache
346 ultimate_post()->set_setting('save_version', wp_rand(1, 1000));
347
348 update_post_meta($post_id, '_ultp_active', 'yes');
349 $ultp_block_css = $this->set_top_css($params['block_css']);
350
351 // Preview Check
352 if ( $params['preview'] ) {
353 set_transient('_ultp_preview_'.$post_id, $ultp_block_css , 60*60);
354 return ['success' => true];
355 }
356
357 WP_Filesystem( false, $upload_dir_url['basedir'], true );
358 if ( ! $wp_filesystem->is_dir( $dir ) ) {
359 $wp_filesystem->mkdir( $dir );
360 }
361 if ( ! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) {
362 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post')); //phpcs:ignore
363 }
364 update_post_meta($post_id, '_ultp_css', $ultp_block_css);
365 return ['success'=>true, 'message'=>__('PostX css file has been updated.', 'ultimate-post')];
366 } else {
367 delete_post_meta($post_id, '_ultp_active');
368 if ( file_exists($dir.$filename) ) {
369 wp_delete_file($dir.$filename);
370 }
371 delete_post_meta($post_id, '_ultp_css');
372 return ['success' => true, 'message' => __('Data Delete Done', 'ultimate-post')];
373 }
374 }catch(Exception $e) {
375 return [ 'success'=> false, 'message'=> $e->getMessage() ];
376 }
377 }
378
379
380 /**
381 * Save Import CSS in the top of the File
382 *
383 * @since v.1.0.0
384 * @param STRING | CSS (STRING)
385 * @return STRING | Generated CSS
386 */
387 public function set_top_css($get_css = '') {
388 $disable_google_font = ultimate_post()->get_setting('disable_google_font');
389 if ( $disable_google_font != 'yes' ) {
390 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
391 $font_exists = substr_count($get_css, $css_url);
392 if ( $font_exists ) {
393 $pattern = sprintf('/%s(.+?)%s/ims', preg_quote($css_url, '/'), preg_quote("');", '/'));
394 if ( preg_match_all($pattern, $get_css, $matches) ) {
395 $fonts = $matches[0];
396 $get_css = str_replace($fonts, '', $get_css);
397 if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/' , $get_css, $matche_weight ) ) {
398 $weight = array_map( function($val) {
399 $process = trim( str_replace( array( 'font-weight',':',';' ) , '', $val ) );
400 if (is_numeric( $process )) {
401 return $process;
402 }
403 }, $matche_weight[0] );
404 foreach ( $fonts as $key => $val ) {
405 $fonts[$key] = str_replace( "');",'', $val ).':'.implode( ',',$weight )."');";
406 }
407 }
408 $fonts = array_unique($fonts);
409 $get_css = implode('', $fonts).$get_css;
410 }
411 }
412 }
413 return $get_css;
414 }
415
416
417 /**
418 * Enqueue CSS in HEAD or as a File
419 *
420 * @since v.1.0.0
421 * @return NULL
422 */
423 public function require_block_css() {
424 $css_type = ultimate_post()->get_setting('css_save_as');
425 if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { // @codingStandardsIgnoreLine
426 add_action( 'wp_head', array( $this, 'add_block_inline_css' ), 100 );
427 } else if ( $css_type === 'filesystem' ) {
428 add_action( 'wp_enqueue_scripts', array( $this, 'add_block_css_file' ) );
429 } else {
430 add_action( 'wp_head', array( $this, 'add_block_inline_css' ), 100 );
431 }
432
433 add_action('wp_enqueue_scripts', array($this, 'postx_global_css'));
434 add_action( 'admin_init', array( $this, 'postx_global_css' ), 1 );
435 add_action( 'admin_init', array( $this, 'postx_global_css_dependancies' ), 2 );
436 }
437
438 /**
439 * Check global style loaded or not
440 *
441 * @since 4.0.0
442 * @return NULL
443 */
444 public function postx_global_css_dependancies() {
445 $wp_styles = wp_styles();
446 $style = $wp_styles->query( 'wp-block-library', 'registered' );
447 if( ! $style ) {
448 return;
449 }
450 $array = ['wpxpo-global-style', 'ultp-preset-colors-style', 'ultp-preset-gradient-style', 'ultp-preset-typo-style' ];
451 foreach ($array as $arr) {
452 if( wp_style_is( $arr, 'registered' ) && !in_array( $arr, $style->deps, true ) ) {
453 $style->deps[] = $arr;
454 }
455 }
456 }
457
458 /**
459 * Set Global Color Codes
460 *
461 * @since v.1.0.0
462 * @return NULL
463 */
464 public function postx_global_css() {
465 // Preset CSS
466 $global = get_option('postx_global', []);
467 $custom_css = ':root {
468 --preset-color1: '.(isset($global['presetColor1'])?$global['presetColor1']:'#037fff').';
469 --preset-color2: '.(isset($global['presetColor2'])?$global['presetColor2']:'#026fe0').';
470 --preset-color3: '.(isset($global['presetColor3'])?$global['presetColor3']:'#071323').';
471 --preset-color4: '.(isset($global['presetColor4'])?$global['presetColor4']:'#132133').';
472 --preset-color5: '.(isset($global['presetColor5'])?$global['presetColor5']:'#34495e').';
473 --preset-color6: '.(isset($global['presetColor6'])?$global['presetColor6']:'#787676').';
474 --preset-color7: '.(isset($global['presetColor7'])?$global['presetColor7']:'#f0f2f3').';
475 --preset-color8: '.(isset($global['presetColor8'])?$global['presetColor8']:'#f8f9fa').';
476 --preset-color9: '.(isset($global['presetColor9'])?$global['presetColor9']:'#ffffff').';
477 }';
478 $theme_css = isset($global['globalCSS']) && $global['globalCSS'] ? $global['globalCSS'] : $custom_css.'{}';
479
480 wp_register_style( 'wpxpo-global-style', false, array(), ULTP_VER );
481 wp_enqueue_style( 'wpxpo-global-style' );
482 wp_add_inline_style( 'wpxpo-global-style', $theme_css );
483
484 // preset Colors
485 $ultpPresetColors = get_option('ultpPresetColors', []);
486 $rootCSS = ( isset($ultpPresetColors['rootCSS']) && $ultpPresetColors['rootCSS'] ) ? $ultpPresetColors['rootCSS'] : '{}';
487 $savedDLMode = ( isset($global['enableDark']) && $global['enableDark'] ) ? 'ultpdark' : 'ultplight';
488 $localDLMode = isset($_COOKIE['ultplocalDLMode']) ? $_COOKIE['ultplocalDLMode'] : $savedDLMode;
489
490
491 if ( $localDLMode == 'ultplight' && $savedDLMode == 'ultpdark' ) {
492 $rootCSS = $this->handle_dark_light_color_switcher($rootCSS);
493 } else if ( $localDLMode == 'ultpdark' ) {
494 if ( $savedDLMode == 'ultplight' ) {
495 $rootCSS = $this->handle_dark_light_color_switcher($rootCSS);
496 }
497 $rootCSS = $rootCSS.' .ultp-dark-logo.wp-block-site-logo img {content: url("'.get_option('ultp_site_dark_logo', '').'");}';
498 }
499
500 wp_register_style( 'ultp-preset-colors-style', false, array(), ULTP_VER );
501 wp_enqueue_style( 'ultp-preset-colors-style' );
502 wp_add_inline_style( 'ultp-preset-colors-style', $rootCSS );
503
504 // preset Gradients
505 $ultpPresetGradients = get_option('ultpPresetGradients', []);
506 wp_register_style( 'ultp-preset-gradient-style', false, array(), ULTP_VER );
507 wp_enqueue_style( 'ultp-preset-gradient-style' );
508 wp_add_inline_style( 'ultp-preset-gradient-style', isset($ultpPresetGradients['rootCSS']) && $ultpPresetGradients['rootCSS'] ? $ultpPresetGradients['rootCSS'] : '{}' );
509
510 // preset Typos
511 $ultpPresetTypos = get_option('ultpPresetTypos', []);
512 wp_register_style( 'ultp-preset-typo-style', false, array(), ULTP_VER );
513 wp_enqueue_style( 'ultp-preset-typo-style' );
514 wp_add_inline_style( 'ultp-preset-typo-style', isset($ultpPresetTypos['presetTypoCSS']) && $ultpPresetTypos['presetTypoCSS'] ? $ultpPresetTypos['presetTypoCSS'] : '{}' );
515 }
516
517 /**
518 * swap color for dark light
519 *
520 * @since 4.0.0
521 * @return NULL
522 */
523 public function handle_dark_light_color_switcher( $rootCSS ) {
524 $rootCSS = str_replace(
525 [ '--postx_preset_Base_1_color', '--postx_preset_Base_2_color', '--postx_preset_Base_3_color' ],
526 [ '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum', '--postx_preset_Base_3_color_dum' ],
527 $rootCSS
528 );
529 $rootCSS = str_replace(
530 [ '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color','--postx_preset_Contrast_3_color' ],
531 [ '--postx_preset_Base_1_color', '--postx_preset_Base_2_color','--postx_preset_Base_3_color' ],
532 $rootCSS
533 );
534 $rootCSS = str_replace(
535 [ '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum','--postx_preset_Base_3_color_dum' ],
536 [ '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color', '--postx_preset_Contrast_3_color' ],
537 $rootCSS
538 );
539 return $rootCSS;
540 }
541
542 /**
543 * Set CSS as File
544 *
545 * @since v.1.0.0
546 * @return NULL
547 */
548 public function add_block_css_file() {
549 $header_id = ultimate_post()->conditions('header');
550 if ( $header_id ) {
551 ultimate_post()->set_css_style( $header_id );
552 }
553 $footer_id = ultimate_post()->conditions('footer');
554 if ( $footer_id ) {
555 ultimate_post()->set_css_style( $footer_id );
556 }
557 ultimate_post()->set_css_style( ultimate_post()->get_ID() );
558 $this->add_fse_theme_css('filesystem'); // FSE theme support
559 }
560
561 public function add_fse_theme_css( $type = 'filesystem' ) {
562 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
563 global $_wp_current_template_id;
564 if ( isset($_wp_current_template_id) ) {
565 ultimate_post()->register_scripts_common();
566 $template_id = str_replace('//', '__',$_wp_current_template_id);
567 if ( $type == 'inline' ) {
568 $this->set_inline_css_style( $template_id );
569 } else {
570 ultimate_post()->set_css_style( $template_id );
571 }
572 }
573 }
574 }
575
576 /**
577 * Set Inline CSS in Head
578 *
579 * @since v.1.0.0
580 * @return NULL
581 */
582 public function add_block_inline_css() {
583 $header_id = ultimate_post()->conditions('header');
584 if ( $header_id ) {
585 $this->set_inline_css_style( $header_id );
586 }
587 $footer_id = ultimate_post()->conditions('footer');
588 if ( $footer_id ) {
589 $this->set_inline_css_style( $footer_id );
590 }
591 $this->set_inline_css_style(ultimate_post()->get_ID());
592 $this->add_fse_theme_css('inline'); // FSE theme support
593 }
594 public function set_inline_css_style( $post_id ) {
595 if ( $post_id ) {
596 $upload_dir_url = wp_get_upload_dir();
597 $upload_css_dir_url = trailingslashit( $upload_dir_url['basedir'] );
598 $css_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$post_id}.css";
599
600 // Reusable CSS
601 $reusable_id = [];
602 $reusable_css = '';
603 if ( strpos($post_id, '__') !== false ) {
604 $template = get_block_template( str_replace('__', '//', $post_id) );
605 if ( $template->wp_id ) {
606 $reusable_id = ultimate_post()->reusable_id($template->wp_id);
607 }
608 } else {
609 $reusable_id = ultimate_post()->reusable_id($post_id);
610 }
611 if ( !empty($reusable_id) ) {
612 foreach ( $reusable_id as $id ) {
613 $reusable_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$id}.css";
614 if ( file_exists( $reusable_dir_path ) ) {
615 global $wp_filesystem;
616 if (! $wp_filesystem ) {
617 require_once( ABSPATH . 'wp-admin/includes/file.php' );
618 }
619 WP_Filesystem();
620 $reusable_css .= $wp_filesystem->get_contents($reusable_dir_path);
621 } else {
622 $reusable_css .= get_post_meta($id, '_ultp_css', true);
623 }
624 }
625 }
626 if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { // @codingStandardsIgnoreLine
627 $css = get_transient('_ultp_preview_'.$post_id, true);
628 if ( !$css ) {
629 $css = get_post_meta($post_id, '_ultp_css', true);
630 }
631 if ( $css ) {
632 if ( $reusable_css ) {
633 $css = $this->set_top_css($css.$reusable_css);
634 }
635 echo ultimate_post()->set_inline($css, $post_id); //phpcs:ignore
636 }
637 } else if ( file_exists( $css_dir_path ) ) {
638 global $wp_filesystem;
639 if (! $wp_filesystem ) {
640 require_once( ABSPATH . 'wp-admin/includes/file.php' );
641 }
642 WP_Filesystem();
643 $css = $wp_filesystem->get_contents($css_dir_path);
644 if ( $reusable_css ) {
645 $css = $this->set_top_css($css.$reusable_css);
646 }
647 if ( $css ) {
648 echo ultimate_post()->set_inline($css, $post_id); //phpcs:ignore
649 }
650 } else {
651 $css = get_post_meta($post_id, '_ultp_css', true);
652 if ( $reusable_css ) {
653 $css = $this->set_top_css($css.$reusable_css);
654 }
655 if ( $css ) {
656 echo ultimate_post()->set_inline($css, $post_id); //phpcs:ignore
657 }
658 }
659 }
660 }
661 }