File: //opt/plugins/cbxwpbookmark/public/class-cbxwpbookmark-public.php
<?php
/**
* The public-facing functionality of the plugin.
*
* @link codeboxr.com
* @since 1.0.0
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the admin-specific stylesheet and JavaScript.
*
* @package CBXWPbookmark
* @subpackage CBXWPbookmark/public
* @author CBX Team <info@codeboxr.com>
*/
class CBXWPBookmark_Public {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
private $settings_api;
/**
* Initialize the class and set its properties.
*
* @param string $plugin_name The name of the plugin.
* @param string $version The version of this plugin.
*
* @since 1.0.0
*
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->settings_api = new CBXWPBookmark_Settings_API();
}//end constructor
public function init_shortcodes() {
//bookmark button using shortcode
add_shortcode( 'cbxwpbookmarkbtn', array( $this, 'bookmark_button_shortcode' ) ); //bookmark button
//show bookmark list using shortcode
add_shortcode( 'cbxwpbookmark', array( $this, 'my_bookmarked_posts_shortcode' ) ); //my bookmarks
//show most bookmarked posts using shortcode
add_shortcode( 'cbxwpbookmark-most', array( $this, 'most_bookmarked_posts_shortcode' ) ); //bookmarked post
//show bookmark categories using shortcode
add_shortcode( 'cbxwpbookmark-mycat', array( $this, 'my_bookmark_categories_shortcode' ) );//bookmark category
}//end init_shortcodes
/**
* Register Widget
*/
public function init_widgets() {
register_widget( "CBXWPBookmark_Widget" ); //my bookmarks
register_widget( "CBXWPBookmark_Category" ); //my bookmark category //if user category mode enabled
register_widget( "CBXWPBookmarkedMost_Widget" ); //most bookmarked items
}//end init_widgets
/**
*
* @global type $wpdb
*/
public function find_category() {
global $wpdb;
check_ajax_referer( 'cbxbookmarknonce', 'security' );
$setting = $this->settings_api;
$bookmark_mode = $setting->get_option( 'bookmark_mode', 'cbxwpbookmark_basics', 'user_cat' );
$category_table = $wpdb->prefix . 'cbxwpbookmarkcat';
$bookmark_table = $wpdb->prefix . 'cbxwpbookmark';
$user_id = get_current_user_id(); //get the current logged in user id
$object_id = intval( $_POST['object_id'] );
$object_type = isset( $_POST['object_type'] ) ? esc_attr( $_POST['object_type'] ) : 'post'; //post, page, user, product, any thing custom
if ( $bookmark_mode == 'user_cat' ) {
$cats_by_user = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $category_table WHERE user_id = %d", array( $user_id ) ), ARRAY_A );
} else {
$cats_by_user = $wpdb->get_results( "SELECT * FROM $category_table WHERE 1", ARRAY_A );
}
$post_in_cats_t = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT cat_id FROM $bookmark_table WHERE object_type = %s AND user_id = %d AND object_id = %d", array( $object_type, $user_id, $object_id ) ), ARRAY_A
);
//
$post_in_cats = array();
foreach ( $post_in_cats_t as $cat ) {
$post_in_cats[] = $cat['cat_id'];
}
foreach ( $cats_by_user as &$row ) {
if ( in_array( $row['id'], $post_in_cats ) ) {
$row['incat'] = 1;
} else {
$row['incat'] = 0;
}
}
$bookmark_total = CBXWPBookmarkHelper::getTotalBookmark( $object_id );
$bookmark_by_user = CBXWPBookmarkHelper::isBookmarkedByUser( $object_id );
$message = array();
//code 1 = category found
//code 0 = category not found
$cats_by_user = apply_filters( 'cbxwpbookmark_user_cats_found', $cats_by_user, $user_id, $object_id, $object_type );
if ( $cats_by_user != null ) {
$message['code'] = 1;
$message['msg'] = esc_html__( 'Categories loaded', 'cbxwpbookmark' );
if ( $cats_by_user !== false ) {
$message['cats'] = json_encode( $cats_by_user );
}
} else {
$message['code'] = 0;
$message['msg'] = esc_html__( 'Category not found, create one.', 'cbxwpbookmark' );
}
$message['bookmark_count'] = $bookmark_total;
$message['bookmark_byuser'] = ( $bookmark_by_user ) ? 1 : 0;
echo json_encode( $message );
wp_die();
}//end find_category
/**
* Show Bookmark button before or after the content
*
* @param $content
*/
public function bookmark_auto_integration( $content ) {
//disable for woocommerce pages
if ( function_exists( 'is_account_page' ) ) {
if ( is_account_page() ) {
return $content;
}
}
$setting = $this->settings_api;
$user_id = get_current_user_id();
global $post;
$post_id = $post->ID;
$post_type = $post->post_type;
$post_types_to_show_bookmark = $setting->get_option( 'cbxbookmarkposttypes', 'cbxwpbookmark_basics', array(
'post',
'page'
) );
$position = $setting->get_option( 'cbxbookmarkpostion', 'cbxwpbookmark_basics', 'after_content' );
$skip_ids = $setting->get_option( 'skip_ids', 'cbxwpbookmark_basics', '' );
$skip_roles = $setting->get_option( 'skip_roles', 'cbxwpbookmark_basics', '' );
$show_in_archive = intval( $setting->get_option( 'showinarchive', 'cbxwpbookmark_basics', 0 ) );
$show_in_home = intval( $setting->get_option( 'showinhome', 'cbxwpbookmark_basics', 0 ) );
$showcount = intval( $setting->get_option( 'showcount', 'cbxwpbookmark_basics', 0 ) );
//if disabled return content
if ( $position == 'disable' ) {
return $content;
}
//global $wp_the_query;
//write_log(is_main_query());
//write_log($post_id);
/*if(is_main_query() && is_singular()){
if($skip_ids != ''){
$skip_ids_arr = explode( ',', $skip_ids );
if ( sizeof( $skip_ids_arr ) > 0 ) {
if ( in_array( $skip_ids_arr, $skip_ids_arr ) ) {
return $content;
}
}
}
}*/
//if(!is_main_query()) return $content;
//check if the bookmark button is allowed
if ( ! in_array( $post_type, $post_types_to_show_bookmark ) ) {
return $content;
}
//if archive and show archive false then return content
if ( ! $show_in_archive && is_archive() ) {
return $content;
}
//if home and show in home false then return content
if ( ! $show_in_home && ( is_home() && is_front_page() ) ) {
return $content;
}
//grab bookmark button html
if ( is_array( $skip_roles ) ) {
$skip_roles = implode( ',', $skip_roles );
}
$auto_integration_ok = true;
$bookmark_html = apply_filters( 'cbxwpbookmark_auto_integration', $auto_integration_ok, $post_id, $post_type, $showcount, $skip_ids, $skip_roles ) ? show_cbxbookmark_btn( $post_id, $post_type, $showcount, '', $skip_ids, $skip_roles ) : '';
//attach the bookmark button html before or after the content
if ( $position == 'after_content' ) {
return $content . $bookmark_html;
} elseif ( $position == 'before_content' ) {
return $bookmark_html . $content;
}
}//end bookmark_auto_integration
/**
* Render bookmark button - shortcode callback
*
* @param $attr
*
* @return string
*/
public function bookmark_button_shortcode( $attr ) {
// Checking Available Parameter
global $post;
$attr = shortcode_atts(
array(
'object_id' => $post->ID,
'object_type' => $post->post_type,
'show_count' => 1,
'extra_wrap_class' => '',
'skip_ids' => '',
'skip_roles' => '' //example 'administrator, editor, author, contributor, subscriber'
), $attr, 'cbxwpbookmarkbtn' );
extract( $attr );
return show_cbxbookmark_btn( $object_id, $object_type, $show_count, $extra_wrap_class, $skip_ids, $skip_roles );
}//end bookmark_button_shortcode
/**
* Bookmarked Posts shortcode callback
*
* @param $attr
*
* @return string
*/
public function my_bookmarked_posts_shortcode( $attr ) {
// Checking Available Parameter
global $wpdb;
$cbxwpbookmrak_table = $wpdb->prefix . 'cbxwpbookmark';
$cbxwpbookmak_category_table = $wpdb->prefix . 'cbxwpbookmarkcat';
$setting = $this->settings_api;
$bookmark_mode = $setting->get_option( 'bookmark_mode', 'cbxwpbookmark_basics', 'user_cat' );
$current_user_id = get_current_user_id();
$attr = shortcode_atts(
array(
'userid' => $current_user_id,
'order' => 'DESC',
'orderby' => 'id', //id, object_id, object_type
'limit' => 10,
'offset' => 0,
'type' => '', //post or object type, multiple post type in comma
'loadmore' => 1, //this is shortcode only params
'catid' => '', //category id
'cattitle' => 1, //show category title,
'catcount' => 1, //show item count per category
'allowdelete' => 0
), $attr, 'cbxwpbookmark' );
//if the url has cat id (cbxbmcatid get param) thenm use it or try it from shortcode
$attr['catid'] = ( isset( $_GET['cbxbmcatid'] ) && $_GET['cbxbmcatid'] != null ) ? sanitize_text_field( $_GET['cbxbmcatid'] ) : $attr['catid'];
if ( $attr['catid'] == 0 ) {
$attr['catid'] = '';
}//compatibility with previous shortcode default values
$attr['catid'] = array_filter( explode( ',', $attr['catid'] ) );
//if the shortcode page linked with user id
if ( isset( $_GET['userid'] ) && absint( $_GET['userid'] ) > 0 ) {
$attr['userid'] = absint( $_GET['userid'] );
}
if ( $attr['userid'] == '' || $attr['userid'] == 0 ) {
$attr['userid'] = $current_user_id;
}
$attr['type'] = array_filter( explode( ',', $attr['type'] ) );
extract( $attr );
$show_loadmore_html = '';
$loadmore_busy_icon = '';
$wpbm_ajax_icon = CBXWPBOOKMARK_ROOT_URL . 'assets/img/busy.gif';
$privacy = 2; //all
if ( $userid == 0 || ( $userid != get_current_user_id() ) ) {
$privacy = 1;
$allowdelete = 0;
$attr['privacy'] = $privacy;
$attr['allowdelete'] = $allowdelete;
}
$total_sql = '';
$cat_sql = '';
$category_privacy_sql = '';
$type_sql = '';
//if ($catid != 0 && $bookmark_mode != 'no_cat')
if ( is_array( $catid ) && sizeof( $catid ) > 0 && ( $bookmark_mode != 'no_cat' ) ) {
$cats_ids_str = implode( ', ', $catid );
$cat_sql .= " AND cat_id IN ($cats_ids_str) ";
}
//get cats
if ( $bookmark_mode == 'user_cat' ) {
//same user seeing
if ( $privacy != 2 ) {
$cats = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $cbxwpbookmak_category_table WHERE privacy = %d", intval( $privacy ) ), ARRAY_A );
} else {
$cats = $wpdb->get_results( "SELECT * FROM $cbxwpbookmak_category_table WHERE 1", ARRAY_A );
}
$cats_ids = array();
if ( is_array( $cats ) && sizeof( $cats ) > 0 ) {
foreach ( $cats as $cat ) {
$cats_ids[] = intval( $cat['id'] );
}
$cats_ids_str = implode( ', ', $cats_ids );
$category_privacy_sql .= " AND cat_id IN ($cats_ids_str) ";
}
}
if ( sizeof( $type ) == 0 ) {
$param = array( $userid );
$total_sql .= "SELECT COUNT(*) FROM (select count(*) as totalobject FROM $cbxwpbookmrak_table WHERE user_id = %d $cat_sql $category_privacy_sql group by object_id ORDER BY $orderby $order) AS TotalData";
} else {
$type_sql .= " AND object_type IN ('" . implode( "',' ", $type ) . "') ";
$param = array( $userid );
$total_sql .= "SELECT COUNT(*) FROM (select count(*) as totalobject FROM $cbxwpbookmrak_table WHERE user_id = %d $cat_sql $type_sql $category_privacy_sql group by object_id ORDER BY $orderby $order) AS TotalData";
}
$total_count = intval( $wpdb->get_var( $wpdb->prepare( $total_sql, $param ) ) );
$total_page = ceil( $total_count / $limit );
$extra_css_class = '';
if ( $attr['loadmore'] == 1 && $total_page > 1 ) {
$extra_css_class = 'cbxwpbookmark-mylist-sc-more';
$offset += $limit;
$loadmore_busy_icon = '<span data-busy="0" class="cbxwpbm_ajax_icon">' . esc_html__( 'Loading ...', 'cbxwpbookmark' ) . '<img src = "' . $wpbm_ajax_icon . '"/></span>';
$show_loadmore_html = '<p class="cbxbookmark-more-wrap"><a href="#" class="cbxbookmark-more" data-cattitle="' . $cattitle . '" data-order="' . $order . '" data-orderby="' . $orderby . '" data-userid="' . $userid . '" data-limit="' . $limit . '" data-offset="' . $offset . '" data-catid="' . implode( ',', $catid ) . '" data-type="' . implode( ',', $type ) . '" data-totalpage="' . $total_page . '" data-currpage="1" data-allowdelete="' . intval( $allowdelete ) . '">' . esc_html__( 'Load More', 'cbxwpbookmark' ) . '</a>' . $loadmore_busy_icon . '</p>';
}
$category_title = '';
//if only bookmark mode is user or global cat
if ( intval( $cattitle ) && $bookmark_mode != 'no_cat' ) {
//if($catid == 0) {
if ( sizeof( $catid ) == 0 ) {
$category_title = '<h4 class="cbxwpbookmark-mylist-cattitle">' . esc_html__( 'All Bookmarks', 'cbxwpbookmark' ) . '</h4>';
} else {
if ( sizeof( $catid ) == 1 ) {
//$cat_info = CBXWPBookmarkHelper::getBookmarkCategoryById(intval($catid));
$cat_info = CBXWPBookmarkHelper::getBookmarkCategoryById( reset( $catid ) );
if ( is_array( $cat_info ) && sizeof( $cat_info ) > 0 ) {
$catcount_html = '';
if ( $catcount ) {
//$cat_bookmark_count = CBXWPBookmarkHelper::getTotalBookmarkByCategory($catid);
$cat_bookmark_count = CBXWPBookmarkHelper::getTotalBookmarkByCategory( reset( $catid ) );
$catcount_html = '<i>(' . number_format_i18n( $cat_bookmark_count ) . ')</i>';
}
$category_title = '<h4 class="cbxwpbookmark-mylist-cattitle">' . $cat_info['cat_name'] . $catcount_html . '</h4>';
}
}
}
}
return '<div class="cbxwpbookmark-mylist-wrap">' . $category_title . '<ul class="cbxwpbookmark-mylist cbxwpbookmark-mylist-sc ' . $extra_css_class . '" >' . cbxbookmarkmypost_html( $attr ) . '</ul>' . $show_loadmore_html . '</div>';
}//end my_bookmarked_posts_shortcode
/**
* My Bookmarked posts Load more ajax hook
*/
public function bookmark_loadmore() {
check_ajax_referer( 'cbxbookmarknonce', 'security' );
$instance = array();
$message = array();
if ( isset( $_POST['limit'] ) && $_POST['limit'] != null ) {
$instance['limit'] = intval( $_POST['limit'] );
}
if ( isset( $_POST['offset'] ) && $_POST['offset'] != null ) {
$instance['offset'] = intval( $_POST['offset'] );
}
if ( isset( $_POST['catid'] ) ) {
$catid = sanitize_text_field( $_POST['catid'] );
$instance['catid'] = array_filter( explode( ',', $catid ) );
}
if ( isset( $_POST['type'] ) ) {
$type = sanitize_text_field( $_POST['type'] );
$instance['type'] = array_filter( explode( ',', $type ) );
}
if ( isset( $_POST['userid'] ) && $_POST['userid'] != 0 ) {
$instance['userid'] = intval( $_POST['userid'] );
}
if ( isset( $_POST['order'] ) && $_POST['order'] != null ) {
$instance['order'] = esc_attr( $_POST['order'] );
}
if ( isset( $_POST['orderby'] ) && $_POST['orderby'] != null ) {
$instance['orderby'] = esc_attr( $_POST['orderby'] );
}
$instance['allowdelete'] = intval( $_POST['allowdelete'] );
if ( function_exists( 'cbxbookmarkmypost_html' ) && cbxbookmarkmypost_html( $instance, false ) ) {
$message['code'] = 1;
$message['data'] = cbxbookmarkmypost_html( $instance, false );
} else {
$message['code'] = 0;
}
echo json_encode( $message );
wp_die();
}//end bookmark_loadmore
/**
* Shows any user's bookmarked categories using shortcode
*
* @param $attr
*
* @return string
*/
public function my_bookmark_categories_shortcode( $attr ) {
$setting = new CBXWPBookmark_Settings_API();
$bookmark_mode = $setting->get_option( 'bookmark_mode', 'cbxwpbookmark_basics', 'user_cat' );
$current_user_id = get_current_user_id();
$attr = shortcode_atts(
array(
'userid' => $current_user_id,
'order' => "ASC", //DESC, ASC
'orderby' => "cat_name", //other possible values title, id
'privacy' => 2, //1 = public 0 = private 2= ignore
'display' => 0, //0 = list 1= dropdown,
'show_count' => 0,
'allowedit' => 0,
'show_bookmarks' => 0, //show bookmark as sublist on click on category
'base_url' => cbxwpbookmarks_mybookmark_page_url()
), $attr, 'cbxwpbookmark-mycat'
);
//if the shortcode page linked with user id
if ( isset( $_GET['userid'] ) && absint( $_GET['userid'] ) > 0 ) {
$attr['userid'] = absint( $_GET['userid'] );
}
if ( $attr['userid'] == '' || $attr['userid'] == 0 ) {
$attr['userid'] = $current_user_id;
}
$output = '';
//if other than no_cat mode we will have category
if ( $bookmark_mode != 'no_cat' ) {
$output .= ( intval( $attr['display'] ) == 0 ) ? '<ul class="cbxbookmark-category-list cbxbookmark-category-list-' . $bookmark_mode . ' cbxbookmark-category-list-sc">' : '';
$output .= cbxbookmarkmycat_html( $attr );
$output .= ( intval( $attr['display'] ) == 0 ) ? '</ul>' : '';
} else {
//this message is better to hide
//$output = __( '<strong>Sorry, This widget is not compatible as per setting. This widget can be used only if bookmark mode is "User owns category" or "Global Category"</strong>', 'cbxwpbookmark' );
}
return $output;
}//end my_bookmark_categories_shortcode
/**
* Most bookmarked post shortcode
*
* @param $attr
*
* @return string
*/
public function most_bookmarked_posts_shortcode( $attr ) {
$attr = shortcode_atts(
array(
'orderby' => 'object_count', //id, object_id, object_type, object_count
'order' => 'DESC',
'limit' => 10,
'type' => '', //db col name object_type, post types eg, post, page, any custom post type, for multiple comma separated
'daytime' => 0, // 0 means all time, any numeric values as days
'show_count' => 1,
'show_thumb' => 1,
'ul_class' => 'product_list_widget',
'li_class' => ''
), $attr, 'cbxwpbookmark-most' );
$style_attr = array(
'ul_class' => $attr['ul_class'],
'li_class' => $attr['li_class']
);
$attr['type'] = array_filter( explode( ',', $attr['type'] ) );
return '<div class="cbxbmmostlisting cbxbmmostlisting-sc">' . cbxbookmarkmost_html( $attr, $style_attr ) . '</div>';
}//end most_bookmarked_posts_shortcode
/**
* Add new category
*
*/
public function add_category() {
check_ajax_referer( 'cbxbookmarknonce', 'security' );
global $wpdb;
$category_table = $wpdb->prefix . 'cbxwpbookmarkcat';
$bookmark_table = $wpdb->prefix . 'cbxwpbookmark';
$cat_name = sanitize_text_field( $_POST['cat_name'] );
$cat_privacy = intval( $_POST['privacy'] );
$object_id = intval( $_POST['object_id'] );
$object_type = isset( $_POST['object_type'] ) ? esc_attr( $_POST['object_type'] ) : 'post'; //post, page, user, product, any thing custom
$cat_id = 0;
$user_id = get_current_user_id(); //get the current logged in user id
$message = array();
$sql = $wpdb->prepare( "SELECT id FROM $category_table WHERE cat_name = %s and user_id = %d", $cat_name, $user_id );
$duplicate = $wpdb->get_var( $sql );
if ( intval( $duplicate ) > 0 ) {
$message['code'] = 0;
$message['msg'] = esc_html__( 'Category with same name already exists!', 'cbxwpbookmark' );
} else {
$return = $wpdb->query( $wpdb->prepare( "INSERT INTO $category_table ( cat_name, user_id, privacy ) VALUES ( %s, %d, %d )", array(
$cat_name,
$user_id,
$cat_privacy
) ) );
if ( $return !== false ) {
$cat_id = $wpdb->insert_id; //get the newly created category id
$cats_by_user = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $category_table WHERE user_id = %d", array( $user_id ) ), ARRAY_A );
$post_in_cats_t = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT cat_id FROM $bookmark_table WHERE object_type = %s AND user_id = %d AND object_id = %d", array(
$object_type,
$user_id,
$object_id
) ), ARRAY_A );
$post_in_cats = array();
foreach ( $post_in_cats_t as $cat ) {
$post_in_cats[] = $cat['cat_id'];
}
foreach ( $cats_by_user as &$row ) {
if ( in_array( $row['id'], $post_in_cats ) ) {
$row['incat'] = 1;
} else {
$row['incat'] = 0;
}
}
$message['code'] = 1;
$message['msg'] = esc_html__( 'Category created successfully!', 'cbxwpbookmark' );
if ( $cats_by_user !== false ) {
$message['cats'] = json_encode( $cats_by_user );
} else {
$message['cats'] = 0;
}
do_action( 'cbxbookmark_category_added', $cat_id, $user_id, $cat_name );
} else {
$message['code'] = 0;
$message['msg'] = esc_html__( 'Category creation failed or database query failed!', 'cbxwpbookmark' );
}
}
echo json_encode( $message );
wp_die();
}//end add_category
/**
* Edit a Category (From bookmark popup panel)
*
*/
public function edit_category() {
check_ajax_referer( 'cbxbookmarknonce', 'security' );
global $wpdb;
$message = array();
$category_table = $wpdb->prefix . 'cbxwpbookmarkcat';
$bookmark_table = $wpdb->prefix . 'cbxwpbookmark';
$cat_name = sanitize_text_field( $_POST['cat_name'] );
$cat_id = intval( $_POST['cat_id'] );
$cat_privacy = intval( $_POST['privacy'] );
$object_id = intval( $_POST['object_id'] );
$object_type = isset( $_POST['object_type'] ) ? esc_attr( $_POST['object_type'] ) : 'post'; //post, page, user, product, any thing custom
$user_id = get_current_user_id(); //get the current logged in user id
$sql = $wpdb->prepare( "SELECT id FROM $category_table WHERE cat_name = %s AND id != %d AND user_id = %d", $cat_name, $cat_id, $user_id );
$duplicate = $wpdb->get_var( $sql );
if ( $cat_name == '' ) {
$message['code'] = 0;
$message['msg'] = esc_html__( 'Category name can not be empty', 'cbxwpbookmark' );
} else if ( $cat_id == 0 ) {
$message['code'] = 0;
$message['msg'] = esc_html__( 'Category id missing, are you cheating?', 'cbxwpbookmark' );
} else if ( intval( $duplicate ) > 0 ) {
$message['code'] = 0;
$message['msg'] = esc_html__( 'Another Category with same name already exists!', 'cbxwpbookmark' );
} else {
// Update Query
$update = $wpdb->update(
$category_table, array(
'cat_name' => $cat_name, // string
'privacy' => $cat_privacy // integer (number)
), array(
'id' => $cat_id,
'user_id' => $user_id
), array(
'%s', // value1
'%d' // value2
), array(
'%d',
'%d'
)
);
if ( $update !== false ) {
$cats_by_user = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $category_table WHERE user_id = %d", array( $user_id ) ), ARRAY_A );
$post_in_cats_t = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT cat_id FROM $bookmark_table WHERE object_type = %s AND user_id = %d AND object_id = %d", array(
$object_type,
$user_id,
$object_id
) ), ARRAY_A );
$post_in_cats = array();
foreach ( $post_in_cats_t as $cat ) {
$post_in_cats[] = $cat['cat_id'];
}
foreach ( $cats_by_user as &$row ) {
if ( in_array( $row['id'], $post_in_cats ) ) {
$row['incat'] = 1;
} else {
$row['incat'] = 0;
}
}
$message['code'] = 1;
$message['msg'] = esc_html__( 'Category update successfully!', 'cbxwpbookmark' );
if ( $cats_by_user !== false ) {
$message['cats'] = json_encode( $cats_by_user );
} else {
$message['cats'] = 0;
}
do_action( 'cbxbookmark_category_edit', $cat_id, $user_id, $cat_name );
} else {
$message['code'] = 0;
$message['msg'] = esc_html__( 'Category edit failed or database query failed!', 'cbxwpbookmark' );
}
}
echo json_encode( $message );
wp_die();
}//end edit_category
/**
* Update Category(from user edit panel)
*
*/
public function update_bookmark_category() {
check_ajax_referer( 'cbxbookmarknonce', 'security' );
if ( isset( $_POST ) ) {
global $wpdb;
$data = array();
$cat_name = sanitize_text_field( wp_unslash($_POST['catname']) );
$cat_id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
$privacy = intval( $_POST['privacy'] );
$user_id = get_current_user_id();
// Category Table with database Prefix
$bookmarkcategory_table = $wpdb->prefix . 'cbxwpbookmarkcat';
// Update Query
$update = $wpdb->update(
$bookmarkcategory_table, array(
'cat_name' => $cat_name, // string
'privacy' => $privacy // integer (number)
), array(
'id' => $cat_id,
'user_id' => $user_id
), array(
'%s', // value1
'%d' // value2
), array(
'%d',
'%d'
)
);
if ( $update !== false ) {
do_action( 'cbxbookmark_category_edit', $cat_id, $user_id, $cat_name );
$data['msg'] = esc_html__( "Data Updated Successfully", "cbxwpbookmark" );
$data['flag'] = 1;
$data['catname'] = $cat_name;
$data['privacy'] = $privacy;
} else {
$data['msg'] = esc_html__( "Update Failed", "cbxwpbookmark" );
$data['flag'] = 0;
}
echo $data = json_encode( $data );
}
wp_die();
}//end update_bookmark_category
/**
*
* Delete Category
*/
public function delete_bookmark_category() {
check_ajax_referer( 'cbxbookmarknonce', 'security' );
$message = array();
global $wpdb;
$setting = $this->settings_api;
$bookmark_mode = $setting->get_option( 'bookmark_mode', 'cbxwpbookmark_basics', 'user_cat' );
if ( isset( $_POST ) && $bookmark_mode == 'user_cat' ) {
$cat_id = intval( $_POST['id'] );
$bookmarkcategory_table = $wpdb->prefix . 'cbxwpbookmarkcat';
$bookmark_table = $wpdb->prefix . 'cbxwpbookmark';
$user_id = get_current_user_id();
do_action( 'cbxbookmark_category_deleted_before', $cat_id, $user_id );
$delete_category = $wpdb->delete( $bookmarkcategory_table, array( 'id' => $cat_id, 'user_id' => $user_id ), array( '%d', '%d' ) );
if ( $delete_category !== false ) {
//deleted successfully
$message['msg'] = 0;
do_action( 'cbxbookmark_category_deleted', $cat_id, $user_id );
//now delete any bookmark entry for that category
//$delete_bookmark = $wpdb->delete($bookmark_table, array('cat_id' => $cat_id, 'user_id' => $user_id), array('%d', '%d'));
$bookmarks_by_category = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $bookmark_table WHERE cat_id = %d", array( $cat_id ) ), ARRAY_A );
if ( $bookmarks_by_category != null ) {
foreach ( $bookmarks_by_category as $single_bookmark ) {
do_action( 'cbxbookmark_bookmark_removed_before', $single_bookmark['id'], $single_bookmark['user_id'], $single_bookmark['object_id'], $single_bookmark['object_type'] );
$delete_status = $wpdb->query( $wpdb->prepare( "DELETE FROM $bookmark_table WHERE id=%d", intval( $single_bookmark['id'] ) ) );
if ( $delete_status !== false ) {
do_action( 'cbxbookmark_bookmark_removed', $single_bookmark['id'], $single_bookmark['user_id'], $single_bookmark['object_id'], $single_bookmark['object_type'] );
}
}
}
if ( isset( $_POST['object_id'] ) ) {
$object_id = intval( $_POST['object_id'] );
$object_type = isset( $_POST['object_type'] ) ? esc_attr( $_POST['object_type'] ) : 'post'; //post, page, user, product, any thing custom
$cats_by_user = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $bookmarkcategory_table WHERE user_id = %d", array( $user_id ) ), ARRAY_A );
$post_in_cats_t = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT cat_id FROM $bookmark_table WHERE object_type = %s AND user_id = %d AND object_id = %d", array(
$object_type,
$user_id,
$object_id
) ), ARRAY_A );
$post_in_cats = array();
foreach ( $post_in_cats_t as $cat ) {
$post_in_cats[] = $cat['cat_id'];
}
foreach ( $cats_by_user as &$row ) {
if ( in_array( $row['id'], $post_in_cats ) ) {
$row['incat'] = 1;
} else {
$row['incat'] = 0;
}
}
$message['cats'] = json_encode( $cats_by_user );
}
} else {
$message['msg'] = 1;
}
} else {
$message['msg'] = esc_html__( "No data available", "cbxwpbookmark" );
}
echo json_encode( $message );
wp_die();
}//end delete_bookmark_category
/**
* Add Bookmark ajax request and response
*
*/
public function add_bookmark() {
global $wpdb;
check_ajax_referer( 'cbxbookmarknonce', 'security' );
$setting = new CBXWPBookmark_Settings_API();
$bookmark_mode = $setting->get_option( 'bookmark_mode', 'cbxwpbookmark_basics', 'user_cat' );
$user_id = get_current_user_id();
$cat_id = intval( $_POST['cat_id'] );
$object_id = intval( $_POST['object_id'] );
$object_type = isset( $_POST['object_type'] ) ? esc_attr( $_POST['object_type'] ) : 'post'; //post, page or any custom post and later any object type
$bookmark_table = $wpdb->prefix . 'cbxwpbookmark';
if ( $bookmark_mode == 'no_cat' ) {
$duplicate = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $bookmark_table WHERE object_type = %s AND object_id = %d AND user_id = %d", array(
$object_type,
$object_id,
$user_id
) ) );
} else {
$duplicate = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $bookmark_table WHERE object_type = %s AND object_id = %d AND cat_id = %d AND user_id = %d", array(
$object_type,
$object_id,
$cat_id,
$user_id
) ) );
}
$message = array();
if ( intval( $duplicate ) > 0 ) {
if ( $bookmark_mode == 'no_cat' ) {
//already exists, so remove
$return = $wpdb->query( $wpdb->prepare( "DELETE FROM $bookmark_table WHERE object_type = %s AND object_id = %d AND user_id = %d", array(
$object_type,
$object_id,
$user_id
) ) );
} else {
//already exists, so remove
$return = $wpdb->query( $wpdb->prepare( "DELETE FROM $bookmark_table WHERE object_type = %s AND object_id = %d AND cat_id = %d AND user_id = %d", array(
$object_type,
$object_id,
$cat_id,
$user_id
) ) );
}
if ( $return !== false ) {
$message['code'] = 1; //operation success
$message['msg'] = esc_html__( 'Bookmark removed!', 'cbxwpbookmark' );
$message['operation'] = 0;
$bookmark_id = $duplicate;
do_action( 'cbxbookmark_bookmark_removed', $bookmark_id, $user_id, $object_id, $object_type );
} else {
$message['code'] = 0; //operation failed
$message['msg'] = esc_html__( 'Bookmark remove failed!', 'cbxwpbookmark' );
}
} else {
//doesn't exists, so add
$return = $wpdb->query( $wpdb->prepare( "INSERT INTO $bookmark_table ( object_id, object_type, cat_id, user_id ) VALUES ( %d,%s, %d, %d )", array(
$object_id,
$object_type,
$cat_id,
$user_id
) ) );
if ( $return !== false ) {
$message['code'] = 1; //db operation success
$message['msg'] = esc_html__( 'Bookmark added!', 'cbxwpbookmark' );
$message['operation'] = 1;
$bookmark_id = $wpdb->insert_id;
do_action( 'cbxbookmark_bookmark_added', $bookmark_id, $user_id, $object_id, $object_type );
} else {
$message['code'] = 0; //db operation failed
$message['msg'] = esc_html__( 'Bookmark add failed', 'cbxwpbookmark' );
}
}
$bookmark_total = CBXWPBookmarkHelper::getTotalBookmark( $object_id );
$bookmark_by_user = CBXWPBookmarkHelper::isBookmarkedByUser( $object_id );
$message['bookmark_count'] = $bookmark_total;
$message['bookmark_byuser'] = ( $bookmark_by_user ) ? 1 : 0;
echo json_encode( $message );
wp_die();
}//end add_bookmark
/**
* Delete bookmarked Post
*/
public function delete_bookmark_post() {
global $wpdb;
$data = array();
check_ajax_referer( 'cbxbookmarknonce', 'security' );
if ( isset( $_POST ) ) {
$bookmark_id = intval( $_POST['bookmark_id'] );
$object_id = intval( $_POST['object_id'] );
$object_type = isset( $_POST['object_type'] ) ? esc_attr( wp_unslash($_POST['object_type']) ) : 'post'; //post, page or any custom post and later any object type
$bookmark_table = $wpdb->prefix . 'cbxwpbookmark';
$user_id = get_current_user_id();
$single_bookmark = CBXWPBookmarkHelper::singleBookmarkByObjectUser( $object_id, $user_id );
do_action( 'cbxbookmark_bookmark_removed_before', $bookmark_id, $user_id, $object_id, $object_type );
$delete_bookmark = $wpdb->delete( $bookmark_table, array(
'object_id' => $object_id,
'user_id' => $user_id
), array( '%d', '%d' ) );
if ( $delete_bookmark !== false ) {
$data['msg'] = 0;
do_action( 'cbxbookmark_bookmark_removed', $bookmark_id, $user_id, $object_id, $object_type );
} else {
$data['msg'] = 1;
}
} else {
$data['msg'] = esc_html__( "No data available", "cbxwpbookmark" );
}
echo json_encode( $data );
wp_die();
}//end delete_bookmark_post
/**
* Register the stylesheets for the public-facing side of the site.
*
* @since 1.0.0
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in CBXWPBookmark_Loader as all of the hooks are defined
* in that particular class.
*
* The CBXWPBookmark_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
public function enqueue_styles() {
do_action( 'cbxwpbookmark_css_start' );
wp_register_style( 'cbxwpbookmarkpublic-css', plugin_dir_url( __FILE__ ) . '../assets/css/cbxwpbookmark-public.css', array(), '2.0', 'all' );
wp_enqueue_style( 'cbxwpbookmarkpublic-css' );
do_action( 'cbxwpbookmark_css_end' );
}//end enqueue_styles
/**
* Register the stylesheets for the public-facing side of the site.
*
* @since 1.0.0
*
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in CBXWPBookmark_Loader as all of the hooks are defined
* in that particular class.
*
* The CBXWPBookmark_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
public function enqueue_scripts() {
$setting = $this->settings_api;
$bookmark_mode = $setting->get_option( 'bookmark_mode', 'cbxwpbookmark_basics', 'user_cat' );
do_action( 'cbxwpbookmark_js_start' );
wp_enqueue_script( 'jquery' );
$category_template = '
<div class="cbxbookmark-mycat-editbox">
<input class="cbxbmedit-catname" name="catname" value="##catname##" />
<select class="cbxbmedit-privacy input-catprivacy" name="catprivacy">
<option value="1" title="' . esc_html__( 'Public Category', 'cbxwpbookmark' ) . '">' . esc_html__( 'Public', 'cbxwpbookmark' ) . '</option>
<option value="0" title="' . esc_html__( 'Private Category', 'cbxwpbookmark' ) . '">' . esc_html__( 'Private', 'cbxwpbookmark' ) . '</option>
</select>
<a href="#" class="cbxbookmark-btn cbxbookmark-cat-save">' . esc_html__( 'Update', 'cbxwpbookmark' ) . ' <span class="cbxbm_busy" style="display:none;"></span></a>
<a href="#" class="cbxbookmark-btn cbxbookmark-cat-close">' . esc_html__( 'Close', 'cbxwpbookmark' ) . '</a>
</div>';
if ( $bookmark_mode != 'user_cat' ) {
$category_template = '';
}
wp_register_script( 'cbxwpbookmarkpublicjs', plugin_dir_url( __FILE__ ) . '../assets/js/cbxwpbookmark-public.js', array( 'jquery' ), '2.0', true );
$cbxwpbookmark_translation = array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( "cbxbookmarknonce" ),
'cat_template' => json_encode( $category_template ),
'category_delete_success' => esc_html__( 'Category deleted successfully', 'cbxwpbookmark' ),
'category_delete_error' => esc_html__( 'Unable to delete the category', 'cbxwpbookmark' ),
'areyousuretodeletecat' => esc_html__( 'Are you sure you want to delete this Bookmark Category?', 'cbxwpbookmark' ),
'areyousuretodeletebookmark' => esc_html__( 'Are you sure you want to delete this Bookmark?', 'cbxwpbookmark' ),
'bookmark_failed' => esc_html__( 'Faild to Bookmark', 'cbxwpbookmark' ),
'bookmark_removed' => esc_html__( 'Bookmark Removed', 'cbxwpbookmark' ),
'bookmark_removed_empty' => esc_html__( 'All Bookmarks Removed', 'cbxwpbookmark' ),
'bookmark_removed_failed' => esc_html__( 'Bookmark Removed Failed', 'cbxwpbookmark' ),
'error_msg' => esc_html__( 'Error loading data. Response code = ', 'cbxwpbookmark' ),
'category_name_empty' => esc_html__( 'Category name can not be empty', 'cbxwpbookmark' ),
'add_to_head_defult' => esc_html__( 'Click Category to Bookmark', 'cbxwpbookmark' ),
'category_loaded_edit' => esc_html__( 'Click to Edit Category', 'cbxwpbookmark' ),
//'category_loaded_add' => esc_html__('Click Category to Bookmark', 'cbxwpbookmark'),
'max_cat_limit' => 0,
'max_cat_limit_error' => esc_html__( 'Sorry, you reached the maximum category limit and to create one one, please delete unnecessary categories first', 'cbxwpbookmark' ),
'user_current_cat_count' => 0,
'user_current_cats' => '',
'user_can_create_cat' => 1,
'bookmark_mode' => $bookmark_mode,
'bookmark_not_found' => esc_html__( 'No bookmarks found', 'cbxwpbookmark' ),
'load_more' => esc_html__( 'Load More ...', 'cbxwpbookmark' )
);
$cbxwpbookmark_translation = apply_filters( 'cbxwpbookmark_public_jsvar', $cbxwpbookmark_translation );
wp_localize_script( 'cbxwpbookmarkpublicjs', 'cbxwpbookmark', $cbxwpbookmark_translation );
wp_enqueue_script( 'cbxwpbookmarkpublicjs' );
do_action( 'cbxwpbookmark_js_end' );
}//end enqueue_scripts
/**
* Load bookmark sublist via ajax
*/
public function load_bookmarks_sublist() {
check_ajax_referer( 'cbxbookmarknonce', 'security' );
global $wpdb;
$cbxwpbookmrak_table = $wpdb->prefix . 'cbxwpbookmark';
$cbxwpbookmak_category_table = $wpdb->prefix . 'cbxwpbookmarkcat';
$setting = $this->settings_api;
$bookmark_mode = $setting->get_option( 'bookmark_mode', 'cbxwpbookmark_basics', 'user_cat' );
$category_table = $wpdb->prefix . 'cbxwpbookmarkcat';
$bookmark_table = $wpdb->prefix . 'cbxwpbookmark';
$user_id = get_current_user_id(); //get the current logged in user id
$cat_id = absint( $_POST['cat_id'] );
$cat_total = absint( $_POST['cat_total'] );
$privacy = absint( $_POST['privacy'] );
$userid = absint( $_POST['userid'] );
$totalpage = absint( $_POST['totalpage'] );
$page = absint( $_POST['page'] );
$perpage = 10;
$perpage = apply_filters( 'cbxwpbookmark_sublist_perpage', $perpage );
$total_page = ceil( $cat_total / $perpage );
if ( $userid == 0 ) {
$userid = get_current_user_id();
}
if ( $userid == 0 ) {
$privacy = 1; //only public
}
$main_sql = '';
$cat_sql = '';
$category_privacy_sql = '';
//$page = 1;
$start_point = ( $page * $perpage ) - $perpage;
$limit_sql = "LIMIT";
$limit_sql .= ' ' . $start_point . ',';
$limit_sql .= ' ' . $perpage;
$orderby = 'object_id';
$order = 'DESC';
if ( $bookmark_mode == 'user_cat' ) {
$param = array( $userid, $cat_id );
$main_sql .= "SELECT * FROM $cbxwpbookmrak_table WHERE user_id=%d AND cat_id = %d group by object_id ORDER BY $orderby $order $limit_sql";
} else {
$param = array( $cat_id );
$main_sql .= "SELECT * FROM $cbxwpbookmrak_table WHERE cat_id = %d group by object_id ORDER BY $orderby $order $limit_sql";
}
$items = $wpdb->get_results( $wpdb->prepare( $main_sql, $param ) );
$output = '';
if ( $items === null || sizeof( $items ) > 0 ) {
$object_types = CBXWPBookmarkHelper::object_types( true ); //get plain post type as array
$instance = array();
$instance['show_thumb'] = 0;
foreach ( $items as $item ) {
$action_html = '';
if ( in_array( $item->object_type, $object_types ) ) {
ob_start();
//echo '<li ><a href="' . get_permalink( $item->object_id ) . '">' . get_the_title( $item->object_id ) . '</a>' . $action_html . '</li>';
include( cbxwpbookmark_locate_template( 'bookmarkpost/single.php' ) );
$li_output = ob_get_clean();
$output .= $li_output;
} else {
ob_start();
do_action( 'cbxwpbookmark_othertype_item', $instance, $item->object_id, $item->object_type, $action_html );
$li_output = ob_get_clean();
$output .= $li_output;
}
//$output .= '<li class="cbxbookmark-category-list-item-sublist-ul-li"><a href="' . get_the_permalink( $item->object_id ) . '">'.get_the_title( $item->object_id ).'</a></li>';
}
}
$message = array();
//code 1 = bookmarks found
//code 0 = bookmarks not found
if ( $output != '' ) {
$message['page'] = $page;
$message['totalpage'] = $totalpage;
$message['show_more'] = ( $page < $totalpage ) ? 1 : 0;
$message['code'] = 1;
$message['msg'] = esc_html__( 'Bookmarks loaded', 'cbxwpbookmark' );
$message['output'] = json_encode( $output );
} else {
$message['page'] = $page;
$message['totalpage'] = $totalpage;
$message['show_more'] = ( $page < $totalpage ) ? 1 : 0;
$message['code'] = 0;
$message['msg'] = esc_html__( 'Bookmark not found', 'cbxwpbookmark' );
}
echo json_encode( $message );
wp_die();
}//end load_bookmarks_sublist
}//end class CBXWPbookmark_Public