PluginProbe
YayCurrency – WooCommerce Multi-Currency Switcher / trunk
YayCurrency – WooCommerce Multi-Currency Switcher vtrunk
3.3.5 trunk 2.0.1 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.1 2.2 2.3 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 3.0.0 3.0.1 3.0.2 3.1 3.2 3.3 3.3.1 All 28 releases
yaycurrency / blocks / init.php

init.php in YayCurrency – WooCommerce Multi-Currency Switcher trunk, at blocks/init.php

200 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Currency Switcher Gutenberg Block for YayCurrency
4 */
5
6 use Yay_Currency\Helpers\Helper;
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Detect if current request is background or automated (E2E, REST, AJAX, etc.)
12 */
13 function yaycurrency_is_background_or_e2e() {
14
15 // REST API, AJAX, CRON, WP-CLI → skip heavy editor loading
16 if ( ( defined( 'REST_REQUEST' ) && REST_REQUEST )
17 || wp_doing_ajax()
18 || ( defined( 'DOING_CRON' ) && DOING_CRON )
19 || ( defined( 'WP_CLI' ) && WP_CLI )
20 ) {
21 // error_log( 'E2E/background request detected: REST/AJAX/CRON/CLI' );
22 return true;
23 }
24
25 // Localhost test environments (common in Woo E2E, wp-env, Playwright)
26 if ( isset( $_SERVER['HTTP_HOST'] ) && preg_match( '/(localhost|127\.0\.0\.1|wp-env)/', sanitize_text_field( $_SERVER['HTTP_HOST'] ) ) ) {
27 // error_log( 'E2E/background request detected: Localhost environment' );
28 return true;
29 }
30
31 // User-Agent heuristic (Playwright / Puppeteer / Cypress / Woo E2E)
32 if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
33 $ua = strtolower( sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ) );
34 $patterns = [
35 'playwright',
36 'puppeteer',
37 'cypress',
38 'woocommerce e2e',
39 'wp-env',
40 'node/',
41 'postman',
42 'curl',
43 'insomnia',
44 'axios',
45 ];
46 foreach ( $patterns as $keyword ) {
47 if ( strpos( $ua, $keyword ) !== false ) {
48 // error_log( "E2E/background request detected: UA contains '{$keyword}'" );
49 return true;
50 }
51 }
52 }
53
54 // Common REST route used by E2E: creating posts/pages
55 return false;
56 }
57
58 /**
59 * Shared block attributes
60 */
61 function yaycurrency_get_block_attributes() {
62 return array(
63 'currencyName' => array(
64 'type' => 'string',
65 'default' => 'United States dollar',
66 ),
67 'currencySymbol' => array(
68 'type' => 'string',
69 'default' => '($)',
70 ),
71 'hyphen' => array(
72 'type' => 'string',
73 'default' => ' - ',
74 ),
75 'currencyCode' => array(
76 'type' => 'string',
77 'default' => 'USD',
78 ),
79 'isShowFlag' => array(
80 'type' => 'boolean',
81 'default' => true,
82 ),
83 'isShowCurrencyName' => array(
84 'type' => 'boolean',
85 'default' => true,
86 ),
87 'isShowCurrencySymbol' => array(
88 'type' => 'boolean',
89 'default' => true,
90 ),
91 'isShowCurrencyCode' => array(
92 'type' => 'boolean',
93 'default' => true,
94 ),
95 'widgetSize' => array(
96 'type' => 'string',
97 'default' => 'small',
98 ),
99 );
100 }
101
102 /**
103 * Register the Currency Switcher Gutenberg block.
104 */
105 function yaycurrency_register_currency_switcher_block() {
106 $block_name = 'yay-currency/currency-switcher';
107 $attributes = yaycurrency_get_block_attributes();
108
109 // Case 1: E2E or background → Register lightweight fallback only
110 if ( yaycurrency_is_background_or_e2e() ) {
111 // error_log( 'YayCurrency: registering lightweight fallback block (E2E/background)' );
112 register_block_type(
113 $block_name,
114 array(
115 'attributes' => $attributes,
116 'render_callback' => 'yaycurrency_render_currency_switcher',
117 )
118 );
119 return;
120 }
121
122 // Case 2: Admin/editor → Register full block assets
123 if ( is_admin() ) {
124 $asset_path = plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
125 if ( ! file_exists( $asset_path ) ) {
126 // error_log( 'YayCurrency: missing index.asset.php, registering fallback block.' );
127 register_block_type(
128 $block_name,
129 array(
130 'attributes' => $attributes,
131 'render_callback' => 'yaycurrency_render_currency_switcher',
132 )
133 );
134 return;
135 }
136
137 $asset_file = include $asset_path;
138
139 wp_register_script(
140 'yaycurrency-block-editor',
141 plugins_url( 'build/index.js', __FILE__ ),
142 $asset_file['dependencies'],
143 $asset_file['version'],
144 true
145 );
146
147 wp_localize_script(
148 'yaycurrency-block-editor',
149 'yayCurrencyGutenberg',
150 [
151 'nonce' => wp_create_nonce( 'yay-currency-gutenberg-nonce' ),
152 'yayCurrencyPluginURL' => YAY_CURRENCY_PLUGIN_URL,
153 ]
154 );
155
156 wp_register_style(
157 'yaycurrency-block-style',
158 plugins_url( 'style.css', __FILE__ ),
159 [],
160 filemtime( plugin_dir_path( __FILE__ ) . 'style.css' )
161 );
162
163 register_block_type(
164 $block_name,
165 [
166 'attributes' => $attributes,
167 'style' => 'yaycurrency-block-style',
168 'editor_script' => 'yaycurrency-block-editor',
169 'render_callback' => 'yaycurrency_render_currency_switcher',
170 ]
171 );
172 return;
173 }
174
175 // Case 3: Frontend fallback
176 register_block_type(
177 $block_name,
178 array(
179 'attributes' => $attributes,
180 'render_callback' => 'yaycurrency_render_currency_switcher',
181 )
182 );
183 }
184
185 /**
186 * Render the Currency Switcher block HTML.
187 */
188 function yaycurrency_render_currency_switcher( $attributes ) {
189 ob_start();
190 $is_show_flag = $attributes['isShowFlag'];
191 $is_show_currency_name = $attributes['isShowCurrencyName'];
192 $is_show_currency_symbol = $attributes['isShowCurrencySymbol'];
193 $is_show_currency_code = $attributes['isShowCurrencyCode'];
194 $switcher_size = $attributes['widgetSize'];
195 require YAY_CURRENCY_PLUGIN_DIR . 'includes/templates/switcher/template.php';
196 return ob_get_clean();
197 }
198
199 add_action( 'init', 'yaycurrency_register_currency_switcher_block' );
200