Easy-digital-downloads: Introduce shortcode-download.php Template File

Created on 13 Oct 2016  路  7Comments  路  Source: easydigitaldownloads/easy-digital-downloads

I've been harping about this forever now but don't think there is an open issue for tracking.

As EDD makes a push for more themes (Themedd!) and continued development of many extensions that output downloads for various reasons I think this becomes even more important. There are a few issues with the current way the [downloads] shortcode loads multiple template files.

  1. Any theme or plugin that wants to output downloads is forced to use the [downloads] shortcode (not necessarily a bad thing) or call the edd_downloads_query() function directly to achieve the same output as their store page.

In my mind this is the preferred approach that themes and plugins should be taking.

  1. Any theme or plugin that doesn't use the [downloads] shortcode has to either duplicate the code inside of edd_downloads_query() and then selectively include the separate template files. Often times this leaves out many of the filters that are in the shortcode callback, and if they do exist it's different so extra code still needs to be added.

This has caused issues with current extensions that output lists of downloads completely differently than what a theme has styled for the shortcode.

e.g: https://github.com/easydigitaldownloads/edd-recommended-products/issues/15

  1. edd_downloads_query() is massive and is certainly not just querying downloads.

There are many pros to calling these multiple template files from a singular file:

  1. A theme can override one file to remove calls to the other pieces without having to filter the shortcode attributes, plugin filters, etc.
  2. It can be done with complete backwards compatibility since all existing template files remain the exact same.

The bad things:

  1. Existing themes/plugins that do not call the shortcode directly will not be affected until they update their code (this is also really a good thing).
  2. edd_get_template_part() can't pass variables so the existing $atts array would need some finagling.

My proposal would be to replace https://github.com/easydigitaldownloads/easy-digital-downloads/blob/master/includes/shortcodes.php#L564-L587 with an action that calls a template file. This will allow the $atts variable to be dealt with.

function edd_download_item( $atts ) {
    global $atts;

    edd_get_template_part( 'shortcode', 'download' );
}
add_action( 'edd_download_item', 'edd_download_item' );
<?php
/**
 * A download for the [downloads] grid.
 */

global $atts;

https://github.com/easydigitaldownloads/easy-digital-downloads/blob/master/includes/shortcodes.php#L564-L587

So there are technically no immediate benefits for existing themes/plugins but it will certainly help things a lot moving forward.

Thoughts?

component-templates scope-ui type-feature

Most helpful comment

I support this 100%.

All 7 comments

I support this 100%.

Great! I'll try to get a PR ready once 2.7 dev starts.

And the world rejoiced!

Should we have more specific variable names than $atts and $i as they are very generic and in the global scope?

@sunnyratilal yes please

This is working well for me

Left some notes on the PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michaelbeil picture michaelbeil  路  5Comments

amdrew picture amdrew  路  5Comments

Ismail-elkorchi picture Ismail-elkorchi  路  3Comments

scottbuscemi picture scottbuscemi  路  5Comments

DevinWalker picture DevinWalker  路  6Comments