| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Shortcodes; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Utils\Helper; |
| 6 |
use WPDeveloper\BetterDocs\Core\Shortcode; |
| 7 |
|
| 8 |
class CategoryGrid extends Shortcode { |
| 9 |
protected $layout_class = 'layout-1'; |
| 10 |
|
| 11 |
/** |
| 12 |
* A list of deprecated attributes. |
| 13 |
* @var array<string, string> |
| 14 |
*/ |
| 15 |
protected $deprecated_attributes = [ |
| 16 |
'category' => 'taxonomy', |
| 17 |
'posts_per_grid' => 'posts_per_page', |
| 18 |
'icon' => 'show_icon', |
| 19 |
'post_counter' => 'show_count' |
| 20 |
]; |
| 21 |
|
| 22 |
public function get_name() { |
| 23 |
return 'betterdocs_category_grid'; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_style_depends() { |
| 27 |
return [ 'betterdocs-category-grid' ]; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_script_depends() { |
| 31 |
return [ 'betterdocs-category-grid' ]; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Summary of default_attributes |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
public function default_attributes() { |
| 39 |
return [ |
| 40 |
'sidebar_list' => false, |
| 41 |
'taxonomy' => 'doc_category', |
| 42 |
'show_icon' => true, |
| 43 |
'category_icon' => '', |
| 44 |
'masonry' => false, |
| 45 |
'posts_per_page' => $this->settings->get( 'posts_number', 0 ), |
| 46 |
'orderby' => $this->settings->get( 'alphabetically_order_post', 'betterdocs_order' ), |
| 47 |
'order' => $this->settings->get( 'docs_order', 'ASC' ), |
| 48 |
'show_count' => $this->settings->get( 'post_count' ), |
| 49 |
'count_suffix' => '', |
| 50 |
'count_suffix_singular' => '', |
| 51 |
'column' => $this->settings->get( 'column_number' ), |
| 52 |
'nested_subcategory' => $this->settings->get( 'nested_subcategory' ), |
| 53 |
'terms' => '', |
| 54 |
'terms_orderby' => '', |
| 55 |
'terms_order' => '', |
| 56 |
'terms_include' => '', |
| 57 |
'terms_exclude' => '', |
| 58 |
'terms_offset' => '', |
| 59 |
'kb_slug' => '', |
| 60 |
'multiple_knowledge_base' => false, |
| 61 |
'disable_customizer_style' => false, |
| 62 |
'title_tag' => 'h2', |
| 63 |
'category_title_link' => false, |
| 64 |
'layout_type' => '', |
| 65 |
'list_icon_url' => '', |
| 66 |
'list_icon_name' => 'list', |
| 67 |
'show_list_icon' => true, |
| 68 |
'sidebar_layout' => '', |
| 69 |
'wrapper_class' => [] |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
public function generate_attributes() { |
| 74 |
$attributes = [ |
| 75 |
'class' => [ |
| 76 |
'betterdocs-category-grid-inner-wrapper', |
| 77 |
$this->layout_class |
| 78 |
] |
| 79 |
]; |
| 80 |
|
| 81 |
$masonry = (bool) $this->settings->get( 'masonry_layout', false ); |
| 82 |
if ( $this->has( 'masonry' ) ) { |
| 83 |
$masonry = (bool) $this->attributes['masonry']; |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! is_singular( 'docs' ) && ! is_tax( 'doc_category' ) && ! is_tax( 'doc_tag' ) ) { |
| 87 |
if ( $this->attributes['sidebar_list'] == true ) { |
| 88 |
$attributes['class'][] = 'layout-flex'; |
| 89 |
} elseif ( $masonry == true ) { |
| 90 |
wp_enqueue_script( 'masonry' ); |
| 91 |
$attributes['class'][] = 'masonry'; |
| 92 |
} else { |
| 93 |
$attributes['class'][] = 'layout-flex'; |
| 94 |
} |
| 95 |
if ( $this->attributes['sidebar_list'] == true ) { |
| 96 |
$_column_val = 1; |
| 97 |
} elseif ( $this->isset( 'column' ) ) { |
| 98 |
$_column_val = $this->attributes['column']; |
| 99 |
} else { |
| 100 |
$_column_val = $this->settings->get( 'column_number' ); |
| 101 |
} |
| 102 |
|
| 103 |
$attributes['class'][] = 'docs-col-' . $_column_val; |
| 104 |
$attributes['data-column_desktop'] = esc_html( $_column_val ); |
| 105 |
$attributes['style'] = "--column: $_column_val;"; |
| 106 |
|
| 107 |
if ( $this->isset( 'disable_customizer_style', false ) ) { |
| 108 |
$attributes['class'][] = 'single-kb'; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
return $attributes; |
| 114 |
} |
| 115 |
|
| 116 |
public function header_layout_sequence( $sequence, $layout, $widget_type, $args ) { |
| 117 |
return [ 'category_icon', 'category_title', 'category_counts', 'collapse_icon' ]; |
| 118 |
} |
| 119 |
|
| 120 |
public function render( $atts, $content = null ) { |
| 121 |
if ( (bool) $this->attributes['sidebar_list'] ) { |
| 122 |
add_filter( 'betterdocs_header_layout_sequence', [ $this, 'header_layout_sequence' ], 10, 4 ); |
| 123 |
} |
| 124 |
|
| 125 |
$this->views( 'layouts/base' ); |
| 126 |
|
| 127 |
if ( (bool) $this->attributes['sidebar_list'] ) { |
| 128 |
remove_filter( 'betterdocs_header_layout_sequence', [ $this, 'header_layout_sequence' ], 10 ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public function view_params() { |
| 133 |
$exploremore_btn = $this->settings->get( 'exploremore_btn' ); |
| 134 |
$button_text = $this->settings->get( 'exploremore_btn_txt' ); |
| 135 |
$category_title_link = isset( $this->attributes['category_title_link'] ) ? $this->attributes['category_title_link'] : ''; |
| 136 |
|
| 137 |
$show_button = false; |
| 138 |
if ( $this->attributes['posts_per_page'] == -1 ) { |
| 139 |
$show_button = false; |
| 140 |
} elseif ( $exploremore_btn && ! is_singular( 'docs' ) && Helper::get_tax() != 'doc_category' && ! is_tax( 'doc_tag' ) ) { |
| 141 |
$show_button = true; |
| 142 |
} |
| 143 |
|
| 144 |
$terms_query = $this->query->terms_query( |
| 145 |
[ |
| 146 |
'taxonomy' => $this->attributes['taxonomy'], |
| 147 |
'multiple_kb' => $this->attributes['multiple_knowledge_base'], |
| 148 |
'kb_slug' => $this->attributes['kb_slug'], |
| 149 |
'terms' => $this->attributes['terms'], |
| 150 |
'order' => $this->attributes['terms_order'], |
| 151 |
'orderby' => $this->attributes['terms_orderby'], |
| 152 |
'nested_subcategory' => $this->attributes['nested_subcategory'] |
| 153 |
] |
| 154 |
); |
| 155 |
|
| 156 |
if ( $this->attributes['terms_include'] ) { |
| 157 |
$terms_query['include'] = $this->attributes['terms_include']; |
| 158 |
} |
| 159 |
|
| 160 |
if ( $this->attributes['terms_exclude'] ) { |
| 161 |
$terms_query['exclude'] = $this->attributes['terms_exclude']; |
| 162 |
} |
| 163 |
|
| 164 |
if ( $this->attributes['terms_offset'] ) { |
| 165 |
$terms_query['offset'] = (int) $this->attributes['terms_offset']; |
| 166 |
} |
| 167 |
|
| 168 |
$inner_wrapper_attr = $this->generate_attributes(); |
| 169 |
|
| 170 |
$docs_query = [ |
| 171 |
'orderby' => $this->attributes['orderby'], |
| 172 |
'order' => $this->attributes['order'], |
| 173 |
'posts_per_page' => $this->attributes['posts_per_page'] |
| 174 |
]; |
| 175 |
|
| 176 |
/** |
| 177 |
* Add This Attribute When Using Outside Betterdocs Templates Only |
| 178 |
*/ |
| 179 |
if ( $this->attributes['multiple_knowledge_base'] && ( ! empty( $this->attributes['kb_slug'] ) ) && ( ! betterdocs()->helper->is_templates() ) ) { |
| 180 |
$inner_wrapper_attr['data-mkb-slug'] = $this->attributes['kb_slug']; |
| 181 |
} |
| 182 |
|
| 183 |
// Prepare wrapper attributes with custom classes |
| 184 |
$wrapper_attr_classes = [ 'betterdocs-category-grid-wrapper' ]; |
| 185 |
|
| 186 |
// Add custom wrapper classes if provided |
| 187 |
if ( $this->isset( 'wrapper_class' ) ) { |
| 188 |
$wrapper_classes = $this->attributes['wrapper_class']; |
| 189 |
|
| 190 |
// Handle both array and string formats |
| 191 |
if ( is_string( $wrapper_classes ) ) { |
| 192 |
// Split by comma or space and clean up |
| 193 |
$wrapper_classes = preg_split( '/[,\s]+/', $wrapper_classes ); |
| 194 |
$wrapper_classes = array_filter( array_map( 'trim', $wrapper_classes ) ); |
| 195 |
} elseif ( is_array( $wrapper_classes ) ) { |
| 196 |
$wrapper_classes = array_filter( $wrapper_classes ); |
| 197 |
} |
| 198 |
|
| 199 |
if ( ! empty( $wrapper_classes ) ) { |
| 200 |
$wrapper_attr_classes = array_merge( $wrapper_attr_classes, $wrapper_classes ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return [ |
| 205 |
'wrapper_attr' => [ 'class' => $wrapper_attr_classes ], |
| 206 |
'inner_wrapper_attr' => $inner_wrapper_attr, |
| 207 |
'layout' => 'default', |
| 208 |
'widget_type' => 'category-grid', |
| 209 |
'terms_query_args' => $terms_query, |
| 210 |
'docs_query_args' => $docs_query, |
| 211 |
'nested_docs_query_args' => $docs_query, |
| 212 |
'list_icon_name' => ($this->attributes['list_icon_name'] ?? 'list') == 'list' ? 'list' : [ 'value' => $this->attributes['list_icon_name'] ?? 'list' ], |
| 213 |
'show_header' => true, |
| 214 |
'show_list' => true, |
| 215 |
'show_title' => true, |
| 216 |
'show_button' => $show_button, |
| 217 |
'button_text' => $button_text, |
| 218 |
'show_button_icon' => true, |
| 219 |
'button_icon_position' => true, |
| 220 |
'title_tag' => $this->attributes['title_tag'] ?? 'h2', |
| 221 |
'button_icon' => true, |
| 222 |
'category_title_link' => $category_title_link, |
| 223 |
'layout_type' => $this->attributes['layout_type'] ?? '', |
| 224 |
'list_icon_url' => $this->attributes['list_icon_url'] ?? '', |
| 225 |
'show_list_icon' => $this->attributes['show_list_icon'] ?? true, |
| 226 |
'sidebar_layout' => $this->attributes['sidebar_layout'] ?? '' |
| 227 |
]; |
| 228 |
} |
| 229 |
} |
| 230 |
|