first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
<?php
namespace WPML\Core\REST;
class RewriteRules implements \IWPML_REST_Action, \IWPML_DIC_Action {
/** @var \SitePress */
private $sitepress;
/**
* @param \SitePress $sitepress
*/
public function __construct( \SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
public function add_hooks() {
add_action( 'init', [ $this, 'addOptionRewriteRulesHook' ] );
}
public function addOptionRewriteRulesHook() {
if ( $this->isLangInDirectory() && $this->isUseDirectoryForDefaultLanguage() && $this->isInstalledInSubdirectory() ) {
add_filter( 'option_rewrite_rules', [ $this, 'updateRules' ] );
}
}
public function updateRules( $rewriteRules ) {
if ( ! is_array( $rewriteRules ) ) {
return $rewriteRules;
}
$subdirectory = $this->getSubdirectory();
$mapKeys = function ( $value, $key ) use ( $subdirectory ) {
if ( $key === '^wp-json/?$' ) {
$key = "^($subdirectory/)?wp-json/?$";
} elseif ( $key === '^wp-json/(.*)?' ) {
$key = "^($subdirectory/)?wp-json/(.*)?";
$value = str_replace( 'matches[1]', 'matches[2]', $value );
}
return [ $key => $value ];
};
return \wpml_collect( $rewriteRules )->mapWithKeys( $mapKeys )->toArray();
}
/**
* @return bool
*/
private function isLangInDirectory() {
return (int) $this->sitepress->get_setting( 'language_negotiation_type' ) === WPML_LANGUAGE_NEGOTIATION_TYPE_DIRECTORY;
}
/**
* @return bool
*/
private function isUseDirectoryForDefaultLanguage() {
$urlSettings = $this->sitepress->get_setting( 'urls' );
return isset( $urlSettings['directory_for_default_language'] ) && $urlSettings['directory_for_default_language'];
}
/**
* @return bool
*/
private function isInstalledInSubdirectory() {
return ! empty( $this->getSubdirectory() );
}
/**
* @return string
*/
private function getSubdirectory() {
$url = get_option( 'home' );
$home_path = trim( (string) parse_url( $url, PHP_URL_PATH ), '/' );
return $home_path;
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace WPML\Core\REST;
class Status {
const PING_KEY = 'wp-rest-enabled-ping';
const CACHE_EXPIRATION_IN_SEC = 3600;
const ENABLED = 'enabled';
const DISABLED = 'disabled';
const TIMEOUT = 'timeout';
/** @var \WP_Http */
private $wp_http;
/**
* @param \WP_Http $wp_http
*/
public function __construct( \WP_Http $wp_http ) {
$this->wp_http = $wp_http;
}
public function isEnabled() {
// Check this condition first to avoid infinite loop in testing PING request made below.
if ( wpml_is_rest_request() ) {
return true;
}
$filters = [ 'json_enabled', 'json_jsonp_enabled', 'rest_jsonp_enabled', 'rest_enabled' ];
foreach ( $filters as $filter ) {
if ( ! apply_filters( $filter, true ) ) {
return false;
}
}
return $this->is_rest_accessible();
}
/**
* @return bool
*/
private function is_rest_accessible() {
$value = $this->cacheInTransient(
function () {
return $this->pingRestEndpoint();
}
);
return self::DISABLED !== $value;
}
/**
* @param callable $callback
*
* @return mixed
*/
private function cacheInTransient( callable $callback ) {
$value = get_transient( self::PING_KEY );
if ( ! $value ) {
$value = $callback();
set_transient( self::PING_KEY, $value, self::CACHE_EXPIRATION_IN_SEC );
}
return $value;
}
/**
* @return string
*/
private function pingRestEndpoint() {
$url = get_rest_url( '/' );
$response = $this->wp_http->get(
$url,
[
'timeout' => 5,
'headers' => [
'X-WP-Nonce' => wp_create_nonce( 'wp_rest' ),
],
'cookies' => $this->getCookiesWithoutSessionId(),
]
);
if ( is_wp_error( $response ) ) {
return $this->isTimeout( $response ) ? self::TIMEOUT : self::DISABLED;
}
return isset( $response['response']['code'] ) && $response['response']['code'] === 200 ? self::ENABLED : self::DISABLED;
}
/**
* The PHP session ID causes the request to be blocked if some theme/plugin
* calls `session_start` (this always leads to hit the timeout).
*
* @return array
*/
private function getCookiesWithoutSessionId() {
return array_diff_key( $_COOKIE, [ 'PHPSESSID' => '' ] );
}
/**
* @param \WP_Error $response
*
* @return bool
*/
private function isTimeout( \WP_Error $response ) {
return strpos( $response->get_error_message(), 'cURL error 28: Operation timed out after' ) === 0;
}
}