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