Plugin Directory

Changeset 1318959


Ignore:
Timestamp:
12/31/2015 03:49:57 AM (10 years ago)
Author:
Developdaly
Message:

Adds a media button to easily insert shortcodes, Improved error handling

Location:
library/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • library/trunk/README.txt

    r912797 r1318959  
    11=== Library ===
    2 Contributors: developdaly
     2Contributors: developdaly, mikemanger, adamkrawiec
    33Tags: strings, library, content, shortcodes, dictionary, wp-strings
    44Requires at least: 3.8.1
    5 Tested up to: 3.9.1
    6 Stable tag: 1.0.2
     5Tested up to: 4.4
     6Stable tag: 1.1.0
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6060== Changelog ==
    6161
     62= 1.1.0 =
     63* Adds a media button to easily insert shortcodes
     64* Improved error handling
     65
    6266= 1.0.2 =
    6367* Localized admin screen strings
  • library/trunk/admin/class-library-admin.php

    r912797 r1318959  
    5151        $this->plugin_slug = $plugin->get_plugin_slug();
    5252
    53         add_action( 'init', array( $this, 'register' ) );
     53        add_action( 'init', array( $this, 'register' ), 11 );
    5454
    5555        /* Fire our meta box setup function on the post editor screen. */
    5656        add_action( 'load-post.php',        array( $this, 'library_meta_boxes_setup' ) );
     57
     58        add_action( 'media_buttons', array( $this, 'add_form_button' ), 20 );
     59
     60        add_action( 'admin_footer',  array( $this, 'add_mce_popup' ), 11 );
    5761
    5862    }
     
    7882
    7983        $labels = array(
    80             'name' => _x( 'Terms', 'library_term' ),
    81             'singular_name' => _x( 'Term', 'library_term' ),
    82             'add_new' => _x( 'Add New', 'library_term' ),
    83             'add_new_item' => _x( 'Add New Term', 'library_term' ),
    84             'edit_item' => _x( 'Edit Term', 'library_term' ),
    85             'new_item' => _x( 'New Term', 'library_term' ),
    86             'view_item' => _x( 'View Term', 'library_term' ),
    87             'search_items' => _x( 'Search Terms', 'library_term' ),
    88             'not_found' => _x( 'No terms found', 'library_term' ),
    89             'not_found_in_trash' => _x( 'No terms found in Trash', 'library_term' ),
    90             'parent_item_colon' => _x( 'Parent Term:', 'library_term' ),
    91             'menu_name' => _x( 'Library', 'library_term' ),
     84            'name' => _x( 'Terms', 'shortcode terms general name', 'library' ),
     85            'singular_name' => _x( 'Term', 'shortcode term singular name', 'library' ),
     86            'add_new' => _x( 'Add New', 'shortcode term', 'library' ),
     87            'add_new_item' => __( 'Add New Term', 'library' ),
     88            'edit_item' => __( 'Edit Term', 'library' ),
     89            'new_item' => __( 'New Term', 'library' ),
     90            'view_item' => __( 'View Term', 'library' ),
     91            'search_items' => __( 'Search Terms', 'library' ),
     92            'not_found' => __( 'No terms found', 'library' ),
     93            'not_found_in_trash' => __( 'No terms found in Trash', 'library' ),
     94            'parent_item_colon' => __( 'Parent Term:', 'library' ),
     95            'menu_name' => _x( 'Library', 'shortcode term collection', 'library' ),
    9296        );
    9397
    9498        $args = array(
    9599            'labels' => $labels,
    96             'hierarchical' => false,
    97100            'supports' => array( 'title', 'editor', 'revisions' ),
    98             'public' => true,
    99             'show_ui' => true,
    100             'show_in_menu' => true,
     101            'public' => false, // hide the front end UI
     102            'show_ui' => true, // keep admin UI
     103            'show_in_nav_menus' => true,
    101104            'menu_position' => 5,
    102             'show_in_nav_menus' => true,
    103             'publicly_queryable' => true,
    104             'exclude_from_search' => false,
    105             'has_archive' => true,
    106             'query_var' => true,
    107             'can_export' => true,
    108             'rewrite' => true,
    109             'capability_type' => 'post'
    110105        );
    111106
     
    149144        global $post;
    150145
    151         echo esc_html( _e( '<p>To display this inside your content use <code>[library term="'. $post->post_name .'"]</code> OR to use this inside of a template file use <code>&lt;?php echo do_shortcode(\'[library term="'. $post->post_name .'"]\') ?&gt;</code></p>', 'library' ) );
    152         echo esc_html( _e( '<p>You can change the term slug by editing the permalink slug underneath the title.</p>', 'library' ) );
    153 
    154     }
     146        $shortcode_example = '<code>[library term="' . $post->post_name . '"]</code>';
     147        $php_example = '<code>&lt;?php echo do_shortcode( \'[library term="' . $post->post_name . '"]\' ) ?&gt;</code>';
     148        echo '<p>';
     149        printf( __( 'To display this inside your content use %s OR to use this inside of a template file use %s', 'library' ), $shortcode_example, $php_example );
     150        echo '</p><p>';
     151        _e( 'You can change the term slug by editing the permalink slug underneath the title.', 'library' );
     152        echo '</p>';
     153
     154    }
     155
     156    /**
     157     * Checks to see if we are editing or adding a post.
     158     *
     159     * @return bool
     160     * @since  1.0.3
     161     */
     162    public static function page_supports_add_form_button() {
     163        // check we aren't calling this too early
     164        if ( ! function_exists( 'get_current_screen' ) ) {
     165            return false;
     166        }
     167
     168        $screen = get_current_screen();
     169        $is_post_edit_page = in_array( $screen->parent_base, array( 'edit' ) );
     170
     171        if ( 'library_term' == $screen->post_type ) {
     172            $is_post_edit_page = false;
     173        }
     174
     175        $display_add_form_button = apply_filters( 'library_display_add_form_button', $is_post_edit_page );
     176
     177        return $display_add_form_button;
     178    }
     179
     180    /**
     181     * Adds a button to the editor
     182     *
     183     * @since 1.0.3
     184     */
     185    public function add_form_button() {
     186        $is_add_form_page = self::page_supports_add_form_button();
     187        if ( ! $is_add_form_page ) {
     188            return;
     189        }
     190
     191        // do a version check for the new 3.5 UI
     192        $version = get_bloginfo( 'version' );
     193
     194        if ( $version < 3.5 ) {
     195            // show button for v 3.4 and below
     196            echo '<a href="#TB_inline?width=480&inlineId=select_library_shortcode" class="thickbox" id="add_gform">' . __( 'Add Library Shortcode', 'library' ) . '</a>';
     197        } else {
     198            // display button matching new UI
     199            echo '<a href="#TB_inline?width=480&inlineId=select_library_shortcode" class="thickbox button library_media_link" id="add_library_shortcode" title="' . __( 'Add Library Shortcode', 'library' ) . '">' . __( 'Add Library Shortcode', 'library' ) . '</a>';
     200        }
     201    }
     202
     203    /**
     204     * Add the form for inserting shortcodes into posts
     205     *
     206     * @since 1.0.3
     207     */
     208    public static function add_mce_popup() {
     209        if ( ! self::page_supports_add_form_button() ) {
     210            return;
     211        }
     212        ?>
     213        <script>
     214            function library_insert_shortcode() {
     215                var term_slug = jQuery( '#add_term_slug' ).val();
     216                if ( '' === term_slug ) {
     217                    alert( '<?php _e( 'Please select a shortcode', 'library' ); ?>' );
     218                    return;
     219                }
     220
     221                window.send_to_editor( '[library term="' + term_slug + '"]' );
     222            }
     223        </script>
     224
     225        <div id="select_library_shortcode" style="display:none;">
     226            <div class="wrap">
     227                <h3><?php _e( 'Insert A Shortcode', 'library' ); ?></h3>
     228                <p>
     229                    <?php _e( 'Select a term below to add it to your post or page.', 'library' ); ?>
     230                </p>
     231                <p>
     232                    <select id="add_term_slug">
     233                        <option value=""><?php _e( 'Select a Term', 'library' ); ?></option>
     234                        <?php
     235                            // TODO: what happens when there are 1000 terms? AJAX list with a short fallback
     236                            $args = array(
     237                                'post_type'      => 'library_term',
     238                                'order'          => 'ASC',
     239                                'orderby'        => 'title',
     240                                'posts_per_page' => 200,
     241                                'no_found_rows'  => true,
     242                            );
     243                            $query = new WP_Query( $args );
     244
     245                            if ( $query->have_posts() ) {
     246                                global $post;
     247                                while ( $query->have_posts() ) {
     248                                    $query->the_post();
     249                                ?>
     250                                <option value="<?php echo esc_attr( $post->post_name ); ?>"><?php the_title(); ?></option>
     251                                <?php
     252                                }
     253                            }
     254                            wp_reset_postdata();
     255                        ?>
     256                    </select> <br/>
     257                </p>
     258                <p>
     259                    <input type="button" class="button button-primary" value="<?php esc_attr_e( 'Insert Shortcode', 'library' ); ?>" onclick="library_insert_shortcode();"/>&nbsp;&nbsp;&nbsp;
     260                    <a class="button" onclick="tb_remove(); return false;"><?php esc_attr_e( 'Cancel', 'library' ); ?></a>
     261                </p>
     262            </div>
     263        </div>
     264
     265        <?php
     266    }
     267
    155268
    156269}
  • library/trunk/languages/library.pot

    r873050 r1318959  
    33msgid ""
    44msgstr ""
    5 "Project-Id-Version: 1.0.1\n"
     5"Project-Id-Version: 1.0.3\n"
    66"Report-Msgid-Bugs-To: http://wordpress.org/plugins/library\n"
    7 "POT-Creation-Date: 2014-03-10 15:30-0600\n"
    8 "PO-Revision-Date: 2014-03-10 15:30-0600\n"
    9 "Last-Translator: Patrick Daly <patrick@developdaly.com>\n"
     7"POT-Creation-Date: 2014-11-28 10:59-0000\n"
     8"PO-Revision-Date: 2014-11-28 10:59-0000\n"
     9"Last-Translator: Mike Manger <mike.manger@lighthouseuk.net>\n"
    1010"Language-Team: Develop Daly <patrick@developdaly.com>\n"
    1111"Language: en_US\n"
     
    1313"Content-Type: text/plain; charset=UTF-8\n"
    1414"Content-Transfer-Encoding: 8bit\n"
    15 "X-Generator: Poedit 1.6.4\n"
     15"X-Generator: Poedit 1.6.10\n"
    1616"X-Poedit-KeywordsList: __;_e;_n;_x;esc_html_e;esc_html__;esc_attr_e;"
    1717"esc_attr__;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;_x:1,2c;_n:1,2\n"
     
    2020"X-Poedit-SearchPath-0: .\n"
    2121
    22 #: admin/class-library-admin.php:80
    23 msgctxt "library_term"
     22#: admin/class-library-admin.php:84
     23msgctxt "shortcode terms general name"
    2424msgid "Terms"
    25 msgstr "Terms"
    26 
    27 #: admin/class-library-admin.php:81
    28 msgctxt "library_term"
    29 msgid "Term"
    30 msgstr "Term"
    31 
    32 #: admin/class-library-admin.php:82
    33 msgctxt "library_term"
    34 msgid "Add New"
    35 msgstr "Add New"
    36 
    37 #: admin/class-library-admin.php:83
    38 msgctxt "library_term"
    39 msgid "Add New Term"
    40 msgstr "Add New Term"
    41 
    42 #: admin/class-library-admin.php:84
    43 msgctxt "library_term"
    44 msgid "Edit Term"
    45 msgstr "Edit Term"
     25msgstr ""
    4626
    4727#: admin/class-library-admin.php:85
    48 msgctxt "library_term"
    49 msgid "New Term"
    50 msgstr "New Term"
     28msgctxt "shortcode term singular name"
     29msgid "Term"
     30msgstr ""
    5131
    5232#: admin/class-library-admin.php:86
    53 msgctxt "library_term"
    54 msgid "View Term"
    55 msgstr "View Term"
     33msgctxt "shortcode term"
     34msgid "Add New"
     35msgstr ""
    5636
    5737#: admin/class-library-admin.php:87
    58 msgctxt "library_term"
    59 msgid "Search Terms"
    60 msgstr "Search Terms"
     38msgid "Add New Term"
     39msgstr ""
    6140
    6241#: admin/class-library-admin.php:88
    63 msgctxt "library_term"
    64 msgid "No terms found"
    65 msgstr "Not terms found"
     42msgid "Edit Term"
     43msgstr ""
    6644
    6745#: admin/class-library-admin.php:89
    68 msgctxt "library_term"
    69 msgid "No terms found in Trash"
    70 msgstr "No terms found in Trash"
     46msgid "New Term"
     47msgstr ""
    7148
    7249#: admin/class-library-admin.php:90
    73 msgctxt "library_term"
    74 msgid "Parent Term:"
    75 msgstr "Parent Term:"
     50msgid "View Term"
     51msgstr ""
    7652
    7753#: admin/class-library-admin.php:91
    78 msgctxt "library_term"
     54msgid "Search Terms"
     55msgstr ""
     56
     57#: admin/class-library-admin.php:92
     58msgid "No terms found"
     59msgstr ""
     60
     61#: admin/class-library-admin.php:93
     62msgid "No terms found in Trash"
     63msgstr ""
     64
     65#: admin/class-library-admin.php:94
     66msgid "Parent Term:"
     67msgstr ""
     68
     69#: admin/class-library-admin.php:95
     70msgctxt "shortcode term collection"
    7971msgid "Library"
    80 msgstr "LIbrary"
     72msgstr ""
    8173
    82 #: admin/class-library-admin.php:135
     74#: admin/class-library-admin.php:130
    8375msgid "How to Use this Term"
    84 msgstr "How to Use this Term"
     76msgstr ""
     77
     78#: admin/class-library-admin.php:149
     79#, php-format
     80msgid ""
     81"To display this inside your content use %s OR to use this inside of a "
     82"template file use %s"
     83msgstr ""
     84
     85#: admin/class-library-admin.php:151
     86msgid ""
     87"You can change the term slug by editing the permalink slug underneath the "
     88"title."
     89msgstr ""
     90
     91#: admin/class-library-admin.php:196 admin/class-library-admin.php:199
     92msgid "Add Library Shortcode"
     93msgstr ""
     94
     95#: admin/class-library-admin.php:217
     96msgid "Please select a shortcode"
     97msgstr ""
     98
     99#: admin/class-library-admin.php:227
     100msgid "Insert A Shortcode"
     101msgstr ""
     102
     103#: admin/class-library-admin.php:229
     104msgid "Select a term below to add it to your post or page."
     105msgstr ""
     106
     107#: admin/class-library-admin.php:233
     108msgid "Select a Term"
     109msgstr ""
     110
     111#: admin/class-library-admin.php:257
     112msgid "Insert Shortcode"
     113msgstr ""
     114
     115#: admin/class-library-admin.php:258
     116msgid "Cancel"
     117msgstr ""
  • library/trunk/library.php

    r912797 r1318959  
    1616 * Plugin URI:        http://wordpress.org/plugins/library
    1717 * Description:       Create a library of reusable terms (strings) and display their contents anywhere on your site with a shortcode.
    18  * Version:           1.0.2
     18 * Version:           1.1.0
    1919 * Author:            Patrick Daly
    2020 * Author URI:        http://developdaly.com
  • library/trunk/public/class-library.php

    r873050 r1318959  
    2929     * @var     string
    3030     */
    31     const VERSION = '1.0.0';
     31    const VERSION = '1.1.0';
    3232
    3333    /**
     
    6565        // Load plugin text domain
    6666        add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
    67        
     67
    6868        // Add the library shortcode
    6969        add_shortcode( 'library', array( $this, 'shortcode' ) );
     
    9898        return self::$instance;
    9999    }
    100    
     100
    101101    /**
    102102     * Load the plugin text domain for translation.
     
    110110
    111111        load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' );
    112         load_plugin_textdomain( $domain, FALSE, basename( plugin_dir_path( dirname( __FILE__ ) ) ) . '/languages/' );
     112        load_plugin_textdomain( $domain, false, basename( plugin_dir_path( dirname( __FILE__ ) ) ) . '/languages/' );
    113113
    114114    }
     
    121121    public function shortcode( $atts ) {
    122122        global $post;
    123        
     123
    124124        $args = array(
    125125            'name' => $atts['term'],
    126             'post_type' => 'library_term'
     126            'post_type' => 'library_term',
    127127        );
    128        
     128
    129129        $query = new WP_Query( $args );
     130
     131        $output = '';
    130132
    131133        if ( $query->have_posts() ) {
     
    137139
    138140        wp_reset_postdata();
    139        
     141
    140142        return $output;
    141143    }
Note: See TracChangeset for help on using the changeset viewer.