HEX
Server: LiteSpeed
System: Linux server315.web-hosting.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
User: globfdxw (6114)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: /home/globfdxw/www/wp-content/plugins/wpforms-pdf/src/Storage.php
<?php

namespace WPFormsPDF;

use WPForms\Helpers\File;

/**
 * Storage related helpers class.
 *
 * @since 1.0.0
 */
class Storage {

	/**
	 * PDF storage root directory relative to the WPForms uploads dir.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	private const PDF_DIR = 'pdf';

	/**
	 * Get the PDFs storage directory.
	 *
	 * @since 1.0.0
	 *
	 * @param int $form_id  Form ID.
	 * @param int $entry_id Entry ID.
	 *
	 * @return string
	 */
	public static function get_dir( int $form_id = 0, int $entry_id = 0 ): string {

		$pdf_dir = trailingslashit( File::get_upload_dir() . self::PDF_DIR );

		if ( ! File::mkdir( $pdf_dir ) ) {
			wpforms_pdf()->helpers->log( sprintf( 'Unable to create PDF directory: %s', $pdf_dir ), 'error' );

			return '';
		}

		// Return the root PDF addon storage directory if form ID is missing.
		if ( $form_id === 0 ) {
			return $pdf_dir;
		}

		$entry_dir = sprintf(
			'%1$s%2$d-%3$s/',
			$pdf_dir,
			$form_id,
			wp_hash( $form_id . '_' . $entry_id )
		);

		if ( ! File::mkdir( $entry_dir ) ) {
			wpforms_pdf()->helpers->log( sprintf( 'Unable to create entry directory: %s', $entry_dir ), 'error' );

			return '';
		}

		return $entry_dir;
	}

	/**
	 * Get the PDFs storage URL.
	 *
	 * @since 1.0.0
	 *
	 * @param int $form_id  Form ID.
	 * @param int $entry_id Entry ID.
	 *
	 * @return string
	 */
	public static function get_url( int $form_id = 0, int $entry_id = 0 ): string {

		$pdf_dir_url = trailingslashit( File::get_upload_url() . '/' . self::PDF_DIR );

		// Return the root PDF addon storage directory URL.
		if ( $form_id === 0 ) {
			return $pdf_dir_url;
		}

		return sprintf(
			'%1$s%2$d-%3$s/',
			$pdf_dir_url,
			$form_id,
			wp_hash( $form_id . '_' . $entry_id )
		);
	}

	/**
	 * Remove PDF files for a specific entry.
	 *
	 * Deletes the entry's PDF directory and all files within it.
	 *
	 * @since 1.3.0
	 *
	 * @param int $form_id  Form ID.
	 * @param int $entry_id Entry ID.
	 *
	 * @return bool True if the directory was removed or didn't exist, false on failure.
	 */
	public static function remove( int $form_id, int $entry_id ): bool {

		if ( $form_id === 0 || $entry_id === 0 ) {
			return false;
		}

		$entry_dir = self::get_dir( $form_id, $entry_id );

		if ( empty( $entry_dir ) ) {
			return false;
		}

		// Nothing to remove if the directory doesn't exist.
		if ( ! File::exists( $entry_dir ) ) {
			return true;
		}

		if ( ! File::delete( $entry_dir ) ) {
			wpforms_pdf()->helpers->log( sprintf( 'Unable to remove PDF directory: %s', $entry_dir ), 'error' );

			return false;
		}

		return true;
	}
}