PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.2.7
Advanced Ads – Ad Manager & AdSense v1.2.7
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 11 years ago ad_type_plain.php 11 years ago widget.php 11 years ago
ad.php
804 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, $wp_query;
257
258 $query = $wp_query->get_queried_object();
259
260 if(empty($this->options['conditions']) ||
261 !is_array($this->options['conditions'])) return true;
262
263 // display ad if conditions are explicitely disabled
264 if(isset($this->options['conditions']['enabled']) && !$this->options['conditions']['enabled']) return true;
265
266 $conditions = $this->options['conditions'];
267 foreach($conditions as $_cond_key => $_cond_value) {
268 switch($_cond_key){
269 // check for post ids
270 case 'postids' :
271 if(is_singular() && empty($_cond_value['all'])){
272 // included posts
273 if(!empty($_cond_value['include'])){
274 if(is_string($_cond_value['include'])){
275 $post_ids = explode(',', $_cond_value['include']);
276 } else {
277 $post_ids = $_cond_value['include'];
278 }
279 if(is_array($post_ids)
280 && isset($post->ID)
281 && !in_array($post->ID, $post_ids))
282 return false;
283 }
284 // excluded posts
285 if(!empty($_cond_value['exclude'])){
286 if(is_string($_cond_value['exclude'])){
287 $post_ids = explode(',', $_cond_value['exclude']);
288 } else {
289 $post_ids = $_cond_value['exclude'];
290 }
291 if(is_array($post_ids) && isset($post->ID) && in_array($post->ID, $post_ids)){
292 return false;
293 }
294 }
295 }
296 break;
297 // check for category ids
298 case 'categoryids' :
299 // included
300 if(is_singular() && empty($_cond_value['all'])){
301 if(!empty($_cond_value['include'])){
302 if(is_string($_cond_value['include'])){
303 $category_ids = explode(',', $_cond_value['include']);
304 } else {
305 $category_ids = $_cond_value['include'];
306 }
307 // check if currently in a post (not post page, but also posts in loops)
308 if(is_array($category_ids) && isset($post->ID)
309 && !in_category($category_ids, $post)) {
310 return false;
311 }
312 }
313 // check for excluded category ids
314 if(!empty($_cond_value['exclude'])){
315 if(is_string($_cond_value['exclude'])){
316 $category_ids = explode(',', $_cond_value['exclude']);
317 } else {
318 $category_ids = $_cond_value['exclude'];
319 }
320 // check if currently in a post (not post page, but also posts in loops)
321 if(is_array($category_ids) && isset($post->ID)
322 && in_category($category_ids, $post) ) {
323 // being only in one excluded category is enough to not display the ad
324 return false;
325 }
326 }
327 }
328 break;
329 // check for included category archive ids
330 // @link http://codex.wordpress.org/Conditional_Tags#A_Category_Page
331 case 'categoryarchiveids' :
332 if(isset($query->term_id) && is_archive() && empty($_cond_value['all'])){
333 if(!empty($_cond_value['include'])){
334 if(is_string($_cond_value['include'])){
335 $category_ids = explode(',', $_cond_value['include']);
336 } else {
337 $category_ids = $_cond_value['include'];
338 }
339 if(is_array($category_ids) && !in_array($query->term_id, $category_ids))
340 return false;
341 }
342 // check for excluded category archive ids
343 if(!empty($_cond_value['exclude'])){
344 if(is_string($_cond_value['exclude'])){
345 $category_ids = explode(',', $_cond_value['exclude']);
346 } else {
347 $category_ids = $_cond_value['exclude'];
348 }
349 if(is_array($category_ids) && in_array($query->term_id, $category_ids))
350 return false;
351 }
352 }
353 break;
354 // check for included post types
355 case 'posttypes' :
356 // display everywhere, if include not set (= all is checked)
357 // TODO remove condition check for string; deprecated since 1.2.2
358 if(empty($_cond_value['all'])){
359 if(!empty($_cond_value['include'])){
360 if(is_string($_cond_value['include'])){
361 $post_types = explode(',', $_cond_value['include']);
362 } else {
363 $post_types = $_cond_value['include'];
364 }
365 // check if currently in a post (not post page, but also posts in loops)
366 if(is_array($post_types) && !in_array(get_post_type(), $post_types)) {
367 return false;
368 }
369 }
370 // check for excluded post types
371 // TODO remove in a later version, deprecated since 1.2.2
372 if(!empty($_cond_value['exclude'])){
373 $post_types = explode(',', $_cond_value['exclude']);
374 // check if currently in a post (not post page, but also posts in loops)
375 if(is_array($post_types) && in_array(get_post_type(), $post_types)) {
376 return false;
377 }
378 }
379 }
380 break;
381 // check is_front_page
382 // @link https://codex.wordpress.org/Conditional_Tags#The_Front_Page
383 case 'is_front_page' :
384 if(($_cond_value == 1 && (!is_front_page() && !is_home))
385 || ($_cond_value == 0 && (is_front_page() || is_home())))
386 return false;
387 break;
388 // check is_singular
389 // @link https://codex.wordpress.org/Conditional_Tags#A_Post_Type
390 case 'is_singular' :
391 if(($_cond_value == 1 && !is_singular())
392 || ($_cond_value == 0 && is_singular()))
393 return false;
394 break;
395 // check is_archive
396 // @link https://codex.wordpress.org/Conditional_Tags#Any_Archive_Page
397 case 'is_archive' :
398 if(($_cond_value == 1 && !is_archive())
399 || ($_cond_value == 0 && is_archive()))
400 return false;
401 break;
402 // check is_search
403 // @link https://codex.wordpress.org/Conditional_Tags#A_Search_Result_Page
404 case 'is_search' :
405 if(($_cond_value == 1 && !is_search())
406 || ($_cond_value == 0 && is_search()))
407 return false;
408 break;
409 // check is_404
410 // @link https://codex.wordpress.org/Conditional_Tags#A_404_Not_Found_Page
411 case 'is_404' :
412 if(($_cond_value == 1 && !is_404())
413 || ($_cond_value == 0 && is_404()))
414 return false;
415 break;
416 // check is_attachment
417 // @link https://codex.wordpress.org/Conditional_Tags#An_Attachment
418 case 'is_attachment' :
419 if(($_cond_value == 1 && !is_attachment())
420 || ($_cond_value == 0 && is_attachment()))
421 return false;
422 break;
423 }
424 }
425
426 return true;
427 }
428
429 /**
430 * check visitor conditions
431 *
432 * @since 1.1.0
433 * @return bool $can_display true if can be displayed in frontend based on visitor settings
434 */
435 public function can_display_by_visitor(){
436
437 if(empty($this->options['visitor']) ||
438 !is_array($this->options['visitor'])) return true;
439
440 $visitor_conditions = $this->options('visitor');
441
442 // check mobile condition
443 if(!empty($visitor_conditions['mobile'])){
444 switch($visitor_conditions['mobile']){
445 case 'only' :
446 if(!wp_is_mobile()) return false;
447 break;
448 case 'no' :
449 if(wp_is_mobile()) return false;
450 break;
451 }
452 }
453
454 return true;
455 }
456
457 /**
458 * save an ad to the database
459 * takes values from the current state
460 */
461 public function save(){
462 global $wpdb;
463
464 // remove slashes from content
465 $content = $this->prepare_content_to_save();
466
467 $where = array('ID' => $this->id);
468 $wpdb->update( $wpdb->posts, array( 'post_content' => $content ), $where );
469
470 // sanitize conditions
471 // see sanitize_conditions function for example on using this filter
472 $conditions = self::sanitize_conditions_on_save($this->conditions);
473
474 // save other options to post meta field
475 $options = $this->options();
476
477 $options['type'] = $this->type;
478 $options['width'] = $this->width;
479 $options['height'] = $this->height;
480 $options['conditions'] = $conditions;
481
482 // filter to manipulate options or add more to be saved
483 $options = apply_filters('advanced-ads-save-options', $options, $this);
484
485 // update global settings
486 $this->update_general_ad_conditions($conditions);
487
488 update_post_meta($this->id, self::$options_meta_field, $options);
489
490 }
491
492 /**
493 * native filter for content field before being saved
494 *
495 * @return string $content ad content
496 * @since 1.0.0
497 */
498 public function prepare_content_to_save() {
499
500 $content = $this->content;
501
502 // load ad type specific parameter filter
503 $content = $this->type_obj->sanitize_content($content);
504 // apply a custom filter by ad type
505 $content = apply_filters('advanced-ads-pre-ad-save-' . $this->type, $content);
506
507 return $content;
508 }
509
510 /**
511 * native filter for ad parameters before being saved
512 *
513 * @return arr $parameters sanitized parameters
514 */
515 public function prepare_parameters_to_save() {
516
517 $parameters = $this->parameters;
518 // load ad type specific parameter filter
519 $parameters = $this->type_obj->sanitize_parameters($parameters);
520
521 // apply native WP filter for content fields
522 return $parameters;
523 }
524
525 /**
526 * prepare ads output
527 *
528 * @param string $content ad content
529 * @param obj $ad ad object
530 */
531 public function prepare_frontend_output(){
532
533 // load ad type specific content filter
534 $output = $this->type_obj->prepare_output($this);
535
536 // build wrapper around the ad
537 $output = $this->add_wrapper($output);
538
539 // apply a custom filter by ad type
540 $output = apply_filters('advanced-ads-ad-output', $output, $this);
541
542 return $output;
543 }
544
545 /**
546 * sanitize ad display conditions when saving the ad
547 *
548 * @param array $conditions conditions array send via the dashboard form for an ad
549 * @return array with sanitized conditions
550 * @since 1.0.0
551 */
552 public function sanitize_conditions_on_save($conditions = array()){
553
554 global $advanced_ads_ad_conditions;
555
556 if(!is_array($conditions) || $conditions == array()) return array();
557
558 foreach($conditions as $_key => $_condition){
559 if($_key == 'postids'){
560 // sanitize single post conditions
561 if(empty($_condition['ids'])){ // remove, if empty
562 $_condition['include'] = array();
563 $_condition['exclude'] = array();
564 } else {
565 switch($_condition['method']){
566 case 'include' :
567 $_condition['include'] = $_condition['ids'];
568 $_condition['exclude'] = array();
569 break;
570 case 'exclude' :
571 $_condition['include'] = array();
572 $_condition['exclude'] = $_condition['ids'];
573 break;
574 }
575 }
576 } else {
577 if(!is_array($_condition))
578 $_condition = trim($_condition);
579 if($_condition == '') {
580 $conditions[$_key] = $_condition;
581 continue;
582 }
583 }
584 $type = !empty($advanced_ads_ad_conditions[$_key]['type']) ? $advanced_ads_ad_conditions[$_key]['type'] : 0;
585 if(empty($type)) continue;
586
587 // dynamically apply filters for each condition used
588 $conditions[$_key] = apply_filters('advanced-ads-sanitize-condition-' . $type, $_condition);
589 }
590
591 return $conditions;
592 }
593
594 /**
595 * sanitize id input field(s) for pattern /1,2,3,4/
596 *
597 * @pararm array/string $cond input string/array
598 * @return array/string $cond sanitized string/array
599 */
600 public static function sanitize_condition_idfield($cond = ''){
601 // strip anything that is not comma or number
602
603 if(is_array($cond)){
604 foreach($cond as $_key => $_cond){
605 $cond[$_key] = preg_replace('#[^0-9,]#', '', $_cond);
606 }
607 } else {
608 $cond = preg_replace('#[^0-9,]#', '', $cond);
609 }
610 return $cond;
611 }
612
613 /**
614 * sanitize radio input field
615 *
616 * @pararm string $string input string
617 * @return string $string sanitized string
618 */
619 public static function sanitize_condition_radio($string = ''){
620 // only allow 0, 1 and empty
621 return $string = preg_replace('#[^01]#', '', $string);
622 }
623
624 /**
625 * sanitize comma seperated text input field
626 *
627 * @pararm array/string $cond input string/array
628 * @return array/string $cond sanitized string/array
629 */
630 public static function sanitize_condition_textvalues($cond = ''){
631 // strip anything that is not comma, alphanumeric, minus and underscore
632 if(is_array($cond)){
633 foreach($cond as $_key => $_cond){
634 $cond[$_key] = preg_replace('#[^0-9,A-Za-z-_]#', '', $_cond);
635 }
636 } else {
637 $cond = preg_replace('#[^0-9,A-Za-z-_]#', '', $cond);
638 }
639 return $cond;
640 }
641
642 /**
643 * update general ad conditions with conditions for the current ad
644 *
645 * @param array $conditions ad display conditions from ad form
646 * @since 1.0.0
647 * @todo make those condition checks extendible
648 */
649 public function update_general_ad_conditions($conditions){
650 global $advanced_ads_ad_conditions;
651
652 $plugin = Advanced_Ads::get_instance();
653 $ads_by_conditions = $plugin->get_ads_by_conditions_array();
654
655 // remove current ad from general ad condition array
656 $ads_by_conditions = $this->remove_ad_from_general_ad_conditions($this->id, $ads_by_conditions);
657
658 // only run conditions if ad is publically visible
659 if($this->status == 'publish')
660 // iterate through the ads display condition
661 foreach($conditions as $_condition_key => $_condition){
662 if(!isset($advanced_ads_ad_conditions[$_condition_key]['type'])) {
663 $plugin->log(sprintf(__('A "%s" display condition does not exist', ADVADS_SLUG), $_condition_key));
664 return;
665 }
666 // add conditions based on type
667 switch($advanced_ads_ad_conditions[$_condition_key]['type']){
668 case 'idfield' :
669 if(isset($_condition['include']) && $_condition['include'] != ''){
670 $_ids = explode(',', $_condition['include']);
671 if(is_array($_ids)) foreach($_ids as $_id){
672 $ads_by_conditions[$_condition_key][$_id]['include'][] = $this->id;
673 }
674 }
675 if(isset($_condition['exclude']) && $_condition['exclude'] != ''){
676 $_ids = explode(',', $_condition['exclude']);
677 if(is_array($_ids)) foreach($_ids as $_id){
678 $ads_by_conditions[$_condition_key][$_id]['exclude'][] = $this->id;
679 }
680 }
681 break;
682 case 'textvalues' :
683 if(isset($_condition['include']) && $_condition['include'] != ''){
684 $_ids = explode(',', $_condition['include']);
685 if(is_array($_ids)) foreach($_ids as $_id){
686 $ads_by_conditions[$_condition_key][$_id]['include'][] = $this->id;
687 }
688 }
689 if(isset($_condition['exclude']) && $_condition['exclude'] != ''){
690 $_ids = explode(',', $_condition['exclude']);
691 if(is_array($_ids)) foreach($_ids as $_id){
692 $ads_by_conditions[$_condition_key][$_id]['exclude'][] = $this->id;
693 }
694 }
695 break;
696 case 'radio' :
697 if($_condition == 1)
698 $ads_by_conditions[$_condition_key]['include'][] = $this->id;
699 elseif($_condition == 0)
700 $ads_by_conditions[$_condition_key]['exclude'][] = $this->id;
701 break;
702 } // switch
703 } // forearch
704
705 update_option('advads-ads-by-conditions', $ads_by_conditions);
706 }
707
708 /**
709 * remove ad id from ad conditions array
710 *
711 * @param int $ad_id id of the ad (=post id)
712 * @param arr $conditions array with the general, global ad conditions
713 * @since 1.0.0
714 */
715 static function remove_ad_from_general_ad_conditions($ad_id = 0, $conditions = array()){
716 $ad_id = absint($ad_id);
717 if(empty($ad_id) || !is_array($conditions) || $conditions == array()) return;
718
719 foreach($conditions as $_key => $_cond){
720 // remove single elements
721 if(!is_array($_cond) && $_cond == $ad_id){
722 unset($conditions[$_key]);
723 } elseif(empty($_cond)){
724 unset($conditions[$_key]);
725 }
726 // check recursively
727 elseif(is_array($_cond)){
728 $new_cond = self::remove_ad_from_general_ad_conditions($ad_id, $_cond);
729
730 if($new_cond == array() || $new_cond == ''){
731 // remove empty arrays
732 unset($conditions[$_key]);
733 } else {
734 $conditions[$_key] = $new_cond;
735 }
736 }
737 }
738
739 return $conditions;
740 }
741
742 /**
743 * add a wrapper arount the ad content if wrapper information are given
744 *
745 * @since 1.1.4
746 * @param str $ad_content content of the ad
747 * @return str $wrapper ad within the wrapper
748 */
749 protected function add_wrapper($ad_content = ''){
750
751 $wrapper_options = apply_filters('advanced-ads-output-wrapper-options', $this->wrapper, $this);
752
753 if($wrapper_options == array() || !is_array($wrapper_options) || empty($wrapper_options)) return $ad_content;
754
755 $wrapper = $ad_content;
756
757 // create unique id if not yet given
758 if(empty($wrapper_options['id'])){
759 $wrapper_options['id'] = $this->create_wrapper_id();
760 }
761
762 // build the box
763 $wrapper = "<div";
764 foreach($wrapper_options as $_html_attr => $_values){
765 if($_html_attr == 'style'){
766 $_style_values_string = '';
767 foreach($_values as $_style_attr => $_style_values){
768 if(is_array($_style_values))
769 $_style_values_string .= $_style_attr . ': ' .implode(' ', $_style_values). '; ';
770 else
771 $_style_values_string .= $_style_attr . ': ' .$_style_values. '; ';
772 }
773 $wrapper .= " style=\"$_style_values_string\"";
774 } else {
775 if(is_array($_values))
776 $_values_string = implode(' ', $_values);
777 else
778 $_values_string = sanitize_title($_values);
779 $wrapper .= " $_html_attr=\"$_values_string\"";
780 }
781
782
783 }
784 $wrapper .= '>';
785 $wrapper .= apply_filters('advanced-ads-output-wrapper-before-content', '', $this);
786 $wrapper .= $ad_content;
787 $wrapper .= apply_filters('advanced-ads-output-wrapper-after-content', '', $this);
788 $wrapper .= '</div>';
789
790 return $wrapper;
791 }
792
793 /**
794 * create a random wrapper id
795 *
796 * @since 1.1.4
797 * @return string $id random id string
798 */
799 private function create_wrapper_id(){
800 return 'advads-' . mt_rand();
801 }
802
803 }
804