class-ad-list-stats.php
1 week ago
class-addons.php
1 day ago
class-cache.php
1 week ago
class-conditional.php
1 day ago
class-content-injection.php
1 year ago
class-data.php
1 day ago
class-sanitize.php
1 year ago
class-testing.php
1 year ago
class-validation.php
1 day ago
class-wordpress.php
1 day ago
index.php
2 years ago
class-conditional.php
290 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 | * Check if we have cache enabled on site |
| 56 | * |
| 57 | * @return bool |
| 58 | */ |
| 59 | public static function has_cache_plugins(): bool { |
| 60 | return ( defined( 'WP_CACHE' ) && WP_CACHE ) // General cache constant. |
| 61 | || defined( 'W3TC' ) // W3 Total Cache. |
| 62 | || function_exists( 'wp_super_cache_text_domain' ) // WP Super Cache. |
| 63 | || defined( 'WP_ROCKET_SLUG' ) // WP Rocket. |
| 64 | || defined( 'WPFC_WP_CONTENT_DIR' ) // WP Fastest Cache. |
| 65 | || class_exists( 'HyperCache', false ) // Hyper Cache. |
| 66 | || defined( 'CE_CACHE_DIR' ); // Cache Enabler. |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Check if the current user has a role on this site. |
| 71 | * |
| 72 | * @return bool |
| 73 | */ |
| 74 | public static function has_user_role_on_site(): bool { |
| 75 | return in_array( |
| 76 | get_current_blog_id(), |
| 77 | wp_list_pluck( get_blogs_of_user( get_current_user_id() ), 'userblog_id' ), |
| 78 | true |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Is entity allowed |
| 84 | * |
| 85 | * @param string $entity Entity to check. |
| 86 | * |
| 87 | * @return boolean |
| 88 | */ |
| 89 | public static function is_entity_allowed( $entity ): bool { |
| 90 | if ( empty( $entity ) || ! in_array( $entity, [ 'ad', 'group', 'placement' ], true ) ) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Check if ads are disabled. |
| 99 | * |
| 100 | * @return bool |
| 101 | */ |
| 102 | public static function is_ad_disabled(): bool { |
| 103 | return defined( 'ADVADS_ADS_DISABLED' ) && ADVADS_ADS_DISABLED; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Check if any add-on is activated |
| 108 | * |
| 109 | * @return bool true if there is any add-on activated |
| 110 | */ |
| 111 | public static function is_any_addon_activated(): bool { |
| 112 | return has_action( 'advanced-ads-loaded' ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Check if the current page is an AMP page. |
| 117 | * |
| 118 | * @return bool |
| 119 | */ |
| 120 | public static function is_amp(): bool { |
| 121 | return function_exists( 'advads_is_amp' ) && advads_is_amp(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Check if the current user agent is given or a bot |
| 126 | * |
| 127 | * @return bool |
| 128 | */ |
| 129 | public static function is_ua_bot(): bool { |
| 130 | // show ads on AMP version also for bots in order to allow Google (and maybe others) to cache the page. |
| 131 | if ( self::is_amp() ) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | $user_agent = Params::server( 'HTTP_USER_AGENT', '' ); |
| 136 | if ( empty( $user_agent ) ) { |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | // Make sure delimiters in regex are escaped. |
| 141 | $bots = implode( '|', Data::get_bots() ); |
| 142 | $bots = preg_replace( '/(.*?)(?<!\\\)' . preg_quote( '/', '/' ) . '(.*?)/', '$1\\/$2', $bots ); |
| 143 | |
| 144 | return preg_match( sprintf( '/%s/i', $bots ), wp_unslash( $user_agent ) ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns true if a REST request has an Advanced Ads endpoint. |
| 149 | * |
| 150 | * @return bool |
| 151 | */ |
| 152 | public static function is_gutenberg_writing_request(): bool { |
| 153 | global $wp; |
| 154 | |
| 155 | $rest_route = $wp->query_vars['rest_route'] ?? ''; |
| 156 | $is_writing = in_array( Params::server( 'REQUEST_METHOD' ), [ 'POST', 'PUT' ], true ); |
| 157 | $is_gutenberg = mb_strpos( $rest_route, '/wp/v2/posts' ) !== false || mb_strpos( $rest_route, '/wp/v2/pages' ) !== false; |
| 158 | |
| 159 | return $is_gutenberg && $is_writing; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Check if php execution is globally forbidden |
| 164 | * |
| 165 | * @return bool |
| 166 | */ |
| 167 | public static function is_php_allowed(): bool { |
| 168 | return ! ( defined( 'ADVANCED_ADS_DISALLOW_PHP' ) && ADVANCED_ADS_DISALLOW_PHP ) |
| 169 | && ! ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns true if the request is a non-legacy REST API request. |
| 174 | * |
| 175 | * TODO: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061. |
| 176 | * |
| 177 | * @return bool |
| 178 | */ |
| 179 | public static function is_rest_request() { |
| 180 | $request = Params::server( 'REQUEST_URI' ); |
| 181 | if ( empty( $request ) ) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | return false !== strpos( $request, trailingslashit( rest_get_url_prefix() ) ); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Check if we are on desired screen. |
| 190 | * |
| 191 | * @param array|string $screen_id Screen id. |
| 192 | * |
| 193 | * @return bool |
| 194 | */ |
| 195 | public static function is_screen( $screen_id ): bool { |
| 196 | static $advads_current_screen; |
| 197 | |
| 198 | if ( ! is_admin() ) { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | if ( null === $advads_current_screen ) { |
| 203 | $advads_current_screen = get_current_screen()->id; |
| 204 | $advads_current_screen = explode( '_page_', $advads_current_screen ); |
| 205 | $advads_current_screen = array_pop( $advads_current_screen ); |
| 206 | $advads_current_screen = str_replace( 'advanced-ads-', '', $advads_current_screen ); |
| 207 | } |
| 208 | |
| 209 | return is_array( $screen_id ) |
| 210 | ? in_array( $advads_current_screen, $screen_id, true ) |
| 211 | : $advads_current_screen === $screen_id; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Check if the current screen belongs to Advanced Ads |
| 216 | * |
| 217 | * @return bool |
| 218 | */ |
| 219 | public static function is_screen_advanced_ads(): bool { |
| 220 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | $screen = get_current_screen(); |
| 225 | if ( ! isset( $screen->id ) ) { |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | return in_array( $screen->id, Data::get_admin_screen_ids(), true ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Returns whether the current user has the specified capability. |
| 234 | * |
| 235 | * @param string $capability Capability name. |
| 236 | * |
| 237 | * @return bool |
| 238 | */ |
| 239 | public static function user_can( $capability = 'manage_options' ): bool { |
| 240 | // Admins can do everything. |
| 241 | if ( current_user_can( 'manage_options' ) ) { |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | return current_user_can( |
| 246 | apply_filters( 'advanced-ads-capability', $capability ) |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Returns the capability needed to perform an action |
| 252 | * |
| 253 | * @param string $capability A capability to check, can be internal to Advanced Ads. |
| 254 | * |
| 255 | * @return string |
| 256 | */ |
| 257 | public static function user_cap( $capability = 'manage_options' ) { |
| 258 | // Admins can do everything. |
| 259 | if ( current_user_can( 'manage_options' ) ) { |
| 260 | return 'manage_options'; |
| 261 | } |
| 262 | |
| 263 | return apply_filters( 'advanced-ads-capability', $capability ); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Check if the user can subscribe to a notice. |
| 268 | * |
| 269 | * @param string $notice Notice ID. |
| 270 | * |
| 271 | * @return bool |
| 272 | */ |
| 273 | public static function user_can_subscribe( $notice ) { |
| 274 | $current_user = wp_get_current_user(); |
| 275 | // Early bail!! |
| 276 | if ( empty( $current_user->ID ) || empty( $current_user->user_email ) ) { |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | $subscribed_notices = get_user_meta( $current_user->ID, 'advanced-ads-subscribed', true ); |
| 281 | if ( ! is_array( $subscribed_notices ) ) { |
| 282 | $subscribed_notices = []; |
| 283 | } |
| 284 | |
| 285 | // secureserver.net email address belong to GoDaddy (?) and have very, very low open rates. Seems like only temporary setup. |
| 286 | return ( ! isset( $subscribed_notices[ $notice ] ) && is_email( $current_user->user_email ) && false === strpos( $current_user->user_email, 'secureserver.net' ) ) |
| 287 | ? true : false; |
| 288 | } |
| 289 | } |
| 290 |