Add hierarhical managment

This commit is contained in:
Almira Krdzic
2018-07-09 12:34:06 +02:00
parent 06982f22e5
commit 77cb549a3f
9320 changed files with 436076 additions and 4793 deletions

View File

@@ -0,0 +1,167 @@
<?php
/**
* class-groups-admin-notice.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author itthinx
* @package groups
* @since 2.2.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Notices
*/
class Groups_Admin_Notice {
/**
* Time mark.
*
* @var string
*/
const INIT_TIME = 'groups-init-time';
/**
* Used to store user meta and hide the notice asking to review.
*
* @var string
*/
const HIDE_REVIEW_NOTICE = 'groups-hide-review-notice';
/**
* Used to check next time.
*
* @var string
*/
const REMIND_LATER_NOTICE = 'groups-remind-later-notice';
/**
* The number of seconds in seven days, since init date to show the notice.
*
* @var int
*/
const SHOW_LAPSE = 604800;
/**
* The number of seconds in one day, used to show notice later again.
*
* @var int
*/
const REMIND_LAPSE = 86400;
/**
* Adds actions.
*/
public static function init() {
add_action( 'admin_init', array( __CLASS__,'admin_init' ) );
}
/**
* Hooked on the admin_init action.
*/
public static function admin_init() {
if ( current_user_can( 'activate_plugins' ) ) {
$user_id = get_current_user_id();
if ( !empty( $_GET[self::HIDE_REVIEW_NOTICE] ) && wp_verify_nonce( $_GET['groups_notice'], 'hide' ) ) {
add_user_meta( $user_id, self::HIDE_REVIEW_NOTICE, true );
}
if ( !empty( $_GET[self::REMIND_LATER_NOTICE] ) && wp_verify_nonce( $_GET['groups_notice'], 'later' ) ) {
update_user_meta( $user_id, self::REMIND_LATER_NOTICE, time() + self::REMIND_LAPSE );
}
$hide_review_notice = get_user_meta( $user_id, self::HIDE_REVIEW_NOTICE, true );
if ( empty( $hide_review_notice ) ) {
$d = time() - self::get_init_time();
if ( $d >= self::SHOW_LAPSE ) {
$remind_later_notice = get_user_meta( $user_id, self::REMIND_LATER_NOTICE, true );
if ( empty( $remind_later_notice ) || ( time() > $remind_later_notice ) ) {
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
}
}
}
}
}
/**
* Initializes if necessary and returns the init time.
*/
public static function get_init_time() {
$init_time = get_site_option( self::INIT_TIME, null );
if ( $init_time === null ) {
$init_time = time();
add_site_option( self::INIT_TIME, $init_time );
}
return $init_time;
}
/**
* Adds the admin notice.
*/
public static function admin_notices() {
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$hide_url = wp_nonce_url( add_query_arg( self::HIDE_REVIEW_NOTICE, true, $current_url ), 'hide', 'groups_notice' );
$remind_url = wp_nonce_url( add_query_arg( self::REMIND_LATER_NOTICE, true, $current_url ), 'later', 'groups_notice' );
$output = '';
$output .= '<style type="text/css">';
$output .= 'div.groups-rating {';
$output .= sprintf( 'background: url(%s) #fff no-repeat 8px 8px;', GROUPS_PLUGIN_URL . '/images/groups-256x256.png' );
$output .= 'padding-left: 76px ! important;';
$output .= 'background-size: 64px 64px;';
$output .= '}';
$output .= '</style>';
$output .= '<div class="updated groups-rating">';
$output .= '<p>';
$output .= __( 'Many thanks for using <strong>Groups</strong>!', 'groups' );
$output .= ' ';
$output .= __( 'Could you please spare a minute and give it a review over at WordPress.org?', 'groups' );
$output .= ' ';
$output .= sprintf(
'<a style="color:inherit;white-space:nowrap;" href="%s">%s</a>',
esc_url( $hide_url ),
esc_html( __( 'I have already done that.', 'groups' ) )
);
$output .= '</p>';
$output .= '<p>';
$output .= sprintf(
'<a class="button button-primary" href="%s" target="_blank">%s</a>',
esc_url( 'http://wordpress.org/support/view/plugin-reviews/groups?filter=5#postform' ),
__( 'Yes, here we go!', 'groups' )
);
$output .= '&emsp;';
$output .= sprintf(
'<a class="button" href="%s">%s</a>',
esc_url( $remind_url ),
esc_html( __( 'Remind me later', 'groups' ) )
);
$output .= '</p>';
$output .= '<p>';
$output .= sprintf(
__( 'You can also follow <a href="%s">@itthinx</a> on Twitter or visit <a href="%s" target="_blank">itthinx.com</a> to check out other free and premium plugins we provide.', 'groups' ),
esc_url( 'https://twitter.com/itthinx' ),
esc_url( 'https://www.itthinx.com' )
);
$output .= '</p>';
$output .= '</div>';
echo $output;
}
}
Groups_Admin_Notice::init();

View File

@@ -0,0 +1,222 @@
<?php
/**
* class-groups-admin-custom-posts.php
*
* Copyright (c) 2012 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Antonio Blanco
* @author Karim Rahimpur
* @package groups
* @since groups 1.4.2
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Post column extensions.
*/
class Groups_Admin_Post_Columns {
/**
* Groups column header id.
* @var string
*/
const GROUPS = 'groups-read';
/**
* Field name.
* @var string
*/
const GROUPS_READ = 'groups-read';
const CACHE_GROUP = 'groups';
const EDIT_TERM_LINK = 'edit-term-link';
/**
* Adds an admin_init action.
*/
public static function init() {
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
}
/**
* Adds the filters and actions only for users who have the right
* Groups permissions and for the post types that have access
* restrictions enabled.
*/
public static function admin_init() {
if ( current_user_can( GROUPS_ACCESS_GROUPS ) ) {
$post_types = get_post_types( array( 'public' => true ) );
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
foreach ( $post_types as $post_type ) {
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
if ( ( $post_type == 'attachment' ) ) {
// filters to display the media's access restriction groups
add_filter( 'manage_media_columns', array( __CLASS__, 'columns' ) );
// args: string $column_name, int $media_id
add_action( 'manage_media_custom_column', array( __CLASS__, 'custom_column' ), 10, 2 );
// make the groups column sortable
add_filter( 'manage_upload_sortable_columns', array( __CLASS__, 'manage_edit_post_sortable_columns' ) );
} else {
// filters to display the posts' access restriction groups
add_filter( 'manage_' . $post_type . '_posts_columns', array( __CLASS__, 'columns' ) );
// args: string $column_name, int $post_id
add_action( 'manage_' . $post_type . '_posts_custom_column', array( __CLASS__, 'custom_column' ), 10, 2 );
// make the groups column sortable
add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( __CLASS__, 'manage_edit_post_sortable_columns' ) );
}
}
}
}
}
/**
* Adds a new column to the post type's table showing the access
* restriction groups.
*
* @param array $column_headers
* @return array column headers
*/
public static function columns( $column_headers ) {
$column_headers[self::GROUPS] = sprintf(
'<span title="%s">%s</span>',
esc_attr( __( 'One or more groups granting access to entries.', 'groups' ) ),
esc_html( _x( 'Groups', 'Column header', 'groups' ) )
);
return $column_headers;
}
/**
* Renders custom column content.
*
* @param string $column_name
* @param int $post_id
* @return string custom column content
*/
public static function custom_column( $column_name, $post_id ) {
$output = '';
switch ( $column_name ) {
case self::GROUPS :
$entries = array();
$groups_read = get_post_meta( $post_id, Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ );
if ( count( $groups_read ) > 0 ) {
$groups = Groups_Group::get_groups( array( 'order_by' => 'name', 'order' => 'ASC', 'include' => $groups_read ) );
if ( ( count( $groups ) > 0 ) ) {
foreach( $groups as $group ) {
$entries[] = wp_strip_all_tags( $group->name );
}
}
}
if (
function_exists( 'get_term_meta' ) && // >= WordPress 4.4
class_exists( 'Groups_Restrict_Categories' ) &&
method_exists( 'Groups_Restrict_Categories', 'get_controlled_taxonomies' ) &&
method_exists( 'Groups_Restrict_Categories', 'get_term_read_groups' ) // >= Groups Restrict Categories 2.0.0
) {
$taxonomies = Groups_Restrict_Categories::get_controlled_taxonomies();
if ( count( $taxonomies ) > 0 ) {
$terms = wp_get_object_terms( $post_id, $taxonomies );
if ( !( $terms instanceof WP_Error ) ) {
foreach( $terms as $term ) {
if ( in_array( $term->taxonomy, $taxonomies ) ) {
$term_group_ids = Groups_Restrict_Categories::get_term_read_groups( $term->term_id );
if ( count( $term_group_ids ) > 0 ) {
if ( !empty( $term_group_ids ) ) {
$edit_term_link = self::get_edit_term_link( $term->term_id, $term->taxonomy );
$taxonomy_label = '';
if ( $taxonomy = get_taxonomy( $term->taxonomy ) ) {
$taxonomy_label = isset( $taxonomy->label ) ? __( $taxonomy->label ) : '';
$labels = isset( $taxonomy->labels ) ? $taxonomy->labels : null;
if ( $labels !== null ) {
if ( isset( $labels->singular_name ) ) {
$taxonomy_label = __( $labels->singular_name );
}
}
}
$term_taxonomy_title = !empty( $term->name ) ? $term->name : '';
$term_taxonomy_title.= !empty( $taxonomy_label ) ? ' ' . $taxonomy_label : '';
foreach( $term_group_ids as $group_id ) {
if ( $group = Groups_Group::read( $group_id ) ) {
$entries[] = sprintf(
'%s <a href="%s" title="%s" style="cursor: help">%s</a>',
esc_html( $group->name ),
esc_url( $edit_term_link ),
esc_attr( $term_taxonomy_title),
esc_html( $term->name )
);
}
}
}
}
}
}
}
}
}
if ( !empty( $entries ) ) {
sort( $entries );
$output .= '<ul>';
foreach( $entries as $entry ) {
$output .= '<li>';
$output .= $entry; // entries are already escaped for output
$output .= '</li>';
}
$output .= '</ul>';
}
break;
}
echo $output;
}
/**
* Helper to reduce query redundancy due to usage of current_user_can() in get_edit_term_link() etc.
*
* @param int $term_id
* @param string $taxonomy
* @return string or null if edit link could not be retrieved
*/
private static function get_edit_term_link( $term_id, $taxonomy ) {
$result = null;
$user_id = get_current_user_id();
$cached = Groups_Cache::get( self::EDIT_TERM_LINK . '_' . $term_id . '_' . $user_id, self::CACHE_GROUP );
if ( $cached !== null ) {
$result = $cached->value;
unset( $cached );
} else {
$result = get_edit_term_link( $term_id, $taxonomy );
Groups_Cache::set( self::EDIT_TERM_LINK . '_' . $term_id . '_' . $user_id, $result, self::CACHE_GROUP );
}
return $result;
}
/**
* Groups column is sortable.
*
* Sorting depends on the filters Groups_Admin_Posts::posts_join() and Groups_Admin_Posts::posts_orderby()
* which add the relevant group information and sort by group name.
*
* @see Groups_Admin_Posts::posts_join()
* @see Groups_Admin_Posts::posts_orderby()
* @param array $sortable_columns
* @return array
*/
public static function manage_edit_post_sortable_columns( $sortable_columns ) {
$sortable_columns[self::GROUPS] = self::GROUPS;
return $sortable_columns;
}
}
Groups_Admin_Post_Columns::init();

View File

