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