PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.3
Advanced Ads – Ad Manager & AdSense v1.3
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
857 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->output = $this->options('output');
145 $this->status = $_data->post_status;
146 $this->wrapper = $this->load_wrapper_options();
147
148 // load content based on ad type
149 $this->content = $this->type_obj->load_content($_data);
150
151 // set wrapper conditions
152 $this->wrapper = apply_filters('advanced-ads-set-wrapper', $this->wrapper, $this);
153 // add unique wrapper id, if options given
154 if(is_array($this->wrapper) && $this->wrapper !== array() && !isset($this->wrapper['id'])){
155 // create unique id if not yet given
156 $this->wrapper['id'] = $this->create_wrapper_id();
157 }
158 }
159
160 /**
161 * get options from meta field and return specific field
162 *
163 * @param string $field post meta key to be returned
164 * @return mixed meta field content
165 * @since 1.0.0
166 * @todo check against default values
167 */
168 public function options($field = ''){
169 // retrieve options, if not given yet
170 if ($this->options === array()) {
171 // get_post_meta() may return false
172 $this->options = get_post_meta($this->id, self::$options_meta_field, true);
173 }
174
175 // return specific option
176 if($field != '') {
177 if(isset($this->options[$field]))
178 return $this->options[$field];
179 } else { // return all options
180 if(!empty($this->options))
181 return $this->options;
182 }
183 }
184
185 /**
186 * set an option of the ad
187 *
188 * @since 1.1.0
189 * @param string $option name of the option
190 * @param mixed $value value of the option
191 */
192 public function set_option($option = '', $value = ''){
193 if($option == '') return;
194
195 // get current options
196 $options = $this->options();
197
198 // set options
199 $options[$option] = $value;
200
201 // save options
202 $this->options = $options;
203
204 }
205
206
207 /**
208 * return ad content for frontend output
209 *
210 * @since 1.0.0
211 * @return string $output ad output
212 */
213 public function output(){
214 if(!$this->is_ad) return '';
215
216 $output = $this->prepare_frontend_output();
217 return $output;
218 }
219
220 /**
221 * check if the ad can be displayed in frontend due to its conditions
222 *
223 * @since 1.0.0
224 * @return bool $can_display true if can be displayed in frontend
225 */
226 public function can_display(){
227
228 $options = Advanced_Ads::get_instance()->options();
229 $see_ads_capability = (!empty($options['hide-for-user-role'])) ? $options['hide-for-user-role'] : 0;
230
231 // don’t display ads that are not published or private for users not logged in
232 if($this->status !== 'publish' && !($this->status === 'private' && !is_user_logged_in())){
233 return false;
234 }
235
236 // check if user is logged in and if so if users with his rights can see ads
237 if (is_user_logged_in() && $see_ads_capability && current_user_can($see_ads_capability)) {
238 return false;
239 }
240
241 if (!$this->can_display_by_conditions() || !$this->can_display_by_visitor()) {
242 return false;
243 }
244
245 // add own conditions to flag output as possible or not
246 $can_display = apply_filters('advanced-ads-can-display', true, $this);
247
248 return $can_display;
249 }
250
251 /**
252 * check display conditions
253 *
254 * @since 1.1.0 moved here from can_display()
255 * @return bool $can_display true if can be displayed in frontend
256 */
257 public function can_display_by_conditions(){
258 global $post, $wp_query;
259
260 $query = $wp_query->get_queried_object();
261
262 if(empty($this->options['conditions']) ||
263 !is_array($this->options['conditions'])) return true;
264
265 // display ad if conditions are explicitely disabled
266 if(isset($this->options['conditions']['enabled']) && !$this->options['conditions']['enabled']) return true;
267
268 $conditions = $this->options['conditions'];
269 foreach($conditions as $_cond_key => $_cond_value) {
270 switch($_cond_key){
271 // check for post ids
272 case 'postids' :
273 if(is_singular() && empty($_cond_value['all'])){
274 // included posts
275 if(!empty($_cond_value['include'])){
276 if(is_string($_cond_value['include'])){
277 $post_ids = explode(',', $_cond_value['include']);
278 } else {
279 $post_ids = $_cond_value['include'];
280 }
281 if(is_array($post_ids)
282 && isset($post->ID)
283 && !in_array($post->ID, $post_ids))
284 return false;
285 }
286 // excluded posts
287 if(!empty($_cond_value['exclude'])){
288 if(is_string($_cond_value['exclude'])){
289 $post_ids = explode(',', $_cond_value['exclude']);
290 } else {
291 $post_ids = $_cond_value['exclude'];
292 }
293 if(is_array($post_ids) && isset($post->ID) && in_array($post->ID, $post_ids)){
294 return false;
295 }
296 }
297 }
298 break;
299 // check for category ids
300 case 'categoryids' :
301 // included
302 if(is_singular() && empty($_cond_value['all'])){
303 if(!empty($_cond_value['include'])){
304 if(is_string($_cond_value['include'])){
305 $category_ids = explode(',', $_cond_value['include']);
306 } else {
307 $category_ids = $_cond_value['include'];
308 }
309 // check if currently in a post (not post page, but also posts in loops)
310 if(is_array($category_ids) && isset($post->ID)
311 && !in_category($category_ids, $post)) {
312 return false;
313 }
314 }
315 // check for excluded category ids
316 if(!empty($_cond_value['exclude'])){
317 if(is_string($_cond_value['exclude'])){
318 $category_ids = explode(',', $_cond_value['exclude']);
319 } else {
320 $category_ids = $_cond_value['exclude'];
321 }
322 // check if currently in a post (not post page, but also posts in loops)
323 if(is_array($category_ids) && isset($post->ID)
324 && in_category($category_ids, $post) ) {
325 // being only in one excluded category is enough to not display the ad
326 return false;
327 }
328 }
329 }
330 break;
331 // check for included category archive ids
332 // @link http://codex.wordpress.org/Conditional_Tags#A_Category_Page
333 case 'categoryarchiveids' :
334 if(isset($query->term_id) && is_archive() && empty($_cond_value['all'])){
335 if(!empty($_cond_value['include'])){
336 if(is_string($_cond_value['include'])){
337 $category_ids = explode(',', $_cond_value['include']);
338 } else {
339 $category_ids = $_cond_value['include'];
340 }
341 if(is_array($category_ids) && !in_array($query->term_id, $category_ids))
342 return false;
343 }
344 // check for excluded category archive ids
345 if(!empty($_cond_value['exclude'])){
346 if(is_string($_cond_value['exclude'])){
347 $category_ids = explode(',', $_cond_value['exclude']);
348 } else {
349 $category_ids = $_cond_value['exclude'];
350 }
351 if(is_array($category_ids) && in_array($query->term_id, $category_ids))
352 return false;
353 }
354 }
355 break;
356 // check for included post types
357 case 'posttypes' :
358 // display everywhere, if include not set (= all is checked)
359 // TODO remove condition check for string; deprecated since 1.2.2
360 if(empty($_cond_value['all'])){
361 if(!empty($_cond_value['include'])){
362 if(is_string($_cond_value['include'])){
363 $post_types = explode(',', $_cond_value['include']);
364 } else {
365 $post_types = $_cond_value['include'];
366 }
367 // check if currently in a post (not post page, but also posts in loops)
368 if(is_array($post_types) && !in_array(get_post_type(), $post_types)) {
369 return false;
370 }
371 }
372 // check for excluded post types
373 // TODO remove in a later version, deprecated since 1.2.2
374 if(!empty($_cond_value['exclude'])){
375 $post_types = explode(',', $_cond_value['exclude']);
376 // check if currently in a post (not post page, but also posts in loops)
377 if(is_array($post_types) && in_array(get_post_type(), $post_types)) {
378 return false;
379 }
380 }
381 }
382 break;
383 // check is_front_page
384 // @link https://codex.wordpress.org/Conditional_Tags#The_Front_Page
385 case 'is_front_page' :
386 if(($_cond_value == 1 && (!is_front_page() && !is_home))
387 || ($_cond_value == 0 && (is_front_page() || is_home())))
388 return false;
389 break;
390 // check is_singular
391 // @link https://codex.wordpress.org/Conditional_Tags#A_Post_Type
392 case 'is_singular' :
393 if(($_cond_value == 1 && !is_singular())
394 || ($_cond_value == 0 && is_singular()))
395 return false;
396 break;
397 // check is_archive
398 // @link https://codex.wordpress.org/Conditional_Tags#Any_Archive_Page
399 case 'is_archive' :
400 if(($_cond_value == 1 && !is_archive())
401 || ($_cond_value == 0 && is_archive()))
402 return false;
403 break;
404 // check is_search
405 // @link https://codex.wordpress.org/Conditional_Tags#A_Search_Result_Page
406 case 'is_search' :
407 if(($_cond_value == 1 && !is_search())
408 || ($_cond_value == 0 && is_search()))
409 return false;
410 break;
411 // check is_404
412 // @link https://codex.wordpress.org/Conditional_Tags#A_404_Not_Found_Page
413 case 'is_404' :
414 if(($_cond_value == 1 && !is_404())
415 || ($_cond_value == 0 && is_404()))
416 return false;
417 break;
418 // check is_attachment
419 // @link https://codex.wordpress.org/Conditional_Tags#An_Attachment
420 case 'is_attachment' :
421 if(($_cond_value == 1 && !is_attachment())
422 || ($_cond_value == 0 && is_attachment()))
423 return false;
424 break;
425 }
426 }
427
428 return true;
429 }
430
431 /**
432 * check visitor conditions
433 *
434 * @since 1.1.0
435 * @return bool $can_display true if can be displayed in frontend based on visitor settings
436 */
437 public function can_display_by_visitor(){
438
439 if(empty($this->options['visitor']) ||
440 !is_array($this->options['visitor'])) return true;
441
442 $visitor_conditions = $this->options('visitor');
443
444 // check mobile condition
445 if(!empty($visitor_conditions['mobile'])){
446 switch($visitor_conditions['mobile']){
447 case 'only' :
448 if(!wp_is_mobile()) return false;
449 break;
450 case 'no' :
451 if(wp_is_mobile()) return false;
452 break;
453 }
454 }
455
456 return true;
457 }
458
459 /**
460 * save an ad to the database
461 * takes values from the current state
462 */
463 public function save(){
464 global $wpdb;
465
466 // remove slashes from content
467 $content = $this->prepare_content_to_save();
468
469 $where = array('ID' => $this->id);
470 $wpdb->update( $wpdb->posts, array( 'post_content' => $content ), $where );
471
472 // sanitize conditions
473 // see sanitize_conditions function for example on using this filter
474 $conditions = self::sanitize_conditions_on_save($this->conditions);
475
476 // save other options to post meta field
477 $options = $this->options();
478
479 $options['type'] = $this->type;
480 $options['width'] = $this->width;
481 $options['height'] = $this->height;
482 $options['conditions'] = $conditions;
483
484 // filter to manipulate options or add more to be saved
485 $options = apply_filters('advanced-ads-save-options', $options, $this);
486
487 // update global settings
488 $this->update_general_ad_conditions($conditions);
489
490 update_post_meta($this->id, self::$options_meta_field, $options);
491
492 }
493
494 /**
495 * native filter for content field before being saved
496 *
497 * @return string $content ad content
498 * @since 1.0.0
499 */
500 public function prepare_content_to_save() {
501
502 $content = $this->content;
503
504 // load ad type specific parameter filter
505 $content = $this->type_obj->sanitize_content($content);
506 // apply a custom filter by ad type
507 $content = apply_filters('advanced-ads-pre-ad-save-' . $this->type, $content);
508
509 return $content;
510 }
511
512 /**
513 * native filter for ad parameters before being saved
514 *
515 * @return arr $parameters sanitized parameters
516 */
517 public function prepare_parameters_to_save() {
518
519 $parameters = $this->parameters;
520 // load ad type specific parameter filter
521 $parameters = $this->type_obj->sanitize_parameters($parameters);
522
523 // apply native WP filter for content fields
524 return $parameters;
525 }
526
527 /**
528 * prepare ads output
529 *
530 * @param string $content ad content
531 * @param obj $ad ad object
532 */
533 public function prepare_frontend_output(){
534
535 // load ad type specific content filter
536 $output = $this->type_obj->prepare_output($this);
537
538 // build wrapper around the ad
539 $output = $this->add_wrapper($output);
540
541 // add a clearfix, if set
542 if(isset($this->output['clearfix']) && $this->output['clearfix']){
543 $output .= '<br style="clear: both; display: block; float: none;"/>';
544 }
545
546 // apply a custom filter by ad type
547 $output = apply_filters('advanced-ads-ad-output', $output, $this);
548
549 return $output;
550 }
551
552 /**
553 * sanitize ad display conditions when saving the ad
554 *
555 * @param array $conditions conditions array send via the dashboard form for an ad
556 * @return array with sanitized conditions
557 * @since 1.0.0
558 */
559 public function sanitize_conditions_on_save($conditions = array()){
560
561 global $advanced_ads_ad_conditions;
562
563 if(!is_array($conditions) || $conditions == array()) return array();
564
565 foreach($conditions as $_key => $_condition){
566 if($_key == 'postids'){
567 // sanitize single post conditions
568 if(empty($_condition['ids'])){ // remove, if empty
569 $_condition['include'] = array();
570 $_condition['exclude'] = array();
571 } else {
572 switch($_condition['method']){
573 case 'include' :
574 $_condition['include'] = $_condition['ids'];
575 $_condition['exclude'] = array();
576 break;
577 case 'exclude' :
578 $_condition['include'] = array();
579 $_condition['exclude'] = $_condition['ids'];
580 break;
581 }
582 }
583 } else {
584 if(!is_array($_condition))
585 $_condition = trim($_condition);
586 if($_condition == '') {
587 $conditions[$_key] = $_condition;
588 continue;
589 }
590 }
591 $type = !empty($advanced_ads_ad_conditions[$_key]['type']) ? $advanced_ads_ad_conditions[$_key]['type'] : 0;
592 if(empty($type)) continue;
593
594 // dynamically apply filters for each condition used
595 $conditions[$_key] = apply_filters('advanced-ads-sanitize-condition-' . $type, $_condition);
596 }
597
598 return $conditions;
599 }
600
601 /**
602 * sanitize id input field(s) for pattern /1,2,3,4/
603 *
604 * @pararm array/string $cond input string/array
605 * @return array/string $cond sanitized string/array
606 */
607 public static function sanitize_condition_idfield($cond = ''){
608 // strip anything that is not comma or number
609
610 if(is_array($cond)){
611 foreach($cond as $_key => $_cond){
612 $cond[$_key] = preg_replace('#[^0-9,]#', '', $_cond);
613 }
614 } else {
615 $cond = preg_replace('#[^0-9,]#', '', $cond);
616 }
617 return $cond;
618 }
619
620 /**
621 * sanitize radio input field
622 *
623 * @pararm string $string input string
624 * @return string $string sanitized string
625 */
626 public static function sanitize_condition_radio($string = ''){
627 // only allow 0, 1 and empty
628 return $string = preg_replace('#[^01]#', '', $string);
629 }
630
631 /**
632 * sanitize comma seperated text input field
633 *
634 * @pararm array/string $cond input string/array
635 * @return array/string $cond sanitized string/array
636 */
637 public static function sanitize_condition_textvalues($cond = ''){
638 // strip anything that is not comma, alphanumeric, minus and underscore
639 if(is_array($cond)){
640 foreach($cond as $_key => $_cond){
641 $cond[$_key] = preg_replace('#[^0-9,A-Za-z-_]#', '', $_cond);
642 }
643 } else {
644 $cond = preg_replace('#[^0-9,A-Za-z-_]#', '', $cond);
645 }
646 return $cond;
647 }
648
649 /**
650 * update general ad conditions with conditions for the current ad
651 *
652 * @param array $conditions ad display conditions from ad form
653 * @since 1.0.0
654 * @todo make those condition checks extendible
655 */
656 public function update_general_ad_conditions($conditions){
657 global $advanced_ads_ad_conditions;
658
659 $plugin = Advanced_Ads::get_instance();
660 $ads_by_conditions = $plugin->get_ads_by_conditions_array();
661
662 // remove current ad from general ad condition array
663 $ads_by_conditions = $this->remove_ad_from_general_ad_conditions($this->id, $ads_by_conditions);
664
665 // only run conditions if ad is publically visible
666 if($this->status == 'publish')
667 // iterate through the ads display condition
668 foreach($conditions as $_condition_key => $_condition){
669 if(!isset($advanced_ads_ad_conditions[$_condition_key]['type'])) {
670 $plugin->log(sprintf(__('A "%s" display condition does not exist', ADVADS_SLUG), $_condition_key));
671 return;
672 }
673 // add conditions based on type
674 switch($advanced_ads_ad_conditions[$_condition_key]['type']){
675 case 'idfield' :
676 if(isset($_condition['include']) && $_condition['include'] != ''){
677 $_ids = explode(',', $_condition['include']);
678 if(is_array($_ids)) foreach($_ids as $_id){
679 $ads_by_conditions[$_condition_key][$_id]['include'][] = $this->id;
680 }
681 }
682 if(isset($_condition['exclude']) && $_condition['exclude'] != ''){
683 $_ids = explode(',', $_condition['exclude']);
684 if(is_array($_ids)) foreach($_ids as $_id){
685 $ads_by_conditions[$_condition_key][$_id]['exclude'][] = $this->id;
686 }
687 }
688 break;
689 case 'textvalues' :
690 if(isset($_condition['include']) && $_condition['include'] != ''){
691 $_ids = explode(',', $_condition['include']);
692 if(is_array($_ids)) foreach($_ids as $_id){
693 $ads_by_conditions[$_condition_key][$_id]['include'][] = $this->id;
694 }
695 }
696 if(isset($_condition['exclude']) && $_condition['exclude'] != ''){
697 $_ids = explode(',', $_condition['exclude']);
698 if(is_array($_ids)) foreach($_ids as $_id){
699 $ads_by_conditions[$_condition_key][$_id]['exclude'][] = $this->id;
700 }
701 }
702 break;
703 case 'radio' :
704 if($_condition == 1)
705 $ads_by_conditions[$_condition_key]['include'][] = $this->id;
706 elseif($_condition == 0)
707 $ads_by_conditions[$_condition_key]['exclude'][] = $this->id;
708 break;
709 } // switch
710 } // forearch
711
712 update_option('advads-ads-by-conditions', $ads_by_conditions);
713 }
714
715 /**
716 * remove ad id from ad conditions array
717 *
718 * @param int $ad_id id of the ad (=post id)
719 * @param arr $conditions array with the general, global ad conditions
720 * @since 1.0.0
721 */
722 static function remove_ad_from_general_ad_conditions($ad_id = 0, $conditions = array()){
723 $ad_id = absint($ad_id);
724 if(empty($ad_id) || !is_array($conditions) || $conditions == array()) return;
725
726 foreach($conditions as $_key => $_cond){
727 // remove single elements
728 if(!is_array($_cond) && $_cond == $ad_id){
729 unset($conditions[$_key]);
730 } elseif(empty($_cond)){
731 unset($conditions[$_key]);
732 }
733 // check recursively
734 elseif(is_array($_cond)){
735 $new_cond = self::remove_ad_from_general_ad_conditions($ad_id, $_cond);
736
737 if($new_cond == array() || $new_cond == ''){
738 // remove empty arrays
739 unset($conditions[$_key]);
740 } else {
741 $conditions[$_key] = $new_cond;
742 }
743 }
744 }
745
746 return $conditions;
747 }
748
749 /**
750 * load wrapper options set with the ad
751 *
752 * @since 1.3
753 * @return arr $wrapper options array ready to be use in add_wrapper() function
754 */
755 protected function load_wrapper_options(){
756 $wrapper = array();
757
758 // print_r($this->output);
759
760 if(!empty($this->output['position'])) {
761 switch($this->output['position']) {
762 case 'left' :
763 $wrapper['style']['float'] = 'left';
764 break;
765 case 'right' :
766 $wrapper['style']['float'] = 'right';
767 break;
768 case 'center' :
769 $wrapper['style']['text-align'] = 'center';
770 break;
771 case 'clearfix' :
772 $wrapper['style']['clear'] = 'both';
773 break;
774 }
775 }
776
777 if(!empty($this->output['margin']['top'])) {
778 $wrapper['style']['margin-top'] = intval($this->output['margin']['top']) . 'px';
779 }
780 if(!empty($this->output['margin']['right'])) {
781 $wrapper['style']['margin-right'] = intval($this->output['margin']['right']) . 'px';
782 }
783 if(!empty($this->output['margin']['bottom'])) {
784 $wrapper['style']['margin-bottom'] = intval($this->output['margin']['bottom']) . 'px';
785 }
786 if(!empty($this->output['margin']['left'])) {
787 $wrapper['style']['margin-left'] = intval($this->output['margin']['left']) . 'px';
788 }
789
790 // print_r($wrapper);
791
792 return $wrapper;
793 }
794
795 /**
796 * add a wrapper arount the ad content if wrapper information are given
797 *
798 * @since 1.1.4
799 * @param str $ad_content content of the ad
800 * @return str $wrapper ad within the wrapper
801 */
802 protected function add_wrapper($ad_content = ''){
803
804 $wrapper_options = apply_filters('advanced-ads-output-wrapper-options', $this->wrapper, $this);
805
806 if($wrapper_options == array() || !is_array($wrapper_options) || empty($wrapper_options)) return $ad_content;
807
808 $wrapper = $ad_content;
809
810 // create unique id if not yet given
811 if(empty($wrapper_options['id'])){
812 $wrapper_options['id'] = $this->create_wrapper_id();
813 }
814
815 // build the box
816 $wrapper = "<div";
817 foreach($wrapper_options as $_html_attr => $_values){
818 if($_html_attr == 'style'){
819 $_style_values_string = '';
820 foreach($_values as $_style_attr => $_style_values){
821 if(is_array($_style_values))
822 $_style_values_string .= $_style_attr . ': ' .implode(' ', $_style_values). '; ';
823 else
824 $_style_values_string .= $_style_attr . ': ' .$_style_values. '; ';
825 }
826 $wrapper .= " style=\"$_style_values_string\"";
827 } else {
828 if(is_array($_values))
829 $_values_string = implode(' ', $_values);
830 else
831 $_values_string = sanitize_title($_values);
832 $wrapper .= " $_html_attr=\"$_values_string\"";
833 }
834
835
836 }
837 $wrapper .= '>';
838 $wrapper .= apply_filters('advanced-ads-output-wrapper-before-content', '', $this);
839 $wrapper .= $ad_content;
840 $wrapper .= apply_filters('advanced-ads-output-wrapper-after-content', '', $this);
841 $wrapper .= '</div>';
842
843 return $wrapper;
844 }
845
846 /**
847 * create a random wrapper id
848 *
849 * @since 1.1.4
850 * @return string $id random id string
851 */
852 private function create_wrapper_id(){
853 return 'advads-' . mt_rand();
854 }
855
856 }
857