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/lib/custom-image-sizes.php
<?php

/*//////////////////////////////////////
// Register custom image sizes
//////////////////////////////////////*/

add_action( 'init', 'gtm_register_image_sizes', 10 );
function gtm_register_image_sizes() {
	/* Theme image sizes */
	$sizes = get_theme_mod( GTM_KEY_MODIFIED_THEME_IMAGE_SIZES, [] );

	// Load original sizes if no modified sizes
	if ( empty( $sizes ) ) {
		$support = get_theme_support( 'gutenmate-image-sizes' );
		if ( ! empty( $support ) ) {
			$sizes = $support[0];
		}
	}

	if ( ! empty( $sizes ) ) {
		foreach ( $sizes as $i => $size ) {
			gtm_register_image_size( $size );
		}
	}

	/* Custom image sizes */
	$custom_sizes = apply_filters( 'gtm_register_custom_image_sizes', get_option( 'gtm_custom_image_sizes', apply_filters( 'gtm_default_image_sizes', [] ) ) );

	if ( ! empty( $custom_sizes ) ) {
		foreach ( $custom_sizes as $i => $size ) {
			gtm_register_image_size( $size, $i );
		}
	}

	add_filter( 'image_size_names_choose', function ( $image_size_names ) use ( $sizes, $custom_sizes ) {
		// Add image sizes from theme
		if ( ! empty( $sizes ) ) {
			foreach ( $sizes as $size ) {
				$name                    = sanitize_title( $size['name'] );
				$image_size_names[$name] = $size['name'];
			}
		}

		// Add image sizes from custom size
		if ( ! empty( $custom_sizes ) ) {
			foreach ( $custom_sizes as $i => $size ) {
				$name                    = empty( $size['name'] ) ? sanitize_title( 'Custom Image Size ' . $i ) : sanitize_title( $size['name'] );
				$image_size_names[$name] = $size['name'];
			}
		}

		return $image_size_names;
	} );

}

function gtm_register_image_size( $size, $i = 1 ) {
	$size = wp_parse_args( $size, [
		'enable' => true,
		'name'   => '',
		'width'  => 0,
		'height' => 0,
		'crop'   => true,
		'cropX'  => 'center',
		'cropY'  => 'center',
	] );

	if ( ! empty( $size['enable'] ) ) {
		// Sanitize title
		$name = sanitize_title( $size['name'] );
		if ( empty( $name ) ) {
			$name = sanitize_title( 'Custom Image Size ' . $i ); // No need translation, Just to prevent function calling error.
		}

		// Crop
		$crop = $size['crop'] ? [$size['cropX'], $size['cropY']] : false;

		// Register
		add_image_size( $name, intval( $size['width'] ), intval( $size['height'] ), $crop );
	}
}

// add_filter( 'image_size_names_choose', 'gtm_register_image_size_names_choose' );
// function gtm_register_image_size_names_choose( $image_size_names ) {
// 	$sizes = apply_filters( 'gtm_register_custom_image_sizes', get_option( 'gtm_custom_image_sizes', apply_filters( 'gtm_default_image_sizes', [] ) ) );

// 	if ( ! empty( $sizes ) ) {
// 		foreach ( $sizes as $size ) {
// 			$name                    = sanitize_title( $size['name'] );
// 			$image_size_names[$name] = $size['name'];
// 		}
// 	}

// 	return $image_size_names;
// }