I have two php code and want to combine there function into one code (Login to Download)

49 Views Asked by At

I want a function, such that users should download a pdf or access a link after a countdown. Plus Users should be logged in, if they are not logged in they should be redirect to the login page (slug = "/login")

I have two working code for both "Login to Download" Redirection function and the "Count Down" Function.

The Code are :

  1. Login to Download Code :
function um_login_to_download( $atts ) {
  //shortcode attributes
  extract(shortcode_atts(array(
    'url' => '',
    'label' => '',
    'class' => '', // Added 'class' attribute for custom CSS classes
  ), $atts));

  //check if user is logged in
  if ( is_user_logged_in() ) {
    //if logged in, display the download link with the specified label and custom CSS class
    return '<a href="'.$url.'" class="'.$class.'">'.$label.'</a>';
  } else {
    //if not logged in, display a login link with the label "Login to Download" that redirects to Ultimate Member login page and custom CSS class
    $redirect_url = get_permalink();
    return '<a href="/login/?redirect_to='.$redirect_url.'" class="'.$class.'">Login to Download</a>';
  }
}

//add shortcode for login to download
add_shortcode( 'login_to_download', 'um_login_to_download' );


  1. Countdown Code :
// Add this code to your theme's functions.php or a code snippet plugin

function countdown_button_shortcode($atts) {
    $attributes = shortcode_atts(array(
        'minutes' => 5,
        'seconds' => 0,
    ), $atts);

    wp_enqueue_script('jquery');

    ob_start(); ?>

    <div id="countdown-timer">
        <button id="download-button" disabled>Wait <?php echo $attributes['minutes']; ?> min <?php echo $attributes['seconds']; ?> sec to Download the PDF</button>
    </div>

    <script>
        jQuery(document).ready(function($) {
            var targetTime = new Date();
            targetTime.setMinutes(targetTime.getMinutes() + <?php echo $attributes['minutes']; ?>);
            targetTime.setSeconds(targetTime.getSeconds() + <?php echo $attributes['seconds']; ?>);

            var interval = setInterval(function() {
                var currentTime = new Date();
                var timeRemaining = targetTime - currentTime;

                if (timeRemaining <= 0) {
                    clearInterval(interval);
                    $('#download-button').text('Download the PDF').prop('disabled', false);
                } else {
                    var minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
                    var seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
                    $('#download-button').html('Wait ' + minutes + ' min ' + seconds + ' sec to Download the PDF');
                }
            }, 1000);
        });
    </script>

    <?php
    return ob_get_clean();
}

add_shortcode('countdown_button', 'countdown_button_shortcode');

These function works with short code, and the final short code after combining these two code should be like : [login_to_download countdown_button minutes="10" seconds="30" url="https://example.com/download.pdf" label=”Download” class=”download-button”]

where class="download-button" is to style the button

I have tried a lot, but did't get the right solution. recently i have used this :

function um_login_to_download( $atts ) {
  // Shortcode attributes
  extract(shortcode_atts(array(
    'url' => '',
    'label' => '',
    'class' => '', // Added 'class' attribute for custom CSS classes
    'countdown' => 10, // Set the default countdown time (in seconds)
  ), $atts));

  // Check if user is logged in
  if ( is_user_logged_in() ) {
    // If logged in, display the download link with the specified label and custom CSS class
    return '<a href="'.$url.'" class="'.$class.'">'.$label.'</a>';
  } else {
    // If not logged in, display the countdown and JavaScript code
    ob_start();
    ?>
    <div id="countdown-container">
      <button id="countdown-button" class="<?php echo $class; ?>">Wait <span id="countdown"><?php echo $countdown; ?></span> sec to Download</button>
    </div>
    <script>
      // JavaScript countdown logic
      var countdown = <?php echo $countdown; ?>;
      var countdownButton = document.getElementById("countdown-button");
      var countdownSpan = document.getElementById("countdown");

      function updateCountdown() {
        countdown--;
        countdownSpan.innerText = countdown;
        if (countdown <= 0) {
          clearInterval(countdownInterval); // Stop the countdown
          countdownButton.innerHTML = '<a href="<?php echo $url; ?>" class="<?php echo $class; ?>"><?php echo $label; ?></a>';
        }
      }

      var countdownInterval = setInterval(updateCountdown, 1000);
    </script>
    <?php
    return ob_get_clean();
  }
}

// Add shortcode for login to download
add_shortcode( 'login_to_download', 'um_login_to_download' );

But It did't worked.

0

There are 0 best solutions below