public function shortcode($args = array(), $content = '') { $original_arguments = $args; $codec = new Eab_Codec_ArgumentsCodec(); $args = $codec->parse_arguments($args, array('format' => 'H:i T l', 'class' => '', 'add' => 0, 'expired' => __('Closed', Eab_EventsHub::TEXT_DOMAIN), 'legacy' => false, 'category' => false, 'categories' => false)); if (!empty($args['legacy'])) { return $this->_legacy_shortcode($original_arguments); } $class = !empty($args['class']) ? 'class="' . sanitize_html_class($args['class']) . '"' : ''; $format = "<div {$class}>%s</div>"; $output = ''; $additional = 0; if (!empty($args['add']) && (int) $args['add']) { $additional = (int) $args['add'] * 60; } $query = $codec->get_query_args($args); $now = eab_current_time() + $additional; $events = Eab_CollectionFactory::get_upcoming_events($now, $query); $ret = array(); foreach ($events as $event) { $ts = $event->get_start_timestamp(); if ($ts < $now) { continue; } $ret[$ts] = $event; } ksort($ret); $next = reset($ret); if ($next) { $output = date_i18n($args['format'], $next->get_start_timestamp()); } else { $output = !empty($args['expired']) ? esc_html($args['expired']) : $content; } return sprintf($format, $output); }
public function shortcode($args = array(), $content = '') { $original_arguments = $args; $codec = new Eab_Codec_ArgumentsCodec(); $args = $codec->parse_arguments($args, array('id' => '', 'format' => 'dHMS', 'goto' => '', 'class' => '', 'type' => '', 'size' => 70, 'add' => 0, 'allow_scaling' => false, 'compact' => false, 'title' => false, 'footer_script' => false, 'expired' => __('Closed', Eab_EventsHub::TEXT_DOMAIN), 'legacy' => false, 'category' => false, 'categories' => false, 'weeks' => false)); if (!empty($args['legacy'])) { return $this->_legacy_shortcode($original_arguments); } $class = !empty($args['class']) ? 'class="' . sanitize_html_class($args['class']) . '"' : ''; $id = str_replace(array(" ", "'", '"'), "", $args['id']); // We cannot let spaces and quotes in id // Do not add quotes for page refresh if ($args['goto'] && $args['goto'] != "window.location.href") { $args['goto'] = "'" . str_replace(array("'", '"'), "", $args['goto']) . "'"; } // Do not allow quotes which may break js $transform = false; if ($args['size'] < 70 && !empty($args['allow_scaling'])) { $transform = $args['size'] / 70; } switch ($args['size']) { case 70: $height = 72; break; case 82: $height = 84; break; case 127: $height = 130; break; case 254: $height = 260; break; default: $args['size'] = 70; $height = 72; break; } $sprite_file = plugins_url('/events-and-bookings/img/sprite_' . $args['size'] . 'x' . $height . '.png'); $secs = -1; $additional = 0; if (!empty($args['add']) && (int) $args['add']) { $additional = (int) $args['add'] * 60; } $query = $codec->get_query_args($args); $now = eab_current_time() + $additional; //$events = Eab_CollectionFactory::get_upcoming_events($now, $query); $future_peeking_method = false; if (!empty($args['weeks']) && is_numeric($args['weeks'])) { $future_peeking_method = create_function('', 'return ' . (int) $args['weeks'] . ';'); } if (!empty($future_peeking_method)) { add_filter('eab-collection-upcoming_weeks-week_number', $future_peeking_method); } $events = Eab_CollectionFactory::get_upcoming_weeks_events($now, $query); if (!empty($future_peeking_method)) { remove_filter('eab-collection-upcoming_weeks-week_number', $future_peeking_method); } $ret = array(); foreach ($events as $event) { $ts = $event->get_start_timestamp(); if ($ts < $now) { continue; } $ret[$ts] = $event; } ksort($ret); $next = reset($ret); if ($next) { $secs = $next->get_start_timestamp() - $now; } else { return $content; } $script = ''; $script .= "<script type='text/javascript'>"; $script .= "jQuery(document).ready(function(\$) {"; $script .= "\$('#eab_next_event_countdown" . $id . "').countdown({\n\t\t\t\t\tformat: '" . $args['format'] . "',\n\t\t\t\t\texpiryText: '" . $args['expired'] . "',\n\t\t\t\t\tuntil: " . $secs . ","; if ($args['goto']) { $script .= "onExpiry: eab_next_event_refresh" . $id . ","; } if ($args['type'] == 'flip') { $script .= "onTick: function () { \$(document).trigger('eab-event_countdown-tick', [\$(this), '{$sprite_file}']);},"; } $script .= "alwaysExpire: true});"; if ($args['goto']) { $script .= "function eab_next_event_refresh" . $id . "() {window.location.href=" . $args['goto'] . ";}"; } $script .= "});</script>"; if ('flip' == $args['type']) { $script .= '<script type="text/javascript" src="' . plugins_url(basename(EAB_PLUGIN_DIR) . "/js/event_countdown_flip.js") . '"></script>'; } // remove line breaks to prevent wpautop break the script $script = str_replace(array("\r", "\n", "\t", "<br>", "<br />"), "", preg_replace('/\\s+/m', ' ', $script)); $this->add_countdown = true; $markup = '<div class="eab_next_event_countdown-wrapper">' . ($args['title'] ? '<h4><a href="' . get_permalink($next->get_id()) . '">' . $next->get_title() . '</a></h4>' : '') . "<div id='eab_next_event_countdown{$id}' {$class} data-height='{$height}' data-size='" . $args['size'] . "'></div>" . '</div>'; if ($transform && !empty($args['allow_scaling'])) { $markup .= <<<EOStandardTransformCSS <style type="text/css"> #eab_next_event_countdown{$id} .countdown_section { \ttransform: scale({$transform},{$transform}); \t-ms-transform: scale({$transform},{$transform}); \t-webkit-transform: scale({$transform},{$transform}); } </style> EOStandardTransformCSS; } if (!empty($args['size']) && !empty($args['compact'])) { $base_size = $transform && !empty($args['allow_scaling']) ? $args['size'] * $transform : $args['size']; $max_width = $base_size * 8 + 20; $markup .= <<<EOStandardCompactCSS <style type="text/css"> #eab_next_event_countdown{$id} { \tmax-width: {$max_width}px; } </style> EOStandardCompactCSS; } if ($args['footer_script'] && in_array($args['footer_script'], array('yes', 'true', '1'))) { self::add_script($script); add_action('wp_footer', array($this, 'inject_queued_scripts'), 99); } else { $markup .= $script; } return $markup; }