* @since 1.47.0 */ namespace AdvancedAds\Utilities; use AdvancedAds\Admin\Upsell; use AdvancedAds\Constants; use AdvancedAds\Framework\Utilities\Params; use AdvancedAds\Framework\Utilities\Str; use DateTimeZone; defined( 'ABSPATH' ) || exit; /** * Utilities WordPress. */ class WordPress { /** * Debug function * * @return void */ public static function dd(): void { echo '
';
		foreach ( func_get_args() as $arg ) {
			print_r( $arg ); // phpcs:ignore
		}
		echo '
'; die(); } /** * Function to calculate percentage * * @param int $part Part of total. * @param int $total Total value. * * @return string */ public static function calculate_percentage( $part, $total ): string { $percentage = ( $part / $total ) * 100; return number_format( $percentage, 2 ) . '%'; } /** * Get the current action selected from the bulk actions dropdown. * * @return string|false The action name or False if no action was selected */ public static function current_action() { $action = Params::request( 'action' ); if ( '-1' !== $action ) { return sanitize_key( $action ); } $action = Params::request( 'action2' ); if ( '-1' !== $action ) { return sanitize_key( $action ); } return false; } /** * Get count of ads * * @param string $status Status need count for. * * @return int */ public static function get_count_ads( $status = 'any' ): int { $counts = (array) wp_count_posts( Constants::POST_TYPE_AD ); if ( 'any' === $status ) { return array_sum( $counts ); } return $counts[ $status ] ?? 0; } /** * Get site domain * * @param string $part Part of domain. * * @return string */ public static function get_site_domain( $part = 'host' ): string { $domain = wp_parse_url( home_url( '/' ), PHP_URL_HOST ); if ( 'name' === $part ) { $domain = explode( '.', $domain ); $domain = count( $domain ) > 2 ? $domain[1] : $domain[0]; } return $domain; } /** * Get SVG content as string * * @param string $file File name. * @param string $folder Folder name if not default. * * @return string */ public static function get_svg( $file, $folder = '/assets/img/' ): string { $file_path = \untrailingslashit( ADVADS_ABSPATH ) . $folder . $file; if ( file_exists( $file_path ) ) { ob_start(); include $file_path; return ob_get_clean(); } return ''; } /** * Retrieves the timezone of the site as a DateTimeZone object. * * @return DateTimeZone */ public static function get_timezone(): DateTimeZone { static $advads_timezone; // Early bail!! if ( null !== $advads_timezone ) { return $advads_timezone; } if ( function_exists( 'wp_timezone' ) ) { $advads_timezone = wp_timezone(); return $advads_timezone; } $date_time_zone = new DateTimeZone( self::get_timezone_string() ); return $date_time_zone; } /** * Retrieves the timezone of the site as a string. * * @return string */ public static function get_timezone_string(): string { $timezone_string = get_option( 'timezone_string' ); if ( $timezone_string ) { return $timezone_string; } $offset = (float) get_option( 'gmt_offset' ); $hours = (int) $offset; $minutes = ( $offset - $hours ); $sign = ( $offset < 0 ) ? '-' : '+'; $abs_hour = abs( $hours ); $abs_mins = abs( $minutes * 60 ); return sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); } /** * Get literal expression of timezone. * * @return string Human readable timezone name. */ public static function get_timezone_name(): string { $time_zone = self::get_timezone()->getName(); if ( 'UTC' === $time_zone ) { return 'UTC+0'; } if ( 0 === strpos( $time_zone, '+' ) || 0 === strpos( $time_zone, '-' ) ) { return 'UTC' . $time_zone; } /* translators: timezone name */ return sprintf( __( 'time of %s', 'advanced-ads' ), $time_zone ); } /** * Applies image loading optimization attributes to an image HTML tag based on WordPress version. * * @param string $img HTML image tag. * @param string $context Image context. * * @return string Updated HTML image tag with loading optimization attributes. */ public static function img_tag_add_loading_attr( $img, $context ) { if ( is_array( $context ) ) { $context = end( $context ); } // Check if the current WordPress version is compatible. if ( is_wp_version_compatible( '6.3' ) ) { return wp_img_tag_add_loading_optimization_attrs( $img, $context ); } return wp_img_tag_add_loading_attr( $img, $context ); // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_img_tag_add_loading_attrFound } /** * Improve WP_Query performance * * @param array $args Query arguments. * * @return array */ public static function improve_wp_query( $args ): array { $args['no_found_rows'] = true; $args['update_post_meta_cache'] = false; $args['update_post_term_cache'] = false; return $args; } /** * Sanitize conditions * * @param array> $conditions Conditions to sanitize. * * @return array> */ public static function sanitize_conditions( $conditions ): array { foreach ( $conditions as $index => $condition ) { if ( isset( $condition['operator'] ) && in_array( $condition['operator'], [ 'match', 'match_not' ], true ) ) { continue; } // skip paginated_post from value check. if ( isset( $condition['type'] ) && 'paginated_post' === $condition['type'] ) { continue; } // VC - IP address trim each line and drop empties. if ( isset( $condition['type'], $condition['value'] ) && 'ip_address' === $condition['type'] && is_string( $condition['value'] ) ) { $condition['value'] = implode( "\n", array_filter( array_map( 'trim', preg_split( '/\r?\n/', $condition['value'] ) ) ) ); $conditions[ $index ] = $condition; } if ( empty( $condition['value'] ) ) { unset( $conditions[ $index ] ); } } return $conditions; } /** * Check if the current user is a bot prepopulating the cache — ads should be loaded for the bot, because they should show up on the cached site. * * @return bool */ public static function is_cache_bot(): bool { $user_agent = Params::server( 'HTTP_USER_AGENT', '' ); if ( '' !== $user_agent ) { $current = sanitize_text_field( wp_unslash( $user_agent ) ); // WP Rocket. if ( Str::contains( 'wprocketbot', $current ) ) { return true; } // WP Super Cache. $wp_useragent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ), '' ); if ( $current === $wp_useragent ) { return true; } // LiteSpeed Cache: `lscache_runner` and `lscache_walker` user agents. if ( Str::contains( 'lscache_', $current ) ) { return true; } } return false; } /** * Unserializes data only if it was serialized. * * @link https://patchstack.com/articles/unauthenticated-php-object-injection-in-flatsome-theme-3-17-5/ * * @param mixed $data Data that might be unserialized. * * @return mixed Unserialized data can be any type. */ public static function maybe_unserialize( $data ) { if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in. return @unserialize( trim( $data ), [ 'allowed_classes' => false ] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize } return $data; } /** * Create a wrapper for a single option line * * @param string $id internal id of the option wrapper. * @param string $title label of the option. * @param string $content content of the option or full path to template file or custom flag to show a pre-defined information. * @param string $description description of the option. * * @return void */ public static function render_option( $id, $title, $content, $description = '' ) { /** * This filter allows to extend the class dynamically by add-ons — this would allow add-ons to dynamically hide/show only attributes belonging to them, practically not used now. */ $class = apply_filters( 'advanced-ads-option-class', $id ); // The title names the controls of the row for screen readers. The same row can appear several times on a page, so the id is unique. $title_id = wp_unique_id( 'advads-option-title-' ); ?>
' . $description . '

'; // could include various HTML elements. endif; // place an upgrade link below the description if there is one. if ( 'is_pro_pitch' === $content ) { Upsell::pro_feature_link( 'upgrade-pro-' . $id ); } ?>
'advanced-ads', 'utm_medium' => 'link', 'utm_campaign' => $utm_campaign, ], $url ); include ADVADS_ABSPATH . 'views/admin/manual-link.php'; } /** * Get the content of a file * * @param string $file File path. * * @return string File content. */ public static function get_file_content( $file ): string { if ( file_exists( $file ) ) { ob_start(); include $file; return ob_get_clean(); } return ''; } /** * Instantiates the WordPress filesystem for use * * @return \WP_Filesystem_Base|null */ public static function get_filesystem(): ?\WP_Filesystem_Base { global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } return $wp_filesystem instanceof \WP_Filesystem_Base ? $wp_filesystem : null; } }