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/settings/settings.php
<?php

defined( 'GTM_KEY_MODIFIED_THEME_IMAGE_SIZES' ) || define( 'GTM_KEY_MODIFIED_THEME_IMAGE_SIZES', 'gtm_modified_theme_image_sizes' );
defined( 'GTM_KEY_MODIFIED_THEME_FONT_FAMILIES' ) || define( 'GTM_KEY_MODIFIED_THEME_FONT_FAMILIES', 'gtm_modified_theme_font_families' );

/* The below const has the same value as slugValuePrefix in react */
defined( 'GTM_PRESET_SLUG_VALUE_PREFIX' ) || define( 'GTM_PRESET_SLUG_VALUE_PREFIX', 'custom-' );

add_action( 'admin_enqueue_scripts', 'gtm_enqueue_preset_css' );
add_action( 'wp_enqueue_scripts', 'gtm_enqueue_preset_css' );
function gtm_enqueue_preset_css() {
	$should_enqueue = true;

	if ( is_admin() ) {
		$should_enqueue = false;

		$screen = get_current_screen();
		if ( isset( $screen->id ) && in_array( $screen->id, ['site-editor', 'appearance_page_gutenberg-edit-site'] ) ) {
			$should_enqueue = true;
		} else if ( isset( $screen->base ) && $screen->base == 'post' ) {
			$should_enqueue = true;
		}
	}

	if ( $should_enqueue ) {
		// Render css style from theme.json
		$theme_json_style = gtm_render_theme_json_style();
		$css_template     = gtm_css_compileCssTemplate( $theme_json_style );
		wp_add_inline_style( 'gtm-global-style', gtm_css_generateCss( $css_template ) );

		// Render css style from settings page
		$global_style = gtm_render_global_style();
		$css_template = gtm_css_compileCssTemplate( $global_style );
		wp_add_inline_style( 'gtm-global-style', gtm_css_generateCss( $css_template ) );

		// Render custom preset, Must override styles from theme.json if exists.
		$preset       = gtm_get_active_preset();
		$cssTemplate  = gtm_css_presetCssParser( $preset );
		$css_template = gtm_css_compileCssTemplate( $cssTemplate );
		wp_add_inline_style( 'gtm-preset-style', gtm_css_generateCss( $css_template ) );
	}
}

/*
Render css style from theme.json
 */
function gtm_render_theme_json_style() {
	/**
	 * Convert structure of theme support to the same format as preset
	 * for reusing the function gtm_css_presetCssParser
	 */

	$attributes = [];

	/* Convert spacing presets */

	$theme_spacing = get_theme_support( 'gutenmate-spacing' );
	if ( ! empty( $theme_spacing ) ) {
		$attributes['spacing'] = $theme_spacing[0];
	}

	/* Convert typography presets */

	$theme_typography = get_theme_support( 'gutenmate-typography' );
	if ( ! empty( $theme_typography ) ) {
		foreach ( $theme_typography[0] as $typo ) {
			if ( $typo ) {
				$attributes['typography'][] = gtm_keys_to_camel( $typo );
			}
		}
	}

	/* Generate css */

	$css = gtm_css_presetCssParser( $attributes );

	/* Convert font families presets */

	$root = [];

	$theme_font_families = get_theme_support( 'gutenmate-font-families' );
	if ( ! empty( $theme_font_families ) ) {
		$override_font_families = get_theme_mod( GTM_KEY_MODIFIED_THEME_FONT_FAMILIES, [] );

		foreach ( $theme_font_families[0] as $family ) {
			if ( $family ) {

				// Use overriden value if exists
				foreach ( $override_font_families as $override_font_family ) {
					if ( $override_font_family['slug'] == $family['slug'] ) {
						$family = $override_font_family;
					}
				}

				$inlineImport = gtm_css_getImportGoogleFont( $family );
				$root += $inlineImport;
				$root["--gtm--font-family--{$family['slug']}"] = '"' . $family['fontFamily'] . '"';
			}
		}
	}

	/* Convert layout sizes */
	/* It will be removed when this PR merged https://github.com/WordPress/gutenberg/pull/30081 */
	$default_layout = wp_get_global_settings( ['layout'] );
	if ( ! empty( $default_layout ) ) {
		foreach ( $default_layout as $layoutName => $size ) {
			$name = _wp_to_kebab_case( $layoutName );

			// Allow only string value, some settings are kept in array which not related for css
			if ( ! is_array( $size ) ) {
				$root["--gtm--layout--{$name}"] = $size;
			}
		}
	}

	$css = array_merge_recursive( $css, ["body" => $root] );
	return $css;
}

