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