Changeset 3822204
- Timestamp:
- 11/25/2026 07:18:48 AM (less than one hour ago)
- Location:
- windcodex-geoblock
- Files:
-
- 32 added
- 10 edited
-
tags/1.1.0 (added)
-
tags/1.1.0/admin (added)
-
tags/1.1.0/admin/assets (added)
-
tags/1.1.0/admin/assets/admin.css (added)
-
tags/1.1.0/admin/assets/admin.js (added)
-
tags/1.1.0/admin/class-geoblock-admin.php (added)
-
tags/1.1.0/admin/class-geoblock-notices.php (added)
-
tags/1.1.0/admin/views (added)
-
tags/1.1.0/admin/views/product-meta-box.php (added)
-
tags/1.1.0/admin/views/settings-page.php (added)
-
tags/1.1.0/includes (added)
-
tags/1.1.0/includes/class-geoblock-compatibility.php (added)
-
tags/1.1.0/includes/class-geoblock-frontend.php (added)
-
tags/1.1.0/includes/class-geoblock-geolocation.php (added)
-
tags/1.1.0/includes/class-geoblock-health.php (added)
-
tags/1.1.0/includes/class-geoblock-loader.php (added)
-
tags/1.1.0/includes/class-geoblock-product.php (added)
-
tags/1.1.0/includes/class-geoblock-regions.php (added)
-
tags/1.1.0/includes/class-geoblock-restrictions.php (added)
-
tags/1.1.0/includes/class-geoblock-shortcodes.php (added)
-
tags/1.1.0/languages (added)
-
tags/1.1.0/languages/windcodex-geoblock.pot (added)
-
tags/1.1.0/public (added)
-
tags/1.1.0/public/assets (added)
-
tags/1.1.0/public/assets/frontend.js (added)
-
tags/1.1.0/public/assets/public.css (added)
-
tags/1.1.0/public/class-geoblock-public.php (added)
-
tags/1.1.0/readme.txt (added)
-
tags/1.1.0/uninstall.php (added)
-
tags/1.1.0/windcodex-geoblock.php (added)
-
trunk/admin/assets/admin.js (modified) (6 diffs)
-
trunk/admin/class-geoblock-admin.php (modified) (3 diffs)
-
trunk/admin/views/product-meta-box.php (modified) (1 diff)
-
trunk/admin/views/settings-page.php (modified) (6 diffs)
-
trunk/includes/class-geoblock-geolocation.php (modified) (7 diffs)
-
trunk/includes/class-geoblock-health.php (added)
-
trunk/includes/class-geoblock-loader.php (modified) (1 diff)
-
trunk/includes/class-geoblock-regions.php (added)
-
trunk/includes/class-geoblock-restrictions.php (modified) (4 diffs)
-
trunk/includes/class-geoblock-shortcodes.php (modified) (1 diff)
-
trunk/readme.txt (modified) (10 diffs)
-
trunk/windcodex-geoblock.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
windcodex-geoblock/trunk/admin/assets/admin.js
r3716271 r3822204 163 163 return; 164 164 } 165 //setBusy( true );165 setBusy( true ); 166 166 showToast( '', 'saving' ); 167 167 … … 197 197 } 198 198 199 //setBusy( true );199 setBusy( true ); 200 200 showToast( '', 'saving' ); 201 201 … … 241 241 if ( ! data.redirect_enabled ) { data.redirect_enabled = 'no'; } 242 242 if ( ! data.catalog_purchasable ) { data.catalog_purchasable = 'no'; } 243 if ( ! data.bypass_admins ) { data.bypass_admins = 'no'; } 244 if ( ! data.exempt_bots ) { data.exempt_bots = 'no'; } 243 245 244 246 return data; … … 304 306 $( '#gg-debug-toggle' ).prop( 'checked', debugOn ); 305 307 308 $( '#gg-bypass-admins-toggle' ).prop( 'checked', settings.bypass_admins === 'yes' ); 309 $( '#gg-exempt-bots-toggle' ).prop( 'checked', settings.exempt_bots === 'yes' ); 310 306 311 // Show/hide catalog sub-option on reset 307 312 if ( settings.restriction_mode === 'catalog_only' ) { … … 333 338 334 339 if ( type === 'saving' ) { 335 $toast.html( '<span class="gg-spinner " style="border-top-color:var(--gg-accent);border-color:var(--gg-accent-mid);"></span> ' + geoblock_admin.i18n.saving );340 $toast.html( '<span class="gg-spinner gg-spinner-toast"></span> ' + geoblock_admin.i18n.saving ); 336 341 $toast.attr( 'class', 'gg-toast gg-toast-show' ); 337 342 return; … … 370 375 } ); 371 376 377 // Region presets: add a whole group to the selection / clear it. 378 $( '.gg-region-btn' ).on( 'click', function () { 379 var group = ( geoblock_admin.regions || {} )[ $( this ).data( 'region' ) ] || []; 380 var selected = $select.val() || []; 381 $select.val( selected.concat( group.filter( function ( code ) { 382 return selected.indexOf( code ) === -1; 383 } ) ) ).trigger( 'change' ); 384 } ); 385 386 $( '.gg-region-clear' ).on( 'click', function () { 387 $select.val( [] ).trigger( 'change' ); 388 } ); 372 389 373 390 } -
windcodex-geoblock/trunk/admin/class-geoblock-admin.php
r3716271 r3822204 22 22 'custom_message' => '', 23 23 'catalog_purchasable' => 'no', // 'no' = block ATC in catalog_only mode (default), 'yes' = allow 24 'bypass_admins' => 'no', // 'yes' = shop managers are never restricted. 25 'exempt_bots' => 'no', // 'yes' = search engine / link-preview crawlers are never restricted. 24 26 'debug_mode' => 'no', 25 27 ); … … 83 85 $clean['custom_message'] = wp_kses_post( $input['custom_message'] ?? '' ); 84 86 $clean['catalog_purchasable'] = ( ( $input['catalog_purchasable'] ?? '' ) === 'yes' ) ? 'yes' : 'no'; 85 $clean['debug_mode'] = ! empty( $input['debug_mode'] ) ? 'yes' : 'no'; 87 $clean['debug_mode'] = ! empty( $input['debug_mode'] ) && 'no' !== $input['debug_mode'] ? 'yes' : 'no'; 88 $clean['bypass_admins'] = ( ( $input['bypass_admins'] ?? '' ) === 'yes' ) ? 'yes' : 'no'; 89 $clean['exempt_bots'] = ( ( $input['exempt_bots'] ?? '' ) === 'yes' ) ? 'yes' : 'no'; 86 90 87 91 return $clean; … … 255 259 'current_settings' => $settings, 256 260 'countries' => $countries, 261 'regions' => array_map( 262 static function ( $group ) use ( $countries ) { 263 return array_values( array_filter( 264 $group['countries'], 265 static function ( $code ) use ( $countries ) { 266 return isset( $countries[ $code ] ); 267 } 268 ) ); 269 }, 270 GeoBlock_Regions::get_groups() 271 ), 257 272 'i18n' => array( 258 273 'saving' => __( 'Saving...', 'windcodex-geoblock' ), -
windcodex-geoblock/trunk/admin/views/product-meta-box.php
r3590184 r3822204 92 92 <?php endforeach; ?> 93 93 </select> 94 <div class="gg-region-presets" aria-label="<?php esc_attr_e( 'Region presets', 'windcodex-geoblock' ); ?>"> 95 <span class="gg-region-presets-label"><?php esc_html_e( 'Quick add:', 'windcodex-geoblock' ); ?></span> 96 <?php foreach ( GeoBlock_Regions::get_groups() as $region_key => $region ) : ?> 97 <button type="button" class="button button-small gg-region-btn" data-region="<?php echo esc_attr( $region_key ); ?>"> 98 + <?php echo esc_html( $region['label'] ); ?> 99 </button> 100 <?php endforeach; ?> 101 <button type="button" class="button button-small gg-region-clear"> 102 <?php esc_html_e( 'Clear all', 'windcodex-geoblock' ); ?> 103 </button> 104 </div> 94 105 <p class="gg-field-hint" style="margin-top:8px;"> 95 106 <?php esc_html_e( 'Search by name or 2-letter code. Leave empty to remove all restrictions from this product.', 'windcodex-geoblock' ); ?> -
windcodex-geoblock/trunk/admin/views/settings-page.php
r3716271 r3822204 17 17 $gg_catalog_purchasable = $gg_settings['catalog_purchasable'] ?? 'no'; 18 18 $gg_debug = $gg_settings['debug_mode'] ?? 'no'; 19 $gg_bypass_admins = $gg_settings['bypass_admins'] ?? 'no'; 20 $gg_exempt_bots = $gg_settings['exempt_bots'] ?? 'no'; 21 22 $gg_health = new GeoBlock_Health( new GeoBlock_Geolocation() ); 23 $gg_health_checks = $gg_health->get_checks(); 24 $gg_health_status = $gg_health->get_overall_status( $gg_health_checks ); 19 25 20 26 $gg_wc_geo = get_option( 'woocommerce_default_customer_address' ); … … 111 117 <?php esc_html_e( 'Advanced', 'windcodex-geoblock' ); ?> 112 118 </button> 119 <button class="gg-tab-btn" data-tab="status" data-breadcrumb="<?php esc_attr_e( 'Status', 'windcodex-geoblock' ); ?>" role="tab" aria-selected="false"> 120 <?php esc_html_e( 'Status', 'windcodex-geoblock' ); ?> 121 <?php if ( 'warn' === $gg_health_status ) : ?> 122 <span class="gg-tab-dot" aria-hidden="true"></span> 123 <span class="screen-reader-text"><?php esc_html_e( '(needs attention)', 'windcodex-geoblock' ); ?></span> 124 <?php endif; ?> 125 </button> 113 126 </div> 114 127 … … 244 257 ><?php echo esc_textarea( $gg_message ); ?></textarea> 245 258 <p class="gg-field-hint"> 246 <?php esc_html_e( 'Shown on the product page when restricted. HTML is allowed. ', 'windcodex-geoblock' ); ?>259 <?php esc_html_e( 'Shown on the product page when restricted. HTML is allowed. Placeholders: {country_name}, {country} (code) and {country_flag}.', 'windcodex-geoblock' ); ?> 247 260 </p> 248 261 </div> … … 353 366 </div> 354 367 368 <!-- Bypass admins --> 369 <div class="gg-form-row"> 370 <div class="gg-row-label"> 371 <div class="gg-row-title"><?php esc_html_e( 'Admin Bypass', 'windcodex-geoblock' ); ?></div> 372 <div class="gg-row-hint"><?php esc_html_e( 'Never restrict shop managers', 'windcodex-geoblock' ); ?></div> 373 </div> 374 <div class="gg-row-body"> 375 <div class="gg-toggle-row"> 376 <label class="gg-toggle-wrap"> 377 <input type="checkbox" 378 name="geoblock_settings[bypass_admins]" 379 id="gg-bypass-admins-toggle" 380 value="yes" 381 class="gg-toggle-checkbox screen-reader-text" 382 <?php checked( $gg_bypass_admins, 'yes' ); ?>> 383 <span class="gg-toggle-track gg-toggle-sm"> 384 <span class="gg-toggle-thumb"></span> 385 </span> 386 </label> 387 <div class="gg-toggle-label"> 388 <span class="gg-toggle-main"><?php esc_html_e( 'Let administrators and shop managers see everything', 'windcodex-geoblock' ); ?></span> 389 <span class="gg-toggle-sub"><?php esc_html_e( 'Logged-in users who can manage WooCommerce are never restricted, so you can edit and check any product. Use the debug toolbar "Test as" picker to preview a country - bypass is paused while you test.', 'windcodex-geoblock' ); ?></span> 390 </div> 391 </div> 392 </div> 393 </div> 394 395 <!-- Exempt bots --> 396 <div class="gg-form-row"> 397 <div class="gg-row-label"> 398 <div class="gg-row-title"><?php esc_html_e( 'Search Bots', 'windcodex-geoblock' ); ?></div> 399 <div class="gg-row-hint"><?php esc_html_e( 'Keep restricted products indexable', 'windcodex-geoblock' ); ?></div> 400 </div> 401 <div class="gg-row-body"> 402 <div class="gg-toggle-row"> 403 <label class="gg-toggle-wrap"> 404 <input type="checkbox" 405 name="geoblock_settings[exempt_bots]" 406 id="gg-exempt-bots-toggle" 407 value="yes" 408 class="gg-toggle-checkbox screen-reader-text" 409 <?php checked( $gg_exempt_bots, 'yes' ); ?>> 410 <span class="gg-toggle-track gg-toggle-sm"> 411 <span class="gg-toggle-thumb"></span> 412 </span> 413 </label> 414 <div class="gg-toggle-label"> 415 <span class="gg-toggle-main"><?php esc_html_e( 'Never restrict search engine and link-preview bots', 'windcodex-geoblock' ); ?></span> 416 <span class="gg-toggle-sub"><?php esc_html_e( 'Googlebot, Bingbot and social link-preview crawlers always see the product, so Hide mode cannot drop it from search results or break shared-link previews. Detected by user agent, so treat it as an SEO helper, not a security control.', 'windcodex-geoblock' ); ?></span> 417 </div> 418 </div> 419 </div> 420 </div> 421 355 422 <!-- Debug Mode --> 356 423 <div class="gg-form-row gg-form-row-last"> … … 374 441 <div class="gg-toggle-label"> 375 442 <span class="gg-toggle-main"><?php esc_html_e( 'Enable debug toolbar', 'windcodex-geoblock' ); ?></span> 376 <span class="gg-toggle-sub"><?php esc_html_e( 'Shows a bar at the bottom of every frontend page with the detected country and IP address . Visible only to shop managers and administrators.', 'windcodex-geoblock' ); ?></span>443 <span class="gg-toggle-sub"><?php esc_html_e( 'Shows a bar at the bottom of every frontend page with the detected country and IP address, plus a "Test as" picker to preview your store as a visitor from any country. Visible only to shop managers and administrators.', 'windcodex-geoblock' ); ?></span> 377 444 </div> 378 445 </div> … … 383 450 </div><!-- [Advanced] --> 384 451 452 453 <!-- ---- TAB: Status ---- --> 454 <div class="gg-tab-panel" data-panel="status"> 455 <div class="gg-card"> 456 <div class="gg-card-header"> 457 <div> 458 <div class="gg-card-header-title"><?php esc_html_e( 'Setup Health', 'windcodex-geoblock' ); ?></div> 459 <div class="gg-card-header-sub"><?php esc_html_e( 'Checks that country detection is set up correctly on this site.', 'windcodex-geoblock' ); ?></div> 460 </div> 461 </div> 462 <?php 463 $gg_last_check = count( $gg_health_checks ) - 1; 464 foreach ( $gg_health_checks as $gg_i => $gg_check ) : 465 $gg_status_labels = array( 466 'ok' => __( 'OK', 'windcodex-geoblock' ), 467 'warn' => __( 'Needs attention', 'windcodex-geoblock' ), 468 'info' => __( 'Info', 'windcodex-geoblock' ), 469 ); 470 ?> 471 <div class="gg-form-row<?php echo ( $gg_i === $gg_last_check ) ? ' gg-form-row-last' : ''; ?>"> 472 <div class="gg-row-label"> 473 <div class="gg-row-title"><?php echo esc_html( $gg_check['label'] ); ?></div> 474 </div> 475 <div class="gg-row-body"> 476 <span class="gg-health-badge gg-health-<?php echo esc_attr( $gg_check['status'] ); ?>"><?php echo esc_html( $gg_status_labels[ $gg_check['status'] ] ); ?></span> 477 <p class="gg-health-detail"><?php echo wp_kses( $gg_check['detail'], array( 'a' => array( 'href' => array() ) ) ); ?></p> 478 </div> 479 </div> 480 <?php endforeach; ?> 481 </div> 482 483 <div class="gg-card"> 484 <div class="gg-card-header"> 485 <div> 486 <div class="gg-card-header-title"><?php esc_html_e( 'Country Shortcodes', 'windcodex-geoblock' ); ?></div> 487 <div class="gg-card-header-sub"><?php esc_html_e( 'Show or hide any content by visitor country - on pages, posts or page builders.', 'windcodex-geoblock' ); ?></div> 488 </div> 489 </div> 490 <div class="gg-form-row"> 491 <div class="gg-row-label"><div class="gg-row-title"><code>[geoblock_show]</code></div></div> 492 <div class="gg-row-body"> 493 <code>[geoblock_show countries="US,CA"]Free shipping to North America[/geoblock_show]</code> 494 <p class="gg-field-hint"><?php esc_html_e( 'Visible only to visitors from the listed countries.', 'windcodex-geoblock' ); ?></p> 495 </div> 496 </div> 497 <div class="gg-form-row"> 498 <div class="gg-row-label"><div class="gg-row-title"><code>[geoblock_hide]</code></div></div> 499 <div class="gg-row-body"> 500 <code>[geoblock_hide countries="EU"]Prices exclude VAT[/geoblock_hide]</code> 501 <p class="gg-field-hint"><?php esc_html_e( 'Visible to everyone except the listed countries.', 'windcodex-geoblock' ); ?></p> 502 </div> 503 </div> 504 <div class="gg-form-row"> 505 <div class="gg-row-label"><div class="gg-row-title"><code>[geoblock_country]</code></div></div> 506 <div class="gg-row-body"> 507 <code>[geoblock_country format="flag_name"]</code> 508 <p class="gg-field-hint"><?php esc_html_e( 'Prints the visitor country. Formats: name (default), code, flag, flag_name.', 'windcodex-geoblock' ); ?></p> 509 </div> 510 </div> 511 <div class="gg-form-row gg-form-row-last"> 512 <div class="gg-row-label"><div class="gg-row-title"><?php esc_html_e( 'Region names', 'windcodex-geoblock' ); ?></div></div> 513 <div class="gg-row-body"> 514 <p class="gg-field-hint" style="margin-top:0;"> 515 <?php 516 $gg_region_keys = array_keys( GeoBlock_Regions::get_groups() ); 517 echo esc_html( 518 sprintf( 519 /* translators: %s: list of region tokens */ 520 __( 'Use country codes or these region names in the countries attribute: %s.', 'windcodex-geoblock' ), 521 implode( ', ', $gg_region_keys ) 522 ) 523 ); 524 ?> 525 </p> 526 <p class="gg-field-hint"><?php esc_html_e( 'Pages that use these shortcodes are marked as not cacheable so one country never sees another country\'s version.', 'windcodex-geoblock' ); ?></p> 527 </div> 528 </div> 529 </div> 530 </div><!-- [Status] --> 385 531 386 532 <!-- Footer Action Bar --> -
windcodex-geoblock/trunk/includes/class-geoblock-geolocation.php
r3506582 r3822204 28 28 public function get_country(): string { 29 29 30 // 0. Admin "Test as country" override - never cached, managers only. 31 $test_country = $this->get_test_country(); 32 if ( $test_country ) { 33 return $test_country; 34 } 35 30 36 $force_geo = $this->is_force_geolocation_enabled(); 31 37 … … 119 125 } 120 126 127 // -- Test as country (admin only) ----------------------------------- 128 129 /** Cookie that stores the admin's "test as country" choice. */ 130 const TEST_COOKIE = 'geoblock_test_country'; 131 132 /** Nonce action for switching the test country. */ 133 const TEST_NONCE_ACTION = 'geoblock_test_country'; 134 135 /** 136 * Returns the country an admin is currently testing as, or null. 137 * Only honoured for users who can manage WooCommerce. 138 * 139 * @return string|null 140 */ 141 public function get_test_country(): ?string { 142 if ( empty( $_COOKIE[ self::TEST_COOKIE ] ) || ! did_action( 'init' ) ) { 143 return null; 144 } 145 if ( ! current_user_can( 'manage_woocommerce' ) ) { 146 return null; 147 } 148 $code = strtoupper( sanitize_text_field( wp_unslash( $_COOKIE[ self::TEST_COOKIE ] ) ) ); 149 return $this->is_valid_country_code( $code ) ? $code : null; 150 } 151 152 /** @return bool */ 153 public function has_test_country(): bool { 154 return null !== $this->get_test_country(); 155 } 156 157 /** 158 * Handles the "Test as country" form / link: validates the nonce, sets or 159 * clears the cookie and redirects back to the same page without the params. 160 */ 161 public function handle_test_country_request(): void { 162 if ( ! isset( $_GET['geoblock_test_country'] ) ) { 163 return; 164 } 165 if ( ! current_user_can( 'manage_woocommerce' ) ) { 166 return; 167 } 168 $nonce = isset( $_GET['_geoblock_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_geoblock_nonce'] ) ) : ''; 169 if ( ! wp_verify_nonce( $nonce, self::TEST_NONCE_ACTION ) ) { 170 return; 171 } 172 173 $code = strtoupper( sanitize_text_field( wp_unslash( $_GET['geoblock_test_country'] ) ) ); 174 $secure = is_ssl(); 175 176 if ( $this->is_valid_country_code( $code ) && 'OFF' !== $code ) { 177 setcookie( self::TEST_COOKIE, $code, 0, COOKIEPATH ?: '/', COOKIE_DOMAIN, $secure, true ); 178 } else { 179 setcookie( self::TEST_COOKIE, '', time() - YEAR_IN_SECONDS, COOKIEPATH ?: '/', COOKIE_DOMAIN, $secure, true ); 180 } 181 182 wp_safe_redirect( remove_query_arg( array( 'geoblock_test_country', '_geoblock_nonce' ) ) ); 183 exit; 184 } 185 186 /** 187 * Admin bar indicator - shown while the debug toolbar is on or a test 188 * country is active, so an admin can never forget they are testing. 189 * 190 * @param WP_Admin_Bar $bar 191 */ 192 public function register_admin_bar_node( $bar ): void { 193 if ( is_admin() || ! current_user_can( 'manage_woocommerce' ) ) { 194 return; 195 } 196 $test = $this->get_test_country(); 197 if ( ! $test ) { 198 return; 199 } 200 $bar->add_node( array( 201 'id' => 'geoblock-test-country', 202 'title' => esc_html( 203 sprintf( 204 /* translators: %s: country name and flag */ 205 __( 'GeoBlock test: %s (click to exit)', 'windcodex-geoblock' ), 206 $this->get_flag_emoji( $test ) . ' ' . $this->get_country_name( $test ) 207 ) 208 ), 209 'href' => esc_url( $this->get_test_country_url( 'off' ) ), 210 ) ); 211 } 212 213 /** 214 * URL that switches the test country (or clears it with 'off'). 215 * 216 * @param string $code ISO code or 'off'. 217 * @return string 218 */ 219 public function get_test_country_url( string $code ): string { 220 return add_query_arg( 221 array( 222 'geoblock_test_country' => $code, 223 '_geoblock_nonce' => wp_create_nonce( self::TEST_NONCE_ACTION ), 224 ), 225 remove_query_arg( array( 'geoblock_test_country', '_geoblock_nonce' ) ) 226 ); 227 } 228 121 229 // -- Debug toolbar -------------------------------------------------- 122 230 … … 134 242 } 135 243 136 $country = $this->get_country(); 137 $ip = $this->get_ip(); 138 $flag = $this->get_flag_emoji( $country ); 139 $name = $this->get_country_name( $country ); 140 $source = $this->get_detection_source(); 244 $country = $this->get_country(); 245 $ip = $this->get_ip(); 246 $flag = $this->get_flag_emoji( $country ); 247 $name = $this->get_country_name( $country ); 248 $source = $this->get_detection_source(); 249 $testing = $this->has_test_country(); 250 $wc = function_exists( 'WC' ) ? WC() : null; 251 $countries = ( $wc && isset( $wc->countries ) && $wc->countries ) ? $wc->countries->get_countries() : array(); 141 252 ?> 253 <style> 254 #geoblock-debug-bar{box-sizing:border-box;flex-wrap:wrap;gap:8px 20px !important;line-height:1.4;text-align:left;} 255 #geoblock-debug-bar *{box-sizing:border-box;} 256 #geoblock-debug-bar form{margin:0;display:flex;gap:8px;align-items:center;} 257 #geoblock-debug-bar select,#geoblock-debug-bar button{height:28px !important;min-height:0 !important;margin:0 !important;padding:0 10px !important;font:inherit !important;font-size:12px !important;line-height:26px !important;border-radius:4px !important;box-shadow:none !important;width:auto !important;} 258 #geoblock-debug-bar select{max-width:200px;padding-right:24px !important;background-color:#16304f !important;color:#fff !important;border:1px solid #4a78a8 !important;} 259 #geoblock-debug-bar button{cursor:pointer;background:#2e75b6 !important;color:#fff !important;border:1px solid #2e75b6 !important;text-transform:none !important;letter-spacing:0 !important;} 260 #geoblock-debug-bar button:hover{background:#3a86cb !important;} 261 </style> 142 262 <div id="geoblock-debug-bar" style=" 143 263 position:fixed;bottom:0;left:0;right:0;z-index:99999; … … 146 266 border-top:3px solid #2e75b6;"> 147 267 <strong style="color:#a8c8e8;">GeoBlock Debug</strong> 148 <span><?php echo esc_html( $flag . ' ' . $name . ' (' . $country . ')'); ?></span>268 <span><?php echo esc_html( $flag . ' ' . $name . ( false === stripos( $name, '(' . $country . ')' ) ? ' (' . $country . ')' : '' ) ); ?></span> 149 269 <span style="color:#a8c8e8;">IP: <?php echo esc_html( $ip ); ?></span> 150 270 <span style="color:#ffd580;">Source: <?php echo esc_html( $source ); ?></span> 151 271 <span style="color:#88cc88;">WC Geo: <?php echo esc_html( $this->wc_geolocation_enabled() ? 'Enabled' : 'Disabled' ); ?></span> 152 272 <span style="color:#ffa07a;"><?php echo $this->is_force_geolocation_enabled() ? 'Force Geo: ON' : ''; ?></span> 273 <form method="get" action=""> 274 <input type="hidden" name="_geoblock_nonce" value="<?php echo esc_attr( wp_create_nonce( self::TEST_NONCE_ACTION ) ); ?>"> 275 <label for="geoblock-test-select" style="color:#a8c8e8;"><?php esc_html_e( 'Test as:', 'windcodex-geoblock' ); ?></label> 276 <select id="geoblock-test-select" name="geoblock_test_country"> 277 <?php if ( $testing ) : ?> 278 <option value="off"><?php esc_html_e( 'Exit test mode', 'windcodex-geoblock' ); ?></option> 279 <?php else : ?> 280 <option value="off"><?php esc_html_e( 'My real location', 'windcodex-geoblock' ); ?></option> 281 <?php endif; ?> 282 <?php foreach ( $countries as $code => $label ) : ?> 283 <option value="<?php echo esc_attr( $code ); ?>" <?php selected( $testing && $code === $country ); ?>><?php echo esc_html( $label ); ?></option> 284 <?php endforeach; ?> 285 </select> 286 <button type="submit"><?php esc_html_e( 'Apply', 'windcodex-geoblock' ); ?></button> 287 </form> 153 288 <a href="<?php echo esc_url( admin_url( 'admin.php?page=geoblock-settings' ) ); ?>" 154 289 style="color:#a8c8e8;margin-left:auto;">Settings -></a> … … 295 430 * @return string 296 431 */ 297 private function get_detection_source(): string { 432 public function get_detection_source(): string { 433 if ( $this->has_test_country() ) { 434 return 'Test mode (admin override)'; 435 } 298 436 $wc = function_exists( 'WC' ) ? WC() : null; 299 437 if ( is_user_logged_in() && $wc && $wc->customer instanceof WC_Customer ) { … … 325 463 * @return string 326 464 */ 327 p rivatefunction get_flag_emoji( string $code ): string {328 if ( strlen( $code ) !== 2 ) {465 public function get_flag_emoji( string $code ): string { 466 if ( strlen( $code ) !== 2 || ! function_exists( 'mb_chr' ) ) { 329 467 return '??'; 330 468 } … … 343 481 * @return string 344 482 */ 345 p rivatefunction get_country_name( string $code ): string {483 public function get_country_name( string $code ): string { 346 484 $wc = function_exists( 'WC' ) ? WC() : null; 347 485 $countries = ( $wc && isset( $wc->countries ) && $wc->countries ) ? $wc->countries->get_countries() : array(); -
windcodex-geoblock/trunk/includes/class-geoblock-loader.php
r3590184 r3822204 106 106 $this->add_action( 'init', $shortcodes, 'register' ); 107 107 108 // --- Debug toolbar --- 109 $this->add_action( 'wp_footer', $geo, 'maybe_render_debug_toolbar' ); 108 // --- Debug toolbar + admin "Test as country" --- 109 $this->add_action( 'wp_footer', $geo, 'maybe_render_debug_toolbar' ); 110 $this->add_action( 'init', $geo, 'handle_test_country_request' ); 111 $this->add_action( 'admin_bar_menu', $geo, 'register_admin_bar_node', 100 ); 110 112 } 111 113 -
windcodex-geoblock/trunk/includes/class-geoblock-restrictions.php
r3590184 r3822204 26 26 private $settings = null; 27 27 28 /** @var bool|null Cached per-request exemption result */ 29 private $exempt = null; 30 28 31 /** 29 32 * @param GeoBlock_Geolocation $geo … … 47 50 } 48 51 49 $country = $this->geo->get_country(); 50 $rules = $this->get_product_rules( $product_id ); 52 $rules = $this->get_product_rules( $product_id ); 51 53 52 54 if ( empty( $rules ) || empty( $rules['countries'] ) ) { … … 54 56 } 55 57 56 return $this->evaluate( $rules, $country ); 58 if ( $this->is_exempt_visitor() ) { 59 return false; 60 } 61 62 return $this->evaluate( $rules, $this->geo->get_country() ); 63 } 64 65 /** 66 * The geolocation service, for shortcodes that need the visitor's country. 67 * 68 * @return GeoBlock_Geolocation 69 */ 70 public function get_geolocation(): GeoBlock_Geolocation { 71 return $this->geo; 72 } 73 74 /** 75 * True when the current visitor should never be restricted: shop managers 76 * (if "bypass admins" is on and they are not in test mode) or search 77 * engine / link-preview crawlers (if "exempt bots" is on). 78 * 79 * @return bool 80 */ 81 public function is_exempt_visitor(): bool { 82 if ( null !== $this->exempt ) { 83 return $this->exempt; 84 } 85 86 $settings = $this->get_settings(); 87 $exempt = false; 88 89 if ( 'yes' === ( $settings['bypass_admins'] ?? 'no' ) 90 && current_user_can( 'manage_woocommerce' ) 91 && ! $this->geo->has_test_country() ) { 92 $exempt = true; 93 } 94 95 if ( ! $exempt && 'yes' === ( $settings['exempt_bots'] ?? 'no' ) && $this->is_crawler() ) { 96 $exempt = true; 97 } 98 99 /** 100 * Filter whether the current visitor is exempt from all restrictions. 101 * 102 * @param bool $exempt 103 */ 104 $this->exempt = (bool) apply_filters( 'geoblock_is_exempt_visitor', $exempt ); 105 106 return $this->exempt; 107 } 108 109 /** 110 * Matches the request user agent against known search engine and 111 * link-preview crawlers. User agents can be spoofed, so this is meant for 112 * SEO and link previews, not as a security control. 113 * 114 * @return bool 115 */ 116 private function is_crawler(): bool { 117 $agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; 118 if ( '' === $agent ) { 119 return false; 120 } 121 122 $pattern = (string) apply_filters( 123 'geoblock_crawler_pattern', 124 'googlebot|adsbot-google|mediapartners-google|google-inspectiontool|bingbot|bingpreview|duckduckbot|slurp|baiduspider|yandexbot|applebot|facebookexternalhit|linkedinbot|twitterbot|pinterestbot' 125 ); 126 127 return (bool) preg_match( '/(' . $pattern . ')/i', $agent ); 57 128 } 58 129 … … 75 146 public function get_restriction_message(): string { 76 147 $settings = $this->get_settings(); 77 return! empty( $settings['custom_message'] )148 $message = ! empty( $settings['custom_message'] ) 78 149 ? wp_kses_post( $settings['custom_message'] ) 79 150 : esc_html__( 'Sorry, this product is not available in your country.', 'windcodex-geoblock' ); 151 152 return $this->replace_placeholders( $message ); 153 } 154 155 /** 156 * Replaces {country}, {country_name} and {country_flag} with the 157 * visitor's detected country. Values are escaped. 158 * 159 * @param string $message 160 * @return string 161 */ 162 public function replace_placeholders( string $message ): string { 163 if ( false === strpos( $message, '{country' ) ) { 164 return $message; 165 } 166 167 $code = $this->geo->get_country(); 168 $flag = $this->geo->get_flag_emoji( $code ); 169 170 return strtr( $message, array( 171 '{country}' => esc_html( $code ), 172 '{country_name}' => esc_html( $this->geo->get_country_name( $code ) ), 173 '{country_flag}' => '??' === $flag ? '' : esc_html( $flag ), 174 ) ); 80 175 } 81 176 -
windcodex-geoblock/trunk/includes/class-geoblock-shortcodes.php
r3590184 r3822204 39 39 public function register(): void { 40 40 add_shortcode( 'geoblock_product_message', array( $this, 'render_product_message' ) ); 41 add_shortcode( 'geoblock_country', array( $this, 'render_country' ) ); 42 add_shortcode( 'geoblock_show', array( $this, 'render_show' ) ); 43 add_shortcode( 'geoblock_hide', array( $this, 'render_hide' ) ); 44 } 45 46 // --- Country content shortcodes ------------------------------------------- 47 48 /** 49 * [geoblock_country format="name|code|flag|flag_name"] - prints the 50 * visitor's detected country. 51 * 52 * @param array|string $atts 53 * @return string 54 */ 55 public function render_country( $atts ): string { 56 $atts = shortcode_atts( array( 'format' => 'name' ), $atts, 'geoblock_country' ); 57 $this->maybe_disable_page_cache(); 58 59 $geo = $this->restrictions->get_geolocation(); 60 $code = $geo->get_country(); 61 $name = $geo->get_country_name( $code ); 62 $flag = $geo->get_flag_emoji( $code ); 63 $flag = '??' === $flag ? '' : $flag; 64 65 switch ( $atts['format'] ) { 66 case 'code': 67 $out = $code; 68 break; 69 case 'flag': 70 $out = $flag; 71 break; 72 case 'flag_name': 73 $out = trim( $flag . ' ' . $name ); 74 break; 75 default: 76 $out = $name; 77 } 78 79 return esc_html( $out ); 80 } 81 82 /** 83 * [geoblock_show countries="US,CA,EU"]...[/geoblock_show] - content shown 84 * only to visitors from the listed countries or regions. 85 * 86 * @param array|string $atts 87 * @param string|null $content 88 * @return string 89 */ 90 public function render_show( $atts, $content = null ): string { 91 $atts = shortcode_atts( array( 'countries' => '' ), $atts, 'geoblock_show' ); 92 $countries = GeoBlock_Regions::expand( $atts['countries'] ); 93 $this->maybe_disable_page_cache(); 94 95 if ( empty( $countries ) ) { 96 return ''; 97 } 98 99 $country = $this->restrictions->get_geolocation()->get_country(); 100 101 return in_array( $country, $countries, true ) ? do_shortcode( (string) $content ) : ''; 102 } 103 104 /** 105 * [geoblock_hide countries="US,CA,EU"]...[/geoblock_hide] - content shown 106 * to everyone except visitors from the listed countries or regions. 107 * 108 * @param array|string $atts 109 * @param string|null $content 110 * @return string 111 */ 112 public function render_hide( $atts, $content = null ): string { 113 $atts = shortcode_atts( array( 'countries' => '' ), $atts, 'geoblock_hide' ); 114 $countries = GeoBlock_Regions::expand( $atts['countries'] ); 115 $this->maybe_disable_page_cache(); 116 117 if ( empty( $countries ) ) { 118 return do_shortcode( (string) $content ); 119 } 120 121 $country = $this->restrictions->get_geolocation()->get_country(); 122 123 return in_array( $country, $countries, true ) ? '' : do_shortcode( (string) $content ); 124 } 125 126 /** 127 * Tells page caching plugins not to cache a page whose output depends on 128 * the visitor's country, so one country's version is never served to another. 129 */ 130 private function maybe_disable_page_cache(): void { 131 /** 132 * Filter whether country shortcodes mark the page as uncacheable. 133 * 134 * @param bool $disable Default true. 135 */ 136 if ( ! apply_filters( 'geoblock_shortcode_disable_cache', true ) ) { 137 return; 138 } 139 if ( ! defined( 'DONOTCACHEPAGE' ) ) { 140 define( 'DONOTCACHEPAGE', true ); 141 } 41 142 } 42 143 -
windcodex-geoblock/trunk/readme.txt
r3716271 r3822204 1 1 === WindCodex GeoBlock – WooCommerce Country Restriction & Geo Blocking === 2 2 Contributors: windcodex 3 Tags: woocommerce, geo blocking, country restriction, geolocation, blockcountry3 Tags: woocommerce, country restriction, geo blocking, woocommerce country restrictions, restrict products by country 4 4 Requires at least: 6.9 5 5 Tested up to: 7.0 6 Stable tag: 1. 0.36 Stable tag: 1.1.0 7 7 Requires PHP: 7.4 8 8 WC requires at least: 7.0 … … 11 11 License URI: https://www.gnu.org/licenses/gpl-2.0.html 12 12 13 Block WooCommerce products by country. Hide products, restrict checkout, or show a custom message by geolocation – no API key required.13 Restrict WooCommerce products by country: hide them, block purchase or sell only to chosen countries. Uses WooCommerce geolocation. No paid API. 14 14 15 15 == Description == 16 16 17 **WindCodex GeoBlock** lets you restrict WooCommerce products by country using WooCommerce's built-in geolocation – no API key, no third-party service, no code required.17 **WindCodex GeoBlock** lets you restrict WooCommerce products by country using WooCommerce's built-in geolocation – no paid API, no third-party service, no code required. 18 18 19 19 Whether you need to comply with local laws, honour export restrictions, manage licensing territories, or simply limit shipping to countries you serve – GeoBlock gives you per-product country control in minutes. … … 52 52 * **Session & transient caching** – Country detection results cached per session. Zero repeated geolocation lookups. 53 53 * **Admin debug toolbar** – Shows detected country, IP address, detection source, and a settings link. Visible to admins only. 54 * **Test as country** – Preview your store as a visitor from any country from the debug toolbar. Admin-only, with an admin-bar reminder so you never forget you are testing. 55 * **Country content shortcodes** – `[geoblock_show]`, `[geoblock_hide]` and `[geoblock_country]` show or hide any content by visitor country, on any page or post. 56 * **Region presets** – Add the EU, EEA, North America, Nordics, GCC, ASEAN or Australia & NZ to a rule in one click. 57 * **Message placeholders** – Use `{country_name}`, `{country}` and `{country_flag}` in your restriction message. 58 * **Search bot exemption** – Optionally let Googlebot, Bingbot and link-preview crawlers always see restricted products so Hide mode cannot hurt your search visibility. 59 * **Admin bypass** – Optionally never restrict administrators and shop managers. 60 * **Setup health check** – A Status tab that checks geolocation, country data source, page caching and your detected country. 54 61 * **Shortcode support** – `[geoblock_product_message]` renders the restriction message in any page builder, custom template, Elementor, or Divi. 55 62 * **HPOS compatible** – Fully supports WooCommerce High-Performance Order Storage. … … 89 96 `[geoblock_product_message id=123 class="my-notice highlight"]` 90 97 98 = Country Content Shortcodes = 99 100 Show or hide any content by the visitor's country: 101 102 `[geoblock_show countries="US,CA"]Free shipping to North America[/geoblock_show]` 103 `[geoblock_hide countries="EU"]Prices exclude VAT[/geoblock_hide]` 104 `[geoblock_country format="flag_name"]` 105 106 **Attributes:** 107 * `countries` – Comma-separated country codes and/or region names: EU, EEA, NORTH_AMERICA, NORDICS, GCC, ASEAN, ANZ. 108 * `format` (`[geoblock_country]` only) – `name` (default), `code`, `flag` or `flag_name`. 109 110 Pages that use these shortcodes are marked as not cacheable (`DONOTCACHEPAGE`) so one country never sees another country's version. Use the `geoblock_shortcode_disable_cache` filter to turn this off. 111 91 112 = How It Works = 92 113 … … 99 120 = Requirements = 100 121 101 * WordPress 5.8or higher122 * WordPress 6.9 or higher 102 123 * WooCommerce 7.0 or higher 103 124 * PHP 7.4 or higher 104 125 * WooCommerce Geolocation must be enabled: **WooCommerce Settings > General > Default customer location > Geolocate** 105 106 = 🚀 Pro Version = 107 108 Need bulk category rules, payment gateway restrictions by country, variation-level rules, or an analytics dashboard? 109 [GeoBlock Pro is available at windcodex.com](https://windcodex.com/product/woocommerce-country-restriction-plugin/) 126 * For IP lookups, either a free MaxMind license key (**WooCommerce > Settings > Integration**) or a host/CDN that passes the visitor's country, such as Cloudflare 127 128 = GeoBlock Pro = 129 130 The free plugin is fully working. GeoBlock Pro adds bulk category rules, payment gateway restrictions by country, variation-level rules and an analytics dashboard. 131 132 [See GeoBlock Pro](https://windcodex.com/product/woocommerce-country-restriction-plugin/?utm_source=wordpress.org&utm_medium=readme&utm_campaign=geoblock) 133 134 = More Free Plugins from WindCodex = 135 136 * [ReturnDesk](https://wordpress.org/plugins/windcodex-returndesk/) 137 * [ScraperBlock](https://wordpress.org/plugins/windcodex-scraperblock/) 138 * [SwitchUser](https://wordpress.org/plugins/windcodex-switchuser/) 139 * [Ops](https://wordpress.org/plugins/windcodex-ops/) 140 * [Forge](https://wordpress.org/plugins/windcodex-forge/) 110 141 111 142 = Privacy = … … 137 168 = Does this plugin require any API key or account? = 138 169 139 No . GeoBlock uses WooCommerce's built-in geolocation engine (MaxMind GeoLite2), which is already bundled with WooCommerce. No external API key, account, or paid service is needed.170 No paid service or GeoBlock account is needed. GeoBlock uses WooCommerce's own geolocation. If your host or CDN passes the visitor's country (for example Cloudflare), that works with no extra setup. Otherwise WooCommerce looks up the country in the MaxMind GeoLite2 database, which needs a free MaxMind license key under **WooCommerce > Settings > Integration**. The lookup runs on your own server. Customers with a saved shipping or billing address are detected from that address either way. 140 171 141 172 = Which countries can I block or restrict? = … … 177 208 = Does geo blocking affect SEO or Google indexing? = 178 209 179 No. Country restrictions are applied at the application layer. Search engine crawlers are not affected and your products continue to be indexed normally. Country-based product restrictions are standard, accepted practice in international e-commerce. 210 Search engine crawlers are detected by IP like any other visitor. Googlebot crawls mostly from US IP addresses, so if you use **Hide completely** for the US, Google may see a 404 and drop that product from its index. To keep restricted products indexed, turn on **Never restrict search engine and link-preview bots** in the Advanced tab, or use **Show Restriction Message** or **Hide from Catalog** for countries your main crawlers come from. Products open to the crawler's country are indexed normally. 211 212 = Can I show or hide normal page content by country? = 213 214 Yes. Wrap content in `[geoblock_show countries="US,CA"]...[/geoblock_show]` to show it only to those countries, or `[geoblock_hide countries="EU"]...[/geoblock_hide]` to show it to everyone else. You can use country codes or region names such as EU, EEA and NORDICS. 215 216 = How do I preview my store as a visitor from another country? = 217 218 Turn on Debug Mode in **WooCommerce > GeoBlock Restrictions > Advanced**, then use the "Test as" picker in the toolbar at the bottom of your site. Only you see it, and an admin-bar notice reminds you while it is active. 180 219 181 220 = Is this plugin GDPR compliant? = 182 221 183 Yes. GeoBlock processes IP addresses only to determine the visitor's geographic country – a legitimate interest under e-commerce operation. No personal data is stored beyond the session and transient TTL (1 hour). No data is shared with third parties. Update your own privacy policy to document your use of geolocation.222 GeoBlock uses the visitor's IP address only to work out their country, keeps the result for one hour (WooCommerce session and WordPress transient), and sends no data to external services. Whether your whole store meets GDPR depends on your setup, so mention country detection in your privacy policy and check with your own advisor. 184 223 185 224 = Does this work with WooCommerce Subscriptions? = … … 214 253 215 254 == Changelog == 255 256 = 1.1.0 = 257 * Added: `[geoblock_show]`, `[geoblock_hide]` and `[geoblock_country]` shortcodes to show or hide any content by visitor country. 258 * Added: "Test as country" picker in the admin debug toolbar, plus an admin-bar reminder while testing. 259 * Added: Region presets (EU, EEA, North America, Nordics, GCC, ASEAN, Australia & NZ) on the product country selector. 260 * Added: `{country_name}`, `{country}` and `{country_flag}` placeholders in the restriction message. 261 * Added: Setting to never restrict search engine and link-preview bots. 262 * Added: Setting to never restrict administrators and shop managers. 263 * Added: Status tab with a setup health check. 264 * Added: `geoblock_region_groups`, `geoblock_is_exempt_visitor`, `geoblock_crawler_pattern` and `geoblock_shortcode_disable_cache` filters. 265 * Fixed: The debug toolbar could not be switched off from the settings page. 216 266 217 267 = 1.0.3 = … … 254 304 == Upgrade Notice == 255 305 306 = 1.1.0 = 307 Adds country content shortcodes, a Test as country picker, region presets, a search bot exemption and a setup health check. No database changes – safe to update. 308 256 309 = 1.0.3 = 257 310 Adds a Docs link on the Plugins screen and Help menu improvements. No database changes – safe to update. … … 265 318 = 1.0.0 = 266 319 Initial release – no upgrade steps required. 320 -
windcodex-geoblock/trunk/windcodex-geoblock.php
r3716271 r3822204 3 3 * @wordpress-plugin 4 4 * Plugin Name: WindCodex GeoBlock 5 * Plugin URI: https://windcodex.com/product/woocommerce-country-restriction-plugin/ 5 6 * Tagline: Country Restrictions for WooCommerce 6 7 * Description: Restrict WooCommerce products by country using geolocation. Hide products, block purchases, or show a custom message per product. No API key required. 7 * Version: 1. 0.38 * Version: 1.1.0 8 9 * Author: WindCodex 9 10 * Author URI: https://www.windcodex.com … … 26 27 // --- Constants ---------------------------------------------------------------- 27 28 28 define( 'GEOBLOCK_VERSION', '1. 0.3' );29 define( 'GEOBLOCK_VERSION', '1.1.0' ); 29 30 define( 'GEOBLOCK_PLUGIN_FILE', __FILE__ ); 30 31 define( 'GEOBLOCK_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); … … 130 131 require_once GEOBLOCK_PLUGIN_DIR . 'includes/class-geoblock-loader.php'; 131 132 require_once GEOBLOCK_PLUGIN_DIR . 'includes/class-geoblock-geolocation.php'; 133 require_once GEOBLOCK_PLUGIN_DIR . 'includes/class-geoblock-regions.php'; 134 require_once GEOBLOCK_PLUGIN_DIR . 'includes/class-geoblock-health.php'; 132 135 require_once GEOBLOCK_PLUGIN_DIR . 'includes/class-geoblock-restrictions.php'; 133 136 require_once GEOBLOCK_PLUGIN_DIR . 'includes/class-geoblock-product.php';
Note:
See TracChangeset
for help on using the changeset viewer.