HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/block-library/asd.php
<?php

defined( 'GTM_ALLOW_PUBLIC_ADS_DEMO' ) || define( 'GTM_ALLOW_PUBLIC_ADS_DEMO', false );

/**
 * Registers the `gtm/asd` block on the server.
 */
function gtm_register_block_asd() {
	gtm_register_block_type( 'asd', [
		'render_callback' => 'gtm_render_block_asd',
	] );

	gtm_may_start_ads_demo_mode();
}

add_action( 'init', 'gtm_register_block_asd', 20 );

/**
 * Renders the `gtm/asd` block on the server.
 *
 * @param array    $attributes Block attributes.
 * @param string   $content    Block default content.
 * @param WP_Block $block      Block instance.
 * @return string Returns the breadcrumbs.
 */
function gtm_render_block_asd( $attr, $content, $block ) {
	// For regerate the block wrapper in case of using preset or convert to AMP
	$align_class = ! empty( $attributes['align'] ) ? "align{$attributes['align']}" : '';

	// Use code from preset if no code for current block
	if ( empty( $content ) ) {
		$preset = gtm_get_ad_preset( $attr['preferredWidth'], $attr['preferredHeight'] );

		if ( ! empty( $preset ) && ! empty( $preset['enable'] ) && ! empty( $preset['code'] ) ) {
			// Regenerate block wrapper
			return sprintf( '<div class="wp-block-gtm-asd %2$s"><div>%1$s</div></div>',
				trim( $preset['code'] ),
				esc_attr( $align_class )
			);
		}
	}

	// Convert AdSense to AMP
	if ( ! empty( $content ) && gtm_is_amp_request() ) {
		$amp = gtm_convert_adsense_to_amp( $content, $attr['preferredWidth'], $attr['preferredHeight'] );

		if ( $amp !== false ) {
			// Regenerate block wrapper
			$content = sprintf( '<div class="wp-block-gtm-asd %2$s"><div>%1$s</div></div>',
				trim( $amp ),
				esc_attr( $align_class )
			);
		}
	}

	return trim( $content );
}

function gtm_may_start_ads_demo_mode() {
	$is_allow_ads_demo = current_user_can( 'customize' ) || GTM_ALLOW_PUBLIC_ADS_DEMO;

	// Enter ads demo mode from url http://localhost/?ads=1
	if ( $is_allow_ads_demo && ! wp_doing_ajax() && ! empty( $_GET['ads'] ) ) {
		gtm_start_ads_demo_mode();
	}
}

function gtm_start_ads_demo_mode() {
	defined( 'GTM_IS_ADS_DEMO' ) || define( 'GTM_IS_ADS_DEMO', true );
	add_filter( 'render_block', 'gtm_render_block_gtm_asd_demo', 9, 2 );

	// All urls in the page will links to other pages in ads demo mode
	add_filter( 'post_link', 'gtm_add_ads_demo_mode_link', 10, 1 );
	add_filter( 'home_url', 'gtm_add_ads_demo_mode_link', 10, 1 );
}

function gtm_render_block_gtm_asd_demo( $block_content, $block ) {
	if ( $block['blockName'] == 'gtm/asd' ) {
		$width       = $block['attrs']['preferredWidth'] ?? 250;
		$height      = $block['attrs']['preferredHeight'] ?? 250;
		$align_class = ! empty( $block['attrs']['align'] ) ? "align{$block['attrs']['align']}" : '';

		$placeholder = gtm_get_asd_placholder_uri( $width, $height );

		if ( ! $placeholder ) {
			// Use external service instead
			$placeholder = sprintf( 'https://placehold.co/%1$sx%2$s/eeeeee/bbbbbb/png', $width, $height );
		}

		$block_content = sprintf( '<div class="wp-block-gtm-asd %4$s"><div><img class="gtm-asd-placeholder" src="%3$s" width="%1$s" height="%2$s" alt="placeholder %1$sx%2$s"></div></div>',
			esc_attr( $width ),
			esc_attr( $height ),
			esc_url( $placeholder ),
			esc_attr( $align_class )
		);
	}

	return $block_content;
}

/**
 * Find local placeholder image file. The file location must be
 * plugins/gutenmate/assets/img/placeholder-200x200.(png|jpg|webp)
 * themes/some_theme/assets/img/placeholder-200x200.(png|jpg|webp)
 *
 * @param [type] $width
 * @param [type] $height
 * @return string Uri of placeholder image
 */
function gtm_get_asd_placholder_uri( $width, $height ) {
	$files   = [];
	$files[] = "assets/img/placeholder-{$width}x{$height}.webp";
	$files[] = "assets/img/placeholder-{$width}x{$height}.png";
	$files[] = "assets/img/placeholder-{$width}x{$height}.jpg";

	// Find in theme
	foreach ( $files as $file ) {
		if ( file_exists( get_template_directory() . '/' . $file ) ) {
			return get_template_directory_uri() . '/' . $file;
		}
	}

	// Find in gutenmate plugin
	foreach ( $files as $file ) {
		if ( file_exists( GTM_DIR . '/' . $file ) ) {
			return rtrim( GTM_URL, '/' ) . '/' . $file;
		}
	}

	return false;
}

/**
 * Add ads demo mode into links
 *
 * @param [type] $permalink
 * @return void
 */
function gtm_add_ads_demo_mode_link( $permalink ) {
	return add_query_arg( 'ads', '1', $permalink );
}

function gtm_get_ad_preset( $width, $height ) {
	$slug          = "{$width}x{$height}";
	$saved_presets = get_option( 'gtm_asd_presets', [] );

	return gtm_find_array_item( $saved_presets, 'name', $slug );
}

function gtm_convert_adsense_to_amp( $adcode, $width, $height ) {
	$adsense_signature = 'pagead2.googlesyndication.com';
	if ( strpos( $adcode, $adsense_signature ) !== false ) {
		preg_match( '|data-ad-client="(.+)"|i', $adcode, $match_client );
		preg_match( '|data-ad-slot="(.+)"|i', $adcode, $match_slot );

		if ( ! empty( $match_client ) && ! empty( $match_slot ) ) {
			$client = $match_client[1];
			$slot   = $match_slot[1];
			$width  = $width ?: '100vw';
			$height = $height ?: '320';

			$amp = <<<AMP
<amp-ad width="{$width}" height="{$height}"
  type="adsense"
  data-ad-client="{$client}"
  data-ad-slot="{$slot}"
  data-auto-format="rspv"
  data-full-width>
    <div overflow></div>
</amp-ad>
AMP;
			gtm_use_amp_script( 'amp-ad' );
			return $amp;
		}
	}

	// Can't convert
	return false;
}