Abstract_Repository.php
3 years ago
Ad_Repository.php
3 years ago
Group_Repository.php
2 years ago
Placement_Type.php
2 years ago
Placement_Type_Options.php
3 years ago
Group_Repository.php
47 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Group Repository class. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | */ |
| 8 | |
| 9 | namespace Advanced_Ads; |
| 10 | |
| 11 | use WP_Term; |
| 12 | |
| 13 | /** |
| 14 | * Group Repository/Factory class. |
| 15 | * Ensures every ad is only set-up once and the same instance is re-used within one request. |
| 16 | */ |
| 17 | class Group_Repository extends Abstract_Repository { |
| 18 | /** |
| 19 | * Array to hold the \Advanced_Ads_Group objects, indexed by the term. |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | protected static $repo = []; |
| 24 | |
| 25 | /** |
| 26 | * Get the ad object from the repository. Create and add it, if it doesn't exist. |
| 27 | * If the passed id is not an ad, return the created ad object without adding it to the repository. |
| 28 | * This behavior prevents breaking changes. |
| 29 | * |
| 30 | * @param int|WP_Term $term The term to look for. |
| 31 | * |
| 32 | * @return \Advanced_Ads_Group |
| 33 | */ |
| 34 | public static function get( $term ): \Advanced_Ads_Group { |
| 35 | $id = $term->term_id ?? $term; |
| 36 | if ( ! self::has( $id ) ) { |
| 37 | $group = new \Advanced_Ads_Group( $term ); |
| 38 | if ( ! $group->is_group ) { |
| 39 | return $group; |
| 40 | } |
| 41 | self::$repo[ $id ] = $group; |
| 42 | } |
| 43 | |
| 44 | return self::$repo[ $id ]; |
| 45 | } |
| 46 | } |
| 47 |