예제 #1
0
 /**
  * Sideload an image to the WP_CONTENT_DIR/CN_IMAGE_DIR_NAME or in the defined subdirectory.
  *
  * @access public
  * @since  8.2.9
  * @static
  *
  * @uses   trailingslashit()
  * @uses   cnUpload
  *
  * @param array  $filename Reference to a single element of $_FILES.
  * @param string $folder   An associative array containing the upload params.
  *
  * @return mixed array | object On success an associative array of the uploaded file details. On failure, an instance of WP_Error.
  */
 public static function sideload($path, $filename, $folder = '')
 {
     // Add filter to lowercase the image filename extension.
     add_filter('sanitize_file_name', array(__CLASS__, 'extToLowercase'));
     $atts = array('action' => 'cn_image_sideload', 'sub_dir' => empty($folder) ? CN_IMAGE_DIR_NAME : trailingslashit(CN_IMAGE_DIR_NAME) . $folder, 'mimes' => array('jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png'));
     /**
      * Filter the arguments used when processing an image sideload.
      *
      * @since 8.2.9
      *
      * @param array $atts An associative array of the arguments used when processing an image upload.
      */
     $atts = apply_filters('cn_image_sideload_atts', $atts);
     /**
      * Action fires before an image is sideloaded.
      *
      * @since 8.2.9
      *
      * @param array  $filename A reference to a single element of $_FILES.
      * @param string $atts['sub_dir'] The subdirectory the image is to be uploaded.
      */
     do_action('cn_image_sideload', $filename, $atts['sub_dir']);
     // Build an array to match a single element of $_FILES so file can be processed using _wp_handle_upload().
     $file = array();
     $file['name'] = $filename;
     $file['type'] = '';
     $file['size'] = filesize($path . $filename);
     $file['tmp_name'] = $path . $filename;
     $file['error'] = 0;
     //var_dump( $file );
     $sideload = new cnUpload($file, $atts);
     $result = $sideload->result();
     if (!is_wp_error($result) && ($image = @getimagesize($result['path']))) {
         $result['width'] = $image[0];
         $result['height'] = $image[1];
         $result['size'] = $image[3];
         $result['mime'] = $image['mime'];
         $result['type'] = $image[2];
         $order = array('name' => '', 'path' => '', 'url' => '', 'width' => '', 'height' => '', 'size' => '', 'mime' => '', 'type' => '');
         /**
          * The sideloaded image meta data.
          *
          * @since 8.2.9
          *
          * @param array $result An associative array of the sideloaded image metadata.
          */
         $result = apply_filters('cn_image_sideloaded_meta', array_merge($order, $result));
     }
     /**
      * Fires after an image has been uploaded.
      *
      * @since 8.2.9
      *
      * @param mixed $result An associative array of the uploaded image metadata on success or an instance of WP_Error on error.
      */
     do_action('cn_image_sideloaded', $result);
     // Remove the filter which makes the image filename extension lowercase.
     remove_filter('sanitize_file_name', array(__CLASS__, 'extToLowercase'));
     return $result;
 }
 public static function uploadCSV()
 {
     //if ( ! function_exists( 'wp_handle_upload' ) ) {
     //
     //	require_once( ABSPATH . 'wp-admin/includes/file.php' );
     //}
     require_once CN_PATH . 'includes/import/class.csv-import-batch.php';
     if (!wp_verify_nonce($_REQUEST['nonce'], 'csv_upload')) {
         wp_send_json_error(array('form' => $_POST, 'message' => __('Nonce verification failed', 'connections')));
     }
     if (!(bool) apply_filters('cn_csv_import_capability', current_user_can('import'))) {
         wp_send_json_error(array('form' => $_POST, 'message' => __('You do not have permission to import data.', 'connections')));
     }
     if (empty($_FILES)) {
         wp_send_json_error(array('form' => $_POST, 'message' => __('No file file selected. Please select a file to import.', 'connections'), 'request' => $_REQUEST));
     }
     $upload = new cnUpload($_FILES['cn-import-file'], array('mimes' => array('csv' => 'text/csv', 'txt' => 'text/plain')));
     $result = $upload->result();
     if (!is_wp_error($result)) {
         $import = new cnCSV_Batch_Import($result['path']);
         $headers = $import->getHeaders();
         if (is_wp_error($headers)) {
             error_log(print_r($headers, TRUE));
             wp_send_json_error(array('form' => $_POST, 'message' => $headers->get_error_message()));
         }
         wp_send_json_success(array('form' => $_POST, 'file' => $result, 'fields' => array('-1' => esc_html__('Do Not Import', 'connections'), 'name' => esc_html__('Name', 'connections'), 'slug' => esc_html__('Slug', 'connections'), 'desc' => esc_html__('Description', 'connections'), 'parent' => esc_html__('Parent', 'connections')), 'headers' => $headers, 'nonce' => wp_create_nonce('import_csv_term')));
     } else {
         wp_send_json_error(array('form' => $_POST, 'message' => $result->get_error_message()));
     }
     exit;
 }
 /**
  * A filter to change the WP core upload path for files.
  *
  * @access private
  * @static
  * @since  8.1
  * @param  array $file The WP core upload path values.
  *
  * @return array
  */
 public function subDirectory($file)
 {
     // If this is a multi site AND Connections is in multi site mode then the the paths passed by WP can be used.
     if (is_multisite() && CN_MULTISITE_ENABLED) {
         $file['subdir'] = empty($this->subDirectory) ? cnURL::preslashit($file['subdir']) : cnURL::preslashit($this->subDirectory);
         $file['path'] = untrailingslashit($file['basedir']) . $file['subdir'];
         $file['url'] = untrailingslashit($file['baseurl']) . $file['subdir'];
         // If Connections is on single site or in single site mode on a multi site setup use cnUpload::info() to get the path info.
     } else {
         // NOTE: Important! cnUpload::info() can not be used within this class when `if ( is_multisite() && CN_MULTISITE_ENABLED )`
         // because it will cause a infinite loop due to the filter added in $this->file() which add this method as a callback
         // to the `upload_dir` hook.
         $info = cnUpload::info();
         $file['subdir'] = empty($this->subDirectory) ? cnURL::preslashit($file['subdir']) : cnURL::preslashit($this->subDirectory);
         $file['path'] = untrailingslashit($info['base_path']) . $file['subdir'];
         $file['url'] = untrailingslashit($info['base_url']) . $file['subdir'];
     }
     return $file;
 }