From b225075d021271bdd99639d4f987809041e5622c Mon Sep 17 00:00:00 2001 From: Anton Kachurin Date: Thu, 25 Oct 2018 16:34:52 -0400 Subject: [PATCH 1/9] Add class that allows to calculate changes in post hierarchy --- includes/class-tree-view.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/includes/class-tree-view.php b/includes/class-tree-view.php index eedf577..9acee49 100644 --- a/includes/class-tree-view.php +++ b/includes/class-tree-view.php @@ -316,6 +316,37 @@ public function add_node_prefix( $id ) { } +class BU_Navigation_Tree_Hierarchy { + + const HIERARCHICAL_PROPERTIES = array( 'ID', 'excluded', 'menu_order', 'post_parent' ); + public $query; + + public function __construct( $query ) { + $this->query = $query; + } + + public function as_object() { + $hierarchy = $this->query->posts_by_parent(); + foreach ( $hierarchy as $parent ) { + foreach ( $parent as $child ) { + foreach ( $child as $prop_name => $prop_value ) { + if ( ! in_array( $prop_name, self::HIERARCHICAL_PROPERTIES ) ) { + unset( $child->$prop_name ); + } + } + } + } + + return $hierarchy; + } + + public function as_hash() { + $obj = $this->as_object(); + $str = json_encode( $obj ); + return md5( $str ); + } +} + /** * WP_Query-like class optimized for querying hierarchical post types * From e1b5a958865cb822a177e14495cac0ca3f7a2f95 Mon Sep 17 00:00:00 2001 From: Anton Kachurin Date: Thu, 25 Oct 2018 16:40:27 -0400 Subject: [PATCH 2/9] Refactor instantiation of BU_Navigation_Tree_View into separate method --- admin/manager.php | 68 ++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/admin/manager.php b/admin/manager.php index b1ab7df..04d26a8 100644 --- a/admin/manager.php +++ b/admin/manager.php @@ -126,37 +126,8 @@ public function add_scripts( $page ) { wp_register_script( 'bu-jquery-validate', $vendor_url . '/jquery.validate' . $suffix . '.js', array( 'jquery' ), '1.8.1', true ); wp_register_script( 'bu-navman', $scripts_url . '/manage' . $suffix . '.js', array( 'bu-navigation', 'jquery-ui-dialog', 'bu-jquery-validate' ), BU_Navigation_Plugin::VERSION, true ); - // Strings for localization - $nav_menu_label = __( 'Appearance > Primary Navigation', 'bu-navigation' ); - $strings = array( - 'optionsLabel' => __( 'options', 'bu-navigation' ), - 'optionsEditLabel' => __( 'Edit', 'bu-navigation' ), - 'optionsViewLabel' => __( 'View', 'bu-navigation' ), - 'optionsDeleteLabel' => __( 'Delete', 'bu-navigation' ), - 'optionsTrashLabel' => __( 'Move to Trash', 'bu-navigation' ), - 'addLinkDialogTitle' => __( 'Add a Link', 'bu-navigation' ), - 'editLinkDialogTitle' => __( 'Edit Link', 'bu-navigation' ), - 'cancelLinkBtn' => __( 'Cancel', 'bu-navigation' ), - 'confirmLinkBtn' => __( 'Ok', 'bu-navigation' ), - 'noTopLevelNotice' => __( 'You are not allowed to create top level published content.', 'bu-navigation' ), - 'noLinksNotice' => __( 'You are not allowed to add links', 'bu-navigation' ), - 'createLinkNotice' => __( 'Select a page that you can edit and click "Add a Link" to create a new link below the selected page.', 'bu-navigation' ), - 'allowTopNotice' => sprintf( __( 'Site administrators can change this behavior by visiting %s and enabling the "Allow Top-Level Pages" setting.', 'bu-navigation' ), $nav_menu_label ), - 'noChildLinkNotice' => __( 'Links are not permitted to have children.', 'bu-navigation' ), - 'unloadWarning' => __( 'You have made changes to your navigation that have not yet been saved.', 'bu-navigation' ), - 'saveNotice' => __( 'Saving navigation changes...', 'bu-navigation' ), - ); - - // Setup dynamic script context for manage.js - $script_context = array( - 'postTypes' => $this->post_type, - 'postStatuses' => array( 'publish', 'private' ), - 'nodePrefix' => 'nm', - 'lazyLoad' => true, - 'showCounts' => true, - ); // Navigation tree view will handle actual enqueuing of our script - $treeview = new BU_Navigation_Tree_View( 'bu_navman', array_merge( $script_context, $strings ) ); + $treeview = $this->get_navigation_tree(); $treeview->enqueue_script( 'bu-navman' ); // Register custom jQuery UI stylesheet if it isn't already @@ -174,6 +145,43 @@ public function add_scripts( $page ) { } + /** + * Get fully configured instance of BU_Navigation_Tree_View + */ + public function get_navigation_tree() { + // Strings for localization + $nav_menu_label = __( 'Appearance > Primary Navigation', 'bu-navigation' ); + $strings = array( + 'optionsLabel' => __( 'options', 'bu-navigation' ), + 'optionsEditLabel' => __( 'Edit', 'bu-navigation' ), + 'optionsViewLabel' => __( 'View', 'bu-navigation' ), + 'optionsDeleteLabel' => __( 'Delete', 'bu-navigation' ), + 'optionsTrashLabel' => __( 'Move to Trash', 'bu-navigation' ), + 'addLinkDialogTitle' => __( 'Add a Link', 'bu-navigation' ), + 'editLinkDialogTitle' => __( 'Edit Link', 'bu-navigation' ), + 'cancelLinkBtn' => __( 'Cancel', 'bu-navigation' ), + 'confirmLinkBtn' => __( 'Ok', 'bu-navigation' ), + 'noTopLevelNotice' => __( 'You are not allowed to create top level published content.', 'bu-navigation' ), + 'noLinksNotice' => __( 'You are not allowed to add links', 'bu-navigation' ), + 'createLinkNotice' => __( 'Select a page that you can edit and click "Add a Link" to create a new link below the selected page.', 'bu-navigation' ), + 'allowTopNotice' => sprintf( __( 'Site administrators can change this behavior by visiting %s and enabling the "Allow Top-Level Pages" setting.', 'bu-navigation' ), $nav_menu_label ), + 'noChildLinkNotice' => __( 'Links are not permitted to have children.', 'bu-navigation' ), + 'unloadWarning' => __( 'You have made changes to your navigation that have not yet been saved.', 'bu-navigation' ), + 'saveNotice' => __( 'Saving navigation changes...', 'bu-navigation' ), + ); + + // Setup dynamic script context for manage.js + $script_context = array( + 'postTypes' => $this->post_type, + 'postStatuses' => array( 'publish', 'private' ), + 'nodePrefix' => 'nm', + 'lazyLoad' => true, + 'showCounts' => true, + ); + + return new BU_Navigation_Tree_View( 'bu_navman', array_merge( $script_context, $strings ) ); + } + /** * Handle admin page setup */ From 4d55b062a56bbdfdc21629141dc950b55088dd6f Mon Sep 17 00:00:00 2001 From: Anton Kachurin Date: Thu, 25 Oct 2018 16:43:23 -0400 Subject: [PATCH 3/9] Submit original page hierarchy hash when saving page order --- admin/manager.php | 2 +- includes/class-tree-view.php | 3 +++ templates/edit-order.php | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/admin/manager.php b/admin/manager.php index 04d26a8..8c9fdbe 100644 --- a/admin/manager.php +++ b/admin/manager.php @@ -329,7 +329,7 @@ public function render() { $notices = $this->get_notice_list(); $include_links = $this->plugin->supports( 'links' ) && 'page' == $this->post_type; $disable_add_link = ! $this->can_publish_top_level( $this->post_type ); - + $tree = $this->get_navigation_tree(); // Render interface include( BU_NAV_PLUGIN_DIR . '/templates/edit-order.php' ); diff --git a/includes/class-tree-view.php b/includes/class-tree-view.php index 9acee49..3f2a431 100644 --- a/includes/class-tree-view.php +++ b/includes/class-tree-view.php @@ -12,6 +12,8 @@ class BU_Navigation_Tree_View { private $plugin; private $query; + + public $hierarchy; /** * Setup an object capable of creating the navigation management interface @@ -60,6 +62,7 @@ public function __construct( $instance = 'legacy', $script_context = array() ) { 'suppress_urls' => $this->settings['suppressUrls'] ); $this->query = new BU_Navigation_Tree_Query( $query_args ); + $this->hierarchy = new BU_Navigation_Tree_Hierarchy( $this->query ); // No need to register scripts during AJAX requests if( ! defined('DOING_AJAX') || ! DOING_AJAX ) { diff --git a/templates/edit-order.php b/templates/edit-order.php index 0652559..b87cede 100644 --- a/templates/edit-order.php +++ b/templates/edit-order.php @@ -5,6 +5,7 @@