Updated Auth library with settings, added Facebook settings
Showing
6 changed files
with
240 additions
and
2 deletions
| ... | @@ -2,7 +2,9 @@ | ... | @@ -2,7 +2,9 @@ |
| 2 | 2 | ||
| 3 | namespace Tz\WordPress\Tools\Auth; | 3 | namespace Tz\WordPress\Tools\Auth; |
| 4 | 4 | ||
| 5 | use \Exception, \LogicException, \InvalidArgumentException, \BadMethodCallException; | 5 | use Tz\WordPress\Tools; |
| 6 | use Tz\Common; | ||
| 7 | use Exception, LogicException, InvalidArgumentException, BadMethodCallException; | ||
| 6 | 8 | ||
| 7 | const REG_METH_AUTO_REG = 1; | 9 | const REG_METH_AUTO_REG = 1; |
| 8 | const REG_METH_VALID_EMAIL = 2; | 10 | const REG_METH_VALID_EMAIL = 2; |
| ... | @@ -12,6 +14,24 @@ const FORGOT_METH_RAND_PASS = 2; | ... | @@ -12,6 +14,24 @@ const FORGOT_METH_RAND_PASS = 2; |
| 12 | 14 | ||
| 13 | const ACTION_ACTIVATE = 'activate_account'; | 15 | const ACTION_ACTIVATE = 'activate_account'; |
| 14 | 16 | ||
| 17 | const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`) | ||
| 18 | |||
| 19 | call_user_func(function() { | ||
| 20 | Vars::$options = new Tools\WP_Option(OPTION_NAME); | ||
| 21 | |||
| 22 | if (is_admin()) { | ||
| 23 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php'); | ||
| 24 | } | ||
| 25 | |||
| 26 | if (is_array(Vars::$options['third_party'])) { | ||
| 27 | foreach (Vars::$options['third_party'] as $tp => $on) { | ||
| 28 | if ($on) { | ||
| 29 | require_once(__DIR__ . DIRECTORY_SEPARATOR . $tp . DIRECTORY_SEPARATOR . $tp . '.php'); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | }); | ||
| 34 | |||
| 15 | /** | 35 | /** |
| 16 | * Attempts to login the user | 36 | * Attempts to login the user |
| 17 | * @param {String} $username | 37 | * @param {String} $username |
| ... | @@ -80,7 +100,11 @@ function forgot_password($username, $forgot_method) { | ... | @@ -80,7 +100,11 @@ function forgot_password($username, $forgot_method) { |
| 80 | 100 | ||
| 81 | } | 101 | } |
| 82 | 102 | ||
| 83 | class Validation extends \Tz\Validation { | 103 | class Vars { |
| 104 | public static $options; | ||
| 105 | } | ||
| 106 | |||
| 107 | class Validation extends Common\Validation { | ||
| 84 | /** | 108 | /** |
| 85 | * @rule Not blank | 109 | * @rule Not blank |
| 86 | * @rule Valid WordPress username | 110 | * @rule Valid WordPress username | ... | ... |
com/Auth/Facebook/Facebook.php
0 → 100644
| 1 | <?php | ||
| 2 | /** | ||
| 3 | * This needs to go in the <html tag | ||
| 4 | * xmlns:fb="http://www.facebook.com/2008/fbml" | ||
| 5 | * | ||
| 6 | * http://wpdev.tenzinghost.com | ||
| 7 | * API Key: 83f54e078b9aa0e303bba959dc0a566f | ||
| 8 | * App Secret: e542aca35ab698121fa5917211013a41 | ||
| 9 | * App ID: 105917066126941 | ||
| 10 | * | ||
| 11 | * http://wp.cb | ||
| 12 | * API Key: 3bcccfd8c28c52197141266d9e417649 | ||
| 13 | * App Secret: 9bfcd828bc6ccef12336dea57df93ecb | ||
| 14 | * App ID: 138943536118944 | ||
| 15 | */ | ||
| 16 | |||
| 17 | namespace Tz\WordPress\Tools\Auth\Facebook; | ||
| 18 | |||
| 19 | use Tz\WordPress\Tools; | ||
| 20 | use Tz\WordPress\Tools\ShortCodes as SC; | ||
| 21 | |||
| 22 | use InvalidArgumentException; | ||
| 23 | |||
| 24 | const OPTION_NAME = 'tz_auth_fb'; | ||
| 25 | |||
| 26 | call_user_func(function() { | ||
| 27 | SC\add_shortcodes(__NAMESPACE__ . '\ShortCodes'); | ||
| 28 | Vars::$options = new Tools\WP_Option(OPTION_NAME); | ||
| 29 | |||
| 30 | if (is_admin()) { | ||
| 31 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php'); | ||
| 32 | } | ||
| 33 | }); | ||
| 34 | |||
| 35 | function loadJSSDK() { | ||
| 36 | return ' | ||
| 37 | <div id="fb-root"></div> | ||
| 38 | <script> | ||
| 39 | window.fbAsyncInit = function() { | ||
| 40 | FB.init({appId: \'' . Vars::$options['application_id'] . '\', status: true, cookie: true, xfbml: true}); | ||
| 41 | FB.Event.subscribe(\'auth.login\', function(response) { window.location.reload(); }); | ||
| 42 | }; | ||
| 43 | (function() { | ||
| 44 | var e = document.createElement(\'script\'); e.async = true; | ||
| 45 | e.src = document.location.protocol + \'//connect.facebook.net/en_US/all.js\'; | ||
| 46 | document.getElementById(\'fb-root\').appendChild(e); | ||
| 47 | }()); | ||
| 48 | </script> | ||
| 49 | '; | ||
| 50 | } | ||
| 51 | |||
| 52 | function drawLoginButton($value = 'Login') { | ||
| 53 | echo '<fb:login-button></fb:login-button>'; | ||
| 54 | // echo '<a class="fb_button fb_button_medium"><span class="fb_button_text">' . $value . '</span></a>'; | ||
| 55 | } | ||
| 56 | |||
| 57 | class Vars { | ||
| 58 | public static $options = false; | ||
| 59 | } | ||
| 60 | |||
| 61 | class ShortCodes { | ||
| 62 | public static function fb_login_button() { | ||
| 63 | ob_start(); | ||
| 64 | drawLoginButton(); | ||
| 65 | $btn = ob_get_contents(); | ||
| 66 | ob_end_clean(); | ||
| 67 | |||
| 68 | return $btn; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
com/Auth/Facebook/Settings.php
0 → 100644
| 1 | <?php | ||
| 2 | |||
| 3 | // todo: move ext_perms to a constant somewhere | ||
| 4 | |||
| 5 | namespace Tz\WordPress\Tools\Auth\Facebook\Settings; | ||
| 6 | |||
| 7 | use Tz\WordPress\Tools, Tz\WordPress\Tools\Auth, Tz\WordPress\Tools\Auth\Facebook; | ||
| 8 | |||
| 9 | const OPTION_SECTION_CRED = 'fb_creds'; | ||
| 10 | const OPTION_SECTION_SEL = 'fb_opts'; | ||
| 11 | |||
| 12 | call_user_func(function() { | ||
| 13 | Vars::$options = new Tools\WP_Option(Facebook\OPTION_NAME); | ||
| 14 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | ||
| 15 | }); | ||
| 16 | |||
| 17 | function validate($data) { | ||
| 18 | return (array)$data; | ||
| 19 | } | ||
| 20 | |||
| 21 | class Vars { | ||
| 22 | public static $options = false; | ||
| 23 | public static $data_permissions = Array('email', 'read_insights', 'read_stream', 'read_mailbox', 'ads_management', 'xmpp_login', 'user_about_me', 'user_activities', 'user_birthday', 'user_education_history', 'user_events', 'user_groups', 'user_hometown', 'user_interests', 'user_likes', 'user_location', 'user_notes', 'user_online_presence', 'user_photo_video_tags', 'user_photos', 'user_relationships', 'user_religion_politics', 'user_status', 'user_videos', 'user_website', 'user_work_history', 'read_friendlists', 'read_requests'); | ||
| 24 | } | ||
| 25 | |||
| 26 | class Actions { | ||
| 27 | public static function admin_init() { | ||
| 28 | register_setting(Auth\Settings\OPTION_GROUP, Facebook\OPTION_NAME, __NAMESPACE__ . '\validate'); | ||
| 29 | |||
| 30 | add_settings_section(OPTION_SECTION_CRED, 'Facebook Credentials', function(){}, Auth\Settings\ADMIN_PAGE); | ||
| 31 | Tools\add_settings_fields(__NAMESPACE__ . '\Cred_Fields', Auth\Settings\ADMIN_PAGE, OPTION_SECTION_CRED); | ||
| 32 | |||
| 33 | add_settings_section(OPTION_SECTION_SEL, 'Facebook Extended Data Permissions', function() { echo '<p>Select which additional data you wish to collect from the user</p>'; }, Auth\Settings\ADMIN_PAGE); | ||
| 34 | foreach (Vars::$data_permissions as $option) { | ||
| 35 | add_settings_field($option, ucwords(str_replace('_', ' ', $option)), Array(new Opt_Fields(), $option), Auth\Settings\ADMIN_PAGE, OPTION_SECTION_SEL); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | class Cred_Fields { | ||
| 41 | public static function api_key() { | ||
| 42 | echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" size="45" />'; | ||
| 43 | } | ||
| 44 | |||
| 45 | public static function application_secret() { | ||
| 46 | echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" size="45" />'; | ||
| 47 | } | ||
| 48 | |||
| 49 | public static function application_id() { | ||
| 50 | echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" />'; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | class Opt_Fields { | ||
| 55 | public function __call($fn, $args) { | ||
| 56 | echo '<input type="checkbox" id="' . $fn . '" name="' . Facebook\OPTION_NAME . '[ext_perms][' . $fn . ']" value="1" ' . checked('1', Vars::$options['ext_perms'][$fn], false) . ' />'; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
com/Auth/Facebook/facebook-sdk.php
0 → 100644
This diff is collapsed.
Click to expand it.
com/Auth/Settings.php
0 → 100644
| 1 | <?php | ||
| 2 | namespace Tz\WordPress\Tools\Auth\Settings; | ||
| 3 | |||
| 4 | use Tz\Common; | ||
| 5 | use Tz\WordPress\Tools; | ||
| 6 | use Tz\WordPress\Tools\Auth; | ||
| 7 | |||
| 8 | const OPTION_GROUP = 'auth_group'; // Grouping of options used for setting output by WP | ||
| 9 | const OPTION_SECTION = 'tz_auth_main'; | ||
| 10 | const ADMIN_PAGE = 'tz-tools-auth'; // URI of options page | ||
| 11 | const CAPABILITY = 'manage_auth'; // User & Roles capability name | ||
| 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 | // Validation library causing some issues with Array | ||
| 28 | $valid = new Validation((array)$data); | ||
| 29 | return $valid->valid; | ||
| 30 | } | ||
| 31 | |||
| 32 | class Actions { | ||
| 33 | public static function admin_menu() { | ||
| 34 | add_options_page('Authentication', 'Authentication', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\displayPage'); | ||
| 35 | } | ||
| 36 | |||
| 37 | public static function admin_init() { | ||
| 38 | register_setting(OPTION_GROUP, Auth\OPTION_NAME, __NAMESPACE__ . '\validate'); | ||
| 39 | add_settings_section(OPTION_SECTION, 'Authentication Pages', function(){}, ADMIN_PAGE); | ||
| 40 | |||
| 41 | Tools\add_settings_fields(__NAMESPACE__ . '\Fields', ADMIN_PAGE, OPTION_SECTION); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | class Fields { | ||
| 46 | 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__])); | ||
| 48 | } | ||
| 49 | |||
| 50 | public static function account_page() { | ||
| 51 | _dropdown_pages(Array('name' => Auth\OPTION_NAME . '[' . __FUNCTION__ . ']', 'sort_column' => 'menu_order', 'echo' => 1, 'selected' => Auth\Vars::$options[__FUNCTION__])); | ||
| 52 | } | ||
| 53 | |||
| 54 | public static function Facebook() { | ||
| 55 | echo '<input type="checkbox" id="' . __FUNCTION__ . '" name="' . Auth\OPTION_NAME . '[third_party][' . __FUNCTION__ . ']" value="1" ' . checked('1', Auth\Vars::$options['third_party'][__FUNCTION__], false) . ' />'; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | class Validation extends Common\Validation { | ||
| 60 | public static function login_page($val) { | ||
| 61 | // if !is page throw exception | ||
| 62 | } | ||
| 63 | |||
| 64 | public static function account_page($val) { | ||
| 65 | // if !is page throw exception | ||
| 66 | // if is same as login_page throw exception | ||
| 67 | } | ||
| 68 | } | ||
| 69 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
com/Auth/settings_view.php
0 → 100644
| 1 | <?php | ||
| 2 | namespace Tz\WordPress\Tools\Auth\Settings; | ||
| 3 | ?> | ||
| 4 | <div class="wrap"> | ||
| 5 | <?php screen_icon(); ?> | ||
| 6 | <h2>Authentication 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 |
-
Please register or sign in to post a comment