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