/*
Render css style from settings page
 */
function gtm_render_global_style() {
	$css  = [];
	$root = [];

	/* Font stacks */

	$font_stacks = get_option( 'gtm_typography_font_stacks', '' );

	if ( ! empty( $font_stacks ) ) {
		$root += gtm_css_cssProps( "--gtm-global--typography-font-stacks", $font_stacks ?? '' );
	}

	$css = array_merge_recursive( $css, ["body" => $root] );
	return $css;
}

add_action( 'rest_api_init', 'gtm_setup_rest_settings' );
function gtm_setup_rest_settings() {
	register_rest_route(
		'gutenmate/v1',
		'/settings/',
		[
			'schema' => 'gtm_settings_schema',
			[
				'methods'             => 'GET',
				'callback'            => 'gtm_rest_get_settings',
				'permission_callback' => function () {
					return current_user_can( 'edit_posts' );
				},
			],
			[
				'methods'             => 'POST',
				'callback'            => 'gtm_rest_save_settings',
				'permission_callback' => function () {
					return current_user_can( 'edit_posts' );
				},
			],
		]
	);
}

function gtm_settings_schema() {
	return [
		'$schema'              => 'http://json-schema.org/draft-04/schema#',
		'title'                => 'gutenmate-settings',
		'type'                 => 'object',
		'default'              => [],
		'properties'           => [
			'presets'                       => gtm_setting_schema_presets(),
			'typography_font_stacks'        => [
				'type'    => 'string',
				'default' => '',
			],
			'typography_enable_local_fonts' => [
				'type'    => 'boolean',
				'default' => false,
			],
			'theme_font_families'           => [
				'type'  => 'array',
				'items' => [
					'type'       => 'object',
					'properties' => [
						'slug'              => ["type" => 'string'],
						'fontFamily'        => ["type" => 'string'],
						'preloadFontWeight' => ["type" => 'string'],
					],
				],
			],
			'theme_original_image_sizes'    => [
				'type'  => 'array',
				'items' => [
					'type'       => 'object',
					'properties' => [
						'enable' => ["type" => 'boolean'],
						'name'   => ["type" => 'string'],
						'width'  => ["type" => 'number'],
						'height' => ["type" => 'number'],
						'crop'   => ["type" => 'boolean'],
						'cropX'  => ["type" => 'string'],
						'cropY'  => ["type" => 'string'],
					],
				],
			],
			'theme_image_sizes'             => [
				'type'  => 'array',
				'items' => [
					'type'       => 'object',
					'properties' => [
						'enable' => ["type" => 'boolean'],
						'name'   => ["type" => 'string'],
						'width'  => ["type" => 'number'],
						'height' => ["type" => 'number'],
						'crop'   => ["type" => 'boolean'],
						'cropX'  => ["type" => 'string'],
						'cropY'  => ["type" => 'string'],
					],
				],
			],
			'custom_image_sizes'            => [
				'type'  => 'array',
				'items' => [
					'type'       => 'object',
					'properties' => [
						'enable' => ["type" => 'boolean'],
						'name'   => ["type" => 'string'],
						'width'  => ["type" => 'number'],
						'height' => ["type" => 'number'],
						'crop'   => ["type" => 'boolean'],
						'cropX'  => ["type" => 'string'],
						'cropY'  => ["type" => 'string'],
					],
				],
			],
			'share_twitter_username'        => [
				'type'    => 'string',
				'default' => '',
			],
			'share_enable_short_link'       => [
				'type'    => 'boolean',
				'default' => false,
			],
			'post_views_forgery'            => [
				'type'    => 'integer',
				'default' => 0,
			],
			'post_format_icons'             => [
				'type'       => 'object',
				'properties' => [
					'standard' => ["type" => 'string'],
					'aside'    => ["type" => 'string'],
					'audio'    => ["type" => 'string'],
					'chat'     => ["type" => 'string'],
					'gallery'  => ["type" => 'string'],
					'image'    => ["type" => 'string'],
					'link'     => ["type" => 'string'],
					'quote'    => ["type" => 'string'],
					'status'   => ["type" => 'string'],
					'video'    => ["type" => 'string'],
				],
			],
			'asd_enable_detector'           => [
				'type'    => 'boolean',
				'default' => GTM_DEFAULT_ASD_ENABLE_DETECTOR,
			],
			'asd_presets'                   => [
				'type'  => 'array',
				'items' => [
					'type'       => 'object',
					'properties' => [
						'enable' => ["type" => 'boolean'],
						'name'   => ["type" => 'string'],
						'code'   => ["type" => 'string'],
					],
				],
			],
			'seo_enable_title_meta'         => [
				'type'    => 'boolean',
				'default' => false,
			],
			'seo_enable_facebook_meta'      => [
				'type'    => 'boolean',
				'default' => false,
			],
			'seo_enable_twitter_meta'       => [
				'type'    => 'boolean',
				'default' => false,
			],
			'seo_enable_post_rich_snippets' => [
				'type'    => 'boolean',
				'default' => false,
			],
			'icon_sets'                     => [
				'type'    => 'array',
				'items'   => [
					'type' => 'string',
				],
				'default' => ['gtm-basic-icons'],
			],
			'amp_enable_templates'          => [
				'type'    => 'boolean',
				'default' => true,
			],
			'amp_enable_compress_gtm_css'   => [
				'type'    => 'boolean',
				'default' => false,
			],
			'custom_scripts'                => [
				'type'  => 'array',
				'items' => [
					'type'       => 'object',
					'properties' => [
						'enable' => ["type" => 'boolean'],
						'code'   => ["type" => 'string'],
					],
				],
			],
			'custom_styles'                 => [
				'type'  => 'array',
				'items' => [
					'type'       => 'object',
					'properties' => [
						'enable' => ["type" => 'boolean'],
						'code'   => ["type" => 'string'],
					],
				],
			],
		],
		'additionalProperties' => false,
	];
}

