helpers.php 4.96 KB
<?php
    /**
	* @Description : File helpers
	* @Package : Drag & Drop Multiple File Upload - Contact Form 7
	* @Author : CodeDropz
	*/

    if ( ! defined( 'ABSPATH' ) || ! defined('dnd_upload_cf7') || ! defined('dnd_upload_cf7_PRO') ) {
		exit;
	}

    class CodeDropz_Uploader_Helpers {

        private static $instance = null;

        public static function get_instance() {
            if( null === self::$instance ) {
                self::$instance = new self();
            }
            return self::$instance;
        }
        
        /**
        * Get current time based on GMT selected
        */

        public function get_date( $format ) {
            $string = date( 'Y-m-d H:i:s' );
            return get_date_from_gmt( $string, $format );
        }

        /**
        * Get current user information
        */

        public function get_user(){
            return is_user_logged_in() ? wp_get_current_user() : null;
        }

        /**
        * Delete Preview Thumbnail
        * @param : $file_path - basedir
        */

        public function delete_files( $file_path ) {

            // There's no reason to proceed if - null
            if( ! $file_path ) {
                return;
            }

            // Get file info
            $file = pathinfo( $file_path );
            $dirname = trailingslashit( wp_normalize_path( $file['dirname'] ) );

            // Check and validate file type if it's safe to delete...
            $safe_to_delete = dndmfu_cf7_validate_type( $file['extension'] );

            // @bolean - true if validated
            if( $safe_to_delete ) {

                // Delete parent file
                wp_delete_file( $file_path );

                // Delete if there's a thum
                if( file_exists( $thumbnail = $dirname . $file['filename'].'-100x100' .'.'. strtolower( $file['extension'] ) ) ) {
                    wp_delete_file( $thumbnail );
                }
            }
        }

        /**
        * Setup media file on json response after the successfull upload.
        */

        public function media_response( $path, $file_name ) {

            // Default placeholder
            $preview = false;

            // Match to images ext
            $is_image = preg_match( '/\.(jpg|jpeg|png|gif|tiff|svg )$/i', $file_name );

            // Get file type
            $file_type = wp_check_filetype( $file_name );

            // Apply preview on images only
            if( get_option('drag_n_drop_show_img_preview') === true ) {
                $preview = ( $is_image ? true :  wp_mime_type_icon( $file_type['type'] ) );
            }

            $media_files = array(
                'path'		=>	$path,
                'file'		=>	$file_name,
                'preview'	=>	$preview
            );

            // Display column for preview image
            if( get_option('drag_n_drop_thumbnail_display') == 'column' ) {
                $media_files['is_image'] = ( $is_image ? true : false );
                $media_files['ext'] = pathinfo( $file_name, PATHINFO_EXTENSION  );
                if( ! $is_image ) {
                    $media_files['preview'] = wp_mime_type_icon( $file_type['type'] );
                }
            }

            return $media_files;
        }

        /**
        * Get allowed file types @param : form_id
        */

        public function allowed_types( $form_id ) {

            // Initialize contact form instance
            $form = WPCF7_ContactForm::get_instance( $form_id );

            // Check if not valid object and null
            if( ! $form && ! is_object( $form ) ) {
                return false;
            }

            // Get specific tag (mfile is for dnd file upload)
            $tags = $form->scan_form_tags( array( 'type' => array('mfile', 'mfile*') ) );
            $supported_types = array();

            // Loop all upload tags
            if( $tags && is_array( $tags ) ) {
                foreach( $tags as $tag ) {

                    // Get file types option & remove not allowed character..
                    $types = preg_replace( '/[^a-zA-Z0-9|\']/', '', $tag->get_option('filetypes','', true ) );

                    // Assign if filetypes is present otherwise use the default ext list.
                    $supported_types[ $tag->name ] = ( $types ? $types : $this->default_ext() );
                }
            }

            return $supported_types;
        }

        /**
        * Define custom (safe) file extension.
        */

        public function default_ext() {
            return apply_filters('dnd_cf7_default_ext', 'jpg|jpeg|JPG|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv|xls' );
        }

        /**
        * Default error message
        */

        public function get_error( $error_key ) {
            $error_message = CodeDropz_Uploader_Errors::get_instance()->get_error_message();
            if( isset( $error_message[ $error_key ] ) ) {
                return $error_message[ $error_key ];
            }
            return false;
        }

    }