From da912dd1cddbca936c2447ce12e97e0e51865efe Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Tue, 28 Jan 2025 14:17:33 +0530 Subject: [PATCH] feat: add X-API share link in first comment --- .../services/class-rop-twitter-service.php | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/includes/admin/services/class-rop-twitter-service.php b/includes/admin/services/class-rop-twitter-service.php index 89885af83..b45b3c898 100644 --- a/includes/admin/services/class-rop-twitter-service.php +++ b/includes/admin/services/class-rop-twitter-service.php @@ -602,10 +602,19 @@ public function share( $post_details, $args = array() ) { $api = $this->get_api(); } + $model = new Rop_Post_Format_Model; + $post_format = $model->get_post_format( $post_details['account_id'] ); + $post_id = $post_details['post_id']; $post_url = $post_details['post_url']; $share_as_image_post = $post_details['post_with_image']; + $share_link_text = ''; + if ( ! empty( $post_format['share_link_in_comment'] ) && ! empty( $post_format['share_link_text'] ) ) { + $share_link_text = str_replace( '{link}', $post_url, $post_format['share_link_text'] ); + $post_url = ''; + } + // Twitter link post if ( ! empty( $post_url ) && empty( $share_as_image_post ) && get_post_type( $post_id ) !== 'attachment' ) { $new_post = $this->twitter_article_post( $post_details ); @@ -628,9 +637,6 @@ public function share( $post_details, $args = array() ) { return false; } - $model = new Rop_Post_Format_Model; - $post_format = $model->get_post_format( $post_details['account_id'] ); - $hashtags = $post_details['hashtags']; if ( ! empty( $post_format['hashtags_randomize'] ) && $post_format['hashtags_randomize'] ) { @@ -732,6 +738,33 @@ public function share( $post_details, $args = array() ) { } if ( isset( $response['data'] ) && ! empty( $response['data']['id'] ) ) { + if ( $api && ! empty( $share_link_text ) ) { + // Post the first comment (replying to the tweet). + $comment = $api->post( + 'tweets', + array( + 'text' => $share_link_text, + 'reply' => array( + 'in_reply_to_tweet_id' => $response['data']['id'], + ), + ), + true + ); + + $comment = (array) $comment; + $response_headers = $api->getLastXHeaders(); + $this->logger->info( sprintf( '[X API] First Comment Response: %s', json_encode( $response_headers ) ) ); + + if ( $comment && ! empty( $comment['data']['id'] ) ) { + $this->logger->alert_success( + sprintf( + 'Successfully shared first comment to %s on %s ', + html_entity_decode( get_the_title( $post_id ) ), + $post_details['service'] + ) + ); + } + } $this->logger->alert_success( sprintf( 'Successfully shared %s to %s on %s ',