@@ -0,0 +1,598 @@
<?php
/**
* class-groups-admin-posts.php
*
* Copyright (c) 2013 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.4.2
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Additions to post overview admin screens:
* - Filter posts by group.
* - Apply bulk actions to add or remove group access restrictions.
*/
class Groups_Admin_Posts {
/**
* Field name
* @var string
*/
const GROUPS_READ = 'groups-read';
const NOT_RESTRICTED = "#not-restricted#";
/**
* Sets up an admin_init hook where our actions and filters are added.
*/
public static function init() {
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
}
/**
* Adds actions and filters to handle filtering by access restriction
* capability.
*/
public static function admin_init() {
if ( current_user_can( GROUPS_ACCESS_GROUPS ) ) {
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
add_action( 'restrict_manage_posts', array( __CLASS__, 'restrict_manage_posts' ) );
// add_filter( 'parse_query', array( __CLASS__, 'parse_query' ) );
add_filter( 'posts_where', array( __CLASS__, 'posts_where' ), 10, 2 );
add_filter( 'posts_join', array( __CLASS__, 'posts_join' ), 10, 2 );
add_filter( 'posts_orderby', array( __CLASS__, 'posts_orderby' ), 10, 2 );
add_action( 'bulk_edit_custom_box', array( __CLASS__, 'bulk_edit_custom_box' ), 10, 2);
add_action( 'save_post', array( __CLASS__, 'save_post' ) );
}
}
/**
* Enqueues the select script.
*/
public static function admin_enqueue_scripts() {
global $pagenow;
if ( $pagenow == 'edit.php' ) {
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
Groups_UIE::enqueue( 'select' );
}
}
}
/**
* Adds CSS rules to display our access restriction filter coherently.
*/
public static function admin_head() {
global $pagenow;
if ( $pagenow == 'edit.php' ) {
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
echo '<style type="text/css">';
echo '.groups-groups-container { display: inline-block; line-height: 24px; padding-bottom: 1em; vertical-align: top; margin-left: 4px; margin-right: 4px; }';
echo '.groups-groups-container .groups-select-container { display: inline-block; vertical-align: top; }';
echo '.groups-groups-container .groups-select-container select, .groups-bulk-container select.groups-action { float: none; margin-right: 4px; vertical-align: top; }';
echo '.groups-groups-container .selectize-control { min-width: 128px; }';
echo '.groups-groups-container .selectize-control, .groups-bulk-container select.groups-action { margin-right: 4px; vertical-align: top; }';
echo '.groups-groups-container .selectize-input { font-size: inherit; line-height: 18px; padding: 1px 2px 2px 2px; vertical-align: middle; }';
echo '.groups-groups-container .selectize-input input[type="text"] { font-size: inherit; vertical-align: middle; }';
echo '.groups-groups-container input.button { margin-top: 1px; vertical-align: top; }';
echo '.inline-edit-row fieldset .capabilities-bulk-container label span.title { min-width: 5em; padding: 2px 1em; width: auto; }';
echo '.tablenav .actions { overflow: visible; }'; // this is important so that the selectize options aren't hidden
echo '.wp-list-table td { overflow: visible; }'; // idem for bulk actions
echo 'label.groups-read-terms {vertical-align: middle; line-height: 28px; margin-right: 4px; }'; // Terms checkbox
echo '</style>';
}
}
}
/**
* Renders the groups access restriction filter field.
*/
public static function restrict_manage_posts() {
global $pagenow, $wpdb;
if ( is_admin() ) {
if ( $pagenow == 'edit.php' ) { // check that we're on the right screen
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
$output = '';
// capabilities select
$output .= '<div class="groups-groups-container">';
$output .= sprintf(
'<select class="select group" name="%s[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
esc_attr( Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ ),
esc_attr( __( 'Groups &hellip;', 'groups' ) ) ,
esc_attr( __( 'Groups &hellip;', 'groups' ) )
);
$previous_selected = array();
if ( !empty( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ] ) ) {
$previous_selected = $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ];
if ( !is_array( $previous_selected ) ) {
$previous_selected = array();
}
}
$selected = in_array( self::NOT_RESTRICTED, $previous_selected ) ? ' selected="selected" ' : '';
$output .= sprintf( '<option value="%s" %s >%s</option>', self::NOT_RESTRICTED, esc_attr( $selected ), esc_attr( __( '(only unrestricted)', 'groups' ) ) );
$groups = Groups_Group::get_groups( array( 'order_by' => 'name', 'order' => 'ASC' ) );
foreach( $groups as $group ) {
$selected = in_array( $group->group_id, $previous_selected ) ? ' selected="selected" ' : '';
$output .= sprintf( '<option value="%s" %s >%s</option>', esc_attr( $group->group_id ), esc_attr( $selected ), wp_filter_nohtml_kses( $group->name ) );
}
$output .= '</select>';
$output .= '</div>';
$output .= Groups_UIE::render_select( '.select.group' );
if (
function_exists( 'get_term_meta' ) && // >= WordPress 4.4.0 as we query the termmeta table
class_exists( 'Groups_Restrict_Categories' ) &&
method_exists( 'Groups_Restrict_Categories', 'get_controlled_taxonomies' ) &&
method_exists( 'Groups_Restrict_Categories', 'get_term_read_groups' ) // >= Groups Restrict Categories 2.0.0, the method isn't used here but it wouldn't make any sense to query unless we're >= 2.0.0
) {
$output .= sprintf( '<label class="groups-read-terms" title="%s">', esc_attr( __( 'Also look for groups related to terms', 'groups' ) ) );
$output .= sprintf( '<input type="checkbox" name="groups-read-terms" value="1" %s />', empty( $_GET['groups-read-terms'] ) ? '' : ' checked="checked" ' );
$output .= __( 'Terms', 'groups' );
$output .= '</label>';
}
echo $output;
}
}
}
}
/**
* Bulk-edit access restriction groups.
*
* @param string $column_name
* @param string $post_type
*/
public static function bulk_edit_custom_box( $column_name, $post_type ) {
global $pagenow, $wpdb;
if ( $column_name == self::GROUPS_READ ) {
if ( $pagenow == 'edit.php' ) { // check that we're on the right screen
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
$output = '<fieldset class="inline-edit-col-right">';
$output .= '<div class="bulk-edit-groups" style="padding:0 0.5em;">';
// capability/access restriction bulk actions added through extra_tablenav()
$output .= '<div id="group-bulk-actions" class="groups-bulk-container" style="display:inline">';
$output .= '<label style="display:inline;">';
$output .= '<span class="title">';
$output .= __( 'Groups', 'groups' );
$output .= '</span>';
$output .= '<select class="groups-action" name="groups-action">';
$output .= '<option selected="selected" value="-1">' . __( '&mdash; No Change &mdash;', 'groups' ) . '</option>';
$output .= '<option value="add-group">' . __( 'Add restriction', 'groups' ) . '</option>';
$output .= '<option value="remove-group">' . __( 'Remove restriction', 'groups' ) . '</option>';
$output .= '</select>';
$output .= '</label>';
$user = new Groups_User( get_current_user_id() );
$include = Groups_Access_Meta_Boxes::get_user_can_restrict_group_ids( get_current_user_id() );
$groups = Groups_Group::get_groups( array( 'order_by' => 'name', 'order' => 'ASC', 'include' => $include ) );
$output .= '<div class="groups-groups-container">';
$output .= sprintf(
'<select class="select bulk-group" name="%s[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
esc_attr( Groups_Post_Access::POSTMETA_PREFIX . 'bulk-' . Groups_Post_Access::READ ),
esc_attr( __( 'Choose access restriction groups &hellip;', 'groups' ) ) ,
esc_attr( __( 'Choose access restriction groups &hellip;', 'groups' ) )
);
foreach( $groups as $group ) {
$output .= sprintf( '<option value="%s" >%s</option>', esc_attr( $group->group_id ), wp_filter_nohtml_kses( $group->name ) );
}
$output .= '</select>';
$output .= '</div>'; // .groups-groups-container
$output .= Groups_UIE::render_select( '.select.bulk-group' );
$output .= '</div>'; // .groups-bulk-container
$output .= '</div>'; // .bulk-edit-groups
$output .= '</fieldset>'; // .inline-edit-col-right
$output .= wp_nonce_field( 'post-group', 'bulk-post-group-nonce', true, false );
echo $output;
}
}
}
}
/**
* Handles access restriction group modifications from bulk-editing.
* This is called once for each post that is included in bulk-editing.
* The fields that are handled here are rendered through the
* bulk_edit_custom_box() method in this class.
*
* @param int $post_id
*/
public static function save_post( $post_id ) {
if ( isset( $_REQUEST['groups-action'] ) ) {
if ( wp_verify_nonce( $_REQUEST['bulk-post-group-nonce'], 'post-group' ) ) {
$field = Groups_Post_Access::POSTMETA_PREFIX . 'bulk-' . Groups_Post_Access::READ;
if ( !empty( $_REQUEST[$field] ) && is_array( $_REQUEST[$field] ) ) {
if ( Groups_Access_Meta_Boxes::user_can_restrict() ) {
$include = Groups_Access_Meta_Boxes::get_user_can_restrict_group_ids();
$groups = Groups_Group::get_groups( array( 'order_by' => 'name', 'order' => 'ASC', 'include' => $include ) );
$group_ids = array();
foreach( $groups as $group ) {
$group_ids[] = $group->group_id;
}
foreach( $_REQUEST[$field] as $group_id ) {
if ( $group = Groups_Group::read( $group_id ) ) {
if ( in_array( $group->group_id, $group_ids ) ) {
switch( $_REQUEST['groups-action'] ) {
case 'add-group' :
Groups_Post_Access::create( array(
'post_id' => $post_id,
'group_id' => $group->group_id
) );
break;
case 'remove-group' :
Groups_Post_Access::delete( $post_id, array( 'groups_read' => $group->group_id ) );
break;
}
}
}
}
}
}
}
}
}
/**
* Query modifier to take the selected access restriction groups into
* account.
*
* @param WP_Query $query query object passed by reference
*/
public static function parse_query( &$query ) {
global $pagenow;
if ( is_admin() ) {
if ( $pagenow == 'edit.php' ) { // check that we're on the right screen
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
if ( !empty( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ] ) &&
is_array( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ] )
) {
$include_unrestricted = false;
if ( in_array( self::NOT_RESTRICTED, $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ] ) ) {
$include_unrestricted = true;
}
$group_ids = array();
foreach ( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ] as $group_id ) {
if ( Groups_Group::read( $group_id ) ) {
$group_ids[] = $group_id;
}
}
if ( !empty( $group_ids ) ) {
if ( $include_unrestricted ) {
// meta_query does not handle a conjunction
// on the same meta field correctly
// (at least not up to WordPress 3.7.1)
// $query->query_vars['meta_query'] = array (
// 'relation' => 'OR',
// array (
// 'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ,
// 'value' => $group_ids,
// 'compare' => 'IN'
// ),
// array (
// 'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ,
// 'compare' => 'NOT EXISTS'
// )
// );
// we'll limit it to show just unrestricted entries
// until the above is solved
$query->query_vars['meta_query'] = array (
array (
'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ,
'compare' => 'NOT EXISTS'
)
);
} else {
$query->query_vars['meta_query'] = array (
array (
'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ,
'value' => $group_ids,
'compare' => 'IN'
)
);
}
} else if ( $include_unrestricted ) {
$query->query_vars['meta_query'] = array (
array (
'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ,
'compare' => 'NOT EXISTS'
)
);
}
}
}
}
}
}
/**
* Filters out posts by group. This is used when you choose groups on the post admin screen so that
* only those posts who are restricted by groups are shown.
*
* @param string $where
* @param WP_Query $query
* @return string
*/
public static function posts_where( $where, $query ) {
global $wpdb;
if ( self::extend_for_filter_groups_read( $query ) ) {
$group_ids = array();
foreach ( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ] as $group_id ) {
if ( $group_id = Groups_Utility::id( $group_id ) ) {
if ( Groups_Group::read( $group_id ) ) {
$group_ids[] = $group_id;
}
}
}
if ( !empty( $group_ids ) ) {
$groups = ' ( ' . implode(',', $group_ids ) . ' ) ';
$group_table = _groups_get_tablename( 'group' );
if (
!empty( $_GET['groups-read-terms'] ) &&
function_exists( 'get_term_meta' ) && // >= WordPress 4.4.0 as we query the termmeta table
class_exists( 'Groups_Restrict_Categories' ) &&
method_exists( 'Groups_Restrict_Categories', 'get_controlled_taxonomies' ) &&
method_exists( 'Groups_Restrict_Categories', 'get_term_read_groups' ) // >= Groups Restrict Categories 2.0.0, the method isn't used here but it wouldn't make any sense to query unless we're >= 2.0.0
) {
$where .= "
AND $wpdb->posts.ID IN (
SELECT post_id
FROM $wpdb->postmeta pm
WHERE
pm.meta_key = 'groups-read' AND
pm.meta_value IN $groups
UNION ALL
SELECT p.ID post_id
FROM $wpdb->posts p
LEFT JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id
LEFT JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN $wpdb->termmeta tm ON tt.term_id = tm.term_id
WHERE
tm.meta_key = 'groups-read' AND
tm.meta_value IN $groups
)
";
} else {
$where .= "
AND $wpdb->posts.ID IN (
SELECT post_id
FROM $wpdb->postmeta pm
WHERE
pm.meta_key = 'groups-read' AND
pm.meta_value IN $groups
)
";
}
} // !empty( $group_ids )
}
return $where;
}
/**
* Adds to the join to allow advanced sorting by group on the admin back end for post tables.
*
* @param string $join
* @param WP_Query $query
*/
public static function posts_join( $join, $query ) {
global $wpdb;
if ( self::extend_for_orderby_groups_read( $query ) ) {
$group_table = _groups_get_tablename( 'group' );
if ( function_exists( 'get_term_meta' ) ) { // >= WordPress 4.4.0 as we query the termmeta table
$join .= "
LEFT JOIN (
SELECT p.ID post_id, GROUP_CONCAT(DISTINCT groups_read.group_name ORDER BY groups_read.group_name) groups
FROM $wpdb->posts p
LEFT JOIN (
SELECT post_id, g.name group_name
FROM $wpdb->postmeta pm
LEFT JOIN $group_table g ON pm.meta_value = g.group_id
WHERE pm.meta_key = 'groups-read'
UNION ALL
SELECT p.ID post_id, g.name group_name
FROM $wpdb->posts p
LEFT JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id
LEFT JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN $wpdb->termmeta tm ON tt.term_id = tm.term_id
LEFT JOIN $group_table g ON tm.meta_value = g.group_id
WHERE tm.meta_key = 'groups-read'
) as groups_read ON p.ID = groups_read.post_id
GROUP BY p.ID
) groups_tmp ON $wpdb->posts.ID = groups_tmp.post_id
";
} else {
$join .= "
LEFT JOIN (
SELECT p.ID post_id, GROUP_CONCAT(DISTINCT groups_read.group_name ORDER BY groups_read.group_name) groups
FROM $wpdb->posts p
LEFT JOIN (
SELECT post_id, g.name group_name
FROM $wpdb->postmeta pm
LEFT JOIN $group_table g ON pm.meta_value = g.group_id
WHERE pm.meta_key = 'groups-read'
) as groups_read ON p.ID = groups_read.post_id
GROUP BY p.ID
) groups_tmp ON $wpdb->posts.ID = groups_tmp.post_id
";
}
}
return $join;
}
/**
* Extend the orderby clause to sort by groups related to the post and its terms.
*
* @param $string $orderby
* @param WP_Query $query
* @return string
*/
public static function posts_orderby( $orderby, $query ) {
if ( self::extend_for_orderby_groups_read( $query ) ) {
switch( $query->get( 'order' ) ) {
case 'desc' :
case 'DESC' :
$order = 'DESC';
break;
default :
$order = 'ASC';
}
$prefix = ' groups_tmp.groups ' . $order;
if ( !empty( $orderby ) ) {
$prefix .= ' , ';
}
$orderby = $prefix . $orderby;
}
return $orderby;
}
/**
* Check if we should apply our posts_join and posts_orderby filters. Used in those.
*
* @param WP_Query $query
* @return boolean
*/
private static function extend_for_orderby_groups_read( &$query ) {
$result = false;
if ( is_admin() ) {
// check if query is for a post type we handle
$post_types = $query->get( 'post_type' );
if ( !is_array( $post_types ) ) {
$post_types = array( $post_types );
}
foreach( $post_types as $post_type ) {
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
if (
!isset( $post_types_option[$post_type]['add_meta_box'] ) ||
$post_types_option[$post_type]['add_meta_box']
) {
// only act on post etc. screens
$screen = get_current_screen();
if (
!empty( $screen ) &&
!empty( $screen->id ) &&
( $screen->id == 'edit-' . $post_type )
) {
if ( $query->get( 'orderby' ) == self::GROUPS_READ ) {
$result = true;
break;
}
}
}
}
}
return $result;
}
/**
* Check if we should apply our posts_where filter. Used in it.
*
* @param WP_Query $query
* @return boolean
*/
private static function extend_for_filter_groups_read( &$query ) {
$result = false;
if ( is_admin() ) {
// check if query is for a post type we handle
$post_types = $query->get( 'post_type' );
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
if ( !is_array( $post_types ) ) {
$post_types = array( $post_types );
}
foreach( $post_types as $post_type ) {
if (
!isset( $post_types_option[$post_type]['add_meta_box'] ) ||
$post_types_option[$post_type]['add_meta_box']
) {
// only act on post etc. screens
$screen = get_current_screen();
if (
!empty( $screen ) &&
!empty( $screen->id ) &&
( $screen->id == 'edit-' . $post_type )
) {
if (
!empty( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ] ) &&
is_array( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ] )
) {
$result = true;
break;
}
}
}
}
}
return $result;
}
}
Groups_Admin_Posts::init();

View File

@@ -0,0 +1,235 @@
<?php
/**
* class-groups-admin-user-profile.php
*
* Copyright (c) 2013 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.3.11
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Show group info on user profile pages and let admins edit group membership.
*/
class Groups_Admin_User_Profile {
/**
* Adds user profile actions.
*/
public static function init() {
add_action( 'user_new_form', array( __CLASS__, 'user_new_form' ) );
add_action( 'user_register', array( __CLASS__, 'user_register' ) );
add_action( 'show_user_profile', array( __CLASS__, 'show_user_profile' ) );
add_action( 'edit_user_profile', array( __CLASS__, 'edit_user_profile' ) );
add_action( 'personal_options_update', array( __CLASS__, 'personal_options_update' ) );
add_action( 'edit_user_profile_update', array( __CLASS__, 'edit_user_profile_update' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
}
/**
* Enqueues the select script on the user-edit and profile screens.
*/
public static function admin_enqueue_scripts() {
$screen = get_current_screen();
if ( isset( $screen->id ) ) {
switch( $screen->id ) {
case 'user' : // creating a new user
case 'user-edit' :
case 'profile' :
require_once GROUPS_VIEWS_LIB . '/class-groups-uie.php';
Groups_UIE::enqueue( 'select' );
break;
}
}
}
/**
* Hook for the form to create a new user.
*
* See wp-admin/user-new.php
*
* @param string $type form context, expecting 'add-existing-user' (Multisite), or 'add-new-user' (single site and network admin)
*/
public static function user_new_form( $type = null ) {
global $wpdb;
if ( $type == 'add-new-user' ) {
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
$output = '<h3>' . __( 'Groups', 'groups' ) . '</h3>';
$groups_table = _groups_get_tablename( 'group' );
if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ) ) {
$output .= '<style type="text/css">';
$output .= '.groups .selectize-input { font-size: inherit; }';
$output .= '</style>';
$output .= sprintf(
'<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
esc_attr( __( 'Choose groups &hellip;', 'groups' ) ) ,
esc_attr( __( 'Choose groups &hellip;', 'groups' ) )
);
foreach( $groups as $group ) {
$output .= sprintf( '<option value="%d">%s</option>', Groups_Utility::id( $group->group_id ), wp_filter_nohtml_kses( $group->name ) );
}
$output .= '</select>';
$output .= Groups_UIE::render_select( '#user-groups' );
$output .= '<p class="description">' . __( 'The user is a member of the chosen groups.', 'groups' ) . '</p>';
}
echo $output;
}
}
}
/**
* Adds the new user to chosen groups when creating a new user account
* from the admin side.
*
* @param int $user_id
*/
public static function user_register( $user_id ) {
global $wpdb;
if ( is_admin() ) {
if ( function_exists( 'get_current_screen' ) ) {
$screen = get_current_screen();
if ( isset( $screen->id ) && $screen->id === 'user' ) {
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
$groups_table = _groups_get_tablename( 'group' );
if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table" ) ) {
$user_group_ids = isset( $_POST['group_ids'] ) && is_array( $_POST['group_ids'] ) ? $_POST['group_ids'] : array();
foreach( $groups as $group ) {
if ( in_array( $group->group_id, $user_group_ids ) ) {
if ( !Groups_User_Group::read( $user_id, $group->group_id ) ) {
Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) );
}
}
}
}
}
}
}
}
}
/**
* Own profile.
* @param WP_User $user
*/
public static function show_user_profile( $user ) {
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
self::edit_user_profile( $user );
} else {
$output = '<h3>' . __( 'Groups', 'groups' ) . '</h3>';
$user = new Groups_User( $user->ID );
$groups = $user->groups;
if ( is_array( $groups ) ) {
if ( count( $groups ) > 0 ) {
usort( $groups, array( __CLASS__, 'by_group_name' ) );
$output .= '<ul>';
foreach( $groups as $group ) {
$output .= '<li>' . wp_filter_nohtml_kses( $group->name ) . '</li>';
}
$output .= '</ul>';
}
}
echo $output;
}
}
/**
* Editing a user profile.
* @param WP_User $user
*/
public static function edit_user_profile( $user ) {
global $wpdb;
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
$output = '<h3>' . __( 'Groups', 'groups' ) . '</h3>';
$user = new Groups_User( $user->ID );
$user_groups = $user->groups;
$groups_table = _groups_get_tablename( 'group' );
if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ) ) {
$output .= '<style type="text/css">';
$output .= '.groups .selectize-input { font-size: inherit; }';
$output .= '</style>';
$output .= sprintf(
'<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
esc_attr( __( 'Choose groups &hellip;', 'groups' ) ) ,
esc_attr( __( 'Choose groups &hellip;', 'groups' ) )
);
foreach( $groups as $group ) {
$is_member = Groups_User_Group::read( $user->ID, $group->group_id ) ? true : false;
$output .= sprintf( '<option value="%d" %s>%s</option>', Groups_Utility::id( $group->group_id ), $is_member ? ' selected="selected" ' : '', wp_filter_nohtml_kses( $group->name ) );
}
$output .= '</select>';
$output .= Groups_UIE::render_select( '#user-groups' );
$output .= '<p class="description">' . __( 'The user is a member of the chosen groups.', 'groups' ) . '</p>';
}
echo $output;
}
}
/**
* Updates the group membership when a user's own profile is saved - but
* for group admins on their own profile page only.
*
* @param int $user_id
* @see Groups_Admin_User_Profile::edit_user_profile_update()
*/
public static function personal_options_update( $user_id ) {
// We're using the same method as for editing another user's profile,
// but let's check for group admin here as well.
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
self::edit_user_profile_update( $user_id );
}
}
/**
* Updates the group membership.
* @param int $user_id
*/
public static function edit_user_profile_update( $user_id ) {
global $wpdb;
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
$groups_table = _groups_get_tablename( 'group' );
if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table" ) ) {
$user_group_ids = isset( $_POST['group_ids'] ) && is_array( $_POST['group_ids'] ) ? $_POST['group_ids'] : array();
foreach( $groups as $group ) {
if ( in_array( $group->group_id, $user_group_ids ) ) {
if ( !Groups_User_Group::read( $user_id, $group->group_id ) ) {
Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) );
}
} else {
if ( Groups_User_Group::read( $user_id, $group->group_id ) ) {
Groups_User_Group::delete( $user_id, $group->group_id );
}
}
}
}
}
}
/**
* usort helper
* @param Groups_Group $o1
* @param Groups_Group $o2
* @return int strcmp result for group names
*/
public static function by_group_name( $o1, $o2 ) {
return strcmp( $o1->name, $o2->name );
}
}
Groups_Admin_User_Profile::init();

View File

