first commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TranslateLinkTargets;
|
||||
|
||||
use WPML\LIB\WP\Hooks as WPHooks;
|
||||
use WPML\LIB\WP\Post;
|
||||
use function WPML\FP\spreadArgs;
|
||||
|
||||
class Hooks implements \IWPML_Backend_Action_Loader, \IWPML_Frontend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
return [ self::class, 'add_hooks' ];
|
||||
}
|
||||
|
||||
public static function add_hooks() {
|
||||
WPHooks::onAction( 'wpml_pro_translation_completed', 10, 1 )
|
||||
->then( spreadArgs( [ self::class, 'clearStatus' ] ) );
|
||||
}
|
||||
|
||||
public static function clearStatus( $postId ) {
|
||||
\WPML_Links_Fixed_Status_For_Posts::clear( $postId, 'post_' . Post::getType( $postId ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
abstract class WPML_Ajax_Update_Link_Targets_In_Content extends WPML_WPDB_User implements IWPML_AJAX_Action_Run {
|
||||
|
||||
/** @var WPML_Translate_Link_Targets_In_Content $translate_link_targets */
|
||||
private $translate_link_targets;
|
||||
private $post_data;
|
||||
/** @var WPML_Translate_Link_Target_Global_State $translate_link_target_global_state */
|
||||
protected $translate_link_target_global_state;
|
||||
|
||||
public function __construct( WPML_Translate_Link_Target_Global_State $translate_link_target_global_state, &$wpdb, $post_data ) {
|
||||
parent::__construct( $wpdb );
|
||||
$this->translate_link_target_global_state = $translate_link_target_global_state;
|
||||
$this->translate_link_targets = $this->create_translate_link_target();
|
||||
$this->post_data = $post_data;
|
||||
}
|
||||
|
||||
public function run() {
|
||||
if ( wp_verify_nonce( $this->post_data['nonce'], 'WPML_Ajax_Update_Link_Targets' ) ) {
|
||||
|
||||
$this->translate_link_target_global_state->clear_rescan_required();
|
||||
|
||||
$last_processed = $this->translate_link_targets->fix( $this->post_data['last_processed'], $this->post_data['number_to_process'] );
|
||||
|
||||
return new WPML_Ajax_Response(
|
||||
true,
|
||||
array(
|
||||
'last_processed' => (int) $last_processed,
|
||||
'number_left' => $last_processed ? $this->translate_link_targets->get_number_to_be_fixed( $last_processed + 1 ) : 0,
|
||||
'links_fixed' => $this->translate_link_targets->get_number_of_links_that_were_fixed(),
|
||||
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return new WPML_Ajax_Response( false, 'wrong nonce' );
|
||||
}
|
||||
}
|
||||
|
||||
abstract protected function create_translate_link_target();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
class WPML_Ajax_Update_Link_Targets_In_Posts extends WPML_Ajax_Update_Link_Targets_In_Content {
|
||||
|
||||
private $pro_translation;
|
||||
|
||||
public function __construct( WPML_Translate_Link_Target_Global_State $translate_link_target_global_state, &$wpdb, $pro_translation, $post_data ) {
|
||||
$this->pro_translation = $pro_translation;
|
||||
parent::__construct( $translate_link_target_global_state, $wpdb, $post_data );
|
||||
}
|
||||
|
||||
protected function create_translate_link_target() {
|
||||
return new WPML_Translate_Link_Targets_In_Posts_Global( $this->translate_link_target_global_state, $this->wpdb, $this->pro_translation );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
class WPML_Ajax_Update_Link_Targets_In_Strings extends WPML_Ajax_Update_Link_Targets_In_Content {
|
||||
|
||||
private $wp_api;
|
||||
private $pro_translation;
|
||||
public function __construct( WPML_Translate_Link_Target_Global_State $translate_link_target_global_state, &$wpdb, $wp_api, $pro_translation, $post_data ) {
|
||||
$this->wp_api = $wp_api;
|
||||
$this->pro_translation = $pro_translation;
|
||||
parent::__construct( $translate_link_target_global_state, $wpdb, $post_data );
|
||||
}
|
||||
|
||||
protected function create_translate_link_target() {
|
||||
return new WPML_Translate_Link_Targets_In_Strings_Global( $this->translate_link_target_global_state, $this->wpdb, $this->wp_api, $this->pro_translation );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Links_Fixed_Status_Factory
|
||||
*
|
||||
* @package wpml-translation-management
|
||||
*/
|
||||
class WPML_Links_Fixed_Status_Factory extends WPML_WPDB_User {
|
||||
|
||||
private $wp_api;
|
||||
|
||||
public function __construct( &$wpdb, $wp_api ) {
|
||||
parent::__construct( $wpdb );
|
||||
|
||||
$this->wp_api = $wp_api;
|
||||
}
|
||||
|
||||
public function create( $element_id, $element_type ) {
|
||||
$links_fixed_status = null;
|
||||
|
||||
if ( strpos( $element_type, 'post' ) === 0 ) {
|
||||
$links_fixed_status = new WPML_Links_Fixed_Status_For_Posts( $this->wpdb, $element_id, $element_type );
|
||||
} elseif ( $element_type == 'string' ) {
|
||||
$links_fixed_status = new WPML_Links_Fixed_Status_For_Strings( $this->wp_api, $element_id );
|
||||
}
|
||||
|
||||
return $links_fixed_status;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Links_Fixed_Status_For_Posts
|
||||
*
|
||||
* @package wpml-tm
|
||||
*/
|
||||
class WPML_Links_Fixed_Status_For_Posts extends WPML_Links_Fixed_Status {
|
||||
|
||||
/* @var int $translation_id */
|
||||
private $translation_id;
|
||||
private $wpdb;
|
||||
|
||||
public function __construct( $wpdb, $element_id, $element_type ) {
|
||||
$this->wpdb = $wpdb;
|
||||
|
||||
$this->translation_id = $wpdb->get_var( $wpdb->prepare( "SELECT translation_id
|
||||
FROM {$wpdb->prefix}icl_translations
|
||||
WHERE element_id=%d
|
||||
AND element_type=%s",
|
||||
$element_id,
|
||||
$element_type ) );
|
||||
}
|
||||
|
||||
public function set( $status ) {
|
||||
$status = $status ? 1 : 0;
|
||||
|
||||
$q = "UPDATE {$this->wpdb->prefix}icl_translation_status SET links_fixed=%d WHERE translation_id=%d";
|
||||
$q_prepared = $this->wpdb->prepare( $q, array( $status, $this->translation_id ) );
|
||||
$this->wpdb->query($q_prepared);
|
||||
}
|
||||
|
||||
public function are_links_fixed() {
|
||||
$state = $this->wpdb->get_var( $this->wpdb->prepare( "SELECT links_fixed
|
||||
FROM {$this->wpdb->prefix}icl_translation_status
|
||||
WHERE translation_id=%d",
|
||||
$this->translation_id ) );
|
||||
return (bool) $state;
|
||||
}
|
||||
|
||||
public static function clear( $element_id, $element_type ) {
|
||||
global $wpdb;
|
||||
$status = new WPML_Links_Fixed_Status_For_Posts( $wpdb, $element_id, $element_type );
|
||||
$status->set( false );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Links_Fixed_Status_For_Posts
|
||||
*
|
||||
* @package wpml-tm
|
||||
*/
|
||||
class WPML_Links_Fixed_Status_For_Strings extends WPML_Links_Fixed_Status {
|
||||
|
||||
private $wp_api;
|
||||
private $string_id;
|
||||
private $option_name = 'wpml_strings_need_links_fixed';
|
||||
|
||||
public function __construct( &$wp_api, $string_id ) {
|
||||
$this->wp_api = &$wp_api;
|
||||
$this->string_id = $string_id;
|
||||
}
|
||||
|
||||
public function set( $status ) {
|
||||
if ( $status ) {
|
||||
$this->remove_string_from_strings_that_need_fixing();
|
||||
} else {
|
||||
$this->add_string_to_strings_that_need_fixing();
|
||||
}
|
||||
}
|
||||
|
||||
public function are_links_fixed() {
|
||||
$strings_that_need_links_fixed = $this->load_strings_that_need_fixing();
|
||||
return array_search( $this->string_id, $strings_that_need_links_fixed ) === false;
|
||||
}
|
||||
|
||||
private function remove_string_from_strings_that_need_fixing() {
|
||||
$strings_that_need_links_fixed = $this->load_strings_that_need_fixing();
|
||||
|
||||
if ( ( $key = array_search( $this->string_id, $strings_that_need_links_fixed ) ) !== false ) {
|
||||
unset( $strings_that_need_links_fixed[ $key ] );
|
||||
}
|
||||
|
||||
$this->save_strings_that_need_fixing( $strings_that_need_links_fixed );
|
||||
}
|
||||
|
||||
private function add_string_to_strings_that_need_fixing() {
|
||||
$strings_that_need_links_fixed = $this->load_strings_that_need_fixing();
|
||||
|
||||
if ( ( array_search( $this->string_id, $strings_that_need_links_fixed ) ) === false ) {
|
||||
$strings_that_need_links_fixed[] = $this->string_id;
|
||||
}
|
||||
|
||||
$this->save_strings_that_need_fixing( $strings_that_need_links_fixed );
|
||||
}
|
||||
|
||||
private function load_strings_that_need_fixing() {
|
||||
return $this->wp_api->get_option( $this->option_name, array() );
|
||||
}
|
||||
|
||||
private function save_strings_that_need_fixing( $strings_that_need_links_fixed ) {
|
||||
$this->wp_api->update_option( $this->option_name, $strings_that_need_links_fixed );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Links_Fixed_Status
|
||||
*
|
||||
* @package wpml-translation-management
|
||||
*/
|
||||
abstract class WPML_Links_Fixed_Status {
|
||||
|
||||
abstract public function set( $status );
|
||||
|
||||
abstract public function are_links_fixed();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translate_Link_Target_Global_State extends WPML_SP_User {
|
||||
|
||||
private $rescan_required;
|
||||
const OPTION_NAME = 'WPML_Translate_Link_Target_Global_State';
|
||||
const SHOULD_FIX_CONTENT_STATE = 'WPML_Translate_Link_Target_Global_State::should_fix_content';
|
||||
|
||||
public function __construct( SitePress &$sitepress ) {
|
||||
parent::__construct( $sitepress );
|
||||
$this->rescan_required = $sitepress->get_setting( self::OPTION_NAME, false );
|
||||
}
|
||||
|
||||
public function should_fix_content() {
|
||||
return $this->sitepress->get_current_request_data( self::SHOULD_FIX_CONTENT_STATE, true );
|
||||
}
|
||||
|
||||
public function is_rescan_required() {
|
||||
return $this->rescan_required;
|
||||
}
|
||||
|
||||
public function set_rescan_required() {
|
||||
$this->rescan_required = true;
|
||||
$this->sitepress->set_setting( self::OPTION_NAME, $this->rescan_required, true );
|
||||
$this->sitepress->set_current_request_data( self::SHOULD_FIX_CONTENT_STATE, false );
|
||||
}
|
||||
|
||||
public function clear_rescan_required() {
|
||||
$this->rescan_required = false;
|
||||
$this->sitepress->set_setting( self::OPTION_NAME, $this->rescan_required, true );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Translate_Link_Targets_In_Content
|
||||
*
|
||||
* @package wpml-tm
|
||||
*/
|
||||
abstract class WPML_Translate_Link_Targets_In_Content extends WPML_WPDB_User {
|
||||
|
||||
protected $scanning_in_progress = false;
|
||||
protected $content_to_fix;
|
||||
protected $number_of_links_fixed;
|
||||
/* var WPML_Pro_Translation $pro_translation */
|
||||
protected $pro_translation;
|
||||
/** @var WPML_Translate_Link_Target_Global_State $translate_link_target_global_state */
|
||||
private $translate_link_target_global_state;
|
||||
|
||||
const MAX_TO_FIX_FOR_NEW_CONTENT = 10;
|
||||
|
||||
public function __construct( WPML_Translate_Link_Target_Global_State $translate_link_target_global_state,
|
||||
&$wpdb,
|
||||
$pro_translation ) {
|
||||
parent::__construct( $wpdb );
|
||||
$this->pro_translation = $pro_translation;
|
||||
$this->translate_link_target_global_state = $translate_link_target_global_state;
|
||||
}
|
||||
|
||||
public function new_content() {
|
||||
if ( $this->translate_link_target_global_state->should_fix_content() ) {
|
||||
if ( ! $this->do_new_content() ) {
|
||||
$this->translate_link_target_global_state->set_rescan_required();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function do_new_content() {
|
||||
|
||||
if ( $this->pro_translation && ! $this->scanning_in_progress ) {
|
||||
$number_needing_to_be_fixed = $this->get_number_to_be_fixed();
|
||||
$this->fix( 0, self::MAX_TO_FIX_FOR_NEW_CONTENT );
|
||||
return $number_needing_to_be_fixed <= self::MAX_TO_FIX_FOR_NEW_CONTENT;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function get_number_of_links_that_were_fixed() {
|
||||
return $this->number_of_links_fixed;
|
||||
}
|
||||
|
||||
|
||||
public function fix( $start = 0, $count = 0 ) {
|
||||
$this->scanning_in_progress = true;
|
||||
$this->get_contents_with_links_needing_fix( $start, $count );
|
||||
$last_content_processed = 0;
|
||||
$this->number_of_links_fixed = 0;
|
||||
|
||||
foreach ( $this->content_to_fix as $content ) {
|
||||
|
||||
$this->number_of_links_fixed += $this->pro_translation->fix_links_to_translated_content( $content->element_id, $content->language_code, $this->get_content_type() );
|
||||
$last_content_processed = $content->element_id;
|
||||
}
|
||||
|
||||
$this->scanning_in_progress = false;
|
||||
|
||||
return $last_content_processed;
|
||||
}
|
||||
|
||||
abstract protected function get_contents_with_links_needing_fix( $start = 0, $count = 0 );
|
||||
abstract protected function get_content_type();
|
||||
abstract public function get_number_to_be_fixed( $start_id = 0 );
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translate_Link_Targets_In_Posts_Global extends WPML_Translate_Link_Targets_In_Posts {
|
||||
|
||||
protected function get_contents_with_links_needing_fix( $start_id = 0, $count = 0 ) {
|
||||
$this->content_to_fix = $this->wpdb->get_results( $this->get_sql( $start_id, $count, false) );
|
||||
}
|
||||
|
||||
public function get_number_to_be_fixed( $start_id = 0 ) {
|
||||
return $this->wpdb->get_var( $this->get_sql( $start_id, 0, true ) );
|
||||
}
|
||||
|
||||
protected function get_sql( $start_id , $count, $return_count_only ) {
|
||||
|
||||
$limit = '';
|
||||
if ( $count > 0 ) {
|
||||
$limit = " LIMIT " . $count;
|
||||
}
|
||||
|
||||
if ( $return_count_only ) {
|
||||
$sql = "SELECT COUNT(t.element_id)";
|
||||
} else {
|
||||
$sql = "SELECT t.element_id, t.language_code";
|
||||
}
|
||||
$sql = $this->wpdb->prepare( $sql .
|
||||
" FROM {$this->wpdb->prefix}icl_translations AS t
|
||||
INNER JOIN {$this->wpdb->prefix}icl_translation_status AS ts
|
||||
ON t.translation_id = ts.translation_id
|
||||
WHERE t.element_id IS NOT NULL
|
||||
AND ts.links_fixed = 0
|
||||
AND t.element_id >= %d
|
||||
AND t.element_type LIKE 'post_%%'
|
||||
ORDER BY t.element_id ASC" . $limit,
|
||||
$start_id
|
||||
);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Translate_Link_Targets_In_Posts
|
||||
*
|
||||
* @package wpml-tm
|
||||
*/
|
||||
class WPML_Translate_Link_Targets_In_Posts extends WPML_Translate_Link_Targets_In_Content {
|
||||
|
||||
protected function get_contents_with_links_needing_fix( $start_id = 0, $count = 0 ) {
|
||||
$this->content_to_fix = $this->wpdb->get_results( $this->get_sql( $start_id, $count, false) );
|
||||
}
|
||||
|
||||
protected function get_content_type() {
|
||||
return 'post';
|
||||
}
|
||||
|
||||
public function get_number_to_be_fixed( $start_id = 0 ) {
|
||||
return $this->wpdb->get_var( $this->get_sql( $start_id, 0, true ) );
|
||||
}
|
||||
|
||||
protected function get_sql( $start_id , $count, $return_count_only ) {
|
||||
$limit = '';
|
||||
if ( $count > 0 ) {
|
||||
$limit = " LIMIT " . $count;
|
||||
}
|
||||
|
||||
if ( $return_count_only ) {
|
||||
$sql = "SELECT COUNT(t.element_id)";
|
||||
} else {
|
||||
$sql = "SELECT t.element_id, t.language_code";
|
||||
}
|
||||
|
||||
$sql = $this->wpdb->prepare( $sql .
|
||||
" FROM {$this->wpdb->prefix}icl_translations AS t
|
||||
INNER JOIN {$this->wpdb->prefix}icl_translation_status AS ts
|
||||
ON t.translation_id = ts.translation_id
|
||||
WHERE ts.links_fixed = 0
|
||||
AND t.element_id IS NOT NULL
|
||||
AND t.element_id >= %d
|
||||
AND t.element_type LIKE 'post_%%'
|
||||
ORDER BY t.element_id ASC" . $limit,
|
||||
$start_id
|
||||
);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translate_Link_Targets_In_Strings_Global extends WPML_Translate_Link_Targets_In_Strings {
|
||||
|
||||
protected function get_contents_with_links_needing_fix( $start_id = 0, $count = 0 ) {
|
||||
|
||||
$limit = '';
|
||||
if ( $count > 0 ) {
|
||||
$limit = ' LIMIT ' . $count;
|
||||
}
|
||||
|
||||
$this->content_to_fix = $this->wpdb->get_results(
|
||||
$this->wpdb->prepare(
|
||||
"SELECT id as element_id, language as language_code FROM {$this->wpdb->prefix}icl_string_translations WHERE id >= %d AND status = %d ORDER BY id " . $limit,
|
||||
$start_id,
|
||||
ICL_TM_COMPLETE)
|
||||
);
|
||||
}
|
||||
|
||||
public function get_number_to_be_fixed( $start_id = 0 ) {
|
||||
return $this->wpdb->get_var(
|
||||
$this->wpdb->prepare(
|
||||
"SELECT COUNT(id) FROM {$this->wpdb->prefix}icl_string_translations WHERE id >= %d AND status = %d ORDER BY id ",
|
||||
$start_id,
|
||||
ICL_TM_COMPLETE)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Translate_Link_Targets_In_Strings
|
||||
*
|
||||
* @package wpml-tm
|
||||
*/
|
||||
class WPML_Translate_Link_Targets_In_Strings extends WPML_Translate_Link_Targets_In_Content {
|
||||
|
||||
private $option_name = 'wpml_strings_need_links_fixed';
|
||||
|
||||
/* var WPML_WP_API $wp_api */
|
||||
private $wp_api;
|
||||
|
||||
public function __construct( WPML_Translate_Link_Target_Global_State $translate_link_target_global_state, &$wpdb, $wp_api, $pro_translation ) {
|
||||
parent::__construct( $translate_link_target_global_state, $wpdb, $pro_translation );
|
||||
$this->wp_api = $wp_api;
|
||||
}
|
||||
|
||||
protected function get_contents_with_links_needing_fix( $start = 0, $count = 0 ) {
|
||||
$strings_to_fix = $this->wp_api->get_option( $this->option_name, array() );
|
||||
sort( $strings_to_fix, SORT_NUMERIC );
|
||||
$strings_to_fix_part = array();
|
||||
$include_all = $count == 0 ? true : false;
|
||||
foreach ( $strings_to_fix as $string_id ) {
|
||||
if ( $string_id >= $start ) {
|
||||
$strings_to_fix_part[] = $string_id;
|
||||
}
|
||||
if ( ! $include_all ) {
|
||||
$count--;
|
||||
if ( $count == 0 ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->content_to_fix = array();
|
||||
|
||||
if ( sizeof( $strings_to_fix_part ) ) {
|
||||
$strings_to_fix_part = wpml_prepare_in( $strings_to_fix_part, '%d' );
|
||||
$this->content_to_fix = $this->wpdb->get_results(
|
||||
"SELECT id as element_id, language as language_code
|
||||
FROM {$this->wpdb->prefix}icl_string_translations
|
||||
WHERE id in ( {$strings_to_fix_part} )"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function get_content_type() {
|
||||
return 'string';
|
||||
}
|
||||
|
||||
public function get_number_to_be_fixed( $start_id = 0 ) {
|
||||
$this->get_contents_with_links_needing_fix( $start_id );
|
||||
return sizeof( $this->content_to_fix );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user