From 43d3b951b29032552a327faacf6516c7d5038790 Mon Sep 17 00:00:00 2001 From: aschweigert Date: Thu, 7 Jan 2016 11:21:16 -0500 Subject: [PATCH 1/2] move some stuff around because it drives me crazy, general housekeeping --- inc/avatars.php | 47 ++++++++++ inc/helpers.php | 115 +++++++++++++++++++++++ inc/post-tags.php | 46 --------- inc/users.php | 174 +++++------------------------------ tests/inc/test-avatars.php | 4 + tests/inc/test-helpers.php | 17 ++++ tests/inc/test-post-tags.php | 4 - tests/inc/test-users.php | 24 +---- 8 files changed, 206 insertions(+), 225 deletions(-) diff --git a/inc/avatars.php b/inc/avatars.php index bf3731884..cdd3ae991 100644 --- a/inc/avatars.php +++ b/inc/avatars.php @@ -2,3 +2,50 @@ // Include avatars module include_once dirname(__FILE__) . '/avatars/functions.php'; + +/** + * Determine whether or not an author has a valid gravatar image + * see: http://codex.wordpress.org/Using_Gravatars + * + * @param $email string an author's email address + * @return bool true if a gravatar is available for this user + * @since 0.3 + */ +function largo_has_gravatar( $email ) { + // Craft a potential url and test its headers + $hash = md5( strtolower( trim( $email ) ) ); + + $cache_key = 'largo_has_gravatar_' . $hash; + if ( false !== ( $cache_value = get_transient( $cache_key ) ) ) { + return (bool) $cache_value; + } + + $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404'; + $response = wp_remote_head( $uri ); + if ( 200 == wp_remote_retrieve_response_code( $response ) ) { + $cache_value = '1'; + } else { + $cache_value = '0'; + } + set_transient( $cache_key, $cache_value ); + return (bool) $cache_value; +} + +/** + * Determine whether or not a user has an avatar. Fallback checks if user has a gravatar. + * + * @param $email string an author's email address + * @return bool true if an avatar is available for this user + * @since 0.4 + */ +function largo_has_avatar($email) { + $user = get_user_by('email', $email); + $result = largo_get_user_avatar_id($user->ID); + if (!empty($result)) + return true; + else { + if (largo_has_gravatar($email)) + return true; + } + return false; +} \ No newline at end of file diff --git a/inc/helpers.php b/inc/helpers.php index 940257624..a18b863ec 100644 --- a/inc/helpers.php +++ b/inc/helpers.php @@ -27,6 +27,7 @@ function largo_fb_url_to_username( $url ) { return $username; } + /** * Checks to see if a given Facebook username or ID has following enabled by * checking the iframe of that user's "Follow" button for . @@ -53,6 +54,65 @@ function largo_fb_user_is_followable( $username ) { } } +/** + * Cleans a Facebook url to the bare username or id when the user is edited + * + * Edits $_POST directly because there's no other way to save the corrected username + * from this callback. The action hooks this is used for run before edit_user in + * wp-admin/user-edit.php, which overwrites the user's contact methods. edit_user + * reads from $_POST. + * + * @param object $user_id the WP_User object being edited + * @param array $_POST + * @since 0.4 + * @uses largo_fb_url_to_username + * @link http://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile_update + * @link http://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update + */ +function clean_user_fb_username($user_id) { + + if ( current_user_can('edit_user', $user_id) ) { + $fb = largo_fb_url_to_username( $_POST['fb'] ); + if ( preg_match( '/[^a-zA-Z0-9\.\-]/', $fb ) ) { + // it's not a valid Facebook username, because it uses an invalid character + $fb = ""; + } + update_user_meta($user_id, 'fb', $fb); + if ( get_user_meta($user_id, 'fb', true) != $fb ) { + wp_die(__('An error occurred.')); + } + $_POST['fb'] = $fb; + } +} + +/** + * Checks that the Facebook URL submitted is valid and the user is followable and causes an error if not + * + * @uses largo_fb_url_to_username + * @uses largo_fb_user_is_followable + * @param $errors the error object + * @param bool $update whether this is a user update + * @param object $user a WP_User object + * @link http://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors + * @since 0.4 + */ +function validate_fb_username( $errors, $update, $user ) { + + if ( isset( $_POST["fb"] ) ) { + $fb_suspect = trim( $_POST["fb"] ); + if( ! empty( $fb_suspect ) ) { + $fb_user = largo_fb_url_to_username( $fb_suspect ); + if ( preg_match( '/[^a-zA-Z0-9\.\-]/', $fb_user ) ) { + // it's not a valid Facebook username, because it uses an invalid character + $errors->add('fb_username', '' . $fb_suspect . ' ' . __('is an invalid Facebook username.') . '

