first commit
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Batch_Url_Translation
|
||||
*/
|
||||
abstract class WPML_Media_Batch_Url_Translation {
|
||||
|
||||
const BATCH_SIZE = 10;
|
||||
|
||||
const BATCH_SIZE_FACTOR_ALL_MEDIA = 1;
|
||||
const BATCH_SIZE_FACTOR_SPECIFIC_MEDIA = 10;
|
||||
|
||||
/**
|
||||
* @var wpdb
|
||||
*/
|
||||
protected $wpdb;
|
||||
|
||||
/**
|
||||
* WPML_Media_Batch_Url_Translation constructor.
|
||||
*
|
||||
* @param wpdb $wpdb
|
||||
*/
|
||||
public function __construct( wpdb $wpdb ) {
|
||||
$this->wpdb = $wpdb;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'wp_ajax_' . $this->get_ajax_action(), array( $this, 'run_batch' ) );
|
||||
}
|
||||
|
||||
public function run_batch() {
|
||||
$offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0;
|
||||
$attachment_id = isset( $_POST['attachment_id'] ) ? (int) $_POST['attachment_id'] : 0;
|
||||
$all_media = ! empty( $_POST['global'] );
|
||||
|
||||
if ( $all_media ) {
|
||||
$number_of_elements_left = $this->process_batch( $offset );
|
||||
} else {
|
||||
$number_of_elements_left = $this->process_batch_for_selected_media( $offset, $attachment_id );
|
||||
}
|
||||
$batch_size_factor = $all_media ? self::BATCH_SIZE_FACTOR_ALL_MEDIA : self::BATCH_SIZE_FACTOR_SPECIFIC_MEDIA;
|
||||
$response = array(
|
||||
'offset' => $offset + $this->get_batch_size( $batch_size_factor ),
|
||||
'continue' => (int) ( $number_of_elements_left > 0 ),
|
||||
'message' => $this->get_response_message( $number_of_elements_left ),
|
||||
);
|
||||
|
||||
wp_send_json_success( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number_of_elements_left
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function get_response_message( $number_of_elements_left );
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract protected function process_batch( $offset );
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param int $attachment_id
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract protected function process_batch_for_selected_media( $offset, $attachment_id );
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function get_ajax_error_message();
|
||||
|
||||
/**
|
||||
* @param int $batch_size_factor
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function get_batch_size( $batch_size_factor = self::BATCH_SIZE_FACTOR_ALL_MEDIA ) {
|
||||
return $batch_size_factor * self::BATCH_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function get_ajax_action();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Custom_Field_Batch_Url_Translation_Factory
|
||||
*/
|
||||
class WPML_Media_Custom_Field_Batch_Url_Translation_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
global $wpdb, $sitepress;
|
||||
|
||||
if ( WPML_Media_Custom_Field_Batch_Url_Translation::is_ajax_request() ) {
|
||||
|
||||
$translatable_custom_fields = $sitepress->get_custom_fields_translation_settings(
|
||||
$sitepress->get_wp_api()->constant( 'WPML_TRANSLATE_CUSTOM_FIELD' )
|
||||
);
|
||||
|
||||
$custom_field_images_translation_factory = new WPML_Media_Custom_Field_Images_Translation_Factory();
|
||||
|
||||
return new WPML_Media_Custom_Field_Batch_Url_Translation(
|
||||
$custom_field_images_translation_factory->create(),
|
||||
$wpdb,
|
||||
$translatable_custom_fields
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Custom_Field_Batch_Url_Translation
|
||||
*/
|
||||
class WPML_Media_Custom_Field_Batch_Url_Translation extends WPML_Media_Batch_Url_Translation implements IWPML_Action {
|
||||
|
||||
const AJAX_ACTION = 'wpml_media_translate_media_url_in_custom_fields';
|
||||
|
||||
/**
|
||||
* @var WPML_Media_Custom_Field_Images_Translation
|
||||
*/
|
||||
private $custom_field_translation;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $translatable_custom_fields;
|
||||
|
||||
/**
|
||||
* WPML_Media_Custom_Field_Batch_Url_Translation constructor.
|
||||
*
|
||||
* @param WPML_Media_Custom_Field_Images_Translation $custom_field_translation
|
||||
* @param wpdb $wpdb
|
||||
* @param array $translatable_custom_fields
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_Media_Custom_Field_Images_Translation $custom_field_translation,
|
||||
wpdb $wpdb,
|
||||
array $translatable_custom_fields
|
||||
) {
|
||||
parent::__construct( $wpdb );
|
||||
$this->custom_field_translation = $custom_field_translation;
|
||||
$this->translatable_custom_fields = $translatable_custom_fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function get_ajax_action() {
|
||||
return self::AJAX_ACTION;
|
||||
}
|
||||
|
||||
public static function is_ajax_request() {
|
||||
return isset( $_POST['action'] ) && self::AJAX_ACTION === $_POST['action'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number_of_custom_fields_left
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_response_message( $number_of_custom_fields_left ) {
|
||||
return sprintf(
|
||||
__( 'Translating media urls in custom field translations: %s', 'wpml-media' ),
|
||||
$number_of_custom_fields_left > 0 ?
|
||||
sprintf( __( '%d left', 'wpml-media' ), $number_of_custom_fields_left ) :
|
||||
__( 'done!', 'wpml-media' )
|
||||
);
|
||||
}
|
||||
|
||||
protected function get_ajax_error_message() {
|
||||
return array(
|
||||
'key' => 'wpml_media_batch_urls_update_error_custom_fields',
|
||||
'value' => esc_js( __( 'Translating media urls in custom fields translations failed: Please try again (%s)', 'wpml-media' ) ),
|
||||
);
|
||||
}
|
||||
|
||||
protected function process_batch( $offset ) {
|
||||
|
||||
if ( $this->translatable_custom_fields ) {
|
||||
$translatable_custom_fields_where_in = wpml_prepare_in( $this->translatable_custom_fields );
|
||||
$custom_fields = $this->wpdb->get_results(
|
||||
"
|
||||
SELECT SQL_CALC_FOUND_ROWS t.element_id AS post_id, p.meta_id, p.meta_key, p.meta_value
|
||||
FROM {$this->wpdb->prefix}icl_translations t
|
||||
JOIN {$this->wpdb->prefix}postmeta p ON t.element_id = p.post_id
|
||||
WHERE t.element_type LIKE 'post_%'
|
||||
AND t.element_type <> 'post_attachment'
|
||||
AND t.source_language_code IS NULL
|
||||
AND p.meta_key IN ({$translatable_custom_fields_where_in})
|
||||
ORDER BY t.element_id ASC
|
||||
LIMIT {$offset}, " . self::BATCH_SIZE
|
||||
);
|
||||
|
||||
$number_of_all_custom_fields = (int) $this->wpdb->get_var( 'SELECT FOUND_ROWS()' );
|
||||
|
||||
foreach ( $custom_fields as $custom_field ) {
|
||||
$this->custom_field_translation->translate_images(
|
||||
$custom_field->meta_id,
|
||||
$custom_field->post_id,
|
||||
$custom_field->meta_key,
|
||||
$custom_field->meta_value
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$number_of_all_custom_fields = 0;
|
||||
}
|
||||
|
||||
return $number_of_all_custom_fields - $offset - self::BATCH_SIZE;
|
||||
}
|
||||
|
||||
protected function process_batch_for_selected_media( $offset, $attachment_id ) {
|
||||
$media_url = wpml_like_escape( wp_get_attachment_url( $attachment_id ) );
|
||||
if ( ! $media_url ) {
|
||||
return 0;
|
||||
}
|
||||
preg_match( '/(.+)\.([a-z]+)$/', $media_url, $match );
|
||||
$media_url_no_extension = wpml_like_escape( $match[1] );
|
||||
$extension = wpml_like_escape( $match[2] );
|
||||
|
||||
$batch_size = $this->get_batch_size( parent::BATCH_SIZE_FACTOR_SPECIFIC_MEDIA );
|
||||
if ( $this->translatable_custom_fields ) {
|
||||
$translatable_custom_fields_where_in = wpml_prepare_in( $this->translatable_custom_fields );
|
||||
$custom_fields = $this->wpdb->get_results(
|
||||
"
|
||||
SELECT SQL_CALC_FOUND_ROWS t.element_id AS post_id, p.meta_id, p.meta_key, p.meta_value
|
||||
FROM {$this->wpdb->prefix}icl_translations t
|
||||
JOIN {$this->wpdb->prefix}postmeta p ON t.element_id = p.post_id
|
||||
WHERE t.element_type LIKE 'post_%'
|
||||
AND t.element_type <> 'post_attachment'
|
||||
AND t.source_language_code IS NULL
|
||||
AND p.meta_key IN ({$translatable_custom_fields_where_in})
|
||||
AND (
|
||||
p.meta_value LIKE '%{$media_url}%' OR
|
||||
p.meta_value LIKE '%{$media_url_no_extension}-%x%.{$extension}%'
|
||||
)
|
||||
ORDER BY t.element_id ASC
|
||||
LIMIT {$offset}, " . $batch_size
|
||||
);
|
||||
|
||||
$number_of_all_custom_fields = (int) $this->wpdb->get_var( 'SELECT FOUND_ROWS()' );
|
||||
|
||||
foreach ( $custom_fields as $custom_field ) {
|
||||
$this->custom_field_translation->translate_images(
|
||||
$custom_field->meta_id,
|
||||
$custom_field->post_id,
|
||||
$custom_field->meta_key,
|
||||
$custom_field->meta_value
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$number_of_all_custom_fields = 0;
|
||||
}
|
||||
|
||||
return $number_of_all_custom_fields - $offset - $batch_size;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Post_Batch_Url_Translation_Factory
|
||||
*/
|
||||
class WPML_Media_Post_Batch_Url_Translation_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
global $wpdb;
|
||||
|
||||
if ( WPML_Media_Post_Batch_Url_Translation::is_ajax_request() ) {
|
||||
$post_images_translation_factory = new WPML_Media_Post_Images_Translation_Factory();
|
||||
|
||||
return new WPML_Media_Post_Batch_Url_Translation( $post_images_translation_factory->create(), $wpdb );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Post_Batch_Url_Translation
|
||||
*/
|
||||
class WPML_Media_Post_Batch_Url_Translation extends WPML_Media_Batch_Url_Translation implements IWPML_Action {
|
||||
|
||||
const AJAX_ACTION = 'wpml_media_translate_media_url_in_posts';
|
||||
|
||||
/**
|
||||
* @var WPML_Media_Post_Images_Translation
|
||||
*/
|
||||
private $post_image_translation;
|
||||
|
||||
/**
|
||||
* WPML_Media_Post_Batch_Url_Translation constructor.
|
||||
*
|
||||
* @param WPML_Media_Post_Images_Translation $post_image_translation
|
||||
* @param wpdb $wpdb
|
||||
*/
|
||||
public function __construct( WPML_Media_Post_Images_Translation $post_image_translation, wpdb $wpdb ) {
|
||||
parent::__construct( $wpdb );
|
||||
$this->post_image_translation = $post_image_translation;
|
||||
$this->wpdb = $wpdb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function get_ajax_action() {
|
||||
return self::AJAX_ACTION;
|
||||
}
|
||||
|
||||
public static function is_ajax_request() {
|
||||
return isset( $_POST['action'] ) && self::AJAX_ACTION === $_POST['action'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number_of_posts_left
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_response_message( $number_of_posts_left ) {
|
||||
return sprintf(
|
||||
__( 'Translating media urls in post translations: %s', 'wpml-media' ),
|
||||
$number_of_posts_left > 0 ?
|
||||
sprintf( __( '%d left', 'wpml-media' ), $number_of_posts_left ) :
|
||||
__( 'done!', 'wpml-media' )
|
||||
);
|
||||
}
|
||||
|
||||
protected function get_ajax_error_message() {
|
||||
return array(
|
||||
'key' => 'wpml_media_batch_urls_update_errors_posts',
|
||||
'value' => esc_js( __( 'Translating media urls in posts translations failed: Please try again (%s)', 'wpml-media' ) )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function process_batch( $offset ) {
|
||||
|
||||
$posts = $this->wpdb->get_col( "
|
||||
SELECT SQL_CALC_FOUND_ROWS element_id AS id
|
||||
FROM {$this->wpdb->prefix}icl_translations
|
||||
WHERE element_type LIKE 'post_%'
|
||||
AND element_type <> 'post_attachment'
|
||||
AND source_language_code IS NULL
|
||||
ORDER BY element_id ASC
|
||||
LIMIT {$offset}, " . self::BATCH_SIZE );
|
||||
|
||||
$number_of_all_posts = (int) $this->wpdb->get_var( "SELECT FOUND_ROWS()" );
|
||||
|
||||
foreach ( $posts as $post_id ) {
|
||||
$this->post_image_translation->translate_images( $post_id );
|
||||
}
|
||||
|
||||
return $number_of_all_posts - $offset - self::BATCH_SIZE;
|
||||
}
|
||||
|
||||
protected function process_batch_for_selected_media( $offset, $attachment_id ) {
|
||||
$media_url = wpml_like_escape( wp_get_attachment_url( $attachment_id ) );
|
||||
if ( ! $media_url ) {
|
||||
return 0;
|
||||
}
|
||||
preg_match( "/(.+)\.([a-z]+)$/", $media_url, $match );
|
||||
$media_url_no_extension = wpml_like_escape( $match[1] );
|
||||
$extension = wpml_like_escape( $match[2] );
|
||||
|
||||
$batch_size = $this->get_batch_size( parent::BATCH_SIZE_FACTOR_SPECIFIC_MEDIA );
|
||||
|
||||
$posts = $this->wpdb->get_col( "
|
||||
SELECT SQL_CALC_FOUND_ROWS element_id AS id
|
||||
FROM {$this->wpdb->prefix}icl_translations t
|
||||
JOIN {$this->wpdb->posts} p ON t.element_id = p.ID
|
||||
WHERE element_type LIKE 'post_%'
|
||||
AND element_type <> 'post_attachment'
|
||||
AND source_language_code IS NULL
|
||||
AND (
|
||||
post_content LIKE '%{$media_url}%' OR
|
||||
post_content LIKE '%{$media_url_no_extension}-%x%.{$extension}%'
|
||||
)
|
||||
ORDER BY element_id ASC
|
||||
LIMIT {$offset}, " . $batch_size );
|
||||
|
||||
$number_of_all_posts = (int) $this->wpdb->get_var( "SELECT FOUND_ROWS()" );
|
||||
|
||||
foreach ( $posts as $post_id ) {
|
||||
$this->post_image_translation->translate_images( $post_id );
|
||||
}
|
||||
|
||||
return $number_of_all_posts - $offset - $batch_size;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_String_Batch_Url_Translation
|
||||
*/
|
||||
class WPML_Media_String_Batch_Url_Translation_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
global $wpdb;
|
||||
|
||||
if ( WPML_Media_String_Batch_Url_Translation::is_ajax_request() ) {
|
||||
$string_factory = new WPML_ST_String_Factory( $wpdb );
|
||||
|
||||
return new WPML_Media_String_Batch_Url_Translation( $wpdb, $string_factory );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_String_Batch_Url_Translation
|
||||
*/
|
||||
class WPML_Media_String_Batch_Url_Translation extends WPML_Media_Batch_Url_Translation implements IWPML_Action {
|
||||
|
||||
const BATCH_SIZE = 500;
|
||||
const AJAX_ACTION = 'wpml_media_translate_media_url_in_strings';
|
||||
|
||||
/**
|
||||
* @var WPML_ST_String_Factory
|
||||
*/
|
||||
private $string_factory;
|
||||
|
||||
/**
|
||||
* WPML_Media_String_Batch_Url_Translation constructor.
|
||||
*
|
||||
* @param wpdb $wpdb
|
||||
* @param WPML_ST_String_Factory $string_factory
|
||||
*/
|
||||
public function __construct(
|
||||
wpdb $wpdb,
|
||||
WPML_ST_String_Factory $string_factory
|
||||
) {
|
||||
parent::__construct( $wpdb );
|
||||
$this->string_factory = $string_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $batch_size_factor
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function get_batch_size( $batch_size_factor = self::BATCH_SIZE_FACTOR_ALL_MEDIA ) {
|
||||
return $batch_size_factor * self::BATCH_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function get_ajax_action() {
|
||||
return self::AJAX_ACTION;
|
||||
}
|
||||
|
||||
public static function is_ajax_request() {
|
||||
return isset( $_POST['action'] ) && self::AJAX_ACTION === $_POST['action'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number_of_strings_left
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_response_message( $number_of_strings_left ) {
|
||||
return sprintf(
|
||||
__( 'Translating media urls in string translations: %s', 'wpml-media' ),
|
||||
$number_of_strings_left > 0 ?
|
||||
sprintf( __( '%d left', 'wpml-media' ), $number_of_strings_left ) :
|
||||
__( 'done!', 'wpml-media' )
|
||||
);
|
||||
}
|
||||
|
||||
protected function get_ajax_error_message() {
|
||||
return array(
|
||||
'key' => 'wpml_media_batch_urls_update_error_strings',
|
||||
'value' => esc_js( __( 'Translating media urls in string translations failed: Please try again (%s)', 'wpml-media' ) ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function process_batch( $offset ) {
|
||||
|
||||
$original_strings = $this->wpdb->get_col(
|
||||
"
|
||||
SELECT SQL_CALC_FOUND_ROWS id, language
|
||||
FROM {$this->wpdb->prefix}icl_strings
|
||||
ORDER BY id ASC
|
||||
LIMIT {$offset}, " . self::BATCH_SIZE
|
||||
);
|
||||
|
||||
$number_of_all_strings = (int) $this->wpdb->get_var( 'SELECT FOUND_ROWS()' );
|
||||
|
||||
foreach ( $original_strings as $string_id ) {
|
||||
$string = $this->string_factory->find_by_id( $string_id );
|
||||
$string_translations = $string->get_translations();
|
||||
foreach ( $string_translations as $string_translation ) {
|
||||
if ( $string_translation->value ) {
|
||||
$string->set_translation( $string_translation->language, $string_translation->value );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $number_of_all_strings - $offset - self::BATCH_SIZE;
|
||||
}
|
||||
|
||||
protected function process_batch_for_selected_media( $offset, $attachment_id ) {
|
||||
|
||||
$media_url = wpml_like_escape( wp_get_attachment_url( $attachment_id ) );
|
||||
if ( ! $media_url ) {
|
||||
return 0;
|
||||
}
|
||||
preg_match( '/(.+)\.([a-z]+)$/', $media_url, $match );
|
||||
$media_url_no_extension = wpml_like_escape( $match[1] );
|
||||
$extension = wpml_like_escape( $match[2] );
|
||||
|
||||
$batch_size = $this->get_batch_size( parent::BATCH_SIZE_FACTOR_SPECIFIC_MEDIA );
|
||||
|
||||
$original_strings = $this->wpdb->get_col(
|
||||
"
|
||||
SELECT SQL_CALC_FOUND_ROWS id, language
|
||||
FROM {$this->wpdb->prefix}icl_strings
|
||||
WHERE (
|
||||
value LIKE '%{$media_url}%' OR
|
||||
value LIKE '%{$media_url_no_extension}-%x%.{$extension}%'
|
||||
)
|
||||
ORDER BY id ASC
|
||||
LIMIT {$offset}, " . $batch_size
|
||||
);
|
||||
|
||||
$number_of_all_strings = (int) $this->wpdb->get_var( 'SELECT FOUND_ROWS()' );
|
||||
|
||||
foreach ( $original_strings as $string_id ) {
|
||||
$string = $this->string_factory->find_by_id( $string_id );
|
||||
$string_translations = $string->get_translations();
|
||||
foreach ( $string_translations as $string_translation ) {
|
||||
if ( $string_translation->value ) {
|
||||
$string->set_translation( $string_translation->language, $string_translation->value );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $number_of_all_strings - $offset - $batch_size;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user