PluginProbe
Property Hive / 2.3.0
Property Hive v2.3.0
2.3.1 2.3.0 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 All 261 releases
propertyhive / includes / ph-page-functions.php

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

94 lines 3.5 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 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Legacy public global helper ph_get_page_id; the established callable name is part of the plugin/extension API and must remain stable.
24 function ph_get_page_id( $page ) {
25
26 $page = apply_filters( 'propertyhive_get_' . $page . '_page_id', get_option('propertyhive_' . $page . '_page_id' ) );
27
28 return $page ? $page : -1;
29 }
30
31 /**
32 * Fix active class in nav for search results page.
33 *
34 * @param array $menu_items
35 * @return array
36 */
37 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Legacy public global helper ph_nav_menu_item_classes; the established callable name is part of the plugin/extension API and must remain stable.
38 function ph_nav_menu_item_classes( $menu_items ) {
39 if ( ! is_propertyhive() ) {
40 return $menu_items;
41 }
42 $search_results_page = (int) ph_get_page_id( 'search_results' );
43 $page_for_posts = (int) get_option( 'page_for_posts' );
44
45 foreach ( (array) $menu_items as $key => $menu_item ) {
46 $classes = (array) $menu_item->classes;
47 // Unset active class for blog page
48 if ( $page_for_posts == $menu_item->object_id ) {
49 $menu_items[ $key ]->current = false;
50 if ( in_array( 'current_page_parent', $classes ) ) {
51 unset( $classes[ array_search( 'current_page_parent', $classes ) ] );
52 }
53 if ( in_array( 'current-menu-item', $classes ) ) {
54 unset( $classes[ array_search( 'current-menu-item', $classes ) ] );
55 }
56 // Set active state if this is the search results page link
57 } elseif ( is_search_results() && $search_results_page == $menu_item->object_id && 'page' === $menu_item->object ) {
58 $menu_items[ $key ]->current = true;
59 $classes[] = 'current-menu-item';
60 $classes[] = 'current_page_item';
61 // Set parent state if this is a property page
62 } elseif ( is_singular( 'property' ) && $search_results_page == $menu_item->object_id ) {
63 $classes[] = 'current_page_parent';
64 }
65 $menu_items[ $key ]->classes = array_unique( $classes );
66 }
67 return $menu_items;
68 }
69 add_filter( 'wp_nav_menu_objects', 'ph_nav_menu_item_classes', 2 );
70
71 /**
72 * Fix active class in wp_list_pages for search results page.
73 *
74 * @param string $pages
75 * @return string
76 */
77 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Legacy public global helper ph_list_pages; the established callable name is part of the plugin/extension API and must remain stable.
78 function ph_list_pages( $pages ) {
79 if ( is_propertyhive() ) {
80 // Remove current_page_parent class from any item.
81 $pages = str_replace( 'current_page_parent', '', $pages );
82 // Find search_results_page_id through Property Hive options.
83 $search_results_page = 'page-item-' . ph_get_page_id( 'search_results' );
84 if ( is_search_results() ) {
85 // Add current_page_item class to search results page.
86 $pages = str_replace( $search_results_page, $search_results_page . ' current_page_item', $pages );
87 } else {
88 // Add current_page_parent class to search results page.
89 $pages = str_replace( $search_results_page, $search_results_page . ' current_page_parent', $pages );
90 }
91 }
92 return $pages;
93 }
94 add_filter( 'wp_list_pages', 'ph_list_pages' );