@@ -0,0 +1,395 @@
<?php
/**
* class-groups-admin-users.php
*
* Copyright (c) 2012 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Users admin integration with Groups.
*/
class Groups_Admin_Users {
const GROUPS = 'groups_user_groups';
/**
* Hooks into filters to add the Groups column to the users table.
*/
public static function init() {
// we hook this on admin_init so that current_user_can() is available
add_action( 'admin_init', array( __CLASS__, 'setup' ) );
}
/**
* Adds the filters and actions only for users who have the right
* Groups permissions.
*/
public static function setup() {
if ( current_user_can( GROUPS_ACCESS_GROUPS ) ) {
// filters to display the user's groups
add_filter( 'manage_users_columns', array( __CLASS__, 'manage_users_columns' ) );
// args: unknown, string $column_name, int $user_id
add_filter( 'manage_users_custom_column', array( __CLASS__, 'manage_users_custom_column' ), 10, 3 );
}
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
if ( !is_network_admin() ) {
// scripts
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
// styles
add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
// allow to add or remove selected users to groups
add_action( 'load-users.php', array( __CLASS__, 'load_users' ) );
// add links to filter users by group
add_filter( 'views_users', array( __CLASS__, 'views_users' ) );
// modify query to filter users by group
add_filter( 'pre_user_query', array( __CLASS__, 'pre_user_query' ) );
// WP_Users_List_Table implements extra_tablenav() where the restrict_manage_users action is invoked.
// As the extra_tablenav() method does not define a generic extension point, this is
// the best shot we get at inserting our group actions block (currently we're at WordPress 3.6.1).
// We choose to use our own group-actions block instead of re-using the existing bulk-actions,
// to have a more explicit user interface which makes it clear that these actions
// are directed at relating users and groups.
add_action( 'restrict_manage_users', array( __CLASS__, 'restrict_manage_users' ), 0 );
}
}
}
/**
* Modify query to filter users by group.
*
* @param WP_User_Query $user_query
* @return WP_User_Query
*/
public static function pre_user_query( $user_query ) {
global $pagenow, $wpdb;
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) {
if ( isset( $_REQUEST['group_ids'] ) && is_array( $_REQUEST['group_ids'] ) ) {
$group_ids = array_map( array( 'Groups_Utility', 'id' ), array_map( 'trim', $_REQUEST['group_ids'] ) );
$include = array();
foreach ( $group_ids as $group_id ) {
if ( Groups_Group::read( $group_id ) ) {
$group = new Groups_Group( $group_id );
$users = $group->users;
if ( count( $users ) > 0 ) {
foreach( $users as $user ) {
$include[] = $user->user->ID;
}
} else { // no results
$include[] = 0;
}
unset( $group );
unset( $users );
}
}
if ( count( $include ) > 0 ) {
$include = array_unique( $include );
$ids = implode( ',', wp_parse_id_list( $include ) );
$user_query->query_where .= " AND $wpdb->users.ID IN ($ids)";
}
}
}
return $user_query;
}
/**
* Enqueue scripts the group-actions.
*/
public static function admin_enqueue_scripts() {
global $pagenow;
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) {
Groups_UIE::enqueue( 'select' );
}
}
/**
* Adds the group add/remove buttons after the last action box.
*/
public static function admin_head() {
global $pagenow;
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) {
// .subsubsub rule added because with views_users() the list can get long
// icon distinguishes from role links
echo '<style type="text/css">';
echo '.subsubsub { white-space: normal; }';
echo 'a.group { background: url(' . GROUPS_PLUGIN_URL . '/images/groups-grey-8x8.png) transparent no-repeat left center; padding-left: 10px;}';
echo '</style>';
// group-actions
echo '<style type="text/css">';
echo '.groups-bulk-container { display: inline-block; line-height: 24px; padding-bottom: 2px; vertical-align: top; margin-left: 0.31em; margin-right: 0.31em; }';
echo '.groups-bulk-container .groups-select-container { display: inline-block; vertical-align: top; }';
echo '.groups-bulk-container .groups-select-container select, .groups-bulk-container select.groups-action { float: none; margin-right: 4px; vertical-align: top; }';
echo '.groups-bulk-container .selectize-control { min-width: 128px; }';
echo '.groups-bulk-container .selectize-control, .groups-bulk-container select.groups-action { margin-right: 4px; vertical-align: top; }';
echo '.groups-bulk-container .selectize-input { font-size: inherit; line-height: 18px; padding: 1px 2px 2px 2px; vertical-align: middle; }';
echo '.groups-bulk-container .selectize-input input[type="text"] { font-size: inherit; vertical-align: middle; height: 24px; }';
echo '.groups-bulk-container input.button { margin-top: 1px; vertical-align: top; }';
echo '.tablenav .actions { overflow: visible; }'; // this is important so that the selectize options aren't hidden
echo '</style>';
// groups filter
echo '<style type="text/css">';
echo '.groups-filter-container { display: inline-block; line-height: 24px; vertical-align: middle; }';
echo '.groups-filter-container .groups-select-container { display: inline-block; vertical-align: top; }';
echo '.groups-filter-container .groups-select-container select, .groups-bulk-container select.groups-action { float: none; margin-right: 4px; vertical-align: top; }';
echo '.groups-filter-container .selectize-control { min-width: 128px; }';
echo '.groups-filter-container .selectize-control, .groups-bulk-container select.groups-action { margin-right: 4px; vertical-align: top; }';
echo '.groups-filter-container .selectize-input { font-size: inherit; line-height: 18px; padding: 1px 2px 2px 2px; vertical-align: middle; }';
echo '.groups-filter-container .selectize-input input[type="text"] { font-size: inherit; vertical-align: middle; height: 24px; }';
echo '.groups-filter-container .selectize-input .item a { line-height: inherit; }'; // neutralize .subsubsub a rule
echo '.groups-filter-container input.button { margin-top: 1px; vertical-align: top; }';
echo '</style>';
}
}
/**
* Renders group actions in the users table's extra_tablenav().
*/
public static function restrict_manage_users() {
global $pagenow, $wpdb, $groups_select_user_groups_index;
// We don't handle multiple instances so don't render another.
if ( !isset( $groups_select_user_groups_index ) ) {
$groups_select_user_groups_index = 0;
} else {
return '';
}
$output = '';
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) {
// groups select
$groups_table = _groups_get_tablename( 'group' );
if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ) ) {
$groups_select = sprintf(
'<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
esc_attr( __( 'Choose groups &hellip;', 'groups' ) ) ,
esc_attr( __( 'Choose groups &hellip;', 'groups' ) )
);
foreach( $groups as $group ) {
$is_member = false;
$groups_select .= sprintf(
'<option value="%d" %s>%s</option>',
Groups_Utility::id( $group->group_id ),
$is_member ? ' selected="selected" ' : '',
wp_filter_nohtml_kses( $group->name )
);
}
$groups_select .= '</select>';
}
// group bulk actions added through extra_tablenav()
$box = '<div id="group-bulk-actions" class="groups-bulk-container">';
$box .= '<div class="groups-select-container">';
$box .= $groups_select;
$box .= '</div>';
$box .= '<select class="groups-action" name="groups-action">';
$box .= '<option selected="selected" value="-1">' . __( 'Group Actions', 'groups' ) . '</option>';
$box .= '<option value="add-group">' . __( 'Add to group', 'groups' ) . '</option>';
$box .= '<option value="remove-group">' . __( 'Remove from group', 'groups' ) . '</option>';
$box .= '</select>';
$box .= sprintf( '<input class="button" type="submit" name="groups" value="%s" />', __( 'Apply', 'groups' ) );
$box .= '</div>';
$box = str_replace( '"', "'", $box );
$nonce = wp_nonce_field( 'user-group', 'bulk-user-group-nonce', true, false );
$nonce = str_replace( '"', "'", $nonce );
$box .= $nonce;
$box .= '<script type="text/javascript">';
$box .= 'if ( typeof jQuery !== "undefined" ) {';
$box .= 'jQuery("document").ready(function(){';
$box .= 'jQuery(".tablenav.top .alignleft.actions:last").after("<div id=\"groups-bulk-actions-block\" class=\"alignleft actions\"></div>");';
$box .= 'jQuery("#group-bulk-actions").appendTo(jQuery("#groups-bulk-actions-block"));';
$box .= '});';
$box .= '}';
$box .= '</script>';
$output .= $box;
$output .= Groups_UIE::render_select( '#user-groups' );
}
echo $output;
}
/**
* Hooked on filter in class-wp-list-table.php to
* filter by group.
* @param array $views
*/
public static function views_users( $views ) {
global $pagenow, $wpdb;
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) {
$output = '<form id="filter-groups-form" action="" method="get">';
$output .= '<div class="groups-filter-container">';
$output .= '<div class="groups-select-container">';
$output .= sprintf(
'<select id="filter-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
esc_attr( __( 'Choose groups &hellip;', 'groups' ) ) ,
esc_attr( __( 'Choose groups &hellip;', 'groups' ) )
);
$user_group_table = _groups_get_tablename( 'user_group' );
$groups = Groups_Group::get_groups( array( 'order_by' => 'name', 'order' => 'ASC' ) );
$user_counts = array();
$counts = $wpdb->get_results( "SELECT COUNT(user_id) AS count, group_id FROM $user_group_table GROUP BY group_id" );
if ( !empty( $counts ) && is_array( $counts ) ) {
foreach( $counts as $count ) {
$user_counts[$count->group_id] = $count->count;
}
}
foreach( $groups as $group ) {
// Do not use $user_count = count( $group->users ); here,
// as it creates a lot of unneccessary objects and can lead
// to out of memory issues on large user bases.
$user_count = isset( $user_counts[$group->group_id] ) ? $user_counts[$group->group_id] : 0;
$selected = isset( $_REQUEST['group_ids'] ) && is_array( $_REQUEST['group_ids'] ) && in_array( $group->group_id, $_REQUEST['group_ids'] );
$output .= sprintf(
'<option value="%d" %s>%s</option>',
Groups_Utility::id( $group->group_id ),
$selected ? ' selected="selected" ' : '',
sprintf( '%s <span class="count">(%s)</span>', wp_filter_nohtml_kses( $group->name ), esc_html( $user_count ) )
);
}
$output .= '</select>';
$output .= '</div>'; // .groups-select-container
$output .= '</div>'; // .groups-filter-container
$output .= '<input class="button" type="submit" value="' . esc_attr( __( 'Filter', 'groups' ) ) . '"/>';
$output .= '</form>';
$output .= Groups_UIE::render_select( '#filter-groups' );
$views['groups'] = $output;
}
return $views;
}
/**
* Adds or removes users to/from groups.
*/
public static function load_users() {
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
$users = isset( $_REQUEST['users'] ) ? $_REQUEST['users'] : null;
$action = null;
if ( !empty( $_REQUEST['groups'] ) ) {
if ( $_GET['groups-action'] == "add-group" ) {
$action = 'add';
} else if ( $_GET['groups-action'] == "remove-group" ) {
$action = 'remove';
}
}
if ( $users !== null && $action !== null ) {
if ( wp_verify_nonce( $_REQUEST['bulk-user-group-nonce'], 'user-group' ) ) {
foreach( $users as $user_id ) {
switch ( $action ) {
case 'add':
$group_ids = isset( $_GET['group_ids'] ) ? $_GET['group_ids'] : null;
if ( $group_ids !== null ) {
foreach ( $group_ids as $group_id ) {
if ( !Groups_User_Group::read( $user_id, $group_id ) ) {
Groups_User_Group::create(
array(
'user_id' => $user_id,
'group_id' => $group_id
)
);
}
}
}
break;
case 'remove':
$group_ids = isset( $_GET['group_ids'] ) ? $_GET['group_ids'] : null;
if ( $group_ids !== null ) {
foreach ( $group_ids as $group_id ) {
if ( Groups_User_Group::read( $user_id, $group_id ) ) {
Groups_User_Group::delete( $user_id, $group_id );
}
}
}
break;
}
}
$referer = wp_get_referer();
if ( $referer ) {
$redirect_to = remove_query_arg( array( 'action', 'action2', 'add-to-group', 'bulk-user-group-nonce', 'group_id', 'new_role', 'remove-from-group', 'users' ), $referer );
wp_redirect( $redirect_to );
exit;
}
}
}
}
}
/**
* Adds a new column to the users table to show the groups that users
* belong to.
*
* @param array $column_headers
* @return array column headers
*/
public static function manage_users_columns( $column_headers ) {
$column_headers[self::GROUPS] = __( 'Groups', 'groups' );
return $column_headers;
}
/**
* Renders custom column content.
*
* @param string $output
* @param string $column_name
* @param int $user_id
* @return string custom column content
*/
public static function manage_users_custom_column( $output, $column_name, $user_id ) {
switch ( $column_name ) {
case self::GROUPS :
$groups_user = new Groups_User( $user_id );
$groups = $groups_user->groups;
if ( count( $groups ) > 0 ) {
usort( $groups, array( __CLASS__, 'by_group_name' ) );
$output = '<ul>';
foreach( $groups as $group ) {
$output .= '<li>';
$output .= wp_filter_nohtml_kses( $group->name );
$output .= '</li>';
}
$output .= '</ul>';
} else {
$output .= __( '--', 'groups' );
}
break;
}
return $output;
}
/**
* usort helper
* @param Groups_Group $o1
* @param Groups_Group $o2
* @return int strcmp result for group names
*/
public static function by_group_name( $o1, $o2 ) {
return strcmp( $o1->name, $o2->name );
}
}
Groups_Admin_Users::init();

View File

@@ -0,0 +1,247 @@
<?php
/**
* class-groups-admin-welcome.php
*
* Copyright (c) 2017 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 2.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Groups admin welcome and update screen.
*/
class Groups_Admin_Welcome {
/**
* Adds actions to admin_menu, admin_head and admin_init.
*/
public static function init() {
add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 );
}
/**
* Adds the welcome screen to the dashboard menu.
*/
public static function admin_menu() {
add_dashboard_page(
__( 'Welcome to Groups', 'groups' ),
__( 'Welcome to Groups', 'groups' ),
'manage_options',
'groups-welcome',
array( __CLASS__, 'groups_welcome' )
);
}
/**
* Removes the welcome screen from the dashboard menu.
*/
public static function admin_head() {
remove_submenu_page( 'index.php', 'groups-welcome' );
}
/**
* Checks if the welcome screen should be shown and redirected to.
*/
public static function admin_init() {
global $groups_version;
if (
current_user_can( GROUPS_ACCESS_GROUPS ) &&
isset( $_GET['groups-welcome-dismiss'] ) &&
isset( $_GET['_groups_welcome_nonce'] )
) {
if ( wp_verify_nonce( $_GET['_groups_welcome_nonce'], 'groups_welcome_dismiss' ) ) {
Groups_Options::update_user_option( 'groups-welcome-dismiss', $groups_version );
}
}
$groups_welcome_dismiss = Groups_Options::get_user_option( 'groups-welcome-dismiss', null );
if ( version_compare( $groups_version, $groups_welcome_dismiss ) > 0 ) {
if ( get_transient( 'groups_plugin_activated' ) || get_transient( 'groups_plugin_updated_legacy' ) ) {
$doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
$doing_cron = defined( 'DOING_CRON' ) && DOING_CRON;
// we'll delete the transients in the welcome screen handler
if (
!$doing_ajax &&
!$doing_cron &&
( empty( $_GET['page'] ) || $_GET['page'] !== 'groups-welcome' ) &&
!is_network_admin() &&
!isset( $_GET['activate-multi'] ) &&
current_user_can( GROUPS_ACCESS_GROUPS ) &&
apply_filters( 'groups_welcome_show', true )
) {
wp_safe_redirect( admin_url( 'index.php?page=groups-welcome' ) );
exit;
}
}
}
}
/**
* Adds an entry leading to the welcome screen.
*
* @param array $links
* @param string $file plugin file basename
* @return array
*/
public static function plugin_row_meta( $links, $file ) {
if ( $file == plugin_basename( GROUPS_FILE ) ) {
$row_meta = array(
'welcome' => sprintf(
'<a href="%s" title="%s">%s</a>',
esc_url( admin_url( 'index.php?page=groups-welcome' ) ),
esc_attr( __( 'View the Welcome screen for this version of Groups', 'groups' ) ),
esc_html( __( 'Welcome', 'groups' ) )
)
);
return array_merge( $links, $row_meta );
}
return (array) $links;
}
/**
* Renders the welcome screen.
*/
public static function groups_welcome() {
global $groups_version;
wp_enqueue_style( 'groups_admin' );
delete_transient( 'groups_plugin_activated' );
$legacy_update = get_transient( 'groups_plugin_updated_legacy' );
delete_transient( 'groups_plugin_updated_legacy' );
echo '<div class="groups-welcome-panel">';
echo '<div class="groups-welcome-panel-content">';
printf( '<img class="groups-welcome-icon" width="64" height="64" src="%s"/>', esc_attr( GROUPS_PLUGIN_URL . 'images/groups-256x256.png' ) );
echo '<h1>';
printf( __( 'Welcome to Groups %s', 'groups' ), esc_html( $groups_version ) );
echo '</h1>';
printf(
'<a class="notice-dismiss" href="%s" title="%s" aria-label="%s"></a>',
esc_url( wp_nonce_url( add_query_arg( 'groups-welcome-dismiss', '1', admin_url() ), 'groups_welcome_dismiss', '_groups_welcome_nonce' ) ),
esc_attr( __( 'Dismiss', 'groups' ) ),
esc_html( __( 'Dismiss', 'groups' ) )
);
echo '<p class="headline">';
_e( 'Thanks for using Groups! We have made it even easier to protect your content and hope you like it :)', 'groups' );
echo '</p>';
if ( $legacy_update ) {
echo '<p class="important">';
echo '<strong>';
_e( 'Important', 'groups' );
echo '</strong>';
echo '<br/><br/>';
_e( 'It seems that you have updated from Groups 1.x where access restrictions were based on capabilities.', 'groups' );
echo '<br/>';
printf( __( 'Please make sure to read the notes on <strong>Switching to Groups %s</strong> below.', 'groups' ), esc_html( $groups_version ) );
echo '</p>';
}
echo '<h2>';
_e( "What's New?", 'groups' );
echo '</h2>';
echo '<h3>';
_e( 'Protect Content Easily', 'groups' );
echo '</h3>';
echo '<p>';
_e( 'We have made it even easier to protect your content!', 'groups' );
echo ' ';
_e( 'Now you can protect your posts, pages and any other custom post type like products or events by simply assigning them to one or more groups.', 'groups' );
echo ' ';
_e( 'Previously we used capabilities to do that, but changing to this new model makes things even easier.', 'groups' );
echo '</p>';
echo '<h3>';
_e( 'Improved User Interface', 'groups' );
echo '</h3>';
echo '<p>';
_e( 'Now you can assign new users directly to groups when you create a new user account from the Dashboard.', 'groups' );
echo ' ';
_e( 'Another improvement is better filtering by groups and a reduced footprint on the Users admin screen.', 'groups' );
echo ' ';
_e( 'And you can now filter the list of users by one or multiple groups with one convenient field.', 'groups' );
echo '</p>';
echo '<h3>';
_e( 'New Documentation', 'groups' );
echo '</h3>';
echo '<p>';
_e( 'Whether you are new to Groups or have been using it before, please make sure to visit the <a target="_blank" href="http://docs.itthinx.com/document/groups/">Documentation</a> pages to know more about how to use it.', 'groups' );
echo '</p>';
$legacy_enabled = Groups_Options::get_option( GROUPS_LEGACY_ENABLE );
echo '<h2>';
printf( __( 'Switching to Groups %s', 'groups' ), esc_html( $groups_version ) );
echo '</h2>';
echo '<p>';
printf( __( 'Groups %s features a simpler model for access restrictions based on groups instead of capabilities used in previous versions.', 'groups' ), esc_html( $groups_version ) );
echo ' ';
_e( 'To put it simple, previously you would have used capabilities to restrict access to posts and now you simply use groups.', 'groups' );
echo ' ';
_e( 'To make it easier to transition to the new model for those who migrate from a previous version, we have included legacy access control based on capabilities.', 'groups' );
echo '</p>';
echo '<div class="indent">';
echo '<p>';
_e( 'The following is only of interest if you have upgraded from Groups 1.x:', 'groups' );
echo '<br/>';
if ( $legacy_enabled ) {
_e( 'You are running the system with legacy access control based on capabilities enabled.', 'groups' );
echo ' ';
_e( 'This means that if you had access restrictions in place that were based on capabilities, your entries will still be protected.', 'groups' );
} else {
_e( 'You are running the system with legacy access control based on capabilities disabled.', 'groups' );
echo ' ';
_e( 'This could be important!', 'groups' );
echo ' ';
_e( 'If you had any access restrictions in place based on capabilities, the entries will now be unprotected, unless you enable legacy access restrictions or place appropriate access restrictions based on groups on the desired entries.', 'groups' );
}
echo '</p>';
echo '<p>';
_e( 'If you would like to switch to access restrictions based on groups (recommended) instead of capabilities, you can easily do so by setting the appropriate groups on your protected posts, pages and other entries to restrict access.', 'groups' );
echo ' ';
_e( 'Once you have adjusted your access restrictions based on groups, you can disable legacy access control.', 'groups' );
echo ' ';
_e( 'Please refer to the <a target="_blank" href="http://docs.itthinx.com/document/groups/">Documentation</a> for details on how to switch to and use the new access restrictions.', 'groups' );
echo '</p>';
echo '</div>'; // .indent
echo '<h2>';
_e( 'Add-Ons', 'groups' );
echo '</h2>';
echo '<p>';
_e( 'Perfect complements to memberships and access control with Groups.', 'groups' );
echo '</p>';
echo '<div class="groups-admin-add-ons">';
groups_admin_add_ons_content( array( 'offset' => 1 ) );
echo '</div>'; // .groups-admin-add-ons
echo '</div>'; // .groups-welcome-panel-content
echo '</div>'; // .groups-welcome-panel
}
}
Groups_Admin_Welcome::init();

