| 1 |
<?php |
| 2 |
// Exit if accessed directly. |
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if (!class_exists('BetterdocsStyleHandler')) { |
| 8 |
final class BetterdocsStyleHandler |
| 9 |
{ |
| 10 |
private static $instance; |
| 11 |
|
| 12 |
private $media_desktop = [ |
| 13 |
'name' => 'desktop', |
| 14 |
'screen_size' => '' |
| 15 |
]; |
| 16 |
private $media_tab = [ |
| 17 |
'name' => 'tab', |
| 18 |
'screen_size' => 1024 |
| 19 |
]; |
| 20 |
private $media_mobile = [ |
| 21 |
'name' => 'mobile', |
| 22 |
'screen_size' => 767 |
| 23 |
]; |
| 24 |
|
| 25 |
|
| 26 |
public static function init() |
| 27 |
{ |
| 28 |
if (null === self::$instance) { |
| 29 |
self::$instance = new self; |
| 30 |
} |
| 31 |
return self::$instance; |
| 32 |
} |
| 33 |
|
| 34 |
public function __construct() |
| 35 |
{ |
| 36 |
add_action('admin_enqueue_scripts', [$this, 'betterdocs_blocks_edit_post']); |
| 37 |
add_action('wp_ajax_betterdocs_write_block_css', [$this, 'write_block_css']); |
| 38 |
add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_css']); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Enqueue a script in the WordPress admin on edit.php. |
| 43 |
* @param int $hook Hook suffix for the current admin page. |
| 44 |
*/ |
| 45 |
public function betterdocs_blocks_edit_post($hook) |
| 46 |
{ |
| 47 |
$dir = dirname(__FILE__); |
| 48 |
if ($hook == 'post-new.php' || $hook == 'post.php') { |
| 49 |
wp_enqueue_script( |
| 50 |
'betterdocs-blocks-edit-post', |
| 51 |
BETTERDOCS_URL . 'public/js/style-handler.js', |
| 52 |
array( |
| 53 |
'lodash', |
| 54 |
'wp-i18n', |
| 55 |
'wp-element', |
| 56 |
'wp-hooks', |
| 57 |
'wp-util', |
| 58 |
'wp-components', |
| 59 |
'wp-blocks', |
| 60 |
'wp-editor', |
| 61 |
'wp-block-editor', |
| 62 |
'wp-edit-post' |
| 63 |
), |
| 64 |
filemtime(BETTERDOCS_DIR_PATH . 'public/js/style-handler.js'), |
| 65 |
true |
| 66 |
); |
| 67 |
wp_localize_script('betterdocs-blocks-edit-post', 'betterdocs_style_handler', [ |
| 68 |
'sth_nonce' => wp_create_nonce('betterdocs_style_handler_nonce'), |
| 69 |
'editor_type' => 'edit-post' |
| 70 |
]); |
| 71 |
} elseif ($hook == 'site-editor.php') { |
| 72 |
wp_enqueue_script( |
| 73 |
'betterdocs-blocks-edit-post', |
| 74 |
BETTERDOCS_URL . 'public/js/style-handler.js', |
| 75 |
array( |
| 76 |
'lodash', |
| 77 |
'wp-i18n', |
| 78 |
'wp-element', |
| 79 |
'wp-hooks', |
| 80 |
'wp-util', |
| 81 |
'wp-components', |
| 82 |
'wp-blocks', |
| 83 |
'wp-editor', |
| 84 |
'wp-block-editor', |
| 85 |
'wp-edit-site' |
| 86 |
), |
| 87 |
filemtime(BETTERDOCS_DIR_PATH . 'public/js/style-handler.js'), |
| 88 |
true |
| 89 |
); |
| 90 |
wp_localize_script('betterdocs-blocks-edit-post', 'betterdocs_style_handler', [ |
| 91 |
'sth_nonce' => wp_create_nonce('betterdocs_style_handler_nonce'), |
| 92 |
'editor_type' => 'edit-site' |
| 93 |
]); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Ajax callback to write css in upload directory |
| 99 |
* @retun void |
| 100 |
* @since 1.0.2 |
| 101 |
*/ |
| 102 |
public function write_block_css() |
| 103 |
{ |
| 104 |
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'betterdocs_style_handler_nonce') || !current_user_can('manage_options')) { |
| 105 |
echo 'Invalid request'; |
| 106 |
wp_die(); |
| 107 |
} |
| 108 |
|
| 109 |
$block_styles = (array)json_decode(stripslashes($_POST['data'])); |
| 110 |
|
| 111 |
if (isset($_POST['editorType']) && $_POST['editorType'] === "edit-site") { |
| 112 |
$upload_dir = wp_upload_dir()['basedir'] . '/betterdocs-style/'; |
| 113 |
$editSiteCssPath = $upload_dir . 'betterdocs-style-' . $_POST['editorType'] . '.min.css'; |
| 114 |
if (file_exists($editSiteCssPath)) { |
| 115 |
$existingCss = file_get_contents($editSiteCssPath); |
| 116 |
$pattern = "~\/\*(.*?)\*\/~"; |
| 117 |
preg_match_all($pattern, $existingCss, $result, PREG_PATTERN_ORDER); |
| 118 |
$allComments = $result[0]; |
| 119 |
$seperatedIds = array(); |
| 120 |
foreach ($allComments as $comment) { |
| 121 |
$id = preg_replace('/[^A-Za-z0-9\-]|Ends|Starts/', '', $comment); |
| 122 |
|
| 123 |
if (strpos($comment, "Starts")) { |
| 124 |
$seperatedIds[$id]['start'] = $comment; |
| 125 |
} else if (strpos($comment, "Ends")) { |
| 126 |
$seperatedIds[$id]['end'] = $comment; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$seperateStyles = array(); |
| 131 |
foreach ($seperatedIds as $key => $ids) { |
| 132 |
$data = $this->get_between_data($existingCss, $ids['start'], $ids['end']); |
| 133 |
$seperateStyles[$key] = $data; |
| 134 |
} |
| 135 |
|
| 136 |
$finalCSSArray = array_merge($seperateStyles, $block_styles); |
| 137 |
|
| 138 |
if (!empty($css = $this->build_css($finalCSSArray))) { |
| 139 |
$upload_dir = wp_upload_dir()['basedir'] . '/betterdocs-style/'; |
| 140 |
if (!file_exists($upload_dir)) { |
| 141 |
mkdir($upload_dir); |
| 142 |
} |
| 143 |
|
| 144 |
file_put_contents($editSiteCssPath, $css); |
| 145 |
} |
| 146 |
} else { |
| 147 |
if (!empty($css = $this->build_css($block_styles))) { |
| 148 |
$upload_dir = wp_upload_dir()['basedir'] . '/betterdocs-style/'; |
| 149 |
if (!file_exists($upload_dir)) { |
| 150 |
mkdir($upload_dir); |
| 151 |
} |
| 152 |
|
| 153 |
file_put_contents($editSiteCssPath, $css); |
| 154 |
} |
| 155 |
} |
| 156 |
} else { |
| 157 |
if (!empty($css = $this->build_css($block_styles))) { |
| 158 |
$upload_dir = wp_upload_dir()['basedir'] . '/betterdocs-style/'; |
| 159 |
if (!file_exists($upload_dir)) { |
| 160 |
mkdir($upload_dir); |
| 161 |
} |
| 162 |
file_put_contents($upload_dir . 'betterdocs-style-' . abs($_POST['id']) . '.min.css', $css); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
wp_die(); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Enqueue frontend css for post if have one |
| 171 |
* @return void |
| 172 |
* @since 1.0.2 |
| 173 |
*/ |
| 174 |
public function enqueue_frontend_css() |
| 175 |
{ |
| 176 |
global $post; |
| 177 |
|
| 178 |
if (!empty($post) && !empty($post->ID)) { |
| 179 |
$upload_dir = wp_upload_dir(); |
| 180 |
|
| 181 |
if (file_exists($upload_dir['basedir'] . '/betterdocs-style/betterdocs-style-' . $post->ID . '.min.css')) { |
| 182 |
wp_enqueue_style('betterdocs-block-style-' . $post->ID, $upload_dir['baseurl'] . '/betterdocs-style/betterdocs-style-' . $post->ID . '.min.css', [], substr(md5(microtime(true)), 0, 10)); |
| 183 |
} |
| 184 |
if (function_exists('wp_is_block_theme') && wp_is_block_theme() && file_exists($upload_dir['basedir'] . '/betterdocs-style/betterdocs-style-edit-site.min.css')) { |
| 185 |
wp_enqueue_style('betterdocs-fullsite-style', $upload_dir['baseurl'] . '/betterdocs-style/betterdocs-style-edit-site.min.css', [], substr(md5(microtime(true)), 0, 10)); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Enqueue frontend css for post if have one |
| 192 |
* @param string |
| 193 |
* @return string |
| 194 |
* @since 1.0.2 |
| 195 |
*/ |
| 196 |
private function build_css($style_object) |
| 197 |
{ |
| 198 |
// $block_styles = (array)json_decode(stripslashes($style_object)); |
| 199 |
$block_styles = $style_object; |
| 200 |
|
| 201 |
$css = ''; |
| 202 |
foreach ($block_styles as $block_style_key => $block_style) { |
| 203 |
if (!empty($block_css = (array) $block_style)) { |
| 204 |
$css .= sprintf( |
| 205 |
'/* %1$s Starts */', |
| 206 |
$block_style_key |
| 207 |
); |
| 208 |
foreach ($block_css as $media => $style) { |
| 209 |
switch ($media) { |
| 210 |
case $this->media_desktop['name']: |
| 211 |
$css .= preg_replace('/\s+/', ' ', $style); |
| 212 |
break; |
| 213 |
case $this->media_tab['name']: |
| 214 |
$css .= ' @media(max-width: 1024px){'; |
| 215 |
$css .= preg_replace('/\s+/', ' ', $style); |
| 216 |
$css .= '}'; |
| 217 |
break; |
| 218 |
case $this->media_mobile['name']: |
| 219 |
$css .= ' @media(max-width: 767px){'; |
| 220 |
$css .= preg_replace('/\s+/', ' ', $style); |
| 221 |
$css .= '}'; |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
$css .= sprintf( |
| 226 |
'/* =%1$s= Ends */', |
| 227 |
$block_style_key |
| 228 |
); |
| 229 |
} |
| 230 |
} |
| 231 |
return trim($css); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Helper function to get string between 2 string |
| 236 |
* @since 3.3.0 |
| 237 |
*/ |
| 238 |
private function get_between_data($string, $start, $end) |
| 239 |
{ |
| 240 |
$pos_string = stripos($string, $start); |
| 241 |
$substr_data = substr($string, $pos_string); |
| 242 |
$string_two = substr($substr_data, strlen($start)); |
| 243 |
$second_pos = stripos($string_two, $end); |
| 244 |
$string_three = substr($string_two, 0, $second_pos); |
| 245 |
|
| 246 |
// remove whitespaces from result |
| 247 |
$result_unit = trim($string_three); |
| 248 |
|
| 249 |
// return result_unit |
| 250 |
return $result_unit; |
| 251 |
} |
| 252 |
} |
| 253 |
BetterdocsStyleHandler::init(); |
| 254 |
} |
| 255 |
|