Exemple #1
0
function synved_social_button_list_markup($context, $vars = null, $buttons = null, $params = null)
{
    $buttons_default = synved_social_service_provider_list($context);
    if ($buttons == null) {
        $buttons = $buttons_default;
    } else {
        $keys = array_keys($buttons);
        foreach ($keys as $key) {
            if ($buttons[$key] == null && isset($buttons_default[$key])) {
                $buttons[$key] = $buttons_default[$key];
            }
        }
    }
    $id = get_the_ID();
    if ($id == null) {
        global $post;
        if ($post != null) {
            $id = $post->ID;
        }
    }
    if (!isset($vars['url']) || !isset($vars['short_url'])) {
        $full_url = synved_option_get('synved_social', 'share_full_url');
        $home_url = home_url();
        $req_uri = $_SERVER['REQUEST_URI'];
        $path = parse_url($home_url, PHP_URL_PATH);
        $path_len = strlen($path);
        if (strtolower(substr($req_uri, 0, $path_len)) == strtolower($path)) {
            $req_uri = substr($req_uri, $path_len);
        }
        $url = home_url($req_uri);
        $short_url = $url;
        if ($id != null && in_the_loop()) {
            $post_full_url = strtolower(get_post_meta($id, 'synved_social_share_full_url', true));
            if ($post_full_url != null) {
                if ($post_full_url == 'yes') {
                    $full_url = true;
                } else {
                    if ($post_full_url == 'no') {
                        $full_url = false;
                    }
                }
            }
        }
        if ($id != null && in_the_loop()) {
            $use_shortlinks = synved_option_get('synved_social', 'use_shortlinks');
            $url = get_permalink($id);
            $short_url = wp_get_shortlink($id);
            if ($short_url != null) {
                if ($use_shortlinks && function_exists('wp_get_shortlink')) {
                    $url = $short_url;
                }
            } else {
                $short_url = $url;
            }
        } else {
            if (is_front_page()) {
                $url = $home_url;
            }
        }
        if (!isset($vars['url'])) {
            $vars['url'] = $url;
        }
        if (!isset($vars['short_url'])) {
            $vars['short_url'] = $short_url;
        }
    }
    if (!isset($vars['image'])) {
        $image_src = null;
        if ($id != null) {
            $image_id = get_post_thumbnail_id($id);
            if ($image_id != null) {
                $src = wp_get_attachment_image_src($image_id, 'full');
                $image_src = $src[0];
            } else {
                $post = get_post($id);
                $match = null;
                if (preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $match) > 0) {
                    $image_src = $match[1];
                }
            }
        }
        $vars['image'] = $image_src;
    }
    if (!isset($vars['title'])) {
        $title = get_the_title();
        // do this encoding to prevent non-tags things, like emoticons, from being stripped, i.e. <8
        $title = preg_replace('/\\<\\s*([^[:alpha:]\\/])/', '&lt;$1', $title);
        $vars['title'] = html_entity_decode(wp_strip_all_tags($title));
    }
    if (!isset($vars['message'])) {
        $message = synved_option_get('synved_social', 'share_message_default');
        if ($message == null) {
            $message = __('Hey check this out', 'social-media-feather');
        }
        $vars['message'] = $message;
    }
    if (!isset($vars['author_wp'])) {
        if ($id != null && in_the_loop()) {
            $author = get_the_author();
            if ($author != null) {
                $vars['author_wp'] = $author;
            }
        }
    }
    if (!isset($vars['author'])) {
        if ($id != null && in_the_loop()) {
            $author = get_the_author_meta('_synved_twitter_handle');
            if ($author == null) {
                $author = get_the_author_meta('twitter');
            }
            if ($author == null) {
                $author = get_the_author();
            }
            if ($author != null) {
                $vars['author'] = $author;
            }
        }
    }
    if (!isset($vars['date'])) {
        if ($id != null && in_the_loop()) {
            $date = get_the_date('', $id);
            if ($date != null) {
                $vars['date'] = $date;
            }
        }
    }
    if (isset($vars['url']) && !isset($vars['url_trimmed'])) {
        $url_trimmed = trim($vars['url']);
        while (substr($url_trimmed, -1) == '/') {
            $url_trimmed = substr($url_trimmed, 0, -1);
        }
        while (strtolower(substr($url_trimmed, -3)) == '%2f') {
            $url_trimmed = substr($url_trimmed, 0, -3);
        }
        $vars['url_trimmed'] = $url_trimmed;
    }
    if (isset($params['class']) && !is_array($params['class'])) {
        $class = explode(' ', $params['class']);
        $params['class'] = array_map('trim', $class);
    }
    if (isset($params['show']) && !is_array($params['show'])) {
        $show = explode(',', $params['show']);
        $params['show'] = array_map('trim', $show);
    }
    if (isset($params['hide']) && !is_array($params['hide'])) {
        $hide = explode(',', $params['hide']);
        $params['hide'] = array_map('trim', $hide);
    }
    $vars = apply_filters('synved_social_markup_variable_list', $vars, $context, $params);
    $params = apply_filters('synved_social_markup_parameter_list', $params, $context, $vars);
    if ($vars != null) {
        $vars = array_map('wp_kses_decode_entities', $vars);
        $vars = urlencode_deep($vars);
        // urlencode_deep converts space characters to + rather than %20 which messes things up
        $vars['message'] = str_ireplace('+', '%20', $vars['message']);
        $vars['title'] = str_ireplace('+', '%20', $vars['title']);
        // urlencode_deep tries to be smart and apostrophes (') to %19 not %27 and double quotes (") to their equivalent open/closed counterparts which doesn't work on most social networks sharings
        $vars['message'] = str_ireplace('%18', '%27', $vars['message']);
        $vars['title'] = str_ireplace('%18', '%27', $vars['title']);
        $vars['message'] = str_ireplace('%19', '%27', $vars['message']);
        $vars['title'] = str_ireplace('%19', '%27', $vars['title']);
        $vars['message'] = str_ireplace('%1c', '%22', $vars['message']);
        $vars['title'] = str_ireplace('%1c', '%22', $vars['title']);
        $vars['message'] = str_ireplace('%1d', '%22', $vars['message']);
        $vars['title'] = str_ireplace('%1d', '%22', $vars['title']);
    }
    $path = synved_social_path();
    $uri = synved_social_path_uri();
    $skin = synved_social_icon_skin_current();
    if (isset($params['skin'])) {
        $skin = synved_social_icon_skin_get($params['skin']);
    }
    $skin_path = isset($skin['path']) ? $skin['path'] : $path . '/image/social/regular/';
    $skin_uri = isset($skin['uri']) ? $skin['uri'] : $uri . '/image/social/regular/';
    $icon_size = synved_option_get('synved_social', 'icon_size');
    $size = 48;
    if ($icon_size != null) {
        $size = $icon_size;
    }
    if (isset($params['size'])) {
        $size = $params['size'];
        if (is_string($size)) {
            $size = strtolower($size);
            $size_parts = explode('x', $size);
            $size = (int) $size_parts[0];
        }
    }
    $icon_spacing = synved_option_get('synved_social', 'icon_spacing');
    $buttons_container = synved_option_get('synved_social', 'buttons_container');
    $buttons_container_type = synved_option_get('synved_social', 'buttons_container_type');
    $buttons_alignment = synved_option_get('synved_social', 'buttons_alignment_' . $context);
    $layout_rtl = synved_option_get('synved_social', 'layout_rtl');
    $spacing = 5;
    $container = 'none';
    $container_type = 'basic';
    $alignment = 'none';
    if ($icon_spacing != null) {
        $spacing = $icon_spacing;
    }
    if (isset($params['spacing'])) {
        $spacing = $params['spacing'];
    }
    if ($buttons_container != null) {
        $container = $buttons_container;
    }
    if ($buttons_container_type != null) {
        $container_type = $buttons_container_type;
    }
    if ($buttons_alignment != null) {
        $alignment = $buttons_alignment;
    }
    if (isset($params['alignment'])) {
        $alignment = $params['alignment'];
    }
    if ($alignment != 'none') {
        if ($container == 'none') {
            $container = $context;
        } else {
            if ($container != 'both' && $container != $context) {
                $container = 'both';
            }
        }
    }
    // Allow parameters to override container after we decide a default based on selected alignment
    if (isset($params['container'])) {
        $container = $params['container'];
    }
    if ($alignment != 'none') {
        $container_type = 'block';
    }
    // Allow parameters to override container after we decide a default based on selected alignment
    if (isset($params['container_type'])) {
        $container_type = $params['container_type'];
    }
    $class = isset($params['class']) ? $params['class'] : null;
    $show = isset($params['show']) ? $params['show'] : null;
    $hide = isset($params['hide']) ? $params['hide'] : null;
    if ($show != null) {
        $button_list = array();
        foreach ($show as $button_key) {
            if (isset($buttons[$button_key])) {
                $button_list[$button_key] = $buttons[$button_key];
                unset($buttons[$button_key]);
            }
        }
        foreach ($buttons as $button_key => $button_item) {
            $button_list[$button_key] = $button_item;
        }
        $buttons = $button_list;
    }
    if ($hide != null) {
        foreach ($hide as $button_key) {
            if (isset($buttons[$button_key])) {
                unset($buttons[$button_key]);
            }
        }
    }
    $out_list = array();
    $out_params = array();
    $image_list = array();
    $icon_resolution = synved_option_get('synved_social', 'icon_resolution');
    $resolutions = array('normal' => $size, 'hidef' => $size * 2);
    if (is_feed()) {
        $icon_resolution = 'single';
    }
    if ($icon_resolution == 'single') {
        $resolutions = array('single' => $size * 2);
    }
    foreach ($resolutions as $resolution_name => $resolution_size) {
        $image_list[$resolution_name] = synved_social_icon_skin_get_image_list($skin, array_keys($buttons), $resolution_size);
    }
    $index = 0;
    $count = count($buttons);
    foreach ($buttons as $button_key => $button_item) {
        $href = $button_item['link'];
        $title = $button_item['title'];
        $matches = null;
        if (preg_match_all('/%%(\\w+)%%/', $href, $matches, PREG_SET_ORDER) > 0) {
            foreach ($matches as $match) {
                $var_key = $match[1];
                $replace = null;
                if (isset($vars[$var_key])) {
                    $replace = $vars[$var_key];
                }
                $href = str_replace($match[0], $replace, $href);
            }
        }
        $icon_sizes = $resolutions;
        foreach ($icon_sizes as $icon_def => $icon_size) {
            $image = $image_list[$icon_def][$button_key];
            $image_size = $image[$icon_size];
            $image_sub = $image_size['sub'];
            $image_path = $image_size['path'];
            $image_uri = $image_size['uri'];
            if (!file_exists($image_path)) {
                $size_list = array_keys($image);
                $image_path = apply_filters('synved_social_button_image_path', $image_path, $image_uri, $icon_size, $image_sub, $skin_path, $skin_uri, $size_list);
                $image_uri = apply_filters('synved_social_button_image_uri', $image_uri, $image_path, $icon_size, $image_sub, $skin_path, $skin_uri, $size_list);
            }
            $style = 'margin:0;';
            if (true) {
                $style .= 'margin-bottom:' . $spacing . 'px;';
            }
            if ($index < $count - 1 || $layout_rtl) {
                $style .= 'margin-right:' . $spacing . 'px;';
            }
            $class_extra = null;
            if ($class != null) {
                $class_extra = ' ' . implode(' ', $class);
            }
            // don't use "nofancybox" because some plugins/themes interpret it as enabling fancybox
            $class_extra .= ' nolightbox';
            $out_button = array('tag' => 'a', 'class' => 'synved-social-button synved-social-button-' . $context . ' synved-social-size-' . $size . ' synved-social-resolution-' . $icon_def . ' synved-social-provider-' . $button_key . $class_extra, 'data-provider' => $button_key, 'target' => $button_key != 'mail' ? '_blank' : null, 'rel' => 'nofollow', 'title' => $title, 'href' => $href, 'style' => 'font-size: 0px; width:' . $size . 'px;' . 'height:' . $size . 'px;' . $style, 'child-list' => array(array('tag' => 'img', 'alt' => $button_key, 'title' => $title, 'class' => 'synved-share-image synved-social-image synved-social-image-' . $context, 'width' => $size, 'height' => $size, 'style' => 'display: inline; width:' . $size . 'px;' . 'height:' . $size . 'px; margin: 0; padding: 0; border: none; box-shadow: none;', 'src' => $image_uri)));
            $out_list[$icon_def][$button_key] = $out_button;
            $out_params[$icon_def][$button_key] = array('icon-resolution' => $icon_def);
        }
        $index++;
    }
    $out = null;
    if ($out_list != null) {
        foreach ($out_list as $def_key => $def_list) {
            $out_list[$def_key] = apply_filters('synved_social_button_list_markup', $def_list, $out_params[$def_key], $context, $vars, $params);
        }
    }
    if ($out_list != null) {
        $container_tag = 'span';
        if ($container_type == 'block') {
            $container_tag = 'div';
        }
        if ($container != 'none' && ($container == 'both' || $container == $context)) {
            $container_style = $alignment != 'none' ? ' style="text-align: ' . $alignment . '"' : null;
            $out .= '<' . $container_tag . ' class="synved-social-container synved-social-container-' . $context . '"' . $container_style . '>';
        }
        foreach ($out_list as $def_key => $def_list) {
            foreach ($def_list as $button_key => $out_item) {
                $out .= synved_social_button_list_markup_item_out($out_item);
            }
        }
        if (synved_option_get('synved_social', 'show_credit')) {
            $out .= '<a class="synved-social-credit" target="_blank" rel="nofollow" title="' . __('WordPress Social Media Feather', 'social-media-feather') . '" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>' . __('by ', 'social-media-feather') . '</span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="' . $uri . '/image/icon.png" /></a>';
        }
        if ($container != 'none' && ($container == 'both' || $container == $context)) {
            $out .= '</' . $container_tag . '>';
        }
    }
    return $out;
}
function synved_social_init()
{
    if (current_user_can('edit_posts') || current_user_can('edit_pages')) {
        if (get_user_option('rich_editing') == 'true') {
            //add_filter('mce_external_plugins', 'synved_social_wp_tinymce_plugin');
            //add_filter('mce_buttons', 'synved_social_wp_tinymce_button');
        }
    }
    $priority = defined('SHORTCODE_PRIORITY') ? SHORTCODE_PRIORITY : 11;
    if (synved_option_get('synved_social', 'shortcode_widgets')) {
        remove_filter('widget_text', 'do_shortcode', $priority);
        add_filter('widget_text', 'do_shortcode', $priority);
    }
    if (function_exists('synved_shortcode_add')) {
        synved_shortcode_add('feather_share', 'synved_social_share_shortcode');
        synved_shortcode_add('feather_follow', 'synved_social_follow_shortcode');
        $size_set = '16,24,32,48,64,96';
        $size_item = synved_option_item('synved_social', 'icon_size');
        if ($size_item != null) {
            $item_set = synved_option_item_set($size_item);
            if ($item_set != null) {
                $set_items = array();
                foreach ($item_set as $set_item) {
                    $item_keys = array_keys($set_item);
                    $set_items[] = $item_keys[0];
                }
                $size_set = implode(',', $set_items);
            }
        }
        $providers_share = array_keys(synved_social_service_provider_list('share'));
        $providers_follow = array_keys(synved_social_service_provider_list('follow'));
        $providers_params = array('show' => __('Specify a comma-separated list of %1$s providers to show and their order, possible values are %2$s', 'social-media-feather'), 'hide' => __('Specify a comma-separated list of %1$s providers to hide, possible values are %2$s', 'social-media-feather'));
        $common_params = array('skin' => __('Specify which skin to use for the icons', 'social-media-feather'), 'size' => sprintf(__('Specify the size for the icons, possible values are %s', 'social-media-feather'), $size_set), 'spacing' => __('Determines how much blank space there will be between the buttons, in pixels', 'social-media-feather'), 'container' => __('Determines whether to wrap the buttons in a container', 'social-media-feather'), 'container_type' => sprintf(__('Determines what type of container to use, possible values are %1$s', 'social-media-feather'), 'basic, block'), 'class' => __('Select additional CSS classes for the buttons, separated by spaces', 'social-media-feather'));
        $share_params = array('url' => __('URL to use for the sharing buttons, default is the current post URL', 'social-media-feather'), 'title' => __('Title to use for the sharing buttons, default is the current post title', 'social-media-feather'));
        $follow_params = array();
        $share_params = array_merge($common_params, $share_params);
        $follow_params = array_merge($common_params, $follow_params);
        foreach ($providers_params as $param_name => $param_value) {
            $share_params[$param_name] = sprintf($param_value, 'share', implode(', ', $providers_share));
            $follow_params[$param_name] = sprintf($param_value, 'follow', implode(', ', $providers_follow));
        }
        synved_shortcode_item_help_set('feather_share', array('tip' => __('Creates a list of buttons for social sharing as selected in the Social Media options', 'social-media-feather'), 'parameters' => $share_params));
        synved_shortcode_item_help_set('feather_follow', array('tip' => __('Creates a list of buttons for social following as selected in the Social Media options', 'social-media-feather'), 'parameters' => $follow_params));
    } else {
        add_shortcode('feather_share', 'synved_social_share_shortcode');
        add_shortcode('synved_feather_share', 'synved_social_share_shortcode');
        add_shortcode('feather_follow', 'synved_social_follow_shortcode');
        add_shortcode('synved_feather_follow', 'synved_social_follow_shortcode');
    }
    //add_action('wp_ajax_synved_social', 'synved_social_ajax_callback');
    //add_action('wp_ajax_nopriv_synved_social', 'synved_social_ajax_callback');
    if (!is_admin()) {
        add_action('wp_enqueue_scripts', 'synved_social_enqueue_scripts');
        add_action('wp_head', 'synved_social_print_styles');
    }
    if (synved_option_get('synved_social', 'automatic_share') || synved_option_get('synved_social', 'automatic_follow')) {
        add_filter('the_content', 'synved_social_wp_the_content', 10, 2);
    }
}