PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.2
Advanced Ads – Ad Manager & AdSense v1.2
2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / classes / ad.php
advanced-ads / classes Last commit date
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
724 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 * multidimensional array contains information about the wrapper
70 * each possible html attribute is an array with possible multiple elements
71 */
72 public $wrapper = array();
73
74 /**
75 * init ad object
76 *
77 * @param int $id id of the ad (= post id)
78 */
79 public function __construct($id) {
80 global $advanced_ads_ad_conditions;
81 $id = absint($id);
82 $this->id = $id;
83
84 if(!empty($id)) $this->load($id);
85
86 // dynamically add sanitize filters for condition types
87 $_types = array();
88 foreach($advanced_ads_ad_conditions as $_condition) {
89 $_types[] = $_condition['type'];
90 }
91 $_types = array_unique($_types);
92 foreach($_types as $_type) {
93 $method_name = 'sanitize_condition_'. $_type;
94 if(method_exists($this, $method_name)) {
95 add_filter('advanced-ads-sanitize-condition-' . $_type, array($this, $method_name), 10, 1);
96 } elseif(function_exists('advads_sanitize_condition_' . $_type)) {
97 // check for public function to sanitize this
98 add_filter('advanced-ads-sanitize-condition-' . $_type, 'advads_sanitize_condition_' . $_type, 10, 1);
99
100 }
101 }
102 }
103
104 /**
105 * load an ad object by id based on its ad type
106 *
107 * @since 1.0.0
108 */
109 private function load($id = 0){
110
111 $_data = get_post($id);
112 if($_data == null) return false;
113
114 // return, if not an ad
115 if($_data->post_type != Advanced_Ads::POST_TYPE_SLUG) {
116 return false;
117 } else {
118 $this->is_ad = true;
119 }
120
121 $this->type = $this->options('type');
122 /* load ad type object */
123 $types = Advanced_Ads::get_instance()->ad_types;
124 if(isset($types[$this->type])){
125 $this->type_obj = $types[$this->type];
126 } else {
127 $this->type_obj = new Advads_Ad_Type_Abstract;
128 }
129
130 $this->conditions = $this->options('conditions');
131 $this->status = $_data->post_status;
132
133 // load content based on ad type
134 $this->content = $this->type_obj->load_content($_data);
135
136 // set wrapper conditions
137 $this->wrapper = apply_filters('advanced-ads-set-wrapper', $this->wrapper, $this);
138 // add unique wrapper id, if options given
139 if(!empty($this->wrapper) && empty($wrapper_options['id'])){
140 // create unique id if not yet given
141 $this->wrapper['id'] = $this->create_wrapper_id();
142 }
143 }
144
145 /**
146 * get options from meta field and return specific field
147 *
148 * @param string $field post meta key to be returned
149 * @return mixed meta field content
150 * @since 1.0.0
151 * @todo check against default values
152 */
153 public function options($field = ''){
154 // retrieve options, if not given yet
155 if($this->options == array()) {
156 $this->options = get_post_meta($this->id, self::$options_meta_field, true);
157 }
158
159 // return specific option
160 if($field != '') {
161 if(!empty($this->options[$field]))
162 return $this->options[$field];
163 } else { // return all options
164 if(!empty($this->options))
165 return $this->options;
166 }
167 }
168
169 /**
170 * set an option of the ad
171 *
172 * @since 1.1.0
173 * @param string $option name of the option
174 * @param mixed $value value of the option
175 */
176 public function set_option($option = '', $value = ''){
177 if($option == '') return;
178
179 // get current options
180 $options = $this->options();
181
182 // set options
183 $options[$option] = $value;
184
185 // save options
186 $this->options = $options;
187
188 }
189
190
191 /**
192 * return ad content for frontend output
193 *
194 * @since 1.0.0
195 * @return string $output ad output
196 */
197 public function output(){
198 if(!$this->is_ad) return '';
199
200 $output = $this->prepare_frontend_output();
201 return $output;
202 }
203
204 /**
205 * check if the ad can be displayed in frontend due to its conditions
206 *
207 * @since 1.0.0
208 * @return bool $can_display true if can be displayed in frontend
209 */
210 public function can_display(){
211
212 $can_display = false;
213 $options = Advanced_Ads::get_instance()->options();
214 $see_ads_capability = (!empty($options['hide-for-user-role'])) ? $options['hide-for-user-role'] : 0;
215
216 // check if user is logged in and if so if users with his rights can see ads
217 if(is_user_logged_in() && $see_ads_capability && current_user_can($see_ads_capability)) return false;
218
219 if($this->can_display_by_conditions() && $this->can_display_by_visitor()) {
220 $can_display = true;
221 } else {
222 return false;
223 }
224
225 // add own conditions to flag output as possible or not
226 $can_display = apply_filters('advanced-ads-can-display', $can_display, $this);
227
228 return $can_display;
229 }
230
231 /**
232 * check display conditions
233 *
234 * @since 1.1.0 moved here from can_display()
235 * @return bool $can_display true if can be displayed in frontend
236 */
237 public function can_display_by_conditions(){
238 global $post;
239
240 if(empty($this->options['conditions']) ||
241 !is_array($this->options['conditions'])) return true;
242
243 // display ad if conditions are explicitely disabled
244 if(isset($this->options['conditions']['enabled']) && !$this->options['conditions']['enabled']) return true;
245
246 $conditions = $this->options['conditions'];
247 foreach($conditions as $_cond_key => $_cond_value) {
248 switch($_cond_key){
249 // check for post ids
250 case 'postids' :
251 // included posts
252 if(!empty($_cond_value['include'])){
253 $post_ids = explode(',', $_cond_value['include']);
254 if(is_array($post_ids)
255 && isset($post->ID)
256 && !in_array($post->ID, $post_ids))
257 return false;
258 }
259 // excluded posts
260 if(!empty($_cond_value['exclude'])){
261 $post_ids = explode(',', $_cond_value['exclude']);
262 if(is_array($post_ids)
263 && isset($post->ID)
264 && in_array($post->ID, $post_ids))
265 return false;
266 }
267 break;
268 // check for category ids
269 case 'categoryids' :
270 // included
271 if(!empty($_cond_value['include'])){
272 $category_ids = explode(',', $_cond_value['include']);
273 // check if currently in a post (not post page, but also posts in loops)
274 if(is_array($category_ids) && isset($post->ID)
275 && !in_category($category_ids, $post)) {
276 return false;
277 }
278 }
279 // check for excluded category ids
280 if(!empty($_cond_value['exclude'])){
281 $category_ids = explode(',', $_cond_value['exclude']);
282 // check if currently in a post (not post page, but also posts in loops)
283 if(is_array($category_ids) && isset($post->ID)
284 && in_category($category_ids, $post) ) {
285 // being only in one excluded category is enough to not display the ad
286 return false;
287 }
288 }
289 break;
290 // check for included category archive ids
291 // @link http://codex.wordpress.org/Conditional_Tags#A_Category_Page
292 case 'categoryarchiveids' :
293 if(!empty($_cond_value['include'])){
294 $category_ids = explode(',', $_cond_value['include']);
295 if(is_array($category_ids) && !is_category($category_ids))
296 return false;
297 }
298 // check for excluded category archive ids
299 if(!empty($_cond_value['exclude'])){
300 $category_ids = explode(',', $_cond_value['exclude']);
301 if(is_array($category_ids) && is_category($category_ids))
302 return false;
303 }
304 break;
305 // check for included post types
306 case 'posttypes' :
307 if(!empty($_cond_value['include'])){
308 $post_types = explode(',', $_cond_value['include']);
309 // check if currently in a post (not post page, but also posts in loops)
310 if(is_array($post_types) && !in_array(get_post_type(), $post_types)) {
311 return false;
312 }
313 }
314 // check for excluded post types
315 if(!empty($_cond_value['exclude'])){
316 $post_types = explode(',', $_cond_value['exclude']);
317 // check if currently in a post (not post page, but also posts in loops)
318 if(is_array($post_types) && in_array(get_post_type(), $post_types)) {
319 return false;
320 }
321 }
322 break;
323 // check is_front_page
324 // @link https://codex.wordpress.org/Conditional_Tags#The_Front_Page
325 case 'is_front_page' :
326 if(($_cond_value == 1 && !is_front_page())
327 || ($_cond_value == 0 && is_front_page()))
328 return false;
329 break;
330 // check is_singular
331 // @link https://codex.wordpress.org/Conditional_Tags#A_Post_Type
332 case 'is_singular' :
333 if(($_cond_value == 1 && !is_singular())
334 || ($_cond_value == 0 && is_singular()))
335 return false;
336 break;
337 // check is_archive
338 // @link https://codex.wordpress.org/Conditional_Tags#Any_Archive_Page
339 case 'is_archive' :
340 if(($_cond_value == 1 && !is_archive())
341 || ($_cond_value == 0 && is_archive()))
342 return false;
343 break;
344 // check is_search
345 // @link https://codex.wordpress.org/Conditional_Tags#A_Search_Result_Page
346 case 'is_search' :
347 if(($_cond_value == 1 && !is_search())
348 || ($_cond_value == 0 && is_search()))
349 return false;
350 break;
351 // check is_404
352 // @link https://codex.wordpress.org/Conditional_Tags#A_404_Not_Found_Page
353 case 'is_404' :
354 if(($_cond_value == 1 && !is_404())
355 || ($_cond_value == 0 && is_404()))
356 return false;
357 break;
358 // check is_attachment
359 // @link https://codex.wordpress.org/Conditional_Tags#An_Attachment
360 case 'is_attachment' :
361 if(($_cond_value == 1 && !is_attachment())
362 || ($_cond_value == 0 && is_attachment()))
363 return false;
364 break;
365 }
366 }
367
368 return true;
369 }
370
371 /**
372 * check visitor conditions
373 *
374 * @since 1.1.0
375 * @return bool $can_display true if can be displayed in frontend based on visitor settings
376 */
377 public function can_display_by_visitor(){
378
379 if(empty($this->options['visitor']) ||
380 !is_array($this->options['visitor'])) return true;
381
382 $visitor_conditions = $this->options('visitor');
383
384 // check mobile condition
385 if(!empty($visitor_conditions['mobile'])){
386 switch($visitor_conditions['mobile']){
387 case 'only' :
388 if(!wp_is_mobile()) return false;
389 break;
390 case 'no' :
391 if(wp_is_mobile()) return false;
392 break;
393 }
394 }
395
396 return true;
397 }
398
399 /**
400 * save an ad to the database
401 * takes values from the current state
402 */
403 public function save(){
404 global $wpdb;
405
406 // remove slashes from content
407 $content = $this->prepare_content_to_save();
408
409 $where = array('ID' => $this->id);
410 $wpdb->update( $wpdb->posts, array( 'post_content' => $content ), $where );
411
412 // sanitize conditions
413 // see sanitize_conditions function for example on using this filter
414 $conditions = self::sanitize_conditions_on_save($this->conditions);
415
416 // save other options to post meta field
417 $options = $this->options();
418
419 $options['type'] = $this->type;
420 $options['conditions'] = $conditions;
421
422 // filter to manipulate options or add more to be saved
423 $options = apply_filters('advanced-ads-save-options', $options, $this);
424
425 // update global settings
426 $this->update_general_ad_conditions($conditions);
427
428 update_post_meta($this->id, self::$options_meta_field, $options);
429
430 }
431
432 /**
433 * native filter for content field before being saved
434 *
435 * @return string $content ad content
436 * @since 1.0.0
437 */
438 public function prepare_content_to_save() {
439
440 $content = $this->content;
441
442 // load ad type specific parameter filter
443 $content = $this->type_obj->sanitize_content($content);
444 // apply a custom filter by ad type
445 $content = apply_filters('advanced-ads-pre-ad-save-' . $this->type, $content);
446
447 return $content;
448 }
449
450 /**
451 * native filter for ad parameters before being saved
452 *
453 * @return arr $parameters sanitized parameters
454 */
455 public function prepare_parameters_to_save() {
456
457 $parameters = $this->parameters;
458 // load ad type specific parameter filter
459 $parameters = $this->type_obj->sanitize_parameters($parameters);
460
461 // apply native WP filter for content fields
462 return $parameters;
463 }
464
465 /**
466 * prepare ads output
467 *
468 * @param string $content ad content
469 * @param obj $ad ad object
470 */
471 public function prepare_frontend_output(){
472
473 // load ad type specific content filter
474 $output = $this->type_obj->prepare_output($this);
475
476 // build wrapper around the ad
477 $output = $this->add_wrapper($output);
478
479 // apply a custom filter by ad type
480 $output = apply_filters('advanced-ads-ad-output', $output, $this);
481
482 return $output;
483 }
484
485 /**
486 * sanitize ad display conditions when saving the ad
487 *
488 * @param array $conditions conditions array send via the dashboard form for an ad
489 * @return array with sanitized conditions
490 * @since 1.0.0
491 */
492 public function sanitize_conditions_on_save($conditions = array()){
493
494 global $advanced_ads_ad_conditions;
495
496 if(!is_array($conditions) || $conditions == array()) return array();
497
498 foreach($conditions as $_key => $_condition){
499 if(!is_array($_condition))
500 $_condition = trim($_condition);
501 if($_condition == '') {
502 $conditions[$_key] = $_condition;
503 continue;
504 }
505 $type = !empty($advanced_ads_ad_conditions[$_key]['type']) ? $advanced_ads_ad_conditions[$_key]['type'] : 0;
506 if(empty($type)) continue;
507
508 // dynamically apply filters for each condition used
509 $conditions[$_key] = apply_filters('advanced-ads-sanitize-condition-' . $type, $_condition);
510 }
511
512 return $conditions;
513 }
514
515 /**
516 * sanitize id input field(s) for pattern /1,2,3,4/
517 *
518 * @pararm array/string $cond input string/array
519 * @return array/string $cond sanitized string/array
520 */
521 public static function sanitize_condition_idfield($cond = ''){
522 // strip anything that is not comma or number
523 if(is_array($cond)){
524 foreach($cond as $_key => $_cond){
525 $cond[$_key] = preg_replace('#[^0-9,]#', '', $_cond);
526 }
527 } else {
528 $cond = preg_replace('#[^0-9,]#', '', $cond);
529 }
530 return $cond;
531 }
532
533 /**
534 * sanitize radio input field
535 *
536 * @pararm string $string input string
537 * @return string $string sanitized string
538 */
539 public static function sanitize_condition_radio($string = ''){
540 // only allow 0, 1 and empty
541 return $string = preg_replace('#[^01]#', '', $string);
542 }
543
544 /**
545 * sanitize comma seperated text input field
546 *
547 * @pararm array/string $cond input string/array
548 * @return array/string $cond sanitized string/array
549 */
550 public static function sanitize_condition_textvalues($cond = ''){
551 // strip anything that is not comma, alphanumeric, minus and underscore
552 if(is_array($cond)){
553 foreach($cond as $_key => $_cond){
554 $cond[$_key] = preg_replace('#[^0-9,A-Za-z-_]#', '', $_cond);
555 }
556 } else {
557 $cond = preg_replace('#[^0-9,A-Za-z-_]#', '', $cond);
558 }
559 return $cond;
560 }
561
562 /**
563 * update general ad conditions with conditions for the current ad
564 *
565 * @param array $conditions ad display conditions from ad form
566 * @since 1.0.0
567 * @todo make those condition checks extendible
568 */
569 public function update_general_ad_conditions($conditions){
570 global $advanced_ads_ad_conditions;
571
572 $plugin = Advanced_Ads::get_instance();
573 $ads_by_conditions = $plugin->get_ads_by_conditions_array();
574 $plugin_slug = $plugin->get_plugin_slug();
575
576 // remove current ad from general ad condition array
577 $ads_by_conditions = $this->remove_ad_from_general_ad_conditions($this->id, $ads_by_conditions);
578
579 // only run conditions if ad is publically visible
580 if($this->status == 'publish')
581 // iterate through the ads display condition
582 foreach($conditions as $_condition_key => $_condition){
583 if(!isset($advanced_ads_ad_conditions[$_condition_key]['type'])) {
584 $plugin->log(sprintf(__('A "%s" display condition does not exist', $plugin_slug), $_condition_key));
585 return;
586 }
587 // add conditions based on type
588 switch($advanced_ads_ad_conditions[$_condition_key]['type']){
589 case 'idfield' :
590 if(isset($_condition['include']) && $_condition['include'] != ''){
591 $_ids = explode(',', $_condition['include']);
592 if(is_array($_ids)) foreach($_ids as $_id){
593 $ads_by_conditions[$_condition_key][$_id]['include'][] = $this->id;
594 }
595 }
596 if(isset($_condition['exclude']) && $_condition['exclude'] != ''){
597 $_ids = explode(',', $_condition['exclude']);
598 if(is_array($_ids)) foreach($_ids as $_id){
599 $ads_by_conditions[$_condition_key][$_id]['exclude'][] = $this->id;
600 }
601 }
602 break;
603 case 'textvalues' :
604 if(isset($_condition['include']) && $_condition['include'] != ''){
605 $_ids = explode(',', $_condition['include']);
606 if(is_array($_ids)) foreach($_ids as $_id){
607 $ads_by_conditions[$_condition_key][$_id]['include'][] = $this->id;
608 }
609 }
610 if(isset($_condition['exclude']) && $_condition['exclude'] != ''){
611 $_ids = explode(',', $_condition['exclude']);
612 if(is_array($_ids)) foreach($_ids as $_id){
613 $ads_by_conditions[$_condition_key][$_id]['exclude'][] = $this->id;
614 }
615 }
616 break;
617 case 'radio' :
618 if($_condition == 1)
619 $ads_by_conditions[$_condition_key]['include'][] = $this->id;
620 elseif($_condition == 0)
621 $ads_by_conditions[$_condition_key]['exclude'][] = $this->id;
622 break;
623 } // switch
624 } // forearch
625
626 update_option('advads-ads-by-conditions', $ads_by_conditions);
627 }
628
629 /**
630 * remove ad id from ad conditions array
631 *
632 * @param int $ad_id id of the ad (=post id)
633 * @param arr $conditions array with the general, global ad conditions
634 * @since 1.0.0
635 */
636 static function remove_ad_from_general_ad_conditions($ad_id = 0, $conditions = array()){
637 $ad_id = absint($ad_id);
638 if(empty($ad_id) || !is_array($conditions) || $conditions == array()) return;
639
640 foreach($conditions as $_key => $_cond){
641 // remove single elements
642 if(!is_array($_cond) && $_cond == $ad_id){
643 unset($conditions[$_key]);
644 } elseif(empty($_cond)){
645 unset($conditions[$_key]);
646 }
647 // check recursively
648 elseif(is_array($_cond)){
649 $new_cond = self::remove_ad_from_general_ad_conditions($ad_id, $_cond);
650
651 if($new_cond == array() || $new_cond == ''){
652 // remove empty arrays
653 unset($conditions[$_key]);
654 } else {
655 $conditions[$_key] = $new_cond;
656 }
657 }
658 }
659
660 return $conditions;
661 }
662
663 /**
664 * add a wrapper arount the ad content if wrapper information are given
665 *
666 * @since 1.1.4
667 * @param str $ad_content content of the ad
668 * @return str $wrapper ad within the wrapper
669 */
670 protected function add_wrapper($ad_content = ''){
671
672 $wrapper_options = apply_filters('advanced-ads-output-wrapper-options', $this->wrapper, $this);
673
674 if($wrapper_options == array() || !is_array($wrapper_options) || empty($wrapper_options)) return $ad_content;
675
676 $wrapper = $ad_content;
677
678 // create unique id if not yet given
679 if(empty($wrapper_options['id'])){
680 $wrapper_options['id'] = $this->create_wrapper_id();
681 }
682
683 // build the box
684 $wrapper = "<div";
685 foreach($wrapper_options as $_html_attr => $_values){
686 if($_html_attr == 'style'){
687 $_style_values_string = '';
688 foreach($_values as $_style_attr => $_style_values){
689 if(is_array($_style_values))
690 $_style_values_string .= $_style_attr . ': ' .implode(' ', $_style_values). '; ';
691 else
692 $_style_values_string .= $_style_attr . ': ' .$_style_values. '; ';
693 }
694 $wrapper .= " style=\"$_style_values_string\"";
695 } else {
696 if(is_array($_values))
697 $_values_string = implode(' ', $_values);
698 else
699 $_values_string = sanitize_title($_values);
700 $wrapper .= " $_html_attr=\"$_values_string\"";
701 }
702
703
704 }
705 $wrapper .= '>';
706 $wrapper .= apply_filters('advanced-ads-output-wrapper-before-content', '', $this);
707 $wrapper .= $ad_content;
708 $wrapper .= apply_filters('advanced-ads-output-wrapper-after-content', '', $this);
709 $wrapper .= '</div>';
710
711 return $wrapper;
712 }
713
714 /**
715 * create a random wrapper id
716 *
717 * @since 1.1.4
718 * @return string $id random id string
719 */
720 private function create_wrapper_id(){
721 return 'advads-' . mt_rand();
722 }
723
724 }