ad-ajax.php
11 years ago
ad-model.php
11 years ago
ad-select.php
11 years ago
ad.php
11 years ago
ad_ajax_callbacks.php
11 years ago
ad_group.php
11 years ago
ad_placements.php
11 years ago
ad_type_abstract.php
11 years ago
ad_type_content.php
11 years ago
ad_type_plain.php
11 years ago
plugin.php
11 years ago
widget.php
11 years ago
ad.php
841 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Advanced Ads. |
| 5 | * |
| 6 | * @package Advads_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 Advads_Ad |
| 17 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 18 | */ |
| 19 | class Advads_Ad { |
| 20 | |
| 21 | /** |
| 22 | * id of the post type for this ad |
| 23 | */ |
| 24 | public $id = 0; |
| 25 | |
| 26 | /** |
| 27 | * true, if this is an Advanced Ads Ad post type |
| 28 | */ |
| 29 | protected $is_ad = false; |
| 30 | |
| 31 | /** |
| 32 | * ad type |
| 33 | */ |
| 34 | public $type = 'content'; |
| 35 | |
| 36 | /** |
| 37 | * ad width |
| 38 | */ |
| 39 | public $width = 0; |
| 40 | |
| 41 | /** |
| 42 | * ad height |
| 43 | */ |
| 44 | public $height = 0; |
| 45 | |
| 46 | /** |
| 47 | * object of current ad type |
| 48 | */ |
| 49 | protected $type_obj; |
| 50 | |
| 51 | /** |
| 52 | * content of the ad |
| 53 | * |
| 54 | * only needed for ad types using the post content field |
| 55 | */ |
| 56 | public $content = ''; |
| 57 | |
| 58 | /** |
| 59 | * conditions of the ad display |
| 60 | */ |
| 61 | public $conditions = array(); |
| 62 | |
| 63 | /** |
| 64 | * status of the ad (e.g. publish, pending) |
| 65 | */ |
| 66 | public $status = array(); |
| 67 | |
| 68 | /** |
| 69 | * array with meta field options aka parameters |
| 70 | */ |
| 71 | protected $options = array(); |
| 72 | |
| 73 | /** |
| 74 | * name of the meta field to save options to |
| 75 | */ |
| 76 | static $options_meta_field = 'advanced_ads_ad_options'; |
| 77 | |
| 78 | /** |
| 79 | * additional arguments set when ad is loaded, overwrites or extends options |
| 80 | */ |
| 81 | public $args = array(); |
| 82 | |
| 83 | /** |
| 84 | * multidimensional array contains information about the wrapper |
| 85 | * each possible html attribute is an array with possible multiple elements |
| 86 | */ |
| 87 | public $wrapper = array(); |
| 88 | |
| 89 | /** |
| 90 | * init ad object |
| 91 | * |
| 92 | * @param int $id id of the ad (= post id) |
| 93 | * @param arr $args additional arguments |
| 94 | */ |
| 95 | public function __construct($id, $args = array()) { |
| 96 | $id = absint( $id ); |
| 97 | $this->id = $id; |
| 98 | $this->args = is_array( $args ) ? $args : array(); |
| 99 | |
| 100 | if ( ! empty($id) ) { $this->load( $id ); } |
| 101 | |
| 102 | // dynamically add sanitize filters for condition types |
| 103 | $_types = array(); |
| 104 | // -TODO use model |
| 105 | $advanced_ads_ad_conditions = Advanced_Ads::get_ad_conditions(); |
| 106 | foreach ( $advanced_ads_ad_conditions as $_condition ) { |
| 107 | // add unique |
| 108 | $_types[$_condition['type']] = false; |
| 109 | } |
| 110 | // iterate types |
| 111 | foreach ( array_keys( $_types ) as $_type ) { |
| 112 | // -TODO might be faster to use __call() method or isset()-test class method array |
| 113 | $method_name = 'sanitize_condition_'. $_type; |
| 114 | if ( method_exists( $this, $method_name ) ) { |
| 115 | add_filter( 'advanced-ads-sanitize-condition-' . $_type, array($this, $method_name), 10, 1 ); |
| 116 | } elseif ( function_exists( 'advads_sanitize_condition_' . $_type ) ) { |
| 117 | // check for public function to sanitize this |
| 118 | add_filter( 'advanced-ads-sanitize-condition-' . $_type, 'advads_sanitize_condition_' . $_type, 10, 1 ); |
| 119 | |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * load an ad object by id based on its ad type |
| 126 | * |
| 127 | * @since 1.0.0 |
| 128 | */ |
| 129 | private function load($id = 0){ |
| 130 | |
| 131 | $_data = get_post( $id ); |
| 132 | if ( $_data == null ) { return false; } |
| 133 | |
| 134 | // return, if not an ad |
| 135 | if ( $_data->post_type != Advanced_Ads::POST_TYPE_SLUG ) { |
| 136 | return false; |
| 137 | } else { |
| 138 | $this->is_ad = true; |
| 139 | } |
| 140 | |
| 141 | $this->type = $this->options( 'type' ); |
| 142 | $this->title = $_data->post_title; |
| 143 | /* load ad type object */ |
| 144 | $types = Advanced_Ads::get_instance()->ad_types; |
| 145 | if ( isset($types[$this->type]) ){ |
| 146 | $this->type_obj = $types[$this->type]; |
| 147 | } else { |
| 148 | $this->type_obj = new Advads_Ad_Type_Abstract; |
| 149 | } |
| 150 | $this->width = $this->options( 'width' ); |
| 151 | $this->height = $this->options( 'height' ); |
| 152 | $this->conditions = $this->options( 'conditions' ); |
| 153 | $this->description = $this->options( 'description' ); |
| 154 | $this->output = $this->options( 'output' ); |
| 155 | $this->status = $_data->post_status; |
| 156 | $this->wrapper = $this->load_wrapper_options(); |
| 157 | $this->expiry_date = $this->options( 'expiry_date' ); |
| 158 | |
| 159 | // load content based on ad type |
| 160 | $this->content = $this->type_obj->load_content( $_data ); |
| 161 | |
| 162 | // set wrapper conditions |
| 163 | $this->wrapper = apply_filters( 'advanced-ads-set-wrapper', $this->wrapper, $this ); |
| 164 | // add unique wrapper id, if options given |
| 165 | if ( is_array( $this->wrapper ) && $this->wrapper !== array() && ! isset($this->wrapper['id']) ){ |
| 166 | // create unique id if not yet given |
| 167 | $this->wrapper['id'] = $this->create_wrapper_id(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * get options from meta field and return specific field |
| 173 | * |
| 174 | * @param string $field post meta key to be returned |
| 175 | * @return mixed meta field content |
| 176 | * @since 1.0.0 |
| 177 | * @todo check against default values |
| 178 | */ |
| 179 | public function options($field = ''){ |
| 180 | // retrieve options, if not given yet |
| 181 | if ( $this->options === array() ) { |
| 182 | // load arguments given on ad load |
| 183 | $this->options = $this->args; |
| 184 | // get_post_meta() may return false |
| 185 | $meta = get_post_meta( $this->id, self::$options_meta_field, true ); |
| 186 | if ( $meta ){ |
| 187 | $this->options = array_merge_recursive( $this->options, $meta ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // return specific option |
| 192 | if ( $field != '' ) { |
| 193 | if ( isset($this->options[$field]) ) { |
| 194 | return $this->options[$field]; } |
| 195 | } else { // return all options |
| 196 | if ( ! empty($this->options) ) { |
| 197 | return $this->options; } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * set an option of the ad |
| 203 | * |
| 204 | * @since 1.1.0 |
| 205 | * @param string $option name of the option |
| 206 | * @param mixed $value value of the option |
| 207 | */ |
| 208 | public function set_option($option = '', $value = ''){ |
| 209 | if ( $option == '' ) { return; } |
| 210 | |
| 211 | // get current options |
| 212 | $options = $this->options(); |
| 213 | |
| 214 | // set options |
| 215 | $options[$option] = $value; |
| 216 | |
| 217 | // save options |
| 218 | $this->options = $options; |
| 219 | |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /** |
| 224 | * return ad content for frontend output |
| 225 | * |
| 226 | * @since 1.0.0 |
| 227 | * @return string $output ad output |
| 228 | */ |
| 229 | public function output(){ |
| 230 | if ( ! $this->is_ad ) { return ''; } |
| 231 | |
| 232 | $output = $this->prepare_frontend_output(); |
| 233 | |
| 234 | // add the ad to the global output array |
| 235 | $advads = Advanced_Ads::get_instance(); |
| 236 | $advads->current_ads[] = array('type' => 'ad', 'id' => $this->id, 'title' => $this->title); |
| 237 | |
| 238 | // action when output is created |
| 239 | do_action( 'advanced-ads-output', $this, $output ); |
| 240 | |
| 241 | return $output; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * check if the ad can be displayed in frontend due to its own conditions |
| 246 | * |
| 247 | * @since 1.0.0 |
| 248 | * @return bool $can_display true if can be displayed in frontend |
| 249 | */ |
| 250 | public function can_display(){ |
| 251 | |
| 252 | // don’t display ads that are not published or private for users not logged in |
| 253 | if ( $this->status !== 'publish' && ! ($this->status === 'private' && ! is_user_logged_in()) ){ |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | if ( ! $this->can_display_by_conditions() |
| 258 | || ! $this->can_display_by_visitor() |
| 259 | || ! $this->can_display_by_expiry_date() ) { |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | // add own conditions to flag output as possible or not |
| 264 | $can_display = apply_filters( 'advanced-ads-can-display', true, $this ); |
| 265 | |
| 266 | return $can_display; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * check display conditions |
| 271 | * |
| 272 | * @since 1.1.0 moved here from can_display() |
| 273 | * @return bool $can_display true if can be displayed in frontend |
| 274 | */ |
| 275 | public function can_display_by_conditions(){ |
| 276 | // use $wp_the_query to check the original query and not custom queries |
| 277 | global $post, $wp_the_query; |
| 278 | |
| 279 | $query = $wp_the_query->get_queried_object(); |
| 280 | |
| 281 | if ( empty($this->options['conditions']) || |
| 282 | ! is_array( $this->options['conditions'] ) ) { return true; } |
| 283 | |
| 284 | // display ad if conditions are explicitely disabled |
| 285 | if ( isset($this->options['conditions']['enabled']) && ! $this->options['conditions']['enabled'] ) { return true; } |
| 286 | |
| 287 | $conditions = $this->options['conditions']; |
| 288 | foreach ( $conditions as $_cond_key => $_cond_value ) { |
| 289 | switch ( $_cond_key ){ |
| 290 | // check for post ids |
| 291 | case 'postids' : |
| 292 | if ( $wp_the_query->is_singular() && empty($_cond_value['all']) ){ |
| 293 | // included posts |
| 294 | if ( ! empty($_cond_value['include']) ){ |
| 295 | if ( is_string( $_cond_value['include'] ) ){ |
| 296 | $post_ids = explode( ',', $_cond_value['include'] ); |
| 297 | } else { |
| 298 | $post_ids = $_cond_value['include']; |
| 299 | } |
| 300 | if ( is_array( $post_ids ) |
| 301 | && isset($post->ID) |
| 302 | && ! in_array( $post->ID, $post_ids ) ) { |
| 303 | return false; } |
| 304 | } |
| 305 | // excluded posts |
| 306 | if ( ! empty($_cond_value['exclude']) ){ |
| 307 | if ( is_string( $_cond_value['exclude'] ) ){ |
| 308 | $post_ids = explode( ',', $_cond_value['exclude'] ); |
| 309 | } else { |
| 310 | $post_ids = $_cond_value['exclude']; |
| 311 | } |
| 312 | if ( is_array( $post_ids ) && isset($post->ID) && in_array( $post->ID, $post_ids ) ){ |
| 313 | return false; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | break; |
| 318 | // check for category ids |
| 319 | case 'categoryids' : |
| 320 | // included |
| 321 | if ( $wp_the_query->is_singular() && empty($_cond_value['all']) ){ |
| 322 | // get all taxonomies of the post |
| 323 | $term_ids = $this->get_object_terms( $post->ID ); |
| 324 | |
| 325 | if ( ! empty($_cond_value['include']) ){ |
| 326 | if ( is_string( $_cond_value['include'] ) ){ |
| 327 | $category_ids = explode( ',', $_cond_value['include'] ); |
| 328 | } else { |
| 329 | $category_ids = $_cond_value['include']; |
| 330 | } |
| 331 | |
| 332 | // check if currently in a post (not post page, but also posts in loops) |
| 333 | if ( is_array( $category_ids ) && isset($post->ID) |
| 334 | && ! count( array_intersect( $category_ids, $term_ids ) ) ) { // is there any taxonomy the same? |
| 335 | return false; |
| 336 | } |
| 337 | } |
| 338 | // check for excluded category ids |
| 339 | if ( ! empty($_cond_value['exclude']) ){ |
| 340 | if ( is_string( $_cond_value['exclude'] ) ){ |
| 341 | $category_ids = explode( ',', $_cond_value['exclude'] ); |
| 342 | } else { |
| 343 | $category_ids = $_cond_value['exclude']; |
| 344 | } |
| 345 | // check if currently in a post (not post page, but also posts in loops) |
| 346 | if ( is_array( $category_ids ) && isset($post->ID) |
| 347 | && count( array_intersect( $category_ids, $term_ids ) ) ) { // is there any taxonomy the same |
| 348 | // being only in one excluded category is enough to not display the ad |
| 349 | return false; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | break; |
| 354 | // check for included category archive ids |
| 355 | // @link http://codex.wordpress.org/Conditional_Tags#A_Category_Page |
| 356 | case 'categoryarchiveids' : |
| 357 | if ( isset($query->term_id) && $wp_the_query->is_archive() && empty($_cond_value['all']) ){ |
| 358 | if ( ! empty($_cond_value['include']) ){ |
| 359 | if ( is_string( $_cond_value['include'] ) ){ |
| 360 | $category_ids = explode( ',', $_cond_value['include'] ); |
| 361 | } else { |
| 362 | $category_ids = $_cond_value['include']; |
| 363 | } |
| 364 | if ( is_array( $category_ids ) && ! in_array( $query->term_id, $category_ids ) ) { |
| 365 | return false; } |
| 366 | } |
| 367 | // check for excluded category archive ids |
| 368 | if ( ! empty($_cond_value['exclude']) ){ |
| 369 | if ( is_string( $_cond_value['exclude'] ) ){ |
| 370 | $category_ids = explode( ',', $_cond_value['exclude'] ); |
| 371 | } else { |
| 372 | $category_ids = $_cond_value['exclude']; |
| 373 | } |
| 374 | if ( is_array( $category_ids ) && in_array( $query->term_id, $category_ids ) ) { |
| 375 | return false; } |
| 376 | } |
| 377 | } |
| 378 | break; |
| 379 | // check for included post types |
| 380 | case 'posttypes' : |
| 381 | // display everywhere, if include not set (= all is checked) |
| 382 | // TODO remove condition check for string; deprecated since 1.2.2 |
| 383 | if ( empty($_cond_value['all']) ){ |
| 384 | if ( ! empty($_cond_value['include']) ){ |
| 385 | if ( is_string( $_cond_value['include'] ) ){ |
| 386 | $post_types = explode( ',', $_cond_value['include'] ); |
| 387 | } else { |
| 388 | $post_types = $_cond_value['include']; |
| 389 | } |
| 390 | // check if currently in a post (not post page, but also posts in loops) |
| 391 | if ( is_array( $post_types ) && ! in_array( get_post_type(), $post_types ) ) { |
| 392 | return false; |
| 393 | } |
| 394 | } |
| 395 | // check for excluded post types |
| 396 | // TODO remove in a later version, deprecated since 1.2.2 |
| 397 | if ( ! empty($_cond_value['exclude']) ){ |
| 398 | $post_types = explode( ',', $_cond_value['exclude'] ); |
| 399 | // check if currently in a post (not post page, but also posts in loops) |
| 400 | if ( is_array( $post_types ) && in_array( get_post_type(), $post_types ) ) { |
| 401 | return false; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | break; |
| 406 | // check is_front_page |
| 407 | // @link https://codex.wordpress.org/Conditional_Tags#The_Front_Page |
| 408 | case 'is_front_page' : |
| 409 | if ( $_cond_value == 0 && $wp_the_query->is_front_page()) { |
| 410 | return false; } |
| 411 | break; |
| 412 | // check is_singular |
| 413 | // @link https://codex.wordpress.org/Conditional_Tags#A_Post_Type |
| 414 | case 'is_singular' : |
| 415 | if ( $_cond_value == 0 && $wp_the_query->is_singular() ) { |
| 416 | return false; } |
| 417 | break; |
| 418 | // check is_archive |
| 419 | // @link https://codex.wordpress.org/Conditional_Tags#Any_Archive_Page |
| 420 | case 'is_archive' : |
| 421 | if ( $_cond_value == 0 && $wp_the_query->is_archive() ) { |
| 422 | return false; } |
| 423 | break; |
| 424 | // check is_search |
| 425 | // @link https://codex.wordpress.org/Conditional_Tags#A_Search_Result_Page |
| 426 | case 'is_search' : |
| 427 | if ( $_cond_value == 0 && $wp_the_query->is_search() ) { |
| 428 | return false; } |
| 429 | break; |
| 430 | // check is_404 |
| 431 | // @link https://codex.wordpress.org/Conditional_Tags#A_404_Not_Found_Page |
| 432 | case 'is_404' : |
| 433 | if ( $_cond_value == 0 && $wp_the_query->is_404() ) { |
| 434 | return false; } |
| 435 | break; |
| 436 | // check is_attachment |
| 437 | // @link https://codex.wordpress.org/Conditional_Tags#An_Attachment |
| 438 | case 'is_attachment' : |
| 439 | if ( $_cond_value == 0 && $wp_the_query->is_attachment() ) { |
| 440 | return false; } |
| 441 | break; |
| 442 | // check !is_main_query |
| 443 | // @link https://codex.wordpress.org/Function_Reference/is_main_query |
| 444 | case 'is_main_query' : |
| 445 | if ( $_cond_value == 0 && !is_main_query() ) { |
| 446 | return false; } |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * get all terms of a specific post or post type |
| 456 | * |
| 457 | * @param int $post_id id of the post |
| 458 | * @return arr $out ids of terms this post belongs to |
| 459 | */ |
| 460 | private function get_object_terms($post_id = 0){ |
| 461 | |
| 462 | $post_id = absint( $post_id ); |
| 463 | if ( ! $post_id ) { return array(); } |
| 464 | |
| 465 | // get post by post id |
| 466 | $post = get_post( $post_id ); |
| 467 | |
| 468 | // get post type by post |
| 469 | $post_type = $post->post_type; |
| 470 | |
| 471 | // get post type taxonomies |
| 472 | $taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 473 | |
| 474 | $term_ids = array(); |
| 475 | foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){ |
| 476 | |
| 477 | // get the terms related to post |
| 478 | $terms = get_the_terms( $post->ID, $taxonomy_slug ); |
| 479 | |
| 480 | if ( ! empty( $terms ) ) { |
| 481 | foreach ( $terms as $term ) { |
| 482 | $term_ids[] = $term->term_id; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | return $term_ids; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * check visitor conditions |
| 492 | * |
| 493 | * @since 1.1.0 |
| 494 | * @return bool $can_display true if can be displayed in frontend based on visitor settings |
| 495 | */ |
| 496 | public function can_display_by_visitor(){ |
| 497 | |
| 498 | if ( empty($this->options['visitor']) || |
| 499 | ! is_array( $this->options['visitor'] ) ) { return true; } |
| 500 | |
| 501 | $visitor_conditions = $this->options( 'visitor' ); |
| 502 | |
| 503 | // check mobile condition |
| 504 | if ( ! empty($visitor_conditions['mobile']) ){ |
| 505 | switch ( $visitor_conditions['mobile'] ){ |
| 506 | case 'only' : |
| 507 | if ( ! wp_is_mobile() ) { return false; } |
| 508 | break; |
| 509 | case 'no' : |
| 510 | if ( wp_is_mobile() ) { return false; } |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return true; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * check expiry date |
| 520 | * |
| 521 | * @since 1.3.15 |
| 522 | * @return bool $can_display true if can be displayed in frontend based on expiry date |
| 523 | */ |
| 524 | public function can_display_by_expiry_date(){ |
| 525 | |
| 526 | if ( !isset($this->options['expiry_date']) ) { return true; } |
| 527 | |
| 528 | $ad_expiry_date = absint($this->options( 'expiry_date' )); |
| 529 | |
| 530 | if ( $ad_expiry_date == 0 ) { return true; } |
| 531 | |
| 532 | // create blog specific timestamp |
| 533 | // TODO this is broken: use get_date_from_gmt() |
| 534 | $blog_expiry_date = time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); |
| 535 | |
| 536 | // check blog time against current time |
| 537 | if ( $blog_expiry_date >= $ad_expiry_date ) { return false; } |
| 538 | |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * save an ad to the database |
| 544 | * takes values from the current state |
| 545 | */ |
| 546 | public function save(){ |
| 547 | global $wpdb; |
| 548 | |
| 549 | // remove slashes from content |
| 550 | $content = $this->prepare_content_to_save(); |
| 551 | |
| 552 | $where = array('ID' => $this->id); |
| 553 | $wpdb->update( $wpdb->posts, array( 'post_content' => $content ), $where ); |
| 554 | |
| 555 | // sanitize conditions |
| 556 | // see sanitize_conditions function for example on using this filter |
| 557 | $conditions = self::sanitize_conditions_on_save( $this->conditions ); |
| 558 | |
| 559 | // save other options to post meta field |
| 560 | $options = $this->options(); |
| 561 | |
| 562 | $options['type'] = $this->type; |
| 563 | $options['width'] = $this->width; |
| 564 | $options['height'] = $this->height; |
| 565 | $options['conditions'] = $conditions; |
| 566 | $options['expiry_date'] = $this->expiry_date; |
| 567 | $options['description'] = $this->description; |
| 568 | |
| 569 | // filter to manipulate options or add more to be saved |
| 570 | $options = apply_filters( 'advanced-ads-save-options', $options, $this ); |
| 571 | |
| 572 | update_post_meta( $this->id, self::$options_meta_field, $options ); |
| 573 | |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * native filter for content field before being saved |
| 578 | * |
| 579 | * @return string $content ad content |
| 580 | * @since 1.0.0 |
| 581 | */ |
| 582 | public function prepare_content_to_save() { |
| 583 | |
| 584 | $content = $this->content; |
| 585 | |
| 586 | // load ad type specific parameter filter |
| 587 | $content = $this->type_obj->sanitize_content( $content ); |
| 588 | // apply a custom filter by ad type |
| 589 | $content = apply_filters( 'advanced-ads-pre-ad-save-' . $this->type, $content ); |
| 590 | |
| 591 | return $content; |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * native filter for ad parameters before being saved |
| 596 | * |
| 597 | * @return arr $parameters sanitized parameters |
| 598 | */ |
| 599 | public function prepare_parameters_to_save() { |
| 600 | |
| 601 | $parameters = $this->parameters; |
| 602 | // load ad type specific parameter filter |
| 603 | $parameters = $this->type_obj->sanitize_parameters( $parameters ); |
| 604 | |
| 605 | // apply native WP filter for content fields |
| 606 | return $parameters; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * prepare ads output |
| 611 | * |
| 612 | * @param string $content ad content |
| 613 | * @param obj $ad ad object |
| 614 | */ |
| 615 | public function prepare_frontend_output(){ |
| 616 | |
| 617 | // load ad type specific content filter |
| 618 | $output = $this->type_obj->prepare_output( $this ); |
| 619 | |
| 620 | // filter to manipulate the output before the wrapper is added |
| 621 | $output = apply_filters( 'advanced-ads-output-inside-wrapper', $output, $this ); |
| 622 | |
| 623 | // build wrapper around the ad |
| 624 | $output = $this->add_wrapper( $output ); |
| 625 | |
| 626 | // add a clearfix, if set |
| 627 | if ( isset($this->output['clearfix']) && $this->output['clearfix'] ){ |
| 628 | $output .= '<br style="clear: both; display: block; float: none;"/>'; |
| 629 | } |
| 630 | |
| 631 | // apply a custom filter by ad type |
| 632 | $output = apply_filters( 'advanced-ads-ad-output', $output, $this ); |
| 633 | |
| 634 | return $output; |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * sanitize ad display conditions when saving the ad |
| 639 | * |
| 640 | * @param array $conditions conditions array send via the dashboard form for an ad |
| 641 | * @return array with sanitized conditions |
| 642 | * @since 1.0.0 |
| 643 | */ |
| 644 | public function sanitize_conditions_on_save($conditions = array()){ |
| 645 | |
| 646 | global $advanced_ads_ad_conditions; |
| 647 | |
| 648 | if ( ! is_array( $conditions ) || $conditions == array() ) { return array(); } |
| 649 | |
| 650 | foreach ( $conditions as $_key => $_condition ){ |
| 651 | if ( $_key == 'postids' ){ |
| 652 | // sanitize single post conditions |
| 653 | if ( empty($_condition['ids']) ){ // remove, if empty |
| 654 | $_condition['include'] = array(); |
| 655 | $_condition['exclude'] = array(); |
| 656 | } else { |
| 657 | switch ( $_condition['method'] ){ |
| 658 | case 'include' : |
| 659 | $_condition['include'] = $_condition['ids']; |
| 660 | $_condition['exclude'] = array(); |
| 661 | break; |
| 662 | case 'exclude' : |
| 663 | $_condition['include'] = array(); |
| 664 | $_condition['exclude'] = $_condition['ids']; |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | } else { |
| 669 | if ( ! is_array( $_condition ) ) { |
| 670 | $_condition = trim( $_condition ); } |
| 671 | if ( $_condition == '' ) { |
| 672 | $conditions[$_key] = $_condition; |
| 673 | continue; |
| 674 | } |
| 675 | } |
| 676 | $type = ! empty($advanced_ads_ad_conditions[$_key]['type']) ? $advanced_ads_ad_conditions[$_key]['type'] : 0; |
| 677 | if ( empty($type) ) { continue; } |
| 678 | |
| 679 | // dynamically apply filters for each condition used |
| 680 | $conditions[$_key] = apply_filters( 'advanced-ads-sanitize-condition-' . $type, $_condition ); |
| 681 | } |
| 682 | |
| 683 | return $conditions; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * sanitize id input field(s) for pattern /1,2,3,4/ |
| 688 | * |
| 689 | * @pararm array/string $cond input string/array |
| 690 | * @return array/string $cond sanitized string/array |
| 691 | */ |
| 692 | public static function sanitize_condition_idfield($cond = ''){ |
| 693 | // strip anything that is not comma or number |
| 694 | |
| 695 | if ( is_array( $cond ) ){ |
| 696 | foreach ( $cond as $_key => $_cond ){ |
| 697 | $cond[$_key] = preg_replace( '#[^0-9,]#', '', $_cond ); |
| 698 | } |
| 699 | } else { |
| 700 | $cond = preg_replace( '#[^0-9,]#', '', $cond ); |
| 701 | } |
| 702 | return $cond; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * sanitize radio input field |
| 707 | * |
| 708 | * @pararm string $string input string |
| 709 | * @return string $string sanitized string |
| 710 | */ |
| 711 | public static function sanitize_condition_radio($string = ''){ |
| 712 | // only allow 0, 1 and empty |
| 713 | return $string = preg_replace( '#[^01]#', '', $string ); |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * sanitize comma seperated text input field |
| 718 | * |
| 719 | * @pararm array/string $cond input string/array |
| 720 | * @return array/string $cond sanitized string/array |
| 721 | */ |
| 722 | public static function sanitize_condition_textvalues($cond = ''){ |
| 723 | // strip anything that is not comma, alphanumeric, minus and underscore |
| 724 | if ( is_array( $cond ) ){ |
| 725 | foreach ( $cond as $_key => $_cond ){ |
| 726 | $cond[$_key] = preg_replace( '#[^0-9,A-Za-z-_]#', '', $_cond ); |
| 727 | } |
| 728 | } else { |
| 729 | $cond = preg_replace( '#[^0-9,A-Za-z-_]#', '', $cond ); |
| 730 | } |
| 731 | return $cond; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * load wrapper options set with the ad |
| 736 | * |
| 737 | * @since 1.3 |
| 738 | * @return arr $wrapper options array ready to be use in add_wrapper() function |
| 739 | */ |
| 740 | protected function load_wrapper_options(){ |
| 741 | $wrapper = array(); |
| 742 | |
| 743 | // print_r($this->output); |
| 744 | |
| 745 | if ( ! empty($this->output['position']) ) { |
| 746 | switch ( $this->output['position'] ) { |
| 747 | case 'left' : |
| 748 | $wrapper['style']['float'] = 'left'; |
| 749 | break; |
| 750 | case 'right' : |
| 751 | $wrapper['style']['float'] = 'right'; |
| 752 | break; |
| 753 | case 'center' : |
| 754 | $wrapper['style']['text-align'] = 'center'; |
| 755 | break; |
| 756 | case 'clearfix' : |
| 757 | $wrapper['style']['clear'] = 'both'; |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if ( ! empty($this->output['class']) && is_array( $this->output['class'] ) ) { |
| 763 | $wrapper['class'] = $this->output['class']; |
| 764 | } |
| 765 | |
| 766 | if ( ! empty($this->output['margin']['top']) ) { |
| 767 | $wrapper['style']['margin-top'] = intval( $this->output['margin']['top'] ) . 'px'; |
| 768 | } |
| 769 | if ( ! empty($this->output['margin']['right']) ) { |
| 770 | $wrapper['style']['margin-right'] = intval( $this->output['margin']['right'] ) . 'px'; |
| 771 | } |
| 772 | if ( ! empty($this->output['margin']['bottom']) ) { |
| 773 | $wrapper['style']['margin-bottom'] = intval( $this->output['margin']['bottom'] ) . 'px'; |
| 774 | } |
| 775 | if ( ! empty($this->output['margin']['left']) ) { |
| 776 | $wrapper['style']['margin-left'] = intval( $this->output['margin']['left'] ) . 'px'; |
| 777 | } |
| 778 | |
| 779 | return $wrapper; |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * add a wrapper arount the ad content if wrapper information are given |
| 784 | * |
| 785 | * @since 1.1.4 |
| 786 | * @param str $ad_content content of the ad |
| 787 | * @return str $wrapper ad within the wrapper |
| 788 | */ |
| 789 | protected function add_wrapper($ad_content = ''){ |
| 790 | |
| 791 | $wrapper_options = apply_filters( 'advanced-ads-output-wrapper-options', $this->wrapper, $this ); |
| 792 | |
| 793 | if ( $wrapper_options == array() || ! is_array( $wrapper_options ) || empty($wrapper_options) ) { return $ad_content; } |
| 794 | |
| 795 | $wrapper = $ad_content; |
| 796 | |
| 797 | // create unique id if not yet given |
| 798 | if ( empty($wrapper_options['id']) ){ |
| 799 | $wrapper_options['id'] = $this->create_wrapper_id(); |
| 800 | } |
| 801 | |
| 802 | // build the box |
| 803 | $wrapper = '<div'; |
| 804 | foreach ( $wrapper_options as $_html_attr => $_values ){ |
| 805 | if ( $_html_attr == 'style' ){ |
| 806 | $_style_values_string = ''; |
| 807 | foreach ( $_values as $_style_attr => $_style_values ){ |
| 808 | if ( is_array( $_style_values ) ) { |
| 809 | $_style_values_string .= $_style_attr . ': ' .implode( ' ', $_style_values ). '; '; } |
| 810 | else { |
| 811 | $_style_values_string .= $_style_attr . ': ' .$_style_values. '; '; } |
| 812 | } |
| 813 | $wrapper .= " style=\"$_style_values_string\""; |
| 814 | } else { |
| 815 | if ( is_array( $_values ) ) { |
| 816 | $_values_string = implode( ' ', $_values ); } |
| 817 | else { |
| 818 | $_values_string = sanitize_title( $_values ); } |
| 819 | $wrapper .= " $_html_attr=\"$_values_string\""; |
| 820 | } |
| 821 | } |
| 822 | $wrapper .= '>'; |
| 823 | $wrapper .= apply_filters( 'advanced-ads-output-wrapper-before-content', '', $this ); |
| 824 | $wrapper .= $ad_content; |
| 825 | $wrapper .= apply_filters( 'advanced-ads-output-wrapper-after-content', '', $this ); |
| 826 | $wrapper .= '</div>'; |
| 827 | |
| 828 | return $wrapper; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * create a random wrapper id |
| 833 | * |
| 834 | * @since 1.1.4 |
| 835 | * @return string $id random id string |
| 836 | */ |
| 837 | private function create_wrapper_id(){ |
| 838 | return 'advads-' . mt_rand(); |
| 839 | } |
| 840 | } |
| 841 |