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

307 lines 8.6 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 }
78
79
80 /**
81 * Save Import CSS in the top of the File
82 *
83 * @since v.1.0.0
84 * @param OBJECT | Request Param of the REST API
85 * @return ARRAY/Exception | Array of the Custom Message
86 */
87 public function appened_css_call($server) {
88 global $wp_filesystem;
89 if ( ! $wp_filesystem ) {
90 require_once( ABSPATH . 'wp-admin/includes/file.php' );
91 }
92 $post = $server->get_params();
93 $css = $post['inner_css'];
94 $post_id = (int) sanitize_text_field($post['post_id']);
95 if ($post_id) {
96 $upload_dir_url = wp_upload_dir();
97 $filename = "ultp-css-{$post_id}.css";
98 $dir = trailingslashit($upload_dir_url['basedir']).'ultimate-post/';
99 update_post_meta($post_id, '_wopb_css', $css);
100 WP_Filesystem( false, $upload_dir_url['basedir'], true );
101 if( ! $wp_filesystem->is_dir( $dir ) ) {
102 $wp_filesystem->mkdir( $dir );
103 }
104 if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
105 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post' ));
106 }
107 wp_send_json_success(array('success' => true, 'message' => __('Data retrive done', 'ultimate-post')));
108 } else {
109 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
110 }
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 get_posts_call($server) {
122 $post = $server->get_params();
123 if (isset($post['postId'])) {
124 return array('success' => true, 'data'=> get_post($post['postId'])->post_content, 'message' => __('Data retrive done', 'ultimate-post'));
125 } else {
126 return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post'));
127 }
128 }
129
130
131 /**
132 * Save Import CSS in the top of the File
133 *
134 * @since v.1.0.0
135 * @param OBJECT | Request Param of the REST API
136 * @return ARRAY/Exception | Array of the Custom Message
137 */
138 public function save_block_content_css($request){
139 try{
140 global $wp_filesystem;
141 if ( ! $wp_filesystem ) {
142 require_once( ABSPATH . 'wp-admin/includes/file.php' );
143 }
144 $params = $request->get_params();
145 $post_id = sanitize_text_field($params['post_id']);
146 if ($post_id == 'ultp-widget' && $params['has_block']) {
147 update_option($post_id, $params['block_css']);
148 return ['success' => true, 'message' => __('Widget CSS Saved', 'ultimate-post')];
149 }
150
151 $post_id = (int) $post_id;
152 $filename = "ultp-css-{$post_id}.css";
153 $upload_dir_url = wp_upload_dir();
154 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultimate-post/';
155
156 if ($params['has_block']) {
157 update_post_meta($post_id, '_ultp_active', 'yes');
158 $ultp_block_css = $this->set_top_css($params['block_css']);
159
160 // Preview Check
161 if ($params['preview']) {
162 set_transient('_ultp_preview_'.$post_id, $ultp_block_css , 60*60);
163 return ['success' => true];
164 }
165
166 WP_Filesystem( false, $upload_dir_url['basedir'], true );
167 if( ! $wp_filesystem->is_dir( $dir ) ) {
168 $wp_filesystem->mkdir( $dir );
169 }
170 if ( ! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) {
171 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post'));
172 }
173 update_post_meta($post_id, '_ultp_css', $ultp_block_css);
174 return ['success'=>true, 'message'=>__('PostX css file has been updated.', 'ultimate-post')];
175 } else {
176 delete_post_meta($post_id, '_ultp_active');
177 if (file_exists($dir.$filename)) {
178 unlink($dir.$filename);
179 }
180 delete_post_meta($post_id, '_ultp_css');
181 return ['success' => true, 'message' => __('Data Delete Done', 'ultimate-post')];
182 }
183 }catch(Exception $e){
184 return [ 'success'=> false, 'message'=> $e->getMessage() ];
185 }
186 }
187
188
189 /**
190 * Save Import CSS in the top of the File
191 *
192 * @since v.1.0.0
193 * @param STRING | CSS (STRING)
194 * @return STRING | Generated CSS
195 */
196 public function set_top_css($get_css = ''){
197 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
198 $font_exists = substr_count($get_css, $css_url);
199 if ($font_exists){
200 $pattern = sprintf('/%s(.+?)%s/ims', preg_quote($css_url, '/'), preg_quote("');", '/'));
201 if (preg_match_all($pattern, $get_css, $matches)) {
202 $fonts = $matches[0];
203 $get_css = str_replace($fonts, '', $get_css);
204 if( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/' , $get_css, $matche_weight ) ){
205 $weight = array_map( function($val){
206 $process = trim( str_replace( array( 'font-weight',':',';' ) , '', $val ) );
207 if( is_numeric( $process ) ){
208 return $process;
209 }
210 }, $matche_weight[0] );
211 foreach ( $fonts as $key => $val ) {
212 $fonts[$key] = str_replace( "');",'', $val ).':'.implode( ',',$weight )."');";
213 }
214 }
215 $fonts = array_unique($fonts);
216 $get_css = implode('', $fonts).$get_css;
217 }
218 }
219 return $get_css;
220 }
221
222
223 /**
224 * Enqueue CSS in HEAD or as a File
225 *
226 * @since v.1.0.0
227 * @return NULL
228 */
229 public function require_block_css(){
230 $save_as = ultimate_post()->get_setting('css_save_as');
231 $save_as = $save_as ? $save_as : '';
232 if (isset($_GET['preview_id']) && isset($_GET['preview_nonce'])) {
233 add_action('wp_head', array( $this, 'add_block_inline_css' ), 100);
234 } else if ($save_as === 'filesystem') {
235 add_action('wp_enqueue_scripts', array($this, 'add_block_css_file'));
236 } else {
237 add_action('wp_head', array( $this, 'add_block_inline_css' ), 100);
238 }
239 }
240
241
242 /**
243 * Set CSS as File
244 *
245 * @since v.1.0.0
246 * @return NULL
247 */
248 public function add_block_css_file() {
249 ultimate_post()->set_css_style( ultimate_post()->get_ID() );
250 }
251
252
253 /**
254 * Set Inline CSS in Head
255 *
256 * @since v.1.0.0
257 * @return NULL
258 */
259 public function add_block_inline_css(){
260 $post_id = ultimate_post()->get_ID();
261 if( $post_id ){
262 $upload_dir_url = wp_get_upload_dir();
263 $upload_css_dir_url = trailingslashit( $upload_dir_url['basedir'] );
264 $css_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$post_id}.css";
265
266 // Reusable CSS
267 $reusable_css = '';
268 $reusable_id = ultimate_post()->reusable_id($post_id);
269 foreach ( $reusable_id as $id ) {
270 $reusable_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$id}.css";
271 if (file_exists( $reusable_dir_path )) {
272 $reusable_css .= file_get_contents($reusable_dir_path);
273 } else {
274 $reusable_css .= get_post_meta($id, '_ultp_css', true);
275 }
276 }
277 if (isset($_GET['preview_id']) && isset($_GET['preview_nonce'])) {
278 $css = get_transient('_ultp_preview_'.$post_id, true);
279 if (!$css) {
280 $css = get_post_meta($post_id, '_ultp_css', true);
281 }
282 if ($css) {
283 if ($reusable_css) {
284 $css = $this->set_top_css($css.$reusable_css);
285 }
286 echo '<style type="text/css">'.$css.'</style>';
287 }
288 } else if (file_exists( $css_dir_path )) {
289 $css = file_get_contents($css_dir_path);
290 if ($reusable_css) {
291 $css = $this->set_top_css($css.$reusable_css);
292 }
293 if ($css) {
294 echo '<style type="text/css">'.$css.'</style>';
295 }
296 } else {
297 $css = get_post_meta($post_id, '_ultp_css', true);
298 if ($reusable_css) {
299 $css = $this->set_top_css($css.$reusable_css);
300 }
301 if ($css) {
302 echo '<style type="text/css">'.$css.'</style>';
303 }
304 }
305 }
306 }
307 }