63da00b4 by Chris Boden

Updated Auth library, added Analytics library

1 parent 2be7e464
<?php
namespace Tz\WordPress\Tools\Analytics;
use Tz\WordPress\Tools;
const OPTION_NAME = 'tz_analytics';
call_user_func(function() {
Vars::$options = new Tools\WP_Option(OPTION_NAME);
Tools\add_actions(__NAMESPACE__ . '\Actions');
if (is_admin()) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
}
});
class Actions {
public static function wp_print_scripts() {
if (Tz\LIVE !== 1 || empty(Vars::$options['api_key'])) {
return;
}
?>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<?php echo Vars::$options['api_key']; ?>']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<?php
}
}
class Vars {
public static $options;
}
?>
\ No newline at end of file
<?php
namespace Tz\WordPress\Tools\Analytics\Settings;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\Analytics;
const OPTION_GROUP = 'tz_analytics_group';
const OPTION_SECTION = 'tz_analytics_main';
const ADMIN_PAGE = 'tz-tool-analytics';
const CAPABILITY = 'configure_analytics';
call_user_func(function() {
$role = get_role('administrator');
$role->add_cap(CAPABILITY);
Tools\add_actions(__NAMESPACE__ . '\Actions');
});
function displayPage() {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'settings_view.php');
}
function validate($data) {
return $data;
}
class Actions {
public static function admin_menu() {
add_options_page('Analytics', 'Analytics', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\displayPage');
}
public static function admin_init() {
register_setting(OPTION_GROUP, Analytics\OPTION_NAME, __NAMESPACE__ . '\validate');
add_settings_section(OPTION_SECTION, '', function() {}, ADMIN_PAGE);
Tools\add_settings_fields(__NAMESPACE__ . '\Fields', ADMIN_PAGE, OPTION_SECTION);
}
}
class Fields {
public static function api_key() {
echo '<input tpe="text" name="' . Analytics\OPTION_NAME . '[' . __FUNCTION__ . ']" id="' . __FUNCTION__ . '" value="' . Analytics\Vars::$options[__FUNCTION__] . '" />';
}
}
?>
\ No newline at end of file
<?php
namespace Tz\WordPress\Tools\Analytics\Settings;
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Analytics Settings</h2>
<form method="post" action="options.php">
<?php
settings_fields(OPTION_GROUP);
do_settings_sections(ADMIN_PAGE);
?>
<p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p>
</form>
</div>
\ No newline at end of file
......@@ -12,8 +12,7 @@ const REG_METH_VALID_EMAIL = 2;
const FORGOT_METH_VALID_EMAIL = 1;
const FORGOT_METH_RAND_PASS = 2;
const ACTION_LOGIN = 'auth_login'; // probably don't need
const ACTION_LOGOUT = 'auth_logout'; // probably need, tell FB/etc to remove their cookies
// The things with these is they're dynamic but static functions aren't...
const ACTION_ACTIVATE = 'auth_activate';
const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`)
......@@ -55,6 +54,8 @@ function login($username, $password, $remember = true) {
));
if (get_class($auth) == 'WP_User') {
_set_current_user($auth->ID); // Not sure why I had to do this, oh well
return $auth;
}
......@@ -77,7 +78,19 @@ function logout() {
return true;
}
function register($user_data = Array(), $registration_method) {
/**
* @param {Array} $user_data User data array, requires minimum (username, password, email)
* @param {Integer} $registration_method Method of registeration, see constants beginning with REG_METH
* @throws {InvalidArgumentException} If an invalid $registration_method is passed
* @throw {BadMethodCallException} If any of the $user_data parameters are invalid
* @returns {Integer} New user $id if successful
* @uses wp-includes/registration.php
*/
function register($user_data = Array(), $registration_method = 1) {
if (!in_array($registration_method, Array(REG_METH_AUTO_REG, REG_METH_VALID_EMAIL))) {
throw new InvalidArgumentException("Invalid registration method selected");
}
require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
$valid = new Validation($user_data);
......@@ -86,26 +99,39 @@ function register($user_data = Array(), $registration_method) {
}
array_filter($user_data, 'esc_sql');
// $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
// possibly call wpmu_signup_user() if REG_METH_VALID_EMAIL; _insert_user if REG_METH_AUTO_REG
// Can't do that without making a database call; the unique registration key is created and destroyed in the function
// I'll have to make a database call to retreive it, at the very lest
// I can't do that at all; the function sends an email to the user with a auto-generated password
// I'll have to do database manipulation manually
$id = (int)_insert_user($user_data);
// should I call ACTION_ACTIVATE if REG_METHOD_AUTO_REG?
// this is so wrong
global $wpdb;
$wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}");
return $id;
}
// Don't think I need $username
function activate($username, $activation_key) {
// wpmu_activate_signup
// I can't do that either; that function sends a WordPress email
do_action(ACTION_ACTIVATE, $user_id);
}
// Not sure I need this function
// Application can just set rand password
// Or perhapds I do need it, move it to registered again or something???
function forgot_password($username, $forgot_method) {
}
class Vars {
public static $options;
}
class Validation extends Common\Validation {
/**
* @rule Not blank
......@@ -151,4 +177,12 @@ class Validation extends Common\Validation {
}
}
}
class Vars {
/**
* WordPress option for this module
* @type WP_Option
*/
public static $options;
}
?>
\ No newline at end of file
......
......@@ -43,8 +43,13 @@ const OPTION_NAME = 'tz_auth_fb';
}
});
/**
* Generates markup for a Facebook Login button
* @param {Boolean} $echo TRUE to echo the button, false to return a string
* @returns NULL|String
*/
function drawLoginButton($echo = true) {
$btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . Vars::$options['button_title'] . '</span></a>';
$btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . Vars::$options['button_title'] ?: 'Login' . '</span></a>';
if (!$echo) {
return $btn;
......@@ -53,17 +58,31 @@ function drawLoginButton($echo = true) {
echo $btn;
}
function loadSDK() {
static $loaded = false;
if ($loaded) {
return;
}
$loaded = true;
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'facebook-sdk.php');
Vars::$sdk = new \FB\Facebook(Array(
'appId' => Vars::$options['application_id']
, 'secret' => Vars::$options['application_secret']
, 'cookie' => true
));
}
class Actions {
/**
* Logs the user in to WP if they logged into FB
* @global $post
*/
public static function wp() {
global $post; // I want a better way to do this
if ($post->ID == Auth\Vars::$options['login_page'] && !is_user_logged_in()) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'facebook-sdk.php');
Vars::$sdk = new \FB\Facebook(Array(
'appId' => Vars::$options['application_id']
, 'secret' => Vars::$options['application_secret']
, 'cookie' => true
));
loadSDK();
if (Vars::$sdk->getSession()) {
$info = Vars::$sdk->api('/me');
......@@ -73,6 +92,9 @@ class Actions {
}
}
/**
* Load the Facebook scripts for login
*/
public static function wp_enqueue_scripts() {
if (is_admin() || is_user_logged_in()) {
return;
......@@ -84,9 +106,20 @@ class Actions {
_localize_script('tz-facebook', 'TzFBData', Array('AppID' => Vars::$options['application_id'], 'ext_perms' => implode(',', array_keys(Vars::$options['ext_perms']))));
}
/**
* Creates the anchor needed for Facebook scripts
*/
public static function get_footer() {
echo '<div id="fb-root"></div>';
}
/**
* Destroy Facebook session data on site if the log out of WordPress
*/
public static function wp_logout() {
loadSDK();
Vars::$sdk->setSession(); // I think this is how you log them out of Facebook
}
}
class ShortCodes {
......@@ -105,7 +138,16 @@ class ShortCodes {
}
class Vars {
/**
* WordPress option for this module
* @type WP_Option
*/
public static $options;
/**
* An instance of Facebook's PHP SDK
* @type Facebook
*/
public static $sdk;
}
?>
\ No newline at end of file
......
......@@ -17,10 +17,18 @@ const CAPABILITY = 'manage_auth'; // User & Roles capability name
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;
......@@ -29,11 +37,20 @@ function validate($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);
......@@ -42,6 +59,10 @@ class Actions {
}
}
/**
* 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__]));
......
......@@ -31,6 +31,7 @@ function import($com) {
$file = $dir . $com . '.php';
if (is_dir($dir) && is_file($file)) {
require_once($file);
Vars::$loaded[$com] = 1;
}
}
......@@ -97,4 +98,8 @@ function add_settings_fields($class, $page = 'general', $section = 'default') {
add_settings_field($method->name, ucwords(str_replace('_', ' ', $method->name)), Array($class, $method->name), $page, $section);
}
}
class Vars {
public static $loaded = Array();
}
?>
\ No newline at end of file
......
......@@ -39,6 +39,11 @@ function _die() {
return call_user_func_array('wp_die', $params);
}
function _set_current_user() {
$params = func_get_args();
return call_user_func_array('wp_set_current_user', $params);
}
function _get_current_user() {
$params = func_get_args();
return call_user_func_array('wp_get_current_user', $params);
......