| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly. |
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
if ( ! class_exists( 'Qi_Blocks_Categories_Block' ) ) { |
| 7 |
class Qi_Blocks_Categories_Block extends Qi_Blocks_Blocks { |
| 8 |
private static $instance; |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
// Set block data |
| 12 |
$this->set_block_name( 'categories' ); |
| 13 |
$this->set_block_title( esc_html__( 'Categories', 'qi-blocks' ) ); |
| 14 |
$this->set_block_subcategory( esc_html__( 'Content', 'qi-blocks' ) ); |
| 15 |
$this->set_block_demo_url( 'https://qodeinteractive.com/qi-blocks-for-gutenberg/categories/#categories' ); |
| 16 |
$this->set_block_documentation( 'https://qodeinteractive.com/qi-blocks-for-gutenberg/documentation/#categories' ); |
| 17 |
|
| 18 |
$block_options = array( |
| 19 |
'render_callback' => array( $this, 'dynamic_render_callback' ), |
| 20 |
'attributes' => array_merge( |
| 21 |
array( |
| 22 |
'uniqueClass' => array( |
| 23 |
'type' => 'string', |
| 24 |
'default' => '', |
| 25 |
), |
| 26 |
'blockContainerIds' => array( |
| 27 |
'type' => 'string', |
| 28 |
'default' => '', |
| 29 |
), |
| 30 |
'blockContainerData' => array( |
| 31 |
'type' => 'string', |
| 32 |
'default' => '', |
| 33 |
), |
| 34 |
'blockContainerClasses' => array( |
| 35 |
'type' => 'string', |
| 36 |
'default' => '', |
| 37 |
), |
| 38 |
), |
| 39 |
qi_blocks_get_block_container_attributes(), |
| 40 |
array( |
| 41 |
'showPostCounts' => array( |
| 42 |
'type' => 'boolean', |
| 43 |
'default' => false, |
| 44 |
), |
| 45 |
'showEmpty' => array( |
| 46 |
'type' => 'boolean', |
| 47 |
'default' => false, |
| 48 |
), |
| 49 |
'showHierarchy' => array( |
| 50 |
'type' => 'boolean', |
| 51 |
'default' => false, |
| 52 |
), |
| 53 |
'showOnlyTopLevel' => array( |
| 54 |
'type' => 'boolean', |
| 55 |
'default' => false, |
| 56 |
), |
| 57 |
'textUnderlineBehavior' => array( |
| 58 |
'type' => 'string', |
| 59 |
'default' => '', |
| 60 |
), |
| 61 |
'color' => array( |
| 62 |
'type' => 'string', |
| 63 |
'default' => '', |
| 64 |
), |
| 65 |
'hoverColor' => array( |
| 66 |
'type' => 'string', |
| 67 |
'default' => '', |
| 68 |
), |
| 69 |
), |
| 70 |
qi_blocks_get_block_option_typography_attributes( 'categories' ), |
| 71 |
array( |
| 72 |
'categoriesSpaceBetweenItems' => array( |
| 73 |
'type' => 'number', |
| 74 |
'default' => '', |
| 75 |
), |
| 76 |
'categoriesSpaceBetweenItemsUnit' => array( |
| 77 |
'type' => 'string', |
| 78 |
'default' => 'px', |
| 79 |
), |
| 80 |
'categoriesSpaceBetweenItemsDecimal' => array( |
| 81 |
'type' => 'number', |
| 82 |
'default' => '', |
| 83 |
), |
| 84 |
) |
| 85 |
), |
| 86 |
); |
| 87 |
|
| 88 |
$this->set_block_options( $block_options ); |
| 89 |
|
| 90 |
parent::__construct(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @return Qi_Blocks_Categories_Block |
| 95 |
*/ |
| 96 |
public static function get_instance() { |
| 97 |
if ( is_null( self::$instance ) ) { |
| 98 |
self::$instance = new self(); |
| 99 |
} |
| 100 |
|
| 101 |
return self::$instance; |
| 102 |
} |
| 103 |
|
| 104 |
function dynamic_render_callback( $attributes ) { |
| 105 |
$html = ''; |
| 106 |
|
| 107 |
$args = array( |
| 108 |
'echo' => false, |
| 109 |
'hierarchical' => $attributes['showHierarchy'] ?? true, |
| 110 |
'orderby' => 'name', |
| 111 |
'show_count' => $attributes['showPostCounts'] ?? 0, |
| 112 |
'title_li' => '', |
| 113 |
'hide_empty' => ! $attributes['showEmpty'] ?? 1, |
| 114 |
); |
| 115 |
|
| 116 |
if ( $attributes['showOnlyTopLevel'] ) { |
| 117 |
$args['parent'] = 0; |
| 118 |
} |
| 119 |
|
| 120 |
$categories = wp_list_categories( $args ); |
| 121 |
|
| 122 |
$block_classes = qi_blocks_get_block_holder_classes( 'categories', $attributes ); |
| 123 |
|
| 124 |
if ( ! empty( $attributes['textUnderlineBehavior'] ) ) { |
| 125 |
$block_classes[] = 'qodef-text--' . esc_attr( $attributes['textUnderlineBehavior'] ); |
| 126 |
} |
| 127 |
|
| 128 |
$html .= '<div ' . qi_blocks_get_block_container_html_attributes_string( $attributes ) . '>'; |
| 129 |
$html .= '<div class="' . implode( ' ', $block_classes ) . '">'; |
| 130 |
|
| 131 |
if ( ! empty( $categories ) ) { |
| 132 |
$html .= '<ul>' . $categories . '</ul>'; |
| 133 |
} else { |
| 134 |
$html .= '<p class="qodef-m-terms-not-found">' . esc_html__( 'No terms were found for provided query parameters.', 'qi-blocks' ) . '</p>'; |
| 135 |
} |
| 136 |
|
| 137 |
$html .= '</div>'; |
| 138 |
$html .= '</div>'; |
| 139 |
|
| 140 |
return $html; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
Qi_Blocks_Categories_Block::get_instance(); |
| 145 |
} |
| 146 |
|