PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.8
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.8
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 / Blocks.php

Blocks.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.8, at blocks/Blocks.php

188 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions to register client-side assets (scripts and stylesheets) for the
4 * Gutenberg block.
5 *
6 * @package notificationx
7 */
8
9 namespace NotificationX\Blocks;
10
11 use NotificationX\Core\Helper;
12 use NotificationX\GetInstance;
13
14 /**
15 *
16 * @method static Blocks get_instance($args = null)
17 */
18 class Blocks {
19 /**
20 * Instance of NotificationX
21 *
22 * @var Blocks
23 */
24 use GetInstance;
25
26
27 public function __construct() {
28 StyleHandler::get_instance();
29 add_action( 'init', [ $this, 'notificationx_block_init' ] );
30 }
31
32 /**
33 * Registers all block assets so that they can be enqueued through Gutenberg in
34 * the corresponding context.
35 *
36 * @see https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/block-tutorial/applying-styles-with-stylesheets/
37 */
38 function notificationx_block_init() {
39 // Skip block registration if Gutenberg is not enabled/merged.
40 if ( ! function_exists( 'register_block_type' ) ) {
41 return;
42 }
43 $dir = dirname( __FILE__ );
44
45 // Enqueue Controls CSS & JS
46 $controls_css = 'controls/dist/index.css';
47 wp_register_style(
48 'notificationx-block-controls-css',
49 plugins_url( $controls_css, __FILE__ ),
50 [],
51 filemtime( "{$dir}/{$controls_css}" )
52 );
53
54 $asset_file = include NOTIFICATIONX_PATH . 'blocks/controls/dist/index.asset.php';
55 $index_js = 'controls/dist/index.js';
56 wp_register_script(
57 'notificationx-block-controls',
58 plugins_url( $index_js, __FILE__ ),
59 $asset_file['dependencies'],
60 $asset_file['version'],
61 false
62 );
63
64 $asset_file = include NOTIFICATIONX_PATH . 'blocks/notificationx/index.asset.php';
65 $asset_file['dependencies'][] = 'notificationx-pro-blocks-edit-post';
66 $index_js = 'notificationx/index.js';
67 wp_register_script(
68 'notificationx-block-editor',
69 plugins_url( $index_js, __FILE__ ),
70 array_merge($asset_file['dependencies'], ['notificationx-block-controls']),
71 $asset_file['version']
72 );
73
74 $editor_css = 'notificationx/editor.css';
75 wp_register_style(
76 'notificationx-block-editor',
77 plugins_url( $editor_css, __FILE__ ),
78 array( 'notificationx-block-controls-css' ),
79 filemtime( "{$dir}/{$editor_css}" )
80 );
81
82 $style_css = 'notificationx/style.css';
83 wp_register_style(
84 'notificationx-block',
85 plugins_url( $style_css, __FILE__ ),
86 [],
87 filemtime( "{$dir}/{$style_css}" )
88 );
89 wp_register_script(
90 'notificationx-block-frontend',
91 plugins_url( 'notificationx/frontend.js', __FILE__ ),
92 [],
93 filemtime( "{$dir}/notificationx/frontend.js" ),
94 true
95 );
96 wp_localize_script('notificationx-block-frontend', 'notificationxBlockRest', [
97 'root' => rest_url(),
98 ]);
99 register_block_type( 'notificationx-pro/notificationx',
100 [
101 'editor_script' => 'notificationx-block-editor',
102 'editor_style' => 'notificationx-block-editor',
103 // 'style' => 'notificationx-block',
104 // 'script' => 'notificationx-block-frontend',
105 'render_callback' => [ $this, 'notificationx_render_callback' ],
106 'attributes' => array(
107 'nx_id' => array(
108 'type' => 'string',
109 ),
110 'blockId' => array(
111 'type' => 'string',
112 ),
113 'product_id' => array(
114 'type' => 'string',
115 ),
116 ),
117 ]
118 );
119 register_block_type( 'notificationx-pro/notificationx-render',
120 [
121 'render_callback' => [ $this, 'gutenberg_examples_dynamic_render_callback' ],
122 'attributes' => array(
123 'nx_id' => array(
124 'type' => 'string',
125 ),
126 'blockId' => array(
127 'type' => 'string',
128 ),
129 'product_id' => array(
130 'type' => 'string',
131 ),
132 'post_type' => array(
133 'type' => 'string',
134 ),
135 ),
136 ]
137 );
138 }
139
140 function notificationx_render_callback( $block_attributes, $content ) {
141 if( ! is_admin() ){
142 wp_enqueue_style('notificationx-block');
143 wp_enqueue_script('notificationx-block-frontend');
144 }
145 if ( is_admin() || $this->isRestUrl() ) {
146 do_action( 'nx_ignore_analytics' );
147 }
148 $nx_id = ! empty( $block_attributes['nx_id'] ) ? esc_attr($block_attributes['nx_id']) : '';
149 $product_id = ! empty( $block_attributes['product_id'] ) ? $block_attributes['product_id'] : '';
150 $block_id = ! empty( $block_attributes['blockId'] ) ? esc_attr($block_attributes['blockId']) : '';
151 $html = '<div class="' . $block_id . ' notificationx-block-wrapper" data-nx_id="' . $nx_id . '">';
152 $html .= do_shortcode( "[notificationx_inline product_id='{$product_id}' id='{$nx_id}']" );
153 $html .= '</div>';
154 return $html;
155 }
156
157 function gutenberg_examples_dynamic_render_callback( $block_attributes, $content ) {
158 do_action( 'nx_ignore_analytics' );
159 $nx_id = ! empty( $block_attributes['nx_id'] ) ? esc_attr($block_attributes['nx_id']) : '';
160 $product_id = ! empty( $block_attributes['product_id'] ) ? $block_attributes['product_id'] : '';
161 $post_type = ! empty( $block_attributes['post_type'] ) ? $block_attributes['post_type'] : '';
162 $html = '<div class="' . $block_attributes['blockId'] . ' notificationx-block-wrapper">';
163 if( 'wp_template' == $post_type ) {
164 add_filter('nx_is_preview',function(){
165 return true;
166 });
167 $product_id = rand();
168 }
169 $shortcode = do_shortcode( "[notificationx_inline post_type='{$post_type}' product_id='{$product_id}' id='{$nx_id}' show_link=false]" );
170 if ( $shortcode ) {
171 $html .= $shortcode;
172 } else {
173 $html .= '<p class="nx-shortcode-notice">' . __( 'There is no data in this notification.', 'notificationx' ) . '</p>';
174 }
175 $html .= '</div>';
176
177 return wp_kses($html, Helper::nx_allowed_html());
178 }
179
180 function isRestUrl() {
181 if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
182 return false;
183 }
184 return true;
185 }
186
187 }
188