| 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 |
if( ! is_array( $data ) ) { |
| 51 |
return $data; |
| 52 |
} |
| 53 |
$new_array = []; |
| 54 |
if( $using === 'key' ) { |
| 55 |
if( $way !== 'ASC' ) { |
| 56 |
krsort( $data ); |
| 57 |
} else { |
| 58 |
ksort( $data ); |
| 59 |
} |
| 60 |
} else { |
| 61 |
foreach( $data as $key => $value ) { |
| 62 |
if( ! is_array( $value ) ) continue; |
| 63 |
foreach( $value as $inner_key => $single ) { |
| 64 |
if( $inner_key == $using ) { |
| 65 |
$value[ 'tempid' ] = $key; |
| 66 |
$single = self::numeric_key_gen( $new_array, $single ); |
| 67 |
$new_array[ $single ] = $value; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if( $way !== 'ASC' ) { |
| 73 |
krsort( $new_array ); |
| 74 |
} else { |
| 75 |
ksort( $new_array ); |
| 76 |
} |
| 77 |
|
| 78 |
if( ! empty( $new_array ) ) { |
| 79 |
foreach( $new_array as $array ) { |
| 80 |
$index = $array['tempid']; |
| 81 |
unset( $array['tempid'] ); |
| 82 |
$new_data[ $index ] = $array; |
| 83 |
} |
| 84 |
$data = $new_data; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return $data; |
| 89 |
} |
| 90 |
/** |
| 91 |
* This function is responsible for generate unique numeric key for a given array. |
| 92 |
* |
| 93 |
* @param array $data |
| 94 |
* @param integer $index |
| 95 |
* @return integer |
| 96 |
*/ |
| 97 |
private static function numeric_key_gen( $data, $index = 0 ){ |
| 98 |
if( isset( $data[ $index ] ) ) { |
| 99 |
$index+=1; |
| 100 |
return self::numeric_key_gen( $data, $index ); |
| 101 |
} |
| 102 |
return $index; |
| 103 |
} |
| 104 |
/** |
| 105 |
* Sorting Data |
| 106 |
* by their type |
| 107 |
* |
| 108 |
* @param array $value |
| 109 |
* @param string $key |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
public static function sortBy( &$value, $key = 'comments' ) { |
| 113 |
switch( $key ){ |
| 114 |
case 'comments' : |
| 115 |
return self::sorter( $value, 'key', 'DESC' ); |
| 116 |
break; |
| 117 |
default: |
| 118 |
return self::sorter( $value, 'timestamp', 'DESC' ); |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
/** |
| 123 |
* Human Readable Time Diff |
| 124 |
* |
| 125 |
* @param boolean $time |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public static function get_timeago_html( $time = false ) { |
| 129 |
if ( ! $time ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
$offset = get_option('gmt_offset'); // Time offset in hours |
| 134 |
$local_time = $time + ($offset * 60 * 60 ); // added offset in seconds |
| 135 |
$time = human_time_diff( $local_time, current_time('timestamp') ); |
| 136 |
ob_start(); |
| 137 |
?> |
| 138 |
<small><?php echo esc_html__( 'About', 'betterdocs' ) . ' ' . esc_html( $time ) . ' ' . esc_html__( 'ago', 'betterdocs' ) ?></small> |
| 139 |
<?php |
| 140 |
$time_ago = ob_get_clean(); |
| 141 |
return $time_ago; |
| 142 |
} |
| 143 |
/** |
| 144 |
* Get all post types |
| 145 |
* |
| 146 |
* @param array $exclude |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public static function post_types( $exclude = array() ) { |
| 150 |
$post_types = get_post_types(array( |
| 151 |
'public' => true, |
| 152 |
'show_ui' => true |
| 153 |
), 'objects'); |
| 154 |
|
| 155 |
unset( $post_types['attachment'] ); |
| 156 |
|
| 157 |
if ( count( $exclude ) ) { |
| 158 |
foreach ( $exclude as $type ) { |
| 159 |
if ( isset( $post_types[$type] ) ) { |
| 160 |
unset( $post_types[$type] ); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return apply_filters('betterdocs_post_types', $post_types ); |
| 166 |
} |
| 167 |
/** |
| 168 |
* Get all taxonomies |
| 169 |
* |
| 170 |
* @param string $post_type |
| 171 |
* @param array $exclude |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
public static function taxonomies( $post_type = '', $exclude = array() ) { |
| 175 |
if ( empty( $post_type ) ) { |
| 176 |
$taxonomies = get_taxonomies( |
| 177 |
array( |
| 178 |
'public' => true, |
| 179 |
'_builtin' => false |
| 180 |
), |
| 181 |
'objects' |
| 182 |
); |
| 183 |
} else { |
| 184 |
$taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 185 |
} |
| 186 |
|
| 187 |
$data = array(); |
| 188 |
if( is_array( $taxonomies ) ) { |
| 189 |
foreach ( $taxonomies as $tax_slug => $tax ) { |
| 190 |
if( ! $tax->public || ! $tax->show_ui ) { |
| 191 |
continue; |
| 192 |
} |
| 193 |
if( in_array( $tax_slug, $exclude ) ) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
$data[$tax_slug] = $tax; |
| 197 |
} |
| 198 |
} |
| 199 |
return apply_filters('betterdocs_loop_taxonomies', $data, $taxonomies, $post_type ); |
| 200 |
} |
| 201 |
} |
| 202 |
|