option, array($this, 'validate_options')); } public function initialize_default_options() { $default_options = array( "max_upload_size" => "100 ", "max_upload_no" => "2", "allow_ext" => "jpg,gif,png" ); update_option($this->option, $default_options); } public function display($atts = null) { if (isset($atts)) { if ($atts['theme'] == true) { $this->enquee(true); } } include_once (AAIU_DIR . '/html.php'); } public function validate_options($input) { return $input; } public function enquee($theme = false) { if ($theme) { $this->add_script(); } elseif ($this->has_shortcode('AAIU')) { $this->add_script(); } } public function add_script() { $this->options = get_option('aaiu-options'); wp_enqueue_script('jquery'); wp_enqueue_script('plupload-handlers'); $max_file_size = intval($this->options['max_upload_size']) * 1000 * 1000; $max_upload_no = intval($this->options['max_upload_no']); $allow_ext = $this->options['allow_ext']; wp_enqueue_script('aaiu_upload', AAIU_URL . 'js/aaiu_upload.js', array('jquery')); wp_localize_script('aaiu_upload', 'aaiu_upload', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('aaiu_upload'), 'remove' => wp_create_nonce('aaiu_remove'), 'number' => $max_upload_no, 'upload_enabled' => true, 'confirmMsg' => __('Are you sure you want to delete this?'), 'plupload' => array( 'runtimes' => 'html5,flash,html4', 'browse_button' => 'aaiu-uploader', 'container' => 'aaiu-upload-container', 'file_data_name' => 'aaiu_upload_file', 'max_file_size' => $max_file_size . 'b', 'url' => admin_url('admin-ajax.php') . '?action=aaiu_upload&nonce=' . wp_create_nonce('aaiu_allow'), 'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'), 'filters' => array(array('title' => __('Allowed Files'), 'extensions' => $allow_ext)), 'multipart' => true, 'urlstream_upload' => true, ) )); } public function upload() { check_ajax_referer('aaiu_allow', 'nonce'); $file = array( 'name' => $_FILES['aaiu_upload_file']['name'], 'type' => $_FILES['aaiu_upload_file']['type'], 'tmp_name' => $_FILES['aaiu_upload_file']['tmp_name'], 'error' => $_FILES['aaiu_upload_file']['error'], 'size' => $_FILES['aaiu_upload_file']['size'] ); $file = $this->fileupload_process($file); } public function fileupload_process($file) { $attachment = $this->handle_file($file); if (is_array($attachment)) { $html = $this->getHTML($attachment); $response = array( 'success' => true, 'html' => $html, ); echo json_encode($response); exit; } $response = array('success' => false); echo json_encode($response); exit; } function handle_file($upload_data) { $return = false; $uploaded_file = wp_handle_upload($upload_data, array('test_form' => false)); if (isset($uploaded_file['file'])) { $file_loc = $uploaded_file['file']; $file_name = basename($upload_data['name']); $file_type = wp_check_filetype($file_name); $attachment = array( 'post_mime_type' => $file_type['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', basename($file_name)), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment($attachment, $file_loc); $attach_data = wp_generate_attachment_metadata($attach_id, $file_loc); wp_update_attachment_metadata($attach_id, $attach_data); $return = array('data' => $attach_data, 'id' => $attach_id); return $return; } return $return; } function getHTML($attachment) { $attach_id = $attachment['id']; $file = explode('/', $attachment['data']['file']); $file = array_slice($file, 0, count($file) - 1); $path = implode('/', $file); $image = $attachment['data']['sizes']['thumbnail']['file']; $post = get_post($attach_id); $dir = wp_upload_dir(); $path = $dir['baseurl'] . '/' . $path; $html = ''; $html .= '