| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined('ABSPATH')) exit; // if direct access |
| 4 |
|
| 5 |
|
| 6 |
function social_share_button_sanitize_arr($array) { |
| 7 |
|
| 8 |
foreach ( $array as $key => &$value ) { |
| 9 |
if ( is_array( $value ) ) { |
| 10 |
$value = social_share_button_sanitize_arr($value); |
| 11 |
} |
| 12 |
else { |
| 13 |
$value = sanitize_text_field( $value ); |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
return $array; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
function social_share_button_number_short($number){ |
| 30 |
|
| 31 |
$short_value = $number; |
| 32 |
|
| 33 |
if($number>1000000000000){ |
| 34 |
$short_value = round(($number/1000000000000),1).'t+'; |
| 35 |
} |
| 36 |
else if($number>1000000000){ |
| 37 |
$short_value = round(($number/1000000000),1).'b+'; |
| 38 |
} |
| 39 |
else if($number>1000000){ |
| 40 |
$short_value = round(($number/1000000),1).'m+'; |
| 41 |
} |
| 42 |
else if($number>1000){ |
| 43 |
$short_value = round(($number/1000),1).'k+'; |
| 44 |
} |
| 45 |
|
| 46 |
return $short_value; |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
function social_share_button_filter_buttons_before_extra($html){ |
| 52 |
|
| 53 |
|
| 54 |
$social_share_button_settings = get_option( 'social_share_button_settings' ); |
| 55 |
$display_total_count = $social_share_button_settings['display_total_count']; |
| 56 |
$count_format = $social_share_button_settings['count_format']; |
| 57 |
|
| 58 |
|
| 59 |
// var_dump('Hello'); |
| 60 |
|
| 61 |
$social_share_button_tc_text = get_option('social_share_button_tc_text' ,'Total Share'); |
| 62 |
$social_share_button_tc_themes = get_option('social_share_button_tc_themes'); |
| 63 |
|
| 64 |
if($display_total_count == 'yes'){ |
| 65 |
|
| 66 |
$share_count = get_post_meta( get_the_ID(), 'social_share_button_share_count', true ); |
| 67 |
$total_count = 0; |
| 68 |
//var_dump($share_count); |
| 69 |
if(!empty($share_count)){ |
| 70 |
foreach($share_count as $key=>$count){ |
| 71 |
$total_count +=$count; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
if($count_format=='short'){ |
| 77 |
|
| 78 |
|
| 79 |
$total_count = social_share_button_number_short($total_count); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
$html.= '<span class="total-share '.$social_share_button_tc_themes.'">'; |
| 84 |
$html.= '<i class="total-count-text">'.$social_share_button_tc_text.'</i> '; |
| 85 |
$html.= '<i class="total-count">'.$total_count.'</i> '; |
| 86 |
$html.= '</span>'; |
| 87 |
|
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
return $html; |
| 94 |
|
| 95 |
} |
| 96 |
add_filter('social_share_button_filter_buttons_before','social_share_button_filter_buttons_before_extra'); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
function social_share_button_open_graph(){ |
| 107 |
|
| 108 |
$data = ''; |
| 109 |
|
| 110 |
if(is_singular()){ |
| 111 |
|
| 112 |
$data.= '<meta property="og:title" content="'.get_the_title(get_the_ID()).'" />'; |
| 113 |
$data.= '<meta property="og:url" content="'.get_permalink(get_the_ID()).'" />'; |
| 114 |
|
| 115 |
if(has_post_thumbnail()){ |
| 116 |
|
| 117 |
$team_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'full' ); |
| 118 |
$team_thumb_url = $team_thumb['0']; |
| 119 |
|
| 120 |
$data.= '<meta property="og:image" content="'.$team_thumb_url.'" />'; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
echo wp_kses_post($data); |
| 130 |
|
| 131 |
|
| 132 |
} |
| 133 |
add_action('wp_head','social_share_button_open_graph'); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
add_action('the_content', 'social_share_button_display_auto'); |
| 143 |
|
| 144 |
function social_share_button_display_auto($content){ |
| 145 |
|
| 146 |
$related_post_settings = get_option('social_share_button_settings'); |
| 147 |
|
| 148 |
$post_types_display = isset($related_post_settings['post_types_display']) ? $related_post_settings['post_types_display'] : array(); |
| 149 |
|
| 150 |
$post_id = get_the_ID(); |
| 151 |
$post_type = get_post_type($post_id); |
| 152 |
|
| 153 |
//var_dump($post_types_display); |
| 154 |
|
| 155 |
$posttype = $post_type; |
| 156 |
$enable = isset($post_types_display[$posttype]['enable']) ? $post_types_display[$posttype]['enable'] : 'no'; |
| 157 |
|
| 158 |
//var_dump($enable); |
| 159 |
|
| 160 |
if($enable == 'yes'){ |
| 161 |
|
| 162 |
$content_position = isset($post_types_display[$posttype]['content_position']) ? $post_types_display[$posttype]['content_position'] : array(''); |
| 163 |
$paragraph_positions = isset($post_types_display[$posttype]['paragraph_positions']) ? $post_types_display[$posttype]['paragraph_positions'] : ''; |
| 164 |
$headline_text = isset($post_types_display[$posttype]['headline_text']) ? $post_types_display[$posttype]['headline_text'] : ''; |
| 165 |
|
| 166 |
$social_share_button_html = do_shortcode('[social_share_button post_id="'.$post_id.'" headline="'.$headline_text.'"]'); |
| 167 |
|
| 168 |
$paragraph_positions = !empty($paragraph_positions) ? explode(',', $paragraph_positions) : array(); |
| 169 |
|
| 170 |
|
| 171 |
//var_dump($paragraph_positions); |
| 172 |
|
| 173 |
$html = ''; |
| 174 |
|
| 175 |
if( in_array('before', $content_position)){ |
| 176 |
|
| 177 |
$html .= do_shortcode('[social_share_button post_id="'.get_the_id().'" headline="'.$headline_text.'"]'); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
if(!empty($paragraph_positions) && is_singular($post_type)){ |
| 182 |
$split_by = "\n"; |
| 183 |
$content_blocks = explode( $split_by, $content); |
| 184 |
$content_blocks = array_filter($content_blocks); |
| 185 |
$content_blocks_count = count($content_blocks); |
| 186 |
|
| 187 |
$positions = array(); |
| 188 |
foreach ($paragraph_positions as $position){ |
| 189 |
if(strpos($position, 'N') !== false){ |
| 190 |
|
| 191 |
$position = str_replace('N', $content_blocks_count, $position ); |
| 192 |
|
| 193 |
if(strpos($position, '-') !== false){ |
| 194 |
|
| 195 |
$position = explode('-', $position); |
| 196 |
|
| 197 |
$max_pos = (int)$position[0]; |
| 198 |
$sub_pos = (int)$position[1]; |
| 199 |
|
| 200 |
$position = $max_pos - $sub_pos; |
| 201 |
} |
| 202 |
|
| 203 |
$positions[] = $position; |
| 204 |
}else{ |
| 205 |
$positions[] = $position; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
$content_html = ''; |
| 211 |
|
| 212 |
$i = 1; |
| 213 |
foreach ($content_blocks as $content_block){ |
| 214 |
|
| 215 |
if(in_array($i, $positions)){ |
| 216 |
$content_html .= $content_block.$social_share_button_html; |
| 217 |
}else{ |
| 218 |
$content_html .= $content_block; |
| 219 |
} |
| 220 |
|
| 221 |
$i++; |
| 222 |
} |
| 223 |
|
| 224 |
$html .= $content_html; |
| 225 |
|
| 226 |
}else{ |
| 227 |
$html .= $content; |
| 228 |
} |
| 229 |
|
| 230 |
if( in_array('after', $content_position)){ |
| 231 |
|
| 232 |
$html .= do_shortcode('[social_share_button post_id="'.get_the_id().'" headline="'.$headline_text.'"]'); |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
return $html; |
| 237 |
|
| 238 |
}else{ |
| 239 |
return $content; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
add_action('the_excerpt', 'social_share_button_display_on_excerpt'); |
| 255 |
|
| 256 |
function social_share_button_display_on_excerpt($excerpt){ |
| 257 |
|
| 258 |
$related_post_settings = get_option('social_share_button_settings'); |
| 259 |
|
| 260 |
$post_types_display = isset($related_post_settings['post_types_display']) ? $related_post_settings['post_types_display'] : array(); |
| 261 |
|
| 262 |
global $post; |
| 263 |
$posttype = isset($post->post_type) ? $post->post_type : ''; |
| 264 |
$enable = isset($post_types_display[$posttype]['enable']) ? $post_types_display[$posttype]['enable'] : 'no'; |
| 265 |
|
| 266 |
if($enable == 'yes'){ |
| 267 |
|
| 268 |
$excerpt_position = isset($post_types_display[$posttype]['excerpt_position']) ? $post_types_display[$posttype]['excerpt_position'] : array(); |
| 269 |
$headline_text = isset($post_types_display[$posttype]['headline_text']) ? $post_types_display[$posttype]['headline_text'] : ''; |
| 270 |
|
| 271 |
|
| 272 |
$html = ''; |
| 273 |
|
| 274 |
if( in_array('before', $excerpt_position)){ |
| 275 |
|
| 276 |
$html .= do_shortcode('[social_share_button post_id="'.get_the_id().'" headline="'.$headline_text.'"]'); |
| 277 |
} |
| 278 |
$html .= $excerpt; |
| 279 |
|
| 280 |
if( in_array('after', $excerpt_position)){ |
| 281 |
|
| 282 |
$html .= do_shortcode('[social_share_button post_id="'.get_the_id().'" headline="'.$headline_text.'"]'); |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
return $html; |
| 288 |
|
| 289 |
}else{ |
| 290 |
return $excerpt; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
function social_share_button_ajax_update_count(){ |
| 317 |
|
| 318 |
$nonce = sanitize_text_field(wp_unslash($_POST['nonce'])); |
| 319 |
|
| 320 |
|
| 321 |
if (wp_verify_nonce($nonce, 'social_share_button_nonce')) { |
| 322 |
|
| 323 |
die(); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
$current_site_id = isset($_POST['site_id']) ? sanitize_text_field(wp_unslash($_POST['site_id'])) : ''; |
| 328 |
$post_id = isset($_POST['post_id']) ? (int)sanitize_text_field(wp_unslash($_POST['post_id'])) : ''; |
| 329 |
|
| 330 |
$social_share_button_sites = get_option( 'social_share_button_sites' ); |
| 331 |
$share_count = get_post_meta( $post_id, 'social_share_button_share_count', true ); |
| 332 |
|
| 333 |
|
| 334 |
do_action('social_share_button_update_count', $post_id, $current_site_id); |
| 335 |
|
| 336 |
foreach($social_share_button_sites as $site_key=>$site_info){ |
| 337 |
$site_id = $site_info['id']; |
| 338 |
if($current_site_id == $site_id){ |
| 339 |
$social_share_button_share_count[$site_id] = (int)$share_count[$site_id]+1; |
| 340 |
|
| 341 |
} |
| 342 |
else{ |
| 343 |
$social_share_button_share_count[$site_id] = (int)$share_count[$site_id]; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
// update count |
| 349 |
update_post_meta( $post_id, 'social_share_button_share_count', $social_share_button_share_count ); |
| 350 |
|
| 351 |
|
| 352 |
die(); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
add_action('wp_ajax_social_share_button_ajax_update_count', 'social_share_button_ajax_update_count'); |
| 358 |
add_action('wp_ajax_nopriv_social_share_button_ajax_update_count', 'social_share_button_ajax_update_count'); |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
function social_share_button_inline_css() { |
| 363 |
|
| 364 |
global $socialShareButtonCss; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
if ( empty( $socialShareButtonCss ) ) { |
| 369 |
return; |
| 370 |
} |
| 371 |
|
| 372 |
$social_share_button_settings = get_option( 'social_share_button_settings' ); |
| 373 |
$custom_css = isset($social_share_button_settings['custom_css']) ? $social_share_button_settings['custom_css'] : ''; |
| 374 |
|
| 375 |
|
| 376 |
wp_register_style( 'social-share-button-style', false ); |
| 377 |
wp_enqueue_style( 'social-share-button-style' ); |
| 378 |
|
| 379 |
$socialShareButtonCss = $custom_css; |
| 380 |
|
| 381 |
wp_add_inline_style( 'social-share-button-style', $socialShareButtonCss ); |
| 382 |
} |
| 383 |
add_action( 'wp_footer', 'social_share_button_inline_css' ); |
| 384 |
|
| 385 |
|