-
Notifications
You must be signed in to change notification settings - Fork 0
/
pie-custom-functions-mu.php
331 lines (260 loc) · 9.2 KB
/
pie-custom-functions-mu.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
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
<?php
/**
* Plugin Name: PIE Hosting Companion (MU)
* Description: Required Functionality for PIE Hosting
* Author: The team at PIE
* Author URI: https://pie.co.de
* Version: 1.3.3
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: pie-custom-functions
* Domain Path: /languages
*
*/
namespace Pie\CustomFunctionsMUPlugin;
//exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Add custom user role for Pie Admin
function add_pie_admin_role()
{
remove_role('pie_admin');
add_role('pie_admin', 'Pie Admin', get_role('administrator')->capabilities);
}
// Hook the function into the 'init' action with a lower priority
add_action('init', __NAMESPACE__ . '\add_pie_admin_role', 10);
/**
* Add the 'Pie Admin' role to any existing user that has an email address ending in '@pie.co.de'
* This function runs when the plugin is activated
*
* @return void
*/
function add_pie_admin_role_to_existing_users()
{
$users = get_users(array(
'search' => '*@pie.co.de',
'search_columns' => array('user_email'),
));
foreach ($users as $user) {
assign_pie_admin_to_user($user);
}
}
add_action('init', __NAMESPACE__ . '\add_pie_admin_role_to_existing_users', 10);
/**
* Add a meta box to the user profile page to allow the user to select a custom role
* This box is only to be shown on Multisite as a user must be a super admin witht the 'pie_admin' role added as an additional role
*
* @return void
*/
if (is_multisite()) {
/**
* Add a custom meta box to the user profile page
*
* @return void
*/
add_action('admin_init', __NAMESPACE__ . '\custom_user_profile_meta_box');
function custom_user_profile_meta_box()
{
add_action('show_user_profile', __NAMESPACE__ . '\custom_user_role_meta_box_callback');
add_action('edit_user_profile', __NAMESPACE__ . '\custom_user_role_meta_box_callback');
}
/**
* Display the custom meta box on the user profile page
*
* @param [type] $user
* @return void
*/
function custom_user_role_meta_box_callback($user)
{
$selected_role = get_user_meta($user->ID, 'custom_user_role', true);
echo '<h3>Custom User Role</h3>';
echo '<table class="form-table"><tr>';
echo '<th><label for="custom_user_role">Select Custom User Role:</label></th>';
echo '<td><select name="custom_user_role" id="custom_user_role">';
$selected = selected($selected_role, 'pie_admin', false);
echo "<option value='' $selected>Select Role</option>";
echo "<option value='pie_admin' $selected>Pie Admin</option>";
echo '</select></td></tr></table>';
}
// Save the selected custom role when the user profile is updated
add_action('personal_options_update', __NAMESPACE__ . '\custom_save_user_role');
add_action('edit_user_profile_update', __NAMESPACE__ . '\custom_save_user_role');
function custom_save_user_role($user_id)
{
if (current_user_can('edit_user', $user_id)) {
$selected_role = sanitize_key($_POST['custom_user_role']);
update_user_meta($user_id, 'custom_user_role', $selected_role);
}
}
// Set the custom role after the user has been saved
add_action('profile_update', __NAMESPACE__ . '\custom_set_user_role',);
function custom_set_user_role($user_id)
{
$selected_role = get_user_meta($user_id, 'custom_user_role', true);
if ($selected_role) {
$user = get_userdata($user_id);
$user->add_role($selected_role);
}
$user = get_userdata($user_id);
}
}
/**
* Add the 'Pie Admin' role to any user that registers with an email address ending in '@pie.co.de'
*
* @param int $user_id
* @return void
*/
add_action('user_register', __NAMESPACE__ . '\add_pie_admin_role_to_user_pie_email');
function add_pie_admin_role_to_user_pie_email($user_id)
{
$user = get_userdata($user_id);
assign_pie_admin_to_user($user);
}
/**
* Get the current users email address
*
* @param object $user
* @return string $email
*/
function assign_pie_admin_to_user($user)
{
$email = $user->user_email;
$email = explode('@', $email);
$email = $email[1];
if ($email == 'pie.co.de') {
$user->add_role('pie_admin');
}
}
/**
* Remove the 'Brand Pro' and 'Pie Custom Functions' plugins from the plugins page for all users except Pie Admin
*
* @param array $plugins
* @return array $plugins
*/
add_filter('all_plugins', __NAMESPACE__ . '\hide_plugins_on_plugins_page');
function hide_plugins_on_plugins_page($plugins)
{
$current_user = wp_get_current_user();
if (!in_array('pie_admin', $current_user->roles)) {
if (isset($plugins['ultimate-branding/ultimate-branding.php'])) {
unset($plugins['ultimate-branding/ultimate-branding.php']);
}
if (isset($plugins['pie-custom-functions/pie-custom-functions.php'])) {
unset($plugins['pie-custom-functions/pie-custom-functions.php']);
}
}
return $plugins;
}
/**
* Remove the 'Ultimate Branding' tips post type and 'Ultimate Branding' menu item from the admin menu for all users except Pie Admin
*
* @return void
*/
add_action('admin_menu', __NAMESPACE__ . '\hide_plugins_from_side_bar');
add_action('network_admin_menu', __NAMESPACE__ . '\hide_plugins_from_side_bar');
function hide_plugins_from_side_bar()
{
$current_user = wp_get_current_user();
if (!in_array('pie_admin', $current_user->roles)) {
add_filter('branda_permissions_allowed_roles', '__return_empty_array');
remove_menu_page('edit.php?post_type=admin_panel_tip');
remove_menu_page('wpmudev-videos');
}
}
/**
* Only allow access to the 'WPMU DEV' plugin item for Pie Admin
*
* @return void
*/
$users = get_users(array(
'role' => 'pie_admin'
));
$pie_admins = wp_list_pluck($users, 'ID');
if (!empty($pie_admins)) {
define('WPMUDEV_LIMIT_TO_USER', implode(',', $pie_admins));
}
/**
* Staging setup, check the url and if it contains the word staging then begin staging setup
* Always set to staging as WPMU DEV has staging setup for all sites
*
* @return void
*/
// Before performing checks, ensure there is a live site url, if not, assume this is the live site
set_duplicate_site_url_lock();
if (is_staging_site()) {
// Marks all WPCF7 forms as not spam if on staging sie
add_filter('wpcf7_spam', '__return_false');
// Hooks into admin_init for code specific to the area on staging sites
add_action('admin_init', __NAMESPACE__ . '\staging_setup');
}
function staging_setup(){
if( is_multisite()) {
// Temporary fix to avoid issues with staging on multisite
if( strpos($_SERVER['HTTP_HOST'], 'staging.tempurl.host') !== false){
update_domain_mapping();
}
}
}
/**
* Update the domain mapping for all sites on the multisite network
*
* For example if the main site is https://pie.staging.tempurl.host and the staging site is https://.pie.co.de then the staging site will be updated to https://pie.co.de.pie.staging.tempurl.host
*
* TO Do: Check on how get_site_url and other functions work with multisite.
*
* To Do: Ensure that links in network admin are updated to the new domain
*
* @return void
*/
function update_domain_mapping(){
$sites = get_sites();
$main_site_url = get_site_url(1);
$main_site_url = str_replace('http://', '', $main_site_url);
$main_site_url = str_replace('https://', '', $main_site_url);
foreach($sites as $site){
$site_id = $site->blog_id;
$site_url = $site->domain;
if('1' == $site_id || strpos($site_url, 'staging.tempurl.host') ){
continue;
}
$site_domain = $site_url . '.' . $main_site_url;
$site_url = 'https://' . $site_url . '.' . $main_site_url;
update_blog_details($site_id, array('domain' => $site_domain, 'path' => '/'));
update_blog_option($site_id, 'siteurl', $site_url);
update_blog_option($site_id, 'home', $site_url);
}
}
/**
*
* Compares known live site url and curent url to check if this is a staging site
*
*/
function set_duplicate_site_url_lock() {
// Add option does not overwrite options that are already set
add_option( 'pcf_siteurl', get_duplicate_site_lock_key() );
}
function get_duplicate_site_lock_key() {
// Grabs site url from current site
$site_url = get_site_url( 'current_wp_site' );
// Inserts constant into url to ensure no search and replace done to staging will affect it and returns the value
return substr_replace(
$site_url,
'_[pcf_site_url]_',
intval( strlen( $site_url ) / 2 ),
0
);
}
function is_staging_site() {
// Gets both current site url and known live site url
$pcf_current_site_url = get_option( 'siteurl' );
$pcf_live_site_url = get_option( 'pcf_siteurl' );
// Remove constant from saved live site url to produce accurate live site url
$live_site_url = str_replace('_[pcf_site_url]_', '', $pcf_live_site_url);
// Compare strings of current site and known live site, returns bool
if ( $live_site_url === $pcf_current_site_url ) {
return false;
} else {
return true;
}
}