PodsField_File

The Pods - Custom Content Types and Fields PodsField File class.

Defined (1)

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

/classes/fields/file.php  
  1. class PodsField_File extends PodsField { 
  2.  
  3. /** 
  4. * Field Type Group 
  5. * @var string 
  6. * @since 2.0 
  7. */ 
  8. public static $group = 'Relationships / Media'; 
  9.  
  10. /** 
  11. * Field Type Identifier 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. public static $type = 'file'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'File / Image / Video'; 
  23.  
  24. /** 
  25. * API caching for fields that need it during validate/save 
  26. * @var \PodsAPI 
  27. * @since 2.3 
  28. */ 
  29. protected static $api = false; 
  30.  
  31. /** 
  32. * Do things like register/enqueue scripts and stylesheets 
  33. * @since 2.0 
  34. */ 
  35. public function __construct () { 
  36.  
  37.  
  38. /** 
  39. * Add admin_init actions 
  40. * @since 2.3 
  41. */ 
  42. public function admin_init() { 
  43. // AJAX for Uploads 
  44. add_action( 'wp_ajax_pods_upload', array( $this, 'admin_ajax_upload' ) ); 
  45. add_action( 'wp_ajax_nopriv_pods_upload', array( $this, 'admin_ajax_upload' ) ); 
  46.  
  47. /** 
  48. * Add options and set defaults to 
  49. * @param array $options 
  50. * @since 2.0 
  51. */ 
  52. public function options () { 
  53. $sizes = get_intermediate_image_sizes(); 
  54.  
  55. $image_sizes = array(); 
  56.  
  57. foreach ( $sizes as $size ) { 
  58. $image_sizes[ $size ] = ucwords( str_replace( '-', ' ', $size ) ); 
  59.  
  60. $options = array( 
  61. self::$type . '_format_type' => array( 
  62. 'label' => __( 'Upload Limit', 'pods' ),  
  63. 'default' => 'single',  
  64. 'type' => 'pick',  
  65. 'data' => array( 
  66. 'single' => __( 'Single File', 'pods' ),  
  67. 'multi' => __( 'Multiple Files', 'pods' ) 
  68. ),  
  69. 'dependency' => true 
  70. ),  
  71. self::$type . '_uploader' => array( 
  72. 'label' => __( 'File Uploader', 'pods' ),  
  73. 'default' => 'attachment',  
  74. 'type' => 'pick',  
  75. 'data' => apply_filters( 
  76. 'pods_form_ui_field_file_uploader_options',  
  77. array( 
  78. 'attachment' => __( 'Attachments (WP Media Library)', 'pods' ),  
  79. 'plupload' => __( 'Plupload', 'pods' ) 
  80. ),  
  81. 'dependency' => true 
  82. ),  
  83. self::$type . '_attachment_tab' => array( 
  84. 'label' => __( 'Attachments Default Tab', 'pods' ),  
  85. 'depends-on' => array( self::$type . '_uploader' => 'attachment' ),  
  86. 'default' => 'upload',  
  87. 'type' => 'pick',  
  88. 'data' => array( 
  89. // keys MUST match WP's router names 
  90. 'upload' => __( 'Upload File', 'pods' ),  
  91. 'browse' => __( 'Media Library', 'pods' ) 
  92. ),  
  93. self::$type . '_edit_title' => array( 
  94. 'label' => __( 'Editable Title', 'pods' ),  
  95. 'default' => 1,  
  96. 'type' => 'boolean' 
  97. ),  
  98. self::$type . '_linked' => array( 
  99. 'label' => __( 'Link to File in editor', 'pods' ),  
  100. 'default' => 0,  
  101. 'type' => 'boolean' 
  102. ),  
  103. self::$type . '_limit' => array( 
  104. 'label' => __( 'Max Number of Files', 'pods' ),  
  105. 'depends-on' => array( self::$type . '_format_type' => 'multi' ),  
  106. 'default' => 0,  
  107. 'type' => 'number' 
  108. ),  
  109. self::$type . '_restrict_filesize' => array( 
  110. 'label' => __( 'Restrict File Size', 'pods' ),  
  111. 'depends-on' => array( self::$type . '_uploader' => 'plupload' ),  
  112. 'default' => '10MB',  
  113. 'type' => 'text' 
  114. ),  
  115. self::$type . '_type' => array( 
  116. 'label' => __( 'Restrict File Types', 'pods' ),  
  117. 'default' => apply_filters( 'pods_form_ui_field_file_type_default', 'images' ),  
  118. 'type' => 'pick',  
  119. 'data' => apply_filters( 
  120. 'pods_form_ui_field_file_type_options',  
  121. array( 
  122. 'images' => __( 'Images (jpg, jpeg, png, gif)', 'pods' ),  
  123. 'video' => __( 'Video (mpg, mov, flv, mp4, etc..)', 'pods' ),  
  124. 'audio' => __( 'Audio (mp3, m4a, wav, wma, etc..)', 'pods' ),  
  125. 'text' => __( 'Text (txt, csv, tsv, rtx, etc..)', 'pods' ),  
  126. 'any' => __( 'Any Type (no restriction)', 'pods' ),  
  127. 'other' => __( 'Other (customize allowed extensions)', 'pods' ) 
  128. ),  
  129. 'dependency' => true 
  130. ),  
  131. self::$type . '_allowed_extensions' => array( 
  132. 'label' => __( 'Allowed File Extensions', 'pods' ),  
  133. 'description' => __( 'Separate file extensions with a comma (ex. jpg, png, mp4, mov)', 'pods' ),  
  134. 'depends-on' => array( self::$type . '_type' => 'other' ),  
  135. 'default' => apply_filters( 'pods_form_ui_field_file_extensions_default', '' ),  
  136. 'type' => 'text' 
  137. ), /** 
  138. self::$type . '_image_size' => array( 
  139. 'label' => __( 'Excluded Image Sizes', 'pods' ),  
  140. 'description' => __( 'Image sizes not to generate when processing the image', 'pods' ),  
  141. 'depends-on' => array( self::$type . '_type' => 'images' ),  
  142. 'default' => 'images',  
  143. 'type' => 'pick',  
  144. 'pick_format_type' => 'multi',  
  145. 'pick_format_multi' => 'checkbox',  
  146. 'data' => apply_filters( 
  147. 'pods_form_ui_field_file_image_size_options',  
  148. $image_sizes 
  149. ), */ 
  150. self::$type . '_add_button' => array( 
  151. 'label' => __( 'Add Button Text', 'pods' ),  
  152. 'default' => __( 'Add File', 'pods' ),  
  153. 'type' => 'text' 
  154. ),  
  155. self::$type . '_modal_title' => array( 
  156. 'label' => __( 'Modal Title', 'pods' ),  
  157. 'depends-on' => array( self::$type . '_uploader' => 'attachment' ),  
  158. 'default' => __( 'Attach a file', 'pods' ),  
  159. 'type' => 'text' 
  160. ),  
  161. self::$type . '_modal_add_button' => array( 
  162. 'label' => __( 'Modal Add Button Text', 'pods' ),  
  163. 'depends-on' => array( self::$type . '_uploader' => 'attachment' ),  
  164. 'default' => __( 'Add File', 'pods' ),  
  165. 'type' => 'text' 
  166. ); 
  167.  
  168. if ( !pods_version_check( 'wp', '3.5' ) ) { 
  169. unset( $options[ self::$type . '_linked' ] ); 
  170. unset( $options[ self::$type . '_modal_title' ] ); 
  171. unset( $options[ self::$type . '_modal_add_button' ] ); 
  172.  
  173. $options[ self::$type . '_attachment_tab' ][ 'default' ] = 'type'; 
  174. $options[ self::$type . '_attachment_tab' ][ 'data' ] = array( 
  175. 'type' => __( 'Upload File', 'pods' ),  
  176. 'library' => __( 'Media Library', 'pods' ) 
  177. ); 
  178.  
  179. return $options; 
  180.  
  181. /** 
  182. * Define the current field's schema for DB table storage 
  183. * @param array $options 
  184. * @return array 
  185. * @since 2.0 
  186. */ 
  187. public function schema ( $options = null ) { 
  188. $schema = false; 
  189.  
  190. return $schema; 
  191.  
  192. /** 
  193. * Change the way the value of the field is displayed with Pods::get 
  194. * @param mixed $value 
  195. * @param string $name 
  196. * @param array $options 
  197. * @param array $pod 
  198. * @param int $id 
  199. * @return mixed|null 
  200. * @since 2.0 
  201. */ 
  202. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  203. if ( is_array( $value ) && !empty( $value ) ) { 
  204. if ( isset( $value[ 'ID' ] ) ) 
  205. $value = wp_get_attachment_url( $value[ 'ID' ] ); 
  206. else { 
  207. $attachments = $value; 
  208. $value = array(); 
  209.  
  210. foreach ( $attachments as $v ) { 
  211. if ( !is_array( $v ) ) 
  212. $value[] = $v; 
  213. elseif ( isset( $v[ 'ID' ] ) ) 
  214. $value[] = wp_get_attachment_url( $v[ 'ID' ] ); 
  215.  
  216. $value = implode( ' ', $value ); 
  217.  
  218. return $value; 
  219.  
  220. /** 
  221. * Customize output of the form field 
  222. * @param string $name 
  223. * @param mixed $value 
  224. * @param array $options 
  225. * @param array $pod 
  226. * @param int $id 
  227. * @since 2.0 
  228. */ 
  229. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  230. $options = (array) $options; 
  231. $form_field_type = PodsForm::$field_type; 
  232.  
  233. if ( !is_admin() ) { 
  234. include_once( ABSPATH . '/wp-admin/includes/template.php' ); 
  235.  
  236. if ( is_multisite() ) 
  237. include_once( ABSPATH . '/wp-admin/includes/ms.php' ); 
  238.  
  239. if ( ( ( defined( 'PODS_DISABLE_FILE_UPLOAD' ) && true === PODS_DISABLE_FILE_UPLOAD ) 
  240. || ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && true === PODS_UPLOAD_REQUIRE_LOGIN && !is_user_logged_in() ) 
  241. || ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && !is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && ( !is_user_logged_in() || !current_user_can( PODS_UPLOAD_REQUIRE_LOGIN ) ) ) ) 
  242. && ( ( defined( 'PODS_DISABLE_FILE_BROWSER' ) && true === PODS_DISABLE_FILE_BROWSER ) 
  243. || ( defined( 'PODS_FILES_REQUIRE_LOGIN' ) && is_bool( PODS_FILES_REQUIRE_LOGIN ) && true === PODS_FILES_REQUIRE_LOGIN && !is_user_logged_in() ) 
  244. || ( defined( 'PODS_FILES_REQUIRE_LOGIN' ) && !is_bool( PODS_FILES_REQUIRE_LOGIN ) && ( !is_user_logged_in() || !current_user_can( PODS_FILES_REQUIRE_LOGIN ) ) ) ) 
  245. ) { 
  246. ?> 
  247. <p>You do not have access to upload / browse files. Contact your website admin to resolve.</p> 
  248. <?php 
  249. return; 
  250.  
  251. // Use plupload if attachment isn't available 
  252. if ( 'attachment' == pods_var( self::$type . '_uploader', $options ) && ( !is_user_logged_in() || ( !current_user_can( 'upload_files' ) && !current_user_can( 'edit_files' ) ) ) ) 
  253. $field_type = 'plupload'; 
  254. elseif ( 'plupload' == pods_var( self::$type . '_uploader', $options ) ) 
  255. $field_type = 'plupload'; 
  256. elseif ( 'attachment' == pods_var( self::$type . '_uploader', $options ) ) { 
  257. if ( !pods_version_check( 'wp', '3.5' ) || !is_admin() ) // @todo test frontend media modal 
  258. $field_type = 'attachment'; 
  259. else 
  260. $field_type = 'media'; 
  261. else { 
  262. // Support custom File Uploader integration 
  263. do_action( 'pods_form_ui_field_file_uploader_' . pods_var( self::$type . '_uploader', $options ), $name, $value, $options, $pod, $id ); 
  264. do_action( 'pods_form_ui_field_file_uploader', pods_var( self::$type . '_uploader', $options ), $name, $value, $options, $pod, $id ); 
  265. return; 
  266.  
  267. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  268.  
  269. /** 
  270. * Build regex necessary for JS validation 
  271. * @param mixed $value 
  272. * @param string $name 
  273. * @param array $options 
  274. * @param string $pod 
  275. * @param int $id 
  276. * @return bool 
  277. * @since 2.0 
  278. */ 
  279. public function regex ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  280. return false; 
  281.  
  282. /** 
  283. * Validate a value before it's saved 
  284. * @param mixed $value 
  285. * @param string $name 
  286. * @param array $options 
  287. * @param array $fields 
  288. * @param array $pod 
  289. * @param int $id 
  290. * @param null $params 
  291. * @return bool 
  292. * @since 2.0 
  293. */ 
  294. public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { 
  295. // check file size 
  296. // check file extensions 
  297. return true; 
  298.  
  299. /** 
  300. * Change the value or perform actions after validation but before saving to the DB 
  301. * @param mixed $value 
  302. * @param int $id 
  303. * @param string $name 
  304. * @param array $options 
  305. * @param array $fields 
  306. * @param array $pod 
  307. * @param object $params 
  308. * @return mixed 
  309. * @since 2.0 
  310. */ 
  311. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  312. return $value; 
  313.  
  314. /** 
  315. * Save the value to the DB 
  316. * @param mixed $value 
  317. * @param int $id 
  318. * @param string $name 
  319. * @param array $options 
  320. * @param array $fields 
  321. * @param array $pod 
  322. * @param object $params 
  323. * @since 2.3 
  324. */ 
  325. public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  326. if ( empty( self::$api ) ) 
  327. self::$api = pods_api(); 
  328.  
  329. // File title / field handling 
  330. foreach ( $value as $id ) { 
  331. $title = false; 
  332.  
  333. if ( is_array( $id ) ) { 
  334. if ( isset( $id[ 'title' ] ) && 0 < strlen( trim( $id[ 'title' ] ) ) ) 
  335. $title = trim( $id[ 'title' ] ); 
  336.  
  337. if ( isset( $id[ 'id' ] ) ) 
  338. $id = (int) $id[ 'id' ]; 
  339. else 
  340. $id = 0; 
  341.  
  342. if ( empty( $id ) ) 
  343. continue; 
  344.  
  345. // Update the title if set 
  346. if ( false !== $title && 1 == pods_var( self::$type . '_edit_title', $options, 0 ) ) { 
  347. $attachment_data = array( 
  348. 'ID' => $id,  
  349. 'post_title' => $title 
  350. ); 
  351.  
  352. self::$api->save_wp_object( 'media', $attachment_data ); 
  353.  
  354. /** 
  355. * Customize the Pods UI manage table column output 
  356. * @param int $id 
  357. * @param mixed $value 
  358. * @param string $name 
  359. * @param array $options 
  360. * @param array $fields 
  361. * @param array $pod 
  362. * @return mixed|void 
  363. * @since 2.0 
  364. */ 
  365. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  366. if ( empty( $value ) ) 
  367. return; 
  368.  
  369. if ( !empty( $value ) && isset( $value[ 'ID' ] ) ) 
  370. $value = array( $value ); 
  371.  
  372. $image_size = apply_filters( 'pods_form_ui_field_file_ui_image_size', 'thumbnail', $id, $value, $name, $options, $pod ); 
  373.  
  374. return $this->images( $id, $value, $name, $options, $pod, $image_size ); 
  375.  
  376. /** 
  377. * Return image(s) markup 
  378. * @param int $id 
  379. * @param mixed $value 
  380. * @param string $name 
  381. * @param array $options 
  382. * @param array $pod 
  383. * @param string $image_size 
  384. * @return string 
  385. * @since 2.3 
  386. */ 
  387. public function images ( $id, $value, $name = null, $options = null, $pod = null, $image_size = null ) { 
  388. $images = ''; 
  389.  
  390. if ( empty( $value ) || !is_array( $value ) ) 
  391. return $images; 
  392.  
  393. foreach ( $value as $v ) { 
  394. $images .= pods_image( $v, $image_size ); 
  395.  
  396. return $images; 
  397.  
  398. /** 
  399. * Handle file row output for uploaders 
  400. * @param array $attributes 
  401. * @param int $limit 
  402. * @param bool $editable 
  403. * @param int $id 
  404. * @param string $icon 
  405. * @param string $name 
  406. * @return string 
  407. * @since 2.0 
  408. */ 
  409. public function markup ( $attributes, $limit = 1, $editable = true, $id = null, $icon = null, $name = null, $linked = false, $link = null ) { 
  410. // Preserve current file type 
  411. $field_type = PodsForm::$field_type; 
  412.  
  413. ob_start(); 
  414.  
  415. if ( empty( $id ) ) 
  416. $id = '{{id}}'; 
  417.  
  418. if ( empty( $icon ) ) { 
  419. $icon = '{{icon}}'; 
  420. }else{ 
  421. $icon = esc_url( $icon ); 
  422.  
  423.  
  424. if ( empty( $name ) ) 
  425. $name = '{{name}}'; 
  426.  
  427. if ( empty( $link ) ) 
  428. $link = '{{link}}'; 
  429.  
  430. $editable = (boolean) $editable; 
  431. $linked = (boolean) $linked; 
  432. ?> 
  433. <li class="pods-file hidden" id="pods-file-<?php echo esc_attr( $id ); ?>"> 
  434. <?php echo PodsForm::field( $attributes[ 'name' ] . '[' . $id . '][id]', $id, 'hidden' ); ?> 
  435.  
  436. <ul class="pods-file-meta media-item"> 
  437. <?php if ( 1 != $limit ) { ?> 
  438. <li class="pods-file-col pods-file-handle">Handle</li> 
  439. <?php } ?> 
  440.  
  441. <li class="pods-file-col pods-file-icon"> 
  442. <img class="pinkynail" src="<?php echo $icon; ?>" alt="Icon" /> 
  443. </li> 
  444.  
  445. <li class="pods-file-col pods-file-name"> 
  446. <?php 
  447. if ( $editable ) 
  448. echo PodsForm::field( $attributes[ 'name' ] . '[' . $id . '][title]', $name, 'text' ); 
  449. else 
  450. echo ( empty( $name ) ? '{{name}}' : $name ); 
  451. ?> 
  452. </li> 
  453.  
  454. <li class="pods-file-col pods-file-delete"><a href="#delete">Delete</a></li> 
  455.  
  456. <?php 
  457. if ( $linked ) { 
  458. ?> 
  459. <li class="pods-file-col pods-file-download"><a href="<?php echo esc_url( $link ); ?>" target="_blank">Download</a></li> 
  460. <?php 
  461. ?> 
  462. </ul> 
  463. </li> 
  464. <?php 
  465. PodsForm::$field_type = $field_type; 
  466.  
  467. return ob_get_clean(); 
  468.  
  469. /** 
  470. * Handle plupload AJAX 
  471. * @since 2.3 
  472. */ 
  473. public function admin_ajax_upload () { 
  474. pods_session_start(); 
  475.  
  476. // Sanitize input 
  477. $params = pods_unslash( (array) $_POST ); 
  478.  
  479. foreach ( $params as $key => $value ) { 
  480. if ( 'action' == $key ) 
  481. continue; 
  482.  
  483. unset( $params[ $key ] ); 
  484.  
  485. $params[ str_replace( '_podsfix_', '', $key ) ] = $value; 
  486.  
  487. $params = (object) $params; 
  488.  
  489. $methods = array( 
  490. 'upload',  
  491. ); 
  492.  
  493. if ( !isset( $params->method ) || !in_array( $params->method, $methods ) || !isset( $params->pod ) || !isset( $params->field ) || !isset( $params->uri ) || empty( $params->uri ) ) 
  494. pods_error( 'Invalid AJAX request', PodsInit::$admin ); 
  495. elseif ( !empty( $params->pod ) && empty( $params->field ) ) 
  496. pods_error( 'Invalid AJAX request', PodsInit::$admin ); 
  497. elseif ( empty( $params->pod ) && !current_user_can( 'upload_files' ) ) 
  498. pods_error( 'Invalid AJAX request', PodsInit::$admin ); 
  499.  
  500. // Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead 
  501. if ( is_ssl() && empty( $_COOKIE[ SECURE_AUTH_COOKIE ] ) && !empty( $_REQUEST[ 'auth_cookie' ] ) ) 
  502. $_COOKIE[ SECURE_AUTH_COOKIE ] = $_REQUEST[ 'auth_cookie' ]; 
  503. elseif ( empty( $_COOKIE[ AUTH_COOKIE ] ) && !empty( $_REQUEST[ 'auth_cookie' ] ) ) 
  504. $_COOKIE[ AUTH_COOKIE ] = $_REQUEST[ 'auth_cookie' ]; 
  505.  
  506. if ( empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) && !empty( $_REQUEST[ 'logged_in_cookie' ] ) ) 
  507. $_COOKIE[ LOGGED_IN_COOKIE ] = $_REQUEST[ 'logged_in_cookie' ]; 
  508.  
  509. global $current_user; 
  510. unset( $current_user ); 
  511.  
  512. /** 
  513. * Access Checking 
  514. */ 
  515. $upload_disabled = false; 
  516.  
  517. if ( defined( 'PODS_DISABLE_FILE_UPLOAD' ) && true === PODS_DISABLE_FILE_UPLOAD ) 
  518. $upload_disabled = true; 
  519. elseif ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && true === PODS_UPLOAD_REQUIRE_LOGIN && !is_user_logged_in() ) 
  520. $upload_disabled = true; 
  521. elseif ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && !is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && ( !is_user_logged_in() || !current_user_can( PODS_UPLOAD_REQUIRE_LOGIN ) ) ) 
  522. $upload_disabled = true; 
  523.  
  524. $uid = @session_id(); 
  525.  
  526. if ( is_user_logged_in() ) 
  527. $uid = 'user_' . get_current_user_id(); 
  528.  
  529. $nonce_check = 'pods_upload_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field; 
  530.  
  531. if ( true === $upload_disabled || !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) ) 
  532. pods_error( __( 'Unauthorized request', 'pods' ), PodsInit::$admin ); 
  533.  
  534. $pod = array(); 
  535. $field = array( 
  536. 'type' => 'file',  
  537. 'options' => array() 
  538. ); 
  539.  
  540. $api = pods_api(); 
  541.  
  542. $api->display_errors = false; 
  543.  
  544. if ( !empty( $params->pod ) ) { 
  545. $pod = $api->load_pod( array( 'id' => (int) $params->pod ) ); 
  546. $field = $api->load_field( array( 'id' => (int) $params->field ) ); 
  547.  
  548. if ( empty( $pod ) || empty( $field ) || $pod[ 'id' ] != $field[ 'pod_id' ] || !isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) ) 
  549. pods_error( __( 'Invalid field request', 'pods' ), PodsInit::$admin ); 
  550.  
  551. if ( !in_array( $field[ 'type' ], PodsForm::file_field_types() ) ) 
  552. pods_error( __( 'Invalid field', 'pods' ), PodsInit::$admin ); 
  553.  
  554. $method = $params->method; 
  555.  
  556. // Cleaning up $params 
  557. unset( $params->action ); 
  558. unset( $params->method ); 
  559. unset( $params->_wpnonce ); 
  560.  
  561. $params->post_id = pods_var( 'post_id', $params, 0, null, true ); 
  562.  
  563. /** 
  564. * Upload a new file (advanced - returns URL and ID) 
  565. */ 
  566. if ( 'upload' == $method ) { 
  567. $file = $_FILES[ 'Filedata' ]; 
  568.  
  569. $limit_size = pods_var( $field[ 'type' ] . '_restrict_filesize', $field[ 'options' ] ); 
  570.  
  571. if ( !empty( $limit_size ) ) { 
  572. if ( false !== stripos( $limit_size, 'MB' ) ) { 
  573. $limit_size = (float) trim( str_ireplace( 'MB', '', $limit_size ) ); 
  574. $limit_size = $limit_size * 1025 * 1025; // convert to KB to B 
  575. elseif ( false !== stripos( $limit_size, 'KB' ) ) { 
  576. $limit_size = (float) trim( str_ireplace( 'KB', '', $limit_size ) ); 
  577. $limit_size = $limit_size * 1025 * 1025; // convert to B 
  578. elseif ( false !== stripos( $limit_size, 'GB' ) ) { 
  579. $limit_size = (float) trim( str_ireplace( 'GB', '', $limit_size ) ); 
  580. $limit_size = $limit_size * 1025 * 1025 * 1025; // convert to MB to KB to B 
  581. elseif ( false !== stripos( $limit_size, 'B' ) ) 
  582. $limit_size = (float) trim( str_ireplace( 'B', '', $limit_size ) ); 
  583. else 
  584. $limit_size = wp_max_upload_size(); 
  585.  
  586. if ( 0 < $limit_size && $limit_size < $file[ 'size' ] ) { 
  587. $error = __( 'File size too large, max size is %s', 'pods' ); 
  588. $error = sprintf( $error, pods_var( $field[ 'type' ] . '_restrict_filesize', $field[ 'options' ] ) ); 
  589.  
  590. pods_error( '<div style="color:#FF0000">Error: ' . $error . '</div>' ); 
  591.  
  592. $limit_file_type = pods_var( $field[ 'type' ] . '_type', $field[ 'options' ], 'images' ); 
  593.  
  594. if ( 'images' == $limit_file_type ) 
  595. $limit_types = 'jpg, jpeg, png, gif'; 
  596. elseif ( 'video' == $limit_file_type ) 
  597. $limit_types = 'mpg, mov, flv, mp4'; 
  598. elseif ( 'audio' == $limit_file_type ) 
  599. $limit_types = 'mp3, m4a, wav, wma'; 
  600. elseif ( 'text' == $limit_file_type ) 
  601. $limit_types = 'txt, rtx, csv, tsv'; 
  602. elseif ( 'any' == $limit_file_type ) 
  603. $limit_types = ''; 
  604. else 
  605. $limit_types = pods_var( $field[ 'type' ] . '_allowed_extensions', $field[ 'options' ], '', null, true ); 
  606.  
  607. $limit_types = trim( str_replace( array( ' ', '.', "\n", "\t", ';' ), array( '', ', ', ', ', ', ' ), $limit_types ), ', ' ); 
  608.  
  609. if ( pods_version_check( 'wp', '3.5' ) ) { 
  610. $mime_types = wp_get_mime_types(); 
  611.  
  612. if ( in_array( $limit_file_type, array( 'images', 'audio', 'video' ) ) ) { 
  613. $new_limit_types = array(); 
  614.  
  615. foreach ( $mime_types as $type => $mime ) { 
  616. if ( 0 === strpos( $mime, $limit_file_type ) ) { 
  617. $type = explode( '|', $type ); 
  618.  
  619. $new_limit_types = array_merge( $new_limit_types, $type ); 
  620.  
  621. if ( !empty( $new_limit_types ) ) 
  622. $limit_types = implode( ', ', $new_limit_types ); 
  623. elseif ( 'any' != $limit_file_type ) { 
  624. $new_limit_types = array(); 
  625.  
  626. $limit_types = explode( ', ', $limit_types ); 
  627.  
  628. foreach ( $limit_types as $k => $limit_type ) { 
  629. $found = false; 
  630.  
  631. foreach ( $mime_types as $type => $mime ) { 
  632. if ( 0 === strpos( $mime, $limit_type ) ) { 
  633. $type = explode( '|', $type ); 
  634.  
  635. foreach ( $type as $t ) { 
  636. if ( !in_array( $t, $new_limit_types ) ) 
  637. $new_limit_types[] = $t; 
  638.  
  639. $found = true; 
  640.  
  641. if ( !$found ) 
  642. $new_limit_types[] = $limit_type; 
  643.  
  644. if ( !empty( $new_limit_types ) ) 
  645. $limit_types = implode( ', ', $new_limit_types ); 
  646.  
  647. $limit_types = explode( ', ', $limit_types ); 
  648.  
  649. $limit_types = array_filter( array_unique( $limit_types ) ); 
  650.  
  651. if ( !empty( $limit_types ) ) { 
  652. $ok = false; 
  653.  
  654. foreach ( $limit_types as $limit_type ) { 
  655. $limit_type = '.' . trim( $limit_type, ' .' ); 
  656.  
  657. $pos = ( strlen( $file[ 'name' ] ) - strlen( $limit_type ) ); 
  658.  
  659. if ( $pos === stripos( $file[ 'name' ], $limit_type ) ) { 
  660. $ok = true; 
  661.  
  662. break; 
  663.  
  664. if ( false === $ok ) { 
  665. $error = __( 'File type not allowed, please use one of the following: %s', 'pods' ); 
  666. $error = sprintf( $error, '.' . implode( ', .', $limit_types ) ); 
  667.  
  668. pods_error( '<div style="color:#FF0000">Error: ' . $error . '</div>' ); 
  669.  
  670. $custom_handler = apply_filters( 'pods_upload_handle', null, 'Filedata', $params->post_id, $params, $field ); 
  671.  
  672. if ( null === $custom_handler ) { 
  673. $linked = pods_var( $field[ 'type' ] . '_linked', $field[ 'options' ], 0 ); 
  674.  
  675. $attachment_id = media_handle_upload( 'Filedata', $params->post_id ); 
  676.  
  677. if ( is_object( $attachment_id ) ) { 
  678. $errors = array(); 
  679.  
  680. foreach ( $attachment_id->errors[ 'upload_error' ] as $error_code => $error_message ) { 
  681. $errors[] = '[' . $error_code . '] ' . $error_message; 
  682.  
  683. pods_error( '<div style="color:#FF0000">Error: ' . implode( '</div><div>', $errors ) . '</div>' ); 
  684. else { 
  685. $attachment = get_post( $attachment_id, ARRAY_A ); 
  686.  
  687. $attachment[ 'filename' ] = basename( $attachment[ 'guid' ] ); 
  688.  
  689. $thumb = wp_get_attachment_image_src( $attachment[ 'ID' ], 'thumbnail', true ); 
  690. $attachment[ 'thumbnail' ] = $thumb[ 0 ]; 
  691.  
  692. $attachment[ 'link' ] = ''; 
  693.  
  694. if ( $linked ) { 
  695. $attachment[ 'link' ] = wp_get_attachment_url( $attachment[ 'ID' ] ); 
  696.  
  697. $attachment = apply_filters( 'pods_upload_attachment', $attachment, $params->post_id ); 
  698.  
  699. wp_send_json( $attachment ); 
  700.  
  701. die(); // KBAI!