Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Suggestion: TimberShortcode class #1336

Closed
danburzo opened this issue Feb 25, 2017 · 3 comments
Closed

Feature Suggestion: TimberShortcode class #1336

danburzo opened this issue Feb 25, 2017 · 3 comments
Labels
enhancement help wanted Do you know computer? Then lend a hand and have your code live in infamy!

Comments

@danburzo
Copy link
Contributor

danburzo commented Feb 25, 2017

Hi, hopefully this is the right place to come with suggestions for Timber, since the guidelines encourage only bugs.

I was just implementing some custom shortcodes for a theme, and was thinking that having a TimberShortcode class to handle basic things you need to do on your content (escaping content, default parameters, normalizing the attribute key-case, etc.) would be a great addition.

I'll keep it short because I want to gauge the interest for such a feature, and I can look into specifics if you agree it's a step in the right direction.

A "proper" shortcode creation snippet from here:

<?php
function wporg_shortcode($atts = [], $content = null, $tag = '')
{
    // normalize attribute keys, lowercase
    $atts = array_change_key_case((array)$atts, CASE_LOWER);
 
    // override default attributes with user attributes
    $wporg_atts = shortcode_atts([
                                     'title' => 'WordPress.org',
                                 ], $atts, $tag);
 
    // start output
    $o = '';
 
    // start box
    $o .= '<div class="wporg-box">';
 
    // title
    $o .= '<h2>' . esc_html__($wporg_atts['title'], 'wporg') . '</h2>';
 
    // enclosing tags
    if (!is_null($content)) {
        // secure output by executing the_content filter hook on $content
        $o .= apply_filters('the_content', $content);
 
        // run shortcode parser recursively
        $o .= do_shortcode($content);
    }
 
    // end box
    $o .= '</div>';
 
    // return output
    return $o;
}
 
function wporg_shortcodes_init() {
    add_shortcode('wporg', 'wporg_shortcode');
}
 
add_action('init', 'wporg_shortcodes_init');
@jarednova
Copy link
Member

Hi @danburzo I'm all for finding ways to simplify interacting with WP and Timber. Here's the pattern I usually use/recommend for adding shortcodes....

add_shortcode('wporg', function($atts, $content = null, $tag = '') {
    $atts = do_things_here($atts);
    return Timber::compile('my-template-partial.twig', $atts);
});

What ideas do you have for simplifying and turning into a class?

@jarednova jarednova added help wanted Do you know computer? Then lend a hand and have your code live in infamy! enhancement labels Feb 28, 2017
@danburzo
Copy link
Contributor Author

danburzo commented Mar 1, 2017

I've slept on the idea, and maybe creating a TimberShortcode class is not the best approach here -- mainly because other Timber classes are convenience wrappers over 'physical' concepts in Wordpress (Post, Menu, Term, etc.), and in the case of shortcodes, I'm not really sure what you would wrap.

I guess I was thinking about something like:

$shortcode = new TimberShortcode($atts, $content, $tag);
return Timber::compile('my-shortcode.twig', $shortcode);

And the TimberShortcode class would:

  1. Normalize / sanitize $atts / pass them through the appropriate filters -- not sure how you would provide defaults in the constructor
  2. Possibly differentiate between $shortcode.content (passed through the_content filter) and $shortcode.shortcode_content (in the raw form), much like TimberPost -- however, I don't have a particular use-case where I'd need the raw content
  3. Possibly expand out $atts so that they can be accessed via $shortcode->some_attribute, etc.

On the other hand, the problems that have actually hindered implementing shortcodes are mostly around wpautop behavior and the limitation in nesting the same shortcode inside itself.

@gchtr
Copy link
Member

gchtr commented May 3, 2023

I’m closing this, because it doesn’t look like there’s more to do here :).

@gchtr gchtr closed this as completed May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted Do you know computer? Then lend a hand and have your code live in infamy!
Projects
None yet
Development

No branches or pull requests

3 participants