Listing.php
336 lines
| 1 | <?php namespace NestedPages\Entities\Listing; |
| 2 | |
| 3 | use NestedPages\Helpers; |
| 4 | use NestedPages\Entities\Confirmation\ConfirmationFactory; |
| 5 | use NestedPages\Entities\Post\PostDataFactory; |
| 6 | use NestedPages\Entities\Post\PostRepository; |
| 7 | use NestedPages\Entities\User\UserRepository; |
| 8 | use NestedPages\Entities\PostType\PostTypeRepository; |
| 9 | use NestedPages\Entities\Listing\ListingRepository; |
| 10 | use NestedPages\Config\SettingsRepository; |
| 11 | |
| 12 | /** |
| 13 | * Primary Post Listing |
| 14 | */ |
| 15 | class Listing { |
| 16 | |
| 17 | /** |
| 18 | * Post Type |
| 19 | * @var object WP Post Type Object |
| 20 | */ |
| 21 | private $post_type; |
| 22 | |
| 23 | /** |
| 24 | * Hierarchical Taxonomies |
| 25 | * @var array |
| 26 | */ |
| 27 | private $h_taxonomies; |
| 28 | |
| 29 | /** |
| 30 | * Flat Taxonomies |
| 31 | * @var array |
| 32 | */ |
| 33 | private $f_taxonomies; |
| 34 | |
| 35 | /** |
| 36 | * Post Data Factory |
| 37 | */ |
| 38 | private $post_data_factory; |
| 39 | |
| 40 | /** |
| 41 | * Post Data |
| 42 | * @var object |
| 43 | */ |
| 44 | private $post; |
| 45 | |
| 46 | /** |
| 47 | * Post Repository |
| 48 | */ |
| 49 | private $post_repo; |
| 50 | |
| 51 | /** |
| 52 | * Post Type Repository |
| 53 | */ |
| 54 | private $post_type_repo; |
| 55 | |
| 56 | /** |
| 57 | * Listing Repository |
| 58 | */ |
| 59 | private $listing_repo; |
| 60 | |
| 61 | /** |
| 62 | * Confirmation Factory |
| 63 | */ |
| 64 | private $confirmation; |
| 65 | |
| 66 | /** |
| 67 | * User Repository |
| 68 | */ |
| 69 | private $user; |
| 70 | |
| 71 | /** |
| 72 | * Sorting Options |
| 73 | * @var array |
| 74 | */ |
| 75 | private $sort_options; |
| 76 | |
| 77 | /** |
| 78 | * Settings Repository |
| 79 | */ |
| 80 | private $settings; |
| 81 | |
| 82 | |
| 83 | public function __construct($post_type) |
| 84 | { |
| 85 | $this->setPostType($post_type); |
| 86 | $this->post_repo = new PostRepository; |
| 87 | $this->user = new UserRepository; |
| 88 | $this->confirmation = new ConfirmationFactory; |
| 89 | $this->post_type_repo = new PostTypeRepository; |
| 90 | $this->listing_repo = new ListingRepository; |
| 91 | $this->post_data_factory = new PostDataFactory; |
| 92 | $this->settings = new SettingsRepository; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Called by Menu Class |
| 98 | * Instantiates Listing Class |
| 99 | * @since 1.2.0 |
| 100 | */ |
| 101 | public static function admin_menu($post_type) { |
| 102 | $class_name = get_class(); |
| 103 | $classinstance = new $class_name($post_type); |
| 104 | return array(&$classinstance, "listPosts"); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Set the Sort Options |
| 110 | */ |
| 111 | private function setSortOptions() |
| 112 | { |
| 113 | $this->sort_options = new \StdClass(); |
| 114 | $this->sort_options->orderby = isset($_GET['orderby']) |
| 115 | ? sanitize_text_field($_GET['orderby']) |
| 116 | : 'menu_order'; |
| 117 | $this->sort_options->order = isset($_GET['order']) |
| 118 | ? sanitize_text_field($_GET['order']) |
| 119 | : 'ASC'; |
| 120 | $this->sort_options->author = isset($_GET['author']) |
| 121 | ? sanitize_text_field($_GET['author']) |
| 122 | : null; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Get the Current Page URL |
| 128 | */ |
| 129 | private function pageURL() |
| 130 | { |
| 131 | $base = ( $this->post_type->name == 'post' ) ? admin_url('edit.php') : admin_url('admin.php'); |
| 132 | return $base . '?page=' . $_GET['page']; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Set the Post Type |
| 138 | * @since 1.1.16 |
| 139 | */ |
| 140 | private function setPostType($post_type) |
| 141 | { |
| 142 | $this->post_type = get_post_type_object($post_type); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * The Main View |
| 148 | * Replaces Default Post Listing |
| 149 | */ |
| 150 | public function listPosts() |
| 151 | { |
| 152 | $this->setSortOptions(); |
| 153 | include( Helpers::view('listing') ); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Set the Taxonomies for Post Type |
| 159 | */ |
| 160 | private function setTaxonomies() |
| 161 | { |
| 162 | $this->h_taxonomies = $this->post_type_repo->getTaxonomies($this->post_type->name, true); |
| 163 | $this->f_taxonomies = $this->post_type_repo->getTaxonomies($this->post_type->name, false); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Opening list tag <ol> |
| 169 | * @param array $pages - array of page objects from current query |
| 170 | * @param int $count - current count in loop |
| 171 | */ |
| 172 | private function listOpening($pages, $count, $sortable = true) |
| 173 | { |
| 174 | if ( $this->isSearch() ) $sortable = false; |
| 175 | |
| 176 | // Get array of child pages |
| 177 | $children = array(); |
| 178 | $all_children = $pages->posts; |
| 179 | foreach($all_children as $child){ |
| 180 | array_push($children, $child->ID); |
| 181 | } |
| 182 | // Compare child pages with user's toggled pages |
| 183 | $compared = array_intersect($this->listing_repo->visiblePages($this->post_type->name), $children); |
| 184 | |
| 185 | // Primary List |
| 186 | if ( $count == 1 ) { |
| 187 | echo ( $this->user->canSortPages() && $sortable ) |
| 188 | ? '<ol class="sortable nplist visible" id="np-' . $this->post_type->name . '">' |
| 189 | : '<ol class="sortable no-sort nplist" visible" id="np-' . $this->post_type->name . '">'; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | // Don't create new list for child elements of posts in trash |
| 194 | if ( get_post_status($pages->query['post_parent']) == 'trash' ) return; |
| 195 | |
| 196 | echo '<ol class="nplist'; |
| 197 | if ( count($compared) > 0 ) echo ' visible" style="display:block;'; |
| 198 | echo '" id="np-' . $this->post_type->name . '">'; |
| 199 | |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | * Set Post Data |
| 205 | * @param object post object |
| 206 | */ |
| 207 | private function setPost($post) |
| 208 | { |
| 209 | $this->post = $this->post_data_factory->build($post); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /** |
| 214 | * Get count of published posts |
| 215 | * @param object $pages (WP Query object) |
| 216 | */ |
| 217 | private function publishCount($pages) |
| 218 | { |
| 219 | $publish_count = 1; |
| 220 | if ( $this->parentTrashed($pages) ) return; |
| 221 | foreach ( $pages->posts as $p ){ |
| 222 | if ( $p->post_status !== 'trash' ) $publish_count++; |
| 223 | } |
| 224 | return $publish_count; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /** |
| 229 | * Is this a search |
| 230 | * @return boolean |
| 231 | */ |
| 232 | private function isSearch() |
| 233 | { |
| 234 | return ( isset($_GET['search']) && $_GET['search'] !== "" ) ? true : false; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /** |
| 239 | * Loop through all the pages and create the nested / sortable list |
| 240 | * Recursive Method, called in page.php view |
| 241 | */ |
| 242 | private function loopPosts($parent_id = 0, $count = 0) |
| 243 | { |
| 244 | $this->setTaxonomies(); |
| 245 | $post_type = ( $this->post_type->name == 'page' ) ? array('page', 'np-redirect') : array($this->post_type->name); |
| 246 | $query_args = array( |
| 247 | 'post_type' => $post_type, |
| 248 | 'posts_per_page' => -1, |
| 249 | 'author' => $this->sort_options->author, |
| 250 | 'orderby' => $this->sort_options->orderby, |
| 251 | 'post_status' => array('publish', 'pending', 'draft', 'private', 'future', 'trash'), |
| 252 | 'post_parent' => $parent_id, |
| 253 | 'order' => $this->sort_options->order |
| 254 | ); |
| 255 | |
| 256 | if ( $this->isSearch() ) $query_args = $this->searchParams($query_args); |
| 257 | |
| 258 | $pages = new \WP_Query(apply_filters('nestedpages_page_listing', $query_args)); |
| 259 | |
| 260 | if ( $pages->have_posts() ) : |
| 261 | $count++; |
| 262 | |
| 263 | if ( $this->publishCount($pages) > 1 ){ |
| 264 | $this->listOpening($pages, $count); |
| 265 | } |
| 266 | |
| 267 | while ( $pages->have_posts() ) : $pages->the_post(); |
| 268 | |
| 269 | global $post; |
| 270 | $this->setPost($post); |
| 271 | |
| 272 | if ( $this->post->status !== 'trash' ) : |
| 273 | |
| 274 | echo '<li id="menuItem_' . $this->post->id . '" class="page-row'; |
| 275 | |
| 276 | // Published? |
| 277 | if ( $this->post->status == 'publish' ) echo ' published'; |
| 278 | |
| 279 | // Hidden in Nested Pages? |
| 280 | if ( $this->post->np_status == 'hide' ) echo ' np-hide'; |
| 281 | |
| 282 | // Taxonomies |
| 283 | echo ' ' . $this->post_repo->getTaxonomyCSS($this->post->id, $this->h_taxonomies); |
| 284 | echo ' ' . $this->post_repo->getTaxonomyCSS($this->post->id, $this->f_taxonomies, false); |
| 285 | |
| 286 | echo '">'; |
| 287 | |
| 288 | $count++; |
| 289 | |
| 290 | $row_view = ( $this->post->type !== 'np-redirect' ) ? 'partials/row' : 'partials/row-link'; |
| 291 | include( Helpers::view($row_view) ); |
| 292 | |
| 293 | endif; // trash status |
| 294 | |
| 295 | if ( !$this->isSearch() ) $this->loopPosts($this->post->id, $count); |
| 296 | |
| 297 | if ( $this->post->status !== 'trash' ) { |
| 298 | echo '</li>'; |
| 299 | } |
| 300 | |
| 301 | endwhile; // Loop |
| 302 | |
| 303 | if ( $this->publishCount($pages) > 1 ){ |
| 304 | echo '</ol>'; |
| 305 | } |
| 306 | |
| 307 | endif; wp_reset_postdata(); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | /** |
| 312 | * Search Posts |
| 313 | */ |
| 314 | private function searchParams($query_args) |
| 315 | { |
| 316 | $query_args['post_title_like'] = sanitize_text_field($_GET['search']); |
| 317 | unset($query_args['post_parent']); |
| 318 | return $query_args; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * Parent Trash Status |
| 324 | * @param WP Query object |
| 325 | * @return boolean |
| 326 | */ |
| 327 | private function parentTrashed($pages) |
| 328 | { |
| 329 | if ( !isset($pages->query['post_parent']) ) return false; |
| 330 | if ( get_post_status($pages->query['post_parent']) == 'trash' ) return true; |
| 331 | return false; |
| 332 | |
| 333 | } |
| 334 | |
| 335 | |
| 336 | } |