PluginProbe
FV Player 8 / trunk
FV Player 8 vtrunk
trunk 8.0.18 8.0.19 8.0.20 8.0.21 8.0.25 8.0.27 8.1 8.1.3
fv-player / models / conversion / positionsMeta2Table.php

positionsMeta2Table.php in FV Player 8 trunk, at models/conversion/positionsMeta2Table.php

443 lines 12.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class FV_Player_Positions_Meta2Table_Conversion extends FV_Player_Conversion_Base {
8
9 function __construct() {
10
11 parent::__construct( array(
12 'title' => 'FV Player PositionsMeta2Table Conversion',
13 'slug' => 'positions_meta2table',
14 ) );
15
16 $this->conversion_limit = 2500;
17 $this->make_chages_button = false; // disable make changes button
18
19 $this->screen_fields = array(
20 'User ID',
21 'Video ID',
22 'Result',
23 'Error',
24 );
25
26 add_action( 'init', array( $this, 'cron_init' ) );
27 add_action( 'admin_init', array( $this, 'set_pointer_checked' ) );
28 add_action( 'fv_player_' .$this->slug . '_cleanup' , array( $this, 'meta_cleanup' ) );
29 }
30
31 /**
32 * Override parent method
33 *
34 * @return int
35 */
36 function get_count() {
37 global $wpdb;
38 return (int) $wpdb->get_var( "SELECT COUNT(*) AS count FROM `$wpdb->usermeta` WHERE meta_key LIKE 'fv_wp_flowplayer_%'" );
39 }
40
41 function get_items( $offset, $limit ) {
42 global $wpdb;
43
44 // select umeta_id to prevent using filesort
45 $ids = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM `$wpdb->usermeta` WHERE meta_key LIKE %s ORDER BY umeta_id ASC LIMIT %d, %d", $wpdb->esc_like( 'fv_wp_flowplayer_' ). '%', $offset, $limit ) );
46
47 // select all meta data fields by ids
48 $umeta_ids = implode(',', array_map( 'intval', $ids ) );
49
50 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
51 $meta_data = $wpdb->get_results( "SELECT * FROM `$wpdb->usermeta` WHERE umeta_id IN ( {$umeta_ids} ) ORDER BY umeta_id ASC" );
52
53 return $meta_data;
54 }
55
56 function convert_one($meta) {
57 $output_data = array(); // output for html
58 $errors = array(); // all errors for export
59
60 // get meta data
61 $meta_key = $meta->meta_key;
62 $meta_value = $meta->meta_value;
63 $user_id = $meta->user_id;
64
65 $type = '';
66
67 if( strpos($meta_key, 'fv_wp_flowplayer_position') !== false ) { // last
68 $type = 'last_position';
69 } else if ( strpos($meta_key, 'fv_wp_flowplayer_saw') !== false ) { // finished
70 $type = 'finished';
71 } else if ( strpos($meta_key, 'fv_wp_flowplayer_top_position') !== false ) { // top
72 $type = 'top_position';
73 } else if ( strpos($meta_key, 'fv_wp_flowplayer_player_playlist') !== false ) { // playlist
74 $type = 'playlist';
75 }
76
77 if( $type == 'playlist' ) {
78 preg_match('/fv_wp_flowplayer_player_playlist_(\d+)/', $meta_key, $matches);
79 if( isset( $matches[1] ) ) {
80 $playlist_id = $matches[1];
81
82 // check if its db video or external
83 $row_exitst = $this->position_row_exists( $user_id, $playlist_id, 'playlist' );
84
85 $result = $this->insert_update_playlist_row( $user_id, $playlist_id, $meta_value, $row_exitst );
86
87 if( $result ) {
88 $output_data[] = array(
89 'ID' => $user_id,
90 'Name' => $meta_key,
91 'output' => $row_exitst ? 'Playlist position updated' : 'Playlist position inserted',
92 'error' => ''
93 );
94 } else { // failed to update
95 $output_data[] = array(
96 'ID' => $user_id,
97 'Name' => $meta_key,
98 'output' => 'Playlist position failed to update',
99 'error' => 'Failed to update playlist position'
100 );
101
102 $errors[] = array(
103 'ID' => $user_id,
104 'Name' => $meta_key
105 );
106 }
107
108 } else {
109 // failed to get playlist id
110 $output_data[] = array(
111 'ID' => $user_id,
112 'Name' => $meta_key,
113 'output' => 'Playlist position failed to update',
114 'error' => 'Cannot get playlist id'
115 );
116
117 $errors[] = array(
118 'ID' => $user_id,
119 'Name' => $meta_key
120 );
121 }
122
123 } else {
124 preg_match('/fv_wp_flowplayer_\w+_(.*)/', $meta_key, $matches);
125 if( isset( $matches[1] ) ) {
126 $video_id = $matches[1];
127
128 // check if video exitst in db
129 $video_exists = $this->video_exists( $video_id );
130 $row_exitst = $this->position_row_exists( $user_id, $video_id, 'position', $video_exists );
131
132 $result = $this->insert_update_video_row( $user_id, $video_id, $type, $meta_value, $row_exitst, $video_exists );
133
134 if( $result ) {
135 $output_data[] = array(
136 'ID' => $user_id,
137 'Name' => $meta_key,
138 'output' => $row_exitst ? 'Video position updated' : 'Video position inserted',
139 'error' => ''
140 );
141 } else {
142 $output_data[] = array(
143 'ID' => $user_id,
144 'Name' => $meta_key,
145 'output' => 'Video position failed to update',
146 'error' => 'Failed to update position'
147 );
148
149 $errors[] = array(
150 'ID' => $user_id,
151 'Name' => $meta_key
152 );
153 }
154
155 } else {
156 $output_data[] = array(
157 'ID' => $user_id,
158 'Name' => $meta_key,
159 'output' => 'Video position failed to update',
160 'error' => 'Cannot get video id'
161 );
162
163 // failed to get video id
164 $errors[] = array(
165 'ID' => $user_id,
166 'Name' => $meta_key
167 );
168 }
169
170 }
171
172 return array(
173 'output_data' => $output_data,
174 'errors' => $errors
175 );
176
177 }
178
179 public function get_text( $type ) {
180 if ( 'conversion_done_details' === $type ) {
181 return __( 'The conversion has finished. The usermeta table will be purged of the FV Player video position data in 4 weeks.', 'fv-player' );
182
183 } else if ( 'help' === $type ) {
184 global $wpdb;
185 return sprintf( __( "This converts position values from <code>%s</code> to <code>%s</code> table.", 'fv-player' ), $wpdb->usermeta, $wpdb->prefix . 'fv_player_user_video_positions' );
186
187 } else if ( 'start_warning' === $type ) {
188 return __( 'This will convert positions from usermeta to new tables. Please make sure you have a backup of your database before continuing.', 'fv-player' );
189 }
190
191 return parent::get_text( $type );
192 }
193
194 /**
195 * Insert or update video row
196 *
197 * @param int $user_id
198 * @param int $video_id
199 * @param string $type
200 * @param int $value
201 * @param boolean $row_exitst
202 * @param boolean $video_exists
203 *
204 * @return boolean $res result of insert or update
205 */
206 function insert_update_video_row( $user_id, $video_id, $type, $value, $row_exitst, $video_exists ) {
207 global $wpdb;
208
209 if( !$video_exists ) { // non db video
210 $legacy_id = $video_id;
211 $video_id = 0;
212 } else { // db video
213 $legacy_id = '';
214 $video_id = intval($video_id);
215 }
216
217 if( $row_exitst ) {
218 $res = $wpdb->update(
219 $wpdb->prefix . 'fv_player_user_video_positions',
220 array(
221 $type => $value
222 ),
223 array(
224 'user_id' => $user_id,
225 'video_id' => $video_id,
226 'legacy_video_id' => $legacy_id
227 )
228 );
229
230 $res = is_numeric($res);
231 } else {
232 $res = $wpdb->insert(
233 $wpdb->prefix . 'fv_player_user_video_positions',
234 array(
235 $type => $value,
236 'user_id' => $user_id,
237 'video_id' => $video_id,
238 'legacy_video_id' => $legacy_id
239 )
240 );
241
242 $res = !empty($res);
243 }
244
245 return $res;
246 }
247
248 /**
249 * Insert or update playlist row
250 *
251 * @param int $user_id
252 * @param int $playlist_id
253 * @param int $value
254 * @param boolean $exitst
255 *
256 * @return boolean $res result of insert or update
257 */
258 function insert_update_playlist_row( $user_id, $playlist_id, $value, $exitst ) {
259 global $wpdb;
260
261 if( $exitst ) {
262 $res = $wpdb->update(
263 $wpdb->prefix . 'fv_player_user_playlist_positions',
264 array(
265 'item_index' => $value
266 ),
267 array(
268 'user_id' => $user_id,
269 'player_id' => $playlist_id
270 )
271 );
272
273 $res = is_numeric($res);
274 } else {
275 $res = $wpdb->insert(
276 $wpdb->prefix . 'fv_player_user_playlist_positions',
277 array(
278 'user_id' => $user_id,
279 'player_id' => $playlist_id,
280 'item_index' => $value
281 )
282 );
283
284 $res = !empty($res);
285 }
286
287 return $res;
288 }
289
290 /**
291 * Check if video exitst in db
292 *
293 * @param int $video_id
294 *
295 * @return object|null
296 */
297 function video_exists( $video_id ) {
298 global $wpdb;
299
300 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}fv_player_videos` WHERE id = %d", $video_id ) );
301
302 return $row;
303 }
304
305 /**
306 * Check if position or playlist row exitst
307 *
308 * @param int $user_id
309 * @param int|string $id
310 * @param string $type
311 * @param boolean $video_exists
312 *
313 * @return object|null
314 */
315 function position_row_exists( $user_id, $id, $type, $video_exists = false ) {
316 global $wpdb;
317
318 if( $type == 'position' ) {
319
320 if( $video_exists ) { // db video
321 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}fv_player_user_video_positions` WHERE user_id = %d AND video_id = %d", $user_id, $id ) );
322 } else { // legacy video
323 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}fv_player_user_video_positions` WHERE user_id = %d AND legacy_video_id = %d", $user_id, $id ) );
324 }
325
326 }
327
328 if( $type == 'playlist' ) {
329 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}fv_player_user_playlist_positions` WHERE user_id = %d AND player_id = %d", $user_id, $id ) );
330 }
331
332 return $row;
333 }
334
335 function conversion_button() {
336 ?>
337 <tr>
338 <td><label>Convert position meta values stored in usermeta to new table :</label></td>
339 <td>
340 <p class="description">
341 <input type="button" class="button" value="<?php esc_attr_e('Convert positions', 'fv-player-pro'); ?>" style="margin-top: 2ex;" onclick="location.href='<?php echo admin_url('admin.php?page=' . $this->screen ) ?>'; "/>
342 </p>
343 </td>
344 </tr>
345 <?php
346 }
347
348 function iterate_data( $data ) {
349 $conversions_output = array();
350 $convert_error = false;
351
352 foreach( $data as $meta ) {
353 $result = $this->convert_one( $meta );
354
355 if( !empty($result['errors']) ) {
356 $convert_error = true;
357 }
358
359 $conversions_output = array_merge( $conversions_output, $result['output_data'] );
360 }
361
362 return array(
363 'convert_error' => $convert_error,
364 'conversions_output' => $conversions_output
365 );
366 }
367
368 function build_output_html( $data, $percent_done ) {
369 $html = array();
370
371 foreach( $data as $output_data ) {
372 if(!empty( $output_data['error']) ) { // show only errors
373 $html[] = "<tr><td>". $output_data['ID'] . "</td><td>". $output_data['Name'] ."</td><td>". $output_data['output'] ."</td><td>". $output_data['error'] ."</td></tr>";
374 }
375 }
376
377 if( empty($html) && $percent_done == 0 ) {
378 $html[] = "<tr><td colspan='4'>No matching meta found.</td></tr>";
379 }
380
381 return $html;
382 }
383
384 function cron_init() {
385 global $fv_fp;
386
387 $last_conversions = $fv_fp->_get_option('conversion');
388 $should_cleanup = false;
389
390 if(isset($last_conversions[$this->slug])) {
391 $last_conversion = $last_conversions[$this->slug]['date'];
392 $did_cleanup = $last_conversions[$this->slug]['did_cleanup'];
393
394 // check if enough time has passed since last conversion (4 weeks), date is in Y-m-d H:i:s format
395 if( !$did_cleanup && strtotime($last_conversion) < strtotime('-4 weeks') ) {
396 $should_cleanup = true;
397 }
398 }
399
400 if ( $should_cleanup && !wp_next_scheduled( 'fv_player_' .$this->slug . '_cleanup' ) ) {
401 wp_schedule_event( time(), '5minutes', 'fv_player_' .$this->slug . '_cleanup' );
402 } else if( !$should_cleanup && wp_next_scheduled( 'fv_player_' .$this->slug . '_cleanup' ) ) {
403 wp_clear_scheduled_hook( 'fv_player_' .$this->slug . '_cleanup' );
404 }
405 }
406
407 function meta_cleanup() {
408 set_time_limit( 0 );
409
410 global $wpdb;
411
412 $options = get_option( 'fvwpflowplayer' );
413 $options['conversion'][$this->slug]['did_cleanup'] = true;
414
415 update_option( 'fvwpflowplayer', $options );
416
417 // delete all meta
418 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s", $wpdb->esc_like( 'fv_wp_flowplayer_') . '%' ) );
419
420 // optimize table
421 $wpdb->query( "OPTIMIZE TABLE {$wpdb->usermeta}" );
422
423 }
424
425 function set_pointer_checked() {
426 // check if we are on the right page
427 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
428 if( isset($_GET['page'] ) && sanitize_key( $_GET['page'] ) == $this->screen ) {
429 $conf = get_option( 'fvwpflowplayer' );
430
431 // set pointer box to seen
432 if( !isset($conf['notice_user_video_positions_conversion']) ) {
433 $conf['notice_user_video_positions_conversion'] = true;
434 update_option( 'fvwpflowplayer', $conf );
435 }
436 }
437 }
438
439 }
440
441 global $FV_Player_Positions_Meta2Table_Conversion;
442 $FV_Player_Positions_Meta2Table_Conversion = new FV_Player_Positions_Meta2Table_Conversion;
443