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