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