Permalink
Browse files

Initial commit

  • Loading branch information...
robertstaddon committed Apr 5, 2017
0 parents commit f8a6e06660585dce50ac57c25bf5d743b45af995
Showing with 270 additions and 0 deletions.
  1. +157 −0 groups-login-redirect-settings.php
  2. +113 −0 groups-login-redirect.php
@@ -0,0 +1,157 @@
<?php
class Groups_Login_Redirect_Settings {
// groups-login-redirect class
private $groups_login_redirect;
// options page slug
private $options_page_name = 'groups-login-redirect';
// options page settings group name
private $settings_field_group = 'groups-login-redirect-settings';
/**
*
*/
function __construct() {
}
/**
* @return \Groups_Login_Redirect_Settings
*/
static public function init( $groups_login_redirect ){
$settings_page = new self();
$settings_page->groups_login_redirect = $groups_login_redirect;
// add our options page the the admin menu
add_action( 'admin_menu', array( $settings_page, 'admin_menu' ), 11 );
// add settings link in plugins menu
add_filter( 'plugin_action_links_groups_login_redirect', array( $settings_page, 'admin_settings_link' ) );
// register our settings
add_action( 'admin_init', array( $settings_page, 'admin_init' ) );
return $settings_page;
}
/**
* Add the Settings > Groups 404 section.
*/
public function admin_menu() {
add_submenu_page(
'groups-admin',
__( 'Groups Login Redirect', GROUPS_PLUGIN_DOMAIN ),
__( 'Groups Login', GROUPS_PLUGIN_DOMAIN ),
GROUPS_ADMINISTER_OPTIONS,
'groups-login-redirect',
array( $this, 'settings_page' )
);
}
/*
* Adds plugin links.
*
* @param array $links
* @param array $links with additional links
*/
public function admin_settings_link( $links ) {
$links[] = sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'admin.php?page=' . $this->options_page_name ) ),
esc_html( __( 'Settings', GROUPS_LOGIN_REDIRECT_PLUGIN_DOMAIN ) )
);
return $links;
}
/**
* Implements hook admin_init to register our settings
*/
public function admin_init() {
add_settings_section(
'glr_page_redirects_section',
__( 'Redirect Groups to Pages', 'groups-login-redirect' ),
array( $this, 'page_redirects_section_description' ),
$this->options_page_name
);
$groups = $this->groups_login_redirect->get_all_groups();
foreach( $groups as $group ) {
$setting_id = 'glr_page_redirect_' . $group->group_id;
add_settings_field(
$setting_id,
__( $group->name, 'groups-login-redirect' ),
array( $this, 'glr_page_redirect_select_field_render' ),
$this->options_page_name,
'glr_page_redirects_section',
array( 'group' => $group, 'setting_id' => $setting_id )
);
register_setting( $this->settings_field_group, 'glr_page_redirect_' . $group->group_id );
}
}
/**
*
*/
public function page_redirects_section_description() {
echo
'<p>Select a page to which a user should be redirected after logging in, based on the group to which they are a member.</p>
<p>If a user is part of multiple groups, they will be <strong>redirected to the page with the first priority number</strong>
(e.g. a page with a "1" priority will take precedence over a page with a priority of "3").</p>';
}
public function glr_page_redirect_select_field_render( $args ) {
$group = $args['group'];
$setting_id = $args['setting_id'];
$existing_values = get_option( $setting_id );
// Page setting
$dropdown_args = array(
'selected' => $existing_values['page'],
'id' => $setting_id . '_page',
'name' => $setting_id . '[page]',
'show_option_none' => ' - Select a Page - ',
);
wp_dropdown_pages( $dropdown_args );
// Priority setting
echo '<label for="' . $setting_id . '_priority">Priority:&nbsp;</label>';
echo '<input name="' . $setting_id . '[priority]" id="' . $setting_id . '_priority" type="text" value="' . $existing_values['priority'] . '">';
}
/**
* Output the options/settings page
*/
public function settings_page() {
?>
<div class="wrap">
<h2><?php print esc_html( get_admin_page_title() ); ?></h2>
<form method="post" action="options.php">
<?php
settings_fields( $this->settings_field_group );
do_settings_sections( $this->options_page_name );
submit_button();
// simple debug to view settings array
/*
echo '<pre>';
$groups = $this->groups_login_redirect->get_all_groups();
foreach( $groups as $group ) {
$setting_id = 'glr_page_redirect_' . $group->group_id;
echo $setting_id . ': ' . var_export( get_option( $setting_id ), true );
}
echo '</pre>';
*/
?>
</form>
</div>
<?php
}
}
@@ -0,0 +1,113 @@
<?php
/*
Plugin Name: Groups Login Redirect
Description: Redirect users after login based on their group
Version: 1.0
Author: Robert Staddon
Author URI: http://abundantdesigns.com
License: GPLv2 or later
Text Domain: groups-login-redirect
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
define( 'GROUPS_LOGIN_REDIRECT_PLUGIN_DOMAIN', 'groups-404-redirect' );
class Groups_Login_Redirect {
private $settings_page;
function __construct() {
}
public static function init() {
$groups_login_redirect = new self();
require_once 'groups-login-redirect-settings.php';
// Require Groups
add_action( 'admin_init', array( $groups_login_redirect, 'child_plugin_has_parent_plugin' ) );
// Add Redirect
add_filter( 'login_redirect', array( $groups_login_redirect, 'groups_login_redirect' ), 10, 3 );
// Add Filters
if ( is_admin() ) {
$groups_login_redirect->settings_page = Groups_Login_Redirect_Settings::init( $groups_login_redirect );
}
return $groups_login_redirect;
}
/**
* Require Groups
* http://wordpress.stackexchange.com/questions/127818/how-to-make-a-plugin-require-another-plugin
*/
public function child_plugin_has_parent_plugin() {
if ( is_admin() && current_user_can( 'activate_plugins' ) ) {
if( !is_plugin_active( 'groups/groups.php' ) ) {
add_action( 'admin_notices', array( $this, 'child_plugin_notice' ) );
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
}
public function child_plugin_notice() {
echo '<div class="error"><p>';
echo __('Sorry, but <strong>Groups</strong> is required for the Groups Login Redirect extension plugin to be installed and activated.', GROUPS_LOGIN_REDIRECT_PLUGIN_DOMAIN);
echo '</p></div>';
}
/*
* Redirect to appropriate page based on the user's group
*/
public function groups_login_redirect( $redirect_to, $request, $user ) {
if ( ! is_wp_error($user) ) {
// Get user's groups
$groups_user = new Groups_User( $user->ID );
$user_group_ids = $groups_user->group_ids_deep;
// Get redirect options for user's groups
$page_redirects = array();
foreach( $user_group_ids as $group_id ) {
$option = get_option( 'glr_page_redirect_' . $group_id );
if( !empty( $option['page'] ) )
$page_redirects[] = $option;
}
if ( !empty( $page_redirects ) ) {
// Sort array by priority
usort( $page_redirects, function ( $a, $b ) {
return $a['priority'] - $b['priority'];
});
// Redirect to the first page specified in the array
$page_redirect = current( $page_redirects );
return get_permalink( $page_redirect['page'] );
}
}
return $redirect_to;
}
/**
* Get all groups
*/
function get_all_groups() {
global $wpdb;
$groups_table = _groups_get_tablename( 'group' );
return $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" );
}
}
Groups_Login_Redirect::init();

0 comments on commit f8a6e06

Please sign in to comment.