| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Post_Category { |
| 7 |
public function __construct() { |
| 8 |
add_action( 'init', array( $this, 'register' ) ); |
| 9 |
} |
| 10 |
public function get_attributes() { |
| 11 |
|
| 12 |
return array( |
| 13 |
'blockId' => '', |
| 14 |
|
| 15 |
/* |
| 16 |
============================ |
| 17 |
Post Category Setting |
| 18 |
============================*/ |
| 19 |
'catLabelShow' => true, |
| 20 |
'catIconShow' => true, |
| 21 |
'catSeparator' => ',', |
| 22 |
'catAlign' => (object) array(), |
| 23 |
|
| 24 |
/* |
| 25 |
============================ |
| 26 |
Categories Label Settings |
| 27 |
============================*/ |
| 28 |
'catLabel' => 'Category : ', |
| 29 |
|
| 30 |
/* |
| 31 |
============================ |
| 32 |
Categories Icon Style |
| 33 |
============================*/ |
| 34 |
'catIconStyle' => '', |
| 35 |
|
| 36 |
/* |
| 37 |
============================ |
| 38 |
Advance Setting |
| 39 |
============================*/ |
| 40 |
'advanceId' => '', |
| 41 |
'advanceZindex' => '', |
| 42 |
'hideExtraLarge' => false, |
| 43 |
'hideDesktop' => false, |
| 44 |
'hideTablet' => false, |
| 45 |
'hideMobile' => false, |
| 46 |
'advanceCss' => '', |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
public function register() { |
| 51 |
register_block_type( |
| 52 |
'ultimate-post/post-category', |
| 53 |
array( |
| 54 |
'editor_script' => 'ultp-blocks-editor-script', |
| 55 |
'editor_style' => 'ultp-blocks-editor-css', |
| 56 |
'render_callback' => array( $this, 'content' ), |
| 57 |
) |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public function content( $attr, $noAjax ) { |
| 62 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 63 |
$block_name = 'post-category'; |
| 64 |
$wrapper_before = $wrapper_after = $content = ''; |
| 65 |
|
| 66 |
$attr['className'] = isset( $attr['className'] ) && $attr['className'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['className'] ) : ''; |
| 67 |
$attr['align'] = isset( $attr['align'] ) && $attr['align'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['align'] ) : ''; |
| 68 |
$attr['advanceId'] = isset( $attr['advanceId'] ) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 69 |
$attr['blockId'] = isset( $attr['blockId'] ) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 70 |
$allowed_html_tags = ultimate_post()->ultp_allowed_html_tags(); |
| 71 |
$attr['catLabel'] = wp_kses( $attr['catLabel'], $allowed_html_tags ); |
| 72 |
$attr['catSeparator'] = wp_kses( $attr['catSeparator'], $allowed_html_tags ); |
| 73 |
|
| 74 |
$categories = get_the_category(); |
| 75 |
if ( ! empty( $categories ) ) { |
| 76 |
$wrapper_before .= '<div ' . ( $attr['advanceId'] ? 'id="' . $attr['advanceId'] . '" ' : '' ) . ' class="wp-block-ultimate-post-' . $block_name . ' ultp-block-' . $attr['blockId'] . ( $attr['className'] ? ' ' . $attr['className'] : '' ) . '' . ( $attr['align'] ? ' align' . $attr['align'] : '' ) . '">'; |
| 77 |
$wrapper_before .= '<div class="ultp-block-wrapper">'; |
| 78 |
$content .= '<div class="ultp-builder-category">'; |
| 79 |
if ( $attr['catIconShow'] ) { |
| 80 |
$content .= ultimate_post()->get_svg_icon( '' . $attr['catIconStyle'] . '' ); |
| 81 |
} |
| 82 |
if ( $attr['catLabelShow'] ) { |
| 83 |
$content .= '<div class="cat-builder-label">' . $attr['catLabel'] . '</div>'; |
| 84 |
} |
| 85 |
$content .= '<div class="cat-builder-content">'; |
| 86 |
foreach ( $categories as $key => $category ) { |
| 87 |
$content .= ( ( $key > 0 && $attr['catSeparator'] ) ? ' ' . $attr['catSeparator'] : '' ) . '<a class="ultp-category-list" href="' . get_term_link( $category->term_id ) . '">' . $category->name . '</a>'; |
| 88 |
} |
| 89 |
$content .= '</div>'; |
| 90 |
$content .= '</div>'; |
| 91 |
$wrapper_after .= '</div>'; |
| 92 |
$wrapper_after .= '</div>'; |
| 93 |
} |
| 94 |
|
| 95 |
return $wrapper_before . $content . $wrapper_after; |
| 96 |
} |
| 97 |
} |
| 98 |
|