View File

@@ -0,0 +1,315 @@
<?php
/**
* class-groups-admin.php
*
* Copyright (c) 2011 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Groups admin sections initialization.
*/
class Groups_Admin {
/**
* The position of the Groups menu.
*
* @var int
*/
const MENU_POSITION = '38.381';
/**
* Holds admin messages.
* @var string
*/
private static $messages = array();
/**
* Sets up action hooks.
*/
public static function init() {
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
add_action( 'network_admin_menu', array( __CLASS__, 'network_admin_menu' ) );
add_filter( 'plugin_action_links_'. plugin_basename( GROUPS_FILE ), array( __CLASS__, 'plugin_action_links' ) );
add_action( 'after_plugin_row_' . plugin_basename( GROUPS_FILE ), array( __CLASS__, 'after_plugin_row' ), 10, 3 );
}
/**
* Hooks into admin_init.
* @see Groups_Admin::admin_menu()
* @see Groups_Admin::admin_print_styles()
* @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_scripts_only_on_plugin_pages
*/
public static function admin_init() {
global $groups_version;
wp_register_style( 'groups_admin', GROUPS_PLUGIN_URL . 'css/groups_admin.css', array(), $groups_version );
require_once GROUPS_VIEWS_LIB . '/class-groups-uie.php';
}
/**
* Loads styles for the Groups admin section.
*
* @see Groups_Admin::admin_menu()
*/
public static function admin_print_styles() {
wp_enqueue_style( 'groups_admin' );
}
/**
* Loads scripts.
*/
public static function admin_print_scripts() {
global $groups_version;
// this one's currently empty
//wp_enqueue_script( 'groups_admin', GROUPS_PLUGIN_URL . 'js/groups_admin.js', array( ), $groups_version );
Groups_UIE::enqueue( 'select' );
}
/**
* Add a message to the list of messages displayed in the admin sections.
* The message is filtered using wp_filter_kses() and wrapped in a div
* with class 'updated' for messages of type 'info' and 'error' for
* those of type 'error'.
*
* @param string $message the message
* @param string $type type of message, defaults to 'info'
* @uses wp_filter_kses()
*/
public static function add_message( $message, $type = 'info' ) {
$class = 'updated';
switch( $type ) {
case 'error' :
$class = 'error';
}
self::$messages[] = '<div class="'.$class.'">' . balanceTags( stripslashes( wp_filter_kses( $message ) ), true ) . '</div>';
}
/**
* Returns the list of messages as a string.
* An empty string is returned if there are no messages.
*
* @return string
*/
public static function render_messages() {
$output = '';
if ( !empty( self::$messages ) ) {
$output .= '<div class="groups messages">';
$output .= implode( '', self::$messages );
$output .= '</div>';
}
return $output;
}
/**
* Prints admin notices.
*/
public static function admin_notices() {
global $groups_admin_messages;
if ( !empty( $groups_admin_messages ) ) {
foreach ( $groups_admin_messages as $msg ) {
echo $msg;
}
}
}
/**
* Use a context-sensitive menu item title.
*/
public static function admin_head() {
global $submenu;
if ( isset( $submenu['groups-admin'] ) ) {
$submenu['groups-admin'][0][0] = _x( 'Groups', 'menu item title', 'groups' );
}
}
/**
* Admin menu.
*/
public static function admin_menu() {
include_once( GROUPS_ADMIN_LIB . '/groups-admin-groups.php');
include_once( GROUPS_ADMIN_LIB . '/groups-admin-capabilities.php');
include_once( GROUPS_ADMIN_LIB . '/groups-admin-options.php');
include_once( GROUPS_ADMIN_LIB . '/groups-admin-add-ons.php');
$pages = array();
// main
$page = add_menu_page(
_x( 'Groups', 'page-title', 'groups' ),
'Groups', // don't translate, reasons: a) Groups menu title consistency and b) http://core.trac.wordpress.org/ticket/18857 translation affects $screen->id
GROUPS_ADMINISTER_GROUPS,
'groups-admin',
apply_filters( 'groups_add_menu_page_function', 'groups_admin_groups' ),
GROUPS_PLUGIN_URL . '/images/groups.png',
self::MENU_POSITION
);
$pages[] = $page;
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) );
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) );
if ( isset( $_POST[GROUPS_ADMIN_OPTIONS_NONCE] ) && wp_verify_nonce( $_POST[GROUPS_ADMIN_OPTIONS_NONCE], 'admin' ) ) {
$show_tree_view = !empty( $_POST[GROUPS_SHOW_TREE_VIEW] );
} else {
$show_tree_view = Groups_Options::get_option( GROUPS_SHOW_TREE_VIEW, GROUPS_SHOW_TREE_VIEW_DEFAULT );
}
if ( $show_tree_view ) {
include_once( GROUPS_ADMIN_LIB . '/groups-admin-tree-view.php');
$page = add_submenu_page(
'groups-admin',
__( 'Tree', 'groups' ),
__( 'Tree', 'groups' ),
GROUPS_ACCESS_GROUPS,
'groups-admin-tree-view',
apply_filters( 'groups_add_submenu_page_function', 'groups_admin_tree_view' )
);
$pages[] = $page;
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) );
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) );
}
// capabilities
$page = add_submenu_page(
'groups-admin',
__( 'Groups Capabilities', 'groups' ),
__( 'Capabilities', 'groups' ),
GROUPS_ADMINISTER_GROUPS,
'groups-admin-capabilities',
apply_filters( 'groups_add_submenu_page_function', 'groups_admin_capabilities' )
);
$pages[] = $page;
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) );
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) );
// options
$page = add_submenu_page(
'groups-admin',
__( 'Groups options', 'groups' ),
__( 'Options', 'groups' ),
GROUPS_ADMINISTER_OPTIONS,
'groups-admin-options',
apply_filters( 'groups_add_submenu_page_function', 'groups_admin_options' )
);
$pages[] = $page;
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) );
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) );
// add-ons
$page = add_submenu_page(
'groups-admin',
__( 'Groups Add-Ons', 'groups' ),
__( 'Add-Ons', 'groups' ),
GROUPS_ACCESS_GROUPS,
'groups-admin-add-ons',
apply_filters( 'groups_add_submenu_page_function', 'groups_admin_add_ons' )
);
$pages[] = $page;
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) );
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) );
do_action( 'groups_admin_menu', $pages );
}
/**
* Network admin menu.
*/
public static function network_admin_menu() {
include_once( GROUPS_ADMIN_LIB . '/groups-admin-options.php');
$pages = array();
// main
$page = add_menu_page(
__( 'Groups', 'groups' ),
__( 'Groups', 'groups' ),
GROUPS_ADMINISTER_GROUPS,
'groups-network-admin',
apply_filters( 'groups_add_menu_page_function', 'groups_network_admin_options' ),
GROUPS_PLUGIN_URL . '/images/groups.png'
);
$pages[] = $page;
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) );
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) );
do_action( 'groups_network_admin_menu', $pages );
}
/**
* Adds plugin links.
*
* @param array $links
* @param array $links with additional links
*/
public static function plugin_action_links( $links ) {
if ( current_user_can( GROUPS_ADMINISTER_OPTIONS ) ) {
array_unshift(
$links,
'<a href="' . get_admin_url( null, 'admin.php?page=groups-admin-options' ) . '">' . __( 'Options', 'groups' ) . '</a>'
);
}
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
array_unshift(
$links,
'<a href="' . get_admin_url( null, 'admin.php?page=groups-admin' ) . '">' . __( 'Groups', 'groups' ) . '</a>'
);
}
return $links;
}
/**
* Prints a warning when data is deleted on deactivation.
*
* @param string $plugin_file
* @param array $plugin_data
* @param string $status
*/
public static function after_plugin_row( $plugin_file, $plugin_data, $status ) {
if ( $plugin_file == plugin_basename( GROUPS_FILE ) ) {
$delete_data = Groups_Options::get_option( 'groups_delete_data', false );
$delete_network_data = Groups_Options::get_option( 'groups_network_delete_data', false );
if (
( is_plugin_active( $plugin_file ) && $delete_data && current_user_can( 'install_plugins' ) ) ||
( is_plugin_active_for_network( $plugin_file ) && $delete_network_data && current_user_can( 'manage_network_plugins' ) )
) {
echo '<tr class="active">';
echo '<td>&nbsp;</td>';
echo '<td colspan="2">';
echo '<div style="border: 2px solid #dc3232; padding: 1em">';
echo '<p>';
echo '<strong>';
echo esc_html( __( 'Warning!', 'groups' ) );
echo '</strong>';
echo '</p>';
echo '<p>';
echo esc_html( __( 'Groups is configured to delete its plugin data on deactivation.', 'groups' ) );
echo '</p>';
echo '</div>';
echo '</td>';
echo '</tr>';
}
}
}
}
Groups_Admin::init();

View File

@@ -0,0 +1,202 @@
<?php
/**
* groups-admin-add-ons.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.8.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Renders the heading and content container for the Add-Ons section.
*/
function groups_admin_add_ons() {
echo '<div class="groups-admin-add-ons wrap">';
echo '<h1>';
echo __( 'Add-Ons', 'groups' );
echo '</h1>';
groups_admin_add_ons_content();
echo '</div>'; // .groups-admin-add-ons.wrap
}
/**
* Renders the content of the Add-Ons section.
*
* @param $params array of options (offset is 0 by default and used to adjust heading h2)
*/
function groups_admin_add_ons_content( $params = array( 'offset' => 0 ) ) {
$d = intval( $params['offset'] );
$h2 = sprintf( 'h%d', 2+$d );
echo "<$h2>";
echo __( 'Recommended extensions for Groups', 'groups' );
echo "</$h2>";
$entries = array(
'groups-file-access' => array(
'title' => 'Groups File Access',
'content' => 'Groups File Access is a WordPress plugin that allows to provide file download links for authorized users. Access to files is restricted to users by their group membership.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/groups-file-access.png',
'url' => 'http://www.itthinx.com/shop/groups-file-access/',
'index' => 100
),
'groups-forums' => array(
'title' => 'Groups Forums',
'content' => 'Groups Forums provides a powerful and yet light-weight forum system for WordPress sites.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/groups-forums.png',
'url' => 'http://www.itthinx.com/shop/groups-forums/',
'index' => 100
),
'groups-gravity-forms' => array(
'title' => 'Groups Gravity Forms',
'content' => 'This extension integrates Groups with Gravity Forms. It allows to add users to groups automatically, based on form submissions.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/groups-gravity-forms.png',
'url' => 'http://www.itthinx.com/shop/groups-gravity-forms/',
'index' => 100
),
'groups-import-export' => array(
'title' => 'Groups Import Export',
'content' => 'This is an extension for Groups, providing import and export facilities. Users can be imported and assigned to groups in bulk from a text file. Users can be exported in bulk, including all users or users that belong to specific groups.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/groups-import-export.png',
'url' => 'http://www.itthinx.com/shop/groups-import-export/',
'index' => 100
),
'groups-newsletters' => array(
'title' => 'Groups Newsletters',
'content' => 'Newsletter Campaigns for Subscribers and Groups. Groups Newsletters helps you to communicate efficiently, providing targeted information to groups of recipients through automated campaigns.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/groups-newsletters.png',
'url' => 'http://www.itthinx.com/shop/groups-newsletters/',
'index' => 100
),
'groups-paypal' => array(
'title' => 'Groups PayPal',
'content' => 'Sell memberships and subscriptions with Groups and PayPal.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/groups-paypal.png',
'url' => 'http://www.itthinx.com/shop/groups-paypal/',
'index' => 10
),
'groups-restrict-categories' => array(
'title' => 'Groups Restrict Categories',
'content' => 'Access restrictions for categories and tags, also supporting custom post types and taxonomies.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/groups-restrict-categories.png',
'url' => 'http://www.itthinx.com/shop/groups-restrict-categories/',
'index' => 10
),
'groups-restrict-comments-pro' => array(
'title' => 'Groups Restrict Comments Pro',
'content' => 'This extension allows to restrict who can post or read comments based on a users group membership.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/groups-restrict-comments-pro.png',
'url' => 'http://www.itthinx.com/shop/groups-restrict-comments-pro/',
'index' => 100
),
'groups-woocommerce' => array(
'title' => 'Groups WooCommerce',
'content' => 'This extension allows you to sell memberships with WooCommerce.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/groups-woocommerce.png',
'url' => 'http://www.itthinx.com/shop/groups-woocommerce/',
'index' => 20
),
'widgets-control-pro' => array(
'title' => 'Widgets Control Pro',
'content' => 'An advanced Widget toolbox that adds visibility management and helps to control where widgets are shown efficiently. Show or hide widgets based on a users group membership.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/widgets-control-pro.png',
'url' => 'http://www.itthinx.com/shop/widgets-control-pro/',
'index' => 20
),
'woocommerce-group-coupons' => array(
'title' => 'WooCommerce Group Coupons',
'content' => 'This extension allows to limit the validity of coupons based on groups and roles.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/woocommerce-group-coupons.png',
'url' => 'http://www.itthinx.com/shop/woocommerce-group-coupons/',
'index' => 100
),
'woocommerce-groups-newsletters' => array(
'title' => 'WooCommerce Groups Newsletters',
'content' => 'The WooCommerce Groups Newsletters extension lets customers subscribe to newsletters at checkout.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/woocommerce-groups-newsletters.png',
'url' => 'http://www.itthinx.com/shop/woocommerce-groups-newsletters/',
'index' => 100
),
);
usort( $entries, 'groups_admin_add_ons_sort' );
echo '<ul class="add-ons">';
foreach( $entries as $key => $entry ) {
echo '<li class="add-on">';
echo sprintf( '<a href="%s">', $entry['url'] );
echo '<h3>';
echo sprintf( '<img src="%s"/>', $entry['image'] );
echo $entry['title'];
echo '</h3>';
echo '<p>';
echo $entry['content'];
echo '</p>';
echo '</a>';
echo '</li>'; // .add-on
}
echo '</ul>'; // .add-ons
echo "<$h2>";
echo __( 'Recommended plugins by itthinx', 'groups' );
echo "</$h2>";
$entries = array(
'affiliates-pro' => array(
'title' => 'Affiliates Pro',
'content' => 'Boost Sales with Affiliate Marketing for your WordPress site.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/affiliates-pro.png',
'url' => 'http://www.itthinx.com/shop/affiliates-pro/',
'index' => 100
),
'affiliates-enterprise' => array(
'title' => 'Affiliates Enterprise',
'content' => 'Affiliates Enterprise provides an affiliate management system for sellers, shops and developers, who want to boost sales with their own affiliate program. Features affiliate campaigns, tracking pixels and multiple tiers.',
'image' => GROUPS_PLUGIN_URL . 'images/add-ons/affiliates-enterprise.png',
'url' => 'http://www.itthinx.com/shop/affiliates-enterprise/',
'index' => 100
),
);
usort( $entries, 'groups_admin_add_ons_sort' );
echo '<ul class="add-ons">';
foreach( $entries as $key => $entry ) {
echo '<li class="add-on">';
echo sprintf( '<a href="%s">', $entry['url'] );
echo '<h3>';
echo sprintf( '<img src="%s"/>', $entry['image'] );
echo $entry['title'];
echo '</h3>';
echo '<p>';
echo $entry['content'];
echo '</p>';
echo '</a>';
echo '</li>'; // .add-on
}
echo '</ul>'; // .add-ons
}
function groups_admin_add_ons_sort( $e1, $e2 ) {
$i1 = isset( $e1['index'] ) ? $e1['index'] : 0;
$i2 = isset( $e2['index'] ) ? $e2['index'] : 0;
$t1 = isset( $e1['title'] ) ? $e1['title'] : '';
$t2 = isset( $e2['title'] ) ? $e2['title'] : '';
return $i1 - $i2 + strnatcmp( $t1, $t2 );
}

View File

@@ -0,0 +1,108 @@
<?php
/**
* groups-admin-capabilities-add.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Show add capability form.
*/
function groups_admin_capabilities_add() {
global $wpdb;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'paged', $current_url );
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'capability_id', $current_url );
$capability = isset( $_POST['capability-field'] ) ? $_POST['capability-field'] : '';
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : '';
$capability_table = _groups_get_tablename( 'capability' );
$output =
'<div class="manage-capabilities wrap">' .
'<h1>' .
__( 'Add a new capability', 'groups' ) .
'</h1>' .
Groups_Admin::render_messages() .
'<form id="add-capability" action="' . esc_url( $current_url ) . '" method="post">' .
'<div class="capability new">' .
'<div class="field">' .
'<label for="capability-field" class="field-label first required">' .__( 'Capability', 'groups' ) . '</label>' .
'<input id="name-field" name="capability-field" class="capability-field" type="text" value="' . esc_attr( stripslashes( $capability ) ) . '"/>' .
'</div>' .
'<div class="field">' .
'<label for="description-field" class="field-label description-field">' .__( 'Description', 'groups' ) . '</label>' .
'<textarea id="description-field" name="description-field" rows="5" cols="45">' . stripslashes( wp_filter_nohtml_kses( $description ) ) . '</textarea>' .
'</div>' .
'<div class="field">' .
wp_nonce_field( 'capabilities-add', GROUPS_ADMIN_GROUPS_NONCE, true, false ) .
'<input class="button button-primary" type="submit" value="' . __( 'Add', 'groups' ) . '"/>' .
'<input type="hidden" value="add" name="action"/>' .
'<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', 'groups' ) . '</a>' .
'</div>' .
'</div>' . // .capability.new
'</form>' .
'</div>'; // .manage-capabilities
echo $output;
} // function groups_admin_capabilities_add
/**
* Handle add capability form submission.
* @return int new capability's id or false if unsuccessful
*/
function groups_admin_capabilities_add_submit() {
global $wpdb;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE], 'capabilities-add' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$capability = isset( $_POST['capability-field'] ) ? $_POST['capability-field'] : null;
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : '';
$capability_id = Groups_Capability::create( compact( "capability", "description" ) );
if ( !$capability_id ) {
if ( empty( $capability ) ) {
Groups_Admin::add_message( __( 'The <em>Capability</em> must not be empty.', 'groups' ), 'error' );
} else if ( Groups_Capability::read_by_capability( $capability ) ) {
Groups_Admin::add_message( sprintf( __( 'The <em>%s</em> capability already exists.', 'groups' ), stripslashes( wp_filter_nohtml_kses( ( $capability ) ) ) ), 'error' );
}
}
return $capability_id;
} // function groups_admin_capabilities_add_submit

View File

