ad.php
11 years ago
ad_ajax_callbacks.php
11 years ago
ad_group.php
11 years ago
ad_placements.php
11 years ago
ad_type_abstract.php
12 years ago
ad_type_content.php
12 years ago
ad_type_plain.php
12 years ago
widget.php
11 years ago
ad.php
760 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Advanced Ads. |
| 5 | * |
| 6 | * @package Advads_Ad |
| 7 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 8 | * @license GPL-2.0+ |
| 9 | * @link http://webgilde.com |
| 10 | * @copyright 2013 Thomas Maier, webgilde GmbH |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * an ad object |
| 15 | * |
| 16 | * @package Advads_Ad |
| 17 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 18 | */ |
| 19 | class Advads_Ad { |
| 20 | |
| 21 | /** |
| 22 | * id of the post type for this ad |
| 23 | */ |
| 24 | public $id = 0; |
| 25 | |
| 26 | /** |
| 27 | * true, if this is an Advanced Ads Ad post type |
| 28 | */ |
| 29 | protected $is_ad = false; |
| 30 | |
| 31 | /** |
| 32 | * ad type |
| 33 | */ |
| 34 | public $type = 'content'; |
| 35 | |
| 36 | /** |
| 37 | * ad width |
| 38 | */ |
| 39 | public $width = 0; |
| 40 | |
| 41 | /** |
| 42 | * ad height |
| 43 | */ |
| 44 | public $height = 0; |
| 45 | |
| 46 | /** |
| 47 | * object of current ad type |
| 48 | */ |
| 49 | protected $type_obj; |
| 50 | |
| 51 | /** |
| 52 | * content of the ad |
| 53 | * |
| 54 | * only needed for ad types using the post content field |
| 55 | */ |
| 56 | public $content = ''; |
| 57 | |
| 58 | /** |
| 59 | * conditions of the ad display |
| 60 | */ |
| 61 | public $conditions = array(); |
| 62 | |
| 63 | /** |
| 64 | * status of the ad (e.g. publish, pending) |
| 65 | */ |
| 66 | public $status = array(); |
| 67 | |
| 68 | /** |
| 69 | * array with meta field options aka parameters |
| 70 | */ |
| 71 | protected $options = array(); |
| 72 | |
| 73 | /** |
| 74 | * name of the meta field to save options to |
| 75 | */ |
| 76 | static $options_meta_field = 'advanced_ads_ad_options'; |
| 77 | |
| 78 | /** |
| 79 | * multidimensional array contains information about the wrapper |
| 80 | * each possible html attribute is an array with possible multiple elements |
| 81 | */ |
| 82 | public $wrapper = array(); |
| 83 | |
| 84 | /** |
| 85 | * init ad object |
| 86 | * |
| 87 | * @param int $id id of the ad (= post id) |
| 88 | */ |
| 89 | public function __construct($id) { |
| 90 | global $advanced_ads_ad_conditions; |
| 91 | $id = absint($id); |
| 92 | $this->id = $id; |
| 93 | |
| 94 | if(!empty($id)) $this->load($id); |
| 95 | |
| 96 | // dynamically add sanitize filters for condition types |
| 97 | $_types = array(); |
| 98 | foreach($advanced_ads_ad_conditions as $_condition) { |
| 99 | // add unique |
| 100 | $_types[$_condition['type']] = false; |
| 101 | } |
| 102 | // iterate types |
| 103 | foreach(array_keys($_types) as $_type) { |
| 104 | // -TODO might be faster to use __call() method or isset()-test class method array |
| 105 | $method_name = 'sanitize_condition_'. $_type; |
| 106 | if (method_exists($this, $method_name)) { |
| 107 | add_filter('advanced-ads-sanitize-condition-' . $_type, array($this, $method_name), 10, 1); |
| 108 | } elseif (function_exists('advads_sanitize_condition_' . $_type)) { |
| 109 | // check for public function to sanitize this |
| 110 | add_filter('advanced-ads-sanitize-condition-' . $_type, 'advads_sanitize_condition_' . $_type, 10, 1); |
| 111 | |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * load an ad object by id based on its ad type |
| 118 | * |
| 119 | * @since 1.0.0 |
| 120 | */ |
| 121 | private function load($id = 0){ |
| 122 | |
| 123 | $_data = get_post($id); |
| 124 | if($_data == null) return false; |
| 125 | |
| 126 | // return, if not an ad |
| 127 | if($_data->post_type != Advanced_Ads::POST_TYPE_SLUG) { |
| 128 | return false; |
| 129 | } else { |
| 130 | $this->is_ad = true; |
| 131 | } |
| 132 | |
| 133 | $this->type = $this->options('type'); |
| 134 | /* load ad type object */ |
| 135 | $types = Advanced_Ads::get_instance()->ad_types; |
| 136 | if(isset($types[$this->type])){ |
| 137 | $this->type_obj = $types[$this->type]; |
| 138 | } else { |
| 139 | $this->type_obj = new Advads_Ad_Type_Abstract; |
| 140 | } |
| 141 | $this->width = $this->options('width'); |
| 142 | $this->height = $this->options('height'); |
| 143 | $this->conditions = $this->options('conditions'); |
| 144 | $this->status = $_data->post_status; |
| 145 | |
| 146 | // load content based on ad type |
| 147 | $this->content = $this->type_obj->load_content($_data); |
| 148 | |
| 149 | // set wrapper conditions |
| 150 | $this->wrapper = apply_filters('advanced-ads-set-wrapper', $this->wrapper, $this); |
| 151 | // add unique wrapper id, if options given |
| 152 | if(is_array($this->wrapper) && $this->wrapper !== array() && !isset($this->wrapper['id'])){ |
| 153 | // create unique id if not yet given |
| 154 | $this->wrapper['id'] = $this->create_wrapper_id(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * get options from meta field and return specific field |
| 160 | * |
| 161 | * @param string $field post meta key to be returned |
| 162 | * @return mixed meta field content |
| 163 | * @since 1.0.0 |
| 164 | * @todo check against default values |
| 165 | */ |
| 166 | public function options($field = ''){ |
| 167 | // retrieve options, if not given yet |
| 168 | if ($this->options === array()) { |
| 169 | // get_post_meta() may return false |
| 170 | $this->options = get_post_meta($this->id, self::$options_meta_field, true); |
| 171 | } |
| 172 | |
| 173 | // return specific option |
| 174 | if($field != '') { |
| 175 | if(isset($this->options[$field])) |
| 176 | return $this->options[$field]; |
| 177 | } else { // return all options |
| 178 | if(!empty($this->options)) |
| 179 | return $this->options; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * set an option of the ad |
| 185 | * |
| 186 | * @since 1.1.0 |
| 187 | * @param string $option name of the option |
| 188 | * @param mixed $value value of the option |
| 189 | */ |
| 190 | public function set_option($option = '', $value = ''){ |
| 191 | if($option == '') return; |
| 192 | |
| 193 | // get current options |
| 194 | $options = $this->options(); |
| 195 | |
| 196 | // set options |
| 197 | $options[$option] = $value; |
| 198 | |
| 199 | // save options |
| 200 | $this->options = $options; |
| 201 | |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * return ad content for frontend output |
| 207 | * |
| 208 | * @since 1.0.0 |
| 209 | * @return string $output ad output |
| 210 | */ |
| 211 | public function output(){ |
| 212 | if(!$this->is_ad) return ''; |
| 213 | |
| 214 | $output = $this->prepare_frontend_output(); |
| 215 | return $output; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * check if the ad can be displayed in frontend due to its conditions |
| 220 | * |
| 221 | * @since 1.0.0 |
| 222 | * @return bool $can_display true if can be displayed in frontend |
| 223 | */ |
| 224 | public function can_display(){ |
| 225 | |
| 226 | $options = Advanced_Ads::get_instance()->options(); |
| 227 | $see_ads_capability = (!empty($options['hide-for-user-role'])) ? $options['hide-for-user-role'] : 0; |
| 228 | |
| 229 | // don’t display ads that are not published or private for users not logged in |
| 230 | if($this->status !== 'publish' && ($this->status === 'private' && !is_user_logged_in())){ |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // check if user is logged in and if so if users with his rights can see ads |
| 235 | if (is_user_logged_in() && $see_ads_capability && current_user_can($see_ads_capability)) { |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | if (!$this->can_display_by_conditions() || !$this->can_display_by_visitor()) { |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | // add own conditions to flag output as possible or not |
| 244 | $can_display = apply_filters('advanced-ads-can-display', true, $this); |
| 245 | |
| 246 | return $can_display; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * check display conditions |
| 251 | * |
| 252 | * @since 1.1.0 moved here from can_display() |
| 253 | * @return bool $can_display true if can be displayed in frontend |
| 254 | */ |
| 255 | public function can_display_by_conditions(){ |
| 256 | global $post; |
| 257 | |
| 258 | if(empty($this->options['conditions']) || |
| 259 | !is_array($this->options['conditions'])) return true; |
| 260 | |
| 261 | // display ad if conditions are explicitely disabled |
| 262 | if(isset($this->options['conditions']['enabled']) && !$this->options['conditions']['enabled']) return true; |
| 263 | |
| 264 | $conditions = $this->options['conditions']; |
| 265 | foreach($conditions as $_cond_key => $_cond_value) { |
| 266 | switch($_cond_key){ |
| 267 | // check for post ids |
| 268 | case 'postids' : |
| 269 | // included posts |
| 270 | if(!empty($_cond_value['include'])){ |
| 271 | $post_ids = explode(',', $_cond_value['include']); |
| 272 | if(is_array($post_ids) |
| 273 | && isset($post->ID) |
| 274 | && !in_array($post->ID, $post_ids)) |
| 275 | return false; |
| 276 | } |
| 277 | // excluded posts |
| 278 | if(!empty($_cond_value['exclude'])){ |
| 279 | $post_ids = explode(',', $_cond_value['exclude']); |
| 280 | if(is_array($post_ids) |
| 281 | && isset($post->ID) |
| 282 | && in_array($post->ID, $post_ids)) |
| 283 | return false; |
| 284 | } |
| 285 | break; |
| 286 | // check for category ids |
| 287 | case 'categoryids' : |
| 288 | // included |
| 289 | if(!empty($_cond_value['include'])){ |
| 290 | if(is_string($_cond_value['include'])){ |
| 291 | $category_ids = explode(',', $_cond_value['include']); |
| 292 | } else { |
| 293 | $category_ids = $_cond_value['include']; |
| 294 | } |
| 295 | // check if currently in a post (not post page, but also posts in loops) |
| 296 | if(is_array($category_ids) && isset($post->ID) |
| 297 | && !in_category($category_ids, $post)) { |
| 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | // check for excluded category ids |
| 302 | if(!empty($_cond_value['exclude'])){ |
| 303 | if(is_string($_cond_value['exclude'])){ |
| 304 | $category_ids = explode(',', $_cond_value['exclude']); |
| 305 | } else { |
| 306 | $category_ids = $_cond_value['exclude']; |
| 307 | } |
| 308 | // check if currently in a post (not post page, but also posts in loops) |
| 309 | if(is_array($category_ids) && isset($post->ID) |
| 310 | && in_category($category_ids, $post) ) { |
| 311 | // being only in one excluded category is enough to not display the ad |
| 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | break; |
| 316 | // check for included category archive ids |
| 317 | // @link http://codex.wordpress.org/Conditional_Tags#A_Category_Page |
| 318 | case 'categoryarchiveids' : |
| 319 | if(!empty($_cond_value['include'])){ |
| 320 | $category_ids = explode(',', $_cond_value['include']); |
| 321 | if(is_array($category_ids) && !is_category($category_ids)) |
| 322 | return false; |
| 323 | } |
| 324 | // check for excluded category archive ids |
| 325 | if(!empty($_cond_value['exclude'])){ |
| 326 | $category_ids = explode(',', $_cond_value['exclude']); |
| 327 | if(is_array($category_ids) && is_category($category_ids)) |
| 328 | return false; |
| 329 | } |
| 330 | break; |
| 331 | // check for included post types |
| 332 | case 'posttypes' : |
| 333 | // display everywhere, if include not set (= all is checked) |
| 334 | // TODO remove condition check for string; deprecated since 1.2.2 |
| 335 | if(!empty($_cond_value['include'])){ |
| 336 | if(is_string($_cond_value['include'])){ |
| 337 | $post_types = explode(',', $_cond_value['include']); |
| 338 | } else { |
| 339 | $post_types = $_cond_value['include']; |
| 340 | } |
| 341 | // check if currently in a post (not post page, but also posts in loops) |
| 342 | if(is_array($post_types) && !in_array(get_post_type(), $post_types)) { |
| 343 | return false; |
| 344 | } |
| 345 | } |
| 346 | // check for excluded post types |
| 347 | // TODO remove in a later version, deprecated since 1.2.2 |
| 348 | if(!empty($_cond_value['exclude'])){ |
| 349 | $post_types = explode(',', $_cond_value['exclude']); |
| 350 | // check if currently in a post (not post page, but also posts in loops) |
| 351 | if(is_array($post_types) && in_array(get_post_type(), $post_types)) { |
| 352 | return false; |
| 353 | } |
| 354 | } |
| 355 | break; |
| 356 | // check is_front_page |
| 357 | // @link https://codex.wordpress.org/Conditional_Tags#The_Front_Page |
| 358 | case 'is_front_page' : |
| 359 | if(($_cond_value == 1 && !is_front_page()) |
| 360 | || ($_cond_value == 0 && is_front_page())) |
| 361 | return false; |
| 362 | break; |
| 363 | // check is_singular |
| 364 | // @link https://codex.wordpress.org/Conditional_Tags#A_Post_Type |
| 365 | case 'is_singular' : |
| 366 | if(($_cond_value == 1 && !is_singular()) |
| 367 | || ($_cond_value == 0 && is_singular())) |
| 368 | return false; |
| 369 | break; |
| 370 | // check is_archive |
| 371 | // @link https://codex.wordpress.org/Conditional_Tags#Any_Archive_Page |
| 372 | case 'is_archive' : |
| 373 | if(($_cond_value == 1 && !is_archive()) |
| 374 | || ($_cond_value == 0 && is_archive())) |
| 375 | return false; |
| 376 | break; |
| 377 | // check is_search |
| 378 | // @link https://codex.wordpress.org/Conditional_Tags#A_Search_Result_Page |
| 379 | case 'is_search' : |
| 380 | if(($_cond_value == 1 && !is_search()) |
| 381 | || ($_cond_value == 0 && is_search())) |
| 382 | return false; |
| 383 | break; |
| 384 | // check is_404 |
| 385 | // @link https://codex.wordpress.org/Conditional_Tags#A_404_Not_Found_Page |
| 386 | case 'is_404' : |
| 387 | if(($_cond_value == 1 && !is_404()) |
| 388 | || ($_cond_value == 0 && is_404())) |
| 389 | return false; |
| 390 | break; |
| 391 | // check is_attachment |
| 392 | // @link https://codex.wordpress.org/Conditional_Tags#An_Attachment |
| 393 | case 'is_attachment' : |
| 394 | if(($_cond_value == 1 && !is_attachment()) |
| 395 | || ($_cond_value == 0 && is_attachment())) |
| 396 | return false; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * check visitor conditions |
| 406 | * |
| 407 | * @since 1.1.0 |
| 408 | * @return bool $can_display true if can be displayed in frontend based on visitor settings |
| 409 | */ |
| 410 | public function can_display_by_visitor(){ |
| 411 | |
| 412 | if(empty($this->options['visitor']) || |
| 413 | !is_array($this->options['visitor'])) return true; |
| 414 | |
| 415 | $visitor_conditions = $this->options('visitor'); |
| 416 | |
| 417 | // check mobile condition |
| 418 | if(!empty($visitor_conditions['mobile'])){ |
| 419 | switch($visitor_conditions['mobile']){ |
| 420 | case 'only' : |
| 421 | if(!wp_is_mobile()) return false; |
| 422 | break; |
| 423 | case 'no' : |
| 424 | if(wp_is_mobile()) return false; |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * save an ad to the database |
| 434 | * takes values from the current state |
| 435 | */ |
| 436 | public function save(){ |
| 437 | global $wpdb; |
| 438 | |
| 439 | // remove slashes from content |
| 440 | $content = $this->prepare_content_to_save(); |
| 441 | |
| 442 | $where = array('ID' => $this->id); |
| 443 | $wpdb->update( $wpdb->posts, array( 'post_content' => $content ), $where ); |
| 444 | |
| 445 | // sanitize conditions |
| 446 | // see sanitize_conditions function for example on using this filter |
| 447 | $conditions = self::sanitize_conditions_on_save($this->conditions); |
| 448 | |
| 449 | // save other options to post meta field |
| 450 | $options = $this->options(); |
| 451 | |
| 452 | $options['type'] = $this->type; |
| 453 | $options['width'] = $this->width; |
| 454 | $options['height'] = $this->height; |
| 455 | $options['conditions'] = $conditions; |
| 456 | |
| 457 | // filter to manipulate options or add more to be saved |
| 458 | $options = apply_filters('advanced-ads-save-options', $options, $this); |
| 459 | |
| 460 | // update global settings |
| 461 | $this->update_general_ad_conditions($conditions); |
| 462 | |
| 463 | update_post_meta($this->id, self::$options_meta_field, $options); |
| 464 | |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * native filter for content field before being saved |
| 469 | * |
| 470 | * @return string $content ad content |
| 471 | * @since 1.0.0 |
| 472 | */ |
| 473 | public function prepare_content_to_save() { |
| 474 | |
| 475 | $content = $this->content; |
| 476 | |
| 477 | // load ad type specific parameter filter |
| 478 | $content = $this->type_obj->sanitize_content($content); |
| 479 | // apply a custom filter by ad type |
| 480 | $content = apply_filters('advanced-ads-pre-ad-save-' . $this->type, $content); |
| 481 | |
| 482 | return $content; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * native filter for ad parameters before being saved |
| 487 | * |
| 488 | * @return arr $parameters sanitized parameters |
| 489 | */ |
| 490 | public function prepare_parameters_to_save() { |
| 491 | |
| 492 | $parameters = $this->parameters; |
| 493 | // load ad type specific parameter filter |
| 494 | $parameters = $this->type_obj->sanitize_parameters($parameters); |
| 495 | |
| 496 | // apply native WP filter for content fields |
| 497 | return $parameters; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * prepare ads output |
| 502 | * |
| 503 | * @param string $content ad content |
| 504 | * @param obj $ad ad object |
| 505 | */ |
| 506 | public function prepare_frontend_output(){ |
| 507 | |
| 508 | // load ad type specific content filter |
| 509 | $output = $this->type_obj->prepare_output($this); |
| 510 | |
| 511 | // build wrapper around the ad |
| 512 | $output = $this->add_wrapper($output); |
| 513 | |
| 514 | // apply a custom filter by ad type |
| 515 | $output = apply_filters('advanced-ads-ad-output', $output, $this); |
| 516 | |
| 517 | return $output; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * sanitize ad display conditions when saving the ad |
| 522 | * |
| 523 | * @param array $conditions conditions array send via the dashboard form for an ad |
| 524 | * @return array with sanitized conditions |
| 525 | * @since 1.0.0 |
| 526 | */ |
| 527 | public function sanitize_conditions_on_save($conditions = array()){ |
| 528 | |
| 529 | global $advanced_ads_ad_conditions; |
| 530 | |
| 531 | if(!is_array($conditions) || $conditions == array()) return array(); |
| 532 | |
| 533 | foreach($conditions as $_key => $_condition){ |
| 534 | if(!is_array($_condition)) |
| 535 | $_condition = trim($_condition); |
| 536 | if($_condition == '') { |
| 537 | $conditions[$_key] = $_condition; |
| 538 | continue; |
| 539 | } |
| 540 | $type = !empty($advanced_ads_ad_conditions[$_key]['type']) ? $advanced_ads_ad_conditions[$_key]['type'] : 0; |
| 541 | if(empty($type)) continue; |
| 542 | |
| 543 | // dynamically apply filters for each condition used |
| 544 | $conditions[$_key] = apply_filters('advanced-ads-sanitize-condition-' . $type, $_condition); |
| 545 | } |
| 546 | |
| 547 | return $conditions; |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * sanitize id input field(s) for pattern /1,2,3,4/ |
| 552 | * |
| 553 | * @pararm array/string $cond input string/array |
| 554 | * @return array/string $cond sanitized string/array |
| 555 | */ |
| 556 | public static function sanitize_condition_idfield($cond = ''){ |
| 557 | // strip anything that is not comma or number |
| 558 | if(is_array($cond)){ |
| 559 | foreach($cond as $_key => $_cond){ |
| 560 | $cond[$_key] = preg_replace('#[^0-9,]#', '', $_cond); |
| 561 | } |
| 562 | } else { |
| 563 | $cond = preg_replace('#[^0-9,]#', '', $cond); |
| 564 | } |
| 565 | return $cond; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * sanitize radio input field |
| 570 | * |
| 571 | * @pararm string $string input string |
| 572 | * @return string $string sanitized string |
| 573 | */ |
| 574 | public static function sanitize_condition_radio($string = ''){ |
| 575 | // only allow 0, 1 and empty |
| 576 | return $string = preg_replace('#[^01]#', '', $string); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * sanitize comma seperated text input field |
| 581 | * |
| 582 | * @pararm array/string $cond input string/array |
| 583 | * @return array/string $cond sanitized string/array |
| 584 | */ |
| 585 | public static function sanitize_condition_textvalues($cond = ''){ |
| 586 | // strip anything that is not comma, alphanumeric, minus and underscore |
| 587 | if(is_array($cond)){ |
| 588 | foreach($cond as $_key => $_cond){ |
| 589 | $cond[$_key] = preg_replace('#[^0-9,A-Za-z-_]#', '', $_cond); |
| 590 | } |
| 591 | } else { |
| 592 | $cond = preg_replace('#[^0-9,A-Za-z-_]#', '', $cond); |
| 593 | } |
| 594 | return $cond; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * update general ad conditions with conditions for the current ad |
| 599 | * |
| 600 | * @param array $conditions ad display conditions from ad form |
| 601 | * @since 1.0.0 |
| 602 | * @todo make those condition checks extendible |
| 603 | */ |
| 604 | public function update_general_ad_conditions($conditions){ |
| 605 | global $advanced_ads_ad_conditions; |
| 606 | |
| 607 | $plugin = Advanced_Ads::get_instance(); |
| 608 | $ads_by_conditions = $plugin->get_ads_by_conditions_array(); |
| 609 | $plugin_slug = $plugin->get_plugin_slug(); |
| 610 | |
| 611 | // remove current ad from general ad condition array |
| 612 | $ads_by_conditions = $this->remove_ad_from_general_ad_conditions($this->id, $ads_by_conditions); |
| 613 | |
| 614 | // only run conditions if ad is publically visible |
| 615 | if($this->status == 'publish') |
| 616 | // iterate through the ads display condition |
| 617 | foreach($conditions as $_condition_key => $_condition){ |
| 618 | if(!isset($advanced_ads_ad_conditions[$_condition_key]['type'])) { |
| 619 | $plugin->log(sprintf(__('A "%s" display condition does not exist', $plugin_slug), $_condition_key)); |
| 620 | return; |
| 621 | } |
| 622 | // add conditions based on type |
| 623 | switch($advanced_ads_ad_conditions[$_condition_key]['type']){ |
| 624 | case 'idfield' : |
| 625 | if(isset($_condition['include']) && $_condition['include'] != ''){ |
| 626 | $_ids = explode(',', $_condition['include']); |
| 627 | if(is_array($_ids)) foreach($_ids as $_id){ |
| 628 | $ads_by_conditions[$_condition_key][$_id]['include'][] = $this->id; |
| 629 | } |
| 630 | } |
| 631 | if(isset($_condition['exclude']) && $_condition['exclude'] != ''){ |
| 632 | $_ids = explode(',', $_condition['exclude']); |
| 633 | if(is_array($_ids)) foreach($_ids as $_id){ |
| 634 | $ads_by_conditions[$_condition_key][$_id]['exclude'][] = $this->id; |
| 635 | } |
| 636 | } |
| 637 | break; |
| 638 | case 'textvalues' : |
| 639 | if(isset($_condition['include']) && $_condition['include'] != ''){ |
| 640 | $_ids = explode(',', $_condition['include']); |
| 641 | if(is_array($_ids)) foreach($_ids as $_id){ |
| 642 | $ads_by_conditions[$_condition_key][$_id]['include'][] = $this->id; |
| 643 | } |
| 644 | } |
| 645 | if(isset($_condition['exclude']) && $_condition['exclude'] != ''){ |
| 646 | $_ids = explode(',', $_condition['exclude']); |
| 647 | if(is_array($_ids)) foreach($_ids as $_id){ |
| 648 | $ads_by_conditions[$_condition_key][$_id]['exclude'][] = $this->id; |
| 649 | } |
| 650 | } |
| 651 | break; |
| 652 | case 'radio' : |
| 653 | if($_condition == 1) |
| 654 | $ads_by_conditions[$_condition_key]['include'][] = $this->id; |
| 655 | elseif($_condition == 0) |
| 656 | $ads_by_conditions[$_condition_key]['exclude'][] = $this->id; |
| 657 | break; |
| 658 | } // switch |
| 659 | } // forearch |
| 660 | |
| 661 | update_option('advads-ads-by-conditions', $ads_by_conditions); |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * remove ad id from ad conditions array |
| 666 | * |
| 667 | * @param int $ad_id id of the ad (=post id) |
| 668 | * @param arr $conditions array with the general, global ad conditions |
| 669 | * @since 1.0.0 |
| 670 | */ |
| 671 | static function remove_ad_from_general_ad_conditions($ad_id = 0, $conditions = array()){ |
| 672 | $ad_id = absint($ad_id); |
| 673 | if(empty($ad_id) || !is_array($conditions) || $conditions == array()) return; |
| 674 | |
| 675 | foreach($conditions as $_key => $_cond){ |
| 676 | // remove single elements |
| 677 | if(!is_array($_cond) && $_cond == $ad_id){ |
| 678 | unset($conditions[$_key]); |
| 679 | } elseif(empty($_cond)){ |
| 680 | unset($conditions[$_key]); |
| 681 | } |
| 682 | // check recursively |
| 683 | elseif(is_array($_cond)){ |
| 684 | $new_cond = self::remove_ad_from_general_ad_conditions($ad_id, $_cond); |
| 685 | |
| 686 | if($new_cond == array() || $new_cond == ''){ |
| 687 | // remove empty arrays |
| 688 | unset($conditions[$_key]); |
| 689 | } else { |
| 690 | $conditions[$_key] = $new_cond; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | return $conditions; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * add a wrapper arount the ad content if wrapper information are given |
| 700 | * |
| 701 | * @since 1.1.4 |
| 702 | * @param str $ad_content content of the ad |
| 703 | * @return str $wrapper ad within the wrapper |
| 704 | */ |
| 705 | protected function add_wrapper($ad_content = ''){ |
| 706 | |
| 707 | $wrapper_options = apply_filters('advanced-ads-output-wrapper-options', $this->wrapper, $this); |
| 708 | |
| 709 | if($wrapper_options == array() || !is_array($wrapper_options) || empty($wrapper_options)) return $ad_content; |
| 710 | |
| 711 | $wrapper = $ad_content; |
| 712 | |
| 713 | // create unique id if not yet given |
| 714 | if(empty($wrapper_options['id'])){ |
| 715 | $wrapper_options['id'] = $this->create_wrapper_id(); |
| 716 | } |
| 717 | |
| 718 | // build the box |
| 719 | $wrapper = "<div"; |
| 720 | foreach($wrapper_options as $_html_attr => $_values){ |
| 721 | if($_html_attr == 'style'){ |
| 722 | $_style_values_string = ''; |
| 723 | foreach($_values as $_style_attr => $_style_values){ |
| 724 | if(is_array($_style_values)) |
| 725 | $_style_values_string .= $_style_attr . ': ' .implode(' ', $_style_values). '; '; |
| 726 | else |
| 727 | $_style_values_string .= $_style_attr . ': ' .$_style_values. '; '; |
| 728 | } |
| 729 | $wrapper .= " style=\"$_style_values_string\""; |
| 730 | } else { |
| 731 | if(is_array($_values)) |
| 732 | $_values_string = implode(' ', $_values); |
| 733 | else |
| 734 | $_values_string = sanitize_title($_values); |
| 735 | $wrapper .= " $_html_attr=\"$_values_string\""; |
| 736 | } |
| 737 | |
| 738 | |
| 739 | } |
| 740 | $wrapper .= '>'; |
| 741 | $wrapper .= apply_filters('advanced-ads-output-wrapper-before-content', '', $this); |
| 742 | $wrapper .= $ad_content; |
| 743 | $wrapper .= apply_filters('advanced-ads-output-wrapper-after-content', '', $this); |
| 744 | $wrapper .= '</div>'; |
| 745 | |
| 746 | return $wrapper; |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * create a random wrapper id |
| 751 | * |
| 752 | * @since 1.1.4 |
| 753 | * @return string $id random id string |
| 754 | */ |
| 755 | private function create_wrapper_id(){ |
| 756 | return 'advads-' . mt_rand(); |
| 757 | } |
| 758 | |
| 759 | } |
| 760 |