resolve_account( $post ); if ( is_wp_error( $account ) ) { return $this->fail( $account->get_error_message() ); } $token = (string) $account['access_token']; $text = isset( $post['text'] ) ? (string) $post['text'] : ''; $author = isset( $account['parent_external_id'] ) ? trim( (string) $account['parent_external_id'] ) : ''; if ( '' === $author ) { $author = 'urn:li:organization:' . $account['external_id']; } $payload = array( 'author' => $author, 'commentary' => $text, 'visibility' => 'PUBLIC', 'distribution' => array( 'feedDistribution' => 'MAIN_FEED', 'targetEntities' => array(), 'thirdPartyDistributionChannels' => array(), ), 'lifecycleState' => 'PUBLISHED', 'isReshareDisabledByAuthor' => false, ); $response = wp_remote_post( self::POSTS_URL, array( 'timeout' => 20, 'headers' => array( 'Authorization' => 'Bearer ' . $token, 'LinkedIn-Version' => self::VERSION, 'X-Restli-Protocol-Version' => '2.0.0', 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => wp_json_encode( $payload ), ) ); if ( is_wp_error( $response ) ) { return $this->fail( $response->get_error_message() ); } $code = (int) wp_remote_retrieve_response_code( $response ); $raw = wp_remote_retrieve_body( $response ); if ( $code < 200 || $code >= 300 ) { return $this->fail_http( $code, $raw, $account ); } $urn = trim( (string) wp_remote_retrieve_header( $response, 'x-restli-id' ) ); if ( '' === $urn ) { $urn = trim( (string) wp_remote_retrieve_header( $response, 'x-linkedin-id' ) ); } return array( 'ok' => true, 'platform_post_id' => $urn, 'permalink' => ( '' === $urn ) ? 'https://www.linkedin.com/' : 'https://www.linkedin.com/feed/update/' . $urn, 'error' => '', ); } public function fetch_metrics( $post_ref ) { return array(); } 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 ( 'linkedin' !== ( isset( $account['platform'] ) ? $account['platform'] : '' ) ) { return new WP_Error( 'aieo_social_wrong_platform', 'The target account is not a LinkedIn account.' ); } if ( empty( $account['external_id'] ) ) { return new WP_Error( 'aieo_social_no_org_id', 'The target LinkedIn page has no id.' ); } if ( empty( $account['access_token'] ) ) { return new WP_Error( 'aieo_social_no_token', 'The target LinkedIn page has no access token; reconnect it.' ); } return $account; } protected function fail_http( $code, $raw, array $account ) { $data = json_decode( (string) $raw, true ); $message = ''; if ( is_array( $data ) && ! empty( $data['message'] ) ) { $message = (string) $data['message']; } if ( '' === $message ) { $message = sprintf( 'LinkedIn rejected the request (HTTP %d).', (int) $code ); } if ( ( 401 === (int) $code || 403 === (int) $code ) && ! 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, ); } }