| 1 |
<?php |
| 2 |
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query,WordPress.DB.SlowDBQuery.slow_db_query_meta_key,WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- core block query; meta/tax filtering required. |
| 3 |
// phpcs:disable WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- block exposes user-driven exclusion controls. |
| 4 |
namespace WPDeveloper\BetterDocs\Editors\BlockEditor\Blocks; |
| 5 |
|
| 6 |
use WPDeveloper\BetterDocs\Editors\BlockEditor\Block; |
| 7 |
|
| 8 |
class CategorySlateLayout extends Block { |
| 9 |
|
| 10 |
public $view_wrapper = 'betterdocs-category-slate-layout-block'; |
| 11 |
|
| 12 |
protected $editor_styles = [ |
| 13 |
'betterdocs-blocks-category-slate-layout' |
| 14 |
]; |
| 15 |
|
| 16 |
protected $frontend_styles = [ |
| 17 |
'betterdocs-blocks-category-slate-layout', |
| 18 |
'betterdocs-category-grid', |
| 19 |
'betterdocs-fontawesome' |
| 20 |
]; |
| 21 |
/** |
| 22 |
* Name of the block. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $name = 'category-slate-layout'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Unique ID for the block. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $unique_id = 'betterdocs_category_slate_layout'; |
| 34 |
|
| 35 |
public function get_name() { |
| 36 |
return $this->name; |
| 37 |
} |
| 38 |
|
| 39 |
public function get_unique_id() { |
| 40 |
return $this->unique_id; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Check if the block can be enabled. |
| 45 |
* |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
public function can_enable() { |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Register scripts and styles for the block. |
| 54 |
*/ |
| 55 |
public function register_scripts() { |
| 56 |
// The styles are already registered in Scripts.php blocks() method |
| 57 |
// This method is called to ensure the block is properly initialized |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get default attributes for the block. |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function get_default_attributes() { |
| 66 |
return [ |
| 67 |
'blockId' => '', |
| 68 |
'categories' => [], |
| 69 |
'includeCategories' => '', |
| 70 |
'excludeCategories' => '', |
| 71 |
'gridPerPage' => 9, |
| 72 |
'orderBy' => 'doc_category_order', |
| 73 |
'order' => 'asc', |
| 74 |
'layout' => 'layout-3', |
| 75 |
'showTitle' => true, |
| 76 |
'titleTag' => 'h2', |
| 77 |
'showIcon' => true, |
| 78 |
'categoryIcon' => 'folder', |
| 79 |
'categoryTitleLink' => true, |
| 80 |
'gridColumns' => 4, |
| 81 |
'gridSpace' => 48, |
| 82 |
'selectKB' => '', |
| 83 |
'postsPerPage' => 5, |
| 84 |
'postsOrderBy' => 'betterdocs_order', |
| 85 |
'postsOrder' => 'asc', |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Render method required by Block class. |
| 91 |
* |
| 92 |
* @param array $attributes Block attributes. |
| 93 |
* @param string $content Block content. |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function render( $attributes, $content ) { |
| 97 |
$attributes = &$this->attributes; |
| 98 |
echo '<div class="betterdocs-blocks betterdocs-category-layout-8 ' . esc_attr( $attributes['blockId'] ) . '">'; |
| 99 |
$this->views( 'layouts/base' ); |
| 100 |
echo '</div>'; |
| 101 |
} |
| 102 |
|
| 103 |
public function view_params() { |
| 104 |
$attributes = &$this->attributes; |
| 105 |
|
| 106 |
// Sanitize the layout attribute to prevent path traversal (LFI). |
| 107 |
$attributes['layout'] = sanitize_file_name( $attributes['layout'] ); |
| 108 |
|
| 109 |
$terms_object = [ |
| 110 |
'taxonomy' => 'doc_category', |
| 111 |
'order' => $attributes['order'], |
| 112 |
'orderby' => $attributes['orderBy'], |
| 113 |
'number' => isset( $attributes['gridPerPage'] ) ? $attributes['gridPerPage'] : 9, |
| 114 |
'hide_empty' => true // Hide empty categories on frontend |
| 115 |
]; |
| 116 |
|
| 117 |
if ( 'doc_category_order' === $attributes['orderBy'] ) { |
| 118 |
$terms_object['meta_key'] = 'doc_category_order'; |
| 119 |
$terms_object['orderby'] = 'meta_value_num'; |
| 120 |
} |
| 121 |
|
| 122 |
$includes = $this->string_to_array( $attributes['includeCategories'] ); |
| 123 |
$excludes = $this->string_to_array( $attributes['excludeCategories'] ); |
| 124 |
|
| 125 |
if ( ! empty( $includes ) ) { |
| 126 |
$terms_object['include'] = array_diff( $includes, (array) $excludes ); |
| 127 |
} |
| 128 |
|
| 129 |
if ( ! empty( $excludes ) ) { |
| 130 |
$terms_object['exclude'] = $excludes; |
| 131 |
} |
| 132 |
|
| 133 |
$_wrapper_classes = [ |
| 134 |
'betterdocs-category-grid-three-wrapper', |
| 135 |
'betterdocs-blocks-grid' |
| 136 |
]; |
| 137 |
|
| 138 |
$layout_class = ( $attributes['layout'] === 'default' ) ? 'layout-1' : $attributes['layout']; |
| 139 |
|
| 140 |
$_inner_wrapper_classes = [ |
| 141 |
'betterdocs-category-grid-inner-wrapper', |
| 142 |
'layout-flex', |
| 143 |
'docs-col-4', |
| 144 |
$layout_class, |
| 145 |
'betterdocs-column-' . ($attributes['gridColumnsRange'] ?? 3), |
| 146 |
]; |
| 147 |
|
| 148 |
$wrapper_attr = [ |
| 149 |
'class' => $_wrapper_classes |
| 150 |
]; |
| 151 |
|
| 152 |
$inner_wrapper_attr = [ |
| 153 |
'class' => $_inner_wrapper_classes, |
| 154 |
'data-column_desktop' => $attributes['gridColumnsRange'] ?? 3, |
| 155 |
'data-column_space_desktop' => $attributes['gridSpaceRange'] ?? 48, |
| 156 |
]; |
| 157 |
|
| 158 |
$docs_query = [ |
| 159 |
'orderby' => isset( $attributes['postsOrderBy'] ) ? $attributes['postsOrderBy'] : 'betterdocs_order', |
| 160 |
'order' => isset( $attributes['postsOrder'] ) ? $attributes['postsOrder'] : 'asc', |
| 161 |
'posts_per_page' => isset( $attributes['postsPerPage'] ) ? $attributes['postsPerPage'] : 5, |
| 162 |
]; |
| 163 |
|
| 164 |
$default_multiple_kb = betterdocs()->settings->get( 'multiple_kb' ); |
| 165 |
|
| 166 |
$select_kb = isset( $attributes['selectKB'] ) ? $attributes['selectKB'] : ''; |
| 167 |
if ( is_string( $select_kb ) && $select_kb !== '' ) { |
| 168 |
$decoded = json_decode( $select_kb, true ); |
| 169 |
$select_kb = is_array( $decoded ) ? $decoded : []; |
| 170 |
} |
| 171 |
$kb_slug = is_array( $select_kb ) && isset( $select_kb['value'] ) ? $select_kb['value'] : ''; |
| 172 |
|
| 173 |
if ( is_tax( 'knowledge_base' ) && $default_multiple_kb == 1 ) { |
| 174 |
$object = get_queried_object(); |
| 175 |
$kb_slug = $object->slug; |
| 176 |
} |
| 177 |
|
| 178 |
if ( ! empty( $kb_slug ) && $default_multiple_kb == 1 ) { |
| 179 |
$terms_object['meta_query'] = [ |
| 180 |
'relation' => 'OR', |
| 181 |
[ |
| 182 |
'key' => 'doc_category_knowledge_base', |
| 183 |
'value' => $kb_slug, |
| 184 |
'compare' => 'LIKE' |
| 185 |
] |
| 186 |
]; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Add This Attribute When Using Outside Betterdocs Templates Only |
| 191 |
*/ |
| 192 |
if ( $default_multiple_kb == 1 && ( ! empty( $kb_slug ) ) && ( ! betterdocs()->helper->is_templates() ) ) { |
| 193 |
$inner_wrapper_attr['data-mkb-slug'] = $kb_slug; |
| 194 |
} |
| 195 |
|
| 196 |
return [ |
| 197 |
'wrapper_attr' => $wrapper_attr, |
| 198 |
'inner_wrapper_attr' => $inner_wrapper_attr, |
| 199 |
'terms_query_args' => betterdocs()->query->terms_query( $terms_object ), |
| 200 |
'docs_query_args' => $docs_query, |
| 201 |
'widget_type' => 'category-grid', |
| 202 |
'layout' => $attributes['layout'], |
| 203 |
'multiple_knowledge_base' => ( $default_multiple_kb && ! empty( $kb_slug ) ) ? true : false, |
| 204 |
'kb_slug' => $kb_slug, |
| 205 |
'layout_type' => 'block', |
| 206 |
'show_title' => $attributes['showTitle'], |
| 207 |
'title_tag' => $attributes['titleTag'], |
| 208 |
'show_icon' => $attributes['showIcon'], |
| 209 |
'category_icon' => $attributes['categoryIcon'], |
| 210 |
'category_title_link' => $attributes['categoryTitleLink'], |
| 211 |
'show_count' => false, |
| 212 |
'show_header' => true, |
| 213 |
'show_list' => true, |
| 214 |
]; |
| 215 |
} |
| 216 |
} |
| 217 |
|