| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPBrigade Telemetry SDK bootstrap broker. |
| 4 |
* |
| 5 |
* @package wpbrigade_sdk |
| 6 |
* @since 3.2.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! defined( 'WPBRIGADE_SDK_VERSION' ) ) { |
| 14 |
require_once __DIR__ . '/config.php'; |
| 15 |
} |
| 16 |
|
| 17 |
$wpb_sdk_version = WPBRIGADE_SDK_VERSION; |
| 18 |
|
| 19 |
if ( ! function_exists( 'wpb_sdk_register_provider' ) ) { |
| 20 |
/** |
| 21 |
* Register an SDK provider from a plugin bundle. |
| 22 |
* |
| 23 |
* @param array $provider Provider payload. |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
function wpb_sdk_register_provider( $provider ) { |
| 27 |
if ( ! isset( $GLOBALS['wpb_sdk_registry'] ) || ! is_array( $GLOBALS['wpb_sdk_registry'] ) ) { |
| 28 |
$GLOBALS['wpb_sdk_registry'] = array( |
| 29 |
'providers' => array(), |
| 30 |
'active_provider' => null, |
| 31 |
'runtime_loaded' => false, |
| 32 |
'initialized_modules' => array(), |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
$key = isset( $provider['provider_key'] ) ? $provider['provider_key'] : md5( wp_json_encode( $provider ) ); |
| 37 |
$GLOBALS['wpb_sdk_registry']['providers'][ $key ] = $provider; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Select the active runtime provider. |
| 42 |
* |
| 43 |
* @return array|null |
| 44 |
*/ |
| 45 |
function wpb_sdk_select_provider() { |
| 46 |
$registry = isset( $GLOBALS['wpb_sdk_registry'] ) ? $GLOBALS['wpb_sdk_registry'] : null; |
| 47 |
if ( empty( $registry['providers'] ) ) { |
| 48 |
return null; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! empty( $registry['active_provider'] ) ) { |
| 52 |
return $registry['active_provider']; |
| 53 |
} |
| 54 |
|
| 55 |
$providers = array_values( $registry['providers'] ); |
| 56 |
usort( |
| 57 |
$providers, |
| 58 |
function ( $left, $right ) { |
| 59 |
$left_ver = isset( $left['sdk_version'] ) ? (string) $left['sdk_version'] : '0'; |
| 60 |
$right_ver = isset( $right['sdk_version'] ) ? (string) $right['sdk_version'] : '0'; |
| 61 |
$by_ver = version_compare( $right_ver, $left_ver ); |
| 62 |
if ( 0 !== $by_ver ) { |
| 63 |
return $by_ver; |
| 64 |
} |
| 65 |
$left_key = isset( $left['provider_key'] ) ? (string) $left['provider_key'] : ''; |
| 66 |
$right_key = isset( $right['provider_key'] ) ? (string) $right['provider_key'] : ''; |
| 67 |
return strcmp( $left_key, $right_key ); |
| 68 |
} |
| 69 |
); |
| 70 |
|
| 71 |
$GLOBALS['wpb_sdk_registry']['active_provider'] = $providers[0]; |
| 72 |
return $providers[0]; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Load the selected runtime exactly once. |
| 77 |
* |
| 78 |
* @return bool |
| 79 |
*/ |
| 80 |
function wpb_sdk_ensure_runtime_loaded() { |
| 81 |
if ( function_exists( 'wpb_sdk_runtime_is_complete' ) && wpb_sdk_runtime_is_complete() ) { |
| 82 |
if ( isset( $GLOBALS['wpb_sdk_registry'] ) && is_array( $GLOBALS['wpb_sdk_registry'] ) ) { |
| 83 |
$GLOBALS['wpb_sdk_registry']['runtime_loaded'] = true; |
| 84 |
} |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
$provider = wpb_sdk_select_provider(); |
| 89 |
if ( empty( $provider ) || empty( $provider['runtime_file'] ) ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
require_once $provider['runtime_file']; |
| 94 |
|
| 95 |
if ( ! defined( 'WP_WPBRIGADE_SDK_VERSION' ) ) { |
| 96 |
$runtime_version = ! empty( $provider['sdk_version'] ) |
| 97 |
? (string) $provider['sdk_version'] |
| 98 |
: ( defined( 'WPBRIGADE_SDK_VERSION' ) ? WPBRIGADE_SDK_VERSION : '3.2.0' ); |
| 99 |
define( 'WP_WPBRIGADE_SDK_VERSION', $runtime_version ); |
| 100 |
} |
| 101 |
|
| 102 |
$complete = function_exists( 'wpb_sdk_runtime_is_complete' ) && wpb_sdk_runtime_is_complete(); |
| 103 |
if ( $complete && isset( $GLOBALS['wpb_sdk_registry'] ) && is_array( $GLOBALS['wpb_sdk_registry'] ) ) { |
| 104 |
$GLOBALS['wpb_sdk_registry']['runtime_loaded'] = true; |
| 105 |
} |
| 106 |
|
| 107 |
return $complete; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Shared dynamic initializer used by all bundled SDK copies. |
| 112 |
* |
| 113 |
* @param array $module Module configuration. |
| 114 |
* @return array|false |
| 115 |
*/ |
| 116 |
function wpb_sdk_dynamic_init( $module ) { |
| 117 |
if ( ! is_array( $module ) || empty( $module['slug'] ) || empty( $module['id'] ) ) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
if ( ! wpb_sdk_ensure_runtime_loaded() || ! class_exists( 'WPBRIGADE_Logger' ) ) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
if ( function_exists( 'wpb_sdk_apply_module_defaults' ) ) { |
| 126 |
$module = wpb_sdk_apply_module_defaults( $module ); |
| 127 |
} |
| 128 |
|
| 129 |
$slug = $module['slug']; |
| 130 |
|
| 131 |
if ( ! isset( $GLOBALS['wpb_sdk_registry']['modules'] ) || ! is_array( $GLOBALS['wpb_sdk_registry']['modules'] ) ) { |
| 132 |
$GLOBALS['wpb_sdk_registry']['modules'] = array(); |
| 133 |
} |
| 134 |
$GLOBALS['wpb_sdk_registry']['modules'][ $slug ] = $module; |
| 135 |
|
| 136 |
if ( |
| 137 |
isset( $GLOBALS['wpb_sdk_registry']['initialized_modules'][ $slug ] ) && |
| 138 |
true === $GLOBALS['wpb_sdk_registry']['initialized_modules'][ $slug ] |
| 139 |
) { |
| 140 |
if ( class_exists( 'WPBRIGADE_Logger', false ) ) { |
| 141 |
if ( function_exists( 'wpb_sdk_store_module_if_missing_compat' ) ) { |
| 142 |
wpb_sdk_store_module_if_missing_compat( $module ); |
| 143 |
} elseif ( method_exists( 'WPBRIGADE_Logger', 'wpb_sdk_store_module_if_missing' ) ) { |
| 144 |
WPBRIGADE_Logger::wpb_sdk_store_module_if_missing( $module ); |
| 145 |
} |
| 146 |
} |
| 147 |
if ( function_exists( 'wpb_sdk_register_opt_manager_for_module' ) ) { |
| 148 |
wpb_sdk_register_opt_manager_for_module( $module ); |
| 149 |
} |
| 150 |
if ( function_exists( 'wpb_sdk_enqueue_optin_initiator_backfill' ) ) { |
| 151 |
wpb_sdk_enqueue_optin_initiator_backfill( $slug ); |
| 152 |
} |
| 153 |
if ( function_exists( 'wpb_sdk_maybe_normalize_legacy_sdk_sharing_option' ) ) { |
| 154 |
wpb_sdk_maybe_normalize_legacy_sdk_sharing_option( $slug ); |
| 155 |
} |
| 156 |
$logger = WPBRIGADE_Logger::instance( $module['id'], $slug, true ); |
| 157 |
return array( |
| 158 |
'logger' => $logger, |
| 159 |
'slug' => $slug, |
| 160 |
'id' => $module['id'], |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
$logger = WPBRIGADE_Logger::instance( $module['id'], $slug, true ); |
| 165 |
$logger->wpb_init( $module ); |
| 166 |
|
| 167 |
if ( function_exists( 'wpb_sdk_register_opt_manager_for_module' ) ) { |
| 168 |
wpb_sdk_register_opt_manager_for_module( $module ); |
| 169 |
} |
| 170 |
|
| 171 |
if ( function_exists( 'wpb_sdk_enqueue_optin_initiator_backfill' ) ) { |
| 172 |
wpb_sdk_enqueue_optin_initiator_backfill( $slug ); |
| 173 |
} |
| 174 |
|
| 175 |
if ( function_exists( 'wpb_sdk_maybe_normalize_legacy_sdk_sharing_option' ) ) { |
| 176 |
wpb_sdk_maybe_normalize_legacy_sdk_sharing_option( $slug ); |
| 177 |
} |
| 178 |
|
| 179 |
$GLOBALS['wpb_sdk_registry']['initialized_modules'][ $slug ] = true; |
| 180 |
|
| 181 |
return array( |
| 182 |
'logger' => $logger, |
| 183 |
'slug' => $slug, |
| 184 |
'id' => $module['id'], |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Derive provider_key from where this start.php lives on disk. |
| 190 |
* |
| 191 |
* Bundled copy: {plugin}/lib/wpb-sdk/start.php → plugin folder basename. |
| 192 |
* Standalone wpb-sdk package: plugins/wpb-sdk/start.php → empty string. |
| 193 |
* |
| 194 |
* @param string $start_dir Directory containing start.php (__DIR__). |
| 195 |
* @return string |
| 196 |
*/ |
| 197 |
function wpb_sdk_detect_provider_key_from_path( $start_dir ) { |
| 198 |
$start_dir = function_exists( 'wp_normalize_path' ) |
| 199 |
? wp_normalize_path( $start_dir ) |
| 200 |
: str_replace( '\\', '/', $start_dir ); |
| 201 |
|
| 202 |
if ( preg_match( '#/lib/wpb-sdk$#', $start_dir ) ) { |
| 203 |
$plugin_root = dirname( dirname( $start_dir ) ); |
| 204 |
$folder = basename( $plugin_root ); |
| 205 |
|
| 206 |
if ( '' !== $folder && '.' !== $folder && '..' !== $folder ) { |
| 207 |
return sanitize_key( $folder ); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
return ''; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Resolve the telemetry API endpoint from WPBRIGADE_SDK_API_ENDPOINT. |
| 216 |
* |
| 217 |
* Loads this bundle's config.php only when the constant is not set yet. |
| 218 |
* Path constants (WPBRIGADE_SDK_DIR) are guarded in config.php, so requiring |
| 219 |
* it here is safe for the endpoint; all synced bundles share the same URL. |
| 220 |
* |
| 221 |
* @param string $sdk_dir Path to lib/wpb-sdk. |
| 222 |
* @return string API base URL without trailing slash, or empty if unavailable. |
| 223 |
*/ |
| 224 |
function wpb_sdk_parse_bundle_api_endpoint( $sdk_dir ) { |
| 225 |
if ( ! defined( 'WPBRIGADE_SDK_API_ENDPOINT' ) ) { |
| 226 |
$path = trailingslashit( $sdk_dir ) . 'config.php'; |
| 227 |
if ( is_readable( $path ) ) { |
| 228 |
require_once $path; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if ( defined( 'WPBRIGADE_SDK_API_ENDPOINT' ) ) { |
| 233 |
return rtrim( (string) WPBRIGADE_SDK_API_ENDPOINT, '/' ); |
| 234 |
} |
| 235 |
|
| 236 |
return ''; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
wpb_sdk_register_provider( |
| 241 |
array( |
| 242 |
'provider_key' => wpb_sdk_detect_provider_key_from_path( __DIR__ ), |
| 243 |
'sdk_version' => $wpb_sdk_version, |
| 244 |
'runtime_file' => __DIR__ . '/require.php', |
| 245 |
'sdk_dir' => __DIR__, |
| 246 |
'api_endpoint' => wpb_sdk_parse_bundle_api_endpoint( __DIR__ ), |
| 247 |
) |
| 248 |
); |
| 249 |
|
| 250 |
if ( ! function_exists( 'wpb_dynamic_init' ) ) { |
| 251 |
/** |
| 252 |
* Backward-compatible init proxy. |
| 253 |
* |
| 254 |
* @param array $module Module configuration. |
| 255 |
* @return array|false |
| 256 |
*/ |
| 257 |
function wpb_dynamic_init( $module ) { |
| 258 |
return wpb_sdk_dynamic_init( $module ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if ( ! function_exists( 'wpb_sdk_run_product_uninstall' ) ) { |
| 263 |
/** |
| 264 |
* Parse-time uninstall entry (stored in uninstall_plugins; must exist before runtime loads). |
| 265 |
* |
| 266 |
* @return void |
| 267 |
*/ |
| 268 |
function wpb_sdk_run_product_uninstall() { |
| 269 |
if ( function_exists( 'wpb_sdk_ensure_runtime_loaded' ) ) { |
| 270 |
wpb_sdk_ensure_runtime_loaded(); |
| 271 |
} |
| 272 |
if ( function_exists( 'wpb_sdk_migrate_legacy_lifecycle_callbacks' ) ) { |
| 273 |
wpb_sdk_migrate_legacy_lifecycle_callbacks(); |
| 274 |
} |
| 275 |
if ( function_exists( 'wpb_sdk_invoke_product_uninstall' ) ) { |
| 276 |
wpb_sdk_invoke_product_uninstall(); |
| 277 |
return; |
| 278 |
} |
| 279 |
if ( class_exists( 'WPBRIGADE_Logger', false ) && method_exists( 'WPBRIGADE_Logger', 'log_uninstallation' ) ) { |
| 280 |
WPBRIGADE_Logger::log_uninstallation(); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
if ( ! function_exists( 'wpb_sdk_bootstrap_runtime_for_uninstall' ) ) { |
| 286 |
/** |
| 287 |
* Load Logger before WordPress runs the stored uninstall_plugins callback. |
| 288 |
* |
| 289 |
* @return void |
| 290 |
*/ |
| 291 |
function wpb_sdk_bootstrap_runtime_for_uninstall() { |
| 292 |
if ( ! function_exists( 'wpb_sdk_ensure_runtime_loaded' ) ) { |
| 293 |
return; |
| 294 |
} |
| 295 |
$should_bootstrap = ( defined( 'WP_UNINSTALL_PLUGIN' ) && WP_UNINSTALL_PLUGIN ) |
| 296 |
|| ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && 'delete-plugin' === $_REQUEST['action'] ); |
| 297 |
if ( $should_bootstrap ) { |
| 298 |
wpb_sdk_ensure_runtime_loaded(); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
wpb_sdk_bootstrap_runtime_for_uninstall(); |
| 303 |
|