-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshareaccess.php
194 lines (163 loc) · 5.5 KB
/
shareaccess.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
185
186
187
188
189
190
191
192
193
194
<?php
/*
* Plugin Name: Share Access
* Plugin URI: https://losst.ru
* Description: Plugin helps to allow to users edit a certain posts
* Version: 1.1
* Author: Seriyyy95
* Author URI: https://losst.ru
*/
register_activation_hook( __FILE__, function(){
global $wpdb;
$table_name = $wpdb->prefix . 'share_access';
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
post_id mediumint(9) NOT NULL,
user_id mediumint(9) NOT NULL,
PRIMARY KEY (id)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
});
function shareaccess_register_options_page() {
add_options_page('Page Title', 'Share Access', 'manage_options', 'ShareAccess', 'shareaccess_options_page');
}
add_action('admin_menu', 'shareaccess_register_options_page');
function shareaccess_options_page()
{
global $wpdb;
$table_name = $wpdb->prefix . 'share_access';
$shared_posts = $wpdb->get_results("SELECT id, post_id, user_id FROM $table_name ORDER BY user_id", ARRAY_A);
require_once ('settings-page.php');
}
add_action( 'admin_post_shareaccess_unshare', 'shareaccess_unshare' );
function shareaccess_unshare() {
$share_id = intval($_REQUEST["id"]);
global $wpdb;
$table_name = $wpdb->prefix . 'share_access';
$wpdb->query("DELETE FROM $table_name WHERE id='$share_id'");
$location = $_SERVER['HTTP_REFERER'];
wp_safe_redirect($location);
}
add_action('add_meta_boxes', 'shareaccess_add_custom_box');
function shareaccess_add_custom_box(){
$screens = array( 'post', 'page' );
$params = array(
'__block_editor_compatible_meta_box' => true,
'__back_compat_meta_box' => false,
);
if(current_user_can('administrator')){
add_meta_box( 'shareaccess_select_author', 'Разрешить доступ к записи пользователю', 'shareaccess_meta_box_callback', $screens, 'normal', 'high', $params );
}
}
function shareaccess_meta_box_callback( $post, $meta ){
global $wpdb;
$table_name = $wpdb->prefix . 'share_access';
$post_id = $post->ID;
$shared_to = $wpdb->get_row( "SELECT user_id FROM $table_name WHERE post_id=$post_id" );
if(isset($shared_to->user_id)){
$selected_user = $shared_to->user_id;
}else{
$selected_user = -1;
}
wp_dropdown_users( $args = array(
"name" => "shareaccess_user",
"show_option_none" => "Не выбран",
"selected" => $selected_user,
));
wp_nonce_field( plugin_basename(__FILE__), 'shareaccess_nonce' );
}
add_action( 'save_post', 'shareaccess_save_postdata' );
function shareaccess_save_postdata( $post_id ) {
if ( ! isset( $_POST['shareaccess_user'] ) )
return;
if ( ! wp_verify_nonce( $_POST['shareaccess_nonce'], plugin_basename(__FILE__) ) )
return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if( ! current_user_can( 'edit_post', $post_id ) )
return;
if( ! current_user_can( 'administrator' ) )
return;
$user_id = sanitize_text_field( $_POST['shareaccess_user'] );
global $wpdb;
$table_name = $wpdb->prefix . 'share_access';
$wpdb->delete( $table_name, array( 'post_id' => $post_id) );
if($user_id > -1){
$wpdb->query("INSERT INTO $table_name (post_id, user_id) VALUES ('$post_id', '$user_id')");
}
}
function shareaccess_user_can_edit( $user_id, $post_id ) {
global $wpdb;
$table_name = $wpdb->prefix . 'share_access';
$shared_to = $wpdb->get_row( "SELECT user_id FROM $table_name WHERE post_id=$post_id" );
if(isset($shared_to->user_id)){
if($user_id == $shared_to->user_id){
return true;
}
}
}
function shareaccess_user_can_edit_other( $user_id ) {
global $wpdb;
$table_name = $wpdb->prefix . 'share_access';
$shared_to = $wpdb->get_row( "SELECT user_id FROM $table_name WHERE user_id=$user_id" );
if(isset($shared_to->user_id)){
return true;
}else{
return false;
}
}
add_filter( 'map_meta_cap', function ( $caps, $cap, $user_id, $args ) {
$to_filter = array( 'edit_post', 'edit_others_posts' );
// If the capability being filtered isn't of our interest, just return current value
if ( ! in_array( $cap, $to_filter, true ) ) {
return $caps;
}
if(isset($args[0])){
$bool = shareaccess_user_can_edit( $user_id, $args[0] );
}else{
$bool = shareaccess_user_can_edit_other( $user_id );
}
if ($bool){
return [ 'edit_posts' ];
}
// Otherwise just return current value
return $caps;
}, 10, 4 );
function shareaccess_only_user_posts($query) {
global $pagenow;
global $wpdb;
$table_name = $wpdb->prefix . 'share_access';
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if(!$query->is_main_query())
return $query;
if(current_user_can('administrator')){
return $query;
}
if(current_user_can('editor')){
return $query;
}
global $user_ID;
$shared_rows = $wpdb->get_results( "SELECT post_id FROM $table_name WHERE user_id=$user_ID" );
$shared_ids = array();
foreach($shared_rows as $row){
$shared_ids[] = $row->post_id;
}
if(count($shared_ids) > 0){
$args = array(
'author' => $user_ID,
'orderby' => 'post_date',
'order' => 'ASC',
'posts_per_page' => -1
);
$authors_query = new WP_Query($args);
$author_ids = wp_list_pluck($authors_query->posts, 'ID');
$shared_ids = array_merge($shared_ids, $author_ids);
$query->set('post__in', $shared_ids );
}else{
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'shareaccess_only_user_posts');