| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Functions to register client-side assets (scripts and stylesheets) for the |
| 5 |
* Gutenberg block. |
| 6 |
* |
| 7 |
* @package betterdocs |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Registers all block assets so that they can be enqueued through Gutenberg in |
| 12 |
* the corresponding context. |
| 13 |
* |
| 14 |
* @see https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/block-tutorial/applying-styles-with-stylesheets/ |
| 15 |
*/ |
| 16 |
function betterdocs_categorybox_block_init() |
| 17 |
{ |
| 18 |
// Skip block registration if Gutenberg is not enabled/merged. |
| 19 |
if (!function_exists('register_block_type')) { |
| 20 |
return; |
| 21 |
} |
| 22 |
$dir = dirname(__FILE__); |
| 23 |
|
| 24 |
$index_js = 'categorybox/index.js'; |
| 25 |
wp_register_script( |
| 26 |
'betterdocs-categorybox-block-editor', |
| 27 |
plugins_url($index_js, __FILE__), |
| 28 |
array( |
| 29 |
'wp-blocks', |
| 30 |
'wp-i18n', |
| 31 |
'wp-element', |
| 32 |
'wp-editor', |
| 33 |
'wp-block-editor', |
| 34 |
'betterdocs-blocks-edit-post' |
| 35 |
), |
| 36 |
filemtime("$dir/$index_js") |
| 37 |
); |
| 38 |
|
| 39 |
wp_register_style( |
| 40 |
'betterdocs-fontawesome-frontend', |
| 41 |
BETTERDOCS_URL . 'admin/assets/css/font-awesome5.css', |
| 42 |
array(), |
| 43 |
BETTERDOCS_VERSION, |
| 44 |
'all' |
| 45 |
); |
| 46 |
|
| 47 |
$editor_style = 'categorybox/style.css'; |
| 48 |
wp_register_style( |
| 49 |
'betterdocs-categorybox-block-editor', |
| 50 |
plugins_url($editor_style, __FILE__), |
| 51 |
array('betterdocs-fontawesome-frontend'), |
| 52 |
filemtime("$dir/$editor_style"), |
| 53 |
'all' |
| 54 |
); |
| 55 |
|
| 56 |
register_block_type(__DIR__ . '/categorybox', array( |
| 57 |
'editor_script' => 'betterdocs-categorybox-block-editor', |
| 58 |
'editor_style' => 'betterdocs-categorybox-block-editor', |
| 59 |
'render_callback' => 'betterdocs_categorybox_server_side_render' |
| 60 |
)); |
| 61 |
} |
| 62 |
add_action('init', 'betterdocs_categorybox_block_init'); |
| 63 |
|
| 64 |
/** |
| 65 |
* Category Box Server Side Render |
| 66 |
*/ |
| 67 |
function betterdocs_categorybox_server_side_render(array $attributes) |
| 68 |
{ |
| 69 |
if (!is_admin()) { |
| 70 |
wp_enqueue_style("betterdocs-categorybox-block-editor"); |
| 71 |
} |
| 72 |
|
| 73 |
$attributes = wp_parse_args( |
| 74 |
$attributes, |
| 75 |
[ |
| 76 |
'blockId' => '', |
| 77 |
'categories' => array(), |
| 78 |
'includeCategories' => '', |
| 79 |
'excludeCategories' => '', |
| 80 |
'boxPerPage' => 9, |
| 81 |
'orderBy' => 'name', |
| 82 |
'order' => 'asc', |
| 83 |
'layout' => 'default', |
| 84 |
'showIcon' => true, |
| 85 |
'showTitle' => true, |
| 86 |
'titleTag' => 'h2', |
| 87 |
'showCount' => true, |
| 88 |
'prefix' => '', |
| 89 |
'suffix' => __('articles', 'betterdocs'), |
| 90 |
'suffixSingular' => __('article', 'betterdocs'), |
| 91 |
] |
| 92 |
); |
| 93 |
|
| 94 |
$blockId = $attributes['blockId']; |
| 95 |
$includeCategories = $attributes['includeCategories']; |
| 96 |
$excludeCategories = $attributes['excludeCategories']; |
| 97 |
$boxPerPage = $attributes['boxPerPage']; |
| 98 |
$orderBy = $attributes['orderBy']; |
| 99 |
$order = $attributes['order']; |
| 100 |
$layout = $attributes['layout']; |
| 101 |
$showIcon = $attributes['showIcon']; |
| 102 |
$showTitle = $attributes['showTitle']; |
| 103 |
$titleTag = $attributes['titleTag']; |
| 104 |
$showCount = $attributes['showCount']; |
| 105 |
$prefix = $attributes['prefix']; |
| 106 |
$suffix = $attributes['suffix']; |
| 107 |
$suffixSingular = $attributes['suffixSingular']; |
| 108 |
$enableNestedSubcategory = false; |
| 109 |
|
| 110 |
$terms_object = array( |
| 111 |
'taxonomy' => 'doc_category', |
| 112 |
'order' => $order, |
| 113 |
'orderby' => $orderBy, |
| 114 |
'hide_empty' => true, |
| 115 |
'number' => $boxPerPage ? $boxPerPage : 9, |
| 116 |
); |
| 117 |
|
| 118 |
if ('doc_category_order' === $orderBy) { |
| 119 |
$terms_object['meta_key'] = 'doc_category_order'; |
| 120 |
$terms_object['orderby'] = 'meta_value_num'; |
| 121 |
} |
| 122 |
|
| 123 |
$includes = array(); |
| 124 |
if ($includeCategories && $includeCategories !== '[]') { |
| 125 |
$includes = json_decode($includeCategories, true); |
| 126 |
$includes = array_map(function ($item) { |
| 127 |
return $item['value']; |
| 128 |
}, $includes); |
| 129 |
} |
| 130 |
|
| 131 |
$excludes = array(); |
| 132 |
if ($excludeCategories && $excludeCategories !== '[]') { |
| 133 |
$excludes = json_decode($excludeCategories, true); |
| 134 |
$excludes = array_map(function ($item) { |
| 135 |
return $item['value']; |
| 136 |
}, $excludes); |
| 137 |
} |
| 138 |
|
| 139 |
if (is_array($includes) && count($includes)) { |
| 140 |
$terms_object['include'] = array_diff($includes, (array) $excludes); |
| 141 |
} |
| 142 |
|
| 143 |
if (is_array($excludes) && count($excludes)) { |
| 144 |
$terms_object['exclude'] = $excludes; |
| 145 |
} |
| 146 |
|
| 147 |
$taxonomy_objects = get_terms($terms_object); |
| 148 |
|
| 149 |
$html = ''; |
| 150 |
$default_multiple_kb = false; |
| 151 |
|
| 152 |
$html .= "<div class='el-betterdocs-category-box-wrapper $blockId'>"; |
| 153 |
$html .= "<div class='el-betterdocs-category-box el-betterdocs-column'>"; |
| 154 |
|
| 155 |
|
| 156 |
if ($taxonomy_objects && !is_wp_error($taxonomy_objects)) { |
| 157 |
foreach ($taxonomy_objects as $term) { |
| 158 |
$term_id = $term->term_id; |
| 159 |
$term_slug = $term->slug; |
| 160 |
$count = $term->count; |
| 161 |
$get_term_count = betterdocs_get_postcount($count, $term_id, $enableNestedSubcategory); |
| 162 |
$term_count = apply_filters('betterdocs_postcount', $get_term_count, $default_multiple_kb, $term_id, $term_slug, $count, $enableNestedSubcategory); |
| 163 |
if ($term_count > 0) { |
| 164 |
if ($layout === 'default') { |
| 165 |
include BETTERDOCS_DIR_PATH . 'includes/gutenberg/template/category-box/Layout_Default.php'; |
| 166 |
} else if ($layout === 'layout-2') { |
| 167 |
include BETTERDOCS_DIR_PATH . 'includes/gutenberg/template/category-box/Layout_2.php'; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} else { |
| 172 |
$html .= '<p class="no-posts-found">' . __('No posts found!', 'betterdocs') . '</p>'; |
| 173 |
} |
| 174 |
|
| 175 |
$html .= "</div>"; |
| 176 |
$html .= "</div>"; |
| 177 |
|
| 178 |
return $html; |
| 179 |
} |
| 180 |
|