PluginProbe
Post Grid Gutenberg Blocks – PostX / 2.9.4
Post Grid Gutenberg Blocks – PostX v2.9.4
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 2.9.4, at classes/Styles.php

422 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Styles Add and Style REST API Action.
4 *
5 * @package ULTP\Styles
6 * @since v.1.0.0
7 */
8 namespace ULTP;
9
10 defined('ABSPATH') || exit;
11
12 /**
13 * Styles class.
14 */
15 class Styles {
16
17 /**
18 * Setup class.
19 *
20 * @since v.1.0.0
21 */
22 public function __construct() {
23 $this->require_block_css();
24 add_action('rest_api_init', array($this, 'save_block_css_callback'));
25 add_action('wp_ajax_disable_google_font', array($this, 'disable_google_font_callback'));
26 }
27
28 /**
29 * Disable Google Font Callback
30 *
31 * * @since v.2.8.1
32 * @return STRING
33 */
34 public function disable_google_font_callback($type) {
35 if (!wp_verify_nonce(wp_unslash($_REQUEST['wpnonce']), 'ultp-nonce') && $local){
36 return ;
37 }
38
39 global $wp_filesystem;
40 if (! $wp_filesystem ) {
41 require_once( ABSPATH . 'wp-admin/includes/file.php' );
42 WP_Filesystem();
43 }
44 $upload_dir_url = wp_upload_dir();
45 $dir = trailingslashit($upload_dir_url['basedir']).'ultimate-post/';
46 $css_dir = glob($dir.'*.css');
47 $exclude_typo = implode('|', ['Arial','Tahoma','Verdana','Helvetica','Times New Roman','Trebuchet MS','Georgia']);
48
49 if (count($css_dir) > 0) {
50 foreach( $css_dir as $key => $value ) {
51 $css = $wp_filesystem->get_contents($value);
52 $filter_css = preg_replace('/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $css); // Remove Import Font
53 $final_css = preg_replace('/(font-family:)((?!'.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css); // Font Replace Except Default Font
54 $wp_filesystem->put_contents( $value, $final_css ); // Update CSS File
55 }
56 }
57
58 global $wpdb;
59 $results = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE `meta_key`='_ultp_css'" );
60 if (!empty($results)) {
61 foreach ($results as $key => $value) {
62 $filter_css = preg_replace('/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $value->meta_value); // Remove Import Font
63 $final_css = preg_replace('/(font-family:)((?!'.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css); // Font Replace Except Default Font
64 update_option($value->meta_key, $final_css);
65 }
66 }
67
68 return wp_send_json_success(__('CSS Updated!', 'ultimate-post'));
69 }
70
71 /**
72 * REST API Action
73 *
74 * @since v.1.0.0
75 * @return NULL
76 */
77 public function save_block_css_callback() {
78 register_rest_route(
79 'ultp/v1',
80 '/save_block_css/',
81 array(
82 array(
83 'methods' => 'POST',
84 'callback' => array( $this, 'save_block_content_css'),
85 'permission_callback' => function () {
86 return current_user_can( 'edit_posts' );
87 },
88 'args' => array()
89 )
90 )
91 );
92 register_rest_route(
93 'ultp/v1',
94 '/get_reusable_posts/',
95 array(
96 array(
97 'methods' => 'POST',
98 'callback' => array($this, 'get_reusable_posts_call'),
99 'permission_callback' => function () {
100 return current_user_can('edit_posts');
101 },
102 'args' => array()
103 )
104 )
105 );
106 register_rest_route(
107 'ultp/v1',
108 '/appened_css/',
109 array(
110 array(
111 'methods' => 'POST',
112 'callback' => array($this, 'appened_css_call'),
113 'permission_callback' => function () {
114 return current_user_can('edit_posts');
115 },
116 'args' => array()
117 )
118 )
119 );
120 register_rest_route(
121 'ultp/v1',
122 '/action_option/',
123 array(
124 array(
125 'methods' => 'POST',
126 'callback' => array($this, 'global_settings_action'),
127 'permission_callback' => function () {
128 return current_user_can('edit_posts');
129 },
130 'args' => array()
131 )
132 )
133 );
134 }
135
136
137 /**
138 * Get and Set PostX Global Settings
139 *
140 * @since v.2.4.24
141 * @param OBJECT | Request Param of the REST API
142 * @return ARRAY | Array of the Custom Message
143 */
144 public function global_settings_action($server) {
145 $post = $server->get_params();
146 if (isset($post['type'])) {
147 if ($post['type'] == 'set') {
148 update_option('postx_global', $post['data']);
149 return ['success' => true];
150 } else {
151 return ['success' => true, 'data' => get_option('postx_global', [])];
152 }
153 } else {
154 return ['success' => false];
155 }
156 }
157
158 /**
159 * Save Import CSS in the top of the File
160 *
161 * @since v.1.0.0
162 * @param OBJECT | Request Param of the REST API
163 * @return ARRAY/Exception | Array of the Custom Message
164 */
165 public function appened_css_call($server) {
166 global $wp_filesystem;
167 if (! $wp_filesystem ) {
168 require_once( ABSPATH . 'wp-admin/includes/file.php' );
169 }
170 $post = $server->get_params();
171 $css = $post['inner_css'];
172 $post_id = (int) sanitize_text_field($post['post_id']);
173 if ($post_id) {
174 $upload_dir_url = wp_upload_dir();
175 $filename = "ultp-css-{$post_id}.css";
176 $dir = trailingslashit($upload_dir_url['basedir']).'ultimate-post/';
177 update_post_meta($post_id, '_wopb_css', $css);
178 WP_Filesystem( false, $upload_dir_url['basedir'], true );
179 if (! $wp_filesystem->is_dir( $dir ) ) {
180 $wp_filesystem->mkdir( $dir );
181 }
182 if (! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
183 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post' ));
184 }
185 wp_send_json_success(array('success' => true, 'message' => __('Data retrive done', 'ultimate-post')));
186 } else {
187 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
188 }
189 }
190
191
192 /**
193 * Save Import CSS in the top of the File
194 *
195 * @since v.1.0.0
196 * @param OBJECT | Request Param of the REST API
197 * @return ARRAY/Exception | Array of the Custom Message
198 */
199 public function get_reusable_posts_call($server) {
200 $post = $server->get_params();
201 if (isset($post['postId'])) {
202 return array('success' => true, 'data'=> get_post($post['postId'])->post_content, 'message' => __('Data retrive done', 'ultimate-post'));
203 } else {
204 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
205 }
206 }
207
208
209 /**
210 * Save Import CSS in the top of the File
211 *
212 * @since v.1.0.0
213 * @param OBJECT | Request Param of the REST API
214 * @return ARRAY/Exception | Array of the Custom Message
215 */
216 public function save_block_content_css($request) {
217 try{
218 global $wp_filesystem;
219 if (! $wp_filesystem ) {
220 require_once( ABSPATH . 'wp-admin/includes/file.php' );
221 }
222 $params = $request->get_params();
223 $post_id = sanitize_text_field($params['post_id']);
224 if ($post_id == 'ultp-widget' && $params['has_block']) {
225 update_option($post_id, $params['block_css']);
226 return ['success' => true, 'message' => __('Widget CSS Saved', 'ultimate-post')];
227 }
228
229 $post_id = (int) $post_id;
230 $filename = "ultp-css-{$post_id}.css";
231 $upload_dir_url = wp_upload_dir();
232 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultimate-post/';
233
234 if ($params['has_block']) {
235 // Set Saving ID for Clean Cache
236 ultimate_post()->set_setting('save_version', rand(1, 1000));
237
238 update_post_meta($post_id, '_ultp_active', 'yes');
239 $ultp_block_css = $this->set_top_css($params['block_css']);
240
241 // Preview Check
242 if ($params['preview']) {
243 set_transient('_ultp_preview_'.$post_id, $ultp_block_css , 60*60);
244 return ['success' => true];
245 }
246
247 WP_Filesystem( false, $upload_dir_url['basedir'], true );
248 if (! $wp_filesystem->is_dir( $dir ) ) {
249 $wp_filesystem->mkdir( $dir );
250 }
251 if (! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) {
252 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post'));
253 }
254 update_post_meta($post_id, '_ultp_css', $ultp_block_css);
255 return ['success'=>true, 'message'=>__('PostX css file has been updated.', 'ultimate-post')];
256 } else {
257 delete_post_meta($post_id, '_ultp_active');
258 if (file_exists($dir.$filename)) {
259 unlink($dir.$filename);
260 }
261 delete_post_meta($post_id, '_ultp_css');
262 return ['success' => true, 'message' => __('Data Delete Done', 'ultimate-post')];
263 }
264 }catch(Exception $e) {
265 return [ 'success'=> false, 'message'=> $e->getMessage() ];
266 }
267 }
268
269
270 /**
271 * Save Import CSS in the top of the File
272 *
273 * @since v.1.0.0
274 * @param STRING | CSS (STRING)
275 * @return STRING | Generated CSS
276 */
277 public function set_top_css($get_css = '') {
278 $disable_google_font = ultimate_post()->get_setting('disable_google_font');
279 if ($disable_google_font != 'yes') {
280 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
281 $font_exists = substr_count($get_css, $css_url);
282 if ($font_exists) {
283 $pattern = sprintf('/%s(.+?)%s/ims', preg_quote($css_url, '/'), preg_quote("');", '/'));
284 if (preg_match_all($pattern, $get_css, $matches)) {
285 $fonts = $matches[0];
286 $get_css = str_replace($fonts, '', $get_css);
287 if (preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/' , $get_css, $matche_weight )) {
288 $weight = array_map( function($val) {
289 $process = trim( str_replace( array( 'font-weight',':',';' ) , '', $val ) );
290 if (is_numeric( $process )) {
291 return $process;
292 }
293 }, $matche_weight[0] );
294 foreach ( $fonts as $key => $val ) {
295 $fonts[$key] = str_replace( "');",'', $val ).':'.implode( ',',$weight )."');";
296 }
297 }
298 $fonts = array_unique($fonts);
299 $get_css = implode('', $fonts).$get_css;
300 }
301 }
302 }
303 return $get_css;
304 }
305
306
307 /**
308 * Enqueue CSS in HEAD or as a File
309 *
310 * @since v.1.0.0
311 * @return NULL
312 */
313 public function require_block_css() {
314 $save_as = ultimate_post()->get_setting('css_save_as');
315 $save_as = $save_as ? $save_as : '';
316 if (isset($_GET['preview_id']) && isset($_GET['preview_nonce'])) {
317 add_action('wp_head', array( $this, 'add_block_inline_css' ), 100);
318 } else if ($save_as === 'filesystem') {
319 add_action('wp_enqueue_scripts', array($this, 'add_block_css_file'));
320 } else {
321 add_action('wp_head', array( $this, 'add_block_inline_css' ), 100);
322 }
323
324 add_action('wp_enqueue_scripts', array($this, 'postx_global_css'));
325 add_action('admin_enqueue_scripts', array($this, 'postx_global_css'));
326 }
327
328 /**
329 * Set Global Color Codes
330 *
331 * @since v.1.0.0
332 * @return NULL
333 */
334 public function postx_global_css() {
335 // Preset CSS
336 $global = get_option('postx_global', []);
337 $custom_css = ':root {
338 --preset-color1: '.(isset($global['presetColor1'])?$global['presetColor1']:'#037fff').';
339 --preset-color2: '.(isset($global['presetColor2'])?$global['presetColor2']:'#026fe0').';
340 --preset-color3: '.(isset($global['presetColor3'])?$global['presetColor3']:'#071323').';
341 --preset-color4: '.(isset($global['presetColor4'])?$global['presetColor4']:'#132133').';
342 --preset-color5: '.(isset($global['presetColor5'])?$global['presetColor5']:'#34495e').';
343 --preset-color6: '.(isset($global['presetColor6'])?$global['presetColor6']:'#787676').';
344 --preset-color7: '.(isset($global['presetColor7'])?$global['presetColor7']:'#f0f2f3').';
345 --preset-color8: '.(isset($global['presetColor8'])?$global['presetColor8']:'#f8f9fa').';
346 --preset-color9: '.(isset($global['presetColor9'])?$global['presetColor9']:'#ffffff').';
347 }';
348 wp_register_style( 'wpxpo-global-style', false );
349 wp_enqueue_style( 'wpxpo-global-style' );
350 wp_add_inline_style( 'wpxpo-global-style', $custom_css );
351 }
352
353 /**
354 * Set CSS as File
355 *
356 * @since v.1.0.0
357 * @return NULL
358 */
359 public function add_block_css_file() {
360 ultimate_post()->set_css_style( ultimate_post()->get_ID() );
361 // ultimate_post()->set_css_style( get_the_ID() ); // Only for the Builder
362 }
363
364
365 /**
366 * Set Inline CSS in Head
367 *
368 * @since v.1.0.0
369 * @return NULL
370 */
371 public function add_block_inline_css() {
372 $this->set_inline_css_style(ultimate_post()->get_ID());
373 // $this->set_inline_css_style(get_the_ID()); // Only for the Builder
374 }
375 public function set_inline_css_style($post_id) {
376 if ($post_id) {
377 $upload_dir_url = wp_get_upload_dir();
378 $upload_css_dir_url = trailingslashit( $upload_dir_url['basedir'] );
379 $css_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$post_id}.css";
380
381 // Reusable CSS
382 $reusable_css = '';
383 $reusable_id = ultimate_post()->reusable_id($post_id);
384 foreach ( $reusable_id as $id ) {
385 $reusable_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$id}.css";
386 if (file_exists( $reusable_dir_path )) {
387 $reusable_css .= file_get_contents($reusable_dir_path);
388 } else {
389 $reusable_css .= get_post_meta($id, '_ultp_css', true);
390 }
391 }
392 if (isset($_GET['preview_id']) && isset($_GET['preview_nonce'])) {
393 $css = get_transient('_ultp_preview_'.$post_id, true);
394 if (!$css) {
395 $css = get_post_meta($post_id, '_ultp_css', true);
396 }
397 if ($css) {
398 if ($reusable_css) {
399 $css = $this->set_top_css($css.$reusable_css);
400 }
401 echo ultimate_post()->set_inline($css);
402 }
403 } else if (file_exists( $css_dir_path )) {
404 $css = file_get_contents($css_dir_path);
405 if ($reusable_css) {
406 $css = $this->set_top_css($css.$reusable_css);
407 }
408 if ($css) {
409 echo ultimate_post()->set_inline($css);
410 }
411 } else {
412 $css = get_post_meta($post_id, '_ultp_css', true);
413 if ($reusable_css) {
414 $css = $this->set_top_css($css.$reusable_css);
415 }
416 if ($css) {
417 echo ultimate_post()->set_inline($css);
418 }
419 }
420 }
421 }
422 }