function gtm_setting_schema_presets() {
	$responsiveprop = [
		"type"       => 'object',
		'properties' => [
			'lg' => ["type" => ['string', 'number']],
			'md' => ["type" => ['string', 'number']],
			'sm' => ["type" => ['string', 'number']],
		],
	];

	return [
		'type'  => 'array',
		'items' => [
			'type'       => 'object',
			'properties' => [
				'slug'       => ["type" => 'string'],
				'name'       => ["type" => 'string'],
				'removable'  => ["type" => 'boolean'],
				'default'    => ["type" => 'boolean'],
				'fontSizes'  => [
					"type"    => 'array',
					'items'   => [
						'type'       => 'object',
						'properties' => [
							'name' => ["type" => 'string'],
							'slug' => ["type" => 'string'],
							'size' => ["type" => 'string'],
						],
					],
					'default' => [],
				],
				'typography' => [
					"type"  => 'array',
					'items' => [
						"type"       => 'object',
						'properties' => [
							'name'             => ["type" => 'string'],
							'slug'             => ["type" => 'string'],
							'fontFamily'       => [
								"type" => 'string',
							],
							'fontWeight'       => [
								"type" => 'string',
							],
							'fontWeightBold'   => [
								"type" => 'string',
							],
							'fontWeightItalic' => [
								"type" => 'string',
							],
							'fontSize'         => $responsiveprop,
							'lineHeight'       => $responsiveprop,
							'letterSpacing'    => $responsiveprop,
							'textTransform'    => [
								"type" => 'string',
							],
						],
					],
				],
				'spacing'    => [
					"type"    => 'array',
					'items'   => [
						'type'       => 'object',
						'properties' => [
							'name' => ["type" => 'string'],
							'slug' => ["type" => 'string'],
							'size' => ["type" => 'string'],
						],
					],
					'default' => [],
				],
			],
		],
	];
}

