File: /home/globfdxw/public_html/wp-content/plugins/wpforms-pdf/src/Helpers.php
<?php
namespace WPFormsPDF;
use WPForms\Helpers\File;
use WPFormsPDF\Access\PDF as AccessPDF;
use WPFormsPDF\Notifications\Process;
/**
* PDF helpers.
*
* @since 1.0.0
*/
class Helpers {
/**
* Detect debug mode.
*
* @since 1.0.0
*
* @return bool
* @noinspection PhpUndefinedConstantInspection
*/
public function is_debug(): bool {
return defined( 'WPFORMS_PDF_DEBUG' ) && WPFORMS_PDF_DEBUG;
}
/**
* Log message.
*
* @since 1.0.0
*
* @param mixed $message Log a message or any data.
* @param string $type Message type.
*/
public function log( $message, string $type = 'log' ): void {
if (
! (
( $this->is_debug() && $type === 'debug' ) || // The `debug` messages are allowed only in WPFORMS_PDF_DEBUG mode.
( in_array( $type, [ 'log', 'error' ], true ) )
)
) {
return;
}
// Output to the error.log only `debug` messages.
if ( $type === 'debug' ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
$message_str = is_array( $message ) || is_object( $message ) ? print_r( $message, true ) : $message;
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( 'WPForms PDF: ' . $message_str );
}
wpforms_log( 'WPForms PDF', $message, [ 'type' => $type === 'debug' ? 'log' : $type ] );
}
/**
* Get uploads directory data.
*
* @since 1.0.0
*
* @return array
*/
public function get_wp_upload_dir(): array {
static $data = null;
if ( $data ) {
return $data;
}
$data = [
'dir' => '',
'url' => '',
];
$upload_dir = wp_upload_dir();
if ( ! empty( $upload_dir['error'] ) ) {
return $data;
}
$basedir = wp_is_stream( $upload_dir['basedir'] ) ? $upload_dir['basedir'] : realpath( $upload_dir['basedir'] );
$data['dir'] = trailingslashit( wp_normalize_path( $basedir ) );
$data['url'] = trailingslashit( $upload_dir['baseurl'] );
return $data;
}
/**
* Determine if the license is valid.
*
* @since 1.2.0
*
* @return bool
*/
public function is_valid_license(): ?bool {
static $is_valid = null;
if ( $is_valid !== null ) {
return $is_valid;
}
// License data.
$license = (array) get_option( 'wpforms_license', [] );
$is_valid = ! empty( wpforms_get_license_key() )
&& ! empty( $license['type'] )
&& empty( $license['is_expired'] )
&& empty( $license['is_disabled'] )
&& empty( $license['is_invalid'] );
return $is_valid;
}
/**
* Get PDF download URL.
*
* @since 1.2.0
*
* @param int $pdf_id PDF ID.
* @param array $pdf PDF data.
* @param array $form_data Form data.
* @param array $fields Entry fields.
* @param int $entry_id Entry ID.
*
* @return string
*/
public static function get_pdf_download_url( int $pdf_id, array $pdf, array $form_data, array $fields, int $entry_id ): string {
// Access restriction enabled, special download link.
if ( ! empty( $pdf['access'] ) ) {
return AccessPDF::generate_pdf_download_url( $form_data['id'], $entry_id, $pdf_id );
}
$file_name = Process::get_pdf_file_name( $pdf, $form_data, $fields, $entry_id );
$file_path = Storage::get_dir( $form_data['id'], $entry_id ) . $file_name;
if ( ! File::exists( $file_path ) || ! File::get_contents( $file_path ) ) {
return '';
}
// No access restriction, direct download link.
return Storage::get_url( $form_data['id'], $entry_id ) . $file_name;
}
}