class-wpml-troubleshoot-sync-posts-taxonomies.php
2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
use WPML\API\Sanitize;
/**
* Class WPML_Troubleshoot_Sync_Posts_Taxonomies
*/
class WPML_Troubleshoot_Sync_Posts_Taxonomies {
const BATCH_SIZE = 5;
/** @var SitePress $sitepress */
private $sitepress;
/** @var WPML_Term_Translation_Utils $term_translation_utils */
private $term_translation_utils;
public function __construct( SitePress $sitePress, WPML_Term_Translation_Utils $term_translation_utils ) {
$this->sitepress = $sitePress;
$this->term_translation_utils = $term_translation_utils;
}
public function run() {
if ( ! array_key_exists( 'post_type', $_POST ) || ! array_key_exists( 'batch_number', $_POST ) ) {
wp_send_json_error( array( 'message' => esc_html__( 'Some parameters are missing for this request.', 'sitepress' ) ) );
return;
}
$post_type = Sanitize::stringProp( 'post_type', $_POST );
$batch_number = (int) filter_var( $_POST['batch_number'], FILTER_SANITIZE_NUMBER_INT );
$posts = $this->get_posts_batch( (string) $post_type, $batch_number );
$this->synchronize_batch( $posts );
$new_batch_number = $batch_number + 1;
$response_data = array(
'post_type' => $post_type,
'batch_number' => $new_batch_number,
'message' => sprintf( esc_html__( 'Running now batch #%d', 'sitepress' ), $new_batch_number ),
);
if ( count( $posts ) < self::BATCH_SIZE ) {
$total_posts_processed = ( $batch_number * self::BATCH_SIZE ) + count( $posts );
$response_data['completed'] = true;
$response_data['message'] = sprintf( esc_html__( 'Completed: %1$d posts were processed for "%2$s".', 'sitepress' ), $total_posts_processed, $post_type );
}
wp_send_json_success( $response_data );
}
/**
* @param string $type
* @param int $batch_number
*
* @return array
*/
private function get_posts_batch( $type, $batch_number ) {
$this->sitepress->switch_lang( $this->sitepress->get_default_language() );
$args = array(
'post_type' => $type,
'offset' => $batch_number * self::BATCH_SIZE,
'order_by' => 'ID',
'order' => 'ASC',
'posts_per_page' => self::BATCH_SIZE,
'suppress_filters' => false,
);
$posts = get_posts( $args );
$this->sitepress->switch_lang();
return $posts;
}
/**
* @param array $posts
*/
private function synchronize_batch( $posts ) {
$active_languages = $this->sitepress->get_active_languages();
foreach ( $active_languages as $language_code => $active_language ) {
foreach ( $posts as $post ) {
$this->term_translation_utils->sync_terms( $post->ID, $language_code );
}
}
}
}