PostDataFactory.php
11 years ago
PostFactory.php
11 years ago
PostRepository.php
11 years ago
PostTrashActions.php
11 years ago
PostUpdateRepository.php
11 years ago
PostRepository.php
130 lines
| 1 | <?php namespace NestedPages\Entities\Post; |
| 2 | |
| 3 | class PostRepository { |
| 4 | |
| 5 | /** |
| 6 | * Get count of hidden posts |
| 7 | * @since 1.1.4 |
| 8 | */ |
| 9 | public function getHiddenCount($type) |
| 10 | { |
| 11 | if ( in_array('page', $type) ) array_push($type, 'np-redirect'); |
| 12 | $hidden = new \WP_Query(array( |
| 13 | 'post_type' => $type, |
| 14 | 'meta_key' => 'nested_pages_status', |
| 15 | 'meta_value' => 'hide', |
| 16 | 'perm' => 'readable')); |
| 17 | return $hidden->found_posts; |
| 18 | } |
| 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(array('post_type'=>$post_type,'post_status'=>'trash','posts_per_page'=>-1)); |
| 28 | return $trashed->found_posts; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Get count of published posts |
| 34 | * @param object $pages |
| 35 | */ |
| 36 | public function publishCount($pages) |
| 37 | { |
| 38 | $publish_count = 1; |
| 39 | foreach ( $pages->posts as $p ){ |
| 40 | if ( $p->post_status !== 'trash' ) $publish_count++; |
| 41 | } |
| 42 | return $publish_count; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Return css class string of taxonomies |
| 48 | * @param int post_id |
| 49 | * @return string |
| 50 | */ |
| 51 | public function getTaxonomyCSS($post_id, $taxonomies, $hierarchical = true) |
| 52 | { |
| 53 | $out = ''; |
| 54 | if ( count($taxonomies) > 0 ) { |
| 55 | foreach ( $taxonomies as $taxonomy ){ |
| 56 | $terms = wp_get_post_terms($post_id, $taxonomy->name); |
| 57 | foreach ( $terms as $term ){ |
| 58 | $out .= ( $hierarchical ) ? 'in-' : 'inf-'; |
| 59 | $out .= $taxonomy->name; |
| 60 | $out .= ( $hierarchical ) ? '-' : '-nps-'; |
| 61 | $out .= $term->term_id . ' '; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | return $out; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Get an array of pages given an array of IDs |
| 71 | * @since 1.1.8 (used in creation of new pages) |
| 72 | * @param ids array |
| 73 | * @return array |
| 74 | */ |
| 75 | public function postArray($ids, $post_type) |
| 76 | { |
| 77 | $pages = array(); |
| 78 | $page_query = new \WP_Query(array( |
| 79 | 'post_type' => $post_type, |
| 80 | 'posts_per_page' => -1, |
| 81 | 'post__in' => $ids, |
| 82 | 'post_status' => array('publish', 'draft') |
| 83 | )); |
| 84 | if ( $page_query->have_posts() ) : $c = 0; while ( $page_query->have_posts() ) : $page_query->the_post(); |
| 85 | global $post; |
| 86 | |
| 87 | $pages[$c]['id'] = get_the_id(); |
| 88 | $pages[$c]['title'] = get_the_title(); |
| 89 | $pages[$c]['slug'] = $post->post_name; |
| 90 | $pages[$c]['author'] = get_the_author_meta('ID'); |
| 91 | $pages[$c]['status'] = ucfirst(get_post_status()); |
| 92 | $pages[$c]['page_template'] = get_page_template_slug($post->ID); |
| 93 | $pages[$c]['post_parent'] = $post->post_parent; |
| 94 | $pages[$c]['edit_link'] = get_edit_post_link($post->ID); |
| 95 | $pages[$c]['view_link'] = get_the_permalink(); |
| 96 | $pages[$c]['delete_link'] = get_delete_post_link($post->ID); |
| 97 | $pages[$c]['comment_status'] = $post->comment_status; |
| 98 | $pages[$c]['comment_count'] = wp_count_comments($post->ID); |
| 99 | |
| 100 | // Date Vars |
| 101 | $pages[$c]['day'] = get_the_time('d'); |
| 102 | $pages[$c]['month'] = get_the_time('m'); |
| 103 | $pages[$c]['year'] = get_the_time('Y'); |
| 104 | $pages[$c]['hour'] = get_the_time('H'); |
| 105 | $pages[$c]['minute'] = get_the_time('i'); |
| 106 | $pages[$c]['datepicker'] = date_i18n('n/j/Y', get_the_time('U')); |
| 107 | $pages[$c]['time'] = date_i18n('H:i', get_the_time('U')); |
| 108 | $pages[$c]['formattedtime'] = date_i18n('g:i', get_the_time('U')); |
| 109 | $pages[$c]['ampm'] = get_the_time('a'); |
| 110 | |
| 111 | |
| 112 | $c++; endwhile; endif; wp_reset_postdata(); |
| 113 | return $pages; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * Empty the Trash for a given post type |
| 119 | * @since 1.3.1 |
| 120 | */ |
| 121 | public function emptyTrash($post_type) |
| 122 | { |
| 123 | $posts_q = new \WP_Query(array('post_type'=>$post_type, 'post_status'=>'trash', 'posts_per_page'=>-1)); |
| 124 | if ( $posts_q->have_posts() ) : while ( $posts_q->have_posts() ) : $posts_q->the_post(); |
| 125 | wp_delete_post(get_the_id(), true); |
| 126 | endwhile; endif; wp_reset_postdata(); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | } |