PageRenderTime 327ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/the-events-calendar/common/src/functions/template-tags/general.php

https://bitbucket.org/johnhorsema/canaan_wordpress
PHP | 692 lines | 263 code | 71 blank | 358 comment | 44 complexity | 028bb44c20fd689a2a1cbbebc52f7ac7 MD5 | raw file
Possible License(s): MIT, Apache-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * Display functions (template-tags) for use in WordPress templates.
  4. */
  5. // Don't load directly
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. die( '-1' );
  8. }
  9. if ( ! class_exists( 'Tribe__Main' ) ) {
  10. return;
  11. }
  12. if ( ! function_exists( 'tribe_get_option' ) ) {
  13. /**
  14. * Get Options
  15. *
  16. * Retrieve specific key from options array, optionally provide a default return value
  17. *
  18. * @category Events
  19. * @param string $optionName Name of the option to retrieve.
  20. * @param string $default Value to return if no such option is found.
  21. *
  22. * @return mixed Value of the option if found.
  23. * @todo Abstract this function out of template tags or otherwise secure it from other namespace conflicts.
  24. */
  25. function tribe_get_option( $optionName, $default = '' ) {
  26. return apply_filters( 'tribe_get_option', Tribe__Settings_Manager::get_option( $optionName, $default ), $optionName, $default );
  27. }
  28. }//end if
  29. if ( ! function_exists( 'tribe_update_option' ) ) {
  30. /**
  31. * Update Option
  32. *
  33. * Set specific key from options array, optionally provide a default return value
  34. *
  35. * @category Events
  36. * @param string $optionName Name of the option to retrieve.
  37. * @param string $value Value to save
  38. *
  39. * @return bool
  40. */
  41. function tribe_update_option( $optionName, $value ) {
  42. return Tribe__Settings_Manager::set_option( $optionName, $value );
  43. }
  44. }//end if
  45. if ( ! function_exists( 'tribe_get_network_option' ) ) {
  46. /**
  47. * Get Network Options
  48. *
  49. * Retrieve specific key from options array, optionally provide a default return value
  50. *
  51. * @category Events
  52. * @param string $optionName Name of the option to retrieve.
  53. * @param string $default Value to return if no such option is found.
  54. *
  55. * @return mixed Value of the option if found.
  56. * @todo Abstract this function out of template tags or otherwise secure it from other namespace conflicts.
  57. */
  58. function tribe_get_network_option( $optionName, $default = '' ) {
  59. return Tribe__Settings_Manager::get_network_option( $optionName, $default );
  60. }
  61. }
  62. if ( ! function_exists( 'tribe_resource_url' ) ) {
  63. /**
  64. * Returns or echoes a url to a file in the Events Calendar plugin resources directory
  65. *
  66. * @category Events
  67. *
  68. * @param string $resource the filename of the resource
  69. * @param bool $echo whether or not to echo the url
  70. * @param string $root_dir directory to hunt for resource files (null or the actual path)
  71. * @param object $origin Which plugin we are dealing with
  72. *
  73. * @return string
  74. **/
  75. function tribe_resource_url( $resource, $echo = false, $root_dir = null, $origin = null ) {
  76. $extension = pathinfo( $resource, PATHINFO_EXTENSION );
  77. $resource_path = $root_dir;
  78. if ( is_null( $resource_path ) ) {
  79. $resources_path = 'src/resources/';
  80. switch ( $extension ) {
  81. case 'css':
  82. $resource_path = $resources_path .'css/';
  83. break;
  84. case 'js':
  85. $resource_path = $resources_path .'js/';
  86. break;
  87. case 'scss':
  88. $resource_path = $resources_path .'scss/';
  89. break;
  90. default:
  91. $resource_path = $resources_path;
  92. break;
  93. }
  94. }
  95. $path = $resource_path . $resource;
  96. if ( is_object( $origin ) ) {
  97. $plugin_path = trailingslashit( ! empty( $origin->plugin_path ) ? $origin->plugin_path : $origin->pluginPath );
  98. } else {
  99. $plugin_path = trailingslashit( dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) );
  100. }
  101. $file = wp_normalize_path( $plugin_path . $path );
  102. // Turn the Path into a URL
  103. $url = plugins_url( basename( $file ), $file );
  104. /**
  105. * Filters the resource URL
  106. *
  107. * @param $url
  108. * @param $resource
  109. */
  110. $url = apply_filters( 'tribe_resource_url', $url, $resource );
  111. /**
  112. * Deprecated the tribe_events_resource_url filter in 4.0 in favor of tribe_resource_url. Remove in 5.0
  113. */
  114. $url = apply_filters( 'tribe_events_resource_url', $url, $resource );
  115. if ( $echo ) {
  116. echo $url;
  117. }
  118. return $url;
  119. }
  120. }//end if
  121. if ( ! function_exists( 'tribe_multi_line_remove_empty_lines' ) ) {
  122. /**
  123. * helper function to remove empty lines from multi-line strings
  124. *
  125. * @category Events
  126. * @link http://stackoverflow.com/questions/709669/how-do-i-remove-blank-lines-from-text-in-php
  127. *
  128. * @param string $multi_line_string a multiline string
  129. *
  130. * @return string the same string without empty lines
  131. */
  132. function tribe_multi_line_remove_empty_lines( $multi_line_string ) {
  133. return preg_replace( "/^\n+|^[\t\s]*\n+/m", '', $multi_line_string );
  134. }
  135. }//end if
  136. if ( ! function_exists( 'tribe_get_date_format' ) ) {
  137. /**
  138. * Get the date format specified in the tribe options
  139. *
  140. * @category Events
  141. * @param bool $with_year
  142. *
  143. * @return mixed
  144. */
  145. function tribe_get_date_format( $with_year = false ) {
  146. if ( $with_year ) {
  147. $format = tribe_get_date_option( 'dateWithYearFormat', get_option( 'date_format' ) );
  148. } else {
  149. $format = tribe_get_date_option( 'dateWithoutYearFormat', 'F j' );
  150. }
  151. return apply_filters( 'tribe_date_format', $format );
  152. }
  153. }//end if
  154. if ( ! function_exists( 'tribe_get_datetime_format' ) ) {
  155. /**
  156. * Get the Datetime Format
  157. *
  158. * @category Events
  159. *
  160. * @param bool $with_year
  161. *
  162. * @return mixed|void
  163. */
  164. function tribe_get_datetime_format( $with_year = false ) {
  165. $raw_separator = tribe_get_option( 'dateTimeSeparator', ' @ ' );
  166. $separator = (array) str_split( $raw_separator );
  167. if ( empty( $raw_separator ) ) {
  168. /**
  169. * Filterable fallback for when the dateTimeSeparator is an empty string. Defaults to a space.
  170. *
  171. * @since 4.5.6
  172. *
  173. * @param string $fallback The string to use as the fallback.
  174. * @param string $raw_separator The raw value of the dateTimeSeparator option.
  175. * @return string
  176. */
  177. $separator[0] = apply_filters( 'tribe_empty_datetime_separator_fallback', ' ', $raw_separator );
  178. }
  179. $format = tribe_get_date_format( $with_year );
  180. $format .= ( ! empty( $separator ) ? '\\' : '' ) . implode( '\\', $separator );
  181. $format .= get_option( 'time_format' );
  182. return apply_filters( 'tribe_datetime_format', $format );
  183. }
  184. }//end if
  185. if ( ! function_exists( 'tribe_get_time_format' ) ) {
  186. /**
  187. * Get the time format
  188. *
  189. * @category Events
  190. *
  191. * @return mixed|void
  192. */
  193. function tribe_get_time_format( ) {
  194. $format = get_option( 'time_format' );
  195. return apply_filters( 'tribe_time_format', $format );
  196. }
  197. }//end if
  198. if ( ! function_exists( 'tribe_get_days_between' ) ) {
  199. /**
  200. * Accepts two dates and returns the number of days between them
  201. *
  202. * @category Events
  203. *
  204. * @param string $start_date
  205. * @param string $end_date
  206. * @param string|bool $day_cutoff
  207. *
  208. * @return int
  209. * @see Tribe__Date_Utils::date_diff()
  210. **/
  211. function tribe_get_days_between( $start_date, $end_date, $day_cutoff = '00:00' ) {
  212. if ( $day_cutoff === false ) {
  213. $day_cutoff = '00:00';
  214. } elseif ( $day_cutoff === true ) {
  215. $day_cutoff = tribe_get_option( 'multiDayCutoff', '00:00' );
  216. }
  217. $start_date = new DateTime( $start_date );
  218. if ( $start_date < new DateTime( $start_date->format( 'Y-m-d ' . $day_cutoff ) ) ) {
  219. $start_date->modify( '-1 day' );
  220. }
  221. $end_date = new DateTime( $end_date );
  222. if ( $end_date <= new DateTime( $end_date->format( 'Y-m-d ' . $day_cutoff ) ) ) {
  223. $end_date->modify( '-1 day' );
  224. }
  225. return Tribe__Date_Utils::date_diff( $start_date->format( 'Y-m-d ' . $day_cutoff ), $end_date->format( 'Y-m-d ' . $day_cutoff ) );
  226. }
  227. }//end if
  228. if ( ! function_exists( 'tribe_prepare_for_json' ) ) {
  229. /**
  230. * Function to prepare content for use as a value in a json encoded string destined for storage on a html data attribute.
  231. * Hence the double quote fun, especially in case they pass html encoded &quot; along. Any of those getting through to the data att will break jquery's parseJSON method.
  232. * Themers can use this function to prepare data they may want to send to tribe_events_template_data() in the templates, and we use it in that function ourselves.
  233. *
  234. * @category Events
  235. *
  236. * @param $string
  237. *
  238. * @return string
  239. */
  240. function tribe_prepare_for_json( $string ) {
  241. $value = trim( htmlspecialchars( $string, ENT_QUOTES, 'UTF-8' ) );
  242. $value = str_replace( '&quot;', '"', $value );
  243. return $value;
  244. }
  245. }//end if
  246. if ( ! function_exists( 'tribe_prepare_for_json_deep' ) ) {
  247. /**
  248. * Recursively iterate through an nested structure, calling
  249. * tribe_prepare_for_json() on all scalar values
  250. *
  251. * @category Events
  252. *
  253. * @param mixed $value The data to be cleaned
  254. *
  255. * @return mixed The clean data
  256. */
  257. function tribe_prepare_for_json_deep( $value ) {
  258. if ( is_array( $value ) ) {
  259. $value = array_map( 'tribe_prepare_for_json_deep', $value );
  260. } elseif ( is_object( $value ) ) {
  261. $vars = get_object_vars( $value );
  262. foreach ( $vars as $key => $data ) {
  263. $value->{$key} = tribe_prepare_for_json_deep( $data );
  264. }
  265. } elseif ( is_string( $value ) ) {
  266. $value = tribe_prepare_for_json( $value );
  267. }
  268. return $value;
  269. }
  270. }//end if
  271. if ( ! function_exists( 'tribe_the_notices' ) ) {
  272. /**
  273. * Generates html for any notices that have been queued on the current view
  274. *
  275. * @category Events
  276. *
  277. * @param bool $echo Whether or not to echo the notices html
  278. *
  279. * @return void | string
  280. * @see Tribe__Notices::get()
  281. **/
  282. function tribe_the_notices( $echo = true ) {
  283. $notices = Tribe__Notices::get();
  284. $html = ! empty( $notices ) ? '<div class="tribe-events-notices"><ul><li>' . implode( '</li><li>', $notices ) . '</li></ul></div>' : '';
  285. /**
  286. * Deprecated the tribe_events_the_notices filter in 4.0 in favor of tribe_the_notices. Remove in 5.0
  287. */
  288. $the_notices = apply_filters( 'tribe_events_the_notices', $html, $notices );
  289. /**
  290. * filters the notices HTML
  291. */
  292. $the_notices = apply_filters( 'tribe_the_notices', $html, $notices );
  293. if ( $echo ) {
  294. echo $the_notices;
  295. } else {
  296. return $the_notices;
  297. }
  298. }
  299. }//end if
  300. if ( ! function_exists( 'tribe_is_bot' ) ) {
  301. /**
  302. * tribe_is_bot checks if the visitor is a bot and returns status
  303. *
  304. * @category Events
  305. *
  306. * @return bool
  307. */
  308. function tribe_is_bot() {
  309. // get the current user agent
  310. $user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  311. // check if the user agent is empty since most browsers identify themselves, so possibly a bot
  312. if ( empty( $user_agent ) ) {
  313. return apply_filters( 'tribe_is_bot_status', true, $user_agent, null );
  314. }
  315. // declare known bot user agents (lowercase)
  316. $user_agent_bots = (array) apply_filters(
  317. 'tribe_is_bot_list', array(
  318. 'bot',
  319. 'slurp',
  320. 'spider',
  321. 'crawler',
  322. 'yandex',
  323. )
  324. );
  325. foreach ( $user_agent_bots as $bot ) {
  326. if ( stripos( $user_agent, $bot ) !== false ) {
  327. return apply_filters( 'tribe_is_bot_status', true, $user_agent, $bot );
  328. }
  329. }
  330. // we think this is probably a real human
  331. return apply_filters( 'tribe_is_bot_status', false, $user_agent, null );
  332. }
  333. }//end if
  334. if ( ! function_exists( 'tribe_count_hierarchical_keys' ) ) {
  335. /**
  336. * Count keys in a hierarchical array
  337. *
  338. * @param $value
  339. * @param $key
  340. * @todo - remove, only used in the meta walker
  341. */
  342. function tribe_count_hierarchical_keys( $value, $key ) {
  343. global $tribe_count_hierarchical_increment;
  344. $tribe_count_hierarchical_increment++;
  345. }
  346. }//end if
  347. if ( ! function_exists( 'tribe_count_hierarchical' ) ) {
  348. /**
  349. * Count items in a hierarchical array
  350. *
  351. * @param array $walk
  352. *
  353. * @return int
  354. * @todo - remove, only used in the meta walker
  355. */
  356. function tribe_count_hierarchical( array $walk ) {
  357. global $tribe_count_hierarchical_increment;
  358. $tribe_count_hierarchical_increment = 0;
  359. array_walk_recursive( $walk, 'tribe_count_hierarchical_keys' );
  360. return $tribe_count_hierarchical_increment;
  361. }
  362. }//end if
  363. if ( ! function_exists( 'tribe_get_mobile_breakpoint' ) ) {
  364. /**
  365. * Mobile breakpoint
  366. *
  367. * Get the breakpoint for switching to mobile styles. Defaults to 768.
  368. *
  369. * @category Events
  370. *
  371. * @param int $default The default width (in pixels) at which to break into mobile styles
  372. *
  373. * @return int
  374. */
  375. function tribe_get_mobile_breakpoint( $default = 768 ) {
  376. return apply_filters( 'tribe_events_mobile_breakpoint', $default );
  377. }
  378. }//end if
  379. if ( ! function_exists( 'tribe_format_currency' ) ) {
  380. /**
  381. * Receives a float and formats it with a currency symbol
  382. *
  383. * @category Cost
  384. * @param string $cost pricing to format
  385. * @param null|int $post_id
  386. * @param null|string $currency_symbol
  387. * @param null|bool $reverse_position
  388. *
  389. * @return string
  390. */
  391. function tribe_format_currency( $cost, $post_id = null, $currency_symbol = null, $reverse_position = null ) {
  392. $post_id = Tribe__Main::post_id_helper( $post_id );
  393. if ( empty( $currency_symbol ) ) {
  394. $currency_symbol = tribe_get_option( 'defaultCurrencySymbol', '$' );
  395. /**
  396. * Filters the currency symbol that will be used to format the price, defaults
  397. * to the one set in the options.
  398. *
  399. * This will only apply if the currency symbol was not passed as a parameter.
  400. *
  401. * @since 4.7.7
  402. *
  403. * @param string $currency_symbol
  404. * @param int $post_id
  405. */
  406. $currency_symbol = apply_filters( 'tribe_currency_symbol', $currency_symbol, $post_id );
  407. }
  408. if ( null === $reverse_position ) {
  409. /**
  410. * Filters whether the currency symbol that will be used to format the price should be
  411. * prefixed (`false`) or appended (`true`) to the price value.
  412. *
  413. * This will only apply if the currency symbol reverse position not passed as a parameter.
  414. *
  415. * @since 4.7.7
  416. *
  417. * @param bool $reverse_position
  418. * @param int $post_id
  419. */
  420. $reverse_position = apply_filters( 'tribe_reverse_currency_position', (bool) $reverse_position, $post_id );
  421. }
  422. // if no currency position was passed and we're not looking at a particular event,
  423. // let's get the default currency position
  424. if ( null === $reverse_position && ! $post_id ) {
  425. $reverse_position = tribe_get_option( 'reverseCurrencyPosition', false );
  426. }
  427. $cost = $reverse_position ? $cost . $currency_symbol : $currency_symbol . $cost;
  428. return $cost;
  429. }
  430. }//end if
  431. if ( ! function_exists( 'tribe_get_date_option' ) ) {
  432. /**
  433. * Get a date option.
  434. *
  435. * Retrieve an option value taking care to escape it to preserve date format slashes.
  436. *
  437. * @category Events
  438. * @param string $optionName Name of the option to retrieve.
  439. * @param string $default Value to return if no such option is found.
  440. *
  441. * @return mixed Value of the option if found
  442. */
  443. function tribe_get_date_option( $optionName, $default = '' ) {
  444. $value = tribe_get_option( $optionName, $default );
  445. return Tribe__Date_Utils::unescape_date_format($value);
  446. }
  447. }
  448. /**
  449. * Shortcut for Tribe__Admin__Notices::register(), create a Admin Notice easily
  450. *
  451. * @param string $slug Slug to save the notice
  452. * @param callable|string $callback A callable Method/Fuction to actually display the notice
  453. * @param array $arguments Arguments to Setup a notice
  454. * @param callable|null $active_callback An optional callback that should return bool values
  455. * to indicate whether the notice should display or not.
  456. *
  457. * @return stdClass Which notice was registered
  458. */
  459. function tribe_notice( $slug, $callback, $arguments = array(), $active_callback = null ) {
  460. return Tribe__Admin__Notices::instance()->register( $slug, $callback, $arguments, $active_callback );
  461. }
  462. /**
  463. * Shortcut for Tribe__Admin__Notices::register_transient(), create a transient Admin Notice easily.
  464. *
  465. * A transient admin notice is a "fire-and-forget" admin notice that will display once registered and
  466. * until dismissed (if dismissible) without need, on the side of the source code, to register it on each request.
  467. *
  468. * @param string $slug Slug to save the notice
  469. * @param string $html The notice output HTML code
  470. * @param array $arguments Arguments to Setup a notice
  471. * @param int $expire After how much time (in seconds) the notice will stop showing.
  472. *
  473. * @return stdClass Which notice was registered
  474. */
  475. function tribe_transient_notice( $slug, $html, $arguments = array(), $expire = null ) {
  476. $expire = null !== $expire ? (int) $expire : WEEK_IN_SECONDS;
  477. return Tribe__Admin__Notices::instance()->register_transient( $slug, $html, $arguments, $expire );
  478. }
  479. /**
  480. * Removes a transient notice based on its slug.
  481. *
  482. * @since 4.7.7
  483. *
  484. * @param string $slug
  485. */
  486. function tribe_transient_notice_remove( $slug ) {
  487. Tribe__Admin__Notices::instance()->remove_transient( $slug );
  488. }
  489. /**
  490. * A quick internal way of sending errors using WP_Error
  491. *
  492. * @param string|array $indexes Which Error we are looking for
  493. * @param array $context Gives the Error context
  494. * @param array $sprintf Allows variables on the message
  495. *
  496. * @return WP_Error
  497. */
  498. function tribe_error( $indexes, $context = array(), $sprintf = array() ) {
  499. return Tribe__Error::instance()->send( $indexes, $context, $sprintf );
  500. }
  501. /**
  502. * Register a new error based on a Namespace
  503. *
  504. * @param string|array $indexes A list of the namespaces and last item should be the error name
  505. * @param string $message What is going to be the message associate with this indexes
  506. *
  507. * @return boolean
  508. */
  509. function tribe_register_error( $indexes, $message ) {
  510. return Tribe__Error::instance()->register( $indexes, $message );
  511. }
  512. /**
  513. * Shortcut for Tribe__Assets::register(), include a single asset
  514. *
  515. * @since 4.3
  516. *
  517. * @param object $origin The main Object for the plugin you are enqueueing the script/style for
  518. * @param string $slug Slug to save the asset
  519. * @param string $file Which file will be loaded, either CSS or JS
  520. * @param array $deps Dependencies
  521. * @param string $action A WordPress Action, needs to happen after: `wp_enqueue_scripts`, `admin_enqueue_scripts`, or `login_enqueue_scripts`
  522. * @param array $arguments Look at `Tribe__Assets::register()` for more info
  523. *
  524. * @return array Which Assets was registered
  525. */
  526. function tribe_asset( $origin, $slug, $file, $deps = array(), $action = null, $arguments = array() ) {
  527. return tribe( 'assets' )->register( $origin, $slug, $file, $deps, $action, $arguments );
  528. }
  529. /**
  530. * Shortcut for Tribe__Assets::enqueue(), include assets
  531. *
  532. * @since 4.7
  533. *
  534. * @param string|array $slug Slug to enqueue
  535. *
  536. * @return string
  537. */
  538. function tribe_asset_enqueue( $slug ) {
  539. return tribe( 'assets' )->enqueue( $slug );
  540. }
  541. /**
  542. * Shortcut for Tribe__Assets::enqueue_group() include assets by groups
  543. *
  544. * @since 4.7
  545. *
  546. * @param string|array $group Which group(s) should be enqueued
  547. *
  548. * @return string
  549. */
  550. function tribe_asset_enqueue_group( $group ) {
  551. return tribe( 'assets' )->enqueue_group( $group );
  552. }
  553. /**
  554. * Function to include more the one asset, based on `tribe_asset`
  555. *
  556. * @since 4.3
  557. *
  558. * @param object $origin The main Object for the plugin you are enqueueing the script/style for
  559. * @param array $assets {
  560. * Indexed array, don't use any associative key.
  561. * E.g.: array( 'slug-my-script', 'my/own/path.js', array( 'jquery' ) )
  562. *
  563. * @type string $slug Slug to save the asset
  564. * @type string $file Which file will be loaded, either CSS or JS
  565. * @type array $deps (optional) Dependencies
  566. * }
  567. * @param string $action A WordPress hook that will automatically enqueue this asset once fired
  568. * @param array $arguments Look at `Tribe__Assets::register()` for more info
  569. *
  570. * @return array Which Assets were registered
  571. */
  572. function tribe_assets( $origin, $assets, $action = null, $arguments = array() ) {
  573. $registered = array();
  574. foreach ( $assets as $asset ) {
  575. if ( ! is_array( $asset ) ) {
  576. continue;
  577. }
  578. $slug = reset( $asset );
  579. if ( empty( $asset[1] ) ) {
  580. continue;
  581. }
  582. $file = $asset[1];
  583. $deps = ! empty( $asset[2] ) ? $asset[2] : array();
  584. $registered[] = tribe_asset( $origin, $slug, $file, $deps, $action, $arguments );
  585. }
  586. return $registered;
  587. }
  588. if ( ! function_exists( 'tribe_doing_frontend' ) ) {
  589. /**
  590. * Registers truthy or falsy callbacks on the filters used to detect if
  591. * any frontend operation is being done for logged in users or not.
  592. *
  593. * @since 4.7.4
  594. *
  595. * @param bool $doing_frontend Whether what is being done happens in the
  596. * context of the frontend or not.
  597. */
  598. function tribe_doing_frontend( $doing_frontend ) {
  599. $callback = $doing_frontend ? '__return_true' : '__return_false';
  600. add_filter( 'tribe_doing_frontend', $callback );
  601. }
  602. }
  603. if ( ! function_exists( 'tribe_is_frontend' ) ) {
  604. /**
  605. * Whether we are currently performing a frontend operation or not.
  606. *
  607. * @since 4.6.2
  608. *
  609. * @return bool
  610. */
  611. function tribe_is_frontend() {
  612. /**
  613. * Whether we are currently performing a frontend operation or not.
  614. *
  615. * @since 4.6.2
  616. *
  617. * @param bool $is_frontend
  618. */
  619. return (bool) apply_filters( 'tribe_doing_frontend', false );
  620. }
  621. }