AnsPress_Admin_Ajax::ap_uninstall_data()

Description #

Uninstall actions.

Source #

File: admin/ajax.php

	public static function ap_uninstall_data() {
		check_ajax_referer( 'ap_uninstall_data', '__nonce' );

		$data_type  = ap_sanitize_unslash( 'data_type', 'r' );
		$valid_data = array( 'qa', 'answers', 'options', 'userdata', 'terms', 'tables' );

		global $wpdb;

		// Only allow super admin to delete data.
		if ( is_super_admin() && in_array( $data_type, $valid_data, true ) ) {
			$done = 0;

			if ( 'qa' === $data_type ) {
				$count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_type='question' OR post_type='answer'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery

				$ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='question' OR post_type='answer' LIMIT 30" );  // phpcs:ignore WordPress.DB.DirectDatabaseQuery

				foreach ( (array) $ids as $id ) {
					if ( false !== wp_delete_post( $id, true ) ) {
						++$done;
					}
				}

				wp_send_json(
					array(
						'done'  => (int) $done,
						'total' => (int) $count,
					)
				);
			} elseif ( 'answers' === $data_type ) {
				$count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_type='answer'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
				$ids   = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='answer' LIMIT 30" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery

				foreach ( (array) $ids as $id ) {
					if ( false !== wp_delete_post( $id, true ) ) {
						++$done;
					}
				}

				wp_send_json(
					array(
						'done'  => (int) $done,
						'total' => (int) $count,
					)
				);
			} elseif ( 'userdata' === $data_type ) {
				$wp_filesystem = new WP_Filesystem_Direct( false );
				$upload_dir    = wp_upload_dir();
				$avatar_dir    = $upload_dir['basedir'] . '/ap_avatars';

				if ( $wp_filesystem->is_dir( $avatar_dir ) ) {
					$wp_filesystem->rmdir( $avatar_dir, true );
				}

				// Remove user roles.
				AP_Roles::remove_roles();

				// Delete vote meta.
				$wpdb->delete( $wpdb->usermeta, [ 'meta_key' => '__up_vote_casted' ], array( '%s' ) ); // @codingStandardsIgnoreLine
				$wpdb->delete( $wpdb->usermeta, [ 'meta_key' => '__down_vote_casted' ], array( '%s' ) ); // @codingStandardsIgnoreLine

				wp_send_json(
					array(
						'done'  => 1,
						'total' => 0,
					)
				);
			} elseif ( 'options' === $data_type ) {
				delete_option( 'anspress_opt' );
				delete_option( 'anspress_reputation_events' );
				delete_option( 'anspress_addons' );

				wp_send_json(
					array(
						'done'  => 1,
						'total' => 0,
					)
				);
			} elseif ( 'terms' === $data_type ) {
				$question_taxo = (array) get_object_taxonomies( 'question', 'names' );
				$answer_taxo   = (array) get_object_taxonomies( 'answer', 'names' );

				$taxos = $question_taxo + $answer_taxo;

				foreach ( (array) $taxos as $tax ) {
					$terms = get_terms(
						array(
							'taxonomy'   => $tax,
							'hide_empty' => false,
							'fields'     => 'ids',
						)
					);

					foreach ( (array) $terms as $t ) {
						wp_delete_term( $t, $tax );
					}
				}

				wp_send_json(
					array(
						'done'  => 1,
						'total' => 0,
					)
				);
			} elseif ( 'tables' === $data_type ) {
				$tables = array( $wpdb->ap_qameta, $wpdb->ap_votes, $wpdb->ap_views, $wpdb->ap_reputations, $wpdb->ap_subscribers, $wpdb->prefix . 'ap_notifications' );

				foreach ( $tables as $table ) {
					$wpdb->query( "DROP TABLE IF EXISTS {$table}" ); // phpcs:ignore WordPress.DB
				}

				wp_send_json(
					array(
						'done'  => 1,
						'total' => 0,
					)
				);
			}
		}

		// Send empty JSON if nothing done.
		wp_send_json( array() );
	}

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