| 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, 'better_docs_edit_post']); |
| 37 |
add_action('wp_ajax_better_docs_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 better_docs_edit_post($hook) |
| 46 |
{ |
| 47 |
global $post; |
| 48 |
if ($hook == 'post-new.php' || $hook == 'post.php') { |
| 49 |
$frontend_js = "style-handler.js"; |
| 50 |
wp_enqueue_script('better-docs-edit-post', BETTERDOCS_URL . "public/js/style-handler.js", array("jquery", "wp-editor"), BETTERDOCS_VERSION, true); |
| 51 |
wp_localize_script('better-docs-edit-post', 'better_docs_style_handler', [ |
| 52 |
'sth_nonce' => wp_create_nonce('better_docs_style_handler_nonce') |
| 53 |
]); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Ajax callback to write css in upload directory |
| 59 |
* @retun void |
| 60 |
* @since 1.0.2 |
| 61 |
*/ |
| 62 |
public function write_block_css() |
| 63 |
{ |
| 64 |
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'better_docs_style_handler_nonce') || !current_user_can('manage_options')) { |
| 65 |
echo 'Invalid request'; |
| 66 |
wp_die(); |
| 67 |
} |
| 68 |
if (!empty($css = $this->build_css($_POST['data']))) { |
| 69 |
$upload_dir = wp_upload_dir()['basedir'] . '/betterdocs-style/'; |
| 70 |
if (!file_exists($upload_dir)) { |
| 71 |
mkdir($upload_dir); |
| 72 |
} |
| 73 |
file_put_contents($upload_dir . 'betterdocs-style-' . abs($_POST['id']) . '.min.css', $css); |
| 74 |
} |
| 75 |
|
| 76 |
wp_die(); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Enqueue frontend css for post if have one |
| 81 |
* @return void |
| 82 |
* @since 1.0.2 |
| 83 |
*/ |
| 84 |
public function enqueue_frontend_css() |
| 85 |
{ |
| 86 |
global $post; |
| 87 |
if (!empty($post) && !empty($post->ID)) { |
| 88 |
$upload_dir = wp_upload_dir(); |
| 89 |
if (file_exists($upload_dir['basedir'] . '/betterdocs-style/betterdocs-style-' . $post->ID . '.min.css')) { |
| 90 |
wp_enqueue_style('eb-block-style-' . $post->ID, $upload_dir['baseurl'] . '/betterdocs-style/betterdocs-style-' . $post->ID . '.min.css', [], substr(md5(microtime(true)), 0, 10)); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Enqueue frontend css for post if have one |
| 97 |
* @param string |
| 98 |
* @return string |
| 99 |
* @since 1.0.2 |
| 100 |
*/ |
| 101 |
private function build_css($style_object) |
| 102 |
{ |
| 103 |
$block_styles = (array)json_decode(stripslashes($style_object)); |
| 104 |
|
| 105 |
$css = ''; |
| 106 |
foreach ($block_styles as $block_style) { |
| 107 |
if (!empty($block_css = (array) $block_style)) { |
| 108 |
foreach ($block_css as $media => $style) { |
| 109 |
switch ($media) { |
| 110 |
case $this->media_desktop['name']: |
| 111 |
$css .= preg_replace('/\s+/', ' ', $style); |
| 112 |
break; |
| 113 |
case $this->media_tab['name']: |
| 114 |
$css .= ' @media(max-width: 1024px){'; |
| 115 |
$css .= preg_replace('/\s+/', ' ', $style); |
| 116 |
$css .= '}'; |
| 117 |
break; |
| 118 |
case $this->media_mobile['name']: |
| 119 |
$css .= ' @media(max-width: 767px){'; |
| 120 |
$css .= preg_replace('/\s+/', ' ', $style); |
| 121 |
$css .= '}'; |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
return trim($css); |
| 128 |
} |
| 129 |
} |
| 130 |
BetterDocsStyleHandler::init(); |
| 131 |
} |
| 132 |
|