示例#1
0
function _likebtn_get_markup($entity_name, $entity_id, $values = null, $use_entity_name = '', $use_entity_settings = true, $wrap = true, $include_script = false, $like_box = false)
{
    global $wp_version;
    global $likebtn_settings_deprecated;
    $prepared_settings = array();
    if (!$use_entity_name) {
        $use_entity_name = $entity_name;
    }
    // Cut excerpt flag from entity_name
    if ($entity_id !== 'demo') {
        $entity_name = _likebtn_cut_list_flag($entity_name);
    }
    if ($values && isset($values['identifier']) && $values['identifier'] !== '') {
        $identifier = $values['identifier'];
    } else {
        $identifier = _likebtn_entity_to_identifier($entity_name, $entity_id);
    }
    $data = ' data-identifier="' . $identifier . '" ';
    // Site ID
    if (get_option('likebtn_site_id')) {
        $data .= ' data-site_id="' . get_option('likebtn_site_id') . '" ';
    }
    // Authorization check
    if (get_option('likebtn_user_logged_in_' . $use_entity_name) == LIKEBTN_USER_LOGGED_IN_MODAL && !is_user_logged_in()) {
        $values['voting_enabled'] = '0';
        $data .= ' data-clk_modal="' . htmlspecialchars(_likebtn_get_user_logged_in_alert($use_entity_name)) . '" ';
    }
    if (get_option('likebtn_voting_author_' . $use_entity_name) == '1' && get_current_user_id()) {
        $author_id = _likebtn_get_author_id($use_entity_name, $entity_id);
        if ($author_id == get_current_user_id()) {
            $values['voting_enabled'] = '0';
        }
    }
    $likebtn_settings = _likebtn_get_all_settings();
    foreach ($likebtn_settings as $option_name => $option_info) {
        if ($values && isset($values[$option_name])) {
            // if values passed
            $option_value = $values[$option_name];
        } elseif (!$use_entity_settings && !in_array($option_name, $likebtn_settings_deprecated)) {
            // Do not use entity value - use default. Usually in shortcodes.
            $option_value = $option_info['default'];
        } else {
            $option_value = get_option('likebtn_settings_' . $option_name . '_' . $use_entity_name);
        }
        $option_value_prepared = _likebtn_prepare_option($option_name, $option_value);
        $prepared_settings[$option_name] = $option_value_prepared;
        // do not add option if it has default value
        if (isset($likebtn_settings[$option_name]['default']) && $option_value == $likebtn_settings[$option_name]['default'] || $option_value === '' && (isset($likebtn_settings[$option_name]['default']) && $likebtn_settings[$option_name]['default'] == '0')) {
            // option has default value
        } else {
            // Some options need extra procession
            if ($option_name == 'event_handler') {
                $option_name = 'custom_eh';
            }
            $data .= ' data-' . $option_name . '="' . $option_value_prepared . '" ';
        }
    }
    // Add item options
    $entity = null;
    $entity_url = '';
    $entity_title = '';
    $entity_image = '';
    $entity_date = '';
    if ($entity_name == LIKEBTN_ENTITY_COMMENT) {
        $entity = get_comment($entity_id);
        if ($entity) {
            $entity_url = get_comment_link($entity->comment_ID);
            $entity_title = _likebtn_shorten_title($entity->comment_content);
        }
    } else {
        if (in_array($entity_name, array(LIKEBTN_ENTITY_BP_ACTIVITY_POST, LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE, LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT, LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC))) {
            $entity_title = _likebtn_shorten_title(_likebtn_bp_get_activity_title($entity_id));
            if (function_exists('bp_activity_get_permalink')) {
                $entity_url = bp_activity_get_permalink($entity_id);
            }
        } else {
            if ($entity_name == LIKEBTN_ENTITY_BP_MEMBER || $entity_name == LIKEBTN_ENTITY_USER || $entity_name == LIKEBTN_ENTITY_BBP_USER) {
                if (function_exists('bp_core_get_user_displayname')) {
                    $entity_title = bp_core_get_user_displayname($entity_id);
                } else {
                    $entity_title = get_the_author_meta('user_nicename', $entity_id);
                }
                if (function_exists('bp_core_get_user_domain')) {
                    $entity_url = bp_core_get_user_domain($entity_id);
                } else {
                    $entity_url = get_author_posts_url($entity_id);
                }
                $entity_image = _likebtn_get_avatar_url($entity_id);
                $user_info = get_userdata($entity_id);
                if (!empty($user_info) && !empty($user_info->user_registered)) {
                    $entity_date = mysql2date("c", $user_info->user_registered);
                }
            } else {
                $entity = get_post($entity_id);
                if ($entity) {
                    $entity_url = get_permalink($entity->ID);
                    $entity_title = $entity->post_title;
                    $entity_image = _likebtn_get_entity_image($entity_name, $entity_id);
                    $entity_date = mysql2date("c", $entity->post_date);
                }
            }
        }
    }
    if ($entity_url && !$prepared_settings['item_url']) {
        $data .= ' data-item_url="' . $entity_url . '" ';
    }
    if ($entity_title && !$prepared_settings['item_title']) {
        $entity_title = strip_shortcodes($entity_title);
        $entity_title = preg_replace('/\\s+/', ' ', $entity_title);
        $entity_title = htmlspecialchars($entity_title);
        $data .= ' data-item_title="' . $entity_title . '" ';
    }
    if ($entity_image && !$prepared_settings['item_image']) {
        $data .= ' data-item_image="' . $entity_image . '" ';
    }
    if ($entity_date && !$prepared_settings['item_date']) {
        $data .= ' data-item_date="' . $entity_date . '" ';
    }
    // Set engine and plugin info
    $data .= ' data-engine="WordPress" ';
    $data .= ' data-engine_v="' . $wp_version . '" ';
    $plugin_v = LIKEBTN_VERSION;
    if ($plugin_v) {
        $data .= ' data-plugin_v="' . $plugin_v . '" ';
    }
    if (get_option('likebtn_acc_data_correct') == '1') {
        // Proxy
        $prx = admin_url('admin-ajax.php') . '?action=likebtn_prx';
        $data .= ' data-prx="' . $prx . '" ';
    }
    // Event handler
    $data .= ' data-event_handler="likebtn_eh" ';
    $data .= ' data-engine_v="' . $wp_version . '" ';
    $public_url = _likebtn_get_public_url();
    if ($include_script) {
        $markup = <<<MARKUP
<!-- LikeBtn.com BEGIN --><span class="likebtn-wrapper" {$data}></span><script>(function(d, e, s) {a = d.createElement(e);m = d.getElementsByTagName(e)[0];a.async = 1;a.src = s;m.parentNode.insertBefore(a, m)})(document, 'script', '//w.likebtn.com/js/w/widget.js'); if (typeof(LikeBtn) != "undefined") { LikeBtn.init(); }</script><!-- LikeBtn.com END -->
MARKUP;
    } else {
        $markup = <<<MARKUP
<!-- LikeBtn.com BEGIN --><span class="likebtn-wrapper" {$data}></span><!-- LikeBtn.com END -->
MARKUP;
    }
    // HTML before
    $html_before = '';
    if (isset($values['html_before'])) {
        $html_before = $values['html_before'];
    } elseif (get_option('likebtn_html_before_' . $use_entity_name)) {
        $html_before = get_option('likebtn_html_before_' . $use_entity_name);
    }
    if (trim($html_before)) {
        $markup = $html_before . $markup;
    }
    // HTML after
    $html_after = '';
    if (isset($values['html_after'])) {
        $html_after = $values['html_after'];
    } elseif (get_option('likebtn_html_after_' . $use_entity_name)) {
        $html_after = get_option('likebtn_html_after_' . $use_entity_name);
    }
    if (trim($html_after)) {
        $markup = $markup . $html_after;
    }
    if ($wrap) {
        if (get_option('likebtn_wrap_' . $use_entity_name) != '1') {
            $wrap = false;
        }
    }
    if ($wrap) {
        $alignment = get_option('likebtn_alignment_' . $use_entity_name);
        $newline = get_option('likebtn_newline_' . $use_entity_name);
        $style = '';
        if ($newline == '1') {
            $style .= 'clear:both;';
        }
        if ($alignment == LIKEBTN_ALIGNMENT_RIGHT) {
            $style .= 'text-align:right;';
            $markup = '<div class="likebtn_container" style="' . $style . '">' . $markup . '</div>';
        } elseif ($alignment == LIKEBTN_ALIGNMENT_CENTER) {
            $style .= 'text-align:center;';
            $markup = '<div class="likebtn_container" style="' . $style . '">' . $markup . '</div>';
        } else {
            $markup = '<div class="likebtn_container" style="' . $style . '">' . $markup . '</div>';
        }
    }
    // Like box
    if ($like_box && !is_admin()) {
        $like_box = get_option('likebtn_like_box_' . $entity_name);
        if ($like_box) {
            $like_box_html = _likebtn_like_box($identifier, get_option('likebtn_like_box_size_' . $entity_name), get_option('likebtn_like_box_text_' . $entity_name), get_option('likebtn_like_box_type_' . $entity_name));
            if ($like_box == LIKEBTN_LIKE_BOX_BEFORE) {
                $markup = $like_box_html . $markup;
            } else {
                $markup = $markup . $like_box_html;
            }
        }
    }
    return $markup;
}
示例#2
0
function _likebtn_get_markup($entity_name, $entity_id, $values = null, $use_entity_name = '', $use_entity_settings = true, $wrap = true)
{
    global $wp_version;
    global $likebtn_map_entities;
    global $likebtn_settings_deprecated;
    $prepared_settings = array();
    if (!$use_entity_name) {
        $use_entity_name = $entity_name;
    }
    // Cut excerpt flag from entity_name
    if ($entity_id !== 'demo') {
        $entity_name = _likebtn_cut_list_flag($entity_name);
    }
    if ($values && isset($values['identifier']) && $values['identifier'] !== '') {
        $data = ' data-identifier="' . $values['identifier'] . '" ';
    } else {
        $identifier = $entity_name;
        if (!empty($likebtn_map_entities[$entity_name])) {
            $identifier = $likebtn_map_entities[$entity_name];
        }
        $data = ' data-identifier="' . $identifier . '_' . $entity_id . '" ';
    }
    // Site ID
    if (get_option('likebtn_site_id')) {
        $data .= ' data-site_id="' . get_option('likebtn_site_id') . '" ';
    }
    $likebtn_settings = _likebtn_get_all_settings();
    foreach ($likebtn_settings as $option_name => $option_info) {
        if ($values && isset($values[$option_name])) {
            // if values passed
            $option_value = $values[$option_name];
        } elseif (!$use_entity_settings && !in_array($option_name, $likebtn_settings_deprecated)) {
            // Do not use entity value - use default. Usually in shortcodes.
            $option_value = $option_info['default'];
        } else {
            $option_value = get_option('likebtn_settings_' . $option_name . '_' . $use_entity_name);
        }
        $option_value_prepared = _likebtn_prepare_option($option_name, $option_value);
        $prepared_settings[$option_name] = $option_value_prepared;
        // do not add option if it has default value
        if (isset($likebtn_settings[$option_name]['default']) && $option_value == $likebtn_settings[$option_name]['default'] || $option_value === '' && (isset($likebtn_settings[$option_name]['default']) && $likebtn_settings[$option_name]['default'] == '0')) {
            // option has default value
        } else {
            $data .= ' data-' . $option_name . '="' . $option_value_prepared . '" ';
        }
    }
    // Add item options
    $entity = null;
    $entity_url = '';
    $entity_title = '';
    $entity_image = '';
    if ($entity_name == LIKEBTN_ENTITY_COMMENT) {
        $entity = get_comment($entity_id);
        if ($entity) {
            $entity_url = get_comment_link($entity->comment_ID);
            $entity_title = $entity->comment_content;
        }
    } else {
        if (in_array($entity_name, array(LIKEBTN_ENTITY_BP_ACTIVITY_POST, LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE, LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT, LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC))) {
            $entity_title = _likebtn_bp_get_activity_title($entity_id);
            if (function_exists('bp_activity_get_permalink')) {
                $entity_url = bp_activity_get_permalink($entity_id);
            }
        } else {
            if ($entity_name == LIKEBTN_ENTITY_BP_MEMBER) {
                if (function_exists('bp_core_get_username')) {
                    $entity_title = bp_core_get_username($entity_id);
                }
                if (function_exists('bp_core_get_user_domain')) {
                    $entity_url = bp_core_get_user_domain($entity_id);
                }
            } else {
                $entity = get_post($entity_id);
                if ($entity) {
                    $entity_url = get_permalink($entity->ID);
                    $entity_title = $entity->post_title;
                    $entity_image_url = wp_get_attachment_image_src(get_post_thumbnail_id($entity->ID), 'large');
                    if (!empty($entity_image_url[0])) {
                        $entity_image = $entity_image_url[0];
                    }
                }
            }
        }
    }
    if ($entity_url && !$prepared_settings['item_url']) {
        $data .= ' data-item_url="' . $entity_url . '" ';
    }
    if ($entity_title && !$prepared_settings['item_title']) {
        $entity_title = strip_shortcodes($entity_title);
        $entity_title = preg_replace('/\\s+/', ' ', $entity_title);
        $entity_title = htmlspecialchars($entity_title);
        $data .= ' data-item_title="' . $entity_title . '" ';
    }
    if ($entity_image && !$prepared_settings['item_image']) {
        $data .= ' data-item_image="' . $entity_image . '" ';
    }
    // Set engine and plugin info
    $data .= ' data-engine="WordPress" ';
    $data .= ' data-engine_v="' . $wp_version . '" ';
    $plugin_v = LIKEBTN_VERSION;
    if ($plugin_v) {
        $data .= ' data-plugin_v="' . $plugin_v . '" ';
    }
    $public_url = _likebtn_get_public_url();
    $markup = <<<MARKUP
<!-- LikeBtn.com BEGIN --><span class="likebtn-wrapper" {$data}></span><script>(function(d, e, s) {a = d.createElement(e);m = d.getElementsByTagName(e)[0];a.async = 1;a.src = s;m.parentNode.insertBefore(a, m)})(document, 'script', '//w.likebtn.com/js/w/widget.js');
if (typeof(LikeBtn) != "undefined") { LikeBtn.init(); }</script><!-- LikeBtn.com END -->
MARKUP;
    // HTML before
    $html_before = '';
    if (isset($values['html_before'])) {
        $html_before = $values['html_before'];
    } elseif (get_option('likebtn_html_before_' . $use_entity_name)) {
        $html_before = get_option('likebtn_html_before_' . $use_entity_name);
    }
    if (trim($html_before)) {
        $markup = $html_before . $markup;
    }
    // HTML after
    $html_after = '';
    if (isset($values['html_after'])) {
        $html_after = $values['html_after'];
    } elseif (get_option('likebtn_html_after_' . $use_entity_name)) {
        $html_after = get_option('likebtn_html_after_' . $use_entity_name);
    }
    if (trim($html_after)) {
        $markup = $markup . $html_after;
    }
    if ($wrap) {
        if (get_option('likebtn_wrap_' . $use_entity_name) != '1') {
            $wrap = false;
        }
    }
    if ($wrap) {
        $alignment = get_option('likebtn_alignment_' . $use_entity_name);
        $newline = get_option('likebtn_newline_' . $use_entity_name);
        $style = '';
        if ($newline == '1') {
            $style .= 'clear:both;';
        }
        if ($alignment == LIKEBTN_ALIGNMENT_RIGHT) {
            $style .= 'text-align:right;';
            $markup = '<div class="likebtn_container" style="' . $style . '">' . $markup . '</div>';
        } elseif ($alignment == LIKEBTN_ALIGNMENT_CENTER) {
            $style .= 'text-align:center;';
            $markup = '<div class="likebtn_container" style="' . $style . '">' . $markup . '</div>';
        } else {
            $markup = '<div class="likebtn_container" style="' . $style . '">' . $markup . '</div>';
        }
    }
    return $markup;
}