| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
global $wpscfunction; |
| 6 |
|
| 7 |
if ( |
| 8 |
check_ajax_referer('rb_upload_file', '_ajax_nonce', false) != 1 |
| 9 |
) wp_send_json_error('Unauthorised request!', 400); |
| 10 |
|
| 11 |
$extensions = array('jpg','JPG','jpeg','JPEG','png','PNG','gif','GIF'); |
| 12 |
$tempExtension = explode('.', $_FILES['file']['name']); |
| 13 |
$tempExtension = strtolower($tempExtension[count($tempExtension)-1]); |
| 14 |
|
| 15 |
$wpsc_allow_attachment_type = get_option('wpsc_allow_attachment_type'); |
| 16 |
$wpsc_attachment_type = explode(',',$wpsc_allow_attachment_type); |
| 17 |
$wpsc_attachment_type = array_map('trim', $wpsc_attachment_type); |
| 18 |
$wpsc_attachment_type = array_map('strtolower', $wpsc_attachment_type); |
| 19 |
|
| 20 |
$file_url = ''; |
| 21 |
if (in_array($tempExtension,$extensions) && in_array($tempExtension,$wpsc_attachment_type)) { |
| 22 |
$upload_dir = wp_upload_dir(); |
| 23 |
$save_file_name = $_FILES['file']['name']; |
| 24 |
$save_file_name = str_replace(' ','_',$save_file_name); |
| 25 |
$save_file_name = str_replace(',','_',$save_file_name); |
| 26 |
$save_file_name = time().'_'. $save_file_name; |
| 27 |
|
| 28 |
$now = date("Y-m-d H:i:s"); |
| 29 |
$time = strtotime($now); |
| 30 |
$month = date("m",$time); |
| 31 |
$year = date("Y",$time); |
| 32 |
|
| 33 |
$upload_dir = wp_upload_dir(); |
| 34 |
if (!file_exists($upload_dir['basedir'] . '/wpsc/'.$year)) { |
| 35 |
mkdir($upload_dir['basedir'] . '/wpsc/'.$year, 0755, true); |
| 36 |
} |
| 37 |
if (!file_exists($upload_dir['basedir'] . '/wpsc/'.$year.'/'.$month)) { |
| 38 |
mkdir($upload_dir['basedir'] . '/wpsc/'.$year.'/'.$month, 0755, true); |
| 39 |
} |
| 40 |
|
| 41 |
$save_directory = $upload_dir['basedir'] . '/wpsc/'.$year.'/'.$month.'/'.$save_file_name; |
| 42 |
$file_url = ""; |
| 43 |
|
| 44 |
$responce = move_uploaded_file( $_FILES['file']['tmp_name'], $save_directory ); |
| 45 |
if( $responce ){ |
| 46 |
|
| 47 |
$attachment_count = get_option('wpsc_attachment_count'); |
| 48 |
if(!$attachment_count) $attachment_count = 1; |
| 49 |
$term = wp_insert_term( 'attachment_img_'.$attachment_count, 'wpsc_image_attachment' ); |
| 50 |
if(!$term || is_wp_error($term)) die(); |
| 51 |
update_option('wpsc_attachment_count', ++$attachment_count); |
| 52 |
|
| 53 |
$file_path = addslashes($save_directory); |
| 54 |
add_term_meta ($term['term_id'], 'file_path', $file_path); |
| 55 |
|
| 56 |
$file_url = home_url() . "?wpsc_img_attachment=" . $term['term_id']; |
| 57 |
|
| 58 |
} |
| 59 |
} |
| 60 |
echo json_encode($file_url); |
| 61 |
|