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