media.php 4.12 KB
<?php
    /**
	* @Description : Process media library
	* @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_Media {

        private static $instance = null;

        public $media_files = array();

        public $helper = null;

        public static function get_instance() {
            if( null === self::$instance ) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        public function __construct(){
            $this->helper = CodeDropz_Uploader_Helpers::get_instance();
        }

        /**
        * Upload File and Save to Media Libray
        */

        public function save( $files = array(), $name = null ) {

            // Response status
            $response = array();

            // Required wordpress file and image library
            if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
                require_once( ABSPATH . 'wp-admin/includes/image.php' );
                require_once( ABSPATH . 'wp-admin/includes/media.php' );
            }

            // Check if $_Files is not empty
            if( empty( $files ) ) {
                $response['error'] = dnmfu_cf7_option('drag_n_drop_error_failed_to_upload') ? dnmfu_cf7_option('drag_n_drop_error_failed_to_upload') : $this->helper->get_error('failed_upload');
            }

            // Get file name and mimetype
            $filename = $files;
            $filetype = wp_check_filetype( basename( $filename ), null );
            $thumbnail = pathinfo( $filename );

            // Prepare an array of post data for the attachment.
            $attachment = array(
                'guid'           => $filename,
                'post_mime_type' => $filetype['type'],
                'post_title'     => preg_replace( '/\.[^.]+$/', '', wp_basename( $filename ) ),
                'post_content'   => '',
                'post_status'    => 'inherit'
            );

            // Insert the attachment.
            $attachment_id = wp_insert_attachment( $attachment, $filename, 0, true );

            // // Check status of upload if it's success or not
            if ( ! is_wp_error( $attachment_id ) ) {

                // Generate metadata for attachment
                $attach_data = wp_generate_attachment_metadata( $attachment_id, $filename );

                // Update database record for meta data information
                wp_update_attachment_metadata( $attachment_id, $attach_data );

                // get attachment meta data
                $attachment = wp_get_attachment_url( $attachment_id );

                // Assign response
                $response['file'] = $attachment;

                // Store media files
                if( $name ){
                    $this->media_files[ $name ][] = $attachment;
                }else{
                    $this->media_files[] = $attachment;
                }

                // Covert base_url to base_dir
                $file_path =  $thumbnail['dirname'] .'/'. $thumbnail['filename'] .'-100x100.' . strtolower( $thumbnail['extension'] );

                // Update post_meta
                update_post_meta( $attachment_id, '_dndmfu_cf7_file_upload', 'yes' );

                // Remove files & thumbnail
                if( file_exists( $file_path ) ) {
                    $this->helper->delete_files( $file_path );
                }

                // Allow theme/plugins to hook
                do_action('dndmfu_cf7_after_media_saved', $attachment_id );

            }else {
                $response['error']= ( $attachment_id->get_error_message() ? $attachment_id->get_error_message() : get_option('drag_n_drop_error_failed_to_upload') );
            }

            return $response;
        }

        /**
        * Get media files
        */

        public function get_media_files(){
            return $this->media_files;
        }

        /**
        * Reset media files
        */

        public function reset() {
            $this->media_files = array();
        }

    }