forked from sosokruashvili/wp-sticker-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
381 lines (344 loc) · 13.9 KB
/
plugin.php
File metadata and controls
381 lines (344 loc) · 13.9 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/*
Plugin Name: WP Sticky Notes
Plugin URI: http://sticker-notes.com/
Description: Add sticky note for any page to any position
Version: 2.1.5
Author: Kruashvili
Author URI: http://sticker-notes.com/
License: GPL 2
*/
/*
Copyright 2014 Kruashvili (email : soso@kruashvili.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
global $wpst_db_version;
$wpst_db_version = "1.5";
register_activation_hook( __FILE__, '__wp_sticker_plugin_install' );
add_action( 'plugins_loaded', 'wpst_update_db_check' );
add_action( 'admin_menu', '__wp_sticker_menu' );
add_action( 'wp_enqueue_scripts', 'wpst_load_front_files' );
// Plugin information vars
$WPST_PLUGIN['name'] = "WP Sticky Notes";
$WPST_PLUGIN['folder'] = basename( dirname( __FILE__ ) );
/* Set cookie variable to identify unauthorized users */
if( !$_COOKIE["wpst_id"] ) {
setcookie( "wpst_id", uniqid(), time()+60*60*24*300, "/" );
}
/* Add custom caps to administrator by default */
$role = get_role( "administrator" );
$role->add_cap( "wpst_read" );
$role->add_cap( "wpst_edit" );
$role->add_cap( "wpst_create" );
// Update wordpress capabilities
if(isset($_POST['permissions_submit']) ){
if (!isset($_POST['wpst_allowable_post_type'])){
header('Location: '.admin_url().'admin.php?page=wpst_main&message=2');
exit;
}
$role_to_change = $_POST["wpst_group"];
if( !in_array('everyone', $role_to_change) ) {
foreach( $role_to_change as $key => $caps ) {
$role = get_role( $key );
if (!empty($caps)){
foreach ($caps as $cap) {
$role->remove_cap( $cap );
$role->add_cap( $cap );
}
} else {
$role->remove_cap( "wpst_read" );
$role->remove_cap( "wpst_edit" );
$role->remove_cap( "wpst_create" );
}
}
update_option( "wpst_allowable_post_type", $_POST['wpst_allowable_post_type'] );
header('Location: '.admin_url().'admin.php?page=wpst_main&message=1');
exit;
}
else {
update_option( "wpst_allow_unauthorized", $_POST['everyone'] );
update_option( "wpst_allowable_post_type", $_POST['wpst_allowable_post_type'] );
header('Location: '.admin_url().'admin.php?page=wpst_main&message=1');
exit;
}
}
function __wp_sticker_menu() {
global $WPST_PLUGIN;
add_menu_page( 'WP Sticky Notes', 'WP Sticky Notes', 'edit_pages', 'wpst_main', '__wp_sticker_get_page', plugins_url() . "/" . $WPST_PLUGIN['folder'] . "/scripts/images/Notes-icon.png" );
}
function __wp_sticker_get_page() {
global $WPSticker;
global $WPST_PLUGIN;
wp_enqueue_style( 'wpst-main-style', plugins_url() . "/" . $WPST_PLUGIN['folder'] . "/scripts/admin-style.css", false, "1.2.9" );
require_once( __DIR__ . "/pages/admin_main.php" );
}
function wpst_is_unauth_and_can( $cap ) {
if( is_user_logged_in() ) return false;
$unauth_caps = get_option( "wpst_allow_unauthorized" );
if( @in_array( $cap, $unauth_caps ) ) return true;
}
function wpst_load_front_files() {
global $WPST_PLUGIN, $post;
$post_types = get_option('wpst_allowable_post_type');
if (in_array($post->post_type, $post_types)){
wp_enqueue_style( 'wpst-main-style', plugins_url() . "/" . $WPST_PLUGIN['folder'] . "/scripts/wpst_style.css", false, "1.6.5" );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-draggable', "", array("jquery"), "", true );
wp_enqueue_script( 'jquery-ui-resizable', "", array("jquery"), "", true );
wp_enqueue_script( 'wpst-main-script', plugins_url() . "/" . $WPST_PLUGIN['folder'] . "/scripts/wpst_script.js", array("jquery"), "1.6.5", true );
wp_enqueue_script( 'wpst-autolinker-tool', plugins_url() . "/" . $WPST_PLUGIN['folder'] . "/scripts/parsers/Autolinker.min.js", array(), "1.0", true );
// Send data to client
wpst_send_client_data();
}
}
function wpst_send_client_data() {
global $WPST_PLUGIN;
$user_data = get_userdata( get_current_user_id() );
$wpst_current_caps = array( "wpst_read" => wpst_user_can( "wpst_read" ),
"wpst_create" => wpst_user_can( "wpst_create" ),
"wpst_edit" => wpst_user_can( "wpst_edit" ) );
$wpst_current_caps = array_filter( $wpst_current_caps );
// Send CDATA for JS
wp_localize_script( 'wpst-main-script', 'wpst_data', array(
'userid' => get_current_user_id(),
'home_url' => home_url(),
'plugin_dir' => plugins_url() . "/" . $WPST_PLUGIN['folder'],
'stickers' => get_stickers_json(),
'wpst_current_caps' => $wpst_current_caps
));
}
function wpst_user_can( $subject ) {
switch( $subject ) {
case "wpst_read":
if( current_user_can( "wpst_read" ) || wpst_is_unauth_and_can( "wpst_read" ) )
return true;
break;
case "wpst_create":
if( current_user_can( "wpst_create" ) || wpst_is_unauth_and_can( "wpst_create" ) )
return true;
break;
case "wpst_edit":
if( current_user_can( "wpst_edit" ) || wpst_is_unauth_and_can( "wpst_edit" ) )
return true;
break;
default:
return false;
}
}
function get_stickers_json() {
global $wpdb;
$current_url = "http" . (( $_SERVER['SERVER_PORT'] == 443 ) ? "s://" : "://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_user_id = ( get_current_user_id() ) ? get_current_user_id() : "NAN";
if( wpst_user_can( "wpst_read" ) ) {
$results[] = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "sticker_notes WHERE url = '{$current_url}' AND target_users = '' ");
}
$results[] = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "sticker_notes WHERE url = '{$current_url}' AND author = " . $current_user_id );
$results[] = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "sticker_notes WHERE url = '{$current_url}' AND cookie_user_id = '" . $_COOKIE["wpst_id"] . "'" );
$results[] = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "sticker_notes WHERE url = '{$current_url}' AND target_users LIKE '%" . $current_user_id . "|%' ");
foreach( $results as $res ) {
if( !empty( $res ) ) {
foreach( $res as $sticker_objects ) {
$result[$sticker_objects->sticker_id] = $sticker_objects;
}
}
}
return json_encode( $result );
}
add_action( 'wp_ajax_nopriv_wpst_save_sticker', 'wpst_save_sticker' );
add_action( 'wp_ajax_wpst_save_sticker', 'wpst_save_sticker' );
function wpst_save_sticker() {
global $wpdb;
$sticker_id = $_POST["sticker_id"];
$sticker_props = stripslashes( $_POST['properties'] );
$sticker_url = $_POST['url'];
$sticker_note = stripslashes( $_POST['note'] );
$t_users = json_decode( $_POST['users'] );
foreach( $t_users as $t_user ) {
$target_users .= $t_user . "|";
}
if( $sticker_id ) {
if( !$wpdb->get_results( "SELECT sticker_id FROM " . $wpdb->prefix ."sticker_notes WHERE sticker_id = '{$sticker_id}'" ) ) {
if( !wpst_user_can( "wpst_create" ) ) {
exit( __("Yout cannot create sticky note") );
}
$res = $wpdb->insert( $wpdb->prefix ."sticker_notes", array( "sticker_id" => $sticker_id,
"url" => $sticker_url,
"properties" => $sticker_props,
"note" => $sticker_note,
"cr_date" => date("Y-m-d"),
"author" => get_current_user_id(),
"target_users" => $target_users,
"cookie_user_id" => $_COOKIE["wpst_id"]
) );
if( !$res )
exit( __("Something wrong with query") );
else
wpst_send_notification( $t_users ); // Send notification mail to target users
}
else {
$where["sticker_id"] = $sticker_id;
if( !wpst_user_can( "wpst_edit" ) ) {
$where["author"] = get_current_user_id();
if( !is_user_logged_in() ) {
$where["cookie_user_id"] = $_COOKIE["wpst_id"];
}
}
$check = $wpdb->get_results( "SELECT target_users FROM " . $wpdb->prefix ."sticker_notes WHERE sticker_id = '{$sticker_id}'", "ARRAY_A" );
$old_target_users = array_filter( explode( "|", $check[0]['target_users'] ) );
$notification_users = array_diff( $t_users, $old_target_users );
$res1 = $wpdb->update( $wpdb->prefix ."sticker_notes", array( "sticker_id" => $sticker_id,
"url" => $sticker_url,
"properties" => $sticker_props,
"target_users" => $target_users,
"note" => $sticker_note ),
$where );
if( !$res1 )
exit( __("You cannot edit this sticky note or is already deleted") );
else
wpst_send_notification( $notification_users ); // Send notification mail to only new target users
}
}
unset( $where );
exit("OK");
}
add_action( 'wp_ajax_nopriv_wpst_delete_sticker', 'wpst_delete_sticker' );
add_action( 'wp_ajax_wpst_delete_sticker', 'wpst_delete_sticker' );
function wpst_delete_sticker() {
global $wpdb;
$sticker_id = $_POST["sticker_id"];
if( $sticker_id ) {
if( !wpst_user_can( "wpst_edit" ) ) {
$where["sticker_id"] = $sticker_id;
$where["author"] = get_current_user_id();
if( !is_user_logged_in() )
$where["cookie_user_id"] = $_COOKIE['wpst_id'];
$res2 = $wpdb->delete( $wpdb->prefix ."sticker_notes", $where );
if( !$res2 )
exit( __("You cannot delete this sticky note or is already deleted") );
}
else {
$res2 = $wpdb->delete( $wpdb->prefix ."sticker_notes", array( "sticker_id" => $sticker_id ) );
if( !$res2 )
exit( __("Something wrong with query") );
}
}
unset( $where );
exit( "OK" );
}
/*
Ajax get users list by search keyword
*/
add_action( 'wp_ajax_nopriv_wpst_get_users', 'wpst_get_users' );
add_action( 'wp_ajax_wpst_get_users', 'wpst_get_users' );
function wpst_get_users() {
$search = strip_tags( $_POST['letters'] );
$args = array (
'search' => '*' . $search .'*',
'fields' => array( 'ID', 'display_name' ),
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
'user_url',
),
'number' => 10
);
$user_query = new WP_User_Query( $args );
echo json_encode( $user_query->results );
exit();
}
function wpst_send_notification( $notification_users ) {
foreach( $notification_users as $userid ) {
$tempObj = get_userdata( $userid );
$notification_emails[] = $tempObj->user_email;
}
$stc_id = $_POST["sticker_id"];
$note_url = $_POST['url'];
$website_url = get_site_url( $blog_id, $path, "http" );
$current_user_info = get_userdata( get_current_user_id() );
$current_user_display_name = $current_user_info->data->display_name;
$headers = 'From: ' . $_SERVER['HTTP_HOST'] . ' <' . get_option( "admin_email") . ">\r\n";
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
wp_mail( $notification_emails, "New sticky note ", "User: {$current_user_display_name} has tagged you on a note with text: <br><blockquote style='color:#7f7f7f'>" . strip_tags( stripslashes( $_POST['note'] ) ) . "</blockquote><a href='{$note_url}#{$stc_id}'> See the note</a><br/><br/><span style='color:#7f7f7f'>Note: You may not see the note if you wont authorize</span>", $headers );
// Reset content-type to avoid conflicts
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}
function set_html_content_type() {
return 'text/html';
}
function wpst_pr( $ar ) {
echo "<pre>";
print_r( $ar );
echo "</pre>";
}
/*
Create and display user meta field on user edit page, this field is for
storing permission value
*/
add_action( 'show_user_profile', 'wpst_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'wpst_extra_user_profile_fields' );
add_action( 'personal_options_update', 'wpst_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'wpst_save_extra_user_profile_fields' );
function wpst_save_extra_user_profile_fields( $user_id ) {
$user = new WP_User( $user_id );
$user->remove_cap( "wpst_read" );
$user->remove_cap( "wpst_edit" );
$user->remove_cap( "wpst_create" );
foreach( $_POST['wpst_caps'] as $cap ) {
$user->add_cap( $cap );
}
}
function wpst_extra_user_profile_fields( $user ) { ?>
<h3><?php echo __("Sticky Notes Permissions"); ?></h3>
<table class="form-table">
<tr>
<th><label for="wpst_caps"><?php echo __("Sticky Notes caps") ?></label></th>
<td>
<select name="wpst_caps[]" id="wpst-user-profile-caps-select" multiple style="width:200px; height:100px;">
<option <?php echo ( user_can( $user, "wpst_read" ) ) ? "selected" : ""; ?> value="wpst_read"><?php echo __("Read")?></option>
<option <?php echo ( user_can( $user, "wpst_create" ) ) ? "selected" : ""; ?> value="wpst_create"><?php echo __("Create")?></option>
<option <?php echo ( user_can( $user, "wpst_edit" ) ) ? "selected" : ""; ?> value="wpst_edit"><?php echo __("Edit")?></option>
</select>
<div style="clear:both;"></div>
<span class="description"><?php echo __("Please choose sticky notes capabilities for this user") ?></span>
</td>
</tr>
</table>
<?php
}
function wpst_update_db_check() {
global $wpst_db_version;
if ( get_site_option( 'wpst_db_version' ) != $wpst_db_version ) {
__wp_sticker_plugin_install();
}
}
function __wp_sticker_plugin_install() {
global $wpdb;
global $wpst_db_version;
$main_table = "CREATE TABLE " . $wpdb->prefix . "sticker_notes (
id INT NOT NULL AUTO_INCREMENT,
sticker_id varchar(200) NOT NULL,
url varchar(200) NOT NULL,
properties varchar(200) NOT NULL,
note text NOT NULL,
color varchar(100) NOT NULL,
cr_date varchar(200) NOT NULL,
author INT NOT NULL,
cookie_user_id varchar(100) NOT NULL,
target_users varchar(200) NOT NULL,
UNIQUE KEY id (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $main_table );
update_option( "wpst_db_version", $wpst_db_version );
}
?>