function gtm_rest_get_settings( WP_REST_Request $req ) {
	$data = [
		'presets'                       => get_option( 'gtm_presets', [] ),
		'typography_font_stacks'        => get_option( 'gtm_typography_font_stacks', '' ),
		'typography_enable_local_fonts' => (bool) get_option( 'gtm_typography_enable_local_fonts', false ),
		'custom_image_sizes'            => get_option( 'gtm_custom_image_sizes', [] ),
		'share_twitter_username'        => get_option( 'gtm_share_twitter_username', '' ),
		'share_enable_short_link'       => (bool) get_option( 'gtm_share_enable_short_link', false ),
		'post_views_forgery'            => get_option( 'gtm_post_views_forgery', 0 ),
		'post_format_icons'             => get_option( 'gtm_post_format_icons', [] ),
		'asd_enable_detector'           => (bool) get_option( 'gtm_asd_enable_detector', GTM_DEFAULT_ASD_ENABLE_DETECTOR ),
		'asd_presets'                   => get_option( 'gtm_asd_presets', [] ),
		'seo_enable_title_meta'         => (bool) get_option( 'gtm_seo_enable_title_meta', true ),
		'seo_enable_facebook_meta'      => (bool) get_option( 'gtm_seo_enable_facebook_meta', true ),
		'seo_enable_twitter_meta'       => (bool) get_option( 'gtm_seo_enable_twitter_meta', true ),
		'seo_enable_post_rich_snippets' => (bool) get_option( 'gtm_seo_enable_post_rich_snippets', true ),
		'icon_sets'                     => GTM_Icon_Set::get_active_list(),
		'amp_enable_templates'          => (bool) get_option( 'gtm_amp_enable_templates', true ),
		'amp_enable_compress_gtm_css'   => (bool) get_option( 'gtm_amp_enable_compress_gtm_css', false ),
		'custom_scripts'                => get_option( 'gtm_custom_scripts', [] ),
		'custom_styles'                 => get_option( 'gtm_custom_styles', [] ),
	];

	/* Add theme image sizes */
	$theme_original_image_sizes = get_theme_support( 'gutenmate-image-sizes' );
	if ( ! empty( $theme_original_image_sizes ) ) {
		$data['theme_original_image_sizes'] = $theme_original_image_sizes[0];
		$data['theme_image_sizes']          = get_theme_mod( GTM_KEY_MODIFIED_THEME_IMAGE_SIZES, [] );
	}

	/* Add theme font families */
	$data['theme_font_families'] = get_theme_mod( GTM_KEY_MODIFIED_THEME_FONT_FAMILIES, [] );

	try {
		return rest_ensure_response( [
			'success' => true,
			'message' => esc_html__( 'Gutenmate settings has been fetched.', 'gutenmate' ),
			'data'    => $data,
		] );

	} catch ( Exception $e ) {
		return rest_ensure_response( [
			'success' => false,
			'message' => $e->getMessage(),
		] );
	}
}

function gtm_rest_save_settings( WP_REST_Request $req ) {
	try {
		$params   = $req->get_json_params();
		$validate = rest_validate_value_from_schema( $params, gtm_settings_schema() );

		if ( is_wp_error( $validate ) ) {
			return new WP_Error(
				$validate->get_error_code(),
				$validate->get_error_message(),
				['status' => 500]
			);
		}

		$direct_save_options = [
			'presets',
			'typography_font_stacks',
			'typography_enable_local_fonts',
			'custom_image_sizes',
			'share_twitter_username',
			'share_enable_short_link',
			'post_views_forgery',
			'post_format_icons',
			'asd_enable_detector',
			'asd_presets',
			'seo_enable_title_meta',
			'seo_enable_facebook_meta',
			'seo_enable_twitter_meta',
			'seo_enable_post_rich_snippets',
			'amp_enable_templates',
			'amp_enable_compress_gtm_css',
			'custom_scripts',
			'custom_styles',
		];

		foreach ( $direct_save_options as $option ) {
			if ( isset( $params[$option] ) ) {
				gtm_update_option( 'gtm_' . $option, $params[$option] );
			}
		}

		// Save theme image sizes
		if ( empty( $params['theme_image_sizes'] ) ) {
			remove_theme_mod( GTM_KEY_MODIFIED_THEME_IMAGE_SIZES );
		} else {
			set_theme_mod( GTM_KEY_MODIFIED_THEME_IMAGE_SIZES, $params['theme_image_sizes'] );
		}

		// Save theme font families
		if ( empty( $params['theme_font_families'] ) ) {
			remove_theme_mod( GTM_KEY_MODIFIED_THEME_FONT_FAMILIES );
		} else {
			set_theme_mod( GTM_KEY_MODIFIED_THEME_FONT_FAMILIES, $params['theme_font_families'] );
		}

		// Save icon sets
		if ( isset( $params['icon_sets'] ) ) {
			GTM_Icon_Set::save_setting( $params['icon_sets'] );
		}

		do_action( 'gtm_update_settings', $params );

		return rest_ensure_response( [
			'success' => true,
			'message' => esc_html__( 'Gutenmate settings has been updated.', 'gutenmate' ),
			'data'    => $req->get_params(),
		] );

	} catch ( Exception $e ) {
		return rest_ensure_response( [
			'success' => false,
			'message' => $e->getMessage(),
		] );
	}
}

function gtm_get_active_preset() {
	$prefer_preset  = false;
	$default_preset = false;

	if ( is_singular() ) {
		$prefer_preset = get_post_meta( get_queried_object_id(), 'gtm_active_preset', true );
	}

	$presets = get_option( 'gtm_presets', gtm_get_restore_default_preset() );

	foreach ( $presets as $preset ) {
		if ( $prefer_preset === $preset['slug'] ) {
			return $preset;

		} else if ( ! empty( $preset['default'] ) ) {
			$default_preset = $preset;
		}
	}

	return $default_preset;
}