Advanced_Ads_Modal.php
2 years ago
EDD_SL_Plugin_Updater.php
2 years ago
ad-ajax.php
2 years ago
ad-debug.php
1 year ago
ad-expiration.php
3 years ago
ad-health-notices.php
2 years ago
ad-model.php
2 years ago
ad-select.php
3 years ago
ad.php
2 years ago
ad_ajax_callbacks.php
2 years ago
ad_group.php
1 year ago
ad_placements.php
2 years ago
ad_type_abstract.php
2 years ago
ad_type_content.php
2 years ago
ad_type_dummy.php
2 years ago
ad_type_group.php
2 years ago
ad_type_image.php
2 years ago
ad_type_plain.php
2 years ago
checks.php
2 years ago
class-translation-promo.php
2 years ago
compatibility.php
2 years ago
display-conditions.php
2 years ago
filesystem.php
2 years ago
frontend_checks.php
1 year ago
in-content-injector.php
1 year ago
inline-css.php
2 years ago
plugin.php
1 year ago
upgrades.php
1 year ago
utils.php
3 years ago
visitor-conditions.php
2 years ago
widget.php
2 years ago
ad-model.php
165 lines
| 1 | <?php |
| 2 | |
| 3 | use AdvancedAds\Entities; |
| 4 | |
| 5 | /** |
| 6 | * Advanced Ads Model |
| 7 | */ |
| 8 | class Advanced_Ads_Model { |
| 9 | |
| 10 | /** |
| 11 | * Cache group for WP Object Cache |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | const OBJECT_CACHE_GROUP = 'advanced-ads'; |
| 16 | |
| 17 | /** |
| 18 | * Default time-to-live for WP Object Cache |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | const OBJECT_CACHE_TTL = 720; // 12 Minutes |
| 23 | |
| 24 | /** |
| 25 | * WordPress database object. |
| 26 | * |
| 27 | * @var wpdb |
| 28 | */ |
| 29 | protected $db; |
| 30 | |
| 31 | /** |
| 32 | * General ad conditions. |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | protected $ad_conditions; |
| 37 | |
| 38 | /** |
| 39 | * Placements |
| 40 | * |
| 41 | * @var array |
| 42 | */ |
| 43 | protected $ad_placements; |
| 44 | |
| 45 | /** |
| 46 | * Advanced_Ads_Model constructor. |
| 47 | * |
| 48 | * @param wpdb $wpdb WordPress database access. |
| 49 | */ |
| 50 | public function __construct( wpdb $wpdb ) { |
| 51 | $this->db = $wpdb; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Load ad conditions. |
| 56 | * |
| 57 | * @return array |
| 58 | */ |
| 59 | public function get_ad_conditions() { |
| 60 | if ( ! isset( self::$ad_conditions ) ) { |
| 61 | $this->ad_conditions = include ADVADS_ABSPATH . 'includes/array_ad_conditions.php'; |
| 62 | } |
| 63 | |
| 64 | return $this->ad_conditions; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get all blog ids of blogs in the current network that are: |
| 69 | * - not archived |
| 70 | * - not spam |
| 71 | * - not deleted |
| 72 | * |
| 73 | * @since 1.0.0 |
| 74 | * @return array|false The blog ids, false if no matches. |
| 75 | */ |
| 76 | public function get_blog_ids() { |
| 77 | // get an array of blog ids. |
| 78 | $sql = "SELECT blog_id FROM $this->db->blogs WHERE archived = '0' AND spam = '0' AND deleted = '0'"; |
| 79 | |
| 80 | return $this->db->get_col( $sql ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Load all ads based on WP_Query conditions |
| 85 | * |
| 86 | * @since 1.1.0 |
| 87 | * @param array $args WP_Query arguments that are more specific that default. |
| 88 | * @return array $ads array with post objects. |
| 89 | */ |
| 90 | public function get_ads( $args = [] ) { |
| 91 | $args = wp_parse_args( $args, [ |
| 92 | 'posts_per_page' => -1, |
| 93 | 'post_status' => [ 'publish', 'future' ], |
| 94 | ] ); |
| 95 | // add default WP_Query arguments. |
| 96 | $args['post_type'] = Entities::POST_TYPE_AD; |
| 97 | |
| 98 | return ( new WP_Query( $args ) )->posts; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Load all ad groups |
| 103 | * |
| 104 | * @param iterable $args array with options. |
| 105 | * |
| 106 | * @return Advanced_Ads_Group[] array with ad groups |
| 107 | * @since 1.1.0 |
| 108 | * @link http://codex.wordpress.org/Function_Reference/get_terms |
| 109 | */ |
| 110 | public function get_ad_groups( iterable $args = [] ) { |
| 111 | $args['hide_empty'] = $args['hide_empty'] ?? false; |
| 112 | unset( $args['fields'] ); |
| 113 | |
| 114 | return array_map( |
| 115 | static function( WP_Term $term ) { |
| 116 | return \Advanced_Ads\Group_Repository::get( $term ); |
| 117 | }, |
| 118 | get_terms( Entities::TAXONOMY_AD_GROUP, $args ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the array with ad placements |
| 124 | * |
| 125 | * @since 1.1.0 |
| 126 | * @return array $ad_placements |
| 127 | */ |
| 128 | public function get_ad_placements_array() { |
| 129 | |
| 130 | if ( ! isset( $this->ad_placements ) ) { |
| 131 | $this->ad_placements = get_option( 'advads-ads-placements', [] ); |
| 132 | |
| 133 | // load default array if not saved yet. |
| 134 | if ( ! is_array( $this->ad_placements ) ) { |
| 135 | $this->ad_placements = []; |
| 136 | } |
| 137 | |
| 138 | $this->ad_placements = apply_filters( 'advanced-ads-get-ad-placements-array', $this->ad_placements ); |
| 139 | } |
| 140 | |
| 141 | return $this->ad_placements; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Reset placement array. |
| 146 | */ |
| 147 | public function reset_placement_array() { |
| 148 | $this->ad_placements = null; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Update the array with ad placements |
| 153 | * |
| 154 | * @param array $ad_placements array with placements. |
| 155 | */ |
| 156 | public function update_ad_placements_array( $ad_placements ) { |
| 157 | $ad_placements = Advanced_Ads_Placements::sort( $ad_placements, 'type' ); |
| 158 | update_option( 'advads-ads-placements', $ad_placements ); |
| 159 | $this->ad_placements = $ad_placements; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | } |
| 165 |