| @@ -1,208 +1,859 @@ | ||
| 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 | -defined('ABSPATH') || exit; | |
| 10 | +defined( 'ABSPATH' ) || exit; | |
| 5 | 11 | |
| 12 | +/** | |
| 13 | + * Styles class. | |
| 14 | + * | |
| 15 | + * Handles enqueuing and management of plugin stylesheets for Ultimate Post. | |
| 16 | + * | |
| 17 | + * @package ULTP\Styles | |
| 18 | + * @since 4.0.0 | |
| 19 | + */ | |
| 6 | 20 | class Styles { |
| 7 | - public function __construct(){ | |
| 8 | - $this->require_block_css(); | |
| 9 | - add_action('rest_api_init', array($this, 'save_block_css_callback')); | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * Setup class. | |
| 24 | + * | |
| 25 | + * @since v.1.0.0 | |
| 26 | + */ | |
| 27 | + private $changed_wp_block = ''; | |
| 28 | + | |
| 29 | + public function __construct() { | |
| 30 | + add_action( 'rest_api_init', array( $this, 'rest_api_callback' ) ); | |
| 31 | + add_action( 'wp_ajax_disable_google_font', array( $this, 'disable_google_font_callback' ) ); | |
| 32 | + | |
| 33 | + add_action( 'admin_init', array( $this, 'postx_global_css_callback' ) ); | |
| 34 | + add_action( 'admin_init', array( $this, 'postx_global_css_dependancies' ) ); | |
| 35 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_postx_block_css' ) ); | |
| 36 | + add_filter( 'render_block', array( $this, 'render_block_callback' ), 10, 2 ); // render block to enqueue corresponding css. | |
| 37 | + add_action( 'ultp_enqueue_postx_block_css', array( $this, 'ultp_enqueue_postx_block_css_callback' ), 10, 1 ); // action to enqueue the block css. | |
| 38 | + | |
| 39 | + add_action( 'after_delete_post', array( $this, 'ultp_delete_post_callback' ), 10, 2 ); // Delete Plugin Data CSS file delete Action. | |
| 10 | 40 | } |
| 11 | 41 | |
| 12 | - // API Routes for save CSS | |
| 13 | - public function save_block_css_callback(){ | |
| 42 | + /** | |
| 43 | + * REST API Action | |
| 44 | + * | |
| 45 | + * @since v.1.0.0 | |
| 46 | + * @return void | |
| 47 | + */ | |
| 48 | + public function rest_api_callback() { | |
| 14 | 49 | register_rest_route( |
| 15 | - 'ultp/v1', | |
| 50 | + 'ultp/v1', | |
| 16 | 51 | '/save_block_css/', |
| 17 | 52 | array( |
| 18 | 53 | array( |
| 19 | - 'methods' => 'POST', | |
| 20 | - 'callback' => array( $this, 'save_block_content_css'), | |
| 54 | + 'methods' => 'POST', | |
| 55 | + 'callback' => array( $this, 'save_block_content_css' ), | |
| 21 | 56 | 'permission_callback' => function () { |
| 22 | - return current_user_can( 'edit_posts' ); | |
| 57 | + return current_user_can( 'publish_posts' ); | |
| 23 | 58 | }, |
| 24 | - 'args' => array() | |
| 25 | - ) | |
| 59 | + 'args' => array(), | |
| 60 | + ), | |
| 26 | 61 | ) |
| 27 | 62 | ); |
| 28 | - | |
| 29 | 63 | register_rest_route( |
| 30 | 64 | 'ultp/v1', |
| 31 | - '/get_posts/', | |
| 65 | + '/get_other_post_content/', | |
| 32 | 66 | array( |
| 33 | 67 | array( |
| 34 | - 'methods' => 'POST', | |
| 35 | - 'callback' => array($this, 'get_posts_call'), | |
| 68 | + 'methods' => 'POST', | |
| 69 | + 'callback' => array( $this, 'get_other_post_content_callback' ), | |
| 36 | 70 | 'permission_callback' => function () { |
| 37 | - return current_user_can('edit_posts'); | |
| 71 | + return current_user_can( 'publish_posts' ); | |
| 38 | 72 | }, |
| 39 | - 'args' => array() | |
| 40 | - ) | |
| 73 | + 'args' => array(), | |
| 74 | + ), | |
| 41 | 75 | ) |
| 42 | 76 | ); |
| 43 | - | |
| 44 | 77 | register_rest_route( |
| 45 | 78 | 'ultp/v1', |
| 46 | - '/appened_css/', | |
| 79 | + '/action_option/', | |
| 47 | 80 | array( |
| 48 | 81 | array( |
| 49 | - 'methods' => 'POST', | |
| 50 | - 'callback' => array($this, 'appened_css_call'), | |
| 82 | + 'methods' => 'POST', | |
| 83 | + 'callback' => array( $this, 'global_settings_action' ), | |
| 51 | 84 | 'permission_callback' => function () { |
| 52 | - return current_user_can('edit_posts'); | |
| 85 | + return current_user_can( 'edit_posts' ); | |
| 53 | 86 | }, |
| 54 | - 'args' => array() | |
| 55 | - ) | |
| 87 | + 'args' => array(), | |
| 88 | + ), | |
| 56 | 89 | ) |
| 57 | 90 | ); |
| 91 | + register_rest_route( | |
| 92 | + 'ultp/v1', | |
| 93 | + '/postx_presets/', | |
| 94 | + array( | |
| 95 | + array( | |
| 96 | + 'methods' => 'POST', | |
| 97 | + 'callback' => array( $this, 'postx_presets_callback' ), | |
| 98 | + 'permission_callback' => function () { | |
| 99 | + return current_user_can( 'manage_options' ); // manage_options For patch security issue. | |
| 100 | + }, | |
| 101 | + 'args' => array(), | |
| 102 | + ), | |
| 103 | + ) | |
| 104 | + ); | |
| 105 | + } | |
| 58 | 106 | |
| 59 | - } | |
| 107 | + /** | |
| 108 | + * Save block css corresponding to page id | |
| 109 | + * | |
| 110 | + * @since v.1.0.0 | |
| 111 | + * | |
| 112 | + * @param OBJECT $request Request Param of the REST API. | |
| 113 | + * @return ARRAY/Exception | Array of the Custom Message. | |
| 114 | + * @throws \Exception If CSS cannot be saved due to permission issues. | |
| 115 | + */ | |
| 116 | + public function save_block_content_css( $request ) { | |
| 60 | 117 | |
| 61 | - 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'); | |
| 118 | + $params = $request->get_params(); | |
| 119 | + $post_id = isset( $params['post_id'] ) ? ultimate_post()->ultp_rest_sanitize_params( $params['post_id'] ) : ''; | |
| 120 | + $has_block = isset( $params['has_block'] ) ? rest_sanitize_boolean( $params['has_block'] ) : ''; | |
| 121 | + $is_preview = isset( $params['preview'] ) ? rest_sanitize_boolean( $params['preview'] ) : ''; | |
| 122 | + $is_widget = $post_id == 'ultp-widget'; | |
| 123 | + | |
| 124 | + $cap = ''; | |
| 125 | + if ( $post_id == 'ultp-widget' || get_post_type( $post_id ) == 'wp_template' || get_post_type( $post_id ) == 'wp_template_part' ) { | |
| 126 | + $cap = 'publish_posts'; | |
| 127 | + } | |
| 128 | + if ( ! ultimate_post()->permission_check_for_restapi( is_numeric( $post_id ) ? $post_id : false, $cap ) ) { | |
| 129 | + return; | |
| 130 | + } | |
| 131 | + if ( ! empty( $params['fseTempId'] ) ) { | |
| 132 | + // Delete previously saved fse css/old compatibility | |
| 133 | + $this->ultp_delete_post_callback( str_replace( '//', '__', $params['fseTempId'] ), '' ); | |
| 134 | + } | |
| 135 | + try { | |
| 136 | + $upload_dir_url = wp_upload_dir(); | |
| 137 | + $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'ultimate-post/'; | |
| 138 | + $upload_dir_url = wp_upload_dir(); | |
| 139 | + $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'ultimate-post/'; | |
| 140 | + if ( $has_block ) { | |
| 141 | + | |
| 142 | + $ultp_block_css = $this->set_top_css( $params['block_css'] ); | |
| 143 | + if ( $is_preview ) { | |
| 144 | + set_transient( '_ultp_preview_' . $post_id, $ultp_block_css, 60 * 60 ); | |
| 145 | + return array( | |
| 146 | + 'success' => true, | |
| 147 | + 'preview' => true, | |
| 148 | + ); | |
| 149 | + } | |
| 150 | + | |
| 151 | + global $wp_filesystem; | |
| 152 | + if ( ! $wp_filesystem ) { | |
| 153 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 154 | + } | |
| 155 | + if ( $is_widget ) { | |
| 156 | + update_option( $post_id, $params['block_css'] ); | |
| 157 | + } else { | |
| 158 | + $post_id = (int) $post_id; | |
| 159 | + update_post_meta( $post_id, '_ultp_active', 'yes' ); | |
| 160 | + update_post_meta( $post_id, '_ultp_css', $ultp_block_css ); | |
| 161 | + } | |
| 162 | + ultimate_post()->set_setting( 'save_version', wp_rand( 1, 1000 ) ); | |
| 67 | 163 | $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); | |
| 164 | + | |
| 165 | + WP_Filesystem( false, $upload_dir_url['basedir'], true ); | |
| 166 | + if ( ! $wp_filesystem->is_dir( $dir ) ) { | |
| 167 | + $wp_filesystem->mkdir( $dir ); | |
| 73 | 168 | } |
| 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')); | |
| 169 | + if ( ! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) { | |
| 170 | + throw new \Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post')); //phpcs:ignore | |
| 171 | + } | |
| 172 | + return array( | |
| 173 | + 'success' => true, | |
| 174 | + 'message' => __( 'PostX css file has been updated.', 'ultimate-post' ), | |
| 175 | + ); | |
| 176 | + | |
| 177 | + } else { | |
| 178 | + if ( $is_widget ) { | |
| 179 | + update_option( $post_id, '' ); | |
| 180 | + } else { | |
| 181 | + $post_id = (int) $post_id; | |
| 182 | + delete_post_meta( $post_id, '_ultp_active' ); | |
| 183 | + delete_post_meta( $post_id, '_ultp_css' ); | |
| 184 | + } | |
| 185 | + $filename = "ultp-css-{$post_id}.css"; | |
| 186 | + if ( file_exists( $dir . $filename ) ) { | |
| 187 | + wp_delete_file( $dir . $filename ); | |
| 188 | + } | |
| 189 | + return array( | |
| 190 | + 'success' => true, | |
| 191 | + 'message' => __( 'Data Delete Done', 'ultimate-post' ), | |
| 192 | + ); | |
| 79 | 193 | } |
| 194 | + } catch ( \Exception $e ) { | |
| 195 | + return array( | |
| 196 | + 'success' => false, | |
| 197 | + 'message' => $e->getMessage(), | |
| 198 | + ); | |
| 199 | + } | |
| 80 | 200 | } |
| 81 | 201 | |
| 82 | - public function get_posts_call($server) { | |
| 83 | - $post = $server->get_params(); | |
| 84 | - if (isset($post['postId'])) { | |
| 85 | - return array('success' => true, 'data'=> get_post($post['postId'])->post_content, 'message' => __('Data retrive done', 'ultimate-post')); | |
| 202 | + /** | |
| 203 | + * Get Post Content for other Posts while performing css save | |
| 204 | + * | |
| 205 | + * @since v.4.1.10 | |
| 206 | + * @param OBJECT | $server Request Param of the REST API. | |
| 207 | + * @return ARRAY/Exception | Array of the Custom Message. | |
| 208 | + */ | |
| 209 | + public function get_other_post_content_callback( $server ) { | |
| 210 | + $post = $server->get_params(); | |
| 211 | + $post_id = isset( $post['postId'] ) ? ultimate_post()->ultp_rest_sanitize_params( $post['postId'] ) : ''; | |
| 212 | + $p_type = get_post_type( $post_id ); | |
| 213 | + | |
| 214 | + if ( $post_id && | |
| 215 | + ( | |
| 216 | + 'wp_template_part' === $p_type || | |
| 217 | + 'wp_block' === $p_type || | |
| 218 | + ultimate_post()->permission_check_for_restapi( $post_id ) | |
| 219 | + ) | |
| 220 | + ) { | |
| 221 | + if ( 'wp_block' === $p_type ) { | |
| 222 | + $this->handle_wpblock_current_id( $post_id ); | |
| 223 | + } | |
| 224 | + return array( | |
| 225 | + 'success' => true, | |
| 226 | + 'data' => get_post( $post_id )->post_content, | |
| 227 | + 'message' => __( 'Data retrive done', 'ultimate-post' ), | |
| 228 | + ); | |
| 86 | 229 | } else { |
| 87 | - return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post')); | |
| 230 | + return array( | |
| 231 | + 'success' => false, | |
| 232 | + 'message' => __( 'Data not found!!', 'ultimate-post' ), | |
| 233 | + ); | |
| 88 | 234 | } |
| 89 | 235 | } |
| 90 | 236 | |
| 91 | - // Save CSS Action in File | |
| 92 | - public function save_block_content_css($request){ | |
| 93 | - try{ | |
| 94 | - global $wp_filesystem; | |
| 95 | - if ( ! $wp_filesystem ) { | |
| 96 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * Get and Set PostX Global Settings | |
| 240 | + * | |
| 241 | + * @since v.2.4.24 | |
| 242 | + * @param OBJECT | $server Request Param of the REST API. | |
| 243 | + * @return ARRAY | Array of the Custom Message. | |
| 244 | + */ | |
| 245 | + public function global_settings_action( $server ) { | |
| 246 | + $post = $server->get_params(); | |
| 247 | + $_type = isset( $post['type'] ) ? ultimate_post()->ultp_rest_sanitize_params( $post['type'] ) : ''; | |
| 248 | + if ( $_type && ultimate_post()->permission_check_for_restapi() ) { | |
| 249 | + if ( $_type == 'set' ) { | |
| 250 | + if ( current_user_can( 'edit_others_posts' ) ) { | |
| 251 | + update_option( 'postx_global', ultimate_post()->ultp_rest_sanitize_params( $post['data'] ) ); | |
| 252 | + } | |
| 253 | + return array( 'success' => true ); | |
| 254 | + } else { | |
| 255 | + return array( | |
| 256 | + 'success' => true, | |
| 257 | + 'data' => get_option( 'postx_global', array() ), | |
| 258 | + ); | |
| 97 | 259 | } |
| 260 | + } else { | |
| 261 | + return array( 'success' => false ); | |
| 262 | + } | |
| 263 | + } | |
| 98 | 264 | |
| 99 | - $params = $request->get_params(); | |
| 100 | - $post_id = (int) sanitize_text_field($params['post_id']); | |
| 101 | - $filename = "ultp-css-{$post_id}.css"; | |
| 102 | - $upload_dir_url = wp_upload_dir(); | |
| 103 | - $dir = trailingslashit($upload_dir_url['basedir']) . 'ultimate-post/'; | |
| 265 | + /** | |
| 266 | + * Get and Set PostX Presets Settings | |
| 267 | + * | |
| 268 | + * @since v.2.4.24 | |
| 269 | + * @param OBJECT | $server Request Param of the REST API. | |
| 270 | + * @return ARRAY | Array of the Custom Message. | |
| 271 | + */ | |
| 272 | + public function postx_presets_callback( $server ) { | |
| 273 | + $post = $server->get_params(); | |
| 274 | + $type = isset( $post['type'] ) ? sanitize_text_field( $post['type'] ) : ''; | |
| 275 | + $key = isset( $post['key'] ) ? sanitize_text_field( $post['key'] ) : ''; | |
| 276 | + $data = isset( $post['data'] ) ? $post['data'] : ''; | |
| 104 | 277 | |
| 105 | - if ($params['has_block']) { | |
| 106 | - update_post_meta($post_id, '_ultp_active', 'yes'); | |
| 107 | - $ultp_block_css = $this->set_top_css($params['block_css']); | |
| 108 | - WP_Filesystem( false, $upload_dir_url['basedir'], true ); | |
| 109 | - if( ! $wp_filesystem->is_dir( $dir ) ) { | |
| 110 | - $wp_filesystem->mkdir( $dir ); | |
| 278 | + if ( $type ) { | |
| 279 | + if ( $type == 'set' ) { | |
| 280 | + if ( current_user_can( 'edit_others_posts' ) ) { | |
| 281 | + update_option( $key, $data ); | |
| 111 | 282 | } |
| 112 | - if ( ! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) { | |
| 113 | - throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post')); | |
| 114 | - } | |
| 115 | - 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')]; | |
| 283 | + return array( 'success' => true ); | |
| 117 | 284 | } else { |
| 118 | - delete_post_meta($post_id, '_ultp_active'); | |
| 119 | - if (file_exists($dir.$filename)) { | |
| 120 | - unlink($dir.$filename); | |
| 285 | + return array( | |
| 286 | + 'success' => true, | |
| 287 | + 'data' => get_option( $key, array() ), | |
| 288 | + ); | |
| 289 | + } | |
| 290 | + } else { | |
| 291 | + return array( 'success' => false ); | |
| 292 | + } | |
| 293 | + } | |
| 294 | + | |
| 295 | + /** | |
| 296 | + * Disable Google Font Callback | |
| 297 | + * | |
| 298 | + * * @since v.2.8.1 | |
| 299 | + * | |
| 300 | + * @return STRING | |
| 301 | + */ | |
| 302 | + public function disable_google_font_callback() { | |
| 303 | + if ( ! ( isset( $_REQUEST['wpnonce'] ) && | |
| 304 | + wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) | |
| 305 | + ) { | |
| 306 | + return; | |
| 307 | + } | |
| 308 | + if ( ! ultimate_post()->permission_check_for_restapi() ) { | |
| 309 | + return; | |
| 310 | + } | |
| 311 | + | |
| 312 | + global $wp_filesystem; | |
| 313 | + if ( ! $wp_filesystem ) { | |
| 314 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 315 | + WP_Filesystem(); | |
| 316 | + } | |
| 317 | + | |
| 318 | + $upload_dir_url = wp_upload_dir(); | |
| 319 | + $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'ultimate-post/'; | |
| 320 | + $css_dir = glob( $dir . '*.css' ); | |
| 321 | + | |
| 322 | + // Custom Font. | |
| 323 | + $custom_fonts = array(); | |
| 324 | + if ( ultimate_post()->get_setting( 'ultp_custom_font' ) == 'true' ) { | |
| 325 | + $args = array( | |
| 326 | + 'post_type' => 'ultp_custom_font', | |
| 327 | + 'post_status' => 'publish', | |
| 328 | + 'numberposts' => -1, | |
| 329 | + 'order' => 'ASC', | |
| 330 | + ); | |
| 331 | + $posts = get_posts( $args ); | |
| 332 | + if ( $posts ) { | |
| 333 | + foreach ( $posts as $post ) { | |
| 334 | + if ( ! empty( $post->post_title ) ) { | |
| 335 | + $custom_fonts[] = $post->post_title; | |
| 336 | + } | |
| 121 | 337 | } |
| 122 | - delete_post_meta($post_id, '_ultp_css'); | |
| 123 | 338 | } |
| 124 | - }catch(Exception $e){ | |
| 125 | - return [ 'success'=> false, 'message'=> $e->getMessage() ]; | |
| 126 | - } | |
| 339 | + } | |
| 340 | + wp_reset_postdata(); | |
| 341 | + $custom_fonts = implode( '|', $custom_fonts ); | |
| 342 | + // system font. | |
| 343 | + $exclude_typo = implode( '|', array( 'Arial', 'Tahoma', 'Verdana', 'Helvetica', 'Times New Roman', 'Trebuchet MS', 'Georgia' ) ); | |
| 344 | + | |
| 345 | + if ( count( $css_dir ) > 0 ) { | |
| 346 | + foreach ( $css_dir as $key => $value ) { | |
| 347 | + $css = $wp_filesystem->get_contents( $value ); | |
| 348 | + $filter_css = preg_replace( '/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $css ); // Remove Import Font. | |
| 349 | + $final_css = preg_replace( '/(font-family:)((?!' . $custom_fonts . $exclude_typo . ')[\w\s:,\\\'-])*;/mi', '', $filter_css ); // Font Replace Except Default Font | |
| 350 | + $wp_filesystem->put_contents( $value, $final_css ); // Update CSS File. | |
| 351 | + } | |
| 352 | + } | |
| 353 | + | |
| 354 | + global $wpdb; | |
| 355 | + $results = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE `meta_key`='_ultp_css'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 356 | + if ( ! empty( $results ) ) { | |
| 357 | + foreach ( $results as $key => $value ) { | |
| 358 | + $filter_css = preg_replace( '/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $value->meta_value ); // Remove Import Font. | |
| 359 | + $final_css = preg_replace( '/(font-family:)((?!' . $custom_fonts . $exclude_typo . ')[\w\s:,\\\'-])*;/mi', '', $filter_css ); // Font Replace Except Default Font. | |
| 360 | + update_post_meta( $value->post_id, '_ultp_css', $final_css ); | |
| 361 | + } | |
| 362 | + } | |
| 363 | + | |
| 364 | + return wp_send_json_success( __( 'CSS Updated!', 'ultimate-post' ) ); | |
| 127 | 365 | } |
| 128 | 366 | |
| 129 | - // Save Import CSS in the top of the File | |
| 130 | - public function set_top_css($get_css = ''){ | |
| 131 | - $css_url = "@import url('https://fonts.googleapis.com/css?family="; | |
| 132 | - $font_exists = substr_count($get_css, $css_url); | |
| 133 | - if ($font_exists){ | |
| 134 | - $pattern = sprintf('/%s(.+?)%s/ims', preg_quote($css_url, '/'), preg_quote("');", '/')); | |
| 135 | - if (preg_match_all($pattern, $get_css, $matches)) { | |
| 136 | - $fonts = $matches[0]; | |
| 137 | - $get_css = str_replace($fonts, '', $get_css); | |
| 138 | - if( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/' , $get_css, $matche_weight ) ){ | |
| 139 | - $weight = array_map( function($val){ | |
| 140 | - $process = trim( str_replace( array( 'font-weight',':',';' ) , '', $val ) ); | |
| 141 | - if( is_numeric( $process ) ){ | |
| 142 | - return $process; | |
| 367 | + /** | |
| 368 | + * Check global style loaded or not | |
| 369 | + * | |
| 370 | + * @since 4.0.0 | |
| 371 | + * @return NULL | |
| 372 | + */ | |
| 373 | + public function postx_global_css_dependancies() { | |
| 374 | + $this->postx_global_css_callback(); | |
| 375 | + $wp_styles = wp_styles(); | |
| 376 | + $style = $wp_styles->query( 'wp-block-library', 'registered' ); | |
| 377 | + if ( ! $style ) { | |
| 378 | + return; | |
| 379 | + } | |
| 380 | + $array = array( 'wpxpo-global-style', 'ultp-preset-colors-style', 'ultp-preset-gradient-style', 'ultp-preset-typo-style' ); | |
| 381 | + foreach ( $array as $arr ) { | |
| 382 | + if ( wp_style_is( $arr, 'registered' ) && ! in_array( $arr, $style->deps, true ) ) { | |
| 383 | + $style->deps[] = $arr; | |
| 384 | + } | |
| 385 | + } | |
| 386 | + } | |
| 387 | + | |
| 388 | + /** | |
| 389 | + * Set Global Color Codes | |
| 390 | + * | |
| 391 | + * @since v.1.0.0 | |
| 392 | + * @return void | |
| 393 | + */ | |
| 394 | + public function postx_global_css_callback() { | |
| 395 | + // Preset CSS | |
| 396 | + $global = get_option( 'postx_global', array() ); | |
| 397 | + $custom_css = ':root { | |
| 398 | + --preset-color1: ' . ( isset( $global['presetColor1'] ) ? $global['presetColor1'] : '#037fff' ) . '; | |
| 399 | + --preset-color2: ' . ( isset( $global['presetColor2'] ) ? $global['presetColor2'] : '#026fe0' ) . '; | |
| 400 | + --preset-color3: ' . ( isset( $global['presetColor3'] ) ? $global['presetColor3'] : '#071323' ) . '; | |
| 401 | + --preset-color4: ' . ( isset( $global['presetColor4'] ) ? $global['presetColor4'] : '#132133' ) . '; | |
| 402 | + --preset-color5: ' . ( isset( $global['presetColor5'] ) ? $global['presetColor5'] : '#34495e' ) . '; | |
| 403 | + --preset-color6: ' . ( isset( $global['presetColor6'] ) ? $global['presetColor6'] : '#787676' ) . '; | |
| 404 | + --preset-color7: ' . ( isset( $global['presetColor7'] ) ? $global['presetColor7'] : '#f0f2f3' ) . '; | |
| 405 | + --preset-color8: ' . ( isset( $global['presetColor8'] ) ? $global['presetColor8'] : '#f8f9fa' ) . '; | |
| 406 | + --preset-color9: ' . ( isset( $global['presetColor9'] ) ? $global['presetColor9'] : '#ffffff' ) . '; | |
| 407 | + }'; | |
| 408 | + $theme_css = isset( $global['globalCSS'] ) && $global['globalCSS'] ? $global['globalCSS'] : $custom_css . '{}'; | |
| 409 | + | |
| 410 | + wp_register_style( 'wpxpo-global-style', false, array(), ULTP_VER ); | |
| 411 | + wp_enqueue_style( 'wpxpo-global-style' ); | |
| 412 | + wp_add_inline_style( 'wpxpo-global-style', $theme_css ); | |
| 413 | + | |
| 414 | + // preset Colors. | |
| 415 | + $ultpPresetColors = get_option( 'ultpPresetColors', array() ); | |
| 416 | + | |
| 417 | + $rootCSS = ( isset( $ultpPresetColors['rootCSS'] ) && $ultpPresetColors['rootCSS'] ) ? $ultpPresetColors['rootCSS'] : ':root { --postx_preset_Base_1_color: #f4f4ff; --postx_preset_Base_2_color: #dddff8; --postx_preset_Base_3_color: #B4B4D6; --postx_preset_Primary_color: #3323f0; --postx_preset_Secondary_color: #4a5fff; --postx_preset_Tertiary_color: #FFFFFF; --postx_preset_Contrast_3_color: #545472; --postx_preset_Contrast_2_color: #262657; --postx_preset_Contrast_1_color: #10102e; --postx_preset_Over_Primary_color: #ffffff; }'; | |
| 418 | + $savedDLMode = ( isset( $global['enableDark'] ) && $global['enableDark'] ) ? 'ultpdark' : 'ultplight'; | |
| 419 | + $localDLMode = isset( $_COOKIE['ultplocalDLMode'] ) ? $_COOKIE['ultplocalDLMode'] : $savedDLMode; | |
| 420 | + | |
| 421 | + if ( $localDLMode == 'ultplight' && $savedDLMode == 'ultpdark' ) { | |
| 422 | + $rootCSS = $this->handle_dark_light_color_switcher( $rootCSS ); | |
| 423 | + } elseif ( $localDLMode == 'ultpdark' ) { | |
| 424 | + if ( $savedDLMode == 'ultplight' ) { | |
| 425 | + $rootCSS = $this->handle_dark_light_color_switcher( $rootCSS ); | |
| 426 | + } | |
| 427 | + $rootCSS = $rootCSS . ' .ultp-dark-logo.wp-block-site-logo img {content: url("' . get_option( 'ultp_site_dark_logo', '' ) . '");}'; | |
| 428 | + } | |
| 429 | + | |
| 430 | + wp_register_style( 'ultp-preset-colors-style', false, array(), ULTP_VER ); | |
| 431 | + wp_enqueue_style( 'ultp-preset-colors-style' ); | |
| 432 | + wp_add_inline_style( 'ultp-preset-colors-style', $rootCSS ); | |
| 433 | + | |
| 434 | + // preset Gradients. | |
| 435 | + $ultpPresetGradients = get_option( 'ultpPresetGradients', array() ); | |
| 436 | + wp_register_style( 'ultp-preset-gradient-style', false, array(), ULTP_VER ); | |
| 437 | + wp_enqueue_style( 'ultp-preset-gradient-style' ); | |
| 438 | + wp_add_inline_style( 'ultp-preset-gradient-style', isset( $ultpPresetGradients['rootCSS'] ) && $ultpPresetGradients['rootCSS'] ? $ultpPresetGradients['rootCSS'] : ':root { --postx_preset_Primary_to_Secondary_to_Right_gradient: linear-gradient(90deg, var(--postx_preset_Primary_color) 0%, var(--postx_preset_Secondary_color) 100%); --postx_preset_Primary_to_Secondary_to_Bottom_gradient: linear-gradient(180deg, var(--postx_preset_Primary_color) 0%, var(--postx_preset_Secondary_color) 100%); --postx_preset_Secondary_to_Primary_to_Right_gradient: linear-gradient(90deg, var(--postx_preset_Secondary_color) 0%, var(--postx_preset_Primary_color) 100%); --postx_preset_Secondary_to_Primary_to_Bottom_gradient: linear-gradient(180deg, var(--postx_preset_Secondary_color) 0%, var(--postx_preset_Primary_color) 100%); --postx_preset_Cold_Evening_gradient: linear-gradient(0deg, rgb(12, 52, 131) 0%, rgb(162, 182, 223) 100%, rgb(107, 140, 206) 100%, rgb(162, 182, 223) 100%); --postx_preset_Purple_Division_gradient: linear-gradient(0deg, rgb(112, 40, 228) 0%, rgb(229, 178, 202) 100%); --postx_preset_Over_Sun_gradient: linear-gradient(60deg, rgb(171, 236, 214) 0%, rgb(251, 237, 150) 100%); --postx_preset_Morning_Salad_gradient: linear-gradient(-255deg, rgb(183, 248, 219) 0%, rgb(80, 167, 194) 100%); --postx_preset_Fabled_Sunset_gradient: linear-gradient(-270deg, rgb(35, 21, 87) 0%, rgb(68, 16, 122) 29%, rgb(255, 19, 97) 67%, rgb(255, 248, 0) 100%); }' ); | |
| 439 | + | |
| 440 | + // preset Typos. | |
| 441 | + $ultpPresetTypos = get_option( 'ultpPresetTypos', array() ); | |
| 442 | + wp_register_style( 'ultp-preset-typo-style', false, array(), ULTP_VER ); | |
| 443 | + wp_enqueue_style( 'ultp-preset-typo-style' ); | |
| 444 | + wp_add_inline_style( 'ultp-preset-typo-style', isset( $ultpPresetTypos['presetTypoCSS'] ) && $ultpPresetTypos['presetTypoCSS'] ? $ultpPresetTypos['presetTypoCSS'] : ':root { --postx_preset_Heading_typo_font_family: Helvetica; --postx_preset_Heading_typo_font_family_type: sans-serif; --postx_preset_Heading_typo_font_weight: 600; --postx_preset_Heading_typo_text_transform: capitalize; --postx_preset_Body_and_Others_typo_font_family: Helvetica; --postx_preset_Body_and_Others_typo_font_family_type: sans-serif; --postx_preset_Body_and_Others_typo_font_weight: 400; --postx_preset_Body_and_Others_typo_text_transform: lowercase; --postx_preset_body_typo_font_size_lg: 16px; --postx_preset_paragraph_1_typo_font_size_lg: 12px; --postx_preset_paragraph_2_typo_font_size_lg: 12px; --postx_preset_paragraph_3_typo_font_size_lg: 12px; --postx_preset_heading_h1_typo_font_size_lg: 42px; --postx_preset_heading_h2_typo_font_size_lg: 36px; --postx_preset_heading_h3_typo_font_size_lg: 30px; --postx_preset_heading_h4_typo_font_size_lg: 24px; --postx_preset_heading_h5_typo_font_size_lg: 20px; --postx_preset_heading_h6_typo_font_size_lg: 16px; }' ); | |
| 445 | + } | |
| 446 | + | |
| 447 | + /** | |
| 448 | + * Enqueue The Block Style | |
| 449 | + * | |
| 450 | + * * @since v.4.1.8 | |
| 451 | + * | |
| 452 | + * @return void | |
| 453 | + */ | |
| 454 | + public function enqueue_postx_block_css() { | |
| 455 | + $this->postx_global_css_callback(); | |
| 456 | + if ( apply_filters( 'postx_common_script', false ) || | |
| 457 | + isset( $_GET['et_fb'] ) // divi theme issue. | |
| 458 | + ) { | |
| 459 | + ultimate_post()->register_scripts_common(); | |
| 460 | + } | |
| 461 | + if ( is_admin() ) { | |
| 462 | + return; | |
| 463 | + } | |
| 464 | + if ( wp_is_block_theme() ) { | |
| 465 | + $this->handle_old_fse_css(); | |
| 466 | + $this->handle_fse_template_parts_css(); | |
| 467 | + } | |
| 468 | + $css = ''; | |
| 469 | + $post_id = ultimate_post()->get_ID(); | |
| 470 | + if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { // @codingStandardsIgnoreLine. | |
| 471 | + $css = get_transient( '_ultp_preview_' . $post_id, true ); | |
| 472 | + if ( ! $css ) { | |
| 473 | + $css = get_post_meta( $post_id, '_ultp_css', true ); | |
| 474 | + } | |
| 475 | + } | |
| 476 | + do_action( | |
| 477 | + 'ultp_enqueue_postx_block_css', | |
| 478 | + array( | |
| 479 | + 'post_id' => $post_id, | |
| 480 | + 'css' => $css, | |
| 481 | + ) | |
| 482 | + ); | |
| 483 | + } | |
| 484 | + | |
| 485 | + | |
| 486 | + /** | |
| 487 | + * Enqueue The Block Style based on block( wp_block, fse_template, wp_template, wp_template_part ) | |
| 488 | + * | |
| 489 | + * @since v.4.1.8 | |
| 490 | + * | |
| 491 | + * @param STRING $block_content The block content. | |
| 492 | + * @param ARRAY $block The block data array. | |
| 493 | + * @return void | |
| 494 | + */ | |
| 495 | + public function render_block_callback( $block_content, $block ) { | |
| 496 | + // ultimate-post/social | |
| 497 | + $block_list = array( | |
| 498 | + 'ultimate-post/menu', | |
| 499 | + 'ultimate-post/menu-item', | |
| 500 | + 'ultimate-post/accordion-item', | |
| 501 | + 'ultimate-post/tabs', | |
| 502 | + 'ultimate-post/gallery', | |
| 503 | + 'ultimate-post/social', | |
| 504 | + 'ultimate-post/button', | |
| 505 | + 'ultimate-post/list', | |
| 506 | + 'ultimate-post/table-of-content' | |
| 507 | + ); | |
| 508 | + | |
| 509 | + if ( isset( $block['blockName'] ) ) { | |
| 510 | + if ( in_array($block['blockName'], $block_list) ) { | |
| 511 | + switch ($block['blockName']) { | |
| 512 | + case 'ultimate-post/menu': | |
| 513 | + $start = '_ultp_mn_ic_'; | |
| 514 | + $end = '_ultp_mn_ic_end_'; | |
| 515 | + break; | |
| 516 | + case 'ultimate-post/menu-item': | |
| 517 | + $start = '_ultp_mi_ic_'; | |
| 518 | + $end = '_ultp_mi_ic_end_'; | |
| 519 | + break; | |
| 520 | + case 'ultimate-post/accordion-item': | |
| 521 | + $start = '_ultp_aci_ic_'; | |
| 522 | + $end = '_ultp_aci_ic_end_'; | |
| 523 | + break; | |
| 524 | + case 'ultimate-post/tabs': | |
| 525 | + $start = '_ultp_tb_ic_'; | |
| 526 | + $end = '_ultp_tb_ic_end_'; | |
| 527 | + break; | |
| 528 | + case 'ultimate-post/gallery': | |
| 529 | + $start = '_ultp_gl_ic_'; | |
| 530 | + $end = '_ultp_gl_ic_end_'; | |
| 531 | + break; | |
| 532 | + case 'ultimate-post/social': | |
| 533 | + $start = '_ultp_sc_ic_'; | |
| 534 | + $end = '_ultp_sc_ic_end_'; | |
| 535 | + break; | |
| 536 | + case 'ultimate-post/button': | |
| 537 | + $start = '_ultp_btn_ic_'; | |
| 538 | + $end = '_ultp_btn_ic_end_'; | |
| 539 | + break; | |
| 540 | + case 'ultimate-post/list': | |
| 541 | + $start = '_ultp_list_ic_'; | |
| 542 | + $end = '_ultp_list_ic_end_'; | |
| 543 | + break; | |
| 544 | + case 'ultimate-post/table-of-content': | |
| 545 | + $start = '_ultp_toc_ic_'; | |
| 546 | + $end = '_ultp_toc_ic_end_'; | |
| 547 | + break; | |
| 548 | + } | |
| 549 | + // ultimate-post/list | |
| 550 | + $pattern = '/' . $start . '(.*?)' . $end . '/'; | |
| 551 | + preg_match_all( $pattern, $block_content, $matches ); | |
| 552 | + if ( is_array( $matches[1] ) ) { | |
| 553 | + foreach ( $matches[1] as $m ) { | |
| 554 | + if ( $m ) { | |
| 555 | + $block_content = str_replace( $start . $m . $end, ultimate_post()->get_svg_icon( $m ), $block_content ); | |
| 143 | 556 | } |
| 144 | - }, $matche_weight[0] ); | |
| 145 | - foreach ( $fonts as $key => $val ) { | |
| 146 | - $fonts[$key] = str_replace( "');",'', $val ).':'.implode( ',',$weight )."');"; | |
| 147 | 557 | } |
| 148 | 558 | } |
| 149 | - $fonts = array_unique($fonts); | |
| 150 | - $get_css = implode('', $fonts).$get_css; | |
| 151 | 559 | } |
| 560 | + if ( | |
| 561 | + $block['blockName'] == 'ultimate-post/mega-menu' && | |
| 562 | + ! ultimate_post()->is_lc_active() | |
| 563 | + ) { | |
| 564 | + return ''; | |
| 565 | + } | |
| 152 | 566 | } |
| 153 | - return $get_css; | |
| 567 | + if ( ! is_admin() && | |
| 568 | + isset( $block['blockName'] ) && | |
| 569 | + $block['blockName'] === 'core/block' && | |
| 570 | + ! empty( $block['attrs']['ref'] ) | |
| 571 | + ) { | |
| 572 | + // Validate ref_id is a positive integer | |
| 573 | + $ref_id = absint( $block['attrs']['ref'] ); | |
| 574 | + if ( $ref_id < 1 ) { | |
| 575 | + return $block_content; | |
| 576 | + } | |
| 577 | + | |
| 578 | + if ( get_post_type( $ref_id ) === 'wp_block' && | |
| 579 | + get_post_meta( $ref_id, '_ultp_active', true ) === 'yes' | |
| 580 | + ) { | |
| 581 | + do_action( | |
| 582 | + 'ultp_enqueue_postx_block_css', | |
| 583 | + array( | |
| 584 | + 'post_id' => $ref_id, | |
| 585 | + 'css' => '', | |
| 586 | + ) | |
| 587 | + ); | |
| 588 | + } | |
| 589 | + } | |
| 590 | + if ( ! is_admin() && | |
| 591 | + isset( $block['blockName'] ) && | |
| 592 | + strpos( $block['blockName'], 'ultimate-post/' ) === 0 | |
| 593 | + && ! empty( $block['attrs']['currentPostId'] ) | |
| 594 | + ) { | |
| 595 | + do_action( | |
| 596 | + 'ultp_enqueue_postx_block_css', | |
| 597 | + array( | |
| 598 | + 'post_id' => $block['attrs']['currentPostId'], | |
| 599 | + 'css' => '', | |
| 600 | + ) | |
| 601 | + ); | |
| 602 | + } | |
| 603 | + return $block_content; | |
| 154 | 604 | } |
| 155 | 605 | |
| 606 | + /** | |
| 607 | + * Enqueue The Block Style | |
| 608 | + * | |
| 609 | + * @since v.4.1.8 | |
| 610 | + * | |
| 611 | + * @param ARRAY $data The data array containing post_id and css. | |
| 612 | + * @return NULL | |
| 613 | + */ | |
| 614 | + public function ultp_enqueue_postx_block_css_callback( $data ) { | |
| 615 | + $post_id = isset( $data['post_id'] ) ? $data['post_id'] : ''; | |
| 616 | + $css = isset( $data['css'] ) ? $data['css'] : ''; | |
| 617 | + if ( wp_style_is( "ultp-post-{$post_id}", 'enqueued' ) ) { | |
| 618 | + return; | |
| 619 | + } | |
| 156 | 620 | |
| 157 | - // Require CSS | |
| 158 | - 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); | |
| 621 | + if ( $post_id ) { | |
| 622 | + if ( $css == '' ) { | |
| 623 | + global $wp_filesystem; | |
| 624 | + if ( ! $wp_filesystem ) { | |
| 625 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 626 | + } | |
| 627 | + WP_Filesystem(); | |
| 628 | + $upload_dir_url = wp_upload_dir(); | |
| 629 | + $_path = trailingslashit( $upload_dir_url['basedir'] ) . "ultimate-post/ultp-css-{$post_id}.css"; | |
| 630 | + $css = ''; | |
| 631 | + if ( file_exists( $_path ) ) { | |
| 632 | + $css = $wp_filesystem->get_contents( $_path ); | |
| 633 | + } elseif ( $post_id == 'ultp-widget' ) { | |
| 634 | + $css = get_option( $post_id, true ); | |
| 635 | + } else { | |
| 636 | + $css = get_post_meta( $post_id, '_ultp_css', true ); | |
| 637 | + } | |
| 166 | 638 | } |
| 639 | + if ( $css ) { | |
| 640 | + ultimate_post()->register_scripts_common(); | |
| 641 | + wp_register_style( "ultp-post-{$post_id}", false ); | |
| 642 | + wp_enqueue_style( "ultp-post-{$post_id}" ); | |
| 643 | + wp_add_inline_style( "ultp-post-{$post_id}", $css ); | |
| 644 | + } | |
| 167 | 645 | } |
| 168 | 646 | } |
| 169 | 647 | |
| 648 | + /** | |
| 649 | + * Enqueue CSS for FSE template parts (header, footer, etc.) that contain PostX blocks. | |
| 650 | + * Must run during wp_enqueue_scripts (before wp_head) so styles land in <head>. | |
| 651 | + * | |
| 652 | + * @since v.4.1.8 | |
| 653 | + * @return void | |
| 654 | + */ | |
| 655 | + public function handle_fse_template_parts_css() { | |
| 656 | + $parts = get_posts( | |
| 657 | + array( | |
| 658 | + 'post_type' => 'wp_template_part', | |
| 659 | + 'meta_query' => array( | |
| 660 | + array( | |
| 661 | + 'key' => '_ultp_active', | |
| 662 | + 'value' => 'yes', | |
| 663 | + ), | |
| 664 | + ), | |
| 665 | + 'posts_per_page' => -1, | |
| 666 | + 'fields' => 'ids', | |
| 667 | + ) | |
| 668 | + ); | |
| 669 | + | |
| 670 | + if ( empty( $parts ) ) { | |
| 671 | + return; | |
| 672 | + } | |
| 673 | + | |
| 674 | + $upload_dir = wp_upload_dir(); | |
| 675 | + $dir = trailingslashit( $upload_dir['basedir'] ) . 'ultimate-post/'; | |
| 676 | + | |
| 677 | + foreach ( $parts as $part_id ) { | |
| 678 | + // Validate post ID is numeric | |
| 679 | + $part_id = absint( $part_id ); | |
| 680 | + if ( $part_id < 1 ) { | |
| 681 | + continue; | |
| 682 | + } | |
| 683 | + | |
| 684 | + $css_file = $dir . "ultp-css-{$part_id}.css"; | |
| 685 | + | |
| 686 | + if ( file_exists( $css_file ) ) { | |
| 687 | + do_action( | |
| 688 | + 'ultp_enqueue_postx_block_css', | |
| 689 | + array( | |
| 690 | + 'post_id' => $part_id, | |
| 691 | + 'css' => '', | |
| 692 | + ) | |
| 693 | + ); | |
| 694 | + } | |
| 695 | + } | |
| 696 | + } | |
| 170 | 697 | |
| 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); | |
| 698 | + /** | |
| 699 | + * Enqueue The Block Style for old saved fse | |
| 700 | + * | |
| 701 | + * * @since v.4.1.8 | |
| 702 | + * | |
| 703 | + * @return void | |
| 704 | + */ | |
| 705 | + public function handle_old_fse_css() { | |
| 706 | + global $_wp_current_template_id; | |
| 707 | + if ( isset( $_wp_current_template_id ) ) { | |
| 708 | + $template_id = str_replace( '//', '__', $_wp_current_template_id ); | |
| 709 | + $upload_dir_url = wp_upload_dir(); | |
| 710 | + $_path = trailingslashit( $upload_dir_url['basedir'] ) . "ultimate-post/ultp-css-{$template_id}.css"; | |
| 711 | + if ( file_exists( $_path ) ) { | |
| 712 | + do_action( | |
| 713 | + 'ultp_enqueue_postx_block_css', | |
| 714 | + array( | |
| 715 | + 'post_id' => $template_id, | |
| 716 | + 'css' => '', | |
| 717 | + ) | |
| 718 | + ); | |
| 719 | + } | |
| 720 | + } | |
| 175 | 721 | } |
| 176 | 722 | |
| 177 | 723 | |
| 178 | - // Set Inline CSS in Head | |
| 179 | - public function add_block_inline_css(){ | |
| 180 | - $post_id = get_the_ID(); | |
| 181 | - if( $post_id ){ | |
| 182 | - $upload_dir_url = wp_get_upload_dir(); | |
| 183 | - $upload_css_dir_url = trailingslashit( $upload_dir_url['basedir'] ); | |
| 184 | - $css_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$post_id}.css"; | |
| 724 | + /** | |
| 725 | + * Handle WP Block postid | |
| 726 | + * | |
| 727 | + * @since v.4.1.8 | |
| 728 | + * @param OBJECT $post_id Request Param of the REST API. | |
| 729 | + * @return void | |
| 730 | + */ | |
| 731 | + public function handle_wpblock_current_id( $post_id ) { | |
| 732 | + $this->changed_wp_block = ''; | |
| 733 | + $post = get_post( $post_id ); | |
| 734 | + $post_content = $post->post_content; | |
| 185 | 735 | |
| 186 | - // Reusable CSS | |
| 187 | - $reusable_css = ''; | |
| 188 | - $reusable_id = ultimate_post()->reusable_id($post_id); | |
| 189 | - foreach ( $reusable_id as $id ) { | |
| 190 | - $reusable_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$id}.css"; | |
| 191 | - if (file_exists( $reusable_dir_path )) { | |
| 192 | - $reusable_css .= file_get_contents($reusable_dir_path); | |
| 193 | - }else{ | |
| 194 | - $reusable_css .= get_post_meta($id, '_ultp_css', true); | |
| 195 | - } | |
| 736 | + // Parse the blocks | |
| 737 | + $blocks = parse_blocks( $post_content ); | |
| 738 | + $updated_blocks = $this->update_block_attributes_func( $blocks, $post_id ); | |
| 739 | + if ( $this->changed_wp_block ) { | |
| 740 | + wp_update_post( | |
| 741 | + array( | |
| 742 | + 'ID' => $post_id, | |
| 743 | + 'post_content' => serialize_blocks( $updated_blocks ), | |
| 744 | + ) | |
| 745 | + ); | |
| 746 | + } | |
| 747 | + } | |
| 748 | + | |
| 749 | + /** | |
| 750 | + * Handle WP Block postid save | |
| 751 | + * | |
| 752 | + * @since v.4.1.8 | |
| 753 | + * @param ARRAY $blocks The array of blocks to update. | |
| 754 | + * @param INT|STRING $post_id The post ID to set in block attributes. | |
| 755 | + * @return array/Exception | Array of the Custom Message | |
| 756 | + */ | |
| 757 | + function update_block_attributes_func( $blocks, $post_id ) { | |
| 758 | + foreach ( $blocks as &$block ) { | |
| 759 | + if ( strpos( $block['blockName'], 'ultimate-post/' ) > -1 && | |
| 760 | + isset( $block['attrs']['currentPostId'] ) && | |
| 761 | + $post_id != $block['attrs']['currentPostId'] | |
| 762 | + ) { | |
| 763 | + $this->changed_wp_block = true; | |
| 764 | + $block['attrs'] = array_merge( $block['attrs'], array( 'currentPostId' => $post_id ) ); | |
| 196 | 765 | } |
| 766 | + // Recursively update inner blocks. | |
| 767 | + if ( ! empty( $block['innerBlocks'] ) ) { | |
| 768 | + $block['innerBlocks'] = $this->update_block_attributes_func( $block['innerBlocks'], $post_id ); | |
| 769 | + } | |
| 770 | + } | |
| 771 | + return $blocks; | |
| 772 | + } | |
| 197 | 773 | |
| 198 | - if (file_exists( $css_dir_path )) { | |
| 199 | - echo '<style type="text/css">'.file_get_contents($css_dir_path).$reusable_css.'</style>'; | |
| 200 | - } else { | |
| 201 | - $css = get_post_meta($post_id, '_ultp_css', true); | |
| 202 | - if($css) { | |
| 203 | - echo '<style type="text/css">'.$css.$reusable_css.'</style>'; | |
| 774 | + /** | |
| 775 | + * Save Import CSS in the top of the File | |
| 776 | + * | |
| 777 | + * @since v.1.0.0 | |
| 778 | + * @param STRING | $get_css CSS (STRING). | |
| 779 | + * @return STRING | Generated CSS | |
| 780 | + */ | |
| 781 | + public function set_top_css( $get_css = '' ) { | |
| 782 | + | |
| 783 | + $disable_google_font = ultimate_post()->get_setting( 'disable_google_font' ); | |
| 784 | + if ( $disable_google_font != 'yes' ) { | |
| 785 | + $css_url = "@import url('https://fonts.googleapis.com/css?family="; | |
| 786 | + $font_exists = substr_count( $get_css, $css_url ); | |
| 787 | + if ( $font_exists ) { | |
| 788 | + $pattern = sprintf( '/%s(.+?)%s/ims', preg_quote( $css_url, '/' ), preg_quote( "');", '/' ) ); | |
| 789 | + if ( preg_match_all( $pattern, $get_css, $matches ) ) { | |
| 790 | + $fonts = $matches[0]; | |
| 791 | + $get_css = str_replace( $fonts, '', $get_css ); | |
| 792 | + if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/', $get_css, $matche_weight ) ) { | |
| 793 | + $weight = array_map( | |
| 794 | + function ( $val ) { | |
| 795 | + $process = trim( str_replace( array( 'font-weight', ':', ';' ), '', $val ) ); | |
| 796 | + if ( is_numeric( $process ) ) { | |
| 797 | + return $process; | |
| 798 | + } | |
| 799 | + }, | |
| 800 | + $matche_weight[0] | |
| 801 | + ); | |
| 802 | + foreach ( $fonts as $key => $val ) { | |
| 803 | + $fonts[ $key ] = str_replace( "');", '', $val ) . ':' . implode( ',', $weight ) . "');"; | |
| 804 | + } | |
| 805 | + } | |
| 806 | + $fonts = array_unique( $fonts ); | |
| 807 | + $get_css = implode( '', $fonts ) . $get_css; | |
| 204 | 808 | } |
| 205 | 809 | } |
| 206 | 810 | } |
| 811 | + return $get_css; | |
| 207 | 812 | } |
| 208 | -} | |
| 813 | + | |
| 814 | + | |
| 815 | + /** | |
| 816 | + * Swap color for dark light | |
| 817 | + * | |
| 818 | + * @since 4.0.0 | |
| 819 | + * @param STRING $rootCSS The root CSS string to swap colors in. | |
| 820 | + * @return NULL | |
| 821 | + */ | |
| 822 | + public function handle_dark_light_color_switcher( $rootCSS ) { | |
| 823 | + $rootCSS = str_replace( | |
| 824 | + array( '--postx_preset_Base_1_color', '--postx_preset_Base_2_color', '--postx_preset_Base_3_color' ), | |
| 825 | + array( '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum', '--postx_preset_Base_3_color_dum' ), | |
| 826 | + $rootCSS | |
| 827 | + ); | |
| 828 | + $rootCSS = str_replace( | |
| 829 | + array( '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color', '--postx_preset_Contrast_3_color' ), | |
| 830 | + array( '--postx_preset_Base_1_color', '--postx_preset_Base_2_color', '--postx_preset_Base_3_color' ), | |
| 831 | + $rootCSS | |
| 832 | + ); | |
| 833 | + $rootCSS = str_replace( | |
| 834 | + array( '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum', '--postx_preset_Base_3_color_dum' ), | |
| 835 | + array( '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color', '--postx_preset_Contrast_3_color' ), | |
| 836 | + $rootCSS | |
| 837 | + ); | |
| 838 | + return $rootCSS; | |
| 839 | + } | |
| 840 | + | |
| 841 | + | |
| 842 | + /** | |
| 843 | + * Delete Plugin Data CSS file delete Action | |
| 844 | + * | |
| 845 | + * @since v.2.9.8 | |
| 846 | + * | |
| 847 | + * @param MIXED $post_id The post ID whose CSS file should be deleted. | |
| 848 | + * @param MIXED $post The post object (unused). | |
| 849 | + * @return void | |
| 850 | + */ | |
| 851 | + public function ultp_delete_post_callback( $post_id, $post ) { | |
| 852 | + $upload = wp_upload_dir(); | |
| 853 | + $upload_dir = $upload['basedir']; | |
| 854 | + $upload_dir_path = $upload_dir . "/ultimate-post/ultp-css-{$post_id}.css"; | |
| 855 | + if ( file_exists( $upload_dir_path ) ) { | |
| 856 | + wp_delete_file( $upload_dir_path ); | |
| 857 | + } | |
| 858 | + } | |
| 859 | +} | |