Skip to content

Commit

Permalink
Merge pull request #1023 from Codeinwp/feat/pro/484
Browse files Browse the repository at this point in the history
Feat: share link in first comment
  • Loading branch information
vytisbulkevicius authored Feb 6, 2025
2 parents 03aabb2 + 68950df commit 181bbb1
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
php-unit:
name: PHPUnit
needs: code-sniff
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
services:
mysql:
image: mysql:5.7
Expand Down
18 changes: 18 additions & 0 deletions includes/admin/abstract/class-rop-services-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ abstract class Rop_Services_Abstract {
* @var Rop_Logger $logger The logger handler.
*/
protected $logger;
/**
* Stores a share first comment text.
*
* @since 9.1.3
* @access protected
* @var string $share_link_text Comment text.
*/
protected $share_link_text = '';

/**
* Rop_Services_Abstract constructor.
Expand Down Expand Up @@ -320,6 +328,16 @@ public function get_service_id() {

}

/**
* Share the post link in the first comment.
*
* @access public
*
* @param string $url API endpoint.
* @param array $data API data.
*/
public function share_as_first_comment( $url, $data = array() ) {}

/**
* Method to request a token from api.
*
Expand Down
42 changes: 22 additions & 20 deletions includes/admin/class-rop-global-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,26 +180,28 @@ class Rop_Global_Settings {
*/
private $post_format_defaults = array(
'facebook' => array(
'wpml_language' => '',
'post_content' => 'post_title',
'custom_meta_field' => '',
'maximum_length' => '1000',
'custom_text' => '',
'custom_text_pos' => 'beginning',
'include_link' => true,
'url_from_meta' => false,
'url_meta_key' => '',
'short_url' => false,
'short_url_service' => 'rviv.ly',
'hashtags' => 'no-hashtags',
'hashtags_length' => '200',
'hashtags_common' => '',
'hashtags_custom' => '',
'hashtags_randomize' => false,
'shortner_credentials' => array(),
'image' => false,
'utm_campaign_medium' => 'social',
'utm_campaign_name' => 'ReviveOldPost',
'wpml_language' => '',
'post_content' => 'post_title',
'custom_meta_field' => '',
'maximum_length' => '1000',
'custom_text' => '',
'custom_text_pos' => 'beginning',
'include_link' => true,
'url_from_meta' => false,
'url_meta_key' => '',
'short_url' => false,
'short_url_service' => 'rviv.ly',
'hashtags' => 'no-hashtags',
'hashtags_length' => '200',
'hashtags_common' => '',
'hashtags_custom' => '',
'hashtags_randomize' => false,
'shortner_credentials' => array(),
'image' => false,
'utm_campaign_medium' => 'social',
'utm_campaign_name' => 'ReviveOldPost',
'share_link_in_comment' => false,
'share_link_text' => '',
),
'twitter' => array(
'wpml_language' => '',
Expand Down
65 changes: 58 additions & 7 deletions includes/admin/services/class-rop-facebook-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ public function share( $post_details, $args = array() ) {
$share_as_image_post = $post_details['post_with_image'];
$global_settings = new Rop_Global_Settings();

if ( ! empty( $post_format['share_link_in_comment'] ) && ! empty( $post_format['share_link_text'] ) ) {
$this->share_link_text = str_replace( '{link}', self::get_url( $post_details ), $post_format['share_link_text'] );
}

if ( array_key_exists( 'account_type', $args ) ) {

if ( ( $args['account_type'] === 'instagram_account' || $args['account_type'] === 'facebook_group' ) && $global_settings->license_type() < 1 ) {
Expand All @@ -486,7 +490,8 @@ public function share( $post_details, $args = array() ) {
// **** Instagram Sharing ***** //
if ( $args['account_type'] === 'instagram_account' && class_exists( 'Rop_Pro_Instagram_Service' ) ) {

$args['correct_aspect_ratio'] = $post_format['correct_aspect_ratio'];
$args['correct_aspect_ratio'] = isset( $post_format['correct_aspect_ratio'] ) ? $post_format['correct_aspect_ratio'] : '';
$post_details['share_link_text'] = $this->share_link_text;

$response = Rop_Pro_Instagram_Service::share( $post_details, $hashtags, $args );

Expand Down Expand Up @@ -568,7 +573,9 @@ private function fb_article_post( $post_details, $hashtags ) {

$new_post['message'] = $this->strip_excess_blank_lines( $post_details['content'] ) . $hashtags;

$new_post['link'] = $this->get_url( $post_details );
if ( empty( $this->share_link_text ) ) {
$new_post['link'] = $this->get_url( $post_details );
}

return array(
'post_data' => $new_post,
Expand Down Expand Up @@ -601,7 +608,8 @@ private function fb_image_post( $post_details, $hashtags ) {

$new_post['url'] = $attachment_url;
$new_post['source'] = $this->get_path_by_url( $attachment_url, $post_details['mimetype'] ); // get image path
$new_post['caption'] = $post_details['content'] . $this->get_url( $post_details ) . $hashtags;
$post_url = empty( $this->share_link_text ) ? $this->get_url( $post_details ) : '';
$new_post['caption'] = $post_details['content'] . $post_url . $hashtags;

return array(
'post_data' => $new_post,
Expand All @@ -628,7 +636,8 @@ private function fb_video_post( $post_details, $hashtags ) {
$new_post['source'] = $image;
// $new_post['source'] = $api->videoToUpload( $image );
$new_post['title'] = html_entity_decode( get_the_title( $post_details['post_id'] ), ENT_QUOTES );
$new_post['description'] = $post_details['content'] . $this->get_url( $post_details ) . $hashtags;
$post_url = empty( $this->share_link_text ) ? $this->get_url( $post_details ) : '';
$new_post['description'] = $post_details['content'] . $post_url . $hashtags;

return array(
'post_data' => $new_post,
Expand Down Expand Up @@ -705,7 +714,19 @@ private function try_post( $new_post, $page_id, $token, $post_id, $posting_type
$this->rop_fb_scrape_url( $posting_type, $post_id, $token );
}

$api->post( $path, $new_post, $token );
$response = $api->post( $path, $new_post, $token );
$fb_post_id = $response->getGraphNode()->getField( 'id' );

if ( class_exists( 'Rop_Pro_Facebook_Helper' ) ) {
$fb_helper = new Rop_Pro_Facebook_Helper();
$fb_helper->share_as_first_comment(
$fb_post_id,
array(
'message' => $this->share_link_text,
'access_token' => $token,
)
);
}

return true;
} catch ( Facebook\Exceptions\FacebookResponseException $e ) {
Expand All @@ -729,8 +750,18 @@ private function try_post( $new_post, $page_id, $token, $post_id, $posting_type
}

try {
$api->post( $path, $new_post, $token );

$response = $api->post( $path, $new_post, $token );
$fb_post_id = $response->getGraphNode()->getField( 'id' );
if ( class_exists( 'Rop_Pro_Facebook_Helper' ) ) {
$fb_helper = new Rop_Pro_Facebook_Helper();
$fb_helper->share_as_first_comment(
$fb_post_id,
array(
'message' => $this->share_link_text,
'access_token' => $token,
)
);
}
return true;
} catch ( Facebook\Exceptions\FacebookResponseException $e ) {
$this->logger->alert_error( 'Unable to share post for facebook. (FacebookResponseException) Error: ' . $e->getMessage() );
Expand Down Expand Up @@ -819,6 +850,16 @@ private function try_post( $new_post, $page_id, $token, $post_id, $posting_type
}

if ( ! empty( $body['id'] ) ) {
if ( class_exists( 'Rop_Pro_Facebook_Helper' ) ) {
$fb_helper = new Rop_Pro_Facebook_Helper();
$fb_helper->share_as_first_comment(
$body['id'],
array(
'message' => $this->share_link_text,
'access_token' => $token,
)
);
}
return true;
} elseif ( ! empty( $body['error']['message'] ) ) {
if (
Expand Down Expand Up @@ -878,6 +919,16 @@ private function try_post( $new_post, $page_id, $token, $post_id, $posting_type
}

if ( ! empty( $body['id'] ) ) {
if ( class_exists( 'Rop_Pro_Facebook_Helper' ) ) {
$fb_helper = new Rop_Pro_Facebook_Helper();
$fb_helper->share_as_first_comment(
$body['id'],
array(
'message' => $this->share_link_text,
'access_token' => $token,
)
);
}
return true;
} elseif ( ! empty( $body['error']['message'] ) ) {
$this->logger->alert_error( 'Error Posting to Facebook: ' . $body['error']['message'] );
Expand Down
35 changes: 31 additions & 4 deletions includes/admin/services/class-rop-linkedin-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ public function share( $post_details, $args = array() ) {
$model = new Rop_Post_Format_Model;
$post_format = $model->get_post_format( $post_details['account_id'] );

if ( ! empty( $post_format['share_link_in_comment'] ) && ! empty( $post_format['share_link_text'] ) ) {
$this->share_link_text = str_replace( '{link}', self::get_url( $post_details ), $post_format['share_link_text'] );
}

$hashtags = $post_details['hashtags'];

if ( ! empty( $post_format['hashtags_randomize'] ) && $post_format['hashtags_randomize'] ) {
Expand Down Expand Up @@ -591,10 +595,30 @@ public function share( $post_details, $args = array() ) {
return false;
}

$body = json_decode( wp_remote_retrieve_body( $response ), true );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
$headers = wp_remote_retrieve_headers( $response );

if ( null === $body ) {

if ( ! empty( $this->share_link_text ) ) {
$post_id = isset( $headers['x-restli-id'] ) ? $headers['x-restli-id'] : '';
if ( class_exists( 'Rop_Pro_Linkdin_Helper' ) ) {
$linkedin_helper = new Rop_Pro_Linkdin_Helper();
$linkedin_helper->share_as_first_comment(
$post_id,
array(
'token' => $token,
'body' => array(
'actor' => $new_post['author'],
'message' => array(
'text' => $this->share_link_text,
),
),
)
);
}
}

$title = isset( $new_post['content']['media']['title'] ) ? $new_post['content']['media']['title'] : $new_post['content']['article']['title'];
$this->logger->alert_success(
sprintf(
Expand Down Expand Up @@ -634,7 +658,8 @@ private function linkedin_article_post( $post_details, $hashtags, $args, $token

$author_urn = $args['is_company'] ? 'urn:li:organization:' : 'urn:li:person:';

$commentary = $this->strip_excess_blank_lines( $post_details['content'] ) . $this->get_url( $post_details ) . $hashtags;
$post_url = empty( $this->share_link_text ) ? $this->get_url( $post_details ) : '';
$commentary = $this->strip_excess_blank_lines( $post_details['content'] ) . $post_url . $hashtags;
$commentary = preg_replace_callback(
'/([\(\)\{\}\[\]])|([@*<>\\\\\_~])/m',
function ( $matches ) {
Expand Down Expand Up @@ -691,7 +716,8 @@ private function linkedin_text_post( $post_details, $hashtags, $args ) {

$author_urn = $args['is_company'] ? 'urn:li:organization:' : 'urn:li:person:';

$commentary = $this->strip_excess_blank_lines( $post_details['content'] ) . $this->get_url( $post_details ) . $hashtags;
$post_url = empty( $this->share_link_text ) ? $this->get_url( $post_details ) : '';
$commentary = $this->strip_excess_blank_lines( $post_details['content'] ) . $post_url . $hashtags;
$commentary = preg_replace_callback(
'/([\(\)\{\}\[\]])|([@*<>\\\\\_~])/m',
function ( $matches ) {
Expand Down Expand Up @@ -758,7 +784,8 @@ private function linkedin_image_post( $post_details, $hashtags, $args, $token )
}

$asset = $asset_data['value']['image'];
$commentary = $this->strip_excess_blank_lines( $post_details['content'] ) . $this->get_url( $post_details ) . $hashtags;
$post_url = empty( $this->share_link_text ) ? $this->get_url( $post_details ) : '';
$commentary = $this->strip_excess_blank_lines( $post_details['content'] ) . $post_url . $hashtags;
$commentary = preg_replace_callback(
'/([\(\)\{\}\[\]])|([@*<>\\\\\_~])/m',
function ( $matches ) {
Expand Down
42 changes: 37 additions & 5 deletions includes/admin/services/class-rop-twitter-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ public function request_api_token() {
*/
private function twitter_article_post( $post_details ) {

$new_post['text'] = $this->strip_excess_blank_lines( $post_details['content'] ) . $this->get_url( $post_details );
$post_url = empty( $this->share_link_text ) ? $this->get_url( $post_details ) : '';
$new_post['text'] = $this->strip_excess_blank_lines( $post_details['content'] ) . $post_url;

return $new_post;
}
Expand Down Expand Up @@ -557,7 +558,8 @@ private function twitter_media_post( $post_details, $api ) {
wp_delete_file( $media_path );
}

$new_post['text'] = $this->strip_excess_blank_lines( $post_details['content'] ) . $this->get_url( $post_details );
$post_url = empty( $this->share_link_text ) ? $this->get_url( $post_details ) : '';
$new_post['text'] = $this->strip_excess_blank_lines( $post_details['content'] ) . $post_url;

return $new_post;
}
Expand Down Expand Up @@ -602,10 +604,17 @@ 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'];

if ( ! empty( $post_format['share_link_in_comment'] ) && ! empty( $post_format['share_link_text'] ) ) {
$this->share_link_text = str_replace( '{link}', self::get_url( $post_details ), $post_format['share_link_text'] );
}

// 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,32 @@ public function share( $post_details, $args = array() ) {
}

if ( isset( $response['data'] ) && ! empty( $response['data']['id'] ) ) {
if ( $api && ! empty( $this->share_link_text ) ) {
// Post the first comment (replying to the tweet).
$comment = $api->post(
'tweets',
array(
'text' => $this->share_link_text,
'reply' => array(
'in_reply_to_tweet_id' => $response['data']['id'],
),
),
true
);

$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->info(
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
Loading

0 comments on commit 181bbb1

Please sign in to comment.