first commit
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\Settings;
|
||||
|
||||
|
||||
class Repository {
|
||||
|
||||
public static function getSetting( $indexes ) {
|
||||
$settings = self::getAllSettings();
|
||||
|
||||
/**
|
||||
* I do not know why the foreach loop looks like that. I have just copied it from WPML_Translation_Job_Helper
|
||||
* @todo Review it later and try simplify if possible
|
||||
*/
|
||||
foreach ( $indexes as $index ) {
|
||||
$settings = isset( $settings[ $index ] ) ? $settings[ $index ] : null;
|
||||
if ( ! isset( $settings ) ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
public static function getCustomFieldsToTranslate() {
|
||||
return array_values( array_filter( array_keys(
|
||||
self::getSetting( [ 'custom_fields_translation' ] ) ?: [],
|
||||
WPML_TRANSLATE_CUSTOM_FIELD
|
||||
) ) );
|
||||
}
|
||||
|
||||
public static function getCustomFields() {
|
||||
return \wpml_collect( self::getSetting( [ 'custom_fields_translation' ] ) ?: [] )
|
||||
->filter( function ( $value, $key ) {
|
||||
return (bool) $key;
|
||||
} )->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private static function getAllSettings() {
|
||||
/** @var \TranslationManagement $iclTranslationManagement */
|
||||
global $iclTranslationManagement;
|
||||
|
||||
if ( ! $iclTranslationManagement ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ( empty( $iclTranslationManagement->settings ) ) {
|
||||
$iclTranslationManagement->init();
|
||||
}
|
||||
|
||||
return $iclTranslationManagement->get_settings();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Serialized_Custom_Field_Package_Handler_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
|
||||
$translation_management = wpml_load_core_tm();
|
||||
return new WPML_TM_Serialized_Custom_Field_Package_Handler(
|
||||
new WPML_Custom_Field_Setting_Factory( $translation_management )
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Serialized_Custom_Field_Package_Handler {
|
||||
|
||||
/** @var WPML_Custom_Field_Setting_Factory $custom_field_setting_factory */
|
||||
private $custom_field_setting_factory;
|
||||
|
||||
public function __construct( WPML_Custom_Field_Setting_Factory $custom_field_setting_factory ) {
|
||||
$this->custom_field_setting_factory = $custom_field_setting_factory;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_filter(
|
||||
'wpml_translation_job_post_meta_value_translated',
|
||||
array(
|
||||
$this,
|
||||
'translate_only_whitelisted_attributes',
|
||||
),
|
||||
10,
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $translated
|
||||
* @param string $custom_field_job_type - e.g: field-my_custom_field-0-my_attribute.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function translate_only_whitelisted_attributes( $translated, $custom_field_job_type ) {
|
||||
if ( $translated ) {
|
||||
list( $custom_field, $attributes ) = WPML_TM_Field_Type_Encoding::decode( $custom_field_job_type );
|
||||
if ( $custom_field && $attributes ) {
|
||||
$settings = $this->custom_field_setting_factory->post_meta_setting( $custom_field );
|
||||
$attributes_whitelist = $settings->get_attributes_whitelist();
|
||||
|
||||
if ( $attributes_whitelist ) {
|
||||
$translated = $this->match_in_order( $attributes, $attributes_whitelist ) ? $translated : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $translated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches the attributes array to the whitelist array
|
||||
* The whitelist array has the attribute as the key to another array for sub keys
|
||||
* eg. array( 'attribute1' => array( 'subkey1' => array() ) )
|
||||
*
|
||||
* @param array $attributes - The attributes in the custom field.
|
||||
* @param array $whitelist - The whitelist attributes to match against.
|
||||
* @param int $current_depth - The current depth in the attributes array.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function match_in_order( $attributes, $whitelist, $current_depth = 0 ) {
|
||||
$current_attribute = $attributes[ $current_depth ];
|
||||
$wildcard_match = $this->match_with_wildcards( $current_attribute, array_keys( $whitelist ) );
|
||||
if ( $wildcard_match ) {
|
||||
if ( count( $attributes ) === $current_depth + 1 ) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->match_in_order( $attributes, $whitelist[ $wildcard_match ], $current_depth + 1 );
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches the attribute to the whitelist array using wildcards.
|
||||
* Wildcards can only be used at the end of the string.
|
||||
* eg. 'title-*', 'data*', '*'
|
||||
* A '*' matches everything.
|
||||
*
|
||||
* @param string $attribute - the current attributes.
|
||||
* @param array $whitelist - the whitelist to match against.
|
||||
*
|
||||
* @return string - Returns the whitelist string match.
|
||||
*/
|
||||
private function match_with_wildcards( $attribute, $whitelist ) {
|
||||
foreach ( $whitelist as $white_value ) {
|
||||
$asterisk_pos = strpos( $white_value, '*' );
|
||||
if ( false === $asterisk_pos ) {
|
||||
if ( $attribute === $white_value ) {
|
||||
return $white_value;
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
0 === $asterisk_pos ||
|
||||
substr( $attribute, 0, $asterisk_pos ) === substr( $white_value, 0, $asterisk_pos )
|
||||
) {
|
||||
return $white_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Default_Settings_Factory implements IWPML_Frontend_Action_Loader, IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
/** @var TranslationManagement */
|
||||
global $iclTranslationManagement;
|
||||
|
||||
return new WPML_TM_Default_Settings( $iclTranslationManagement );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Default_Settings implements IWPML_Action {
|
||||
|
||||
/** @var TranslationManagement */
|
||||
private $tm;
|
||||
|
||||
public function __construct( TranslationManagement $tm ) {
|
||||
$this->tm = $tm;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'init', array( $this, 'init_action' ), $this->tm->get_init_priority() );
|
||||
}
|
||||
|
||||
public function init_action() {
|
||||
$this->maybe_update_notification( 'new-job', WPML_TM_Emails_Settings::NOTIFY_IMMEDIATELY );
|
||||
$this->maybe_update_notification( 'include_xliff', (int) apply_filters( 'wpml_setting', 0, 'include_xliff_in_notification' ) );
|
||||
|
||||
if ( ! $this->has_notification( WPML_TM_Emails_Settings::COMPLETED_JOB_FREQUENCY ) ) {
|
||||
if ( $this->has_notification( 'completed' ) ) {
|
||||
$this->update_notification( WPML_TM_Emails_Settings::COMPLETED_JOB_FREQUENCY, $this->get_notification( 'completed' ) );
|
||||
} else {
|
||||
$this->update_notification( WPML_TM_Emails_Settings::COMPLETED_JOB_FREQUENCY, WPML_TM_Emails_Settings::NOTIFY_WEEKLY );
|
||||
}
|
||||
}
|
||||
|
||||
$this->maybe_update_notification( 'completed', WPML_TM_Emails_Settings::NOTIFY_IMMEDIATELY );
|
||||
$this->maybe_update_notification( 'resigned', WPML_TM_Emails_Settings::NOTIFY_IMMEDIATELY );
|
||||
$this->maybe_update_notification( 'overdue', WPML_TM_Emails_Settings::NOTIFY_IMMEDIATELY );
|
||||
$this->maybe_update_notification( 'overdue_offset', 7 );
|
||||
$this->maybe_update_notification( 'dashboard', true );
|
||||
$this->maybe_update_notification( 'purge-old', 7 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function get_notification( $key, $default = null ) {
|
||||
return isset( $this->tm->settings['notification'][ $key ] )
|
||||
? $this->tm->settings['notification'][ $key ]
|
||||
: $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function has_notification( $key ) {
|
||||
return isset( $this->tm->settings['notification'][ $key ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
private function maybe_update_notification( $key, $value ) {
|
||||
if ( ! $this->has_notification( $key ) ) {
|
||||
$this->update_notification( $key, $value );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
private function update_notification( $key, $value ) {
|
||||
$this->tm->settings['notification'][ $key ] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user