| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Plugin Name: WP Map Block |
| 5 |
* Plugin URI: https://wpmapblock.com/ |
| 6 |
* Description: All-in-one interactive map builder for WordPress. Build maps visually with markers, categories, clustering, shapes and dynamic data across Google Maps, OpenStreetMap, Mapbox, MapTiler, Esri and more — no API key to start. |
| 7 |
* Author: Kodezen |
| 8 |
* Author URI: https://kodezen.com/ |
| 9 |
* Version: 3.0.0 |
| 10 |
* Requires at least: 6.5 |
| 11 |
* Requires PHP: 7.4 |
| 12 |
* License: GPL2+ |
| 13 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt |
| 14 |
* Text Domain: wp-map-block |
| 15 |
* Domain Path: /languages/ |
| 16 |
* |
| 17 |
* Freemium deployment (StoreEngine): this repo is the full/pro source. The paths |
| 18 |
* below are premium-only — StoreEngine's freemium deployment STRIPS them to build |
| 19 |
* the free wordpress.org zip, and keeps them in the pro zip. The SDK + license |
| 20 |
* bootstrap under includes/Pro/License.php are intentionally NOT listed, so they |
| 21 |
* ship in both builds (a free site can still activate a licence and pull pro). |
| 22 |
* SE Premium Only: /includes/Pro/Loader.php, /includes/Pro/Features |
| 23 |
* @fs_premium_only /includes/Pro/Loader.php, /includes/Pro/Features |
| 24 |
*/ |
| 25 |
|
| 26 |
// Exit if accessed directly. |
| 27 |
if (!defined('ABSPATH')) { |
| 28 |
exit; |
| 29 |
} |
| 30 |
|
| 31 |
// Composer autoloader — loaded at most ONCE across both builds. The free |
| 32 |
// (wp-map-block) and premium (wp-map-block-premium) zips bundle an identical |
| 33 |
// vendor tree whose autoloader class name — ComposerAutoloaderInit<hash> — is |
| 34 |
// derived from the shared composer.json, so it is the SAME in both. If both are |
| 35 |
// active, the second include fatally redeclares that class ("Cannot declare |
| 36 |
// class ... already in use") before any hook runs, which is what blocks the Pro |
| 37 |
// build from activating alongside free. Guard with a shared sentinel: whichever |
| 38 |
// build loads first registers the autoloader; the other reuses it (the vendor |
| 39 |
// trees are identical, so the SDK it needs is already there). Once loaded, the |
| 40 |
// single-instance enforcer / admin notice below retires the redundant copy. |
| 41 |
if (!defined('WPMAPBLOCK_VENDOR_LOADED')) { |
| 42 |
define('WPMAPBLOCK_VENDOR_LOADED', true); |
| 43 |
if (file_exists(dirname(__FILE__) . '/vendor/autoload.php')) { |
| 44 |
require_once dirname(__FILE__) . '/vendor/autoload.php'; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// StoreEngine SDK — must be loaded at FILE SCOPE, not inside boot(). The SDK |
| 49 |
// registers its version election on `plugins_loaded` at priorities 0 and 1, so |
| 50 |
// requiring it inside boot() (which itself runs on plugins_loaded, priority 10) |
| 51 |
// is already too late — those callbacks never fire, se_license_init() is never |
| 52 |
// defined, and licensing silently dies (this is exactly what broke Pro when it |
| 53 |
// ran alone, without the free build loading the SDK on its behalf). Requiring it |
| 54 |
// here — before plugins_loaded fires — makes the version election immune to load |
| 55 |
// order, per the SDK's own init.php header. Composer's autoload_files usually |
| 56 |
// pulls this in via the require above; this direct require keeps it working even |
| 57 |
// in a build where the SDK is dropped from the autoloader. The SDK's internal |
| 58 |
// function_exists guard makes the double require_once a no-op. |
| 59 |
if (file_exists(dirname(__FILE__) . '/vendor/storeengine/wordpress-sdk/init.php')) { |
| 60 |
require_once dirname(__FILE__) . '/vendor/storeengine/wordpress-sdk/init.php'; |
| 61 |
} |
| 62 |
|
| 63 |
if (!class_exists('WPMapBlock')) { |
| 64 |
|
| 65 |
/** |
| 66 |
* Main plugin bootstrap. Keeps the historical singleton shape so the |
| 67 |
* upgrade from 2.x is seamless for the existing install base. |
| 68 |
*/ |
| 69 |
final class WPMapBlock |
| 70 |
{ |
| 71 |
private static $instances = []; |
| 72 |
|
| 73 |
protected function __construct() |
| 74 |
{ |
| 75 |
$this->define_constant(); |
| 76 |
register_activation_hook(__FILE__, [$this, 'activate']); |
| 77 |
add_action('plugins_loaded', [$this, 'boot']); |
| 78 |
} |
| 79 |
|
| 80 |
public function define_constant() |
| 81 |
{ |
| 82 |
define('WPMAPBLOCK_VERSION', '3.0.0'); |
| 83 |
define('WPMAPBLOCK_PLUGIN_FILE', __FILE__); |
| 84 |
define('WPMAPBLOCK_PLUGIN_BASENAME', plugin_basename(__FILE__)); |
| 85 |
define('WPMAPBLOCK_PLUGIN_SLUG', 'wp-map-block'); |
| 86 |
define('WPMAPBLOCK_PLUGIN_ROOT_URI', plugins_url('/', __FILE__)); |
| 87 |
define('WPMAPBLOCK_ROOT_DIR_PATH', plugin_dir_path(__FILE__)); |
| 88 |
define('WPMAPBLOCK_ASSETS_DIR_PATH', WPMAPBLOCK_ROOT_DIR_PATH . 'assets/'); |
| 89 |
define('WPMAPBLOCK_ASSETS_URI', WPMAPBLOCK_PLUGIN_ROOT_URI . 'assets/'); |
| 90 |
define('WPMAPBLOCK_BUILD_URI', WPMAPBLOCK_PLUGIN_ROOT_URI . 'build/'); |
| 91 |
define('WPMAPBLOCK_BUILD_DIR_PATH', WPMAPBLOCK_ROOT_DIR_PATH . 'build/'); |
| 92 |
define('WPMAPBLOCK_REST_NAMESPACE', 'wpmb/v1'); |
| 93 |
define('WPMAPBLOCK_POST_TYPE', 'wpmb_map'); |
| 94 |
|
| 95 |
// ── Freemium (StoreEngine deployment) ───────────────────────────── |
| 96 |
// The SDK bootstrap + Pro feature modules already speak these |
| 97 |
// WPMB_PRO_* names. In the single-codebase model they map onto THIS |
| 98 |
// plugin: the SDK's product/slug identify the store product, while the |
| 99 |
// package file/dir are just this plugin. (Overridable via wp-config.) |
| 100 |
if (!defined('WPMB_PRO_PRODUCT_ID')) { |
| 101 |
define('WPMB_PRO_PRODUCT_ID', 399); |
| 102 |
} |
| 103 |
if (!defined('WPMB_PRO_LICENSE_SERVER')) { |
| 104 |
define('WPMB_PRO_LICENSE_SERVER', 'https://store.kodezen.com/'); |
| 105 |
} |
| 106 |
define('WPMB_PRO_VERSION', WPMAPBLOCK_VERSION); |
| 107 |
define('WPMB_PRO_PLUGIN_FILE', WPMAPBLOCK_PLUGIN_FILE); |
| 108 |
define('WPMB_PRO_PLUGIN_BASENAME', WPMAPBLOCK_PLUGIN_BASENAME); |
| 109 |
define('WPMB_PRO_PLUGIN_SLUG', 'wp-map-block-pro'); |
| 110 |
define('WPMB_PRO_DIR', WPMAPBLOCK_ROOT_DIR_PATH); |
| 111 |
define('WPMB_PRO_URI', WPMAPBLOCK_PLUGIN_ROOT_URI); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Autoloader-free requires kept explicit for clarity during the rebuild. |
| 116 |
*/ |
| 117 |
public function boot() |
| 118 |
{ |
| 119 |
$inc = WPMAPBLOCK_ROOT_DIR_PATH . 'includes/'; |
| 120 |
|
| 121 |
require_once $inc . 'Config.php'; |
| 122 |
require_once $inc . 'Pro.php'; |
| 123 |
require_once $inc . 'Settings.php'; |
| 124 |
require_once $inc . 'Presets.php'; |
| 125 |
require_once $inc . 'Recommendations.php'; |
| 126 |
require_once $inc . 'PostType.php'; |
| 127 |
require_once $inc . 'Integrations/Manager.php'; |
| 128 |
require_once $inc . 'api.php'; |
| 129 |
require_once $inc . 'Rest/Maps_Controller.php'; |
| 130 |
require_once $inc . 'Rest/Settings_Controller.php'; |
| 131 |
require_once $inc . 'Rest/Data_Controller.php'; |
| 132 |
require_once $inc . 'Rest/Integrations_Controller.php'; |
| 133 |
require_once $inc . 'Rest/Promos_Controller.php'; |
| 134 |
require_once $inc . 'Admin/Sdk.php'; |
| 135 |
require_once $inc . 'Admin/Onboarding.php'; |
| 136 |
require_once $inc . 'Frontend/Assets.php'; |
| 137 |
require_once $inc . 'Frontend/DataSources.php'; |
| 138 |
require_once $inc . 'Frontend/Renderer.php'; |
| 139 |
require_once $inc . 'Frontend/Shortcode.php'; |
| 140 |
require_once $inc . 'Blocks/Embed_Block.php'; |
| 141 |
require_once $inc . 'Legacy/Block.php'; |
| 142 |
|
| 143 |
WPMapBlock\PostType::init(); |
| 144 |
WPMapBlock\Integrations\Manager::init(); |
| 145 |
WPMapBlock\Rest\Maps_Controller::init(); |
| 146 |
WPMapBlock\Rest\Settings_Controller::init(); |
| 147 |
WPMapBlock\Rest\Data_Controller::init(); |
| 148 |
WPMapBlock\Rest\Integrations_Controller::init(); |
| 149 |
WPMapBlock\Rest\Promos_Controller::init(); |
| 150 |
WPMapBlock\Admin\Onboarding::init(); |
| 151 |
WPMapBlock\Frontend\Assets::init(); |
| 152 |
WPMapBlock\Frontend\Shortcode::init(); |
| 153 |
WPMapBlock\Blocks\Embed_Block::init(); |
| 154 |
WPMapBlock\Legacy\Block::init(); |
| 155 |
|
| 156 |
if (is_admin()) { |
| 157 |
require_once $inc . 'Admin/Menu.php'; |
| 158 |
WPMapBlock\Admin\Menu::init(); |
| 159 |
|
| 160 |
// Only ONE WP Map Block may run at a time, and the Pro build wins. |
| 161 |
// If a free and a Pro copy are both active (e.g. the free wp.org |
| 162 |
// plugin plus a separately-installed Pro), retire the free one — |
| 163 |
// and if that auto-deactivation didn't take, warn with a notice. |
| 164 |
add_action('admin_init', [$this, 'enforce_single_instance']); |
| 165 |
add_action('admin_notices', [$this, 'coexistence_notice']); |
| 166 |
} |
| 167 |
|
| 168 |
// Dev-only: unlock all Pro features so the team can build/test the Pro |
| 169 |
// UI without a licence. Opt-in via `define('WPMB_DEV_UNLOCK', true)`; |
| 170 |
// the dev/ folder is excluded from the release ZIP so it never ships. |
| 171 |
// Loaded BEFORE the Pro loader so its `has_feature`/`active` filters |
| 172 |
// are in place when feature modules decide whether to hook. |
| 173 |
if (defined('WPMB_DEV_UNLOCK') && WPMB_DEV_UNLOCK && file_exists(__DIR__ . '/dev/pro-unlock.php')) { |
| 174 |
require_once __DIR__ . '/dev/pro-unlock.php'; |
| 175 |
} |
| 176 |
|
| 177 |
// ── Freemium (single-codebase) ──────────────────────────────────── |
| 178 |
// Licensing/SDK ships in EVERY build (ECM-style) so a free site can |
| 179 |
// activate a licence and pull the Pro package. The premium loader + |
| 180 |
// feature modules live under the @fs_premium_only paths (see header) |
| 181 |
// and are stripped from the free zip — so guard their load: a stripped |
| 182 |
// build simply has no Pro, no fatal. |
| 183 |
// |
| 184 |
// NB: the StoreEngine SDK itself is required at FILE SCOPE near the top |
| 185 |
// of this file (its version election hooks plugins_loaded 0/1, which |
| 186 |
// have already fired by the time boot() runs at priority 10). Here we |
| 187 |
// only wire OUR code that sits on top of the SDK. |
| 188 |
if (file_exists($inc . 'Pro/License.php')) { |
| 189 |
require_once $inc . 'Pro/License.php'; |
| 190 |
WPMapBlockPro\License::init(); |
| 191 |
} |
| 192 |
if (file_exists($inc . 'Pro/Loader.php')) { // absent in the free zip |
| 193 |
require_once $inc . 'Pro/Loader.php'; |
| 194 |
WPMapBlockPro\Loader::init(); |
| 195 |
} |
| 196 |
|
| 197 |
// Extension point: integrations hook here to register features, data |
| 198 |
// sources and REST routes once free + Pro are both wired. |
| 199 |
do_action('wpmb/loaded'); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Enforce a single active WP Map Block, with the Pro build taking |
| 204 |
* precedence. A build is "Pro" when it carries the premium loader |
| 205 |
* (includes/Pro/Loader.php) — present in the pro package, stripped from |
| 206 |
* the free one. So when a customer buys Pro and installs it alongside the |
| 207 |
* free plugin, the free copy is automatically deactivated (and vice-versa |
| 208 |
* the free build stands down if a Pro copy is active). Same-slug installs |
| 209 |
* simply replace in place, so this is a no-op there. |
| 210 |
*/ |
| 211 |
public function enforce_single_instance() |
| 212 |
{ |
| 213 |
if (!current_user_can('activate_plugins') || !is_admin()) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
$siblings = $this->find_sibling_maps(); |
| 218 |
|
| 219 |
if ($this->is_pro_build()) { |
| 220 |
// I'm Pro — retire every other WP Map Block copy (free or pro). |
| 221 |
$retire = array_merge($siblings['free'], $siblings['pro']); |
| 222 |
if ($retire) { |
| 223 |
deactivate_plugins($retire); |
| 224 |
} |
| 225 |
} elseif ($siblings['pro']) { |
| 226 |
// I'm the free build and a Pro copy is active — stand down. |
| 227 |
deactivate_plugins(WPMAPBLOCK_PLUGIN_BASENAME); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Fallback for when auto-deactivation couldn't run (e.g. the request that |
| 233 |
* activated Pro lacked caps, or a filter re-activated free): if this is |
| 234 |
* the Pro build and a free copy is still active, nag the admin to |
| 235 |
* deactivate it, with a one-click link. Shows nothing once the free copy |
| 236 |
* is gone — so enforce_single_instance() succeeding suppresses it. |
| 237 |
*/ |
| 238 |
public function coexistence_notice() |
| 239 |
{ |
| 240 |
if (!current_user_can('activate_plugins') || !$this->is_pro_build()) { |
| 241 |
return; |
| 242 |
} |
| 243 |
$free = $this->find_sibling_maps()['free']; |
| 244 |
if (!$free) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
$basename = $free[0]; |
| 249 |
$deactivate_url = wp_nonce_url( |
| 250 |
self_admin_url('plugins.php?action=deactivate&plugin=' . rawurlencode($basename)), |
| 251 |
'deactivate-plugin_' . $basename |
| 252 |
); |
| 253 |
|
| 254 |
echo '<div class="notice notice-error"><p>'; |
| 255 |
echo '<strong>' . esc_html__('WP Map Block Pro is active.', 'wp-map-block') . '</strong> '; |
| 256 |
echo esc_html__('The free WP Map Block plugin is also active and must be deactivated to avoid a conflict (both bundle the same libraries).', 'wp-map-block'); |
| 257 |
echo ' <a href="' . esc_url($deactivate_url) . '" class="button button-small">' |
| 258 |
. esc_html__('Deactivate free version', 'wp-map-block') . '</a>'; |
| 259 |
echo '</p></div>'; |
| 260 |
} |
| 261 |
|
| 262 |
/** A build is "Pro" when it carries the premium loader (stripped from free). */ |
| 263 |
protected function is_pro_build(): bool |
| 264 |
{ |
| 265 |
return file_exists(WPMAPBLOCK_ROOT_DIR_PATH . 'includes/Pro/Loader.php'); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Every OTHER active "WP Map Block" plugin (not this file), split into |
| 270 |
* free vs pro by whether that copy carries the premium loader. Shared by |
| 271 |
* the single-instance enforcer and the fallback admin notice. |
| 272 |
* |
| 273 |
* @return array{free: string[], pro: string[]} |
| 274 |
*/ |
| 275 |
protected function find_sibling_maps(): array |
| 276 |
{ |
| 277 |
if (!function_exists('get_plugin_data')) { |
| 278 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 279 |
} |
| 280 |
|
| 281 |
$self = WPMAPBLOCK_PLUGIN_BASENAME; |
| 282 |
$free = []; |
| 283 |
$pro = []; |
| 284 |
foreach ((array) get_option('active_plugins', []) as $basename) { |
| 285 |
if ($basename === $self) { |
| 286 |
continue; |
| 287 |
} |
| 288 |
$file = WP_PLUGIN_DIR . '/' . $basename; |
| 289 |
if (!file_exists($file)) { |
| 290 |
continue; |
| 291 |
} |
| 292 |
$data = get_plugin_data($file, false, false); |
| 293 |
if (false === stripos((string) ($data['Name'] ?? ''), 'WP Map Block')) { |
| 294 |
continue; // not one of ours |
| 295 |
} |
| 296 |
if (file_exists(dirname($file) . '/includes/Pro/Loader.php')) { |
| 297 |
$pro[] = $basename; |
| 298 |
} else { |
| 299 |
$free[] = $basename; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return ['free' => $free, 'pro' => $pro]; |
| 304 |
} |
| 305 |
|
| 306 |
public function activate() |
| 307 |
{ |
| 308 |
require_once WPMAPBLOCK_ROOT_DIR_PATH . 'includes/PostType.php'; |
| 309 |
WPMapBlock\PostType::register(); |
| 310 |
flush_rewrite_rules(); |
| 311 |
|
| 312 |
$is_first = !get_option('wpmb_first_install_time'); |
| 313 |
if ($is_first) { |
| 314 |
add_option('wpmb_first_install_time', time()); |
| 315 |
} |
| 316 |
update_option('wpmb_version', WPMAPBLOCK_VERSION); |
| 317 |
|
| 318 |
// Queue the first-run Setup screen; in-place updates are handled at |
| 319 |
// admin_init by Onboarding::detect_version_change(). |
| 320 |
require_once WPMAPBLOCK_ROOT_DIR_PATH . 'includes/Admin/Onboarding.php'; |
| 321 |
if ($is_first) { |
| 322 |
WPMapBlock\Admin\Onboarding::flag_first_install(); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
protected function __clone() {} |
| 327 |
|
| 328 |
public function __wakeup() |
| 329 |
{ |
| 330 |
throw new \Exception('Cannot unserialize singleton'); |
| 331 |
} |
| 332 |
|
| 333 |
public static function getInstance() |
| 334 |
{ |
| 335 |
$subclass = static::class; |
| 336 |
if (!isset(self::$instances[$subclass])) { |
| 337 |
self::$instances[$subclass] = new static(); |
| 338 |
} |
| 339 |
return self::$instances[$subclass]; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
if (!function_exists('WPMapBlock_Start')) { |
| 345 |
function WPMapBlock_Start() |
| 346 |
{ |
| 347 |
return WPMapBlock::getInstance(); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
WPMapBlock_Start(); |
| 352 |
|