ap_insert_qameta( int $post_id, array $args, bool $wp_error = false )

Description #

Insert post meta

Parameters #

  • $post_id
    int (Required) Post ID.
  • $args
    array (Required) Arguments.
  • $wp_error
    bool (Optional) Return wp_error on fail. Default value: false

Source #

File: includes/qameta.php

function ap_insert_qameta( $post_id, $args, $wp_error = false ) {
	if ( empty( $post_id ) ) {
		return $wp_error ? new WP_Error( 'empty_post_id', __( 'Post ID is required', 'anspress-question-answer' ) ) : false;
	}

	$_post  = get_post( $post_id );
	$exists = ap_get_qameta( $post_id );

	if ( ! is_object( $_post ) || ! isset( $_post->post_type ) || ! in_array( $_post->post_type, array( 'question', 'answer' ), true ) ) {
		return false;
	}

	$args = wp_unslash(
		wp_parse_args(
			$args,
			array(
				'ptype' => $_post->post_type,
			)
		)
	);

	$sanitized_values = array();
	$formats          = array();

	// Include and sanitize valid fields.
	foreach ( (array) ap_qameta_fields() as $field => $type ) {
		if ( isset( $args[ $field ] ) ) {
			$value = $args[ $field ];

			if ( 'fields' === $field ) {
				$value     = maybe_serialize( array_merge( (array) $exists->$field, (array) $value ) );
				$formats[] = '%s';
			} elseif ( 'activities' === $field ) {
				$value     = maybe_serialize( $value );
				$formats[] = '%s';
			} elseif ( 'terms' === $field || 'attach' === $field ) {
				$value     = is_array( $value ) ? sanitize_comma_delimited( $value ) : (int) $value;
				$formats[] = '%s';
			} elseif ( in_array( $field, array( 'selected', 'featured', 'closed' ), true ) ) {
				$value     = (bool) $value;
				$formats[] = '%d';
			} elseif ( in_array( $field, array( 'selected_id', 'comments', 'answers', 'views', 'votes_up', 'votes_down', 'subscribers', 'flags' ), true ) ) {
				$value     = (int) $value;
				$formats[] = '%d';
			} else {
				$value     = sanitize_text_field( $value );
				$formats[] = '%s';
			}

			$sanitized_values[ $field ] = $value;
		}
	}

	global $wpdb;

	// Don't insert or update if not AnsPress CPT.
	// This check will also prevent inserting qameta for deleted post.
	if ( ! isset( $exists->ptype ) || ! in_array( $exists->ptype, array( 'question', 'answer' ), true ) ) {
		return $wp_error ? new WP_Error( 'invalid_post_type', __( 'Not question or answer CPT', 'anspress-question-answer' ) ) : false;
	}

	if ( $exists->is_new ) {
		$sanitized_values['post_id'] = (int) $post_id;

		if ( ! empty( $_post->post_author ) ) {
			$sanitized_values['roles'] = $_post->post_author;
		}

		$inserted = $wpdb->insert( $wpdb->ap_qameta, $sanitized_values, $formats ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
	} else {
		$inserted = $wpdb->update( $wpdb->ap_qameta, $sanitized_values, array( 'post_id' => $post_id ), $formats ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
	}

	if ( false !== $inserted ) {
		return $post_id;
	}

	return $wp_error ? new WP_Error( 'qameta_insert_failed', __( 'Unable to insert AnsPress qameta', 'anspress-question-answer' ) ) : false;
}

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