From 28fe19e0337aa93325eeb7395508e45e0516415e Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Tue, 06 Dec 2016 23:37:43 +0000 Subject: [PATCH] Implement menu support into the application --- src/helpers.php | 18 ++++++++++++++++++ src/Proxy/WordpressDatabase.php | 4 ++++ src/Support/RecursiveMenuIterator.php | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Support/Wordpress.php | 12 ++++++++++++ 4 files changed, 154 insertions(+) diff --git a/src/helpers.php b/src/helpers.php index 55f04fc..c30132d 100644 --- a/src/helpers.php +++ a/src/helpers.php @@ -1,7 +1,8 @@ setMaxDepth($depth); + return $iterator; } } diff --git a/src/Proxy/WordpressDatabase.php b/src/Proxy/WordpressDatabase.php index 3b72958..b711024 100644 --- a/src/Proxy/WordpressDatabase.php +++ a/src/Proxy/WordpressDatabase.php @@ -205,6 +205,10 @@ $return = $this->result; } elseif (preg_match('/^\s*(insert|delete|update|replace)\s/i', $query)) { $this->rows_affected = $return = $this->result; + + if (preg_match('/^\s*(insert|replace)\s/i', $query)) { + $this->insert_id = DB::getPdo()->lastInsertId(); + } } else { // Log number of rows the query returned // and return number of rows selected diff --git a/src/Support/RecursiveMenuIterator.php b/src/Support/RecursiveMenuIterator.php new file mode 100644 index 0000000..f85b7ed 100644 --- /dev/null +++ a/src/Support/RecursiveMenuIterator.php @@ -1,0 +1,120 @@ + + */ +class RecursiveMenuIterator implements RecursiveIterator +{ + private $current = 0; + public $items; + + /** + * Create a new RecursiveMenuIterator instance. + * + * @param string $menu menu to get items of + */ + public function __construct($menu) + { + if (is_string($menu)) { + $menu = wp_get_nav_menu_object(get_nav_menu_locations()[$menu]); + + // Wordpress is nice and will always place children below parents so we can easily create a tree out of it + $items = collect(wp_get_nav_menu_items($menu))->keyBy('ID')->reverse(); + $itemsArray = $items->all(); + + foreach ($itemsArray as $id => $item) { + $itemsArray[$id]->children = $items->where('menu_item_parent', $id)->values(); + } + + // only have nodes without a parent at the top level of the tree + $this->items = collect($itemsArray)->filter(function ($item) { + return $item->menu_item_parent == 0; + })->reverse()->values(); + } else { + $this->items = $menu; + } + } + + /** + * Return the current element + * @link http://php.net/manual/en/iterator.current.php + * @return \WP_Post Can return any type. + * @since 5.0.0 + */ + public function current() + { + return $this->items[$this->current]; + } + + /** + * Move forward to next element + * @link http://php.net/manual/en/iterator.next.php + * @return void Any returned value is ignored. + * @since 5.0.0 + */ + public function next() + { + $this->current++; + } + + /** + * Return the key of the current element + * @link http://php.net/manual/en/iterator.key.php + * @return mixed scalar on success, or null on failure. + * @since 5.0.0 + */ + public function key() + { + return $this->current; + } + + /** + * Checks if current position is valid + * @link http://php.net/manual/en/iterator.valid.php + * @return boolean The return value will be casted to boolean and then evaluated. + * Returns true on success or false on failure. + * @since 5.0.0 + */ + public function valid() + { + return isset($this->items[$this->current]); + } + + /** + * Rewind the Iterator to the first element + * @link http://php.net/manual/en/iterator.rewind.php + * @return void Any returned value is ignored. + * @since 5.0.0 + */ + public function rewind() + { + $this->current = 0; + } + + /** + * Returns if an iterator can be created for the current entry. + * @link http://php.net/manual/en/recursiveiterator.haschildren.php + * @return bool true if the current entry can be iterated over, otherwise returns false. + * @since 5.1.0 + */ + public function hasChildren() + { + return !$this->current()->children->isEmpty(); + } + + /** + * Returns an iterator for the current entry. + * @link http://php.net/manual/en/recursiveiterator.getchildren.php + * @return RecursiveIterator An iterator for the current entry. + * @since 5.1.0 + */ + public function getChildren() + { + return new static($this->current()->children); + } +} diff --git a/src/Support/Wordpress.php b/src/Support/Wordpress.php index 6df353f..868be71 100644 --- a/src/Support/Wordpress.php +++ a/src/Support/Wordpress.php @@ -90,6 +90,18 @@ } /** + * Get a Wordpress option from the database. + * + * @param string $name name of the option to get + * @param mixed $default value to return if we don't have a value for the option. + * @return mixed + */ + public static function option($name, $default = false) + { + return get_option($name, $default); + } + + /** * Get the current multisite id. * * @return int -- rgit 0.1.5