Skip to content

Instantly share code, notes, and snippets.

@foysalit
Last active February 12, 2017 05:36
Show Gist options
  • Save foysalit/8223222 to your computer and use it in GitHub Desktop.
Save foysalit/8223222 to your computer and use it in GitHub Desktop.
bdgeeks wp dashboard extension
<?php
/**
* class to customize dashboard of users
*/
class CFA_dashboard{
public $user_data;
public $username;
public function __construct()
{
$this->user_data = get_userdata(get_current_user_id());
$this->username = $this->user_data->user_login;
add_action('wp_dashboard_setup', array($this, 'my_custom_dashboard_widgets'));
add_action('wp_dashboard_setup', array($this, 'remove_useless_dashboard_widgets'));
}
// Create the function to use in the action hook
function remove_useless_dashboard_widgets() {
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
remove_meta_box( 'dashboard_secondary', 'dashboard', 'side' );
}
public function my_custom_dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget('liked_videos_widget', 'আপনার পছন্দের ভিডিওসমূহ', array($this, 'liked_videos_widget'));
wp_add_dashboard_widget('user_comments_widget', 'আপনার comments', array($this, 'user_comments_widget'));
}
public function liked_videos_widget() {
$this->get_liked_videos($this->username);
}
public function get_liked_videos($username){
global $wpdb;
$rating_table = 'wp_likes';
$liked_videos = "SELECT * FROM $rating_table WHERE username = '".$username."'";
$liked_videos = $wpdb->get_results($liked_videos);
echo '<ul class="mycarousel">';
foreach ($liked_videos as $liked_video):
$args = array(
'post_type' => 'cfa_videos',
'orderby' => 'date',
'tax_query' => array(array(
'taxonomy' => 'topic',
'field' => 'slug',
'terms' => $liked_video->vid_sec
)),
'meta_query' => array(
array(
'key' => 'video_id',
'value' => $liked_video->vid_num,
'compare' => "="
)
)
);
$vid_tuts = new WP_Query($args);
if($vid_tuts->have_posts()):
while($vid_tuts->have_posts()):
$vid_tuts->the_post();
$vid_id = get_post_meta(get_the_ID(), 'cfa_vid_id', true);
$youtube_data = get_video_data ($vid_id);
$topics = get_the_terms(get_the_ID(), 'topic');
$topic = array_pop($topics);
?>
<li>
<a href="<?php echo the_permalink(); ?>">
<img src="<?php echo $youtube_data->thumbnailURL; ?>" />
</a>
<span class="caption"><?php the_title(); ?></span>
<a class="video-topic" href="<?php bloginfo('home'); echo '/videos/topics/'.$topic->slug; ?>"><?php echo $topic->name; ?></a>
<?php $the_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID())); ?>
<a class="lightbox" href="http://youtube.com/watch?v=<?php echo get_post_meta(get_the_ID(), 'cfa_vid_id', true); ?>" title="<?php the_title(); ?>" rel="prettyPhoto">item in lightbox</a>
<a class="more" href="<?php the_permalink(); ?>">more about the item</a>
</li>
<?php
endwhile;
else:
echo "";
endif;
endforeach;
echo "</ul>";
}
/*******get comments of the user*************/
public function user_comments_widget() {
$this->show_user_comments(get_current_user_id());
}
public function show_user_comments($user_id){
$comments = get_comments(array('user_id' => $user_id));
foreach ($comments as $comment) {
echo '<div class="user-comments">';
echo '<p>'.$comment->comment_content;
echo ' On <a href="'.get_permalink($comment->comment_post_ID).'">'.get_the_title($comment->comment_post_ID).'</a></p>';
echo '</div>';
}
}
}
add_action('admin_init', 'CFA_add_dashboard');
function CFA_add_dashboard(){
new CFA_dashboard();
}
?>
<?php
require_once("../../../../wp-load.php");
if(isset($_POST['user_id']) && isset($_POST['rate'])){
global $wpdb;
extract($_POST);
$field = ($rate == 'like') ? 'likes' : 'dislikes';
$user_data = get_userdata($user_id);
$data = array(
'username' => $user_data->user_login,
'vid_sec' => $topic,
'vid_num' => $video_id
);
$rating_table = 'wp_'.$field;
$check_query = "SELECT * FROM $rating_table WHERE username = '".$data['username']."' AND vid_sec = '$topic' AND vid_num = $video_id";
$wpdb->get_results($check_query);
if($wpdb->num_rows == 0){
if($wpdb->insert($rating_table, $data)){
$cur_rate = get_field($field, $post_id);
update_field($field, ++$cur_rate, $post_id);
$return_data = array(
'status' => 'true',
'message' => 'আপনার ভালো লাগাতে খুশি হলাম! আশা করি আরো অনেক দরকারী ও ভালো মানের রিসোর্স বিডিগিকস থেকে পাবেন!'
);
}else{
$return_data = array(
'status' => 'false',
'message' => 'সাময়িক ত্রুটির কারনে আপনার রেটিং যুক্ত করা গেলো না! দু:খিত!'
);
}
}else{
$return_data = array(
'status' => 'false',
'message' => 'আপনি আগেই ভিডিওটিতে লাইক দিয়েছেন!'
);
}
echo json_encode($return_data);
}
/***************download functionality*******************/
if(isset($_POST['user_id']) && isset($_POST['download'])){
global $wpdb;
extract($_POST);
$field = 'downloads';
$user_data = get_userdata($user_id);
$data = array(
'username' => $user_data->user_login,
'vid_sec' => $topic,
'vid_num' => $video_id
);
$download_table = 'wp_'.$field;
if($wpdb->insert($download_table, $data)){
$cur_downloads = get_field($field, $post_id);
update_field($field, ++$cur_downloads, $post_id);
$return_data = array(
'status' => 'true',
'download_url' => "http://bdgeeks.com/".$topic." store/tutorial ".$video_id.".zip",
'message' => 'কিছুক্ষনের মধ্যেই ভিডিওটি ডাউনলোড শুরু হবে.......'
);
}else{
$return_data = array(
'status' => 'false',
'message' => 'ডাউনলোড ফাইলটি খুজে পাওয়া যায়নি! দু:খিত!'
);
}
echo json_encode($return_data);
}
?>
//like and dislike functionality
var rating = {
rate_func : function(e, rate, btn){
e.preventDefault();
var user_id = btn.data('user'),
topic = btn.data('topic'),
video_id= btn.data('id'),
data = {
user_id : user_id,
topic : topic,
video_id : video_id,
post_id : post_id,
rate : rate
};
if(user_id != 0){
$.post(tmp_uri+"/includes/handle_ajax.php", data, function(msg){
$('#rate_modal').reveal('show').find('h2').text(msg.message);
}, 'json');
}else{
$('#rate_modal').reveal('show').find('h2').text('ভিডিওটিতে রেটিং দেওয়ার জন্য আপনাকে লগিন করতে হবে!');
}
}
};
$('.like-btn').on('click', function(e){
rating.rate_func(e, "like", $(this));
});
$('.dislike-btn').on('click', function(e){
rating.rate_func(e, "dislike", $(this));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment