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