<?php 
/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;
global $current_user;

$sign_success = isset($_GET['success']) ? $_GET['success'] : 0; 

$sign_without_login = isset($campoal_options['conikal_sign_petition_without_login_field']) ? $campoal_options['conikal_sign_petition_without_login_field'] : 0;
$action_after_sign = isset($campoal_options['conikal_action_after_sign_field']) ? $campoal_options['conikal_action_after_sign_field'] : 'modal';

$post_type = get_post_type($post->ID);
if ($post_type != 'petition') {
    $petition_id = get_post_meta($post->ID, $post_type . '_post_id', true);
} else {
    $petition_id = $post->ID;
}

// Get metadata of petition
$title = get_the_title($petition_id);
$link = get_permalink($petition_id);
$status = get_post_meta($petition_id, 'petition_status', true);
$confirmation_page = get_post_meta($petition_id, 'petition_confirmation_page', true);

// Get supporters data
$sign = get_user_meta($current_user->ID, 'petition_sign', true);
$sign = $sign != '' ? $sign : array();

if(is_user_logged_in()) {
    if ($action_after_sign == 'modal' && (in_array($petition_id, $sign) === false || $sign_success != 0) && (int)$status == 0) { 
        get_template_part('templates/sign-share/sign-successful-modal'); 
    }
} elseif ($sign_without_login == 1) {
    if ($action_after_sign == 'modal' && (int)$status == 0) {
        get_template_part('templates/sign-share/sign-successful-modal'); 
    }
}

if ($action_after_sign == 'redirect' && (int)$status == '0') {
    // Check custom confirmation page for petition are available, if not so will use default confirm page in settings.
    if (!empty($confirmation_page)) {
        $sign_success_page = $confirmation_page;
    } else {
        $sign_success_page = isset($campoal_options['conikal_page_sign_successful_field']) ? $campoal_options['conikal_page_sign_successful_field'] : 0;
    }

    // Get url of confirmation page and add argument
    $sign_success_page = get_permalink($sign_success_page);
    $sign_success_page = add_query_arg(array('petition_id' => $petition_id), $sign_success_page); 
    ?>

    <input type="hidden" id="sign-successful-url<?php echo esc_attr($petition_id) ?>" value="<?php echo esc_url($sign_success_page ? $sign_success_page : '') ?>">

<?php } ?>