Support » Plugin: Tip Jar WP » Can i link the shortcode to the file download location?

  • Resolved parismolver

    (@parismolver)


    Hi, I plan on taking photos of people and using the tip jar to convert free photography into sales.

    my only issue is that there is a lot of manual labor involved in creating a new tip jar shortcode for each photograph i take. At the moment mywebsite.com/id/12345678 has an associated zip 12345678.zip and i would like to create the shortcode [tipjarwp id=”1″ dl=????/12345678.zip] so that for my many many webpages each tipjar knows which file it needs to deliver.

    I was planning on emailing the correct zip for each sale but I’m trying to avoid manual labor with a $0 payment system.

    Alternatively, can we discuss editing the file-delivery.php?

    $deliverable_file_path = get_attached_file( $file_download_data[‘attachment_id’] );

    could possibly become

    $deliverable_file_path = ‘mywebsite.com/assets/’ + pagetitle + ‘.zip’ ?

    I know that it’s a pretty hack way of doing things but if it’s possible to extract the /12345678 out of the page url and use that to reconstruct the file path to deliver this could be a relatively easy fix for somebody who actually knows php.

    • This topic was modified 11 months, 1 week ago by parismolver. Reason: brainwave
Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter parismolver

    (@parismolver)

    Edit. Absolutely stuck here.

    I have edited the php in such a way that the page will tell me the download link to be delivered.

    $url = “https://”;
    // Append the host(domain name, ip) to the URL.
    $url.= $_SERVER[‘HTTP_HOST’];

    // Append the requested resource location to the URL
    $url.= $_SERVER[‘REQUEST_URI’];

    $split_str = str_split($url, 29);
    $double_split_str = str_split($split_str[1], 10);
    $unique_id = $double_split_str[0];

    //echo “zip_dl:”.$unique_id;
    //echo”zip_dl->>>”.”https://www.parisphoto.nz/wp-content/uploads/”.$unique_id.”.zip”;
    $deliverable_file_path = “https://www.parisphoto.nz/wp-content/uploads/”.$unique_id.”.zip”;

    Is used outside of the function tip_jar_wp_deliver_attached_file() method, however,

    echo $deliverable_file_path

    returns nothing when placed in the method and even when the download fails, trying to observe via the upcomming leaves blank space where the download url should be, indicating that the global is probably not being passed into the function

    // If no attachment file path was found…
    if ( ! $deliverable_file_path ) {
    wp_die( esc_textarea( __( “The download link “.$deliverable_file_path.’ is an invalid file download.’, ‘tip-jar-wp’ ) ) );
    }

    I’m feeling so close with getting my hack working. It’s not elegant but to be able to dynamically link downloads to this plugin, either via a shortcode entry point or another means would be fantastic, especially if you’re running a podcast site for example where each post includes a unique file.

    Plugin Author Tip Jar WP

    (@tipjarwp)

    Hey there,

    I’d love to help you get this working as it’s a great use case.

    Just to help me understand the main issue, is it that you are delivering your images via a URL that isn’t an attachment in your WordPress?

    Plugin Author Tip Jar WP

    (@tipjarwp)

    Just to clarify, I am wanting to make sure you’ve already considered using a separate Tip Jar shortcode for each photo, and attaching each photo to that tip jar from your media library. If so, is there a reason that doesn’t work for your use-case?

    Plugin Author Tip Jar WP

    (@tipjarwp)

    Oh my apologies. I re-read your original question and I see what you are asking. You already have the image attached to each post. I will think about this for a bit and get back to you.

    Plugin Author Tip Jar WP

    (@tipjarwp)

    I think what might be happening is that you are passing a file URL to the $deliverable_file_path variable, when you really need a file PATH.

    Note the difference between a URL and a PATH here. A path refers to an internal file, not publicly accessible, but only accessible by your server internally. So when reconstructing the file path, make sure it refers to the PATH, and not the URL.

    Also I did some digging, and you should be able to do this without needing to modify any PHP code. Rather, you can use the filter like this in your theme’s functions.php file, or a custom plugin:

    function swap_in_my_file( $file, $attachment_id ) {
    	return '/your_server_file_root/whatever_file_path_you_want.zip'
    }
    add_filter( 'get_attached_file', 'swap_in_my_file', 10, 2 );

    Let me know if that helps at all. I think we can get this working for you in a nice and elegant way.

    • This reply was modified 11 months, 1 week ago by Tip Jar WP.
    Plugin Author Tip Jar WP

    (@tipjarwp)

    A more complete (but untested) example:

    function swap_in_my_file( $file, $attachment_id ) {
    	$url = 'https://&#8221';
    	// Append the host(domain name, ip) to the URL.
    	$url .= $_SERVER[‘HTTP_HOST’];
    
    	// Append the requested resource location to the URL
    	$url .= $_SERVER[‘REQUEST_URI’];
    
    	$split_str        = str_split($url, 29);
    	$double_split_str = str_split($split_str[1], 10);
    	$unique_id        = $double_split_str[0];
    
    	// If a unique ID was not found, don't modify this attachment's path.
    	if( empty( $unique_id ) ) {
    		return $file;
    	}
    
    	$your_custom_file_path = '/your_server_file_root/wp-content/uploads/' . $unique_id . '.zip';
    
    	return $your_custom_file_path;
    }
    add_filter( 'get_attached_file', 'swap_in_my_file', 10, 2 );
    Plugin Author Tip Jar WP

    (@tipjarwp)

    Actually there’s a bit more to it I think, as the download doesn’t actually come from the page where the tip jar is embedded (via shortcode/block/etc).

    I will do a bit more digging to find a solution here and let you know.

    Thread Starter parismolver

    (@parismolver)

    😮 😮 😮

    Thank you for getting back to me during my slumber. will do more digging and get back to you on any fixed, findings and successes I have whilst I work on it today. I see how I’ve been using an html link instead of a file location as you pointed out. Thanks for correcting me.

    I’ve worked with c, java and python but php is still rather foreign. I think the filter idea is a good one but I think we’ve both run into the same problem. There’s no way to tell the download button what file to fetch. I used a php plugin that creates a shortcode which is pretty mindblowing. How it’s setup at the moment is that the default file delivery is a txt file warning the customer that their download has failed.

    I was diving into the download transient file and i think this script could work, but the location it takes me to is a 404 page ../wp-content/uploads/.zip

    	
    	// Create the secret file download URL.
    	// 						https://www.parisphoto.nz/?tjwp_file_download=2e8GjZ5XCovW&tjwp_file_download_value=zFVQ7hZMDSox
    	// $file_download_url = get_bloginfo( 'url' ) . '?tjwp_file_download=' . $file_download_key . '&tjwp_file_download_value=' . $file_download_value;
    	// ZIP LOCATION 		https://www.parisphoto.nz/wp-content/uploads/3082508124.zip
    
    	$url = 'https://';
    	// Append the host(domain name, ip) to the URL.
    	$url .= $_SERVER[‘HTTP_HOST’];
    	// Append the requested resource location to the URL
    	$url .= $_SERVER[‘REQUEST_URI’];
    
    	$split_str        = str_split($url, 29);
    	$double_split_str = str_split($split_str[1], 10);
    	$unique_id        = $double_split_str[0];
    
    	$file_download_url = get_bloginfo( 'url' ) . 'wp-content/uploads/' . $unique_id . '.zip';
    

    I really appreciate your help on this but i think this is as deep as I can go with my limited understanding, specifically with how the button is written / the button call back implementation so that information can be passed from the post housing the TJ to the download transient.

    • This reply was modified 11 months, 1 week ago by parismolver.
    • This reply was modified 11 months, 1 week ago by parismolver.
    Thread Starter parismolver

    (@parismolver)

    I see this is the transient download

    
     * @param    int    $form_id - The ID of the form containing the attachment we want to download.
    

    If possible I’d like to maintain the security of my files and the files of my users.

    When I make a new download attachment it creates a new $form_id correct? And this is also noticed when the new form is placed as the tipjarwp id increments.

    Is there some way I can create my own TipJarWP form? in such a way where I specify where the file location exists (as I do in the GUI interface) and what the id used in the shortcode is to be (which i can’t do in the GUI).

    I’m planning on making 100 business cards for example, would it be possible to create the entire TipJarWP database and then manually upload that instead? That way site 1234 could have the shortcode id 1234 which downloads 1234.zip. I also think this solves every-bodies solution in the future instead of hacking together something to work for just me.

    • This reply was modified 11 months, 1 week ago by parismolver.
    Thread Starter parismolver

    (@parismolver)

    I just did some more digging and I think I’ve found success.

    With the WP Data Access plugin installed I can browse through my databases.
    If i click >explore and page forward in the list till I find the database;

    myuser_tip_jar_wp_forms

    From here I can export and import the database as needed. Being able to create a database listing with the id 1234 means i can edit the following fields;

    
       "id":1,
       "mode":"form",
       "open_style":"in_place",
       "currency_code":"NZD",
       "currency_symbol":"$",
       "currency_type":"decimal",
       "blank_flag_url":"https:\/\/www.parisphoto.nz\/wp-content\/plugins\/tip-jar-wp\/\/assets\/images\/flags\/blank.gif",
       "flag_sprite_url":"https:\/\/www.parisphoto.nz\/wp-content\/plugins\/tip-jar-wp\/\/assets\/images\/flags\/flags.png",
       "default_amount":500,
       "top_media_type":"none",
       "featured_image_url":false,
       "featured_embed":"",
       "header_media":null,
       "file_download_attachment_data":{
          "file_download_mode_enabled":true,
          "attachment_id":128,
          "attachment_filename":"12345678.zip",
          "email_required":true,
          "instructions_title":"Thanks for the tip!",
          "instructions_description":""
       },
       "recurring_options_enabled":false,
    

    I am guessing that in python I can create a dummy entry, duplicate it and edit only the fields I wish to change. I can do this at the same time my QR codes and webpages are being generated.

    I’m still unsure of two things tho.

    
    "attachment_id":128,
    

    what does this do? It it for bookkeeping in the download logs database or does it serve a function elsewhere as discussed in the download.php

    
    "attachment_filename":"12345678.zip",
    

    How does this link to the actual file path? Does TipJarWP always link and look in the /wp-content/ directory? If so then simply placing 1234.zip in /wp-content/ and creating a database with “attachment_filename”:”1234.zip” and “id”:1234, would be enough to create the shortcode [tipjarwp id=1234] which links to 1234.zip

    • This reply was modified 11 months, 1 week ago by parismolver.
    Thread Starter parismolver

    (@parismolver)

    Hi,

    I’ve managed to upload my own mysql script to my dreamhost php sql interface and it’s working pretty alright in terms of creating a new tipjar. However, the download doesn’t work till i go into the edit mode and reassign the zip, even though in the interface the naming is correct.

    I’ve compared the database entry from before the reassign and after and the only thing that changes is the attachment_id parameter.

    Thread Starter parismolver

    (@parismolver)

    Solved.

    1. With a fresh install, create a new tip jar block and configure it how you wish.

    2. download a plugin or use a tool such as phpMyAdmin (for dreamhost users) and investigate your database for wp_tj_forms database. copy the JSON data for your form.

    3. Edit the ID and attachment ID for your specific form create a ‘INSERT INTO’ … ‘VALUES’ (https://stackoverflow.com/questions/452859/inserting-multiple-rows-in-a-single-sql-query) that inserts the details of a new tip jar form.

    4. use the shortcode [wp-tj id=’your id’] !

    Plugin Author Tip Jar WP

    (@tipjarwp)

    Awesome! That’s a great solution here. Thanks for sharing and glad you found a solution 😀

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Can i link the shortcode to the file download location?’ is closed to new replies.