class-ad-display-condition.php
1 year ago
class-ad-renderer.php
1 year ago
class-debug-ads.php
1 year ago
class-manager.php
1 year ago
class-scripts.php
11 months ago
class-stats.php
1 year ago
class-stats.php
75 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Frontend Stats. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Frontend; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Frontend Stats. |
| 16 | */ |
| 17 | class Stats { |
| 18 | |
| 19 | /** |
| 20 | * Array with ads currently delivered in the frontend |
| 21 | * |
| 22 | * @var array Ads already loaded in the frontend |
| 23 | */ |
| 24 | public $entities = []; |
| 25 | |
| 26 | /** |
| 27 | * Main instance |
| 28 | * |
| 29 | * Ensure only one instance is loaded or can be loaded. |
| 30 | * |
| 31 | * @return Stats |
| 32 | */ |
| 33 | public static function get() { |
| 34 | static $instance; |
| 35 | |
| 36 | if ( null === $instance ) { |
| 37 | $instance = new Stats(); |
| 38 | } |
| 39 | |
| 40 | return $instance; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Add an entity to the stats. |
| 45 | * |
| 46 | * @param string $type Entity type. |
| 47 | * @param string $id Entity id. |
| 48 | * @param string $title Entity title. |
| 49 | * @param string $parent_id Parent entity id. |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | public function add_entity( $type, $id, $title, $parent_id = false ): void { |
| 54 | if ( ! isset( $this->entities[ $id ] ) ) { |
| 55 | $this->entities[ $id ] = [ |
| 56 | 'type' => $type, |
| 57 | 'id' => $id, |
| 58 | 'title' => $title, |
| 59 | 'count' => 0, |
| 60 | 'childs' => [], |
| 61 | ]; |
| 62 | } |
| 63 | |
| 64 | if ( ! $parent_id ) { |
| 65 | ++$this->entities[ $id ]['count']; |
| 66 | } else { |
| 67 | $this->entities[ $parent_id ]['childs'][ $id ] = [ |
| 68 | 'type' => $type, |
| 69 | 'id' => $id, |
| 70 | 'title' => $title, |
| 71 | ]; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 |