Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new utility function #1739

Merged
merged 8 commits into from Jan 4, 2018
3 changes: 2 additions & 1 deletion readme.txt
Expand Up @@ -72,7 +72,7 @@ Just getting started? Definitely watch and read through the [New User Primer](ht
* Widget: Upcoming events list
* Events Taxonomies (Categories & Tags)
* Google Calendar and iCal exporting
* WP REST API endpoints
* WP REST API endpoints
* Completely ajaxified for super smooth browsing
* Completely responsive from mobile to tablet to desktop
* Tested on the major theme frameworks such as Avada, Genesis, Woo Themes, Thesis and many more.
Expand Down Expand Up @@ -314,6 +314,7 @@ The plugin is made with love by [Modern Tribe Inc](http://m.tri.be/2s).
* Fix - Make sure latitude and longitude information from iCal feeds is used if available [96363]
* Tweak - Made it possible to translate the iCal feed's description field (props @gafderks) [96677]
* Tweak - Improved escaping of map IDs (props LucaPipolo) [96772]
* Feature - Add new utility functions tribe_is_events_home and tribe_is_events_front_page similar to native WP is_home and is_front_page [42195]

= [4.6.8] 2017-12-18 =

Expand Down
73 changes: 71 additions & 2 deletions src/functions/template-tags/general.php
Expand Up @@ -163,7 +163,7 @@ function tribe_get_template_part( $slug, $name = null, array $data = null ) {
*
* @category Events
* @param bool $view
* @return boolean
* @return bool
*/
function tribe_is_ajax_view_request( $view = false ) {
$is_ajax_view_request = false;
Expand Down Expand Up @@ -866,7 +866,7 @@ function tribe_map_cost_array_callback( $costs ) {
* @param string $event_cat_slug
* @param int $event_id
*
* @return boolean
* @return bool
*/
function tribe_event_in_category( $event_cat_slug, $event_id = null ) {

Expand Down Expand Up @@ -1669,4 +1669,73 @@ function tribe_separated_field( $body, $separator, $field ) {

return $field ? $body_and_separator . $field : $body;
}

/**
* Tests if we are on the site homepage and if it is set to display the main events page.
*
* As WordPress front page it might be different from is_home, if we have a front page on the reading options and
* if the User is on that page, this function will return true otherwise will return false. So either if the User has
* the frontpage set on the reading options and the User is visiting this page.
*
* Another consideration about this is it might behave as a WordPress function which means after any Ajax action is
* fired the result of call this function via Ajax might not be the expected result so ideally can be used to test
* if you are on the front page on first load of the page only.
*
* @since TBD
*
* @return bool
*/
function tribe_is_events_front_page() {
global $wp_query;

$events_as_front_page = tribe_get_option( 'front_page_event_archive', false );
// If the reading option has an events page as front page and we are on that page is on the home of events.
return (
$wp_query->is_main_query()
&& $events_as_front_page
&& $wp_query->tribe_is_event
&& true === get_query_var( 'tribe_events_front_page' )
);
}

/**
* Test if we are on the home of events either if is set to frontpage or the default /events page.
*
* Utility function to test if we are on the home of events, it makes a test in cases when the page is set to be on
* the frontpage of the site and if the User is on that page is on the homepage or if the User is on the events page
* where the eventDisplay is set to default.
*
* Also consider this might not work as expected inside of Ajax Calls as this one is fired on initial loading of the
* page so be aware of unexpected results via Ajax calls.
*
* @since TBD
*
* @return bool
*/
function tribe_is_events_home() {
global $wp_query;

if ( tribe_is_events_front_page() ) {
return true;
}

$events_as_front_page = tribe_get_option( 'front_page_event_archive', false );
// If the readme option does not has an event page as front page and if id the 'default' view on the main query
// as is going to set to 'default' when is loading the root of events/ rewrite rule also makes sure is not on
// a taxonomy or a tag.
if (
! $events_as_front_page
&& $wp_query->is_main_query()
&& $wp_query->tribe_is_event // Make sure following conditionals operate only on events
&& ( isset( $wp_query->query['eventDisplay'] ) && 'default' === $wp_query->query['eventDisplay'] )
&& is_post_type_archive()
&& ! is_tax()
&& ! is_tag()
) {
return true;
}

// No condition was true so is not on home of events.
return false;
}
}