<?php

//###############################################################################
// Theme support items needed for Unit Tests
//###############################################################################
if( ! function_exists( 'simply_jigoshop_setup') ) {
  function simply_jigoshop_setup() {
    add_theme_support( 'automatic-feed-links' );
    load_theme_textdomain( 'simply-jigoshop', get_template_directory() . '/languages' );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'nav-menus' );
    // Makes theme translation ready

    register_nav_menus( array(
    'categories' => __( 'Category Navigation', 'simply-jigoshop' ),
    'pages' => __( 'Pages Navigation', 'simply-jigoshop' ),
  ) );

    add_editor_style( 'css/style.css' );
  }
}
add_action( 'after_setup_theme', 'simply_jigoshop_setup' );

$simply_jigoshop_default_options = array(
  'jigotheme_logo_path' => get_template_directory_uri() . '/images/jigoshop.png',
  'jigowatt_show_subpages' => 'yes',
  'jigotheme_color' => 'green',
  );

if ( is_admin() ) include_once "includes/theme-config.php";

include_once "includes/theme-enqueue.php";


if ( ! function_exists( 'simply_jigoshop_show_posts_nav' ) ) :
  /**
   * Display navigation to next/previous set of posts when applicable.
   *
   * @return void
   */
  function simply_jigoshop_show_posts_nav() {
    // Don't print empty markup if there's only one page.
    if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
      return;
    }
?>
  <nav class="navigation paging-navigation" role="navigation">
    <h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'simply-jigoshop' ); ?></h1>
    <div class="posts-nav-links">

      <?php if ( get_next_posts_link() ) : ?>
      <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'simply-jigoshop' ) ); ?></div>
      <?php endif; ?>

      <?php if ( get_previous_posts_link() ) : ?>
      <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'simply-jigoshop' ) ); ?></div>
      <?php endif; ?>

    </div><!-- .nav-links -->
  </nav><!-- .navigation -->
  <?php
  }
endif;

//###############################################################################
// Jigoshop Overrides
//###############################################################################

// Adjust jigoshop image sizes

add_filter( 'jigoshop_get_var_shop_large_w', create_function( '', 'return 436;' ) );
add_filter( 'jigoshop_get_var_shop_large_h', create_function( '', 'return 436;' ) );

// Remove sidebar on single shop pages

add_action( 'wp', create_function( "", "if (is_singular(array('product'))) remove_action( 'jigoshop_sidebar', 'jigoshop_get_sidebar', 10);" ) );

// Change columns in related products output to 4

remove_action( 'jigoshop_after_single_product_summary', 'simply_jigoshop_output_related_products', 20 );
add_action( 'jigoshop_after_single_product_summary', 'simply_jigoshop_output_related_products', 20 );

function simply_jigoshop_output_related_products() {
  jigoshop_related_products( 4, 4 ); // 4 products, 4 columns
}

// Change columns in product loop to 3
function simply_jigoshop_loop_columns() {
  return 3;
}
add_filter( 'loop_shop_columns', 'simply_jigoshop_loop_columns' );

// Adjust markup on all Jigoshop pages
remove_action( 'jigoshop_before_main_content', 'jigoshop_output_content_wrapper', 10 );
remove_action( 'jigoshop_after_main_content', 'jigoshop_output_content_wrapper_end', 10 );
function simply_jigoshop_wrapper() {
  echo '<section id="main-content">';
}
function simply_jigoshop_close_wrapper() {
  echo '</section>';
}
add_action( 'jigoshop_before_main_content', 'simply_jigoshop_wrapper', 10 );
add_action( 'jigoshop_after_main_content', 'simply_jigoshop_close_wrapper', 10 );


//###############################################################################
// Add theme sidebars
//###############################################################################

function simply_jigoshop_widgets_init() {
    register_sidebar( array(
      'name' => __( 'Primary Widget Area', 'simply-jigoshop' ),
      'id' => 'primary-widget-area',
      'before_widget' => '<section id="%1$s" class="widget %2$s">',
      'after_widget' => '</section>',
      'before_title' => '<h3 class="widget_title">',
      'after_title' => '</h3>',
    ) );
  register_sidebar( array(
      'name' => __( 'Homepage Widget Area', 'simply-jigoshop' ),
      'id' => 'homepage-widget-area',
      'before_widget' => '<section id="%1$s" class="widget %2$s">',
      'after_widget' => '</section>',
      'before_title' => '<h3 class="widget_title">',
      'after_title' => '</h3>',
    ) );
}
add_action( 'widgets_init', 'simply_jigoshop_widgets_init' );


//###############################################################################
// Comment formatting
//###############################################################################

