PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.7
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.7
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.7, at blocks/style-handler/style-handler.php

277 lines 11.1 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 }
97 }
98
99 /**
100 * Ajax callback to write css in upload directory
101 *
102 * @retun void
103 * @since 1.0.2
104 */
105 public function write_block_css() {
106 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'nx_style_handler_nonce' ) || ! current_user_can( 'manage_options' ) ) {
107 echo 'Invalid request';
108 wp_die();
109 }
110
111 $block_styles = (array) json_decode( stripslashes( $_POST['data'] ) );
112
113 if ( isset( $_POST['editorType'] ) && $_POST['editorType'] === 'edit-site' ) {
114 $upload_dir = wp_upload_dir()['basedir'] . '/nx-style/';
115 $editSiteCssPath = $upload_dir . 'nx-style-' . $_POST['editorType'] . '.min.css';
116 if ( file_exists( $editSiteCssPath ) ) {
117 $existingCss = file_get_contents( $editSiteCssPath );
118 $pattern = '~\/\*(.*?)\*\/~';
119 preg_match_all( $pattern, $existingCss, $result, PREG_PATTERN_ORDER );
120 $allComments = $result[0];
121 $seperatedIds = array();
122 foreach ( $allComments as $comment ) {
123 $id = preg_replace( '/[^A-Za-z0-9\-]|Ends|Starts/', '', $comment );
124
125 if ( strpos( $comment, 'Starts' ) ) {
126 $seperatedIds[ $id ]['start'] = $comment;
127 } elseif ( strpos( $comment, 'Ends' ) ) {
128 $seperatedIds[ $id ]['end'] = $comment;
129 }
130 }
131
132 $seperateStyles = array();
133 foreach ( $seperatedIds as $key => $ids ) {
134 $data = $this->get_between_data( $existingCss, $ids['start'], $ids['end'] );
135 $seperateStyles[ $key ] = $data;
136 }
137
138 $finalCSSArray = array_merge( $seperateStyles, $block_styles );
139
140 if ( ! empty( $css = $this->build_css( $finalCSSArray ) ) ) {
141 $upload_dir = wp_upload_dir()['basedir'] . '/nx-style/';
142 if ( ! file_exists( $upload_dir ) ) {
143 mkdir( $upload_dir );
144 }
145
146 file_put_contents( $editSiteCssPath, $css );
147 }
148 } else {
149 if ( ! empty( $css = $this->build_css( $block_styles ) ) ) {
150 $upload_dir = wp_upload_dir()['basedir'] . '/nx-style/';
151 if ( ! file_exists( $upload_dir ) ) {
152 mkdir( $upload_dir );
153 }
154
155 file_put_contents( $editSiteCssPath, $css );
156 }
157 }
158 } else {
159 if ( ! empty( $css = $this->build_css( $block_styles ) ) ) {
160 $upload_dir = wp_upload_dir()['basedir'] . '/nx-style/';
161 if ( ! file_exists( $upload_dir ) ) {
162 mkdir( $upload_dir );
163 }
164 file_put_contents( $upload_dir . 'nx-style-' . abs( $_POST['id'] ) . '.min.css', $css );
165 }
166 }
167
168 wp_die();
169 }
170
171 /**
172 * Enqueue frontend css for post if have one
173 *
174 * @return void
175 * @since 1.0.2
176 */
177 public function enqueue_frontend_css() {
178 global $post;
179 if ( ! empty( $post ) && ! empty( $post->ID ) ) {
180 $upload_dir = wp_upload_dir();
181 $args = [ 'enabled' => true, 'source' => 'press_bar', 'updated_at' => ['<=', current_time('mysql') ] ];
182 $pressbar_notifications = \NotificationX\Core\PostType::get_instance()->get_posts($args);
183 foreach ($pressbar_notifications as $pressbar) {
184 if( !empty( $pressbar['gutenberg_id'] ) ) {
185 $style_file = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'eb-style' . DIRECTORY_SEPARATOR . $this->eb_prefix . '-' . $pressbar['gutenberg_id'] . '.min.css';
186 $style_url = set_url_scheme($upload_dir['baseurl']) . '/' . 'eb-style' . '/' . $this->eb_prefix . '-' . $pressbar['gutenberg_id'] . '.min.css';
187 if (file_exists($style_file)) {
188 wp_enqueue_style(
189 'nx-style-' . $pressbar['gutenberg_id'], // Handle based only on post ID
190 $style_url,
191 [],
192 filemtime($style_file) // Cache busting with last modified time
193 );
194 }
195 }
196
197 }
198
199 if ( file_exists( $upload_dir['basedir'] . '/nx-style/nx-style-' . $post->ID . '.min.css' ) ) {
200 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 ) );
201 } elseif ( function_exists( 'icl_object_id' ) ) {
202 $default_language = apply_filters( 'wpml_default_language', null );
203 $english_version = icl_object_id( $post->ID, 'post', false, $default_language );
204 if ( file_exists( $upload_dir['basedir'] . '/nx-style/nx-style-' . $english_version . '.min.css' ) ) {
205 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 ) );
206 }
207 }
208 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && file_exists( $upload_dir['basedir'] . '/nx-style/nx-style-edit-site.min.css' ) ) {
209 wp_enqueue_style( 'nx-fullsite-style', $upload_dir['baseurl'] . '/nx-style/nx-style-edit-site.min.css', [], substr( md5( microtime( true ) ), 0, 10 ) );
210 }
211 }
212 }
213
214 /**
215 * Enqueue frontend css for post if have one
216 *
217 * @param string
218 * @return string
219 * @since 1.0.2
220 */
221 private function build_css( $style_object ) {
222 // $block_styles = (array)json_decode(stripslashes($style_object));
223 $block_styles = $style_object;
224
225 $css = '';
226 foreach ( $block_styles as $block_style_key => $block_style ) {
227 if ( ! empty( $block_css = (array) $block_style ) ) {
228 $css .= sprintf(
229 '/* %1$s Starts */',
230 $block_style_key
231 );
232 foreach ( $block_css as $media => $style ) {
233 switch ( $media ) {
234 case $this->media_desktop['name']:
235 $css .= preg_replace( '/\s+/', ' ', $style );
236 break;
237 case $this->media_tab['name']:
238 $css .= ' @media(max-width: 1024px){';
239 $css .= preg_replace( '/\s+/', ' ', $style );
240 $css .= '}';
241 break;
242 case $this->media_mobile['name']:
243 $css .= ' @media(max-width: 767px){';
244 $css .= preg_replace( '/\s+/', ' ', $style );
245 $css .= '}';
246 break;
247 }
248 }
249 $css .= sprintf(
250 '/* =%1$s= Ends */',
251 $block_style_key
252 );
253 }
254 }
255 return trim( $css );
256 }
257
258 /**
259 * Helper function to get string between 2 string
260 *
261 * @since 3.3.0
262 */
263 private function get_between_data( $string, $start, $end ) {
264 $pos_string = stripos( $string, $start );
265 $substr_data = substr( $string, $pos_string );
266 $string_two = substr( $substr_data, strlen( $start ) );
267 $second_pos = stripos( $string_two, $end );
268 $string_three = substr( $string_two, 0, $second_pos );
269
270 // remove whitespaces from result
271 $result_unit = trim( $string_three );
272
273 // return result_unit
274 return $result_unit;
275 }
276 }
277