abstracts
2 months ago
admin
2 months ago
ads
3 months ago
compatibility
3 months ago
crons
3 months ago
frontend
2 months ago
groups
3 months ago
importers
2 months ago
installation
1 year ago
interfaces
4 months ago
license
3 months ago
placements
3 months ago
rest
1 year ago
traits
4 months ago
utilities
3 months ago
array_ad_conditions.php
1 year ago
cap_map.php
3 years ago
class-assets-registry.php
3 months ago
class-autoloader.php
1 year ago
class-constants.php
1 year ago
class-content-injector.php
2 months ago
class-entities.php
3 months ago
class-modal.php
1 year ago
class-modules.php
1 year ago
class-options.php
1 year ago
class-plugin.php
2 months ago
class-post-data.php
10 months ago
class-shortcodes.php
2 months ago
class-upgrades.php
1 year ago
class-widget.php
11 months ago
default-hooks.php
4 months ago
functions-ad.php
1 year ago
functions-components.php
3 months ago
functions-conditional.php
1 year ago
functions-core.php
1 year ago
functions-group.php
1 year ago
functions-placement.php
1 year ago
functions.php
3 months ago
index.php
2 years ago
load_modules.php
2 years ago
functions.php
122 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Functions that are directly available in WordPress themes (and plugins) |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.0 |
| 8 | */ |
| 9 | |
| 10 | use AdvancedAds\Framework\Utilities\Params; |
| 11 | |
| 12 | /** |
| 13 | * Returns the default arguments for an entity. |
| 14 | * |
| 15 | * @param string $method The method to get the entity. |
| 16 | * @param int|string $id The ID of the entity. |
| 17 | * @param array $args Additional arguments for the entity. |
| 18 | * |
| 19 | * @return array The default arguments for the entity. |
| 20 | */ |
| 21 | function wp_advads_default_entity_arguments( $method, $id, $args ): array { |
| 22 | $args = (array) $args; |
| 23 | |
| 24 | $args['previous_id'] = $args['id'] ?? null; |
| 25 | $args['previous_method'] = $args['method'] ?? null; |
| 26 | |
| 27 | if ( $id || ! isset( $args['id'] ) ) { |
| 28 | $args['id'] = $id; |
| 29 | } |
| 30 | |
| 31 | $args['method'] = $method; |
| 32 | |
| 33 | return apply_filters( 'advanced-ads-ad-select-args', $args, $method, $id ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Sets additional arguments for an entity. |
| 38 | * |
| 39 | * @param object $entity The entity object. |
| 40 | * @param array $args The additional arguments to set for the entity. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | function wp_advads_set_additional_args( $entity, $args ): void { |
| 45 | $entity->set_prop_temp( 'ad_args', $args ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Load ad conditions. |
| 50 | * |
| 51 | * @return array |
| 52 | */ |
| 53 | function wp_advads_get_ad_conditions(): array { |
| 54 | static $ad_conditions; |
| 55 | if ( null === $ad_conditions ) { |
| 56 | $ad_conditions = include ADVADS_ABSPATH . 'includes/array_ad_conditions.php'; |
| 57 | } |
| 58 | |
| 59 | return $ad_conditions; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get user IP address. |
| 64 | * |
| 65 | * @return bool|string IP address or false if not found |
| 66 | */ |
| 67 | function get_user_ip_address() { |
| 68 | // phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- NO NEED TO SANITIZE HEADERS |
| 69 | |
| 70 | // Define the list of IP headers in the order of priority. |
| 71 | $ip_headers = [ |
| 72 | 'HTTP_CF_CONNECTING_IP', // Cloudflare. |
| 73 | 'HTTP_CLIENT_IP', // General. |
| 74 | 'HTTP_X_REAL_IP', // General. |
| 75 | 'HTTP_X_FORWARDED_FOR', // General. |
| 76 | 'HTTP_X_FORWARDED', // General. |
| 77 | 'HTTP_X_CLUSTER_CLIENT_IP', // General. |
| 78 | 'HTTP_FORWARDED_FOR', // General. |
| 79 | 'HTTP_FORWARDED', // General. |
| 80 | 'REMOTE_ADDR', // Default server value. |
| 81 | ]; |
| 82 | |
| 83 | // Get the server's IP address. |
| 84 | $server_ip = Params::server( 'SERVER_ADDR', '' ); |
| 85 | |
| 86 | foreach ( $ip_headers as $header ) { |
| 87 | // Check if the header exists and is not empty. |
| 88 | $data = Params::server( $header, '' ); |
| 89 | if ( ! empty( $data ) ) { |
| 90 | // Split the header value by comma to handle multiple IP addresses. |
| 91 | $ip_list = explode( ',', $data ); |
| 92 | |
| 93 | foreach ( $ip_list as $ip ) { |
| 94 | // Trim whitespace and remove any 'for=' prefix from the IP address. |
| 95 | $ip = trim( str_replace( 'for=', '', $ip ) ); |
| 96 | |
| 97 | // Validate the IP address and ensure it's not the server's IP. |
| 98 | if ( filter_var( $ip, FILTER_VALIDATE_IP ) && $ip !== $server_ip ) { |
| 99 | return $ip; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | // phpcs:enable |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns an empty array if the value is null. |
| 110 | * |
| 111 | * @param mixed $value The value to check. |
| 112 | * |
| 113 | * @return array |
| 114 | */ |
| 115 | if ( ! function_exists( '__return_array_if_null' ) ) { |
| 116 | // phpcs:disable |
| 117 | function __return_array_if_null( $value ): array { |
| 118 | return $value ?? []; |
| 119 | } |
| 120 | // phpcs:enable |
| 121 | } |
| 122 |