Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
View_Helpers.php
349 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Various helper methods used in views |
| 4 | */ |
| 5 | |
| 6 | // Don't load directly |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | die( '-1' ); |
| 9 | } |
| 10 | |
| 11 | if ( ! class_exists( 'Tribe__View_Helpers' ) ) { |
| 12 | class Tribe__View_Helpers { |
| 13 | |
| 14 | /** |
| 15 | * Get the countries being used and available for the plugin. |
| 16 | * |
| 17 | * @param string $postId The post ID. |
| 18 | * @param bool $useDefault Should we use the defaults? |
| 19 | * |
| 20 | * @return array The countries array. |
| 21 | */ |
| 22 | public static function constructCountries( $postId = '', $useDefault = true ) { |
| 23 | static $cache_var_name = __METHOD__; |
| 24 | |
| 25 | $countries = tribe_get_var( $cache_var_name, null ); |
| 26 | |
| 27 | if ( $countries ) { |
| 28 | return $countries; |
| 29 | } |
| 30 | |
| 31 | $eventCountries = tribe_get_option( 'tribeEventsCountries' ); |
| 32 | |
| 33 | if ( $eventCountries != '' ) { |
| 34 | $countries = []; |
| 35 | |
| 36 | $country_rows = explode( "\n", $eventCountries ); |
| 37 | foreach ( $country_rows as $crow ) { |
| 38 | $country = explode( ',', $crow ); |
| 39 | if ( isset( $country[0] ) && isset( $country[1] ) ) { |
| 40 | $country[0] = trim( $country[0] ); |
| 41 | $country[1] = trim( $country[1] ); |
| 42 | |
| 43 | if ( $country[0] && $country[1] ) { |
| 44 | $countries[ $country[0] ] = $country[1]; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if ( ! isset( $countries ) || ! is_array( $countries ) || count( $countries ) < 1 ) { |
| 51 | $countries = tribe( 'languages.locations' )->get_countries(); |
| 52 | } |
| 53 | |
| 54 | // Perform a natural sort: this maintains the key -> index associations but ensures the countries |
| 55 | // are in the expected order, even once translated |
| 56 | natsort( $countries ); |
| 57 | |
| 58 | // Placeholder option ('Select a Country') first by default |
| 59 | $select_country = [ '' => esc_html__( 'Select a Country:', 'tribe-common' ) ]; |
| 60 | $countries = $select_country + $countries; |
| 61 | |
| 62 | if ( ( $postId || $useDefault ) ) { |
| 63 | $countryValue = get_post_meta( $postId, '_EventCountry', true ); |
| 64 | if ( $countryValue ) { |
| 65 | $defaultCountry = [ array_search( $countryValue, $countries ), $countryValue ]; |
| 66 | } else { |
| 67 | $defaultCountry = tribe_get_default_value( 'country' ); |
| 68 | } |
| 69 | if ( $defaultCountry && $defaultCountry[0] != '' ) { |
| 70 | $selectCountry = array_shift( $countries ); |
| 71 | asort( $countries ); |
| 72 | $countries = [ $defaultCountry[0] => $defaultCountry[1] ] + $countries; |
| 73 | $countries = [ '' => $selectCountry ] + $countries; |
| 74 | array_unique( $countries ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | tribe_set_var( $cache_var_name, $countries ); |
| 79 | |
| 80 | return $countries; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the i18ned states available to the plugin. |
| 85 | * |
| 86 | * @return array The states array. |
| 87 | */ |
| 88 | public static function loadStates() { |
| 89 | $states = tribe( 'languages.locations' )->get_us_states(); |
| 90 | |
| 91 | /** |
| 92 | * Enables filtering the list of states in the USA available to venues. |
| 93 | * |
| 94 | * @since 4.5.12 |
| 95 | * |
| 96 | * @param array $states The list of states. |
| 97 | */ |
| 98 | return apply_filters( 'tribe_get_state_options', $states ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Builds a set of options for displaying an hour chooser |
| 103 | * |
| 104 | * @param string $date the current date (optional) |
| 105 | * @param bool $isStart |
| 106 | * |
| 107 | * @return string a set of HTML options with hours (current hour selected) |
| 108 | */ |
| 109 | public static function getHourOptions( $date = '', $isStart = false ) { |
| 110 | $hours = self::hours(); |
| 111 | |
| 112 | if ( count( $hours ) == 12 ) { |
| 113 | $h = 'h'; |
| 114 | } else { |
| 115 | $h = 'H'; |
| 116 | } |
| 117 | $options = ''; |
| 118 | |
| 119 | if ( empty( $date ) ) { |
| 120 | $hour = ( $isStart ) ? '08' : ( count( $hours ) == 12 ? '05' : '17' ); |
| 121 | } else { |
| 122 | $timestamp = strtotime( $date ); |
| 123 | $hour = date( $h, $timestamp ); |
| 124 | // fix hours if time_format has changed from what is saved |
| 125 | if ( preg_match( '(pm|PM)', $timestamp ) && $h == 'H' ) { |
| 126 | $hour = $hour + 12; |
| 127 | } |
| 128 | if ( $hour > 12 && $h == 'h' ) { |
| 129 | $hour = $hour - 12; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | $hour = apply_filters( 'tribe_get_hour_options', $hour, $date, $isStart ); |
| 134 | |
| 135 | foreach ( $hours as $hourText ) { |
| 136 | if ( $hour == $hourText ) { |
| 137 | $selected = 'selected="selected"'; |
| 138 | } else { |
| 139 | $selected = ''; |
| 140 | } |
| 141 | $options .= "<option value='$hourText' $selected>$hourText</option>\n"; |
| 142 | } |
| 143 | |
| 144 | return $options; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Builds a set of options for displaying a minute chooser |
| 149 | * |
| 150 | * @param string $date the current date (optional) |
| 151 | * @param bool $isStart |
| 152 | * |
| 153 | * @return string a set of HTML options with minutes (current minute selected) |
| 154 | */ |
| 155 | public static function getMinuteOptions( $date = '', $isStart = false ) { |
| 156 | $options = ''; |
| 157 | |
| 158 | if ( empty( $date ) ) { |
| 159 | $minute = '00'; |
| 160 | } else { |
| 161 | $minute = date( 'i', strtotime( $date ) ); |
| 162 | } |
| 163 | |
| 164 | $minute = apply_filters( 'tribe_get_minute_options', $minute, $date, $isStart ); |
| 165 | $minutes = self::minutes( $minute ); |
| 166 | |
| 167 | foreach ( $minutes as $minuteText ) { |
| 168 | if ( $minute == $minuteText ) { |
| 169 | $selected = 'selected="selected"'; |
| 170 | } else { |
| 171 | $selected = ''; |
| 172 | } |
| 173 | $options .= "<option value='$minuteText' $selected>$minuteText</option>\n"; |
| 174 | } |
| 175 | |
| 176 | return $options; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Helper method to return an array of 1-12 for hours |
| 181 | * |
| 182 | * @return array The hours array. |
| 183 | */ |
| 184 | private static function hours() { |
| 185 | $hours = []; |
| 186 | $rangeMax = self::is_24hr_format() ? 23 : 12; |
| 187 | $rangeStart = $rangeMax > 12 ? 0 : 1; |
| 188 | foreach ( range( $rangeStart, $rangeMax ) as $hour ) { |
| 189 | if ( $hour < 10 ) { |
| 190 | $hour = '0' . $hour; |
| 191 | } |
| 192 | $hours[ $hour ] = $hour; |
| 193 | } |
| 194 | |
| 195 | // In a 12hr context lets put 12 at the start (so the sequence will run 12, 1, 2, 3 ... 11) |
| 196 | if ( 12 === $rangeMax ) { |
| 197 | array_unshift( $hours, array_pop( $hours ) ); |
| 198 | } |
| 199 | |
| 200 | return $hours; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Determines if the provided date/time format (or else the default WordPress time_format) |
| 205 | * is 24hr or not. |
| 206 | * |
| 207 | * In inconclusive cases, such as if there are now hour-format characters, 12hr format is |
| 208 | * assumed. |
| 209 | * |
| 210 | * @param null $format |
| 211 | * @return bool |
| 212 | */ |
| 213 | public static function is_24hr_format( $format = null ) { |
| 214 | // Use the provided format or else use the value of the current time_format setting |
| 215 | $format = ( null === $format ) ? get_option( 'time_format', Tribe__Date_Utils::TIMEFORMAT ) : $format; |
| 216 | |
| 217 | // Count instances of the H and G symbols |
| 218 | $h_symbols = substr_count( $format, 'H' ); |
| 219 | $g_symbols = substr_count( $format, 'G' ); |
| 220 | |
| 221 | // If none have been found then consider the format to be 12hr |
| 222 | if ( ! $h_symbols && ! $g_symbols ) return false; |
| 223 | |
| 224 | // It's possible H or G have been included as escaped characters |
| 225 | $h_escaped = substr_count( $format, '\H' ); |
| 226 | $g_escaped = substr_count( $format, '\G' ); |
| 227 | |
| 228 | // Final check, accounting for possibility of escaped values |
| 229 | return ( $h_symbols > $h_escaped || $g_symbols > $g_escaped ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Helper method to return an array of 00-59 for minutes |
| 234 | * |
| 235 | * @param int $exact_minute optionally specify an exact minute to be included (outwith the default intervals) |
| 236 | * |
| 237 | * @return array The minutes array. |
| 238 | */ |
| 239 | private static function minutes( $exact_minute = 0 ) { |
| 240 | $minutes = []; |
| 241 | |
| 242 | // The exact minute should be an absint between 0 and 59 |
| 243 | $exact_minute = absint( $exact_minute ); |
| 244 | |
| 245 | if ( $exact_minute < 0 || $exact_minute > 59 ) { |
| 246 | $exact_minute = 0; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Filters the amount of minutes to increment the minutes drop-down by |
| 251 | * |
| 252 | * @param int Increment amount (defaults to 5) |
| 253 | */ |
| 254 | $default_increment = apply_filters( 'tribe_minutes_increment', 5 ); |
| 255 | |
| 256 | // Unless an exact minute has been specified we can minimize the amount of looping we do |
| 257 | $increment = ( 0 === $exact_minute ) ? $default_increment : 1; |
| 258 | |
| 259 | for ( $minute = 0; $minute < 60; $minute += $increment ) { |
| 260 | // Skip if this $minute doesn't meet the increment pattern and isn't an additional exact minute |
| 261 | if ( 0 !== $minute % $default_increment && $exact_minute !== $minute ) { |
| 262 | continue; |
| 263 | } |
| 264 | |
| 265 | if ( $minute < 10 ) { |
| 266 | $minute = '0' . $minute; |
| 267 | } |
| 268 | $minutes[ $minute ] = $minute; |
| 269 | } |
| 270 | |
| 271 | return $minutes; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Builds a set of options for diplaying a meridian chooser |
| 276 | * |
| 277 | * @param string $date YYYY-MM-DD HH:MM:SS to select (optional) |
| 278 | * @param bool $isStart |
| 279 | * |
| 280 | * @return string a set of HTML options with all meridians |
| 281 | */ |
| 282 | public static function getMeridianOptions( $date = '', $isStart = false ) { |
| 283 | if ( strstr( get_option( 'time_format', Tribe__Date_Utils::TIMEFORMAT ), 'A' ) ) { |
| 284 | $a = 'A'; |
| 285 | $meridians = [ 'AM', 'PM' ]; |
| 286 | } else { |
| 287 | $a = 'a'; |
| 288 | $meridians = [ 'am', 'pm' ]; |
| 289 | } |
| 290 | if ( empty( $date ) ) { |
| 291 | $meridian = ( $isStart ) ? $meridians[0] : $meridians[1]; |
| 292 | } else { |
| 293 | $meridian = date( $a, strtotime( $date ) ); |
| 294 | } |
| 295 | |
| 296 | $meridian = apply_filters( 'tribe_get_meridian_options', $meridian, $date, $isStart ); |
| 297 | |
| 298 | $return = ''; |
| 299 | foreach ( $meridians as $m ) { |
| 300 | $return .= "<option value='$m'"; |
| 301 | if ( $m == $meridian ) { |
| 302 | $return .= ' selected="selected"'; |
| 303 | } |
| 304 | $return .= ">$m</option>\n"; |
| 305 | } |
| 306 | |
| 307 | return $return; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Helper method to return an array of years |
| 312 | * default is back 5 and forward 5 |
| 313 | * |
| 314 | * @return array The array of years. |
| 315 | */ |
| 316 | private static function years() { |
| 317 | $current_year = (int) date_i18n( 'Y' ); |
| 318 | $years_back = (int) apply_filters( 'tribe_years_to_go_back', 5, $current_year ); |
| 319 | $years_forward = (int) apply_filters( 'tribe_years_to_go_forward', 5, $current_year ); |
| 320 | $years = []; |
| 321 | for ( $i = $years_back; $i > 0; $i -- ) { |
| 322 | $year = $current_year - $i; |
| 323 | $years[] = $year; |
| 324 | } |
| 325 | $years[] = $current_year; |
| 326 | for ( $i = 1; $i <= $years_forward; $i ++ ) { |
| 327 | $year = $current_year + $i; |
| 328 | $years[] = $year; |
| 329 | } |
| 330 | |
| 331 | return (array) apply_filters( 'tribe_years_array', $years ); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Helper method to return an array of 1-31 for days |
| 336 | * |
| 337 | * @return array The days array. |
| 338 | */ |
| 339 | public static function days( $totalDays ) { |
| 340 | $days = []; |
| 341 | foreach ( range( 1, $totalDays ) as $day ) { |
| 342 | $days[ $day ] = $day; |
| 343 | } |
| 344 | |
| 345 | return $days; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 |