You are here

function _wp_blog_get_blog_archive_tree in WP Blog - a WordPress-style blogging module. 7

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

1 call to _wp_blog_get_blog_archive_tree()
wp_blog_block_view in ./wp_blog.module
Implements hook_block_view().

File

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

Code

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;
}