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-supports/block-visibility.php
<?php
defined( 'GTM_BLOCK_SUPPORT_VISIBILITY_KEY' ) || define( 'GTM_BLOCK_SUPPORT_VISIBILITY_KEY', 'gtmBlockVisibility' );

/**
 * Registers the align block attribute for block types that support it.
 *
 * @param WP_Block_Type $block_type Block Type.
 */
function gtm_register_block_visibility_support( $block_type ) {
	$support = gtm_get_block_support( $block_type, [GTM_BLOCK_SUPPORT_VISIBILITY_KEY], false );
	if ( $support ) {
		add_filter( "gtm.BlockStyleCompiler.{$block_type->name}", 'gtm_compile_css_block_visibility', 10, 3 );

		if ( ! $block_type->attributes ) {
			$block_type->attributes = [];
		}

		if ( ! array_key_exists( 'gtmHideDesktop', $block_type->attributes ) ) {
			$block_type->attributes['gtmHideDesktop'] = [
				'type'    => 'boolean',
				'default' => false,
			];
		}

		if ( ! array_key_exists( 'gtmHideTablet', $block_type->attributes ) ) {
			$block_type->attributes['gtmHideTablet'] = [
				'type'    => 'boolean',
				'default' => false,
			];
		}

		if ( ! array_key_exists( 'gtmHidePhone', $block_type->attributes ) ) {
			$block_type->attributes['gtmHidePhone'] = [
				'type'    => 'boolean',
				'default' => false,
			];
		}

		if ( ! empty( $support['hideEmptyInnerBlock'] ) && ! array_key_exists( 'gtmHideEmptyInnerBlock', $block_type->attributes ) ) {
			$block_type->attributes['gtmHideEmptyInnerBlock'] = [
				'type'    => 'boolean',
				'default' => false,
			];
		}

		if ( ! array_key_exists( 'gtmDisplayConditionBehavior', $block_type->attributes ) ) {
			$block_type->attributes['gtmDisplayConditionBehavior'] = [
				'type'    => 'string',
				'default' => 'off',
			];
		}

		if ( ! array_key_exists( 'gtmDisplayConditions', $block_type->attributes ) ) {
			$block_type->attributes['gtmDisplayConditions'] = [
				'type'    => 'array',
				'default' => [],
			];
		}
	}
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
	GTM_BLOCK_SUPPORT_VISIBILITY_KEY,
	[
		'register_attribute' => 'gtm_register_block_visibility_support',
	]
);

function gtm_compile_css_block_visibility( $output, $attributes, $blockName ) {
	if ( ! empty( $attributes['gtmHideDesktop'] ) ) {
		$output['classes'][] = 'gtm-hide-desktop';
	}

	if ( ! empty( $attributes['gtmHideTablet'] ) ) {
		$output['classes'][] = 'gtm-hide-tablet';
	}

	if ( ! empty( $attributes['gtmHidePhone'] ) ) {
		$output['classes'][] = 'gtm-hide-phone';
	}

	return $output;
}

add_filter( 'render_block', 'gtm_render_block_visibility_display', PHP_INT_MAX, 2 );
/**
 * Render out the block if met the conditions`.
 *
 * @param  string $block_content Rendered block content.
 * @param  array  $block         Block object.
 * @return string                Filtered block content.
 */
function gtm_render_block_visibility_display( $block_content, $block ) {
	if ( isset( $block['attrs']['gtmDisplayConditionBehavior'] ) && $block['attrs']['gtmDisplayConditionBehavior'] !== 'off' ) {
		$met_conditions = gtm_block_visibility_apply_display_conditions( $block['attrs']['gtmDisplayConditions'] ?? [] );

		// Apply condition when has value, it can be null
		// since some conditions can't processed due to incomplete data
		if ( ! is_null( $met_conditions ) ) {
			if ( 'hidden' == $block['attrs']['gtmDisplayConditionBehavior'] ) {
				// Block should hidden if met the conditions
				$should_display = ! $met_conditions;
			} else {
				// Block should display if met the conditions
				$should_display = $met_conditions;
			}

			if ( ! $should_display ) {
				return '';
			}
		}
	}

	if ( ! empty( $block['attrs']['gtmHideEmptyInnerBlock'] ) && gtm_is_inner_block_empty( $block_content ) ) {
		return '';
	}

	return $block_content;
}

