PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.24.1
Advanced Ads – Ad Manager & AdSense v1.24.1
2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / classes / ad.php
advanced-ads / classes Last commit date
EDD_SL_Plugin_Updater.php 5 years ago ad-ajax.php 5 years ago ad-debug.php 6 years ago ad-health-notices.php 6 years ago ad-model.php 5 years ago ad-select.php 9 years ago ad.php 5 years ago ad_ajax_callbacks.php 5 years ago ad_group.php 5 years ago ad_placements.php 5 years ago ad_type_abstract.php 5 years ago ad_type_content.php 5 years ago ad_type_dummy.php 5 years ago ad_type_group.php 5 years ago ad_type_image.php 5 years ago ad_type_plain.php 5 years ago checks.php 5 years ago compatibility.php 5 years ago display-conditions.php 5 years ago filesystem.php 8 years ago frontend-notices.php 6 years ago frontend_checks.php 5 years ago plugin.php 5 years ago upgrades.php 6 years ago utils.php 5 years ago visitor-conditions.php 6 years ago widget.php 6 years ago
ad.php
1026 lines
1 <?php
2 /**
3 * Advanced Ads Ad.
4 *
5 * @package Advanced_Ads_Ad
6 * @author Thomas Maier <support@wpadvancedads.com>
7 * @license GPL-2.0+
8 * @link https://wpadvancedads.com
9 * @copyright 2013-2020 Thomas Maier, Advanced Ads GmbH
10 */
11
12 /**
13 * An ad object
14 *
15 * @package Advanced_Ads_Ad
16 * @author Thomas Maier <support@wpadvancedads.com>
17 * @deprecated since version 1.5.3 (May 6th 2015)
18 * might still be needed if some old add-ons are running somewhere
19 */
20 if ( ! class_exists( 'Advads_Ad', false ) ) {
21 class Advads_Ad extends Advanced_Ads_Ad {
22
23 }
24 }
25
26 /**
27 * An ad object
28 *
29 * @package Advanced_Ads_Ad
30 * @author Thomas Maier <support@wpadvancedads.com>
31 */
32 class Advanced_Ads_Ad {
33
34 /**
35 * Id of the post type for this ad
36 *
37 * @var int $id
38 */
39 public $id = 0;
40
41 /**
42 * True, if this is an Advanced Ads Ad post type
43 *
44 * @var bool $is_ad
45 */
46 public $is_ad = false;
47
48 /**
49 * Ad type
50 *
51 * @var string $type ad type.
52 */
53 public $type = 'content';
54
55 /**
56 * Ad width
57 *
58 * @var int $width width of the ad.
59 */
60 public $width = 0;
61
62 /**
63 * Target url
64 *
65 * @var string $url ad URL parameter.
66 */
67 public $url = '';
68
69 /**
70 * Ad height
71 *
72 * @var int $height height of the ad.
73 */
74 public $height = 0;
75
76 /**
77 * Object of current ad type
78 *
79 * @var object $type_obj object of the current ad type.
80 */
81 protected $type_obj;
82
83 /**
84 * Content of the ad
85 *
86 * Only needed for ad types using the post content field
87 *
88 * @var string $content content of the ad.
89 */
90 public $content = '';
91
92 /**
93 * Conditions of the ad display
94 *
95 * @var array $conditions display and visitor conditions.
96 */
97 public $conditions = array();
98
99 /**
100 * Status of the ad (e.g. publish, pending)
101 *
102 * @var string $status status of the ad.
103 */
104 public $status = '';
105
106 /**
107 * Array with meta field options aka parameters
108 *
109 * @var array $options ad options.
110 */
111 protected $options;
112
113 /**
114 * Name of the meta field to save options to
115 *
116 * @var string $options_meta_field under which post meta key the ad options are stored.
117 */
118 public static $options_meta_field = 'advanced_ads_ad_options';
119
120 /**
121 * Additional arguments set when ad is loaded, overwrites or extends options
122 *
123 * @var array $args
124 */
125 public $args = array();
126
127 /**
128 * Multidimensional array contains information about the wrapper
129 * Each possible html attribute is an array with possible multiple elements
130 *
131 * @var array $wrapper options of the ad wrapper.
132 */
133 public $wrapper = array();
134
135 /**
136 * Will the ad be tracked?
137 *
138 * @var mixed $global_output
139 */
140 public $global_output;
141
142 /**
143 * Title of the ad
144 *
145 * @var string $title
146 */
147 public $title = '';
148
149 /**
150 * Displayed above the ad.
151 *
152 * @var string $label ad label.
153 */
154 protected $label = '';
155
156 /**
157 * Init ad object
158 *
159 * @param int $id id of the ad.
160 * @param array $args additional arguments.
161 */
162 public function __construct( $id, $args = array() ) {
163 $id = absint( $id );
164 $this->id = $id;
165 $this->args = is_array( $args ) ? $args : array();
166
167 // whether the ad will be tracked.
168 $this->global_output = isset( $this->args['global_output'] ) ? (bool) $this->args['global_output'] : true;
169
170 if ( ! empty( $id ) ) {
171 $this->load( $id );
172 }
173
174 // dynamically add sanitize filters for condition types.
175 $_types = array();
176 // -TODO use model
177 $advanced_ads_ad_conditions = Advanced_Ads::get_ad_conditions();
178 foreach ( $advanced_ads_ad_conditions as $_condition ) {
179 // add unique.
180 $_types[ $_condition['type'] ] = false;
181 }
182 // iterate types.
183 foreach ( array_keys( $_types ) as $_type ) {
184 // -TODO might be faster to use __call() method or isset()-test class method array
185 $method_name = 'sanitize_condition_' . $_type;
186 if ( method_exists( $this, $method_name ) ) {
187 add_filter( 'advanced-ads-sanitize-condition-' . $_type, array( $this, $method_name ), 10, 1 );
188 } elseif ( function_exists( 'advads_sanitize_condition_' . $_type ) ) {
189 // check for public function to sanitize this.
190 add_filter( 'advanced-ads-sanitize-condition-' . $_type, 'advads_sanitize_condition_' . $_type, 10, 1 );
191 }
192 }
193 }
194
195 /**
196 * Load an ad object by id based on its ad type
197 *
198 * @param int $id ad id.
199 *
200 * @return bool false if ad could not be loaded.
201 */
202 private function load( $id = 0 ) {
203
204 $_data = get_post( $id );
205 if ( null === $_data ) {
206 return false;
207 }
208
209 // return, if not an ad.
210 if ( Advanced_Ads::POST_TYPE_SLUG !== $_data->post_type ) {
211 return false;
212 } else {
213 $this->is_ad = true;
214 }
215
216 $this->type = $this->options( 'type' );
217 $this->title = $_data->post_title;
218 /* load ad type object */
219 $types = Advanced_Ads::get_instance()->ad_types;
220 if ( isset( $types[ $this->type ] ) ) {
221 $this->type_obj = $types[ $this->type ];
222 } else {
223 $this->type_obj = new Advanced_Ads_Ad_Type_Abstract();
224 }
225 $this->url = $this->get_url();
226 $this->width = absint( $this->options( 'width' ) );
227 $this->height = absint( $this->options( 'height' ) );
228 $this->conditions = $this->options( 'conditions' );
229 $this->description = $this->options( 'description' );
230 $this->output = $this->options( 'output' );
231 $this->status = $_data->post_status;
232 $this->expiry_date = $this->options( 'expiry_date' );
233 $this->is_head_placement = isset( $this->args['placement_type'] ) && 'header' === $this->args['placement_type'];
234 $this->args['is_top_level'] = ! isset( $this->args['is_top_level'] );
235
236 // load content based on ad type.
237 $this->content = $this->type_obj->load_content( $_data );
238
239 if ( ! $this->is_head_placement ) {
240 $this->maybe_create_label();
241 $this->wrapper = $this->load_wrapper_options();
242
243 // set wrapper conditions.
244 $this->wrapper = apply_filters( 'advanced-ads-set-wrapper', $this->wrapper, $this );
245 // add unique wrapper id.
246 if ( is_array( $this->wrapper )
247 && array() !== $this->wrapper
248 && ! isset( $this->wrapper['id'] ) ) {
249 // create unique id if not yet given.
250 $this->wrapper['id'] = $this->create_wrapper_id();
251 }
252 }
253 }
254
255 /**
256 * Get options from meta field and return specific field
257 *
258 * @param string $field post meta key to be returned. Can be passed as array keys separated with `.`, i.e. 'parent.child' to retrieve multidimensional array values.
259 * @param array $default default options.
260 *
261 * @return mixed meta field content
262 */
263 public function options( $field = '', $default = null ) {
264 // retrieve options, if not given yet
265 if ( is_null( $this->options ) ) {
266 // may return false.
267 $meta = get_post_meta( $this->id, self::$options_meta_field, true );
268 if ( $meta && is_array( $meta ) ) {
269 // merge meta with arguments given on ad load.
270 $this->options = Advanced_Ads_Utils::merge_deep_array( array( $meta, $this->args ) );
271 } else {
272 // load arguments given on ad load.
273 $this->options = $this->args;
274 }
275
276 if ( isset( $this->options['change-ad'] ) ) {
277 // some options was provided by the user.
278 $this->options = Advanced_Ads_Utils::merge_deep_array(
279 array(
280 $this->options,
281 $this->options['change-ad'],
282 )
283 );
284 }
285 }
286
287 // return all options if no field given.
288 if ( empty( $field ) ) {
289 return $this->options;
290 }
291
292 $field = preg_replace( '/\s/', '', $field );
293 $value = $this->options;
294 foreach ( explode( '.', $field ) as $key ) {
295 if ( ! isset( $value[ $key ] ) ) {
296 $value = $default;
297 break;
298 }
299 $value = $value[ $key ];
300 }
301
302 if ( is_null( $value ) ) {
303 $value = $default;
304 }
305
306 /**
307 * Filter the option value retrieved for $field.
308 * `$field` parameter makes dynamic hook portion.
309 *
310 * @var mixed $value The option value (may be set to default).
311 * @var Advanced_Ads_Ad $this The current Advanced_Ads_Ad instance.
312 */
313 return apply_filters( "advanced-ads-ad-option-{$field}", $value, $this );
314 }
315
316 /**
317 * Set an option of the ad
318 *
319 * @param string $option name of the option.
320 * @param mixed $value value of the option.
321 *
322 * @since 1.1.0
323 */
324 public function set_option( $option = '', $value = '' ) {
325 if ( '' === $option ) {
326 return;
327 }
328
329 // get current options.
330 $options = $this->options();
331
332 // set options.
333 $options[ $option ] = $value;
334
335 // save options.
336 $this->options = $options;
337
338 }
339
340
341 /**
342 * Return ad content for frontend output
343 *
344 * @param array $output_options output options.
345 *
346 * @return string $output ad output
347 * @since 1.0.0
348 */
349 public function output( $output_options = array() ) {
350 if ( ! $this->is_ad ) {
351 return '';
352 }
353
354 $this->global_output = isset( $output_options['global_output'] ) ? $output_options['global_output'] : $this->global_output;
355 $output_options['global_output'] = $this->global_output;
356
357 // switch between normal and debug mode.
358 // check if debug output should only be displayed to admins.
359 $user_can_manage_ads = current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options' ) );
360 if ( $this->options( 'output.debugmode' )
361 && ( $user_can_manage_ads || ( ! $user_can_manage_ads && ! defined( 'ADVANCED_ADS_AD_DEBUG_FOR_ADMIN_ONLY' ) ) ) ) {
362 $debug = new Advanced_Ads_Ad_Debug();
363
364 return $debug->prepare_debug_output( $this );
365 } else {
366 $output = $this->prepare_frontend_output();
367 }
368
369 // add the ad to the global output array.
370 $advads = Advanced_Ads::get_instance();
371 if ( $output_options['global_output'] ) {
372 $new_ad = array(
373 'type' => 'ad',
374 'id' => $this->id,
375 'title' => $this->title,
376 'output' => $output,
377 );
378 // if ( method_exists( 'Advanced_Ads_Tracking_Plugin' , 'check_ad_tracking_enabled' ) ) {
379 // if ( class_exists( 'Advanced_Ads_Tracking_Plugin', false ) ) {
380 if ( defined( 'AAT_VERSION' ) && - 1 < version_compare( AAT_VERSION, '1.4.2' ) ) {
381
382 $new_ad['tracking_enabled'] = Advanced_Ads_Tracking_Plugin::get_instance()->check_ad_tracking_enabled( $this );
383
384 $tracking_options = Advanced_Ads_Tracking_Plugin::get_instance()->options();
385 if ( isset( $tracking_options['method'] ) && 'frontend' === $tracking_options['method'] && isset( $this->output['placement_id'] ) ) {
386 $new_ad['placement_id'] = $this->output['placement_id'];
387 }
388 }
389
390 $advads->current_ads[] = $new_ad;
391 }
392
393 // action when output is created.
394 do_action( 'advanced-ads-output', $this, $output, $output_options );
395
396 return apply_filters( 'advanced-ads-output-final', $output, $this, $output_options );
397 }
398
399 /**
400 * Check if the ad can be displayed in frontend due to its own conditions
401 *
402 * @param array $check_options check options.
403 *
404 * @return bool $can_display true if can be displayed in frontend
405 * @since 1.0.0
406 */
407 public function can_display( $check_options = array() ) {
408 $check_options = wp_parse_args(
409 $check_options,
410 array(
411 'passive_cache_busting' => false,
412 'ignore_debugmode' => false,
413 )
414 );
415
416 // prevent ad to show up through wp_head, if this is not a header placement.
417 if ( doing_action( 'wp_head' ) && isset( $this->options['placement_type'] ) && 'header' !== $this->options['placement_type']
418 && ! Advanced_Ads_Compatibility::can_inject_during_wp_head() ) {
419 return false;
420 }
421
422 // Check If the current ad is requested using a shortcode placed in the content of the current ad.
423 if ( isset( $this->options['shortcode_ad_id'] ) && (int) $this->options['shortcode_ad_id'] === $this->id ) {
424 return false;
425 }
426
427 // force ad display if debug mode is enabled.
428 if ( isset( $this->output['debugmode'] ) && ! $check_options['ignore_debugmode'] ) {
429 return true;
430 }
431
432 if ( ! $check_options['passive_cache_busting'] ) {
433 // don’t display ads that are not published or private for users not logged in.
434 if ( 'publish' !== $this->status && ! ( 'private' === $this->status && is_user_logged_in() ) ) {
435 return false;
436 }
437
438 if ( ! $this->can_display_by_visitor() || ! $this->can_display_by_expiry_date() ) {
439 return false;
440 }
441 } else {
442 if ( 'publish' !== $this->status || ! $this->can_display_by_expiry_date() ) {
443 return false;
444 }
445 }
446
447 // add own conditions to flag output as possible or not.
448 $can_display = apply_filters( 'advanced-ads-can-display', true, $this, $check_options );
449
450 return $can_display;
451 }
452
453 /**
454 * Check visitor conditions
455 *
456 * @return bool $can_display true if can be displayed in frontend based on visitor settings
457 * @since 1.1.0
458 */
459 public function can_display_by_visitor() {
460 if ( ! empty( $this->options['wp_the_query']['is_feed'] ) ) {
461 return true;
462 }
463
464 // check old "visitor" and new "visitors" conditions.
465 if ( ( empty( $this->options['visitors'] ) ||
466 ! is_array( $this->options['visitors'] ) )
467 && ( empty( $this->options['visitor'] ) ||
468 ! is_array( $this->options['visitor'] )
469 ) ) {
470 return true;
471 }
472
473 if ( isset( $this->options['visitors'] ) && is_array( $this->options['visitors'] ) ) {
474
475 $visitor_conditions = $this->options['visitors'];
476
477 $last_result = false;
478 $length = count( $visitor_conditions );
479
480 for ( $i = 0; $i < $length; ++ $i ) {
481 $_condition = current( $visitor_conditions );
482 // ignore OR if last result was true.
483 if ( $last_result && isset( $_condition['connector'] ) && 'or' === $_condition['connector'] ) {
484 next( $visitor_conditions );
485 continue;
486 }
487 $result = Advanced_Ads_Visitor_Conditions::frontend_check( $_condition, $this );
488 $last_result = $result;
489 if ( ! $result ) {
490 // return false only, if the next condition doesn’t have an OR operator.
491 $next = next( $visitor_conditions );
492 if ( ! isset( $next['connector'] ) || 'or' !== $next['connector'] ) {
493 return false;
494 }
495 } else {
496 next( $visitor_conditions );
497 }
498 }
499 }
500
501 /**
502 * "old" visitor conditions
503 *
504 * @deprecated since version 1.5.4
505 */
506
507 if ( empty( $this->options['visitor'] ) ||
508 ! is_array( $this->options['visitor'] ) ) {
509 return true;
510 }
511 $visitor_conditions = $this->options( 'visitor' );
512
513 // check mobile condition.
514 if ( isset( $visitor_conditions['mobile'] ) ) {
515 switch ( $visitor_conditions['mobile'] ) {
516 case 'only':
517 if ( ! wp_is_mobile() ) {
518 return false;
519 }
520 break;
521 case 'no':
522 if ( wp_is_mobile() ) {
523 return false;
524 }
525 break;
526 }
527 }
528
529 return true;
530 }
531
532 /**
533 * Check expiry date
534 *
535 * @return bool $can_display true if can be displayed in frontend based on expiry date
536 * @since 1.3.15
537 */
538 public function can_display_by_expiry_date() {
539
540 // if expiry_date is not set, null is returned.
541 $ad_expiry_date = (int) $this->options( 'expiry_date' );
542
543 if ( $ad_expiry_date <= 0 || $ad_expiry_date > time() ) {
544 return true;
545 }
546
547 // set status to 'draft' if the ad is expired.
548 if ( 'draft' !== $this->status ) {
549 // removing the kses filters here so that expiring ads don’t lose HTML or other code.
550 kses_remove_filters();
551 wp_update_post(
552 array(
553 'ID' => $this->id,
554 'post_status' => 'draft',
555 )
556 );
557 kses_init_filters();
558 /**
559 * Run when an ad expires
560 */
561 do_action( 'advanced-ads-ad-expired', $this->id, $this );
562 }
563
564 return false;
565 }
566
567 /**
568 * Save an ad to the database
569 * takes values from the current state
570 */
571 public function save() {
572 global $wpdb;
573
574 // remove slashes from content.
575 $this->content = $this->prepare_content_to_save();
576
577 $where = array( 'ID' => $this->id );
578 $wpdb->update( $wpdb->posts, array( 'post_content' => $this->content ), $where );
579
580 // clean post from object cache.
581 clean_post_cache( $this->id );
582
583 // sanitize conditions
584 // see sanitize_conditions function for example on using this filter.
585 $conditions = self::sanitize_conditions_on_save( $this->conditions );
586
587 // save other options to post meta field.
588 $options = $this->options();
589
590 $options['type'] = $this->type;
591 $options['url'] = $this->url;
592 // Inform the tracking add-on about the new url.
593 unset( $options['tracking']['link'] );
594 $options['width'] = $this->width;
595 $options['height'] = $this->height;
596 $options['conditions'] = $conditions;
597 $options['expiry_date'] = $this->expiry_date;
598 $options['description'] = $this->description;
599
600 // sanitize container ID option.
601 $options['output']['wrapper-id'] = isset( $options['output']['wrapper-id'] ) ? sanitize_key( $options['output']['wrapper-id'] ) : '';
602
603 // sanitize options before saving
604 $options = $this->prepare_options_to_save( $options );
605
606 // filter to manipulate options or add more to be saved.
607 $options = apply_filters( 'advanced-ads-save-options', $options, $this );
608
609 update_post_meta( $this->id, self::$options_meta_field, $options );
610 }
611
612 /**
613 * Save ad options.
614 * Meant to be used from the outside of an ad.
615 *
616 * @param int $ad_id post ID of the ad.
617 * @param array $options ad options.
618 */
619 public static function save_ad_options( $ad_id, array $options ) {
620
621 // don’t allow to clear options by accident.
622 if ( array() === $options ) {
623 return;
624 }
625
626 update_post_meta( $ad_id, self::$options_meta_field, $options );
627 }
628
629 /**
630 * Native filter for content field before being saved
631 *
632 * @return string $content ad content
633 */
634 public function prepare_content_to_save() {
635
636 $content = $this->content;
637
638 // load ad type specific parameter filter
639 // @todo this is just a hotfix for type_obj not set, yet the cause is still unknown. Likely when the ad is first saved
640 if ( is_object( $this->type_obj ) ) {
641 $content = $this->type_obj->sanitize_content( $content );
642 }
643 // apply a custom filter by ad type.
644 $content = apply_filters( 'advanced-ads-pre-ad-save-' . $this->type, $content );
645
646 return $content;
647 }
648
649 /**
650 * Sanitize ad options before being saved
651 * allows some ad types to sanitize certain values
652 *
653 * @param array $options ad options.
654 * @return array sanitized options.
655 */
656 public function prepare_options_to_save( $options ) {
657
658 // load ad type specific sanitize function.
659 // we need to load the ad type object if not set (e.g., when the ad is saved for the first time)
660 if ( ! is_object( $this->type_obj ) || ! $this->type_obj->ID ) {
661 $types = Advanced_Ads::get_instance()->ad_types;
662 if ( isset( $types[ $this->type ] ) ) {
663 $this->type_obj = $types[ $this->type ];
664 }
665 }
666
667 $options = $this->type_obj->sanitize_options( $options );
668
669 return $options;
670 }
671
672 /**
673 * Prepare ads output
674 *
675 * @return string.
676 */
677 public function prepare_frontend_output() {
678 $options = $this->options();
679
680 if ( isset( $options['change-ad']['content'] ) ) {
681 // output was provided by the user.
682 $output = $options['change-ad']['content'];
683 } else {
684 // load ad type specific content filter.
685 $output = $this->type_obj->prepare_output( $this );
686 }
687
688 // don’t deliver anything, if main ad content is empty.
689 if ( empty( $output ) ) {
690 return;
691 }
692
693 if ( ! $this->is_head_placement ) {
694 // filter to manipulate the output before the wrapper is added
695 $output = apply_filters( 'advanced-ads-output-inside-wrapper', $output, $this );
696
697 // build wrapper around the ad.
698 $output = $this->add_wrapper( $output );
699
700 // add a clearfix, if set.
701 if ( ( isset( $this->output['clearfix'] ) && $this->output['clearfix'] )
702 || ( ! empty( $this->args['is_top_level'] ) && ! empty( $this->args['placement_clearfix'] ) ) ) {
703 $output .= '<br style="clear: both; display: block; float: none;"/>';
704 }
705 }
706
707 // apply a custom filter by ad type.
708 $output = apply_filters( 'advanced-ads-ad-output', $output, $this );
709
710 return $output;
711 }
712
713 /**
714 * Sanitize ad display conditions when saving the ad
715 *
716 * @param array $conditions conditions array send via the dashboard form for an ad.
717 *
718 * @return array with sanitized conditions
719 * @since 1.0.0
720 */
721 public function sanitize_conditions_on_save( $conditions = array() ) {
722
723 global $advanced_ads_ad_conditions;
724
725 if ( ! is_array( $conditions ) || array() === $conditions ) {
726 return array();
727 }
728
729 foreach ( $conditions as $_key => $_condition ) {
730 if ( 'postids' === $_key ) {
731 // sanitize single post conditions
732 if ( empty( $_condition['ids'] ) ) { // remove, if empty.
733 $_condition['include'] = array();
734 $_condition['exclude'] = array();
735 } elseif ( isset( $_condition['method'] ) ) {
736 switch ( $_condition['method'] ) {
737 case 'include':
738 $_condition['include'] = $_condition['ids'];
739 $_condition['exclude'] = array();
740 break;
741 case 'exclude':
742 $_condition['include'] = array();
743 $_condition['exclude'] = $_condition['ids'];
744 break;
745 }
746 }
747 } else {
748 if ( ! is_array( $_condition ) ) {
749 $_condition = trim( $_condition );
750 }
751 if ( $_condition == '' ) {
752 $conditions[ $_key ] = $_condition;
753 continue;
754 }
755 }
756 $type = ! empty( $advanced_ads_ad_conditions[ $_key ]['type'] ) ? $advanced_ads_ad_conditions[ $_key ]['type'] : 0;
757 if ( empty( $type ) ) {
758 continue;
759 }
760
761 // dynamically apply filters for each condition used.
762 $conditions[ $_key ] = apply_filters( 'advanced-ads-sanitize-condition-' . $type, $_condition );
763 }
764
765 return $conditions;
766 }
767
768 /**
769 * Sanitize id input field(s) for pattern /1,2,3,4/
770 *
771 * @param mixed $cond input string/array.
772 *
773 * @return array/string $cond sanitized string/array
774 */
775 public static function sanitize_condition_idfield( $cond = '' ) {
776 // strip anything that is not comma or number.
777
778 if ( is_array( $cond ) ) {
779 foreach ( $cond as $_key => $_cond ) {
780 $cond[ $_key ] = preg_replace( '#[^0-9,]#', '', $_cond );
781 }
782 } else {
783 $cond = preg_replace( '#[^0-9,]#', '', $cond );
784 }
785
786 return $cond;
787 }
788
789 /**
790 * Sanitize radio input field
791 *
792 * @param string $string input string.
793 *
794 * @return string $string sanitized string.
795 */
796 public static function sanitize_condition_radio( $string = '' ) {
797 // only allow 0, 1 and empty.
798 return preg_replace( '#[^01]#', '', $string );
799 }
800
801 /**
802 * Sanitize comma seperated text input field
803 *
804 * @param mixed $cond input string/array.
805 *
806 * @return array/string $cond sanitized string/array.
807 */
808 public static function sanitize_condition_textvalues( $cond = '' ) {
809 // strip anything that is not comma, alphanumeric, minus and underscore.
810 if ( is_array( $cond ) ) {
811 foreach ( $cond as $_key => $_cond ) {
812 $cond[ $_key ] = preg_replace( '#[^0-9,A-Za-z-_]#', '', $_cond );
813 }
814 } else {
815 $cond = preg_replace( '#[^0-9,A-Za-z-_]#', '', $cond );
816 }
817
818 return $cond;
819 }
820
821 /**
822 * Load wrapper options set with the ad
823 *
824 * @return array $wrapper options array ready to be use in add_wrapper() function.
825 * @since 1.3
826 */
827 protected function load_wrapper_options() {
828 $wrapper = array();
829
830 $position = ! empty( $this->output['position'] ) ? $this->output['position'] : '';
831 $use_placement_pos = false;
832
833 if ( $this->args['is_top_level'] ) {
834 if ( isset( $this->output['class'] ) && is_array( $this->output['class'] ) ) {
835 $wrapper['class'] = $this->output['class'];
836 }
837 if ( ! empty( $this->args['placement_position'] ) ) {
838 // If not group, Set placement position instead of ad position.
839 $use_placement_pos = true;
840 $position = $this->args['placement_position'];
841 }
842 }
843
844 switch ( $position ) {
845 case 'left':
846 $wrapper['style']['float'] = 'left';
847 break;
848 case 'right':
849 $wrapper['style']['float'] = 'right';
850 break;
851 case 'center':
852 $wrapper['style']['margin-left'] = 'auto';
853 $wrapper['style']['margin-right'] = 'auto';
854
855 $width = (int) $this->width;
856 if (
857 ( ! $width || empty( $this->output['add_wrapper_sizes'] ) )
858 || $use_placement_pos
859 ) {
860 $wrapper['style']['text-align'] = 'center';
861 }
862
863 // add css rule after wrapper to center the ad.
864 break;
865 case 'clearfix':
866 $wrapper['style']['clear'] = 'both';
867 break;
868 }
869
870 // add manual classes.
871 if ( isset( $this->output['wrapper-class'] ) && '' !== $this->output['wrapper-class'] ) {
872 $classes = explode( ' ', $this->output['wrapper-class'] );
873
874 foreach ( $classes as $_class ) {
875 $wrapper['class'][] = sanitize_text_field( $_class );
876 }
877 }
878
879 if ( ! empty( $this->output['margin']['top'] ) ) {
880 $wrapper['style']['margin-top'] = (int) $this->output['margin']['top'] . 'px';
881 }
882 if ( empty( $wrapper['style']['margin-right'] ) && ! empty( $this->output['margin']['right'] ) ) {
883 $wrapper['style']['margin-right'] = (int) $this->output['margin']['right'] . 'px';
884 }
885 if ( ! empty( $this->output['margin']['bottom'] ) ) {
886 $wrapper['style']['margin-bottom'] = (int) $this->output['margin']['bottom'] . 'px';
887 }
888 if ( empty( $wrapper['style']['margin-left'] ) && ! empty( $this->output['margin']['left'] ) ) {
889 $wrapper['style']['margin-left'] = (int) $this->output['margin']['left'] . 'px';
890 }
891
892 if ( ! empty( $this->output['add_wrapper_sizes'] ) ) {
893 $width = (int) $this->width;
894 $height = (int) $this->height;
895
896 if ( $width ) {
897 $wrapper['style']['width'] = $width . 'px';
898 }
899 if ( $height ) {
900 $wrapper['style']['height'] = $height . 'px';
901 }
902 }
903
904 if ( ! empty( $this->output['clearfix_before'] ) ) {
905 $wrapper['style']['clear'] = 'both';
906 }
907
908 return $wrapper;
909 }
910
911 /**
912 * Add a wrapper arount the ad content if wrapper information are given
913 *
914 * @param string $ad_content content of the ad.
915 *
916 * @return string $wrapper ad within the wrapper
917 * @since 1.1.4
918 */
919 protected function add_wrapper( $ad_content = '' ) {
920 $wrapper_options = apply_filters( 'advanced-ads-output-wrapper-options', $this->wrapper, $this );
921
922 if ( ( ! isset( $this->output['wrapper-id'] ) || '' === $this->output['wrapper-id'] )
923 && array() === $wrapper_options || ! is_array( $wrapper_options ) ) {
924 return $ad_content;
925 }
926
927 // create unique id if not yet given.
928 if ( empty( $wrapper_options['id'] ) ) {
929 $wrapper_options['id'] = $this->create_wrapper_id();
930 $this->wrapper['id'] = $wrapper_options['id'];
931 }
932
933 if ( $this->label && ! empty( $wrapper_options['style']['height'] ) ) {
934 // Create another wrapper so that the label does not reduce the height of the ad wrapper.
935 $height = array( 'style' => array( 'height' => $wrapper_options['style']['height'] ) );
936 unset( $wrapper_options['style']['height'] );
937 $ad_content = '<div' . Advanced_Ads_Utils::build_html_attributes( $height ) . '>'
938 . $ad_content
939 . '</div>';
940 }
941
942 // add edit button for users with the appropriate rights.
943 if ( ! defined( 'ADVANCED_ADS_DISABLE_EDIT_BAR' ) && current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) ) {
944 ob_start();
945 include ADVADS_BASE_PATH . 'public/views/ad-edit-bar.php';
946 $ad_content = ob_get_clean() . $ad_content;
947 }
948
949
950
951 // build the box
952 $wrapper = '<div' . Advanced_Ads_Utils::build_html_attributes( $wrapper_options ) . '>';
953 $wrapper .= $this->label;
954 $wrapper .= apply_filters( 'advanced-ads-output-wrapper-before-content', '', $this );
955 $wrapper .= $ad_content;
956 $wrapper .= apply_filters( 'advanced-ads-output-wrapper-after-content', '', $this );
957 $wrapper .= '</div>';
958
959 return $wrapper;
960 }
961
962 /**
963 * Create a random wrapper id
964 *
965 * @return string $id random id string
966 * @since 1.1.4
967 */
968 private function create_wrapper_id() {
969
970 if ( isset( $this->output['wrapper-id'] ) ) {
971 $id = sanitize_key( $this->output['wrapper-id'] );
972 if ( '' !== $id ) {
973 return $id;
974 }
975 }
976
977 $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix();
978
979 return $prefix . mt_rand();
980 }
981
982 /**
983 * Create an "Advertisement" label if conditions are met.
984 */
985 public function maybe_create_label() {
986 $placement_state = isset( $this->args['ad_label'] ) ? $this->args['ad_label'] : 'default';
987
988 $label = Advanced_Ads::get_instance()->get_label( $placement_state );
989
990 if ( $this->args['is_top_level'] && $label ) {
991 $this->label = $label;
992 }
993 }
994
995 /**
996 * Get the ad url.
997 *
998 * @return string
999 */
1000 private function get_url() {
1001 $this->url = $this->options( 'url' );
1002
1003 // If the tracking add-on is not active.
1004 if ( ! defined( 'AAT_VERSION' ) ) {
1005 global $pagenow;
1006 // If this is not the ad edit page.
1007 if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) {
1008 // Remove placeholders.
1009 $this->url = str_replace(
1010 array(
1011 '[POST_ID]',
1012 '[POST_SLUG]',
1013 '[CAT_SLUG]',
1014 '[AD_ID]',
1015 ),
1016 '',
1017 $this->url
1018 );
1019 }
1020 }
1021
1022 return $this->url;
1023 }
1024
1025 }
1026