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