| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_ACB_Reaction' ) ) : |
| 7 |
|
| 8 |
class WPSC_ACB_Reaction { |
| 9 |
|
| 10 |
// Do not change the values of these constants as they are stored in the database. |
| 11 |
public const HAPPY = 1; |
| 12 |
public const UNHAPPY = 2; |
| 13 |
|
| 14 |
/** |
| 15 |
* Reaction labels |
| 16 |
* |
| 17 |
* @var array<int, string> |
| 18 |
*/ |
| 19 |
private static $labels = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* Return all labels. |
| 23 |
* |
| 24 |
* @return array<int, string> |
| 25 |
*/ |
| 26 |
public static function get_labels(): array { |
| 27 |
|
| 28 |
if ( self::$labels === null ) { |
| 29 |
self::$labels = array( |
| 30 |
self::HAPPY => esc_attr__( 'Happy', 'supportcandy' ), |
| 31 |
self::UNHAPPY => esc_attr__( 'Unhappy', 'supportcandy' ), |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
return self::$labels; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get label by reaction. |
| 40 |
* |
| 41 |
* @param mixed $reaction Reaction value - callers may pass it straight from |
| 42 |
* $_POST (a string, e.g. via sanitize_text_field()), so |
| 43 |
* this must not assume it's already an int. |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public static function get_label( $reaction ): string { |
| 47 |
|
| 48 |
$labels = self::get_labels(); |
| 49 |
|
| 50 |
return $labels[ (int) $reaction ] ?? esc_attr__( 'Unknown', 'supportcandy' ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get all valid reaction values. |
| 55 |
* |
| 56 |
* @return array<int> |
| 57 |
*/ |
| 58 |
public static function values(): array { |
| 59 |
return array_keys( self::get_labels() ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Check if reaction is valid. |
| 64 |
* |
| 65 |
* @param mixed $reaction Reaction value - callers may pass it straight from |
| 66 |
* $_POST (a string, e.g. via sanitize_text_field()), so |
| 67 |
* this must not assume it's already an int: a strict |
| 68 |
* int-typed parameter would fatal with a TypeError on a |
| 69 |
* non-numeric string instead of just returning false. |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
public static function is_valid( $reaction ): bool { |
| 73 |
|
| 74 |
if ( ! is_numeric( $reaction ) ) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
return in_array( (int) $reaction, self::values(), true ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get formatted HTML badge for reaction. |
| 82 |
* |
| 83 |
* @param mixed $reaction Reaction value - see is_valid(). |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
public static function get_badge( $reaction ): string { |
| 87 |
|
| 88 |
if ( ! self::is_valid( $reaction ) ) { |
| 89 |
return ''; |
| 90 |
} |
| 91 |
$reaction = (int) $reaction; |
| 92 |
|
| 93 |
$classes = array( |
| 94 |
self::HAPPY => 'happy', |
| 95 |
self::UNHAPPY => 'unhappy', |
| 96 |
); |
| 97 |
|
| 98 |
$class = isset( $classes[ $reaction ] ) ? $classes[ $reaction ] : 'unhappy'; |
| 99 |
|
| 100 |
return sprintf( |
| 101 |
'<span class="wpsc-acb-reaction wpsc-acb-reaction-%1$s">%2$s</span>', |
| 102 |
esc_attr( $class ), |
| 103 |
esc_html( self::get_label( $reaction ) ) |
| 104 |
); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
endif; |
| 109 |
|