| 1 |
<?php |
| 2 |
/** |
| 3 |
* This class will provide all kind of helper methods. |
| 4 |
*/ |
| 5 |
class BetterDocs_Helper { |
| 6 |
/** |
| 7 |
* This function is responsible for the data sanitization |
| 8 |
* |
| 9 |
* @param array $field |
| 10 |
* @param string|array $value |
| 11 |
* @return string|array |
| 12 |
*/ |
| 13 |
public static function sanitize_field( $field, $value ) { |
| 14 |
if ( isset( $field['sanitize'] ) && ! empty( $field['sanitize'] ) ) { |
| 15 |
if ( function_exists( $field['sanitize'] ) ) { |
| 16 |
$value = call_user_func( $field['sanitize'], $value ); |
| 17 |
} |
| 18 |
return $value; |
| 19 |
} |
| 20 |
|
| 21 |
if( is_array( $field ) && isset( $field['type'] ) ) { |
| 22 |
switch ( $field['type'] ) { |
| 23 |
case 'text': |
| 24 |
$value = sanitize_text_field( $value ); |
| 25 |
break; |
| 26 |
case 'textarea': |
| 27 |
$value = sanitize_textarea_field( $value ); |
| 28 |
break; |
| 29 |
case 'email': |
| 30 |
$value = sanitize_email( $value ); |
| 31 |
break; |
| 32 |
default: |
| 33 |
return $value; |
| 34 |
break; |
| 35 |
} |
| 36 |
} else { |
| 37 |
$value = sanitize_text_field( $value ); |
| 38 |
} |
| 39 |
|
| 40 |
return $value; |
| 41 |
} |
| 42 |
/** |
| 43 |
* This function is responsible for making an array sort by their key |
| 44 |
* @param array $data |
| 45 |
* @param string $using |
| 46 |
* @param string $way |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
public static function sorter( $data, $using = 'time_date', $way = 'DESC' ){ |
| 50 |
|
| 51 |
if( ! is_array( $data ) ) { |
| 52 |
return $data; |
| 53 |
} |
| 54 |
|
| 55 |
$new_array = []; |
| 56 |
|
| 57 |
if( $using === 'key' ) { |
| 58 |
|
| 59 |
if( $way !== 'ASC' ) { |
| 60 |
|
| 61 |
krsort( $data ); |
| 62 |
|
| 63 |
} else { |
| 64 |
|
| 65 |
ksort( $data ); |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
} else { |
| 70 |
|
| 71 |
foreach( $data as $key => $value ) { |
| 72 |
if( ! is_array( $value ) ) continue; |
| 73 |
foreach( $value as $inner_key => $single ) { |
| 74 |
if( $inner_key == $using ) { |
| 75 |
$value[ 'tempid' ] = $key; |
| 76 |
$single = self::numeric_key_gen( $new_array, $single ); |
| 77 |
$new_array[ $single ] = $value; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if( $way !== 'ASC' ) { |
| 83 |
|
| 84 |
krsort( $new_array ); |
| 85 |
|
| 86 |
} else { |
| 87 |
|
| 88 |
ksort( $new_array ); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
if( ! empty( $new_array ) ) { |
| 93 |
|
| 94 |
foreach( $new_array as $array ) { |
| 95 |
|
| 96 |
$index = $array['tempid']; |
| 97 |
unset( $array['tempid'] ); |
| 98 |
$new_data[ $index ] = $array; |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
$data = $new_data; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return $data; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* This function is responsible for generate unique numeric key for a given array. |
| 111 |
* |
| 112 |
* @param array $data |
| 113 |
* @param integer $index |
| 114 |
* @return integer |
| 115 |
*/ |
| 116 |
private static function numeric_key_gen( $data, $index = 0 ) { |
| 117 |
|
| 118 |
if( isset( $data[ $index ] ) ) { |
| 119 |
|
| 120 |
$index+=1; |
| 121 |
return self::numeric_key_gen( $data, $index ); |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
return $index; |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Sorting Data |
| 131 |
* by their type |
| 132 |
* |
| 133 |
* @param array $value |
| 134 |
* @param string $key |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public static function sortBy( &$value, $key = 'comments' ) { |
| 138 |
|
| 139 |
switch( $key ) { |
| 140 |
|
| 141 |
case 'comments' : |
| 142 |
return self::sorter( $value, 'key', 'DESC' ); |
| 143 |
break; |
| 144 |
|
| 145 |
default: |
| 146 |
return self::sorter( $value, 'timestamp', 'DESC' ); |
| 147 |
break; |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Human Readable Time Diff |
| 154 |
* |
| 155 |
* @param boolean $time |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
public static function get_timeago_html( $time = false ) { |
| 159 |
if ( ! $time ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
$offset = get_option('gmt_offset'); // Time offset in hours |
| 164 |
$local_time = $time + ($offset * 60 * 60 ); // added offset in seconds |
| 165 |
$time = human_time_diff( $local_time, current_time('timestamp') ); |
| 166 |
ob_start(); |
| 167 |
?> |
| 168 |
<small><?php echo esc_html__( 'About', 'betterdocs' ) . ' ' . esc_html( $time ) . ' ' . esc_html__( 'ago', 'betterdocs' ) ?></small> |
| 169 |
<?php |
| 170 |
$time_ago = ob_get_clean(); |
| 171 |
return $time_ago; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get all post types |
| 176 |
* |
| 177 |
* @param array $exclude |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
public static function post_types( $exclude = array() ) { |
| 181 |
|
| 182 |
$post_types = get_post_types(array( |
| 183 |
'public' => true, |
| 184 |
'show_ui' => true |
| 185 |
), 'objects'); |
| 186 |
|
| 187 |
unset( $post_types['attachment'] ); |
| 188 |
|
| 189 |
if ( count( $exclude ) ) { |
| 190 |
foreach ( $exclude as $type ) { |
| 191 |
if ( isset( $post_types[$type] ) ) { |
| 192 |
unset( $post_types[$type] ); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return apply_filters('betterdocs_post_types', $post_types ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Get all taxonomies |
| 202 |
* |
| 203 |
* @param string $post_type |
| 204 |
* @param array $exclude |
| 205 |
* @return void |
| 206 |
*/ |
| 207 |
public static function taxonomies( $post_type = '', $exclude = array() ) { |
| 208 |
|
| 209 |
if ( empty( $post_type ) ) { |
| 210 |
$taxonomies = get_taxonomies( |
| 211 |
array( |
| 212 |
'public' => true, |
| 213 |
'_builtin' => false |
| 214 |
), |
| 215 |
'objects' |
| 216 |
); |
| 217 |
} else { |
| 218 |
|
| 219 |
$taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 220 |
} |
| 221 |
|
| 222 |
$data = array(); |
| 223 |
|
| 224 |
if( is_array( $taxonomies ) ) { |
| 225 |
foreach ( $taxonomies as $tax_slug => $tax ) { |
| 226 |
if( ! $tax->public || ! $tax->show_ui ) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
if( in_array( $tax_slug, $exclude ) ) { |
| 230 |
continue; |
| 231 |
} |
| 232 |
$data[$tax_slug] = $tax; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return apply_filters('betterdocs_loop_taxonomies', $data, $taxonomies, $post_type ); |
| 237 |
} |
| 238 |
|
| 239 |
public static function list_svg() { |
| 240 |
|
| 241 |
$html = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" width="0.86em" height="1em" style="-ms-transform: rotate(360deg); -webkit-transform: rotate(360deg); transform: rotate(360deg);" preserveAspectRatio="xMidYMid meet" viewBox="0 0 1536 1792"><path d="M1468 380q28 28 48 76t20 88v1152q0 40-28 68t-68 28H96q-40 0-68-28t-28-68V96q0-40 28-68T96 0h896q40 0 88 20t76 48zm-444-244v376h376q-10-29-22-41l-313-313q-12-12-41-22zm384 1528V640H992q-40 0-68-28t-28-68V128H128v1536h1280zM384 800q0-14 9-23t23-9h704q14 0 23 9t9 23v64q0 14-9 23t-23 9H416q-14 0-23-9t-9-23v-64zm736 224q14 0 23 9t9 23v64q0 14-9 23t-23 9H416q-14 0-23-9t-9-23v-64q0-14 9-23t23-9h704zm0 256q14 0 23 9t9 23v64q0 14-9 23t-23 9H416q-14 0-23-9t-9-23v-64q0-14 9-23t23-9h704z"/></svg>'; |
| 242 |
return $html; |
| 243 |
} |
| 244 |
|
| 245 |
public static function arrow_right_svg() { |
| 246 |
|
| 247 |
$html = '<svg class="toggle-arrow arrow-right" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" width="0.48em" height="1em" style="-ms-transform: rotate(360deg); -webkit-transform: rotate(360deg); transform: rotate(360deg);" preserveAspectRatio="xMidYMid meet" viewBox="0 0 608 1280"><g transform="translate(608 0) scale(-1 1)"><path d="M595 288q0 13-10 23L192 704l393 393q10 10 10 23t-10 23l-50 50q-10 10-23 10t-23-10L23 727q-10-10-10-23t10-23l466-466q10-10 23-10t23 10l50 50q10 10 10 23z"/></g></svg>'; |
| 248 |
return $html; |
| 249 |
} |
| 250 |
|
| 251 |
public static function arrow_down_svg() { |
| 252 |
|
| 253 |
$html = '<svg class="toggle-arrow arrow-down" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" width="0.8em" height="1em" style="-ms-transform: rotate(360deg); -webkit-transform: rotate(360deg); transform: rotate(360deg);" preserveAspectRatio="xMidYMid meet" viewBox="0 0 1024 1280"><path d="M1011 480q0 13-10 23L535 969q-10 10-23 10t-23-10L23 503q-10-10-10-23t10-23l50-50q10-10 23-10t23 10l393 393l393-393q10-10 23-10t23 10l50 50q10 10 10 23z"/></svg>'; |
| 254 |
return $html; |
| 255 |
} |
| 256 |
|
| 257 |
public static function get_tax( $tax = '' ) { |
| 258 |
|
| 259 |
global $wp_query; |
| 260 |
|
| 261 |
if ( is_tax( 'knowledge_base' ) ) { |
| 262 |
$get_tax = $wp_query->tax_query->queried_terms; |
| 263 |
if (array_key_exists( "doc_category", $get_tax )) { |
| 264 |
$tax = 'doc_category'; |
| 265 |
} else { |
| 266 |
$tax = 'knowledge_base'; |
| 267 |
} |
| 268 |
} elseif ( is_tax( 'doc_category' ) ) { |
| 269 |
$tax = 'doc_category'; |
| 270 |
} |
| 271 |
|
| 272 |
return $tax; |
| 273 |
} |
| 274 |
|
| 275 |
public static function list_query_arg( $post_type, $multiple_kb, $tax_slug, $posts_per_grid, $alphabetically_order_post ) { |
| 276 |
|
| 277 |
$args = array( |
| 278 |
'post_type' => $post_type, |
| 279 |
'post_status' => 'publish', |
| 280 |
); |
| 281 |
|
| 282 |
$tax_query = array( |
| 283 |
array( |
| 284 |
'taxonomy' => 'doc_category', |
| 285 |
'field' => 'slug', |
| 286 |
'terms' => $tax_slug, |
| 287 |
'operator' => 'AND', |
| 288 |
'include_children' => false |
| 289 |
), |
| 290 |
); |
| 291 |
|
| 292 |
$args['tax_query'] = apply_filters( 'betterdocs_list_tax_query_arg', $tax_query, $multiple_kb, $tax_slug ); |
| 293 |
|
| 294 |
|
| 295 |
if ( $posts_per_grid ) { |
| 296 |
$args['posts_per_page'] = $posts_per_grid; |
| 297 |
} |
| 298 |
|
| 299 |
if($alphabetically_order_post == 1) { |
| 300 |
$args['orderby'] = 'title'; |
| 301 |
$args['order'] = 'ASC'; |
| 302 |
} |
| 303 |
|
| 304 |
return $args; |
| 305 |
} |
| 306 |
|
| 307 |
public static function count_kb() { |
| 308 |
|
| 309 |
$result = array(); |
| 310 |
$kbs = get_terms( array( |
| 311 |
'taxonomy' => 'knowledge_base', |
| 312 |
'hide_empty' => false, |
| 313 |
'fields' => 'id=>slug' |
| 314 |
) ); |
| 315 |
|
| 316 |
$cats = get_terms( array( |
| 317 |
'taxonomy' => 'doc_category', |
| 318 |
'hide_empty' => false, |
| 319 |
'fields' => 'id=>slug' |
| 320 |
) ); |
| 321 |
|
| 322 |
|
| 323 |
foreach ($kbs as $kb) { |
| 324 |
|
| 325 |
foreach ($cats as $cat) { |
| 326 |
|
| 327 |
$args = array( |
| 328 |
'post_type' => 'docs', |
| 329 |
'post_status'=>'publish', |
| 330 |
'tax_query' => array( |
| 331 |
'relation' => 'AND', |
| 332 |
array( |
| 333 |
'taxonomy' => 'knowledge_base', |
| 334 |
'field' => 'slug', |
| 335 |
'terms' => array( $kb ), |
| 336 |
), |
| 337 |
array( |
| 338 |
'taxonomy' => 'doc_category', |
| 339 |
'field' => 'slug', |
| 340 |
'terms' => array( $cat ), |
| 341 |
), |
| 342 |
), |
| 343 |
); |
| 344 |
$query = new WP_Query( $args ); |
| 345 |
|
| 346 |
$result[$kb][$cat] = $query->post_count; |
| 347 |
|
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return $result; |
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
public static function count_category( $kb_slug, $cat_slug ) { |
| 356 |
|
| 357 |
$args = array( |
| 358 |
'post_type' => 'docs', |
| 359 |
'post_status' => 'publish', |
| 360 |
); |
| 361 |
|
| 362 |
$taxes = array( 'knowledge_base', 'doc_category' ); |
| 363 |
foreach ( $taxes as $tax ) { |
| 364 |
$terms = get_terms( $tax ); |
| 365 |
|
| 366 |
foreach ( $terms as $term ) |
| 367 |
$tax_map[$tax][$term->slug] = $term->term_taxonomy_id; |
| 368 |
} |
| 369 |
|
| 370 |
$args['tax_query'] = array( |
| 371 |
'relation' => 'AND', |
| 372 |
array( |
| 373 |
'taxonomy' => 'knowledge_base', |
| 374 |
'field' => 'term_taxonomy_id', |
| 375 |
'terms' => array( $tax_map['knowledge_base'][$kb_slug] ), |
| 376 |
'operator' => 'IN', |
| 377 |
'include_children' => false, |
| 378 |
), |
| 379 |
array( |
| 380 |
'taxonomy' => 'doc_category', |
| 381 |
'field' => 'term_taxonomy_id', |
| 382 |
'operator' => 'IN', |
| 383 |
'terms' => array( $tax_map['doc_category'][$cat_slug] ), |
| 384 |
'include_children' => false, |
| 385 |
), |
| 386 |
); |
| 387 |
|
| 388 |
$query = new WP_Query( $args ); |
| 389 |
|
| 390 |
$count = $query->found_posts; |
| 391 |
|
| 392 |
return $count; |
| 393 |
} |
| 394 |
|
| 395 |
public static function taxonomy_object( $multiple_kb, $terms ) { |
| 396 |
|
| 397 |
$terms_object = array( |
| 398 |
'hide_empty' => true, |
| 399 |
'taxonomy' => 'doc_category', |
| 400 |
'orderby' => 'name', |
| 401 |
'parent' => 0 |
| 402 |
); |
| 403 |
|
| 404 |
$meta_query = ''; |
| 405 |
$terms_object['meta_query'] = apply_filters( 'betterdocs_taxonomy_object_meta_query', $meta_query, $multiple_kb ); |
| 406 |
|
| 407 |
if ( $terms ) { |
| 408 |
unset($terms_object['parent']); |
| 409 |
} |
| 410 |
|
| 411 |
if ( $terms ) { |
| 412 |
$terms_object['include'] = explode(',', $terms ); |
| 413 |
$terms_object['orderby'] = 'include'; |
| 414 |
} |
| 415 |
|
| 416 |
$taxonomy_objects = get_terms( $terms_object ); |
| 417 |
|
| 418 |
return $taxonomy_objects; |
| 419 |
} |
| 420 |
|
| 421 |
public static function child_taxonomy_terms( $term_id, $multiple_kb ) { |
| 422 |
|
| 423 |
$terms_object = array( |
| 424 |
'child_of' => $term_id, |
| 425 |
'orderby' => 'name' |
| 426 |
); |
| 427 |
|
| 428 |
$meta_query = ''; |
| 429 |
$terms_object['meta_query'] = apply_filters( 'betterdocs_child_taxonomy_meta_query', $meta_query, $multiple_kb ); |
| 430 |
|
| 431 |
$taxonomy_objects = get_terms( 'doc_category', $terms_object); |
| 432 |
|
| 433 |
return $taxonomy_objects; |
| 434 |
} |
| 435 |
|
| 436 |
public static function term_permalink( $texanomy, $term_slug ) { |
| 437 |
|
| 438 |
$get_term_link = get_term_link( $term_slug, $texanomy ); |
| 439 |
|
| 440 |
$term_permalink = apply_filters( 'betterdocs_cat_term_permalink', $get_term_link ); |
| 441 |
|
| 442 |
return $term_permalink; |
| 443 |
} |
| 444 |
|
| 445 |
} |
| 446 |
|