PluginProbe
Property Hive / trunk
Property Hive vtrunk
2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 1.4.63 All 259 releases
propertyhive / includes / ph-page-functions.php

ph-page-functions.php in Property Hive trunk, at includes/ph-page-functions.php

91 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PropertyHive Page Functions
4 *
5 * Functions related to pages and menus.
6 *
7 * @author PropertyHive
8 * @category Core
9 * @package PropertyHive/Functions
10 * @version 1.0.0
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 /**
18 * Retrieve page ids - used for search results, property. returns -1 if no page is found
19 *
20 * @param string $page
21 * @return int
22 */
23 function ph_get_page_id( $page ) {
24
25 $page = apply_filters( 'propertyhive_get_' . $page . '_page_id', get_option('propertyhive_' . $page . '_page_id' ) );
26
27 return $page ? $page : -1;
28 }
29
30 /**
31 * Fix active class in nav for search results page.
32 *
33 * @param array $menu_items
34 * @return array
35 */
36 function ph_nav_menu_item_classes( $menu_items ) {
37 if ( ! is_propertyhive() ) {
38 return $menu_items;
39 }
40 $search_results_page = (int) ph_get_page_id( 'search_results' );
41 $page_for_posts = (int) get_option( 'page_for_posts' );
42
43 foreach ( (array) $menu_items as $key => $menu_item ) {
44 $classes = (array) $menu_item->classes;
45 // Unset active class for blog page
46 if ( $page_for_posts == $menu_item->object_id ) {
47 $menu_items[ $key ]->current = false;
48 if ( in_array( 'current_page_parent', $classes ) ) {
49 unset( $classes[ array_search( 'current_page_parent', $classes ) ] );
50 }
51 if ( in_array( 'current-menu-item', $classes ) ) {
52 unset( $classes[ array_search( 'current-menu-item', $classes ) ] );
53 }
54 // Set active state if this is the search results page link
55 } elseif ( is_search_results() && $search_results_page == $menu_item->object_id && 'page' === $menu_item->object ) {
56 $menu_items[ $key ]->current = true;
57 $classes[] = 'current-menu-item';
58 $classes[] = 'current_page_item';
59 // Set parent state if this is a property page
60 } elseif ( is_singular( 'property' ) && $search_results_page == $menu_item->object_id ) {
61 $classes[] = 'current_page_parent';
62 }
63 $menu_items[ $key ]->classes = array_unique( $classes );
64 }
65 return $menu_items;
66 }
67 add_filter( 'wp_nav_menu_objects', 'ph_nav_menu_item_classes', 2 );
68
69 /**
70 * Fix active class in wp_list_pages for search results page.
71 *
72 * @param string $pages
73 * @return string
74 */
75 function ph_list_pages( $pages ) {
76 if ( is_propertyhive() ) {
77 // Remove current_page_parent class from any item.
78 $pages = str_replace( 'current_page_parent', '', $pages );
79 // Find search_results_page_id through Property Hive options.
80 $search_results_page = 'page-item-' . ph_get_page_id( 'search_results' );
81 if ( is_search_results() ) {
82 // Add current_page_item class to search results page.
83 $pages = str_replace( $search_results_page, $search_results_page . ' current_page_item', $pages );
84 } else {
85 // Add current_page_parent class to search results page.
86 $pages = str_replace( $search_results_page, $search_results_page . ' current_page_parent', $pages );
87 }
88 }
89 return $pages;
90 }
91 add_filter( 'wp_list_pages', 'ph_list_pages' );