class-wpml-media-set-posts-media-flag.php
4.46 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
class WPML_Media_Set_Posts_Media_Flag implements IWPML_Action {
const HAS_MEDIA_POST_FLAG = '_wpml_media_has_media';
const BATCH_SIZE = 100;
/**
* @var wpdb $wpdb
*/
private $wpdb;
/**
* @var WPML_Notices
*/
private $wpml_notices;
/**
* @var WPML_Media_Post_Media_Usage
*/
private $post_media_usage;
/**
* @var WPML_Media_Post_With_Media_Files_Factory
*/
private $post_with_media_files_factory;
public function __construct(
wpdb $wpdb,
WPML_Notices $wpml_notices,
WPML_Media_Post_Media_Usage $post_media_usage,
WPML_Media_Post_With_Media_Files_Factory $post_with_media_files_factory
) {
$this->wpdb = $wpdb;
$this->wpml_notices = $wpml_notices;
$this->post_media_usage = $post_media_usage;
$this->post_with_media_files_factory = $post_with_media_files_factory;
}
public function add_hooks() {
add_action( 'wp_ajax_' . WPML_Media_Posts_Media_Flag_Notice::PREPARE_ACTION, [ $this, 'clear_flags_action' ] );
add_action( 'wp_ajax_' . WPML_Media_Posts_Media_Flag_Notice::PROCESS_ACTION, [ $this, 'process_batch_action' ] );
add_action( 'save_post', array( $this, 'update_post_flag' ) );
}
public function clear_flags_action() {
if ( $this->verify_nonce( WPML_Media_Posts_Media_Flag_Notice::PREPARE_ACTION ) ) {
$this->clear_flags();
wp_send_json_success( array( 'status' => __( 'Running setup...', 'wpml-media' ) ) );
} else {
wp_send_json_error( array( 'status' => 'Invalid nonce' ) );
}
}
public function clear_flags() {
if ( ! WPML_Media::has_setup_started() ) {
$this->wpdb->delete( $this->wpdb->postmeta, array( 'meta_key' => self::HAS_MEDIA_POST_FLAG ), array( '%s' ) );
}
}
public function process_batch_action() {
if ( $this->verify_nonce( WPML_Media_Posts_Media_Flag_Notice::PROCESS_ACTION ) ) {
$offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0;
list( $status, $offset, $continue ) = $this->process_batch( $offset );
wp_send_json_success( array(
'status' => $status,
'offset' => $offset,
'continue' => $continue
) );
} else {
wp_send_json_error( array( 'status' => 'Invalid nonce' ) );
}
}
public function process_batch( $offset ) {
$this->mark_started();
$continue = false;
$status = __( 'Setup complete!', 'wpml-media' );
if ( ! WPML_Media::has_setup_run() ) {
$sql = $this->wpdb->prepare( "
SELECT SQL_CALC_FOUND_ROWS ID, post_content FROM {$this->wpdb->posts} p
JOIN {$this->wpdb->prefix}icl_translations t
ON t.element_id = p.ID AND t.element_type LIKE 'post_%'
LEFT JOIN {$this->wpdb->prefix}postmeta m ON p.ID = m.post_id AND m.meta_key='%s'
WHERE p.post_type NOT IN ( 'auto-draft', 'attachment', 'revision' )
AND t.source_language_code IS NULL AND m.meta_id IS NULL
ORDER BY ID ASC
LIMIT %d, %d
", self::HAS_MEDIA_POST_FLAG, $offset, self::BATCH_SIZE );
$posts = $this->wpdb->get_results( $sql );
$total_posts_found = $this->wpdb->get_var( 'SELECT FOUND_ROWS()' );
if ( $continue = ( count( $posts ) > 0 ) ) {
$this->flag_posts( $posts );
$this->record_media_usage( $posts );
$progress = round( 100 * min( $offset, $total_posts_found ) / $total_posts_found );
$status = sprintf( __( 'Setup in progress: %d%% complete...', 'wpml-media' ), $progress );
}
}
if ( ! $continue ) {
$this->mark_complete();
}
return [ $status, $offset + self::BATCH_SIZE, $continue ];
}
private function verify_nonce( $action ) {
return isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], $action );
}
/**
* @param array $posts
*/
private function flag_posts( $posts ) {
foreach ( $posts as $post ) {
$this->update_post_flag( $post->ID );
}
}
public function update_post_flag( $post_id ) {
$post_with_media_files = $this->post_with_media_files_factory->create( $post_id );
if ( $post_with_media_files->get_media_ids() ) {
update_post_meta( $post_id, self::HAS_MEDIA_POST_FLAG, 1 );
} else {
delete_post_meta( $post_id, self::HAS_MEDIA_POST_FLAG );
}
}
/**
* @param array $posts
*/
private function record_media_usage( $posts ) {
foreach ( $posts as $post ) {
$this->post_media_usage->update_media_usage( $post->ID );
}
}
private function mark_complete() {
WPML_Media::set_setup_run();
$this->wpml_notices->remove_notice(
WPML_Media_Posts_Media_Flag_Notice::NOTICE_GROUP,
WPML_Media_Posts_Media_Flag_Notice::NOTICE_ID
);
}
private function mark_started() {
WPML_Media::set_setup_started();
}
}