PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.7.25
Advanced Ads – Ad Manager & AdSense v1.7.25
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
EDD_SL_Plugin_Updater.php 9 years ago ad-ajax.php 9 years ago ad-debug.php 9 years ago ad-model.php 9 years ago ad-select.php 9 years ago ad.php 9 years ago ad_ajax_callbacks.php 9 years ago ad_group.php 9 years ago ad_placements.php 9 years ago ad_type_abstract.php 11 years ago ad_type_content.php 9 years ago ad_type_group.php 10 years ago ad_type_image.php 9 years ago ad_type_plain.php 9 years ago checks.php 9 years ago display-conditions.php 9 years ago filesystem.php 9 years ago frontend_checks.php 9 years ago plugin.php 9 years ago upgrades.php 9 years ago utils.php 9 years ago visitor-conditions.php 9 years ago widget.php 9 years ago
ad.php
820 lines
1 <?php
2
3 /**
4 * Advanced Ads Ad.
5 *
6 * @package Advanced_Ads_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 Advanced_Ads_Ad
17 * @author Thomas Maier <thomas.maier@webgilde.com>
18 * @deprecated since version 1.5.3 (May 6th 2015)
19 * might still be needed if some old add-ons are running somewhere
20 */
21 if( ! class_exists ( 'Advads_Ad', false ) ){
22 class Advads_Ad extends Advanced_Ads_Ad {
23
24 }
25 }
26
27 /**
28 * an ad object
29 *
30 * @package Advanced_Ads_Ad
31 * @author Thomas Maier <thomas.maier@webgilde.com>
32 */
33 class Advanced_Ads_Ad {
34
35 /**
36 * id of the post type for this ad
37 */
38 public $id = 0;
39
40 /**
41 * true, if this is an Advanced Ads Ad post type
42 */
43 protected $is_ad = false;
44
45 /**
46 * ad type
47 */
48 public $type = 'content';
49
50 /**
51 * ad width
52 */
53 public $width = 0;
54
55 /**
56 * target url
57 *
58 * @since 1.6.10
59 */
60 public $url = '';
61
62 /**
63 * ad height
64 */
65 public $height = 0;
66
67 /**
68 * object of current ad type
69 */
70 protected $type_obj;
71
72 /**
73 * content of the ad
74 *
75 * only needed for ad types using the post content field
76 */
77 public $content = '';
78
79 /**
80 * conditions of the ad display
81 */
82 public $conditions = array();
83
84 /**
85 * status of the ad (e.g. publish, pending)
86 */
87 public $status = array();
88
89 /**
90 * array with meta field options aka parameters
91 */
92 protected $options = array();
93
94 /**
95 * name of the meta field to save options to
96 */
97 static $options_meta_field = 'advanced_ads_ad_options';
98
99 /**
100 * additional arguments set when ad is loaded, overwrites or extends options
101 */
102 public $args = array();
103
104 /**
105 * multidimensional array contains information about the wrapper
106 * each possible html attribute is an array with possible multiple elements
107 */
108 public $wrapper = array();
109
110 /**
111 * Whether to create wrapper even if array is empty.
112 */
113 protected $need_wrapper = false;
114
115 /**
116 * Displayed above the ad.
117 */
118 protected $label = '';
119
120 /**
121 * init ad object
122 *
123 * @param int $id id of the ad (= post id)
124 * @param arr $args additional arguments
125 */
126 public function __construct($id, $args = array()) {
127 $id = absint( $id );
128 $this->id = $id;
129 $this->args = is_array( $args ) ? $args : array();
130
131 // whether the ad will be tracked
132 $this->global_output = isset( $this->args['global_output'] ) ? (bool) $this->args['global_output'] : true;
133
134 if ( ! empty($id) ) { $this->load( $id ); }
135
136 // dynamically add sanitize filters for condition types
137 $_types = array();
138 // -TODO use model
139 $advanced_ads_ad_conditions = Advanced_Ads::get_ad_conditions();
140 foreach ( $advanced_ads_ad_conditions as $_condition ) {
141 // add unique
142 $_types[$_condition['type']] = false;
143 }
144 // iterate types
145 foreach ( array_keys( $_types ) as $_type ) {
146 // -TODO might be faster to use __call() method or isset()-test class method array
147 $method_name = 'sanitize_condition_'. $_type;
148 if ( method_exists( $this, $method_name ) ) {
149 add_filter( 'advanced-ads-sanitize-condition-' . $_type, array($this, $method_name), 10, 1 );
150 } elseif ( function_exists( 'advads_sanitize_condition_' . $_type ) ) {
151 // check for public function to sanitize this
152 add_filter( 'advanced-ads-sanitize-condition-' . $_type, 'advads_sanitize_condition_' . $_type, 10, 1 );
153
154 }
155 }
156 }
157
158 /**
159 * load an ad object by id based on its ad type
160 *
161 * @since 1.0.0
162 */
163 private function load($id = 0){
164
165 $_data = get_post( $id );
166 if ( $_data == null ) { return false; }
167
168 // return, if not an ad
169 if ( $_data->post_type != Advanced_Ads::POST_TYPE_SLUG ) {
170 return false;
171 } else {
172 $this->is_ad = true;
173 }
174
175 $this->type = $this->options( 'type' );
176 $this->title = $_data->post_title;
177 /* load ad type object */
178 $types = Advanced_Ads::get_instance()->ad_types;
179 if ( isset($types[$this->type]) ){
180 $this->type_obj = $types[$this->type];
181 } else {
182 $this->type_obj = new Advanced_Ads_Ad_Type_Abstract;
183 }
184 $this->url = $this->options( 'url' );
185 $this->width = absint( $this->options( 'width' ) );
186 $this->height = absint( $this->options( 'height' ) );
187 $this->conditions = $this->options( 'conditions' );
188 $this->description = $this->options( 'description' );
189 $this->output = $this->options( 'output' );
190 $this->status = $_data->post_status;
191 $this->maybe_create_label();
192 $this->wrapper = $this->load_wrapper_options();
193 $this->expiry_date = $this->options( 'expiry_date' );
194
195 // load content based on ad type
196 $this->content = $this->type_obj->load_content( $_data );
197
198 // set wrapper conditions
199 $this->wrapper = apply_filters( 'advanced-ads-set-wrapper', $this->wrapper, $this );
200 // add unique wrapper id
201 if ( is_array( $this->wrapper )
202 && ( $this->wrapper !== array() || $this->need_wrapper )
203 && ! isset( $this->wrapper['id'] ) ){
204 // create unique id if not yet given
205 $this->wrapper['id'] = $this->create_wrapper_id();
206 }
207 }
208
209 /**
210 * get options from meta field and return specific field
211 *
212 * @param string $field post meta key to be returned
213 * @return mixed meta field content
214 * @since 1.0.0
215 * @todo check against default values
216 */
217 public function options( $field = '', $default = null ) {
218 // retrieve options, if not given yet
219 // -TODO may execute multiple times (if empty); bad design and risk to access unintialised data with direct access to $this->options property.
220 if ( $this->options === array() ) {
221 // get_post_meta() may return false
222 $meta = get_post_meta( $this->id, self::$options_meta_field, true );
223 if ( $meta ) {
224 // merge meta with arguments given on ad load
225 $this->options = Advanced_Ads_Utils::merge_deep_array( array( $meta, $this->args ) );
226 } else {
227 // load arguments given on ad load
228 $this->options = $this->args;
229 }
230
231 if ( isset( $this->options['change-ad'] ) ) {
232 // some options was provided by the user
233 $this->options = Advanced_Ads_Utils::merge_deep_array( array( $this->options, $this->options['change-ad'] ) );
234 }
235 }
236
237 // return specific option
238 if ( $field != '' ) {
239 if ( isset($this->options[$field]) ) {
240 return $this->options[$field];
241 }
242 } else { // return all options
243 if ( ! empty($this->options) ) {
244 return $this->options;
245 }
246 }
247
248 return $default;
249 }
250
251 /**
252 * set an option of the ad
253 *
254 * @since 1.1.0
255 * @param string $option name of the option
256 * @param mixed $value value of the option
257 */
258 public function set_option($option = '', $value = ''){
259 if ( $option == '' ) { return; }
260
261 // get current options
262 $options = $this->options();
263
264 // set options
265 $options[$option] = $value;
266
267 // save options
268 $this->options = $options;
269
270 }
271
272
273 /**
274 * return ad content for frontend output
275 *
276 * @since 1.0.0
277 * @param array $output_options output options
278 * @return string $output ad output
279 */
280 public function output( $output_options = array() ){
281 if ( ! $this->is_ad ) { return ''; }
282
283 $output_options['global_output'] = $this->global_output = isset( $output_options['global_output'] ) ? $output_options['global_output'] : $this->global_output;
284
285 // switch between normal and debug mode
286 // check if debug output should only be displayed to admins
287 $user_can_manage_ads = current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') );
288 if( isset( $this->output['debugmode'] )
289 && ( $user_can_manage_ads || ( ! $user_can_manage_ads && ! defined('ADVANCED_ADS_AD_DEBUG_FOR_ADMIN_ONLY') ) ) ){
290 $debug = new Advanced_Ads_Ad_Debug;
291 return $debug->prepare_debug_output( $this );
292 } else {
293 $output = $this->prepare_frontend_output();
294 }
295
296 // add the ad to the global output array
297 $advads = Advanced_Ads::get_instance();
298 if ( $output_options['global_output'] ) {
299 $new_ad = array('type' => 'ad', 'id' => $this->id, 'title' => $this->title, 'output' => $output);
300 // if ( method_exists( 'Advanced_Ads_Tracking_Plugin' , 'check_ad_tracking_enabled' ) ) {
301 // if ( class_exists( 'Advanced_Ads_Tracking_Plugin', false ) ) {
302 if ( defined( 'AAT_VERSION' ) && -1 < version_compare( AAT_VERSION, '1.4.2' ) ) {
303 $new_ad['tracking_enabled'] = Advanced_Ads_Tracking_Plugin::get_instance()->check_ad_tracking_enabled( $this );
304 }
305 $advads->current_ads[] = $new_ad;
306 }
307
308 // action when output is created
309 do_action( 'advanced-ads-output', $this, $output, $output_options );
310
311 return apply_filters( 'advanced-ads-output-final', $output, $this, $output_options );
312 }
313
314 /**
315 * check if the ad can be displayed in frontend due to its own conditions
316 *
317 * @since 1.0.0
318 * @param array $check_options check options
319 * @return bool $can_display true if can be displayed in frontend
320 */
321 public function can_display( $check_options = array() ) {
322 $check_options = wp_parse_args( $check_options, array( 'passive_cache_busting' => false, 'ignore_debugmode' => false ) );
323
324 // prevent ad to show up through wp_head, if this is not a header placement
325 if( doing_action( 'wp_head' ) && isset( $this->options['placement_type'] ) && 'header' !== $this->options['placement_type'] ){
326 return false;
327 }
328
329 // force ad display if debug mode is enabled
330 if( isset( $this->output['debugmode'] ) && ! $check_options['ignore_debugmode'] ) {
331 return true;
332 }
333
334 if ( ! $check_options['passive_cache_busting'] ) {
335 // don’t display ads that are not published or private for users not logged in
336 if ( $this->status !== 'publish' && ! ($this->status === 'private' && is_user_logged_in() ) ) {
337 return false;
338 }
339
340 if ( ! $this->can_display_by_visitor() || ! $this->can_display_by_expiry_date() ) {
341 return false;
342 }
343 } else {
344 if ( $this->status !== 'publish' || ! $this->can_display_by_expiry_date() ) {
345 return false;
346 }
347 }
348
349 // add own conditions to flag output as possible or not
350 $can_display = apply_filters( 'advanced-ads-can-display', true, $this, $check_options );
351
352 return $can_display;
353 }
354
355 /**
356 * check visitor conditions
357 *
358 * @since 1.1.0
359 * @return bool $can_display true if can be displayed in frontend based on visitor settings
360 */
361 public function can_display_by_visitor(){
362 if ( ! empty( $this->options['wp_the_query']['is_feed'] ) ) {
363 return true;
364 }
365
366 // check old "visitor" and new "visitors" conditions
367 if ( ( empty($this->options['visitors']) ||
368 ! is_array( $this->options['visitors'] ) )
369 && ( empty($this->options['visitor']) ||
370 ! is_array( $this->options['visitor'] )
371 )) { return true; }
372
373 if ( isset( $this->options['visitors'] ) && is_array( $this->options['visitors'] ) ) {
374
375 $visitor_conditions = $this->options['visitors'];
376
377 $last_result = false;
378 $length = count( $visitor_conditions );
379
380 for($i = 0; $i < $length; ++$i) {
381 $_condition = current( $visitor_conditions );
382 // ignore OR if last result was true
383 if( $last_result && isset( $_condition['connector'] ) && 'or' === $_condition['connector'] ){
384 next( $visitor_conditions );
385 continue;
386 }
387 $last_result = $result = Advanced_Ads_Visitor_Conditions::frontend_check( $_condition, $this );
388 if( ! $result ) {
389 // return false only, if the next condition doesn’t have an OR operator
390 $next = next( $visitor_conditions );
391 if( ! isset( $next['connector'] ) || $next['connector'] !== 'or' ) {
392 return false;
393 }
394 } else {
395 next( $visitor_conditions );
396 }
397 }
398 }
399
400 /**
401 * "old" visitor conditions
402 *
403 * @deprecated since version 1.5.4
404 */
405
406 if ( empty($this->options['visitor']) ||
407 ! is_array( $this->options['visitor'] ) ) { return true; }
408 $visitor_conditions = $this->options( 'visitor' );
409
410 // check mobile condition
411 if ( isset($visitor_conditions['mobile']) ){
412 switch ( $visitor_conditions['mobile'] ){
413 case 'only' :
414 if ( ! wp_is_mobile() ) { return false; }
415 break;
416 case 'no' :
417 if ( wp_is_mobile() ) { return false; }
418 break;
419 }
420 }
421
422 return true;
423 }
424
425 /**
426 * check expiry date
427 *
428 * @since 1.3.15
429 * @return bool $can_display true if can be displayed in frontend based on expiry date
430 */
431 public function can_display_by_expiry_date(){
432
433 // if expiry_date is not set null is returned
434 $ad_expiry_date = (int) $this->options( 'expiry_date' );
435
436 if ( $ad_expiry_date <= 0 || $ad_expiry_date > time() ) {
437 return true;
438 }
439
440 // set status to 'draft' if the ad is expired
441 if ( $this->status !== 'draft' ) {
442 wp_update_post( array( 'ID' => $this->id, 'post_status' => 'draft' ) );
443 }
444
445 return false;
446 }
447
448 /**
449 * save an ad to the database
450 * takes values from the current state
451 */
452 public function save(){
453 global $wpdb;
454
455 // remove slashes from content
456 $content = $this->prepare_content_to_save();
457
458 $where = array('ID' => $this->id);
459 $wpdb->update( $wpdb->posts, array( 'post_content' => $content ), $where );
460
461 // clean post from object cache
462 clean_post_cache( $this->id );
463
464 // sanitize conditions
465 // see sanitize_conditions function for example on using this filter
466 $conditions = self::sanitize_conditions_on_save( $this->conditions );
467
468 // save other options to post meta field
469 $options = $this->options();
470
471 $options['type'] = $this->type;
472 $options['url'] = $this->url;
473 $options['width'] = $this->width;
474 $options['height'] = $this->height;
475 $options['conditions'] = $conditions;
476 $options['expiry_date'] = $this->expiry_date;
477 $options['description'] = $this->description;
478
479 // filter to manipulate options or add more to be saved
480 $options = apply_filters( 'advanced-ads-save-options', $options, $this );
481
482 update_post_meta( $this->id, self::$options_meta_field, $options );
483 }
484
485 /**
486 * native filter for content field before being saved
487 *
488 * @return string $content ad content
489 * @since 1.0.0
490 */
491 public function prepare_content_to_save() {
492
493 $content = $this->content;
494
495 // load ad type specific parameter filter
496 // @todo this is just a hotfix for type_obj not set, yet the cause is still unknown
497 if(is_object( $this->type_obj )){
498 $content = $this->type_obj->sanitize_content( $content );
499 }
500 // apply a custom filter by ad type
501 $content = apply_filters( 'advanced-ads-pre-ad-save-' . $this->type, $content );
502
503 return $content;
504 }
505
506 /**
507 * native filter for ad parameters before being saved
508 *
509 * @return arr $parameters sanitized parameters
510 */
511 public function prepare_parameters_to_save() {
512
513 $parameters = $this->parameters;
514 // load ad type specific parameter filter
515 $parameters = $this->type_obj->sanitize_parameters( $parameters );
516
517 // apply native WP filter for content fields
518 return $parameters;
519 }
520
521 /**
522 * prepare ads output
523 *
524 */
525 public function prepare_frontend_output() {
526 $options = $this->options();
527
528 if ( isset( $options['change-ad']['content'] ) ) {
529 // output was provided by the user
530 $output = $options['change-ad']['content'];
531 } else {
532 // load ad type specific content filter
533 $output = $this->type_obj->prepare_output( $this );
534 }
535
536 // don’t deliver anything, if main ad content is empty
537 if ( $output == '' ) {
538 return;
539 }
540
541 // filter to manipulate the output before the wrapper is added
542 $output = apply_filters( 'advanced-ads-output-inside-wrapper', $output, $this );
543
544 if ( $this->label ) {
545 $output = $this->label . $output;
546 }
547
548 // build wrapper around the ad
549 $output = $this->add_wrapper( $output );
550
551 // add a clearfix, if set
552 if ( isset($this->output['clearfix']) && $this->output['clearfix'] ){
553 $output .= '<br style="clear: both; display: block; float: none;"/>';
554 }
555
556 // apply a custom filter by ad type
557 $output = apply_filters( 'advanced-ads-ad-output', $output, $this );
558
559 return $output;
560 }
561
562 /**
563 * sanitize ad display conditions when saving the ad
564 *
565 * @param array $conditions conditions array send via the dashboard form for an ad
566 * @return array with sanitized conditions
567 * @since 1.0.0
568 */
569 public function sanitize_conditions_on_save($conditions = array()){
570
571 global $advanced_ads_ad_conditions;
572
573 if ( ! is_array( $conditions ) || $conditions == array() ) { return array(); }
574
575 foreach ( $conditions as $_key => $_condition ){
576 if ( $_key == 'postids' ){
577 // sanitize single post conditions
578 if ( empty($_condition['ids']) ){ // remove, if empty
579 $_condition['include'] = array();
580 $_condition['exclude'] = array();
581 } elseif( isset( $_condition['method'] ) ) {
582 switch ( $_condition['method'] ){
583 case 'include' :
584 $_condition['include'] = $_condition['ids'];
585 $_condition['exclude'] = array();
586 break;
587 case 'exclude' :
588 $_condition['include'] = array();
589 $_condition['exclude'] = $_condition['ids'];
590 break;
591 }
592 }
593 } else {
594 if ( ! is_array( $_condition ) ) {
595 $_condition = trim( $_condition ); }
596 if ( $_condition == '' ) {
597 $conditions[$_key] = $_condition;
598 continue;
599 }
600 }
601 $type = ! empty($advanced_ads_ad_conditions[$_key]['type']) ? $advanced_ads_ad_conditions[$_key]['type'] : 0;
602 if ( empty($type) ) { continue; }
603
604 // dynamically apply filters for each condition used
605 $conditions[$_key] = apply_filters( 'advanced-ads-sanitize-condition-' . $type, $_condition );
606 }
607 return $conditions;
608 }
609
610 /**
611 * sanitize id input field(s) for pattern /1,2,3,4/
612 *
613 * @pararm array/string $cond input string/array
614 * @return array/string $cond sanitized string/array
615 */
616 public static function sanitize_condition_idfield($cond = ''){
617 // strip anything that is not comma or number
618
619 if ( is_array( $cond ) ){
620 foreach ( $cond as $_key => $_cond ){
621 $cond[$_key] = preg_replace( '#[^0-9,]#', '', $_cond );
622 }
623 } else {
624 $cond = preg_replace( '#[^0-9,]#', '', $cond );
625 }
626 return $cond;
627 }
628
629 /**
630 * sanitize radio input field
631 *
632 * @pararm string $string input string
633 * @return string $string sanitized string
634 */
635 public static function sanitize_condition_radio($string = ''){
636 // only allow 0, 1 and empty
637 return $string = preg_replace( '#[^01]#', '', $string );
638 }
639
640 /**
641 * sanitize comma seperated text input field
642 *
643 * @pararm array/string $cond input string/array
644 * @return array/string $cond sanitized string/array
645 */
646 public static function sanitize_condition_textvalues($cond = ''){
647 // strip anything that is not comma, alphanumeric, minus and underscore
648 if ( is_array( $cond ) ){
649 foreach ( $cond as $_key => $_cond ){
650 $cond[$_key] = preg_replace( '#[^0-9,A-Za-z-_]#', '', $_cond );
651 }
652 } else {
653 $cond = preg_replace( '#[^0-9,A-Za-z-_]#', '', $cond );
654 }
655 return $cond;
656 }
657
658 /**
659 * load wrapper options set with the ad
660 *
661 * @since 1.3
662 * @return arr $wrapper options array ready to be use in add_wrapper() function
663 */
664 protected function load_wrapper_options(){
665 $wrapper = array();
666
667 // print_r($this->output);
668
669 if ( ! empty($this->output['position']) ) {
670 switch ( $this->output['position'] ) {
671 case 'left' :
672 $wrapper['style']['float'] = 'left';
673 break;
674 case 'right' :
675 $wrapper['style']['float'] = 'right';
676 break;
677 case 'center' :
678 $wrapper['style']['text-align'] = 'center';
679 // add css rule after wrapper to center the ad
680 // add_filter( 'advanced-ads-output-wrapper-after-content', array( $this, 'center_ad_content' ), 10, 2 );
681 break;
682 case 'clearfix' :
683 $wrapper['style']['clear'] = 'both';
684 break;
685 }
686 }
687
688 if ( isset($this->output['class'] ) && is_array( $this->output['class'] )
689 && ( ! isset( $this->args['previous_method'] ) || 'group' !== $this->args['previous_method'] )
690 ) {
691 $wrapper['class'] = $this->output['class'];
692 }
693
694 // add manual classes
695 if ( isset($this->output['wrapper-class']) && '' !== $this->output['wrapper-class'] ) {
696 $classes = explode( ' ', $this->output['wrapper-class'] );
697
698 foreach( $classes as $_class ){
699 $wrapper['class'][] = sanitize_text_field( $_class );
700 }
701 }
702
703 if ( ! empty($this->output['margin']['top']) ) {
704 $wrapper['style']['margin-top'] = intval( $this->output['margin']['top'] ) . 'px';
705 }
706 if ( ! empty($this->output['margin']['right']) ) {
707 $wrapper['style']['margin-right'] = intval( $this->output['margin']['right'] ) . 'px';
708 }
709 if ( ! empty($this->output['margin']['bottom']) ) {
710 $wrapper['style']['margin-bottom'] = intval( $this->output['margin']['bottom'] ) . 'px';
711 }
712 if ( ! empty($this->output['margin']['left']) ) {
713 $wrapper['style']['margin-left'] = intval( $this->output['margin']['left'] ) . 'px';
714 }
715
716 if ( ! empty ($this->output['add_wrapper_sizes'] ) ) {
717 $wrapper['style']['width'] = intval( $this->width ) . 'px';
718 $wrapper['style']['height'] = intval( $this->height ) . 'px';
719 }
720
721 return $wrapper;
722 }
723
724 /**
725 * add a wrapper arount the ad content if wrapper information are given
726 *
727 * @since 1.1.4
728 * @param str $ad_content content of the ad
729 * @return str $wrapper ad within the wrapper
730 */
731 protected function add_wrapper($ad_content = ''){
732 $wrapper_options = apply_filters( 'advanced-ads-output-wrapper-options', $this->wrapper, $this );
733
734 if ( ( ! isset( $this->output['wrapper-id'] ) || '' === $this->output['wrapper-id'] )
735 && $wrapper_options == array() || ! is_array( $wrapper_options ) ) { return $ad_content; }
736
737 // create unique id if not yet given
738 if ( empty($wrapper_options['id']) ){
739 $this->wrapper['id'] = $wrapper_options['id'] = $this->create_wrapper_id();
740 }
741
742 // build the box
743 $wrapper = '<div' . Advanced_Ads_Utils::build_html_attributes( $wrapper_options ) . '>';
744 $wrapper .= apply_filters( 'advanced-ads-output-wrapper-before-content', '', $this );
745 $wrapper .= $ad_content;
746 $wrapper .= apply_filters( 'advanced-ads-output-wrapper-after-content', '', $this );
747 $wrapper .= '</div>';
748
749 return $wrapper;
750 }
751
752 /**
753 * function to add css rule after the ad to center its content
754 *
755 * @since 1.6.9.5
756 * @param str $output additional output in wrapper after content
757 * @param obj $ad Advanced_Ads_Ad object
758 * @return str $output
759 *
760 */
761 /*public function center_ad_content( $output, Advanced_Ads_Ad $ad ){
762
763 // no additional check needed, because the hook is only called when the ad is centered
764 if( isset( $ad->wrapper['id'] )){
765 // does not work with most div elements, so actually not used now
766 $output .= '<style type="text/css">#'. $ad->wrapper['id'] . ' img, #'. $ad->wrapper['id'] . ' div { display: inline !important; }</style>';
767 }
768
769 return $output;
770 }*/
771
772 /**
773 * create a random wrapper id
774 *
775 * @since 1.1.4
776 * @return string $id random id string
777 */
778 private function create_wrapper_id(){
779
780 if( isset( $this->output['wrapper-id'] )){
781 $id = sanitize_key( $this->output['wrapper-id'] );
782 if( '' !== $id ){
783 return $id;
784 }
785 }
786
787 $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix();
788
789 return $prefix . mt_rand();
790 }
791
792 /**
793 * Create an "Advertisement" label if conditions are met.
794 */
795 public function maybe_create_label() {
796 if ( ( ! isset( $this->args['placement_type'] ) || $this->args['placement_type'] !== 'header' ) &&
797 $this->type !== 'group' &&
798 $label = Advanced_Ads::get_instance()->get_label()
799 ) {
800 // ignore slider and group with refresh
801 if ( ( isset( $this->args['group_info']['type'] ) && $this->args['group_info']['type'] === 'slider' )
802 || ! empty( $this->args['group_info']['refresh_enabled'] )
803 ) {
804 return;
805 }
806 // Label probably will be shown.
807 // Passive cache-busting can display it above any ad.
808 $this->need_wrapper = true;
809
810 // first ad in a group or single ad without group, and not group output for passive cb
811 if ( empty( $this->args['group_info']['ads_displayed'] ) && empty( $this->args['group_info']['passive_cb'] ) ) {
812 $this->label = $label;
813 }
814 }
815 }
816
817
818
819 }
820