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