Subversion Repositories ZenDark

Rev

Rev 3 | Rev 25 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// Add localisation support
load_theme_textdomain( 'sdt', TEMPLATEPATH . '/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH ."/languages/$locale.php";
if (is_readable($locale_file)) require_once($locale_file);

// Register theme options page
require_once(get_template_directory().'/theme-options.php');

// Add default posts and comments RSS feed links to <head>.
add_theme_support('automatic-feed-links');

// This theme uses wp_nav_menu() in one location.
function sdt_register_nav_menu() {
  register_nav_menu('primary', __('Primary Menu', 'sdt'));
}
add_action('init', 'sdt_register_nav_menu');

// Sets the post excerpt length to 40 words.
function sdt_excerpt_length($length) {
        return 40;
}
add_filter('excerpt_length', 'sdt_excerpt_length');

// Returns a "Continue Reading" link for excerpts
function sdt_continue_reading_link() {
        return '<a href="'.esc_url(get_permalink()).'">'.__('Continue reading ⇾', 'sdt').'</a>';
}

// Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and sdt_continue_reading_link().
function sdt_auto_excerpt_more($more) {
        return '&hellip;<br />'.sdt_continue_reading_link();
}
add_filter('excerpt_more', 'sdt_auto_excerpt_more');

// Display navigation to next/previous pages when applicable
function sdt_content_nav( $nav_id ) {
        global $wp_query;

        if ( $wp_query->max_num_pages > 1 ) : ?>
                <nav id="<?php echo $nav_id; ?>">
                        <div class="nav-next"><?php previous_posts_link( __('⇽ Newer posts', 'sdt')); ?></div>
                        <div class="nav-previous"><?php next_posts_link( __('Older posts ⇾', 'sdt')); ?></div>
                </nav><!-- #<?php echo $nav_id; ?> -->
        <?php endif;
}

// Count authors number (users with writing rights)
function sdt_count_authors(){
  $number = 0;
  $result = count_users();
  foreach($result['avail_roles'] as $role => $count)
    if($role != "subscriber") { $number = $number + $count; }
  return $number;
}

// Register sidebars
function sdt_widgets_init() {

        register_sidebar( array(
                'name' => __( 'Main Sidebar', 'sdt' ),
                'id' => 'sidebar-1',
                'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                'after_widget' => "</aside>",
                'before_title' => '<h3 class="widget-title">',
                'after_title' => '</h3>',
        ) );
}
add_action('widgets_init', 'sdt_widgets_init');

/*
 * Adds two classes to the array of body classes.
 * The first is if the site has only had one author with published posts.
 * The second is if a singular post being displayed
 */

function sdt_body_classes($classes) {
        if (!is_multi_author()) { $classes[] = 'single-author'; }
        if (is_singular() && !is_home()) $classes[] = 'singular';
        return $classes;
}
add_filter('body_class', 'sdt_body_classes');

// Get the first parent of a page
function sdt_get_root_parent($page_id) {
  global $wpdb;
        $parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");
        if ($parent == 0) return $page_id;
        else return sdt_get_root_parent($parent);
}


// Add class to menu <li> for first parent page
add_filter('nav_menu_css_class', 'sdt_special_nav_class', 10, 2);
function sdt_special_nav_class($classes, $item){
  if($item->object_id == sdt_get_root_parent(get_the_ID())){
    $classes[] = "current_page_parent_function";
  }
  return $classes;
}

// This check if a gravatar exists for a user, and if not return false
function validate_gravatar($email) {
  /* first of all, we want to do the check only if it's needed, so
   * we check that gravatars are actually enabled in settings, and
   * that you want to display a blank avatar (no avatar) as default.
   */

  if (get_option('show_avatars') && get_option('avatar_default') == 'blank') {
    // Craft a potential url and test its headers
    $hash = md5($email);
    $uri = 'http://www.gravatar.com/avatar/'.$hash.'?d=404';
    $headers = @get_headers($uri);
    if (!preg_match("|200|", $headers[0])) $has_valid_avatar = false;
    else $has_valid_avatar = true;
  }
  elseif (!get_option('show_avatars')) $has_valid_avatar = false;
  else $has_valid_avatar = true;
        return $has_valid_avatar;
}

/* Function to generate comments, I believe it's
 * necessary if you want paged comments.
 */

 function sdt_comment($comment, $args, $depth) {
        $GLOBALS['comment'] = $comment;

  if ($comment->comment_type != "trackback" && $comment->comment_type != "pingback") : ?>
  <div id="comment-<?php comment_ID() ?>" <?php comment_class(); ?>>
    <header class="comment-header">
      <?php
        if (validate_gravatar(get_comment_author_email()))
          echo '<div class="comment-avatar">'.get_avatar($comment, 40).'</div>';
      ?>
      <div>
        <div class="comment-meta">
          <span class="comment-author"><?php comment_author_link() ?></span> <?php edit_comment_link( __( 'Edit', 'sdt' ), ' &bull; ', '' ); ?><br />
          <?php printf('<span class="comment-datetime"><a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a></span>',
                        esc_url(get_comment_link($comment->comment_ID)),
                        get_comment_time('c'),
                        /* translators: 1: date, 2: time */
                        sprintf(__('%1$s %2$s', 'sdt'), get_comment_date(), get_comment_time())
                ); ?>
        </div>
        <?php comment_reply_link(array_merge($args, array('reply_text' => __( 'reply', 'sdt'), 'login_text' => __( 'login to reply', 'sdt'), 'depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
      </div>
    </header>
    <div class="comment-content">
      <?php if ($comment->comment_approved == '0') : ?>
        <div class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation', 'sdt'); ?></div>
                        <?php endif; ?>
      <?php comment_text(); ?>
    </div>
  </div>
  <?php
  endif;
}

// Function to generate trackback/pingback
function sdt_trackback($comment, $args, $depth) {
        $GLOBALS['comment'] = $comment;

  if ($comment->comment_type == "trackback" || $comment->comment_type == "pingback") : ?>
    <div id="comment-<?php comment_ID() ?>" <?php comment_class(); ?>>
      <span class="comment-author"><?php comment_author_link() ?></span><br />
      <?php comment_excerpt(); ?>
    </div>
  <?php
        endif;
}

// Function to count trackback/pingback
function sdt_trackback_number($comments) {
  $numero_trackback = 0;
        foreach ($comments as $comment) :
    if ($comment->comment_type == "trackback" || $comment->comment_type == "pingback") $numero_trackback++;
  endforeach;
        return $numero_trackback;
}

/* Function to generate both comments and trackbacks/pingbacks together,
 * in case of paged comments. If you find a way to nicely separate comments
 * and pingbacks/trackbacks in paged comments, please tell me.
 */

function sdt_all_comment($comment, $args, $depth) {
        $GLOBALS['comment'] = $comment;

  if ($comment->comment_type != "trackback" && $comment->comment_type != "pingback") : ?>
  <div id="comment-<?php comment_ID() ?>" <?php comment_class(); ?>>
    <header class="comment-header">
      <?php
        if (validate_gravatar(get_comment_author_email()))
          echo '<div class="comment-avatar">'.get_avatar($comment, 40).'</div>';
      ?>
      <div>
        <div class="comment-meta">
          <span class="comment-author"><?php comment_author_link() ?></span> <?php edit_comment_link( __( 'Edit', 'sdt' ), ' &bull; ', '' ); ?><br />
          <?php printf('<span class="comment-datetime"><a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a></span>',
                        esc_url(get_comment_link($comment->comment_ID)),
                        get_comment_time('c'),
                        /* translators: 1: date, 2: time */
                        sprintf(__('%1$s %2$s', 'sdt'), get_comment_date(), get_comment_time())
                ); ?>
        </div>
        <?php comment_reply_link(array_merge($args, array('reply_text' => __( 'reply', 'sdt'), 'login_text' => __( 'login to reply', 'sdt'), 'depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
      </div>
    </header>
    <div class="comment-content">
      <?php if ($comment->comment_approved == '0') : ?>
        <div class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation', 'sdt'); ?></div>
                        <?php endif; ?>
      <?php comment_text(); ?>
    </div>
  </div>
  <?php
  endif;
  if ($comment->comment_type == "trackback" || $comment->comment_type == "pingback") : ?>
    <div id="comment-<?php comment_ID() ?>" <?php comment_class(); ?>>
      <span class="comment-author"><?php comment_author_link() ?></span><br />
      <?php comment_excerpt(); ?>
    </div>
  <?php
        endif;
}
?>