| @@ -5,71 +5,122 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | class CoolClock_Shortcode { |
| 8 | 8 | |
| 9 | - public static function handle_shortcode( $atts, $content = null ) | |
| 10 | - { | |
| 11 | - if ( is_feed() ) | |
| 12 | - return ''; | |
| 13 | - | |
| 14 | - $atts = shortcode_atts( array_merge( CoolClock::$defaults, CoolClock::$advanced_defaults ), $atts, 'coolclock' ); | |
| 15 | - | |
| 9 | + public static function handle_shortcode( $atts, $content = null ) { | |
| 16 | 10 | /** |
| 17 | - * skin Must be one of these: 'swissRail' (default skin), 'chunkySwiss', 'chunkySwissOnBlack', 'fancy', 'machine', 'simonbaird_com', 'classic', 'modern', 'simple', 'securephp', 'Tes2', 'Lev', 'Sand', 'Sun', 'Tor', 'Cold', 'Babosa', 'Tumb', 'Stone', 'Disc', 'watermelon' or 'mister'. | |
| 18 | - * If the Advanced extension is activated, there is also 'minimal' available. | |
| 19 | - * radius A number to define the clock radius. Do not add 'px' or any other measure descriptor. | |
| 20 | - * noseconds Set to true (or 1) to hide the second hand | |
| 21 | - * gmtoffset A number to define a timezone relative the Greenwhich Mean Time. Do not set this parameter to default to local time. | |
| 22 | - * showdigital Set to 'digital12' to show the time in 12h digital format (with am/pm) too | |
| 23 | - * digitalcolor Set to a color value to change the digital time color | |
| 24 | - * scale Must be one of these: 'linear' (default scale), 'logClock' or 'logClockRev'. Linear is our normal clock scale, the other two show a logarithmic time scale | |
| 25 | - * subtext Optional text, centered below the clock | |
| 26 | - * align Sets floating of the clock: 'left', 'right' or 'center' | |
| 11 | + * skin Must be one of these: 'swissRail' (default skin), 'chunkySwiss', 'chunkySwissOnBlack', 'fancy', 'machine', 'simonbaird_com', 'classic', 'modern', 'simple', 'securephp', 'Tes2', 'Lev', 'Sand', 'Sun', 'Tor', 'Cold', 'Babosa', 'Tumb', 'Stone', 'Disc', 'watermelon' or 'mister'. | |
| 12 | + * If the Advanced extension is activated, there is also 'minimal' available. | |
| 13 | + * radius A number to define the clock radius. Do not add 'px' or any other measure descriptor. | |
| 14 | + * noseconds Set to 'true' or 1 or without value to hide the second hand. | |
| 15 | + * gmtoffset A number to define a timezone relative the Greenwhich Mean Time. Do not set this parameter to default to local time. | |
| 16 | + * showdigital Set to 'true' or 1 or 'digital12' or without value to show the time in 12h digital format (with am/pm) too | |
| 17 | + * fontcolor Set to a color value to change the digital time color, digitalcolor for backward compatibility | |
| 18 | + * scale Must be one of these: 'linear' (default scale), 'logClock' or 'logClockRev'. Linear is our normal clock scale, the other two show a logarithmic time scale | |
| 19 | + * subtext Optional text, centered below the clock | |
| 20 | + * align Sets floating of the clock: 'left', 'right' or 'center' | |
| 27 | 21 | */ |
| 28 | 22 | |
| 29 | - // set footer script flags | |
| 30 | - CoolClock::$add_script = true; | |
| 23 | + if ( is_feed() ) { | |
| 24 | + return ''; | |
| 25 | + } | |
| 31 | 26 | |
| 32 | - $atts['skin'] = !empty($atts['skin']) ? self::parse_skin( $atts['skin'], $content ) : CoolClock::$defauls['skin']; | |
| 27 | + // set empty parameter noseconds to default | |
| 28 | + if ( ! empty( $atts ) && in_array( 'noseconds', $atts, true ) && ! array_key_exists( 'noseconds', $atts ) ) { | |
| 29 | + $atts['noseconds'] = true; | |
| 30 | + } | |
| 31 | + // set empty parameter showdigital to default | |
| 32 | + if ( ! empty( $atts ) && in_array( 'showdigital', $atts, true ) && ! array_key_exists( 'showdigital', $atts ) ) { | |
| 33 | + $atts['showdigital'] = 'digital12'; | |
| 34 | + } | |
| 33 | 35 | |
| 34 | - // get output | |
| 35 | - $output = CoolClock::canvas( $atts ); | |
| 36 | + // filter shortcode attributes | |
| 37 | + $defaults = array_merge( CoolClock::$defaults, CoolClock::$advanced_defaults ); | |
| 38 | + $atts = shortcode_atts( $defaults, $atts, 'coolclock' ); | |
| 36 | 39 | |
| 37 | - return apply_filters( 'coolclock_shortcode_advanced', $output, $atts, $content ); | |
| 38 | - } | |
| 40 | + // sanitize user input | |
| 41 | + if ( $content ) { | |
| 42 | + $content = wp_strip_all_tags( $content ); | |
| 43 | + } | |
| 39 | 44 | |
| 40 | - private static function parse_skin( $skin_name, $content ) | |
| 41 | - { | |
| 42 | - // look trhough the default skins first | |
| 43 | - foreach ( CoolClock::$default_skins as $skin ) { | |
| 44 | - if ( strtolower($skin_name) == strtolower($skin) ) | |
| 45 | - // return the matching skin | |
| 46 | - return $skin; | |
| 45 | + // backward compat fontcolor | |
| 46 | + if ( !empty( $atts['digitalcolor'] ) && empty($atts['fontcolor']) ) { | |
| 47 | + $atts['fontcolor'] = $atts['digitalcolor']; | |
| 47 | 48 | } |
| 48 | 49 | |
| 49 | - // still here? then search in the $more_skins array | |
| 50 | - foreach ( CoolClock::$more_skins as $skin ) { | |
| 51 | - if ( strtolower($skin_name) == strtolower($skin) ) { | |
| 52 | - // set more_skins flag | |
| 53 | - CoolClock::$add_moreskins = true; | |
| 54 | - // return the matching skin | |
| 55 | - return $skin; | |
| 56 | - } | |
| 50 | + // pre-treat possible empty attributes | |
| 51 | + if ( is_int( array_search( 'noseconds', $atts ) ) ) { | |
| 52 | + $atts['noseconds'] = true; | |
| 57 | 53 | } |
| 54 | + if ( is_int( array_search( 'showdigital', $atts ) ) ) { | |
| 55 | + $atts['showdigital'] = 'digital12'; | |
| 56 | + } | |
| 58 | 57 | |
| 59 | - // still here? then skin is a custom skin | |
| 58 | + // parse skin | |
| 59 | + $atts['skin'] = CoolClock::parse_skin( $atts['skin'], $content ); | |
| 60 | 60 | |
| 61 | - // add custom skin parameters to the advanced skins array | |
| 62 | - if ( !in_array( $skin_name, CoolClock::$advanced_skins ) && !empty( $content ) ) { | |
| 63 | - CoolClock::$advanced_skins[] = $skin_name; | |
| 64 | - CoolClock::$advanced_skins_config[$skin_name] = wp_strip_all_tags( $content, true ); | |
| 61 | + // radius, used in wrapper style and coolclock fields | |
| 62 | + $atts['radius'] = !empty( $atts['radius'] ) && is_numeric($atts['radius']) ? (int) $atts['radius'] : $defaults['radius']; | |
| 63 | + if ( 10 > $atts['radius'] ) $atts['radius'] = 10; // absolute minimum size 20x20 | |
| 64 | + | |
| 65 | + // clean gmtoffset | |
| 66 | + if ( !empty( $atts['gmtoffset'] ) ) { | |
| 67 | + $atts['gmtoffset'] = str_replace( ',', '.', $atts['gmtoffset'] ); | |
| 68 | + $atts['gmtoffset'] = str_replace( array('1/2','½'), '.5', $atts['gmtoffset'] ); | |
| 69 | + $atts['gmtoffset'] = str_replace( array('h',' '), '', $atts['gmtoffset'] ); | |
| 70 | + $atts['gmtoffset'] = is_numeric( $atts['gmtoffset'] ) ? (float) trim( $atts['gmtoffset'] ) : ''; | |
| 65 | 71 | } |
| 66 | 72 | |
| 67 | - return $skin_name; | |
| 73 | + if ( !empty( $atts['scale'] ) ) | |
| 74 | + $atts['scale'] = wp_strip_all_tags( $atts['scale'] ); | |
| 75 | + | |
| 76 | + // post-treat showdigital | |
| 77 | + if ( in_array( $atts['showdigital'], array('true','1') ) ) | |
| 78 | + $atts['showdigital'] = true; | |
| 79 | + elseif ( !in_array( $atts['showdigital'], array('false','0') ) ) | |
| 80 | + $atts['showdigital'] = wp_strip_all_tags( $atts['showdigital'] ); | |
| 81 | + else | |
| 82 | + $atts['showdigital'] = ''; | |
| 83 | + | |
| 84 | + // post-treat noseconds | |
| 85 | + if ( 'false' === $atts['noseconds'] ) | |
| 86 | + $atts['noseconds'] = false; | |
| 87 | + | |
| 88 | + if ( !empty( $atts['fontcolor'] ) ) | |
| 89 | + $atts['fontcolor'] = CoolClock::colorval( $atts['fontcolor'] ); | |
| 90 | + | |
| 91 | + if ( !empty( $atts['font'] ) ) | |
| 92 | + $atts['font'] = wp_strip_all_tags( $atts['font'] ); | |
| 93 | + | |
| 94 | + if ( !empty( $atts['subtext'] ) ) | |
| 95 | + $atts['subtext'] = wp_kses( $atts['subtext'], CoolClock::$allowed_tags ); | |
| 96 | + | |
| 97 | + $align = !empty( $atts['align'] ) ? wp_strip_all_tags( $atts['align'] ) : $defaults['align']; | |
| 98 | + $class = in_array( $align, array('left','right','center') ) ? ' align' . $align : ''; | |
| 99 | + | |
| 100 | + // inline container style | |
| 101 | + $styles = array( | |
| 102 | + 'width' => 2 * $atts['radius'] . 'px', | |
| 103 | + 'height' => 'auto' // leave height:auto at the end for old pro filter to find and replace | |
| 104 | + ); | |
| 105 | + $styles = apply_filters( 'coolclock_container_styles', $styles, $atts, $defaults ); | |
| 106 | + | |
| 107 | + // set footer script flags | |
| 108 | + CoolClock::$add_script = true; | |
| 109 | + | |
| 110 | + // Build output | |
| 111 | + // begin wrapper | |
| 112 | + $output = '<div class="coolclock-container ' . $class . '"' . CoolClock::inline_style( $styles ) . '>'; // should end with height:auto"> for old pro plugin to find and replace | |
| 113 | + // add canvas | |
| 114 | + $output .= CoolClock::canvas( $atts ); | |
| 115 | + // end wrapper | |
| 116 | + $output .= '</div>'; | |
| 117 | + | |
| 118 | + // Return filtered output | |
| 119 | + return apply_filters( 'coolclock_shortcode', $output, $atts, $content ); | |
| 68 | 120 | } |
| 69 | 121 | |
| 70 | - public static function no_wptexturize( $shortcodes ) | |
| 71 | - { | |
| 122 | + public static function no_wptexturize( $shortcodes ) { | |
| 72 | 123 | $shortcodes[] = 'coolclock'; |
| 73 | 124 | return $shortcodes; |
| 74 | 125 | } |
| 75 | 126 | |