<?php
/**
 * Created by PhpStorm.
 * User: VanIllaSkyPE
 * Date: 24/06/2017
 * Time: 20:32
 */

/***********************************Menus & sidebars************************************************/
function materialize_wordpress_widgets_init() {
    if (function_exists('register_nav_menus')) {
        register_nav_menus(
            array(
                'main' => __( 'Main menu', 'materializewordpress' ),
                'footer' => __( 'Footer menu', 'materializewordpress' )
            )
        );
    }

    if (function_exists('register_sidebar')) {
        register_sidebar( array(
            'name'          => __( 'Footer sidebar 1', 'materializewordpress' ),
            'id'            => 'footer-widgets-1',
            'description'   => __( 'First widgets area of the footer', 'materializewordpress' ),
            'before_widget' => '',
            'after_widget'  => '',
            'before_title'  => '<h5 class="white-text">',
            'after_title'   => '</h5>',
        ));

        register_sidebar( array(
            'name'          => __( 'Footer sidebar 2', 'materializewordpress' ),
            'id'            => 'footer-widgets-2',
            'description'   => __( 'Second widgets area of the footer', 'materializewordpress' ),
            'before_widget' => '',
            'after_widget'  => '',
            'before_title'  => '<h5 class="white-text">',
            'after_title'   => '</h5>',
        ));

        register_sidebar( array(
            'name'          => __( 'Footer sidebar 3', 'materializewordpress' ),
            'id'            => 'footer-widgets-3',
            'description'   => __( 'Third widgets area of the footer', 'materializewordpress' ),
            'before_widget' => '',
            'after_widget'  => '',
            'before_title'  => '<h5 class="white-text">',
            'after_title'   => '</h5>',
        ));

        register_sidebar( array(
            'name'          => __( 'Left sidebar', 'materializewordpress' ),
            'id'            => 'left-sidebar',
            'description'   => __( 'Widgets area for left sidebar', 'materializewordpress' ),
            'before_widget' => '',
            'after_widget'  => '',
            'before_title'  => '<h3 class="white-text">',
            'after_title'   => '</h3>',
        ));

        register_sidebar( array(
            'name'          => __( 'Right sidebar', 'materializewordpress' ),
            'id'            => 'right-sidebar',
            'description'   => __( 'Widgets area for right sidebar', 'materializewordpress' ),
            'before_widget' => '',
            'after_widget'  => '',
            'before_title'  => '<h3 class="white-text">',
            'after_title'   => '</h3>',
        ));
    }
}

add_action( 'widgets_init', 'materialize_wordpress_widgets_init' );

/***********************************Theme support************************************************/
/*
 * Let WordPress manage the document title.
 * By adding theme support, we declare that this theme does not use a
 * hard-coded <title> tag in the document head, and expect WordPress to
 * provide it for us.
 */
add_theme_support( 'title-tag' );

add_theme_support( 'post-thumbnails' );

// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );

/*
 * Switch default core markup for search form, comment form, and comments
 * to output valid HTML5.
 */
add_theme_support('html5', array(
    'search-form',
    'comment-form',
    'comment-list',
    'gallery',
    'caption',
));

function load_translations(){
	load_theme_textdomain( 'materializewordpress', get_template_directory() . '/languages' );
}

add_action( 'after_setup_theme', 'load_translations' );

add_editor_style( array() );

/*************************************Search template***********************************************/

/**
 * Generate custom search form
 *
 * @param string $form Form HTML.
 * @return string Modified form HTML.
 */
function materializewordpress_search_form( $form ) {
    $form = '<form role="search" method="get" id="searchform" class="searchform" action="' . home_url( '/' ) . '" >
    <div class="input-field"><label class="screen-reader-text" for="s">' . __( 'Search ...', 'materializewordpress' ) . '</label>
    <input type="text" value="' . get_search_query() . '" name="s" id="s" class="validate" />
    <input type="submit" class="btn waves-effect waves-light right" id="searchsubmit" value="'. esc_attr__( 'Search', 'default' ) .'" />
    </div>
    </form>';

    return $form;
}
add_filter( 'get_search_form', 'materializewordpress_search_form' );

/***********************************Comments template***********************************************/
/**
 * @return string
 */
function materialize_wordpress_default_image() {
    if (wp_get_attachment_image_src(get_post_thumbnail_id(), 'full')[0] != '') {
        $image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full')[0];
    } else {
        $image = get_theme_mod( 'default_image',  get_template_directory_uri() . '/img/default-parallax-header-image.jpeg');
    }
    return $image;
}

/***********************************Comments template***********************************************/
/**
 * @param $comments
 */
function materializewordpress_comment($comments) {
    global $current_user;
    wp_get_current_user();
    foreach($comments as $comment) {
        $post_id = get_the_ID();
        $comment_id = $comment->comment_ID;

        $author_email = get_comment_author_email($comment_id);

        //get the setting configured in the admin panel under settings discussions "Enable threaded (nested) comments  levels deep"
        $max_depth = get_option('thread_comments_depth');
        //add max_depth to the array and give it the value from above and set the depth to 1
        $default = array(
            'add_below'  => 'comment',
            'respond_id' => 'respond',
            'reply_text' => '<i class="material-icons">reply</i>',
            'login_text' => __('Log in to Reply', 'default'),
            'depth'      => 1,
            'before'     => '',
            'after'      => '',
            'max_depth'  => $max_depth
        );

        ?>

        <li id="comment-<?php echo $comment_id ?>" class="collection-item avatar<?php if ($comment->comment_parent != 0){ echo " comment-response"; } ?>">
            <img src="<?php echo get_avatar_url(get_comment_author_email($comment)); ?>" alt="" class="circle">

            <span class="title">
                <?php if ($comment->comment_parent != 0){ echo '<i class="material-icons">reply</i>'; } ?>
                <a href="<?php echo get_comment_author_url($comment) ?>">
                    <?php echo esc_attr(get_comment_author($comment)); ?>
                </a>
            </span>

            <p>
                <?php comment_text($comment) ?>
            </p>
            <p class="">
                <ul class="comment-footer inline">
                    <li><i class="material-icons">account_box</i> <?php echo esc_attr(get_comment_author($comment)); ?></li>
                    <li><i class="material-icons">date_range</i> <?php comment_date(null, $comment_id); ?> <?php comment_time(''); ?></li>
                    <?php
                    if ($author_email == $current_user->user_email){
                        ?>
                        <li><a href="<?php echo get_edit_comment_link($comment_id) ?>"><i class="material-icons">edit</i></a></li>
                        <?php
                    }
                    ?>
                </ul>
            <?php
             if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
     	                $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
         	                        esc_url( wp_login_url( get_permalink() ) ),
                            $default['login_text']
	                );
	        } else {
                     $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
                         $default['add_below'], $comment->comment_ID, $default['respond_id'], $post_id
	                );

	                $link = sprintf( "<a rel='nofollow' class='comment-reply-link secondary-content' href='%s' onclick='%s' aria-label='%s'>%s</a>",
         	                        esc_url( add_query_arg( 'replytocom', $comment->comment_ID, get_permalink( $post_id ) ) ) . "#" . $default['respond_id'],
	                        $onclick,
	                        esc_attr( sprintf( $default['reply_to_text'], $comment->comment_author ) ),
                            $default['reply_text']
	                );
                 echo $link;
	        }
            ?>
        <?php
    }
}