'wordpress-plugin', 'utm_medium' => 'plugin', 'utm_campaign' => $campaign, ], $url); } define('ADVOCATE_LOOP_WOO_MIN_EXT_API', 1); /** * Autoloader scoped to this add-on's namespace subtree. Stacks with core's * autoloader; declines anything outside AdvocateLoop\WooCommerce\. */ spl_autoload_register(function ($class) { $prefix = 'AdvocateLoop\\WooCommerce\\'; $len = strlen($prefix); if (strncmp($prefix, $class, $len) !== 0) { return; } $relative = substr($class, $len); $file = ADVOCATE_LOOP_WOO_DIR . 'includes/WooCommerce/' . str_replace('\\', '/', $relative) . '.php'; if (file_exists($file)) { require $file; } }); /** * Safe accessor for this add-on's settings section in the AdvocateLoop core. */ function advocate_loop_woo_settings(): array { if (!function_exists('advocate_loop_get_settings')) { return []; } try { $settings = advocate_loop_get_settings('woocommerce'); return is_array($settings) ? $settings : []; } catch (\Throwable $e) { // Defensive breadcrumb only — never breaks the site, just records why // defaults were used. Gated on WP_DEBUG so nothing is written in normal // production environments. if (defined('WP_DEBUG') && WP_DEBUG) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- debug-only diagnostic, guarded by WP_DEBUG. error_log('AdvocateLoop for WooCommerce: settings read failed (' . $e->getMessage() . '); using defaults.'); } return []; } } function advocate_loop_woo_core_compatible(): bool { return function_exists('advocate_loop_get_settings') && defined('ADVOCATE_LOOP_EXT_API') && ADVOCATE_LOOP_EXT_API >= ADVOCATE_LOOP_WOO_MIN_EXT_API; } /** * Register the WooCommerce tab and platform provider into the core registry. */ add_action('advocate_loop_register_extensions', function () { if (!advocate_loop_woo_core_compatible()) { return; } \AdvocateLoop\Admin\Extensions::register_tab([ 'id' => 'woocommerce', 'label' => __('WooCommerce', 'advocateloop-for-woocommerce'), 'priority' => 20, 'icon' => '', 'render' => function () { include ADVOCATE_LOOP_WOO_DIR . 'includes/views/tab-woocommerce.php'; }, ]); // Upgrade tracking to the 'woocommerce' platform when WooCommerce is // active and the integration has not been disabled in settings. \AdvocateLoop\Admin\Extensions::register_platform('woocommerce', function () { if (!class_exists('WooCommerce')) { return false; } $woo = advocate_loop_woo_settings(); return !(isset($woo['enabled']) && !$woo['enabled']); }, 20); }); /** * Boot the integration. Runs after core's default-priority plugins_loaded * handler (advocate_loop_init at priority 10) so the core helpers, registry, * and ADVOCATE_LOOP_EXT_API constant are all defined. */ add_action('plugins_loaded', function () { if (!advocate_loop_woo_core_compatible()) { add_action('admin_notices', function () { if (!current_user_can('activate_plugins')) { return; } echo '

'; echo esc_html__('AdvocateLoop for WooCommerce needs the AdvocateLoop plugin (a compatible version) to be installed and active. Please install or update AdvocateLoop.', 'advocateloop-for-woocommerce'); echo '

'; }); return; } if (!class_exists('WooCommerce')) { return; } if (function_exists('advocate_loop_is_connected') && advocate_loop_is_connected()) { (new \AdvocateLoop\WooCommerce\Integration())->init(); } }, 11); /** * Enqueue the WooCommerce tab's settings script on the AdvocateLoop admin page. */ add_action('admin_enqueue_scripts', function ($hook) { if (strpos((string) $hook, 'advocate-loop') === false) { return; } wp_enqueue_script( 'advocate-loop-woo-settings', ADVOCATE_LOOP_WOO_URL . 'assets/js/woo-settings.js', ['jquery', 'advocate-loop-settings-js'], ADVOCATE_LOOP_WOO_VERSION, true ); wp_localize_script('advocate-loop-woo-settings', 'advocateLoopWooSettings', [ 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('advocate_loop_admin_nonce'), ]); }, 110); add_filter('plugin_row_meta', function ($links, $file) { if ($file !== plugin_basename(ADVOCATE_LOOP_WOO_FILE)) { return $links; } $site_plain = esc_url(ADVOCATE_LOOP_WOO_SITE_URL); $site_utm = esc_url(advocate_loop_woo_utm(ADVOCATE_LOOP_WOO_SITE_URL, 'plugins-screen-site')); foreach ($links as $i => $link) { if (strpos($link, $site_plain) !== false) { $links[$i] = str_replace($site_plain, $site_utm, $link); break; } } $links[] = '' . esc_html__('Documentation', 'advocateloop-for-woocommerce') . ''; return $links; }, 10, 2);