-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmz-favorite-posts.php
184 lines (169 loc) · 5.98 KB
/
kmz-favorite-posts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Plugin Name: KMZ Favorite Posts
* Description: Display and manage favorite posts
* Version: 1.0.0
* Author: Vladimir Kamuz
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Author URI: https://wpdev.pp.ua/
* Plugin URI: https://github.com/kamuz/wp-favorite-posts
* Text Domain: kmz-favorite-posts
* Domain Path: /languages
*/
require_once __DIR__ . '/inc/KMZ_Favorite_Widget.php';
/**
* Add button before content single post for logged users
*/
function kmz_favorites_content( $content ) {
global $post;
$img_loader_src = plugins_url( '/img/ajax-loader.gif', __FILE__ );
if ( !is_single() || !is_user_logged_in() ) {
return $content;
}
elseif (kmz_is_favorites($post->ID)){
return '<p class="favorite-links remove-favorite"><a href="#" data-action="del">Remove from favorites</a> <img src="' . $img_loader_src . '" alt="loader" class="loader-gif hidden" data-action="del"> </p>' . $content;
}
else {
return '<p class="favorite-links add-to-favorite"><a href="#" data-action="add">Add to Favorite</a> <img src="' . $img_loader_src . '" alt="loader" class="hidden"> </p>' . $content;
}
}
add_filter( 'the_content', 'kmz_favorites_content' );
/**
* Add CSS and JavaScript
*/
function kmz_favorite_css_js() {
global $post;
if( is_user_logged_in() ){
wp_enqueue_style( 'dashicons' );
wp_enqueue_style( 'kmz-favorite-style', plugins_url('/css/style.css', __FILE__), null, '1.0.0', 'screen' );
wp_enqueue_script( 'kmz-favorite-script', plugins_url('/js/script.js', __FILE__), array( 'jquery' ), '1.0.0', true);
wp_localize_script( 'kmz-favorite-script', 'kmzFavorites', [ 'url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'kmz-favorites' ), 'postId' => $post->ID, 'postTitle' => $post->post_title, 'postUrl' => get_permalink($post->ID)] );
}
}
add_action( 'wp_enqueue_scripts', 'kmz_favorite_css_js' );
/**
* Function for AJAX request for add post to favorite
*/
function kmz_add_favorite(){
if(!wp_verify_nonce( $_POST['security'], 'kmz-favorites' )){
wp_die("Security error!");
}
$post_id = (int)$_POST['postId'];
$user = wp_get_current_user();
if(kmz_is_favorites($post_id)){
wp_die('Current post is already added');
}
if(add_user_meta( $user->ID, 'kmz_favorites', $post_id )){
wp_die('You post successfully added');
}
wp_die('AJAX request completed!');
}
add_action( 'wp_ajax_kmz_add_favorite', 'kmz_add_favorite' );
/**
* Function for AJAX request for delete post from favorite
*/
function kmz_del_favorite(){
if(!wp_verify_nonce( $_POST['security'], 'kmz-favorites' )){
wp_die("Security error!");
}
$post_id = (int)$_POST['postId'];
$user = wp_get_current_user();
if(!kmz_is_favorites($post_id)){
wp_die();
}
if(delete_user_meta( $user->ID, 'kmz_favorites', $post_id )){
wp_die('Removed');
}
wp_die('Error of removing post meta data from the database!');
}
add_action( 'wp_ajax_kmz_del_favorite', 'kmz_del_favorite' );
/**
* Check in current post is added
*/
function kmz_is_favorites($post_id){
$user = wp_get_current_user();
$favorites = get_user_meta( $user->ID, 'kmz_favorites' );
foreach($favorites as $favorite){
if($favorite == $post_id){
return true;
}
}
return false;
}
/**
* Add dashboard widget
*/
function kmz_favorites_dashboard_widget(){
wp_add_dashboard_widget( 'kmz_favorites_dashboard', 'Your list of favorite posts', 'kmz_show_dashboard_widget' );
}
add_action('wp_dashboard_setup', 'kmz_favorites_dashboard_widget' );
function kmz_show_dashboard_widget(){
$user = wp_get_current_user();
$favorites = get_user_meta( $user->ID, 'kmz_favorites' );
$favorites = array_reverse($favorites);
if(!$favorites){
echo "You don't have favorite posts yet!";
}
else{
$img_loader_src = plugins_url( '/img/ajax-loader.gif', __FILE__ );
echo '<ul class="kmz-widget-favorite-posts">';
foreach($favorites as $favorite){
echo '<li class="favorite-post-' . $favorite . '"><a href="' . get_the_permalink( $favorite ) . '" target="_blank">' . get_the_title($favorite) . '</a><span><a href="#" data-post="' . $favorite . '" class="dashicons dashicons-no"></a></span><img src="' . $img_loader_src . '" alt="loader" class="loader-gif hidden"> </li>';
}
echo '</ul>';
echo '<div class="kmz-favorites-del-all"><button class="button button-primary btn btn-danger mt-3">Delete All</button><img src="' . $img_loader_src . '" alt="loader" class="loader-gif hidden"></div>';
}
}
function kmz_widget_content(){
$user = wp_get_current_user();
$favorites = get_user_meta( $user->ID, 'kmz_favorites' );
$favorites = array_reverse($favorites);
if(!$favorites){
echo "You don't have favorite posts yet!";
}
else{
echo '<ul class="kmz-widget-favorite-posts">';
foreach($favorites as $favorite){
echo '<li class="favorite-post-' . $favorite . '"><a href="' . get_the_permalink( $favorite ) . '" target="_blank">' . get_the_title($favorite) . '</a></li>';
}
echo '</ul>';
}
}
/**
* Add files of JS scripts and CSS styles to admin section
*/
function kmz_favorite_admin_scripts( $hook) {
if($hook != 'index.php'){
return;
}
else{
wp_enqueue_script( 'kmz-favorite-admin-script', plugins_url('/js/admin-script.js', __FILE__), array( 'jquery' ), '1.0.0', true);
wp_enqueue_style( 'kmz-favorite-admin-style', plugins_url('/css/admin-style.css', __FILE__), null, '1.0.0', 'screen' );
wp_localize_script( 'kmz-favorite-admin-script', 'kmzFavorites', [ 'nonce' => wp_create_nonce( 'kmz-favorites' ) ] );
}
}
add_action( 'admin_enqueue_scripts', 'kmz_favorite_admin_scripts' );
/**
* Delete all favorite posts from admin widget
*/
function kmz_del_all(){
if(!wp_verify_nonce( $_POST['security'], 'kmz-favorites' )){
wp_die("Security error!");
}
$user = wp_get_current_user();
if(delete_metadata('user', $user->ID, 'kmz_favorites')){
wp_die('List empty');
}
else{
wp_die('Error of deleting');
}
}
add_action( 'wp_ajax_kmz_del_all', 'kmz_del_all' );
/**
* Create new widget for Front End
*/
function kmz_favorite_widget(){
register_widget('KMZ_Favorite_Widget');
}
add_action('widgets_init', 'kmz_favorite_widget');