' . '

' . __('Facebook usernames only use the uppercase and lowercase alphabet letters (a-z A-Z), the Arabic numbers (0-9), periods (.) and dashes (-)') ); + } + if ( ! largo_fb_user_is_followable( $fb_user ) ) { + $errors->add('fb_username',' ' . $fb_suspect . ' ' . __('does not allow followers on Facebook.') . '

' . '

' . __('Follow these instructions to allow others to follow you.') ); + } + } + } +} + /** * Returns a Twitter username (without the @ symbol) * @@ -76,6 +136,61 @@ function largo_twitter_url_to_username( $url ) { return $username; } +/** + * Cleans a Twitter url or an @username to the bare username when the user is edited + * + * Edits $_POST directly because there's no other way to save the corrected username + * from this callback. The action hooks this is used for run before edit_user in + * wp-admin/user-edit.php, which overwrites the user's contact methods. edit_user + * reads from $_POST. + * + * @param object $user_id the WP_User object being edited + * @param array $_POST + * @since 0.4 + * @uses largo_twitter_url_to_username + * @link http://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile_update + * @link http://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update + */ +function clean_user_twitter_username($user_id) { + + if ( current_user_can('edit_user', $user_id) ) { + $twitter = largo_twitter_url_to_username( $_POST['twitter'] ); + if ( preg_match( '/[^a-zA-Z0-9_]/', $twitter ) ) { + // it's not a valid twitter username, because it uses an invalid character + $twitter = ""; + } + update_user_meta($user_id, 'twitter_link', $twitter); + if ( get_user_meta($user_id, 'twitter_link', true) != $twitter ) { + wp_die(__('An error occurred.')); + } + $_POST['twitter'] = $twitter; + } +} + +/** + * Checks that the Twitter URL is composed of valid characters [a-zA-Z0-9_] and + * causes an error if there is not. + * + * @param $errors the error object + * @param bool $update whether this is a user update + * @param object $user a WP_User object + * @uses largo_twitter_url_to_username + * @link http://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors + * @since 0.4 + */ +function validate_twitter_username( $errors, $update, $user ) { + + if ( isset( $_POST["twitter"] ) ) { + $tw_suspect = trim( $_POST["twitter"] ); + if( ! empty( $tw_suspect ) ) { + if ( preg_match( '/[^a-zA-Z0-9_]/', largo_twitter_url_to_username( $tw_suspect ) ) ) { + // it's not a valid twitter username, because it uses an invalid character + $errors->add('twitter_username', '' . $tw_suspect . '' . __('is an invalid Twitter username.') . '

' . '

' . __('Twitter usernames only use the uppercase and lowercase alphabet letters (a-z A-Z), the Arabic numbers (0-9), and underscores (_).') ); + } + } + } +} + /** * Give it a YouTube URL, it'll give you just the video ID * diff --git a/inc/post-tags.php b/inc/post-tags.php index 9aff98b73..f009bfc9e 100644 --- a/inc/post-tags.php +++ b/inc/post-tags.php @@ -258,52 +258,6 @@ function largo_post_social_links( $echo = true ) { } } -/** - * Determine whether or not an author has a valid gravatar image - * see: http://codex.wordpress.org/Using_Gravatars - * - * @param $email string an author's email address - * @return bool true if a gravatar is available for this user - * @since 0.3 - */ -function largo_has_gravatar( $email ) { - // Craft a potential url and test its headers - $hash = md5( strtolower( trim( $email ) ) ); - - $cache_key = 'largo_has_gravatar_' . $hash; - if ( false !== ( $cache_value = get_transient( $cache_key ) ) ) { - return (bool) $cache_value; - } - - $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404'; - $response = wp_remote_head( $uri ); - if ( 200 == wp_remote_retrieve_response_code( $response ) ) { - $cache_value = '1'; - } else { - $cache_value = '0'; - } - set_transient( $cache_key, $cache_value ); - return (bool) $cache_value; -} -/** - * Determine whether or not a user has an avatar. Fallback checks if user has a gravatar. - * - * @param $email string an author's email address - * @return bool true if an avatar is available for this user - * @since 0.4 - */ -function largo_has_avatar($email) { - $user = get_user_by('email', $email); - $result = largo_get_user_avatar_id($user->ID); - if (!empty($result)) - return true; - else { - if (largo_has_gravatar($email)) - return true; - } - return false; -} - /** * Replaces the_content() with paginated content (if is used in the post) * diff --git a/inc/users.php b/inc/users.php index b6eb10a78..d338a95c5 100644 --- a/inc/users.php +++ b/inc/users.php @@ -5,7 +5,7 @@ * Remove old contact methods (yahoo, aol and jabber) * Add new ones (twitter, facebook, linkedin) * - * @since 1.0 + * @since 0.1 */ function largo_contactmethods( $contactmethods ) { @@ -32,12 +32,20 @@ function largo_contactmethods( $contactmethods ) { } add_filter( 'user_contactmethods', 'largo_contactmethods' ); +// clean and validate fb and twitter usernames when user profiles are updated +add_action('edit_user_profile_update', 'clean_user_twitter_username'); +add_action('personal_options_update', 'clean_user_twitter_username'); +add_action( 'user_profile_update_errors', 'validate_twitter_username', 10, 3); + +add_action( 'edit_user_profile_update', 'clean_user_fb_username' ); +add_action( 'personal_options_update', 'clean_user_fb_username' ); +add_action( 'user_profile_update_errors', 'validate_fb_username', 10, 3); /** * Same deal, but for guest authors in the Co-Authors Plus plugin * @TODO: figure out if there's a way to remove fields as we do for regular users above * - * @since 1.0 + * @since 0.1 */ function largo_filter_guest_author_fields( $fields_to_return, $groups ) { @@ -78,9 +86,9 @@ function largo_filter_guest_author_fields( $fields_to_return, $groups ) { /** * In a multisite network, allow site admins to edit user profiles - * props: http://thereforei.am/2011/03/15/how-to-allow-administrators-to-edit-users-in-a-wordpress-network/ + * @link http://thereforei.am/2011/03/15/how-to-allow-administrators-to-edit-users-in-a-wordpress-network/ * - * @since 1.0 + * @since 0.3 */ function largo_admin_users_caps( $caps, $cap, $user_id, $args ){ @@ -113,6 +121,8 @@ function largo_admin_users_caps( $caps, $cap, $user_id, $args ){ /** * Checks that both the editing user and the user being edited are * members of the blog and prevents the super admin being edited. + * + * @since 0.3 */ function largo_edit_permission_check() { global $current_user, $profileuser; @@ -132,131 +142,6 @@ function largo_edit_permission_check() { } add_filter( 'admin_head', 'largo_edit_permission_check', 1, 4 ); - -/** - * Cleans a Twitter url or an @username to the bare username when the user is edited - * - * Edits $_POST directly because there's no other way to save the corrected username - * from this callback. The action hooks this is used for run before edit_user in - * wp-admin/user-edit.php, which overwrites the user's contact methods. edit_user - * reads from $_POST. - * - * @param object $user_id the WP_User object being edited - * @param array $_POST - * @since 0.4 - * @uses largo_twitter_url_to_username - * @link http://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile_update - * @link http://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update - */ - -add_action('edit_user_profile_update', 'clean_user_twitter_username'); -add_action('personal_options_update', 'clean_user_twitter_username'); - -function clean_user_twitter_username($user_id) { - - if ( current_user_can('edit_user', $user_id) ) { - $twitter = largo_twitter_url_to_username( $_POST['twitter'] ); - if ( preg_match( '/[^a-zA-Z0-9_]/', $twitter ) ) { - // it's not a valid twitter username, because it uses an invalid character - $twitter = ""; - } - update_user_meta($user_id, 'twitter_link', $twitter); - if ( get_user_meta($user_id, 'twitter_link', true) != $twitter ) { - wp_die(__('An error occurred.')); - } - $_POST['twitter'] = $twitter; - } -} - -/** - * Checks that the Twitter URL is composed of valid characters [a-zA-Z0-9_] and - * causes an error if there is not. - * - * @param $errors the error object - * @param bool $update whether this is a user update - * @param object $user a WP_User object - * @uses largo_twitter_url_to_username - * @link http://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors - * @since 0.4 - */ - -add_action( 'user_profile_update_errors', 'validate_twitter_username', 10, 3); - -function validate_twitter_username( $errors, $update, $user ) { - - if ( isset( $_POST["twitter"] ) ) { - $tw_suspect = trim( $_POST["twitter"] ); - if( ! empty( $tw_suspect ) ) { - if ( preg_match( '/[^a-zA-Z0-9_]/', largo_twitter_url_to_username( $tw_suspect ) ) ) { - // it's not a valid twitter username, because it uses an invalid character - $errors->add('twitter_username', '' . $tw_suspect . '' . __('is an invalid Twitter username.') . '

' . '

' . __('Twitter usernames only use the uppercase and lowercase alphabet letters (a-z A-Z), the Arabic numbers (0-9), and underscores (_).') ); - } - } - } -} - -/** - * Cleans a Facebook url to the bare username or id when the user is edited - * - * Edits $_POST directly because there's no other way to save the corrected username - * from this callback. The action hooks this is used for run before edit_user in - * wp-admin/user-edit.php, which overwrites the user's contact methods. edit_user - * reads from $_POST. - * - * @param object $user_id the WP_User object being edited - * @param array $_POST - * @since 0.4 - * @uses largo_fb_url_to_username - * @link http://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile_update - * @link http://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update - */ -add_action('edit_user_profile_update', 'clean_user_fb_username'); -add_action('personal_options_update', 'clean_user_fb_username'); - -function clean_user_fb_username($user_id) { - - if ( current_user_can('edit_user', $user_id) ) { - $fb = largo_fb_url_to_username( $_POST['fb'] ); - if ( preg_match( '/[^a-zA-Z0-9\.\-]/', $fb ) ) { - // it's not a valid Facebook username, because it uses an invalid character - $fb = ""; - } - update_user_meta($user_id, 'fb', $fb); - if ( get_user_meta($user_id, 'fb', true) != $fb ) { - wp_die(__('An error occurred.')); - } - $_POST['fb'] = $fb; - } -} -/** - * Checks that the Facebook URL submitted is valid and the user is followable and causes an error if not - * - * @uses largo_fb_url_to_username - * @uses largo_fb_user_is_followable - * @param $errors the error object - * @param bool $update whether this is a user update - * @param object $user a WP_User object - * @link http://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors - * @since 0.4 - */ -add_action( 'user_profile_update_errors', 'validate_fb_username', 10, 3); - -function validate_fb_username( $errors, $update, $user ) { - - if ( isset( $_POST["fb"] ) ) { - $fb_suspect = trim( $_POST["fb"] ); - if( ! empty( $fb_suspect ) ) { - $fb_user = largo_fb_url_to_username( $fb_suspect ); - if ( preg_match( '/[^a-zA-Z0-9\.\-]/', $fb_user ) ) { - // it's not a valid Facebook username, because it uses an invalid character - $errors->add('fb_username', '' . $fb_suspect . ' ' . __('is an invalid Facebook username.') . '

' . '

' . __('Facebook usernames only use the uppercase and lowercase alphabet letters (a-z A-Z), the Arabic numbers (0-9), periods (.) and dashes (-)') ); - } - if ( ! largo_fb_user_is_followable( $fb_user ) ) { - $errors->add('fb_username',' ' . $fb_suspect . ' ' . __('does not allow followers on Facebook.') . '

' . '

' . __('Follow these instructions to allow others to follow you.') ); - } - } - } -} /** * Get users based on a role. Defaults to fetching all authors for the current blog. * @@ -369,44 +254,33 @@ function largo_render_staff_list_shortcode($atts=array()) { function more_profile_info($user) { $show_email = get_user_meta( $user->ID, "show_email", true ); $hide = get_user_meta( $user->ID, "hide", true ); - $emeritus = get_user_meta( $user->ID, "emeritus", true ); - $honorary = get_user_meta( $user->ID, "honorary", true ); - ?> -

More profile information

+

- + - + - + @@ -432,9 +306,7 @@ function save_more_profile_info($user_id) { $values = wp_parse_args($_POST, array( 'show_email' => 'on', - 'hide' => 'off', - 'emeritus' => 'off', - 'honorary' => 'off' + 'hide' => 'off' )); extract($values); @@ -442,8 +314,6 @@ function save_more_profile_info($user_id) { update_user_meta($user_id, 'job_title', $job_title); update_user_meta($user_id, 'show_email', $show_email); update_user_meta($user_id, 'hide', $hide); - update_user_meta($user_id, 'emeritus', $emeritus); - update_user_meta($user_id, 'honorary', $honorary); } add_action('personal_options_update', 'save_more_profile_info'); add_action('edit_user_profile_update', 'save_more_profile_info'); diff --git a/tests/inc/test-avatars.php b/tests/inc/test-avatars.php index cea29a5ed..3f8a283d5 100644 --- a/tests/inc/test-avatars.php +++ b/tests/inc/test-avatars.php @@ -90,6 +90,10 @@ function test_largo_get_avatar_filter() { function test_largo_get_avatar_src() { $this->markTestIncomplete('This test has not been implemented yet.'); } + + function test_largo_has_avatar() { + $this->markTestIncomplete("This test has not yet been implemented."); + } } diff --git a/tests/inc/test-helpers.php b/tests/inc/test-helpers.php index 0a4371bcd..88872f83e 100644 --- a/tests/inc/test-helpers.php +++ b/tests/inc/test-helpers.php @@ -94,6 +94,14 @@ function test_largo_fb_user_is_followable() { $this->assertFalse($result, "Either https://www.facebook.com/%22Aardvarks+lurk%2C+OK%3F%22 is user that exists and allows follows (not at all likely), or the Facebook follow button iframe HTML structure has changed and largo_fb_url_to_username no longer operates predictably."); unset($result); } + + function test_clean_user_fb_username() { + $this->markTestIncomplete('This test has not been implemented yet.'); + } + + function test_validate_fb_username() { + $this->markTestIncomplete('This test has not been implemented yet.'); + } function test_largo_twitter_url_to_username() { /** @@ -165,6 +173,15 @@ function test_largo_twitter_url_to_username() { $this->assertEquals("", $result); unset($result); } + + function test_clean_user_twitter_username() { + $this->markTestIncomplete('This test has not been implemented yet.');; + } + + function test_validate_twitter_username() { + $this->markTestIncomplete('This test has not been implemented yet.'); + } + function test_largo_youtube_url_to_ID() { $this->markTestIncomplete('This test has not been implemented yet.'); } diff --git a/tests/inc/test-post-tags.php b/tests/inc/test-post-tags.php index ace48b060..53a215c4f 100644 --- a/tests/inc/test-post-tags.php +++ b/tests/inc/test-post-tags.php @@ -113,10 +113,6 @@ function test_largo_post_social_links() { } - function test_largo_has_avatar() { - $this->markTestIncomplete("This test has not yet been implemented."); - } - function test_my_queryvars() { $this->markTestIncomplete("This test has not yet been implemented."); } diff --git a/tests/inc/test-users.php b/tests/inc/test-users.php index afd938e09..735fa00d8 100644 --- a/tests/inc/test-users.php +++ b/tests/inc/test-users.php @@ -27,22 +27,6 @@ function test_largo_edit_permission_check() { $this->markTestIncomplete('This test has not been implemented yet.'); } - function test_clean_user_twitter_username() { - $this->markTestIncomplete('This test has not been implemented yet.');; - } - - function test_validate_twitter_username() { - $this->markTestIncomplete('This test has not been implemented yet.'); - } - - function test_clean_user_fb_username() { - $this->markTestIncomplete('This test has not been implemented yet.'); - } - - function test_validate_fb_username() { - $this->markTestIncomplete('This test has not been implemented yet.'); - } - function test_largo_get_user_list() { /** * With no arguments, `largo_get_user_list` should get a list of all authors for the current blog; @@ -203,7 +187,7 @@ function test_more_profile_info() { // Four inputs should be present and four should be checked or "on" after running // `save_more_profile_info`, because $this->_more_profile_info sets show_email to true. - $this->assertEquals(4, substr_count($output, 'checked'), "Not all inputs that should have been checked were."); + $this->assertEquals(2, substr_count($output, 'checked'), "Not all inputs that should have been checked were."); // There should be one job_title input and it should be populated with the value set by // `save_more_profile_info`. @@ -218,8 +202,6 @@ function test_save_more_profile_info() { save_more_profile_info($user_id); $this->assertEquals($hide, get_user_meta($user_id, "hide", true)); - $this->assertEquals($emeritus, get_user_meta($user_id, "emeritus", true)); - $this->assertEquals($honorary, get_user_meta($user_id, "honorary", true)); $this->assertEquals($job_title, get_user_meta($user_id, "job_title", true)); } @@ -230,8 +212,6 @@ function _more_profile_info_setup() { $args = array( 'job_title' => 'Test Job Title', 'hide' => 'on', - 'emeritus' => 'on', - 'honorary' => 'on', 'show_email' => 'on' ); @@ -239,8 +219,6 @@ function _more_profile_info_setup() { $_POST = array_merge($_POST, array( 'hide' => $hide, - 'emeritus' => $emeritus, - 'honorary' => $honorary, 'job_title' => $job_title, 'show_email' => $show_email )); From 28a2bd1df1fbdeb3e7b56b97a3342025ef173ea7 Mon Sep 17 00:00:00 2001 From: aschweigert Date: Thu, 7 Jan 2016 11:26:35 -0500 Subject: [PATCH 2/2] update the staff roster widget to remove honorary/emeritus options --- inc/widgets/largo-staff.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/inc/widgets/largo-staff.php b/inc/widgets/largo-staff.php index 0a62e58aa..63ce0c663 100644 --- a/inc/widgets/largo-staff.php +++ b/inc/widgets/largo-staff.php @@ -45,20 +45,8 @@ public function widget( $args, $instance ) { $avatar = get_avatar($user->ID, '65'); $author_url = get_author_posts_url($user->ID); - $emeritus = get_user_meta($user->ID, 'emeritus', true); - $honorary = get_user_meta($user->ID, 'honorary', true); $job_title = get_user_meta($user->ID, 'job_title', true); - if ($honorary == 'on' && $emeritus == 'on') { - $job_title = $job_title . ' (Honorary, emeritus)'; - } else { - if ($honorary == 'on') - $job_title = $job_title . ' (Honorary)'; - - if ($emeritus =='on') - $job_title = $job_title . ' (Emeritus)'; - } - $user_posts_link = ''; if (count_user_posts($user->ID) > 0) $user_posts_link = "{$user->first_name}'s posts";

- Please enter your job title. +
checked /> -
+
checked /> -
- - checked /> -
- - checked /> - +