Skip to content

Commit d3fe0a7

Browse files
committed
Switched to using a more concise file_put_contents()
1 parent 320caac commit d3fe0a7

File tree

2 files changed

+3
-27
lines changed

2 files changed

+3
-27
lines changed

modules/pdf-ninja.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -818,15 +818,7 @@ public function api_image( $destfile, $attachment_id, $page )
818818
if( ! isset( $result['content'] ) || ! isset( $result['content_type'] ) || $result['content_type'] != 'image/jpeg' )
819819
throw new Exception( __( "Pdf.Ninja API server did not send an expected response", 'pdf-forms-for-contact-form-7' ) );
820820

821-
$handle = @fopen( $destfile, 'w' );
822-
823-
if( ! $handle )
824-
throw new Exception( __( "Failed to open file for writing", 'pdf-forms-for-contact-form-7' ) );
825-
826-
fwrite( $handle, $result['content'] );
827-
fclose( $handle );
828-
829-
if( ! file_exists( $destfile ) )
821+
if( file_put_contents( $destfile, $result['content'] ) === false || ! is_file( $destfile ) )
830822
throw new Exception( __( "Failed to create file", 'pdf-forms-for-contact-form-7' ) );
831823

832824
return true;
@@ -932,15 +924,7 @@ private function api_pdf_helper( $destfile, $endpoint, $attachment_id, $data, $e
932924
if( ! isset( $result['content'] ) || ! isset( $result['content_type'] ) || $result['content_type'] != 'application/pdf' )
933925
throw new Exception( __( "Pdf.Ninja API server did not send an expected response", 'pdf-forms-for-contact-form-7' ) );
934926

935-
$handle = @fopen( $destfile, 'w' );
936-
937-
if( ! $handle )
938-
throw new Exception( __( "Failed to open file for writing", 'pdf-forms-for-contact-form-7' ) );
939-
940-
fwrite( $handle, $result['content'] );
941-
fclose( $handle );
942-
943-
if( ! file_exists( $destfile ) )
927+
if( file_put_contents( $destfile, $result['content'] ) === false || ! is_file( $destfile ) )
944928
throw new Exception( __( "Failed to create file", 'pdf-forms-for-contact-form-7' ) );
945929

946930
return true;

pdf-forms-for-contact-form-7.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -820,15 +820,7 @@ private static function download_file( $url, $filepath, &$mimetype = null )
820820

821821
$body = wp_remote_retrieve_body( $response );
822822

823-
$handle = @fopen( $filepath, 'w' );
824-
825-
if( ! $handle )
826-
throw new Exception( __( "Failed to open file for writing", 'pdf-forms-for-contact-form-7' ) );
827-
828-
fwrite( $handle, $body );
829-
fclose( $handle );
830-
831-
if( ! is_file( $filepath ) )
823+
if( file_put_contents( $filepath, $body ) === false || ! is_file( $filepath ) )
832824
throw new Exception( __( "Failed to create file", 'pdf-forms-for-contact-form-7' ) );
833825
}
834826

0 commit comments

Comments
 (0)