View source
<?php
define('WP_BLOG_DEFAULT_CTYPE', 'wp_blog');
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 – 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>';
$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;
}
}
function wp_blog_views_api() {
return array(
'api' => 3,
);
}
function wp_blog_menu() {
$items = array();
$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;
}
function wp_blog_menu_alter(&$menu) {
$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;
$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',
);
}
function wp_blog_theme() {
return array(
'wp_blog_archive' => array(
'render element' => 'element',
'file' => 'wp_blog.theme.inc',
),
);
}
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,
),
);
}
function wp_blog_node_view($node, $view_mode, $langcode) {
if (!($view_mode == 'full' && $node->type == WP_BLOG_DEFAULT_CTYPE)) {
return;
}
$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);
}
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;
}
function wp_blog_block_info() {
$blocks['date_navigation'] = array(
'info' => t('Blog date navigation'),
'cache' => DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_USER,
);
return $blocks;
}
function wp_blog_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'date_navigation':
$archive = _wp_blog_get_blog_archive_tree();
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(
'wp_blog' => array(
'node/add',
array(),
),
),
),
);
}
return $block;
}
function _wp_blog_get_blog_archive_tree() {
$tree = array();
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) {
$date = (object) getdate($post->created);
if (!array_key_exists($date->year, $tree)) {
$tree[$date->year] = array(
'count' => 0,
'text' => $date->year,
'url' => 'blog/' . $date->year,
'months' => array(),
);
}
if (!array_key_exists($date->mon, $tree[$date->year]['months'])) {
$tree[$date->year]['months'][$date->mon] = array(
'count' => 0,
'text' => t($date->month),
'url' => $tree[$date->year]['url'] . '/' . str_pad($date->mon, 2, '0', STR_PAD_LEFT),
'days' => array(),
);
}
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,
'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;
}
function _wp_blog__get_blog_posts() {
$blog_posts =& drupal_static(__FUNCTION__, NULL);
if (is_null($blog_posts)) {
$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;
}