function simply_jigoshop_theme_comments( $comment, $args, $depth ) {
  $GLOBALS['comment'] = $comment; ?>
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
    <article id="comment-<?php comment_ID(); ?>">
      <div class="comment-author vcard">
        <?php echo get_avatar( $comment, 21 ); ?>
        <?php printf( __( '%s <span class="says">says:</span>', 'simply-jigoshop' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
      </div><!-- .comment-author .vcard -->
      <?php if ( $comment->comment_approved == '0' ) : ?>
        <em><?php _e( 'Your comment is awaiting moderation.', 'simply-jigoshop' ); ?></em>
        <br />
      <?php endif; ?>
      <div class="comment-body"><?php comment_text(); ?>
        <footer class="comment-footer">
          <div class="reply">
            <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
          </div><!-- .reply -->

          <?php
  /* translators: 1: date, 2: time */
  printf( __( '%1$s at %2$s', 'simply-jigoshop' ), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'simply-jigoshop' ), ' ' );
?>

        </footer>
      </div>

    </article><!-- #comment-##  -->
    <!-- </li> is added by wordpress automatically -->
    <?php
}

//###############################################################################
// Custom Widgets
//###############################################################################

class Simply_Jigoshop_Banner extends WP_Widget {

  function __construct() {
    parent::__construct(
      'no-border', // Base ID
      __( 'No Border', 'simply-jigoshop' ), // Name
      array( 'description' => __( 'A simple text widget sans border', 'simply-jigoshop' ), ) // Args
    );
  }

  function form( $instance ) {
    $instance = wp_parse_args( $instance, array(
      'text' => '',
    ) );
?>
  <p>
    <label for="<?php echo $this->get_field_id( 'text' ); ?>">
      <?php _e( 'Text:', 'simply-jigoshop' ); ?>
      <textarea class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo $instance['text']; ?></textarea>
    </label>
  </p>
<?php
  }
  function update( $new_instance, $old_instance ) {
    // processes widget options to be saved
    return $new_instance['jigo_banner_text'] = $new_instance;
  }
  function widget( $args, $instance ) {
    $instance = wp_parse_args( $instance, array(
      'text' => '',
    ) );
?>
    <section class="no-border">
      <?php echo $instance['text']; ?>
    </section>
    <?php
  }
}
function simply_jigoshop_register_widgets() {
  register_widget( 'Simply_Jigoshop_Banner' );
}
add_action( 'widgets_init', 'simply_jigoshop_register_widgets' );

//###############################################################################
// Actions + Filters
//###############################################################################

// Remove links to the extra feeds (e.g. category feeds)
remove_action( 'wp_head', 'feed_links_extra', 3 );
// Remove links to the general feeds (e.g. posts and comments)
remove_action( 'wp_head', 'feed_links', 2 );
// Remove link to the RSD service endpoint, EditURI link
remove_action( 'wp_head', 'rsd_link' );
// Remove link to the Windows Live Writer manifest file
remove_action( 'wp_head', 'wlwmanifest_link' );
// Remove index link
remove_action( 'wp_head', 'index_rel_link' );
// Remove prev link
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
// Remove start link
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
// Display relational links for adjacent posts
remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 );
// Remove XHTML generator showing WP version
remove_action( 'wp_head', 'wp_generator' );

function simply_jigoshop_custom_excerpt( $text ) {
  return str_replace( '[...]', ' <a href="'. get_permalink( $post->ID ) . '" class="more">' . __( 'More&nbsp;&raquo;', 'simply-jigoshop' ) . '</a>', $text );
}
add_filter( 'the_excerpt', 'simply_jigoshop_custom_excerpt' );

// Allow HTML in descriptions
$filters = array( 'pre_term_description', 'pre_link_description', 'pre_link_notes', 'pre_user_description' );
foreach ( $filters as $filter ) {
  remove_filter( $filter, 'wp_filter_kses' );
}

function simply_jigoshop_remove_recent_comments_style() {
  global $wp_widget_factory;
  remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );
}
add_action( 'widgets_init', 'simply_jigoshop_remove_recent_comments_style' );

//################################################################################
// Next + Previous Links in Posts
//################################################################################
if ( ! function_exists( 'simply_jigoshop_post_nav' ) ) :
  function simply_jigoshop_post_nav() {
    // Don't print empty markup if there's nowhere to navigate.
    $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
    $next     = get_adjacent_post( false, '', false );

    if ( ! $next && ! $previous ) {
      return;
    }

?>
  <nav class="post-navigation" role="navigation">
    <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'simply-jigoshop' ); ?></h1>
    <div class="nav-links">
      <?php
    if ( is_attachment() ) :
      previous_post_link( '%link', __( '<span class="meta-nav">Published In</span>%title', 'simply-jigoshop' ) );
    else :
      previous_post_link( '%link', __( '<span class="meta-nav alignleft">Previous Post</span>', 'simply-jigoshop' ) );
    next_post_link( '%link', __( '<span class="meta-nav alignright">Next Post</span>', 'simply-jigoshop' ) );
    endif;
?>
    </div><!-- .nav-links -->
  </nav><!-- .navigation -->
  <?php
  }
endif;

function simply_jigoshop_wp_title( $title, $sep ) {
  global $paged, $page;

  if ( is_feed() )
    return $title;

  // Add the site name.
  $title .= get_bloginfo( 'name' );

  // Add the site description for the home/front page.
  $site_description = get_bloginfo( 'description', 'display' );
  if ( $site_description && ( is_home() || is_front_page() ) )
    $title = "$title $sep $site_description";

  // Add a page number if necessary.
  if ( $paged >= 2 || $page >= 2 )
    $title = "$title $sep " . sprintf( __( 'Page %s', 'simply-jigoshop' ), max( $paged, $page ) );

  return $title;
}
add_filter( 'wp_title', 'simply_jigoshop_wp_title', 10, 2 );

if ( ! isset( $content_width ) ) $content_width = 620;
