wp-shortcake / shortcake

Shortcake makes using WordPress shortcodes a piece of cake.
GNU General Public License v2.0
664 stars 142 forks source link

One-click paths to adding a shortcode #94

Open danielbachhuber opened 9 years ago

danielbachhuber commented 9 years ago

There are some places in core where it might make sense to clone existing UI and add a one-click path to adding a shortcode:

Related https://github.com/fusioneng/Shortcake/issues/92#issuecomment-67864002

MickeyKay commented 9 years ago

Here's a quick go at adding button insertion - the code is pasted at the very bottom. It does work, but has the following drawbacks:

Code

add_action( 'media_buttons_context', 'shortcode_ui_add_editor_buttons' );
/**
 * Add shortcode insertion buttons to the editor.
 *
 * @since   0.2.0
 *
 * @param   string  $html  Editor buttons HTML.
 *
 * @return  string  $html  Updated buttons HTML.
 */
function shortcode_ui_add_editor_buttons( $html ) {

    // Get all registered UI shortcodes
    $ui_shortcodes = Shortcode_UI::get_instance()->get_shortcodes();

    // Initialize empty jQuery string       
    $script = '';

    // Add button and jQuery for each UI shortcode with add_button attribute
    foreach ( $ui_shortcodes as $shortcode => $atts ) {

        // Skip if add_button attribute isn't set
        if ( ! $atts['add_button'] ) {
            continue;
        }

        // Compile button HTML
        ob_start(); 
        ?>
        <a href="#" id="insert-<?php echo $shortcode; ?>-button" class="button insert-<?php echo $shortcode; ?> add_<?php echo $shortcode; ?>" data-editor="content" title="Add <?php echo $atts['label']; ?>"><span class="dashicons wp-media-buttons-icon <?php echo $atts['listItemImage']; ?>"></span> Add <?php echo $atts['label']; ?></a>
        <?php 
        $html .= ob_get_clean();

        /**
         * Compile button JS
         *
         * @todo This should likely be reworked to incorporate existing backbone.js functionality.
         */
        $att_markup = array();
        $has_content_att = false;

        foreach( $atts['attrs'] as $att_array ) {
            $att_markup[] = $att_array['attr'] . '=""';

            // Flag if shortcode has a content attribute (e.g. single shortcode [button] vs [button][/button])
            if ( 'content' == $att_array['attr'] ) {
                $has_content_att = true;
            }

        }

        // Compose button jQuery
        ob_start(); 
        ?>
        jQuery('#insert-<?php echo $shortcode; ?>-button').on('click', function() {
            window.parent.send_to_editor('[<?php echo $shortcode; ?> <?php echo implode( ' ', $att_markup ); ?>]<?php echo $has_content_att ? '[/' . $shortcode . ']' : ''; ?>');
        });
        <?php 

        // Wrap individual button scripts up together
        $script .= '<script>' . ob_get_clean() . '</script>';
    }

    $html .= $script;

    return $html;

}
DevinWalker commented 9 years ago

Would love to see this as an option as well +1

cliffordp commented 9 years ago

Thanks for the head start, @MickeyKay!

I cleaned it up a bit and added some new functionality here: https://gist.github.com/cliffordp/90235d62ce8df52caa34

Here's a preview of all but one as 'icon_only' and the last one's hover.

screenshot 2015-08-07 12 26 02
MickeyKay commented 9 years ago

Sweet! This is looking cool. Not sure if/how it will fit into the backbone paradigm that seems to be gaining in popularity. Any insights/feedback from the main devs?