PluginProbe
Knowledge Base documentation & wiki plugin – BasePress Docs / trunk
Knowledge Base documentation & wiki plugin – BasePress Docs vtrunk
basepress / blocks / gb-blocks.php

gb-blocks.php in Knowledge Base documentation & wiki plugin – BasePress Docs trunk, at blocks/gb-blocks.php

165 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Include all Gutenberg Blocks
5 */
6 // Exit if called directly.
7 if ( !defined( 'ABSPATH' ) ) {
8 exit;
9 }
10 //Exit if Gutenberg editor is not active
11 if ( !class_exists( 'WP_Block_Type_Registry' ) ) {
12 return;
13 }
14 if ( !class_exists( 'BasePress_Editor_Blocks' ) ) {
15 class BasePress_Editor_Blocks {
16 public function __construct() {
17 //Add Knowledge Base category for Gutenberg Blocks
18 if ( class_exists( 'WP_Block_Editor_Context' ) ) {
19 add_filter(
20 'block_categories_all',
21 array($this, 'add_block_categories'),
22 10,
23 2
24 );
25 } else {
26 add_filter(
27 'block_categories',
28 array($this, 'add_block_categories'),
29 10,
30 2
31 );
32 }
33 add_action( 'rest_api_init', array($this, 'register_rest_routes') );
34 //Include the Products block
35 require_once 'gutenberg-products-block.php';
36 require_once 'gutenberg-searchbar-block.php';
37 }
38
39 /**
40 * @param $categories
41 * @param $post
42 * @return array
43 */
44 public function add_block_categories( $categories, $post ) {
45 return array_merge( $categories, array(array(
46 'slug' => 'basepress-kb-block-cat',
47 'title' => __( 'Knowledge Base', 'basepress' ),
48 'icon' => '',
49 )) );
50 }
51
52 /**
53 * Register API end point to fetch KB Categories
54 *
55 * @since 2.1.0
56 */
57 public function register_rest_routes() {
58 register_rest_route( 'basepress_kb/v1', '/kb_categories/', array(
59 'methods' => WP_REST_Server::READABLE,
60 'callback' => array($this, 'get_kb_categories'),
61 'permission_callback' => function ( WP_REST_Request $request ) {
62 return current_user_can( 'edit_posts' );
63 },
64 ) );
65 register_rest_route( 'basepress_kb/v1', '/kb_css_url', array(
66 'methods' => WP_REST_Server::READABLE,
67 'callback' => array($this, 'get_css_url'),
68 'permission_callback' => function ( WP_REST_Request $request ) {
69 return current_user_can( 'edit_posts' );
70 },
71 ) );
72 }
73
74 /**
75 * API endpoint function to get the list of products and sections for the block settings
76 *
77 * @since 2.1.0
78 */
79 public function get_kb_categories( $request ) {
80 $include_sections = ( isset( $_REQUEST['products'] ) && 'true' === $_REQUEST['products'] ? false : true );
81 $response = array();
82 $products = array(array(
83 'value' => 0,
84 'label' => __( 'Select KB', 'basepress' ),
85 ));
86 //Get all products terms in knowledgebase_cat
87 $args = array(
88 'taxonomy' => 'knowledgebase_cat',
89 'hide_empty' => true,
90 'parent' => 0,
91 'meta_key' => 'basepress_position',
92 'orderby' => 'meta_value_num',
93 'order' => 'ASC',
94 );
95 $product_terms = get_terms( $args );
96 foreach ( $product_terms as $product ) {
97 $products[] = array(
98 'value' => $product->term_id,
99 'label' => $product->name,
100 );
101 }
102 $response['products'] = $products;
103 //Sections data
104 if ( $include_sections ) {
105 $sections = array(
106 0 => array(array(
107 'value' => 0,
108 'label' => __( 'Select Section', 'basepress' ),
109 )),
110 );
111 foreach ( $product_terms as $product ) {
112 $sections[$product->term_id] = array(array(
113 'value' => 0,
114 'label' => __( 'Select Section', 'basepress' ),
115 ));
116 //Get all sections terms in knowledgebase_cat
117 $args = array(
118 'taxonomy' => 'knowledgebase_cat',
119 'hide_empty' => false,
120 'parent' => $product->term_id,
121 'meta_query' => array(array(
122 'relation' => 'OR',
123 array(
124 'key' => 'basepress_position',
125 ),
126 array(
127 'key' => 'basepress_position',
128 'compare' => 'NOT EXISTS',
129 ),
130 )),
131 'orderby' => 'meta_value_num',
132 'order' => 'ASC',
133 );
134 $sections_terms = get_terms( $args );
135 foreach ( $sections_terms as $section ) {
136 $sections[$product->term_id][] = array(
137 'value' => $section->term_id,
138 'label' => $section->name,
139 );
140 $sub_sections = get_term_children( $section->term_id, 'knowledgebase_cat' );
141 foreach ( $sub_sections as $sub_section ) {
142 $term = get_term( $sub_section, 'knowledgebase_cat' );
143 $sections[$product->term_id][] = array(
144 'value' => $term->term_id,
145 'label' => '- ' . $term->name,
146 );
147 }
148 }
149 }
150 $response['sections'] = $sections;
151 }
152 return $response;
153 }
154
155 public function get_css_url() {
156 global $basepress_utils;
157 $stylesheet = apply_filters( 'basepress_theme_style', 'style.css' );
158 $theme_css = $basepress_utils->get_theme_file_uri( 'css/' . $stylesheet );
159 return $theme_css;
160 }
161
162 }
163
164 new BasePress_Editor_Blocks();
165 }