PostCloner.php
3 years ago
PostDataFactory.php
3 years ago
PostFactory.php
2 days ago
PostRepository.php
5 years ago
PostSaveActions.php
3 years ago
PostTrashActions.php
8 years ago
PostUpdateRepository.php
2 days ago
PrivatePostParent.php
3 years ago
PostRepository.php
225 lines
| 1 | <?php |
| 2 | namespace NestedPages\Entities\Post; |
| 3 | |
| 4 | class PostRepository |
| 5 | { |
| 6 | /** |
| 7 | * Get count of hidden posts |
| 8 | * @since 1.1.4 |
| 9 | */ |
| 10 | public function getHiddenCount($type) |
| 11 | { |
| 12 | if ( in_array('page', $type) ) array_push($type, 'np-redirect'); |
| 13 | $hidden = new \WP_Query([ |
| 14 | 'post_type' => $type, |
| 15 | 'meta_key' => '_nested_pages_status', |
| 16 | 'meta_value' => 'hide', |
| 17 | 'perm' => 'readable']); |
| 18 | return $hidden->found_posts; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Get Trash Count (pages) |
| 23 | * @since 1.1.4 |
| 24 | */ |
| 25 | public function trashedCount($post_type) |
| 26 | { |
| 27 | $trashed = new \WP_Query(['post_type'=>$post_type,'post_status'=>'trash','posts_per_page'=>-1]); |
| 28 | return $trashed->found_posts; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get count of published posts |
| 33 | * @param object $pages |
| 34 | */ |
| 35 | public function publishCount($pages) |
| 36 | { |
| 37 | $publish_count = 1; |
| 38 | foreach ( $pages->posts as $p ){ |
| 39 | if ( $p->post_status !== 'trash' ) $publish_count++; |
| 40 | } |
| 41 | return $publish_count; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Return css class string of taxonomies |
| 46 | * @param object post object with taxonomies added |
| 47 | * @return string |
| 48 | */ |
| 49 | public function getTaxonomyCSS($post, $h_taxonomies = [], $f_taxonomies = []) |
| 50 | { |
| 51 | $out = ' '; |
| 52 | |
| 53 | // Build Hierarchical string |
| 54 | if ( count($h_taxonomies) > 0 ) { |
| 55 | foreach ( $h_taxonomies as $taxonomy ){ |
| 56 | $taxname = $taxonomy->name; |
| 57 | if ( !isset($post->$taxname) ) continue; |
| 58 | $terms = $post->$taxname; |
| 59 | foreach ( $terms as $term ){ |
| 60 | $out .= 'in-' . $taxonomy->name . '-' . $term . ' '; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Build Non-Hierarchical string |
| 66 | if ( count($f_taxonomies) > 0 ) { |
| 67 | foreach ( $f_taxonomies as $taxonomy ){ |
| 68 | $taxname = $taxonomy->name; |
| 69 | if ( !isset($post->$taxname) ) continue; |
| 70 | $terms = $post->$taxname; |
| 71 | foreach ( $terms as $term ){ |
| 72 | $out .= 'inf-' . $taxonomy->name . '-nps-' . $term . ' '; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return $out; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get an array of pages given an array of IDs |
| 81 | * @since 1.1.8 (used in creation of new pages) |
| 82 | * @param ids array |
| 83 | * @return array |
| 84 | */ |
| 85 | public function postArray($ids, $post_type) |
| 86 | { |
| 87 | $pages = []; |
| 88 | $page_query = new \WP_Query([ |
| 89 | 'post_type' => $post_type, |
| 90 | 'posts_per_page' => -1, |
| 91 | 'post__in' => $ids, |
| 92 | 'post_status' => array('publish', 'draft') |
| 93 | ]); |
| 94 | if ( $page_query->have_posts() ) : $c = 0; while ( $page_query->have_posts() ) : $page_query->the_post(); |
| 95 | global $post; |
| 96 | |
| 97 | $pages[$c]['id'] = get_the_id(); |
| 98 | $pages[$c]['title'] = get_the_title(); |
| 99 | $pages[$c]['slug'] = $post->post_name; |
| 100 | $pages[$c]['author'] = get_the_author_meta('ID'); |
| 101 | $pages[$c]['author_formatted'] = get_the_author(); |
| 102 | $pages[$c]['status'] = ucfirst(get_post_status()); |
| 103 | $pages[$c]['page_template'] = get_page_template_slug($post->ID); |
| 104 | $pages[$c]['post_parent'] = $post->post_parent; |
| 105 | $pages[$c]['edit_link'] = get_edit_post_link($post->ID); |
| 106 | $pages[$c]['view_link'] = get_the_permalink(); |
| 107 | $pages[$c]['delete_link'] = get_delete_post_link($post->ID); |
| 108 | $pages[$c]['comment_status'] = $post->comment_status; |
| 109 | $pages[$c]['comment_count'] = wp_count_comments($post->ID); |
| 110 | |
| 111 | // Date Vars |
| 112 | $pages[$c]['day'] = get_the_time('d'); |
| 113 | $pages[$c]['month'] = get_the_time('m'); |
| 114 | $pages[$c]['year'] = get_the_time('Y'); |
| 115 | $pages[$c]['hour'] = get_the_time('H'); |
| 116 | $pages[$c]['minute'] = get_the_time('i'); |
| 117 | $pages[$c]['datepicker'] = date_i18n('n/j/Y', get_the_time('U')); |
| 118 | $pages[$c]['time'] = date_i18n('H:i', get_the_time('U')); |
| 119 | $pages[$c]['formattedtime'] = date_i18n('g:i', get_the_time('U')); |
| 120 | $pages[$c]['ampm'] = get_the_time('a'); |
| 121 | $pages[$c]['date_formatted'] = get_the_date(); |
| 122 | |
| 123 | // NP Variables |
| 124 | $all_meta = get_post_meta(get_the_id()); |
| 125 | $pages[$c]['post_meta'] = $all_meta; |
| 126 | $pages[$c]['np_nav_status'] = ( isset($all_meta['np_nav_status']) && $all_meta['np_nav_status'][0] == 'hide' ) ? 'hide' : 'show'; |
| 127 | |
| 128 | $c++; endwhile; endif; wp_reset_postdata(); |
| 129 | return $pages; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Empty the Trash for a given post type |
| 134 | * @since 1.3.1 |
| 135 | */ |
| 136 | public function emptyTrash($post_type) |
| 137 | { |
| 138 | $posts_q = new \WP_Query(['post_type'=>$post_type, 'post_status'=>'trash', 'posts_per_page'=>-1]); |
| 139 | if ( $posts_q->have_posts() ) : while ( $posts_q->have_posts() ) : $posts_q->the_post(); |
| 140 | $capability = ( $post_type == 'page' ) ? 'delete_page' : 'delete_posts'; |
| 141 | if( current_user_can( $capability, get_the_id() ) ) |
| 142 | wp_delete_post(get_the_id(), true); |
| 143 | endwhile; endif; wp_reset_postdata(); |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Does a post exist? |
| 149 | * @since 1.7.0 |
| 150 | * @param int post_id |
| 151 | * @return boolean |
| 152 | */ |
| 153 | public function postExists($post_id, $post_type = 'post') |
| 154 | { |
| 155 | $post_q = new \WP_Query(['post_type' => $post_type, 'p' => $post_id]); |
| 156 | if ( $post_q->have_posts() ){ |
| 157 | wp_reset_postdata(); |
| 158 | return true; |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get all terms for a post |
| 165 | */ |
| 166 | public function getAllTerms($post_id) |
| 167 | { |
| 168 | global $wpdb; |
| 169 | $query = $wpdb->prepare("SELECT p.post_title, tr.term_taxonomy_id AS tax_id, t.slug AS term_name, tt.taxonomy AS tax_name, tt.term_id AS term_id FROM {$wpdb->prefix}posts AS p INNER JOIN {$wpdb->prefix}term_relationships AS tr ON tr.object_id = p.ID LEFT JOIN {$wpdb->prefix}terms AS t ON t.term_id = tr.term_taxonomy_id LEFT JOIN {$wpdb->prefix}term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id WHERE p.ID = %s", $post_id); |
| 170 | return $wpdb->get_results($query); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get a nested array of posts based on a parent |
| 175 | * @param $parent_id (int) |
| 176 | * @param $post_type (string) |
| 177 | * @param $include_parent (bool), whether to include parent in tree |
| 178 | */ |
| 179 | public function postTree($parent_id = 0, $post_type = 'page', $include_parent = true) |
| 180 | { |
| 181 | $posts = []; |
| 182 | if ( $parent_id !== 0 && $include_parent ){ |
| 183 | $args = [ |
| 184 | 'posts_per_page' => 1, |
| 185 | 'post_type' => $post_type, |
| 186 | 'p' => $parent_id |
| 187 | ]; |
| 188 | $q = new \WP_Query(apply_filters('nestedpages_post_tree_parent', $args)); |
| 189 | if ( $q->have_posts() ) $posts = $q->posts; |
| 190 | wp_reset_postdata(); |
| 191 | } |
| 192 | $children = $this->getChildren($parent_id, $post_type, $posts); |
| 193 | $posts = []; |
| 194 | foreach ( $children as $child ){ |
| 195 | $posts[$child->ID] = $child->post_parent; |
| 196 | } |
| 197 | return $posts; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get all children of a post/page |
| 202 | * Recursive function |
| 203 | */ |
| 204 | public function getChildren($parent_id, $post_type, $posts = []) |
| 205 | { |
| 206 | $new_posts = []; |
| 207 | $args = [ |
| 208 | 'posts_per_page' => -1, |
| 209 | 'post_type' => $post_type, |
| 210 | 'post_parent' => $parent_id, |
| 211 | 'orderby' => 'menu_order', |
| 212 | 'order' => 'ASC' |
| 213 | ]; |
| 214 | $q = new \WP_Query(apply_filters('nestedpages_post_tree_children', $args)); |
| 215 | if ( $q->have_posts() ) $new_posts = $q->posts; |
| 216 | wp_reset_postdata(); |
| 217 | if ( !empty($new_posts) ){ |
| 218 | $posts = array_merge($posts, $new_posts); |
| 219 | foreach ( $new_posts as $post ){ |
| 220 | return $this->getChildren($post->ID, $post_type, $posts); |
| 221 | } |
| 222 | } |
| 223 | return $posts; |
| 224 | } |
| 225 | } |