input; $httpUrl = new JUri($url); $baseUrl = new JUri(site_url()); /** * As WP Site Health suggests, an active session might close timeouts for loopback connection. * For this reason, every time an HTTP request is made, we should try to detect whether the * provided end-point has the same root of the current website, meaning that we are performing * a self connection, probably through the REST API. * * @since 10.1.39 */ if (strpos($httpUrl->toString(['host', 'path']), $baseUrl->toString(['host', 'path'])) !== false) { // loopback detected, terminate the session before starting a request session_write_close(); } return $preempt; }, 10, 3); /** * Suppress "session active" critical error when Site Health performs its tests. * * Even if the session is active on Site Health, the plugin always call the * `session_write_close` method before making any HTTP requests. * * @since 5.5.0 */ add_action('init', function() { global $pagenow; // check if the current page is Site Health if (preg_match("/^site-health/i", $pagenow)) { // always write session to avoid receiving a critical issue session_write_close(); } }); } /** * Destroys the current active session, only if it already exists. * * @param boolean $restart True to immediately restart a new session. * * @return void */ public static function destroy($restart = true) { if (!self::isStarted()) { return; } session_destroy(); if ($restart) { self::start(); } } /** * Checks if a session has been started. * * @return boolean True if active, otherwise false. */ public static function isStarted() { /** * Make sure the session status is currently active. This because, * in case someone had manually closed the session, this method would * have improperly returned true. * * @since 10.1.43 */ return session_status() === PHP_SESSION_ACTIVE && self::getId(); } /** * Returns the session ID, if any. * * @return string The unique session ID if active, otherwise an empty string. */ public static function getId() { return session_id(); } /** * Returns the session name, if any. * * @return string The session name. */ public static function getName() { return session_name(); } }