INSTAGRAM_PUBLISH_PHOTO::process_action( $user_id,  $action_data,  $recipe_id,  $args,  $parsed )

Process the Instagram action.


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)

$args

(Required)

$parsed

(Required)


Top ↑

Return Return

(Uncanny_Automatorvoid.)


Source Source

File: src/integrations/instagram/actions/instagram-publish-photo.php

	protected function process_action( int $user_id, array $action_data, int $recipe_id, array $args, $parsed ) {

		$instagram = Automator()->helpers->recipe->instagram->options;

		$page_id   = sanitize_text_field( $parsed['INSTAGRAM_PUBLISH_PHOTO_ACCOUNT_ID'] );
		$image_uri = sanitize_text_field( $parsed['INSTAGRAM_IMAGE_URL'] );
		$hashtags  = sanitize_text_field( $parsed['INSTAGRAM_HASHTAGS'] );

		$page_props = $instagram->get_user_page_connected_ig( $page_id );

		// Bailout if no facebook account connected.
		if ( empty( $page_props ) ) {
			$action_data['complete_with_errors'] = true;
			Automator()->complete->action( $user_id, $action_data, $recipe_id, esc_html__( 'Cound not find any settings for Facebook Authentication.', 'uncanny-automator' ) );
			return;
		}

		// Bailout if no instagram account connected.
		if ( empty( $page_props['ig_account'] ) ) {
			$action_data['complete_with_errors'] = true;
			Automator()->complete->action( $user_id, $action_data, $recipe_id, esc_html__( 'Cannot find any Instagram account connected to the Facebook page.', 'uncanny-automator' ) );
			return;
		}

		$page_ig_account = end( $page_props['ig_account']->data );

		$business_account_id = $page_ig_account->instagram_business_account;

		$http_request_query = array(
			'body' => array(
				'action'                 => 'page-ig-media-publish',
				'access_token'           => $page_props['page_access_token'],
				'ig_business_account_id' => $business_account_id,
				'page_id'                => $page_id,
				'image_uri'              => $image_uri,
				'caption'                => $hashtags,
			),
		);

		// Add generous timeout limit for slow Instagram endpoint.
		$http_request_query['timeout'] = 60;

		$request = wp_remote_post(
			$this->fb_endpoint_uri,
			$http_request_query
		);

		if ( ! is_wp_error( $request ) ) {

			$response = json_decode( wp_remote_retrieve_body( $request ) );

			// Bailout if statusCode is not set.
			if ( ! isset( $response->statusCode ) ) {
				$action_data['complete_with_errors'] = true;
				Automator()->complete->action( $user_id, $action_data, $recipe_id, esc_html__( 'There was an error in the response code.', 'uncanny-automator' ) );
				return;
			}
			if ( 200 === $response->statusCode ) {
				// Otherwise, complete the action.
				Automator()->complete->action( $user_id, $action_data, $recipe_id );
			} else {
				$action_data['complete_with_errors'] = true;
				// Log error if there are any error messages.
				Automator()->complete->action( $user_id, $action_data, $recipe_id, $response->error->description );
			}
		} else {

			// Log if there are any http errors.
			$action_data['complete_with_errors'] = true;

			Automator()->complete->action( $user_id, $action_data, $recipe_id, $request->get_error_message() );
		}

	}