function gtm_block_visibility_apply_display_conditions( $conditions ) {
	$result = null;

	if ( is_array( $conditions ) && ! empty( $conditions ) ) {
		foreach ( $conditions as $condition ) {
			$condition_result = null;

			if ( 'screen' === $condition['entity'] ) {
				if ( 'single' === $condition['condition'] ) {
					if ( ! empty( $condition['criteria'] ) ) {
						$condition_result = is_single( $condition['criteria'] );
					} else {
						$condition_result = is_single();
					}
				} else if ( 'singular' === $condition['condition'] ) {
					if ( ! empty( $condition['criteria'] ) ) {
						$condition_result = is_singular( $condition['criteria'] );
					} else {
						$condition_result = is_singular();
					}
				} else if ( 'archive' === $condition['condition'] ) {
					if ( ! empty( $condition['criteria'] ) ) {
						$condition_result = is_post_type_archive( $condition['criteria'] );
					} else {
						$condition_result = is_archive();
					}
				} else if ( 'tax' === $condition['condition'] ) {
					if ( ! empty( $condition['criteria'] ) ) {
						$condition_result = is_tax( $condition['criteria'] );
					} else {
						$condition_result = is_archive();
					}
				} else if ( 'frontpage' === $condition['condition'] ) {
					$condition_result = is_front_page();
				} else if ( 'not-frontpage' === $condition['condition'] ) {
					$condition_result = ! is_front_page();
				}
			} else if ( 'post' === $condition['entity'] ) {
				if ( 'id' === $condition['condition'] ) {
					$post = get_post();

					if ( ! empty( $condition['criteria'] ) && ! empty( $post ) ) {
						$condition_result = $post->ID === $condition['criteria'];
					}
				} else if ( 'type' === $condition['condition'] ) {
					if ( ! empty( $condition['criteria'] ) ) {
						$condition_result = get_post_type() === $condition['criteria'];
					}
				} else if ( 'thumbnail' === $condition['condition'] ) {
					$condition_result = has_post_thumbnail();
				} else if ( 'no-thumbnail' === $condition['condition'] ) {
					$condition_result = ! has_post_thumbnail();
				} else if ( 'format' === $condition['condition'] ) {
					if ( ! empty( $condition['criteria'] ) ) {
						$condition_result = has_post_format( $condition['criteria'] );
					} else {
						$condition_result = has_post_format();
					}
				} else if ( 'no-format' === $condition['condition'] ) {
					$condition_result = ! has_post_format();
				} else if ( 'has-cats' === $condition['condition'] ) {
					$condition_result = has_category();
				} else if ( 'no-cats' === $condition['condition'] ) {
					$condition_result = ! has_category();
				} else if ( 'has-tags' === $condition['condition'] ) {
					$condition_result = has_tag();
				} else if ( 'no-tags' === $condition['condition'] ) {
					$condition_result = ! has_tag();
				} else if ( 'has-comments' === $condition['condition'] ) {
					global $withcomments;
					$has_no_comments  = ! ( is_single() || is_page() || $withcomments ) || empty( $post );
					$condition_result = ! $has_no_comments || comments_open();
				} else if ( 'no-comments' === $condition['condition'] ) {
					global $withcomments;
					$has_no_comments  = ! ( is_single() || is_page() || $withcomments ) || empty( $post );
					$condition_result = ! ( ! $has_no_comments || comments_open() );
				} else if ( 'protected' === $condition['condition'] ) {
					$condition_result = post_password_required();
				} else if ( 'not-protected' === $condition['condition'] ) {
					$condition_result = ! post_password_required();
				} else if ( 'cat' === $condition['condition'] ) {
					$condition_result = in_category( $condition['criteria'] );
				}
			} else if ( 'term' === $condition['entity'] ) {
				if ( 'id' === $condition['condition'] ) {
					if ( ! empty( $condition['criteria'] ) ) {
						$term = get_queried_object();

						if ( $term ) {
							$is_term_archive = isset( $term->term_id );

							$condition_result = $is_term_archive && $term->term_id == $condition['criteria'];
						}
					}
				} else if ( 'subterms' === $condition['condition'] ) {
					$term = get_queried_object();

					if ( $term ) {
						$children        = get_term_children( $term->term_id, $term->taxonomy );
						$is_term_archive = isset( $term->term_id );

						$condition_result = $is_term_archive && ! empty( $children );
					}
				} else if ( 'no-subterms' === $condition['condition'] ) {
					$term = get_queried_object();

					if ( $term ) {
						$children        = get_term_children( $term->term_id, $term->taxonomy );
						$is_term_archive = isset( $term->term_id );

						$condition_result = $is_term_archive && empty( $children );
					}
				}
			} else if ( 'author' === $condition['entity'] ) {
				if ( 'has-bio' === $condition['condition'] ) {
					$bio              = get_the_author_meta( 'description' );
					$condition_result = ! empty( $bio );
				}
			} else if ( 'user' === $condition['entity'] ) {
				if ( 'id' === $condition['condition'] ) {
					if ( ! empty( $condition['criteria'] ) ) {
						$condition_result = wp_get_current_user()->ID === $condition['criteria'];
					}
				} else if ( 'role' === $condition['condition'] ) {
					$condition_result = in_array( $condition['criteria'], wp_get_current_user()->roles );
				} else if ( 'logged-in' === $condition['condition'] ) {
					$condition_result = is_user_logged_in();
				} else if ( 'not-logged-in' === $condition['condition'] ) {
					$condition_result = ! is_user_logged_in();
				}
			} else if ( 'meta' === $condition['entity'] ) {
				$post = get_post();

				if ( ! empty( $condition['entityCriteria'] ) && $post ) {
					$post_meta = get_post_meta( $post->ID, $condition['entityCriteria'], true );

					if ( 'eq' === $condition['condition'] ) {
						$condition_result = $post_meta == $condition['criteria'];
					} else if ( 'ne' === $condition['condition'] ) {
						$condition_result = $post_meta != $condition['criteria'];
					} else if ( 'has-val' === $condition['condition'] ) {
						$condition_result = ! empty( $post_meta );
					} else if ( 'no-val' === $condition['condition'] ) {
						$condition_result = empty( $post_meta );
					}
				}
			} else if ( 'fieldset' === $condition['entity'] ) {
				$post = get_post();

				if ( ! empty( $condition['entityCriteria'] ) && $post ) {
					$has = gtm_has_custom_field_set( $condition['entityCriteria'], $post->ID );

					if ( 'has-val' === $condition['condition'] ) {
						$condition_result = $has;
					} else if ( 'no-val' === $condition['condition'] ) {
						$condition_result = ! $has;
					}
				}
			}

			// Apply condition result
			if ( ! is_null( $condition_result ) ) {
				if ( is_null( $result ) ) {
					$result = $condition_result;
				} else {
					if ( $condition['operator'] == 'or' ) {
						$result = $result || $condition_result;
					} else {
						$result = $result && $condition_result;
					}
				}
			}
		}
	}

	return $result;
}

/**
 * Check the emptyness of inner block content
 * Assume the empty block has only 1 open/close tag
 *
 * @param [type] $block_content
 * @return void
 */
function gtm_is_inner_block_empty( $block_content ) {
	$is_empty = ( empty( trim( $block_content ) ) || substr_count( $block_content, '<' ) <= 2 );

	return $is_empty;
}