assets
11 years ago
includes
12 years ago
views
12 years ago
class-advanced-ads.php
11 years ago
functions.php
11 years ago
class-advanced-ads.php
763 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Advanced Ads. |
| 5 | * |
| 6 | * @package Advanced_Ads_Admin |
| 7 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 8 | * @license GPL-2.0+ |
| 9 | * @link http://webgilde.com |
| 10 | * @copyright 2013-2014 Thomas Maier, webgilde GmbH |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * Plugin class. This class should ideally be used to work with the |
| 15 | * public-facing side of the WordPress site. |
| 16 | * |
| 17 | * @package Advanced_Ads |
| 18 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 19 | */ |
| 20 | class Advanced_Ads { |
| 21 | /** |
| 22 | * Plugin version, used for cache-busting of style and script file references. |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | * @var string |
| 26 | */ |
| 27 | |
| 28 | const VERSION = '1.3.1'; |
| 29 | |
| 30 | /** |
| 31 | * post type slug |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | * @var string |
| 35 | */ |
| 36 | const POST_TYPE_SLUG = 'advanced_ads'; |
| 37 | |
| 38 | /** |
| 39 | * ad group slug |
| 40 | * |
| 41 | * @since 1.0.0 |
| 42 | * @var string |
| 43 | */ |
| 44 | const AD_GROUP_TAXONOMY = 'advanced_ads_groups'; |
| 45 | |
| 46 | /** |
| 47 | * general plugin slug |
| 48 | * |
| 49 | * DEPRECATED: The variable name is used as the text domain when internationalizing strings |
| 50 | * of text. Its value should match the Text Domain file header in the main |
| 51 | * plugin file. |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | * @var string |
| 55 | */ |
| 56 | protected $plugin_slug = 'advanced-ads'; |
| 57 | |
| 58 | /** |
| 59 | * Instance of this class. |
| 60 | * |
| 61 | * @since 1.0.0 |
| 62 | * @var object |
| 63 | */ |
| 64 | protected static $instance = null; |
| 65 | |
| 66 | /** |
| 67 | * ad types |
| 68 | */ |
| 69 | public $ad_types = array(); |
| 70 | |
| 71 | /** |
| 72 | * plugin options |
| 73 | * |
| 74 | * @since 1.0.1 |
| 75 | * @var array (if loaded) |
| 76 | */ |
| 77 | protected $options = false; |
| 78 | |
| 79 | /** |
| 80 | * Initialize the plugin by setting localization and loading public scripts |
| 81 | * and styles. |
| 82 | * |
| 83 | * @since 1.0.0 |
| 84 | */ |
| 85 | private function __construct() { |
| 86 | |
| 87 | // Load plugin text domain |
| 88 | add_action('plugins_loaded', array($this, 'load_plugin_textdomain')); |
| 89 | |
| 90 | // activate plugin when new blog is added on multisites |
| 91 | add_action('wpmu_new_blog', array($this, 'activate_new_site')); |
| 92 | |
| 93 | // Load public-facing style sheet and JavaScript. |
| 94 | add_action('wp_enqueue_scripts', array($this, 'enqueue_styles')); |
| 95 | add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts')); |
| 96 | |
| 97 | // initialize plugin specific functions |
| 98 | add_action( 'init', array( $this, 'init' ) ); |
| 99 | register_activation_hook(__FILE__, array($this,'post_types_rewrite_flush')); |
| 100 | |
| 101 | // add short codes |
| 102 | add_shortcode('the_ad', array($this, 'shortcode_display_ad')); |
| 103 | add_shortcode('the_ad_group', array($this, 'shortcode_display_ad_group')); |
| 104 | add_shortcode('the_ad_placement', array($this, 'shortcode_display_ad_placement')); |
| 105 | // remove default ad group menu item |
| 106 | add_action('admin_menu', array($this, 'remove_taxonomy_menu_item')); |
| 107 | |
| 108 | // setup default ad types |
| 109 | add_filter('advanced-ads-ad-types', array($this, 'setup_default_ad_types')); |
| 110 | |
| 111 | // register hooks and filters for auto ad injection |
| 112 | add_action('wp_head', array($this, 'inject_header'), 20); |
| 113 | add_action('wp_footer', array($this, 'inject_footer'), 20); |
| 114 | add_filter('the_content', array($this, 'inject_content'), 100); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * init / load plugin specific functions and settings |
| 119 | * |
| 120 | * @since 1.0.0 |
| 121 | */ |
| 122 | public function init(){ |
| 123 | // load ad post types |
| 124 | $this->create_post_types(); |
| 125 | // set ad types array |
| 126 | $this->set_ad_types(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Return the plugin slug. |
| 131 | * |
| 132 | * @since 1.0.0 |
| 133 | * @return Plugin slug variable. |
| 134 | */ |
| 135 | public function get_plugin_slug() { |
| 136 | return $this->plugin_slug; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Return an instance of this class. |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | * @return object A single instance of this class. |
| 144 | */ |
| 145 | public static function get_instance() { |
| 146 | |
| 147 | // If the single instance hasn't been set, set it now. |
| 148 | if (null == self::$instance) { |
| 149 | self::$instance = new self; |
| 150 | } |
| 151 | |
| 152 | return self::$instance; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Fired when the plugin is activated. |
| 157 | * |
| 158 | * @since 1.0.0 |
| 159 | * @param boolean $network_wide True if WPMU superadmin uses |
| 160 | * "Network Activate" action, false if |
| 161 | * WPMU is disabled or plugin is |
| 162 | * activated on an individual blog. |
| 163 | */ |
| 164 | public static function activate($network_wide) { |
| 165 | |
| 166 | if (function_exists('is_multisite') && is_multisite()) { |
| 167 | |
| 168 | if ($network_wide) { |
| 169 | |
| 170 | // Get all blog ids |
| 171 | $blog_ids = self::get_blog_ids(); |
| 172 | |
| 173 | foreach ($blog_ids as $blog_id) { |
| 174 | |
| 175 | switch_to_blog($blog_id); |
| 176 | self::single_activate(); |
| 177 | } |
| 178 | |
| 179 | restore_current_blog(); |
| 180 | } else { |
| 181 | self::single_activate(); |
| 182 | } |
| 183 | } else { |
| 184 | self::single_activate(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Fired when the plugin is deactivated. |
| 190 | * |
| 191 | * @since 1.0.0 |
| 192 | * @param boolean $network_wide |
| 193 | * |
| 194 | * True if WPMU superadmin uses |
| 195 | * "Network Deactivate" action, false if |
| 196 | * WPMU is disabled or plugin is |
| 197 | * deactivated on an individual blog. |
| 198 | */ |
| 199 | public static function deactivate($network_wide) { |
| 200 | |
| 201 | if (function_exists('is_multisite') && is_multisite()) { |
| 202 | |
| 203 | if ($network_wide) { |
| 204 | |
| 205 | // Get all blog ids |
| 206 | $blog_ids = self::get_blog_ids(); |
| 207 | |
| 208 | foreach ($blog_ids as $blog_id) { |
| 209 | |
| 210 | switch_to_blog($blog_id); |
| 211 | self::single_deactivate(); |
| 212 | } |
| 213 | |
| 214 | restore_current_blog(); |
| 215 | } else { |
| 216 | self::single_deactivate(); |
| 217 | } |
| 218 | } else { |
| 219 | self::single_deactivate(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Fired when a new site is activated with a WPMU environment. |
| 225 | * |
| 226 | * @since 1.0.0 |
| 227 | * @param int $blog_id ID of the new blog. |
| 228 | */ |
| 229 | public function activate_new_site($blog_id) { |
| 230 | |
| 231 | if (1 !== did_action('wpmu_new_blog')) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | switch_to_blog($blog_id); |
| 236 | self::single_activate(); |
| 237 | restore_current_blog(); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get all blog ids of blogs in the current network that are: |
| 242 | * - not archived |
| 243 | * - not spam |
| 244 | * - not deleted |
| 245 | * |
| 246 | * @since 1.0.0 |
| 247 | * @return array|false The blog ids, false if no matches. |
| 248 | */ |
| 249 | private static function get_blog_ids() { |
| 250 | |
| 251 | global $wpdb; |
| 252 | |
| 253 | // get an array of blog ids |
| 254 | $sql = "SELECT blog_id FROM $wpdb->blogs |
| 255 | WHERE archived = '0' AND spam = '0' |
| 256 | AND deleted = '0'"; |
| 257 | |
| 258 | return $wpdb->get_col($sql); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Fired for each blog when the plugin is activated. |
| 263 | * |
| 264 | * @since 1.0.0 |
| 265 | */ |
| 266 | private static function single_activate() { |
| 267 | // @TODO: Define activation functionality here |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Fired for each blog when the plugin is deactivated. |
| 272 | * |
| 273 | * @since 1.0.0 |
| 274 | */ |
| 275 | private static function single_deactivate() { |
| 276 | // @TODO: Define deactivation functionality here |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Load the plugin text domain for translation. |
| 281 | * |
| 282 | * @since 1.0.0 |
| 283 | */ |
| 284 | public function load_plugin_textdomain() { |
| 285 | |
| 286 | // $locale = apply_filters('advanced-ads-plugin-locale', get_locale(), $domain); |
| 287 | |
| 288 | load_plugin_textdomain(ADVADS_SLUG, false, 'advanced-ads/languages'); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Register and enqueue public-facing style sheet. |
| 293 | * |
| 294 | * @since 1.0.0 |
| 295 | */ |
| 296 | public function enqueue_styles() { |
| 297 | wp_enqueue_style($this->plugin_slug . '-plugin-styles', plugins_url('assets/css/public.css', __FILE__), array(), self::VERSION); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Register and enqueues public-facing JavaScript files. |
| 302 | * |
| 303 | * @since 1.0.0 |
| 304 | */ |
| 305 | public function enqueue_scripts() { |
| 306 | // wp_enqueue_script($this->plugin_slug . '-plugin-script', plugins_url('assets/js/public.js', __FILE__), array('jquery'), self::VERSION); |
| 307 | $options = $this->options(); |
| 308 | if(!empty($options['advanced-js'])){ |
| 309 | wp_enqueue_script($this->plugin_slug . '-advanced-js', plugins_url('assets/js/advanced.js', __FILE__), array('jquery'), self::VERSION); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * define ad types with their options |
| 315 | * |
| 316 | * name => publically readable name |
| 317 | * description => publically readable description |
| 318 | * editor => kind of editor: text (normal text field), content (WP content field), none (no content field) |
| 319 | * will display text field, if left empty |
| 320 | * |
| 321 | * @since 1.0.0 |
| 322 | */ |
| 323 | public function set_ad_types() { |
| 324 | |
| 325 | /** |
| 326 | * load default ad type files |
| 327 | * custom ad types can also be loaded in your own plugin or functions.php |
| 328 | */ |
| 329 | $types = array(); |
| 330 | |
| 331 | /** |
| 332 | * developers can add new ad types using this filter |
| 333 | * see classes/ad-type-content.php for an example for an ad type and usage of this filter |
| 334 | */ |
| 335 | $this->ad_types = apply_filters('advanced-ads-ad-types', $types); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * add plain and content ad types to the default ads of the plugin using a filter |
| 340 | * |
| 341 | * @since 1.0.0 |
| 342 | * |
| 343 | */ |
| 344 | function setup_default_ad_types($types){ |
| 345 | $types['plain'] = new Advads_Ad_Type_Plain(); /* plain text and php code */ |
| 346 | $types['content'] = new Advads_Ad_Type_Content(); /* rich content editor */ |
| 347 | return $types; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * get an array with all ads by conditions |
| 352 | * = list possible ads for each condition |
| 353 | * |
| 354 | * @since 1.0.0 |
| 355 | * @return arr $ads_by_conditions |
| 356 | * @todo make static |
| 357 | */ |
| 358 | public function get_ads_by_conditions_array(){ |
| 359 | |
| 360 | $ads_by_conditions = get_option('advads-ads-by-conditions', array()); |
| 361 | // load default array if no conditions saved yet |
| 362 | if(!is_array($ads_by_conditions)){ |
| 363 | $ads_by_conditions = array(); |
| 364 | } |
| 365 | |
| 366 | return $ads_by_conditions; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * get the array with global ad injections |
| 371 | * |
| 372 | * @since 1.1.0 |
| 373 | * @return arr $ad_injections |
| 374 | * @todo make static |
| 375 | */ |
| 376 | public function get_ad_injections_array(){ |
| 377 | |
| 378 | $ad_injections = get_option('advads-ads-injections', array()); |
| 379 | // load default array if not saved yet |
| 380 | if(!is_array($ad_injections)){ |
| 381 | $ad_injections = array(); |
| 382 | } |
| 383 | |
| 384 | return $ad_injections; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * get the array with ad placements |
| 389 | * |
| 390 | * @since 1.1.0 |
| 391 | * @return arr $ad_placements |
| 392 | */ |
| 393 | static public function get_ad_placements_array(){ |
| 394 | |
| 395 | $ad_placements = get_option('advads-ads-placements', array()); |
| 396 | // load default array if not saved yet |
| 397 | if(!is_array($ad_placements)){ |
| 398 | $ad_placements = array(); |
| 399 | } |
| 400 | |
| 401 | return $ad_placements; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * shortcode to include ad in frontend |
| 406 | * |
| 407 | * @since 1.0.0 |
| 408 | * @param arr $atts |
| 409 | */ |
| 410 | public function shortcode_display_ad($atts){ |
| 411 | extract( shortcode_atts( array( |
| 412 | 'id' => 0, |
| 413 | ), $atts ) ); |
| 414 | |
| 415 | // use the public available function here |
| 416 | return get_ad($id); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * shortcode to include ad from an ad group in frontend |
| 421 | * |
| 422 | * @since 1.0.0 |
| 423 | * @param arr $atts |
| 424 | */ |
| 425 | public function shortcode_display_ad_group($atts){ |
| 426 | extract( shortcode_atts( array( |
| 427 | 'id' => 0, |
| 428 | ), $atts ) ); |
| 429 | |
| 430 | // use the public available function here |
| 431 | return get_ad_group($id); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * shortcode to display content of an ad placement in frontend |
| 436 | * |
| 437 | * @since 1.1.0 |
| 438 | * @param arr $atts |
| 439 | */ |
| 440 | public function shortcode_display_ad_placement($atts){ |
| 441 | extract( shortcode_atts( array( |
| 442 | 'id' => '', |
| 443 | ), $atts ) ); |
| 444 | |
| 445 | // use the public available function here |
| 446 | return get_ad_placement($id); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Registers ad post type and group taxonomies |
| 451 | * |
| 452 | * @since 1.0.0 |
| 453 | */ |
| 454 | public function create_post_types() { |
| 455 | if (did_action('init') !== 1) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | // register ad group taxonomy |
| 460 | if(!taxonomy_exists(self::AD_GROUP_TAXONOMY)){ |
| 461 | $post_type_params = $this->get_group_taxonomy_params(); |
| 462 | register_taxonomy(self::AD_GROUP_TAXONOMY, array(self::POST_TYPE_SLUG), $post_type_params); |
| 463 | } |
| 464 | |
| 465 | // register ad post type |
| 466 | if (!post_type_exists(self::POST_TYPE_SLUG)) { |
| 467 | $post_type_params = $this->get_post_type_params(); |
| 468 | register_post_type(self::POST_TYPE_SLUG, $post_type_params); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Defines the parameters for the ad post type taxonomy |
| 474 | * |
| 475 | * @since 1.0.0 |
| 476 | * @return array |
| 477 | */ |
| 478 | protected function get_group_taxonomy_params(){ |
| 479 | $labels = array( |
| 480 | 'name' => _x('Ad Groups', 'ad group general name', ADVADS_SLUG), |
| 481 | 'singular_name' => _x('Ad Group', 'ad group singular name', ADVADS_SLUG), |
| 482 | 'search_items' => __('Search Ad Groups', ADVADS_SLUG), |
| 483 | 'all_items' => __('All Ad Groups', ADVADS_SLUG), |
| 484 | 'parent_item' => __('Parent Ad Groups', ADVADS_SLUG), |
| 485 | 'parent_item_colon' => __('Parent Ad Groups:', ADVADS_SLUG), |
| 486 | 'edit_item' => __('Edit Ad Group', ADVADS_SLUG), |
| 487 | 'update_item' => __('Update Ad Group', ADVADS_SLUG), |
| 488 | 'add_new_item' => __('Add New Ad Group', ADVADS_SLUG), |
| 489 | 'new_item_name' => __('New Ad Groups Name', ADVADS_SLUG), |
| 490 | 'menu_name' => __('Groups', ADVADS_SLUG), |
| 491 | 'not_found' => __('No Ad Group found', ADVADS_SLUG), |
| 492 | ); |
| 493 | |
| 494 | $args = array( |
| 495 | 'hierarchical' => true, |
| 496 | 'labels' => $labels, |
| 497 | 'show_ui' => true, |
| 498 | 'show_in_nav_menus' => false, |
| 499 | 'show_tagcloud' => false, |
| 500 | 'show_admin_column' => true, |
| 501 | 'query_var' => false, |
| 502 | 'rewrite' => false, |
| 503 | ); |
| 504 | |
| 505 | return $args; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Defines the parameters for the custom post type |
| 510 | * |
| 511 | * @since 1.0.0 |
| 512 | * @return array |
| 513 | */ |
| 514 | protected function get_post_type_params() { |
| 515 | $labels = array( |
| 516 | 'name' => __('Ads', ADVADS_SLUG), |
| 517 | 'singular_name' => __('Ad', ADVADS_SLUG), |
| 518 | 'add_new' => __('New Ad', ADVADS_SLUG), |
| 519 | 'add_new_item' => __('Add New Ad', ADVADS_SLUG), |
| 520 | 'edit' => __('Edit', ADVADS_SLUG), |
| 521 | 'edit_item' => __('Edit Ad', ADVADS_SLUG), |
| 522 | 'new_item' => __('New Ad', ADVADS_SLUG), |
| 523 | 'view' => __('View', ADVADS_SLUG), |
| 524 | 'view_item' => __('View the Ad', ADVADS_SLUG), |
| 525 | 'search_items' => __('Search Ads', ADVADS_SLUG), |
| 526 | 'not_found' => __('No Ads found', ADVADS_SLUG), |
| 527 | 'not_found_in_trash' => __('No Ads found in Trash', ADVADS_SLUG), |
| 528 | 'parent' => __('Parent Ad', ADVADS_SLUG), |
| 529 | ); |
| 530 | |
| 531 | $post_type_params = array( |
| 532 | 'labels' => $labels, |
| 533 | 'singular_label' => __('Ad', ADVADS_SLUG), |
| 534 | 'public' => false, |
| 535 | 'show_ui' => true, |
| 536 | 'show_in_menu' => false, |
| 537 | 'hierarchical' => false, |
| 538 | 'capability_type' => 'page', |
| 539 | 'has_archive' => false, |
| 540 | 'rewrite' => array('slug' => ADVADS_SLUG), |
| 541 | 'query_var' => true, |
| 542 | 'supports' => array('title'), |
| 543 | 'taxonomies' => array(self::AD_GROUP_TAXONOMY) |
| 544 | ); |
| 545 | |
| 546 | return apply_filters('advanced-ads-post-type-params', $post_type_params); |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * remove WP tag edit page for the ad group taxonomy |
| 551 | * needed, because we can’t remove it with `show_ui` without also removing the meta box |
| 552 | * |
| 553 | * @since 1.0.0 |
| 554 | */ |
| 555 | public function remove_taxonomy_menu_item() { |
| 556 | remove_submenu_page( 'edit.php?post_type=advanced_ads', 'edit-tags.php?taxonomy=advanced_ads_groups&post_type=advanced_ads' ); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * flush rewrites on plugin activation so permalinks for them work from the beginning on |
| 561 | * |
| 562 | * @since 1.0.0 |
| 563 | * @link http://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation |
| 564 | */ |
| 565 | public function post_types_rewrite_flush(){ |
| 566 | // load custom post type |
| 567 | $this->create_post_types(); |
| 568 | // flush rewrite rules |
| 569 | flush_rewrite_rules(); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * log error messages when debug is enabled |
| 574 | * |
| 575 | * @since 1.0.0 |
| 576 | * @link http://www.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/ |
| 577 | */ |
| 578 | public function log($message) { |
| 579 | if (WP_DEBUG === true) { |
| 580 | if (is_array($message) || is_object($message)) { |
| 581 | error_log('Advanced Ads Error following:', ADVADS_SLUG); |
| 582 | error_log(print_r($message, true)); |
| 583 | } else { |
| 584 | $message = sprintf(__('Advanced Ads Error: %s', ADVADS_SLUG), $message); |
| 585 | error_log($message); |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * return plugin options |
| 592 | * |
| 593 | * @since 1.0.1 |
| 594 | * @return arr $options |
| 595 | * @todo parse default options |
| 596 | */ |
| 597 | public function options(){ |
| 598 | if($this->options === false){ |
| 599 | $this->options = get_option(ADVADS_SLUG, array()); |
| 600 | } |
| 601 | |
| 602 | return $this->options; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * injected ad into header |
| 607 | * |
| 608 | * @since 1.1.0 |
| 609 | */ |
| 610 | public function inject_header(){ |
| 611 | $placements = get_option('advads-ads-placements', array()); |
| 612 | foreach($placements as $_placement_id => $_placement){ |
| 613 | if(isset($_placement['type']) && $_placement['type'] == 'header'){ |
| 614 | echo Advads_Ad_Placements::output($_placement_id); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* FROM HERE, THE CODE IS DEPRECATED – MOVE AUTO INJECTED ADS TO PLACEMENTS */ |
| 619 | // get information about injected ads |
| 620 | $injections = get_option('advads-ads-injections', array()); |
| 621 | if(isset($injections['header']) && is_array($injections['header'])){ |
| 622 | $ads = $injections['header']; |
| 623 | // randomize ads |
| 624 | shuffle($ads); |
| 625 | // check ads one by one for being able to be displayed on this spot |
| 626 | foreach ($ads as $_ad_id) { |
| 627 | // load the ad object |
| 628 | $ad = new Advads_Ad($_ad_id); |
| 629 | if ($ad->can_display()) { |
| 630 | // display the ad |
| 631 | echo $ad->output(); |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * injected ads into footer |
| 639 | * |
| 640 | * @since 1.1.0 |
| 641 | */ |
| 642 | public function inject_footer(){ |
| 643 | $placements = get_option('advads-ads-placements', array()); |
| 644 | foreach($placements as $_placement_id => $_placement){ |
| 645 | if(isset($_placement['type']) && $_placement['type'] == 'footer'){ |
| 646 | echo Advads_Ad_Placements::output($_placement_id); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /* FROM HERE, THE CODE IS DEPRECATED – MOVE AUTO INJECTED ADS TO PLACEMENTS */ |
| 651 | // get information about injected ads |
| 652 | $injections = get_option('advads-ads-injections', array()); |
| 653 | if(isset($injections['footer']) && is_array($injections['footer'])){ |
| 654 | $ads = $injections['footer']; |
| 655 | // randomize ads |
| 656 | shuffle($ads); |
| 657 | // check ads one by one for being able to be displayed on this spot |
| 658 | foreach ($ads as $_ad_id) { |
| 659 | // load the ad object |
| 660 | $ad = new Advads_Ad($_ad_id); |
| 661 | if ($ad->can_display()) { |
| 662 | // display the ad |
| 663 | echo $ad->output(); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * injected ad into content (before and after) |
| 671 | * displays ALL ads |
| 672 | * |
| 673 | * @since 1.1.0 |
| 674 | * @param str $content post content |
| 675 | */ |
| 676 | public function inject_content($content = ''){ |
| 677 | // run only on single pages |
| 678 | if(!is_singular(array('post', 'page'))) return $content; |
| 679 | |
| 680 | $placements = get_option('advads-ads-placements', array()); |
| 681 | foreach($placements as $_placement_id => $_placement){ |
| 682 | if(isset($_placement['type']) && $_placement['type'] == 'post_top'){ |
| 683 | $content = Advads_Ad_Placements::output($_placement_id) . $content; |
| 684 | } |
| 685 | if(isset($_placement['type']) && $_placement['type'] == 'post_bottom'){ |
| 686 | $content .= Advads_Ad_Placements::output($_placement_id); |
| 687 | } |
| 688 | if(isset($_placement['type']) && $_placement['type'] == 'post_content'){ |
| 689 | $content = Advads_Ad_Placements::inject_in_content($_placement_id, $_placement['options'], $content); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /* FROM HERE, THE CODE IS DEPRECATED – MOVE AUTO INJECTED ADS TO PLACEMENTS */ |
| 694 | // get information about injected ads |
| 695 | $injections = get_option('advads-ads-injections', array()); |
| 696 | |
| 697 | // display ad before the content |
| 698 | if(isset($injections['post_start']) && is_array($injections['post_start'])){ |
| 699 | $ads = $injections['post_start']; |
| 700 | // randomize ads |
| 701 | shuffle($ads); |
| 702 | // check ads one by one for being able to be displayed on this spot |
| 703 | foreach ($ads as $_ad_id) { |
| 704 | // load the ad object |
| 705 | $ad = new Advads_Ad($_ad_id); |
| 706 | if ($ad->can_display()) { |
| 707 | // display the ad |
| 708 | $content = $ad->output() . $content; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | // display ad after the content |
| 714 | if(isset($injections['post_end']) && is_array($injections['post_end'])){ |
| 715 | $ads = $injections['post_end']; |
| 716 | // randomize ads |
| 717 | shuffle($ads); |
| 718 | // check ads one by one for being able to be displayed on this spot |
| 719 | foreach ($ads as $_ad_id) { |
| 720 | // load the ad object |
| 721 | $ad = new Advads_Ad($_ad_id); |
| 722 | if ($ad->can_display()) { |
| 723 | // display the ad |
| 724 | $content .= $ad->output(); |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return $content; |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * load all ads based on WP_Query conditions |
| 734 | * |
| 735 | * @since 1.1.0 |
| 736 | * @param arr $args WP_Query arguments that are more specific that default |
| 737 | * @return arr $ads array with post objects |
| 738 | */ |
| 739 | static function get_ads($args = array()){ |
| 740 | // add default WP_Query arguments |
| 741 | $args['post_type'] = self::POST_TYPE_SLUG; |
| 742 | $args['posts_per_page'] = -1; |
| 743 | if(empty($args['post_status'])) $args['post_status'] = 'publish'; |
| 744 | |
| 745 | $ads = new WP_Query($args); |
| 746 | return $ads->posts; |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * load all ad groups |
| 751 | * |
| 752 | * @since 1.1.0 |
| 753 | * @return arr $groups array with ad groups |
| 754 | * @link http://codex.wordpress.org/Function_Reference/get_terms |
| 755 | */ |
| 756 | static function get_ad_groups(){ |
| 757 | $args = array( |
| 758 | 'hide_empty' => false // also display groups without any ads |
| 759 | ); |
| 760 | return get_terms(Advanced_Ads::AD_GROUP_TAXONOMY, $args); |
| 761 | } |
| 762 | } |
| 763 |