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