UserDetails.php 1.68 KB
<?php
namespace Tz\WordPress\Tools\UserDetails;

use Tz, Tz\Common, Tz\WordPress\Tools;

const SETTING_NS = 'tz-user-details';
const CAPABILITY = 'edit_user_details';
const HTML_NS    = 'tzud-';

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

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

        // This for later if done via front end instead of backend
        //Vars::$meta_fields = new Tools\WP_Option(SETTING_NS);
    });

function register_field($id, $display = null) { //, Common\Callback $fn = null) {
    if (is_null($display)) {
        $display = ucwords(str_replace(Array('-', '_'), ' ', $id));
    }

    Vars::$fields[$id] = Array(
        'id'       => $id,
        'label'    => $display
    );
}

function get_user_id() {
    if (isset($_GET['user_id'])) {
        return $_GET['user_id'];
    }

    if (isset($_POST['user_id'])) {
        return $_POST['user_id'];
    }

    global $current_user;
    get_currentuserinfo();
    return $current_user->ID;
}

class Actions {
    public static function edit_user_profile() {
        require(__DIR__ . DIRECTORY_SEPARATOR . 'view-user_profile.php');
    }

    public static function show_user_profile() {
        static::edit_user_profile();
    }

    public static function profile_update() {
        $uid = get_user_id();

        foreach ($_POST as $key => $val) {
            if (substr($key, 0, strlen(HTML_NS)) == HTML_NS) {
                update_user_meta($uid, substr($key, strlen(HTML_NS)), $val);
            }
        }
    }
}

class Vars {
    /**
     * @private
     * @deprecated
     */
    public static $meta_fields;

    public static $fields = Array();
}