/** * Share buttons field filter. * * Populate share buttons field on theme options page. * * @since 1.0.0 * * @param array $buttons * @return array */ function presscore_themeoptions_add_share_buttons($buttons) { $theme_soc_buttons = presscore_themeoptions_get_social_buttons_list(); if ($theme_soc_buttons && is_array($theme_soc_buttons)) { $buttons = array_merge($buttons, $theme_soc_buttons); } return $buttons; }
/** * Display share buttons. */ function presscore_display_share_buttons($place = '', $options = array()) { global $post; $buttons = of_get_option('social_buttons-' . $place, array()); if (empty($buttons)) { return ''; } $default_options = array('echo' => true, 'class' => array(), 'id' => null); $options = wp_parse_args($options, $default_options); $class = $options['class']; if (!is_array($class)) { $class = explode(' ', $class); } $class[] = 'entry-share'; // get title if (!$options['id']) { $options['id'] = $post->ID; $t = isset($post->post_title) ? $post->post_title : ''; } else { $_post = get_post($options['id']); $t = isset($_post->post_title) ? $_post->post_title : ''; } // get permalink $u = get_permalink($options['id']); $protocol = "http"; if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { $protocol = "https"; } $buttons_list = presscore_themeoptions_get_social_buttons_list(); $html = ''; $html .= '<div class="' . esc_attr(implode(' ', $class)) . '"> <div class="soc-ico">'; foreach ($buttons as $button) { $classes = array('share-button'); $url = ''; $desc = $buttons_list[$button]; $share_title = _x('share', 'share buttons', LANGUAGE_ZONE); $custom = ''; switch ($button) { case 'twitter': $classes[] = 'twitter'; $share_title = _x('tweet', 'share buttons', LANGUAGE_ZONE); $url = add_query_arg(array('status' => urlencode($t . ' ' . $u)), $protocol . '://twitter.com/home'); break; case 'facebook': $url_args = array('s=100', urlencode('p[url]') . '=' . esc_url($u), urlencode('p[title]') . '=' . urlencode($t)); if (has_post_thumbnail($options['id'])) { $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($options['id']), 'full'); if ($thumbnail) { $url_args[] = urlencode('p[images][0]') . '=' . esc_url($thumbnail[0]); } } // mobile args $url_args[] = 't=' . urlencode($t); $url_args[] = 'u=' . esc_url($u); $classes[] = 'facebook'; $url = $protocol . '://www.facebook.com/sharer.php?' . implode('&', $url_args); break; case 'google+': $t = str_replace(' ', '+', $t); $classes[] = 'google'; $url = add_query_arg(array('url' => $u, 'title' => $t), $protocol . '://plus.google.com/share'); break; case 'pinterest': $url = '//pinterest.com/pin/create/button/'; $custom = ' data-pin-config="above" data-pin-do="buttonBookmark"'; // if image if (wp_attachment_is_image($options['id'])) { $image = wp_get_attachment_image_src($options['id'], 'full'); if (!empty($image)) { $url = add_query_arg(array('url' => $u, 'media' => $image[0], 'description' => $t), $url); $custom = ''; } } $classes[] = 'pinterest'; $share_title = _x('pin it', 'share buttons', LANGUAGE_ZONE); break; } $desc = esc_attr($desc); $share_title = esc_attr($share_title); $classes_str = esc_attr(implode(' ', $classes)); $url = esc_url($url); $share_button = sprintf('<a href="%2$s" class="%1$s" target="_blank" title="%3$s"%5$s><span class="assistive-text">%3$s</span><span class="share-content">%4$s</span></a>', $classes_str, $url, $desc, $share_title, $custom); $html .= apply_filters('presscore_share_button', $share_button, $button, $classes, $url, $desc, $share_title, $t, $u); } $html .= '</div> </div>'; $html = apply_filters('presscore_display_share_buttons', $html); if ($options['echo']) { echo $html; } return $html; }
function presscore_get_share_buttons_list($place, $post_id = null) { global $post; $buttons = of_get_option('social_buttons-' . $place, array()); if (empty($buttons)) { return array(); } // get title if (!$post_id) { $_post = $post; $post_id = $_post->ID; } else { $_post = get_post($post_id); } $t = isset($_post->post_title) ? $_post->post_title : ''; // get permalink $u = get_permalink($post_id); $buttons_list = presscore_themeoptions_get_social_buttons_list(); $protocol = is_ssl() ? "https" : "http"; $share_buttons = array(); foreach ($buttons as $button) { $url = ''; $desc = $buttons_list[$button]; $custom = ''; switch ($button) { case 'twitter': $icon_class = 'twitter'; $url = add_query_arg(array('status' => urlencode($t . ' ' . $u)), $protocol . '://twitter.com/home'); break; case 'facebook': $url_args = array('m2w', 's=100', urlencode('p[url]') . '=' . esc_url($u), urlencode('p[title]') . '=' . urlencode($t)); if (has_post_thumbnail($post_id)) { $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full'); if ($thumbnail) { $url_args[] = urlencode('p[images][0]') . '=' . esc_url($thumbnail[0]); } } $icon_class = 'facebook'; $url = $protocol . '://www.facebook.com/sharer.php?' . implode('&', $url_args); break; case 'google+': $t = str_replace(' ', '+', $t); $icon_class = 'google'; $url = add_query_arg(array('url' => $u, 'title' => $t), $protocol . '://plus.google.com/share'); break; case 'pinterest': $icon_class = 'pinterest pinit-marklet'; $url = '//pinterest.com/pin/create/button/'; $custom = ' data-pin-config="above" data-pin-do="buttonBookmark"'; // if image if (wp_attachment_is_image($post_id)) { $image = wp_get_attachment_image_src($post_id, 'full'); if (!empty($image)) { $url = add_query_arg(array('url' => rawurlencode($u), 'media' => rawurlencode($image[0]), 'description' => rawurlencode(apply_filters('get_the_excerpt', $_post->post_content))), $url); $custom = ' data-pin-config="above" data-pin-do="buttonPin"'; $icon_class = 'pinterest'; } } break; case 'linkedin': $bt = get_bloginfo('name'); $url = $protocol . '://www.linkedin.com/shareArticle?mini=true&url=' . urlencode($u) . '&title=' . urlencode($t) . '&summary=&source=' . urlencode($bt); $icon_class = 'linkedin'; break; } $desc = esc_attr($desc); $url = esc_url($url); $share_button = sprintf('<a href="%2$s" class="%1$s" target="_blank" title="%3$s"%4$s><span class="assistive-text">%3$s</span></a>', $icon_class, $url, $desc, $custom); $share_buttons[] = apply_filters('presscore_share_button', $share_button, $button, $icon_class, $url, $desc, $t, $u); } return $share_buttons; }