0

I have been working on a WordPress plugin and have used the do_shortcode() to implement some functionality within a form...a file upload. The upload processing comes from a 3rd party plugin and It works brilliantly.

BUT now I have a problem. I want to assign the name of the file being uploaded to a record in the database, within my plugin. However the identifiers to that db record is not presently available within the callback hook, provided by the 3rd party.

This used to work, thought the use of cookies, but since the upgrade to a newer PHP version, the process fails as of now.

Below is a subset of the code I am using to demo the issue

add_shortcode('MMD_SHOW_ENTRY_FORM', 'mmd_vimeo_videoupload');
function mmd_vimeo_videoupload($atts)
{
    
$OrderId             = sanitize_text_field(stripslashes($_GET['or']));
$BillingEmail        = sanitize_text_field(stripslashes($_GET['id']));
$FormType            = sanitize_text_field(stripslashes($_GET['type']));
$RideCount           = sanitize_text_field(stripslashes($_GET['cnt']));
$ListId              = sanitize_text_field(stripslashes($_GET['show']));


setcookie("VHSEntryListId", $ListId, time() + ( 3600 * 3));  <<< THROWS ERROR
$_COOKIE['VHSEntryListId'] = $ListId;
   
setcookie("VHSOrderId", $OrderId, time() + (3600 * 3)); <<< THROWS ERROR
$_COOKIE['VHSOrderId'] = $OrderId ;
   
setcookie("VHSRideCount", $RideCount, time() + (3600 * 3)); <<< THROWS ERROR
$_COOKIE['VHSRideCount'] = $RideCount;
  

?>
<form enctype="multipart/form-data" id="RiderEntryForm" name="RiderEntryForm" action="" method="post" autocomplete="off">    

<div class="inner-wrap">
do_shortcode("[google_upload]"); 
</div>

<DIV style="width:100%; "><input type="submit" style="float:left:" id="MMD_SEND_VIDEO"  name="MMD_SEND_VIDEO" value="SUBMIT YOUR RIDE"/>
      <input type="button" style="" id="MMD_CANCEL_ENTRY"  name="MMD_CANCEL_ENTRY" value="GO BACK" onclick="history.back()"/>
      </DIV>

</form>

<?php

}



add_action('useyourdrive_upload_post_process', 'mmd_store_upload_information', 10, 2); // Start watching the upload
function mmd_store_upload_information($uploaded_entries, $processor)
{

 $ListId  = $_COOKIE["VHSEntryListId"]; <<<< NO LONGER WORK IN PHP 8.SetCookie throws error
 $OrderId = $_COOKIE["VHSOrderId"];     <<<< NO LONGER WORK IN PHP 8 SetCookie throws error
 $RideCount = $_COOKIE["VHSRideCount"]; <<<< NO LONGER WORK IN PHP 8 SetCookie throws error


// retrieve file name from uploaded_entries 

$entries = (array)$uploaded_entries;    
 foreach ($entries as $cached_node) 
       {
        $Node = (array)$cached_node; 
        if(empty($Node['name']))
          continue;
            
    
  $UploadedFileName = $Node['name'];
  $DownloadLink     = $Node['direct_download_link'];

  mmd_StoreUploadedFileName( $ListId, $OrderId, $RideCount, $UploadedFileName, $DownloadLink );
 break;
 }  

}

WHAT I HAVE TRIED:

  1. setting Cookies, but since it is not in the header, it is throwing warnings and not storing the cookie value. (Seen above).

  2. Global variables are out, since I could have multiple users entering data at the same time.

  3. Reading the URL parameters, from the page that the shortcode is set on...the callback does not know what that is and the $_Get() fails.

So I wonder, if there is anyway to "add parameters" to the 3rd party callback function? Passing these variables to be recieved when the upload is finished from the other plugin.

Thoughts, Ideas??? Presently I am out of ideas. Need help!

1 Answer 1

0

I tried...but ended up going a completely different direction, as there appears to be no viable solution. I ended Storing in a temporary database entry. Does potentially have race conditions, if two people are using the same account. But for those who are interested, here was my solution:

