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