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

368 lines 10.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 }
26
27
28 /**
29 * REST API Action
30 *
31 * @since v.1.0.0
32 * @return NULL
33 */
34 public function save_block_css_callback(){
35 register_rest_route(
36 'ultp/v1',
37 '/save_block_css/',
38 array(
39 array(
40 'methods' => 'POST',
41 'callback' => array( $this, 'save_block_content_css'),
42 'permission_callback' => function () {
43 return current_user_can( 'edit_posts' );
44 },
45 'args' => array()
46 )
47 )
48 );
49 register_rest_route(
50 'ultp/v1',
51 '/get_posts/',
52 array(
53 array(
54 'methods' => 'POST',
55 'callback' => array($this, 'get_posts_call'),
56 'permission_callback' => function () {
57 return current_user_can('edit_posts');
58 },
59 'args' => array()
60 )
61 )
62 );
63 register_rest_route(
64 'ultp/v1',
65 '/appened_css/',
66 array(
67 array(
68 'methods' => 'POST',
69 'callback' => array($this, 'appened_css_call'),
70 'permission_callback' => function () {
71 return current_user_can('edit_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 }
92
93 /**
94 * Get and Set PostX Global Settings
95 *
96 * @since v.2.4.24
97 * @param OBJECT | Request Param of the REST API
98 * @return ARRAY | Array of the Custom Message
99 */
100 public function global_settings_action($server) {
101 $post = $server->get_params();
102 if (isset($post['type'])) {
103 if ($post['type'] == 'set') {
104 update_option('postx_global', $post['data']);
105 return ['success' => true];
106 } else {
107 return ['success' => true, 'data' => get_option('postx_global', [])];
108 }
109 } else {
110 return ['success' => false];
111 }
112 }
113
114 /**
115 * Save Import CSS in the top of the File
116 *
117 * @since v.1.0.0
118 * @param OBJECT | Request Param of the REST API
119 * @return ARRAY/Exception | Array of the Custom Message
120 */
121 public function appened_css_call($server) {
122 global $wp_filesystem;
123 if ( ! $wp_filesystem ) {
124 require_once( ABSPATH . 'wp-admin/includes/file.php' );
125 }
126 $post = $server->get_params();
127 $css = $post['inner_css'];
128 $post_id = (int) sanitize_text_field($post['post_id']);
129 if ($post_id) {
130 $upload_dir_url = wp_upload_dir();
131 $filename = "ultp-css-{$post_id}.css";
132 $dir = trailingslashit($upload_dir_url['basedir']).'ultimate-post/';
133 update_post_meta($post_id, '_wopb_css', $css);
134 WP_Filesystem( false, $upload_dir_url['basedir'], true );
135 if( ! $wp_filesystem->is_dir( $dir ) ) {
136 $wp_filesystem->mkdir( $dir );
137 }
138 if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
139 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post' ));
140 }
141 wp_send_json_success(array('success' => true, 'message' => __('Data retrive done', 'ultimate-post')));
142 } else {
143 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
144 }
145 }
146
147
148 /**
149 * Save Import CSS in the top of the File
150 *
151 * @since v.1.0.0
152 * @param OBJECT | Request Param of the REST API
153 * @return ARRAY/Exception | Array of the Custom Message
154 */
155 public function get_posts_call($server) {
156 $post = $server->get_params();
157 if (isset($post['postId'])) {
158 return array('success' => true, 'data'=> get_post($post['postId'])->post_content, 'message' => __('Data retrive done', 'ultimate-post'));
159 } else {
160 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
161 }
162 }
163
164
165 /**
166 * Save Import CSS in the top of the File
167 *
168 * @since v.1.0.0
169 * @param OBJECT | Request Param of the REST API
170 * @return ARRAY/Exception | Array of the Custom Message
171 */
172 public function save_block_content_css($request){
173 try{
174 global $wp_filesystem;
175 if ( ! $wp_filesystem ) {
176 require_once( ABSPATH . 'wp-admin/includes/file.php' );
177 }
178 $params = $request->get_params();
179 $post_id = sanitize_text_field($params['post_id']);
180 if ($post_id == 'ultp-widget' && $params['has_block']) {
181 update_option($post_id, $params['block_css']);
182 return ['success' => true, 'message' => __('Widget CSS Saved', 'ultimate-post')];
183 }
184
185 $post_id = (int) $post_id;
186 $filename = "ultp-css-{$post_id}.css";
187 $upload_dir_url = wp_upload_dir();
188 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultimate-post/';
189
190 if ($params['has_block']) {
191 update_post_meta($post_id, '_ultp_active', 'yes');
192 $ultp_block_css = $this->set_top_css($params['block_css']);
193
194 // Preview Check
195 if ($params['preview']) {
196 set_transient('_ultp_preview_'.$post_id, $ultp_block_css , 60*60);
197 return ['success' => true];
198 }
199
200 WP_Filesystem( false, $upload_dir_url['basedir'], true );
201 if( ! $wp_filesystem->is_dir( $dir ) ) {
202 $wp_filesystem->mkdir( $dir );
203 }
204 if ( ! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) {
205 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post'));
206 }
207 update_post_meta($post_id, '_ultp_css', $ultp_block_css);
208 return ['success'=>true, 'message'=>__('PostX css file has been updated.', 'ultimate-post')];
209 } else {
210 delete_post_meta($post_id, '_ultp_active');
211 if (file_exists($dir.$filename)) {
212 unlink($dir.$filename);
213 }
214 delete_post_meta($post_id, '_ultp_css');
215 return ['success' => true, 'message' => __('Data Delete Done', 'ultimate-post')];
216 }
217 }catch(Exception $e){
218 return [ 'success'=> false, 'message'=> $e->getMessage() ];
219 }
220 }
221
222
223 /**
224 * Save Import CSS in the top of the File
225 *
226 * @since v.1.0.0
227 * @param STRING | CSS (STRING)
228 * @return STRING | Generated CSS
229 */
230 public function set_top_css($get_css = ''){
231 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
232 $font_exists = substr_count($get_css, $css_url);
233 if ($font_exists){
234 $pattern = sprintf('/%s(.+?)%s/ims', preg_quote($css_url, '/'), preg_quote("');", '/'));
235 if (preg_match_all($pattern, $get_css, $matches)) {
236 $fonts = $matches[0];
237 $get_css = str_replace($fonts, '', $get_css);
238 if( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/' , $get_css, $matche_weight ) ){
239 $weight = array_map( function($val){
240 $process = trim( str_replace( array( 'font-weight',':',';' ) , '', $val ) );
241 if( is_numeric( $process ) ){
242 return $process;
243 }
244 }, $matche_weight[0] );
245 foreach ( $fonts as $key => $val ) {
246 $fonts[$key] = str_replace( "');",'', $val ).':'.implode( ',',$weight )."');";
247 }
248 }
249 $fonts = array_unique($fonts);
250 $get_css = implode('', $fonts).$get_css;
251 }
252 }
253 return $get_css;
254 }
255
256
257 /**
258 * Enqueue CSS in HEAD or as a File
259 *
260 * @since v.1.0.0
261 * @return NULL
262 */
263 public function require_block_css(){
264 $save_as = ultimate_post()->get_setting('css_save_as');
265 $save_as = $save_as ? $save_as : '';
266 if (isset($_GET['preview_id']) && isset($_GET['preview_nonce'])) {
267 add_action('wp_head', array( $this, 'add_block_inline_css' ), 100);
268 } else if ($save_as === 'filesystem') {
269 add_action('wp_enqueue_scripts', array($this, 'add_block_css_file'));
270 } else {
271 add_action('wp_head', array( $this, 'add_block_inline_css' ), 100);
272 }
273
274 add_action('wp_enqueue_scripts', array($this, 'postx_global_css'));
275 add_action('admin_enqueue_scripts', array($this, 'postx_global_css'));
276 }
277
278 /**
279 * Set Global Color Codes
280 *
281 * @since v.1.0.0
282 * @return NULL
283 */
284 public function postx_global_css() {
285 // Preset CSS
286 $global = get_option('postx_global', []);
287 $custom_css = ':root {
288 --preset-color1: '.($global['presetColor1']??'#037fff').';
289 --preset-color2: '.($global['presetColor2']??'#026fe0').';
290 --preset-color3: '.($global['presetColor3']??'#071323').';
291 --preset-color4: '.($global['presetColor4']??'#132133').';
292 --preset-color5: '.($global['presetColor5']??'#34495e').';
293 --preset-color6: '.($global['presetColor6']??'#787676').';
294 --preset-color7: '.($global['presetColor7']??'#f0f2f3').';
295 --preset-color8: '.($global['presetColor8']??'#f8f9fa').';
296 --preset-color9: '.($global['presetColor9']??'#ffffff').';
297 }';
298 wp_register_style( 'wpxpo-global-style', false );
299 wp_enqueue_style( 'wpxpo-global-style' );
300 wp_add_inline_style( 'wpxpo-global-style', $custom_css );
301 }
302
303 /**
304 * Set CSS as File
305 *
306 * @since v.1.0.0
307 * @return NULL
308 */
309 public function add_block_css_file() {
310 ultimate_post()->set_css_style( ultimate_post()->get_ID() );
311 }
312
313
314 /**
315 * Set Inline CSS in Head
316 *
317 * @since v.1.0.0
318 * @return NULL
319 */
320 public function add_block_inline_css(){
321 $post_id = ultimate_post()->get_ID();
322 if( $post_id ){
323 $upload_dir_url = wp_get_upload_dir();
324 $upload_css_dir_url = trailingslashit( $upload_dir_url['basedir'] );
325 $css_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$post_id}.css";
326
327 // Reusable CSS
328 $reusable_css = '';
329 $reusable_id = ultimate_post()->reusable_id($post_id);
330 foreach ( $reusable_id as $id ) {
331 $reusable_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$id}.css";
332 if (file_exists( $reusable_dir_path )) {
333 $reusable_css .= file_get_contents($reusable_dir_path);
334 } else {
335 $reusable_css .= get_post_meta($id, '_ultp_css', true);
336 }
337 }
338 if (isset($_GET['preview_id']) && isset($_GET['preview_nonce'])) {
339 $css = get_transient('_ultp_preview_'.$post_id, true);
340 if (!$css) {
341 $css = get_post_meta($post_id, '_ultp_css', true);
342 }
343 if ($css) {
344 if ($reusable_css) {
345 $css = $this->set_top_css($css.$reusable_css);
346 }
347 echo '<style type="text/css">'.$css.'</style>';
348 }
349 } else if (file_exists( $css_dir_path )) {
350 $css = file_get_contents($css_dir_path);
351 if ($reusable_css) {
352 $css = $this->set_top_css($css.$reusable_css);
353 }
354 if ($css) {
355 echo '<style type="text/css">'.$css.'</style>';
356 }
357 } else {
358 $css = get_post_meta($post_id, '_ultp_css', true);
359 if ($reusable_css) {
360 $css = $this->set_top_css($css.$reusable_css);
361 }
362 if ($css) {
363 echo '<style type="text/css">'.$css.'</style>';
364 }
365 }
366 }
367 }
368 }