I am using the very effective plugin call UserYourDrive as the file upload interface. They were kind enough to provide callbacks that proved to be of assistance in this problem.

I am using the current user, as they must be logged in to perform this task. Current user is the binding element.

Starting at the shortcode, enter the :

mmd_InsertFileUploadCoreData($current_user->ID, $ListId, $RideCount, $OrderId);   // straight 1st time load

But you have to made sure that this is not a post response, thus, if nothing is set for posting, only then do you call the initial core data, with the elements you need later.

Once the upload is complete, the call back is recieved. This is where you store the file name of the uploaded file.

mmd_InsertFileUploadName($current_user->ID, $UploadedFileName);   // Don't save it. We only have the userid to find it.

When the user clicks on the submit button, containing the rest of the form data, only then do you clear the temporary stored data.

 mmd_DeleteFileUploadData($current_user->ID);

It does have a potential race-condition hole, but that should be a rare occurance. Code set:

add_action('useyourdrive_upload_post_process', 'mmd_store_upload_information', 10, 2); // Start watching the upload
function mmd_store_upload_information($uploaded_entries, $processor)
{ 
 $ListId        = NULL;
 $OrderId       = NULL;
 $RideCount     = NULL;
 $current_user = wp_get_current_user();
 $Record        = mmd_FindFileUploadData($current_user->ID);
 if(!empty($Record))
     {
     $ListId        = $Record['ListId'];
     $OrderId       = $Record['OrderId'];
     $RideCount     = $Record['RideId'];
     $DBId          = $Record['id'];
     unset($Record);                           // Clear the record
     mmd_DebugLog('READ  UserId: ' . $current_user->ID .  ' List Id: ' . $ListId . '  OrderId: ' . $OrderId  . 'Ride Count:' .  $RideCount);
     }
  else
   return;   // Nothing here, so bail out.   
 
 if($ListId == NULL || $ListId == "")
    return;
 
 if($OrderId == NULL || $OrderId == "")
    return;
    
 if($RideCount == NULL || $RideCount == "")
    return; 
  
 $entries = (array)$uploaded_entries;   
 foreach ($entries as $cached_node) 
       {
        $Node = (array)$cached_node; 
        if(empty($Node['name']))
          continue;
                
        $UploadedFileName = $Node['name'];
        $DownloadLink     = $Node['direct_download_link'];  
        mmd_InsertFileUploadName($current_user->ID, $UploadedFileName);   // Don't save it. We only have the userid to find it.
        mmd_StoreUploadedFileName( $ListId, $OrderId, $RideCount, $UploadedFileName, $DownloadLink );
        break;
    }   
}


function mmd_InsertFileUploadName($UserId, $FileName)
{
global $wpdb;   
$Table_Name = $wpdb->prefix.'mmd_temp_fileupload_data'; 
$sql_query = $wpdb->prepare( "UPDATE $Table_Name SET FileName = %s WHERE UserId = %d", $FileName, $UserId);
$wpdb->query( $sql_query );     

}


function mmd_InsertFileUploadName($UserId, $FileName)
{
global $wpdb;   
$Table_Name = $wpdb->prefix.'mmd_temp_fileupload_data'; 
$sql_query = $wpdb->prepare( "UPDATE $Table_Name SET FileName = %s WHERE UserId = %d", $FileName, $UserId);
$wpdb->query( $sql_query ); 


}




function mmd_FindFileUploadData($UserId)
{
global $wpdb;   
$Table_Name = $wpdb->prefix.'mmd_temp_fileupload_data'; 
$sql_query  = $wpdb->prepare("SELECT * FROM $Table_Name WHERE UserId=%d", $UserId);
 $Record    = $wpdb->get_row($sql_query, ARRAY_A);

return $Record;     
}


function mmd_DeleteFileUploadData($UserId)
{
global $wpdb;   
$Table_Name = $wpdb->prefix.'mmd_temp_fileupload_data'; 
$sql_query  = $wpdb->prepare("DELETE FROM $Table_Name WHERE UserId=%d", $UserId);
$wpdb->query( $sql_query );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.