Пример #1
0
 /**
  * Deletes custom role.
  */
 public static function wpcf_access_delete_role_ajax()
 {
     if (!current_user_can('manage_options')) {
         _e('There are security problems. You do not have permissions.', 'wpcf-access');
         die;
     }
     if (!isset($_POST['wpnonce']) || !wp_verify_nonce($_POST['wpnonce'], 'wpcf-access-error-pages')) {
         die('verification failed');
     }
     if (in_array(strtolower(trim($_POST['wpcf_access_delete_role'])), Access_Helper::wpcf_get_default_roles())) {
         $error = 'true';
         $output = '<div class="error toolset-alert toolset-alert-error js-toolset-alert">' . __('Role can not be deleted', 'wpcf-access') . '</div>';
     } else {
         $delete_role = sanitize_text_field($_POST['wpcf_access_delete_role']);
         TAccess_Loader::load('CLASS/Admin_Edit');
         $model = TAccess_Loader::get('MODEL/Access');
         $access_roles = $model->getAccessRoles();
         if ($_POST['wpcf_reassign'] != 'ignore') {
             $users = get_users('role=' . $delete_role);
             foreach ($users as $user) {
                 $user = new WP_User($user->ID);
                 $user->add_role(Access_Helper::wpcf_esc_like($_POST['wpcf_reassign']));
                 $user->remove_role($delete_role);
             }
         }
         remove_role($delete_role);
         if (isset($access_roles[$delete_role])) {
             unset($access_roles[$delete_role]);
         }
         $model->updateAccessRoles($access_roles);
         $error = 'false';
         $output = Access_Admin_Edit::wpcf_access_admin_set_custom_roles_level_form(Access_Helper::wpcf_get_editable_roles());
     }
     echo json_encode(array('error' => $error, 'output' => $output));
     die;
 }
