Example #1
0
/**
 * Get directories
 *
 * @since 2.0.1
 * @param $dir (packages or import)
 * @return array (directories)
 */
function sell_media_get_directories($dir = null)
{
    $directories = '';
    $path = $dir == 'packages' ? sell_media_get_packages_upload_dir() : sell_media_get_import_dir();
    foreach (glob($path . "/*", GLOB_ONLYDIR) as $directory) {
        $directories[] = $directory;
    }
    return $directories;
}
Example #2
0
/**
 * Bulk upload callback
 */
function sell_media_upload_bulk_callback()
{
    check_ajax_referer('_sell_media_meta_box_nonce', 'security');
    @ini_set('max_execution_time', '300');
    if (isset($_POST['dir'])) {
        $path = sell_media_get_import_dir() . '/' . $_POST['dir'] . '/';
        if (file_exists($path)) {
            $files = glob($path . '*.*');
            $html = '';
            if ($files) {
                foreach ($files as $file) {
                    /**
                     * The WP function media_handle_sideload requires a files array ($_FILES).
                     * Since we want to keep the original file, we copy the original file
                     * and create a tmp file, which we can then unlink (delete)
                     */
                    $tmp = $file . '.tmp';
                    copy($file, $tmp);
                    $file_array = array('name' => basename($file), 'tmp_name' => $tmp);
                    $attachment_id = media_handle_sideload($file_array, $_POST['id']);
                    // remove temp file
                    @unlink($file_array['tmp_name']);
                    if (is_wp_error($attachment_id)) {
                        $html .= '<li class="attachment">' . sprintf(__('Sorry, %1$s could\'t be added.', 'sell_media'), basename($file)) . '</li>';
                    } else {
                        $html .= sell_media_list_uploads($attachment_id);
                    }
                }
            }
            echo $html;
        }
    }
    die;
}