@@ -52,6 +52,34 @@ class Redux_Extension_Custom_Fonts extends Redux_Extension_Abstract {
52
52
*/
53
53
public $ upload_url = '' ;
54
54
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
+
55
83
/**
56
84
* Extension instance.
57
85
*
@@ -306,9 +334,16 @@ public function ajax() {
306
334
307
335
try {
308
336
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
+ }
312
347
313
348
echo wp_json_encode ( $ result );
314
349
}
@@ -332,9 +367,12 @@ public function ajax() {
332
367
$ _POST ['filename ' ] = '' ;
333
368
}
334
369
370
+ $ this ->font_folder = sanitize_file_name ( wp_unslash ( $ _POST ['title ' ] ) );
371
+ $ this ->font_filename = sanitize_file_name ( wp_unslash ( $ _POST ['filename ' ] ) );
372
+
335
373
if ( ! empty ( $ _POST ['attachment_id ' ] ) ) {
336
374
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 ' ] ) ) );
338
376
339
377
if ( empty ( $ msg ) ) {
340
378
$ msg = '' ;
@@ -388,11 +426,9 @@ public function get_valid_files( string $path ): array {
388
426
* Take a valid web font and process the missing pieces.
389
427
*
390
428
* @param string $attachment_id ID.
391
- * @param string $name Name.
392
- * @param string $true_filename Filename.
393
429
* @param string $mime_type Mine type.
394
430
*/
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 ) {
396
432
// phpcs:ignore WordPress.Security.NonceVerification
397
433
if ( ! isset ( $ _POST ['conversion ' ] ) ) {
398
434
$ _POST ['conversion ' ] = 'false ' ;
@@ -412,22 +448,21 @@ public function process_web_font( string $attachment_id, string $name, string $t
412
448
'otf ' ,
413
449
);
414
450
415
- $ subfolder = 'custom/ ' ;
416
- $ subtype = explode ( '/ ' , $ mime_type );
417
- $ subtype = trim ( max ( $ subtype ) );
451
+ $ subtype = explode ( '/ ' , $ mime_type );
452
+ $ subtype = trim ( max ( $ subtype ) );
418
453
419
454
if ( ! is_dir ( $ this ->upload_dir ) ) {
420
455
$ this ->parent ->filesystem ->execute ( 'mkdir ' , $ this ->upload_dir );
421
456
}
422
457
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 );
425
460
}
426
461
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 );
429
464
430
- if ( empty ( $ path ) ) {
465
+ if ( empty ( $ this -> selected_file ) ) {
431
466
echo wp_json_encode (
432
467
array (
433
468
'type ' => 'error ' ,
@@ -438,7 +473,7 @@ public function process_web_font( string $attachment_id, string $name, string $t
438
473
die ();
439
474
}
440
475
441
- $ filename = explode ( '/ ' , $ path );
476
+ $ filename = explode ( '/ ' , $ this -> selected_file );
442
477
443
478
$ filename = $ filename [ ( count ( $ filename ) - 1 ) ];
444
479
@@ -458,8 +493,8 @@ public function process_web_font( string $attachment_id, string $name, string $t
458
493
)
459
494
);
460
495
461
- if ( empty ( $ name ) ) {
462
- $ name = $ fontname ;
496
+ if ( empty ( $ this -> font_folder ) ) {
497
+ $ this -> font_folder = $ fontname ;
463
498
}
464
499
465
500
$ ret = array ();
@@ -469,7 +504,7 @@ public function process_web_font( string $attachment_id, string $name, string $t
469
504
}
470
505
471
506
if ( 'zip ' === $ subtype ) {
472
- $ unzipfile = unzip_file ( $ path , $ temp );
507
+ $ unzipfile = unzip_file ( $ this -> selected_file , $ temp );
473
508
474
509
if ( is_wp_error ( $ unzipfile ) ) {
475
510
echo wp_json_encode (
@@ -491,13 +526,13 @@ public function process_web_font( string $attachment_id, string $name, string $t
491
526
}
492
527
}
493
528
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 . '/ ' );
496
531
}
497
532
498
533
foreach ( $ output as $ key => $ value ) {
499
534
$ param_array = array (
500
- 'destination ' => $ this ->upload_dir . $ subfolder . $ name . '/ ' . $ fontname . '. ' . $ key ,
535
+ 'destination ' => $ this ->upload_dir . $ this -> subfolder . $ this -> font_folder . '/ ' . $ fontname . '. ' . $ key ,
501
536
'overwrite ' => true ,
502
537
'chmod ' => 755 ,
503
538
);
@@ -506,7 +541,7 @@ public function process_web_font( string $attachment_id, string $name, string $t
506
541
}
507
542
508
543
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 );
510
545
}
511
546
}
512
547
@@ -524,23 +559,36 @@ public function process_web_font( string $attachment_id, string $name, string $t
524
559
}
525
560
}
526
561
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 . '/ ' );
529
564
}
530
565
531
- $ output = array ( $ subtype => $ path );
566
+ $ output = array ( $ subtype => $ this -> selected_file );
532
567
533
- // TODO: COnversion error not moving single file.
534
568
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
+ );
542
590
543
- $ this -> parent -> filesystem -> execute ( ' copy ' , $ path , $ param_array ) ;
591
+ die ;
544
592
}
545
593
546
594
$ 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
572
620
return '' ;
573
621
}
574
622
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
+
575
638
/**
576
639
* Ping the WebFontOMatic API to get the missing files.
577
640
*
578
- * @param string $name Name.
579
641
* @param string $fontname Font name.
580
642
* @param array $missing Missing.
581
643
* @param array $output Output.
582
- * @param string $subfolder Folder.
583
- * @param string $true_filename Font name with extension.
584
644
*/
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 ) ) {
587
647
$ temp = $ this ->upload_dir . 'temp ' ;
588
648
589
- $ font_ext = pathinfo ( $ true_filename , PATHINFO_EXTENSION );
649
+ $ font_ext = pathinfo ( $ this -> font_filename , PATHINFO_EXTENSION );
590
650
591
651
$ unsupported = array ( 'eot ' , 'woff ' , 'woff2 ' );
592
652
@@ -609,12 +669,12 @@ private function get_missing_files( string $name, string $fontname, array $missi
609
669
);
610
670
611
671
$ 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 ) );
613
673
614
674
die ();
615
675
}
616
676
617
- update_option ( 'redux_custom_font_current ' , $ name . '.zip ' );
677
+ update_option ( 'redux_custom_font_current ' , $ this -> font_folder . '.zip ' );
618
678
619
679
$ boundary = wp_generate_password ( 24 );
620
680
@@ -670,7 +730,11 @@ private function get_missing_files( string $name, string $fontname, array $missi
670
730
671
731
$ zip_file = $ temp . DIRECTORY_SEPARATOR . $ fontname . '.zip ' ;
672
732
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
+ }
674
738
675
739
$ zip = unzip_file ( $ zip_file , $ temp );
676
740
@@ -684,18 +748,18 @@ private function get_missing_files( string $name, string $fontname, array $missi
684
748
685
749
foreach ( $ files as $ file ) {
686
750
$ 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 ' ] ) ,
688
752
'overwrite ' => true ,
689
753
'chmod ' => 755 ,
690
754
);
691
755
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 );
693
757
}
694
758
} else {
695
759
$ path_parts = pathinfo ( $ output [ $ main ] );
696
760
697
761
$ 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 ' ] ) ,
699
763
'overwrite ' => true ,
700
764
'chmod ' => 755 ,
701
765
);
@@ -719,7 +783,7 @@ private function get_missing_files( string $name, string $fontname, array $missi
719
783
delete_option ( 'redux_custom_font_current ' );
720
784
}
721
785
722
- return '' ;
786
+ return true ;
723
787
}
724
788
725
789
/**
0 commit comments