Akismet::api_request( integer $post_id, boolean $submit = false )

Description #

Check post for spam, if spam then hold it for moderation.

Parameters #

  • $post_id
    integer (Required) Post id.
  • $submit
    boolean (Optional) Submit. Default value: false

Source #

File: addons/akismet/akismet.php

	private function api_request( $post_id, $submit = false ) {
		$post         = ap_get_post( $post_id );
		$comment_type = 'question' === $post->post_type ? 'forum-post' : 'replay';

		// Set default arguments to pass.
		$defaults = array(
			'blog'                 => home_url( '/' ),
			'user_ip'              => get_post_meta( $post->ID, 'create_ip', true ),
			'user_agent'           => ! empty( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '',
			'referrer'             => ! empty( $_SERVER['HTTP_REFERER'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '',
			'permalink'            => get_permalink( $post->ID ),
			'comment_type'         => $comment_type,
			'comment_author'       => get_the_author_meta( 'nicename', $post->post_author ),
			'comment_author_email' => get_the_author_meta( 'user_email', $post->post_author ),
			'comment_author_url'   => get_the_author_meta( 'url', $post->post_author ),
			'comment_content'      => $post->post_title . "\n\n" . $post->post_content,
		);

		$akismet_ua = sprintf( 'WordPress/%s | AnsPress/%s', $GLOBALS['wp_version'], AP_VERSION );
		$akismet_ua = apply_filters( 'akismet_ua', $akismet_ua );
		$api_key    = \Akismet::get_api_key();
		$host       = \Akismet::API_HOST;

		if ( ! empty( $api_key ) ) {
			$host = $api_key . '.' . $host;
		}

		$http_host = $host;
		$http_args = array(
			'body'        => $defaults,
			'headers'     => array(
				'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ),
				'Host'         => $host,
				'User-Agent'   => $akismet_ua,
			),
			'httpversion' => '1.0',
			'timeout'     => 15,
		);

		$http_akismet_url = "http://{$http_host}/1.1/";
		$akismet_url      = $http_akismet_url;

		if ( false === $submit ) {
			$akismet_url .= 'comment-check';
		} else {
			$akismet_url .= 'submit-spam';
		}

		$akismet_url = set_url_scheme( $akismet_url, 'https' );
		$response    = wp_remote_post( $akismet_url, $http_args );

		\Akismet::log( compact( 'akismet_url', 'http_args', 'response' ) );

		if ( is_wp_error( $response ) ) {
			// Intermittent connection problems may cause the first HTTPS
			// request to fail and subsequent HTTP requests to succeed randomly.
			// Retry the HTTPS request once before disabling SSL for a time.
			$response = wp_remote_post( $akismet_url, $http_args );

			\Akismet::log( compact( 'akismet_url', 'http_args', 'response' ) );

			if ( is_wp_error( $response ) ) {
				// Try the request again without SSL.
				$response = wp_remote_post( $http_akismet_url, $http_args );
				\Akismet::log( compact( 'http_akismet_url', 'http_args', 'response' ) );
			}
		}

		if ( is_wp_error( $response ) ) {
			return array( '', '' );
		}

		// Lastly if true mark it as spam.
		if ( 'true' === $response['body'] || 'Thanks for making the web a better place.' === $response['body'] ) {
			$this->spam_post_action( $post_id );
			update_post_meta( $post_id, '__ap_spam', current_time( 'timestamp' ) ); // phpcs:ignore
		}
	}

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