EDD_SL_Plugin_Updater.php
7 years ago
ad-ajax.php
7 years ago
ad-debug.php
8 years ago
ad-health-notices.php
7 years ago
ad-model.php
8 years ago
ad-select.php
9 years ago
ad.php
7 years ago
ad_ajax_callbacks.php
7 years ago
ad_group.php
7 years ago
ad_placements.php
7 years ago
ad_type_abstract.php
8 years ago
ad_type_content.php
7 years ago
ad_type_dummy.php
7 years ago
ad_type_group.php
8 years ago
ad_type_image.php
7 years ago
ad_type_plain.php
7 years ago
checks.php
7 years ago
compatibility.php
7 years ago
display-conditions.php
7 years ago
filesystem.php
8 years ago
frontend_checks.php
7 years ago
plugin.php
7 years ago
upgrades.php
7 years ago
utils.php
7 years ago
visitor-conditions.php
7 years ago
widget.php
7 years ago
utils.php
131 lines
| 1 | <?php |
| 2 | class Advanced_Ads_Utils { |
| 3 | /** |
| 4 | * Merges multiple arrays, recursively, and returns the merged array. |
| 5 | * |
| 6 | * This function is similar to PHP's array_merge_recursive() function, but it |
| 7 | * handles non-array values differently. When merging values that are not both |
| 8 | * arrays, the latter value replaces the former rather than merging with it. |
| 9 | * |
| 10 | * Example: |
| 11 | * $link_options_1 = array( 'fragment' => 'x', 'class' => array( 'a', 'b' ) ); |
| 12 | * $link_options_2 = array( 'fragment' => 'y', 'class' => array( 'c', 'd' ) ); |
| 13 | * // This results in array( 'fragment' => 'y', 'class' => array( 'a', 'b', 'c', 'd' ) ). |
| 14 | * |
| 15 | * @param array $arrays An arrays of arrays to merge. |
| 16 | * @param bool $preserve_integer_keys (optional) If given, integer keys will be preserved and merged instead of appended. |
| 17 | * @return array The merged array. |
| 18 | * @copyright Copyright 2001 - 2013 Drupal contributors. License: GPL-2.0+. Drupal is a registered trademark of Dries Buytaert. |
| 19 | */ |
| 20 | public static function merge_deep_array( array $arrays, $preserve_integer_keys = FALSE ) { |
| 21 | $result = array(); |
| 22 | foreach ( $arrays as $array ) { |
| 23 | if ( ! is_array( $array ) ) { continue; } |
| 24 | |
| 25 | foreach ( $array as $key => $value ) { |
| 26 | // Renumber integer keys as array_merge_recursive() does unless |
| 27 | // $preserve_integer_keys is set to TRUE. Note that PHP automatically |
| 28 | // converts array keys that are integer strings (e.g., '1') to integers. |
| 29 | if ( is_integer( $key ) && ! $preserve_integer_keys ) { |
| 30 | $result[] = $value; |
| 31 | } |
| 32 | // Recurse when both values are arrays. |
| 33 | elseif ( isset( $result[ $key ] ) && is_array( $result[ $key ] ) && is_array( $value ) ) { |
| 34 | $result[ $key ] = self::merge_deep_array( array( $result[ $key ], $value ), $preserve_integer_keys ); |
| 35 | } |
| 36 | // Otherwise, use the latter value, overriding any previous value. |
| 37 | else { |
| 38 | $result[ $key ] = $value; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | return $result; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Convert array of html attributes to string. |
| 47 | * |
| 48 | * @param array $data |
| 49 | * @return string |
| 50 | * @since untagged |
| 51 | */ |
| 52 | public static function build_html_attributes( $data ) { |
| 53 | $result = ''; |
| 54 | foreach ( $data as $_html_attr => $_values ){ |
| 55 | if ( $_html_attr == 'style' ){ |
| 56 | $_style_values_string = ''; |
| 57 | foreach ( $_values as $_style_attr => $_style_values ){ |
| 58 | if ( is_array( $_style_values ) ) { |
| 59 | $_style_values_string .= $_style_attr . ': ' .implode( ' ', $_style_values ). '; '; } |
| 60 | else { |
| 61 | $_style_values_string .= $_style_attr . ': ' .$_style_values. '; '; } |
| 62 | } |
| 63 | $result .= " style=\"$_style_values_string\""; |
| 64 | } else { |
| 65 | if ( is_array( $_values ) ) { |
| 66 | $_values_string = esc_attr( implode( ' ', $_values ) ); } |
| 67 | else { |
| 68 | $_values_string = esc_attr( $_values ); } |
| 69 | $result .= " $_html_attr=\"$_values_string\""; |
| 70 | } |
| 71 | } |
| 72 | return $result; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get inline asset. |
| 77 | * |
| 78 | * @param str $content |
| 79 | * @return str $content |
| 80 | */ |
| 81 | public static function get_inline_asset( $content ) { |
| 82 | if ( Advanced_Ads_Checks::active_autoptimize() || Advanced_Ads_Checks::active_wp_rocket() ) { |
| 83 | return '<!--noptimize-->' . $content . '<!--/noptimize-->'; |
| 84 | } |
| 85 | // WP Fastest Cache Premium: "Render Blocking Js" feature. |
| 86 | if ( class_exists( 'WpFastestCache', false ) |
| 87 | && '<script' === substr( $content, 0, 7 ) ) { |
| 88 | $content = substr_replace( $content, '<script data-wpfc-render="false"', 0, 7 ); |
| 89 | } |
| 90 | return $content; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get nested ads of an ad or a group. |
| 95 | * |
| 96 | * @param str $id Id. |
| 97 | * @param str $type Type (placement, ad or group). |
| 98 | * @return array of Advanced_Ads_Ad objects. |
| 99 | */ |
| 100 | public static function get_nested_ads( $id, $type ) { |
| 101 | $result = array(); |
| 102 | |
| 103 | switch( $type ) { |
| 104 | case 'placement': |
| 105 | $placements = Advanced_Ads::get_ad_placements_array(); |
| 106 | if ( isset( $placements[ $id ]['item'] ) ) { |
| 107 | $item = explode( '_', $placements[ $id ]['item'] ); |
| 108 | if ( isset( $item[1] ) ) { |
| 109 | return self::get_nested_ads( $item[1], $item[0] ); |
| 110 | } |
| 111 | } |
| 112 | case 'ad': |
| 113 | $ad = new Advanced_Ads_Ad( $id ); |
| 114 | $result[] = $ad; |
| 115 | if ( 'group' === $ad->type && ! empty( $ad->output['group_id'] ) ) { |
| 116 | $result = array_merge( $result, self::get_nested_ads( $ad->output['group_id'], 'group' ) ); |
| 117 | } |
| 118 | break; |
| 119 | case 'group': |
| 120 | $group = new Advanced_Ads_Group( $id ); |
| 121 | $ads = $group->get_all_ads(); |
| 122 | foreach ( $ads as $ad ) { |
| 123 | $result = array_merge( $result, self::get_nested_ads( $ad->ID, 'ad' ) ); |
| 124 | } |
| 125 | break; |
| 126 | } |
| 127 | return $result; |
| 128 | } |
| 129 | } |
| 130 | ?> |
| 131 |