resolve_account( $post ); if ( is_wp_error( $account ) ) { return $this->fail( $account->get_error_message() ); } $page_id = $account['external_id']; $token = $account['access_token']; $text = isset( $post['text'] ) ? (string) $post['text'] : ''; $image = isset( $post['image_url'] ) ? trim( (string) $post['image_url'] ) : ''; $link = isset( $post['link'] ) ? trim( (string) $post['link'] ) : ''; if ( '' !== $image ) { $endpoint = self::GRAPH . '/' . rawurlencode( $page_id ) . '/photos'; $body = array( 'url' => $image, 'caption' => $text, 'access_token' => $token, ); } else { $endpoint = self::GRAPH . '/' . rawurlencode( $page_id ) . '/feed'; $body = array( 'message' => $text, 'access_token' => $token, ); if ( '' !== $link ) { $body['link'] = $link; } } $result = $this->http_post( $endpoint, $body ); if ( is_wp_error( $result ) ) { return $this->fail( $result->get_error_message() ); } if ( ! empty( $result['error'] ) ) { return $this->fail_graph( $result['error'], $account ); } $post_id = ''; if ( ! empty( $result['post_id'] ) ) { $post_id = (string) $result['post_id']; } elseif ( ! empty( $result['id'] ) ) { $post_id = (string) $result['id']; } if ( '' === $post_id ) { return $this->fail( 'Publish succeeded but no post id was returned.' ); } return array( 'ok' => true, 'platform_post_id' => $post_id, 'permalink' => 'https://facebook.com/' . $post_id, 'error' => '', ); } public function fetch_metrics( $post_ref ) { $post_ref = (string) $post_ref; if ( '' === $post_ref ) { return array(); } $token = $this->token_for_post_ref( $post_ref ); if ( '' === $token ) { return array(); } $endpoint = self::GRAPH . '/' . rawurlencode( $post_ref ) . '/insights'; $result = $this->http_get( add_query_arg( array( 'metric' => 'post_impressions,post_engaged_users,post_clicks' ), $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 ( 'facebook' !== ( isset( $account['platform'] ) ? $account['platform'] : '' ) ) { return new WP_Error( 'aieo_social_wrong_platform', 'The target account is not a Facebook account.' ); } if ( 'page' !== ( isset( $account['type'] ) ? $account['type'] : '' ) ) { return new WP_Error( 'aieo_social_not_page', 'Facebook publishing requires a Page account.' ); } if ( empty( $account['external_id'] ) ) { return new WP_Error( 'aieo_social_no_page_id', 'The target Page has no id.' ); } if ( empty( $account['access_token'] ) ) { return new WP_Error( 'aieo_social_no_token', 'The target Page has no access token; reconnect it.' ); } return $account; } protected function token_for_post_ref( $post_ref ) { $page_id = $post_ref; if ( false !== strpos( $post_ref, '_' ) ) { $page_id = substr( $post_ref, 0, strpos( $post_ref, '_' ) ); } $accounts = AIEO_Social_Store::get_accounts( 'facebook' ); if ( empty( $accounts ) || ! is_array( $accounts ) ) { return ''; } foreach ( $accounts as $account ) { if ( isset( $account['external_id'] ) && (string) $account['external_id'] === (string) $page_id && ! 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'] : 'Facebook 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, ); } }