Plugin Directory

Changeset 2186230


Ignore:
Timestamp:
11/05/2019 02:40:33 PM (6 years ago)
Author:
roba87
Message:

RoBa87: Fixed shortcode bug.

Bug: If the featured images for a post type have been disabled, the shortcode extension still loaded its script for that post type.
This script relied on the main script of the SMFI plugin. Since the main script was not there, it came to the error.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • simple-multiple-featured-images/trunk/includes/class-smfi-shortcodes.php

    r1971107 r2186230  
    88 */
    99if ( ! class_exists( 'SMFI_Shortcodes' ) ) {
    10    
     10
    1111    /**
    12      * Adds shortcodes support to the simple multiple featured images plugin. 
     12     * Adds shortcodes support to the simple multiple featured images plugin.
    1313     *
    1414     * Allows to visualize the featured images via shortcodes.
     
    1717     */
    1818    class SMFI_Shortcodes {
    19        
     19
    2020        /**
    2121         * The default gallery shortcode.
    2222         *
    2323         * @since 1.0.0
    24          * @var string 
     24         * @var string
    2525         */
    2626        private $shortcode_insert_default_gallery = 'smfi-insert-default-gallery';
    27        
     27
    2828        /**
    2929         * The default slider shortcode.
    3030         *
    3131         * @since 1.0.0
    32          * @var string 
     32         * @var string
    3333         */
    3434        private $shortcode_insert_default_slider = 'smfi-insert-default-slider';
    35        
     35
    3636        /**
    3737         * The smfi plugin.
    3838         *
    3939         * @since 1.0.0
    40          * @var string 
     40         * @var string
    4141         */
    4242        private $smfi_plugin;
    43        
     43
    4444        /**
    4545         * Constructor.
    4646         *
    47          * @since 1.0.0 
     47         * @since 1.0.0
    4848         *
    4949         */
     
    5151            $this -> smfi_plugin = $smfi_plugin;
    5252        }
    53        
     53
    5454        /**
    5555         * Initialize the shortcodes.
     
    5858         */
    5959        public function init() {
    60            
     60
    6161            // Enqueue all necessary CSS and JS into the admin screen.
    6262            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
    63            
     63
    6464            // Enqueue all necessary CSS and JS into the front end.
    6565            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
    66            
     66
    6767            // Register shortcodes.
    6868            add_action('init', array( $this, 'register_shortcodes' ) );
    69            
     69
    7070            /*
    7171            * Attach new buttons to the TinyMCE editor.
     
    7474            add_action( 'init', array( $this, 'init_shortcode_buttons') );
    7575        }
    76        
     76
    7777        /**
    7878         * Enqueue admin scripts.
     
    8080         * Enqueue all necessary CSS and javascript files. If the current post type is not supported by this plugin then no files will be enqueued.
    8181         *
    82          * @since 1.0.0 
     82         * @since 1.0.0
    8383         */
    8484        public function enqueue_admin_scripts() {
    85            
     85
    8686            if( ! is_null( $this -> smfi_plugin ) ) {
    8787                if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) {
    88                    
     88
    8989                    // Add plugin specific js with translations.
    9090                    $translation_object_name = 'smfi_shortcode_translation_object';
     
    9393                        'insert_default_slider_btn_tooltip' => esc_html__( 'Insert SMFI slider', 'simple-multiple-featured-images'),
    9494                    );
    95                    
     95
    9696                    // Add shortcodes JS.
    9797                    SMFI_JS_Importer::import_js(
    98                         'smfi_shortcodes_js', 
    99                         plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/jquery.smfi-shortcodes.js', 
     98                        'smfi_shortcodes_js',
     99                        plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/jquery.smfi-shortcodes.js',
    100100                        false /*use minified version*/,
    101                         $translation_object_name, 
     101                        $translation_object_name,
    102102                        $translation_array
    103103                    );
     
    105105            }
    106106        }
    107        
     107
    108108        /**
    109109         * Enqueue frontend scripts.
     
    111111         * Enqueue all necessary CSS and javascript files into the frontend. If the current post type is not supported by this plugin then no files will be enqueued.
    112112         *
    113          * @since 1.0.0 
     113         * @since 1.0.0
    114114         */
    115115        public function enqueue_frontend_scripts() {
    116            
     116
    117117            if( ! is_null( $this -> smfi_plugin ) ) {
    118118                if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) {
    119                    
     119
    120120                    // Add shortcode and CSS
    121121                    SMFI_CSS_Importer::import_css(
    122                         'smfi_shortcodes_frontend_css', 
    123                         plugin_dir_url( dirname( __FILE__ ) ) . 'public/css/smfi-shortcodes-frontend-style.css', 
    124                         false /*use minified version*/ 
     122                        'smfi_shortcodes_frontend_css',
     123                        plugin_dir_url( dirname( __FILE__ ) ) . 'public/css/smfi-shortcodes-frontend-style.css',
     124                        false /*use minified version*/
    125125                    );
    126                    
     126
    127127                    // Add shortcode JS.
    128128                    SMFI_JS_Importer::import_js(
    129                         'smfi_shortcodes_frontend_js', 
    130                         plugin_dir_url( dirname( __FILE__ ) ) . 'public/js/jquery.smfi-shortcodes-frontend.js', 
     129                        'smfi_shortcodes_frontend_js',
     130                        plugin_dir_url( dirname( __FILE__ ) ) . 'public/js/jquery.smfi-shortcodes-frontend.js',
    131131                        false /*use minified version*/
    132132                    );
     
    134134            }
    135135        }
    136        
     136
    137137        /**
    138138         * Registers the shortcodes in wordpress.
     
    147147            add_shortcode( $this -> shortcode_insert_default_slider,  array( $this, 'get_frontend_html_for_shortcode' ) );
    148148        }
    149        
     149
    150150        /**
    151151         * Attachs new shortcode buttons into the TinyMCE editor. For each shortcode a seperate button will be provided.
     
    153153         * Pressing the button will add the corresponding shortcode automatically into the current selected area inside the editor.
    154154         *
    155          * @since 1.0.0 
     155         * @since 1.0.0
    156156         */
    157157        function init_shortcode_buttons() {
    158158            // Register the shortcode buttons.
    159159            add_filter( 'mce_buttons', array( $this, 'register_shortcode_buttons') );
    160            
     160
    161161            // Register the necessary JS plugin for the shortcode buttons.
    162162            add_filter( 'mce_external_plugins', array( $this, 'register_shortcode_buttons_js') );
    163163        }
    164        
     164
    165165        /**
    166166         * Register the shortcode buttons.
    167167         *
    168          * @since 1.0.0 
     168         * @since 1.0.0
    169169         *
    170170         * @param array $buttonIDs All IDs (as string) of all already registered buttons.
     
    172172         */
    173173        function register_shortcode_buttons( $buttonIDs ) {
    174             $buttonIDs[] = 'smfiDefaultGalleryBtn';
    175             $buttonIDs[] = 'smfiDefaultSliderBtn';
     174            if( ! is_null( $this -> smfi_plugin ) ) {
     175                if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) {
     176                    $buttonIDs[] = 'smfiDefaultGalleryBtn';
     177                    $buttonIDs[] = 'smfiDefaultSliderBtn';
     178                }
     179            }
    176180            return $buttonIDs;
    177181        }
    178        
     182
    179183        /**
    180184         * Register the necessary JS for the registered shortcode buttons.
    181185         *
    182          * @since 1.0.0 
     186         * @since 1.0.0
    183187         *
    184188         * @param array $paths All absolute paths of all JS scripts of all already registered buttons.
     
    186190         */
    187191        function register_shortcode_buttons_js( $paths ) {
    188             $paths[ 'smfiShortcodesButtonsPlugin' ] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/jquery.smfi-shortcodes-buttons-plugin.js';
     192            if( ! is_null( $this -> smfi_plugin ) ) {
     193                if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) {
     194                    $paths[ 'smfiShortcodesButtonsPlugin' ] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/jquery.smfi-shortcodes-buttons-plugin.js';
     195                }
     196            }
    189197            return $paths;
    190198        }
     
    193201         * Returns the HTML for the provided shortcode.
    194202         *
    195          * @since 1.0.0 
     203         * @since 1.0.0
    196204         *
    197205         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     
    201209         */
    202210        public function get_frontend_html_for_shortcode( $shortcode_attributes = [], $content = null, $shortcode_name = '' ) {
    203            
     211
    204212            $output = '';
    205            
     213
    206214            // Search the SMFI plugin.
    207215            if( ! is_null( $this -> smfi_plugin ) ) {
    208                
     216
    209217                // Check if the current post uses the featured images of the SMFI plugin.
    210218                if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) {
    211                    
     219
    212220                    // Sanitizes the raw shortcode attributes for further using.
    213221                    $sanitize_shortcode_attributes = $this -> sanitize_shortcode_attributes( $shortcode_attributes, $shortcode_name );
    214                    
     222
    215223                    // Get the appropriate image size for the current shortcode.
    216224                    $image_size = $this -> get_image_size_by_shortcode( $sanitize_shortcode_attributes, $shortcode_name );
    217                    
     225
    218226                    // Create and collect custom image tag attributes.
    219227                    $imgAttributes = array();
    220228                    $imgAttributes[ 'class' ] = $this -> get_image_tag_css_classes( $shortcode_name );
    221                    
     229
    222230                    // Get the HTML for each featured image individually.
    223231                    $smfi_api = $this -> smfi_plugin -> get_public_api();
    224232                    $img_tags = $smfi_api -> get_all_featured_images_tags( get_the_id(), $image_size, $imgAttributes );
    225                    
     233
    226234                    // Generate the frontend HTML by using the generated image tags.
    227235                    $output .= '<div class="' . $this -> get_image_container_css_class( $shortcode_name ) . '"';
     
    234242                    }
    235243                    $output .= '>';
    236                    
     244
    237245                        // Adjust the image tags as necessary for the current shortcode.
    238246                        foreach( $img_tags as $index => $img_tag ) {
    239247                            $output .= $this -> adjust_image_tag_by_shortcode( $img_tag, $shortcode_name );
    240248                        }
    241                        
     249
    242250                        // Check if the current shortcode has some HTML which should be inserted after the image tags.
    243251                        $number_of_image_tags = count( $img_tags );
     
    246254                }
    247255            }
    248            
     256
    249257            return $output;
    250258        }
    251        
     259
    252260        /**
    253261         * Sanitizes the raw shortcode attributes for further using.
     
    255263         * Removes unused attributes and creates and initialize missing attributes with default values.
    256264         *
    257          * @since 1.0.0 
     265         * @since 1.0.0
    258266         *
    259267         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     
    264272            // Change all received shortcode attributes to lowercase.
    265273            $shortcode_attributes = array_change_key_case( ( array ) $shortcode_attributes , CASE_LOWER );
    266                    
     274
    267275            // Set default attribute values and override them with attribute values defined by the user.
    268276            return shortcode_atts( [
     
    275283                                            ], $shortcode_attributes, $shortcode_name );
    276284        }
    277        
     285
    278286        /**
    279287         * Returns the user defined image size via shortcode.
    280288         *
    281289         * If no image size can be found then a default image size will be returned.
    282          * 
    283          * @since 1.0.0 
     290         *
     291         * @since 1.0.0
    284292         *
    285293         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     
    288296         */
    289297        private function get_image_size_by_shortcode( $shortcode_attributes, $shortcode_name ) {
    290            
     298
    291299            // Get all available intermediate image sizes.
    292300            $all_available_image_sizes = get_intermediate_image_sizes();
    293            
     301
    294302            // Attach additionally the full image size.
    295303            $all_available_image_sizes[] = 'full';
    296            
     304
    297305            if( in_array( $shortcode_attributes[ 'image-size' ], $all_available_image_sizes ) ) {
    298306                // User defined a valid image size via shortcode -> return this image size.
     
    303311            }
    304312        }
    305        
     313
    306314        /**
    307315         * Returns the default image size for the given shortcode.
    308316         *
    309          * @since 1.0.0 
     317         * @since 1.0.0
    310318         *
    311319         * @param string $shortcode_name The shortcode name.
     
    324332            }
    325333        }
    326        
     334
    327335        /**
    328336         * Returns the value for the class attribute of the image tags of the given shortcode.
    329337         *
    330          * @since 1.0.0 
     338         * @since 1.0.0
    331339         *
    332340         * @param string $shortcode_name The shortcode name.
     
    343351            }
    344352        }
    345        
     353
    346354        /**
    347355         * Returns the class of the container tag which will contains all image tags.
    348356         *
    349          * @since 1.0.0 
     357         * @since 1.0.0
    350358         *
    351359         * @param string $shortcode_name The shortcode name.
     
    362370            }
    363371        }
    364        
    365        
     372
     373
    366374        /**
    367375         * Checks if the given shortcode should create a slideshow.
    368          * 
    369          * @since 1.0.0 
     376         *
     377         * @since 1.0.0
    370378         *
    371379         * @param string $shortcode_name The shortcode name.
     
    378386            return false;
    379387        }
    380        
     388
    381389        /**
    382390         * Returns the slideshow speed as a HTML5 data attribute.
    383          * 
    384          * @since 1.0.0 
     391         *
     392         * @since 1.0.0
    385393         *
    386394         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     
    396404            return '';
    397405        }
    398        
     406
    399407        /**
    400408         * Returns the slideshow dot color as a HTML5 data attribute.
    401          * 
    402          * @since 1.0.0 
     409         *
     410         * @since 1.0.0
    403411         *
    404412         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     
    414422            return '';
    415423        }
    416        
     424
    417425        /**
    418426         * Returns the slideshow active dot color as a HTML5 data attribute.
    419          * 
    420          * @since 1.0.0 
     427         *
     428         * @since 1.0.0
    421429         *
    422430         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     
    432440            return '';
    433441        }
    434        
     442
    435443        /**
    436444         * Returns the slideshow arrow color as a HTML5 data attribute.
    437          * 
    438          * @since 1.0.0 
     445         *
     446         * @since 1.0.0
    439447         *
    440448         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     
    450458            return '';
    451459        }
    452        
     460
    453461        /**
    454462         * Returns the slideshow active arrow color as a HTML5 data attribute.
    455          * 
    456          * @since 1.0.0 
     463         *
     464         * @since 1.0.0
    457465         *
    458466         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     
    468476            return '';
    469477        }
    470        
     478
    471479        /**
    472480         * Returns the user defined time after which the slider automatically changes images.
    473          * 
    474          * @since 1.0.0 
    475          *
    476          * @param array $shortcode_attributes The shortcode attributes entered by the user.
    477          * @param string $shortcode_name The shortcode name.
    478          * @return int Duration in millisonds. Returns -1 if the given shortcode is invalid, 
     481         *
     482         * @since 1.0.0
     483         *
     484         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     485         * @param string $shortcode_name The shortcode name.
     486         * @return int Duration in millisonds. Returns -1 if the given shortcode is invalid,
    479487         *                                     shortcode does not support slideshow speed or
    480488         *                                     user entered an invalid shortcode attribute value (less than 1).
     
    483491            // Search time entered by user and convert it into int.
    484492            $time = intval( $shortcode_attributes[ 'slideshow-speed' ] );
    485            
     493
    486494            if( $time > 0 ) {
    487495                // User defined a valid time via shortcode -> return this time.
     
    492500            }
    493501        }
    494        
     502
    495503        /**
    496504         * Returns the default time after which the slider automatically changes images.
    497505         *
    498          * @since 1.0.0 
     506         * @since 1.0.0
    499507         *
    500508         * @param string $shortcode_name The shortcode name.
     
    508516            return -1;
    509517        }
    510        
     518
    511519        /**
    512520         * Returns the user defined color for the dots of a slideshow.
    513          * 
    514          * @since 1.0.0 
    515          *
    516          * @param array $shortcode_attributes The shortcode attributes entered by the user.
    517          * @param string $shortcode_name The shortcode name.
    518          * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 
     521         *
     522         * @since 1.0.0
     523         *
     524         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     525         * @param string $shortcode_name The shortcode name.
     526         * @return string Color as HEX. Returns empty string if the given shortcode is invalid,
    519527         *                                 shortcode does not support dots or
    520528         *                                 user entered an invalid color as shortcode attribute value.
    521529         */
    522530        private function get_dot_color( $shortcode_attributes, $shortcode_name ) {
    523            
     531
    524532            // Search color entered by user.
    525533            $color = sanitize_hex_color( $shortcode_attributes[ 'slideshow-dot-color' ] );
    526            
     534
    527535            if( isset( $color ) || ! empty( $color ) ) {
    528536                // User defined a valid color via shortcode -> return this color.
     
    533541            }
    534542        }
    535        
     543
    536544        /**
    537545         * Returns the default color for the dots of a slideshow.
    538546         *
    539          * @since 1.0.0 
     547         * @since 1.0.0
    540548         *
    541549         * @param string $shortcode_name The shortcode name.
     
    549557            return '';
    550558        }
    551        
     559
    552560        /**
    553561         * Returns the user defined color for active dots of a slideshow.
    554          * 
    555          * @since 1.0.0 
    556          *
    557          * @param array $shortcode_attributes The shortcode attributes entered by the user.
    558          * @param string $shortcode_name The shortcode name.
    559          * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 
     562         *
     563         * @since 1.0.0
     564         *
     565         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     566         * @param string $shortcode_name The shortcode name.
     567         * @return string Color as HEX. Returns empty string if the given shortcode is invalid,
    560568         *                                 shortcode does not support dots or
    561569         *                                 user entered an invalid color as shortcode attribute value.
    562570         */
    563571        private function get_active_dot_color( $shortcode_attributes, $shortcode_name ) {
    564            
     572
    565573            // Search color entered by user.
    566574            $color = sanitize_hex_color( $shortcode_attributes[ 'slideshow-active-dot-color' ] );
    567            
     575
    568576            if( isset( $color ) || ! empty( $color ) ) {
    569577                // User defined a valid color via shortcode -> return this color.
     
    574582            }
    575583        }
    576        
     584
    577585        /**
    578586         * Returns the default color for active dots of a slideshow.
    579587         *
    580          * @since 1.0.0 
     588         * @since 1.0.0
    581589         *
    582590         * @param string $shortcode_name The shortcode name.
     
    590598            return '';
    591599        }
    592        
     600
    593601        /**
    594602         * Returns the user defined color for the arrows of a slideshow.
    595          * 
    596          * @since 1.0.0 
    597          *
    598          * @param array $shortcode_attributes The shortcode attributes entered by the user.
    599          * @param string $shortcode_name The shortcode name.
    600          * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 
     603         *
     604         * @since 1.0.0
     605         *
     606         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     607         * @param string $shortcode_name The shortcode name.
     608         * @return string Color as HEX. Returns empty string if the given shortcode is invalid,
    601609         *                                 shortcode does not support arrows or
    602610         *                                 user entered an invalid color as shortcode attribute value.
    603611         */
    604612        private function get_arrow_color( $shortcode_attributes, $shortcode_name ) {
    605            
     613
    606614            // Search color entered by user.
    607615            $color = sanitize_hex_color( $shortcode_attributes[ 'slideshow-arrow-color' ] );
    608            
     616
    609617            if( isset( $color ) || ! empty( $color ) ) {
    610618                // User defined a valid color via shortcode -> return this color.
     
    615623            }
    616624        }
    617        
     625
    618626        /**
    619627         * Returns the default color for the arrows of a slideshow.
    620628         *
    621          * @since 1.0.0 
     629         * @since 1.0.0
    622630         *
    623631         * @param string $shortcode_name The shortcode name.
     
    631639            return '';
    632640        }
    633            
     641
    634642        /**
    635643         * Returns the user defined color for active arrows of a slideshow.
    636          * 
    637          * @since 1.0.0 
    638          *
    639          * @param array $shortcode_attributes The shortcode attributes entered by the user.
    640          * @param string $shortcode_name The shortcode name.
    641          * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 
     644         *
     645         * @since 1.0.0
     646         *
     647         * @param array $shortcode_attributes The shortcode attributes entered by the user.
     648         * @param string $shortcode_name The shortcode name.
     649         * @return string Color as HEX. Returns empty string if the given shortcode is invalid,
    642650         *                                 shortcode does not support arrows or
    643651         *                                 user entered an invalid color as shortcode attribute value.
    644652         */
    645653        private function get_active_arrow_color( $shortcode_attributes, $shortcode_name ) {
    646            
     654
    647655            // Search color entered by user.
    648656            $color = sanitize_hex_color( $shortcode_attributes[ 'slideshow-active-arrow-color' ] );
    649            
     657
    650658            if( isset( $color ) || ! empty( $color ) ) {
    651659                // User defined a valid color via shortcode -> return this color.
     
    656664            }
    657665        }
    658        
     666
    659667        /**
    660668         * Returns the default color for active arrows of a slideshow.
    661669         *
    662          * @since 1.0.0 
     670         * @since 1.0.0
    663671         *
    664672         * @param string $shortcode_name The shortcode name.
     
    672680            return '';
    673681        }
    674        
     682
    675683        /**
    676684         * Adjusts the given image tag for the provided shortcode.
    677685         *
    678          * @since 1.0.0 
     686         * @since 1.0.0
    679687         *
    680688         * @param array $img_tag The image tag.
     
    683691         */
    684692        private function adjust_image_tag_by_shortcode( $img_tag, $shortcode_name ) {
    685            
     693
    686694            if( $shortcode_name === $this -> shortcode_insert_default_gallery ) {
    687695                // Currently the default gallery do not need any adjustments -> just return the image tag.
     
    702710            }
    703711        }
    704        
     712
    705713        /**
    706714         * Returns the HTML which should be appended after all image tags.
    707715         *
    708          * @since 1.0.0 
     716         * @since 1.0.0
    709717         *
    710718         * @param string $number_of_image_tags The number of all image tags.
     
    726734                $output .= '<button class="smfi-default-slider-next">&#10095;</button>';
    727735                $output .= '<div class="smfi-default-slider-dot-container">';
    728                     for ( $i = 0 ; $i < $number_of_image_tags; $i++ ){ 
    729                         $output .= '<div class="smfi-default-slider-dot" tabindex=0></div>'; 
     736                    for ( $i = 0 ; $i < $number_of_image_tags; $i++ ){
     737                        $output .= '<div class="smfi-default-slider-dot" tabindex=0></div>';
    730738                    }
    731739                $output .= '</div>';
Note: See TracChangeset for help on using the changeset viewer.