PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 All 72 releases
bbpress / includes / admin / classes / class-bbp-converter-base.php

class-bbp-converter-base.php in bbPress 2.6.17, at includes/admin/classes/class-bbp-converter-base.php

1,285 lines 39.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Converter Base Class
5 *
6 * Based on the hard work of Adam Ellis
7 *
8 * @package bbPress
9 * @subpackage Administration
10 */
11
12 // Exit if accessed directly
13 defined( 'ABSPATH' ) || exit;
14
15 if ( ! class_exists( 'BBP_Converter_Base' ) ) :
16 /**
17 * Base class to be extended by specific individual importers
18 *
19 * phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
20 *
21 * @since 2.1.0 bbPress (r3813)
22 */
23 abstract class BBP_Converter_Base {
24
25 /**
26 * @var array() This is the field mapping array to process.
27 */
28 protected $field_map = array();
29
30 /**
31 * @var object This is the connection to the WordPress database.
32 */
33 protected $wpdb;
34
35 /**
36 * @var object This is the connection to the other platforms database.
37 */
38 protected $opdb;
39
40 /**
41 * @var int Halt on error. Default 0.
42 */
43 protected $halt = 0;
44
45 /**
46 * @var int Maximum number of rows to convert at 1 time. Default 100.
47 */
48 protected $max_rows = 100;
49
50 /**
51 * @var array() Map of topic to forum. It is for optimization.
52 */
53 protected $map_topicid_to_forumid = array();
54
55 /**
56 * @var array() Map of from old forum ids to new forum ids. It is for optimization.
57 */
58 protected $map_forumid = array();
59
60 /**
61 * @var array() Map of from old topic ids to new topic ids. It is for optimization.
62 */
63 protected $map_topicid = array();
64
65 /**
66 * @var array() Map of from old reply_to ids to new reply_to ids. It is for optimization.
67 */
68 protected $map_reply_to = array();
69
70 /**
71 * @var array() Map of from old user ids to new user ids. It is for optimization.
72 */
73 protected $map_userid = array();
74
75 /**
76 * @var string This is the charset for your wp database.
77 */
78 public $charset = '';
79
80 /**
81 * @var boolean Sync table available.
82 */
83 public $sync_table = false;
84
85 /**
86 * @var string Sync table name.
87 */
88 public $sync_table_name = '';
89
90 /**
91 * @var bool Whether users should be converted or not. Default false.
92 */
93 public $convert_users = false;
94
95 /**
96 * @var bool Whether to clean up any old converter data. Default false.
97 */
98 public $clean = false;
99
100 /**
101 * @var array Custom BBCode class properties in a key => value format
102 */
103 public $bbcode_parser_properties = array();
104
105 /** Methods ***************************************************************/
106
107 /**
108 * This is the constructor and it connects to the platform databases.
109 */
110 public function __construct() {
111 $this->init();
112 $this->setup_globals();
113 }
114
115 /**
116 * Initialize the converter
117 *
118 * @since 2.1.0
119 */
120 private function init() {
121
122 /** BBCode Parse Properties *******************************************/
123
124 // Setup smiley URL & path
125 $this->bbcode_parser_properties = array(
126 'smiley_url' => includes_url( 'images/smilies' ),
127 'smiley_dir' => '/' . WPINC . '/images/smilies'
128 );
129
130 /** Sanitize Options **************************************************/
131
132 $this->clean = ! empty( $_POST['_bbp_converter_clean'] );
133 $this->convert_users = (bool) get_option( '_bbp_converter_convert_users', false );
134 $this->halt = (bool) get_option( '_bbp_converter_halt', 0 );
135 $this->max_rows = (int) get_option( '_bbp_converter_rows', 100 );
136
137 /** Sanitize Connection ***********************************************/
138
139 $db_user = get_option( '_bbp_converter_db_user', DB_USER );
140 $db_pass = get_option( '_bbp_converter_db_pass', DB_PASSWORD );
141 $db_name = get_option( '_bbp_converter_db_name', DB_NAME );
142 $db_host = get_option( '_bbp_converter_db_server', DB_HOST );
143 $db_port = get_option( '_bbp_converter_db_port', '' );
144 $db_prefix = get_option( '_bbp_converter_db_prefix', '' );
145
146 // Maybe add port to server
147 if ( ! empty( $db_port ) && ! empty( $db_host ) && ! strstr( $db_host, ':' ) ) {
148 $db_host = "{$db_host}:{$db_port}";
149 }
150
151 /** Get database connections ******************************************/
152
153 // Setup WordPress Database
154 $this->wpdb = bbp_db();
155
156 // Setup old forum Database
157 $this->opdb = new BBP_Converter_DB( $db_user, $db_pass, $db_name, $db_host );
158
159 // Connection failed
160 if ( ! $this->opdb->db_connect( false ) ) {
161 $error = new WP_Error( 'bbp_converter_db_connection_failed', esc_html__( 'Database connection failed.', 'bbpress' ) );
162 wp_send_json_error( $error );
163 }
164
165 // Maybe setup the database prefix
166 $this->opdb->prefix = $db_prefix;
167
168 /**
169 * Don't wp_die() uncontrollably
170 */
171 $this->wpdb->show_errors( false );
172 $this->opdb->show_errors( false );
173
174 /**
175 * Syncing
176 */
177 $this->sync_table_name = $this->wpdb->prefix . 'bbp_converter_translator';
178 $this->sync_table = $this->sync_table_name === $this->wpdb->get_var( "SHOW TABLES LIKE '{$this->sync_table_name}'" )
179 ? true
180 : false;
181
182 /**
183 * Character set
184 */
185 $this->charset = ! empty( $this->wpdb->charset )
186 ? $this->wpdb->charset
187 : 'UTF8';
188
189 /**
190 * Default mapping.
191 */
192
193 /** Forum Section *****************************************************/
194
195 $this->field_map[] = array(
196 'to_type' => 'forum',
197 'to_fieldname' => 'post_status',
198 'default' => 'publish'
199 );
200 $this->field_map[] = array(
201 'to_type' => 'forum',
202 'to_fieldname' => 'comment_status',
203 'default' => 'closed'
204 );
205 $this->field_map[] = array(
206 'to_type' => 'forum',
207 'to_fieldname' => 'ping_status',
208 'default' => 'closed'
209 );
210 $this->field_map[] = array(
211 'to_type' => 'forum',
212 'to_fieldname' => 'post_type',
213 'default' => 'forum'
214 );
215
216 /** Topic Section *****************************************************/
217
218 $this->field_map[] = array(
219 'to_type' => 'topic',
220 'to_fieldname' => 'post_status',
221 'default' => 'publish'
222 );
223 $this->field_map[] = array(
224 'to_type' => 'topic',
225 'to_fieldname' => 'comment_status',
226 'default' => 'closed'
227 );
228 $this->field_map[] = array(
229 'to_type' => 'topic',
230 'to_fieldname' => 'ping_status',
231 'default' => 'closed'
232 );
233 $this->field_map[] = array(
234 'to_type' => 'topic',
235 'to_fieldname' => 'post_type',
236 'default' => 'topic'
237 );
238
239 /** Post Section ******************************************************/
240
241 $this->field_map[] = array(
242 'to_type' => 'reply',
243 'to_fieldname' => 'post_status',
244 'default' => 'publish'
245 );
246 $this->field_map[] = array(
247 'to_type' => 'reply',
248 'to_fieldname' => 'comment_status',
249 'default' => 'closed'
250 );
251 $this->field_map[] = array(
252 'to_type' => 'reply',
253 'to_fieldname' => 'ping_status',
254 'default' => 'closed'
255 );
256 $this->field_map[] = array(
257 'to_type' => 'reply',
258 'to_fieldname' => 'post_type',
259 'default' => 'reply'
260 );
261
262 /** User Section ******************************************************/
263
264 $this->field_map[] = array(
265 'to_type' => 'user',
266 'to_fieldname' => 'role',
267 'default' => get_option( 'default_role' )
268 );
269 }
270
271 /**
272 * Setup global values
273 */
274 public function setup_globals() {}
275
276 /**
277 * Convert Forums
278 */
279 public function convert_forums( $start = 1 ) {
280 return $this->convert_table( 'forum', $start );
281 }
282
283 /**
284 * Convert Topics / Threads
285 */
286 public function convert_topics( $start = 1 ) {
287 return $this->convert_table( 'topic', $start );
288 }
289
290 /**
291 * Convert Posts
292 */
293 public function convert_replies( $start = 1 ) {
294 return $this->convert_table( 'reply', $start );
295 }
296
297 /**
298 * Convert Users
299 */
300 public function convert_users( $start = 1 ) {
301 return $this->convert_table( 'user', $start );
302 }
303
304 /**
305 * Convert Topic Tags
306 */
307 public function convert_tags( $start = 1 ) {
308 return $this->convert_table( 'tags', $start );
309 }
310
311 /**
312 * Convert Forum Subscriptions
313 */
314 public function convert_forum_subscriptions( $start = 1 ) {
315 return $this->convert_table( 'forum_subscriptions', $start );
316 }
317
318 /**
319 * Convert Topic Subscriptions
320 */
321 public function convert_topic_subscriptions( $start = 1 ) {
322 return $this->convert_table( 'topic_subscriptions', $start );
323 }
324
325 /**
326 * Convert Favorites
327 */
328 public function convert_favorites( $start = 1 ) {
329 return $this->convert_table( 'favorites', $start );
330 }
331
332 /**
333 * Convert Table
334 *
335 * @param string $to_type The destination type
336 * @param int $start Start row
337 */
338 public function convert_table( $to_type, $start ) {
339
340 // Set some defaults
341 $has_insert = false;
342 $from_tablename = '';
343 $field_list = $from_tables = $tablefield_array = array();
344
345 // Toggle Table Name based on $to_type (destination)
346 switch ( $to_type ) {
347 case 'user' :
348 $tablename = $this->wpdb->users;
349 break;
350
351 case 'tags' :
352 $tablename = '';
353 break;
354
355 case 'forum_subscriptions' :
356 $tablename = $this->wpdb->postmeta;
357 break;
358
359 case 'topic_subscriptions' :
360 $tablename = $this->wpdb->postmeta;
361 break;
362
363 case 'favorites' :
364 $tablename = $this->wpdb->postmeta;
365 break;
366
367 default :
368 $tablename = $this->wpdb->posts;
369 }
370
371 // Get the fields from the destination table
372 if ( ! empty( $tablename ) ) {
373 $tablefield_array = $this->get_fields( $tablename );
374 }
375
376 /** Step 1 ************************************************************/
377
378 // Loop through the field maps, and look for to_type matches
379 foreach ( $this->field_map as $item ) {
380
381 // Yay a match, and we have a from table, too
382 if ( ( $item['to_type'] === $to_type ) && ! empty( $item['from_tablename'] ) ) {
383
384 // $from_tablename was set from a previous loop iteration
385 if ( ! empty( $from_tablename ) ) {
386
387 // Doing some joining
388 if ( ! in_array( $item['from_tablename'], $from_tables, true ) && in_array( $item['join_tablename'], $from_tables, true ) ) {
389 $from_tablename .= ' ' . $item['join_type'] . ' JOIN ' . $this->opdb->prefix . $item['from_tablename'] . ' AS ' . $item['from_tablename'] . ' ' . $item['join_expression'];
390 }
391
392 // $from_tablename needs to be set
393 } else {
394 $from_tablename = $item['from_tablename'] . ' AS ' . $item['from_tablename'];
395 }
396
397 // Specific FROM expression data used
398 if ( ! empty( $item['from_expression'] ) ) {
399
400 // No 'WHERE' in expression
401 if ( stripos( $from_tablename, 'WHERE' ) === false ) {
402 $from_tablename .= ' ' . $item['from_expression'];
403
404 // 'WHERE' in expression, so replace with 'AND'
405 } else {
406 $from_tablename .= ' ' . str_replace( 'WHERE', 'AND', $item['from_expression'] );
407 }
408 }
409
410 // Add tablename and fieldname to arrays, formatted for querying
411 $from_tables[] = $item['from_tablename'];
412 $field_list[] = 'convert(' . $item['from_tablename'] . '.' . $item['from_fieldname'] . ' USING "' . $this->charset . '") AS ' . $item['from_fieldname'];
413 }
414 }
415
416 /** Step 2 ************************************************************/
417
418 // We have a $from_tablename, so we want to get some data to convert
419 if ( ! empty( $from_tablename ) ) {
420
421 // Update rows
422 $this->count_rows_by_table( "{$this->opdb->prefix}{$from_tablename}" );
423
424 // Get some data from the old forums
425 $field_list = array_unique( $field_list );
426 $fields = implode( ',', $field_list );
427 $forum_query = "SELECT {$fields} FROM {$this->opdb->prefix}{$from_tablename} LIMIT {$start}, {$this->max_rows}";
428
429 // Set this query as the last one ran
430 $this->update_query( $forum_query );
431
432 // Get results as an array
433 $forum_array = $this->opdb->get_results( $forum_query, ARRAY_A );
434
435 // Query returned some results
436 if ( ! empty( $forum_array ) ) {
437
438 // Loop through results
439 foreach ( (array) $forum_array as $forum ) {
440
441 // Reset some defaults
442 $insert_post = $insert_postmeta = $insert_data = array();
443
444 // Loop through field map, again...
445 foreach ( $this->field_map as $row ) {
446
447 // Types match and to_fieldname is present. This means
448 // we have some work to do here.
449 if ( ( $row['to_type'] === $to_type ) && isset( $row['to_fieldname'] ) ) {
450
451 // This row has a destination that matches one of the
452 // columns in this table.
453 if ( in_array( $row['to_fieldname'], $tablefield_array, true ) ) {
454
455 // Allows us to set default fields.
456 if ( isset( $row['default'] ) ) {
457 $insert_post[ $row['to_fieldname'] ] = $row['default'];
458
459 // Translates a field from the old forum.
460 } elseif ( isset( $row['callback_method'] ) ) {
461 if ( ( 'callback_userid' === $row['callback_method'] ) && ( false === $this->convert_users ) ) {
462 $insert_post[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
463 } else {
464 $insert_post[ $row['to_fieldname'] ] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[ $row['from_fieldname'] ], $forum ) );
465 }
466
467 // Maps the field from the old forum.
468 } else {
469 $insert_post[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
470 }
471
472 // Destination field is not empty, so we might need
473 // to do some extra work or set a default.
474 } elseif ( ! empty( $row['to_fieldname'] ) ) {
475
476 // Allows us to set default fields.
477 if ( isset( $row['default'] ) ) {
478 $insert_postmeta[ $row['to_fieldname'] ] = $row['default'];
479
480 // Translates a field from the old forum.
481 } elseif ( isset( $row['callback_method'] ) ) {
482 if ( ( 'callback_userid' === $row['callback_method'] ) && ( false === $this->convert_users ) ) {
483 $insert_postmeta[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
484 } else {
485 $insert_postmeta[ $row['to_fieldname'] ] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[ $row['from_fieldname'] ], $forum ) );
486 }
487
488 // Maps the field from the old forum.
489 } else {
490 $insert_postmeta[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
491 }
492 }
493 }
494 }
495
496 /** Step 3 ************************************************/
497
498 // Something to insert into the destination field
499 if ( count( $insert_post ) > 0 || ( 'tags' === $to_type && count( $insert_postmeta ) > 0 ) ) {
500
501 switch ( $to_type ) {
502
503 /** New user **************************************/
504
505 case 'user' :
506 if ( username_exists( $insert_post['user_login'] ) ) {
507 $insert_post['user_login'] = "imported_{$insert_post['user_login']}";
508 }
509
510 if ( email_exists( $insert_post['user_email'] ) ) {
511 $insert_post['user_email'] = "imported_{$insert_post['user_email']}";
512 }
513
514 if ( empty( $insert_post['user_pass'] ) ) {
515 $insert_post['user_pass'] = '';
516 }
517
518 // Internally re-calls _exists() checks above.
519 // Also checks for existing nicename.
520 $post_id = wp_insert_user( $insert_post );
521
522 if ( is_numeric( $post_id ) ) {
523 foreach ( $insert_postmeta as $key => $value ) {
524 add_user_meta( $post_id, $key, $value, true );
525
526 if ( '_id' === substr( $key, -3 ) && ( true === $this->sync_table ) ) {
527 $this->wpdb->insert(
528 $this->sync_table_name,
529 array(
530 'value_type' => 'user',
531 'value_id' => $post_id,
532 'meta_key' => $key,
533 'meta_value' => $value
534 )
535 );
536 }
537 }
538 }
539 break;
540
541 /** New Topic-Tag *********************************/
542
543 case 'tags' :
544 $post_id = wp_set_object_terms( $insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true );
545 $term = get_term_by( 'name', $insert_postmeta['name'], 'topic-tag');
546 if ( false !== $term ) {
547 wp_update_term(
548 $term->term_id,
549 'topic-tag',
550 array(
551 'description' => $insert_postmeta['description'],
552 'slug' => $insert_postmeta['slug']
553 )
554 );
555 }
556 break;
557
558 /** Forum Subscriptions ***************************/
559
560 case 'forum_subscriptions' :
561 $user_id = $insert_post['user_id'];
562 $items = wp_list_pluck( $insert_postmeta, '_bbp_forum_subscriptions' );
563 if ( is_numeric( $user_id ) && ! empty( $items ) ) {
564 foreach ( $items as $value ) {
565
566 // Maybe string with commas
567 $value = is_string( $value )
568 ? explode( ',', $value )
569 : (array) $value;
570
571 // Add user ID to forums subscribed users
572 foreach ( $value as $fav ) {
573 bbp_add_user_subscription( $user_id, $this->callback_forumid( $fav ) );
574 }
575 }
576 }
577 break;
578
579 /** Subscriptions *********************************/
580
581 case 'topic_subscriptions' :
582 $user_id = $insert_post['user_id'];
583 $items = wp_list_pluck( $insert_postmeta, '_bbp_subscriptions' );
584 if ( is_numeric( $user_id ) && ! empty( $items ) ) {
585 foreach ( $items as $value ) {
586
587 // Maybe string with commas
588 $value = is_string( $value )
589 ? explode( ',', $value )
590 : (array) $value;
591
592 // Add user ID to topics subscribed users
593 foreach ( $value as $fav ) {
594 bbp_add_user_subscription( $user_id, $this->callback_topicid( $fav ) );
595 }
596 }
597 }
598 break;
599
600 /** Favorites *************************************/
601
602 case 'favorites' :
603 $user_id = $insert_post['user_id'];
604 $items = wp_list_pluck( $insert_postmeta, '_bbp_favorites' );
605 if ( is_numeric( $user_id ) && ! empty( $items ) ) {
606 foreach ( $items as $value ) {
607
608 // Maybe string with commas
609 $value = is_string( $value )
610 ? explode( ',', $value )
611 : (array) $value;
612
613 // Add user ID to topics favorited users
614 foreach ( $value as $fav ) {
615 bbp_add_user_favorite( $user_id, $this->callback_topicid( $fav ) );
616 }
617 }
618 }
619 break;
620
621 /** Forum, Topic, Reply ***************************/
622
623 default :
624 $post_id = wp_insert_post( $insert_post, true );
625
626 if ( is_numeric( $post_id ) ) {
627 foreach ( $insert_postmeta as $key => $value ) {
628 add_post_meta( $post_id, $key, $value, true );
629
630 /**
631 * If we are using the sync_table add
632 * the meta '_id' keys to the table
633 *
634 * Forums: _bbp_old_forum_id // The old forum ID
635 * _bbp_old_forum_parent_id // The old forum parent ID
636 *
637 * Topics: _bbp_forum_id // The new forum ID
638 * _bbp_old_topic_id // The old topic ID
639 * _bbp_old_closed_status_id // The old topic open/closed status
640 * _bbp_old_sticky_status_id // The old topic sticky status
641 *
642 * Replies: _bbp_forum_id // The new forum ID
643 * _bbp_topic_id // The new topic ID
644 * _bbp_old_reply_id // The old reply ID
645 * _bbp_old_reply_to_id // The old reply to ID
646 */
647 if ( '_id' === substr( $key, -3 ) && ( true === $this->sync_table ) ) {
648 $this->wpdb->insert(
649 $this->sync_table_name,
650 array(
651 'value_type' => 'post',
652 'value_id' => $post_id,
653 'meta_key' => $key,
654 'meta_value' => $value
655 )
656 );
657 }
658
659 /**
660 * Replies need to save their old reply_to ID for
661 * hierarchical replies association. Later we update
662 * the _bbp_reply_to value with the new bbPress
663 * value using convert_reply_to_parents()
664 */
665 if ( ( 'reply' === $to_type ) && ( '_bbp_old_reply_to_id' === $key ) ) {
666 add_post_meta( $post_id, '_bbp_reply_to', $value );
667 }
668 }
669 }
670 break;
671 }
672 $has_insert = true;
673 }
674 }
675 }
676 }
677
678 return ! $has_insert;
679 }
680
681 /**
682 * This method converts old forum hierarchy to new bbPress hierarchy.
683 */
684 public function convert_forum_parents( $start = 1 ) {
685 $has_update = false;
686 $query = ! empty( $this->sync_table )
687 ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_forum_parent_id' )
688 : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_forum_parent_id' );
689
690 foreach ( $this->count_rows_by_results( $query ) as $row ) {
691 $parent_id = $this->callback_forumid( $row->meta_value );
692 $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} SET post_parent = %d WHERE ID = %d LIMIT 1", $parent_id, $row->value_id ) );
693 $has_update = true;
694 }
695
696 return ! $has_update;
697 }
698
699 /**
700 * This method converts old topic stickies to new bbPress stickies.
701 *
702 * @since 2.5.0 bbPress (r5170)
703 */
704 public function convert_topic_stickies( $start = 1 ) {
705 $has_update = false;
706 $query = ! empty( $this->sync_table )
707 ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'sticky' )
708 : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'sticky' );
709
710 foreach ( $this->count_rows_by_results( $query ) as $row ) {
711 bbp_stick_topic( $row->value_id );
712 $has_update = true;
713 }
714
715 return ! $has_update;
716 }
717
718 /**
719 * This method converts old topic super stickies to new bbPress super stickies.
720 *
721 * @since 2.5.0 bbPress (r5170)
722 */
723 public function convert_topic_super_stickies( $start = 1 ) {
724 $has_update = false;
725 $query = ! empty( $this->sync_table )
726 ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'super-sticky' )
727 : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'super-sticky' );
728
729 foreach ( $this->count_rows_by_results( $query ) as $row ) {
730 $super = true;
731 bbp_stick_topic( $row->value_id, $super );
732 $has_update = true;
733 }
734
735 return ! $has_update;
736 }
737
738 /**
739 * This method converts old closed topics to bbPress closed topics.
740 *
741 * @since 2.6.0 bbPress (r5425)
742 */
743 public function convert_topic_closed_topics( $start = 1 ) {
744 $has_update = false;
745 $query = ! empty( $this->sync_table )
746 ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_closed_status_id', 'closed' )
747 : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_closed_status_id', 'closed' );
748
749 foreach ( $this->count_rows_by_results( $query ) as $row ) {
750 bbp_close_topic( $row->value_id );
751 $has_update = true;
752 }
753
754 return ! $has_update;
755 }
756
757 /**
758 * This method converts old reply_to post id to new bbPress reply_to post id.
759 *
760 * @since 2.4.0 bbPress (r5093)
761 */
762 public function convert_reply_to_parents( $start = 1 ) {
763 $has_update = false;
764 $query = ! empty( $this->sync_table )
765 ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_reply_to_id' )
766 : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_reply_to_id' );
767
768 foreach ( $this->count_rows_by_results( $query ) as $row ) {
769 $reply_to = $this->callback_reply_to( $row->meta_value );
770 $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND post_id = %d LIMIT 1", $reply_to, '_bbp_reply_to', $row->value_id ) );
771 $has_update = true;
772 }
773
774 return ! $has_update;
775 }
776
777 /**
778 * This method converts anonymous topics.
779 *
780 * @since 2.6.0 bbPress (r5538)
781 */
782 public function convert_anonymous_topic_authors( $start = 1 ) {
783
784 $has_update = false;
785
786 if ( ! empty( $this->sync_table ) ) {
787 $query = $this->wpdb->prepare( "SELECT sync_table1.value_id AS topic_id, sync_table1.meta_value AS topic_is_anonymous, sync_table2.meta_value AS topic_author
788 FROM {$this->sync_table_name} AS sync_table1
789 INNER JOIN {$this->sync_table_name} AS sync_table2
790 ON ( sync_table1.value_id = sync_table2.value_id )
791 WHERE sync_table1.meta_value = %s
792 AND sync_table2.meta_key = %s
793 LIMIT {$start}, {$this->max_rows}",
794 'true',
795 '_bbp_old_topic_author_name_id'
796 );
797 } else {
798 $query = $this->wpdb->prepare( "SELECT wp_postmeta1.post_id AS topic_id, wp_postmeta1.meta_value AS topic_is_anonymous, wp_postmeta2.meta_value AS topic_author
799 FROM {$this->wpdb->postmeta} AS wp_postmeta1
800 INNER JOIN {$this->wpdb->postmeta} AS wp_postmeta2
801 ON ( wp_postmeta1.post_id = wp_postmeta2.post_id )
802 WHERE wp_postmeta1.meta_value = %s
803 AND wp_postmeta2.meta_key = %s
804 LIMIT {$start}, {$this->max_rows}",
805 'true',
806 '_bbp_old_topic_author_name_id'
807 );
808 }
809
810 foreach ( $this->count_rows_by_results( $query ) as $row ) {
811 $anonymous_topic_author_id = 0;
812 $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} SET post_author = %d WHERE ID = %d LIMIT 1", $anonymous_topic_author_id, $row->topic_id ) );
813
814 add_post_meta( $row->topic_id, '_bbp_anonymous_name', $row->topic_author );
815
816 $has_update = true;
817 }
818
819 return ! $has_update;
820 }
821
822 /**
823 * This method converts anonymous replies.
824 *
825 * @since 2.6.0 bbPress (r5538)
826 */
827 public function convert_anonymous_reply_authors( $start = 1 ) {
828
829 $has_update = false;
830
831 if ( ! empty( $this->sync_table ) ) {
832 $query = $this->wpdb->prepare( "SELECT sync_table1.value_id AS reply_id, sync_table1.meta_value AS reply_is_anonymous, sync_table2.meta_value AS reply_author
833 FROM {$this->sync_table_name} AS sync_table1
834 INNER JOIN {$this->sync_table_name} AS sync_table2
835 ON ( sync_table1.value_id = sync_table2.value_id )
836 WHERE sync_table1.meta_value = %s
837 AND sync_table2.meta_key = %s
838 LIMIT {$start}, {$this->max_rows}",
839 'true',
840 '_bbp_old_reply_author_name_id'
841 );
842 } else {
843 $query = $this->wpdb->prepare( "SELECT wp_postmeta1.post_id AS reply_id, wp_postmeta1.meta_value AS reply_is_anonymous, wp_postmeta2.meta_value AS reply_author
844 FROM {$this->wpdb->postmeta} AS wp_postmeta1
845 INNER JOIN {$this->wpdb->postmeta} AS wp_postmeta2
846 ON ( wp_postmeta1.post_id = wp_postmeta2.post_id )
847 WHERE wp_postmeta1.meta_value = %s
848 AND wp_postmeta2.meta_key = %s
849 LIMIT {$start}, {$this->max_rows}",
850 'true',
851 '_bbp_old_reply_author_name_id'
852 );
853 }
854
855 foreach ( $this->count_rows_by_results( $query ) as $row ) {
856 $anonymous_reply_author_id = 0;
857 $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} SET post_author = %d WHERE ID = %d LIMIT 1", $anonymous_reply_author_id, $row->reply_id ) );
858
859 add_post_meta( $row->reply_id, '_bbp_anonymous_name', $row->reply_author );
860
861 $has_update = true;
862 }
863
864 return ! $has_update;
865 }
866
867 /**
868 * This method deletes data from the wp database.
869 *
870 * @since 2.6.0 bbPress (r6456)
871 */
872 public function clean() {
873
874 // Defaults
875 $has_delete = false;
876
877 /** Delete topics/forums/posts ****************************************/
878
879 $esc_like = $this->wpdb->esc_like( '_bbp_' ) . '%';
880 $query = ! empty( $this->sync_table )
881 ? $this->wpdb->prepare( "SELECT value_id FROM {$this->sync_table_name} INNER JOIN {$this->wpdb->posts} ON(value_id = ID) WHERE meta_key LIKE %s AND value_type = %s GROUP BY value_id ORDER BY value_id DESC LIMIT {$this->max_rows}", $esc_like, 'post' )
882 : $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key LIKE %s GROUP BY post_id ORDER BY post_id DESC LIMIT {$this->max_rows}", $esc_like );
883
884 $posts = $this->get_results( $query, ARRAY_A );
885
886 if ( isset( $posts[0] ) && ! empty( $posts[0]['value_id'] ) ) {
887 foreach ( (array) $posts as $value ) {
888 $deleted = wp_delete_post( $value['value_id'], true );
889
890 // Only flag if not empty or error
891 if ( ( false === $has_delete ) && ! empty( $deleted ) && ! is_wp_error( $deleted ) ) {
892 $has_delete = true;
893 }
894 }
895 }
896
897 /** Delete users ******************************************************/
898
899 $query = ! empty( $this->sync_table )
900 ? $this->wpdb->prepare( "SELECT value_id FROM {$this->sync_table_name} INNER JOIN {$this->wpdb->users} ON(value_id = ID) WHERE meta_key = %s AND value_type = %s LIMIT {$this->max_rows}", '_bbp_old_user_id', 'user' )
901 : $this->wpdb->prepare( "SELECT user_id AS value_id FROM {$this->wpdb->usermeta} WHERE meta_key = %s LIMIT {$this->max_rows}", '_bbp_old_user_id' );
902
903 $users = $this->get_results( $query, ARRAY_A );
904
905 if ( ! empty( $users ) ) {
906 foreach ( $users as $value ) {
907 $deleted = wp_delete_user( $value['value_id'] );
908
909 // Only flag if not empty or error
910 if ( ( false === $has_delete ) && ! empty( $deleted ) && ! is_wp_error( $deleted ) ) {
911 $has_delete = true;
912 }
913 }
914 }
915
916 unset( $posts );
917 unset( $users );
918
919 return ! $has_delete;
920 }
921
922 /**
923 * This method deletes passwords from the wp database.
924 *
925 * @param int $start Start row
926 */
927 public function clean_passwords( $start = 1 ) {
928 $has_delete = false;
929 $query = $this->wpdb->prepare( "SELECT user_id, meta_value FROM {$this->wpdb->usermeta} WHERE meta_key = %s LIMIT {$start}, {$this->max_rows}", '_bbp_password' );
930 $converted = $this->get_results( $query, ARRAY_A );
931
932 if ( ! empty( $converted ) ) {
933 foreach ( $converted as $value ) {
934 if ( is_serialized( $value['meta_value'] ) ) {
935 $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->users} SET user_pass = '' WHERE ID = %d", $value['user_id'] ) );
936 } else {
937 $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->users} SET user_pass = %s WHERE ID = %d", $value['meta_value'], $value['user_id'] ) );
938 $this->query( $this->wpdb->prepare( "DELETE FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d", '_bbp_password', $value['user_id'] ) );
939 }
940 }
941 $has_delete = true;
942 }
943
944 return ! $has_delete;
945 }
946
947 /**
948 * This method implements the authentication for the different forums.
949 *
950 * @param string $password Password.
951 * @param string $hash Password hash.
952 */
953 abstract protected function authenticate_pass( $password, $hash );
954
955 /**
956 * Info
957 */
958 abstract protected function info();
959
960 /**
961 * This method grabs appropriate fields from the table specified
962 *
963 * @param string $tablename The table name to grab fields from
964 */
965 private function get_fields( $tablename = '' ) {
966 $retval = array();
967 $field_array = $this->get_results( "DESCRIBE {$tablename}", ARRAY_A );
968
969 // Bail if no fields
970 if ( empty( $field_array ) ) {
971 return $retval;
972 }
973
974 // Add fields to array
975 foreach ( $field_array as $field ) {
976 if ( ! empty( $field['Field'] ) ) {
977 $retval[] = $field['Field'];
978 }
979 }
980
981 // Add social fields for users table
982 if ( $tablename === $this->wpdb->users ) {
983 $retval[] = 'role';
984 $retval[] = 'yim';
985 $retval[] = 'aim';
986 $retval[] = 'jabber';
987 }
988
989 return $retval;
990 }
991
992 /** Database Wrappers *****************************************************/
993
994 /**
995 * Update the last query option and return results
996 *
997 * @param string $query
998 */
999 private function get_row( $query = '' ) {
1000 $this->update_query( $query );
1001
1002 return $this->wpdb->get_row( $query ); // phpcs:ignore
1003 }
1004
1005 /**
1006 * Update the last query option and return results
1007 *
1008 * @param string $query
1009 * @param string $output
1010 */
1011 private function get_results( $query = '', $output = OBJECT ) {
1012 $this->update_query( $query );
1013
1014 return (array) $this->wpdb->get_results( $query, $output ); // phpcs:ignore
1015 }
1016
1017 /**
1018 * Update the last query option and do a general query
1019 *
1020 * @param string $query
1021 */
1022 private function query( $query = '' ) {
1023 $this->update_query( $query );
1024
1025 return $this->wpdb->query( $query ); // phpcs:ignore
1026 }
1027
1028 /**
1029 * Update the last query ran
1030 *
1031 * @since 2.6.0 bbPress (r6637)
1032 *
1033 * @param string $query The literal MySQL query
1034 * @return bool
1035 */
1036 private function update_query( $query = '' ) {
1037 return update_option( '_bbp_converter_query', $query );
1038 }
1039
1040 /**
1041 * Update the number of rows in the current step
1042 *
1043 * @since 2.6.0 bbPress (r6637)
1044 *
1045 * @param string $query The literal MySQL query
1046 * @return array
1047 */
1048 private function count_rows_by_results( $query = '' ) {
1049 $results = $this->get_results( $query );
1050
1051 update_option( '_bbp_converter_rows_in_step', count( $results ) );
1052
1053 return $results;
1054 }
1055
1056 /**
1057 * Update the number of rows in the current step
1058 *
1059 * @since 2.6.0 bbPress (r6637)
1060 *
1061 * @param string $table_name The literal MySQL query
1062 * @return bool
1063 */
1064 private function count_rows_by_table( $table_name = '' ) {
1065 $count = (int) $this->opdb->get_var( "SELECT COUNT(*) FROM {$table_name}" );
1066
1067 return update_option( '_bbp_converter_rows_in_step', $count );
1068 }
1069
1070 /** Callbacks *************************************************************/
1071
1072 /**
1073 * Run password through wp_hash_password()
1074 *
1075 * @param string $username
1076 * @param string $password
1077 */
1078 public function callback_pass( $username = '', $password = '' ) {
1079
1080 // Get user – Bail if not found
1081 $user = $this->get_row( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->users} WHERE user_login = %s AND user_pass = '' LIMIT 1", $username ) );
1082 if ( empty( $user ) ) {
1083 return;
1084 }
1085
1086 // Get usermeta – Bail if not found
1087 $usermeta = $this->get_row( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d LIMIT 1", '_bbp_password', $user->ID ) );
1088 if ( empty( $usermeta ) ) {
1089 return;
1090 }
1091
1092 // Bail if meta is not a string
1093 if ( empty( $usermeta->meta_value ) || ! is_serialized( $usermeta->meta_value ) ) {
1094 return;
1095 }
1096
1097 // Bail if meta is suspiciously long
1098 if ( strlen( $usermeta->meta_value ) > 512 ) {
1099 return;
1100 }
1101
1102 // Bail if platform-specific auth fails
1103 if ( ! $this->authenticate_pass( $password, $usermeta->meta_value ) ) {
1104 return;
1105 }
1106
1107 // Hash the password
1108 $new_pass = wp_hash_password( $password );
1109
1110 // Update
1111 $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->users} SET user_pass = %s WHERE ID = %d", $new_pass, $user->ID ) );
1112
1113 // Clean up
1114 unset( $new_pass );
1115 $this->query( $this->wpdb->prepare( "DELETE FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d", '_bbp_password', $user->ID ) );
1116 $this->query( $this->wpdb->prepare( "DELETE FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d", '_bbp_class', $user->ID ) );
1117
1118 // Clean the cache for this user since their password was
1119 // upgraded from the old platform to the new.
1120 clean_user_cache( $user->ID );
1121 }
1122
1123 /**
1124 * A mini cache system to reduce database calls to forum ID's
1125 *
1126 * @param string $field
1127 * @return string
1128 */
1129 private function callback_forumid( $field ) {
1130 if ( ! isset( $this->map_forumid[ $field ] ) ) {
1131 $row = ! empty( $this->sync_table )
1132 ? $this->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_forum_id', $field ) )
1133 : $this->get_row( $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_forum_id', $field ) );
1134
1135 $this->map_forumid[ $field ] = ! is_null( $row )
1136 ? $row->value_id
1137 : 0;
1138 }
1139
1140 return $this->map_forumid[ $field ];
1141 }
1142
1143 /**
1144 * A mini cache system to reduce database calls to topic ID's
1145 *
1146 * @param string $field
1147 * @return string
1148 */
1149 private function callback_topicid( $field ) {
1150 if ( ! isset( $this->map_topicid[ $field ] ) ) {
1151 $row = ! empty( $this->sync_table )
1152 ? $this->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_topic_id', $field ) )
1153 : $this->get_row( $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_topic_id', $field ) );
1154
1155 $this->map_topicid[ $field ] = ! is_null( $row )
1156 ? $row->value_id
1157 : 0;
1158 }
1159
1160 return $this->map_topicid[ $field ];
1161 }
1162
1163 /**
1164 * A mini cache system to reduce database calls to reply_to post id.
1165 *
1166 * @since 2.4.0 bbPress (r5093)
1167 *
1168 * @param string $field
1169 * @return string
1170 */
1171 private function callback_reply_to( $field ) {
1172 if ( ! isset( $this->map_reply_to[ $field ] ) ) {
1173 $row = ! empty( $this->sync_table )
1174 ? $this->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_reply_id', $field ) )
1175 : $this->get_row( $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_reply_id', $field ) );
1176
1177 $this->map_reply_to[ $field ] = ! is_null( $row )
1178 ? $row->value_id
1179 : 0;
1180 }
1181
1182 return $this->map_reply_to[ $field ];
1183 }
1184
1185 /**
1186 * A mini cache system to reduce database calls to user ID's
1187 *
1188 * @param string $field
1189 * @return string
1190 */
1191 private function callback_userid( $field ) {
1192 if ( ! isset( $this->map_userid[ $field ] ) ) {
1193 $row = ! empty( $this->sync_table )
1194 ? $this->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_user_id', $field ) )
1195 : $this->get_row( $this->wpdb->prepare( "SELECT user_id AS value_id FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_user_id', $field ) );
1196
1197 if ( ! is_null( $row ) ) {
1198 $this->map_userid[ $field ] = $row->value_id;
1199 } else {
1200 $this->map_userid[ $field ] = ( true === $this->convert_users )
1201 ? 0
1202 : $field;
1203 }
1204 }
1205
1206 return $this->map_userid[ $field ];
1207 }
1208
1209 /**
1210 * Check if the topic or reply author is anonymous
1211 *
1212 * @since 2.6.0 bbPress (r5544)
1213 *
1214 * @param string $field
1215 * @return string
1216 */
1217 private function callback_check_anonymous( $field ) {
1218 $field = ( $this->callback_userid( $field ) == 0 )
1219 ? 'true'
1220 : 'false';
1221
1222 return $field;
1223 }
1224
1225 /**
1226 * A mini cache system to reduce database calls map topics ID's to forum ID's
1227 *
1228 * @param string $field
1229 * @return string
1230 */
1231 private function callback_topicid_to_forumid( $field ) {
1232 $topicid = $this->callback_topicid( $field );
1233 if ( empty( $topicid ) ) {
1234 $this->map_topicid_to_forumid[ $topicid ] = 0;
1235 } elseif ( ! isset( $this->map_topicid_to_forumid[ $topicid ] ) ) {
1236 $row = $this->get_row( $this->wpdb->prepare( "SELECT post_parent FROM {$this->wpdb->posts} WHERE ID = %d LIMIT 1", $topicid ) );
1237
1238 $this->map_topicid_to_forumid[ $topicid ] = ! is_null( $row )
1239 ? $row->post_parent
1240 : 0;
1241 }
1242
1243 return $this->map_topicid_to_forumid[ $topicid ];
1244 }
1245
1246 protected function callback_slug( $field ) {
1247 return sanitize_title( $field );
1248 }
1249
1250 protected function callback_negative( $field ) {
1251 return ( $field < 0 )
1252 ? 0
1253 : $field;
1254 }
1255
1256 protected function callback_html( $field ) {
1257 require_once bbp_admin()->admin_dir . 'parser.php';
1258
1259 // Setup the BBCode parser
1260 $bbcode = BBCode::getInstance();
1261
1262 // Pass BBCode properties to the parser
1263 foreach ( $this->bbcode_parser_properties as $prop => $value ) {
1264 $bbcode->{$prop} = $value;
1265 }
1266
1267 return html_entity_decode( $bbcode->Parse( $field ) );
1268 }
1269
1270 protected function callback_null( $field ) {
1271 return is_null( $field )
1272 ? ''
1273 : $field;
1274 }
1275
1276 protected function callback_datetime( $field ) {
1277 // phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date
1278 return is_numeric( $field )
1279 ? date( 'Y-m-d H:i:s', $field )
1280 : date( 'Y-m-d H:i:s', strtotime( $field ) );
1281 // phpcs:enable
1282 }
1283 }
1284 endif;
1285