Пример #2
0
    /**
     * HTML formatted permissions table.
     *
     * @param type $roles
     * @param type $permissions
     * @param type $name
     * @return string
     */
    public static function wpcf_access_permissions_table($roles, $permissions, $settings, $group_id, $id, $enabled = true, $managed = true, $custom_errors = array(), $type_data = array())
    {
        $ordered_roles = Access_Helper::wpcf_access_order_roles_by_level($roles);
        $default_roles = Access_Helper::wpcf_get_default_roles();
        $output = '';
        $output .= '<table class="wpcf-access-table js-access-table">';
        $output .= '<tr>';
        $output .= '<th>' . __('Action', 'wpcf-access') . '</th>';
        foreach ($ordered_roles as $levels => $roles_data) {
            if (empty($roles_data)) {
                continue;
            }
            $title = '';
            foreach ($roles_data as $role => $details) {
                if (in_array($role, $default_roles)) {
                    $title .= '<p class="access-role-name-wrap js-tooltip"><span class="access-role-name">' . translate_user_role($details['name']) . '</span></p>';
                } else {
                    $title .= '<p class="access-role-name-wrap js-tooltip"><span class="access-role-name">' . taccess_t($details['name'], $details['name']) . '</span></p>';
                }
            }
            $output .= '<th>' . $title . '</th>';
        }
        // Add Guest
        $output .= '<th>' . __('Guest', 'wpcf-access') . '</th>';
        $output .= '<th>' . __('Specific user', 'wpcf-access') . '</th>';
        if ($group_id == 'types' && $id != 'attachment') {
            $output .= '<th>' . __('When disabled', 'wpcf-access') . '</th>';
        }
        $output .= '</tr>';
        $output .= '<tbody>';
        foreach ($settings as $permission_slug => $data) {
            // Change slug for 3rd party
            if (!in_array($group_id, array('types', 'tax'))) {
                $permission_slug = $data['cap_id'];
                $managed = true;
            }
            $check = true;
            $output .= '<tr>';
            $output .= '<td class="wpcf-access-table-action-title">' . $data['title'] . '</td>';
            $name = 'types_access[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][role]';
            // If no settings saved use default setting [role]
            $role_check = !empty($permissions[$permission_slug]['role']) ? $permissions[$permission_slug]['role'] : $data['role'];
            $template_link = '';
            if (isset($custom_errors['_custom_read_errors'][$id]) && $permission_slug == 'read') {
                $current_custom_errors = $custom_errors['_custom_read_errors'][$id]['permissions']['read'];
                $current_custom_errors_value = $custom_errors['_custom_read_errors_value'][$id]['permissions']['read'];
            }
            if (isset($custom_errors['_archive_custom_read_errors'][$id]) && $permission_slug == 'read') {
                $current_archive_custom_errors = $custom_errors['_archive_custom_read_errors'][$id]['permissions']['read'];
                $current_archive_custom_errors_value = $custom_errors['_archive_custom_read_errors_value'][$id]['permissions']['read'];
            }
            foreach ($ordered_roles as $levels => $roles_data) {
                if (empty($roles_data)) {
                    continue;
                }
                $addon = '';
                // Render only first (built-in)
                $role = key($roles_data);
                $details = array_shift($roles_data);
                if ($permission_slug == 'read' && $role != 'administrator' && $id != 'attachment') {
                    $addon_id = $group_id . '_' . $id . '_error_page_' . $permission_slug . '_' . $role . '_role';
                    $error_value_value = $error_type_value = $archive_error_value_value = $archive_error_type_value = $text = $archive_text = '';
                    $link_title = '';
                    if (isset($current_custom_errors[$role]) && !empty($current_custom_errors[$role])) {
                        $error_type_value = $current_custom_errors[$role];
                        $error_value_value = $current_custom_errors_value[$role];
                        if ($error_type_value == 'error_404') {
                            $text = '404';
                            $link_title = __('Show 404 - page not found', 'wpcf-access');
                        } elseif ($error_type_value == 'error_ct') {
                            $text = __('Template', 'wpcf-access') . ': ' . self::get_content_template_name($error_value_value);
                            $link_title = __('Show Content Template', 'wpcf-access') . ' - ' . self::get_content_template_name($error_value_value);
                        } else {
                            $text = __('PHP Template', 'wpcf-access') . ': ' . $error_value_value;
                            $link_title = __('Show Page template', 'wpcf-access') . ' - ' . $error_value_value;
                        }
                    } elseif (isset($current_custom_errors['everyone']) && !empty($current_custom_errors['everyone'])) {
                        if ($error_type_value == 'error_404') {
                            $link_title = __('Show 404 - page not found', 'wpcf-access');
                        } elseif ($error_type_value == 'error_ct') {
                            $link_title = __('Show Content Template', 'wpcf-access') . ' - ' . self::get_content_template_name($error_value_value);
                        } else {
                            $link_title = __('Show Page template', 'wpcf-access') . ' - ' . $error_value_value;
                        }
                    }
                    //Set Archive Errors
                    if (isset($current_archive_custom_errors[$role]) && !empty($current_archive_custom_errors[$role]) && isset($type_data['has_archive']) && $type_data['has_archive'] == 1) {
                        $archive_error_type_value = $current_archive_custom_errors[$role];
                        $archive_error_value_value = $current_archive_custom_errors_value[$role];
                        if ($archive_error_type_value == 'default_error') {
                            $archive_text = __('Display: \'No posts found\'', 'wpcf-access');
                        } elseif ($archive_error_type_value == 'error_ct') {
                            $archive_text = __('View Archive', 'wpcf-access') . ': ' . self::get_view_name($archive_error_value_value);
                        } elseif ($archive_error_type_value == 'error_php') {
                            $archive_text = __('PHP Archive', 'wpcf-access') . ': ' . preg_replace("/.*(\\/.*\\/)/", "\$1", $archive_error_value_value);
                        } else {
                            $archive_text = '';
                        }
                    }
                    $is_archive = '';
                    $archive_vars = '';
                    if (isset($type_data['has_archive']) && $type_data['has_archive'] == 1) {
                        $is_archive = 1;
                        $link_title = ' title="' . __('Set errors', 'wpcf-access') . '" ';
                    } else {
                        if (!empty($link_title)) {
                            $link_title = ' title="' . __('Set single page error', 'wpcf-access') . '. (' . $link_title . ')" ';
                        } else {
                            $link_title = ' title="' . __('Set single page error', 'wpcf-access') . '" ';
                        }
                    }
                    $error_type = 'types_access_error_type[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][' . $role . ']';
                    $error_value = 'types_access_error_value[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][' . $role . ']';
                    $archive_error_type = 'types_access_archive_error_type[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][' . $role . ']';
                    $archive_error_value = 'types_access_archive_error_value[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][' . $role . ']';
                    //				$hide_link = $check ? ' style="display:none;" ' : '';
                    $addon = '<a ' . $link_title . 'class="wpcf-add-error-page js-wpcf-add-error-page"' . ' data-typename="' . $error_type . '" data-valuename="' . $error_value . '"  data-curtype="' . $error_type_value . '" data-curvalue="' . $error_value_value . '"' . ' data-archivetypename="' . $archive_error_type . '" data-archivevaluename="' . $archive_error_value . '"  data-archivecurtype="' . $archive_error_type_value . '" data-archivecurvalue="' . $archive_error_value_value . '"' . ' data-posttype="' . $id . '" data-archive="' . $is_archive . '" data-forall="0" href=""><i class="icon-edit"></i></a>';
                    //Labels
                    $addon .= '<p class="error-page-name-wrap js-tooltip"><span class="error-page-name js-error-page-name">' . $text . '</span></p>' . '<p class="error-page-name-wrap js-tooltip"><span class="error-page-name js-archive_error-page-name">' . $archive_text . '</span></p>' . '<input type="hidden" name="' . $error_type . '" value="' . $error_type_value . '">
						<input type="hidden" name="' . $error_value . '" value="' . $error_value_value . '">';
                    if (isset($type_data['has_archive']) && $type_data['has_archive'] == 1) {
                        $addon .= '<input type="hidden" name="' . $archive_error_type . '" value="' . $archive_error_type_value . '">
							<input type="hidden" name="' . $archive_error_value . '" value="' . $archive_error_value_value . '">';
                    }
                }
                $att_id = $group_id . '_' . $id . '_permissions_' . $permission_slug . '_' . $role . '_role';
                $attributes = $check ? ' checked="checked" ' : '';
                $attributes .= !$managed ? ' readonly="readonly" disabled="disabled" ' : '';
                $output .= '<td><div class="error-page-set-wrap"><input type="checkbox" name="';
                $output .= $role_check == $role ? $name : 'dummy';
                $output .= '" id="' . $att_id . '" value="' . $role . '"' . $attributes . ' class="wpcf-access-check-left wpcf-access-' . $permission_slug . '" data-wpcfaccesscap="' . $permission_slug . '" data-wpcfaccessname="' . $name . '" ' . 'onclick="wpcfAccess.AutoThick(jQuery(this), \'' . $permission_slug . '\', \'' . $name . '\');"';
                if (!$enabled) {
                    $output .= ' disabled="disabled" readonly="readonly"';
                }
                $output .= '/>' . $addon . '</div></td>';
                // Turn off onwards checking
                if ($role_check == $role) {
                    $check = false;
                }
            }
            // Add Guest
            $name = 'types_access[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][role]';
            $attributes = $check ? ' checked="checked"' : '';
            $attributes .= !$managed ? ' readonly="readonly" disabled="disabled"' : '';
            $addon = '';
            if ($permission_slug == 'read' && $id != 'attachment') {
                $addon_id = $group_id . '_' . $id . '_error_page_' . $permission_slug . '_' . $role . '_role';
                $error_value_value = $error_type_value = $archive_error_value_value = $archive_error_type_value = $text = $archive_text = '';
                $link_title = '';
                if (isset($current_custom_errors['guest']) && !empty($current_custom_errors['guest'])) {
                    $error_type_value = $current_custom_errors['guest'];
                    $error_value_value = $current_custom_errors_value['guest'];
                    if ($error_type_value == 'error_404') {
                        $text = '404';
                        $link_title = __('Show 404 - page not found', 'wpcf-access');
                    } elseif ($error_type_value == 'error_ct') {
                        $text = __('Template', 'wpcf-access') . ': ' . self::get_content_template_name($error_value_value);
                        $link_title = __('Show Content Template', 'wpcf-access') . ' - ' . self::get_content_template_name($error_value_value);
                    } else {
                        $text = __('PHP Template', 'wpcf-access') . ': ' . $error_value_value;
                        $link_title = __('Show Page template', 'wpcf-access') . ' - ' . $error_value_value;
                    }
                } elseif (isset($current_custom_errors['everyone']) && !empty($current_custom_errors['everyone'])) {
                    if ($error_type_value == 'error_404') {
                        $link_title = __('Show 404 - page not found', 'wpcf-access');
                    } elseif ($error_type_value == 'error_ct') {
                        $link_title = __('Show Content Template', 'wpcf-access') . ' - ' . self::get_content_template_name($error_value_value);
                    } else {
                        $link_title = __('Show Page template', 'wpcf-access') . ' - ' . $error_value_value;
                    }
                }
                //Set Archive Errors
                if (isset($current_archive_custom_errors['guest']) && !empty($current_archive_custom_errors['guest']) && isset($type_data['has_archive']) && $type_data['has_archive'] == 1) {
                    $archive_error_type_value = $current_archive_custom_errors['guest'];
                    $archive_error_value_value = $current_archive_custom_errors_value['guest'];
                    if ($archive_error_type_value == 'default_error') {
                        $archive_text = __('Display: \'No posts found\'', 'wpcf-access');
                    } elseif ($archive_error_type_value == 'error_ct') {
                        $archive_text = __('View Archive', 'wpcf-access') . ': ' . self::get_view_name($archive_error_value_value);
                    } elseif ($archive_error_type_value == 'error_php') {
                        $archive_text = __('PHP Archive', 'wpcf-access') . ': ' . preg_replace("/.*(\\/.*\\/)/", "\$1", $archive_error_value_value);
                    } else {
                        $archive_text = '';
                    }
                }
                $is_archive = '';
                $archive_vars = '';
                if (isset($type_data['has_archive']) && $type_data['has_archive'] == 1) {
                    $is_archive = 1;
                    $link_title = ' title="' . __('Set errors', 'wpcf-access') . '" ';
                } else {
                    if (!empty($link_title)) {
                        $link_title = ' title="' . __('Set single page error', 'wpcf-access') . '. (' . $link_title . ')" ';
                    } else {
                        $link_title = ' title="' . __('Set single page error', 'wpcf-access') . '" ';
                    }
                }
                $error_type = 'types_access_error_type[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][guest]';
                $error_value = 'types_access_error_value[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][guest]';
                $archive_error_type = 'types_access_archive_error_type[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][guest]';
                $archive_error_value = 'types_access_archive_error_value[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][guest]';
                $hide_link = $check ? ' style="display:none;" ' : '';
                $addon = '<a ' . $link_title . 'class="wpcf-add-error-page js-wpcf-add-error-page"' . ' data-typename="' . $error_type . '" data-valuename="' . $error_value . '"  data-curtype="' . $error_type_value . '" data-curvalue="' . $error_value_value . '"' . ' data-archivetypename="' . $archive_error_type . '" data-archivevaluename="' . $archive_error_value . '"  data-archivecurtype="' . $archive_error_type_value . '" data-archivecurvalue="' . $archive_error_value_value . '"' . ' data-posttype="' . $id . '" data-archive="' . $is_archive . '" data-forall="0" href=""><i class="icon-edit"></i></a>';
                //Labels
                $addon .= '<p class="error-page-name-wrap js-tooltip"><span class="error-page-name js-error-page-name">' . $text . '</span></p>' . '<p class="error-page-name-wrap js-tooltip"><span class="error-page-name js-archive_error-page-name">' . $archive_text . '</span></p>' . '<input type="hidden" name="' . $error_type . '" value="' . $error_type_value . '">
						<input type="hidden" name="' . $error_value . '" value="' . $error_value_value . '">';
                if (isset($type_data['has_archive']) && $type_data['has_archive'] == 1) {
                    $addon .= '<input type="hidden" name="' . $archive_error_type . '" value="' . $archive_error_type_value . '">
							<input type="hidden" name="' . $archive_error_value . '" value="' . $archive_error_value_value . '">';
                }
            }
            $output .= '<td><div class="error-page-set-wrap"><input type="checkbox" name="';
            $output .= $role_check == 'guest' ? $name : 'dummy';
            $output .= '" id="' . $group_id . '_' . $id . '_permissions_' . $permission_slug . '_guest_role" value="guest"' . $attributes . ' class="wpcf-access-check-left wpcf-access-' . $permission_slug . '" data-wpcfaccesscap="' . $permission_slug . '" data-wpcfaccessname="' . $name . '" ' . 'onclick="wpcfAccess.AutoThick(jQuery(this), \'' . $permission_slug . '\', \'' . $name . '\');"';
            if (!$enabled) {
                $output .= ' disabled="disabled" readonly="readonly"';
            }
            $output .= ' />' . $addon;
            // Add admin if all disabled
            $output .= '<input type="hidden" name="types_access[' . $group_id . '][' . $id . '][__permissions]' . '[' . $permission_slug . '][role]" value="administrator" />';
            $output .= '</div></td>';
            $data['users'] = !empty($permissions[$permission_slug]['users']) ? $permissions[$permission_slug]['users'] : array();
            $output .= '<td>' . '<input type="hidden" class="wpcf-access-name-holder" name="wpcf_access_' . $id . '_' . $permission_slug . '" data-wpcfaccesscap="' . $permission_slug . '" data-wpcfaccessname="' . 'types_access[' . $group_id . '][' . $id . ']' . '[permissions][' . $permission_slug . ']" value="types_access[' . $group_id . '][' . $id . ']' . '[permissions][' . $permission_slug . ']" />' . self::wpcf_access_admin_users_form($data, 'types_access[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . ']', $enabled, $managed) . '</td>';
            if ($permission_slug == 'read' && $id != 'attachment') {
                $addon_id = $group_id . '_' . $id . '_error_page_' . $permission_slug . '_' . $role . '_role';
                $link_title = '';
                $error_value_value = $error_type_value = $archive_error_value_value = $archive_error_type_value = $text = $archive_text = '';
                if (isset($current_custom_errors['everyone']) && !empty($current_custom_errors['everyone'])) {
                    $error_type_value = $current_custom_errors['everyone'];
                    $error_value_value = $current_custom_errors_value['everyone'];
                    if ($error_type_value == 'error_404') {
                        $text = '404';
                        $link_title = __('Show 404 - page not found', 'wpcf-access');
                    } elseif ($error_type_value == 'error_ct') {
                        $text = __('Template', 'wpcf-access') . ': ' . self::get_content_template_name($error_value_value);
                        $link_title = __('Show Content Template', 'wpcf-access') . ' - ' . self::get_content_template_name($error_value_value);
                    } else {
                        $text = __('PHP Template', 'wpcf-access') . ': ' . $error_value_value;
                        $link_title = __('Show Page template', 'wpcf-access') . ' - ' . $error_value_value;
                    }
                }
                //Set Archive Errors
                if (isset($current_archive_custom_errors['everyone']) && !empty($current_archive_custom_errors['everyone']) && isset($type_data['has_archive']) && $type_data['has_archive'] == 1) {
                    $archive_error_type_value = $current_archive_custom_errors['everyone'];
                    $archive_error_value_value = $current_archive_custom_errors_value['everyone'];
                    if ($archive_error_type_value == 'default_error') {
                        $archive_text = __('Display: \'No posts found\'', 'wpcf-access');
                    } elseif ($archive_error_type_value == 'error_ct') {
                        $archive_text = __('View Archive', 'wpcf-access') . ': ' . self::get_view_name($archive_error_value_value);
                    } elseif ($archive_error_type_value == 'error_php') {
                        $archive_text = __('PHP Archive', 'wpcf-access') . ': ' . preg_replace("/.*(\\/.*\\/)/", "\$1", $archive_error_value_value);
                    } else {
                        $archive_text = '';
                    }
                }
                $is_archive = '';
                $archive_vars = '';
                if (isset($type_data['has_archive']) && $type_data['has_archive'] == 1) {
                    $is_archive = 1;
                    $link_title = ' title="' . __('Set errors', 'wpcf-access') . '" ';
                } else {
                    if (!empty($link_title)) {
                        $link_title = ' title="' . __('Set single page error', 'wpcf-access') . '. (' . $link_title . ')" ';
                    } else {
                        $link_title = ' title="' . __('Set single page error', 'wpcf-access') . '" ';
                    }
                }
                $error_type = 'types_access_error_type[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][everyone]';
                $error_value = 'types_access_error_value[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][everyone]';
                $archive_error_type = 'types_access_archive_error_type[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][everyone]';
                $archive_error_value = 'types_access_archive_error_value[' . $group_id . '][' . $id . '][permissions]' . '[' . $permission_slug . '][everyone]';
                $addon = '<a ' . $link_title . 'class="wpcf-add-error-page js-wpcf-add-error-page"' . ' data-typename="' . $error_type . '" data-valuename="' . $error_value . '"  data-curtype="' . $error_type_value . '" data-curvalue="' . $error_value_value . '"' . ' data-archivetypename="' . $archive_error_type . '" data-archivevaluename="' . $archive_error_value . '"  data-archivecurtype="' . $archive_error_type_value . '" data-archivecurvalue="' . $archive_error_value_value . '"' . ' data-posttype="' . $id . '" data-archive="' . $is_archive . '" data-forall="1" href=""><i class="icon-edit"></i></a>';
                //Labels
                $addon .= '<p class="error-page-name-wrap js-tooltip"><span class="error-page-name js-error-page-name">' . $text . '</span></p>' . '<p class="error-page-name-wrap js-tooltip"><span class="error-page-name js-archive_error-page-name">' . $archive_text . '</span></p>' . '<input type="hidden" name="' . $error_type . '" value="' . $error_type_value . '">
						<input type="hidden" name="' . $error_value . '" value="' . $error_value_value . '">';
                if (isset($type_data['has_archive']) && $type_data['has_archive'] == 1) {
                    $addon .= '<input type="hidden" name="' . $archive_error_type . '" value="' . $archive_error_type_value . '">
							<input type="hidden" name="' . $archive_error_value . '" value="' . $archive_error_value_value . '">';
                }
                $output .= '<td>' . $addon . '</td>';
            }
            $output .= '</tr>';
        }
        $output .= '</tbody>';
        $output .= '</table>';
        return $output;
    }
Пример #3
0
 public static function wpcf_convert_user_role($role, $user_level)
 {
     if ($role == 'guest') {
         return $role;
     }
     $managed_roles = array();
     $roles = Access_Helper::wpcf_get_editable_roles();
     $default_roles = Access_Helper::wpcf_get_default_roles();
     foreach ($roles as $role => $details) {
         for ($i = 10; $i >= 0; $i--) {
             if (isset($details['capabilities']['level_' . $i])) {
                 if (!isset($managed_roles[$i])) {
                     $managed_roles[$i] = $role;
                     $i = -1;
                 }
             }
         }
     }
     if (isset($managed_roles[$user_level])) {
         return $managed_roles[$user_level];
     } else {
         return 'guest';
     }
 }