first commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_Admin_Blog_Option extends WPML_SP_User {
|
||||
|
||||
/** @var WPML_ST_Admin_Option_Translation $admin_option */
|
||||
private $admin_option;
|
||||
|
||||
/**
|
||||
* WPML_ST_Admin_Blog_Option constructor.
|
||||
*
|
||||
* @param SitePress $sitepress
|
||||
* @param WPML_String_Translation $st_instance
|
||||
* @param string $option_name
|
||||
*/
|
||||
public function __construct(
|
||||
&$sitepress,
|
||||
&$st_instance,
|
||||
$option_name
|
||||
) {
|
||||
if ( ! WPML_ST_Blog_Name_And_Description_Hooks::is_string( $option_name ) ) {
|
||||
throw new InvalidArgumentException( $option_name . ' Is not a valid blog option that is handled by this class, allowed values are "Tagline" and "Blog Title"' );
|
||||
}
|
||||
parent::__construct( $sitepress );
|
||||
$this->admin_option = $st_instance->get_admin_option( $option_name );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $old_value
|
||||
* @param string $new_value
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function pre_update_filter(
|
||||
$old_value,
|
||||
$new_value
|
||||
) {
|
||||
$wp_api = $this->sitepress->get_wp_api();
|
||||
if ( $wp_api->is_multisite() && $wp_api->ms_is_switched() && ! $this->sitepress->get_setting( 'setup_complete' ) ) {
|
||||
throw new RuntimeException( 'You cannot update blog option translations while switched to a blog on which the WPML setup is not complete! You are currently using blog ID:' . $this->sitepress->get_wp_api()->get_current_blog_id() );
|
||||
}
|
||||
WPML_Config::load_config_run();
|
||||
|
||||
return $this->admin_option->update_option(
|
||||
'',
|
||||
$new_value,
|
||||
ICL_TM_COMPLETE
|
||||
) ? $old_value : $new_value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_Admin_Option_Translation extends WPML_SP_User {
|
||||
|
||||
/** @var WPML_String_Translation $st_instance */
|
||||
private $st_instance;
|
||||
/** @var string $option_name */
|
||||
private $option_name;
|
||||
/** @var string $option_name */
|
||||
private $language;
|
||||
|
||||
/**
|
||||
* WPML_ST_Admin_Option constructor.
|
||||
*
|
||||
* @param SitePress $sitepress
|
||||
* @param WPML_String_Translation $st_instance
|
||||
* @param string $option_name
|
||||
* @param string $language
|
||||
*/
|
||||
public function __construct(
|
||||
&$sitepress,
|
||||
&$st_instance,
|
||||
$option_name,
|
||||
$language = ''
|
||||
) {
|
||||
if ( ! $option_name || ! is_scalar( $option_name ) ) {
|
||||
throw new InvalidArgumentException( 'Not a valid option name, received: ' . serialize( $option_name ) );
|
||||
}
|
||||
|
||||
parent::__construct( $sitepress );
|
||||
$this->st_instance = &$st_instance;
|
||||
$this->option_name = $option_name;
|
||||
$this->language = $language ? $language : $this->st_instance->get_current_string_language( $option_name );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $option_name
|
||||
* @param string $new_value
|
||||
* @param int|bool $status
|
||||
* @param int $translator_id
|
||||
* @param int $rec_level
|
||||
*
|
||||
* @return boolean|mixed
|
||||
*/
|
||||
public function update_option(
|
||||
$option_name = '',
|
||||
$new_value = null,
|
||||
$status = false,
|
||||
$translator_id = null,
|
||||
$rec_level = 0
|
||||
) {
|
||||
$option_name = $option_name ? $option_name : $this->option_name;
|
||||
$new_value = (array) $new_value;
|
||||
$updated = array();
|
||||
|
||||
foreach ( $new_value as $index => $value ) {
|
||||
if ( is_array( $value ) ) {
|
||||
$name = '[' . $option_name . '][' . $index . ']';
|
||||
$result = $this->update_option(
|
||||
$name,
|
||||
$value,
|
||||
$status,
|
||||
$translator_id,
|
||||
$rec_level + 1
|
||||
);
|
||||
$updated[] = array_sum( explode( ',', $result ) );
|
||||
} else {
|
||||
if ( is_string( $index ) ) {
|
||||
$name = ( $rec_level == 0 ? '[' . $option_name . ']' : $option_name ) . $index;
|
||||
} else {
|
||||
$name = $option_name;
|
||||
}
|
||||
$string = $this->st_instance->string_factory()->find_admin_by_name( $name );
|
||||
$string_id = $string->string_id();
|
||||
if ( $string_id ) {
|
||||
if ( $this->language !== $string->get_language() ) {
|
||||
$updated[] = $string->set_translation(
|
||||
$this->language,
|
||||
$value,
|
||||
$status,
|
||||
$translator_id
|
||||
);
|
||||
} else {
|
||||
$string->update_value( $value );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_sum( $updated ) > 0 ? join( ',', $updated ) : false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user