You are here

fb_social_like.module in Facebook social plugins integration 6

File

modules/fb_social_like/fb_social_like.module
View source
<?php

/**
 * Implementation of hook init
 * @todo Implement block settings form
 * @todo og:image (http://developers.facebook.com/docs/reference/plugins/like)
 * 
 */
function fb_social_like_init() {
  if (variable_get('fb_social_like_opengraph_tags', 1)) {
    $object = menu_get_object();
    if (is_object($object) && fb_social_like_type($object->type)) {

      //add fbml header meta information
      $data = '<meta property="og:title" content="' . check_plain($object->title) . '"/>' . "\n";
      $data .= '<meta property="og:site_name" content="' . variable_get('site_name', '') . '"/>' . "\n";

      // @todo maybe look for images/imagefields uploaded/attached to the node ?
      // $data .= '<meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>';
      drupal_set_html_head($data);
    }
  }
}

/**
 * Implementation of hook_menu
 */
function fb_social_like_menu() {
  $items = array();
  $items['admin/settings/fb/social/like'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'like',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fb_social_like_settings_form',
    ),
    'file' => 'fb_social_like.admin.inc',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * Implementation of hook_fb_social_event_subscribe
 */
function fb_social_like_fb_social_event_subscribe() {

  // integration with google analytics
  if (module_exists('googleanalytics')) {
    global $user;
    $id = variable_get('googleanalytics_account', '');
    if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
      return array(
        'FB.Event.subscribe("edge.create", function(href, widget) {',
        '_gaq.push(["_trackEvent", "Facebook like", "Drupal", href]);',
        '});',
      );
    }
  }
}

/**
 * Implementation of hook_content_extra_fields.
 */
function fb_social_like_content_extra_fields($type_name) {
  if (fb_social_like_type($type_name) && variable_get('fb_social_like_location', 0)) {
    $extras['fb_social_like_widget'] = array(
      'label' => t('Facebook socials like widget'),
      'description' => t('The facebook like pluging widget'),
      'weight' => -10,
    );
    return $extras;
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function fb_social_like_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      if ($node->status && fb_social_like_type($node->type) && variable_get('fb_social_like_location', 0)) {
        if ($a4 || !$a3 || variable_get('fb_social_like_display_teasers', 1) && $a3) {
          $output = theme('fb_social_like_widget', fb_social_url('node/' . $node->nid));
          $weight = module_exists('content') ? content_extra_field_weight($node->type, 'fb_social_like_widget') : -10;
          $node->content['fb_social_like_widget'] = array(
            '#weight' => $weight,
            '#value' => $output,
          );
        }
      }
      break;
  }
}

/**
 * Implementation of hook_block
 */
function fb_social_like_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks['like'] = array(
        'info' => t('fb like'),
      );
      return $blocks;
    case 'view':
      switch ($delta) {
        case 'like':
          $url = fb_social_url($_GET['q']);
          return array(
            'subject' => t(''),
            'content' => theme('fb_social_like_widget', $url),
          );
      }
      break;
  }
}

/**
 * Implementation of hook_theme
 */
function fb_social_like_theme() {
  return array(
    'fb_social_like_widget' => array(
      'arguments' => array(
        'url' => NULL,
      ),
    ),
    'fb_social_like_block_like_view' => array(),
  );
}

/**
 * returns the fbml content of the widget
 */
function theme_fb_social_like_widget($url) {
  global $_fb_script_init;
  $_fb_script_init = TRUE;
  $attrs = array(
    'href' => $url,
    'send' => variable_get('fb_social_like_send_button', 0) ? 'true' : 'false',
    'layout' => variable_get('fb_social_like_layout_style', 'button_count'),
    'show_faces' => variable_get('fb_social_like_show_faces', 0) ? 'true' : 'false',
    'width' => variable_get('fb_social_like_width', 350),
    'action' => variable_get('fb_social_like_verb', 'like'),
    'font' => variable_get('fb_social_like_font', 'verdana'),
    'colorscheme' => variable_get('fb_social_like_color', 'dark'),
  );
  $attrs = drupal_attributes($attrs);
  return '<div class="fb-social-like-widget"><fb:like ' . $attrs . '></fb:like></div>';
}

/**
 * Implementation of hook_link
 */
function fb_social_like_link($type, $object, $teaser = FALSE) {

  //only nodes are supported
  if ('node' != $type) {
    return;
  }

  // dont like unpublished nodes
  if (!$object->status) {
    return;
  }

  // if no widget on teasers return
  if ($teaser && !variable_get('fb_social_like_display_teasers', 1)) {
    return;
  }

  // widget is not to be displayed in the node links. return
  if (variable_get('fb_social_like_location', 0)) {
    return;
  }

  // good to go
  $links = array();
  if ($type == 'node' && fb_social_like_type($object->type)) {
    $links['fb_social_like'] = array(
      'title' => theme('fb_social_like_widget', fb_social_url('node/' . $object->nid)),
      'html' => TRUE,
    );
  }
  return $links;
}

/**
 * helper function for testing wether $type 
 * is selected to be a fb_social_like type
 */
function fb_social_like_type($type) {
  $fb_like_types = variable_get('fb_social_like_node_types', array());
  return isset($fb_like_types[$type]) && $fb_like_types[$type] ? TRUE : FALSE;
}

/**
 * Implementation of hook_views_api().
 */
function fb_social_like_views_api() {
  return array(
    'api' => '2.0',
  );
}

Functions

Namesort descending Description
fb_social_like_block Implementation of hook_block
fb_social_like_content_extra_fields Implementation of hook_content_extra_fields.
fb_social_like_fb_social_event_subscribe Implementation of hook_fb_social_event_subscribe
fb_social_like_init Implementation of hook init @todo Implement block settings form @todo og:image (http://developers.facebook.com/docs/reference/plugins/like)
fb_social_like_link Implementation of hook_link
fb_social_like_menu Implementation of hook_menu
fb_social_like_nodeapi Implementation of hook_nodeapi().
fb_social_like_theme Implementation of hook_theme
fb_social_like_type helper function for testing wether $type is selected to be a fb_social_like type
fb_social_like_views_api Implementation of hook_views_api().
theme_fb_social_like_widget returns the fbml content of the widget