BBP_Converter_Base::convert_table( $to_type, $start )
Convert Table
Description Description
Parameters Parameters
-
(Required) Start row
Source Source
File: includes/admin/classes/class-bbp-converter-base.php
public function convert_table( $to_type, $start ) { // Set some defaults $has_insert = false; $from_tablename = ''; $field_list = $from_tables = $tablefield_array = array(); // Toggle Table Name based on $to_type (destination) switch ( $to_type ) { case 'user' : $tablename = $this->wpdb->users; break; case 'tags' : $tablename = ''; break; case 'forum_subscriptions' : $tablename = $this->wpdb->postmeta; break; case 'topic_subscriptions' : $tablename = $this->wpdb->postmeta; break; case 'favorites' : $tablename = $this->wpdb->postmeta; break; default : $tablename = $this->wpdb->posts; } // Get the fields from the destination table if ( ! empty( $tablename ) ) { $tablefield_array = $this->get_fields( $tablename ); } /** Step 1 ************************************************************/ // Loop through the field maps, and look for to_type matches foreach ( $this->field_map as $item ) { // Yay a match, and we have a from table, too if ( ( $item['to_type'] === $to_type ) && ! empty( $item['from_tablename'] ) ) { // $from_tablename was set from a previous loop iteration if ( ! empty( $from_tablename ) ) { // Doing some joining if ( ! in_array( $item['from_tablename'], $from_tables, true ) && in_array( $item['join_tablename'], $from_tables, true ) ) { $from_tablename .= ' ' . $item['join_type'] . ' JOIN ' . $this->opdb->prefix . $item['from_tablename'] . ' AS ' . $item['from_tablename'] . ' ' . $item['join_expression']; } // $from_tablename needs to be set } else { $from_tablename = $item['from_tablename'] . ' AS ' . $item['from_tablename']; } // Specific FROM expression data used if ( ! empty( $item['from_expression'] ) ) { // No 'WHERE' in expression if ( stripos( $from_tablename, "WHERE" ) === false ) { $from_tablename .= ' ' . $item['from_expression']; // 'WHERE' in expression, so replace with 'AND' } else { $from_tablename .= ' ' . str_replace( "WHERE", "AND", $item['from_expression'] ); } } // Add tablename and fieldname to arrays, formatted for querying $from_tables[] = $item['from_tablename']; $field_list[] = 'convert(' . $item['from_tablename'] . '.' . $item['from_fieldname'] . ' USING "' . $this->charset . '") AS ' . $item['from_fieldname']; } } /** Step 2 ************************************************************/ // We have a $from_tablename, so we want to get some data to convert if ( ! empty( $from_tablename ) ) { // Update rows $this->count_rows_by_table( "{$this->opdb->prefix}{$from_tablename}" ); // Get some data from the old forums $field_list = array_unique( $field_list ); $fields = implode( ',', $field_list ); $forum_query = "SELECT {$fields} FROM {$this->opdb->prefix}{$from_tablename} LIMIT {$start}, {$this->max_rows}"; // Set this query as the last one ran $this->update_query( $forum_query ); // Get results as an array $forum_array = $this->opdb->get_results( $forum_query, ARRAY_A ); // Query returned some results if ( ! empty( $forum_array ) ) { // Loop through results foreach ( (array) $forum_array as $forum ) { // Reset some defaults $insert_post = $insert_postmeta = $insert_data = array(); // Loop through field map, again... foreach ( $this->field_map as $row ) { // Types match and to_fieldname is present. This means // we have some work to do here. if ( ( $row['to_type'] === $to_type ) && isset( $row['to_fieldname'] ) ) { // This row has a destination that matches one of the // columns in this table. if ( in_array( $row['to_fieldname'], $tablefield_array, true ) ) { // Allows us to set default fields. if ( isset( $row['default'] ) ) { $insert_post[ $row['to_fieldname'] ] = $row['default']; // Translates a field from the old forum. } elseif ( isset( $row['callback_method'] ) ) { if ( ( 'callback_userid' === $row['callback_method'] ) && ( false === $this->convert_users ) ) { $insert_post[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ]; } else { $insert_post[ $row['to_fieldname'] ] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[ $row['from_fieldname'] ], $forum ) ); } // Maps the field from the old forum. } else { $insert_post[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ]; } // Destination field is not empty, so we might need // to do some extra work or set a default. } elseif ( ! empty( $row['to_fieldname'] ) ) { // Allows us to set default fields. if ( isset( $row['default'] ) ) { $insert_postmeta[ $row['to_fieldname'] ] = $row['default']; // Translates a field from the old forum. } elseif ( isset( $row['callback_method'] ) ) { if ( ( $row['callback_method'] === 'callback_userid' ) && ( false === $this->convert_users ) ) { $insert_postmeta[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ]; } else { $insert_postmeta[ $row['to_fieldname'] ] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[ $row['from_fieldname'] ], $forum ) ); } // Maps the field from the old forum. } else { $insert_postmeta[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ]; } } } } /** Step 3 ************************************************/ // Something to insert into the destination field if ( count( $insert_post ) > 0 || ( $to_type == 'tags' && count( $insert_postmeta ) > 0 ) ) { switch ( $to_type ) { /** New user **************************************/ case 'user' : if ( username_exists( $insert_post['user_login'] ) ) { $insert_post['user_login'] = "imported_{$insert_post['user_login']}"; } if ( email_exists( $insert_post['user_email'] ) ) { $insert_post['user_email'] = "imported_{$insert_post['user_email']}"; } if ( empty( $insert_post['user_pass'] ) ) { $insert_post['user_pass'] = ''; } // Internally re-calls _exists() checks above. // Also checks for existing nicename. $post_id = wp_insert_user( $insert_post ); if ( is_numeric( $post_id ) ) { foreach ( $insert_postmeta as $key => $value ) { add_user_meta( $post_id, $key, $value, true ); if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) { $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'user', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) ); } } } break; /** New Topic-Tag *********************************/ case 'tags' : $post_id = wp_set_object_terms( $insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true ); $term = get_term_by( 'name', $insert_postmeta['name'], 'topic-tag'); if ( false !== $term ) { wp_update_term( $term->term_id, 'topic-tag', array( 'description' => $insert_postmeta['description'], 'slug' => $insert_postmeta['slug'] ) ); } break; /** Forum Subscriptions ***************************/ case 'forum_subscriptions' : $user_id = $insert_post['user_id']; $items = wp_list_pluck( $insert_postmeta, '_bbp_forum_subscriptions' ); if ( is_numeric( $user_id ) && ! empty( $items ) ) { foreach ( $items as $value ) { // Maybe string with commas $value = is_string( $value ) ? explode( ',', $value ) : (array) $value; // Add user ID to forums subscribed users foreach ( $value as $fav ) { bbp_add_user_subscription( $user_id, $this->callback_forumid( $fav ) ); } } } break; /** Subscriptions *********************************/ case 'topic_subscriptions' : $user_id = $insert_post['user_id']; $items = wp_list_pluck( $insert_postmeta, '_bbp_subscriptions' ); if ( is_numeric( $user_id ) && ! empty( $items ) ) { foreach ( $items as $value ) { // Maybe string with commas $value = is_string( $value ) ? explode( ',', $value ) : (array) $value; // Add user ID to topics subscribed users foreach ( $value as $fav ) { bbp_add_user_subscription( $user_id, $this->callback_topicid( $fav ) ); } } } break; /** Favorites *************************************/ case 'favorites' : $user_id = $insert_post['user_id']; $items = wp_list_pluck( $insert_postmeta, '_bbp_favorites' ); if ( is_numeric( $user_id ) && ! empty( $items ) ) { foreach ( $items as $value ) { // Maybe string with commas $value = is_string( $value ) ? explode( ',', $value ) : (array) $value; // Add user ID to topics favorited users foreach ( $value as $fav ) { bbp_add_user_favorite( $user_id, $this->callback_topicid( $fav ) ); } } } break; /** Forum, Topic, Reply ***************************/ default : $post_id = wp_insert_post( $insert_post, true ); if ( is_numeric( $post_id ) ) { foreach ( $insert_postmeta as $key => $value ) { add_post_meta( $post_id, $key, $value, true ); /** * If we are using the sync_table add * the meta '_id' keys to the table * * Forums: _bbp_old_forum_id // The old forum ID * _bbp_old_forum_parent_id // The old forum parent ID * * Topics: _bbp_forum_id // The new forum ID * _bbp_old_topic_id // The old topic ID * _bbp_old_closed_status_id // The old topic open/closed status * _bbp_old_sticky_status_id // The old topic sticky status * * Replies: _bbp_forum_id // The new forum ID * _bbp_topic_id // The new topic ID * _bbp_old_reply_id // The old reply ID * _bbp_old_reply_to_id // The old reply to ID */ if ( '_id' === substr( $key, -3 ) && ( true === $this->sync_table ) ) { $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) ); } /** * Replies need to save their old reply_to ID for * hierarchical replies association. Later we update * the _bbp_reply_to value with the new bbPress * value using convert_reply_to_parents() */ if ( ( 'reply' === $to_type ) && ( '_bbp_old_reply_to_id' === $key ) ) { add_post_meta( $post_id, '_bbp_reply_to', $value ); } } } break; } $has_insert = true; } } } } return ! $has_insert; }