You are here

wp_blog.module in WP Blog - a WordPress-style blogging module. 7

WP Blog provides a content-type, taxonomy vocabulary, views and various features to mimic a WordPress-style blog.

File

wp_blog.module
View source
<?php

/**
 * @file
 * WP Blog provides a content-type, taxonomy vocabulary, views and various
 * features to mimic a WordPress-style blog.
 */

// The default system name for the blog-post content type.  This can be changed
// through the admin UI.
define('WP_BLOG_DEFAULT_CTYPE', 'wp_blog');

/**
 * Implements hook_help().
 */
function wp_blog_help($path, $arg) {
  switch ($path) {
    case 'admin/help#wp_blog':
      $output = '';
      $output .= t('The WP blog module provides some of the basic features similar to those found on WordPress:');
      $output .= '<ul>';
      $output .= '<li>' . t('A content type.') . '</li>';
      $output .= '<li>' . t('The ability to tag posts with keywords.') . '</li>';
      $output .= '<li>' . t('A set of pages to browse the blog archive, or browse by year, month or day &ndash; for example: !example_urls.', array(
        '!example_urls' => "/blog<br />\n/blog/2010<br />\n/blog/2010/02<br />\n/blog/2010/02/20",
      )) . '</li>';
      $output .= '<li>' . t('A navigation menu which gives links to the years/months where content is published, and a count of the number of blog posts for each year/month.') . '</li>';
      $output .= '</ul>';
      $output .= t('New blog posts can be added at <a href="@url">@url</a>.', array(
        '@url' => url('node/add/wp-blog'),
      ));
      $output .= '<h2>' . t('Recommended modules') . '</h2>';
      $output .= '<ul>';

      // The linkback module is in development, to add support for pingbacks and trackbacks.
      // $output .= '<li>' . t('The <a href="@linkback_module_url">linkback module</a> can enable <strong>pingbacks</strong> and <strong>trackbacks</strong> to be sent and received by your blog posts.', array('@linkback_module_url' => 'http://drupal.org/project/linkback')) . '</li>';
      $output .= '<li>' . t('The <a href="@mollom_module_url">mollom module</a> provides a spam moderation tool, to prevent spam comments from being added to your site.', array(
        '@mollom_module_url' => 'http://drupal.org/project/mollom',
      )) . '</li>';
      $output .= '</ul>';
      return $output;
  }
}

/**
 * Implements hook_views_api().
 */
function wp_blog_views_api() {
  return array(
    'api' => 3,
  );
}

/**
 * Implements hook_menu().
 */
