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,39 @@
<?php
class WPML_Config_Built_With_Page_Builders extends WPML_WP_Option implements IWPML_Action, IWPML_Backend_Action_Loader, IWPML_AJAX_Action_Loader {
const CONFIG_KEY = 'built-with-page-builder';
public function create() {
return $this; // Use same instance for action
}
public function get_key() {
return 'wpml_built_with_page_builder';
}
public function get_default() {
return array();
}
public function add_hooks() {
add_filter( 'wpml_config_array', array( $this, 'wpml_config_filter' ) );
}
/**
* @param array $config_data
*
* @return array
*/
public function wpml_config_filter( $config_data ) {
if ( isset( $config_data['wpml-config'][ self::CONFIG_KEY ] ) && $config_data['wpml-config'][ self::CONFIG_KEY ] ) {
$data_saved = $this->get();
$data_saved = $data_saved ? $data_saved : array();
$data_saved[] = $config_data['wpml-config'][ self::CONFIG_KEY ];
$this->set( array_unique( $data_saved ) );
}
return $config_data;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* Created by PhpStorm.
* User: bruce
* Date: 24/10/17
* Time: 11:02 AM
*/
use WPML\FP\Obj;
class WPML_Config_Display_As_Translated {
/**
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/wpmlcore-4859
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/wpmlcore-4941
*
* @param array $config
*
* @return array
*/
public static function merge_to_translate_mode( $config ) {
$config = self::merge_to_translate_mode_for_key( $config, 'custom-types', 'custom-type' );
$config = self::merge_to_translate_mode_for_key( $config, 'taxonomies', 'taxonomy' );
return $config;
}
private static function merge_to_translate_mode_for_key( $config, $key_plural, $key_singular ) {
foreach ( Obj::pathOr( [], [ 'wpml-config', $key_plural, $key_singular ], $config ) as $index => $settings ) {
if ( WPML_CONTENT_TYPE_TRANSLATE == Obj::path( [ 'attr', 'translate' ], $settings ) &&
1 == Obj::path( [ 'attr', 'display-as-translated' ], $settings )
) {
$settings['attr']['translate'] = WPML_CONTENT_TYPE_DISPLAY_AS_IF_TRANSLATED;
unset( $settings['attr']['display-as-translated'] );
$config['wpml-config'][ $key_plural ][ $key_singular ][ $index ] = $settings;
}
}
return $config;
}
}

View File

@@ -0,0 +1,36 @@
<?php
class WPML_Config_Shortcode_List extends WPML_WP_Option implements IWPML_Action, IWPML_Backend_Action_Loader, IWPML_AJAX_Action_Loader {
public function create() {
return $this; // Use same instance for action
}
public function get_key() {
return 'wpml_shortcode_list';
}
public function get_default() {
return array();
}
public function add_hooks() {
add_filter( 'wpml_config_array', array( $this, 'wpml_config_filter' ) );
add_filter( 'wpml_shortcode_list', array( $this, 'filter_shortcode_list' ) );
}
public function wpml_config_filter( $config_data ) {
$shortcode_data = array();
if ( isset( $config_data['wpml-config']['shortcode-list'] ) ) {
$shortcode_data = $config_data['wpml-config']['shortcode-list'];
}
$this->set( $shortcode_data );
return $config_data;
}
public function filter_shortcode_list( $shortcodes ) {
return array_unique( array_merge( $shortcodes, $this->get() ) );
}
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_Config_Update_Log implements WPML_Log {
const OPTION_NAME = 'wpml-xml-config-update-log';
public function get( $page_size = 0, $page = 0 ) {
$data = get_option( self::OPTION_NAME );
if ( ! $data ) {
$data = array();
}
return $this->paginate( $data, $page_size, $page );
}
/**
* @param string|int|float $timestamp
* @param array $entry
*/
public function insert( $timestamp, array $entry ) {
if ( $entry && is_array( $entry ) ) {
$log = $this->get();
if ( ! $log ) {
$log = array();
}
$log[ (string) $timestamp ] = $entry;
$this->save( $log );
}
}
public function clear() {
$this->save( array() );
}
public function save( array $data ) {
if ( $data === array() ) {
delete_option( self::OPTION_NAME );
return;
}
update_option( self::OPTION_NAME, $data, false );
}
public function is_empty() {
return ! $this->get();
}
/**
* @param array $data
* @param int $page_size
* @param int $page
*
* @return array
*/
protected function paginate( array $data, $page_size, $page ) {
if ( (int) $page_size > 0 ) {
$total = count( $data ); // total items in array
$limit = $page_size; // per page
$totalPages = ceil( $total / $limit ); // calculate total pages
$page = max( $page, 1 ); // get 1 page when$page <= 0
$page = min( $page, $totalPages ); // get last page when$page > $totalPages
$offset = ( $page - 1 ) * $limit;
if ( $offset < 0 ) {
$offset = 0;
}
$data = array_slice( $data, $offset, $limit );
}
return $data;
}
/**
* @return string
*/
public function get_log_url() {
return add_query_arg( array( 'page' => self::get_support_page_log_section() ), get_admin_url( null, 'admin.php#xml-config-log' ) );
}
/** @return string */
public static function get_support_page_log_section() {
return WPML_PLUGIN_FOLDER . '/menu/support.php';
}
}

View File

@@ -0,0 +1,442 @@
<?php
use WPML\Settings\PostType\Automatic;
class WPML_Config {
const PATH_TO_XSD = WPML_PLUGIN_PATH . '/res/xsd/wpml-config.xsd';
static $wpml_config_files = array();
static $active_plugins = array();
static function load_config() {
global $pagenow, $sitepress;
if ( ! is_admin() || wpml_is_ajax() || ( isset( $_POST['action'] ) && $_POST['action'] === 'heartbeat' ) || ! $sitepress || ! $sitepress->get_default_language() ) {
return;
}
$white_list_pages = array(
'theme_options',
'plugins.php',
'themes.php',
WPML_PLUGIN_FOLDER . '/menu/languages.php',
WPML_PLUGIN_FOLDER . '/menu/theme-localization.php',
WPML_PLUGIN_FOLDER . '/menu/translation-options.php',
);
if ( defined( 'WPML_ST_FOLDER' ) ) {
$white_list_pages[] = WPML_ST_FOLDER . '/menu/string-translation.php';
}
$white_list_pages = apply_filters( 'wpml_config_white_list_pages', $white_list_pages );
// Runs the load config process only on specific pages
$current_page = isset( $_GET['page'] ) ? $_GET['page'] : null;
if ( ( isset( $current_page ) && in_array( $current_page, $white_list_pages ) ) || ( isset( $pagenow ) && in_array( $pagenow, $white_list_pages ) ) ) {
self::load_config_run();
}
}
static function load_config_run() {
global $sitepress;
self::load_config_pre_process();
self::load_plugins_wpml_config();
self::load_theme_wpml_config();
self::parse_wpml_config_files();
self::load_config_post_process();
$sitepress->save_settings();
}
static function get_custom_fields_translation_settings( $translation_actions = array( 0 ) ) {
$iclTranslationManagement = wpml_load_core_tm();
$section = 'custom_fields_translation';
$result = array();
$tm_settings = $iclTranslationManagement->settings;
if ( isset( $tm_settings[ $section ] ) ) {
foreach ( $tm_settings[ $section ] as $meta_key => $translation_type ) {
if ( in_array( $translation_type, $translation_actions ) ) {
$result[] = $meta_key;
}
}
}
return $result;
}
static function parse_wpml_config_post_process( $config ) {
self::parse_custom_fields( $config );
self::parseTaxonomies( $config );
self::parsePostTypes( $config );
do_action( 'wpml_reset_ls_settings', $config['wpml-config']['language-switcher-settings'] );
return $config;
}
static function parseTaxonomies( $config ) {
self::parseTMSetting( 'taxonomy', 'taxonomies', $config );
}
static function parsePostTypes( $config ) {
self::parseTMSetting( 'custom-type', 'custom-types', $config );
Automatic::saveFromConfig( $config );
}
static function parseTMSetting( $singular, $plural, $config ) {
global $sitepress, $iclTranslationManagement;
$tm_settings = new WPML_TM_Settings_Update(
$singular,
$plural,
$iclTranslationManagement,
$sitepress,
wpml_load_settings_helper()
);
$tm_settings->update_from_config( $config['wpml-config'] );
}
static function load_config_post_process() {
global $iclTranslationManagement;
$post_process = new WPML_TM_Settings_Post_Process( $iclTranslationManagement );
$post_process->run();
}
static function load_config_pre_process() {
global $iclTranslationManagement;
$tm_settings = $iclTranslationManagement->settings;
if ( ( isset( $tm_settings['custom_types_readonly_config'] ) && is_array( $tm_settings['custom_types_readonly_config'] ) ) ) {
$iclTranslationManagement->settings['__custom_types_readonly_config_prev'] = $tm_settings['custom_types_readonly_config'];
} else {
$iclTranslationManagement->settings['__custom_types_readonly_config_prev'] = array();
}
$iclTranslationManagement->settings['custom_types_readonly_config'] = array();
if ( ( isset( $tm_settings['custom_fields_readonly_config'] ) && is_array( $tm_settings['custom_fields_readonly_config'] ) ) ) {
$iclTranslationManagement->settings['__custom_fields_readonly_config_prev'] = $tm_settings['custom_fields_readonly_config'];
} else {
$iclTranslationManagement->settings['__custom_fields_readonly_config_prev'] = array();
}
$iclTranslationManagement->settings['custom_fields_readonly_config'] = array();
if ( ( isset( $tm_settings['custom_term_fields_readonly_config'] ) && is_array( $tm_settings['custom_term_fields_readonly_config'] ) ) ) {
$iclTranslationManagement->settings['__custom_term_fields_readonly_config_prev'] = $tm_settings['custom_term_fields_readonly_config'];
} else {
$iclTranslationManagement->settings['__custom_term_fields_readonly_config_prev'] = array();
}
$iclTranslationManagement->settings['custom_term_fields_readonly_config'] = array();
}
static function load_plugins_wpml_config() {
if ( is_multisite() ) {
// Get multi site plugins
$plugins = get_site_option( 'active_sitewide_plugins' );
if ( ! empty( $plugins ) ) {
foreach ( $plugins as $p => $dummy ) {
if ( ! self::check_on_config_file( $p ) ) {
continue;
}
$plugin_slug = dirname( $p );
$config_file = WPML_PLUGINS_DIR . '/' . $plugin_slug . '/wpml-config.xml';
if ( trim( $plugin_slug, '\/.' ) && file_exists( $config_file ) ) {
self::$wpml_config_files[] = $config_file;
}
}
}
}
// Get single site or current blog active plugins
$plugins = get_option( 'active_plugins' );
if ( ! empty( $plugins ) ) {
foreach ( $plugins as $p ) {
if ( ! self::check_on_config_file( $p ) ) {
continue;
}
$plugin_slug = dirname( $p );
$config_file = WPML_PLUGINS_DIR . '/' . $plugin_slug . '/wpml-config.xml';
if ( trim( $plugin_slug, '\/.' ) && file_exists( $config_file ) ) {
self::$wpml_config_files[] = $config_file;
}
}
}
// Get the must-use plugins
$mu_plugins = wp_get_mu_plugins();
if ( ! empty( $mu_plugins ) ) {
foreach ( $mu_plugins as $mup ) {
if ( ! self::check_on_config_file( $mup ) ) {
continue;
}
$plugin_dir_name = dirname( $mup );
$plugin_base_name = basename( $mup, '.php' );
$plugin_sub_dir = $plugin_dir_name . '/' . $plugin_base_name;
if ( file_exists( $plugin_sub_dir . '/wpml-config.xml' ) ) {
$config_file = $plugin_sub_dir . '/wpml-config.xml';
self::$wpml_config_files[] = $config_file;
}
}
}
return self::$wpml_config_files;
}
static function check_on_config_file( $name ) {
if ( empty( self::$active_plugins ) ) {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
self::$active_plugins = get_plugins();
}
$config_index_file_data = maybe_unserialize( get_option( 'wpml_config_index' ) );
$config_files_arr = maybe_unserialize( get_option( 'wpml_config_files_arr' ) );
if ( ! $config_index_file_data || ! $config_files_arr ) {
return true;
}
if ( isset( self::$active_plugins[ $name ] ) ) {
$plugin_info = self::$active_plugins[ $name ];
$plugin_slug = dirname( $name );
$name = $plugin_info['Name'];
$config_data = $config_index_file_data->plugins;
$config_files_arr = $config_files_arr->plugins;
$config_file = WPML_PLUGINS_DIR . '/' . $plugin_slug . '/wpml-config.xml';
$type = 'plugin';
} else {
$config_data = $config_index_file_data->themes;
$config_files_arr = $config_files_arr->themes;
$config_file = get_template_directory() . '/wpml-config.xml';
$type = 'theme';
}
foreach ( $config_data as $item ) {
if ( $name == $item->name && isset( $config_files_arr[ $item->name ] ) ) {
if ( $item->override_local || ! file_exists( $config_file ) ) {
end( self::$wpml_config_files );
$key = key( self::$wpml_config_files ) + 1;
self::$wpml_config_files[ $key ] = new stdClass();
self::$wpml_config_files[ $key ]->config = icl_xml2array( $config_files_arr[ $item->name ] );
self::$wpml_config_files[ $key ]->type = $type;
self::$wpml_config_files[ $key ]->admin_text_context = basename( dirname( $config_file ) );
return false;
} else {
return true;
}
}
}
return true;
}
static function load_theme_wpml_config() {
$theme_data = wp_get_theme();
if ( ! self::check_on_config_file( $theme_data->get( 'Name' ) ) ) {
return self::$wpml_config_files;
}
$parent_theme = $theme_data->parent_theme;
if ( $parent_theme && ! self::check_on_config_file( $parent_theme ) ) {
return self::$wpml_config_files;
}
if ( get_template_directory() != get_stylesheet_directory() ) {
$config_file = get_stylesheet_directory() . '/wpml-config.xml';
if ( file_exists( $config_file ) ) {
self::$wpml_config_files[] = $config_file;
}
}
$config_file = get_template_directory() . '/wpml-config.xml';
if ( file_exists( $config_file ) ) {
self::$wpml_config_files[] = $config_file;
}
return self::$wpml_config_files;
}
static function get_theme_wpml_config_file() {
if ( get_template_directory() != get_stylesheet_directory() ) {
$config_file = get_stylesheet_directory() . '/wpml-config.xml';
if ( file_exists( $config_file ) ) {
return $config_file;
}
}
$config_file = get_template_directory() . '/wpml-config.xml';
if ( file_exists( $config_file ) ) {
return $config_file;
}
return false;
}
static function parse_wpml_config_files() {
$config_all['wpml-config'] = array(
'custom-fields' => array(),
'custom-fields-texts' => array(),
'custom-term-fields' => array(),
'custom-types' => array(),
'taxonomies' => array(),
'admin-texts' => array(),
'language-switcher-settings' => array(),
'shortcodes' => array(),
'shortcode-list' => array(),
'gutenberg-blocks' => array(),
'built-with-page-builder' => array(),
);
$config_all_updated = false;
$validate = new WPML_XML_Config_Validate(); // Validate with no XSD file (see wpmlcore-8444).
$transform = new WPML_XML2Array();
if ( ! empty( self::$wpml_config_files ) ) {
foreach ( self::$wpml_config_files as $file ) {
if ( is_object( $file ) ) {
$config = $file->config;
} else {
$xml_config_file = new WPML_XML_Config_Read_File( $file, $validate, $transform );
$config = $xml_config_file->get();
}
do_action( 'wpml_parse_config_file', $file );
$config_all = self::merge_with( $config_all, $config );
$config_all_updated = true;
}
}
$config_all = self::append_custom_xml_config( $config_all, $config_all_updated );
if ( $config_all_updated ) {
$config_all = apply_filters( 'icl_wpml_config_array', $config_all );
$config_all = apply_filters( 'wpml_config_array', $config_all );
}
$config_all = WPML_Config_Display_As_Translated::merge_to_translate_mode( $config_all );
self::parse_wpml_config_post_process( $config_all );
}
/**
* @param array<string,array<string,mixed>> $config_files
* @param bool|null $updated
*
* @return array
*/
private static function append_custom_xml_config( $config_files, &$updated = null ) {
$validate = new WPML_XML_Config_Validate( self::PATH_TO_XSD );
$transform = new WPML_XML2Array();
$custom_config = self::get_custom_xml_config( $validate, $transform );
if ( $custom_config ) {
$config_files = self::merge_with( $config_files, $custom_config );
$updated = true;
}
return $config_files;
}
/**
* @param \WPML_XML_Config_Validate $validate
* @param \WPML_XML_Transform $transform
*
* @return mixed
*/
private static function get_custom_xml_config( $validate, $transform ) {
if ( class_exists( 'WPML_Custom_XML' ) ) {
$custom_xml_option = new WPML_Custom_XML();
$custom_xml_config = new WPML_XML_Config_Read_Option( $custom_xml_option, $validate, $transform );
$custom_config = $custom_xml_config->get();
if ( $custom_config ) {
$config_object = (object) array(
'config' => $custom_config,
'type' => 'wpml-custom-xml',
'admin_text_context' => 'wpml-custom-xml',
);
do_action( 'wpml_parse_custom_config', $config_object );
return $custom_config;
}
}
return null;
}
/**
* @param array<string,array<string,mixed>> $all_configs
* @param array<string,array<string,mixed>> $config
*
* @return mixed
*/
private static function merge_with( $all_configs, $config ) {
if ( isset( $config['wpml-config'] ) ) {
$wpml_config = $config['wpml-config'];
$wpml_config_all = $all_configs['wpml-config'];
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'custom-field', 'custom-fields' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'custom-term-field', 'custom-term-fields' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'custom-type', 'custom-types' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'taxonomy', 'taxonomies' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'shortcode', 'shortcodes' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'gutenberg-block', 'gutenberg-blocks' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'key', 'custom-fields-texts' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'widget', 'elementor-widgets' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'widget', 'beaver-builder-widgets' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'widget', 'cornerstone-widgets' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'widget', 'siteorigin-widgets' );
// language-switcher-settings
if ( isset( $wpml_config['language-switcher-settings']['key'] ) ) {
if ( ! is_numeric( key( $wpml_config['language-switcher-settings']['key'] ) ) ) { // single
$wpml_config_all['language-switcher-settings']['key'][] = $wpml_config['language-switcher-settings']['key'];
} else {
foreach ( $wpml_config['language-switcher-settings']['key'] as $cf ) {
$wpml_config_all['language-switcher-settings']['key'][] = $cf;
}
}
}
if ( isset( $wpml_config['shortcode-list']['value'] ) ) {
$wpml_config_all['shortcode-list'] = array_merge( $wpml_config_all['shortcode-list'], explode( ',', $wpml_config['shortcode-list']['value'] ) );
}
if ( isset( $wpml_config['built-with-page-builder']['value'] ) ) {
$wpml_config_all['built-with-page-builder'] = $wpml_config['built-with-page-builder']['value'];
}
$all_configs['wpml-config'] = $wpml_config_all;
}
return $all_configs;
}
/**
* @param array<string,array<string,mixed>> $config
*/
protected static function parse_custom_fields( $config ) {
/** @var TranslationManagement $iclTranslationManagement */
global $iclTranslationManagement;
$setting_factory = $iclTranslationManagement->settings_factory();
$import = new WPML_Custom_Field_XML_Settings_Import( $setting_factory, $config['wpml-config'] );
$import->run();
}
private static function parse_config_index( $config_all, $wpml_config, $index_sing, $index_plur ) {
if ( isset( $wpml_config[ $index_plur ][ $index_sing ] ) ) {
if ( isset( $wpml_config[ $index_plur ][ $index_sing ]['value'] ) ) { // single
$config_all[ $index_plur ][ $index_sing ][] = $wpml_config[ $index_plur ][ $index_sing ];
} else {
foreach ( (array) $wpml_config[ $index_plur ][ $index_sing ] as $cf ) {
$config_all[ $index_plur ][ $index_sing ][] = $cf;
}
}
}
return $config_all;
}
}

View File

@@ -0,0 +1,15 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_Custom_XML extends WPML_WP_Option {
public function get_key() {
return 'wpml-tm-custom-xml';
}
public function get_default() {
return '';
}
}

View File

@@ -0,0 +1,72 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_XML_Config_Validate {
/**
* @var \LibXMLError[]
*/
private $errors = [];
private $path_to_xsd;
function __construct( $path_to_xsd = null ) {
$this->path_to_xsd = $path_to_xsd ? realpath( $path_to_xsd ) : null;
}
/**
* @return \LibXMLError[]
*/
public function get_errors() {
return $this->errors;
}
/**
* @param string $file_full_path
*
* @return bool
*/
function from_file( $file_full_path ) {
$this->errors = array();
$xml = file_get_contents( $file_full_path );
return $this->from_string( $xml );
}
/**
* @param string $xml
*
* @return bool
*/
function from_string( $xml ) {
if ( '' === preg_replace( '/(\W)+/', '', $xml ) ) {
return false;
}
$this->errors = array();
libxml_use_internal_errors( true );
$xml_object = $this->get_xml( $xml );
if ( $this->path_to_xsd && ! $xml_object->schemaValidate( $this->path_to_xsd ) ) {
$this->errors = libxml_get_errors();
}
libxml_clear_errors();
return count($this->errors) === 0;
}
/**
* @param string $content The string representation of the XML file
*
* @return DOMDocument
*/
private function get_xml( $content ) {
$xml = new DOMDocument();
$xml->loadXML( $content );
return $xml;
}
}

View File

@@ -0,0 +1,87 @@
<?php
use WPML\API\Sanitize;
/**
* @author OnTheGo Systems
*/
class WPML_XML_Config_Log_Notice {
const NOTICE_ERROR_GROUP = 'wpml-config-update';
const NOTICE_ERROR_ID = 'wpml-config-update-error';
/** @var WPML_Config_Update_Log */
private $log;
public function __construct( WPML_Log $log ) {
$this->log = $log;
}
public function add_hooks() {
if ( is_admin() ) {
add_action( 'wpml_loaded', array( $this, 'refresh_notices' ) );
}
}
public function refresh_notices() {
$notices = wpml_get_admin_notices();
if ( $this->log->is_empty() ) {
$notices->remove_notice( self::NOTICE_ERROR_GROUP, self::NOTICE_ERROR_ID );
return;
}
$text = '<p>' . esc_html__( 'WPML could not load configuration files, which your site needs.', 'sitepress' ) . '</p>';
$notice = $notices->create_notice( self::NOTICE_ERROR_ID, $text, self::NOTICE_ERROR_GROUP );
$notice->set_css_class_types( array( 'error' ) );
$log_url = add_query_arg( array( 'page' => WPML_Config_Update_Log::get_support_page_log_section() ), get_admin_url( null, 'admin.php#xml-config-log' ) );
$show_logs = $notices->get_new_notice_action( __( 'Detailed error log', 'sitepress' ), $log_url );
$return_url = null;
if ( $this->is_admin_user_action() ) {
$admin_uri = preg_replace( '#^/wp-admin/#', '', $_SERVER['SCRIPT_NAME'] );
$return_url = get_admin_url( null, $admin_uri );
$return_url_qs = $_GET;
unset( $return_url_qs[ self::NOTICE_ERROR_GROUP . '-action' ], $return_url_qs[ self::NOTICE_ERROR_GROUP . '-nonce' ] );
$return_url = add_query_arg( $return_url_qs, $return_url );
}
$retry_url = add_query_arg(
array(
self::NOTICE_ERROR_GROUP . '-action' => 'wpml_xml_update_refresh',
self::NOTICE_ERROR_GROUP . '-nonce' => wp_create_nonce( 'wpml_xml_update_refresh' ),
),
$return_url
);
$retry = $notices->get_new_notice_action( __( 'Retry', 'sitepress' ), $retry_url, false, false, true );
$notice->add_action( $show_logs );
$notice->add_action( $retry );
$notice->set_dismissible( true );
$notice->set_restrict_to_page_prefixes(
array(
'sitepress-multilingual-cms',
'wpml-translation-management',
'wpml-package-management',
'wpml-string-translation',
)
);
$notice->set_restrict_to_screen_ids( array( 'dashboard', 'plugins', 'themes' ) );
$notice->add_exclude_from_page( WPML_Config_Update_Log::get_support_page_log_section() );
$notices->add_notice( $notice );
}
/**
* @return bool
*/
private function is_admin_user_action() {
return is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )
&& ( 'heartbeat' !== Sanitize::stringProp( 'action', $_POST ) )
&& ( ! defined( 'DOING_CRON' ) || ! DOING_CRON );
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_XML_Config_Log_Factory {
private $log;
function create_log() {
if ( ! $this->log ) {
$this->log = new WPML_Config_Update_Log();
}
return $this->log;
}
function create_ui() {
$template_paths = array(
WPML_PLUGIN_PATH . '/templates/xml-config/log/',
);
$template_loader = new WPML_Twig_Template_Loader( $template_paths );
$template_service = $template_loader->get_template();
return new WPML_XML_Config_Log_UI( $this->create_log(), $template_service );
}
function create_notice() {
return new WPML_XML_Config_Log_Notice( $this->create_log() );
}
}

View File

@@ -0,0 +1,111 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_XML_Config_Log_UI {
/** @var IWPML_Template_Service */
private $template_service;
/**
* @var \WPML_Config_Update_Log
*/
private $log;
function __construct( WPML_Config_Update_Log $log, IWPML_Template_Service $template_service ) {
$this->log = $log;
$this->template_service = $template_service;
}
/**
* @return string
*/
public function show() {
$model = $this->get_model();
return $this->template_service->show( $model, 'main.twig' );
}
/** @return array */
private function get_model() {
$entries = $this->log->get();
krsort( $entries );
$table_data = array();
$columns = array();
foreach ( $entries as $entry ) {
$columns += array_keys( $entry );
}
$columns = array_unique( $columns );
foreach ( $entries as $timestamp => $entry ) {
$table_row = array( 'timestamp' => $this->getDateTimeFromMicroseconds( $timestamp ) );
foreach ( $columns as $column ) {
$table_row[ $column ] = null;
if ( array_key_exists( $column, $entry ) ) {
$table_row[ $column ] = $entry[ $column ];
}
}
$table_data[] = $table_row;
}
array_unshift( $columns, 'timestamp' );
$model = array(
'strings' => array(
'title' => __( 'Remote XML Config Log', 'sitepress' ),
'message' => __( "WPML needs to load configuration files, which tell it how to translate your theme and the plugins that you use. If there's a problem, use the Retry button. If the problem continues, contact WPML support, show the error details and we'll help you resolve it.", 'sitepress' ),
'details' => __( 'Details', 'sitepress' ),
'empty_log' => __( 'The remote XML Config Log is empty', 'sitepress' ),
),
'buttons' => array(
'retry' => array(
'label' => __( 'Retry', 'sitepress' ),
'url' => null,
'type' => 'primary',
),
'clear' => array(
'label' => __( 'Clear log', 'sitepress' ),
'url' => null,
'type' => 'secondary',
),
),
'columns' => $columns,
'entries' => $table_data,
);
if ( $table_data ) {
$clear_log_url = add_query_arg(
array(
WPML_XML_Config_Log_Notice::NOTICE_ERROR_GROUP . '-action' => 'wpml_xml_update_clear',
WPML_XML_Config_Log_Notice::NOTICE_ERROR_GROUP . '-nonce' => wp_create_nonce( 'wpml_xml_update_clear' ),
),
$this->log->get_log_url()
);
$model['buttons']['clear']['url'] = $clear_log_url;
$retry_url = add_query_arg(
array(
WPML_XML_Config_Log_Notice::NOTICE_ERROR_GROUP . '-action' => 'wpml_xml_update_refresh',
WPML_XML_Config_Log_Notice::NOTICE_ERROR_GROUP . '-nonce' => wp_create_nonce( 'wpml_xml_update_refresh' ),
),
$this->log->get_log_url()
);
$model['buttons']['retry']['url'] = $retry_url;
}
return $model;
}
private function getDateTimeFromMicroseconds( $time ) {
$dFormat = 'Y-m-d H:i:s';
if ( strpos( $time, '.' ) === false ) {
return $time;
}
list( $sec, $usec ) = explode( '.', $time ); // split the microtime on .
return date( $dFormat, $sec ) . $usec;
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_XML_Config_Read_File implements WPML_XML_Config_Read {
private $file_full_path;
private $transform;
private $validate;
function __construct( $file_full_path, WPML_XML_Config_Validate $validate, WPML_XML_Transform $transform ) {
$this->file_full_path = $file_full_path;
$this->validate = $validate;
$this->transform = $transform;
}
function get() {
if ( file_exists( $this->file_full_path ) && $this->validate->from_file( $this->file_full_path ) ) {
$xml = file_get_contents( $this->file_full_path );
return $this->transform->get( $xml );
}
return null;
}
}

View File

@@ -0,0 +1,35 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_XML_Config_Read_Option implements WPML_XML_Config_Read {
private $option;
private $transform;
private $validate;
/**
* WPML_XML_Config_Read_Option constructor.
*
* @param \WPML_WP_Option $option
* @param \WPML_XML_Config_Validate $validate
* @param \WPML_XML_Transform $transform
*/
function __construct( WPML_WP_Option $option, WPML_XML_Config_Validate $validate, WPML_XML_Transform $transform ) {
$this->option = $option;
$this->validate = $validate;
$this->transform = $transform;
}
function get() {
if ( $this->option->get() ) {
$content = $this->option->get();
if ( $this->validate->from_string( $content ) ) {
return $this->transform->get( $content );
}
}
return null;
}
}

View File

@@ -0,0 +1,7 @@
<?php
/**
* @author OnTheGo Systems
*/
interface WPML_XML_Config_Read {
}

View File

@@ -0,0 +1,99 @@
<?php
class WPML_XML2Array implements WPML_XML_Transform {
private $contents;
private $get_attributes;
public function get( $contents, $get_attributes = true ) {
$this->contents = $contents;
$this->get_attributes = (bool) $get_attributes;
$xml_values = array();
if ( $this->contents && function_exists( 'xml_parser_create' ) ) {
$parser = xml_parser_create();
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct( $parser, $this->contents, $xml_values );
xml_parser_free( $parser );
}
// Initializations
$xml_array = array();
$current = &$xml_array;
// Go through the tags.
foreach ( $xml_values as $data ) {
unset( $attributes, $value );// Remove existing values, or there will be trouble
$tag = $data['tag'];
$type = $data['type'];
$level = $data['level'];
$value = isset( $data['value'] ) ? $data['value'] : '';
$attributes = isset( $data['attributes'] ) ? $data['attributes'] : array();
$item = $this->get_item( $value, $attributes );
// See tag status and do the needed.
if ( 'open' === $type ) {// The starting of the tag '<tag>'
$parent[ $level - 1 ] = &$current;
if ( ! is_array( $current ) || ( ! isset( $current[ $tag ] ) ) ) { // Insert New tag
$current[ $tag ] = $item;
$current = &$current[ $tag ];
} else { // There was another element with the same tag name
if ( isset( $current[ $tag ][0] ) ) {
$current[ $tag ][] = $item;
} else {
$current[ $tag ] = array( $current[ $tag ], $item );
}
$last = count( $current[ $tag ] ) - 1;
$current = &$current[ $tag ][ $last ];
}
} elseif ( 'complete' === $type ) { // Tags that ends in 1 line '<tag />'
// See if the key is already taken.
if ( ! isset( $current[ $tag ] ) ) { // New Key
$current[ $tag ] = $item;
} else { // If taken, put all things inside a list(array)
if ( ( is_array( $current[ $tag ] ) && ! $this->get_attributes )// If it is already an array...
|| ( isset( $current[ $tag ][0] ) && is_array( $current[ $tag ][0] ) && $this->get_attributes )
) {
$current[ $tag ][] = $item;
} else { // If it is not an array...
$current[ $tag ] = array( $current[ $tag ], $item );
}
}
} elseif ( 'close' === $type && isset( $parent ) ) { // End of tag '</tag>'
$current = &$parent[ $level - 1 ];
}
}
return $xml_array;
}
/**
* @param mixed|null $value
* @param array $attributes
*
* @return array
*/
private function get_item( $value, array $attributes ) {
$item = array();
if ( $this->get_attributes ) {// The second argument of the function decides this.
if ( null !== $value ) {
$item['value'] = $value;
}
if ( null !== $attributes ) {
foreach ( $attributes as $attr => $val ) {
$item['attr'][ $attr ] = $val;
}
}
} else {
$item = $value;
}
return $item;
}
}

View File

@@ -0,0 +1,8 @@
<?php
/**
* @author OnTheGo Systems
*/
interface WPML_XML_Transform {
public function get( $source, $get_attributes = true );
}