* @since 1.50.0
*/
namespace AdvancedAds\Frontend;
use AdvancedAds\Abstracts\Ad;
use Advanced_Ads_Display_Conditions;
use Advanced_Ads_Visitor_Conditions;
use AdvancedAds\Utilities\Validation;
use AdvancedAds\Utilities\Conditional;
use AdvancedAds\Framework\Utilities\Str;
use AdvancedAds\Framework\Interfaces\Integration_Interface;
defined( 'ABSPATH' ) || exit;
/**
* Frontend Debug Ads.
*/
class Debug_Ads implements Integration_Interface {
/**
* Hook into WordPress.
*
* @return void
*/
public function hooks(): void {
add_filter( 'advanced-ads-ad-pre-output', [ $this, 'override_ad_output' ], 50, 2 );
}
/**
* Override ad output.
*
* @param string $output Override content.
* @param Ad $ad Ad object.
*
* @return string
*/
public function override_ad_output( $output, $ad ) {
$user_can_manage_ads = Conditional::user_can( 'advanced_ads_manage_options' );
if (
$ad->is_debug_mode() &&
( $user_can_manage_ads || ( ! $user_can_manage_ads && ! defined( 'ADVANCED_ADS_AD_DEBUG_FOR_ADMIN_ONLY' ) ) )
) {
return $this->prepare_output( $ad );
}
return $output;
}
/**
* Prepare debug mode output.
*
* @param Ad $ad Ad instance.
*
* @return string The ad debug output.
*/
private function prepare_output( Ad $ad ): string {
global $post, $wp_query;
$width = 300;
$height = 250;
if ( $ad->get_width() > 100 && $ad->get_height() > 100 ) {
$width = $ad->get_width();
$height = $ad->get_height();
}
$style = "width:{$width}px;height:{$height}px;background-color:#ddd;color:#1d2327;overflow:scroll;";
$style_full = 'width: 100%; height: 100vh; background-color: #ddd; color: #1d2327; overflow: scroll; position: fixed; top: 0; left: 0; min-width: 600px; z-index: 99999;';
$wrapper_id = Str::is_non_empty( $ad->get_wrapper_id() )
? $ad->get_wrapper_id()
: wp_advads()->get_frontend_prefix() . wp_rand();
$content = [];
if ( $ad->can_display( [ 'ignore_debugmode' => true ] ) ) {
$content[] = __( 'The ad is displayed on the page', 'advanced-ads' );
} else {
$content[] = __( 'The ad is not displayed on the page', 'advanced-ads' );
}
// Compare current wp_query with global wp_main_query.
if ( ! $wp_query->is_main_query() ) {
$content[] = sprintf( '%s', __( 'Current query is not identical to main query.', 'advanced-ads' ) );
$content[] = $this->build_query_diff_table();
}
if ( isset( $post->post_title ) && isset( $post->ID ) ) {
$content[] = sprintf( '%s: %s, %s: %s', __( 'current post', 'advanced-ads' ), $post->post_title, 'ID', $post->ID );
}
// Compare current post with global post.
if ( $wp_query->post !== $post ) {
$error = sprintf( '%s', __( 'Current post is not identical to main post.', 'advanced-ads' ) );
if ( isset( $wp_query->post->post_title ) && $wp_query->post->ID ) {
$error .= sprintf( '
%s: %s, %s: %s', __( 'main post', 'advanced-ads' ), $wp_query->post->post_title, 'ID', $wp_query->post->ID );
}
$content[] = $error;
}
$content[] = $this->build_call_chain( $ad );
$content[] = $this->build_display_conditions_table( $ad );
$content[] = $this->build_visitor_conditions_table( $ad );
$message = Validation::is_ad_https( $ad );
if ( $message ) {
$content[] = sprintf( '%s', $message );
}
$content = apply_filters( 'advanced-ads-ad-output-debug-content', $content, $ad );
$content = array_filter(
$content,
fn( $value ) => ! empty( $value )
);
ob_start();
include ADVADS_ABSPATH . '/public/views/ad-debug.php';
$output = ob_get_clean();
$output = apply_filters( 'advanced-ads-ad-output-debug', $output, $ad );
$output = apply_filters( 'advanced-ads-ad-output', $output, $ad );
return $output;
}
/**
* Build table with differences between current and main query
*
* @since 1.7.0.3
*
* @return string|false
*/
private function build_query_diff_table() {
global $wp_query, $wp_the_query;
$diff_current = array_diff_assoc( $wp_query->query_vars, $wp_the_query->query_vars );
$diff_main = array_diff_assoc( $wp_the_query->query_vars, $wp_query->query_vars );
if ( ! is_array( $diff_current ) || ! is_array( $diff_main ) ) {
return '';
}
ob_start();
?>
| %s | %s |
|---|
| %s | %s | |
|---|---|---|
| %s | %s | |
| %s | %s | %s |
| %s | %s | |
| %s | post_id: %s is_singular: %s | |
| %s | term_id: %s is_archive: %s | |
| %s | %s |