Files
WP-LIT-MCP/tests/Fixtures/divi/reference/font-install.txt
T

149 lines
3.8 KiB
Plaintext

function install_to_custom_fonts( string $chat_id, string $filename, string $font_name = '' ) {
if ( ! extension_loaded( 'fileinfo' ) ) {
return new WP_Error(
'fileinfo_extension_not_loaded',
esc_html__( 'Font install requires the fileinfo PHP extension.', 'et_builder_5' ),
[ 'status' => 400 ]
);
}
if ( ! self::_validate_chat_id( $chat_id ) ) {
return new WP_Error(
'invalid_chat_id',
esc_html__( 'Invalid chat identifier.', 'et_builder_5' ),
[ 'status' => 400 ]
);
}
$ownership = self::assert_chat_ownership( $chat_id );
if ( is_wp_error( $ownership ) ) {
return $ownership;
}
if ( false !== strpos( $filename, '/' ) || false !== strpos( $filename, '\\' ) ) {
return new WP_Error(
'invalid_url',
esc_html__( 'Could not parse chat ID and filename from URL.', 'et_builder_5' ),
[ 'status' => 400 ]
);
}
$filename = sanitize_file_name( $filename );
$base_dir = realpath( self::_get_base_dir() );
if ( false === $base_dir || '' === $filename ) {
return new WP_Error(
'invalid_file',
esc_html__( 'Invalid file reference.', 'et_builder_5' ),
[ 'status' => 400 ]
);
}
$source_path = self::_get_base_dir() . '/' . $chat_id . '/' . $filename;
$resolved = realpath( $source_path );
if ( false === $resolved || ! str_starts_with( $resolved, $base_dir ) || ! file_exists( $resolved ) ) {
return new WP_Error(
'file_not_found',
esc_html__( 'Attachment file not found.', 'et_builder_5' ),
[ 'status' => 404 ]
);
}
$file_size = filesize( $resolved );
if ( false === $file_size || self::MAX_FILE_SIZE < $file_size ) {
return new WP_Error(
'file_too_large',
esc_html__( 'Font must be under 10MB.', 'et_builder_5' ),
[ 'status' => 400 ]
);
}
$ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
$supported = self::_get_supported_font_formats();
if ( '' === $ext || ! in_array( $ext, $supported, true ) ) {
return new WP_Error(
'invalid_font_format',
esc_html__( 'This font format is not supported.', 'et_builder_5' ),
[ 'status' => 400 ]
);
}
$checked = HooksRegistration::check_filetype_and_ext_font(
[
'ext' => false,
'type' => false,
'proper_filename' => false,
],
$resolved,
$filename
);
if ( empty( $checked['ext'] ) || empty( $checked['type'] ) ) {
return new WP_Error(
'invalid_mime',
esc_html__( 'Only supported font files can be installed as custom fonts.', 'et_builder_5' ),
[ 'status' => 400 ]
);
}
if ( ! in_array( strtolower( (string) $checked['ext'] ), $supported, true ) ) {
return new WP_Error(
'invalid_font_format',
esc_html__( 'This font format is not supported.', 'et_builder_5' ),
[ 'status' => 400 ]
);
}
$temp = wp_tempnam( $filename );
if ( false === $temp ) {
return new WP_Error(
'temp_file_error',
esc_html__( 'Failed to prepare font file for install.', 'et_builder_5' ),
[ 'status' => 500 ]
);
}
if ( ! copy( $resolved, $temp ) ) {
wp_delete_file( $temp );
return new WP_Error(
'copy_failed',
esc_html__( 'Failed to prepare font file for install.', 'et_builder_5' ),
[ 'status' => 500 ]
);
}
$resolved_font_name = '' !== $font_name ? $font_name : pathinfo( $filename, PATHINFO_FILENAME );
$result = FontsUtility::font_add(
[
$checked['ext'] => [
'name' => $filename,
'type' => $checked['type'],
'tmp_name' => $temp,
'error' => 0,
'size' => filesize( $temp ),
],
],
$resolved_font_name,
[
'font_weights' => 'all',
'generic_family' => 'sans-serif',
],
[
'action' => 'wp_handle_upload_rest',
]
);
if ( file_exists( $temp ) ) {
wp_delete_file( $temp );
}
return $result;
}