EDD_SL_Plugin_Updater.php
4 years ago
ad-ajax.php
5 years ago
ad-debug.php
6 years ago
ad-expiration.php
4 years ago
ad-health-notices.php
4 years ago
ad-model.php
5 years ago
ad-select.php
10 years ago
ad.php
4 years ago
ad_ajax_callbacks.php
5 years ago
ad_group.php
4 years ago
ad_placements.php
4 years ago
ad_type_abstract.php
5 years ago
ad_type_content.php
5 years ago
ad_type_dummy.php
5 years ago
ad_type_group.php
5 years ago
ad_type_image.php
5 years ago
ad_type_plain.php
4 years ago
checks.php
4 years ago
compatibility.php
4 years ago
display-conditions.php
4 years ago
filesystem.php
8 years ago
frontend-notices.php
6 years ago
frontend_checks.php
4 years ago
in-content-injector.php
4 years ago
inline-css.php
5 years ago
plugin.php
4 years ago
upgrades.php
6 years ago
utils.php
4 years ago
visitor-conditions.php
4 years ago
widget.php
4 years ago
ad-model.php
156 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 = array() ) { |
| 89 | // add default WP_Query arguments. |
| 90 | $args['post_type'] = Advanced_Ads::POST_TYPE_SLUG; |
| 91 | $args['posts_per_page'] = -1; |
| 92 | if ( empty( $args['post_status'] ) ) { |
| 93 | $args['post_status'] = array( 'publish', 'future' ); } |
| 94 | $ads = new WP_Query( $args ); |
| 95 | |
| 96 | return $ads->posts; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Load all ad groups |
| 101 | * |
| 102 | * @since 1.1.0 |
| 103 | * @param array $args array with options. |
| 104 | * @return array array with ad groups |
| 105 | * @link http://codex.wordpress.org/Function_Reference/get_terms |
| 106 | */ |
| 107 | public function get_ad_groups( $args = array() ) { |
| 108 | $args['hide_empty'] = isset( $args['hide_empty'] ) ? $args['hide_empty'] : false; // display groups without any ads. |
| 109 | |
| 110 | return get_terms( Advanced_Ads::AD_GROUP_TAXONOMY, $args ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the array with ad placements |
| 115 | * |
| 116 | * @since 1.1.0 |
| 117 | * @return array $ad_placements |
| 118 | */ |
| 119 | public function get_ad_placements_array() { |
| 120 | |
| 121 | if ( ! isset( $this->ad_placements ) ) { |
| 122 | $this->ad_placements = get_option( 'advads-ads-placements', array() ); |
| 123 | |
| 124 | // load default array if not saved yet. |
| 125 | if ( ! is_array( $this->ad_placements ) ) { |
| 126 | $this->ad_placements = array(); |
| 127 | } |
| 128 | |
| 129 | $this->ad_placements = apply_filters( 'advanced-ads-get-ad-placements-array', $this->ad_placements ); |
| 130 | } |
| 131 | |
| 132 | return $this->ad_placements; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Reset placement array. |
| 137 | */ |
| 138 | public function reset_placement_array() { |
| 139 | $this->ad_placements = null; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Update the array with ad placements |
| 144 | * |
| 145 | * @param array $ad_placements array with placements. |
| 146 | */ |
| 147 | public function update_ad_placements_array( $ad_placements ) { |
| 148 | $ad_placements = Advanced_Ads_Placements::sort( $ad_placements, 'type' ); |
| 149 | update_option( 'advads-ads-placements', $ad_placements ); |
| 150 | $this->ad_placements = $ad_placements; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | } |
| 156 |