<?php
/**
* Post share buttons
*
* @package FreshWP WordPress Theme
* @copyright Copyright (C) 2025 ThemesDNA
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
* @author ThemesDNA <themesdna@gmail.com>
*/

function freshwp_share_text() {
    $sharetext = esc_html__( 'Share:', 'freshwp' );
    if ( freshwp_get_option('hide_share_text') ) {return;}
    if ( freshwp_get_option('share_text') ) {
        $sharetext = freshwp_get_option('share_text');
    }
    return apply_filters( 'freshwp_share_text', $sharetext );
}

function freshwp_social_sharing_buttons() {

        global $post;

        // Get current page URL 
        $posturl = rawurlencode(get_permalink($post->ID));

        // Get current page title
        $posttitle = rawurlencode(the_title_attribute('echo=0'));

        // Construct sharing URL without using any script
        $buffer_url = 'https://buffer.com/add?url='.$posturl.'&amp;text='.$posttitle;
        $twitter_url = 'https://twitter.com/intent/tweet?text='.$posttitle.'&amp;url='.$posturl;
        $facebook_url = 'https://www.facebook.com/sharer.php?u='.$posturl;
        $linkedin_url = 'https://www.linkedin.com/shareArticle?mini=true&amp;title='.$posttitle.'&amp;url='.$posturl;

        $postthumb = '';
        $postthumb_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full', false );
        $postthumb = isset($postthumb_attributes[0]) ? $postthumb_attributes[0] : '';

        if(!empty($postthumb)) {
            $pinterest_url = 'https://pinterest.com/pin/create/button/?url='.$posturl.'&amp;media='.$postthumb.'&amp;description='.$posttitle;
        }

        // Add sharing button at the end of page/page content
        $socialcontent = '<div class="freshwp-share-buttons freshwp-clearfix"><span class="freshwp-share-text">'.esc_html(freshwp_share_text()).' </span>';
        if ( !(freshwp_get_option('hide_share_twitter')) ) {
            $socialcontent .= '<a class="freshwp-share-buttons-twitter" href="'.esc_url($twitter_url).'" target="_blank" rel="nofollow" title="'.esc_attr__('Tweet This!', 'freshwp').'"><i class="fab fa-twitter" aria-hidden="true"></i>'.esc_html__('Twitter', 'freshwp').'</a>';
        }
        if ( !(freshwp_get_option('hide_share_facebook')) ) {
            $socialcontent .= '<a class="freshwp-share-buttons-facebook" href="'.esc_url($facebook_url).'" target="_blank" rel="nofollow" title="'.esc_attr__('Share this on Facebook', 'freshwp').'"><i class="fab fa-facebook-f" aria-hidden="true"></i>'.esc_html__('Facebook', 'freshwp').'</a>';
        }
        if ( !(freshwp_get_option('hide_share_pinterest')) && !(empty($postthumb)) ) {
            $socialcontent .= '<a class="freshwp-share-buttons-pinterest" href="'.esc_url($pinterest_url).'" target="_blank" rel="nofollow" title="'.esc_attr__('Share this on Pinterest', 'freshwp').'"><i class="fab fa-pinterest" aria-hidden="true"></i>'.esc_html__('Pinterest', 'freshwp').'</a>';
        }
        if ( !(freshwp_get_option('hide_share_linkedin')) ) {
            $socialcontent .= '<a class="freshwp-share-buttons-linkedin" href="'.esc_url($linkedin_url).'" target="_blank" rel="nofollow" title="'.esc_attr__('Share this on Linkedin', 'freshwp').'"><i class="fab fa-linkedin-in" aria-hidden="true"></i>'.esc_html__('Linkedin', 'freshwp').'</a>';
        }
        $socialcontent .= '</div>';

        return apply_filters( 'freshwp_social_sharing_buttons', $socialcontent );

}