PluginProbe
Post Grid Gutenberg Blocks – PostX / 4.1.25
Post Grid Gutenberg Blocks – PostX v4.1.25
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.1.25, at classes/Styles.php

704 lines 25.4 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 private $changed_wp_block = '';
23
24 public function __construct() {
25 add_action( 'rest_api_init', array( $this, 'rest_api_callback' ) );
26 add_action( 'wp_ajax_disable_google_font', array( $this, 'disable_google_font_callback' ) );
27
28 add_action( 'admin_init', array( $this, 'postx_global_css_callback' ) );
29 add_action( 'admin_init', array( $this, 'postx_global_css_dependancies' ) );
30 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_postx_block_css' ) );
31 add_filter( 'render_block', array( $this, 'render_block_callback' ), 10, 2 ); // render block to enqueue corresponding css
32 add_action( 'ultp_enqueue_postx_block_css', array( $this, 'ultp_enqueue_postx_block_css_callback' ), 10, 1 ); // action to enqueue the block css
33
34 add_action( 'after_delete_post', array( $this, 'ultp_delete_post_callback' ), 10, 2 ); // Delete Plugin Data CSS file delete Action
35 }
36
37 /**
38 * REST API Action
39 *
40 * @since v.1.0.0
41 * @return NULL
42 */
43 public function rest_api_callback() {
44 register_rest_route(
45 'ultp/v1',
46 '/save_block_css/',
47 array(
48 array(
49 'methods' => 'POST',
50 'callback' => array( $this, 'save_block_content_css'),
51 'permission_callback' => function () {
52 return current_user_can( 'publish_posts' );
53 },
54 'args' => array()
55 )
56 )
57 );
58 register_rest_route(
59 'ultp/v1',
60 '/get_other_post_content/',
61 array(
62 array(
63 'methods' => 'POST',
64 'callback' => array($this, 'get_other_post_content_callback'),
65 'permission_callback' => function () {
66 return current_user_can('publish_posts');
67 },
68 'args' => array()
69 )
70 )
71 );
72 register_rest_route(
73 'ultp/v1',
74 '/action_option/',
75 array(
76 array(
77 'methods' => 'POST',
78 'callback' => array($this, 'global_settings_action'),
79 'permission_callback' => function () {
80 return current_user_can('edit_posts');
81 },
82 'args' => array()
83 )
84 )
85 );
86 register_rest_route(
87 'ultp/v1',
88 '/postx_presets/',
89 array(
90 array(
91 'methods' => 'POST',
92 'callback' => array($this, 'postx_presets_callback'),
93 'permission_callback' => function () {
94 return current_user_can('edit_posts');
95 },
96 'args' => array()
97 )
98 )
99 );
100 }
101
102 /**
103 * Save block css corresponding to page id
104 *
105 * @since v.1.0.0
106 * @param OBJECT | Request Param of the REST API
107 * @return ARRAY/Exception | Array of the Custom Message
108 */
109 public function save_block_content_css($request) {
110
111 $params = $request->get_params();
112 $post_id = isset($params['post_id']) ? ultimate_post()->ultp_rest_sanitize_params($params['post_id']) : '';
113 $has_block = isset($params['has_block']) ? rest_sanitize_boolean($params['has_block']) : '';
114 $is_preview = isset($params['preview']) ? rest_sanitize_boolean($params['preview']) : '';
115 $is_widget = $post_id == 'ultp-widget';
116
117 $cap = '';
118 if ( $post_id == 'ultp-widget' || get_post_type($post_id) == 'wp_template' || get_post_type($post_id) == 'wp_template_part' ) {
119 $cap = 'publish_posts';
120 }
121 if( !ultimate_post()->permission_check_for_restapi(is_numeric($post_id) ? $post_id : false, $cap ) ) {
122 return;
123 }
124 if ( !empty($params['fseTempId']) ) {
125 // Delete previously saved fse css/old compatibility
126 $this->ultp_delete_post_callback(str_replace('//', '__', $params['fseTempId']), '');
127 }
128 try {
129 $upload_dir_url = wp_upload_dir();
130 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultimate-post/';
131 $upload_dir_url = wp_upload_dir();
132 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultimate-post/';
133 if ( $has_block ) {
134
135 $ultp_block_css = $this->set_top_css($params['block_css']);
136 if ( $is_preview ) {
137 set_transient('_ultp_preview_'.$post_id, $ultp_block_css , 60*60);
138 return ['success' => true, 'preview' => true];
139 }
140
141 global $wp_filesystem;
142 if ( ! $wp_filesystem ) {
143 require_once( ABSPATH . 'wp-admin/includes/file.php' );
144 }
145 if ( $is_widget ) {
146 update_option($post_id, $params['block_css']);
147 } else {
148 $post_id = (int) $post_id;
149 update_post_meta($post_id, '_ultp_active', 'yes');
150 update_post_meta($post_id, '_ultp_css', $ultp_block_css);
151 }
152 ultimate_post()->set_setting('save_version', wp_rand(1, 1000));
153 $filename = "ultp-css-{$post_id}.css";
154
155 WP_Filesystem( false, $upload_dir_url['basedir'], true );
156 if ( ! $wp_filesystem->is_dir( $dir ) ) {
157 $wp_filesystem->mkdir( $dir );
158 }
159 if ( ! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) {
160 throw new \Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post')); //phpcs:ignore
161 }
162 return ['success'=>true, 'message'=> __('PostX css file has been updated.', 'ultimate-post')];
163
164 } else {
165 if ( $is_widget ) {
166 update_option($post_id, '');
167 } else {
168 $post_id = (int) $post_id;
169 delete_post_meta($post_id, '_ultp_active');
170 delete_post_meta($post_id, '_ultp_css');
171 }
172 $filename = "ultp-css-{$post_id}.css";
173 if ( file_exists($dir.$filename) ) {
174 wp_delete_file($dir.$filename);
175 }
176 return ['success' => true, 'message' => __('Data Delete Done', 'ultimate-post')];
177 }
178 }catch( \Exception $e ) {
179 return [ 'success'=> false, 'message'=> $e->getMessage() ];
180 }
181 }
182
183 /**
184 * Get Post Content for other Posts while performing css save
185 *
186 * @since v.4.1.10
187 * @param OBJECT | Request Param of the REST API
188 * @return ARRAY/Exception | Array of the Custom Message
189 */
190 public function get_other_post_content_callback($server) {
191 $post = $server->get_params();
192 $post_id = isset($post['postId']) ? ultimate_post()->ultp_rest_sanitize_params($post['postId']) : '';
193 $p_type = get_post_type($post_id);
194
195 if (
196 $post_id &&
197 (
198 'wp_template_part' === $p_type ||
199 'wp_block'=== $p_type ||
200 ultimate_post()->permission_check_for_restapi($post_id)
201 )
202 ) {
203 if ( 'wp_block' === $p_type ) {
204 $this->handle_wpblock_current_id($post_id);
205 }
206 return array(
207 'success' => true,
208 'data'=> get_post($post_id)->post_content,
209 'message' => __('Data retrive done', 'ultimate-post')
210 );
211 } else {
212 return array(
213 'success' => false,
214 'message' => __('Data not found!!', 'ultimate-post')
215 );
216 }
217 }
218
219
220 /**
221 * Get and Set PostX Global Settings
222 *
223 * @since v.2.4.24
224 * @param OBJECT | Request Param of the REST API
225 * @return ARRAY | Array of the Custom Message
226 */
227 public function global_settings_action($server) {
228 $post = $server->get_params();
229 $_type = isset($post['type'])?ultimate_post()->ultp_rest_sanitize_params($post['type']):'';
230 if ( $_type && ultimate_post()->permission_check_for_restapi() ) {
231 if ( $_type == 'set' ) {
232 if ( current_user_can('edit_others_posts') ) {
233 update_option('postx_global', ultimate_post()->ultp_rest_sanitize_params( $post['data']));
234 }
235 return ['success' => true];
236 } else {
237 return ['success' => true, 'data' => get_option('postx_global', [])];
238 }
239 } else {
240 return ['success' => false];
241 }
242 }
243
244 /**
245 * Get and Set PostX Presets Settings
246 *
247 * @since v.2.4.24
248 * @param OBJECT | Request Param of the REST API
249 * @return ARRAY | Array of the Custom Message
250 */
251 public function postx_presets_callback($server) {
252 $post = $server->get_params();
253 $type = isset($post['type']) ? $post['type'] : '';
254 $key = isset($post['key']) ? $post['key'] : '';
255 $data = isset($post['data']) ? $post['data'] : '';
256
257 if ( $type ) {
258 if ( $type == 'set' ) {
259 if ( current_user_can('edit_others_posts') ) {
260 update_option($key, $data);
261 }
262 return ['success' => true];
263 } else {
264 return ['success' => true, 'data' => get_option($key, [])];
265 }
266 } else {
267 return ['success' => false];
268 }
269 }
270
271 /**
272 * Disable Google Font Callback
273 *
274 * * @since v.2.8.1
275 * @return STRING
276 */
277 public function disable_google_font_callback() {
278 if (
279 !( isset( $_REQUEST['wpnonce'] ) &&
280 wp_verify_nonce( sanitize_key( wp_unslash($_REQUEST['wpnonce']) ), 'ultp-nonce' ) )
281 ) {
282 return ;
283 }
284 if( !ultimate_post()->permission_check_for_restapi() ){
285 return;
286 }
287
288 global $wp_filesystem;
289 if ( ! $wp_filesystem ) {
290 require_once( ABSPATH . 'wp-admin/includes/file.php' );
291 WP_Filesystem();
292 }
293
294 $upload_dir_url = wp_upload_dir();
295 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'ultimate-post/';
296 $css_dir = glob( $dir . '*.css' );
297
298 // Custom Font
299 $custom_fonts = array();
300 if ( ultimate_post()->get_setting( 'ultp_custom_font' ) == 'true' ) {
301 $args = array(
302 'post_type' => 'ultp_custom_font',
303 'post_status' => 'publish',
304 'numberposts' => -1,
305 'order' => 'ASC'
306 );
307 $posts = get_posts( $args );
308 if ( $posts ) {
309 foreach( $posts as $post ) {
310 if ( !empty($post->post_title) ) {
311 $custom_fonts[] = $post->post_title;
312 }
313 }
314 }
315 }
316 wp_reset_postdata();
317 $custom_fonts = implode( '|', $custom_fonts);
318 // system font
319 $exclude_typo = implode( '|', ['Arial','Tahoma','Verdana','Helvetica','Times New Roman','Trebuchet MS','Georgia'] );
320
321 if ( count( $css_dir ) > 0 ) {
322 foreach ( $css_dir as $key => $value ) {
323 $css = $wp_filesystem->get_contents( $value );
324 $filter_css = preg_replace( '/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $css ); // Remove Import Font
325 $final_css = preg_replace( '/(font-family:)((?!'.$custom_fonts.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css ); // Font Replace Except Default Font
326 $wp_filesystem->put_contents( $value, $final_css ); // Update CSS File
327 }
328 }
329
330 global $wpdb;
331 $results = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE `meta_key`='_ultp_css'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
332 if ( ! empty( $results ) ) {
333 foreach ( $results as $key => $value ) {
334 $filter_css = preg_replace('/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $value->meta_value); // Remove Import Font
335 $final_css = preg_replace('/(font-family:)((?!'.$custom_fonts.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css); // Font Replace Except Default Font
336 update_post_meta($value->post_id, '_ultp_css', $final_css);
337 }
338 }
339
340 return wp_send_json_success(__('CSS Updated!', 'ultimate-post'));
341 }
342
343 /**
344 * Check global style loaded or not
345 *
346 * @since 4.0.0
347 * @return NULL
348 */
349 public function postx_global_css_dependancies() {
350 $this->postx_global_css_callback();
351 $wp_styles = wp_styles();
352 $style = $wp_styles->query( 'wp-block-library', 'registered' );
353 if( !$style ) {
354 return;
355 }
356 $array = ['wpxpo-global-style', 'ultp-preset-colors-style', 'ultp-preset-gradient-style', 'ultp-preset-typo-style' ];
357 foreach ($array as $arr) {
358 if( wp_style_is( $arr, 'registered' ) && !in_array( $arr, $style->deps, true ) ) {
359 $style->deps[] = $arr;
360 }
361 }
362 }
363
364 /**
365 * Set Global Color Codes
366 *
367 * @since v.1.0.0
368 * @return NULL
369 */
370 public function postx_global_css_callback() {
371 // Preset CSS
372 $global = get_option('postx_global', []);
373 $custom_css = ':root {
374 --preset-color1: '.(isset($global['presetColor1'])?$global['presetColor1']:'#037fff').';
375 --preset-color2: '.(isset($global['presetColor2'])?$global['presetColor2']:'#026fe0').';
376 --preset-color3: '.(isset($global['presetColor3'])?$global['presetColor3']:'#071323').';
377 --preset-color4: '.(isset($global['presetColor4'])?$global['presetColor4']:'#132133').';
378 --preset-color5: '.(isset($global['presetColor5'])?$global['presetColor5']:'#34495e').';
379 --preset-color6: '.(isset($global['presetColor6'])?$global['presetColor6']:'#787676').';
380 --preset-color7: '.(isset($global['presetColor7'])?$global['presetColor7']:'#f0f2f3').';
381 --preset-color8: '.(isset($global['presetColor8'])?$global['presetColor8']:'#f8f9fa').';
382 --preset-color9: '.(isset($global['presetColor9'])?$global['presetColor9']:'#ffffff').';
383 }';
384 $theme_css = isset($global['globalCSS']) && $global['globalCSS'] ? $global['globalCSS'] : $custom_css.'{}';
385
386 wp_register_style( 'wpxpo-global-style', false, array(), ULTP_VER );
387 wp_enqueue_style( 'wpxpo-global-style' );
388 wp_add_inline_style( 'wpxpo-global-style', $theme_css );
389
390 // preset Colors
391 $ultpPresetColors = get_option('ultpPresetColors', []);
392
393 $rootCSS = ( isset($ultpPresetColors['rootCSS']) && $ultpPresetColors['rootCSS'] ) ? $ultpPresetColors['rootCSS'] : ':root { --postx_preset_Base_1_color: #f4f4ff; --postx_preset_Base_2_color: #dddff8; --postx_preset_Base_3_color: #B4B4D6; --postx_preset_Primary_color: #3323f0; --postx_preset_Secondary_color: #4a5fff; --postx_preset_Tertiary_color: #FFFFFF; --postx_preset_Contrast_3_color: #545472; --postx_preset_Contrast_2_color: #262657; --postx_preset_Contrast_1_color: #10102e; --postx_preset_Over_Primary_color: #ffffff; }';
394 $savedDLMode = ( isset($global['enableDark']) && $global['enableDark'] ) ? 'ultpdark' : 'ultplight';
395 $localDLMode = isset($_COOKIE['ultplocalDLMode']) ? $_COOKIE['ultplocalDLMode'] : $savedDLMode;
396
397
398 if ( $localDLMode == 'ultplight' && $savedDLMode == 'ultpdark' ) {
399 $rootCSS = $this->handle_dark_light_color_switcher($rootCSS);
400 } else if ( $localDLMode == 'ultpdark' ) {
401 if ( $savedDLMode == 'ultplight' ) {
402 $rootCSS = $this->handle_dark_light_color_switcher($rootCSS);
403 }
404 $rootCSS = $rootCSS.' .ultp-dark-logo.wp-block-site-logo img {content: url("'.get_option('ultp_site_dark_logo', '').'");}';
405 }
406
407 wp_register_style( 'ultp-preset-colors-style', false, array(), ULTP_VER );
408 wp_enqueue_style( 'ultp-preset-colors-style' );
409 wp_add_inline_style( 'ultp-preset-colors-style', $rootCSS );
410
411 // preset Gradients
412 $ultpPresetGradients = get_option('ultpPresetGradients', []);
413 wp_register_style( 'ultp-preset-gradient-style', false, array(), ULTP_VER );
414 wp_enqueue_style( 'ultp-preset-gradient-style' );
415 wp_add_inline_style( 'ultp-preset-gradient-style', isset($ultpPresetGradients['rootCSS']) && $ultpPresetGradients['rootCSS'] ? $ultpPresetGradients['rootCSS'] : ':root { --postx_preset_Primary_to_Secondary_to_Right_gradient: linear-gradient(90deg, var(--postx_preset_Primary_color) 0%, var(--postx_preset_Secondary_color) 100%); --postx_preset_Primary_to_Secondary_to_Bottom_gradient: linear-gradient(180deg, var(--postx_preset_Primary_color) 0%, var(--postx_preset_Secondary_color) 100%); --postx_preset_Secondary_to_Primary_to_Right_gradient: linear-gradient(90deg, var(--postx_preset_Secondary_color) 0%, var(--postx_preset_Primary_color) 100%); --postx_preset_Secondary_to_Primary_to_Bottom_gradient: linear-gradient(180deg, var(--postx_preset_Secondary_color) 0%, var(--postx_preset_Primary_color) 100%); --postx_preset_Cold_Evening_gradient: linear-gradient(0deg, rgb(12, 52, 131) 0%, rgb(162, 182, 223) 100%, rgb(107, 140, 206) 100%, rgb(162, 182, 223) 100%); --postx_preset_Purple_Division_gradient: linear-gradient(0deg, rgb(112, 40, 228) 0%, rgb(229, 178, 202) 100%); --postx_preset_Over_Sun_gradient: linear-gradient(60deg, rgb(171, 236, 214) 0%, rgb(251, 237, 150) 100%); --postx_preset_Morning_Salad_gradient: linear-gradient(-255deg, rgb(183, 248, 219) 0%, rgb(80, 167, 194) 100%); --postx_preset_Fabled_Sunset_gradient: linear-gradient(-270deg, rgb(35, 21, 87) 0%, rgb(68, 16, 122) 29%, rgb(255, 19, 97) 67%, rgb(255, 248, 0) 100%); }' );
416
417 // preset Typos
418 $ultpPresetTypos = get_option('ultpPresetTypos', []);
419 wp_register_style( 'ultp-preset-typo-style', false, array(), ULTP_VER );
420 wp_enqueue_style( 'ultp-preset-typo-style' );
421 wp_add_inline_style( 'ultp-preset-typo-style', isset($ultpPresetTypos['presetTypoCSS']) && $ultpPresetTypos['presetTypoCSS'] ? $ultpPresetTypos['presetTypoCSS'] : ':root { --postx_preset_Heading_typo_font_family: Helvetica; --postx_preset_Heading_typo_font_family_type: sans-serif; --postx_preset_Heading_typo_font_weight: 600; --postx_preset_Heading_typo_text_transform: capitalize; --postx_preset_Body_and_Others_typo_font_family: Helvetica; --postx_preset_Body_and_Others_typo_font_family_type: sans-serif; --postx_preset_Body_and_Others_typo_font_weight: 400; --postx_preset_Body_and_Others_typo_text_transform: lowercase; --postx_preset_body_typo_font_size_lg: 16px; --postx_preset_paragraph_1_typo_font_size_lg: 12px; --postx_preset_paragraph_2_typo_font_size_lg: 12px; --postx_preset_paragraph_3_typo_font_size_lg: 12px; --postx_preset_heading_h1_typo_font_size_lg: 42px; --postx_preset_heading_h2_typo_font_size_lg: 36px; --postx_preset_heading_h3_typo_font_size_lg: 30px; --postx_preset_heading_h4_typo_font_size_lg: 24px; --postx_preset_heading_h5_typo_font_size_lg: 20px; --postx_preset_heading_h6_typo_font_size_lg: 16px; }' );
422 }
423
424 /**
425 * Enqueue The Block Style
426 *
427 * * @since v.4.1.8
428 * @return NULL
429 */
430 public function enqueue_postx_block_css() {
431 $this->postx_global_css_callback();
432 if (
433 apply_filters('postx_common_script', false) ||
434 isset($_GET['et_fb']) // divi theme issue
435 ) {
436 ultimate_post()->register_scripts_common();
437 }
438 if ( is_admin() ) {
439 return ;
440 }
441 if ( wp_is_block_theme() ) {
442 $this->handle_old_fse_css();
443 }
444 $css = '';
445 $post_id = ultimate_post()->get_ID();
446 if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { // @codingStandardsIgnoreLine
447 $css = get_transient('_ultp_preview_'.$post_id, true);
448 if ( !$css ) {
449 $css = get_post_meta($post_id, '_ultp_css', true);
450 }
451 }
452 do_action('ultp_enqueue_postx_block_css',
453 [
454 'post_id' => $post_id,
455 'css' => $css,
456 ]
457 );
458 }
459
460
461 /**
462 * Enqueue The Block Style based on block( wp_block, fse_template, wp_template, wp_template_part )
463 *
464 * * @since v.4.1.8
465 * @return NULL
466 */
467 public function render_block_callback($block_content, $block) {
468 if ( isset($block['blockName']) ) {
469 if (
470 $block['blockName'] == "ultimate-post/menu" ||
471 $block['blockName'] == "ultimate-post/menu-item"
472 ) {
473 if ( $block['blockName'] == "ultimate-post/menu" ) {
474 $start = '_ultp_mn_ic_';
475 $end = '_ultp_mn_ic_end_';
476 } else if ( $block['blockName'] == "ultimate-post/menu-item" ) {
477 $start = '_ultp_mi_ic_';
478 $end = '_ultp_mi_ic_end_';
479 }
480 $pattern = '/'.$start.'(.*?)'.$end.'/';
481 preg_match_all($pattern, $block_content, $matches);
482 if ( is_array($matches[1]) ) {
483 foreach ($matches[1] as $m) {
484 if ( $m ) {
485 $block_content = str_replace($start.$m.$end, ultimate_post()->get_svg_icon($m), $block_content);
486 }
487 }
488 }
489 }
490 if (
491 $block['blockName'] == "ultimate-post/mega-menu" &&
492 !ultimate_post()->is_lc_active()
493 ) {
494 return '';
495 }
496 }
497 if (
498 !is_admin() &&
499 isset($block['blockName']) &&
500 strpos($block['blockName'], 'ultimate-post/') === 0
501 && !empty($block['attrs']['currentPostId'])
502 ) {
503 do_action('ultp_enqueue_postx_block_css',
504 [
505 'post_id' => $block['attrs']['currentPostId'],
506 'css' => '',
507 ]
508 );
509 }
510 return $block_content;
511 }
512
513 /**
514 * Enqueue The Block Style
515 *
516 * * @since v.4.1.8
517 * @return NULL
518 */
519 public function ultp_enqueue_postx_block_css_callback($data) {
520 $post_id = isset($data['post_id']) ? $data['post_id'] : '';
521 $css = isset($data['css']) ? $data['css'] : '';
522 if ( wp_style_is("ultp-post-{$post_id}", "enqueued") ) {
523 return ;
524 }
525
526 if ( $post_id ) {
527 if ( $css == '' ) {
528 global $wp_filesystem;
529 if ( ! $wp_filesystem ) {
530 require_once( ABSPATH . 'wp-admin/includes/file.php' );
531 }
532 WP_Filesystem();
533 $upload_dir_url = wp_upload_dir();
534 $_path = trailingslashit($upload_dir_url['basedir']) . "ultimate-post/ultp-css-{$post_id}.css";
535 $css = '';
536 if ( file_exists( $_path ) ) {
537 $css = $wp_filesystem->get_contents($_path);
538 } else {
539 if ( $post_id == 'ultp-widget' ) {
540 $css = get_option($post_id, true);
541 } else {
542 $css = get_post_meta($post_id, '_ultp_css', true);
543 }
544 }
545 }
546 if ( $css ) {
547 ultimate_post()->register_scripts_common();
548 wp_register_style( "ultp-post-{$post_id}", false );
549 wp_enqueue_style( "ultp-post-{$post_id}" );
550 wp_add_inline_style( "ultp-post-{$post_id}", $css );
551 }
552 }
553 }
554
555 /**
556 * Enqueue The Block Style for old saved fse
557 *
558 * * @since v.4.1.8
559 * @return NULL
560 */
561 public function handle_old_fse_css() {
562 global $_wp_current_template_id;
563 if ( isset($_wp_current_template_id) ) {
564 $template_id = str_replace('//', '__', $_wp_current_template_id);
565 $upload_dir_url = wp_upload_dir();
566 $_path = trailingslashit($upload_dir_url['basedir']) . "ultimate-post/ultp-css-{$template_id}.css";
567 if ( file_exists( $_path ) ) {
568 do_action('ultp_enqueue_postx_block_css',
569 [
570 'post_id' => $template_id,
571 'css' => '',
572 ]
573 );
574 }
575 }
576 }
577
578
579 /**
580 * Handle WP Block postid
581 *
582 * @since v.4.1.8
583 * @param OBJECT | Request Param of the REST API
584 * @return ARRAY/Exception | Array of the Custom Message
585 */
586 public function handle_wpblock_current_id($post_id) {
587 $this->changed_wp_block = '';
588 $post = get_post($post_id);
589 $post_content = $post->post_content;
590
591 // Parse the blocks
592 $blocks = parse_blocks($post_content);
593 $updated_blocks = $this->update_block_attributes_func($blocks, $post_id);
594 if ( $this->changed_wp_block ) {
595 wp_update_post(array(
596 'ID' => $post_id,
597 'post_content' => serialize_blocks($updated_blocks)
598 ));
599 }
600 }
601
602 /**
603 * Handle WP Block postid save
604 *
605 * @since v.4.1.8
606 * @param OBJECT | Request Param of the REST API
607 * @return ARRAY/Exception | Array of the Custom Message
608 */
609 function update_block_attributes_func($blocks, $post_id) {
610 foreach ($blocks as &$block) {
611 if (
612 strpos($block['blockName'], 'ultimate-post/') > -1 &&
613 isset($block['attrs']['currentPostId']) &&
614 $post_id != $block['attrs']['currentPostId']
615 ) {
616 $this->changed_wp_block = true;
617 $block['attrs'] = array_merge($block['attrs'], ['currentPostId' => $post_id]);
618 }
619 // Recursively update inner blocks
620 if ( !empty($block['innerBlocks']) ) {
621 $block['innerBlocks'] = $this->update_block_attributes_func($block['innerBlocks'], $post_id);
622 }
623 }
624 return $blocks;
625 }
626
627 /**
628 * Save Import CSS in the top of the File
629 *
630 * @since v.1.0.0
631 * @param STRING | CSS (STRING)
632 * @return STRING | Generated CSS
633 */
634 public function set_top_css($get_css = '') {
635 $disable_google_font = ultimate_post()->get_setting('disable_google_font');
636 if ( $disable_google_font != 'yes' ) {
637 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
638 $font_exists = substr_count($get_css, $css_url);
639 if ( $font_exists ) {
640 $pattern = sprintf('/%s(.+?)%s/ims', preg_quote($css_url, '/'), preg_quote("');", '/'));
641 if ( preg_match_all($pattern, $get_css, $matches) ) {
642 $fonts = $matches[0];
643 $get_css = str_replace($fonts, '', $get_css);
644 if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/' , $get_css, $matche_weight ) ) {
645 $weight = array_map( function($val) {
646 $process = trim( str_replace( array( 'font-weight',':',';' ) , '', $val ) );
647 if (is_numeric( $process )) {
648 return $process;
649 }
650 }, $matche_weight[0] );
651 foreach ( $fonts as $key => $val ) {
652 $fonts[$key] = str_replace( "');",'', $val ).':'.implode( ',',$weight )."');";
653 }
654 }
655 $fonts = array_unique($fonts);
656 $get_css = implode('', $fonts).$get_css;
657 }
658 }
659 }
660 return $get_css;
661 }
662
663
664 /**
665 * swap color for dark light
666 *
667 * @since 4.0.0
668 * @return NULL
669 */
670 public function handle_dark_light_color_switcher( $rootCSS ) {
671 $rootCSS = str_replace(
672 [ '--postx_preset_Base_1_color', '--postx_preset_Base_2_color', '--postx_preset_Base_3_color' ],
673 [ '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum', '--postx_preset_Base_3_color_dum' ],
674 $rootCSS
675 );
676 $rootCSS = str_replace(
677 [ '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color','--postx_preset_Contrast_3_color' ],
678 [ '--postx_preset_Base_1_color', '--postx_preset_Base_2_color','--postx_preset_Base_3_color' ],
679 $rootCSS
680 );
681 $rootCSS = str_replace(
682 [ '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum','--postx_preset_Base_3_color_dum' ],
683 [ '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color', '--postx_preset_Contrast_3_color' ],
684 $rootCSS
685 );
686 return $rootCSS;
687 }
688
689
690 /**
691 * Delete Plugin Data CSS file delete Action
692 *
693 * * @since v.2.9.8
694 * @return STRING
695 */
696 public function ultp_delete_post_callback( $post_id, $post ) {
697 $upload = wp_upload_dir();
698 $upload_dir = $upload['basedir'];
699 $upload_dir_path = $upload_dir . "/ultimate-post/ultp-css-{$post_id}.css";
700 if ( file_exists( $upload_dir_path ) ) {
701 wp_delete_file( $upload_dir_path );
702 }
703 }
704 }