', 'front'). */ /** The REST route of this request — "/wc/store/v1/cart", "/masiou-admin-ai/v1/chat" — or '' when it is not one. */ function masiou_admin_ai_rest_route() { // 1.20.1: both sources are sanitized and reduced to the characters a route can hold before anything compares them $clean = function ($r) { return (string) preg_replace('#[^A-Za-z0-9/_.\-]#', '', (string) $r); }; if (isset($_GET['rest_route']) && is_string($_GET['rest_route'])) return '/' . ltrim($clean(sanitize_text_field(wp_unslash($_GET['rest_route']))), '/'); $uri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : ''; $path = $uri === '' ? '' : $clean(wp_parse_url($uri, PHP_URL_PATH)); $prefix = '/' . trim(rest_get_url_prefix(), '/') . '/'; $pos = $path === '' ? false : strpos($path, $prefix); return $pos === false ? '' : '/' . substr($path, $pos + strlen($prefix)); } /** * The decision, as a pure function of the request kind (tests call it directly). * @param string $route REST route ('' when not a REST request) * @param bool $doing_ajax admin-ajax.php * @param string $ajax_action the admin-ajax action * @param bool $is_admin wp-admin page or admin-post.php * @param bool $cron_or_cli WP-Cron or WP-CLI */ function masiou_admin_ai_should_load_for($route, $doing_ajax, $ajax_action, $is_admin, $cron_or_cli) { if ($cron_or_cli) return true; if ($doing_ajax) { if (strpos((string) $ajax_action, 'masiou_admin_ai') === 0) return true; return (bool) apply_filters('masiou_admin_ai_should_load', false, 'ajax'); } if ($is_admin) return true; $route = (string) $route; if ($route !== '') { if (strpos($route, '/masiou-admin-ai/') === 0) return true; // the plugin's own REST routes return (bool) apply_filters('masiou_admin_ai_should_load', false, 'rest:' . $route); // any other namespace: only when a site's own code asks for it } return (bool) apply_filters('masiou_admin_ai_should_load', false, 'front'); } function masiou_admin_ai_should_load() { $route = masiou_admin_ai_rest_route(); if ($route === '' && defined('REST_REQUEST') && REST_REQUEST) return true; // a process that declared itself REST without a route (internal dispatch) $action = isset($_REQUEST['action']) && is_string($_REQUEST['action']) ? sanitize_key(wp_unslash($_REQUEST['action'])) : ''; return masiou_admin_ai_should_load_for($route, wp_doing_ajax(), $action, is_admin(), wp_doing_cron() || (defined('WP_CLI') && WP_CLI)); } /** Lazy accessor: loads the plugin the first time it is needed. */ function masiou_admin_ai() { if (!class_exists('Masiou_Admin_AI_Plugin', false)) { require_once MASIOU_ADMIN_AI_DIR . 'includes/class-plugin.php'; } return Masiou_Admin_AI_Plugin::instance(); } /** * 1.10.0: is this a front-end page view — the one kind of request on which the plugin may still load, late, for a * logged-in user who sees the WordPress toolbar on the site? Not REST, not admin-ajax, not wp-admin, not cron, not * WP-CLI, not XML-RPC. */ function masiou_admin_ai_is_page_view() { if (is_admin() || wp_doing_ajax() || wp_doing_cron() || (defined('WP_CLI') && WP_CLI) || (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) || (defined('REST_REQUEST') && REST_REQUEST)) return false; return masiou_admin_ai_rest_route() === ''; } /** * 1.10.0: the floating bar and the panel on the site itself — for a logged-in user who may use the assistant and sees the * WordPress toolbar on the front end, when Settings → Behaviour allows it and the user has not hidden them for * themselves (toolbar menu). Decided on `wp`, once the visitor and the query are known. A visitor costs nothing here: * no file, no class, no query (WordPress knows the current user already). A logged-in user who sees the toolbar costs * one option read (the settings) before the plugin loads; one who does not see it, or who hid the bar, costs nothing * more than this check. */ function masiou_admin_ai_maybe_load_front() { if (!is_user_logged_in() || !is_admin_bar_showing()) return; if (is_feed() || is_robots() || is_trackback() || is_embed() || (function_exists('is_favicon') && is_favicon())) return; if (get_user_option('masiou_admin_ai_front_bar') === 'off') return; // hidden by this user, for this site if (!class_exists('Masiou_Admin_AI_Settings', false)) require_once MASIOU_ADMIN_AI_DIR . 'includes/class-settings.php'; $settings = new Masiou_Admin_AI_Settings(); if (!$settings->get('front_bar') || !$settings->user_can_use()) return; masiou_admin_ai()->front(); } /** * Does the site have promotions to show? Read from the options WordPress has already loaded for this request (the * promotions are stored autoloaded): no query, and nothing at all on a site without promotions. */ function masiou_admin_ai_has_promos() { $all = wp_load_alloptions(); return isset($all['masiou_admin_ai_promos']) && !in_array($all['masiou_admin_ai_promos'], ['', 'a:0:{}'], true); } if (masiou_admin_ai_should_load()) { add_action('plugins_loaded', 'masiou_admin_ai'); } elseif (masiou_admin_ai_is_page_view()) { add_action('wp', 'masiou_admin_ai_maybe_load_front', 100); } // Promotions (a bar above the header, a banner in the shop, a note on product pages): a page view loads their one small // file — and nothing else of the plugin — only while the site has promotions. if (masiou_admin_ai_is_page_view() && masiou_admin_ai_has_promos()) { require_once MASIOU_ADMIN_AI_DIR . 'includes/class-promos.php'; Masiou_Admin_AI_Promos::front(); } register_activation_hook(__FILE__, function ($network_wide = false) { require_once MASIOU_ADMIN_AI_DIR . 'includes/class-plugin.php'; Masiou_Admin_AI_Plugin::activate((bool) $network_wide); }); register_deactivation_hook(__FILE__, function () { require_once MASIOU_ADMIN_AI_DIR . 'includes/class-plugin.php'; Masiou_Admin_AI_Plugin::deactivate(); }); } // end of the single-copy guard (1.19.0)