| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Core; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use WP_Query; |
| 7 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 8 |
use WPDeveloper\BetterDocs\Utils\Helper; |
| 9 |
|
| 10 |
class FAQBuilder extends Base { |
| 11 |
/** |
| 12 |
* REST API namespace |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private $namespace = 'betterdocs'; |
| 16 |
public $post_type = 'betterdocs_faq'; |
| 17 |
public $category = 'betterdocs_faq_category'; |
| 18 |
|
| 19 |
/** |
| 20 |
* |
| 21 |
* Initialize the class and start calling our hooks and filters |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
* |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
// assign default admin capabilities for docs, doc terms, doc tags, knowledge base |
| 28 |
add_action( 'init', [ $this, 'register_post' ] ); |
| 29 |
// fires after a new betterdocs_faq_category is created |
| 30 |
add_action( 'created_betterdocs_faq_category', [ $this, 'action_created_betterdocs_faq_category' ], 10, 2 ); |
| 31 |
add_action( 'rest_api_init', [ $this, 'register_api_endpoint' ] ); |
| 32 |
add_action( 'rest_betterdocs_faq_category_query', [ $this, 'faq_category_orderby_meta' ], 10, 2 ); |
| 33 |
} |
| 34 |
|
| 35 |
public function output() { |
| 36 |
betterdocs()->views->get( 'admin/faq-builder' ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* |
| 41 |
* Register post type and taxonomies |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* |
| 45 |
*/ |
| 46 |
public function register_post() { |
| 47 |
/** |
| 48 |
* Register category taxonomy |
| 49 |
*/ |
| 50 |
$category_labels = [ |
| 51 |
'name' => __( 'FAQ Categories', 'betterdocs' ), |
| 52 |
'singular_name' => __( 'FAQ Category', 'betterdocs' ), |
| 53 |
'all_items' => __( 'FAQ Categories', 'betterdocs' ), |
| 54 |
'parent_item' => __( 'Parent FAQ Category', 'betterdocs' ), |
| 55 |
'parent_item_colon' => __( 'Parent FAQ Category:', 'betterdocs' ), |
| 56 |
'edit_item' => __( 'Edit Category', 'betterdocs' ), |
| 57 |
'update_item' => __( 'Update Category', 'betterdocs' ), |
| 58 |
'add_new_item' => __( 'Add New FAQ Category', 'betterdocs' ), |
| 59 |
'new_item_name' => __( 'New FAQ Category Name', 'betterdocs' ), |
| 60 |
'menu_name' => __( 'Categories', 'betterdocs' ) |
| 61 |
]; |
| 62 |
|
| 63 |
$category_args = [ |
| 64 |
'hierarchical' => true, |
| 65 |
'public' => false, |
| 66 |
'labels' => $category_labels, |
| 67 |
'show_ui' => true, |
| 68 |
'show_admin_column' => true, |
| 69 |
'query_var' => true, |
| 70 |
'show_in_rest' => true, |
| 71 |
'has_archive' => false, |
| 72 |
'rewrite' => false, |
| 73 |
'capabilities' => [ |
| 74 |
'manage_terms' => 'manage_doc_terms', |
| 75 |
'edit_terms' => 'edit_doc_terms', |
| 76 |
'delete_terms' => 'delete_doc_terms', |
| 77 |
'assign_terms' => 'edit_docs' |
| 78 |
] |
| 79 |
]; |
| 80 |
|
| 81 |
register_taxonomy( $this->category, [ $this->post_type ], $category_args ); |
| 82 |
register_term_meta( $this->category, 'order', [ 'show_in_rest' => true ] ); |
| 83 |
register_term_meta( $this->category, 'status', [ 'show_in_rest' => true ] ); |
| 84 |
register_term_meta( $this->category, '_betterdocs_faq_order', [ 'show_in_rest' => true ] ); |
| 85 |
register_term_meta( $this->category, 'faq_group_icon', [ 'show_in_rest' => true ]); |
| 86 |
|
| 87 |
/** |
| 88 |
* Register post type |
| 89 |
*/ |
| 90 |
$labels = [ |
| 91 |
'name' => __( 'BetterDocs FAQ', 'betterdocs' ), |
| 92 |
'singular_name' => __( 'BetterDocs FAQ', 'betterdocs' ), |
| 93 |
'menu_name' => __( 'FAQ', 'betterdocs' ), |
| 94 |
'name_admin_bar' => __( 'FAQ', 'betterdocs' ), |
| 95 |
'add_new' => __( 'Add New', 'betterdocs' ), |
| 96 |
'add_new_item' => __( 'Add New FAQ', 'betterdocs' ), |
| 97 |
'new_item' => __( 'New FAQ', 'betterdocs' ), |
| 98 |
'edit_item' => __( 'Edit FAQ', 'betterdocs' ), |
| 99 |
'view_item' => __( 'View FAQ', 'betterdocs' ), |
| 100 |
'all_items' => __( 'All FAQ', 'betterdocs' ), |
| 101 |
'search_items' => __( 'Search FAQ', 'betterdocs' ), |
| 102 |
'parent_item_colorn' => null, |
| 103 |
'not_found' => __( 'No FAQ found', 'betterdocs' ), |
| 104 |
'not_found_in_trash' => __( 'No FAQ found in trash', 'betterdocs' ) |
| 105 |
]; |
| 106 |
|
| 107 |
$args = [ |
| 108 |
'labels' => $labels, |
| 109 |
'description' => __( 'Add new faq from here', 'betterdocs' ), |
| 110 |
'public' => false, |
| 111 |
'public_queryable' => true, |
| 112 |
'exclude_from_search' => false, |
| 113 |
'show_ui' => true, |
| 114 |
'show_in_menu' => false, |
| 115 |
'query_var' => true, |
| 116 |
'capability_type' => [ 'doc', 'docs' ], |
| 117 |
'hierarchical' => true, |
| 118 |
'map_meta_cap' => true, |
| 119 |
'has_archive' => false, |
| 120 |
'rewrite' => false, |
| 121 |
'show_in_rest' => true, |
| 122 |
'menu_icon' => betterdocs()->assets->icon( 'betterdocs-icon-white.svg' ), |
| 123 |
'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt', 'author', 'revisions', 'custom-fields', 'comments' ] |
| 124 |
]; |
| 125 |
|
| 126 |
register_post_type( $this->post_type, $args ); |
| 127 |
register_post_meta( $this->post_type, 'faq_open_by_default', [ 'show_in_rest' => true, 'single' => true ] ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Default the taxonomy's terms' order if it's not set. |
| 132 |
* |
| 133 |
* @param string $tax_slug The taxonomy's slug. |
| 134 |
*/ |
| 135 |
public function action_created_betterdocs_faq_category( $term_id ) { |
| 136 |
$order = $this->get_max_taxonomy_order( 'betterdocs_faq_category' ); |
| 137 |
update_term_meta( $term_id, 'order', $order++ ); |
| 138 |
update_term_meta( $term_id, 'status', 1 ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Default the taxonomy's terms' order if it's not set. |
| 143 |
* |
| 144 |
* @param string $tax_slug The taxonomy's slug. |
| 145 |
*/ |
| 146 |
public function default_term_order( $tax_slug ) { |
| 147 |
$terms = get_terms( |
| 148 |
[ |
| 149 |
'taxonomy' => $tax_slug, |
| 150 |
'hide_empty' => false, |
| 151 |
] |
| 152 |
); |
| 153 |
$order = $this->get_max_taxonomy_order( $tax_slug ); |
| 154 |
|
| 155 |
foreach ( $terms as $term ) { |
| 156 |
if ( ! get_term_meta( $term->term_id, 'order', true ) ) { |
| 157 |
update_term_meta( $term->term_id, 'order', $order ); |
| 158 |
++$order; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get the maximum order for this taxonomy. This will be applied to terms that don't have a tax position. |
| 165 |
*/ |
| 166 |
private function get_max_taxonomy_order( $tax_slug ) { |
| 167 |
global $wpdb; |
| 168 |
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder |
| 169 |
$max_term_order = $wpdb->get_col( |
| 170 |
$wpdb->prepare( |
| 171 |
"SELECT MAX( CAST( tm.meta_value AS UNSIGNED ) ) |
| 172 |
FROM $wpdb->terms t |
| 173 |
JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id AND tt.taxonomy = '%s' |
| 174 |
JOIN $wpdb->termmeta tm ON tm.term_id = t.term_id WHERE tm.meta_key = 'order'", |
| 175 |
$tax_slug |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
$max_term_order = is_array( $max_term_order ) ? current( $max_term_order ) : 0; |
| 180 |
|
| 181 |
return (int) $max_term_order === 0 || empty( $max_term_order ) ? 1 : (int) $max_term_order + 1; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Re-Order the taxonomies based on the order value. |
| 186 |
* |
| 187 |
* @param array $pieces Array of SQL query clauses. |
| 188 |
* @param array $taxonomies Array of taxonomy names. |
| 189 |
* @param array $args Array of term query args. |
| 190 |
*/ |
| 191 |
public function set_tax_order( $pieces, $taxonomies, $args ) { |
| 192 |
foreach ( $taxonomies as $taxonomy ) { |
| 193 |
global $wpdb; |
| 194 |
|
| 195 |
if ( $taxonomy === 'betterdocs_faq_category' ) { |
| 196 |
$join_statement = " LEFT JOIN $wpdb->termmeta AS term_meta ON t.term_id = term_meta.term_id AND term_meta.meta_key = 'order'"; |
| 197 |
|
| 198 |
if ( ! $this->does_substring_exist( $pieces['join'], $join_statement ) ) { |
| 199 |
$pieces['join'] .= $join_statement; |
| 200 |
} |
| 201 |
|
| 202 |
$pieces['orderby'] = 'ORDER BY CAST( term_meta.meta_value AS UNSIGNED )'; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return $pieces; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Order the taxonomies on the front end. |
| 211 |
*/ |
| 212 |
public function front_end_order_terms() { |
| 213 |
if ( ! is_admin() ) { |
| 214 |
add_filter( 'terms_clauses', [ $this, 'set_tax_order' ], 10, 3 ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Check if a substring exists inside a string. |
| 220 |
* |
| 221 |
* @param string $string The main string (haystack) we're searching in. |
| 222 |
* @param string $substring The substring we're searching for. |
| 223 |
* |
| 224 |
* @return bool True if substring exists, else false. |
| 225 |
*/ |
| 226 |
protected function does_substring_exist( $string, $substring ) { |
| 227 |
return strstr( $string, $substring ) !== false; |
| 228 |
} |
| 229 |
|
| 230 |
public function register_api_endpoint() { |
| 231 |
register_rest_route( |
| 232 |
$this->namespace, |
| 233 |
'/faq/sample_data', |
| 234 |
[ |
| 235 |
'methods' => [ 'POST' ], |
| 236 |
'callback' => [ $this, 'create_faq_sample' ], |
| 237 |
'permission_callback' => function () { |
| 238 |
return current_user_can( 'edit_others_posts' ); |
| 239 |
} |
| 240 |
] |
| 241 |
); |
| 242 |
|
| 243 |
register_rest_route( |
| 244 |
$this->namespace, |
| 245 |
'/faq/posts/(?P<type>\S+)', |
| 246 |
[ |
| 247 |
'methods' => [ 'GET' ], |
| 248 |
'callback' => [ $this, 'fetch_faq_posts' ], |
| 249 |
'permission_callback' => function () { |
| 250 |
return current_user_can( 'edit_others_posts' ); |
| 251 |
} |
| 252 |
] |
| 253 |
); |
| 254 |
|
| 255 |
register_rest_route( |
| 256 |
$this->namespace, |
| 257 |
'/faq/create_category', |
| 258 |
[ |
| 259 |
'methods' => [ 'POST' ], |
| 260 |
'callback' => [ $this, 'create_faq_category' ], |
| 261 |
'permission_callback' => function () { |
| 262 |
return current_user_can( 'edit_others_posts' ); |
| 263 |
} |
| 264 |
] |
| 265 |
); |
| 266 |
|
| 267 |
register_rest_route( |
| 268 |
$this->namespace, |
| 269 |
'/faq/update_category', |
| 270 |
[ |
| 271 |
'methods' => [ 'POST' ], |
| 272 |
'callback' => [ $this, 'update_faq_category' ], |
| 273 |
'permission_callback' => function () { |
| 274 |
return current_user_can( 'edit_others_posts' ); |
| 275 |
} |
| 276 |
] |
| 277 |
); |
| 278 |
|
| 279 |
register_rest_route( |
| 280 |
$this->namespace, |
| 281 |
'/faq/delete_category', |
| 282 |
[ |
| 283 |
'methods' => [ 'POST' ], |
| 284 |
'callback' => [ $this, 'delete_faq_category' ], |
| 285 |
'permission_callback' => function () { |
| 286 |
return current_user_can( 'edit_others_posts' ); |
| 287 |
} |
| 288 |
] |
| 289 |
); |
| 290 |
|
| 291 |
register_rest_route( |
| 292 |
$this->namespace, |
| 293 |
'/faq/create_post', |
| 294 |
[ |
| 295 |
'methods' => [ 'POST' ], |
| 296 |
'callback' => [ $this, 'create_betterdocs_faq' ], |
| 297 |
'permission_callback' => function () { |
| 298 |
return current_user_can( 'edit_others_posts' ); |
| 299 |
} |
| 300 |
] |
| 301 |
); |
| 302 |
|
| 303 |
register_rest_route( |
| 304 |
$this->namespace, |
| 305 |
'/faq/update_post', |
| 306 |
[ |
| 307 |
'methods' => [ 'POST' ], |
| 308 |
'callback' => [ $this, 'update_betterdocs_faq' ], |
| 309 |
'permission_callback' => function () { |
| 310 |
return current_user_can( 'edit_others_posts' ); |
| 311 |
} |
| 312 |
] |
| 313 |
); |
| 314 |
|
| 315 |
register_rest_route( |
| 316 |
$this->namespace, |
| 317 |
'/faq/delete_post', |
| 318 |
[ |
| 319 |
'methods' => [ 'POST' ], |
| 320 |
'callback' => [ $this, 'delete_betterdocs_faq' ], |
| 321 |
'permission_callback' => function () { |
| 322 |
return current_user_can( 'edit_others_posts' ); |
| 323 |
} |
| 324 |
] |
| 325 |
); |
| 326 |
|
| 327 |
register_rest_route( |
| 328 |
$this->namespace, |
| 329 |
'/faq/category_status', |
| 330 |
[ |
| 331 |
'methods' => [ 'POST' ], |
| 332 |
'callback' => [ $this, 'update_category_status' ], |
| 333 |
'permission_callback' => function () { |
| 334 |
return current_user_can( 'edit_others_posts' ); |
| 335 |
} |
| 336 |
] |
| 337 |
); |
| 338 |
|
| 339 |
register_rest_route( |
| 340 |
$this->namespace, |
| 341 |
'/faq/category_order', |
| 342 |
[ |
| 343 |
'methods' => [ 'POST' ], |
| 344 |
'callback' => [ $this, 'update_faq_category_order' ], |
| 345 |
'permission_callback' => function () { |
| 346 |
return current_user_can( 'edit_others_posts' ); |
| 347 |
} |
| 348 |
] |
| 349 |
); |
| 350 |
|
| 351 |
register_rest_route( |
| 352 |
$this->namespace, |
| 353 |
'/faq/update_order_by_category', |
| 354 |
[ |
| 355 |
'methods' => [ 'POST' ], |
| 356 |
'callback' => [ $this, 'update_faq_order_by_category' ], |
| 357 |
'permission_callback' => function () { |
| 358 |
return current_user_can( 'edit_others_posts' ); |
| 359 |
} |
| 360 |
] |
| 361 |
); |
| 362 |
|
| 363 |
register_rest_route( |
| 364 |
$this->namespace, |
| 365 |
'/faq/uncategorised', |
| 366 |
[ |
| 367 |
'methods' => [ 'GET' ], |
| 368 |
'callback' => [ $this, 'get_uncategorised_faq' ], |
| 369 |
'permission_callback' => function () { |
| 370 |
return current_user_can( 'edit_others_posts' ); |
| 371 |
} |
| 372 |
] |
| 373 |
); |
| 374 |
|
| 375 |
register_rest_route( |
| 376 |
$this->namespace, |
| 377 |
'/faq/category_search', |
| 378 |
[ |
| 379 |
'methods' => [ 'GET' ], |
| 380 |
'callback' => [ $this, 'category_search' ], |
| 381 |
'permission_callback' => function () { |
| 382 |
return current_user_can( 'edit_others_posts' ); |
| 383 |
}, |
| 384 |
'args' => [ |
| 385 |
'title' => [ |
| 386 |
'type' => 'string', |
| 387 |
'required' => true, |
| 388 |
'sanitize_callback' => 'sanitize_text_field' |
| 389 |
] |
| 390 |
] |
| 391 |
] |
| 392 |
); |
| 393 |
} |
| 394 |
|
| 395 |
public function create_faq_sample( $params ) { |
| 396 |
$sample_data = json_decode( $params->get_param( 'sample_data' ), true ); |
| 397 |
foreach ( $sample_data as $key => $value ) { |
| 398 |
$insert_term = wp_insert_term( |
| 399 |
$key, |
| 400 |
'betterdocs_faq_category' |
| 401 |
); |
| 402 |
if ( $insert_term ) { |
| 403 |
foreach ( $value['posts'] as $key => $value ) { |
| 404 |
$this->insert_betterdocs_faq( $value['post_title'], $value['post_content'], $insert_term['term_id'] ); |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
public function create_faq_category( $params ) { |
| 412 |
$title = $params->get_param( 'title' ); |
| 413 |
$description = $params->get_param( 'description' ); |
| 414 |
$description = ( $description !== 'undefined' ) ? $description : ''; |
| 415 |
$slug = $params->get_param( 'slug' ); |
| 416 |
$group_icon_url = $params->get_param('group_icon_url'); |
| 417 |
return $this->insert_betterdocs_faq_category( $title, $description, $group_icon_url, $slug ); |
| 418 |
} |
| 419 |
|
| 420 |
public function update_faq_category( $params ) { |
| 421 |
$term_id = $params->get_param( 'term_id' ); |
| 422 |
$title = $params->get_param( 'title' ); |
| 423 |
$description = $params->get_param( 'description' ); |
| 424 |
$group_icon_url = $params->get_param('group_icon_url'); |
| 425 |
$description = ( $description !== 'undefined' ) ? $description : ''; |
| 426 |
$slug = $params->get_param( 'slug' ); |
| 427 |
$update = wp_update_term( |
| 428 |
$term_id, |
| 429 |
'betterdocs_faq_category', |
| 430 |
[ |
| 431 |
'name' => $title, |
| 432 |
'slug' => $slug, |
| 433 |
'description' => $description |
| 434 |
] |
| 435 |
); |
| 436 |
|
| 437 |
if ( is_wp_error( $update ) ) { |
| 438 |
return $update; |
| 439 |
} else { |
| 440 |
$term_id = isset( $update['term_id'] ) ? $update['term_id'] : 0; |
| 441 |
if( $term_id != 0 ) { |
| 442 |
$previous_icon_url = get_term_meta($term_id, 'faq_group_icon', true); |
| 443 |
update_term_meta($term_id, 'faq_group_icon', $group_icon_url, $previous_icon_url); |
| 444 |
} |
| 445 |
return true; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
public function delete_faq_category( $params ) { |
| 450 |
$term_id = $params->get_param( 'term_id' ); |
| 451 |
$delete_all_docs = $params->get_param('with_all_post'); |
| 452 |
|
| 453 |
if ( $delete_all_docs ) { |
| 454 |
Helper::delete_specific_faq_posts_by_faq_category( $term_id ); |
| 455 |
} |
| 456 |
|
| 457 |
$delete = wp_delete_term( $term_id, 'betterdocs_faq_category' ); |
| 458 |
|
| 459 |
if ( is_wp_error( $delete ) ) { |
| 460 |
return $delete; |
| 461 |
} else { |
| 462 |
return true; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
public function insert_betterdocs_faq_category( $title, $description, $icon_url, $slug = '' ) { |
| 467 |
$insert_term = wp_insert_term( |
| 468 |
$title, |
| 469 |
'betterdocs_faq_category', |
| 470 |
[ |
| 471 |
'slug' => $slug, |
| 472 |
'description' => $description |
| 473 |
] |
| 474 |
); |
| 475 |
|
| 476 |
if ( is_wp_error( $insert_term ) ) { |
| 477 |
return $insert_term; |
| 478 |
} else { |
| 479 |
$term_id = isset( $insert_term['term_id'] ) ? $insert_term['term_id'] : 0; |
| 480 |
if( $term_id != 0 ) { |
| 481 |
update_term_meta($term_id, 'faq_group_icon', $icon_url); |
| 482 |
} |
| 483 |
return true; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
public function update_faq_category_order( $params ) { |
| 488 |
$faq_category_order = $params->get_param( 'faq_category_order' ); |
| 489 |
$faq_category_order = json_decode( $faq_category_order, true ); |
| 490 |
|
| 491 |
foreach ( $faq_category_order as $order_data ) { |
| 492 |
if ( (int) $order_data['current_position'] != (int) $order_data['updated_position'] ) { |
| 493 |
update_term_meta( $order_data['id'], 'order', ( (int) $order_data['updated_position'] ) ); |
| 494 |
} |
| 495 |
} |
| 496 |
return true; |
| 497 |
} |
| 498 |
|
| 499 |
public function insert_betterdocs_faq( $post_title, $post_content, $term_id ) { |
| 500 |
$post = wp_insert_post( |
| 501 |
[ |
| 502 |
'post_type' => 'betterdocs_faq', |
| 503 |
'post_title' => wp_strip_all_tags( $post_title ), |
| 504 |
'post_content' => $post_content, |
| 505 |
'post_status' => 'publish' |
| 506 |
] |
| 507 |
); |
| 508 |
|
| 509 |
if ( $term_id ) { |
| 510 |
$set_terms = wp_set_object_terms( $post, $term_id, 'betterdocs_faq_category' ); |
| 511 |
if ( is_wp_error( $set_terms ) ) { |
| 512 |
return $set_terms; |
| 513 |
} else { |
| 514 |
return $this->update_faq_order_on_insert( $term_id, $post ); |
| 515 |
} |
| 516 |
} else { |
| 517 |
return $post; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
public function update_faq_order_on_insert( $term_id, $post ) { |
| 522 |
$term_meta = get_term_meta( $term_id, '_betterdocs_faq_order' ); |
| 523 |
if ( ! empty( $term_meta ) ) { |
| 524 |
$term_meta_arr = explode( ',', $term_meta[0] ); |
| 525 |
if ( ! in_array( $post, $term_meta_arr ) ) { |
| 526 |
array_unshift( $term_meta_arr, $post ); |
| 527 |
$docs_ordering_data = filter_var_array( wp_unslash( $term_meta_arr ), FILTER_SANITIZE_NUMBER_INT ); |
| 528 |
return update_term_meta( $term_id, '_betterdocs_faq_order', implode( ',', $docs_ordering_data ) ); |
| 529 |
} |
| 530 |
} else { |
| 531 |
return update_term_meta( $term_id, '_betterdocs_faq_order', $post ); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
/** |
| 536 |
* Update _betterdocs_faq_order meta when new post created |
| 537 |
*/ |
| 538 |
|
| 539 |
public function update_faq_order_by_category( $params ) { |
| 540 |
$term_id = $params->get_param( 'term_id' ); |
| 541 |
$posts = $params->get_param( 'posts' ); |
| 542 |
return update_term_meta( $term_id, '_betterdocs_faq_order', $posts ); |
| 543 |
} |
| 544 |
|
| 545 |
public function create_betterdocs_faq( $params ) { |
| 546 |
$post_title = $params->get_param( 'post_title' ); |
| 547 |
$post_content = $params->get_param( 'post_content' ); |
| 548 |
$term_id = $params->get_param( 'term_id' ); |
| 549 |
return $this->insert_betterdocs_faq( $post_title, $post_content, $term_id ); |
| 550 |
} |
| 551 |
|
| 552 |
public function update_betterdocs_faq( $params ) { |
| 553 |
$post_id = $params->get_param( 'post_id' ); |
| 554 |
$post_title = $params->get_param( 'post_title' ); |
| 555 |
$post_content = $params->get_param( 'post_content' ); |
| 556 |
$status = $params->get_param( 'status' ); |
| 557 |
$term_id = $params->get_param( 'term_id' ); |
| 558 |
if ( $status ) { |
| 559 |
$data = [ |
| 560 |
'post_type' => 'betterdocs_faq', |
| 561 |
'ID' => $post_id, |
| 562 |
'status' => $status |
| 563 |
]; |
| 564 |
} else { |
| 565 |
$data = [ |
| 566 |
'post_type' => 'betterdocs_faq', |
| 567 |
'ID' => $post_id, |
| 568 |
'post_title' => $post_title, |
| 569 |
'post_content' => $post_content |
| 570 |
]; |
| 571 |
|
| 572 |
if ( $term_id ) { |
| 573 |
$data['tax_input'] = [ |
| 574 |
'betterdocs_faq_category' => $term_id |
| 575 |
]; |
| 576 |
|
| 577 |
$term_meta = get_term_meta( $term_id, '_betterdocs_faq_order' ); |
| 578 |
$term_meta_arr = explode( ',', $term_meta[0] ); |
| 579 |
if ( ! in_array( $post_id, $term_meta_arr ) ) { |
| 580 |
array_unshift( $term_meta_arr, $post_id ); |
| 581 |
$docs_ordering_data = filter_var_array( wp_unslash( $term_meta_arr ), FILTER_SANITIZE_NUMBER_INT ); |
| 582 |
update_term_meta( $term_id, '_betterdocs_faq_order', implode( ',', $docs_ordering_data ) ); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
return wp_update_post( $data ); |
| 588 |
} |
| 589 |
|
| 590 |
public function delete_betterdocs_faq( $params ) { |
| 591 |
$post_id = $params->get_param( 'post_id' ); |
| 592 |
return wp_delete_post( $post_id ); |
| 593 |
} |
| 594 |
|
| 595 |
public function faq_post_loop( $args ) { |
| 596 |
$posts = []; |
| 597 |
$query = new WP_Query( $args ); |
| 598 |
if ( $query->have_posts() ) : |
| 599 |
while ( $query->have_posts() ) : |
| 600 |
$query->the_post(); |
| 601 |
$posts[ get_the_ID() ]['title'] = get_the_title(); |
| 602 |
$posts[ get_the_ID() ]['content'] = get_the_content(); |
| 603 |
endwhile; |
| 604 |
endif; |
| 605 |
|
| 606 |
return $posts; |
| 607 |
} |
| 608 |
|
| 609 |
public function update_category_status( $params ) { |
| 610 |
$term_id = $params->get_param( 'term_id' ); |
| 611 |
$status = $params->get_param( 'status' ); |
| 612 |
return update_term_meta( $term_id, 'status', $status ); |
| 613 |
} |
| 614 |
|
| 615 |
public function fetch_faq_posts( $params ) { |
| 616 |
$faq = []; |
| 617 |
$type = $params->get_param( 'type' ); |
| 618 |
|
| 619 |
if ( $type == 'category' ) { |
| 620 |
$taxonomy_objects = get_terms( |
| 621 |
[ |
| 622 |
'taxonomy' => 'betterdocs_faq_category', |
| 623 |
'hide_empty' => false |
| 624 |
] |
| 625 |
); |
| 626 |
|
| 627 |
if ( $taxonomy_objects && ! is_wp_error( $taxonomy_objects ) ) : |
| 628 |
foreach ( $taxonomy_objects as $term ) : |
| 629 |
$args = [ |
| 630 |
'post_type' => 'betterdocs_faq', |
| 631 |
'post_status' => 'publish', |
| 632 |
'post_per_page' => -1, |
| 633 |
'tax_query' => [ |
| 634 |
[ |
| 635 |
'taxonomy' => 'betterdocs_faq_category', |
| 636 |
'field' => 'term_id', |
| 637 |
'terms' => $term->term_id |
| 638 |
] |
| 639 |
] |
| 640 |
]; |
| 641 |
|
| 642 |
$posts = $this->faq_post_loop( $args ); |
| 643 |
|
| 644 |
$faq[ $term->slug ] = [ |
| 645 |
(array) $term, |
| 646 |
'posts' => $posts |
| 647 |
]; |
| 648 |
endforeach; |
| 649 |
endif; |
| 650 |
} else { |
| 651 |
$args = [ |
| 652 |
'post_type' => 'betterdocs_faq', |
| 653 |
'post_status' => 'publish', |
| 654 |
'post_per_page' => -1 |
| 655 |
]; |
| 656 |
$posts = $this->faq_post_loop( $args ); |
| 657 |
$faq['posts'] = $posts; |
| 658 |
} |
| 659 |
|
| 660 |
return $faq; |
| 661 |
} |
| 662 |
|
| 663 |
public function get_uncategorised_faq() { |
| 664 |
$available_terms = array_map( |
| 665 |
function ( $term ) { |
| 666 |
return $term->term_id; |
| 667 |
}, |
| 668 |
get_terms( |
| 669 |
[ |
| 670 |
'taxonomy' => 'betterdocs_faq_category', |
| 671 |
'hide_empty' => false |
| 672 |
] |
| 673 |
) |
| 674 |
); |
| 675 |
|
| 676 |
return get_posts( |
| 677 |
[ |
| 678 |
'post_type' => 'betterdocs_faq', |
| 679 |
'post_status' => current_user_can( 'edit_others_posts' ) ? [ 'publish', 'draft' ] : 'publish', |
| 680 |
'posts_per_page' => -1, |
| 681 |
'tax_query' => [ |
| 682 |
'relation' => 'AND', |
| 683 |
[ |
| 684 |
'taxonomy' => 'betterdocs_faq_category', |
| 685 |
'field' => 'term_id', |
| 686 |
'terms' => $available_terms, |
| 687 |
'operator' => 'NOT IN' |
| 688 |
] |
| 689 |
] |
| 690 |
] |
| 691 |
); |
| 692 |
} |
| 693 |
|
| 694 |
public function category_search( $request ) { |
| 695 |
|
| 696 |
$title = $request['title']; |
| 697 |
|
| 698 |
// Perform the taxonomy search |
| 699 |
$taxonomy_args = [ |
| 700 |
'name__like' => $title, |
| 701 |
'taxonomy' => 'betterdocs_faq_category', |
| 702 |
'hide_empty' => false |
| 703 |
]; |
| 704 |
|
| 705 |
$taxonomies = get_terms( $taxonomy_args ); |
| 706 |
|
| 707 |
if ( ! empty( $taxonomies ) ) { |
| 708 |
$result = []; |
| 709 |
foreach ( $taxonomies as $taxonomy ) { |
| 710 |
$result[] = [ |
| 711 |
'id' => $taxonomy->term_id, |
| 712 |
'count' => $taxonomy->count, |
| 713 |
'description' => $taxonomy->description, |
| 714 |
'name' => $taxonomy->name, |
| 715 |
'slug' => $taxonomy->slug |
| 716 |
// Add more fields as needed |
| 717 |
]; |
| 718 |
} |
| 719 |
// Return the taxonomy data |
| 720 |
return $result; |
| 721 |
} else { |
| 722 |
// Taxonomy not found |
| 723 |
return new WP_Error( 'taxonomy_not_found', 'Taxonomy not found.', [ 'status' => 404 ] ); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
public function faq_category_orderby_meta( $args, $request ) { |
| 728 |
if ( $args['taxonomy'] === 'betterdocs_faq_category' ) { |
| 729 |
$args['orderby'] = 'meta_value_num'; |
| 730 |
$args['meta_key'] = 'order'; |
| 731 |
} |
| 732 |
return $args; |
| 733 |
} |
| 734 |
} |
| 735 |
|