<?php

function restrict_to_logged_in( $atts, $content=null ) {
    if(is_user_logged_in()) {
        return do_shortcode($content);
    }
}

function restrict_to_public( $atts, $content=null ) {
    if(!is_user_logged_in()) {
        return do_shortcode($content);
    }
}

function current_username(){
    $user = wp_get_current_user();
    return $user->display_name;
}

add_shortcode('logged-in-only', 'restrict_to_logged_in');
add_shortcode('public-only', 'restrict_to_public');
add_shortcode('current-user', 'current_username');

// Shortcode [draper_projects industry='' number=""]

function draper_projects ( $atts, $content = null) {
    
    // Attributes
    
    $atts = shortcode_atts(
        array(
            'industry' => '',
            'number' => '-1',
        ),
        $atts,
        'draper_projects'
    );
    
    $args = array(
        'post_type' => 'project',
        'posts_per_page' => $atts['number'],
        'post_status' => 'publish',
        'orderby' => 'title',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'industry',
                'value' => $atts['industry'],
                'compare' => '='
            )
        )
    );
    
    $projects_query = new WP_Query($args);
    
    if($projects_query->have_posts()) {    
        
    ob_start(); ?>
    
    <div class="projects">
    
    <?php
    
    while ($projects_query->have_posts()) {
    $projects_query->the_post(); ?>
    <div class="flex_column av_one_third av-animated-generic top-to-bottom flex_column_table_cell av-equal-height-column av-align-top av-zero-column-padding projects-col" style="border-radius:0px; ">
    	<div style="background-image: url(<?php echo get_the_post_thumbnail_url(get_the_ID(),'full'); ?>); " class="projects-col-img">    
    	</div>
    	<section class="av_textblock_section " itemscope="itemscope" itemtype="https://schema.org/CreativeWork">
    		<div class="avia_textblock projects-col-info " itemprop="text">
    			<p><?php the_title(); ?></p>
    		</div>
    	</section>
    	<div class="avia-button-wrap avia-button-center projects-col-btn">
    		<a href="<?php the_permalink(); ?>" class="avia-button avia-icon_select-yes-right-icon avia-color-theme-color avia-size-large avia-position-center ">
    			<span class="avia_iconbox_title">View Project</span>
    			<span class="avia_button_icon avia_button_icon_right" aria-hidden="true" data-av_icon="๎กต" data-av_iconfont="entypo-fontello"></span>
    		</a>
    	</div>
    </div>
    
    <?php } ?>
    
    </div>
    
    <?php
    wp_reset_postdata();
    } else {
        echo "No Projects found";    
    }
    return ob_get_clean();
}

add_shortcode('draper_projects', 'draper_projects');