PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.7
Advanced Ads – Ad Manager & AdSense v1.7
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
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 display-conditions.php 10 years ago plugin.php 10 years ago upgrades.php 10 years ago visitor-conditions.php 10 years ago widget.php 10 years ago
ad.php
743 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 * @param array $output_options output options
255 * @return string $output ad output
256 */
257 public function output( $output_options = array() ){
258 if ( ! $this->is_ad ) { return ''; }
259
260 $output_options = wp_parse_args( $output_options, array( 'global_output' => true ) );
261
262 $output = $this->prepare_frontend_output();
263
264 if ( $output_options['global_output'] ) {
265 // add the ad to the global output array
266 $advads = Advanced_Ads::get_instance();
267 $advads->current_ads[] = array('type' => 'ad', 'id' => $this->id, 'title' => $this->title, 'output' => $output);
268 }
269
270 // action when output is created
271 do_action( 'advanced-ads-output', $this, $output, $output_options );
272
273 return $output;
274 }
275
276 /**
277 * check if the ad can be displayed in frontend due to its own conditions
278 *
279 * @since 1.0.0
280 * @param array $check_options check options
281 * @return bool $can_display true if can be displayed in frontend
282 */
283 public function can_display( $check_options = array() ) {
284 $check_options = wp_parse_args( $check_options, array( 'passive_cache_busting' => false ) );
285
286 // prevent ad to show up through wp_head, if this is not a header placement
287 if( doing_action( 'wp_head' ) && isset( $this->options['placement_type'] ) && 'header' !== $this->options['placement_type'] ){
288 return false;
289 }
290
291 if ( ! $check_options['passive_cache_busting'] ) {
292 // don’t display ads that are not published or private for users not logged in
293 if ( $this->status !== 'publish' && ! ($this->status === 'private' && ! is_user_logged_in() ) ) {
294 return false;
295 }
296
297 if ( ! $this->can_display_by_visitor() || ! $this->can_display_by_expiry_date() ) {
298 return false;
299 }
300 } else {
301 if ( $this->status !== 'publish' || ! $this->can_display_by_expiry_date() ) {
302 return false;
303 }
304 }
305
306 // add own conditions to flag output as possible or not
307 $can_display = apply_filters( 'advanced-ads-can-display', true, $this, $check_options );
308
309 return $can_display;
310 }
311
312 /**
313 * check visitor conditions
314 *
315 * @since 1.1.0
316 * @return bool $can_display true if can be displayed in frontend based on visitor settings
317 */
318 public function can_display_by_visitor(){
319
320 // check old "visitor" and new "visitors" conditions
321 if ( ( empty($this->options['visitors']) ||
322 ! is_array( $this->options['visitors'] ) )
323 && ( empty($this->options['visitor']) ||
324 ! is_array( $this->options['visitor'] )
325 )) { return true; }
326
327 if ( isset( $this->options['visitors'] ) && is_array( $this->options['visitors'] ) ) {
328
329 $visitor_conditions = $this->options['visitors'];
330
331 $last_result = false;
332 $length = count( $visitor_conditions );
333
334 for($i = 0; $i < $length; ++$i) {
335 $_condition = current( $visitor_conditions );
336 // ignore OR if last result was true
337 if( $last_result && isset( $_condition['connector'] ) && 'or' === $_condition['connector'] ){
338 next( $visitor_conditions );
339 continue;
340 }
341 $last_result = $result = Advanced_Ads_Visitor_Conditions::frontend_check( $_condition, $this );
342 if( ! $result ) {
343 // return false only, if the next condition doesn’t have an OR operator
344 $next = next( $visitor_conditions );
345 if( ! isset( $next['connector'] ) || $next['connector'] !== 'or' ) {
346 return false;
347 }
348 }
349 next( $visitor_conditions );
350 }
351 }
352
353 /**
354 * "old" visitor conditions
355 *
356 * @deprecated since version 1.5.4
357 */
358
359 if ( empty($this->options['visitor']) ||
360 ! is_array( $this->options['visitor'] ) ) { return true; }
361 $visitor_conditions = $this->options( 'visitor' );
362
363 // check mobile condition
364 if ( isset($visitor_conditions['mobile']) ){
365 switch ( $visitor_conditions['mobile'] ){
366 case 'only' :
367 if ( ! wp_is_mobile() ) { return false; }
368 break;
369 case 'no' :
370 if ( wp_is_mobile() ) { return false; }
371 break;
372 }
373 }
374
375 return true;
376 }
377
378 /**
379 * check expiry date
380 *
381 * @since 1.3.15
382 * @return bool $can_display true if can be displayed in frontend based on expiry date
383 */
384 public function can_display_by_expiry_date(){
385
386 // if expiry_date is not set null is returned
387 $ad_expiry_date = (int) $this->options( 'expiry_date' );
388
389 if ( $ad_expiry_date <= 0 ) {
390 return true;
391 }
392
393 // check blog time against current time (GMT)
394 return $ad_expiry_date > time();
395 }
396
397 /**
398 * save an ad to the database
399 * takes values from the current state
400 */
401 public function save(){
402 global $wpdb;
403
404 // remove slashes from content
405 $content = $this->prepare_content_to_save();
406
407 $where = array('ID' => $this->id);
408 $wpdb->update( $wpdb->posts, array( 'post_content' => $content ), $where );
409
410 // clean post from object cache
411 clean_post_cache( $this->id );
412
413 // sanitize conditions
414 // see sanitize_conditions function for example on using this filter
415 $conditions = self::sanitize_conditions_on_save( $this->conditions );
416
417 // save other options to post meta field
418 $options = $this->options();
419
420 $options['type'] = $this->type;
421 $options['url'] = $this->url;
422 $options['width'] = $this->width;
423 $options['height'] = $this->height;
424 $options['conditions'] = $conditions;
425 $options['expiry_date'] = $this->expiry_date;
426 $options['description'] = $this->description;
427
428 // filter to manipulate options or add more to be saved
429 $options = apply_filters( 'advanced-ads-save-options', $options, $this );
430
431 update_post_meta( $this->id, self::$options_meta_field, $options );
432 }
433
434 /**
435 * native filter for content field before being saved
436 *
437 * @return string $content ad content
438 * @since 1.0.0
439 */
440 public function prepare_content_to_save() {
441
442 $content = $this->content;
443
444 // load ad type specific parameter filter
445 $content = $this->type_obj->sanitize_content( $content );
446 // apply a custom filter by ad type
447 $content = apply_filters( 'advanced-ads-pre-ad-save-' . $this->type, $content );
448
449 return $content;
450 }
451
452 /**
453 * native filter for ad parameters before being saved
454 *
455 * @return arr $parameters sanitized parameters
456 */
457 public function prepare_parameters_to_save() {
458
459 $parameters = $this->parameters;
460 // load ad type specific parameter filter
461 $parameters = $this->type_obj->sanitize_parameters( $parameters );
462
463 // apply native WP filter for content fields
464 return $parameters;
465 }
466
467 /**
468 * prepare ads output
469 *
470 * @param string $content ad content
471 * @param obj $ad ad object
472 */
473 public function prepare_frontend_output() {
474
475 // load ad type specific content filter
476 $output = $this->type_obj->prepare_output( $this );
477 // don’t deliver anything, if main ad content is empty
478 if( $output == '' ) {
479 return;
480 }
481
482 // filter to manipulate the output before the wrapper is added
483 $output = apply_filters( 'advanced-ads-output-inside-wrapper', $output, $this );
484
485 // build wrapper around the ad
486 $output = $this->add_wrapper( $output );
487
488 // add a clearfix, if set
489 if ( isset($this->output['clearfix']) && $this->output['clearfix'] ){
490 $output .= '<br style="clear: both; display: block; float: none;"/>';
491 }
492
493 // apply a custom filter by ad type
494 $output = apply_filters( 'advanced-ads-ad-output', $output, $this );
495
496 return $output;
497 }
498
499 /**
500 * sanitize ad display conditions when saving the ad
501 *
502 * @param array $conditions conditions array send via the dashboard form for an ad
503 * @return array with sanitized conditions
504 * @since 1.0.0
505 */
506 public function sanitize_conditions_on_save($conditions = array()){
507
508 global $advanced_ads_ad_conditions;
509
510 if ( ! is_array( $conditions ) || $conditions == array() ) { return array(); }
511
512 foreach ( $conditions as $_key => $_condition ){
513 if ( $_key == 'postids' ){
514 // sanitize single post conditions
515 if ( empty($_condition['ids']) ){ // remove, if empty
516 $_condition['include'] = array();
517 $_condition['exclude'] = array();
518 } elseif( isset( $_condition['method'] ) ) {
519 switch ( $_condition['method'] ){
520 case 'include' :
521 $_condition['include'] = $_condition['ids'];
522 $_condition['exclude'] = array();
523 break;
524 case 'exclude' :
525 $_condition['include'] = array();
526 $_condition['exclude'] = $_condition['ids'];
527 break;
528 }
529 }
530 } else {
531 if ( ! is_array( $_condition ) ) {
532 $_condition = trim( $_condition ); }
533 if ( $_condition == '' ) {
534 $conditions[$_key] = $_condition;
535 continue;
536 }
537 }
538 $type = ! empty($advanced_ads_ad_conditions[$_key]['type']) ? $advanced_ads_ad_conditions[$_key]['type'] : 0;
539 if ( empty($type) ) { continue; }
540
541 // dynamically apply filters for each condition used
542 $conditions[$_key] = apply_filters( 'advanced-ads-sanitize-condition-' . $type, $_condition );
543 }
544 return $conditions;
545 }
546
547 /**
548 * sanitize id input field(s) for pattern /1,2,3,4/
549 *
550 * @pararm array/string $cond input string/array
551 * @return array/string $cond sanitized string/array
552 */
553 public static function sanitize_condition_idfield($cond = ''){
554 // strip anything that is not comma or number
555
556 if ( is_array( $cond ) ){
557 foreach ( $cond as $_key => $_cond ){
558 $cond[$_key] = preg_replace( '#[^0-9,]#', '', $_cond );
559 }
560 } else {
561 $cond = preg_replace( '#[^0-9,]#', '', $cond );
562 }
563 return $cond;
564 }
565
566 /**
567 * sanitize radio input field
568 *
569 * @pararm string $string input string
570 * @return string $string sanitized string
571 */
572 public static function sanitize_condition_radio($string = ''){
573 // only allow 0, 1 and empty
574 return $string = preg_replace( '#[^01]#', '', $string );
575 }
576
577 /**
578 * sanitize comma seperated text input field
579 *
580 * @pararm array/string $cond input string/array
581 * @return array/string $cond sanitized string/array
582 */
583 public static function sanitize_condition_textvalues($cond = ''){
584 // strip anything that is not comma, alphanumeric, minus and underscore
585 if ( is_array( $cond ) ){
586 foreach ( $cond as $_key => $_cond ){
587 $cond[$_key] = preg_replace( '#[^0-9,A-Za-z-_]#', '', $_cond );
588 }
589 } else {
590 $cond = preg_replace( '#[^0-9,A-Za-z-_]#', '', $cond );
591 }
592 return $cond;
593 }
594
595 /**
596 * load wrapper options set with the ad
597 *
598 * @since 1.3
599 * @return arr $wrapper options array ready to be use in add_wrapper() function
600 */
601 protected function load_wrapper_options(){
602 $wrapper = array();
603
604 // print_r($this->output);
605
606 if ( ! empty($this->output['position']) ) {
607 switch ( $this->output['position'] ) {
608 case 'left' :
609 $wrapper['style']['float'] = 'left';
610 break;
611 case 'right' :
612 $wrapper['style']['float'] = 'right';
613 break;
614 case 'center' :
615 $wrapper['style']['text-align'] = 'center';
616 // add css rule after wrapper to center the ad
617 // add_filter( 'advanced-ads-output-wrapper-after-content', array( $this, 'center_ad_content' ), 10, 2 );
618 break;
619 case 'clearfix' :
620 $wrapper['style']['clear'] = 'both';
621 break;
622 }
623 }
624
625 if ( isset($this->output['class']) && is_array( $this->output['class'] ) ) {
626 $wrapper['class'] = $this->output['class'];
627 }
628
629 // add manual classes
630 if ( isset($this->output['wrapper-class']) && '' !== $this->output['wrapper-class'] ) {
631 $classes = explode( ' ', $this->output['wrapper-class'] );
632
633 foreach( $classes as $_class ){
634 $wrapper['class'][] = sanitize_key( $_class );
635 }
636 }
637
638 if ( ! empty($this->output['margin']['top']) ) {
639 $wrapper['style']['margin-top'] = intval( $this->output['margin']['top'] ) . 'px';
640 }
641 if ( ! empty($this->output['margin']['right']) ) {
642 $wrapper['style']['margin-right'] = intval( $this->output['margin']['right'] ) . 'px';
643 }
644 if ( ! empty($this->output['margin']['bottom']) ) {
645 $wrapper['style']['margin-bottom'] = intval( $this->output['margin']['bottom'] ) . 'px';
646 }
647 if ( ! empty($this->output['margin']['left']) ) {
648 $wrapper['style']['margin-left'] = intval( $this->output['margin']['left'] ) . 'px';
649 }
650
651 return $wrapper;
652 }
653
654 /**
655 * add a wrapper arount the ad content if wrapper information are given
656 *
657 * @since 1.1.4
658 * @param str $ad_content content of the ad
659 * @return str $wrapper ad within the wrapper
660 */
661 protected function add_wrapper($ad_content = ''){
662
663 $wrapper_options = apply_filters( 'advanced-ads-output-wrapper-options', $this->wrapper, $this );
664
665 if ( $wrapper_options == array() || ! is_array( $wrapper_options ) || empty($wrapper_options) ) { return $ad_content; }
666
667 $wrapper = $ad_content;
668
669 // create unique id if not yet given
670 if ( empty($wrapper_options['id']) ){
671 $this->wrapper['id'] = $wrapper_options['id'] = $this->create_wrapper_id();
672 }
673
674 // build the box
675 $wrapper = '<div';
676 foreach ( $wrapper_options as $_html_attr => $_values ){
677 if ( $_html_attr == 'style' ){
678 $_style_values_string = '';
679 foreach ( $_values as $_style_attr => $_style_values ){
680 if ( is_array( $_style_values ) ) {
681 $_style_values_string .= $_style_attr . ': ' .implode( ' ', $_style_values ). '; '; }
682 else {
683 $_style_values_string .= $_style_attr . ': ' .$_style_values. '; '; }
684 }
685 $wrapper .= " style=\"$_style_values_string\"";
686 } else {
687 if ( is_array( $_values ) ) {
688 $_values_string = implode( ' ', $_values ); }
689 else {
690 $_values_string = sanitize_title( $_values ); }
691 $wrapper .= " $_html_attr=\"$_values_string\"";
692 }
693 }
694 $wrapper .= '>';
695 $wrapper .= apply_filters( 'advanced-ads-output-wrapper-before-content', '', $this );
696 $wrapper .= $ad_content;
697 $wrapper .= apply_filters( 'advanced-ads-output-wrapper-after-content', '', $this );
698 $wrapper .= '</div>';
699
700 return $wrapper;
701 }
702
703 /**
704 * function to add css rule after the ad to center its content
705 *
706 * @since 1.6.9.5
707 * @param str $output additional output in wrapper after content
708 * @param obj $ad Advanced_Ads_Ad object
709 * @return str $output
710 *
711 */
712 /*public function center_ad_content( $output, Advanced_Ads_Ad $ad ){
713
714 // no additional check needed, because the hook is only called when the ad is centered
715 if( isset( $ad->wrapper['id'] )){
716 // does not work with most div elements, so actually not used now
717 $output .= '<style type="text/css">#'. $ad->wrapper['id'] . ' img, #'. $ad->wrapper['id'] . ' div { display: inline !important; }</style>';
718 }
719
720 return $output;
721 }*/
722
723 /**
724 * create a random wrapper id
725 *
726 * @since 1.1.4
727 * @return string $id random id string
728 */
729 private function create_wrapper_id(){
730
731 if( isset( $this->output['wrapper-id'] )){
732 $id = sanitize_key( $this->output['wrapper-id'] );
733 if( '' !== $id ){
734 return $id;
735 }
736 }
737
738 $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix();
739
740 return $prefix . mt_rand();
741 }
742 }
743