| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
class WatuSharing { |
| 5 |
static function options() { |
| 6 |
global $wpdb; |
| 7 |
if(!empty($_POST['ok']) and check_admin_referer('watu_social_sharing')) { |
| 8 |
$fb_app_id = sanitize_text_field($_POST['facebook_appid']); |
| 9 |
$linkedin_enabled = empty($_POST['linkedin_enabled']) ? 0 : 1; |
| 10 |
$linkedin_title = sanitize_text_field($_POST['linkedin_title']); |
| 11 |
$use_twitter = empty($_POST['use_twitter']) ? 0 : 1; |
| 12 |
$show_count = empty($_POST['show_count']) ? 0 : 1; |
| 13 |
$via = sanitize_text_field($_POST['via']); |
| 14 |
$hashtag = sanitize_text_field($_POST['hashtag']); |
| 15 |
|
| 16 |
update_option('watuproshare_facebook_appid', $fb_app_id, false); |
| 17 |
$linkedin_options = array("enabled" => $linkedin_enabled, "msg"=>wp_kses_post($_POST['linkedin_msg']), |
| 18 |
'title' => $linkedin_title, "facebook_button" => esc_url_raw($_POST['facebook_button']), ); |
| 19 |
update_option('watuproshare_linkedin', $linkedin_options, false); |
| 20 |
$twitter_options = array("use_twitter" => $use_twitter, "show_count" => $show_count, |
| 21 |
"via" => $_POST['via'], "hashtag" => $hashtag, 'large_button' => '', |
| 22 |
"tweet" => wp_kses_post($_POST['tweet']), "twitter_button" => esc_url_raw($_POST['twitter_button'])); |
| 23 |
update_option('watuproshare_twitter', $twitter_options, false); |
| 24 |
} |
| 25 |
|
| 26 |
$appid = get_option('watuproshare_facebook_appid'); |
| 27 |
$linkedin_options = get_option('watuproshare_linkedin'); |
| 28 |
$twitter_options = get_option('watuproshare_twitter'); |
| 29 |
if(@file_exists(get_stylesheet_directory().'/watu/sharing-options.html.php')) include get_stylesheet_directory().'/watu/sharing-options.html.php'; |
| 30 |
else include(WATU_PATH . '/views/sharing-options.html.php'); |
| 31 |
} |
| 32 |
|
| 33 |
// display the social sharing buttons |
| 34 |
static function display() { |
| 35 |
global $wpdb; |
| 36 |
$taking_id = intval($GLOBALS['watu_taking_id']); |
| 37 |
ob_start(); |
| 38 |
// https://developers.facebook.com/docs/sharing/reference/feed-dialog |
| 39 |
$appid = get_option('watuproshare_facebook_appid'); |
| 40 |
|
| 41 |
// get the grade title and description |
| 42 |
$grade_id = $wpdb->get_var($wpdb->prepare("SELECT grade_id FROM ".WATU_TAKINGS." WHERE ID=%d", $taking_id)); |
| 43 |
if(empty($grade_id)) $grade = (object)array("gtitle"=>'None', 'gdescription'=>'None'); |
| 44 |
else $grade = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".WATU_GRADES." WHERE ID=%d", $grade_id)); |
| 45 |
|
| 46 |
// select quiz name |
| 47 |
$quiz_name = $wpdb->get_var($wpdb->prepare("SELECT tE.name FROM ".WATU_EXAMS." tE |
| 48 |
JOIN ".WATU_TAKINGS." tT ON tE.ID = tT.exam_id |
| 49 |
WHERE tT.ID = %d", $taking_id)); |
| 50 |
|
| 51 |
// keep linkedin vars always because they are also used in Facebook |
| 52 |
$linkedin = get_option('watuproshare_linkedin'); |
| 53 |
$linkedin_msg = stripslashes($linkedin['msg']); |
| 54 |
$linkedin_title = stripslashes($linkedin['title']); |
| 55 |
|
| 56 |
// title and description set up? |
| 57 |
if(!empty($linkedin_title)) { |
| 58 |
$linkedin_title = str_replace('{{{grade-title}}}', stripslashes($grade->gtitle), $linkedin_title); |
| 59 |
$linkedin_title = str_replace('{{{quiz-name}}}', stripslashes($quiz_name), $linkedin_title); |
| 60 |
} |
| 61 |
if(!empty($linkedin_msg)) { |
| 62 |
$linkedin_msg = str_replace('{{{grade-title}}}', stripslashes($grade->gtitle), $linkedin_msg); |
| 63 |
$linkedin_msg = str_replace('{{{grade-description}}}', stripslashes($grade->gdescription), $linkedin_msg); |
| 64 |
$linkedin_msg = str_replace('{{{quiz-name}}}', stripslashes($quiz_name), $linkedin_msg); |
| 65 |
$linkedin_msg = str_replace('{{{url}}}', get_permalink($_POST['post_id']), $linkedin_msg); |
| 66 |
} |
| 67 |
|
| 68 |
// if not, default to grade title and desc |
| 69 |
if(empty($linkedin_title)) $linkedin_title = $grade->gtitle; |
| 70 |
if(empty($linkedin_msg)) $linkedin_msg = $grade->gdescription; |
| 71 |
|
| 72 |
$linkedin_title = stripslashes($linkedin_title); |
| 73 |
$linkedin_title = str_replace('"', '"', $linkedin_title); |
| 74 |
$linkedin_title = str_replace("'", '’', $linkedin_title); |
| 75 |
$linkedin_msg = stripslashes($linkedin_msg); |
| 76 |
$linkedin_msg = str_replace('"', '"', $linkedin_msg); |
| 77 |
$linkedin_msg = str_replace("'", '’', $linkedin_msg); |
| 78 |
|
| 79 |
// any picture? |
| 80 |
$picture_str = ''; |
| 81 |
if(strstr($grade->gdescription, '<img')) { |
| 82 |
// find all pictures in the grade descrption |
| 83 |
$html = stripslashes($grade->gdescription); |
| 84 |
$dom = new DOMDocument; |
| 85 |
$dom->loadHTML($html); |
| 86 |
$images = array(); |
| 87 |
foreach ($dom->getElementsByTagName('img') as $image) { |
| 88 |
$src = $image->getAttribute('src'); |
| 89 |
$class = $image->getAttribute('class'); |
| 90 |
$images[] = array('src'=>$src, 'class'=>$class); |
| 91 |
} // end foreach DOM element |
| 92 |
|
| 93 |
if(sizeof($images)) { |
| 94 |
$target_image = $images[0]['src']; |
| 95 |
|
| 96 |
// but check if we have any that are marked with the class |
| 97 |
foreach($images as $image) { |
| 98 |
if(strstr($image['class'], 'watu-share')) { |
| 99 |
$target_image = $image['src']; |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$picture_str = "&picture=".urlencode($target_image); |
| 105 |
} |
| 106 |
} // end searching for image |
| 107 |
|
| 108 |
$twitter_options = get_option('watuproshare_twitter'); |
| 109 |
|
| 110 |
// prepare tweet text |
| 111 |
if(!empty($twitter_options['use_twitter'])) { |
| 112 |
$tweet = stripslashes($twitter_options['tweet']); |
| 113 |
|
| 114 |
if(empty($tweet)) { |
| 115 |
$tweet = stripslashes($grade->gdescription); |
| 116 |
if(empty($tweet)) $tweet = stripslashes($grade->gtitle); |
| 117 |
} |
| 118 |
else { |
| 119 |
$tweet = str_replace('{{{grade-title}}}', stripslashes($grade->gtitle), $tweet); |
| 120 |
$tweet = str_replace('{{{grade-description}}}', stripslashes($grade->gdescription), $tweet); |
| 121 |
$tweet = str_replace('{{{quiz-name}}}', stripslashes($quiz_name), $tweet); |
| 122 |
} |
| 123 |
|
| 124 |
$tweet = substr($tweet, 0, 140); |
| 125 |
} |
| 126 |
|
| 127 |
// buttons |
| 128 |
$facebook_button = empty($linkedin['facebook_button']) ? WATU_URL.'/img/share/facebook.png' : $linkedin['facebook_button']; |
| 129 |
$twitter_button = empty($twitter_options['twitter_button']) ? 'Tweet' : '<img src="'.$twitter_options['twitter_button'].'" alt="Twitter share button">'; |
| 130 |
|
| 131 |
$facebook_button = apply_filters('watu_facebook_button', $facebook_button); |
| 132 |
$twitter_button = apply_filters('watu_twitter_button', $twitter_button); |
| 133 |
?> |
| 134 |
<div><?php if(!empty($appid) ):?> |
| 135 |
<a href="#" title="<?php _e('Share your results on Facebook', 'watu');?>" onclick="Watu.FBShare('<?php echo get_permalink($_POST['post_id'])?>',encodeURIComponent('<?php echo addslashes($linkedin_title)?>'), encodeURIComponent('<?php echo strip_tags(str_replace(array("\r", "\n"), " ", $linkedin_msg));?>'), '<?php echo $picture_str?>');return false;"><img src="<?php echo $facebook_button;?>"></a> |
| 136 |
<?php endif; // end if Facebook |
| 137 |
|
| 138 |
if(!empty($twitter_options['use_twitter'])): |
| 139 |
if(empty($twitter_options['twitter_button'])):?> |
| 140 |
<a href="https://twitter.com/share" class="twitter-share-button watu-twitter-share-button" data-url="<?php echo get_permalink($_POST['post_id'])?>" data-via="<?php echo $twitter_options['via']?>" data-hashtags="<?php echo $twitter_options['hashtag']?>" data-text="<?php echo htmlentities($tweet)?>" <?php if(empty($twitter_options['show_count'])):?>data-count="none"<?php endif;?>><?php echo $twitter_button?></a> |
| 141 |
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> |
| 142 |
<?php else:?> |
| 143 |
<a href="http://twitter.com/share?url=<?php echo get_permalink($_POST['post_id'])?>;text=<?php echo urlencode($tweet)?>;size=l&count=none&hashtags=<?php echo $twitter_options['hashtag']?>&via=<?php echo $twitter_options['via']?>&url=<?php echo get_permalink($_POST['post_id'])?>" target="_blank"> |
| 144 |
<?php echo $twitter_button?> |
| 145 |
</a> |
| 146 |
<?php endif; |
| 147 |
endif;?></div> |
| 148 |
<?php |
| 149 |
$content = ob_get_clean(); |
| 150 |
return $content; |
| 151 |
} |
| 152 |
} |
| 153 |
|