PluginProbe
Post Grid Gutenberg Blocks – PostX / 3.1.5
Post Grid Gutenberg Blocks – PostX v3.1.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 3.1.5, at classes/Styles.php

518 lines 16.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 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
55 global $wp_filesystem;
56 if (! $wp_filesystem ) {
57 require_once( ABSPATH . 'wp-admin/includes/file.php' );
58 WP_Filesystem();
59 }
60 $upload_dir_url = wp_upload_dir();
61 $dir = trailingslashit($upload_dir_url['basedir']).'ultimate-post/';
62 $css_dir = glob($dir.'*.css');
63 $exclude_typo = implode('|', ['Arial','Tahoma','Verdana','Helvetica','Times New Roman','Trebuchet MS','Georgia']);
64
65 if (count($css_dir) > 0) {
66 foreach( $css_dir as $key => $value ) {
67 $css = $wp_filesystem->get_contents($value);
68 $filter_css = preg_replace('/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $css); // Remove Import Font
69 $final_css = preg_replace('/(font-family:)((?!'.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css); // Font Replace Except Default Font
70 $wp_filesystem->put_contents( $value, $final_css ); // Update CSS File
71 }
72 }
73
74 global $wpdb;
75 $results = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE `meta_key`='_ultp_css'" );
76 if (!empty($results)) {
77 foreach ($results as $key => $value) {
78 $filter_css = preg_replace('/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $value->meta_value); // Remove Import Font
79 $final_css = preg_replace('/(font-family:)((?!'.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css); // Font Replace Except Default Font
80 update_option($value->meta_key, $final_css);
81 }
82 }
83
84 return wp_send_json_success(__('CSS Updated!', 'ultimate-post'));
85 }
86
87 /**
88 * REST API Action
89 *
90 * @since v.1.0.0
91 * @return NULL
92 */
93 public function save_block_css_callback() {
94 register_rest_route(
95 'ultp/v1',
96 '/save_block_css/',
97 array(
98 array(
99 'methods' => 'POST',
100 'callback' => array( $this, 'save_block_content_css'),
101 'permission_callback' => function () {
102 return current_user_can( 'publish_posts' );
103 },
104 'args' => array()
105 )
106 )
107 );
108 register_rest_route(
109 'ultp/v1',
110 '/get_reusable_posts/',
111 array(
112 array(
113 'methods' => 'POST',
114 'callback' => array($this, 'get_reusable_posts_call'),
115 'permission_callback' => function () {
116 return current_user_can('edit_posts');
117 },
118 'args' => array()
119 )
120 )
121 );
122 register_rest_route(
123 'ultp/v1',
124 '/appened_css/',
125 array(
126 array(
127 'methods' => 'POST',
128 'callback' => array($this, 'appened_css_call'),
129 'permission_callback' => function () {
130 return current_user_can('publish_posts');
131 },
132 'args' => array()
133 )
134 )
135 );
136 register_rest_route(
137 'ultp/v1',
138 '/action_option/',
139 array(
140 array(
141 'methods' => 'POST',
142 'callback' => array($this, 'global_settings_action'),
143 'permission_callback' => function () {
144 return current_user_can('edit_posts');
145 },
146 'args' => array()
147 )
148 )
149 );
150 }
151
152 /**
153 * Get and Set PostX Global Settings
154 *
155 * @since v.2.4.24
156 * @param OBJECT | Request Param of the REST API
157 * @return ARRAY | Array of the Custom Message
158 */
159 public function global_settings_action($server) {
160 $post = $server->get_params();
161 if (isset($post['type'])) {
162 if ($post['type'] == 'set') {
163 update_option('postx_global', $post['data']);
164 return ['success' => true];
165 } else {
166 return ['success' => true, 'data' => get_option('postx_global', [])];
167 }
168 } else {
169 return ['success' => false];
170 }
171 }
172
173 /**
174 * Save Import CSS in the top of the File
175 *
176 * @since v.1.0.0
177 * @param OBJECT | Request Param of the REST API
178 * @return ARRAY/Exception | Array of the Custom Message
179 */
180 public function appened_css_call($server) {
181 global $wp_filesystem;
182 if (! $wp_filesystem ) {
183 require_once( ABSPATH . 'wp-admin/includes/file.php' );
184 }
185 $post = $server->get_params();
186 $css = $post['inner_css'];
187 $post_id = (int) sanitize_text_field($post['post_id']);
188 if ($post_id) {
189 $upload_dir_url = wp_upload_dir();
190 $filename = "ultp-css-{$post_id}.css";
191 $dir = trailingslashit($upload_dir_url['basedir']).'ultimate-post/';
192 update_post_meta($post_id, '_wopb_css', $css);
193 WP_Filesystem( false, $upload_dir_url['basedir'], true );
194 if (! $wp_filesystem->is_dir( $dir ) ) {
195 $wp_filesystem->mkdir( $dir );
196 }
197 if (! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
198 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post' )); //phpcs:ignore
199 }
200 wp_send_json_success(array('success' => true, 'message' => __('Data retrive done', 'ultimate-post')));
201 } else {
202 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
203 }
204 }
205
206
207 /**
208 * Save Import CSS in the top of the File
209 *
210 * @since v.1.0.0
211 * @param OBJECT | Request Param of the REST API
212 * @return ARRAY/Exception | Array of the Custom Message
213 */
214 public function get_reusable_posts_call($server) {
215 $post = $server->get_params();
216 if (isset($post['postId'])) {
217 return array('success' => true, 'data'=> get_post($post['postId'])->post_content, 'message' => __('Data retrive done', 'ultimate-post'));
218 } else {
219 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
220 }
221 }
222
223
224 /**
225 * Save Import CSS in the top of the File
226 *
227 * @since v.1.0.0
228 * @param OBJECT | Request Param of the REST API
229 * @return ARRAY/Exception | Array of the Custom Message
230 */
231 public function save_block_content_css($request) {
232
233 try{
234 global $wp_filesystem;
235 if (! $wp_filesystem ) {
236 require_once( ABSPATH . 'wp-admin/includes/file.php' );
237 }
238 $params = $request->get_params();
239
240 $post_id = sanitize_text_field($params['post_id']);
241 if ($post_id == 'ultp-widget' && $params['has_block']) {
242 update_option($post_id, $params['block_css']);
243 return ['success' => true, 'message' => __('Widget CSS Saved', 'ultimate-post')];
244 }
245
246 $upload_dir_url = wp_upload_dir();
247 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultimate-post/';
248
249 if ( strpos($post_id, '//') !== false) {
250 $data = explode('//', $post_id);
251
252
253 if ($data[2] == 'wp_template_part') {
254 $filename = "ultp-css-{$data[0]}__{$data[3]}.css";
255 $template_css = get_option($data[0].'__'.$data[3], '');
256 $reg = '/(\/\*TEMPLATE_START_'.$data[1].')[^"]+?(TEMPLATE_CLOSE_'.$data[1].'\*\/)/m';
257
258 $ultp_block_css = '';
259 if (strpos($template_css, 'TEMPLATE_START_'.$data[1]) !== false) {
260 $ultp_block_css = preg_replace($reg, '/*TEMPLATE_START_'.$data[1].'*/' . $params['block_css'] . '/*TEMPLATE_CLOSE_'.$data[1].'*/', $template_css);
261 } else {
262 $ultp_block_css = $template_css . '/*TEMPLATE_START_'.$data[1].'*/' . $params['block_css'] . '/*TEMPLATE_CLOSE_'.$data[1].'*/';
263 }
264 $ultp_block_css = $this->set_top_css($ultp_block_css);
265 WP_Filesystem( false, $upload_dir_url['basedir'], true );
266 if (! $wp_filesystem->is_dir( $dir ) ) {
267 $wp_filesystem->mkdir( $dir );
268 }
269
270 $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css );
271
272 update_option($data[0].'__'.$data[3], $ultp_block_css);
273 } else if ($data[2] == 'wp_template') {
274 $filename = "ultp-css-{$data[0]}__{$data[1]}.css";
275 $ultp_block_css = $this->set_top_css($params['block_css']);
276 WP_Filesystem( false, $upload_dir_url['basedir'], true );
277 if (! $wp_filesystem->is_dir( $dir ) ) {
278 $wp_filesystem->mkdir( $dir );
279 }
280 $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css );
281
282 update_option($data[0].'__'.$data[1], $ultp_block_css);
283 }
284
285 return ['success' => true, 'message' => __('Template & Template Part CSS Saved.', 'ultimate-post')];
286 }
287
288 $post_id = (int) $post_id;
289 $filename = "ultp-css-{$post_id}.css";
290
291 if ($params['has_block']) {
292 // Set Saving ID for Clean Cache
293 ultimate_post()->set_setting('save_version', rand(1, 1000));
294
295 update_post_meta($post_id, '_ultp_active', 'yes');
296 $ultp_block_css = $this->set_top_css($params['block_css']);
297
298 // Preview Check
299 if ($params['preview']) {
300 set_transient('_ultp_preview_'.$post_id, $ultp_block_css , 60*60);
301 return ['success' => true];
302 }
303
304 WP_Filesystem( false, $upload_dir_url['basedir'], true );
305 if (! $wp_filesystem->is_dir( $dir ) ) {
306 $wp_filesystem->mkdir( $dir );
307 }
308 if (! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) {
309 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post')); //phpcs:ignore
310 }
311 update_post_meta($post_id, '_ultp_css', $ultp_block_css);
312 return ['success'=>true, 'message'=>__('PostX css file has been updated.', 'ultimate-post')];
313 } else {
314 delete_post_meta($post_id, '_ultp_active');
315 if (file_exists($dir.$filename)) {
316 unlink($dir.$filename);
317 }
318 delete_post_meta($post_id, '_ultp_css');
319 return ['success' => true, 'message' => __('Data Delete Done', 'ultimate-post')];
320 }
321 }catch(Exception $e) {
322 return [ 'success'=> false, 'message'=> $e->getMessage() ];
323 }
324 }
325
326
327 /**
328 * Save Import CSS in the top of the File
329 *
330 * @since v.1.0.0
331 * @param STRING | CSS (STRING)
332 * @return STRING | Generated CSS
333 */
334 public function set_top_css($get_css = '') {
335 $disable_google_font = ultimate_post()->get_setting('disable_google_font');
336 if ($disable_google_font != 'yes') {
337 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
338 $font_exists = substr_count($get_css, $css_url);
339 if ($font_exists) {
340 $pattern = sprintf('/%s(.+?)%s/ims', preg_quote($css_url, '/'), preg_quote("');", '/'));
341 if (preg_match_all($pattern, $get_css, $matches)) {
342 $fonts = $matches[0];
343 $get_css = str_replace($fonts, '', $get_css);
344 if (preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/' , $get_css, $matche_weight )) {
345 $weight = array_map( function($val) {
346 $process = trim( str_replace( array( 'font-weight',':',';' ) , '', $val ) );
347 if (is_numeric( $process )) {
348 return $process;
349 }
350 }, $matche_weight[0] );
351 foreach ( $fonts as $key => $val ) {
352 $fonts[$key] = str_replace( "');",'', $val ).':'.implode( ',',$weight )."');";
353 }
354 }
355 $fonts = array_unique($fonts);
356 $get_css = implode('', $fonts).$get_css;
357 }
358 }
359 }
360 return $get_css;
361 }
362
363
364 /**
365 * Enqueue CSS in HEAD or as a File
366 *
367 * @since v.1.0.0
368 * @return NULL
369 */
370 public function require_block_css() {
371 $css_type = ultimate_post()->get_setting('css_save_as');
372 if (isset($_GET['preview_id']) && isset($_GET['preview_nonce'])) {
373 add_action('wp_head', array( $this, 'add_block_inline_css' ), 100);
374 } else if ($css_type === 'filesystem') {
375 add_action('wp_enqueue_scripts', array($this, 'add_block_css_file'));
376 } else {
377 add_action('wp_head', array( $this, 'add_block_inline_css' ), 100);
378 }
379
380 add_action('wp_enqueue_scripts', array($this, 'postx_global_css'));
381 add_action('admin_enqueue_scripts', array($this, 'postx_global_css'));
382 }
383
384 /**
385 * Set Global Color Codes
386 *
387 * @since v.1.0.0
388 * @return NULL
389 */
390 public function postx_global_css() {
391 // Preset CSS
392 $global = get_option('postx_global', []);
393 $custom_css = ':root {
394 --preset-color1: '.(isset($global['presetColor1'])?$global['presetColor1']:'#037fff').';
395 --preset-color2: '.(isset($global['presetColor2'])?$global['presetColor2']:'#026fe0').';
396 --preset-color3: '.(isset($global['presetColor3'])?$global['presetColor3']:'#071323').';
397 --preset-color4: '.(isset($global['presetColor4'])?$global['presetColor4']:'#132133').';
398 --preset-color5: '.(isset($global['presetColor5'])?$global['presetColor5']:'#34495e').';
399 --preset-color6: '.(isset($global['presetColor6'])?$global['presetColor6']:'#787676').';
400 --preset-color7: '.(isset($global['presetColor7'])?$global['presetColor7']:'#f0f2f3').';
401 --preset-color8: '.(isset($global['presetColor8'])?$global['presetColor8']:'#f8f9fa').';
402 --preset-color9: '.(isset($global['presetColor9'])?$global['presetColor9']:'#ffffff').';
403 }';
404 wp_register_style( 'wpxpo-global-style', false );
405 wp_enqueue_style( 'wpxpo-global-style' );
406 wp_add_inline_style( 'wpxpo-global-style', $custom_css );
407 }
408
409 /**
410 * Set CSS as File
411 *
412 * @since v.1.0.0
413 * @return NULL
414 */
415 public function add_block_css_file() {
416 $header_id = ultimate_post()->conditions('header');
417 if ($header_id) {
418 ultimate_post()->set_css_style( $header_id );
419 }
420 $footer_id = ultimate_post()->conditions('footer');
421 if ($footer_id) {
422 ultimate_post()->set_css_style( $footer_id );
423 }
424 ultimate_post()->set_css_style( ultimate_post()->get_ID() );
425 $this->add_fse_theme_css('filesystem'); // FSE theme support
426 }
427
428 public function add_fse_theme_css($type = 'filesystem') {
429 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
430 global $_wp_current_template_id;
431 if (isset($_wp_current_template_id)) {
432 ultimate_post()->register_scripts_common();
433 $template_id = str_replace('//', '__',$_wp_current_template_id);
434 if ($type == 'inline') {
435 $this->set_inline_css_style( $template_id );
436 } else {
437 ultimate_post()->set_css_style( $template_id );
438 }
439 }
440 }
441 }
442
443 /**
444 * Set Inline CSS in Head
445 *
446 * @since v.1.0.0
447 * @return NULL
448 */
449 public function add_block_inline_css() {
450 $header_id = ultimate_post()->conditions('header');
451 if ($header_id) {
452 $this->set_inline_css_style( $header_id );
453 }
454 $footer_id = ultimate_post()->conditions('footer');
455 if ($footer_id) {
456 $this->set_inline_css_style( $footer_id );
457 }
458 $this->set_inline_css_style(ultimate_post()->get_ID());
459 $this->add_fse_theme_css('inline'); // FSE theme support
460 }
461 public function set_inline_css_style($post_id) {
462 if ($post_id) {
463 $upload_dir_url = wp_get_upload_dir();
464 $upload_css_dir_url = trailingslashit( $upload_dir_url['basedir'] );
465 $css_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$post_id}.css";
466
467 // Reusable CSS
468 $reusable_id = [];
469 $reusable_css = '';
470 if ( strpos($post_id, '__') !== false) {
471 $template = get_block_template( str_replace('__', '//', $post_id) );
472 if ($template->wp_id) {
473 $reusable_id = ultimate_post()->reusable_id($template->wp_id);
474 }
475 } else {
476 $reusable_id = ultimate_post()->reusable_id($post_id);
477 }
478 if (!empty($reusable_id)) {
479 foreach ( $reusable_id as $id ) {
480 $reusable_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$id}.css";
481 if (file_exists( $reusable_dir_path )) {
482 $reusable_css .= file_get_contents($reusable_dir_path);
483 } else {
484 $reusable_css .= get_post_meta($id, '_ultp_css', true);
485 }
486 }
487 }
488 if (isset($_GET['preview_id']) && isset($_GET['preview_nonce'])) {
489 $css = get_transient('_ultp_preview_'.$post_id, true);
490 if (!$css) {
491 $css = get_post_meta($post_id, '_ultp_css', true);
492 }
493 if ($css) {
494 if ($reusable_css) {
495 $css = $this->set_top_css($css.$reusable_css);
496 }
497 echo ultimate_post()->set_inline($css); //phpcs:ignore
498 }
499 } else if (file_exists( $css_dir_path )) {
500 $css = file_get_contents($css_dir_path);
501 if ($reusable_css) {
502 $css = $this->set_top_css($css.$reusable_css);
503 }
504 if ($css) {
505 echo ultimate_post()->set_inline($css); //phpcs:ignore
506 }
507 } else {
508 $css = get_post_meta($post_id, '_ultp_css', true);
509 if ($reusable_css) {
510 $css = $this->set_top_css($css.$reusable_css);
511 }
512 if ($css) {
513 echo ultimate_post()->set_inline($css); //phpcs:ignore
514 }
515 }
516 }
517 }
518 }