AP_Update_Helper::migrate_post_data()

Description #

Migrate question and answer.

Source #

File: admin/update.php

	public function migrate_post_data() {
		$tasks = $this->get_tasks();

		if ( $tasks['post_data'] ) {
			return;
		}

		global $wpdb;
		$done = (int) get_option( 'anspress_updated_q_offset', 0 );
		$ids = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS ID, post_type, post_parent, post_status FROM {$wpdb->posts} LEFT JOIN {$wpdb->ap_qameta} ON post_id = ID WHERE ptype IS NULL AND post_type IN ('question', 'answer') LIMIT {$done},50" );
		$total_ids = $wpdb->get_var( 'SELECT FOUND_ROWS()' ); // DB call okay, Db cache okay.

		if ( empty( $ids ) ) {
			$options = get_option( 'anspress_updates', [] );
			$options['post_data'] = true;
			update_option( 'anspress_updates', $options );
			$this->send( true, 'post_data', __( 'Post data updated successfully', 'anspress-question-answer' ), true );
		}

		// Update answers count.
		foreach ( (array) $ids as $_post ) {
			if ( 'question' === $_post->post_type ) {
				$this->update_answers_count( $_post );
				// Delete post meta.
				delete_post_meta( $_post->ID, '_ap_answers' );
				$this->migrate_views( $_post->ID );
				$this->subscribers( $_post->ID );
				$this->change_closed_status( $_post );
				$this->best_answers( $_post );
				ap_update_qameta_terms( $_post->ID );
				delete_post_meta( $_post->ID, '_ap_participants' );
			}

			if ( 'answer' === $_post->post_type ) {
				delete_post_meta( $_post->ID, '_ap_best_answer' );
			}

			$this->flags( $_post->ID );
			$this->migrate_votes( $_post->ID );
			$this->post_activities( $_post );
			$this->restore_last_updated( $_post );
		}

		$done = $done + count( $ids );
		update_option( 'anspress_updated_q_offset', $done );
		$this->send( true, 'post_data', sprintf( __( 'Updated %1$d posts out of %2$d', 'anspress-question-answer' ), count( $done ), $total_ids ), true );
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment