class-ad-list-stats.php
1 week ago
class-cache.php
1 week ago
class-conditional.php
3 months ago
class-content-injection.php
1 year ago
class-data.php
1 week ago
class-sanitize.php
1 year ago
class-testing.php
1 year ago
class-validation.php
1 year ago
class-wordpress.php
1 week ago
index.php
2 years ago
class-conditional.php
309 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The class provides utility functions related to check condition related to plugin. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.47.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Utilities; |
| 11 | |
| 12 | use Advanced_Ads; |
| 13 | use AdvancedAds\Framework\Utilities\Params; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Conditional. |
| 19 | */ |
| 20 | class Conditional { |
| 21 | |
| 22 | /** |
| 23 | * Check if the author of the ad can use unfiltered_html. |
| 24 | * |
| 25 | * @param int $author_id User ID of the ad author. |
| 26 | * |
| 27 | * @return bool |
| 28 | */ |
| 29 | public static function can_author_unfiltered_html( $author_id ): bool { |
| 30 | if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | $unfiltered_allowed = user_can( $author_id, 'unfiltered_html' ); |
| 35 | if ( $unfiltered_allowed || ! is_multisite() ) { |
| 36 | return $unfiltered_allowed; |
| 37 | } |
| 38 | |
| 39 | $options = Advanced_Ads::get_instance()->options(); |
| 40 | if ( ! isset( $options['allow-unfiltered-html'] ) ) { |
| 41 | $options['allow-unfiltered-html'] = []; |
| 42 | } |
| 43 | $allowed_roles = $options['allow-unfiltered-html']; |
| 44 | $user = get_user_by( 'id', $author_id ); |
| 45 | |
| 46 | if ( ! $user ) { |
| 47 | // In case the author was removed from the site. |
| 48 | $user = wp_get_current_user(); |
| 49 | } |
| 50 | |
| 51 | return ! empty( array_intersect( $user->roles, $allowed_roles ) ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Checks if the current request is an AJAX request. |
| 56 | * It can be a request to `admin-ajax.php` or to `ajax-handler.php`. |
| 57 | * |
| 58 | * @return bool |
| 59 | */ |
| 60 | public static function doing_ajax(): bool { |
| 61 | return wp_doing_ajax() || 'XMLHttpRequest' === Params::server( 'HTTP_X_REQUESTED_WITH' ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Determines whether the current request is an autosave |
| 66 | * |
| 67 | * @return bool |
| 68 | */ |
| 69 | public static function doing_autosave(): bool { |
| 70 | return defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Check if we have cache enabled on site |
| 75 | * |
| 76 | * @return bool |
| 77 | */ |
| 78 | public static function has_cache_plugins(): bool { |
| 79 | return ( defined( 'WP_CACHE' ) && WP_CACHE ) // General cache constant. |
| 80 | || defined( 'W3TC' ) // W3 Total Cache. |
| 81 | || function_exists( 'wp_super_cache_text_domain' ) // WP Super Cache. |
| 82 | || defined( 'WP_ROCKET_SLUG' ) // WP Rocket. |
| 83 | || defined( 'WPFC_WP_CONTENT_DIR' ) // WP Fastest Cache. |
| 84 | || class_exists( 'HyperCache', false ) // Hyper Cache. |
| 85 | || defined( 'CE_CACHE_DIR' ); // Cache Enabler. |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Check if the current user has a role on this site. |
| 90 | * |
| 91 | * @return bool |
| 92 | */ |
| 93 | public static function has_user_role_on_site(): bool { |
| 94 | return in_array( |
| 95 | get_current_blog_id(), |
| 96 | wp_list_pluck( get_blogs_of_user( get_current_user_id() ), 'userblog_id' ), |
| 97 | true |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Is entity allowed |
| 103 | * |
| 104 | * @param string $entity Entity to check. |
| 105 | * |
| 106 | * @return boolean |
| 107 | */ |
| 108 | public static function is_entity_allowed( $entity ): bool { |
| 109 | if ( empty( $entity ) || ! in_array( $entity, [ 'ad', 'group', 'placement' ], true ) ) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Check if ads are disabled. |
| 118 | * |
| 119 | * @return bool |
| 120 | */ |
| 121 | public static function is_ad_disabled(): bool { |
| 122 | return defined( 'ADVADS_ADS_DISABLED' ) && ADVADS_ADS_DISABLED; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Check if any add-on is activated |
| 127 | * |
| 128 | * @return bool true if there is any add-on activated |
| 129 | */ |
| 130 | public static function is_any_addon_activated(): bool { |
| 131 | return has_action( 'advanced-ads-loaded' ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Check if the current page is an AMP page. |
| 136 | * |
| 137 | * @return bool |
| 138 | */ |
| 139 | public static function is_amp(): bool { |
| 140 | return function_exists( 'advads_is_amp' ) && advads_is_amp(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Check if the current user agent is given or a bot |
| 145 | * |
| 146 | * @return bool |
| 147 | */ |
| 148 | public static function is_ua_bot(): bool { |
| 149 | // show ads on AMP version also for bots in order to allow Google (and maybe others) to cache the page. |
| 150 | if ( self::is_amp() ) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | $user_agent = Params::server( 'HTTP_USER_AGENT', '' ); |
| 155 | if ( empty( $user_agent ) ) { |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | // Make sure delimiters in regex are escaped. |
| 160 | $bots = implode( '|', Data::get_bots() ); |
| 161 | $bots = preg_replace( '/(.*?)(?<!\\\)' . preg_quote( '/', '/' ) . '(.*?)/', '$1\\/$2', $bots ); |
| 162 | |
| 163 | return preg_match( sprintf( '/%s/i', $bots ), wp_unslash( $user_agent ) ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns true if a REST request has an Advanced Ads endpoint. |
| 168 | * |
| 169 | * @return bool |
| 170 | */ |
| 171 | public static function is_gutenberg_writing_request(): bool { |
| 172 | global $wp; |
| 173 | |
| 174 | $rest_route = $wp->query_vars['rest_route'] ?? ''; |
| 175 | $is_writing = in_array( Params::server( 'REQUEST_METHOD' ), [ 'POST', 'PUT' ], true ); |
| 176 | $is_gutenberg = mb_strpos( $rest_route, '/wp/v2/posts' ) !== false || mb_strpos( $rest_route, '/wp/v2/pages' ) !== false; |
| 177 | |
| 178 | return $is_gutenberg && $is_writing; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Check if php execution is globally forbidden |
| 183 | * |
| 184 | * @return bool |
| 185 | */ |
| 186 | public static function is_php_allowed(): bool { |
| 187 | return ! ( defined( 'ADVANCED_ADS_DISALLOW_PHP' ) && ADVANCED_ADS_DISALLOW_PHP ) |
| 188 | && ! ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns true if the request is a non-legacy REST API request. |
| 193 | * |
| 194 | * TODO: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061. |
| 195 | * |
| 196 | * @return bool |
| 197 | */ |
| 198 | public static function is_rest_request() { |
| 199 | $request = Params::server( 'REQUEST_URI' ); |
| 200 | if ( empty( $request ) ) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | return false !== strpos( $request, trailingslashit( rest_get_url_prefix() ) ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Check if we are on desired screen. |
| 209 | * |
| 210 | * @param array|string $screen_id Screen id. |
| 211 | * |
| 212 | * @return bool |
| 213 | */ |
| 214 | public static function is_screen( $screen_id ): bool { |
| 215 | static $advads_current_screen; |
| 216 | |
| 217 | if ( ! is_admin() ) { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | if ( null === $advads_current_screen ) { |
| 222 | $advads_current_screen = get_current_screen()->id; |
| 223 | $advads_current_screen = explode( '_page_', $advads_current_screen ); |
| 224 | $advads_current_screen = array_pop( $advads_current_screen ); |
| 225 | $advads_current_screen = str_replace( 'advanced-ads-', '', $advads_current_screen ); |
| 226 | } |
| 227 | |
| 228 | return is_array( $screen_id ) |
| 229 | ? in_array( $advads_current_screen, $screen_id, true ) |
| 230 | : $advads_current_screen === $screen_id; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Check if the current screen belongs to Advanced Ads |
| 235 | * |
| 236 | * @return bool |
| 237 | */ |
| 238 | public static function is_screen_advanced_ads(): bool { |
| 239 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | $screen = get_current_screen(); |
| 244 | if ( ! isset( $screen->id ) ) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | return in_array( $screen->id, Data::get_admin_screen_ids(), true ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns whether the current user has the specified capability. |
| 253 | * |
| 254 | * @param string $capability Capability name. |
| 255 | * |
| 256 | * @return bool |
| 257 | */ |
| 258 | public static function user_can( $capability = 'manage_options' ): bool { |
| 259 | // Admins can do everything. |
| 260 | if ( current_user_can( 'manage_options' ) ) { |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | return current_user_can( |
| 265 | apply_filters( 'advanced-ads-capability', $capability ) |
| 266 | ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Returns the capability needed to perform an action |
| 271 | * |
| 272 | * @param string $capability A capability to check, can be internal to Advanced Ads. |
| 273 | * |
| 274 | * @return string |
| 275 | */ |
| 276 | public static function user_cap( $capability = 'manage_options' ) { |
| 277 | // Admins can do everything. |
| 278 | if ( current_user_can( 'manage_options' ) ) { |
| 279 | return 'manage_options'; |
| 280 | } |
| 281 | |
| 282 | return apply_filters( 'advanced-ads-capability', $capability ); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Check if the user can subscribe to a notice. |
| 287 | * |
| 288 | * @param string $notice Notice ID. |
| 289 | * |
| 290 | * @return bool |
| 291 | */ |
| 292 | public static function user_can_subscribe( $notice ) { |
| 293 | $current_user = wp_get_current_user(); |
| 294 | // Early bail!! |
| 295 | if ( empty( $current_user->ID ) || empty( $current_user->user_email ) ) { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | $subscribed_notices = get_user_meta( $current_user->ID, 'advanced-ads-subscribed', true ); |
| 300 | if ( ! is_array( $subscribed_notices ) ) { |
| 301 | $subscribed_notices = []; |
| 302 | } |
| 303 | |
| 304 | // secureserver.net email address belong to GoDaddy (?) and have very, very low open rates. Seems like only temporary setup. |
| 305 | return ( ! isset( $subscribed_notices[ $notice ] ) && is_email( $current_user->user_email ) && false === strpos( $current_user->user_email, 'secureserver.net' ) ) |
| 306 | ? true : false; |
| 307 | } |
| 308 | } |
| 309 |