Skip to content

Instantly share code, notes, and snippets.

@manchumahara
Created August 29, 2017 17:40
Show Gist options
  • Save manchumahara/deaaac6a785d6df82f26c45320ff6379 to your computer and use it in GitHub Desktop.
Save manchumahara/deaaac6a785d6df82f26c45320ff6379 to your computer and use it in GitHub Desktop.
if (!function_exists('cbxbookmarkmypost_html')) {
function cbxbookmarkmypost_html($instance, $echo = false)
{
$limit = isset($instance['limit']) ? intval($instance['limit']) : 10;
$orderby = isset($instance['orderby']) ? esc_attr($instance['orderby']) : 'id';
$order = isset($instance['order']) ? esc_attr($instance['order']) : 'desc';
$type = isset($instance['type']) ? esc_attr($instance['type']) : ''; //object type(post types)
$offset = isset($instance['offset']) ? intval($instance['offset']) : 0;
$catid = isset($instance['catid']) ? intval($instance['catid']) : 0;
$allowdelete = isset($instance['allowdelete']) ? intval($instance['allowdelete']) : 0;
$userid_attr = isset($instance['userid']) ? intval($instance['userid']) : 0;
$userid = 0;
if ($userid_attr == 0) {
$userid = get_current_user_id(); //get current logged in user id
} else {
$userid = $userid_attr;
}
if ($userid == 0 || (is_user_logged_in() && $userid != get_current_user_id())) {
$allowdelete = 0;
$privacy = 1;
}
ob_start();
?>
<?php
if ($userid > 0) {
global $wpdb;
$object_types = cbxwpbookmark_object_types(true); //get plain post type as array
$cbxwpbookmrak_table = $wpdb->prefix . 'cbxwpbookmark';
$cat_sql = '';
if ($catid != 0) {
$cat_sql = $wpdb->prepare(' AND cat_id = %d ', $catid);
}
if ($type == '') {
$param = array($userid, $offset, $limit);
$sql = "SELECT * FROM $cbxwpbookmrak_table WHERE user_id = %d $cat_sql group by object_id ORDER BY $orderby $order LIMIT %d, %d";
} else {
$param = array($userid, $type, $offset, $limit);
$sql = "SELECT * FROM $cbxwpbookmrak_table WHERE user_id = %d $cat_sql AND object_type=%s group by object_id ORDER BY $orderby $order LIMIT %d, %d";
}
$items = $wpdb->get_results($wpdb->prepare($sql, $param));
// checking If results are available
if ($items === null || sizeof($items) > 0) {
$post_types = get_post_types();
foreach ($items as $item) {
if (in_array($item->object_type, $object_types)) {
$action_html = ($allowdelete) ? '&nbsp; <a class="cbxbookmark-delete-btn cbxbookmark-post-delete" href="#" data-object_id="' . $item->object_id . '" data-object_type="' . $item->object_type . '" data-bookmark_id="' . $item->id . '"><span></span></a>' : '';
echo '<li ><a href="' . get_permalink($item->object_id) . '">' . get_the_title($item->object_id) . '</a>' . $action_html . '</li>';
} else {
}
}
} else {
echo '<li>' . __('No bookmark found', 'cbxwpbookmark') . '</li>';
}
} else {
$cbxbookmark_login_link = sprintf('Please <a href="%s">%s</a> to view bookmarks',
wp_login_url(get_permalink()),
esc_html__('login', 'cbxwpbookmark')
);
echo '<li>' . $cbxbookmark_login_link . '</li>';
}
?>
<?php
$output = ob_get_clean();
if ($echo) echo '<ul class="cbxwpbookmark-mylist">' . $output . '</ul>';
else return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment