Settings.php 3.01 KB
<?php
namespace Tz\WordPress\Tools\Auth\Settings;

use Tz\Common;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\Auth;

const OPTION_GROUP   = 'auth_group';      // Grouping of options used for setting output by WP
const OPTION_SECTION = 'tz_auth_main';
const ADMIN_PAGE     = 'tz-tools-auth';   // URI of options page
const CAPABILITY     = 'manage_auth';     // User & Roles capability name

    call_user_func(function() {
        $role = get_role('administrator');
        $role->add_cap(CAPABILITY);

        Tools\add_actions(__NAMESPACE__ . '\Actions');
    });

/**
 * Loads the view for the settings page
 */
function displayPage() {
    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'settings_view.php');
}

/**
 * @todo Make this work
 * @param {Array} $data Data provided by WP to sanatize
 * @returns Array
 */
function validate($data) {
    return $data;

    // Validation library causing some issues with Array
    $valid = new Validation((array)$data);
    return $valid->valid;
}

/**
 * @uses Tools\add_actions
 */
class Actions {
    /**
     * Creates a settings page for the WP admin
     */
    public static function admin_menu() {
        add_options_page('Authentication', 'Authentication', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\displayPage');
    }

    /**
     * Generates variables for the module's options
     */
    public static function admin_init() {
        register_setting(OPTION_GROUP, Auth\OPTION_NAME, __NAMESPACE__ . '\validate');
        add_settings_section(OPTION_SECTION, 'Authentication Pages', function(){}, ADMIN_PAGE);

        Tools\add_settings_fields(__NAMESPACE__ . '\Fields', ADMIN_PAGE, OPTION_SECTION);
    }
}

/**
 * Each function is a variable stored in the WP_Option
 * @uses Tools\add_settings_fields
 */
class Fields {
    public static function login_page() {
        _dropdown_pages(Array('name' => Auth\OPTION_NAME . '[' . __FUNCTION__ . ']', 'sort_column' => 'menu_order', 'echo' => 1, 'selected' => Auth\Vars::$options[__FUNCTION__]));
    }

    public static function account_page() {
        _dropdown_pages(Array('name' => Auth\OPTION_NAME . '[' . __FUNCTION__ . ']', 'sort_column' => 'menu_order', 'echo' => 1, 'selected' => Auth\Vars::$options[__FUNCTION__]));        
    }

    public static function Facebook() {
        echo '<input type="checkbox" id="' . __FUNCTION__ . '" name="' . Auth\OPTION_NAME . '[third_party][' . __FUNCTION__ . ']" value="1" ' . checked('1', Auth\Vars::$options['third_party'][__FUNCTION__], false) . ' />';
    }

    public static function Twitter() {
        echo '<input type="checkbox" id="' . __FUNCTION__ . '" name="' . Auth\OPTION_NAME . '[third_party][' . __FUNCTION__ . ']" value="1" ' . checked('1', Auth\Vars::$options['third_party'][__FUNCTION__], false) . ' />';
    }
}

class Validation extends Common\Validation {
    public static function login_page($val) {
        // if !is page throw exception
    }

    public static function account_page($val) {
        // if !is page throw exception
        // if is same as login_page throw exception
    }
}
?>