Skip to content

Commit a93d530

Browse files
committed
#4008 - Fix conversion failure fallback.
1 parent 6b12775 commit a93d530

File tree

1 file changed

+112
-48
lines changed

1 file changed

+112
-48
lines changed

redux-core/inc/extensions/custom_fonts/class-redux-extension-custom-fonts.php

+112-48
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ class Redux_Extension_Custom_Fonts extends Redux_Extension_Abstract {
5252
*/
5353
public $upload_url = '';
5454

55+
/**
56+
* Subfolder name.
57+
*
58+
* @var string
59+
*/
60+
public $subfolder = 'custom/';
61+
62+
/**
63+
* Font folder.
64+
*
65+
* @var string
66+
*/
67+
public $font_folder = '';
68+
69+
/**
70+
* Font Filename.
71+
*
72+
* @var string
73+
*/
74+
public $font_filename = '';
75+
76+
/**
77+
* File selected in media upload.
78+
*
79+
* @var string
80+
*/
81+
public $selected_file = '';
82+
5583
/**
5684
* Extension instance.
5785
*
@@ -306,9 +334,16 @@ public function ajax() {
306334

307335
try {
308336
if ( isset( $_POST['section'] ) || isset( $_POST['name'] ) ) {
309-
$this->parent->filesystem->execute( 'rmdir', $this->upload_dir . sanitize_title( wp_unslash( $_POST['section'] ) ) . '/' . sanitize_title( wp_unslash( $_POST['name'] ) ) . '/', array( 'recursive' => true ) );
310-
311-
$result = array( 'type' => 'success' );
337+
$ret = $this->parent->filesystem->execute( 'rmdir', $this->upload_dir . sanitize_file_name( wp_unslash( $_POST['section'] ) ) . '/' . sanitize_file_name( wp_unslash( $_POST['name'] ) ) . '/', array( 'recursive' => true ) );
338+
339+
if ( true === $ret ) {
340+
$result = array( 'type' => 'success' );
341+
} else {
342+
$result = array(
343+
'type' => 'error',
344+
'msg' => esc_html__( 'File system failure. Could not delete temp dir.', 'redux-framework' ),
345+
);
346+
}
312347

313348
echo wp_json_encode( $result );
314349
}
@@ -332,9 +367,12 @@ public function ajax() {
332367
$_POST['filename'] = '';
333368
}
334369

370+
$this->font_folder = sanitize_file_name( wp_unslash( $_POST['title'] ) );
371+
$this->font_filename = sanitize_file_name( wp_unslash( $_POST['filename'] ) );
372+
335373
if ( ! empty( $_POST['attachment_id'] ) ) {
336374
if ( isset( $_POST['title'] ) || isset( $_POST['mime'] ) ) {
337-
$msg = $this->process_web_font( sanitize_key( wp_unslash( $_POST['attachment_id'] ) ), sanitize_text_field( wp_unslash( $_POST['title'] ) ), sanitize_text_field( wp_unslash( $_POST['filename'] ) ), sanitize_text_field( wp_unslash( $_POST['mime'] ) ) );
375+
$msg = $this->process_web_font( sanitize_key( wp_unslash( $_POST['attachment_id'] ) ), sanitize_text_field( wp_unslash( $_POST['mime'] ) ) );
338376

339377
if ( empty( $msg ) ) {
340378
$msg = '';
@@ -388,11 +426,9 @@ public function get_valid_files( string $path ): array {
388426
* Take a valid web font and process the missing pieces.
389427
*
390428
* @param string $attachment_id ID.
391-
* @param string $name Name.
392-
* @param string $true_filename Filename.
393429
* @param string $mime_type Mine type.
394430
*/
395-
public function process_web_font( string $attachment_id, string $name, string $true_filename, string $mime_type ) {
431+
public function process_web_font( string $attachment_id, string $mime_type ) {
396432
// phpcs:ignore WordPress.Security.NonceVerification
397433
if ( ! isset( $_POST['conversion'] ) ) {
398434
$_POST['conversion'] = 'false';
@@ -412,22 +448,21 @@ public function process_web_font( string $attachment_id, string $name, string $t
412448
'otf',
413449
);
414450

415-
$subfolder = 'custom/';
416-
$subtype = explode( '/', $mime_type );
417-
$subtype = trim( max( $subtype ) );
451+
$subtype = explode( '/', $mime_type );
452+
$subtype = trim( max( $subtype ) );
418453

419454
if ( ! is_dir( $this->upload_dir ) ) {
420455
$this->parent->filesystem->execute( 'mkdir', $this->upload_dir );
421456
}
422457

423-
if ( ! is_dir( $this->upload_dir . $subfolder ) ) {
424-
$this->parent->filesystem->execute( 'mkdir', $this->upload_dir . $subfolder );
458+
if ( ! is_dir( $this->upload_dir . $this->subfolder ) ) {
459+
$this->parent->filesystem->execute( 'mkdir', $this->upload_dir . $this->subfolder );
425460
}
426461

427-
$temp = $this->upload_dir . 'temp';
428-
$path = get_attached_file( $attachment_id );
462+
$temp = $this->upload_dir . 'temp';
463+
$this->selected_file = get_attached_file( $attachment_id );
429464

430-
if ( empty( $path ) ) {
465+
if ( empty( $this->selected_file ) ) {
431466
echo wp_json_encode(
432467
array(
433468
'type' => 'error',
@@ -438,7 +473,7 @@ public function process_web_font( string $attachment_id, string $name, string $t
438473
die();
439474
}
440475

441-
$filename = explode( '/', $path );
476+
$filename = explode( '/', $this->selected_file );
442477

443478
$filename = $filename[ ( count( $filename ) - 1 ) ];
444479

@@ -458,8 +493,8 @@ public function process_web_font( string $attachment_id, string $name, string $t
458493
)
459494
);
460495

461-
if ( empty( $name ) ) {
462-
$name = $fontname;
496+
if ( empty( $this->font_folder ) ) {
497+
$this->font_folder = $fontname;
463498
}
464499

465500
$ret = array();
@@ -469,7 +504,7 @@ public function process_web_font( string $attachment_id, string $name, string $t
469504
}
470505

471506
if ( 'zip' === $subtype ) {
472-
$unzipfile = unzip_file( $path, $temp );
507+
$unzipfile = unzip_file( $this->selected_file, $temp );
473508

474509
if ( is_wp_error( $unzipfile ) ) {
475510
echo wp_json_encode(
@@ -491,13 +526,13 @@ public function process_web_font( string $attachment_id, string $name, string $t
491526
}
492527
}
493528

494-
if ( ! is_dir( $this->upload_dir . $subfolder . $name . '/' ) ) {
495-
$this->parent->filesystem->execute( 'mkdir', $this->upload_dir . $subfolder . $name . '/' );
529+
if ( ! is_dir( $this->upload_dir . $this->subfolder . $this->font_folder . '/' ) ) {
530+
$this->parent->filesystem->execute( 'mkdir', $this->upload_dir . $this->subfolder . $this->font_folder . '/' );
496531
}
497532

498533
foreach ( $output as $key => $value ) {
499534
$param_array = array(
500-
'destination' => $this->upload_dir . $subfolder . $name . '/' . $fontname . '.' . $key,
535+
'destination' => $this->upload_dir . $this->subfolder . $this->font_folder . '/' . $fontname . '.' . $key,
501536
'overwrite' => true,
502537
'chmod' => 755,
503538
);
@@ -506,7 +541,7 @@ public function process_web_font( string $attachment_id, string $name, string $t
506541
}
507542

508543
if ( true === $this->can_convert && 'true' === $conversion ) {
509-
$ret = $this->get_missing_files( $name, $fontname, $missing, $output, $subfolder, $true_filename );
544+
$ret = $this->get_missing_files( $fontname, $missing, $output );
510545
}
511546
}
512547

@@ -524,23 +559,36 @@ public function process_web_font( string $attachment_id, string $name, string $t
524559
}
525560
}
526561

527-
if ( ! is_dir( $this->upload_dir . $subfolder . $name . '/' ) ) {
528-
$this->parent->filesystem->execute( 'mkdir', $this->upload_dir . $subfolder . $name . '/' );
562+
if ( ! is_dir( $this->upload_dir . $this->subfolder . $this->font_folder . '/' ) ) {
563+
$this->parent->filesystem->execute( 'mkdir', $this->upload_dir . $this->subfolder . $this->font_folder . '/' );
529564
}
530565

531-
$output = array( $subtype => $path );
566+
$output = array( $subtype => $this->selected_file );
532567

533-
// TODO: COnversion error not moving single file.
534568
if ( true === $this->can_convert && 'true' === $conversion ) {
535-
$ret = $this->get_missing_files( $name, $fontname, $missing, $output, $subfolder, $true_filename );
536-
} else {
537-
$param_array = array(
538-
'destination' => $this->upload_dir . $subfolder . '/' . $name . '/' . $true_filename, // $fontname . '.' . $subtype,
539-
'overwrite' => true,
540-
'chmod' => 755,
541-
);
569+
$ret = $this->get_missing_files( $fontname, $missing, $output );
570+
571+
if ( false === $ret ) {
572+
if ( false === $this->convert_local_font() ) {
573+
echo wp_json_encode(
574+
array(
575+
'type' => 'error',
576+
'msg' => esc_html__( 'File permission error. Local file could not be installed.', 'redux-framework' ) . ' ' . $subtype,
577+
)
578+
);
579+
580+
die;
581+
}
582+
}
583+
} elseif ( false === $this->convert_local_font() ) {
584+
echo wp_json_encode(
585+
array(
586+
'type' => 'error',
587+
'msg' => esc_html__( 'File permission error. Local file could not be installed.', 'redux-framework' ) . ' ' . $subtype,
588+
)
589+
);
542590

543-
$this->parent->filesystem->execute( 'copy', $path, $param_array );
591+
die;
544592
}
545593

546594
$this->parent->filesystem->execute( 'rmdir', $temp, array( 'recursive' => true ) );
@@ -572,21 +620,33 @@ public function process_web_font( string $attachment_id, string $name, string $t
572620
return '';
573621
}
574622

623+
/**
624+
* Install selected file into Custom Fonts.
625+
*
626+
* @return bool
627+
*/
628+
private function convert_local_font(): bool {
629+
$param_array = array(
630+
'destination' => $this->upload_dir . $this->subfolder . '/' . $this->font_folder . '/' . $this->font_filename,
631+
'overwrite' => true,
632+
'chmod' => 755,
633+
);
634+
635+
return $this->parent->filesystem->execute( 'copy', $this->selected_file, $param_array );
636+
}
637+
575638
/**
576639
* Ping the WebFontOMatic API to get the missing files.
577640
*
578-
* @param string $name Name.
579641
* @param string $fontname Font name.
580642
* @param array $missing Missing.
581643
* @param array $output Output.
582-
* @param string $subfolder Folder.
583-
* @param string $true_filename Font name with extension.
584644
*/
585-
private function get_missing_files( string $name, string $fontname, array $missing, array $output, string $subfolder, string $true_filename ) {
586-
if ( ! empty( $name ) && ! empty( $missing ) ) {
645+
private function get_missing_files( string $fontname, array $missing, array $output ) {
646+
if ( ! empty( $this->font_folder ) && ! empty( $missing ) ) {
587647
$temp = $this->upload_dir . 'temp';
588648

589-
$font_ext = pathinfo( $true_filename, PATHINFO_EXTENSION );
649+
$font_ext = pathinfo( $this->font_filename, PATHINFO_EXTENSION );
590650

591651
$unsupported = array( 'eot', 'woff', 'woff2' );
592652

@@ -609,12 +669,12 @@ private function get_missing_files( string $name, string $fontname, array $missi
609669
);
610670

611671
$this->parent->filesystem->execute( 'rmdir', $temp, array( 'recursive' => true ) );
612-
$this->parent->filesystem->execute( 'rmdir', $this->upload_dir . $subfolder . $name . '/', array( 'recursive' => true ) );
672+
$this->parent->filesystem->execute( 'rmdir', $this->upload_dir . $this->subfolder . $this->font_folder . '/', array( 'recursive' => true ) );
613673

614674
die();
615675
}
616676

617-
update_option( 'redux_custom_font_current', $name . '.zip' );
677+
update_option( 'redux_custom_font_current', $this->font_folder . '.zip' );
618678

619679
$boundary = wp_generate_password( 24 );
620680

@@ -670,7 +730,11 @@ private function get_missing_files( string $name, string $fontname, array $missi
670730

671731
$zip_file = $temp . DIRECTORY_SEPARATOR . $fontname . '.zip';
672732

673-
$ret = $this->parent->filesystem->execute( 'put_contents', $zip_file, $param_array );
733+
$this->parent->filesystem->execute( 'put_contents', $zip_file, $param_array );
734+
735+
if ( 0 === filesize( $zip_file ) ) {
736+
return false;
737+
}
674738

675739
$zip = unzip_file( $zip_file, $temp );
676740

@@ -684,18 +748,18 @@ private function get_missing_files( string $name, string $fontname, array $missi
684748

685749
foreach ( $files as $file ) {
686750
$param_array = array(
687-
'destination' => $this->upload_dir . $subfolder . $name . DIRECTORY_SEPARATOR . $file['name'],
751+
'destination' => $this->upload_dir . $this->subfolder . $this->font_folder . DIRECTORY_SEPARATOR . sanitize_file_name( $file['name'] ),
688752
'overwrite' => true,
689753
'chmod' => 755,
690754
);
691755

692-
$this->parent->filesystem->execute( 'move', $temp . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR . $file['name'], $param_array );
756+
$this->parent->filesystem->execute( 'move', $temp . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR . sanitize_file_name( $file['name'] ), $param_array );
693757
}
694758
} else {
695759
$path_parts = pathinfo( $output[ $main ] );
696760

697761
$param_array = array(
698-
'destination' => $this->upload_dir . $subfolder . $name . DIRECTORY_SEPARATOR . $path_parts['basename'],
762+
'destination' => $this->upload_dir . $this->subfolder . $this->font_folder . DIRECTORY_SEPARATOR . sanitize_file_name( $path_parts['basename'] ),
699763
'overwrite' => true,
700764
'chmod' => 755,
701765
);
@@ -719,7 +783,7 @@ private function get_missing_files( string $name, string $fontname, array $missi
719783
delete_option( 'redux_custom_font_current' );
720784
}
721785

722-
return '';
786+
return true;
723787
}
724788

725789
/**

0 commit comments

Comments
 (0)