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