features
3 years ago
roles
3 years ago
admin-load.php
3 years ago
admin.php
3 years ago
backup-handler.php
4 years ago
backup.php
4 years ago
cap-helper.php
4 years ago
filters-admin.php
4 years ago
filters-woocommerce.php
4 years ago
filters-wp_rest_workarounds.php
4 years ago
filters.php
4 years ago
functions-admin.php
3 years ago
functions.php
4 years ago
handler.php
4 years ago
inflect-cme.php
4 years ago
manager.php
3 years ago
network.php
4 years ago
pp-handler.php
4 years ago
pp-ui.php
4 years ago
publishpress-roles.php
4 years ago
settings-handler.php
3 years ago
settings-ui.php
3 years ago
settings.php
3 years ago
functions.php
352 lines
| 1 | <?php |
| 2 | /* |
| 3 | * PublishPress Capabilities [Free] |
| 4 | * |
| 5 | * Functions available for any URL, which are not contained within a class |
| 6 | * |
| 7 | * For performance and code separation, do not include functions that are only needed for wp-admin requests |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | /** |
| 13 | * Sanitizes a string entry |
| 14 | * |
| 15 | * Keys are used as internal identifiers. Uppercase or lowercase alphanumeric characters, |
| 16 | * spaces, periods, commas, plusses, asterisks, colons, pipes, parentheses, dashes and underscores are allowed. |
| 17 | * |
| 18 | * @param string $entry String entry |
| 19 | * @return string Sanitized entry |
| 20 | */ |
| 21 | function pp_capabilities_sanitize_entry( $entry ) { |
| 22 | $entry = preg_replace( '/[^a-zA-Z0-9 \.\,\+\*\:\|\(\)_\-\=]/', '', $entry ); |
| 23 | return $entry; |
| 24 | } |
| 25 | |
| 26 | function pp_capabilities_is_editable_role($role_name, $args = []) { |
| 27 | static $editable_roles; |
| 28 | |
| 29 | if (!function_exists('wp_roles')) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | if (!isset($editable_roles) || !empty($args['force_refresh'])) { |
| 34 | $all_roles = wp_roles()->roles; |
| 35 | $editable_roles = apply_filters('editable_roles', $all_roles, $args); |
| 36 | } |
| 37 | |
| 38 | return apply_filters('pp_capabilities_editable_role', isset($editable_roles[$role_name]), $role_name); |
| 39 | } |
| 40 | |
| 41 | function _cme_act_pp_active() |
| 42 | { |
| 43 | if (defined('PRESSPERMIT_VERSION') || (defined('PPC_VERSION') && function_exists('pp_init_cap_caster'))) { |
| 44 | define('PRESSPERMIT_ACTIVE', true); |
| 45 | } else { |
| 46 | if (defined('SCOPER_VERSION') || (defined('PP_VERSION') && function_exists('pp_init_users_interceptor'))) { |
| 47 | define('OLD_PRESSPERMIT_ACTIVE', true); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | function _cme_cap_helper() |
| 53 | { |
| 54 | global $cme_cap_helper; |
| 55 | |
| 56 | require_once(dirname(__FILE__) . '/cap-helper.php'); |
| 57 | $cme_cap_helper = new CME_Cap_Helper(); |
| 58 | |
| 59 | add_action('registered_post_type', '_cme_post_type_late_reg', 5, 2); |
| 60 | add_action('registered_taxonomy', '_cme_taxonomy_late_reg', 5, 2); |
| 61 | } |
| 62 | |
| 63 | function _cme_post_type_late_reg($post_type, $type_obj) |
| 64 | { |
| 65 | global $cme_cap_helper; |
| 66 | |
| 67 | if (!empty($type_obj->public) || !empty($type_obj->show_ui)) { |
| 68 | $cme_cap_helper->refresh(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | function _cme_taxonomy_late_reg($taxonomy, $tx_obj) |
| 73 | { |
| 74 | global $cme_cap_helper; |
| 75 | |
| 76 | if (!empty($tx_obj->public)) { |
| 77 | $cme_cap_helper->refresh(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | function _cme_init() |
| 82 | { |
| 83 | require_once(dirname(__FILE__) . '/filters.php'); |
| 84 | |
| 85 | load_plugin_textdomain('capsman-enhanced', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 86 | } |
| 87 | |
| 88 | function cme_is_plugin_active($check_plugin_file) |
| 89 | { |
| 90 | if (!$check_plugin_file) |
| 91 | return false; |
| 92 | |
| 93 | $plugins = (array)get_option('active_plugins'); |
| 94 | |
| 95 | foreach ($plugins as $plugin_file) { |
| 96 | if (false !== strpos($plugin_file, $check_plugin_file)) |
| 97 | return $plugin_file; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // if a role is marked as hidden, also default it for use by Press Permit as a Pattern Role (when PP Collaborative Editing is activated and Advanced Settings enabled) |
| 102 | function _cme_pp_default_pattern_role($role) |
| 103 | { |
| 104 | if (!$pp_role_usage = get_option('pp_role_usage')) |
| 105 | $pp_role_usage = array(); |
| 106 | |
| 107 | if (empty($pp_role_usage[$role])) { |
| 108 | $pp_role_usage[$role] = 'pattern'; |
| 109 | update_option('pp_role_usage', $pp_role_usage); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // deprecated |
| 114 | function capsman_get_pp_option($option_basename) |
| 115 | { |
| 116 | return pp_capabilities_get_permissions_option($option_basename); |
| 117 | } |
| 118 | |
| 119 | function pp_capabilities_autobackup() |
| 120 | { |
| 121 | global $wpdb; |
| 122 | |
| 123 | $roles = get_option($wpdb->prefix . 'user_roles'); |
| 124 | update_option('cme_backup_auto_' . current_time('Y-m-d_g-i-s_a'), $roles, false); |
| 125 | |
| 126 | $max_auto_backups = (defined('CME_AUTOBACKUPS')) ? (int) CME_AUTOBACKUPS : 20; |
| 127 | |
| 128 | $current_options = $wpdb->get_col("SELECT option_name FROM $wpdb->options WHERE option_name LIKE 'cme_backup_auto_%' ORDER BY option_id DESC"); |
| 129 | |
| 130 | if (count($current_options) >= $max_auto_backups) { |
| 131 | $i = 0; |
| 132 | |
| 133 | foreach($current_options as $option_name) { |
| 134 | $i++; |
| 135 | |
| 136 | if ($i > $max_auto_backups) { |
| 137 | $wpdb->query( |
| 138 | $wpdb->prepare( |
| 139 | "DELETE FROM $wpdb->options WHERE option_name = %s", |
| 140 | $option_name |
| 141 | ) |
| 142 | ); |
| 143 | |
| 144 | wp_cache_delete($option_name, 'options'); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | function pp_capabilities_get_permissions_option($option_basename) |
| 151 | { |
| 152 | return (function_exists('presspermit')) ? presspermit()->getOption($option_basename) : pp_get_option($option_basename); |
| 153 | } |
| 154 | |
| 155 | function pp_capabilities_update_permissions_option($option_basename, $option_val) |
| 156 | { |
| 157 | function_exists('presspermit') ? presspermit()->updateOption($option_basename, $option_val) : pp_update_option($option_basename, $option_val); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get post type. |
| 162 | * |
| 163 | * @return null|string String of the post type. |
| 164 | */ |
| 165 | function pp_capabilities_get_post_type() |
| 166 | { |
| 167 | global $post, $typenow, $current_screen; |
| 168 | |
| 169 | // We have a post so we can just get the post type from that. |
| 170 | if ($post && $post->post_type) { |
| 171 | return $post->post_type; |
| 172 | } |
| 173 | |
| 174 | // Check the global $typenow - set in admin.php |
| 175 | if ($typenow) { |
| 176 | return $typenow; |
| 177 | } |
| 178 | |
| 179 | // Check the global $current_screen object - set in screen.php |
| 180 | if ($current_screen && $current_screen->post_type) { |
| 181 | return $current_screen->post_type; |
| 182 | } |
| 183 | |
| 184 | if (isset($_GET['post']) && !is_array($_GET['post'])) { |
| 185 | $post_id = (int) $_GET['post']; |
| 186 | |
| 187 | } elseif (isset($_POST['post_ID'])) { |
| 188 | $post_id = (int) $_POST['post_ID']; |
| 189 | } |
| 190 | |
| 191 | if (!empty($post_id)) { |
| 192 | return get_post_type($post_id); |
| 193 | } |
| 194 | |
| 195 | // lastly check the post_type querystring |
| 196 | if (isset($_REQUEST['post_type'])) { |
| 197 | return sanitize_key($_REQUEST['post_type']); |
| 198 | } |
| 199 | |
| 200 | return 'post'; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Check if Classic Editor plugin is available. |
| 205 | * |
| 206 | * @return bool |
| 207 | */ |
| 208 | function pp_capabilities_is_classic_editor_available() |
| 209 | { |
| 210 | global $wp_version; |
| 211 | |
| 212 | return class_exists('Classic_Editor') |
| 213 | || function_exists( 'the_gutenberg_project' ) |
| 214 | || class_exists('Gutenberg_Ramp') |
| 215 | || version_compare($wp_version, '5.0', '<') |
| 216 | || class_exists('WooCommerce') |
| 217 | || (defined('PP_CAPABILITIES_CONFIGURE_CLASSIC_EDITOR') && PP_CAPABILITIES_CONFIGURE_CLASSIC_EDITOR) |
| 218 | || (function_exists('et_get_option') && 'on' === et_get_option('et_enable_classic_editor', 'off')); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get admin bar node and set as global for our usage. |
| 223 | * Due to admin toolbar, this function need to run in frontend as well |
| 224 | * |
| 225 | * @return array||object $wp_admin_bar nodes. |
| 226 | */ |
| 227 | function ppc_features_get_admin_bar_nodes($wp_admin_bar){ |
| 228 | |
| 229 | $adminBarNode = is_object($wp_admin_bar) ? $wp_admin_bar->get_nodes() : ''; |
| 230 | $ppcAdminBar = []; |
| 231 | |
| 232 | if (is_array($adminBarNode) || is_object($adminBarNode)) { |
| 233 | foreach ($adminBarNode as $adminBarnode) { |
| 234 | $id = $adminBarnode->id; |
| 235 | $title = $adminBarnode->title; |
| 236 | $parent = $adminBarnode->parent; |
| 237 | $ppcAdminBar[$id] = array('id' => $id, 'title' => $title, 'parent' => $parent); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | $GLOBALS['ppcAdminBar'] = $ppcAdminBar; |
| 242 | } |
| 243 | add_action('admin_bar_menu', 'ppc_features_get_admin_bar_nodes', 999); |
| 244 | |
| 245 | /** |
| 246 | * Implement admin features restriction. |
| 247 | * Due to admin toolbar, this function need to run in frontend as well |
| 248 | * |
| 249 | */ |
| 250 | function ppc_admin_feature_restrictions() { |
| 251 | require_once ( dirname(CME_FILE) . '/includes/features/restrict-admin-features.php' ); |
| 252 | PP_Capabilities_Admin_Features::adminFeaturedRestriction(); |
| 253 | } |
| 254 | add_action('init', 'ppc_admin_feature_restrictions', 999); |
| 255 | |
| 256 | /** |
| 257 | * Redirect user to configured role login redirect |
| 258 | * |
| 259 | * @param string $redirect_to URL to redirect to. |
| 260 | * @param string $request URL the user is coming from. |
| 261 | * @param object $user Logged user's data. |
| 262 | * @return string |
| 263 | */ |
| 264 | function ppc_roles_login_redirect($redirect_to, $request, $user) { |
| 265 | |
| 266 | if (isset($user->roles) && is_array($user->roles)) { |
| 267 | foreach ($user->roles as $user_role) { |
| 268 | //get role option |
| 269 | $role_option = get_option("pp_capabilities_{$user_role}_role_option", []); |
| 270 | |
| 271 | if (is_array($role_option) && !empty($role_option) |
| 272 | && !empty($role_option['custom_redirect']) && (int)$role_option['custom_redirect'] > 0 |
| 273 | && !empty($role_option['login_redirect']) |
| 274 | ) { |
| 275 | //custom url redirect |
| 276 | $redirect_to = esc_url_raw($role_option['login_redirect']); |
| 277 | break; |
| 278 | } else if (is_array($role_option) && !empty($role_option) |
| 279 | && !empty($role_option['referer_redirect']) && (int)$role_option['referer_redirect'] > 0 |
| 280 | && wp_get_referer() |
| 281 | ) { |
| 282 | //referer url redirect |
| 283 | $redirect_to = esc_url_raw(wp_get_referer()); |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return $redirect_to; |
| 290 | } |
| 291 | add_filter('login_redirect', 'ppc_roles_login_redirect', 10, 3); |
| 292 | |
| 293 | /** |
| 294 | * Redirect user to configured role logout redirect |
| 295 | * |
| 296 | * @param string $redirect_to URL to redirect to. |
| 297 | * @param string $request URL the user is coming from. |
| 298 | * @param object $user Logged user's data. |
| 299 | * @return string |
| 300 | */ |
| 301 | function ppc_roles_logout_redirect($redirect_to, $request, $user) { |
| 302 | |
| 303 | if (isset($user->roles) && is_array($user->roles)) { |
| 304 | foreach ($user->roles as $user_role) { |
| 305 | //get role option |
| 306 | $role_option = get_option("pp_capabilities_{$user_role}_role_option", []); |
| 307 | if (is_array($role_option) && !empty($role_option) && !empty($role_option['logout_redirect'])) { |
| 308 | $redirect_to = esc_url_raw($role_option['logout_redirect']); |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return $redirect_to; |
| 315 | } |
| 316 | add_filter('logout_redirect', 'ppc_roles_logout_redirect', 10, 3); |
| 317 | |
| 318 | /** |
| 319 | * List of capabilities admin pages |
| 320 | * |
| 321 | */ |
| 322 | function pp_capabilities_admin_pages(){ |
| 323 | |
| 324 | $pp_capabilities_pages = [ |
| 325 | 'pp-capabilities', |
| 326 | 'pp-capabilities-roles', |
| 327 | 'pp-capabilities-admin-menus', |
| 328 | 'pp-capabilities-nav-menus', |
| 329 | 'pp-capabilities-editor-features', |
| 330 | 'pp-capabilities-backup', |
| 331 | 'pp-capabilities-settings', |
| 332 | 'pp-capabilities-admin-features' |
| 333 | ]; |
| 334 | |
| 335 | return apply_filters('pp_capabilities_admin_pages', $pp_capabilities_pages); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Check if user is in capabilities admin page |
| 340 | * |
| 341 | */ |
| 342 | function is_pp_capabilities_admin_page(){ |
| 343 | |
| 344 | $pp_capabilities_pages = pp_capabilities_admin_pages(); |
| 345 | |
| 346 | $is_pp_capabilities_page = false; |
| 347 | if ( isset( $_GET['page'] ) && in_array( $_GET['page'], $pp_capabilities_pages )) { |
| 348 | $is_pp_capabilities_page = true; |
| 349 | } |
| 350 | |
| 351 | return apply_filters('is_pp_capabilities_admin_page', $is_pp_capabilities_page); |
| 352 | } |