PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.10
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.10
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / blocks / style-handler / style-handler.php

style-handler.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.10, at blocks/style-handler/style-handler.php

285 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace NotificationX\Blocks;
4
5 use NotificationX\GetInstance;
6
7 /**
8 *
9 * @method static StyleHandler get_instance($args = null)
10 */
11 final class StyleHandler {
12 /**
13 * Instance of NotificationX
14 *
15 * @var StyleHandler
16 */
17 use GetInstance;
18
19 private $media_desktop = [
20 'name' => 'desktop',
21 'screen_size' => '',
22 ];
23 private $media_tab = [
24 'name' => 'tab',
25 'screen_size' => 1024,
26 ];
27 private $media_mobile = [
28 'name' => 'mobile',
29 'screen_size' => 767,
30 ];
31 private $eb_prefix = 'eb-style';
32
33 public function __construct() {
34 add_action( 'admin_enqueue_scripts', [ $this, 'notificationx_pro_blocks_edit_post' ] );
35 add_action( 'wp_ajax_notificationx_pro_write_block_css', [ $this, 'write_block_css' ] );
36 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_frontend_css' ] );
37 }
38
39 /**
40 * Enqueue a script in the WordPress admin on edit.php.
41 *
42 * @param int $hook Hook suffix for the current admin page.
43 */
44 public function notificationx_pro_blocks_edit_post( $hook ) {
45 $current_screen = get_current_screen();
46 $post_type = !empty( $current_screen->post_type ) ? $current_screen->post_type : '';
47 $dir = dirname( __FILE__ );
48 if ( ($hook == 'post-new.php' || $hook == 'post.php') && $post_type == 'nx_bar_eb' ) {
49 wp_enqueue_script(
50 'notificationx-pro-blocks-edit-post',
51 NOTIFICATIONX_URL . 'blocks/style-handler/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( NOTIFICATIONX_PATH . 'blocks/style-handler/style-handler.js' ),
65 true
66 );
67 wp_localize_script('notificationx-pro-blocks-edit-post', 'nx_style_handler', [
68 'sth_nonce' => wp_create_nonce( 'nx_style_handler_nonce' ),
69 'editor_type' => 'edit-post',
70 ]
71 );
72 } elseif ( $hook == 'site-editor.php' ) {
73 wp_enqueue_script(
74 'notificationx-pro-blocks-edit-post',
75 NOTIFICATIONX_URL . 'blocks/style-handler/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( NOTIFICATIONX_PATH . 'blocks/style-handler/style-handler.js' ),
89 true
90 );
91 wp_localize_script('notificationx-pro-blocks-edit-post', 'nx_style_handler', [
92 'sth_nonce' => wp_create_nonce( 'nx_style_handler_nonce' ),
93 'editor_type' => 'edit-site',
94 ]
95 );
96 // Override the default 'edit-post' global (set on the controls handle
97 // in Blocks.php) so the inline block reads the core/edit-site store
98 // when used inside the Full Site Editor.
99 wp_localize_script('notificationx-block-controls', 'nx_style_handler', [
100 'sth_nonce' => wp_create_nonce( 'nx_style_handler_nonce' ),
101 'editor_type' => 'edit-site',
102 ]
103 );
104 }
105 }
106
107 /**
108 * Ajax callback to write css in upload directory
109 *
110 * @retun void
111 * @since 1.0.2
112 */
113 public function write_block_css() {
114 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'nx_style_handler_nonce' ) || ! current_user_can( 'manage_options' ) ) {
115 echo 'Invalid request';
116 wp_die();
117 }
118
119 $block_styles = (array) json_decode( stripslashes( $_POST['data'] ) );
120
121 if ( isset( $_POST['editorType'] ) && $_POST['editorType'] === 'edit-site' ) {
122 $upload_dir = wp_upload_dir()['basedir'] . '/nx-style/';
123 $editSiteCssPath = $upload_dir . 'nx-style-' . $_POST['editorType'] . '.min.css';
124 if ( file_exists( $editSiteCssPath ) ) {
125 $existingCss = file_get_contents( $editSiteCssPath );
126 $pattern = '~\/\*(.*?)\*\/~';
127 preg_match_all( $pattern, $existingCss, $result, PREG_PATTERN_ORDER );
128 $allComments = $result[0];
129 $seperatedIds = array();
130 foreach ( $allComments as $comment ) {
131 $id = preg_replace( '/[^A-Za-z0-9\-]|Ends|Starts/', '', $comment );
132
133 if ( strpos( $comment, 'Starts' ) ) {
134 $seperatedIds[ $id ]['start'] = $comment;
135 } elseif ( strpos( $comment, 'Ends' ) ) {
136 $seperatedIds[ $id ]['end'] = $comment;
137 }
138 }
139
140 $seperateStyles = array();
141 foreach ( $seperatedIds as $key => $ids ) {
142 $data = $this->get_between_data( $existingCss, $ids['start'], $ids['end'] );
143 $seperateStyles[ $key ] = $data;
144 }
145
146 $finalCSSArray = array_merge( $seperateStyles, $block_styles );
147
148 if ( ! empty( $css = $this->build_css( $finalCSSArray ) ) ) {
149 $upload_dir = wp_upload_dir()['basedir'] . '/nx-style/';
150 if ( ! file_exists( $upload_dir ) ) {
151 mkdir( $upload_dir );
152 }
153
154 file_put_contents( $editSiteCssPath, $css );
155 }
156 } else {
157 if ( ! empty( $css = $this->build_css( $block_styles ) ) ) {
158 $upload_dir = wp_upload_dir()['basedir'] . '/nx-style/';
159 if ( ! file_exists( $upload_dir ) ) {
160 mkdir( $upload_dir );
161 }
162
163 file_put_contents( $editSiteCssPath, $css );
164 }
165 }
166 } else {
167 if ( ! empty( $css = $this->build_css( $block_styles ) ) ) {
168 $upload_dir = wp_upload_dir()['basedir'] . '/nx-style/';
169 if ( ! file_exists( $upload_dir ) ) {
170 mkdir( $upload_dir );
171 }
172 file_put_contents( $upload_dir . 'nx-style-' . abs( $_POST['id'] ) . '.min.css', $css );
173 }
174 }
175
176 wp_die();
177 }
178
179 /**
180 * Enqueue frontend css for post if have one
181 *
182 * @return void
183 * @since 1.0.2
184 */
185 public function enqueue_frontend_css() {
186 global $post;
187 if ( ! empty( $post ) && ! empty( $post->ID ) ) {
188 $upload_dir = wp_upload_dir();
189 $args = [ 'enabled' => true, 'source' => 'press_bar', 'updated_at' => ['<=', current_time('mysql') ] ];
190 $pressbar_notifications = \NotificationX\Core\PostType::get_instance()->get_posts($args);
191 foreach ($pressbar_notifications as $pressbar) {
192 if( !empty( $pressbar['gutenberg_id'] ) ) {
193 $style_file = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'eb-style' . DIRECTORY_SEPARATOR . $this->eb_prefix . '-' . $pressbar['gutenberg_id'] . '.min.css';
194 $style_url = set_url_scheme($upload_dir['baseurl']) . '/' . 'eb-style' . '/' . $this->eb_prefix . '-' . $pressbar['gutenberg_id'] . '.min.css';
195 if (file_exists($style_file)) {
196 wp_enqueue_style(
197 'nx-style-' . $pressbar['gutenberg_id'], // Handle based only on post ID
198 $style_url,
199 [],
200 filemtime($style_file) // Cache busting with last modified time
201 );
202 }
203 }
204
205 }
206
207 if ( file_exists( $upload_dir['basedir'] . '/nx-style/nx-style-' . $post->ID . '.min.css' ) ) {
208 wp_enqueue_style( 'nx-block-style-' . $post->ID, $upload_dir['baseurl'] . '/nx-style/nx-style-' . $post->ID . '.min.css', [], substr( md5( microtime( true ) ), 0, 10 ) );
209 } elseif ( function_exists( 'icl_object_id' ) ) {
210 $default_language = apply_filters( 'wpml_default_language', null );
211 $english_version = icl_object_id( $post->ID, 'post', false, $default_language );
212 if ( file_exists( $upload_dir['basedir'] . '/nx-style/nx-style-' . $english_version . '.min.css' ) ) {
213 wp_enqueue_style( 'nx-block-style-' . $english_version, $upload_dir['baseurl'] . '/nx-style/nx-style-' . $english_version . '.min.css', [], substr( md5( microtime( true ) ), 0, 10 ) );
214 }
215 }
216 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && file_exists( $upload_dir['basedir'] . '/nx-style/nx-style-edit-site.min.css' ) ) {
217 wp_enqueue_style( 'nx-fullsite-style', $upload_dir['baseurl'] . '/nx-style/nx-style-edit-site.min.css', [], substr( md5( microtime( true ) ), 0, 10 ) );
218 }
219 }
220 }
221
222 /**
223 * Enqueue frontend css for post if have one
224 *
225 * @param string
226 * @return string
227 * @since 1.0.2
228 */
229 private function build_css( $style_object ) {
230 // $block_styles = (array)json_decode(stripslashes($style_object));
231 $block_styles = $style_object;
232
233 $css = '';
234 foreach ( $block_styles as $block_style_key => $block_style ) {
235 if ( ! empty( $block_css = (array) $block_style ) ) {
236 $css .= sprintf(
237 '/* %1$s Starts */',
238 $block_style_key
239 );
240 foreach ( $block_css as $media => $style ) {
241 switch ( $media ) {
242 case $this->media_desktop['name']:
243 $css .= preg_replace( '/\s+/', ' ', $style );
244 break;
245 case $this->media_tab['name']:
246 $css .= ' @media(max-width: 1024px){';
247 $css .= preg_replace( '/\s+/', ' ', $style );
248 $css .= '}';
249 break;
250 case $this->media_mobile['name']:
251 $css .= ' @media(max-width: 767px){';
252 $css .= preg_replace( '/\s+/', ' ', $style );
253 $css .= '}';
254 break;
255 }
256 }
257 $css .= sprintf(
258 '/* =%1$s= Ends */',
259 $block_style_key
260 );
261 }
262 }
263 return trim( $css );
264 }
265
266 /**
267 * Helper function to get string between 2 string
268 *
269 * @since 3.3.0
270 */
271 private function get_between_data( $string, $start, $end ) {
272 $pos_string = stripos( $string, $start );
273 $substr_data = substr( $string, $pos_string );
274 $string_two = substr( $substr_data, strlen( $start ) );
275 $second_pos = stripos( $string_two, $end );
276 $string_three = substr( $string_two, 0, $second_pos );
277
278 // remove whitespaces from result
279 $result_unit = trim( $string_three );
280
281 // return result_unit
282 return $result_unit;
283 }
284 }
285