| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_ACB_Status' ) ) : |
| 7 |
|
| 8 |
class WPSC_ACB_Status { |
| 9 |
|
| 10 |
// Do not change the values of these constants as they are stored in the database. |
| 11 |
public const ACTIVE = 1; |
| 12 |
public const INACTIVE = 2; |
| 13 |
public const ABANDONED = 3; |
| 14 |
public const HANDOFF = 4; |
| 15 |
public const RESOLVED = 5; |
| 16 |
public const CLOSED = 6; |
| 17 |
|
| 18 |
/** |
| 19 |
* Status labels |
| 20 |
* |
| 21 |
* @var array<int, string> |
| 22 |
*/ |
| 23 |
private static $labels = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Return all labels. |
| 27 |
* |
| 28 |
* @return array<int, string> |
| 29 |
*/ |
| 30 |
public static function get_labels(): array { |
| 31 |
|
| 32 |
if ( self::$labels === null ) { |
| 33 |
self::$labels = array( |
| 34 |
self::ACTIVE => esc_attr__( 'Active', 'supportcandy' ), |
| 35 |
self::INACTIVE => esc_attr__( 'Inactive', 'supportcandy' ), |
| 36 |
self::ABANDONED => esc_attr__( 'Abandoned', 'supportcandy' ), |
| 37 |
self::HANDOFF => esc_attr__( 'Handoff', 'supportcandy' ), |
| 38 |
self::RESOLVED => esc_attr__( 'Resolved', 'supportcandy' ), |
| 39 |
self::CLOSED => esc_attr__( 'Closed', 'supportcandy' ), |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
return self::$labels; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get label by status. |
| 48 |
* |
| 49 |
* @param int $status Status value. |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public static function get_label( int $status ): string { |
| 53 |
|
| 54 |
$labels = self::get_labels(); |
| 55 |
|
| 56 |
return $labels[ $status ] ?? esc_attr__( 'Unknown', 'supportcandy' ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get all valid status values. |
| 61 |
* |
| 62 |
* @return array<int> |
| 63 |
*/ |
| 64 |
public static function values(): array { |
| 65 |
return array_keys( self::get_labels() ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get status value by key. |
| 70 |
* |
| 71 |
* @param string $key Status key. |
| 72 |
* @return int|null |
| 73 |
*/ |
| 74 |
public static function get_value_by_key( string $key ): ?int { |
| 75 |
|
| 76 |
$map = array( |
| 77 |
'active' => self::ACTIVE, |
| 78 |
'inactive' => self::INACTIVE, |
| 79 |
'abandoned' => self::ABANDONED, |
| 80 |
'handoff' => self::HANDOFF, |
| 81 |
'resolved' => self::RESOLVED, |
| 82 |
'closed' => self::CLOSED, |
| 83 |
); |
| 84 |
|
| 85 |
$key = sanitize_key( $key ); |
| 86 |
|
| 87 |
return $map[ $key ] ?? null; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Check if status is valid. |
| 92 |
* |
| 93 |
* @param int $status Status value. |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public static function is_valid( int $status ): bool { |
| 97 |
return in_array( $status, self::values(), true ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get formatted HTML badge for status. |
| 102 |
* |
| 103 |
* @param int $status Status. |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public static function get_badge( int $status ): string { |
| 107 |
|
| 108 |
$classes = array( |
| 109 |
self::ACTIVE => 'active', |
| 110 |
self::INACTIVE => 'inactive', |
| 111 |
self::ABANDONED => 'abandoned', |
| 112 |
self::HANDOFF => 'handoff', |
| 113 |
self::RESOLVED => 'resolved', |
| 114 |
self::CLOSED => 'closed', |
| 115 |
); |
| 116 |
|
| 117 |
$class = isset( $classes[ $status ] ) ? $classes[ $status ] : 'inactive'; |
| 118 |
|
| 119 |
return sprintf( |
| 120 |
'<span class="wpsc-acb-status wpsc-acb-status-%1$s">%2$s</span>', |
| 121 |
esc_attr( $class ), |
| 122 |
esc_html( self::get_label( $status ) ) |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
endif; |
| 128 |
|