| 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 |
add_action( 'after_delete_post', array( $this, 'ultp_delete_post_callback' ), 10, 2 ); // Delete Plugin Data CSS file delete Action |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Delete Plugin Data CSS file delete Action |
| 31 |
* |
| 32 |
* * @since v.2.9.8 |
| 33 |
* @return STRING |
| 34 |
*/ |
| 35 |
public function ultp_delete_post_callback( $post_id, $post ) { |
| 36 |
$upload = wp_upload_dir(); |
| 37 |
$upload_dir = $upload['basedir']; |
| 38 |
$upload_dir_path = $upload_dir . "/ultimate-post/ultp-css-{$post_id}.css"; |
| 39 |
if ( file_exists( $upload_dir_path ) ) { |
| 40 |
wp_delete_file( $upload_dir_path ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Disable Google Font Callback |
| 46 |
* |
| 47 |
* * @since v.2.8.1 |
| 48 |
* @return STRING |
| 49 |
*/ |
| 50 |
public function disable_google_font_callback() { |
| 51 |
if ( ! ( isset( $_REQUEST['wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) ) { |
| 52 |
return ; |
| 53 |
} |
| 54 |
if(!ultimate_post()->permission_check_for_restapi()){ |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
global $wp_filesystem; |
| 59 |
if ( ! $wp_filesystem ) { |
| 60 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 61 |
WP_Filesystem(); |
| 62 |
} |
| 63 |
|
| 64 |
$upload_dir_url = wp_upload_dir(); |
| 65 |
$dir = trailingslashit( $upload_dir_url['basedir'] ) . 'ultimate-post/'; |
| 66 |
$css_dir = glob( $dir . '*.css' ); |
| 67 |
$exclude_typo = implode( '|', ['Arial','Tahoma','Verdana','Helvetica','Times New Roman','Trebuchet MS','Georgia'] ); |
| 68 |
|
| 69 |
if ( count( $css_dir ) > 0 ) { |
| 70 |
foreach ( $css_dir as $key => $value ) { |
| 71 |
$css = $wp_filesystem->get_contents( $value ); |
| 72 |
$filter_css = preg_replace( '/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $css ); // Remove Import Font |
| 73 |
$final_css = preg_replace( '/(font-family:)((?!'.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css ); // Font Replace Except Default Font |
| 74 |
$wp_filesystem->put_contents( $value, $final_css ); // Update CSS File |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
global $wpdb; |
| 79 |
$results = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE `meta_key`='_ultp_css'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 80 |
if ( ! empty( $results ) ) { |
| 81 |
foreach ( $results as $key => $value ) { |
| 82 |
$filter_css = preg_replace('/(@import)[\w\s:\/?=,;.\'()+]*;/m', '', $value->meta_value); // Remove Import Font |
| 83 |
$final_css = preg_replace('/(font-family:)((?!'.$exclude_typo.')[\w\s:,\\\'-])*;/mi', '', $filter_css); // Font Replace Except Default Font |
| 84 |
update_option($value->meta_key, $final_css); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return wp_send_json_success(__('CSS Updated!', 'ultimate-post')); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* REST API Action |
| 93 |
* |
| 94 |
* @since v.1.0.0 |
| 95 |
* @return NULL |
| 96 |
*/ |
| 97 |
public function save_block_css_callback() { |
| 98 |
register_rest_route( |
| 99 |
'ultp/v1', |
| 100 |
'/save_block_css/', |
| 101 |
array( |
| 102 |
array( |
| 103 |
'methods' => 'POST', |
| 104 |
'callback' => array( $this, 'save_block_content_css'), |
| 105 |
'permission_callback' => function () { |
| 106 |
return current_user_can( 'publish_posts' ); |
| 107 |
}, |
| 108 |
'args' => array() |
| 109 |
) |
| 110 |
) |
| 111 |
); |
| 112 |
register_rest_route( |
| 113 |
'ultp/v1', |
| 114 |
'/get_reusable_posts/', |
| 115 |
array( |
| 116 |
array( |
| 117 |
'methods' => 'POST', |
| 118 |
'callback' => array($this, 'get_reusable_posts_call'), |
| 119 |
'permission_callback' => function () { |
| 120 |
return current_user_can('publish_posts'); |
| 121 |
}, |
| 122 |
'args' => array() |
| 123 |
) |
| 124 |
) |
| 125 |
); |
| 126 |
register_rest_route( |
| 127 |
'ultp/v1', |
| 128 |
'/appened_css/', |
| 129 |
array( |
| 130 |
array( |
| 131 |
'methods' => 'POST', |
| 132 |
'callback' => array($this, 'appened_css_call'), |
| 133 |
'permission_callback' => function () { |
| 134 |
return current_user_can('publish_posts'); |
| 135 |
}, |
| 136 |
'args' => array() |
| 137 |
) |
| 138 |
) |
| 139 |
); |
| 140 |
register_rest_route( |
| 141 |
'ultp/v1', |
| 142 |
'/action_option/', |
| 143 |
array( |
| 144 |
array( |
| 145 |
'methods' => 'POST', |
| 146 |
'callback' => array($this, 'global_settings_action'), |
| 147 |
'permission_callback' => function () { |
| 148 |
return current_user_can('edit_posts'); |
| 149 |
}, |
| 150 |
'args' => array() |
| 151 |
) |
| 152 |
) |
| 153 |
); |
| 154 |
register_rest_route( |
| 155 |
'ultp/v1', |
| 156 |
'/postx_presets/', |
| 157 |
array( |
| 158 |
array( |
| 159 |
'methods' => 'POST', |
| 160 |
'callback' => array($this, 'postx_presets_callback'), |
| 161 |
'permission_callback' => function () { |
| 162 |
return current_user_can('edit_posts'); |
| 163 |
}, |
| 164 |
'args' => array() |
| 165 |
) |
| 166 |
) |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get and Set PostX Presets Settings |
| 172 |
* |
| 173 |
* @since v.2.4.24 |
| 174 |
* @param OBJECT | Request Param of the REST API |
| 175 |
* @return ARRAY | Array of the Custom Message |
| 176 |
*/ |
| 177 |
public function postx_presets_callback($server) { |
| 178 |
$post = $server->get_params(); |
| 179 |
$type = isset($post['type']) ? $post['type'] : ''; |
| 180 |
$key = isset($post['key']) ? $post['key'] : ''; |
| 181 |
$data = isset($post['data']) ? $post['data'] : ''; |
| 182 |
|
| 183 |
if ($type) { |
| 184 |
if ($type == 'set') { |
| 185 |
if ( current_user_can('edit_others_posts') ) { |
| 186 |
update_option($key, $data); |
| 187 |
} |
| 188 |
return ['success' => true]; |
| 189 |
} else { |
| 190 |
return ['success' => true, 'data' => get_option($key, [])]; |
| 191 |
} |
| 192 |
} else { |
| 193 |
return ['success' => false]; |
| 194 |
} |
| 195 |
} |
| 196 |
/** |
| 197 |
* Get and Set PostX Global Settings |
| 198 |
* |
| 199 |
* @since v.2.4.24 |
| 200 |
* @param OBJECT | Request Param of the REST API |
| 201 |
* @return ARRAY | Array of the Custom Message |
| 202 |
*/ |
| 203 |
public function global_settings_action($server) { |
| 204 |
$post = $server->get_params(); |
| 205 |
$post_type = isset($post['type'])?ultimate_post()->ultp_rest_sanitize_params($post['type']):''; |
| 206 |
if ($post_type && ultimate_post()->permission_check_for_restapi()) { |
| 207 |
if ($post_type == 'set') { |
| 208 |
if ( current_user_can('edit_others_posts') ) { |
| 209 |
update_option('postx_global', ultimate_post()->ultp_rest_sanitize_params( $post['data'])); |
| 210 |
} |
| 211 |
return ['success' => true]; |
| 212 |
} else { |
| 213 |
return ['success' => true, 'data' => get_option('postx_global', [])]; |
| 214 |
} |
| 215 |
} else { |
| 216 |
return ['success' => false]; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Save Import CSS in the top of the File |
| 222 |
* |
| 223 |
* @since v.1.0.0 |
| 224 |
* @param OBJECT | Request Param of the REST API |
| 225 |
* @return ARRAY/Exception | Array of the Custom Message |
| 226 |
*/ |
| 227 |
public function appened_css_call($server) { |
| 228 |
global $wp_filesystem; |
| 229 |
if ( ! $wp_filesystem ) { |
| 230 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 231 |
} |
| 232 |
$post = $server->get_params(); |
| 233 |
$css = $post['inner_css']; |
| 234 |
$post_id = isset($post['post_id'])? (int) ultimate_post()->ultp_rest_sanitize_params($post['post_id']):0; |
| 235 |
if ( $post_id && 'wp_block' === get_post_type($post_id) ) { |
| 236 |
|
| 237 |
$upload_dir_url = wp_upload_dir(); |
| 238 |
$filename = "ultp-css-{$post_id}.css"; |
| 239 |
$dir = trailingslashit($upload_dir_url['basedir']).'ultimate-post/'; |
| 240 |
update_post_meta($post_id, '_ultp_css', $css); |
| 241 |
WP_Filesystem( false, $upload_dir_url['basedir'], true ); |
| 242 |
if ( ! $wp_filesystem->is_dir( $dir ) ) { |
| 243 |
$wp_filesystem->mkdir( $dir ); |
| 244 |
} |
| 245 |
if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) { |
| 246 |
throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post' )); //phpcs:ignore |
| 247 |
} |
| 248 |
wp_send_json_success(array('success' => true, 'message' => __('Data retrive done', 'ultimate-post'))); |
| 249 |
} else { |
| 250 |
return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post')); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
/** |
| 256 |
* Save Import CSS in the top of the File |
| 257 |
* |
| 258 |
* @since v.1.0.0 |
| 259 |
* @param OBJECT | Request Param of the REST API |
| 260 |
* @return ARRAY/Exception | Array of the Custom Message |
| 261 |
*/ |
| 262 |
public function get_reusable_posts_call($server) { |
| 263 |
$post = $server->get_params(); |
| 264 |
$post_id = isset($post['postId'])? ultimate_post()->ultp_rest_sanitize_params($post['postId']):''; |
| 265 |
if ( $post_id && ( ultimate_post()->permission_check_for_restapi($post_id) || 'wp_block'===get_post_type($post_id) )) { |
| 266 |
return array('success' => true, 'data'=> get_post($post_id)->post_content, 'message' => __('Data retrive done', 'ultimate-post')); |
| 267 |
} else { |
| 268 |
return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post')); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
/** |
| 274 |
* Save Import CSS in the top of the File |
| 275 |
* |
| 276 |
* @since v.1.0.0 |
| 277 |
* @param OBJECT | Request Param of the REST API |
| 278 |
* @return ARRAY/Exception | Array of the Custom Message |
| 279 |
*/ |
| 280 |
public function save_block_content_css($request) { |
| 281 |
|
| 282 |
$params = $request->get_params(); |
| 283 |
$post_id = isset($params['post_id'])? ultimate_post()->ultp_rest_sanitize_params($params['post_id']):''; |
| 284 |
|
| 285 |
if(!ultimate_post()->permission_check_for_restapi(is_numeric($post_id)?$post_id:false)) { |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
try{ |
| 290 |
global $wp_filesystem; |
| 291 |
if ( ! $wp_filesystem ) { |
| 292 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 293 |
} |
| 294 |
|
| 295 |
$params['has_block'] = isset($params['has_block'])?rest_sanitize_boolean($params['has_block']):''; |
| 296 |
$params['preview'] = isset($params['preview'])?rest_sanitize_boolean($params['preview']):''; |
| 297 |
|
| 298 |
if ( $post_id == 'ultp-widget' && $params['has_block'] ) { // Only Editor and upwards role user can save widget css |
| 299 |
update_option($post_id, $params['block_css']); |
| 300 |
return ['success' => true, 'message' => __('Widget CSS Saved', 'ultimate-post')]; |
| 301 |
} |
| 302 |
|
| 303 |
$upload_dir_url = wp_upload_dir(); |
| 304 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultimate-post/'; |
| 305 |
|
| 306 |
if ( strpos($post_id, '//') !== false ) { |
| 307 |
$data = explode('//', $post_id); |
| 308 |
|
| 309 |
|
| 310 |
if ( $data[2] == 'wp_template_part' ) { |
| 311 |
$filename = "ultp-css-{$data[0]}__{$data[3]}.css"; |
| 312 |
$template_css = get_option($data[0].'__'.$data[3], ''); |
| 313 |
$reg = '/(\/\*TEMPLATE_START_'.$data[1].')[^"]+?(TEMPLATE_CLOSE_'.$data[1].'\*\/)/m'; |
| 314 |
|
| 315 |
$ultp_block_css = ''; |
| 316 |
if ( strpos($template_css, 'TEMPLATE_START_'.$data[1]) !== false ) { |
| 317 |
$ultp_block_css = preg_replace($reg, '/*TEMPLATE_START_'.$data[1].'*/' . $params['block_css'] . '/*TEMPLATE_CLOSE_'.$data[1].'*/', $template_css); |
| 318 |
} else { |
| 319 |
$ultp_block_css = $template_css . '/*TEMPLATE_START_'.$data[1].'*/' . $params['block_css'] . '/*TEMPLATE_CLOSE_'.$data[1].'*/'; |
| 320 |
} |
| 321 |
$ultp_block_css = $this->set_top_css($ultp_block_css); |
| 322 |
WP_Filesystem( false, $upload_dir_url['basedir'], true ); |
| 323 |
if ( ! $wp_filesystem->is_dir( $dir ) ) { |
| 324 |
$wp_filesystem->mkdir( $dir ); |
| 325 |
} |
| 326 |
|
| 327 |
$wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ); |
| 328 |
|
| 329 |
update_option($data[0].'__'.$data[3], $ultp_block_css); |
| 330 |
} else if ( $data[2] == 'wp_template' ) { |
| 331 |
$filename = "ultp-css-{$data[0]}__{$data[1]}.css"; |
| 332 |
$ultp_block_css = $this->set_top_css($params['block_css']); |
| 333 |
WP_Filesystem( false, $upload_dir_url['basedir'], true ); |
| 334 |
if ( ! $wp_filesystem->is_dir( $dir ) ) { |
| 335 |
$wp_filesystem->mkdir( $dir ); |
| 336 |
} |
| 337 |
$wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ); |
| 338 |
|
| 339 |
update_option($data[0].'__'.$data[1], $ultp_block_css); |
| 340 |
} |
| 341 |
|
| 342 |
return ['success' => true, 'message' => __('Template & Template Part CSS Saved.', 'ultimate-post')]; |
| 343 |
} |
| 344 |
|
| 345 |
$post_id = (int) $post_id; |
| 346 |
$filename = "ultp-css-{$post_id}.css"; |
| 347 |
|
| 348 |
if ( $params['has_block'] ) { |
| 349 |
// Set Saving ID for Clean Cache |
| 350 |
ultimate_post()->set_setting('save_version', wp_rand(1, 1000)); |
| 351 |
|
| 352 |
update_post_meta($post_id, '_ultp_active', 'yes'); |
| 353 |
$ultp_block_css = $this->set_top_css($params['block_css']); |
| 354 |
|
| 355 |
// Preview Check |
| 356 |
if ( $params['preview'] ) { |
| 357 |
set_transient('_ultp_preview_'.$post_id, $ultp_block_css , 60*60); |
| 358 |
return ['success' => true]; |
| 359 |
} |
| 360 |
|
| 361 |
WP_Filesystem( false, $upload_dir_url['basedir'], true ); |
| 362 |
if ( ! $wp_filesystem->is_dir( $dir ) ) { |
| 363 |
$wp_filesystem->mkdir( $dir ); |
| 364 |
} |
| 365 |
if ( ! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) { |
| 366 |
throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post')); //phpcs:ignore |
| 367 |
} |
| 368 |
update_post_meta($post_id, '_ultp_css', $ultp_block_css); |
| 369 |
return ['success'=>true, 'message'=>__('PostX css file has been updated.', 'ultimate-post')]; |
| 370 |
} else { |
| 371 |
delete_post_meta($post_id, '_ultp_active'); |
| 372 |
if ( file_exists($dir.$filename) ) { |
| 373 |
wp_delete_file($dir.$filename); |
| 374 |
} |
| 375 |
delete_post_meta($post_id, '_ultp_css'); |
| 376 |
return ['success' => true, 'message' => __('Data Delete Done', 'ultimate-post')]; |
| 377 |
} |
| 378 |
}catch(Exception $e) { |
| 379 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Save Import CSS in the top of the File |
| 386 |
* |
| 387 |
* @since v.1.0.0 |
| 388 |
* @param STRING | CSS (STRING) |
| 389 |
* @return STRING | Generated CSS |
| 390 |
*/ |
| 391 |
public function set_top_css($get_css = '') { |
| 392 |
$disable_google_font = ultimate_post()->get_setting('disable_google_font'); |
| 393 |
if ( $disable_google_font != 'yes' ) { |
| 394 |
$css_url = "@import url('https://fonts.googleapis.com/css?family="; |
| 395 |
$font_exists = substr_count($get_css, $css_url); |
| 396 |
if ( $font_exists ) { |
| 397 |
$pattern = sprintf('/%s(.+?)%s/ims', preg_quote($css_url, '/'), preg_quote("');", '/')); |
| 398 |
if ( preg_match_all($pattern, $get_css, $matches) ) { |
| 399 |
$fonts = $matches[0]; |
| 400 |
$get_css = str_replace($fonts, '', $get_css); |
| 401 |
if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/' , $get_css, $matche_weight ) ) { |
| 402 |
$weight = array_map( function($val) { |
| 403 |
$process = trim( str_replace( array( 'font-weight',':',';' ) , '', $val ) ); |
| 404 |
if (is_numeric( $process )) { |
| 405 |
return $process; |
| 406 |
} |
| 407 |
}, $matche_weight[0] ); |
| 408 |
foreach ( $fonts as $key => $val ) { |
| 409 |
$fonts[$key] = str_replace( "');",'', $val ).':'.implode( ',',$weight )."');"; |
| 410 |
} |
| 411 |
} |
| 412 |
$fonts = array_unique($fonts); |
| 413 |
$get_css = implode('', $fonts).$get_css; |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
return $get_css; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
/** |
| 422 |
* Enqueue CSS in HEAD or as a File |
| 423 |
* |
| 424 |
* @since v.1.0.0 |
| 425 |
* @return NULL |
| 426 |
*/ |
| 427 |
public function require_block_css() { |
| 428 |
$css_type = ultimate_post()->get_setting('css_save_as'); |
| 429 |
if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { // @codingStandardsIgnoreLine |
| 430 |
add_action( 'wp_head', array( $this, 'add_block_inline_css' ), 100 ); |
| 431 |
} else if ( $css_type === 'filesystem' ) { |
| 432 |
add_action( 'wp_enqueue_scripts', array( $this, 'add_block_css_file' ) ); |
| 433 |
} else { |
| 434 |
add_action( 'wp_head', array( $this, 'add_block_inline_css' ), 100 ); |
| 435 |
} |
| 436 |
|
| 437 |
add_action('wp_enqueue_scripts', array($this, 'postx_global_css')); |
| 438 |
add_action( 'admin_init', array( $this, 'postx_global_css' ), 1 ); |
| 439 |
add_action( 'admin_init', array( $this, 'postx_global_css_dependancies' ), 2 ); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Check global style loaded or not |
| 444 |
* |
| 445 |
* @since 4.0.0 |
| 446 |
* @return NULL |
| 447 |
*/ |
| 448 |
public function postx_global_css_dependancies() { |
| 449 |
$wp_styles = wp_styles(); |
| 450 |
$style = $wp_styles->query( 'wp-block-library', 'registered' ); |
| 451 |
if( ! $style ) { |
| 452 |
return; |
| 453 |
} |
| 454 |
$array = ['wpxpo-global-style', 'ultp-preset-colors-style', 'ultp-preset-gradient-style', 'ultp-preset-typo-style' ]; |
| 455 |
foreach ($array as $arr) { |
| 456 |
if( wp_style_is( $arr, 'registered' ) && !in_array( $arr, $style->deps, true ) ) { |
| 457 |
$style->deps[] = $arr; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Set Global Color Codes |
| 464 |
* |
| 465 |
* @since v.1.0.0 |
| 466 |
* @return NULL |
| 467 |
*/ |
| 468 |
public function postx_global_css() { |
| 469 |
// Preset CSS |
| 470 |
$global = get_option('postx_global', []); |
| 471 |
$custom_css = ':root { |
| 472 |
--preset-color1: '.(isset($global['presetColor1'])?$global['presetColor1']:'#037fff').'; |
| 473 |
--preset-color2: '.(isset($global['presetColor2'])?$global['presetColor2']:'#026fe0').'; |
| 474 |
--preset-color3: '.(isset($global['presetColor3'])?$global['presetColor3']:'#071323').'; |
| 475 |
--preset-color4: '.(isset($global['presetColor4'])?$global['presetColor4']:'#132133').'; |
| 476 |
--preset-color5: '.(isset($global['presetColor5'])?$global['presetColor5']:'#34495e').'; |
| 477 |
--preset-color6: '.(isset($global['presetColor6'])?$global['presetColor6']:'#787676').'; |
| 478 |
--preset-color7: '.(isset($global['presetColor7'])?$global['presetColor7']:'#f0f2f3').'; |
| 479 |
--preset-color8: '.(isset($global['presetColor8'])?$global['presetColor8']:'#f8f9fa').'; |
| 480 |
--preset-color9: '.(isset($global['presetColor9'])?$global['presetColor9']:'#ffffff').'; |
| 481 |
}'; |
| 482 |
$theme_css = isset($global['globalCSS']) && $global['globalCSS'] ? $global['globalCSS'] : $custom_css.'{}'; |
| 483 |
|
| 484 |
wp_register_style( 'wpxpo-global-style', false, array(), ULTP_VER ); |
| 485 |
wp_enqueue_style( 'wpxpo-global-style' ); |
| 486 |
wp_add_inline_style( 'wpxpo-global-style', $theme_css ); |
| 487 |
|
| 488 |
// preset Colors |
| 489 |
$ultpPresetColors = get_option('ultpPresetColors', []); |
| 490 |
|
| 491 |
$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; }'; |
| 492 |
$savedDLMode = ( isset($global['enableDark']) && $global['enableDark'] ) ? 'ultpdark' : 'ultplight'; |
| 493 |
$localDLMode = isset($_COOKIE['ultplocalDLMode']) ? $_COOKIE['ultplocalDLMode'] : $savedDLMode; |
| 494 |
|
| 495 |
|
| 496 |
if ( $localDLMode == 'ultplight' && $savedDLMode == 'ultpdark' ) { |
| 497 |
$rootCSS = $this->handle_dark_light_color_switcher($rootCSS); |
| 498 |
} else if ( $localDLMode == 'ultpdark' ) { |
| 499 |
if ( $savedDLMode == 'ultplight' ) { |
| 500 |
$rootCSS = $this->handle_dark_light_color_switcher($rootCSS); |
| 501 |
} |
| 502 |
$rootCSS = $rootCSS.' .ultp-dark-logo.wp-block-site-logo img {content: url("'.get_option('ultp_site_dark_logo', '').'");}'; |
| 503 |
} |
| 504 |
|
| 505 |
wp_register_style( 'ultp-preset-colors-style', false, array(), ULTP_VER ); |
| 506 |
wp_enqueue_style( 'ultp-preset-colors-style' ); |
| 507 |
wp_add_inline_style( 'ultp-preset-colors-style', $rootCSS ); |
| 508 |
|
| 509 |
// preset Gradients |
| 510 |
$ultpPresetGradients = get_option('ultpPresetGradients', []); |
| 511 |
wp_register_style( 'ultp-preset-gradient-style', false, array(), ULTP_VER ); |
| 512 |
wp_enqueue_style( 'ultp-preset-gradient-style' ); |
| 513 |
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%); }' ); |
| 514 |
|
| 515 |
// preset Typos |
| 516 |
$ultpPresetTypos = get_option('ultpPresetTypos', []); |
| 517 |
wp_register_style( 'ultp-preset-typo-style', false, array(), ULTP_VER ); |
| 518 |
wp_enqueue_style( 'ultp-preset-typo-style' ); |
| 519 |
wp_add_inline_style( 'ultp-preset-typo-style', isset($ultpPresetTypos['presetTypoCSS']) && $ultpPresetTypos['presetTypoCSS'] ? $ultpPresetTypos['presetTypoCSS'] : '@import url(\'https://fonts.googleapis.com/css?family=Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i\'); :root { --postx_preset_Heading_typo_font_family: Roboto; --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: Roboto; --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; }' ); |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* swap color for dark light |
| 524 |
* |
| 525 |
* @since 4.0.0 |
| 526 |
* @return NULL |
| 527 |
*/ |
| 528 |
public function handle_dark_light_color_switcher( $rootCSS ) { |
| 529 |
$rootCSS = str_replace( |
| 530 |
[ '--postx_preset_Base_1_color', '--postx_preset_Base_2_color', '--postx_preset_Base_3_color' ], |
| 531 |
[ '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum', '--postx_preset_Base_3_color_dum' ], |
| 532 |
$rootCSS |
| 533 |
); |
| 534 |
$rootCSS = str_replace( |
| 535 |
[ '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color','--postx_preset_Contrast_3_color' ], |
| 536 |
[ '--postx_preset_Base_1_color', '--postx_preset_Base_2_color','--postx_preset_Base_3_color' ], |
| 537 |
$rootCSS |
| 538 |
); |
| 539 |
$rootCSS = str_replace( |
| 540 |
[ '--postx_preset_Base_1_color_dum', '--postx_preset_Base_2_color_dum','--postx_preset_Base_3_color_dum' ], |
| 541 |
[ '--postx_preset_Contrast_1_color', '--postx_preset_Contrast_2_color', '--postx_preset_Contrast_3_color' ], |
| 542 |
$rootCSS |
| 543 |
); |
| 544 |
return $rootCSS; |
| 545 |
} |
| 546 |
|
| 547 |
/** |
| 548 |
* Set CSS as File |
| 549 |
* |
| 550 |
* @since v.1.0.0 |
| 551 |
* @return NULL |
| 552 |
*/ |
| 553 |
public function add_block_css_file() { |
| 554 |
$header_id = ultimate_post()->conditions('header'); |
| 555 |
if ( $header_id ) { |
| 556 |
ultimate_post()->set_css_style( $header_id ); |
| 557 |
} |
| 558 |
$footer_id = ultimate_post()->conditions('footer'); |
| 559 |
if ( $footer_id ) { |
| 560 |
ultimate_post()->set_css_style( $footer_id ); |
| 561 |
} |
| 562 |
ultimate_post()->set_css_style( ultimate_post()->get_ID() ); |
| 563 |
$this->add_fse_theme_css('filesystem'); // FSE theme support |
| 564 |
} |
| 565 |
|
| 566 |
public function add_fse_theme_css( $type = 'filesystem' ) { |
| 567 |
if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { |
| 568 |
global $_wp_current_template_id; |
| 569 |
if ( isset($_wp_current_template_id) ) { |
| 570 |
ultimate_post()->register_scripts_common(); |
| 571 |
$template_id = str_replace('//', '__',$_wp_current_template_id); |
| 572 |
if ( $type == 'inline' ) { |
| 573 |
$this->set_inline_css_style( $template_id ); |
| 574 |
} else { |
| 575 |
ultimate_post()->set_css_style( $template_id ); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* Set Inline CSS in Head |
| 583 |
* |
| 584 |
* @since v.1.0.0 |
| 585 |
* @return NULL |
| 586 |
*/ |
| 587 |
public function add_block_inline_css() { |
| 588 |
$header_id = ultimate_post()->conditions('header'); |
| 589 |
if ( $header_id ) { |
| 590 |
$this->set_inline_css_style( $header_id ); |
| 591 |
} |
| 592 |
$footer_id = ultimate_post()->conditions('footer'); |
| 593 |
if ( $footer_id ) { |
| 594 |
$this->set_inline_css_style( $footer_id ); |
| 595 |
} |
| 596 |
$this->set_inline_css_style(ultimate_post()->get_ID()); |
| 597 |
$this->add_fse_theme_css('inline'); // FSE theme support |
| 598 |
} |
| 599 |
public function set_inline_css_style( $post_id ) { |
| 600 |
if ( $post_id ) { |
| 601 |
$upload_dir_url = wp_get_upload_dir(); |
| 602 |
$upload_css_dir_url = trailingslashit( $upload_dir_url['basedir'] ); |
| 603 |
$css_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$post_id}.css"; |
| 604 |
|
| 605 |
// Reusable CSS |
| 606 |
$reusable_id = []; |
| 607 |
$reusable_css = ''; |
| 608 |
if ( strpos($post_id, '__') !== false ) { |
| 609 |
$template = get_block_template( str_replace('__', '//', $post_id) ); |
| 610 |
if ( $template->wp_id ) { |
| 611 |
$reusable_id = ultimate_post()->reusable_id($template->wp_id); |
| 612 |
} |
| 613 |
} else { |
| 614 |
$reusable_id = ultimate_post()->reusable_id($post_id); |
| 615 |
} |
| 616 |
if ( !empty($reusable_id) ) { |
| 617 |
foreach ( $reusable_id as $id ) { |
| 618 |
$reusable_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$id}.css"; |
| 619 |
if ( file_exists( $reusable_dir_path ) ) { |
| 620 |
global $wp_filesystem; |
| 621 |
if (! $wp_filesystem ) { |
| 622 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 623 |
} |
| 624 |
WP_Filesystem(); |
| 625 |
$reusable_css .= $wp_filesystem->get_contents($reusable_dir_path); |
| 626 |
} else { |
| 627 |
$reusable_css .= get_post_meta($id, '_ultp_css', true); |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { // @codingStandardsIgnoreLine |
| 632 |
$css = get_transient('_ultp_preview_'.$post_id, true); |
| 633 |
if ( !$css ) { |
| 634 |
$css = get_post_meta($post_id, '_ultp_css', true); |
| 635 |
} |
| 636 |
if ( $css ) { |
| 637 |
if ( $reusable_css ) { |
| 638 |
$css = $this->set_top_css($css.$reusable_css); |
| 639 |
} |
| 640 |
echo ultimate_post()->set_inline($css, $post_id); //phpcs:ignore |
| 641 |
} |
| 642 |
} else if ( file_exists( $css_dir_path ) ) { |
| 643 |
global $wp_filesystem; |
| 644 |
if (! $wp_filesystem ) { |
| 645 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 646 |
} |
| 647 |
WP_Filesystem(); |
| 648 |
$css = $wp_filesystem->get_contents($css_dir_path); |
| 649 |
if ( $reusable_css ) { |
| 650 |
$css = $this->set_top_css($css.$reusable_css); |
| 651 |
} |
| 652 |
if ( $css ) { |
| 653 |
echo ultimate_post()->set_inline($css, $post_id); //phpcs:ignore |
| 654 |
} |
| 655 |
} else { |
| 656 |
$css = get_post_meta($post_id, '_ultp_css', true); |
| 657 |
if ( $reusable_css ) { |
| 658 |
$css = $this->set_top_css($css.$reusable_css); |
| 659 |
} |
| 660 |
if ( $css ) { |
| 661 |
echo ultimate_post()->set_inline($css, $post_id); //phpcs:ignore |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
} |