PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.3.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.3.5
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / gutenberg / class-style-handler.php

class-style-handler.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 2.3.5, at includes/gutenberg/class-style-handler.php

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