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