PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.0.5
Post Grid Gutenberg Blocks – PostX v5.0.5
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 5.0.5, at classes/Styles.php

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