| 1 |
<?php |
| 2 |
namespace ULTP; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Styles { |
| 7 |
public function __construct(){ |
| 8 |
$this->require_block_css(); |
| 9 |
add_action('rest_api_init', array($this, 'save_block_css_callback')); |
| 10 |
} |
| 11 |
|
| 12 |
// API Routes for save CSS |
| 13 |
public function save_block_css_callback(){ |
| 14 |
register_rest_route( |
| 15 |
'ultp/v1', |
| 16 |
'/save_block_css/', |
| 17 |
array( |
| 18 |
array( |
| 19 |
'methods' => 'POST', |
| 20 |
'callback' => array( $this, 'save_block_content_css'), |
| 21 |
'permission_callback' => function () { |
| 22 |
return current_user_can( 'edit_posts' ); |
| 23 |
}, |
| 24 |
'args' => array() |
| 25 |
) |
| 26 |
) |
| 27 |
); |
| 28 |
|
| 29 |
register_rest_route( |
| 30 |
'ultp/v1', |
| 31 |
'/get_posts/', |
| 32 |
array( |
| 33 |
array( |
| 34 |
'methods' => 'POST', |
| 35 |
'callback' => array($this, 'get_posts_call'), |
| 36 |
'permission_callback' => function () { |
| 37 |
return current_user_can('edit_posts'); |
| 38 |
}, |
| 39 |
'args' => array() |
| 40 |
) |
| 41 |
) |
| 42 |
); |
| 43 |
|
| 44 |
register_rest_route( |
| 45 |
'ultp/v1', |
| 46 |
'/appened_css/', |
| 47 |
array( |
| 48 |
array( |
| 49 |
'methods' => 'POST', |
| 50 |
'callback' => array($this, 'appened_css_call'), |
| 51 |
'permission_callback' => function () { |
| 52 |
return current_user_can('edit_posts'); |
| 53 |
}, |
| 54 |
'args' => array() |
| 55 |
) |
| 56 |
) |
| 57 |
); |
| 58 |
|
| 59 |
} |
| 60 |
|
| 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'); |
| 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')); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 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')); |
| 86 |
} else { |
| 87 |
return array('success' => false, 'message' => __('Data not found!!', 'ultimate-post')); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 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' ); |
| 97 |
} |
| 98 |
|
| 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/'; |
| 104 |
|
| 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 ); |
| 111 |
} |
| 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')]; |
| 117 |
} else { |
| 118 |
delete_post_meta($post_id, '_ultp_active'); |
| 119 |
if (file_exists($dir.$filename)) { |
| 120 |
unlink($dir.$filename); |
| 121 |
} |
| 122 |
delete_post_meta($post_id, '_ultp_css'); |
| 123 |
} |
| 124 |
}catch(Exception $e){ |
| 125 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 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; |
| 143 |
} |
| 144 |
}, $matche_weight[0] ); |
| 145 |
foreach ( $fonts as $key => $val ) { |
| 146 |
$fonts[$key] = str_replace( "');",'', $val ).':'.implode( ',',$weight )."');"; |
| 147 |
} |
| 148 |
} |
| 149 |
$fonts = array_unique($fonts); |
| 150 |
$get_css = implode('', $fonts).$get_css; |
| 151 |
} |
| 152 |
} |
| 153 |
return $get_css; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 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); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 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); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 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"; |
| 185 |
|
| 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 |
} |
| 196 |
} |
| 197 |
|
| 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>'; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |