PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.2.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.2.0
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 / Editors / BlockEditor / StyleHandler.php

StyleHandler.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.2.0, at includes/Editors/BlockEditor/StyleHandler.php

239 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPDeveloper\BetterDocs\Editors\BlockEditor;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 use WPDeveloper\BetterDocs\Utils\Base;
9
10 final class StyleHandler extends Base {
11 private $media_desktop = [
12 'name' => 'desktop',
13 'screen_size' => ''
14 ];
15
16 private $media_hover_desktop = [
17 'name' => 'desktopHover',
18 'screen_size' => ''
19 ];
20
21 private $media_tab = [
22 'name' => 'tab',
23 'screen_size' => 1024
24 ];
25
26 private $media_hover_tab = [
27 'name' => 'tabHover',
28 'screen_size' => 1024
29 ];
30
31 private $media_mobile = [
32 'name' => 'mobile',
33 'screen_size' => 767
34 ];
35
36 private $media_hover_mobile = [
37 'name' => 'mobileHover',
38 'screen_size' => 767
39 ];
40
41 private $extra_styles = [
42 'name' => 'extraStyles',
43 'screen_size' => ''
44 ];
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 }
50
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 }
59
60 if ( $hook != 'post.php' && $hook != 'post-new.php' && $hook != 'site-editor.php' ) {
61 return;
62 }
63
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 ];
75
76 $current_screen = get_current_screen();
77
78 $_style_handler_object = [
79 'sth_nonce' => wp_create_nonce( 'betterdocs_style_handler_nonce' ),
80 'editor_type' => 'edit-site'
81 ];
82
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 }
90
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 }
94
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 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'betterdocs_style_handler_nonce' ) || ! current_user_can( 'manage_options' ) ) {
102 echo 'Invalid request';
103 wp_die();
104 }
105
106 $block_styles = (array) json_decode( stripslashes( $_POST['data'] ) );
107
108 if ( isset( $_POST['editorType'] ) && $_POST['editorType'] === 'edit-site' ) {
109 $upload_dir = wp_upload_dir()['basedir'] . '/betterdocs-style/';
110 $editSiteCssPath = $upload_dir . 'betterdocs-style-' . $_POST['editorType'] . '.min.css';
111 if ( file_exists( $editSiteCssPath ) ) {
112 $existingCss = file_get_contents( $editSiteCssPath );
113 $pattern = '~\/\*(.*?)\*\/~';
114 preg_match_all( $pattern, $existingCss, $result, PREG_PATTERN_ORDER );
115 $allComments = $result[0];
116 $seperatedIds = [];
117 foreach ( $allComments as $comment ) {
118 $id = preg_replace( '/[^A-Za-z0-9\-]|Ends|Starts/', '', $comment );
119
120 if ( strpos( $comment, 'Starts' ) ) {
121 $seperatedIds[ $id ]['start'] = $comment;
122 } elseif ( strpos( $comment, 'Ends' ) ) {
123 $seperatedIds[ $id ]['end'] = $comment;
124 }
125 }
126
127 $seperateStyles = [];
128 foreach ( $seperatedIds as $key => $ids ) {
129 $data = $this->get_between_data( $existingCss, $ids['start'], $ids['end'] );
130 $seperateStyles[ $key ] = $data;
131 }
132
133 $finalCSSArray = array_merge( $seperateStyles, $block_styles );
134
135 if ( ! empty( $css = $this->build_css( $finalCSSArray ) ) ) {
136 $upload_dir = wp_upload_dir()['basedir'] . '/betterdocs-style/';
137 if ( ! file_exists( $upload_dir ) ) {
138 mkdir( $upload_dir );
139 }
140
141 file_put_contents( $editSiteCssPath, $css );
142 }
143 } elseif ( ! 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 }
148
149 file_put_contents( $editSiteCssPath, $css );
150 }
151 } elseif ( ! empty( $css = $this->build_css( $block_styles ) ) ) {
152 $upload_dir = wp_upload_dir()['basedir'] . '/betterdocs-style/';
153 if ( ! file_exists( $upload_dir ) ) {
154 mkdir( $upload_dir );
155 }
156 file_put_contents( $upload_dir . 'betterdocs-style-' . abs( $_POST['id'] ) . '.min.css', $css );
157 }
158
159 wp_send_json_success( 'done' );
160 }
161
162 /**
163 * Enqueue frontend css for post if have one
164 * @param array
165 * @return string
166 * @since 1.0.2
167 */
168 private function build_css( $style_object ) {
169 // $block_styles = (array)json_decode(stripslashes($style_object));
170 $block_styles = $style_object;
171
172 $css = '';
173 foreach ( $block_styles as $block_style_key => $block_style ) {
174 if ( ! empty( $block_css = (array) $block_style ) ) {
175 $css .= sprintf(
176 '/* %1$s Starts */',
177 $block_style_key
178 );
179 foreach ( $block_css as $media => $style ) {
180 switch ( $media ) {
181 case $this->media_desktop['name']:
182 $css .= preg_replace( '/\s+/', ' ', $style );
183 break;
184 case $this->media_hover_desktop['name']:
185 $css .= preg_replace( '/\s+/', ' ', $style );
186 break;
187 case $this->media_tab['name']:
188 $css .= ' @media(max-width: 1024px){';
189 $css .= preg_replace( '/\s+/', ' ', $style );
190 $css .= '}';
191 break;
192 case $this->media_hover_tab['name']:
193 $css .= ' @media(max-width: 1024px){';
194 $css .= preg_replace( '/\s+/', ' ', $style );
195 $css .= '}';
196 break;
197 case $this->media_mobile['name']:
198 $css .= ' @media(max-width: 767px){';
199 $css .= preg_replace( '/\s+/', ' ', $style );
200 $css .= '}';
201 break;
202 case $this->media_hover_mobile['name']:
203 $css .= ' @media(max-width: 767px){';
204 $css .= preg_replace( '/\s+/', ' ', $style );
205 $css .= '}';
206 break;
207 case $this->extra_styles['name']:
208 $css .= preg_replace( '/\s+/', ' ', $style );
209 break;
210 }
211 }
212 $css .= sprintf(
213 '/* =%1$s= Ends */',
214 $block_style_key
215 );
216 }
217 }
218 return trim( $css );
219 }
220
221 /**
222 * Helper function to get string between 2 string
223 * @since 3.3.0
224 */
225 private function get_between_data( $string, $start, $end ) {
226 $pos_string = stripos( $string, $start );
227 $substr_data = substr( $string, $pos_string );
228 $string_two = substr( $substr_data, strlen( $start ) );
229 $second_pos = stripos( $string_two, $end );
230 $string_three = substr( $string_two, 0, $second_pos );
231
232 // remove whitespaces from result
233 $result_unit = trim( $string_three );
234
235 // return result_unit
236 return $result_unit;
237 }
238 }
239