<?php
/**
 * Includes: Shortcodes
 */
if (!defined('ABSPATH')) {
    die('Sorry, you cannot access this page.');
}

/**
 * Register shortcodes
 */
add_action('init', 'chama_register_shortcodes');
function chama_register_shortcodes()
{
    add_shortcode('chama_login_page', 'chama_create_login_page');
    add_shortcode('chama_registration_page', 'chama_create_registration_page');
    add_shortcode('chama_edit_account_page', 'chama_create_edit_account_page');
    add_shortcode('chama_edit_password_page', 'chama_create_edit_password_page');
    add_shortcode('chama_forgot_password_page', 'chama_create_forgot_password_page');
    add_shortcode('chama_reset_password_page', 'chama_create_reset_password_page');
    add_shortcode('chama_verify_email_page', 'chama_create_verify_email_page');
    add_shortcode('chama_account_page', 'chama_create_account_page');
    add_shortcode('chama_hub_page', 'chama_create_hub_page');
    add_shortcode('chama_donate_page', 'chama_create_donate_page');
    add_shortcode('chama_membership_page', 'chama_create_membership_page');
    add_shortcode('chama_crowdfunding_page', 'chama_create_crowdfunding_page');
    add_shortcode('chama_commissions_page', 'chama_create_commissions_page');
    add_shortcode('chama_payment_page', 'chama_create_payment_page');
    add_shortcode('chama_payment_confirmation_page', 'chama_create_payment_confirmation_page');
    add_shortcode('chama_subscription_page', 'chama_create_subscription_page');
    add_shortcode('chama_subscription_confirmation_page', 'chama_create_subscription_confirmation_page');
    add_shortcode('chama_newsletter_sign_up_form', 'chama_newsletter_sign_up_form_shortcode');
    add_shortcode('chama_newsletter_sign_up_confirmation_page', 'chama_create_newsletter_sign_up_confirmation_page');
    add_shortcode('chama_newsletter_opt_out_confirmation_page', 'chama_create_newsletter_opt_out_confirmation_page');
}
//create login page
function chama_create_login_page($atts)
{
    $validate_with_recaptcha =  CHAMA_RECAPTCHA_SITE_KEY && CHAMA_RECAPTCHA_SECRET_KEY;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    if (!is_user_logged_in()) {
        wp_enqueue_script('chama-jquery-validate');
        wp_enqueue_script('chama-form-functions');
        if($validate_with_recaptcha){
            wp_enqueue_script('chama-google-api-recaptcha');
            wp_enqueue_script('chama-recaptcha');
        }
     
       
        
        $output = $output . $templates->get_template_part('content', 'login');

    }
    else{
        $output = $output . $templates->get_template_part('content', 'loggedin');
    }
    return ob_get_clean();

    // Output needs to be return
    return $output;
}
//create registration page
function chama_create_registration_page($atts)
{
    $validate_with_recaptcha =  CHAMA_RECAPTCHA_SITE_KEY && CHAMA_RECAPTCHA_SECRET_KEY;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    if (!is_user_logged_in()) {
        wp_enqueue_script('chama-jquery-validate');
        wp_enqueue_script('chama-password-validation-functions');  
        wp_enqueue_script( 'password-strength-meter' );   
        if($validate_with_recaptcha){
            wp_enqueue_script('chama-google-api-recaptcha');
            wp_enqueue_script('chama-recaptcha');
        }
        
        $output = $output . $templates->get_template_part('content', 'registration');

    }
    else{
        $output = $output . $templates->get_template_part('content', 'loggedin');
    }
    return ob_get_clean();

    // Output needs to be return
    return $output;
}
//create registration page
function chama_create_edit_account_page($atts)
{

    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    if (is_user_logged_in()) {
        wp_enqueue_style('chama-font-awesome');
        wp_enqueue_script('chama-jquery-validate');
        wp_enqueue_script('chama-form-functions');
       
        $output = $output . $templates->get_template_part('content', 'accountheader');
        $output = $output . $templates->get_template_part('content', 'editaccount');

    }
    else{
        $output = $output . $templates->get_template_part('content', 'login');
    }
    return ob_get_clean();

    // Output needs to be return
    return $output;
}
//create registration page
function chama_create_edit_password_page($atts)
{

    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    if (is_user_logged_in()) {
        wp_enqueue_style('chama-font-awesome');
        wp_enqueue_script('chama-jquery-validate');
        wp_enqueue_script('chama-password-validation-functions');  
        wp_enqueue_script( 'password-strength-meter' );   
       
        $output = $output . $templates->get_template_part('content', 'accountheader');
        $output = $output . $templates->get_template_part('content', 'editpassword');

    }
    else{
        $output = $output . $templates->get_template_part('content', 'login');
    }
    return ob_get_clean();

    // Output needs to be return
    return $output;
}
//create forgot password page
function chama_create_forgot_password_page($atts)
{
    $validate_with_recaptcha =  CHAMA_RECAPTCHA_SITE_KEY && CHAMA_RECAPTCHA_SECRET_KEY;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-jquery-validate');
    wp_enqueue_script('chama-form-functions');  
    if($validate_with_recaptcha){
        wp_enqueue_script('chama-google-api-recaptcha');
        wp_enqueue_script('chama-recaptcha');
    }   
    
    $output = $output . $templates->get_template_part('content', 'forgotpassword');
    return ob_get_clean();

    // Output needs to be return
    return $output;
}
//create forgot password page
function chama_create_reset_password_page($atts)
{

    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-jquery-validate');
    wp_enqueue_script('chama-password-validation-functions');  
    wp_enqueue_script( 'password-strength-meter' );   
   
    
    $output = $output . $templates->get_template_part('content', 'resetpassword');
    return ob_get_clean();

    // Output needs to be return
    return $output;
}

function chama_create_verify_email_page(){
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    $output = $output . $templates->get_template_part('content', 'verifyemail');
    return ob_get_clean();

    // Output needs to be return
    return $output;
}

function chama_create_account_page(){
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_style('chama-micromodal');
    wp_enqueue_script('chama-micromodal');  
    wp_enqueue_script('chama-account');
    
    $output = $output . $templates->get_template_part('content', 'account');
    return ob_get_clean();

    // Output needs to be return
    return $output;
}

function chama_create_hub_page(){
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-tabs'); 
    wp_enqueue_script('chama-share-button');   
    wp_enqueue_script('chama-jquery-validate');
    wp_enqueue_script('chama-form-functions'); 
    wp_enqueue_style('chama-toocheke-style');
    wp_enqueue_script('chama-toocheke-script'); 
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'hub');
    $output = $output . $templates->get_template_part('content', 'footer');
    
    return ob_get_clean();

    // Output needs 
}
function chama_create_donate_page(){
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-tabs'); 
    wp_enqueue_script('chama-share-button');
    wp_enqueue_script('chama-jquery-validate');
    wp_enqueue_script('chama-form-functions');  
    wp_enqueue_style('chama-toocheke-style');
    wp_enqueue_script('chama-toocheke-script'); 
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'donation');
    $output = $output . $templates->get_template_part('content', 'footer');
    
    return ob_get_clean();

    // Output needs 
}
function chama_create_membership_page(){
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');  
    wp_enqueue_script('chama-share-button');
    wp_enqueue_style('chama-toocheke-style');
    wp_enqueue_script('chama-toocheke-script'); 
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'membership');
    $output = $output . $templates->get_template_part('content', 'footer');
    
    return ob_get_clean();

    // Output needs 
}
function chama_create_crowdfunding_page(){
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-share-button');
    wp_enqueue_script('chama-crowdfunding'); 
    wp_enqueue_script('chama-panels');
    wp_enqueue_script('chama-accordion');
    wp_enqueue_style('chama-toocheke-style');
    wp_enqueue_script('chama-toocheke-script'); 
    
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        

    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'crowdfunding');
    $output = $output . $templates->get_template_part('content', 'footer');
    
    return ob_get_clean();

    // Output needs 
}
function chama_create_commissions_page(){
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-share-button');
    wp_enqueue_style('chama-featherlight-min');
    wp_enqueue_style('chama-featherlight-gallery-min');
    wp_enqueue_script('chama-featherlight-min');  
    wp_enqueue_script('chama-featherlight-gallery-min');  
    wp_enqueue_script('chama-gallery'); 
    wp_enqueue_script('chama-commissions');  
    wp_enqueue_script('chama-jquery-validate');
    wp_enqueue_script('chama-form-functions');  
    wp_enqueue_style('chama-toocheke-style');
    wp_enqueue_script('chama-toocheke-script'); 
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'commissions');
    $output = $output . $templates->get_template_part('content', 'footer');
    
    return ob_get_clean();

    // Output needs 
}

