Example #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;
}
Example #2
0
function _likebtn_bp_activity_add($entity_name, $entity_id, $voter_id, $vote_type, $hide_sitewide, $snippet, $snippet_html)
{
    if (!$voter_id) {
        return false;
    }
    if ($vote_type == LIKEBTN_VOTE_LIKE) {
        $type_name = __('liked', LIKEBTN_I18N_DOMAIN);
    } else {
        $type_name = __('disliked', LIKEBTN_I18N_DOMAIN);
    }
    $primary_link = _likebtn_get_entity_url($entity_name, $entity_id);
    $pattern = __('<a href="%user_url%" title="%user_name%">%user_name%</a> %type_name% %entity_name%, <a href="%entity_url%">%entity_title%</a>', LIKEBTN_I18N_DOMAIN);
    $title = _likebtn_get_entity_title($entity_name, $entity_id);
    $action = strtr($pattern, array('%user_url%' => _likebtn_get_entity_url(LIKEBTN_ENTITY_BP_MEMBER, $voter_id), '%user_name%' => bp_core_get_user_displayname($voter_id), '%type_name%' => $type_name, '%entity_url%' => _likebtn_get_entity_url($entity_name, $entity_id), '%entity_name%' => mb_strtolower(_likebtn_get_entity_name_title($entity_name, true)), '%entity_title%' => $title));
    // Add snippet
    $content = "<!--{$entity_name}_{$entity_id}-->";
    if ($snippet == '1') {
        $image_thumbnail = _likebtn_get_entity_image($entity_name, $entity_id, 'thumbnail');
        $excerpt = _likebtn_get_entity_excerpt($entity_name, $entity_id);
        if (!$snippet_html) {
            $snippet_html = LIKEBTN_BP_SNIPPET_TPL;
        }
        $snippet_html = strtr($snippet_html, array('%image_thumbnail%' => $image_thumbnail, '%title%' => $title, '%excerpt%' => $excerpt));
        $content .= $snippet_html;
        // ob_start();
        // include(_likebtn_get_template_path(LIKEBTN_TEMPLATE_ACTIVITY_SNIPPET));
        // $content .= ob_get_contents();
        // ob_get_clean();
    }
    $component = LIKEBTN_BP_COMPONENT_NAME;
    $item_id = $entity_id;
    // Determine hide_sitewide
    $private_group_activity = false;
    $group_id = 0;
    // Set hide_sitewide to true:
    // - BuddyPress forum
    // - Activity update in private group
    if ($entity_name == LIKEBTN_ENTITY_BBP_POST) {
        $group_post_id = (int) get_post_meta($entity_id, '_bbp_forum_id', true);
        if ($group_post_id) {
            $group_id_array = get_post_meta($group_post_id, '_bbp_group_ids', true);
            if ($group_id_array && isset($group_id_array[0])) {
                $group_id = (int) $group_id_array[0];
                //$group = groups_get_group( array( 'group_id' => $group_id ) );
                //$group_permalink = trailingslash*t( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . $group->slug . '/' ) );
                $group_post = get_post($group_post_id);
                if ($group_post && !empty($group_post->post_status) && $group_post->post_status == 'private') {
                    $private_group_activity = true;
                }
            }
        }
    }
    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))) {
        $get_activity = bp_activity_get_specific(array('activity_ids' => $entity_id));
        if (!empty($get_activity['activities']) && !empty($get_activity['activities'][0])) {
            $activity = $get_activity['activities'][0];
            if ($activity->component == 'groups' && isset($activity->item_id) && (int) $activity->hide_sitewide == 1) {
                $private_group_activity = true;
                $group_id = $activity->item_id;
            }
        }
    }
    if ($private_group_activity && $group_id) {
        $hide_sitewide = 1;
        $component = 'groups';
        $item_id = $group_id;
    }
    bp_activity_add(array('user_id' => $voter_id, 'item_id' => $item_id, 'secondary_item_id' => $vote_type, 'primary_link' => $primary_link, 'action' => $action, 'content' => $content, 'component' => $component, 'type' => LIKEBTN_BP_ACTIVITY_TYPE, 'hide_sitewide' => (int) $hide_sitewide));
}