Updated Auth library, added Analytics library
Showing
8 changed files
with
224 additions
and
14 deletions
com/Analytics/Analytics.php
0 → 100644
| 1 | <?php | ||
| 2 | namespace Tz\WordPress\Tools\Analytics; | ||
| 3 | |||
| 4 | use Tz\WordPress\Tools; | ||
| 5 | |||
| 6 | const OPTION_NAME = 'tz_analytics'; | ||
| 7 | |||
| 8 | call_user_func(function() { | ||
| 9 | Vars::$options = new Tools\WP_Option(OPTION_NAME); | ||
| 10 | |||
| 11 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | ||
| 12 | |||
| 13 | if (is_admin()) { | ||
| 14 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php'); | ||
| 15 | } | ||
| 16 | }); | ||
| 17 | |||
| 18 | class Actions { | ||
| 19 | public static function wp_print_scripts() { | ||
| 20 | if (Tz\LIVE !== 1 || empty(Vars::$options['api_key'])) { | ||
| 21 | return; | ||
| 22 | } | ||
| 23 | ?> | ||
| 24 | <script type="text/javascript"> | ||
| 25 | var _gaq = _gaq || []; | ||
| 26 | _gaq.push(['_setAccount', '<?php echo Vars::$options['api_key']; ?>']); | ||
| 27 | _gaq.push(['_trackPageview']); | ||
| 28 | |||
| 29 | (function() { | ||
| 30 | var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; | ||
| 31 | ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; | ||
| 32 | var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); | ||
| 33 | })(); | ||
| 34 | </script> | ||
| 35 | <?php | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | class Vars { | ||
| 40 | public static $options; | ||
| 41 | } | ||
| 42 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
com/Analytics/Settings.php
0 → 100644
| 1 | <?php | ||
| 2 | |||
| 3 | namespace Tz\WordPress\Tools\Analytics\Settings; | ||
| 4 | |||
| 5 | use Tz\WordPress\Tools; | ||
| 6 | use Tz\WordPress\Tools\Analytics; | ||
| 7 | |||
| 8 | const OPTION_GROUP = 'tz_analytics_group'; | ||
| 9 | const OPTION_SECTION = 'tz_analytics_main'; | ||
| 10 | const ADMIN_PAGE = 'tz-tool-analytics'; | ||
| 11 | const CAPABILITY = 'configure_analytics'; | ||
| 12 | |||
| 13 | call_user_func(function() { | ||
| 14 | $role = get_role('administrator'); | ||
| 15 | $role->add_cap(CAPABILITY); | ||
| 16 | |||
| 17 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | ||
| 18 | }); | ||
| 19 | |||
| 20 | function displayPage() { | ||
| 21 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'settings_view.php'); | ||
| 22 | } | ||
| 23 | |||
| 24 | function validate($data) { | ||
| 25 | return $data; | ||
| 26 | } | ||
| 27 | |||
| 28 | class Actions { | ||
| 29 | public static function admin_menu() { | ||
| 30 | add_options_page('Analytics', 'Analytics', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\displayPage'); | ||
| 31 | } | ||
| 32 | |||
| 33 | public static function admin_init() { | ||
| 34 | register_setting(OPTION_GROUP, Analytics\OPTION_NAME, __NAMESPACE__ . '\validate'); | ||
| 35 | add_settings_section(OPTION_SECTION, '', function() {}, ADMIN_PAGE); | ||
| 36 | |||
| 37 | Tools\add_settings_fields(__NAMESPACE__ . '\Fields', ADMIN_PAGE, OPTION_SECTION); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | class Fields { | ||
| 42 | public static function api_key() { | ||
| 43 | echo '<input tpe="text" name="' . Analytics\OPTION_NAME . '[' . __FUNCTION__ . ']" id="' . __FUNCTION__ . '" value="' . Analytics\Vars::$options[__FUNCTION__] . '" />'; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
com/Analytics/settings_view.php
0 → 100644
| 1 | <?php | ||
| 2 | namespace Tz\WordPress\Tools\Analytics\Settings; | ||
| 3 | ?> | ||
| 4 | <div class="wrap"> | ||
| 5 | <?php screen_icon(); ?> | ||
| 6 | <h2>Analytics Settings</h2> | ||
| 7 | |||
| 8 | <form method="post" action="options.php"> | ||
| 9 | <?php | ||
| 10 | settings_fields(OPTION_GROUP); | ||
| 11 | do_settings_sections(ADMIN_PAGE); | ||
| 12 | ?> | ||
| 13 | <p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p> | ||
| 14 | </form> | ||
| 15 | </div> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -12,8 +12,7 @@ const REG_METH_VALID_EMAIL = 2; | ... | @@ -12,8 +12,7 @@ const REG_METH_VALID_EMAIL = 2; |
| 12 | const FORGOT_METH_VALID_EMAIL = 1; | 12 | const FORGOT_METH_VALID_EMAIL = 1; |
| 13 | const FORGOT_METH_RAND_PASS = 2; | 13 | const FORGOT_METH_RAND_PASS = 2; |
| 14 | 14 | ||
| 15 | const ACTION_LOGIN = 'auth_login'; // probably don't need | 15 | // The things with these is they're dynamic but static functions aren't... |
| 16 | const ACTION_LOGOUT = 'auth_logout'; // probably need, tell FB/etc to remove their cookies | ||
| 17 | const ACTION_ACTIVATE = 'auth_activate'; | 16 | const ACTION_ACTIVATE = 'auth_activate'; |
| 18 | 17 | ||
| 19 | const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`) | 18 | const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`) |
| ... | @@ -55,6 +54,8 @@ function login($username, $password, $remember = true) { | ... | @@ -55,6 +54,8 @@ function login($username, $password, $remember = true) { |
| 55 | )); | 54 | )); |
| 56 | 55 | ||
| 57 | if (get_class($auth) == 'WP_User') { | 56 | if (get_class($auth) == 'WP_User') { |
| 57 | _set_current_user($auth->ID); // Not sure why I had to do this, oh well | ||
| 58 | |||
| 58 | return $auth; | 59 | return $auth; |
| 59 | } | 60 | } |
| 60 | 61 | ||
| ... | @@ -77,7 +78,19 @@ function logout() { | ... | @@ -77,7 +78,19 @@ function logout() { |
| 77 | return true; | 78 | return true; |
| 78 | } | 79 | } |
| 79 | 80 | ||
| 80 | function register($user_data = Array(), $registration_method) { | 81 | /** |
| 82 | * @param {Array} $user_data User data array, requires minimum (username, password, email) | ||
| 83 | * @param {Integer} $registration_method Method of registeration, see constants beginning with REG_METH | ||
| 84 | * @throws {InvalidArgumentException} If an invalid $registration_method is passed | ||
| 85 | * @throw {BadMethodCallException} If any of the $user_data parameters are invalid | ||
| 86 | * @returns {Integer} New user $id if successful | ||
| 87 | * @uses wp-includes/registration.php | ||
| 88 | */ | ||
| 89 | function register($user_data = Array(), $registration_method = 1) { | ||
| 90 | if (!in_array($registration_method, Array(REG_METH_AUTO_REG, REG_METH_VALID_EMAIL))) { | ||
| 91 | throw new InvalidArgumentException("Invalid registration method selected"); | ||
| 92 | } | ||
| 93 | |||
| 81 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); | 94 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); |
| 82 | 95 | ||
| 83 | $valid = new Validation($user_data); | 96 | $valid = new Validation($user_data); |
| ... | @@ -86,26 +99,39 @@ function register($user_data = Array(), $registration_method) { | ... | @@ -86,26 +99,39 @@ function register($user_data = Array(), $registration_method) { |
| 86 | } | 99 | } |
| 87 | 100 | ||
| 88 | array_filter($user_data, 'esc_sql'); | 101 | array_filter($user_data, 'esc_sql'); |
| 102 | // $key = substr( md5( time() . rand() . $user_email ), 0, 16 ); | ||
| 103 | |||
| 104 | // possibly call wpmu_signup_user() if REG_METH_VALID_EMAIL; _insert_user if REG_METH_AUTO_REG | ||
| 105 | // Can't do that without making a database call; the unique registration key is created and destroyed in the function | ||
| 106 | // I'll have to make a database call to retreive it, at the very lest | ||
| 107 | // I can't do that at all; the function sends an email to the user with a auto-generated password | ||
| 108 | // I'll have to do database manipulation manually | ||
| 89 | $id = (int)_insert_user($user_data); | 109 | $id = (int)_insert_user($user_data); |
| 90 | 110 | ||
| 111 | // should I call ACTION_ACTIVATE if REG_METHOD_AUTO_REG? | ||
| 112 | |||
| 113 | // this is so wrong | ||
| 91 | global $wpdb; | 114 | global $wpdb; |
| 92 | $wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}"); | 115 | $wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}"); |
| 93 | 116 | ||
| 94 | return $id; | 117 | return $id; |
| 95 | } | 118 | } |
| 96 | 119 | ||
| 120 | // Don't think I need $username | ||
| 97 | function activate($username, $activation_key) { | 121 | function activate($username, $activation_key) { |
| 122 | // wpmu_activate_signup | ||
| 123 | // I can't do that either; that function sends a WordPress email | ||
| 124 | |||
| 98 | do_action(ACTION_ACTIVATE, $user_id); | 125 | do_action(ACTION_ACTIVATE, $user_id); |
| 99 | } | 126 | } |
| 100 | 127 | ||
| 128 | // Not sure I need this function | ||
| 129 | // Application can just set rand password | ||
| 130 | // Or perhapds I do need it, move it to registered again or something??? | ||
| 101 | function forgot_password($username, $forgot_method) { | 131 | function forgot_password($username, $forgot_method) { |
| 102 | 132 | ||
| 103 | } | 133 | } |
| 104 | 134 | ||
| 105 | class Vars { | ||
| 106 | public static $options; | ||
| 107 | } | ||
| 108 | |||
| 109 | class Validation extends Common\Validation { | 135 | class Validation extends Common\Validation { |
| 110 | /** | 136 | /** |
| 111 | * @rule Not blank | 137 | * @rule Not blank |
| ... | @@ -151,4 +177,12 @@ class Validation extends Common\Validation { | ... | @@ -151,4 +177,12 @@ class Validation extends Common\Validation { |
| 151 | } | 177 | } |
| 152 | } | 178 | } |
| 153 | } | 179 | } |
| 180 | |||
| 181 | class Vars { | ||
| 182 | /** | ||
| 183 | * WordPress option for this module | ||
| 184 | * @type WP_Option | ||
| 185 | */ | ||
| 186 | public static $options; | ||
| 187 | } | ||
| 154 | ?> | 188 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -43,8 +43,13 @@ const OPTION_NAME = 'tz_auth_fb'; | ... | @@ -43,8 +43,13 @@ const OPTION_NAME = 'tz_auth_fb'; |
| 43 | } | 43 | } |
| 44 | }); | 44 | }); |
| 45 | 45 | ||
| 46 | /** | ||
| 47 | * Generates markup for a Facebook Login button | ||
| 48 | * @param {Boolean} $echo TRUE to echo the button, false to return a string | ||
| 49 | * @returns NULL|String | ||
| 50 | */ | ||
| 46 | function drawLoginButton($echo = true) { | 51 | function drawLoginButton($echo = true) { |
| 47 | $btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . Vars::$options['button_title'] . '</span></a>'; | 52 | $btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . Vars::$options['button_title'] ?: 'Login' . '</span></a>'; |
| 48 | 53 | ||
| 49 | if (!$echo) { | 54 | if (!$echo) { |
| 50 | return $btn; | 55 | return $btn; |
| ... | @@ -53,17 +58,31 @@ function drawLoginButton($echo = true) { | ... | @@ -53,17 +58,31 @@ function drawLoginButton($echo = true) { |
| 53 | echo $btn; | 58 | echo $btn; |
| 54 | } | 59 | } |
| 55 | 60 | ||
| 61 | function loadSDK() { | ||
| 62 | static $loaded = false; | ||
| 63 | if ($loaded) { | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | $loaded = true; | ||
| 67 | |||
| 68 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'facebook-sdk.php'); | ||
| 69 | Vars::$sdk = new \FB\Facebook(Array( | ||
| 70 | 'appId' => Vars::$options['application_id'] | ||
| 71 | , 'secret' => Vars::$options['application_secret'] | ||
| 72 | , 'cookie' => true | ||
| 73 | )); | ||
| 74 | } | ||
| 75 | |||
| 56 | class Actions { | 76 | class Actions { |
| 77 | /** | ||
| 78 | * Logs the user in to WP if they logged into FB | ||
| 79 | * @global $post | ||
| 80 | */ | ||
| 57 | public static function wp() { | 81 | public static function wp() { |
| 58 | global $post; // I want a better way to do this | 82 | global $post; // I want a better way to do this |
| 59 | 83 | ||
| 60 | if ($post->ID == Auth\Vars::$options['login_page'] && !is_user_logged_in()) { | 84 | if ($post->ID == Auth\Vars::$options['login_page'] && !is_user_logged_in()) { |
| 61 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'facebook-sdk.php'); | 85 | loadSDK(); |
| 62 | Vars::$sdk = new \FB\Facebook(Array( | ||
| 63 | 'appId' => Vars::$options['application_id'] | ||
| 64 | , 'secret' => Vars::$options['application_secret'] | ||
| 65 | , 'cookie' => true | ||
| 66 | )); | ||
| 67 | 86 | ||
| 68 | if (Vars::$sdk->getSession()) { | 87 | if (Vars::$sdk->getSession()) { |
| 69 | $info = Vars::$sdk->api('/me'); | 88 | $info = Vars::$sdk->api('/me'); |
| ... | @@ -73,6 +92,9 @@ class Actions { | ... | @@ -73,6 +92,9 @@ class Actions { |
| 73 | } | 92 | } |
| 74 | } | 93 | } |
| 75 | 94 | ||
| 95 | /** | ||
| 96 | * Load the Facebook scripts for login | ||
| 97 | */ | ||
| 76 | public static function wp_enqueue_scripts() { | 98 | public static function wp_enqueue_scripts() { |
| 77 | if (is_admin() || is_user_logged_in()) { | 99 | if (is_admin() || is_user_logged_in()) { |
| 78 | return; | 100 | return; |
| ... | @@ -84,9 +106,20 @@ class Actions { | ... | @@ -84,9 +106,20 @@ class Actions { |
| 84 | _localize_script('tz-facebook', 'TzFBData', Array('AppID' => Vars::$options['application_id'], 'ext_perms' => implode(',', array_keys(Vars::$options['ext_perms'])))); | 106 | _localize_script('tz-facebook', 'TzFBData', Array('AppID' => Vars::$options['application_id'], 'ext_perms' => implode(',', array_keys(Vars::$options['ext_perms'])))); |
| 85 | } | 107 | } |
| 86 | 108 | ||
| 109 | /** | ||
| 110 | * Creates the anchor needed for Facebook scripts | ||
| 111 | */ | ||
| 87 | public static function get_footer() { | 112 | public static function get_footer() { |
| 88 | echo '<div id="fb-root"></div>'; | 113 | echo '<div id="fb-root"></div>'; |
| 89 | } | 114 | } |
| 115 | |||
| 116 | /** | ||
| 117 | * Destroy Facebook session data on site if the log out of WordPress | ||
| 118 | */ | ||
| 119 | public static function wp_logout() { | ||
| 120 | loadSDK(); | ||
| 121 | Vars::$sdk->setSession(); // I think this is how you log them out of Facebook | ||
| 122 | } | ||
| 90 | } | 123 | } |
| 91 | 124 | ||
| 92 | class ShortCodes { | 125 | class ShortCodes { |
| ... | @@ -105,7 +138,16 @@ class ShortCodes { | ... | @@ -105,7 +138,16 @@ class ShortCodes { |
| 105 | } | 138 | } |
| 106 | 139 | ||
| 107 | class Vars { | 140 | class Vars { |
| 141 | /** | ||
| 142 | * WordPress option for this module | ||
| 143 | * @type WP_Option | ||
| 144 | */ | ||
| 108 | public static $options; | 145 | public static $options; |
| 146 | |||
| 147 | /** | ||
| 148 | * An instance of Facebook's PHP SDK | ||
| 149 | * @type Facebook | ||
| 150 | */ | ||
| 109 | public static $sdk; | 151 | public static $sdk; |
| 110 | } | 152 | } |
| 111 | ?> | 153 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -17,10 +17,18 @@ const CAPABILITY = 'manage_auth'; // User & Roles capability name | ... | @@ -17,10 +17,18 @@ const CAPABILITY = 'manage_auth'; // User & Roles capability name |
| 17 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | 17 | Tools\add_actions(__NAMESPACE__ . '\Actions'); |
| 18 | }); | 18 | }); |
| 19 | 19 | ||
| 20 | /** | ||
| 21 | * Loads the view for the settings page | ||
| 22 | */ | ||
| 20 | function displayPage() { | 23 | function displayPage() { |
| 21 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'settings_view.php'); | 24 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'settings_view.php'); |
| 22 | } | 25 | } |
| 23 | 26 | ||
| 27 | /** | ||
| 28 | * @todo Make this work | ||
| 29 | * @param {Array} $data Data provided by WP to sanatize | ||
| 30 | * @returns Array | ||
| 31 | */ | ||
| 24 | function validate($data) { | 32 | function validate($data) { |
| 25 | return $data; | 33 | return $data; |
| 26 | 34 | ||
| ... | @@ -29,11 +37,20 @@ function validate($data) { | ... | @@ -29,11 +37,20 @@ function validate($data) { |
| 29 | return $valid->valid; | 37 | return $valid->valid; |
| 30 | } | 38 | } |
| 31 | 39 | ||
| 40 | /** | ||
| 41 | * @uses Tools\add_actions | ||
| 42 | */ | ||
| 32 | class Actions { | 43 | class Actions { |
| 44 | /** | ||
| 45 | * Creates a settings page for the WP admin | ||
| 46 | */ | ||
| 33 | public static function admin_menu() { | 47 | public static function admin_menu() { |
| 34 | add_options_page('Authentication', 'Authentication', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\displayPage'); | 48 | add_options_page('Authentication', 'Authentication', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\displayPage'); |
| 35 | } | 49 | } |
| 36 | 50 | ||
| 51 | /** | ||
| 52 | * Generates variables for the module's options | ||
| 53 | */ | ||
| 37 | public static function admin_init() { | 54 | public static function admin_init() { |
| 38 | register_setting(OPTION_GROUP, Auth\OPTION_NAME, __NAMESPACE__ . '\validate'); | 55 | register_setting(OPTION_GROUP, Auth\OPTION_NAME, __NAMESPACE__ . '\validate'); |
| 39 | add_settings_section(OPTION_SECTION, 'Authentication Pages', function(){}, ADMIN_PAGE); | 56 | add_settings_section(OPTION_SECTION, 'Authentication Pages', function(){}, ADMIN_PAGE); |
| ... | @@ -42,6 +59,10 @@ class Actions { | ... | @@ -42,6 +59,10 @@ class Actions { |
| 42 | } | 59 | } |
| 43 | } | 60 | } |
| 44 | 61 | ||
| 62 | /** | ||
| 63 | * Each function is a variable stored in the WP_Option | ||
| 64 | * @uses Tools\add_settings_fields | ||
| 65 | */ | ||
| 45 | class Fields { | 66 | class Fields { |
| 46 | public static function login_page() { | 67 | public static function login_page() { |
| 47 | _dropdown_pages(Array('name' => Auth\OPTION_NAME . '[' . __FUNCTION__ . ']', 'sort_column' => 'menu_order', 'echo' => 1, 'selected' => Auth\Vars::$options[__FUNCTION__])); | 68 | _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) { | ... | @@ -31,6 +31,7 @@ function import($com) { |
| 31 | $file = $dir . $com . '.php'; | 31 | $file = $dir . $com . '.php'; |
| 32 | if (is_dir($dir) && is_file($file)) { | 32 | if (is_dir($dir) && is_file($file)) { |
| 33 | require_once($file); | 33 | require_once($file); |
| 34 | Vars::$loaded[$com] = 1; | ||
| 34 | } | 35 | } |
| 35 | } | 36 | } |
| 36 | 37 | ||
| ... | @@ -97,4 +98,8 @@ function add_settings_fields($class, $page = 'general', $section = 'default') { | ... | @@ -97,4 +98,8 @@ function add_settings_fields($class, $page = 'general', $section = 'default') { |
| 97 | add_settings_field($method->name, ucwords(str_replace('_', ' ', $method->name)), Array($class, $method->name), $page, $section); | 98 | add_settings_field($method->name, ucwords(str_replace('_', ' ', $method->name)), Array($class, $method->name), $page, $section); |
| 98 | } | 99 | } |
| 99 | } | 100 | } |
| 101 | |||
| 102 | class Vars { | ||
| 103 | public static $loaded = Array(); | ||
| 104 | } | ||
| 100 | ?> | 105 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -39,6 +39,11 @@ function _die() { | ... | @@ -39,6 +39,11 @@ function _die() { |
| 39 | return call_user_func_array('wp_die', $params); | 39 | return call_user_func_array('wp_die', $params); |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | function _set_current_user() { | ||
| 43 | $params = func_get_args(); | ||
| 44 | return call_user_func_array('wp_set_current_user', $params); | ||
| 45 | } | ||
| 46 | |||
| 42 | function _get_current_user() { | 47 | function _get_current_user() { |
| 43 | $params = func_get_args(); | 48 | $params = func_get_args(); |
| 44 | return call_user_func_array('wp_get_current_user', $params); | 49 | return call_user_func_array('wp_get_current_user', $params); | ... | ... |
-
Please register or sign in to post a comment