| 1 |
<?php |
| 2 |
namespace OACS\SolidPostLikes\Controllers; |
| 3 |
if ( ! defined( 'WPINC' ) ) { die; } |
| 4 |
/** loads the like count and formats it */ |
| 5 |
|
| 6 |
class SolidPostLikesCounter |
| 7 |
{ |
| 8 |
|
| 9 |
public function oacs_spl_get_like_count( $like_count ) |
| 10 |
{ |
| 11 |
|
| 12 |
/** Load Admin Options so we know how to style the output */ |
| 13 |
$oacs_spl_counter_active_setting = carbon_get_theme_option( 'oacs_spl_show_counter' ) ?? ''; |
| 14 |
$oacs_spl_counter_size_setting = carbon_get_theme_option('oacs_spl_counter_size') ?? ''; |
| 15 |
$oacs_spl_counter_color_setting = carbon_get_theme_option('oacs_spl_counter_color') ?? ''; |
| 16 |
$oacs_spl_counter_padding_setting = carbon_get_theme_option('oacs_spl_counter_padding') ?? ''; |
| 17 |
|
| 18 |
if(empty($oacs_spl_counter_size_setting)) { |
| 19 |
|
| 20 |
$oacs_spl_counter_size_setting = 'inherit'; |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
if(empty($oacs_spl_counter_color_setting)) { |
| 25 |
|
| 26 |
$oacs_spl_counter_color_setting = 'inherit'; |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
if(empty($oacs_spl_counter_padding_setting)) { |
| 31 |
|
| 32 |
$oacs_spl_counter_padding_setting = 'inherit'; |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
if (!empty($oacs_spl_counter_size_setting) OR !empty($oacs_spl_counter_color_setting) OR !empty($oacs_spl_counter_padding_setting)) { $oacs_spl_style_tag = ' style="'; $oacs_spl_style_tag_end = '">'; } else { $oacs_spl_style_tag = '' ;} |
| 37 |
|
| 38 |
|
| 39 |
if ( is_numeric( $like_count ) && $like_count > 0 ) { |
| 40 |
|
| 41 |
$number = $this->oacs_spl_format_count( $like_count ); |
| 42 |
|
| 43 |
} else { |
| 44 |
|
| 45 |
$number = $this->oacs_spl_format_count( $like_count ); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
$count = '<div class="oacs-spl-counter"' . |
| 50 |
$oacs_spl_style_tag . |
| 51 |
(!empty($oacs_spl_counter_color_setting) ? 'color: ' . esc_attr($oacs_spl_counter_color_setting) . '; ': '') . |
| 52 |
(!empty($oacs_spl_counter_size_setting) ? 'font-size: ' . esc_attr($oacs_spl_counter_size_setting) . '; ': '') . |
| 53 |
(!empty($oacs_spl_counter_padding_setting) ? 'padding: ' . esc_attr($oacs_spl_counter_padding_setting) . ';': '') . |
| 54 |
$oacs_spl_style_tag_end . esc_html($number) . '</div>'; |
| 55 |
|
| 56 |
if ($oacs_spl_counter_active_setting) {return apply_filters('oacs_spl_counter', $count);} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* To format the button count, |
| 61 |
* appending "K" if one thousand or greater, |
| 62 |
* "M" if one million or greater, |
| 63 |
* and "B" if one billion or greater (unlikely). |
| 64 |
* $precision = how many decimal points to display (1.25K) |
| 65 |
*/ |
| 66 |
public function oacs_spl_format_count( $number ) |
| 67 |
{ |
| 68 |
$precision = 2; |
| 69 |
if ( $number >= 1000 && $number < 1000000 ) { |
| 70 |
$formatted = number_format( $number/1000, $precision ).'K'; |
| 71 |
} elseif ( $number >= 1000000 && $number < 1000000000 ) { |
| 72 |
$formatted = number_format( $number/1000000, $precision ).'M'; |
| 73 |
} elseif ( $number >= 1000000000 ) { |
| 74 |
$formatted = number_format( $number/1000000000, $precision ).'B'; |
| 75 |
} else { |
| 76 |
$formatted = $number; // Number is less than 1000 |
| 77 |
} |
| 78 |
// Don't display zero decimals. |
| 79 |
$formatted = str_replace( '.00', '', $formatted ); |
| 80 |
return apply_filters('oacs_spl_formatted_number', $formatted); |
| 81 |
} |
| 82 |
} |