PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.1.0
Post Grid Gutenberg Blocks – PostX v5.1.0
5.1.1 5.1.0 5.0.41 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 All 240 releases
ultimate-post / classes / Styles.php

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

866 lines 29.3 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-admin-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'] ) ? sanitize_key( wp_unslash( $_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'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only Divi editor detection (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 $this->handle_fse_template_parts_css();
467 }
468 $css = '';
469 $post_id = ultimate_post()->get_ID();
470 if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { // @codingStandardsIgnoreLine.
471 $css = get_transient( '_ultp_preview_' . $post_id, true );
472 if ( ! $css ) {
473 $css = get_post_meta( $post_id, '_ultp_css', true );
474 }
475 }
476 do_action(
477 'ultp_enqueue_postx_block_css',
478 array(
479 'post_id' => $post_id,
480 'css' => $css,
481 )
482 );
483 }
484
485
486 /**
487 * Enqueue The Block Style based on block( wp_block, fse_template, wp_template, wp_template_part )
488 *
489 * @since v.4.1.8
490 *
491 * @param STRING $block_content The block content.
492 * @param ARRAY $block The block data array.
493 * @return void
494 */
495 public function render_block_callback( $block_content, $block ) {
496 // ultimate-post/social
497 $block_list = array(
498 'ultimate-post/menu',
499 'ultimate-post/menu-item',
500 'ultimate-post/accordion-item',
501 'ultimate-post/tabs',
502 'ultimate-post/gallery',
503 'ultimate-post/social',
504 'ultimate-post/button',
505 'ultimate-post/list',
506 'ultimate-post/table-of-content'
507 );
508
509 if ( isset( $block['blockName'] ) ) {
510 if ( in_array($block['blockName'], $block_list) ) {
511 switch ($block['blockName']) {
512 case 'ultimate-post/menu':
513 $start = '_ultp_mn_ic_';
514 $end = '_ultp_mn_ic_end_';
515 break;
516 case 'ultimate-post/menu-item':
517 $start = '_ultp_mi_ic_';
518 $end = '_ultp_mi_ic_end_';
519 break;
520 case 'ultimate-post/accordion-item':
521 $start = '_ultp_aci_ic_';
522 $end = '_ultp_aci_ic_end_';
523 break;
524 case 'ultimate-post/tabs':
525 $start = '_ultp_tb_ic_';
526 $end = '_ultp_tb_ic_end_';
527 break;
528 case 'ultimate-post/gallery':
529 $start = '_ultp_gl_ic_';
530 $end = '_ultp_gl_ic_end_';
531 break;
532 case 'ultimate-post/social':
533 $start = '_ultp_sc_ic_';
534 $end = '_ultp_sc_ic_end_';
535 break;
536 case 'ultimate-post/button':
537 $start = '_ultp_btn_ic_';
538 $end = '_ultp_btn_ic_end_';
539 break;
540 case 'ultimate-post/list':
541 $start = '_ultp_list_ic_';
542 $end = '_ultp_list_ic_end_';
543 break;
544 case 'ultimate-post/table-of-content':
545 $start = '_ultp_toc_ic_';
546 $end = '_ultp_toc_ic_end_';
547 break;
548 }
549 // ultimate-post/list
550 $pattern = '/' . $start . '(.*?)' . $end . '/';
551 preg_match_all( $pattern, $block_content, $matches );
552 if ( is_array( $matches[1] ) ) {
553 foreach ( $matches[1] as $m ) {
554 if ( $m ) {
555 $block_content = str_replace( $start . $m . $end, ultimate_post()->get_svg_icon( $m ), $block_content );
556 }
557 }
558 }
559 }
560 /**
561 * Whether the Mega Menu block renders. PostX Pro provides it; PostX alone does not.
562 *
563 * @param bool $render Whether to output the block. False by default.
564 * @param array $block The block being rendered.
565 */
566 if (
567 'ultimate-post/mega-menu' === $block['blockName'] &&
568 ! apply_filters( 'ultp_mega_menu_render', false, $block )
569 ) {
570 return '';
571 }
572 }
573 if ( ! is_admin() &&
574 isset( $block['blockName'] ) &&
575 $block['blockName'] === 'core/block' &&
576 ! empty( $block['attrs']['ref'] )
577 ) {
578 // Validate ref_id is a positive integer
579 $ref_id = absint( $block['attrs']['ref'] );
580 if ( $ref_id < 1 ) {
581 return $block_content;
582 }
583
584 if ( get_post_type( $ref_id ) === 'wp_block' &&
585 get_post_meta( $ref_id, '_ultp_active', true ) === 'yes'
586 ) {
587 do_action(
588 'ultp_enqueue_postx_block_css',
589 array(
590 'post_id' => $ref_id,
591 'css' => '',
592 )
593 );
594 }
595 }
596 if ( ! is_admin() &&
597 isset( $block['blockName'] ) &&
598 strpos( $block['blockName'], 'ultimate-post/' ) === 0
599 && ! empty( $block['attrs']['currentPostId'] )
600 ) {
601 do_action(
602 'ultp_enqueue_postx_block_css',
603 array(
604 'post_id' => $block['attrs']['currentPostId'],
605 'css' => '',
606 )
607 );
608 }
609 return $block_content;
610 }
611
612 /**
613 * Enqueue The Block Style
614 *
615 * @since v.4.1.8
616 *
617 * @param ARRAY $data The data array containing post_id and css.
618 * @return NULL
619 */
620 public function ultp_enqueue_postx_block_css_callback( $data ) {
621 $post_id = isset( $data['post_id'] ) ? $data['post_id'] : '';
622 $css = isset( $data['css'] ) ? $data['css'] : '';
623 if ( wp_style_is( "ultp-post-{$post_id}", 'enqueued' ) ) {
624 return;
625 }
626
627 if ( $post_id ) {
628 if ( $css == '' ) {
629 global $wp_filesystem;
630 if ( ! $wp_filesystem ) {
631 require_once ABSPATH . 'wp-admin/includes/file.php';
632 }
633 WP_Filesystem();
634 $upload_dir_url = wp_upload_dir();
635 $_path = trailingslashit( $upload_dir_url['basedir'] ) . "ultimate-post/ultp-css-{$post_id}.css";
636 $css = '';
637 if ( file_exists( $_path ) ) {
638 $css = $wp_filesystem->get_contents( $_path );
639 } elseif ( $post_id == 'ultp-widget' ) {
640 $css = get_option( $post_id, true );
641 } else {
642 $css = get_post_meta( $post_id, '_ultp_css', true );
643 }
644 }
645 if ( $css ) {
646 ultimate_post()->register_scripts_common();
647 wp_register_style( "ultp-post-{$post_id}", false, array(), ULTP_VER );
648 wp_enqueue_style( "ultp-post-{$post_id}" );
649 wp_add_inline_style( "ultp-post-{$post_id}", $css );
650 }
651 }
652 }
653
654 /**
655 * Enqueue CSS for FSE template parts (header, footer, etc.) that contain PostX blocks.
656 * Must run during wp_enqueue_scripts (before wp_head) so styles land in <head>.
657 *
658 * @since v.4.1.8
659 * @return void
660 */
661 public function handle_fse_template_parts_css() {
662 $parts = get_posts(
663 array(
664 'post_type' => 'wp_template_part',
665 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Finds the few template parts that use PostX blocks; sites have only a handful of wp_template_part posts.
666 array(
667 'key' => '_ultp_active',
668 'value' => 'yes',
669 ),
670 ),
671 'posts_per_page' => -1,
672 'fields' => 'ids',
673 )
674 );
675
676 if ( empty( $parts ) ) {
677 return;
678 }
679
680 $upload_dir = wp_upload_dir();
681 $dir = trailingslashit( $upload_dir['basedir'] ) . 'ultimate-post/';
682
683 foreach ( $parts as $part_id ) {
684 // Validate post ID is numeric
685 $part_id = absint( $part_id );
686 if ( $part_id < 1 ) {
687 continue;
688 }
689
690 $css_file = $dir . "ultp-css-{$part_id}.css";
691
692 if ( file_exists( $css_file ) ) {
693 do_action(
694 'ultp_enqueue_postx_block_css',
695 array(
696 'post_id' => $part_id,
697 'css' => '',
698 )
699 );
700 }
701 }
702 }
703
704 /**
705 * Enqueue The Block Style for old saved fse
706 *
707 * * @since v.4.1.8
708 *
709 * @return void
710 */
711 public function handle_old_fse_css() {
712 global $_wp_current_template_id;
713 if ( isset( $_wp_current_template_id ) ) {
714 $template_id = str_replace( '//', '__', $_wp_current_template_id );
715 $upload_dir_url = wp_upload_dir();
716 $_path = trailingslashit( $upload_dir_url['basedir'] ) . "ultimate-post/ultp-css-{$template_id}.css";
717 if ( file_exists( $_path ) ) {
718 do_action(
719 'ultp_enqueue_postx_block_css',
720 array(
721 'post_id' => $template_id,
722 'css' => '',
723 )
724 );
725 }
726 }
727 }
728
729
730 /**
731 * Handle WP Block postid
732 *
733 * @since v.4.1.8
734 * @param OBJECT $post_id Request Param of the REST API.
735 * @return void
736 */
737 public function handle_wpblock_current_id( $post_id ) {
738 $this->changed_wp_block = '';
739 $post = get_post( $post_id );
740 $post_content = $post->post_content;
741
742 // Parse the blocks
743 $blocks = parse_blocks( $post_content );
744 $updated_blocks = $this->update_block_attributes_func( $blocks, $post_id );
745 if ( $this->changed_wp_block ) {
746 wp_update_post(
747 array(
748 'ID' => $post_id,
749 'post_content' => serialize_blocks( $updated_blocks ),
750 )
751 );
752 }
753 }
754
755 /**
756 * Handle WP Block postid save
757 *
758 * @since v.4.1.8
759 * @param ARRAY $blocks The array of blocks to update.
760 * @param INT|STRING $post_id The post ID to set in block attributes.
761 * @return array/Exception | Array of the Custom Message
762 */
763 function update_block_attributes_func( $blocks, $post_id ) {
764 foreach ( $blocks as &$block ) {
765 if ( strpos( $block['blockName'], 'ultimate-post/' ) > -1 &&
766 isset( $block['attrs']['currentPostId'] ) &&
767 $post_id != $block['attrs']['currentPostId']
768 ) {
769 $this->changed_wp_block = true;
770 $block['attrs'] = array_merge( $block['attrs'], array( 'currentPostId' => $post_id ) );
771 }
772 // Recursively update inner blocks.
773 if ( ! empty( $block['innerBlocks'] ) ) {
774 $block['innerBlocks'] = $this->update_block_attributes_func( $block['innerBlocks'], $post_id );
775 }
776 }
777 return $blocks;
778 }
779
780 /**
781 * Save Import CSS in the top of the File
782 *
783 * @since v.1.0.0
784 * @param STRING | $get_css CSS (STRING).
785 * @return STRING | Generated CSS
786 */
787 public function set_top_css( $get_css = '' ) {
788
789 $disable_google_font = ultimate_post()->get_setting( 'disable_google_font' );
790 if ( $disable_google_font != 'yes' ) {
791 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
792 $font_exists = substr_count( $get_css, $css_url );
793 if ( $font_exists ) {
794 $pattern = sprintf( '/%s(.+?)%s/ims', preg_quote( $css_url, '/' ), preg_quote( "');", '/' ) );
795 if ( preg_match_all( $pattern, $get_css, $matches ) ) {
796 $fonts = $matches[0];
797 $get_css = str_replace( $fonts, '', $get_css );
798 if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/', $get_css, $matche_weight ) ) {
799 $weight = array_map(
800 function ( $val ) {
801 $process = trim( str_replace( array( 'font-weight', ':', ';' ), '', $val ) );
802 if ( is_numeric( $process ) ) {
803 return $process;
804 }
805 },
806 $matche_weight[0]
807 );
808 foreach ( $fonts as $key => $val ) {
809 $fonts[ $key ] = str_replace( "');", '', $val ) . ':' . implode( ',', $weight ) . "');";
810 }
811 }
812 $fonts = array_unique( $fonts );
813 $get_css = implode( '', $fonts ) . $get_css;
814 }
815 }
816 }
817 return $get_css;
818 }
819
820
821 /**
822 * Swap color for dark light
823 *
824 * @since 4.0.0
825 * @param STRING $rootCSS The root CSS string to swap colors in.
826 * @return NULL
827 */
828 public function handle_dark_light_color_switcher( $rootCSS ) {
829 $rootCSS = str_replace(
830 array( '--postx_preset_Base_1_color', '--postx_preset_Base_2_color', '--postx_preset_Base_3_color' ),
831 array( '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum', '--postx_preset_Base_3_color_dum' ),
832 $rootCSS
833 );
834 $rootCSS = str_replace(
835 array( '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color', '--postx_preset_Contrast_3_color' ),
836 array( '--postx_preset_Base_1_color', '--postx_preset_Base_2_color', '--postx_preset_Base_3_color' ),
837 $rootCSS
838 );
839 $rootCSS = str_replace(
840 array( '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum', '--postx_preset_Base_3_color_dum' ),
841 array( '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color', '--postx_preset_Contrast_3_color' ),
842 $rootCSS
843 );
844 return $rootCSS;
845 }
846
847
848 /**
849 * Delete Plugin Data CSS file delete Action
850 *
851 * @since v.2.9.8
852 *
853 * @param MIXED $post_id The post ID whose CSS file should be deleted.
854 * @param MIXED $post The post object (unused).
855 * @return void
856 */
857 public function ultp_delete_post_callback( $post_id, $post ) {
858 $upload = wp_upload_dir();
859 $upload_dir = $upload['basedir'];
860 $upload_dir_path = $upload_dir . "/ultimate-post/ultp-css-{$post_id}.css";
861 if ( file_exists( $upload_dir_path ) ) {
862 wp_delete_file( $upload_dir_path );
863 }
864 }
865 }
866