AP_Update_Helper::migrate_views( integer $post_id )

Description #

Migrate views data to new table.

Parameters #

  • $post_id
    integer (Required) Post id.

Source #

File: admin/update.php

	public function migrate_views( $post_id ) {
		global $wpdb;

		if ( ! $this->meta_table_exists ) {
			return;
		}

		$post_id = (int) $post_id;

		$old_views = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}ap_meta WHERE apmeta_type = 'post_view' AND apmeta_actionid = {$post_id}" ); // DB call okay, Db cache okay.

		$apmeta_to_delete = [];
		foreach ( (array) $old_views as $view ) {
			ap_insert_views( $post_id, 'question', $view->apmeta_userid, $view->apmeta_value );
			$apmeta_to_delete[] = $view->apmeta_id;
		}

		// 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})" ); // @codingStandardsIgnoreLine

		$views = (int) get_post_meta( $post_id, '_views', true );
		ap_update_views_count( $post_id, $views );

		// Delete post meta.
		delete_post_meta( $post_id, '_views' );
	}

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