/** * Uploads Gallery Image * * Action: adverts_gallery_upload * * @since 0.1 * @return void */ function adverts_gallery_upload() { if (!check_ajax_referer('adverts-gallery', '_ajax_nonce', false)) { echo json_encode(array("result" => 0, "error" => __("Invalid Session. Please refresh the page and try again.", "adverts"))); exit; } add_filter("adverts_gallery_upload_prefilter", "adverts_file_is_image"); // you can use WP's wp_handle_upload() function: $status = wp_handle_upload($_FILES['async-upload'], array('test_form' => true, 'action' => 'adverts_gallery_upload')); if (isset($status['error'])) { echo json_encode($status); exit; } // $filename should be the path to a file in the upload directory. $filename = $status['file']; // The ID of the post this attachment is for. $parent_post_id = intval($_POST["post_id"]); // Check the type of tile. We'll use this as the 'post_mime_type'. $filetype = wp_check_filetype(basename($filename), null); // Get the path to the upload directory. $wp_upload_dir = wp_upload_dir(); // Prepare an array of post data for the attachment. $attachment = array('guid' => $wp_upload_dir['url'] . '/' . basename($filename), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit'); // Create post if does not exist if ($parent_post_id < 1) { add_filter("post_type_link", "__return_empty_string"); $parent_post_id = wp_insert_post(apply_filters("adverts_insert_post", array('post_title' => 'Adverts Auto Draft', 'post_content' => '', 'post_status' => adverts_tmp_post_status(), 'post_author' => wp_get_current_user()->ID, 'post_type' => 'advert', 'comments_status' => 'closed'))); remove_filter("post_type_link", "__return_empty_string"); } // Insert the attachment. $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id); if (!is_wp_error($attach_id)) { wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $filename)); } include_once ADVERTS_PATH . 'includes/gallery.php'; echo json_encode(adverts_upload_item_data($attach_id)); exit; }
/** * Renders Gallery Input * * Renders gallery drop file input and uploaded items. * * @param WP_Post $post * @param array $conf * @since 0.1 * @return void */ function adverts_gallery_content($post = null, $conf = array()) { wp_nonce_field(plugin_basename(__FILE__), 'adverts_gallery_content_nonce'); $conf = shortcode_atts(array("button_class" => "button-secondary", "post_id_input" => "#post_ID"), $conf); $init = array('runtimes' => 'html5,silverlight,flash,html4', 'browse_button' => 'adverts-plupload-browse-button', 'container' => 'adverts-plupload-upload-ui', 'drop_element' => 'adverts-drag-drop-area', 'file_data_name' => 'async-upload', 'multiple_queues' => true, 'max_file_size' => wp_max_upload_size() . 'b', 'url' => admin_url('admin-ajax.php'), 'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'), 'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'), 'filters' => array(array('title' => __('Allowed Files'), 'extensions' => '*')), 'multipart' => true, 'urlstream_upload' => true, 'multipart_params' => array('_ajax_nonce' => wp_create_nonce('adverts-gallery'), 'action' => 'adverts_gallery_upload')); ?> <?php if (is_admin()) { ?> <div id="adverts-plupload-upload-ui"> <div id="adverts-drag-drop-area" class="adverts-gallery"> <p><?php _e("Drop Images Here", "adverts"); ?> </p> <p><a href="#" id="adverts-plupload-browse-button" class="button-secondary"><?php _e("browse files ...", "adverts"); ?> </a></p> </div> <div class="adverts-gallery-uploads"> </div> <br class="clear" /> </div> <div id="adverts-modal-gallery" class="adverts-modal"> <div class="media-modal wp-core-ui"> <a class="media-modal-close adverts-upload-modal-close" href="#" title="<?php _e("Close", "adverts"); ?> "><span class="media-modal-icon"></span></a> <div class="media-modal-content"> <div class="media-frame wp-core-ui2 hide-menu"> <div class="media-frame-title"><h1><?php _e("Edit Image", "adverts"); ?> </h1></div> <div class="media-frame-router"> <div class="media-router"> <a href="#" class="media-menu-item active"><?php _e("Image Properties", "adverts"); ?> </a> </div> </div> <div class="media-frame-content"> <div class="wrap" style="padding:0px 20px"> <form method="post" action=""> <table class="form-table"> <tbody> <tr> <th scope="row"><label for="adverts_caption"><?php _e("Title", "adverts"); ?> </label></th> <td> <input name="adverts_caption" type="text" id="adverts_caption" value="" class="regular-text" /> </td> </tr> <tr> <th scope="row"><label for="adverts_featured"><?php _e("Featured", "adverts"); ?> </label></th> <td> <label for="adverts_featured"> <input name="adverts_featured" type="checkbox" id="adverts_featured" value="1" /> <?php esc_attr_e("Use this image as main image", "adverts"); ?> </label> </td> </tr> <tr> <th scope="row"><label for="adverts_content"><?php _e("Description", "adverts"); ?> </label></th> <td> <textarea name="adverts_content" rows="10" cols="50" id="adverts_content" class="large-text code"></textarea> </td> </tr> </tbody> </table> </form> </div> </div> <div class="media-frame-toolbar"> <div class="media-toolbar"> <div class="media-toolbar-secondary"></div> <div class="media-toolbar-primary"> <span class="adverts-spinner adverts-loader adverts-icon-spinner animate-spin"></span> <a href="#" class="button media-button button-secondary button-large media-button-select adverts-upload-modal-close"><?php _e("Cancel", "adverts"); ?> </a> <a href="#" class="button media-button button-primary button-large media-button-select adverts-upload-modal-update"><?php _e("Update and Close", "adverts"); ?> </a> </div> </div> </div> </div> </div> </div> <div class="media-modal-backdrop"></div> </div><!-- .adverts-modal --> <?php } else { ?> <?php $iPhone = stripos($_SERVER['HTTP_USER_AGENT'], "iPhone"); $iPad = stripos($_SERVER['HTTP_USER_AGENT'], "iPad"); ?> <div id="adverts-plupload-upload-ui"> <div id="<?php echo $iPhone || $iPad ? 'adverts-drag-drop-disabled' : 'adverts-drag-drop-area'; ?> " class="adverts-gallery"> <p><?php _e("Drop Images Here", "adverts"); ?> </p> <p><a href="#" id="adverts-plupload-browse-button" class="adverts-button"><?php _e("browse files ...", "adverts"); ?> </a></p> </div> <div class="adverts-gallery-uploads"> </div> </div> <?php add_action("wp_footer", "adverts_gallery_modal"); ?> <?php } ?> <?php // Get data for uploaded items and format it as JSON. $data = array(); if ($post) { $children = get_children(array('post_parent' => $post->ID, 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'inherit')); foreach ($children as $child) { $data[] = adverts_upload_item_data($child->ID); } } ?> <script type="text/javascript"> var ADVERTS_PLUPLOAD_INIT = <?php echo json_encode($init); ?> ; var ADVERTS_PLUPLOAD_DATA = <?php echo json_encode($data); ?> ; var ADVERTS_PLUPLOAD_CONF = <?php echo json_encode($conf); ?> ; </script> <?php }