Listing.php
2 years ago
ListingActions.php
8 years ago
ListingQuery.php
3 years ago
ListingRepository.php
2 days ago
ListingRepository.php
186 lines
| 1 | <?php |
| 2 | namespace NestedPages\Entities\Listing; |
| 3 | |
| 4 | use NestedPages\Entities\PluginIntegration\IntegrationFactory; |
| 5 | use NestedPages\Entities\PostType\PostTypeRepository; |
| 6 | use NestedPages\Config\SettingsRepository; |
| 7 | |
| 8 | class ListingRepository |
| 9 | { |
| 10 | /** |
| 11 | * Plugin Integrations |
| 12 | */ |
| 13 | public $integrations; |
| 14 | |
| 15 | /** |
| 16 | * Post Type Repository |
| 17 | */ |
| 18 | private $post_type_repo; |
| 19 | |
| 20 | /** |
| 21 | * Settings Repository |
| 22 | */ |
| 23 | public $settings; |
| 24 | |
| 25 | public function __construct() |
| 26 | { |
| 27 | $this->integrations = new IntegrationFactory; |
| 28 | $this->post_type_repo = new PostTypeRepository; |
| 29 | $this->settings = new SettingsRepository; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * User's Toggled Pages |
| 34 | */ |
| 35 | public function visiblePages($post_type) |
| 36 | { |
| 37 | $meta = get_user_meta(get_current_user_id(), 'np_visible_posts', true); |
| 38 | if ( $meta == '1' ) return []; |
| 39 | if ( is_array($meta) ) return []; |
| 40 | $visible = unserialize($meta); |
| 41 | if ( !$visible || !isset($visible[$post_type]) ) $visible = [$post_type => []]; |
| 42 | return $visible[$post_type]; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Taxonomies |
| 47 | */ |
| 48 | public function taxonomies() |
| 49 | { |
| 50 | $args = apply_filters('nestedpages_listing_taxonomies', ['public' => true]); |
| 51 | $taxonomies = get_taxonomies($args, 'objects'); |
| 52 | return $taxonomies; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get all non-empty Terms for a given taxonomy |
| 57 | */ |
| 58 | public function terms($taxonomy) |
| 59 | { |
| 60 | $args = apply_filters('nestedpages_listing_taxonomy_terms', []); |
| 61 | return get_terms($taxonomy, $args); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Post Types |
| 66 | */ |
| 67 | public function postTypes() |
| 68 | { |
| 69 | $types = get_post_types([ |
| 70 | 'public' => true |
| 71 | ], 'objects'); |
| 72 | return $types; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Recent Posts for a given post type |
| 77 | */ |
| 78 | public function recentPosts($post_type) |
| 79 | { |
| 80 | $pq = new \WP_Query([ |
| 81 | 'post_type' => $post_type, |
| 82 | 'posts_per_page' => 10 |
| 83 | ]); |
| 84 | if ( $pq->have_posts() ) : |
| 85 | return $pq->posts; |
| 86 | else : |
| 87 | return false; |
| 88 | endif; wp_reset_postdata(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Is the page, or it's parent translation, assigned a post type? |
| 93 | * @param $page - int - post id |
| 94 | * @param $assigned_pages - array |
| 95 | * @return bool |
| 96 | */ |
| 97 | public function isAssignedPostType($page_id, $assigned_pages) |
| 98 | { |
| 99 | if ( $this->integrations->plugins->wpml->installed && !$this->integrations->plugins->wpml->isDefaultLanguage() ){ |
| 100 | $page_id = $this->integrations->plugins->wpml->getPrimaryLanguagePost($page_id); |
| 101 | } |
| 102 | if ( array_key_exists($page_id, $assigned_pages) ) return true; |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the assigned post type for a page |
| 108 | * @param $page - int - post id |
| 109 | * @param $assigned_pages - array |
| 110 | * @return post type object |
| 111 | */ |
| 112 | public function assignedPostType($page_id, $assigned_pages) |
| 113 | { |
| 114 | if ( $this->integrations->plugins->wpml->installed && !$this->integrations->plugins->wpml->isDefaultLanguage() ){ |
| 115 | $page_id = $this->integrations->plugins->wpml->getPrimaryLanguagePost($page_id); |
| 116 | } |
| 117 | return get_post_type_object($assigned_pages[$page_id]); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the number of published posts for a given post type |
| 122 | */ |
| 123 | public function postCount($post_type) |
| 124 | { |
| 125 | if ( $this->integrations->plugins->wpml->installed ){ |
| 126 | return $this->integrations->plugins->wpml->getPostTypeCountByLanguage($post_type); |
| 127 | } |
| 128 | return wp_count_posts($post_type)->publish; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Is this a search |
| 133 | * @return boolean |
| 134 | */ |
| 135 | public function isSearch() |
| 136 | { |
| 137 | return ( isset($_GET['search']) && $_GET['search'] !== "" ) ? true : false; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Is the list filtered? |
| 142 | */ |
| 143 | public function isFiltered() |
| 144 | { |
| 145 | $taxonomies = get_taxonomies(); |
| 146 | $tax_filtered = false; |
| 147 | foreach ( $taxonomies as $tax ){ |
| 148 | if ( isset($_GET[$tax]) && $_GET[$tax] !== '' ) $tax_filtered = true; |
| 149 | } |
| 150 | return ( (isset($_GET['category']) && $_GET['category'] !== "all") || $tax_filtered ) ? true : false; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Is the list ordered? |
| 155 | */ |
| 156 | public function isOrdered($post_type = null) |
| 157 | { |
| 158 | $ordered = ( isset($_GET['orderby']) && $_GET['orderby'] !== "" ) ? true : false; |
| 159 | if ( $post_type ){ |
| 160 | $initial_orderby = $this->post_type_repo->defaultSortOption($post_type, 'orderby'); |
| 161 | if ( $initial_orderby ) $ordered = true; |
| 162 | } |
| 163 | if ( $ordered && isset($_GET['orderby']) && $_GET['orderby'] == 'menu_order' && !isset($_GET['order']) ) $ordered = false; |
| 164 | // Enables nesting if sorted by menu order in ascending order |
| 165 | if ( isset($_GET['orderby']) && $_GET['orderby'] == 'menu_order' && isset($_GET['order']) && $_GET['order'] == 'ASC' ) $ordered = false; |
| 166 | return $ordered; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Do we show the "link" interface/functionality? |
| 171 | * @param $post_type - obj (WP_Post_Type) |
| 172 | * @param $user - obj (NestedPages\Entities\User\UserRepository) |
| 173 | * @return bool |
| 174 | */ |
| 175 | public function showLinks($post_type, $user) |
| 176 | { |
| 177 | $show_links = ( $user->canPublish($post_type->name) |
| 178 | && $post_type->name == 'page' |
| 179 | && !$this->isSearch() |
| 180 | && !$this->isOrdered($post_type->name) |
| 181 | && !$this->settings->menusDisabled() |
| 182 | && !$this->integrations->plugins->wpml->installed ) |
| 183 | ? true : false; |
| 184 | return apply_filters('nestedpages_show_links', $show_links, $post_type, $user, $this); |
| 185 | } |
| 186 | } |