class-interest-category.php
9 years ago
class-list.php
8 years ago
class-merge-field.php
9 years ago
class-subscriber.php
9 years ago
class-interest-category.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class MC4WP_MailChimp_Interests_Category |
| 5 | * |
| 6 | * Represents an Interest Category in MailChimp. |
| 7 | * |
| 8 | * @access public |
| 9 | * @since 4.0 |
| 10 | */ |
| 11 | class MC4WP_MailChimp_Interest_Category { |
| 12 | |
| 13 | /** |
| 14 | * @var int |
| 15 | */ |
| 16 | public $id = 0; |
| 17 | |
| 18 | /** |
| 19 | * @var string |
| 20 | */ |
| 21 | public $name = ''; |
| 22 | |
| 23 | /** |
| 24 | * @var string |
| 25 | */ |
| 26 | public $field_type = ''; |
| 27 | |
| 28 | /** |
| 29 | * @var string[] |
| 30 | */ |
| 31 | public $interests = array(); |
| 32 | |
| 33 | /** |
| 34 | * @param int $id |
| 35 | * @param string $name |
| 36 | * @param string $field_type |
| 37 | * @param array $interests |
| 38 | */ |
| 39 | public function __construct( $id, $name, $field_type, $interests = array() ) { |
| 40 | $this->id = $id; |
| 41 | $this->name = $name; |
| 42 | $this->field_type = $field_type; |
| 43 | $this->interests = $interests; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param string $name |
| 48 | * |
| 49 | * @return array|string[] |
| 50 | */ |
| 51 | public function __get( $name ) { |
| 52 | // for backwards compatibility with v3.x, channel these properties to their new names |
| 53 | if( $name === 'groups' ) { |
| 54 | return $this->interests; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param object $data |
| 60 | * |
| 61 | * @return MC4WP_MailChimp_Interest_Category |
| 62 | */ |
| 63 | public static function from_data( $data ) { |
| 64 | $instance = new self( $data->id, $data->title, $data->type ); |
| 65 | return $instance; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @since 4.0 |
| 70 | * |
| 71 | * @param string $interest_id |
| 72 | * @return string |
| 73 | * |
| 74 | * @throws Exception |
| 75 | */ |
| 76 | public function get_interest_name_by_id( $interest_id ) { |
| 77 | foreach( $this->interests as $id => $name ) { |
| 78 | if( $id != $interest_id ) { |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | return $name; |
| 83 | } |
| 84 | |
| 85 | throw new Exception( sprintf( 'No interest with ID %s', $interest_id ) ); |
| 86 | } |
| 87 | } |