| 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 CategoryGrid extends Block { |
| 9 |
protected $editor_scripts = [ |
| 10 |
'betterdocs-blocks-editor', |
| 11 |
'betterdocs-categorygrid' |
| 12 |
]; |
| 13 |
|
| 14 |
protected $editor_styles = [ |
| 15 |
'betterdocs-blocks-editor', |
| 16 |
'betterdocs-fontawesome', |
| 17 |
'betterdocs-blocks-category-grid' |
| 18 |
]; |
| 19 |
|
| 20 |
protected $frontend_scripts = [ |
| 21 |
'betterdocs-category-grid' |
| 22 |
]; |
| 23 |
protected $frontend_styles = [ |
| 24 |
'betterdocs-fontawesome', |
| 25 |
'betterdocs-blocks-category-grid' |
| 26 |
]; |
| 27 |
|
| 28 |
/** |
| 29 |
* unique name of block |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
public function get_name() { |
| 33 |
return 'categorygrid'; |
| 34 |
} |
| 35 |
|
| 36 |
public function register_scripts() { |
| 37 |
//when thrive editor mode is active, do not enqueue this script, this causes an issue with thrive edit mode |
| 38 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only editor detection. |
| 39 |
$check_for_thrive = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; |
| 40 |
if ( $check_for_thrive === 'architect' ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$this->assets_manager->enqueue( |
| 45 |
'betterdocs-categorygrid', |
| 46 |
'blocks/categorygrid/frontend.js', |
| 47 |
[ 'masonry' ] |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
public function get_default_attributes() { |
| 52 |
return [ |
| 53 |
'blockId' => '', |
| 54 |
'categories' => [], |
| 55 |
'includeCategories' => '', |
| 56 |
'excludeCategories' => '', |
| 57 |
'gridPerPage' => 9, |
| 58 |
'subCategoryPerGrid' => 2, |
| 59 |
'orderBy' => 'doc_category_order', |
| 60 |
'order' => 'asc', |
| 61 |
'layout' => 'default', |
| 62 |
'showTitle' => true, |
| 63 |
'titleTag' => 'h2', |
| 64 |
'showCount' => true, |
| 65 |
'showIcon' => true, |
| 66 |
'showList' => true, |
| 67 |
'layoutMode' => 'grid', |
| 68 |
'showHeader' => true, |
| 69 |
'showButton' => true, |
| 70 |
'listIcon' => 'far fa-file-alt', |
| 71 |
'postsPerPage' => 5, |
| 72 |
'postsOrderBy' => 'betterdocs_order', |
| 73 |
'postsOrder' => 'asc', |
| 74 |
'enableNestedSubcategory' => false, |
| 75 |
'enableLazyLoad' => false, |
| 76 |
'postPerSubcategory' => 3, |
| 77 |
'buttonIconPosition' => 'after', |
| 78 |
'buttonIcon' => 'fas fa-angle-right', |
| 79 |
'buttonPosition' => 'after', |
| 80 |
'showButtonIcon' => true, |
| 81 |
'colRange' => 3, |
| 82 |
'TABcolRange' => 2, |
| 83 |
'MOBcolRange' => 1, |
| 84 |
'gridSpaceRange' => 10, |
| 85 |
'TABgridSpaceRange' => 10, |
| 86 |
'MOBgridSpaceRange' => 10, |
| 87 |
'buttonText' => __( 'Explore More', 'betterdocs' ), |
| 88 |
'categoryTitleLink' => false, |
| 89 |
'selectKB' => '', |
| 90 |
'listIconImageUrl' => '', |
| 91 |
'prefix' => '', |
| 92 |
'suffix' => '', |
| 93 |
'suffixSingular' => '' |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
public function render( $attributes, $content ) { |
| 98 |
$this->views( 'layouts/base' ); |
| 99 |
} |
| 100 |
|
| 101 |
public function view_params() { |
| 102 |
$attributes = &$this->attributes; |
| 103 |
|
| 104 |
// Sanitize the layout attribute to prevent path traversal (LFI). |
| 105 |
$attributes['layout'] = sanitize_file_name( $attributes['layout'] ); |
| 106 |
|
| 107 |
$terms_object = [ |
| 108 |
'taxonomy' => 'doc_category', |
| 109 |
'order' => $attributes['order'], |
| 110 |
'orderby' => $attributes['orderBy'], |
| 111 |
'number' => isset( $attributes['gridPerPage'] ) ? $attributes['gridPerPage'] : 5, |
| 112 |
'hide_empty' => true |
| 113 |
]; |
| 114 |
|
| 115 |
if ( 'doc_category_order' === $attributes['orderBy'] ) { |
| 116 |
// Use betterdocs_order which handles fallback logic automatically |
| 117 |
$terms_object['orderby'] = 'betterdocs_order'; |
| 118 |
} |
| 119 |
|
| 120 |
if ( $attributes['enableNestedSubcategory'] ) { |
| 121 |
$terms_object['parent'] = 0; |
| 122 |
} |
| 123 |
|
| 124 |
$includes = $this->string_to_array( $attributes['includeCategories'] ); |
| 125 |
$excludes = $this->string_to_array( $attributes['excludeCategories'] ); |
| 126 |
|
| 127 |
if ( ! empty( $includes ) ) { |
| 128 |
$terms_object['include'] = array_diff( $includes, (array) $excludes ); |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! empty( $excludes ) ) { |
| 132 |
$terms_object['exclude'] = $excludes; |
| 133 |
} |
| 134 |
|
| 135 |
$_wrapper_classes = [ |
| 136 |
'betterdocs-category-grid-wrapper', |
| 137 |
'betterdocs-blocks-grid' |
| 138 |
]; |
| 139 |
|
| 140 |
$layout_class = ( $attributes['layout'] === 'default' ) ? 'layout-1' : $attributes['layout']; |
| 141 |
|
| 142 |
$_inner_wrapper_classes = [ |
| 143 |
'betterdocs-category-grid-inner-wrapper', |
| 144 |
'layout-flex', |
| 145 |
$layout_class, |
| 146 |
$attributes['layoutMode'], |
| 147 |
'betterdocs-column-' . $attributes['colRange'], |
| 148 |
'betterdocs-column-tablet-' . $attributes['TABcolRange'], |
| 149 |
'betterdocs-column-mobile-' . $attributes['MOBcolRange'] |
| 150 |
]; |
| 151 |
|
| 152 |
$wrapper_attr = [ |
| 153 |
'class' => $_wrapper_classes |
| 154 |
]; |
| 155 |
|
| 156 |
$inner_wrapper_attr = [ |
| 157 |
'class' => $_inner_wrapper_classes, |
| 158 |
'data-column_desktop' => $attributes['colRange'], |
| 159 |
'data-column_tab' => $attributes['TABcolRange'], |
| 160 |
'data-column_mobile' => $attributes['MOBcolRange'], |
| 161 |
'data-column_space_desktop' => $attributes['gridSpaceRange'], |
| 162 |
'data-colomn_space_tab' => $attributes['TABgridSpaceRange'], |
| 163 |
'data-colomn_space_mobile' => $attributes['MOBgridSpaceRange'] |
| 164 |
]; |
| 165 |
|
| 166 |
$docs_query = [ |
| 167 |
'orderby' => $attributes['postsOrderBy'], |
| 168 |
'order' => $attributes['postsOrder'], |
| 169 |
'posts_per_page' => $attributes['postsPerPage'], |
| 170 |
'nested_subcategory' => $attributes['enableNestedSubcategory'] |
| 171 |
]; |
| 172 |
|
| 173 |
$default_multiple_kb = betterdocs()->settings->get( 'multiple_kb' ); |
| 174 |
|
| 175 |
$select_kb = isset( $attributes['selectKB'] ) ? $attributes['selectKB'] : ''; |
| 176 |
if ( is_string( $select_kb ) && $select_kb !== '' ) { |
| 177 |
$decoded = json_decode( $select_kb, true ); |
| 178 |
$select_kb = is_array( $decoded ) ? $decoded : []; |
| 179 |
} |
| 180 |
$kb_slug = is_array( $select_kb ) && isset( $select_kb['value'] ) ? $select_kb['value'] : ''; |
| 181 |
|
| 182 |
if ( is_tax( 'knowledge_base' ) && $default_multiple_kb == 1 ) { |
| 183 |
$object = get_queried_object(); |
| 184 |
$kb_slug = $object->slug; |
| 185 |
} |
| 186 |
|
| 187 |
if ( ! empty( $kb_slug ) && $default_multiple_kb == 1 ) { |
| 188 |
$terms_object['meta_query'] = [ |
| 189 |
'relation' => 'OR', |
| 190 |
[ |
| 191 |
'key' => 'doc_category_knowledge_base', |
| 192 |
'value' => $kb_slug, |
| 193 |
'compare' => 'LIKE' |
| 194 |
] |
| 195 |
]; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Add This Attribute When Using Outside Betterdocs Templates Only |
| 200 |
*/ |
| 201 |
if ( $default_multiple_kb == 1 && ( ! empty( $kb_slug ) ) && ( ! betterdocs()->helper->is_templates() ) ) { |
| 202 |
$inner_wrapper_attr['data-mkb-slug'] = $kb_slug; |
| 203 |
} |
| 204 |
|
| 205 |
return [ |
| 206 |
'wrapper_attr' => $wrapper_attr, |
| 207 |
'inner_wrapper_attr' => $inner_wrapper_attr, |
| 208 |
'terms_query_args' => betterdocs()->query->terms_query( $terms_object ), |
| 209 |
'docs_query_args' => $docs_query, |
| 210 |
'widget_type' => 'category-grid', |
| 211 |
'multiple_knowledge_base' => ( $default_multiple_kb && ! empty( $kb_slug ) ) ? true : false, |
| 212 |
'kb_slug' => $kb_slug, |
| 213 |
'nested_terms_query' => [ |
| 214 |
'number' => $attributes['subCategoryPerGrid'] |
| 215 |
], |
| 216 |
'list_icon_url' => '', // not needed for blocks, provided the prop for formality |
| 217 |
'layout_type' => 'block', |
| 218 |
'nested_docs_query_args' => [ |
| 219 |
'orderby' => $attributes['postsOrderBy'], |
| 220 |
'order' => $attributes['postsOrder'], |
| 221 |
'posts_per_page' => $attributes['postPerSubcategory'] |
| 222 |
], |
| 223 |
'list_icon_name' => ! empty( $attributes['listIconImageUrl'] ) ? [ 'value' => [ 'url' => str_replace( 'blob:', '', $attributes['listIconImageUrl'] ) ] ] : ( ! empty( $attributes['listIcon'] ) ? [ 'value' => [ 'url' => $attributes['listIcon'] ] ] : ( ! empty( betterdocs()->settings->get( 'docs_list_icon' ) ) ? [ 'value' => [ 'url' => betterdocs()->settings->get( 'docs_list_icon' )['url'] ] ] : [] ) ), |
| 224 |
'button_icon' => [ |
| 225 |
'value' => $attributes['buttonIcon'] |
| 226 |
], |
| 227 |
'category_title_link' => $attributes['categoryTitleLink'], |
| 228 |
'count_prefix' => $attributes['prefix'], |
| 229 |
'count_suffix' => $attributes['suffix'], |
| 230 |
'count_suffix_singular' => $attributes['suffixSingular'], |
| 231 |
'title_tag' => $attributes['titleTag'], |
| 232 |
'lazy_load' => ! empty( $attributes['enableLazyLoad'] ) |
| 233 |
]; |
| 234 |
} |
| 235 |
} |
| 236 |
|