resolve_account( $post ); if ( is_wp_error( $account ) ) { return $this->fail( $account->get_error_message() ); } $token = $account['access_token']; $text = isset( $post['text'] ) ? (string) $post['text'] : ''; $link = isset( $post['link'] ) ? trim( (string) $post['link'] ) : ''; $text = $this->compose( $text, $link ); if ( '' === $text ) { return $this->fail( 'Nothing to post: the tweet text is empty.' ); } $result = $this->http_post( self::API . '/tweets', array(), array( 'headers' => array( 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'text' => $text ) ), ) ); if ( is_wp_error( $result ) ) { return $this->fail( $result->get_error_message() ); } $error = $this->extract_error( $result ); if ( '' !== $error ) { $this->flag_token_if_auth( $result, $account ); return $this->fail( $error ); } $id = ( isset( $result['data']['id'] ) && '' !== $result['data']['id'] ) ? (string) $result['data']['id'] : ''; if ( '' === $id ) { return $this->fail( 'Publish succeeded but no tweet id was returned.' ); } return array( 'ok' => true, 'platform_post_id' => $id, 'permalink' => 'https://x.com/i/web/status/' . $id, 'error' => '', ); } public function fetch_metrics( $post_ref ) { $post_ref = (string) $post_ref; if ( '' === $post_ref ) { return array(); } $token = $this->token_for_metrics(); if ( '' === $token ) { return array(); } $result = $this->http_get( add_query_arg( array( 'tweet.fields' => 'public_metrics' ), self::API . '/tweets/' . rawurlencode( $post_ref ) ), array( 'headers' => array( 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), ) ); if ( is_wp_error( $result ) || '' !== $this->extract_error( $result ) ) { return array(); } $metrics = isset( $result['data']['public_metrics'] ) ? $result['data']['public_metrics'] : array(); return is_array( $metrics ) ? $metrics : array(); } protected function compose( $text, $link ) { $text = trim( (string) $text ); $link = trim( (string) $link ); if ( '' === $link || false !== strpos( $text, $link ) ) { return $this->clamp( $text, self::MAX_LEN ); } $reserve = 1 + $this->len( $link ); $room = self::MAX_LEN - $reserve; if ( $room < 0 ) { return $this->clamp( $link, self::MAX_LEN ); } $text = $this->clamp( $text, $room ); return '' === $text ? $link : $text . ' ' . $link; } protected function len( $str ) { return function_exists( 'mb_strlen' ) ? mb_strlen( $str, 'UTF-8' ) : strlen( $str ); } protected function clamp( $str, $max ) { if ( $max <= 0 ) { return ''; } if ( $this->len( $str ) <= $max ) { return $str; } $str = function_exists( 'mb_substr' ) ? mb_substr( $str, 0, $max, 'UTF-8' ) : substr( $str, 0, $max ); return rtrim( $str ); } 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 ( 'x' !== ( isset( $account['platform'] ) ? $account['platform'] : '' ) ) { return new WP_Error( 'aieo_social_wrong_platform', 'The target account is not an X account.' ); } if ( empty( $account['access_token'] ) ) { return new WP_Error( 'aieo_social_no_token', 'The target account has no access token; reconnect it.' ); } return $account; } protected function token_for_metrics() { $accounts = AIEO_Social_Store::get_accounts( 'x' ); if ( empty( $accounts ) || ! is_array( $accounts ) ) { return ''; } foreach ( $accounts as $account ) { if ( ! empty( $account['access_token'] ) ) { return (string) $account['access_token']; } } return ''; } protected function extract_error( $resp ) { if ( ! is_array( $resp ) ) { return ''; } if ( ! empty( $resp['errors'] ) && is_array( $resp['errors'] ) ) { $messages = array(); foreach ( $resp['errors'] as $err ) { if ( is_array( $err ) ) { if ( ! empty( $err['detail'] ) ) { $messages[] = (string) $err['detail']; } elseif ( ! empty( $err['message'] ) ) { $messages[] = (string) $err['message']; } elseif ( ! empty( $err['title'] ) ) { $messages[] = (string) $err['title']; } } } $messages = array_filter( $messages ); if ( ! empty( $messages ) ) { return implode( '; ', $messages ); } return 'X rejected the request.'; } if ( empty( $resp['data'] ) && ( ! empty( $resp['detail'] ) || ! empty( $resp['title'] ) ) ) { if ( ! empty( $resp['detail'] ) ) { return (string) $resp['detail']; } return (string) $resp['title']; } return ''; } protected function flag_token_if_auth( $resp, array $account ) { if ( empty( $account['id'] ) || ! is_array( $resp ) ) { return; } $status = isset( $resp['status'] ) ? (int) $resp['status'] : 0; $title = isset( $resp['title'] ) ? strtolower( (string) $resp['title'] ) : ''; $is_auth = ( 401 === $status || false !== strpos( $title, 'unauthorized' ) || false !== strpos( $title, 'forbidden' ) ); if ( $is_auth ) { AIEO_Social_Store::set_token_status( (int) $account['id'], 'expired' ); } } protected function fail( $message ) { return array( 'ok' => false, 'platform_post_id' => '', 'permalink' => '', 'error' => (string) $message, ); } }