Facebook_Helpers::fetch_pages_from_api()

Retrieve the users pages.


Return Return

(array)


Source Source

File: src/integrations/facebook/helpers/facebook-helpers.php

	public function fetch_pages_from_api() {

		$settings = get_option( '_uncannyowl_facebook_settings' );

		$remote = wp_remote_post(
			$this->fb_endpoint_uri,
			array(
				'body' => array(
					'action'       => 'list-user-pages',
					'access_token' => $settings['user']['token'],
				),
			)
		);

		$pages = array();

		if ( ! is_wp_error( $remote ) ) {

			$response = wp_remote_retrieve_body( $remote );

			$response = json_decode( $response );

			$status = $response->statusCode ?? '';

			$message = $response->data->error->message ?? '';

			if ( 200 === $status ) {

				foreach ( $response->data->data as $page ) {

					$pages[] = array(
						'value'             => $page->id,
						'text'              => $page->name,
						'tasks'             => $page->tasks,
						'page_access_token' => $page->access_token,
					);
				}

				$message = esc_html__( 'Pages are fetched successfully', 'automator-pro' );

				// Save the pages.
				update_option( '_uncannyowl_facebook_pages_settings', $pages );

			}
		} else {
			$message = $remote->get_error_message();
			$status  = 500;
		}

		$response = array(
			'status'  => $status,
			'message' => $message,
			'pages'   => $pages,
		);

		return $response;

	}