<?php

class Intense_Social_Share extends Intense_Shortcode {

    function __construct() {
        parent::__construct();

        global $intense_visions_options;

        $this->title = __( 'Social Share', 'intense' );
        $this->description = __( 'The social share shortcode will allow a user to share a post or page to Facebook, Google+, Pinterest, Twitter, LinkedIn or StumbleUpon. Each social network has 3-4 buttons styles that can be picked from, within the Intense admin options.', 'intense' );
        $this->category = __( 'Elements', 'intense' );
        $this->show_preview = true;
        $this->preview_content = __( "Social Share Content", 'intense' );
    }

    function shortcode( $atts, $content = null ) {
        extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

        $output = '';
        $facebook = '';
        $googleplus = '';
        $twitter = '';
        $pinterest = '';
        $linkedin = '';
        $stumbleupon = '';
        $email = '';
        $reddit = '';
        $tumblr = '';  

        if ( empty( $share_url ) ) {
            $share_url = get_permalink();
        }  

        if ( isset( $show_email ) && $show_email ) {
            $email = intense_run_shortcode( 'intense_social_icon', array(
                    'type' => "envelope",
                    'label' => "Email",
                    'mode' => "fontawesome",
                    'link' => 'mailto:?subject=' . $title . '&amp;body=' . $title . '&#37;&#48;&#65;&#37;&#48;&#65;' . urlencode( __( "Read More", "intensity" ) ) . '&#37;&#48;&#65;&#37;&#48;&#65;' . $share_url,
                    'size' => $size,
                    'onclick' => "javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" 
                  ) );
            $output .= $email;
        }

        if ( isset( $show_facebook ) && $show_facebook ) {
            if ( $facebook_button == 'fontawesome' ) {
                $facebook = intense_run_shortcode( 'intense_social_icon', array(
                    'type' => "facebook",
                    'label' => "Facebook",
                    'mode' => "fontawesome",
                    'link' => 'http://www.facebook.com/sharer.php?u=' . $share_url . '&amp;t=' . $title,
                    'size' => $size,
                    'onclick' => "javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" 
                ) );
            } else {
                intense_add_script( 'facebook_share' );
                $facebook = '<div style="margin-top: -2px; display:inline-block; vertical-align: top;"><div class="fb-like" data-href="' . $share_url . '" data-layout="' . $facebook_button . '" data-action="like" data-show-faces="' . ( $facebook_faces == '0' ? 'false' : 'true' ) . '" data-share="' . ( $facebook_share == '0' ? 'false' : 'true' ) . '"></div></div>';
            }
            
            $output .= $facebook;
        }

        if ( isset( $show_googleplus ) &&  $show_googleplus ) {
            if ( $googleplus_button == 'medium' ) {
                $annotation = 'none';
            }
            else if ( $googleplus_button == 'medium_inline' ) {
                $annotation = 'inline';
            }
            else {
                $annotation = 'bubble';
            }

            if ( $googleplus_button == 'fontawesome' ) {
                $googleplus = intense_run_shortcode( 'intense_social_icon', array(
                    'type' => "google-plus",
                    'label' => "Google+",
                    'mode' => "fontawesome",
                    'link' => 'https://plus.google.com/share?url=' . $share_url,
                    'size' => $size,
                    'onclick' => "javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" 
                ) ); 
            } else {
                intense_add_script( 'googleplus_share' );
                $googleplus = '<div style="/* padding-right: 20px; */ display:inline;"><div class="g-plusone" data-size="' . ( $googleplus_button == 'tall_bubble' ? 'tall' : 'medium' ) . '" data-annotation="' . $annotation . '" data-href="' . $share_url . '"></div></div>';
            }

            $output .= $googleplus;
        }

        if ( isset( $show_twitter ) && $show_twitter ) {
            if ( $twitter_button == 'fontawesome' ) {
                $twitter = intense_run_shortcode( 'intense_social_icon', array(
                    'type' => "twitter",
                    'label' => "Twitter",
                    'mode' => "fontawesome",
                    'link' => 'http://twitter.com/home?status=' . $title . "%20" . $share_url,
                    'size' => $size,
                    'onclick' => "javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" 
                ) );
            } else {
                intense_add_script( 'twitter_share' );
                $twitter = '<div style="display:inline; /* padding-right: 20px; */"><a href="https://twitter.com/share" class="twitter-share-button" data-url="' . $share_url . '" data-lang="en" data-count="' . $twitter_button . '">Tweet</a></div>';
            }

            $output .= $twitter;
        }

        if ( isset( $show_linkedin ) && $show_linkedin ) {
            if ( $linkedin_button == 'fontawesome' ) {
                $linkedin = intense_run_shortcode( 'intense_social_icon', array(
                    'type' => "linkedin",
                    'label' => "LinkedIn",
                    'mode' => "fontawesome",
                    'link' => 'http://linkedin.com/shareArticle?mini=true&amp;url=' . $share_url . '&amp;title=' . $title,
                    'size' => $size,
                    'onclick' => "javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" 
                ) );
            } else {
                intense_add_script( 'linkedin_share' );
                $linkedin = '<div style="display:inline; padding-right: 20px;"><script type="IN/Share" data-url="' . $share_url . '" ' . ( $linkedin_button == 'none' ? '' : 'data-counter="' . $linkedin_button . '"' ) . '></script></div>';
            }            

            $output .= $linkedin;
        }

        if ( isset( $show_pinterest ) &&  $show_pinterest ) {
            if ( is_numeric( $pinterest_image ) ) {
                $imageid = $pinterest_image;
            } else if ( !empty( $pinterest_image ) ) {
                $imageurl = $pinterest_image;
            }

            if ( !empty( $imageid ) ) {
                $photo = Intense_Photo_Source::get_wordpress_photo( $imageid, 'medium' );                

                $photo_url = $photo['url'];
                $width = $photo['width'];
            } else if ( !empty( $imageurl ) ) {
                $photo_url = $imageurl;
            } else {
                $photo_url = '';
            }

            if ( $pinterest_button == 'fontawesome' ) {
                $pinterest = intense_run_shortcode( 'intense_social_icon', array(
                    'type' => "pinterest",
                    'label' => "Pinterest",
                    'mode' => "fontawesome",
                    'link' => 'http://pinterest.com/pin/create/button/?url=' . $share_url . '&amp;media=' . $photo_url . '&amp;description=' . $title,
                    'size' => $size,
                    'onclick' => "javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" 
                ) );
            } else {
                intense_add_script( 'pinterest_share' );
                $pinterest = '<div style="display:inline; padding-right: 20px;"><a href="//www.pinterest.com/pin/create/button/?url=' . $share_url . '&media=' . $photo_url . '&description=Next%20stop%3A%20Pinterest" data-pin-do="buttonPin" data-pin-config="' . $pinterest_button . '"><img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" /></a></div>';
            }

            $output .= $pinterest;
        }

        if ( isset( $show_reddit ) &&  $show_reddit ) {
            $reddit = intense_run_shortcode( 'intense_social_icon', array(
                'type' => "reddit-square",
                'label' => "Reddit",
                'mode' => "fontawesome",
                'link' => 'http://reddit.com/submit?url=' . $share_url . '&amp;title=' . $title,
                'size' => $size,
                'onclick' => "javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" 
            ) );

            $output .= $reddit;
        }

        if ( isset( $show_tumblr ) && $show_tumblr ) {
            if ( $tumblr_button == 'fontawesome' ) {
                $tumblr_url = str_replace( 'http://', '', str_replace( 'https://', '', $share_url ) );
                $tumblr = intense_run_shortcode( 'intense_social_icon', array(
                    'type' => "tumblr",
                    'label' => "Tumblr",
                    'mode' => "fontawesome",
                    'link' => 'https://www.tumblr.com/share?v=3&amp;u=' . $tumblr_url . '&amp;t=' . $title . '&amp;s=',
                    'size' => $size,
                    'onclick' => "javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" 
                ) );
            } else {
                $twidth = '20';
                $timg = '4';
                intense_add_script( 'tumblr_share' );
                if ( $tumblr_button === 'wide' ) {
                    $twidth = '81';
                    $timg = '1';
                }

                $tumblr = '<div style="display:inline;"><a href="http://www.tumblr.com/share/link?url=' . $share_url . '&name=' . $title . '&description=' . $title . '" title="Share on Tumblr" style="display:inline-block; text-indent:-9999px; overflow:hidden; width:' . $twidth . 'px; height:20px; background:url(\'https://platform.tumblr.com/v1/share_' . $timg . '.png\') top left no-repeat transparent;">Share on Tumblr</a></div>';
            }

            $output .= $tumblr;
        }

        if ( isset( $show_stumbleupon ) && $show_stumbleupon ) {
            intense_add_script( 'stumbleupon_share' );

            $stumbleupon = '<div style="display:inline; padding-right: 20px;"><su:badge layout="' . $stumbleupon_button . '" location="' . $share_url . '"></su:badge></div>';

            $output .= $stumbleupon;
        }

        return $output;
    }

}