google_authorship.module in Google Authorship 7.2
Same filename and directory in other branches
A module to display Google+ profile pictures of node authors in Google search results.
This module adds a field to the user entity for a link the the user's Google+ profile. Using that field, if it is filled, this module alters the By line of a node's full content display to link to the Google+ profile. Finally, by setting "rel='author'" Google will recognize and display an authors profile picture in its search results.
File
google_authorship.moduleView source
<?php
/**
* @file
* A module to display Google+ profile pictures of node authors in Google
* search results.
*
* This module adds a field to the user entity for a link the the user's
* Google+ profile. Using that field, if it is filled, this module alters the
* By line of a node's full content display to link to the Google+ profile.
* Finally, by setting "rel='author'" Google will recognize and display an
* authors profile picture in its search results.
*/
// The string to prefix the ID to make up the full URL.
define('GOOGLE_AUTHORSHIP_PREFIX', 'http://plus.google.com/');
/**
* Implement hook_menu().
*
* Adds a configuration page.
*/
function google_authorship_menu() {
$items['admin/config/search/google_authorship'] = array(
'title' => 'Google Authorship',
'description' => 'Configure different settings for the Google Authorship module.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'google_authorship_admin',
),
'file' => 'includes/google_authorship.admin.inc',
'access arguments' => array(
'administer google authorship',
),
);
return $items;
}
/**
* Implements hook_permission().
*
* Adds permissions.
*/
function google_authorship_permission() {
return array(
'administer google authorship' => array(
'title' => t('Administer Google Authorship'),
'description' => t('Perform administration tasks for the Google Authorship module.'),
),
);
}
/**
* Implements template_preprocess_username().
*
* Alters the 'name' variable to be a link to a Google+ profile and sets the
* "rel='author'" parameter on the generate URL.
*/
function google_authorship_preprocess_username(&$variables) {
// Checks if Metatag is being used instead of the default behavior
if (variable_get('google_authorship_use_metatag')) {
return;
}
$node = menu_get_object('node');
$bundles = variable_get('google_authorship_bundles', array());
// Checks if the loaded page is a node page. Checks that authorship information should be added for the node content type
if (empty($node) || !in_array($node->type, $bundles)) {
return;
}
// Checks whether author is a comment author.
$is_not_comment = !isset($variables['account']->comment_body);
// Checks to see if the author's Google+ ID is set, and if so, sets a variable
// for the id.
$is_id_set = $google_id = google_authorship_get_google_id($variables['uid']);
// Checks conditions set above and if it passes, alters the link path of the
// author.
if ($node && $is_not_comment && $is_id_set) {
if (variable_get('google_authorship_use_head')) {
$element = array(
'#tag' => 'link',
'#attributes' => array(
'href' => GOOGLE_AUTHORSHIP_PREFIX . $google_id,
'rel' => 'author',
),
);
if (variable_get('google_authorship_use_user')) {
$element['#attributes']['href'] = $GLOBALS['base_url'] . '/' . $variables['link_path'];
}
drupal_add_html_head($element, 'google_authorship_head_link');
}
else {
$variables['link_path'] = GOOGLE_AUTHORSHIP_PREFIX . $google_id . '?rel=author';
}
}
}
/**
* Implements hook_user_view().
*
* Add rel="me" to user page if the the link to author's user page as bio option has been enabled.
*/
function google_authorship_user_view($account, $view, $langcode) {
if ($view == 'full' && ($google_id = google_authorship_get_google_id($account->uid))) {
$element = array(
'#tag' => 'link',
'#attributes' => array(
'href' => GOOGLE_AUTHORSHIP_PREFIX . $google_id,
'rel' => 'me',
),
);
drupal_add_html_head($element, 'google_authorship_head_link');
}
}
/**
* Implements google_authorship_get_google_id().
*
* Returns a string containing the Google+ ID for a specified UID
*
* @param:
* $uid: The user id of the node author
*
* @return:
* A string with the Google+ ID of the node author, if it has been set or
* NULL if it hasn't
*/
function google_authorship_get_google_id($uid) {
// Loads the author's user account.
$account = user_load($uid);
$disp_roles = variable_get('google_authorship_disp_for_roles');
$auth_roles = $account->roles;
$auth_has_role = FALSE;
// Checks that authorship information should be added for the author's roles
if (!empty($disp_roles)) {
foreach (array_keys($disp_roles) as $role) {
if (in_array($role, array_keys($auth_roles))) {
$auth_has_role = TRUE;
break;
}
}
}
else {
$auth_has_role = TRUE;
}
if (!$auth_has_role) {
return NULL;
}
// Checks if Google+ ID field is set for the user and creates array for items
// in field if so.
$items = field_get_items('user', $account, 'field_google_plus_id');
if ($items) {
// Get the Google+ ID for the items set above.
$google_id = $items[0]['safe_value'];
}
else {
// If the Google+ ID field was empty, set $google_id to NULL.
$google_id = NULL;
}
// Return the Google+ ID if it existed and was a valid length,
// otherwise this will return NULL.
return $google_id;
}
/**
* Implements hook_form_alter().
*
* Adds additional validation to the user profile edit form.
*/
function google_authorship_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'google_authorship_form_validate';
}
/**
* Implements hook_form_validate().
*
* Verifies that the Google+ ID entered is a 21 digit numeric string.
*/
function google_authorship_form_validate(&$form, &$form_state) {
// If the user input data into field_google_plus_id.
if (!empty($form_state['values']['field_google_plus_id'][LANGUAGE_NONE][0]['value'])) {
// Get the form input for Google+ ID.
$google_id = $form_state['values']['field_google_plus_id'][LANGUAGE_NONE][0]['value'];
// If the string is not a 21 digit numeric string, throw error.
if (!preg_match('(^[0-9]{21}$)', $google_id)) {
// Return for resubmission and display error.
form_error($form, t('The Google+ ID should be 21 numeric digits.'));
}
}
}
/**
* Implements hook_metatag_config_default_alter().
*
* Automatically assign the google_authorship_url to the 'author' meta tag.
*/
function google_authorship_metatag_config_default_alter(&$config) {
if (isset($config['node'])) {
$config['node']->config['author']['value'] = GOOGLE_AUTHORSHIP_PREFIX . '[node:author:field_google_plus_id]';
}
}
/**
* Implement hook_form_FORM_ID_alter().
*
* Hides the Google+ ID from the user register form if that option has been set on the module's configuration page.
*/
function google_authorship_form_user_register_form_alter(&$form, &$form_state, $form_id) {
if (variable_get('google_authorship_registration_disp')) {
unset($form['field_google_plus_id']);
}
}
Functions
Name![]() |
Description |
---|---|
google_authorship_form_user_profile_form_alter | Implements hook_form_alter(). |
google_authorship_form_user_register_form_alter | Implement hook_form_FORM_ID_alter(). |
google_authorship_form_validate | Implements hook_form_validate(). |
google_authorship_get_google_id | Implements google_authorship_get_google_id(). |
google_authorship_menu | Implement hook_menu(). |
google_authorship_metatag_config_default_alter | Implements hook_metatag_config_default_alter(). |
google_authorship_permission | Implements hook_permission(). |
google_authorship_preprocess_username | Implements template_preprocess_username(). |
google_authorship_user_view | Implements hook_user_view(). |
Constants
Name![]() |
Description |
---|---|
GOOGLE_AUTHORSHIP_PREFIX |