Advanced_Ads_Modal.php
3 years ago
EDD_SL_Plugin_Updater.php
4 years ago
ad-ajax.php
3 years ago
ad-debug.php
3 years ago
ad-expiration.php
3 years ago
ad-health-notices.php
3 years ago
ad-model.php
3 years ago
ad-select.php
3 years ago
ad.php
3 years ago
ad_ajax_callbacks.php
3 years ago
ad_group.php
3 years ago
ad_placements.php
3 years ago
ad_type_abstract.php
3 years ago
ad_type_content.php
3 years ago
ad_type_dummy.php
3 years ago
ad_type_group.php
3 years ago
ad_type_image.php
3 years ago
ad_type_plain.php
3 years ago
checks.php
3 years ago
compatibility.php
3 years ago
display-conditions.php
3 years ago
filesystem.php
3 years ago
frontend-notices.php
3 years ago
frontend_checks.php
3 years ago
in-content-injector.php
3 years ago
inline-css.php
3 years ago
plugin.php
3 years ago
upgrades.php
6 years ago
utils.php
3 years ago
visitor-conditions.php
3 years ago
widget.php
3 years ago
inline-css.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Handles Advanced Ads Inline CSS settings. |
| 5 | */ |
| 6 | class Advanced_Ads_Inline_Css { |
| 7 | /** |
| 8 | * Holds the state if inline css should be output or not. |
| 9 | * |
| 10 | * @var bool |
| 11 | */ |
| 12 | protected $add_inline_css; |
| 13 | |
| 14 | /** |
| 15 | * Initialize the module. |
| 16 | */ |
| 17 | public function __construct() { |
| 18 | |
| 19 | /** |
| 20 | * Filters the state if inline css should be output or not. |
| 21 | * Ajax CB container could have added inline css already. |
| 22 | * |
| 23 | * Set to false if an addon output inline css before the main plugin. |
| 24 | * |
| 25 | * @param bool Contains the state. |
| 26 | */ |
| 27 | $this->add_inline_css = apply_filters( 'advanced-ads-output-inline-css', true ); |
| 28 | if ( ! $this->add_inline_css ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // Add inline css to the tcf container. |
| 33 | $this->check_tcf_option(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Adds inline css. |
| 38 | * |
| 39 | * @param array $wrapper Add wrapper array. |
| 40 | * @param string $css Custom inline css. |
| 41 | * @param bool|null $global_output Whether this ad is using cache-busting. |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | public function add_css( $wrapper, $css, $global_output ) { |
| 46 | $this->add_inline_css = $this->add_inline_css && $global_output !== false; |
| 47 | if ( ! $this->add_inline_css ) { |
| 48 | return $wrapper; |
| 49 | } |
| 50 | |
| 51 | $styles = $this->get_styles_by_string( $css ); |
| 52 | $wrapper['style'] = empty( $wrapper['style'] ) ? $styles : array_merge( $wrapper['style'], $styles ); |
| 53 | $this->add_inline_css = false; |
| 54 | |
| 55 | return $wrapper; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Extend TCF output with a container containing inline css. |
| 60 | * |
| 61 | * @param string $output The output string. |
| 62 | * @param Advanced_Ads_Ad $ad The ad object. |
| 63 | * |
| 64 | * @return string |
| 65 | */ |
| 66 | public function add_tcf_container( $output, Advanced_Ads_Ad $ad ) { |
| 67 | $inline_css = $ad->options( 'inline-css' ); |
| 68 | |
| 69 | if ( |
| 70 | ! $ad->global_output |
| 71 | || empty( $inline_css ) |
| 72 | || strpos( $output, '<div class="tcf-container"' ) === 0 |
| 73 | ) { |
| 74 | return $output; |
| 75 | } |
| 76 | |
| 77 | return sprintf( |
| 78 | '<div class="tcf-container" style="' . $inline_css . '">%s</div>', |
| 79 | $output |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Reformat css styles string to array. |
| 85 | * |
| 86 | * @param string $string CSS-Style. |
| 87 | * |
| 88 | * @return array |
| 89 | */ |
| 90 | private function get_styles_by_string( $string ) { |
| 91 | $chunks = array_chunk( preg_split( '/[:;]/', $string ), 2 ); |
| 92 | array_walk_recursive( $chunks, function( &$value ) { |
| 93 | $value = trim( $value ); |
| 94 | } ); |
| 95 | |
| 96 | return array_combine( array_filter( array_column( $chunks, 0 ) ), array_filter( array_column( $chunks, 1 ) ) ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * If TCF is active, i.e. there is a TCF container, add the options to this container. |
| 101 | */ |
| 102 | private function check_tcf_option() { |
| 103 | static $privacy_options; |
| 104 | if ( $privacy_options === null ) { |
| 105 | $privacy_options = Advanced_Ads_Privacy::get_instance()->options(); |
| 106 | } |
| 107 | |
| 108 | if ( ! empty( $privacy_options['enabled'] ) && $privacy_options['enabled'] === 'on' && $privacy_options['consent-method'] === 'iab_tcf_20' ) { |
| 109 | add_filter( 'advanced-ads-output-final', [ $this, 'add_tcf_container' ], 20, 2 ); |
| 110 | $this->add_inline_css = false; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 |