From 68517aaa506ec339036dd084eda619d8077026c0 Mon Sep 17 00:00:00 2001 From: Mike Hemberger Date: Thu, 21 Sep 2017 00:20:05 -0400 Subject: [PATCH 1/3] filter primary nav to display locatin page tree --- CHANGES.md | 3 ++ includes/class-frontend.php | 100 ++++++++++++++++++++++++++++++++++-- includes/functions.php | 14 ++--- 3 files changed, 107 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1f004a6..11e4677 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +#### 1.3.1 (9/21/17) +* Fixed: Location child pages not showing on frontend. + #### 1.3.0 (9/19/17) * Changed: Allow locations to have child (grandchild) pages. * Changed: Added updater script in plugin so no longer relying on GitHub Updater. diff --git a/includes/class-frontend.php b/includes/class-frontend.php index 51aa9cb..6b57fc3 100644 --- a/includes/class-frontend.php +++ b/includes/class-frontend.php @@ -14,7 +14,7 @@ */ class User_Locations_Frontend { - /** + /** * @var Parisi_Functions The one true Parisi_Functions * @since 1.0.0 */ @@ -31,13 +31,12 @@ public static function instance() { } function init() { - // Hook in the location menu ( not OOP so it can easily be removed/moved ) - add_action( 'genesis_after_header', 'ul_do_location_menu', 20 ); // Hook in the location posts ( not OOP so it can easily be removed/moved ) add_action( 'genesis_after_loop', 'ul_do_location_posts' ); add_filter( 'body_class', array( $this, 'location_content_body_class' ) ); + add_filter( 'wp_get_nav_menu_items', array( $this, 'replace_primary_navigation' ), 10, 2 ); add_filter( 'genesis_post_info', array( $this, 'maybe_remove_post_info' ), 99 ); add_filter( 'genesis_post_meta', array( $this, 'maybe_remove_post_meta' ), 99 ); @@ -60,6 +59,101 @@ public function location_content_body_class( $classes ) { return $classes; } + /** + * Replace the primary navigation with the location menu items. + * + * @since 1.3.1 + * + * @param array $items The existing menu items. + * @param array $menu The menu. + * + * @return array The modified menu items. + */ + public function replace_primary_navigation( $items, $menu ) { + + // Bail if not location page. + if ( ! is_singular( 'location_page' ) ) { + return $items; + } + + // Get all displayed locations. + $locations = get_nav_menu_locations(); + + // Bail if no primary nav displaying. + if ( ! isset( $locations['primary'] ) ) { + return $items; + } + + // Bail if not filtering the primary nav. + if ( $locations['primary'] != $menu->term_id ) { + return $items; + } + + // Get the top level parent. + $ancestors = array_reverse( get_ancestors( get_the_ID(), get_post_type() ) ); + $ancestors = empty( $ancestors ) ? array( get_the_ID() ) : $ancestors; + + return $this->get_menu_tree( $ancestors[0], 'location_page' ); + } + + /** + * Get the menu tree from a specific post ID. + * + * @since 1.3.1 + * + * @param int $parent_id The post ID to base the menu off of. + * @param string $post_type The post type to check. + * @param bool $grandchild Whether we are getting first level or second level menu items. + * + * @return array The menu. + */ + public function get_menu_tree( $parent_id, $post_type, $grandchild = false ) { + + static $original_parent_id = 0; + + if ( ! $grandchild ) { + $original_parent_id = $parent_id; + } + + $children = array(); + + $pages = get_pages( array( + 'child_of' => $parent_id, + 'parent' => -1, + 'sort_column' => 'menu_order', + 'post_type' => $post_type, + ) ); + + if ( empty( $pages ) ) { + return $children; + } + + $menu_order = 1; + + if ( ! $grandchild ) { + // Add parent. + $home = get_post( $parent_id ); + $home->post_title = __( 'Home', 'user-locations' ); + array_unshift( $pages, $home ); + } + + foreach ( $pages as $page ) { + + $child = wp_setup_nav_menu_item( $page ); + + $child->db_id = $page->ID; + $child->menu_item_parent = ( $original_parent_id == $page->post_parent ) ? 0 : $page->post_parent; + $child->menu_order = $menu_order; + + $children[] = $child; + + $menu_order++; + + } + + return $children; + } + /** * Remove post info from location pages * diff --git a/includes/functions.php b/includes/functions.php index 65d95d3..59266bc 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -100,12 +100,12 @@ function ul_get_location_menu() { } $args = array( - 'post_type' => 'location_page', - 'posts_per_page' => 50, - 'post_status' => 'publish', - 'post_parent' => $parent_id, - 'orderby' => 'menu_order', - 'order' => 'ASC', + 'post_type' => 'location_page', + 'posts_per_page' => 50, + 'post_status' => 'publish', + 'post_parent' => $parent_id, + 'orderby' => 'menu_order', + 'order' => 'ASC', ); // Allow for filtering of the menu item args $args = apply_filters( 'userlocations_location_menu_args', $args ); @@ -139,7 +139,7 @@ function ul_get_location_menu() { $classes .= ' current-menu-item'; } // Add each menu item - $output .= ''; + $output .= ''; } $output .= ''; From 91dc1a81d2b11509cffbdc0e899539886fb0c826 Mon Sep 17 00:00:00 2001 From: Mike Hemberger Date: Thu, 21 Sep 2017 10:08:24 -0400 Subject: [PATCH 2/3] change location template hook/priority --- includes/class-template-loader.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/includes/class-template-loader.php b/includes/class-template-loader.php index 5dae3b0..8be3ce8 100644 --- a/includes/class-template-loader.php +++ b/includes/class-template-loader.php @@ -92,17 +92,17 @@ public function init() { */ public function maybe_location_page_template() { - if ( ! is_singular('location_page') || ! ul_is_location_parent_page_template() ) { - return; - } - global $post; - // Bail if we're on a parent Location Page - if ( $post->post_parent == 0 ) { - return; - } + if ( ! is_singular('location_page') || ! ul_is_location_parent_page_template() ) { + return; + } + global $post; + // Bail if we're on a parent Location Page + if ( $post->post_parent == 0 ) { + return; + } // Add custom body class to the head - add_filter( 'body_class', array( $this, 'body_classes' ) ); - add_action( 'genesis_loop', array( $this, 'do_location_page_templates_loop' ) ); + add_filter( 'body_class', array( $this, 'body_classes' ) ); + add_action( 'genesis_entry_content', array( $this, 'do_location_page_templates_loop' ), 12 ); } /** From ece8b9a894c40dcb92f99e8551a91cdd1eabcfe3 Mon Sep 17 00:00:00 2001 From: Mike Hemberger Date: Thu, 21 Sep 2017 10:09:04 -0400 Subject: [PATCH 3/3] 1.3.1 --- user-locations.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user-locations.php b/user-locations.php index bef79eb..4b32d88 100644 --- a/user-locations.php +++ b/user-locations.php @@ -15,7 +15,7 @@ * Text Domain: user-locations * License: GPL-2.0+ * License URI: http://www.gnu.org/licenses/gpl-2.0.txt - * Version: 1.3.0 + * Version: 1.3.1 * GitHub Plugin URI: https://github.com/bizbudding/user-locations * GitHub Branch: master */ @@ -173,7 +173,7 @@ private function setup_constants() { // Plugin version. if ( ! defined( 'USER_LOCATIONS_VERSION' ) ) { - define( 'USER_LOCATIONS_VERSION', '1.3.0' ); + define( 'USER_LOCATIONS_VERSION', '1.3.1' ); } // Plugin Folder Path.