function wp_blog_menu() {
  $items = array();

  // Add a menu-item at blog/add, to provide an ACTION link for all pages
  // beginning blog/. The link simply redirects to node/add/wp-blog.
  $items['blog/add'] = array(
    'title' => 'Add WP blog post',
    'page callback' => 'drupal_goto',
    'page arguments' => array(
      'node/add/wp-blog',
    ),
    'access callback' => 'node_access',
    'access arguments' => array(
      'create',
      'wp_blog',
    ),
    'type' => MENU_LOCAL_ACTION,
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function wp_blog_menu_alter(&$menu) {

  // Hack the menu to allow node/add/wp-blog to be a contextual link.
  $menu['node/add/wp-blog']['_tab'] = TRUE;
  $menu['node/add/wp-blog']['tab_parent'] = 'node/add';
  $menu['node/add/wp-blog']['context'] = MENU_CONTEXT_INLINE;

  // Change the menu-title so the contextual link reads 'Create WP blog post'.
  $menu['node/add/wp-blog']['title'] = 'Create @name';
  $menu['node/add/wp-blog']['title callback'] = 't';
  $menu['node/add/wp-blog']['title arguments'] = array(
    '@name' => 'WP blog post',
  );
}

/**
 * Implements hook_theme().
 */
function wp_blog_theme() {
  return array(
    // wp_blog_archive is the sidebar menu which displays the blog navigation.
    'wp_blog_archive' => array(
      'render element' => 'element',
      'file' => 'wp_blog.theme.inc',
    ),
  );
}

/**
 * Implements hook_node_info().
 */
function wp_blog_node_info() {
  return array(
    WP_BLOG_DEFAULT_CTYPE => array(
      'base' => 'wp_blog',
      'name' => t('WP blog post'),
      'has_title' => TRUE,
      'title_label' => t('Title'),
      'locked' => FALSE,
    ),
  );
}

/**
 * Implements hook_node_view().
 */
function wp_blog_node_view($node, $view_mode, $langcode) {

  // Only act on full page views for WP blog nodes.
  if (!($view_mode == 'full' && $node->type == WP_BLOG_DEFAULT_CTYPE)) {
    return;
  }

  // Set an appropriate menu-trail based on the path alias (which should be
  // blog/yyyy/mm/dd/title).
  $date = (object) getdate($node->created);
  $trail = array(
    array(
      'title' => t('Home'),
      'href' => '<front>',
      'localized_options' => array(),
      'type' => MENU_NORMAL_ITEM,
    ),
    array(
      'title' => t('Blog'),
      'href' => 'blog',
      'localized_options' => array(),
      'type' => MENU_NORMAL_ITEM,
    ),
    array(
      'title' => $date->year,
      'href' => "blog/{$date->year}",
      'localized_options' => array(),
      'type' => MENU_NORMAL_ITEM,
    ),
    array(
      'title' => t($date->month, array(), array(
        'context' => 'Long month name',
      )),
      'href' => "blog/{$date->year}/" . str_pad($date->mon, 2, '0', STR_PAD_LEFT),
      'localized_options' => array(),
      'type' => MENU_NORMAL_ITEM,
    ),
    array(
      'title' => t('@day<sup>@ordinal</sup>', array(
        '@day' => $date->mday,
        '@ordinal' => t(date('S', $node->created), array(), array(
          'context' => 'Date ordinal suffix',
        )),
      )),
      'href' => "blog/{$date->year}/" . str_pad($date->mon, 2, '0', STR_PAD_LEFT) . "/" . str_pad($date->mday, 2, '0', STR_PAD_LEFT),
      'localized_options' => array(
        'html' => TRUE,
      ),
      'type' => MENU_NORMAL_ITEM,
    ),
    array(
      'title' => $node->title,
      'href' => "node/{$node->nid}",
      'localized_options' => array(),
      'type' => MENU_NORMAL_ITEM,
    ),
  );
  menu_set_active_trail($trail);
}

/**
 * Implements hook_form().
 */
function wp_blog_form($node, &$form_state) {
  $type = node_type_get_type($node);
  $form = array();
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#default_value' => !empty($node->title) ? $node->title : '',
    '#required' => TRUE,
    '#weight' => -5,
  );
  return $form;
}

/**
 * Implements hook_block_info().
 */
function wp_blog_block_info() {
  $blocks['date_navigation'] = array(
    'info' => t('Blog date navigation'),
    // The block displays a navigation menu, which may have active menu-items.
    // The active menu-items will change per page, and the accessible blog
    // posts may change per user, depending on permissions.
    'cache' => DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_USER,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function wp_blog_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'date_navigation':
      $archive = _wp_blog_get_blog_archive_tree();

      // If there aren't any blog posts, hide the block.
      if (empty($archive)) {
        return;
      }
      $block = array(
        'subject' => t('Blog Archive'),
        'content' => array(
          'show_all_link' => l(t('All blog posts'), 'blog'),
          'archive' => $archive,
          '#theme' => 'wp_blog_archive',
          '#cache' => DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_USER,
          '#contextual_links' => array(
            // This contextual link depends on the menu_alter above.
            'wp_blog' => array(
              'node/add',
              array(),
            ),
          ),
        ),
      );
  }
  return $block;
}

/**
 * Build a data tree of all published blog posts, with their year, month, and
 * post-counts.
 *
 * Format:
 * [2010] =>
 *           count  => 3
 *           text   => 2010
 *           months =>
 *                     [2] =>
 *                            count => 1
 *                            text  => Februrary
 *                            days  =>
 *                                     [16] =>
 *                                             count => 1
 *                                             text  => Tuesday
 */
function _wp_blog_get_blog_archive_tree() {
  $tree = array();

  // Declare the use of month-names and days to ensure translatioin tools can
  // discover language-use in this module.
  array(
    t('Sunday'),
    t('Monday'),
    t('Tuesday'),
    t('Wednesday'),
    t('Thursday'),
    t('Friday'),
    t('Saturday'),
  );
  array(
    t('January'),
    t('February'),
    t('March'),
    t('April'),
    t('May'),
    t('June'),
    t('July'),
    t('August'),
    t('September'),
    t('October'),
    t('November'),
    t('December'),
  );
  foreach (_wp_blog__get_blog_posts() as $post) {

    // Assume that getdate will return month-names in English.
    $date = (object) getdate($post->created);

    // Add the year.
    if (!array_key_exists($date->year, $tree)) {
      $tree[$date->year] = array(
        'count' => 0,
        // The year is numeric (e.g. 2011) so is not translated.
        'text' => $date->year,
        'url' => 'blog/' . $date->year,
        'months' => array(),
      );
    }

    // Add the month.
    if (!array_key_exists($date->mon, $tree[$date->year]['months'])) {
      $tree[$date->year]['months'][$date->mon] = array(
        'count' => 0,
        // The month-name is a string (January, February, etc) so is translated.
        'text' => t($date->month),
        'url' => $tree[$date->year]['url'] . '/' . str_pad($date->mon, 2, '0', STR_PAD_LEFT),
        'days' => array(),
      );
    }

    // Add the day.
    if (!array_key_exists($date->mday, $tree[$date->year]['months'][$date->mon]['days'])) {
      $tree[$date->year]['months'][$date->mon]['days'][$date->mday] = array(
        'count' => 0,
        // The week-day is numeric (0 - 31) so is not translated.
        'text' => $date->weekday,
        'url' => $tree[$date->year]['months'][$date->mon]['url'] . '/' . str_pad($date->mday, 2, '0', STR_PAD_LEFT),
      );
    }
    $tree[$date->year]['count']++;
    $tree[$date->year]['months'][$date->mon]['count']++;
    $tree[$date->year]['months'][$date->mon]['days'][$date->mday]['count']++;
  }
  return $tree;
}

/**
 * Fetch the nid and created-date of all published blog-posts.
 *
 * @return array
 * An array of objects, each with the property nid and created, sorted by
 * creation-time (new to old).
 */
function _wp_blog__get_blog_posts() {
  $blog_posts =& drupal_static(__FUNCTION__, NULL);
  if (is_null($blog_posts)) {

    // Query for WP blog nodes which the current user has access to.
    $blog_posts = db_select('node', 'n', array(
      'target' => 'slave',
    ))
      ->fields('n', array(
      'nid',
      'created',
    ))
      ->condition("n.type", WP_BLOG_DEFAULT_CTYPE)
      ->condition("n.status", NODE_PUBLISHED)
      ->orderBy("n.created", 'DESC')
      ->addTag('node_access')
      ->execute()
      ->fetchAll();
  }
  return $blog_posts;
}

Functions

Namesort descending Description
wp_blog_block_info Implements hook_block_info().
wp_blog_block_view Implements hook_block_view().
wp_blog_form Implements hook_form().
wp_blog_help Implements hook_help().
wp_blog_menu Implements hook_menu().
wp_blog_menu_alter Implements hook_menu_alter().
wp_blog_node_info Implements hook_node_info().
wp_blog_node_view Implements hook_node_view().
wp_blog_theme Implements hook_theme().
wp_blog_views_api Implements hook_views_api().
_wp_blog_get_blog_archive_tree Build a data tree of all published blog posts, with their year, month, and post-counts.
_wp_blog__get_blog_posts Fetch the nid and created-date of all published blog-posts.

Constants