resolve_account( $post ); if ( is_wp_error( $account ) ) { return $this->fail( $account->get_error_message() ); } $image = isset( $post['image_url'] ) ? trim( (string) $post['image_url'] ) : ''; if ( '' === $image ) { return $this->fail( 'Instagram requires an image.' ); } $ig_id = $account['external_id']; $token = $account['access_token']; $text = isset( $post['text'] ) ? (string) $post['text'] : ''; $headers = array( 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, ); $create = $this->http_post( self::GRAPH . '/' . rawurlencode( $ig_id ) . '/media', array( 'image_url' => $image, 'caption' => $text, ), array( 'headers' => $headers ) ); if ( is_wp_error( $create ) ) { return $this->fail( $create->get_error_message() ); } if ( ! empty( $create['error'] ) ) { return $this->fail_graph( $create['error'], $account ); } $creation_id = ! empty( $create['id'] ) ? (string) $create['id'] : ''; if ( '' === $creation_id ) { return $this->fail( 'Instagram did not return a media container id.' ); } $publish = $this->http_post( self::GRAPH . '/' . rawurlencode( $ig_id ) . '/media_publish', array( 'creation_id' => $creation_id ), array( 'headers' => $headers ) ); if ( is_wp_error( $publish ) ) { return $this->fail( $publish->get_error_message() ); } if ( ! empty( $publish['error'] ) ) { return $this->fail_graph( $publish['error'], $account ); } $media_id = ! empty( $publish['id'] ) ? (string) $publish['id'] : ''; if ( '' === $media_id ) { return $this->fail( 'Publish succeeded but no media id was returned.' ); } return array( 'ok' => true, 'platform_post_id' => $media_id, 'permalink' => 'https://www.instagram.com/', 'error' => '', ); } public function fetch_metrics( $post_ref ) { $post_ref = (string) $post_ref; if ( '' === $post_ref ) { return array(); } $token = $this->token_for_media(); if ( '' === $token ) { return array(); } $endpoint = self::GRAPH . '/' . rawurlencode( $post_ref ) . '/insights'; $result = $this->http_get( add_query_arg( array( 'metric' => 'impressions,reach,likes' ), $endpoint ), array( 'headers' => array( 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), ) ); if ( is_wp_error( $result ) || ! empty( $result['error'] ) || empty( $result['data'] ) || ! is_array( $result['data'] ) ) { return array(); } $metrics = array(); foreach ( $result['data'] as $row ) { if ( empty( $row['name'] ) || empty( $row['values'] ) || ! is_array( $row['values'] ) ) { continue; } $last = end( $row['values'] ); $value = is_array( $last ) && isset( $last['value'] ) ? $last['value'] : null; if ( null !== $value ) { $metrics[ (string) $row['name'] ] = $value; } } return $metrics; } protected function resolve_account( array $post ) { $account_id = isset( $post['account_id'] ) ? (int) $post['account_id'] : 0; if ( $account_id <= 0 ) { return new WP_Error( 'aieo_social_no_account', 'No target account was specified.' ); } $account = AIEO_Social_Store::get_account( $account_id ); if ( empty( $account ) || ! is_array( $account ) ) { return new WP_Error( 'aieo_social_account_missing', 'The target account no longer exists.' ); } if ( 'instagram' !== ( isset( $account['platform'] ) ? $account['platform'] : '' ) ) { return new WP_Error( 'aieo_social_wrong_platform', 'The target account is not an Instagram account.' ); } if ( empty( $account['external_id'] ) ) { return new WP_Error( 'aieo_social_no_ig_id', 'The target Instagram account has no id.' ); } if ( empty( $account['access_token'] ) ) { return new WP_Error( 'aieo_social_no_token', 'The target Instagram account has no access token; reconnect it.' ); } return $account; } protected function token_for_media() { if ( ! method_exists( 'AIEO_Social_Store', 'get_accounts' ) ) { return ''; } $accounts = AIEO_Social_Store::get_accounts( 'instagram' ); if ( empty( $accounts ) || ! is_array( $accounts ) ) { return ''; } foreach ( $accounts as $account ) { if ( ! empty( $account['access_token'] ) ) { return (string) $account['access_token']; } } return ''; } protected function fail_graph( $error, array $account ) { $message = is_array( $error ) && ! empty( $error['message'] ) ? (string) $error['message'] : 'Instagram rejected the request.'; $code = is_array( $error ) && isset( $error['code'] ) ? (int) $error['code'] : 0; $subcode = is_array( $error ) && isset( $error['error_subcode'] ) ? (int) $error['error_subcode'] : 0; $type = is_array( $error ) && isset( $error['type'] ) ? (string) $error['type'] : ''; $is_token = ( 190 === $code || 102 === $code || 'OAuthException' === $type || ( 0 !== $subcode && in_array( $subcode, array( 458, 459, 460, 463, 464, 467 ), true ) ) ); if ( $is_token && ! empty( $account['id'] ) ) { AIEO_Social_Store::set_token_status( (int) $account['id'], 'expired' ); } return $this->fail( $message ); } protected function fail( $message ) { return array( 'ok' => false, 'platform_post_id' => '', 'permalink' => '', 'error' => (string) $message, ); } }