<?php

function codeneric_phmm_shortcode_client( $atts ){
    $att = shortcode_atts( array(
    'id' => 0
    ), $atts );
    
    $post = get_post();
    $id = isset($att['id']) && $att['id'] !== 0 ? $att['id'] : $post->ID;
    
    
    $content = "";
    
    $user_can_access_post = apply_filters('codeneric/phmm/check_user_permission', $id);
    
    //password required, attention!
    if ( !$user_can_access_post ) {
        $content = $content . get_the_password_form();
        return $content;
    }
    
    else {
        
        // enqueue the scripts
        do_action('codeneric_shortcode_enqueue_scripts', $id);
        
        
        $description = '';
        if(isset($_GET['project'])) {
            $projects =  get_post_meta( $id, "projects", true );
            $project_id = $_GET['project'];
            
            if(isset($projects[$project_id]) && isset($projects[$project_id]['description']))
            $description = '<p class="phmm-project-description">' . $projects[$project_id]['description'] . '</p>';
            
        }
        
        
        
        $content = $content . $description . '<div id="codeneric-phmm-public-container"></div>';
        return $content;
    }
    
    
    
}
add_shortcode( 'cc_phmm_client', 'codeneric_phmm_shortcode_client' );




// append login failed to url
add_action('wp_login_failed', 'my_front_end_login_fail');
function my_front_end_login_fail($username){
    // Get the reffering page, where did the post submission come from?
    $referrer = $_SERVER['HTTP_REFERER'];
    
    // if there's a valid referrer, and it's not the default log-in screen
    if(!empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')){
        // let's append some information (login=failed) to the URL for the theme to use
        wp_redirect(add_query_arg ('login','failed',$referrer));
        exit;
    }
}




function codeneric_phmm_shortcode_portal( $atts ){
    
    $content = "";
    
    if(!is_user_logged_in()){
        if(isset($_GET['login']) && $_GET['login'] == 'failed' ) {
            $content = $content . '<div class="codeneric-phmm-login-warn" style="text-align: center; padding: 0.5em 1em; margin-bottom: 1em; border: 2px solid rgba(255, 0, 0, 0.3); font-weight: normal;">'.__( '<strong>ERROR</strong>: Invalid username, email address or incorrect password.' ).'</div>';
            $content = $content . wp_login_form( array("echo" => false) );
            return $content;
        }
        // show form
        $content = $content . wp_login_form( array("echo" => false) );
        return $content;
    }
    
    // is admin
    if ( current_user_can( 'manage_options' )  ) {
        $content = $content . "<h3>You seem to be the Administrator, please open this page in another browser or logout and revisit it to see the login form.</h3>";
        return $content;
    }
    
    $current_user = wp_get_current_user();
    $post_id = get_user_meta($current_user->ID, 'phmm_post_id', true);
    $id = $post_id;
    
    do_action('codeneric_shortcode_enqueue_scripts', $id); // enqueue the script
    
    
    $description = '';
    if(isset($_GET['project'])) {
        $projects =  get_post_meta( $id, "projects", true );
        $project_id = $_GET['project'];
        
        if(isset($projects[$project_id]) && isset($projects[$project_id]['description']))
        $description = '<p class="phmm-project-description">' . $projects[$project_id]['description'] . '</p>';
        
    }
    
    $content = $content . $description .'<div id="codeneric-phmm-public-container"></div>';
    
    return $content;
    
}
add_shortcode( 'cc_phmm_portal', 'codeneric_phmm_shortcode_portal' );