AnsPress_Upgrader::migrate_reputations()

Description #

Migrate migration data to new table.

Source #

File: lib/class-anspress-upgrader.php

	public function migrate_reputations() {
		if ( ! $this->meta_table_exists ) {
			esc_attr_e( 'Successfully migrated all reputations', 'anspress-question-answer' );
			return;
		}

		global $wpdb;
		$old_reputations = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}ap_meta WHERE apmeta_type = 'reputation'" ); // phpcs:ignore WordPress.DB

		if ( empty( $old_reputations ) ) {
			esc_attr_e( 'Successfully migrated all reputations', 'anspress-question-answer' );
			return;
		}

		$apmeta_to_delete = array();

		foreach ( (array) $old_reputations as $rep ) {
			$event = $this->replace_old_reputation_event( $rep->apmeta_param );
			ap_insert_reputation( $event, $rep->apmeta_actionid, $rep->apmeta_userid );
			$apmeta_to_delete[] = $rep->apmeta_id;

			// Delete user meta.
			delete_user_meta( $rep->apmeta_userid, 'ap_reputation' ); // @codingStandardsIgnoreLine.
		}

		// Delete all migrated data.
		$apmeta_to_delete = sanitize_comma_delimited( $apmeta_to_delete, 'int' );
		$wpdb->query( "DELETE FROM {$wpdb->prefix}ap_meta WHERE apmeta_id IN ({$apmeta_to_delete})" ); // phpcs:ignore WordPress.DB
		esc_attr_e( 'Migrated all reputations', 'anspress-question-answer' );
	}

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