| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\REST; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Core\BaseAPI; |
| 6 |
|
| 7 |
class DocCategories extends BaseAPI { |
| 8 |
public function permission_check(): bool { |
| 9 |
return current_user_can( 'edit_docs' ); |
| 10 |
} |
| 11 |
|
| 12 |
public function register() { |
| 13 |
$this->get( 'doc-categories', [ $this, 'get_response' ] ); |
| 14 |
} |
| 15 |
|
| 16 |
public function get_response( $request ) { |
| 17 |
global $wpdb; |
| 18 |
|
| 19 |
$mkb = $request->get_param( 'knowledge_base' ); |
| 20 |
|
| 21 |
$terms_query = betterdocs()->query->terms_query( |
| 22 |
[ |
| 23 |
'hide_empty' => false, |
| 24 |
'taxonomy' => 'doc_category', |
| 25 |
'orderby' => 'meta_value_num', |
| 26 |
'meta_key' => 'doc_category_order', |
| 27 |
'order' => 'ASC' |
| 28 |
] |
| 29 |
); |
| 30 |
|
| 31 |
if ( ! empty( $mkb ) ) { |
| 32 |
$terms_query['meta_query'] = [ |
| 33 |
'relation' => 'AND', |
| 34 |
[ |
| 35 |
'key' => 'doc_category_knowledge_base', |
| 36 |
'value' => $mkb, |
| 37 |
'compare' => 'LIKE' |
| 38 |
] |
| 39 |
]; |
| 40 |
$terms_query['order'] = 'ASC'; |
| 41 |
} |
| 42 |
|
| 43 |
$terms = get_terms( $terms_query ); |
| 44 |
$response = []; |
| 45 |
|
| 46 |
foreach ( $terms as $term ) { |
| 47 |
$original_args = [ |
| 48 |
'post_type' => 'docs', |
| 49 |
'posts_per_page' => '-1', |
| 50 |
'post_status' => 'any', |
| 51 |
'term_id' => $term->term_id, |
| 52 |
'term_slug' => $term->slug, |
| 53 |
'nested_subcategory' => false, |
| 54 |
'orderby' => 'betterdocs_order' |
| 55 |
]; |
| 56 |
|
| 57 |
if ( ! empty( $mkb ) ) { |
| 58 |
$original_args['multiple_kb'] = true; |
| 59 |
$original_args['kb_slug'] = $mkb; |
| 60 |
} |
| 61 |
|
| 62 |
$query_args = betterdocs()->query->docs_query_args( $original_args ); |
| 63 |
|
| 64 |
$posts = betterdocs()->query->get_posts( $query_args, true ); |
| 65 |
$response[ $term->term_id ] = []; |
| 66 |
|
| 67 |
if ( ! $posts->have_posts() ) { |
| 68 |
wp_reset_query(); |
| 69 |
} |
| 70 |
while ( $posts->have_posts() ) : |
| 71 |
$posts->the_post(); |
| 72 |
$data = $this->get_doc_data( get_the_ID() ); |
| 73 |
array_push( $response[ $term->term_id ], $data ); |
| 74 |
endwhile; |
| 75 |
|
| 76 |
wp_reset_postdata(); |
| 77 |
wp_reset_query(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Uncategories Docs |
| 82 |
*/ |
| 83 |
$_post__not_in_query = $wpdb->prepare( |
| 84 |
"SELECT ID as post_id from $wpdb->posts WHERE post_type = %s AND post_status != 'trash' AND post_status != 'auto-draft' AND ID NOT IN ( SELECT object_id as post_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ( SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s ) )", |
| 85 |
'docs', |
| 86 |
'doc_category' |
| 87 |
); |
| 88 |
|
| 89 |
$_post__not_in = $wpdb->get_col( $_post__not_in_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 90 |
|
| 91 |
if ( ! empty( $_post__not_in ) ) { |
| 92 |
$uncategorized_docs = []; |
| 93 |
$_uncategorized_docs_query = new \WP_Query( |
| 94 |
[ |
| 95 |
'post_type' => 'docs', |
| 96 |
'post_status' => 'any', |
| 97 |
'post__in' => $_post__not_in |
| 98 |
] |
| 99 |
); |
| 100 |
|
| 101 |
if ( ! $_uncategorized_docs_query->have_posts() ) { |
| 102 |
wp_reset_query(); |
| 103 |
} |
| 104 |
while ( $_uncategorized_docs_query->have_posts() ) : |
| 105 |
$_uncategorized_docs_query->the_post(); |
| 106 |
$data = $this->get_doc_data( get_the_ID() ); |
| 107 |
array_push( $uncategorized_docs, $data ); |
| 108 |
endwhile; |
| 109 |
|
| 110 |
wp_reset_postdata(); |
| 111 |
wp_reset_query(); |
| 112 |
|
| 113 |
$response['uncategorized'] = $uncategorized_docs; |
| 114 |
} |
| 115 |
|
| 116 |
return $response; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get Doc Data Based On Doc ID |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function get_doc_data( $id ) { |
| 125 |
$post_data = get_post( $id ); |
| 126 |
$data = [ |
| 127 |
'author' => (int) $post_data->post_author, |
| 128 |
'author_info' => [ |
| 129 |
'name' => get_the_author_meta( 'display_name', $post_data->post_author ), |
| 130 |
'author_nicename' => get_the_author_meta( 'nicename', $post_data->post_author ), |
| 131 |
'author_url' => get_author_posts_url( $post_data->post_author ) |
| 132 |
], |
| 133 |
'unique_id' => uniqid( 'doc' ), |
| 134 |
'id' => $post_data->ID, |
| 135 |
'title' => $post_data->post_title, |
| 136 |
'slug' => get_post_field( 'post_name', $id ), |
| 137 |
'link' => get_permalink( $id ), |
| 138 |
'status' => get_post_status(), |
| 139 |
'date' => $post_data->post_date, |
| 140 |
'date_gmt' => $post_data->post_date_gmt, |
| 141 |
'doc_category' => wp_get_post_terms( $id, 'doc_category', [ 'fields' => 'ids' ] ), |
| 142 |
'doc_tag' => wp_get_post_terms( $id, 'doc_tag', [ 'fields' => 'ids' ] ), |
| 143 |
'password' => $post_data->post_password, |
| 144 |
'comment_status' => $post_data->comment_status |
| 145 |
]; |
| 146 |
|
| 147 |
if ( taxonomy_exists( 'knowledge_base' ) ) { |
| 148 |
$data['knowledge_base'] = wp_get_post_terms( $id, 'knowledge_base', [ 'fields' => 'ids' ] ); |
| 149 |
} |
| 150 |
|
| 151 |
return $data; |
| 152 |
} |
| 153 |
} |
| 154 |
|