@@ -0,0 +1,134 @@
<?php
/**
* groups-admin-capability-edit.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Show edit capability form.
* @param int $capability_id capability id
*/
function groups_admin_capabilities_edit( $capability_id ) {
global $wpdb;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$capability = Groups_Capability::read( intval( $capability_id ) );
if ( empty( $capability ) ) {
wp_die( __( 'No such capability.', 'groups' ) );
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'capability_id', $current_url );
$capability_capability = isset( $_POST['capability-field'] ) ? $_POST['capability-field'] : $capability->capability;
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : $capability->description;
$capability_readonly = ( $capability->capability !== Groups_Post_Access::READ_POST_CAPABILITY ) ? "" : ' readonly="readonly" ';
$output =
'<div class="manage-capabilities wrap">' .
'<h1>' .
__( 'Edit a capability', 'groups' ) .
'</h1>' .
Groups_Admin::render_messages() .
'<form id="edit-capability" action="' . esc_url( $current_url ) . '" method="post">' .
'<div class="capability edit">' .
'<input id="capability-id-field" name="capability-id-field" type="hidden" value="' . esc_attr( intval( $capability_id ) ) . '"/>' .
'<div class="field">' .
'<label for="capability-field" class="field-label first required">' .__( 'Capability', 'groups' ) . '</label>' .
'<input ' . $capability_readonly . ' id="capability-field" name="capability-field" class="capability-field" type="text" value="' . esc_attr( stripslashes( $capability_capability ) ) . '"/>' .
'</div>' .
'<div class="field">' .
'<label for="description-field" class="field-label description-field">' .__( 'Description', 'groups' ) . '</label>' .
'<textarea id="description-field" name="description-field" rows="5" cols="45">' . stripslashes( wp_filter_nohtml_kses( $description ) ) . '</textarea>' .
'</div>' .
'<div class="field">' .
wp_nonce_field( 'capabilities-edit', GROUPS_ADMIN_GROUPS_NONCE, true, false ) .
'<input class="button button-primary" type="submit" value="' . __( 'Save', 'groups' ) . '"/>' .
'<input type="hidden" value="edit" name="action"/>' .
'<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', 'groups' ) . '</a>' .
'</div>' .
'</div>' . // .capability.edit
'</form>' .
'</div>'; // .manage-capabilities
echo $output;
} // function groups_admin_capabilities_edit
/**
* Handle edit form submission.
*/
function groups_admin_capabilities_edit_submit() {
$result = false;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE], 'capabilities-edit' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$capability_id = isset( $_POST['capability-id-field'] ) ? $_POST['capability-id-field'] : null;
$capability = Groups_Capability::read( $capability_id );
if ( $capability ) {
$capability_id = $capability->capability_id;
if ( $capability->capability !== Groups_Post_Access::READ_POST_CAPABILITY ) {
$capability_field = isset( $_POST['capability-field'] ) ? $_POST['capability-field'] : null;
} else {
$capability_field = Groups_Post_Access::READ_POST_CAPABILITY;
}
if ( !empty( $capability_field ) ) {
$update = true;
if ( $other_capability = Groups_Capability::read_by_capability( $capability_field ) ) {
if ( $other_capability->capability_id != $capability_id ) {
Groups_Admin::add_message( sprintf( __( 'The <em>%s</em> capability already exists and cannot be assigned to this one.', 'groups' ), stripslashes( wp_filter_nohtml_kses( $other_capability->capability ) ) ), 'error' );
$update = false;
}
}
if ( $update ) {
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : '';
$capability_id = Groups_Capability::update( array( 'capability_id' => $capability_id, 'capability' => $capability_field, 'description' => $description ) );
if ( $capability_id ) {
$result = $capability_id;
} else {
Groups_Admin::add_message( sprintf( __( 'The <em>%s</em> capability could not be updated.', 'groups' ), stripslashes( wp_filter_nohtml_kses( $capability ) ) ), 'error' );
}
}
} else {
Groups_Admin::add_message( __( 'The <em>Capability</em> must not be empty.', 'groups' ), 'error' );
}
}
return $result;
} // function groups_admin_capabilities_edit_submit

View File

@@ -0,0 +1,198 @@
<?php
/**
* groups-admin-capabilities-remove.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Shows form to confirm capability deletion.
* @param int $capability_id capability id
*/
function groups_admin_capabilities_remove( $capability_id ) {
global $wpdb;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$capability = Groups_Capability::read( intval( $capability_id ) );
if ( empty( $capability ) ) {
wp_die( __( 'No such capability.', 'groups' ) );
}
$capability_table = _groups_get_tablename( 'capability' );
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'capability_id', $current_url );
$output =
'<div class="manage-capabilities wrap">' .
'<h1>' .
__( 'Remove a capability', 'groups' ) .
'</h1>' .
'<form id="remove-capability" action="' . esc_url( $current_url ) . '" method="post">' .
'<div class="capability remove">' .
'<input id="capability-id-field" name="capability-id-field" type="hidden" value="' . esc_attr( intval( $capability->capability_id ) ) . '"/>' .
'<ul>' .
'<li>' . sprintf( __( 'Capability : %s', 'groups' ), stripslashes( wp_filter_nohtml_kses( $capability->capability ) ) ) . '</li>' .
'</ul> ' .
wp_nonce_field( 'capabilities-remove', GROUPS_ADMIN_GROUPS_NONCE, true, false ) .
'<input class="button button-primary" type="submit" value="' . __( 'Remove', 'groups' ) . '"/>' .
'<input type="hidden" value="remove" name="action"/>' .
'<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', 'groups' ) . '</a>' .
'</div>' .
'</div>' . // .capability.remove
'</form>' .
'</div>'; // .manage-capabilities
echo $output;
} // function groups_admin_capabilities_remove
/**
* Handle remove form submission.
*/
function groups_admin_capabilities_remove_submit() {
global $wpdb;
$result = false;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE], 'capabilities-remove' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$capability_id = isset( $_POST['capability-id-field'] ) ? $_POST['capability-id-field'] : null;
$capability = Groups_Capability::read( $capability_id );
if ( $capability ) {
if ( $capability->capability !== Groups_Post_Access::READ_POST_CAPABILITY ) {
$result = Groups_Capability::delete( $capability_id );
}
}
return $result;
} // function groups_admin_capabilities_remove_submit
/**
* Shows form to confirm removal bulk capabilities
*/
function groups_admin_capabilities_bulk_remove() {
global $wpdb;
$output = '';
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$capability_ids = isset( $_POST['capability_ids'] ) ? $_POST['capability_ids'] : null;
if ( ! $capability_ids ) {
wp_die( __( 'No such capabilities.', 'groups' ) );
}
$capabilities = array();
foreach ( $capability_ids as $capability_id ) {
$capability = Groups_Capability::read( intval( $capability_id ) );
if ( $capability ) {
$capabilities[] = $capability;
}
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'capability_id', $current_url );
$output .= '<div class="manage-capabilities wrap">';
$output .= '<h1>';
$output .= __( 'Remove capabilities', 'groups' );
$output .= '</h1>';
$output .= '<form id="capabilities-action" method="post" action="">';
$output .= '<div class="capability remove">';
$output .= '<p>';
$output .= __( 'Please confirm to remove the following capabilities. This action cannot be undone.', 'groups' );
$output .= '</p>';
foreach ( $capabilities as $capability ) {
$output .= '<input id="capability_ids" name="capability_ids[]" type="hidden" value="' . esc_attr( intval( $capability->capability_id ) ) . '"/>';
$output .= '<ul>';
$output .= '<li>';
$output .= sprintf( __( '<strong>%s</strong>', 'groups' ), wp_filter_nohtml_kses( $capability->capability ) );
$output .= '</li>';
$output .= '</ul>';
}
$output .= '<input class="button button-primary" type="submit" name="bulk" value="' . __( "Remove", 'groups' ) . '"/>';
$output .= '<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', 'groups' ) . '</a>';
$output .= '<input type="hidden" name="action" value="groups-action"/>';
$output .= '<input type="hidden" name="bulk-action" value="remove"/>';
$output .= '<input type="hidden" name="confirm" value="1"/>';
$output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_ACTION_NONCE, true, false );
$output .= '</div>';
$output .= '</form>';
$output .= '</div>';
echo $output;
} // function groups_admin_capabilities_bulk_remove
/**
* Handle remove form submission.
* @return array of deleted capabilities' ids
*/
function groups_admin_capabilities_bulk_remove_submit() {
global $wpdb;
$result = array();
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_ACTION_NONCE], 'admin' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$capability_ids = isset( $_POST['capability_ids'] ) ? $_POST['capability_ids'] : null;
if ( $capability_ids ) {
foreach ( $capability_ids as $capability_id ) {
$capability = Groups_Capability::read( $capability_id );
if ( $capability ) {
if ( $capability->capability !== Groups_Post_Access::READ_POST_CAPABILITY ) {
if ( Groups_Capability::delete( $capability_id ) ) {
$result[] = $capability->capability_id;
}
}
}
}
}
return $result;
} // function groups_admin_capabilities_bulk_remove_submit

View File

@@ -0,0 +1,506 @@
<?php
/**
* groups-admin-capabilities.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
define( 'GROUPS_CAPABILITIES_PER_PAGE', 10 );
define( 'GROUPS_ADMIN_CAPABILITIES_NONCE_1', 'groups-cap-nonce-1');
define( 'GROUPS_ADMIN_CAPABILITIES_NONCE_2', 'groups-cap-nonce-2');
define( 'GROUPS_ADMIN_CAPABILITIES_ACTION_NONCE', 'groups-cap-action-nonce');
define( 'GROUPS_ADMIN_CAPABILITIES_FILTER_NONCE', 'groups-cap-filter-nonce' );
require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
require_once( GROUPS_ADMIN_LIB . '/groups-admin-capabilities-add.php');
require_once( GROUPS_ADMIN_LIB . '/groups-admin-capabilities-edit.php');
require_once( GROUPS_ADMIN_LIB . '/groups-admin-capabilities-remove.php');
/**
* Manage capabilities: table of capabilities and add, edit, remove actions.
*/
function groups_admin_capabilities() {
global $wpdb;
$output = '';
$today = date( 'Y-m-d', time() );
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
//
// handle actions
//
if ( isset( $_POST['action'] ) ) {
// handle action submit - do it
switch( $_POST['action'] ) {
case 'add' :
if ( !( $capability_id = groups_admin_capabilities_add_submit() ) ) {
return groups_admin_capabilities_add();
} else {
$capability = Groups_Capability::read( $capability_id );
Groups_Admin::add_message( sprintf( __( 'The <em>%s</em> capability has been created.', 'groups' ), stripslashes( wp_filter_nohtml_kses( $capability->capability ) ) ) );
}
break;
case 'edit' :
if ( !( $capability_id = groups_admin_capabilities_edit_submit() ) ) {
return groups_admin_capabilities_edit( $_POST['capability-id-field'] );
} else {
$capability = Groups_Capability::read( $capability_id );
Groups_Admin::add_message( sprintf( __( 'The <em>%s</em> capability has been updated.', 'groups' ), stripslashes( wp_filter_nohtml_kses( $capability->capability ) ) ) );
}
break;
case 'remove' :
if ( $capability_id = groups_admin_capabilities_remove_submit() ) {
Groups_Admin::add_message( __( 'The capability has been deleted.', 'groups' ) );
}
break;
// bulk actions on groups: capabilities
case 'groups-action' :
if ( wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_ACTION_NONCE], 'admin' ) ) {
$capability_ids = isset( $_POST['capability_ids'] ) ? $_POST['capability_ids'] : null;
$bulk = isset( $_POST['bulk'] ) ? $_POST['bulk'] : null;
if ( is_array( $capability_ids ) && ( $bulk !== null ) ) {
foreach ( $capability_ids as $capability_id ) {
$bulk_action = isset( $_POST['bulk-action'] ) ? $_POST['bulk-action'] : null;
switch( $bulk_action ) {
case 'remove' :
if ( isset( $_POST['confirm'] ) ) {
groups_admin_capabilities_bulk_remove_submit();
} else {
return groups_admin_capabilities_bulk_remove();
}
break;
}
break;
}
}
}
break;
}
} else if ( isset ( $_GET['action'] ) ) {
// handle action request - show form
switch( $_GET['action'] ) {
case 'add' :
return groups_admin_capabilities_add();
break;
case 'edit' :
if ( isset( $_GET['capability_id'] ) ) {
return groups_admin_capabilities_edit( $_GET['capability_id'] );
}
break;
case 'remove' :
if ( isset( $_GET['capability_id'] ) ) {
return groups_admin_capabilities_remove( $_GET['capability_id'] );
}
break;
case 'refresh' :
if ( check_admin_referer( 'refresh' ) ) {
$n = Groups_WordPress::refresh_capabilities();
if ( $n > 0 ) {
$output .= '<div class="updated fade"><p>' . sprintf( _n( 'One capability has been added.', '%d capabilities have been added.', $n, 'groups' ), $n ) . '</p></div>';
} else {
$output .= '<div class="updated fade"><p>' . __( 'No new capabilities have been found.', 'groups' ) . '</p></div>';
}
} else {
wp_die( __( 'A Duck!', 'groups' ) );
}
break;
}
}
//
// capabilities table
//
if (
isset( $_POST['clear_filters'] ) ||
isset( $_POST['capability_id'] ) ||
isset( $_POST['capability'] )
) {
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_CAPABILITIES_FILTER_NONCE], 'admin' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
}
// filters
$capability_id = Groups_Options::get_user_option( 'capabilities_capability_id', null );
$capability = Groups_Options::get_user_option( 'capabilities_capability', null );
if ( isset( $_POST['clear_filters'] ) ) {
Groups_Options::delete_user_option( 'capabilities_capability_id' );
Groups_Options::delete_user_option( 'capabilities_capability' );
$capability_id = null;
$capability = null;
} else if ( isset( $_POST['submitted'] ) ) {
// filter by name
if ( !empty( $_POST['capability'] ) ) {
$capability = $_POST['capability'];
Groups_Options::update_user_option( 'capabilities_capability', $capability );
}
// filter by capability id
if ( !empty( $_POST['capability_id'] ) ) {
$capability_id = intval( $_POST['capability_id'] );
Groups_Options::update_user_option( 'capabilities_capability_id', $capability_id );
} else if ( isset( $_POST['capability_id'] ) ) { // empty && isset => '' => all
$capability_id = null;
Groups_Options::delete_user_option( 'capabilities_capability_id' );
}
}
if ( isset( $_POST['row_count'] ) ) {
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_CAPABILITIES_NONCE_1], 'admin' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
}
if ( isset( $_POST['paged'] ) ) {
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_CAPABILITIES_NONCE_2], 'admin' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'paged', $current_url );
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'capability_id', $current_url );
$capability_table = _groups_get_tablename( 'capability' );
$output .=
'<div class="manage-capabilities wrap">' .
'<h1>' .
__( 'Capabilities', 'groups' ) .
// add capability
sprintf(
'<a title="%s" class="add page-title-action" href="%s">',
esc_attr( __( 'Click to add a new capability', 'groups' ) ),
esc_url( $current_url . '&action=add' )
) .
sprintf(
'<img class="icon" alt="%s" src="%s" />',
esc_attr( __( 'Add', 'groups' ) ),
esc_url( GROUPS_PLUGIN_URL . 'images/add.png' )
) .
sprintf(
'<span class="label">%s</span>',
stripslashes( wp_filter_nohtml_kses( __( 'New Capability', 'groups' ) ) )
) .
'</a>' .
// refresh capabilities
sprintf(
'<a title="%s" class="refresh page-title-action" href="%s">',
esc_attr( __( 'Click to refresh capabilities', 'groups' ) ),
esc_url( wp_nonce_url( $current_url . '&action=refresh', 'refresh' ) )
) .
sprintf(
'<img class="icon" alt="%s" src="%s" />',
esc_attr( __( 'Refresh', 'groups' ) ),
esc_url( GROUPS_PLUGIN_URL . 'images/refresh.png' )
) .
sprintf(
'<span class="label">%s</span>',
stripslashes( wp_filter_nohtml_kses( __( 'Refresh', 'groups' ) ) )
) .
'</a>' .
'</h1>';
$output .= Groups_Admin::render_messages();
$row_count = isset( $_POST['row_count'] ) ? intval( $_POST['row_count'] ) : 0;
if ($row_count <= 0) {
$row_count = Groups_Options::get_user_option( 'capabilities_per_page', GROUPS_CAPABILITIES_PER_PAGE );
} else {
Groups_Options::update_user_option('capabilities_per_page', $row_count );
}
$offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
if ( $offset < 0 ) {
$offset = 0;
}
$paged = isset( $_REQUEST['paged'] ) ? intval( $_REQUEST['paged'] ) : 0;
if ( $paged < 0 ) {
$paged = 0;
}
$orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : null;
switch ( $orderby ) {
case 'capability_id' :
case 'capability' :
case 'description' :
break;
default:
$orderby = 'name';
}
$order = isset( $_GET['order'] ) ? $_GET['order'] : null;
switch ( $order ) {
case 'asc' :
case 'ASC' :
$switch_order = 'DESC';
break;
case 'desc' :
case 'DESC' :
$switch_order = 'ASC';
break;
default:
$order = 'ASC';
$switch_order = 'DESC';
}
$filters = array( " 1=%d " );
$filter_params = array( 1 );
if ( $capability_id ) {
$filters[] = " $capability_table.capability_id = %d ";
$filter_params[] = $capability_id;
}
if ( $capability ) {
$filters[] = " $capability_table.capability LIKE '%%%s%%' ";
$filter_params[] = $capability;
}
if ( !empty( $filters ) ) {
$filters = " WHERE " . implode( " AND ", $filters );
} else {
$filters = '';
}
$count_query = $wpdb->prepare( "SELECT COUNT(*) FROM $capability_table $filters", $filter_params );
$count = $wpdb->get_var( $count_query );
if ( $count > $row_count ) {
$paginate = true;
} else {
$paginate = false;
}
$pages = ceil ( $count / $row_count );
if ( $paged > $pages ) {
$paged = $pages;
}
if ( $paged != 0 ) {
$offset = ( $paged - 1 ) * $row_count;
}
$query = $wpdb->prepare(
"SELECT * FROM $capability_table
$filters
ORDER BY $orderby $order
LIMIT $row_count OFFSET $offset",
$filter_params
);
$results = $wpdb->get_results( $query, OBJECT );
$column_display_names = array(
'capability_id' => __( 'ID', 'groups' ),
'capability' => __( 'Capability', 'groups' ),
'description' => __( 'Description', 'groups' )
);
$output .= '<div class="capabilities-overview">';
$output .=
'<div class="filters">' .
'<form id="setfilters" action="" method="post">' .
'<fieldset>' .
'<legend>' . __( 'Filters', 'groups' ) . '</legend>' .
'<label class="capability-id-filter">' .
__( 'Capability ID', 'groups' ) . ' ' .
'<input class="capability-id-filter" name="capability_id" type="text" value="' . esc_attr( $capability_id ) . '"/>' .
'</label>' . ' ' .
'<label class="capability-filter">' .
__( 'Capability', 'groups' ) . ' ' .
'<input class="capability-filter" name="capability" type="text" value="' . $capability . '"/>' .
'</label>' . ' ' .
wp_nonce_field( 'admin', GROUPS_ADMIN_CAPABILITIES_FILTER_NONCE, true, false ) .
'<input class="button" type="submit" value="' . __( 'Apply', 'groups' ) . '"/>' . ' ' .
'<input class="button" type="submit" name="clear_filters" value="' . __( 'Clear', 'groups' ) . '"/>' .
'<input type="hidden" value="submitted" name="submitted"/>' .
'</fieldset>' .
'</form>' .
'</div>';
if ( $paginate ) {
require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
$pagination = new Groups_Pagination( $count, null, $row_count );
$output .= '<form id="posts-filter" method="post" action="">';
$output .= '<div>';
$output .= wp_nonce_field( 'admin', GROUPS_ADMIN_CAPABILITIES_NONCE_2, true, false );
$output .= '</div>';
$output .= '<div class="tablenav top">';
$output .= $pagination->pagination( 'top' );
$output .= '</div>';
$output .= '</form>';
}
$output .= '<div class="page-options right">';
$output .= '<form id="setrowcount" action="" method="post">';
$output .= '<div>';
$output .= '<label for="row_count">' . __( 'Results per page', 'groups' ) . '</label>';
$output .= '<input name="row_count" type="text" size="2" value="' . esc_attr( $row_count ) .'" />';
$output .= wp_nonce_field( 'admin', GROUPS_ADMIN_CAPABILITIES_NONCE_1, true, false );
$output .= '<input class="button" type="submit" value="' . __( 'Apply', 'groups' ) . '"/>';
$output .= '</div>';
$output .= '</form>';
$output .= '</div>';
$output .= '<form id="groups-action" method="post" action="">';
$output .= '<div class="tablenav top">';
$output .= '<div class="capabilities-bulk-container">';
$output .= '<div class="alignleft actions">';
$output .= '<select name="bulk-action">';
$output .= '<option selected="selected" value="-1">' . esc_html( __( 'Bulk Actions', 'groups' ) ) . '</option>';
$output .= '<option value="remove">' . esc_html( __( 'Remove', 'groups' ) ) . '</option>';
$output .= '</select>';
$output .= '<input class="button" type="submit" name="bulk" value="' . esc_attr( __( "Apply", 'groups' ) ) . '"/>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_ACTION_NONCE, true, false );
$output .= '<input type="hidden" name="action" value="groups-action"/>';
$output .= '<table id="" class="wp-list-table widefat fixed" cellspacing="0">';
$output .= '<thead>';
$output .= '<tr>';
$output .= '<th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox"></th>';
foreach ( $column_display_names as $key => $column_display_name ) {
$options = array(
'orderby' => $key,
'order' => $switch_order
);
$class = $key;
if ( !in_array($key, array( 'capabilities', 'edit', 'remove' ) ) ) {
if ( strcmp( $key, $orderby ) == 0 ) {
$lorder = strtolower( $order );
$class = "$key manage-column sorted $lorder";
} else {
$class = "$key manage-column sortable";
}
$column_display_name =
sprintf(
'<a href="%s"><span>%s</span><span class="sorting-indicator"></span></a>',
esc_url( add_query_arg( $options, $current_url ) ),
esc_html( $column_display_name )
);
} else {
$column_display_name = esc_html( $column_display_name );
}
$output .= sprintf(
'<th scope="col" class="%s">%s</th>',
esc_attr( $class ),
$column_display_name
);
}
$output .= '</tr>';
$output .= '</thead>';
$output .= '<tbody>';
if ( count( $results ) > 0 ) {
for ( $i = 0; $i < count( $results ); $i++ ) {
$result = $results[$i];
// Construct the "edit" URL.
$edit_url = add_query_arg(
array(
'capability_id' => intval( $result->capability_id ),
'action' => 'edit',
'paged' => $paged
),
$current_url
);
// Construct the "delete" URL.
$delete_url = add_query_arg(
array(
'capability_id' => intval( $result->capability_id ),
'action' => 'remove',
'paged' => $paged
),
$current_url
);
// Construct row actions for this group.
$row_actions =
'<div class="row-actions">' .
'<span class="edit">' .
'<a href="' . esc_url( $edit_url ) . '">' .
'<img src="' . GROUPS_PLUGIN_URL . 'images/edit.png"/>' .
__( 'Edit', 'groups' ) .
'</a>';
if ( $result->capability !== Groups_Post_Access::READ_POST_CAPABILITY ) {
$row_actions .=
' | '.
'</span>' .
'<span class="remove trash">' .
'<a href="' . esc_url( $delete_url ) . '" class="submitdelete">' .
'<img src="' . GROUPS_PLUGIN_URL . 'images/remove.png"/>' .
__( 'Remove', 'groups' ) .
'</a>' .
'</span>';
}
$row_actions .= '</div>'; // .row-actions
$output .= '<tr class="' . ( $i % 2 == 0 ? 'even' : 'odd' ) . '">';
$output .= '<th class="check-column">';
$output .= '<input type="checkbox" value="' . esc_attr( $result->capability_id ) . '" name="capability_ids[]"/>';
$output .= '</th>';
$output .= '<td class="capability-id">';
$output .= $result->capability_id;
$output .= '</td>';
$output .= '<td class="capability">';
$output .= sprintf( '<a href="%s">%s</a>', esc_url( $edit_url ), stripslashes( wp_filter_nohtml_kses( $result->capability ) ) );
$output .= $row_actions;
$output .= '</td>';
$output .= '<td class="description">';
$output .= stripslashes( wp_filter_nohtml_kses( $result->description ) );
$output .= '</td>';
$output .= '</tr>';
}
} else {
$output .= '<tr><td colspan="3">' . __( 'There are no results.', 'groups' ) . '</td></tr>';
}
$output .= '</tbody>';
$output .= '</table>';
$output .= Groups_UIE::render_add_titles( '.capabilities-overview table td' );
$output .= '</form>'; // #groups-action
if ( $paginate ) {
require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
$pagination = new Groups_Pagination($count, null, $row_count);
$output .= '<div class="tablenav bottom">';
$output .= $pagination->pagination( 'bottom' );
$output .= '</div>';
}
$output .= '</div>'; // .capabilities-overview
$output .= '</div>'; // .manage-capabilities
echo $output;
} // function groups_admin_capabilities()

View File

@@ -0,0 +1,170 @@
<?php
/**
* groups-admin-groups-add.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.1.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Show add group form.
*/
function groups_admin_groups_add() {
global $wpdb;
$output = '';
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'paged', $current_url );
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'group_id', $current_url );
$parent_id = isset( $_POST['parent-id-field'] ) ? $_POST['parent-id-field'] : '';
$name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : '';
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : '';
$group_table = _groups_get_tablename( 'group' );
$parent_select = '<select name="parent-id-field">';
$parent_select .= '<option value="">--</option>';
$groups = $wpdb->get_results( "SELECT * FROM $group_table" );
foreach ( $groups as $group ) {
$parent_select .= '<option value="' . esc_attr( $group->group_id ) . '">' . wp_filter_nohtml_kses( $group->name ) . '</option>';
}
$parent_select .= '</select>';
$output .= '<div class="manage-groups wrap">';
$output .= '<h1>';
$output .= __( 'Add a new group', 'groups' );
$output .= '</h1>';
$output .= Groups_Admin::render_messages();
$output .= '<form id="add-group" action="' . esc_url( $current_url ) . '" method="post">';
$output .= '<div class="group new">';
$output .= '<div class="field">';
$output .= '<label for="name-field" class="field-label first required">';
$output .= __( 'Name', 'groups' );
$output .= '</label>';
$output .= '<input id="name-field" name="name-field" class="namefield" type="text" value="' . esc_attr( stripslashes( $name ) ) . '"/>';
$output .= '</div>';
$output .= '<div class="field">';
$output .= '<label for="parent-id-field" class="field-label">';
$output .= __( 'Parent', 'groups' );
$output .= '</label>';
$output .= $parent_select;
$output .= '</div>';
$output .= '<div class="field">';
$output .= '<label for="description-field" class="field-label description-field">';
$output .= __( 'Description', 'groups' );
$output .= '</label>';
$output .= '<textarea id="description-field" name="description-field" rows="5" cols="45">';
$output .= stripslashes( wp_filter_nohtml_kses( $description ) );
$output .= '</textarea>';
$output .= '</div>';
$output .= '<div class="field">';
$capability_table = _groups_get_tablename( "capability" );
$capabilities = $wpdb->get_results( "SELECT * FROM $capability_table ORDER BY capability" );
$output .= '<div class="select-capability-container" style="width:62%;">';
$output .= '<label>';
$output .= __( 'Capabilities', 'groups' );
$output .= sprintf(
'<select class="select capability" name="capability_ids[]" multiple="multiple" placeholder="%s">',
__( 'Choose capabilities &hellip;', 'groups' )
);
foreach( $capabilities as $capability ) {
$output .= sprintf( '<option value="%s">%s</option>', esc_attr( $capability->capability_id ), wp_filter_nohtml_kses( $capability->capability ) );
}
$output .= '</select>';
$output .= '</label>';
$output .= '</div>';
$output .= '<p class="description">';
$output .= __( 'These capabilities will be assigned to the group.', 'groups' );
$output .= '</p>';
$output .= Groups_UIE::render_select( '.select.capability' );
$output .= '</div>';
$output .= apply_filters( 'groups_admin_groups_add_form_after_fields', '' );
$output .= '<div class="field">';
$output .= wp_nonce_field( 'groups-add', GROUPS_ADMIN_GROUPS_NONCE, true, false );
$output .= '<input class="button button-primary" type="submit" value="' . __( 'Add', 'groups' ) . '"/>';
$output .= '<input type="hidden" value="add" name="action"/>';
$output .= '<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', 'groups' ) . '</a>';
$output .= '</div>';
$output .= '</div>'; // .group.new
$output .= '</form>';
$output .= '</div>'; // .manage-groups
echo $output;
} // function groups_admin_groups_add
/**
* Handle add group form submission.
* @return int new group's id or false if unsuccessful
*/
function groups_admin_groups_add_submit() {
global $wpdb;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-add' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$creator_id = get_current_user_id();
$datetime = date( 'Y-m-d H:i:s', time() );
$parent_id = isset( $_POST['parent-id-field'] ) ? $_POST['parent-id-field'] : null;
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : '';
$name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : null;
$group_id = Groups_Group::create( compact( "creator_id", "datetime", "parent_id", "description", "name" ) );
if ( $group_id ) {
if ( !empty( $_POST['capability_ids'] ) ) {
$caps = $_POST['capability_ids'];
foreach( $caps as $cap ) {
Groups_Group_Capability::create( array( 'group_id' => $group_id, 'capability_id' => $cap ) );
}
}
do_action( 'groups_admin_groups_add_submit_success', $group_id );
} else {
if ( !$name ) {
Groups_Admin::add_message( __( 'The name must not be empty.', 'groups' ), 'error' );
} else if ( Groups_Group::read_by_name( $name ) ) {
Groups_Admin::add_message( sprintf( __( 'The <em>%s</em> group already exists.', 'groups' ), stripslashes( wp_filter_nohtml_kses( ( $name ) ) ) ), 'error' );
}
}
return $group_id;
} // function groups_admin_groups_add_submit

View File

@@ -0,0 +1,253 @@
<?php
/**
* groups-admin-groups-edit.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.1.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Show edit group form.
* @param int $group_id group id
*/
function groups_admin_groups_edit( $group_id ) {
global $wpdb;
$output = '';
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$group = Groups_Group::read( intval( $group_id ) );
if ( empty( $group ) ) {
wp_die( __( 'No such group.', 'groups' ) );
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'group_id', $current_url );
$name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : $group->name;
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : $group->description;
$parent_id = isset( $_POST['parent-id-field'] ) ? $_POST['parent-id-field'] : $group->parent_id;
$group_table = _groups_get_tablename( 'group' );
$parent_select = '<select name="parent-id-field">';
$parent_select .= '<option value="">--</option>';
$groups = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $group_table WHERE group_id != %d", $group->group_id ) );
foreach ( $groups as $g ) {
$selected = ( $g->group_id == $group->parent_id ? ' selected="selected" ' : '' );
$parent_select .= '<option ' . $selected . 'value="' . esc_attr( $g->group_id ) . '">' . wp_filter_nohtml_kses( $g->name ) . '</option>';
}
$parent_select .= '</select>';
$name_readonly = ( $name !== Groups_Registered::REGISTERED_GROUP_NAME ) ? "" : ' readonly="readonly" ';
$output .= '<div class="manage-groups wrap">';
$output .= '<h1>';
$output .= __( 'Edit a group', 'groups' );
$output .= '</h1>';
$output .= Groups_Admin::render_messages();
$output .= '<form id="edit-group" action="' . esc_url( $current_url ) . '" method="post">';
$output .= '<div class="group edit">';
$output .= '<input id="group-id-field" name="group-id-field" type="hidden" value="' . esc_attr( intval( $group_id ) ) . '"/>';
$output .= '<div class="field">';
$output .= '<label for="name-field" class="field-label first required">';
$output .= __( 'Name', 'groups' );
$output .= '</label>';
$output .= '<input ' . $name_readonly . ' id="name-field" name="name-field" class="namefield" type="text" value="' . esc_attr( stripslashes( $name ) ) . '"/>';
$output .= '</div>';
$output .= '<div class="field">';
$output .= '<label for="parent-id-field" class="field-label">';
$output .= __( 'Parent', 'groups' );
$output .= '</label>';
$output .= $parent_select;
$output .= '</div>';
$output .= '<div class="field">';
$output .= '<label for="description-field" class="field-label description-field">';
$output .= __( 'Description', 'groups' );
$output .= '</label>';
$output .= '<textarea id="description-field" name="description-field" rows="5" cols="45">';
$output .= stripslashes( wp_filter_nohtml_kses( $description ) );
$output .= '</textarea>';
$output .= '</div>';
$capability_table = _groups_get_tablename( 'capability' );
$group_capability_table = _groups_get_tablename( 'group_capability' );
$group_capabilities = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM $capability_table WHERE capability_id IN ( SELECT capability_id FROM $group_capability_table WHERE group_id = %d )",
Groups_Utility::id( $group_id )
) );
$group_capabilities_array = array();
if ( count( $group_capabilities ) > 0 ) {
foreach ( $group_capabilities as $group_capability ) {
$group_capabilities_array[] = $group_capability->capability_id;
}
}
$capabilities = $wpdb->get_results( "SELECT * FROM $capability_table ORDER BY capability" );
$output .= '<div class="field">';
$output .= '<div class="select-capability-container" style="width:62%;">';
$output .= '<label>';
$output .= __( 'Capabilities', 'groups' );
$output .= sprintf(
'<select class="select capability" name="capability_ids[]" multiple="multiple" placeholder="%s">',
__( 'Choose capabilities &hellip;', 'groups' )
);
foreach( $capabilities as $capability ) {
$selected = in_array( $capability->capability_id, $group_capabilities_array ) ? ' selected="selected" ' : '';
$output .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $capability->capability_id ), $selected, wp_filter_nohtml_kses( $capability->capability ) );
}
$output .= '</select>';
$output .= '</label>';
$output .= '</div>'; // .select-capability-container
$output .= '<p class="description">';
$output .= __( 'The chosen capabilities are assigned to the group.', 'groups' );
$output .= '</p>';
$output .= '</div>'; // .field
$output .= Groups_UIE::render_select( '.select.capability' );
$group_object = new Groups_Group( $group_id );
$group_capabilities = $group_object->capabilities;
$group_capabilities_deep = $group_object->capabilities_deep;
if (
(
( !empty( $group_capabilities_deep ) ? count( $group_capabilities_deep ) : 0 ) -
( !empty( $group_capabilities ) ? count( $group_capabilities ) : 0 )
) > 0
) {
usort( $group_capabilities_deep, array( 'Groups_Utility', 'cmp' ) );
$output .= '<div class="field">';
$output .= __( 'Inherited capabilities:', 'groups' );
$output .= ' ';
$inherited_caps = array();
foreach ( $group_capabilities_deep as $group_capability ) {
$class = '';
if ( empty( $group_capabilities ) || !in_array( $group_capability, $group_capabilities ) ) {
$inherited_caps[] = wp_filter_nohtml_kses( $group_capability->capability->capability );
}
}
$output .= implode( ' ', $inherited_caps );
$output .= '</div>';
}
$output .= apply_filters( 'groups_admin_groups_edit_form_after_fields', '', $group_id );
$output .= '<div class="field">';
$output .= wp_nonce_field( 'groups-edit', GROUPS_ADMIN_GROUPS_NONCE, true, false );
$output .= '<input class="button button-primary" type="submit" value="' . __( 'Save', 'groups' ) . '"/>';
$output .= '<input type="hidden" value="edit" name="action"/>';
$output .= '<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', 'groups' ) . '</a>';
$output .= '</div>';
$output .= '</div>'; // .group.edit
$output .= '</form>';
$output .= '</div>'; // .manage-groups
echo $output;
} // function groups_admin_groups_edit
/**
* Handle edit form submission.
*/
function groups_admin_groups_edit_submit() {
global $wpdb;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-edit' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$group_id = isset( $_POST['group-id-field'] ) ? $_POST['group-id-field'] : null;
$group = Groups_Group::read( $group_id );
if ( $group ) {
$group_id = $group->group_id;
if ( $group->name !== Groups_Registered::REGISTERED_GROUP_NAME ) {
$name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : null;
} else {
$name = Groups_Registered::REGISTERED_GROUP_NAME;
}
$parent_id = isset( $_POST['parent-id-field'] ) ? $_POST['parent-id-field'] : null;
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : '';
if ( empty( $name ) ) {
Groups_Admin::add_message( __( 'The <em>Name</em> must not be empty.', 'groups' ), 'error' );
return false;
}
if ( $other_group = Groups_Group::read_by_name( $name ) ) {
if ( $other_group->group_id != $group_id ) {
Groups_Admin::add_message(
sprintf(
__( 'The <em>%s</em> group already exists and cannot be used to name this one.', 'groups' ), stripslashes( wp_filter_nohtml_kses( $other_group->name ) )
),
'error'
);
return false;
}
}
$group_id = Groups_Group::update( compact( "group_id", "name", "parent_id", "description" ) );
if ( $group_id ) {
$capability_table = _groups_get_tablename( "capability" );
$group_capability_table = _groups_get_tablename( "group_capability" );
$group_capabilities = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM $capability_table WHERE capability_id IN ( SELECT capability_id FROM $group_capability_table WHERE group_id = %d )",
Groups_Utility::id( $group_id )
) );
$group_capabilities_array = array();
foreach ( $group_capabilities as $group_capability ) {
$group_capabilities_array[] = $group_capability->capability_id;
}
$caps = array();
if ( isset( $_POST['capability_ids'] ) ) {
$caps = $_POST['capability_ids'];
}
// delete
foreach( $group_capabilities_array as $group_cap ) {
if ( !in_array( $group_cap, $caps ) ) {
Groups_Group_Capability::delete( $group_id, $group_cap );
}
}
// add
foreach( $caps as $cap ) {
if ( !in_array( $cap, $group_capabilities_array ) ) {
Groups_Group_Capability::create( array( 'group_id' => $group_id, 'capability_id' => $cap ) );
}
}
do_action( 'groups_admin_groups_edit_submit_success', $group_id );
}
return $group_id;
} else {
return false;
}
} // function groups_admin_groups_edit_submit

View File

@@ -0,0 +1,199 @@
<?php
/**
* groups-admin-groups-remove.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.1.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Shows form to confirm removal of a group.
* @param int $group_id group id
*/
function groups_admin_groups_remove( $group_id ) {
global $wpdb;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$group = Groups_Group::read( intval( $group_id ) );
if ( empty( $group ) ) {
wp_die( __( 'No such group.', 'groups' ) );
}
$group_table = _groups_get_tablename( 'group' );
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'group_id', $current_url );
$output =
'<div class="manage-groups wrap">' .
'<h1>' .
__( 'Remove a group', 'groups' ) .
'</h1>' .
'<form id="remove-group" action="' . esc_url( $current_url ) . '" method="post">' .
'<div class="group remove">' .
'<input id="group-id-field" name="group-id-field" type="hidden" value="' . esc_attr( intval( $group->group_id ) ) . '"/>' .
'<ul>' .
'<li>' . sprintf( __( 'Group Name : %s', 'groups' ), stripslashes( wp_filter_nohtml_kses( $group->name ) ) ) . '</li>' .
'</ul> ' .
wp_nonce_field( 'groups-remove', GROUPS_ADMIN_GROUPS_NONCE, true, false ) .
'<input class="button button-primary" type="submit" value="' . __( 'Remove', 'groups' ) . '"/>' .
'<input type="hidden" value="remove" name="action"/>' .
'<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', 'groups' ) . '</a>' .
'</div>' .
'</div>' . // .group.remove
'</form>' .
'</div>'; // .manage-groups
echo $output;
} // function groups_admin_groups_remove
/**
* Handle remove form submission.
*/
function groups_admin_groups_remove_submit() {
global $wpdb;
$result = false;
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-remove' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$group_id = isset( $_POST['group-id-field'] ) ? $_POST['group-id-field'] : null;
$group = Groups_Group::read( $group_id );
if ( $group ) {
if ( $group->name !== Groups_Registered::REGISTERED_GROUP_NAME ) {
$result = Groups_Group::delete( $group_id );
}
}
return $result;
} // function groups_admin_groups_remove_submit
/**
* Shows form to confirm bulk-removal of groups.
*/
function groups_admin_groups_bulk_remove() {
global $wpdb;
$output = '';
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$group_ids = isset( $_POST['group_ids'] ) ? $_POST['group_ids'] : null;
if ( ! $group_ids ) {
wp_die( __( 'No such groups.', 'groups' ) );
}
$groups = array();
foreach ( $group_ids as $group_id ) {
$group = Groups_Group::read( intval( $group_id ) );
if ( $group ) {
$groups[] = $group;
}
}
$group_table = _groups_get_tablename( 'group' );
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'group_id', $current_url );
$output .= '<div class="manage-groups wrap">';
$output .= '<h1>';
$output .= __( 'Remove groups', 'groups' );
$output .= '</h1>';
$output .= '<form id="groups-action" method="post" action="">';
$output .= '<div class="group remove">';
$output .= '<p>';
$output .= __( 'Please confirm removal of the following groups. This action cannot be undone.', 'groups' );
$output .= '</p>';
foreach ( $groups as $group ) {
$output .= '<input id="group_ids" name="group_ids[]" type="hidden" value="' . esc_attr( intval( $group->group_id ) ) . '"/>';
$output .= '<ul>';
$output .= '<li>';
$output .= sprintf( __( '<strong>%s</strong>', 'groups' ), wp_filter_nohtml_kses( $group->name ) );
$output .= '</li>';
$output .= '</ul>';
}
$output .= '<input class="button button-primary" type="submit" name="bulk" value="' . __( "Remove", 'groups' ) . '"/>';
$output .= '<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', 'groups' ) . '</a>';
$output .= '<input type="hidden" name="action" value="groups-action"/>';
$output .= '<input type="hidden" name="bulk-action" value="remove-group"/>';
$output .= '<input type="hidden" name="confirm" value="1"/>';
$output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_ACTION_NONCE, true, false );
$output .= '</div>';
$output .= '</form>';
$output .= '</div>';
echo $output;
} // function groups_admin_groups_bulk_remove
/**
* Handle remove form submission.
* @return array of deleted groups' ids
*/
function groups_admin_groups_bulk_remove_submit() {
global $wpdb;
$result = array();
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_ACTION_NONCE], 'admin' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$group_ids = isset( $_POST['group_ids'] ) ? $_POST['group_ids'] : null;
if ( $group_ids ) {
foreach ( $group_ids as $group_id ) {
$group = Groups_Group::read( $group_id );
if ( $group ) {
if ( $group->name !== Groups_Registered::REGISTERED_GROUP_NAME ) {
if ( Groups_Group::delete( $group_id ) ) {
$result[] = $group->group_id;
}
}
}
}
}
return $result;
} // function groups_admin_groups_bulk_remove_submit

View File

@@ -0,0 +1,545 @@
<?php
/**
* groups-admin-groups.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
// admin defines
define( 'GROUPS_GROUPS_PER_PAGE', 10 );
define( 'GROUPS_ADMIN_GROUPS_NONCE_1', 'groups-nonce-1');
define( 'GROUPS_ADMIN_GROUPS_NONCE_2', 'groups-nonce-2');
define( 'GROUPS_ADMIN_GROUPS_ACTION_NONCE', 'groups-action-nonce');
define( 'GROUPS_ADMIN_GROUPS_FILTER_NONCE', 'groups-filter-nonce' );
require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
require_once( GROUPS_ADMIN_LIB . '/groups-admin-groups-add.php');
require_once( GROUPS_ADMIN_LIB . '/groups-admin-groups-edit.php');
require_once( GROUPS_ADMIN_LIB . '/groups-admin-groups-remove.php');
/**
* Manage Groups: table of groups and add, edit, remove actions.
*/
function groups_admin_groups() {
global $wpdb;
$output = '';
$today = date( 'Y-m-d', time() );
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
//
// handle actions
//
if ( isset( $_POST['action'] ) ) {
// handle action submit - do it
switch( $_POST['action'] ) {
case 'add' :
if ( !( $group_id = groups_admin_groups_add_submit() ) ) {
return groups_admin_groups_add();
} else {
$group = Groups_Group::read( $group_id );
Groups_Admin::add_message( sprintf( __( "The <em>%s</em> group has been created.", 'groups' ), stripslashes( wp_filter_nohtml_kses( $group->name ) ) ) );
}
break;
case 'edit' :
if ( !( $group_id = groups_admin_groups_edit_submit() ) ) {
return groups_admin_groups_edit( $_POST['group-id-field'] );
} else {
$group = Groups_Group::read( $group_id );
Groups_Admin::add_message( sprintf( __( 'The <em>%s</em> group has been updated.', 'groups' ), stripslashes( wp_filter_nohtml_kses( $group->name ) ) ) );
}
break;
case 'remove' :
if ( $group_id = groups_admin_groups_remove_submit() ) {
Groups_Admin::add_message( __( 'The group has been deleted.', 'groups' ) );
}
break;
// bulk actions on groups: add capabilities, remove capabilities, remove groups
case 'groups-action' :
if ( wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_ACTION_NONCE], 'admin' ) ) {
$group_ids = isset( $_POST['group_ids'] ) ? $_POST['group_ids'] : null;
$bulk_action = null;
if ( isset( $_POST['bulk'] ) ) {
$bulk_action = $_POST['bulk-action'];
}
if ( is_array( $group_ids ) && ( $bulk_action !== null ) ) {
foreach ( $group_ids as $group_id ) {
switch ( $bulk_action ) {
case 'add-capability' :
$capabilities_id = isset( $_POST['capability_id'] ) ? $_POST['capability_id'] : null;
if ( $capabilities_id !== null ) {
foreach ( $capabilities_id as $capability_id ) {
Groups_Group_Capability::create( array( 'group_id' => $group_id, 'capability_id' => $capability_id ) );
}
}
break;
case 'remove-capability' :
$capabilities_id = isset( $_POST['capability_id'] ) ? $_POST['capability_id'] : null;
if ( $capabilities_id !== null ) {
foreach ( $capabilities_id as $capability_id ) {
Groups_Group_Capability::delete( $group_id, $capability_id );
}
}
break;
case 'remove-group' :
$bulk_confirm = isset( $_POST['confirm'] ) ? true : false;
if ( $bulk_confirm ) {
groups_admin_groups_bulk_remove_submit();
} else {
return groups_admin_groups_bulk_remove();
}
break;
}
}
}
}
break;
}
} else if ( isset ( $_GET['action'] ) ) {
// handle action request - show form
switch( $_GET['action'] ) {
case 'add' :
return groups_admin_groups_add();
break;
case 'edit' :
if ( isset( $_GET['group_id'] ) ) {
return groups_admin_groups_edit( $_GET['group_id'] );
}
break;
case 'remove' :
if ( isset( $_GET['group_id'] ) ) {
return groups_admin_groups_remove( $_GET['group_id'] );
}
break;
}
}
//
// group table
//
if (
isset( $_POST['clear_filters'] ) ||
isset( $_POST['group_id'] ) ||
isset( $_POST['group_name'] )
) {
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_FILTER_NONCE], 'admin' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
}
// filters
$group_id = Groups_Options::get_user_option( 'groups_group_id', null );
$group_name = Groups_Options::get_user_option( 'groups_group_name', null );
if ( isset( $_POST['clear_filters'] ) ) {
Groups_Options::delete_user_option( 'groups_group_id' );
Groups_Options::delete_user_option( 'groups_group_name' );
$group_id = null;
$group_name = null;
} else if ( isset( $_POST['submitted'] ) ) {
// filter by name
if ( !empty( $_POST['group_name'] ) ) {
$group_name = $_POST['group_name'];
Groups_Options::update_user_option( 'groups_group_name', $group_name );
}
// filter by group id
if ( !empty( $_POST['group_id'] ) ) {
$group_id = intval( $_POST['group_id'] );
Groups_Options::update_user_option( 'groups_group_id', $group_id );
} else if ( isset( $_POST['group_id'] ) ) { // empty && isset => '' => all
$group_id = null;
Groups_Options::delete_user_option( 'groups_group_id' );
}
}
if ( isset( $_POST['row_count'] ) ) {
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE_1], 'admin' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
}
if ( isset( $_POST['paged'] ) ) {
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE_2], 'admin' ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'paged', $current_url );
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'group_id', $current_url );
$group_table = _groups_get_tablename( 'group' );
$output .=
'<div class="manage-groups wrap">' .
'<h1>' .
_x( 'Groups', 'page-title', 'groups' ) .
sprintf(
'<a title="%s" class="add page-title-action" href="%s">',
esc_attr( __( 'Click to add a new group', 'groups' ) ),
esc_url( $current_url . '&action=add' )
) .
sprintf(
'<img class="icon" alt="%s" src="%s" />',
esc_attr( __( 'Add', 'groups' ) ),
esc_url( GROUPS_PLUGIN_URL . 'images/add.png' )
) .
sprintf(
'<span class="label">%s</span>',
stripslashes( wp_filter_nohtml_kses( __( 'New Group', 'groups' ) ) )
) .
'</a>' .
'</h1>';
$output .= Groups_Admin::render_messages();
$row_count = isset( $_POST['row_count'] ) ? intval( $_POST['row_count'] ) : 0;
if ($row_count <= 0) {
$row_count = Groups_Options::get_user_option( 'groups_per_page', GROUPS_GROUPS_PER_PAGE );
} else {
Groups_Options::update_user_option('groups_per_page', $row_count );
}
$offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
if ( $offset < 0 ) {
$offset = 0;
}
$paged = isset( $_REQUEST['paged'] ) ? intval( $_REQUEST['paged'] ) : 0;
if ( $paged < 0 ) {
$paged = 0;
}
$orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : null;
switch ( $orderby ) {
case 'group_id' :
case 'name' :
case 'description' :
break;
default:
$orderby = 'name';
}
$order = isset( $_GET['order'] ) ? $_GET['order'] : null;
switch ( $order ) {
case 'asc' :
case 'ASC' :
$switch_order = 'DESC';
break;
case 'desc' :
case 'DESC' :
$switch_order = 'ASC';
break;
default:
$order = 'ASC';
$switch_order = 'DESC';
}
$filters = array( " 1=%d " );
$filter_params = array( 1 );
if ( $group_id ) {
$filters[] = " $group_table.group_id = %d ";
$filter_params[] = $group_id;
}
if ( $group_name ) {
$filters[] = " $group_table.name LIKE '%%%s%%' ";
$filter_params[] = $group_name;
}
if ( !empty( $filters ) ) {
$filters = " WHERE " . implode( " AND ", $filters );
} else {
$filters = '';
}
$count_query = $wpdb->prepare( "SELECT COUNT(*) FROM $group_table $filters", $filter_params );
$count = $wpdb->get_var( $count_query );
if ( $count > $row_count ) {
$paginate = true;
} else {
$paginate = false;
}
$pages = ceil ( $count / $row_count );
if ( $paged > $pages ) {
$paged = $pages;
}
if ( $paged != 0 ) {
$offset = ( $paged - 1 ) * $row_count;
}
$query = $wpdb->prepare(
"SELECT * FROM $group_table
$filters
ORDER BY $orderby $order
LIMIT $row_count OFFSET $offset",
$filter_params
);
$results = $wpdb->get_results( $query, OBJECT );
$column_display_names = array(
'group_id' => __( 'ID', 'groups' ),
'name' => __( 'Group', 'groups' ),
'description' => __( 'Description', 'groups' ),
'capabilities' => __( 'Capabilities', 'groups' )
);
$output .= '<div class="groups-overview">';
$output .=
'<div class="filters">' .
'<form id="setfilters" action="" method="post">' .
'<fieldset>' .
'<legend>' . __( 'Filters', 'groups' ) . '</legend>' .
'<label class="group-id-filter">' . __( 'Group ID', 'groups' ) . ' ' .
'<input class="group-id-filter" name="group_id" type="text" value="' . esc_attr( $group_id ) . '"/>' .
'</label>' . ' ' .
'<label class="group-name-filter">' . __( 'Group Name', 'groups' ) . ' ' .
'<input class="group-name-filter" name="group_name" type="text" value="' . $group_name . '"/>' .
'</label>' . ' ' .
wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_FILTER_NONCE, true, false ) .
'<input class="button" type="submit" value="' . __( 'Apply', 'groups' ) . '"/>' . ' ' .
'<input class="button" type="submit" name="clear_filters" value="' . __( 'Clear', 'groups' ) . '"/>' .
'<input type="hidden" value="submitted" name="submitted"/>' .
'</fieldset>' .
'</form>' .
'</div>';
if ( $paginate ) {
require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
$pagination = new Groups_Pagination( $count, null, $row_count );
$output .= '<form id="posts-filter" method="post" action="">';
$output .= '<div>';
$output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_NONCE_2, true, false );
$output .= '</div>';
$output .= '<div class="tablenav top">';
$output .= $pagination->pagination( 'top' );
$output .= '</div>';
$output .= '</form>';
}
$output .= '<div class="page-options right">';
$output .= '<form id="setrowcount" action="" method="post">';
$output .= '<div>';
$output .= '<label for="row_count">' . __('Results per page', 'groups' ) . '</label>';
$output .= '<input name="row_count" type="text" size="2" value="' . esc_attr( $row_count ) .'" />';
$output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_NONCE_1, true, false );
$output .= '<input class="button" type="submit" value="' . __( 'Apply', 'groups' ) . '"/>';
$output .= '</div>';
$output .= '</form>';
$output .= '</div>';
$capability_table = _groups_get_tablename( "capability" );
$group_capability_table = _groups_get_tablename( "group_capability" );
// capabilities select
$capabilities = $wpdb->get_results( "SELECT * FROM $capability_table ORDER BY capability" );
$capabilities_select = sprintf(
'<select class="select capability" name="capability_id[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
esc_attr( __( 'Capabilities &hellip;', 'groups' ) ) ,
esc_attr( __( 'Capabilities &hellip;', 'groups' ) )
);
foreach( $capabilities as $capability ) {
$capabilities_select .= sprintf( '<option value="%s">%s</option>', esc_attr( $capability->capability_id ), wp_filter_nohtml_kses( $capability->capability ) );
}
$capabilities_select .= '</select>';
$capabilities_select .= Groups_UIE::render_select( '.select.capability' );
$output .= '<form id="groups-action" method="post" action="">';
$output .= '<div class="tablenav top">';
$output .= '<div class="groups-bulk-container">';
$output .= '<div class="capabilities-select-container">';
$output .= $capabilities_select;
$output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_ACTION_NONCE, true, false );
$output .= '</div>';
$output .= '<select class="bulk-action" name="bulk-action">';
$output .= '<option selected="selected" value="-1">' . esc_html( __( 'Bulk Actions', 'groups' ) ) . '</option>';
$output .= '<option value="remove-group">' . esc_html( __( 'Remove group', 'groups' ) ) . '</option>';
$output .= '<option value="add-capability">' . esc_html( __( 'Add capability', 'groups' ) ) . '</option>';
$output .= '<option value="remove-capability">' . esc_html( __( 'Remove capability', 'groups' ) ) . '</option>';
$output .= '</select>';
$output .= sprintf( '<input class="button" type="submit" name="bulk" value="%s" />', esc_attr( __( 'Apply', 'groups' ) ) );
$output .= '<input type="hidden" name="action" value="groups-action"/>';
$output .= '</div>';
$output .= '</div>';
$output .= '<table id="" class="wp-list-table widefat fixed" cellspacing="0">';
$output .= '<thead>';
$output .= '<tr>';
$output .= '<th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox"></th>';
foreach ( $column_display_names as $key => $column_display_name ) {
$options = array(
'orderby' => $key,
'order' => $switch_order
);
$class = $key;
if ( !in_array( $key, array( 'capabilities' ) ) ) {
if ( strcmp( $key, $orderby ) == 0 ) {
$lorder = strtolower( $order );
$class = "$key manage-column sorted $lorder";
} else {
$class = "$key manage-column sortable";
}
$column_display_name =
sprintf(
'<a href="%s"><span>%s</span><span class="sorting-indicator"></span></a>',
esc_url( add_query_arg( $options, $current_url ) ),
esc_html( $column_display_name )
);
} else {
$column_display_name = esc_html( $column_display_name );
}
$output .= sprintf(
'<th scope="col" class="%s">%s</th>',
esc_attr( $class ),
$column_display_name
);
}
$output .= '</tr>';
$output .= '</thead>';
$output .= '<tbody>';
if ( count( $results ) > 0 ) {
for ( $i = 0; $i < count( $results ); $i++ ) {
$result = $results[$i];
// Construct the "edit" URL.
$edit_url = add_query_arg(
array(
'group_id' => intval( $result->group_id ),
'action' => 'edit',
'paged' => $paged
),
$current_url
);
// Construct the "delete" URL.
$delete_url = add_query_arg(
array(
'group_id' => intval( $result->group_id ),
'action' => 'remove',
'paged' => $paged
),
$current_url
);
// Construct row actions for this group.
$row_actions =
'<div class="row-actions">' .
'<span class="edit">' .
'<a href="' . esc_url( $edit_url ) . '">' .
'<img src="' . GROUPS_PLUGIN_URL . 'images/edit.png"/>' .
__( 'Edit', 'groups' ) .
'</a>';
if ( $result->name !== Groups_Registered::REGISTERED_GROUP_NAME ) {
$row_actions .=
' | ' .
'</span>' .
'<span class="remove trash">' .
'<a href="' . esc_url( $delete_url ) . '" class="submitdelete">' .
'<img src="' . GROUPS_PLUGIN_URL . 'images/remove.png"/>' .
__( 'Remove', 'groups' ) .
'</a>' .
'</span>';
}
$row_actions .= '</div>'; // .row-actions
$output .= '<tr class="' . ( $i % 2 == 0 ? 'even' : 'odd' ) . '">';
$output .= '<th class="check-column">';
$output .= '<input type="checkbox" value="' . esc_attr( $result->group_id ) . '" name="group_ids[]"/>';
$output .= '</th>';
$output .= '<td class="group-id">';
$output .= $result->group_id;
$output .= '</td>';
$output .= '<td class="group-name">';
$output .= sprintf( '<a href="%s">%s</a>', esc_url( $edit_url ), stripslashes( wp_filter_nohtml_kses( $result->name ) ) );
$output .= $row_actions;
$output .= '</td>';
$output .= '<td class="group-description">';
$output .= stripslashes( wp_filter_nohtml_kses( $result->description ) );
$output .= '</td>';
$output .= '<td class="capabilities">';
$group = new Groups_Group( $result->group_id );
$group_capabilities = $group->capabilities;
$group_capabilities_deep = $group->capabilities_deep;
usort( $group_capabilities_deep, array( 'Groups_Utility', 'cmp' ) );
if ( count( $group_capabilities_deep ) > 0 ) {
$output .= '<ul>';
foreach ( $group_capabilities_deep as $group_capability ) {
$output .= '<li>';
$class = '';
if ( empty( $group_capabilities ) || !in_array( $group_capability, $group_capabilities ) ) {
$class = 'inherited';
}
$output .= sprintf( '<span class="%s">', $class );
if ( isset( $group_capability->capability ) && isset( $group_capability->capability->capability ) ) {
$output .= wp_filter_nohtml_kses( $group_capability->capability->capability );
}
$output .= '</span>';
$output .= '</li>';
}
$output .= '</ul>';
} else {
$output .= __( 'This group has no capabilities.', 'groups' );
}
$output .= '</td>';
$output .= '</tr>';
}
} else {
$output .= '<tr><td colspan="4">' . __( 'There are no results.', 'groups' ) . '</td></tr>';
}
$output .= '</tbody>';
$output .= '</table>';
$output .= Groups_UIE::render_add_titles( '.groups-overview table td' );
$output .= '</form>'; // #groups-action
if ( $paginate ) {
require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
$pagination = new Groups_Pagination($count, null, $row_count);
$output .= '<div class="tablenav bottom">';
$output .= $pagination->pagination( 'bottom' );
$output .= '</div>';
}
$output .= '</div>'; // .groups-overview
$output .= '</div>'; // .manage-groups
echo $output;
} // function groups_admin_groups()

View File

@@ -0,0 +1,403 @@
<?php
/**
* groups-admin-options.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* @var string options form nonce name
*/
define( 'GROUPS_ADMIN_OPTIONS_NONCE', 'groups-admin-nonce' );
/**
* Options admin screen.
*/
function groups_admin_options() {
global $wpdb, $wp_roles;
if ( !current_user_can( GROUPS_ADMINISTER_OPTIONS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$is_sitewide_plugin = false;
if ( is_multisite() ) {
$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins', array() );
$active_sitewide_plugins = array_keys( $active_sitewide_plugins );
$is_sitewide_plugin = in_array( 'groups/groups.php', $active_sitewide_plugins );
}
$caps = array(
GROUPS_ACCESS_GROUPS => __( 'Access Groups', 'groups' ),
GROUPS_ADMINISTER_GROUPS => __( 'Administer Groups', 'groups' ),
GROUPS_ADMINISTER_OPTIONS => __( 'Administer Groups plugin options', 'groups' ),
GROUPS_RESTRICT_ACCESS => __( 'Restrict Access', 'groups' )
);
$previous_legacy_enable = Groups_Options::get_option( GROUPS_LEGACY_ENABLE, GROUPS_LEGACY_ENABLE_DEFAULT );
//
// handle options form submission
//
if ( isset( $_POST['submit'] ) ) {
if ( wp_verify_nonce( $_POST[GROUPS_ADMIN_OPTIONS_NONCE], 'admin' ) ) {
$post_types = get_post_types();
$selected_post_types = !empty( $_POST['add_meta_boxes'] ) && is_array( $_POST['add_meta_boxes'] ) ? $_POST['add_meta_boxes'] : array();
foreach( $post_types as $post_type ) {
$handle_post_types[$post_type] = in_array( $post_type, $selected_post_types );
}
Groups_Post_Access::set_handles_post_types( $handle_post_types );
// tree view
if ( !empty( $_POST[GROUPS_SHOW_TREE_VIEW] ) ) {
Groups_Options::update_option( GROUPS_SHOW_TREE_VIEW, true );
} else {
Groups_Options::update_option( GROUPS_SHOW_TREE_VIEW, false );
}
// show in user profiles
Groups_Options::update_option( GROUPS_SHOW_IN_USER_PROFILE, !empty( $_POST[GROUPS_SHOW_IN_USER_PROFILE] ) );
// roles & capabilities
$rolenames = $wp_roles->get_names();
foreach ( $rolenames as $rolekey => $rolename ) {
$role = $wp_roles->get_role( $rolekey );
foreach ( $caps as $capkey => $capname ) {
$role_cap_id = $rolekey.'-'.$capkey;
if ( !empty($_POST[$role_cap_id] ) ) {
$role->add_cap( $capkey );
} else {
$role->remove_cap( $capkey );
}
}
}
Groups_Controller::assure_capabilities();
if ( !$is_sitewide_plugin ) {
// delete data
if ( !empty( $_POST['delete-data'] ) ) {
Groups_Options::update_option( 'groups_delete_data', true );
} else {
Groups_Options::update_option( 'groups_delete_data', false );
}
}
// legacy enable ?
if ( !empty( $_POST[GROUPS_LEGACY_ENABLE] ) ) {
Groups_Options::update_option( GROUPS_LEGACY_ENABLE, true );
} else {
Groups_Options::update_option( GROUPS_LEGACY_ENABLE, false );
}
Groups_Admin::add_message( __( 'Options saved.', 'groups' ) );
}
}
echo '<div class="groups-options wrap">';
echo
'<h1>' .
__( 'Groups Options', 'groups' ) .
'</h1>';
echo Groups_Admin::render_messages();
$show_tree_view = Groups_Options::get_option( GROUPS_SHOW_TREE_VIEW, GROUPS_SHOW_TREE_VIEW_DEFAULT );
$show_in_user_profile = Groups_Options::get_option( GROUPS_SHOW_IN_USER_PROFILE, GROUPS_SHOW_IN_USER_PROFILE_DEFAULT );
$rolenames = $wp_roles->get_names();
$caps_table = '<table class="groups-permissions">';
$caps_table .= '<thead>';
$caps_table .= '<tr>';
$caps_table .= '<td class="role">';
$caps_table .= __( 'Role', 'groups' );
$caps_table .= '</td>';
foreach ( $caps as $cap ) {
$caps_table .= '<td class="cap">';
$caps_table .= $cap;
$caps_table .= '</td>';
}
$caps_table .= '</tr>';
$caps_table .= '</thead>';
$caps_table .= '<tbody>';
foreach ( $rolenames as $rolekey => $rolename ) {
$role = $wp_roles->get_role( $rolekey );
$caps_table .= '<tr>';
$caps_table .= '<td>';
$caps_table .= translate_user_role( $rolename );
$caps_table .= '</td>';
foreach ( $caps as $capkey => $capname ) {
if ( $role->has_cap( $capkey ) ) {
$checked = ' checked="checked" ';
} else {
$checked = '';
}
$caps_table .= '<td class="checkbox">';
$role_cap_id = $rolekey.'-'.$capkey;
$caps_table .= '<input type="checkbox" name="' . $role_cap_id . '" id="' . $role_cap_id . '" ' . $checked . '/>';
$caps_table .= '</td>';
}
$caps_table .= '</tr>';
}
$caps_table .= '</tbody>';
$caps_table .= '</table>';
$delete_data = Groups_Options::get_option( 'groups_delete_data', false );
if ( isset( $_GET['dismiss-groups-extensions-box'] ) && isset( $_GET['groups-extensions-box-nonce'] ) && wp_verify_nonce( $_GET['groups-extensions-box-nonce'], 'dismiss-box' ) ) {
Groups_Options::update_user_option( 'show-extensions-box', false );
}
$extensions_box = '';
if ( Groups_Options::get_user_option( 'show-extensions-box', true ) ) {
$dismiss_url = wp_nonce_url( add_query_arg( 'dismiss-groups-extensions-box', '1', admin_url( 'admin.php?page=groups-admin-options' ) ), 'dismiss-box', 'groups-extensions-box-nonce' );
$extensions_box =
'<div id="groups-extensions-box">' .
__( 'Enhanced functionality is available via official <a href="http://www.itthinx.com/shop/">Extensions</a> for Groups.', 'groups' ) .
sprintf( '<a class="close" href="%s">x</a>', esc_url( $dismiss_url ) ) .
'</div>';
}
//
// print the options form
//
echo
'<form action="" name="options" method="post">' .
'<div>' .
'<p>' .
'<input class="button button-primary" type="submit" name="submit" value="' . __( 'Save', 'groups' ) . '"/>' .
$extensions_box .
'</p>';
if ( _groups_admin_override() ) {
echo
'<h2 style="color:red">' .
__( 'Administrator Access Override', 'groups' ) .
'</h2>' .
'<p>' .
__( 'Administrators override all access permissions derived from Groups capabilities.', 'groups' ) .
'</p>' .
'<p>' .
__( 'To disable, do not define the constant <code>GROUPS_ADMINISTRATOR_OVERRIDE</code> or set it to <code>false</code>.', 'groups' ) .
'</p>' .
'<p>' .
__( 'Enabling this on production sites is <strong>not</strong> recommended.', 'groups' ) .
'</p>';
}
echo '<h2>';
echo __( 'Access restricions', 'groups' );
echo '</h2>';
echo '<h3>';
echo __( 'Post types', 'groups' );
echo '</h3>';
echo '<p class="description">';
echo __( 'Show access restrictions for these post types.', 'groups' ); // @todo change wording to '...handles access...' ?
echo '</p>';
$post_type_objects = get_post_types( array(), 'objects' );
uasort( $post_type_objects, 'groups_admin_options_compare_post_types' );
echo '<ul>';
foreach( $post_type_objects as $post_type => $post_type_object ) {
echo '<li>';
echo '<label>';
$label = $post_type;
$labels = isset( $post_type_object->labels ) ? $post_type_object->labels : null;
if ( ( $labels !== null ) && isset( $labels->singular_name ) ) {
$label = __( $labels->singular_name );
}
$checked = Groups_Post_Access::handles_post_type( $post_type ) ? ' checked="checked" ' : '';
echo '<input name="add_meta_boxes[]" type="checkbox" value="' . esc_attr( $post_type ) . '" ' . $checked . '/>';
$is_public = isset( $post_type_object->public ) && $post_type_object->public;
echo $is_public ? '<strong>' : '';
echo esc_html( $label );
echo $is_public ? '</strong>' : '';
if ( $post_type != $label ) {
echo ' ';
echo '<code><small>';
echo esc_html( $post_type );
echo '</small></code>';
}
echo '</label>';
echo '</li>';
}
echo '<ul>';
echo
'<p class="description">' .
__( 'This determines for which post types access restriction settings are offered.', 'groups' ) . '<br/>' .
__( 'Disabling this setting for a post type also disables existing access restrictions on individual posts of that type.', 'groups' ) . '<br/>' .
'</p>';
echo
'<h2>' . __( 'User profiles', 'groups' ) . '</h2>' .
'<p>' .
'<label>' .
'<input name="' . GROUPS_SHOW_IN_USER_PROFILE . '" type="checkbox" ' . ( $show_in_user_profile ? 'checked="checked"' : '' ) . '/>' .
__( 'Show groups in user profiles.', 'groups' ) .
'</label>' .
'</p>';
echo
'<h2>' . __( 'Tree view', 'groups' ) . '</h2>' .
'<p>' .
'<label>' .
'<input name="' . GROUPS_SHOW_TREE_VIEW . '" type="checkbox" ' . ( $show_tree_view ? 'checked="checked"' : '' ) . '/>' .
__( 'Show the Groups tree view.', 'groups' ) .
'</label>' .
'</p>';
echo
'<h2>' . __( 'Permissions', 'groups' ) . '</h2>' .
'<p>' . __( 'These permissions apply to Groups management. They do not apply to access permissions derived from Groups capabilities.', 'groups' ) . '</p>' .
$caps_table .
'<p class="description">' .
__( 'A minimum set of permissions will be preserved.', 'groups' ) .
'<br/>' .
__( 'If you lock yourself out, please ask an administrator to help.', 'groups' ) .
'</p>';
if ( !$is_sitewide_plugin ) {
echo
'<h2>' . __( 'Deactivation and data persistence', 'groups' ) . '</h2>' .
'<p>' .
'<label>' .
'<input name="delete-data" type="checkbox" ' . ( $delete_data ? 'checked="checked"' : '' ) . '/>' .
__( 'Delete all Groups plugin data on deactivation', 'groups' ) .
'</label>' .
'</p>' .
'<p class="description warning">' .
__( 'CAUTION: If this option is active while the plugin is deactivated, ALL plugin settings and data will be DELETED. If you are going to use this option, now would be a good time to make a backup. By enabling this option you agree to be solely responsible for any loss of data or any other consequences thereof.', 'groups' ) .
'</p>';
}
$groups_legacy_enable = Groups_Options::get_option( GROUPS_LEGACY_ENABLE, GROUPS_LEGACY_ENABLE_DEFAULT );
echo '<h2>' . __( 'Legacy Settings', 'groups' ) . '</h2>';
echo '<p>' .
'<label>' .
'<input name="' . GROUPS_LEGACY_ENABLE . '" type="checkbox" ' . ( $groups_legacy_enable ? 'checked="checked"' : '' ) . '/>' .
__( 'Enable legacy access control based on capabilities.', 'groups' ) .
'</label>' .
'</p>';
if ( $groups_legacy_enable ) {
require_once GROUPS_LEGACY_LIB . '/admin/groups-admin-options-legacy.php';
do_action( 'groups_admin_options_legacy', $groups_legacy_enable !== $previous_legacy_enable );
}
echo
'<p>' .
wp_nonce_field( 'admin', GROUPS_ADMIN_OPTIONS_NONCE, true, false ) .
'<input class="button button-primary" type="submit" name="submit" value="' . __( 'Save', 'groups' ) . '"/>' .
'</p>' .
'</div>' .
'</form>';
echo '</div>'; // .groups-options
}
/**
* Network administration options.
*/
function groups_network_admin_options() {
if ( !current_user_can( GROUPS_ADMINISTER_OPTIONS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
echo
'<div>' .
'<h1>' .
__( 'Groups network options', 'groups' ) .
'</h1>' .
'</div>';
// handle options form submission
if ( isset( $_POST['submit'] ) ) {
if ( wp_verify_nonce( $_POST[GROUPS_ADMIN_OPTIONS_NONCE], 'admin' ) ) {
// delete data
if ( !empty( $_POST['delete-data'] ) ) {
Groups_Options::update_option( 'groups_network_delete_data', true );
} else {
Groups_Options::update_option( 'groups_network_delete_data', false );
}
}
}
$delete_data = Groups_Options::get_option( 'groups_network_delete_data', false );
// options form
echo
'<form action="" name="options" method="post">' .
'<div>' .
'<h2>' . __( 'Network deactivation and data persistence', 'groups' ) . '</h2>' .
'<p>' .
'<label>' .
'<input name="delete-data" type="checkbox" ' . ( $delete_data ? 'checked="checked"' : '' ) . '/>' .
' ' .
__( 'Delete all Groups plugin data for ALL sites on network deactivation', 'groups' ) .
'</label>' .
'</p>' .
'<p class="description warning">' .
__( 'CAUTION: If this option is active while the plugin is deactivated, ALL plugin settings and data will be DELETED for <strong>all sites</strong>. If you are going to use this option, now would be a good time to make a backup. By enabling this option you agree to be solely responsible for any loss of data or any other consequences thereof.', 'groups' ) .
'</p>' .
'<p>' .
wp_nonce_field( 'admin', GROUPS_ADMIN_OPTIONS_NONCE, true, false ) .
'<input class="button button-primary" type="submit" name="submit" value="' . __( 'Save', 'groups' ) . '"/>' .
'</p>' .
'</div>' .
'</form>';
}
/**
* Compare two post types, considering those that have $public and/or $show_ui true as coming first.
* @param object $o1
* @param object $o2
* @return int
*/
function groups_admin_options_compare_post_types( $o1, $o2 ) {
$name_1 = isset( $o1->name ) ? $o1->name : '';
$name_2 = isset( $o2->name ) ? $o2->name : '';
$public_1 = isset( $o1->public ) && $o1->public;
$public_2 = isset( $o2->public ) && $o2->public;
$show_ui_1 = isset( $o1->show_ui ) && $o1->show_ui;
$show_ui_2 = isset( $o2->show_ui ) && $o2->show_ui;
$n1 = 0;
$n2 = 0;
if ( $public_1 ) {
$n1--;
}
if ( $show_ui_1 ) {
$n1--;
}
if ( $public_2 ) {
$n2--;
}
if ( $show_ui_2 ) {
$n2--;
}
return ( $n1 - $n2 ) * 10 + strcmp( $name_1, $name_2 );
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* groups-admin-tree-view.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code 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.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups
* @since groups 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Tree view : a simple tree view
*/
function groups_admin_tree_view() {
global $wpdb;
$output = '';
$today = date( 'Y-m-d', time() );
if ( !current_user_can( GROUPS_ACCESS_GROUPS ) ) {
wp_die( __( 'Access denied.', 'groups' ) );
}
$output .=
'<div class="groups-tree-view">' .
'<h1>' .
__( 'Tree of Groups', 'groups' ) .
'</h1>';
$tree = Groups_Utility::get_group_tree();
$tree_output = '';
Groups_Utility::render_group_tree( $tree, $tree_output );
$output .= $tree_output;
$output .= '</div>'; // .groups-tree-view
echo $output;
} // function groups_admin_tree_view()