Caldera_Forms_Files

Handles file uploading from file fields.

Defined (1)

The class is defined in the following location(s).

/classes/files.php  
  1. class Caldera_Forms_Files{ 
  2.  
  3. const CRON_ACTION = 'caldera_forms_delete_files'; 
  4.  
  5. /** 
  6. * Holds upload dir path for non-media library uploads 
  7. * @since 1.4.4 
  8. * @var string 
  9. */ 
  10. protected static $dir; 
  11.  
  12. /** 
  13. * Upload a file to WordPress 
  14. * @since 1.4.4 
  15. * @param array $file File 
  16. * @param array $args Optional. Used to place in private dir 
  17. * @return array 
  18. */ 
  19. public static function upload( $file, array $args = array() ) { 
  20. $args = wp_parse_args($args, array( 
  21. 'private' => false,  
  22. 'field_id' => null,  
  23. 'form_id' => null,  
  24. )); 
  25. if( true == $args[ 'private' ] && ! empty( $args[ 'field_id' ] ) && ! empty( $args[ 'form_id' ] )) { 
  26. $private = true; 
  27. }else{ 
  28. $private = false; 
  29.  
  30. if( $private ) { 
  31. wp_schedule_single_event( time() + HOUR_IN_SECONDS, self::CRON_ACTION, array( 
  32. $args[ 'field_id' ],  
  33. $args[ 'form_id' ] 
  34. ) ); 
  35.  
  36. self::add_upload_filter( $args[ 'field_id' ], $args[ 'form_id' ], $private ); 
  37.  
  38. $upload = wp_handle_upload($file, array( 'test_form' => false ) ); 
  39.  
  40. if( $private ) { 
  41. self::remove_upload_filter(); 
  42.  
  43. return $upload; 
  44.  
  45.  
  46. /** 
  47. * Add uploaded file to media library 
  48. * @since 1.4.4 
  49. * @param array $upload Uploaded file data 
  50. * @return int|string|bool The ID of attachment or false if error @since 1.5.0.8 
  51. */ 
  52. public static function add_to_media_library( $upload ) { 
  53. if( isset( $upload[ 'error' ] ) ) { 
  54. return false; 
  55.  
  56. require_once( ABSPATH . 'wp-admin/includes/media.php' ); 
  57. require_once( ABSPATH . 'wp-admin/includes/image.php' ); 
  58.  
  59. $media_item = array( 
  60. 'guid' => $upload['file'],  
  61. 'post_mime_type' => $upload['type'],  
  62. 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $upload['file'] ) ),  
  63. 'post_content' => '',  
  64. 'post_status' => 'inherit' 
  65. ); 
  66.  
  67. $media_id = wp_insert_attachment( $media_item, $upload['file'] ); 
  68.  
  69. $media_data = wp_generate_attachment_metadata( $media_id, $upload['file'] ); 
  70. wp_update_attachment_metadata( $media_id, $media_data ); 
  71.  
  72. return $media_id; 
  73.  
  74. /** 
  75. * Setup upload directory filter 
  76. * @since 1.4.4 
  77. * @param string $field_id The field ID for file field 
  78. * @param string $form_id The form ID 
  79. */ 
  80. public static function add_upload_filter( $field_id , $form_id, $private = true ) { 
  81. if ( $private ) { 
  82. self::$dir = self::secret_dir( $field_id, $form_id ); 
  83. }else{ 
  84. /** 
  85. * Filter directory for uploaded files 
  86. * If null, the file path will be WordPress' default 
  87. * @since 1.4.5 
  88. * @param string|null Directory 
  89. * @param string $field_id Field ID 
  90. * @param string $form_id Form ID 
  91. */ 
  92. $dir = apply_filters( 'caldera_forms_upload_directory', null, $field_id, $form_id ); 
  93. if( null != $dir ) { 
  94. self::$dir = $dir; 
  95. add_filter( 'upload_dir', array( __CLASS__, 'uploads_filter' ) ); 
  96.  
  97. /** 
  98. * Remove the filter for upload directory path 
  99. * @since 1.4.4 
  100. */ 
  101. public static function remove_upload_filter() { 
  102. remove_filter( 'upload_dir', array( __CLASS__, 'uploads_filter' ) ); 
  103.  
  104. /** 
  105. * Filter upload directory 
  106. * @uses "upload_dir" filter 
  107. * @since 1.4.4 
  108. * @param array $args 
  109. * @return array 
  110. */ 
  111. public static function uploads_filter( $args ) { 
  112.  
  113. if ( self::$dir ) { 
  114. $newdir = '/' . self::$dir; 
  115.  
  116. $args[ 'path' ] = str_replace( $args[ 'subdir' ], '', $args[ 'path' ] ); 
  117. $args[ 'url' ] = str_replace( $args[ 'subdir' ], '', $args[ 'url' ] ); 
  118. $args[ 'subdir' ] = $newdir; 
  119. $args[ 'path' ] .= $newdir; 
  120. $args[ 'url' ] .= $newdir; 
  121.  
  122. return $args; 
  123.  
  124. /** 
  125. * Get a secret file fir by field ID and form ID 
  126. * @since 1.4.4 
  127. * @param string $field_id The field ID for file field 
  128. * @param string $form_id The form ID 
  129. * @return string 
  130. */ 
  131. protected static function secret_dir( $field_id, $form_id ) { 
  132. return md5( $field_id . $form_id . NONCE_SALT ); 
  133.  
  134.  
  135. /** 
  136. * Delete all files from the secret dir for a field 
  137. * @since 1.4.4 
  138. * @param string $field_id The field ID for file field 
  139. * @param string $form_id The form ID 
  140. */ 
  141. protected static function delete_uploaded_files( $field_id, $form_id ) { 
  142. $uploads = wp_get_upload_dir(); 
  143. $dir = $uploads[ 'basedir' ] . '/' . self::secret_dir($field_id, $form_id); 
  144. if (is_dir($dir)) { 
  145. array_map('unlink', glob($dir . '/*')); 
  146. rmdir($dir); 
  147.   
  148.   
  149. /** 
  150. * After form submit, clear out files from secret dirs 
  151. * @since 1.4.4 
  152. * @param array $form Form config 
  153. * @param bool $second_run Optional. If using at mail hooks, set true to prevent recurrsion 
  154. */ 
  155. public static function cleanup( $form, $second_run = false ) { 
  156. if( false === $second_run && Caldera_Forms::should_send_mail( $form ) ) { 
  157. add_action( 'caldera_forms_mailer_complete', array( __CLASS__, 'delete_after_mail' ), 10, 3 ); 
  158. add_action( 'caldera_forms_mailer_failed', array( __CLASS__, 'delete_after_mail' ), 10, 3 ); 
  159. return; 
  160.  
  161. $form_id = $form[ 'ID' ]; 
  162. $fields = Caldera_Forms_Forms::get_fields( $form, false ); 
  163. foreach( $fields as $id => $field ) { 
  164. if( Caldera_Forms_Field_Util::is_file_field( $field, $form ) ) { 
  165. self::delete_uploaded_files( $field[ 'ID' ], $form_id ); 
  166.  
  167.  
  168.  
  169. /** 
  170. * Do cleanup after sending email 
  171. * We use "caldera_forms_submit_complete" to start the clean up, but that is too soon, if using mailer. 
  172. * @since 1.4.4 
  173. * @param $mail 
  174. * @param $data 
  175. * @param $form 
  176. */ 
  177. public static function delete_after_mail( $mail, $data, $form ) { 
  178. self::cleanup( $form, true ); 
  179.  
  180. /** 
  181. * Trigger file delete via CRON 
  182. * This is needed because if a form never completed submission, files are not deleted at caldera_forms_submit_complete 
  183. * @since 1.4.4 
  184. * @param array $args 
  185. */ 
  186. public static function cleanup_via_cron( $args ) { 
  187. if( isset( $args[0], $args[1] ) ) { 
  188. self::delete_uploaded_files( $args[0], $args[1] ); 
  189.  
  190.  
  191. /** 
  192. * Check if file field's uploads are private or not 
  193. * @since 1.4.4 
  194. * @param array $field Field config 
  195. * @return bool 
  196. */ 
  197. public static function is_private( array $field ) { 
  198.  
  199. if( Caldera_Forms_Field_Util::is_file_field( $field ) ) { 
  200. if( isset( $field[ 'config']['media_lib'] ) && true == $field['config']['media_lib'] ) { 
  201. return false; 
  202. }else{ 
  203. return true; 
  204.  
  205.  
  206. return true; 
  207.  
  208. /** 
  209. * Get the callback function for file uploads 
  210. * @since 1.4.4 
  211. * @param array $form Form config 
  212. * @param array $field Field config 
  213. * @return array|string|callable 
  214. */ 
  215. public static function get_upload_handler( $form, $field ) { 
  216.  
  217. /** 
  218. * Filter the callback function for file uploads 
  219. * @since 1.4.4 
  220. * @param array|string|callable Callable 
  221. * @param array $form Form config 
  222. * @param array $field Field config 
  223. */ 
  224. return apply_filters( 'caldera_forms_file_upload_handler', array( 'Caldera_Forms_Files', 'upload' ), $form, $field ); 
  225.  
  226. /** 
  227. * Check if field's files should be attached 
  228. * @since 1.5.0 
  229. * @param array $field 
  230. * @param array $form Form config 
  231. * @return bool 
  232. */ 
  233. public static function should_attach( array $field, array $form ) { 
  234. if( in_array( Caldera_Forms_Field_Util::get_type( $field ), $form ) ) { 
  235. return ! empty( $field[ 'config' ][ 'attach'] ); 
  236.  
  237. return false; 
  238.  
  239.  
  240. /** 
  241. * Get types of file fields 
  242. * @since 1.5.0 
  243. * @return array 
  244. */ 
  245. public static function types() { 
  246. return array( 'advanced_file', 'file' ); 
  247.