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