function chama_create_payment_page(){
    $validate_with_recaptcha =  CHAMA_RECAPTCHA_SITE_KEY && CHAMA_RECAPTCHA_SECRET_KEY;
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-share-button');
    wp_enqueue_script('chama-jquery-validate');
    wp_enqueue_script('chama-stripe');
    wp_enqueue_script('chama-payment');  
    wp_enqueue_style('chama-toocheke-style');
    wp_enqueue_script('chama-toocheke-script'); 
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
    if($validate_with_recaptcha){
        wp_enqueue_script('chama-google-api-recaptcha');
        wp_enqueue_script('chama-recaptcha');
    } 
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'payment');
    $output = $output . $templates->get_template_part('content', 'footer');
    
    return ob_get_clean();
}
function chama_create_payment_confirmation_page(){
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-share-button');
    wp_enqueue_style('chama-toocheke-style');
    wp_enqueue_script('chama-toocheke-script'); 
   
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
   
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'paymentconfirmation');
    $output = $output . $templates->get_template_part('content', 'footer');
    
    return ob_get_clean();
}
function chama_create_subscription_page(){
    $validate_with_recaptcha =  CHAMA_RECAPTCHA_SITE_KEY && CHAMA_RECAPTCHA_SECRET_KEY;
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-share-button');
    wp_enqueue_script('chama-jquery-validate');
    wp_enqueue_script('chama-stripe');
    wp_enqueue_script('chama-subscription');  
    wp_enqueue_style('chama-toocheke-style');
    wp_enqueue_script('chama-toocheke-script'); 
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
    if($validate_with_recaptcha){
        wp_enqueue_script('chama-google-api-recaptcha');
        wp_enqueue_script('chama-recaptcha');
    } 
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'subscription');
    $output = $output . $templates->get_template_part('content', 'footer');
    
    return ob_get_clean();
}
function chama_create_subscription_confirmation_page(){
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
    wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-share-button');
    wp_enqueue_style('chama-toocheke-style');
    wp_enqueue_script('chama-toocheke-script'); 
   
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
   
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'subscriptionconfirmation');
    $output = $output . $templates->get_template_part('content', 'footer');
    
    return ob_get_clean();
}

// Shortcode to display newsletter signup form
function chama_newsletter_sign_up_form_shortcode()
{
    $validate_with_recaptcha = CHAMA_RECAPTCHA_SITE_KEY && CHAMA_RECAPTCHA_SECRET_KEY;
    $disable_input = false;
    if (is_user_logged_in()) {
        $user_id = get_current_user_id();
        $is_opted_in = get_user_meta($user_id, 'newsletter_optin', true);
        $disable_input   = !empty($is_opted_in);
    }
    ob_start();
    wp_enqueue_style('chama-template');
    wp_enqueue_style('chama-newsletter-style');
    if (! is_user_logged_in()) {
        wp_enqueue_script('chama-jquery-validate');
        wp_enqueue_script('chama-form-functions');
        if ($validate_with_recaptcha) {
            wp_enqueue_script('chama-google-api-recaptcha');
            wp_enqueue_script('chama-recaptcha');
        }
    }
    $header_text       = ( $val = get_option('chama_newsletter_form_header_text') ) ? $val : 'Join the Newsletter';
    $sub_header_text   = ( $val = get_option('chama_newsletter_form_sub_header_text') ) ? $val : 'Subscribe to our newsletter get the latest news';
    $email_placeholder = ( $val = get_option('chama_newsletter_email_field_label') ) ? $val : 'Email address';

?>
<div class="chama-newsletter-form-wrapper">
    <form class="chama-newsletter-form" method="post">
            	<?php
// show any error messages after form submission
chama_show_error_messages();?>

        <h2><?php echo esc_html($header_text); ?></h2>
        <p><?php echo esc_html($sub_header_text); ?></p>
            <?php wp_nonce_field('chama_newsletter_signup', 'chama_newsletter_nonce'); ?>
        <input type="hidden" name="chama_action" value="process-newsletter-signup"/>
        <div class="chama-newsletter-form-input-wrapper">
        <input
            id="chama_newsletter_email"
            name="chama_newsletter_email"
            placeholder="<?php echo esc_attr($email_placeholder); ?>"
            required
            type="email"
            <?php echo $disable_input ? 'disabled' : ''; ?>
        >      
        <input type="submit" value="Subscribe"  <?php echo $disable_input ? 'disabled' : ''; ?>>
        </div>
          <?php if ($validate_with_recaptcha): ?>
						<div class="g-recaptcha"
						data-sitekey="<?php echo esc_attr(CHAMA_RECAPTCHA_SITE_KEY); ?>"
						data-badge="inline" data-size="invisible"
						data-callback="setResponse"></div>
						<input type="hidden" id="chama-captcha-response" name="chama-captcha-response" />

					<?php endif;?>



    </form>
</div>
<?php
return ob_get_clean();
}
function chama_create_newsletter_sign_up_confirmation_page(){
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
     wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-share-button');
   
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
   
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'newsletterconfirmation');
    $output = $output . $templates->get_template_part('content', 'footer');
  
    
    return ob_get_clean();
}

function chama_create_newsletter_opt_out_confirmation_page(){
    $has_header = get_option('chama_header') ? true : false;
    $output = '';
    $templates = new Chama_Template_Loader;
    ob_start();
     wp_enqueue_style('chama-font-awesome');
    wp_enqueue_style('chama-dynamic-style');
    wp_enqueue_style('chama-template');
    wp_enqueue_script('chama-share-button');
   
    if(!$has_header){
        wp_enqueue_style('chama-no-header');
    }        
   
    $output = $output . $templates->get_template_part('content', 'header');
    $output = $output . $templates->get_template_part('content', 'newsletteroptoutconfirmation');
    $output = $output . $templates->get_template_part('content', 'footer');
  
    
    return ob_get_clean();
}
