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,28 @@
<?php
/**
* Class WPML_Cookie_Admin_Scripts
*/
class WPML_Cookie_Admin_Scripts {
public function enqueue() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
public function enqueue_scripts() {
wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wpml-cookie-ajax-setting', ICL_PLUGIN_URL . '/res/js/cookies/cookie-ajax-setting.js', array( 'jquery', 'wp-pointer' ), ICL_SITEPRESS_VERSION );
wp_localize_script(
'wpml-cookie-ajax-setting',
'wpml_cookie_setting',
array(
'nonce' => WPML_Cookie_Setting_Ajax::NONCE_COOKIE_SETTING,
'button_id' => WPML_Cookie_Admin_UI::BUTTON_ID,
'ajax_response_id' => WPML_Cookie_Setting_Ajax::AJAX_RESPONSE_ID,
'field_name' => WPML_Cookie_Setting::COOKIE_SETTING_FIELD,
'ajax_action' => WPML_Cookie_Setting_Ajax::ACTION,
)
);
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* Class WPML_Cookie_Admin_UI
*/
class WPML_Cookie_Admin_UI {
const BOX_TEMPLATE = 'admin-cookie-box.twig';
const BUTTON_ID = 'js-wpml-store-frontend-cookie';
/**
* @var WPML_Twig_Template
*/
private $template_service;
/**
* @var WPML_Cookie_Setting
*/
private $cookie_setting;
/**
* WPML_Cookie_Admin_UI constructor.
*
* @param WPML_Twig_Template $template_service
* @param WPML_Cookie_Setting $cookie_setting
*/
public function __construct( WPML_Twig_Template $template_service, WPML_Cookie_Setting $cookie_setting ) {
$this->template_service = $template_service;
$this->cookie_setting = $cookie_setting;
}
public function add_hooks() {
add_action( 'wpml_after_settings', array( $this, 'render_cookie_box' ) );
}
public function render_cookie_box() {
echo $this->template_service->show( $this->get_model(), self::BOX_TEMPLATE );
}
/**
* @return array
*/
private function get_model() {
return array(
'strings' => array(
'title' => __( 'Language filtering for AJAX operations', 'sitepress' ),
'field_name' => WPML_Cookie_Setting::COOKIE_SETTING_FIELD,
'field_label' => __( 'Store a language cookie to support language filtering for AJAX', 'sitepress' ),
'tooltip' => __( 'Select this option if your theme or plugins use AJAX operations on the front-end, that WPML needs to filter. WPML will set a cookie using JavaScript which will allow it to return the correct content for AJAX operations.', 'sitepress' ),
'button_text' => __( 'Save', 'sitepress' ),
'button_id' => self::BUTTON_ID,
),
'ajax_response_id' => WPML_Cookie_Setting_Ajax::AJAX_RESPONSE_ID,
'nonce_field' => WPML_Cookie_Setting_Ajax::NONCE_COOKIE_SETTING,
'nonce_value' => wp_create_nonce( WPML_Cookie_Setting_Ajax::NONCE_COOKIE_SETTING ),
'checked' => checked( $this->cookie_setting->get_setting(), true, false ),
);
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Class WPML_Cookie_Scripts
*/
class WPML_Cookie_Scripts {
/**
* @var string
*/
private $language_cookie_name;
/**
* @var string
*/
private $current_language;
/**
* WPML_Cookie_Scripts constructor.
*
* @param string $language_cookie_name
* @param string $current_language
*/
public function __construct( $language_cookie_name, $current_language ) {
$this->language_cookie_name = $language_cookie_name;
$this->current_language = $current_language;
}
public function add_hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), - PHP_INT_MAX );
}
public function enqueue_scripts() {
wp_enqueue_script( 'wpml-cookie', ICL_PLUGIN_URL . '/res/js/cookies/language-cookie.js', array( 'jquery' ), ICL_SITEPRESS_VERSION, false );
$cookies = array(
$this->language_cookie_name => array(
'value' => $this->current_language,
'expires' => 1,
'path' => '/',
),
);
wp_localize_script( 'wpml-cookie', 'wpml_cookies', $cookies );
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* Class WPML_Frontend_Cookie_Setting_Ajax
*/
class WPML_Cookie_Setting_Ajax {
const NONCE_COOKIE_SETTING = 'wpml-frontend-cookie-setting-nonce';
const AJAX_RESPONSE_ID = 'icl_ajx_response_cookie';
const ACTION = 'wpml_update_cookie_setting';
/**
* @var WPML_Cookie_Setting
*/
private $wpml_frontend_cookie_setting;
/**
* WPML_Frontend_Cookie_Setting_Ajax constructor.
*
* @param WPML_Cookie_Setting $wpml_frontend_cookie_setting
*/
public function __construct( WPML_Cookie_Setting $wpml_frontend_cookie_setting ) {
$this->wpml_frontend_cookie_setting = $wpml_frontend_cookie_setting;
}
public function add_hooks() {
add_action( 'wp_ajax_wpml_update_cookie_setting', array( $this, 'update_cookie_setting' ) );
}
public function update_cookie_setting() {
if ( ! $this->is_valid_request() ) {
wp_send_json_error();
} else {
if ( array_key_exists( WPML_Cookie_Setting::COOKIE_SETTING_FIELD, $_POST ) ) {
$store_frontend_cookie = filter_var( $_POST[ WPML_Cookie_Setting::COOKIE_SETTING_FIELD ], FILTER_SANITIZE_NUMBER_INT, FILTER_NULL_ON_FAILURE );
$this->wpml_frontend_cookie_setting->set_setting( $store_frontend_cookie );
} else {
$this->wpml_frontend_cookie_setting->set_setting( 0 );
}
wp_send_json_success();
}
}
/**
* @return bool
*/
private function is_valid_request() {
$valid_request = false;
if ( array_key_exists( 'nonce', $_POST ) ) {
$valid_request = wp_verify_nonce( $_POST['nonce'], self::NONCE_COOKIE_SETTING );
}
return $valid_request;
}
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* Class WPML_Frontend_Cookie_Setting
*/
class WPML_Cookie_Setting {
const COOKIE_SETTING_FIELD = 'store_frontend_cookie';
/**
* @var SitePress
*/
private $sitepress;
/**
* WPML_Frontend_Cookie_Setting constructor.
*
* @param SitePress $sitepress
*/
public function __construct( SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
/**
* @return bool|mixed
*/
public function get_setting() {
return $this->sitepress->get_setting( self::COOKIE_SETTING_FIELD );
}
/**
* @param mixed $value
*/
public function set_setting( $value ) {
$this->sitepress->set_setting( self::COOKIE_SETTING_FIELD, $value );
$this->sitepress->save_settings();
}
}

View File

@@ -0,0 +1,80 @@
<?php
class WPML_Cookie {
/**
* @param string $name
* @param string $value
* @param int $expires
* @param string $path
* @param string $domain
* @param bool $HTTPOnly
* @param string|null $sameSite
*/
public function set_cookie( $name, $value, $expires, $path, $domain, $HTTPOnly = false, $sameSite = null ) {
wp_cache_add_non_persistent_groups( __CLASS__ );
$entryHash = md5( wp_json_encode( [ $name, $value, $path, $domain, $HTTPOnly, $sameSite ] ) );
if ( wp_cache_get( $name, __CLASS__ ) !== $entryHash ) {
$this->handle_cache_plugins( $name );
if ($sameSite) {
header(
'Set-Cookie: ' . rawurlencode( $name ) . '=' . rawurlencode( $value )
. ( $domain ? '; Domain=' . $domain : '' )
. ( $expires ? '; expires=' . gmdate( 'D, d-M-Y H:i:s', $expires ) . ' GMT' : '' )
. ( $path ? '; Path=' . $path : '' )
. ( $this->is_secure_connection() ? '; Secure' : '')
. ( $HTTPOnly ? '; HttpOnly' : '' )
. '; SameSite=' . $sameSite,
false
);
} else {
setcookie( $name, (string) $value, $expires, $path, $domain, $this->is_secure_connection(), $HTTPOnly );
}
wp_cache_set( $name, $entryHash, __CLASS__ );
}
}
/**
* @param string $name
*
* @return string
*/
public function get_cookie( $name ) {
if ( isset( $_COOKIE[ $name ] ) ) {
return $_COOKIE[ $name ];
}
return '';
}
/**
* simple wrapper for \headers_sent
*
* @return bool
*/
public function headers_sent() {
return headers_sent();
}
/**
* @param string $name
*/
private function handle_cache_plugins( $name ) {
// @todo uncomment or delete when #wpmlcore-5796 is resolved
// do_action( 'wpsc_add_cookie', $name );
}
private function is_secure_connection() {
if (
\WPML\FP\Obj::prop( 'HTTPS', $_SERVER ) === 'on' ||
\WPML\FP\Obj::prop( 'HTTP_X_FORWARDED_PROTO', $_SERVER ) === 'https' ||
\WPML\FP\Obj::prop( 'HTTP_X_FORWARDED_SSL', $_SERVER ) === 'on'
) {
return true;
}
return false;
}
}