Skip to content

Commit

Permalink
feat: add X-API share link in first comment
Browse files Browse the repository at this point in the history
  • Loading branch information
girishpanchal30 committed Jan 28, 2025
1 parent b9d24e7 commit da912dd
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions includes/admin/services/class-rop-twitter-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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'] ) {
Expand Down Expand Up @@ -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 ',
Expand Down

0 comments on commit da912dd

Please sign in to comment.