/classes/fields/file.php

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