PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/fields/file.php

https://github.com/ElmsPark/pods
PHP | 451 lines | 224 code | 44 blank | 183 comment | 43 complexity | df0fa968a95bc74f1b7d0c4cdeb7202b MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * @package Pods\Fields
  4. */
  5. class PodsField_File extends PodsField {
  6. /**
  7. * Field Type Group
  8. *
  9. * @var string
  10. * @since 2.0.0
  11. */
  12. public static $group = 'Relationships / Media';
  13. /**
  14. * Field Type Identifier
  15. *
  16. * @var string
  17. * @since 2.0.0
  18. */
  19. public static $type = 'file';
  20. /**
  21. * Field Type Label
  22. *
  23. * @var string
  24. * @since 2.0.0
  25. */
  26. public static $label = 'File / Image / Video';
  27. /**
  28. * Do things like register/enqueue scripts and stylesheets
  29. *
  30. * @since 2.0.0
  31. */
  32. public function __construct () {
  33. }
  34. /**
  35. * Add options and set defaults to
  36. *
  37. * @param array $options
  38. *
  39. * @since 2.0.0
  40. */
  41. public function options () {
  42. $sizes = get_intermediate_image_sizes();
  43. $image_sizes = array();
  44. foreach ( $sizes as $size ) {
  45. $image_sizes[ $size ] = ucwords( str_replace( '-', ' ', $size ) );
  46. }
  47. $options = array(
  48. 'file_format_type' => array(
  49. 'label' => __( 'File Type', 'pods' ),
  50. 'default' => 'single',
  51. 'type' => 'pick',
  52. 'data' => array(
  53. 'single' => __( 'Single Select', 'pods' ),
  54. 'multi' => __( 'Multiple Select', 'pods' )
  55. ),
  56. 'dependency' => true
  57. ),
  58. 'file_uploader' => array(
  59. 'label' => __( 'File Uploader', 'pods' ),
  60. 'default' => 'attachment',
  61. 'type' => 'pick',
  62. 'data' => apply_filters(
  63. 'pods_form_ui_field_file_uploader_options',
  64. array(
  65. 'attachment' => __( 'Attachments (WP Media Library)', 'pods' ),
  66. 'plupload' => __( 'Plupload', 'pods' )
  67. )
  68. ),
  69. 'dependency' => true
  70. ),
  71. 'file_attachment_tab' => array(
  72. 'label' => __( 'Attachments Default Tab', 'pods' ),
  73. 'depends-on' => array( 'file_uploader' => 'attachment' ),
  74. 'default' => 'type',
  75. 'type' => 'pick',
  76. 'data' => array(
  77. // keys MUST match WP's router names
  78. 'upload' => __( 'Upload File', 'pods' ),
  79. 'browse' => __( 'Media Library', 'pods' )
  80. )
  81. ),
  82. 'file_edit_title' => array(
  83. 'label' => __( 'Editable Title', 'pods' ),
  84. 'default' => 1,
  85. 'type' => 'boolean'
  86. ),
  87. 'file_limit' => array(
  88. 'label' => __( 'File Limit', 'pods' ),
  89. 'depends-on' => array( 'file_format_type' => 'multi' ),
  90. 'default' => 0,
  91. 'type' => 'number'
  92. ),
  93. 'file_restrict_filesize' => array(
  94. 'label' => __( 'Restrict File Size', 'pods' ),
  95. 'depends-on' => array( 'file_uploader' => 'plupload' ),
  96. 'default' => '10MB',
  97. 'type' => 'text'
  98. ),
  99. 'file_type' => array(
  100. 'label' => __( 'Restrict File Types', 'pods' ),
  101. 'excludes-on' => array( 'file_uploader' => 'attachment' ),
  102. 'default' => 'images',
  103. 'type' => 'pick',
  104. 'data' => apply_filters(
  105. 'pods_form_ui_field_file_type_options',
  106. array(
  107. 'images' => __( 'Images (jpg, png, gif, etc..)', 'pods' ),
  108. 'video' => __( 'Video (mpg, mov, flv, mp4, etc..)', 'pods' ),
  109. 'audio' => __( 'Audio (mp3, m4a, wav, wma, etc..)', 'pods' ),
  110. 'text' => __( 'Text (txt, csv, tsv, rtx, etc..)', 'pods' ),
  111. 'any' => __( 'Any Type (no restriction)', 'pods' ),
  112. 'other' => __( 'Other (customize allowed extensions)', 'pods' )
  113. )
  114. ),
  115. 'dependency' => true
  116. ),
  117. 'file_allowed_extensions' => array(
  118. 'label' => __( 'Allowed File Extensions', 'pods' ),
  119. 'description' => __( 'Separate file extensions with a comma (ex. jpg,png,mp4,mov)', 'pods' ),
  120. 'depends-on' => array( 'file_type' => 'other' ),
  121. 'excludes-on' => array( 'file_uploader' => 'attachment' ),
  122. 'default' => '',
  123. 'type' => 'text'
  124. ),/*
  125. 'file_image_size' => array(
  126. 'label' => __( 'Excluded Image Sizes', 'pods' ),
  127. 'description' => __( 'Image sizes not to generate when processing the image', 'pods' ),
  128. 'depends-on' => array( 'file_type' => 'images' ),
  129. 'default' => 'images',
  130. 'type' => 'pick',
  131. 'pick_format_type' => 'multi',
  132. 'pick_format_multi' => 'checkbox',
  133. 'data' => apply_filters(
  134. 'pods_form_ui_field_file_image_size_options',
  135. $image_sizes
  136. )
  137. ),*/
  138. 'file_add_button' => array(
  139. 'label' => __( 'Add Button Text', 'pods' ),
  140. 'default' => __( 'Add File', 'pods' ),
  141. 'type' => 'text'
  142. ),
  143. 'file_modal_title' => array(
  144. 'label' => __( 'Modal Title', 'pods' ),
  145. 'depends-on' => array( 'file_uploader' => 'attachment' ),
  146. 'default' => __( 'Attach a file', 'pods' ),
  147. 'type' => 'text'
  148. ),
  149. 'file_modal_add_button' => array(
  150. 'label' => __( 'Modal Add Button Text', 'pods' ),
  151. 'depends-on' => array( 'file_uploader' => 'attachment' ),
  152. 'default' => __( 'Add File', 'pods' ),
  153. 'type' => 'text'
  154. )
  155. );
  156. if ( !pods_wp_version( '3.5' ) ) {
  157. unset( $options[ 'file_modal_add_button' ] );
  158. unset( $options[ 'file_modal_add_button' ] );
  159. unset( $options[ 'file_modal_title' ] );
  160. unset( $options[ 'file_modal_add_button' ] );
  161. $options[ 'file_attachment_tab' ][ 'data' ] = array(
  162. 'type' => __( 'Upload File', 'pods' ),
  163. 'library' => __( 'Media Library', 'pods' )
  164. );
  165. }
  166. return $options;
  167. }
  168. /**
  169. * Define the current field's schema for DB table storage
  170. *
  171. * @param array $options
  172. *
  173. * @return array
  174. * @since 2.0.0
  175. */
  176. public function schema ( $options = null ) {
  177. $schema = false;
  178. return $schema;
  179. }
  180. /**
  181. * Change the way the value of the field is displayed with Pods::get
  182. *
  183. * @param mixed $value
  184. * @param string $name
  185. * @param array $options
  186. * @param array $pod
  187. * @param int $id
  188. *
  189. * @return mixed|null
  190. * @since 2.0.0
  191. */
  192. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
  193. return $value;
  194. }
  195. /**
  196. * Customize output of the form field
  197. *
  198. * @param string $name
  199. * @param mixed $value
  200. * @param array $options
  201. * @param array $pod
  202. * @param int $id
  203. *
  204. * @since 2.0.0
  205. */
  206. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
  207. $options = (array) $options;
  208. $form_field_type = PodsForm::$field_type;
  209. if ( !is_admin() ) {
  210. include_once( ABSPATH . '/wp-admin/includes/template.php' );
  211. if ( is_multisite() )
  212. include_once( ABSPATH . '/wp-admin/includes/ms.php' );
  213. }
  214. if ( ( ( defined( 'PODS_DISABLE_FILE_UPLOAD' ) && true === PODS_DISABLE_FILE_UPLOAD )
  215. || ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && true === PODS_UPLOAD_REQUIRE_LOGIN && !is_user_logged_in() )
  216. || ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && !is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && ( !is_user_logged_in() || !current_user_can( PODS_UPLOAD_REQUIRE_LOGIN ) ) ) )
  217. && ( ( defined( 'PODS_DISABLE_FILE_BROWSER' ) && true === PODS_DISABLE_FILE_BROWSER )
  218. || ( defined( 'PODS_FILES_REQUIRE_LOGIN' ) && is_bool( PODS_FILES_REQUIRE_LOGIN ) && true === PODS_FILES_REQUIRE_LOGIN && !is_user_logged_in() )
  219. || ( defined( 'PODS_FILES_REQUIRE_LOGIN' ) && !is_bool( PODS_FILES_REQUIRE_LOGIN ) && ( !is_user_logged_in() || !current_user_can( PODS_FILES_REQUIRE_LOGIN ) ) ) )
  220. ) {
  221. ?>
  222. <p>You do not have access to upload / browse files. Contact your website admin to resolve.</p>
  223. <?php
  224. return;
  225. }
  226. // Use plupload if attachment isn't available
  227. if ( 'attachment' == pods_var( 'file_uploader', $options ) && ( !is_user_logged_in() || ( !current_user_can( 'upload_files' ) && !current_user_can( 'edit_files' ) ) ) )
  228. $field_type = 'plupload';
  229. elseif ( 'plupload' == pods_var( 'file_uploader', $options ) )
  230. $field_type = 'plupload';
  231. elseif ( 'attachment' == pods_var( 'file_uploader', $options ) ) {
  232. if ( !pods_wp_version( '3.5' ) || !is_admin() || !in_array( $GLOBALS[ 'pagenow' ], array( 'post.php', 'post-new.php' ) ) )
  233. $field_type = 'attachment';
  234. else
  235. $field_type = 'media';
  236. }
  237. else {
  238. // Support custom File Uploader integration
  239. do_action( 'pods_form_ui_field_file_uploader_' . pods_var( 'file_uploader', $options ), $name, $value, $options, $pod, $id );
  240. do_action( 'pods_form_ui_field_file_uploader', pods_var( 'file_uploader', $options ), $name, $value, $options, $pod, $id );
  241. return;
  242. }
  243. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
  244. }
  245. /**
  246. * Build regex necessary for JS validation
  247. *
  248. * @param mixed $value
  249. * @param string $name
  250. * @param array $options
  251. * @param string $pod
  252. * @param int $id
  253. *
  254. * @return bool
  255. * @since 2.0.0
  256. */
  257. public function regex ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
  258. return false;
  259. }
  260. /**
  261. * Validate a value before it's saved
  262. *
  263. * @param mixed $value
  264. * @param string $name
  265. * @param array $options
  266. * @param array $fields
  267. * @param array $pod
  268. * @param int $id
  269. * @param null $params
  270. *
  271. * @return bool
  272. * @since 2.0.0
  273. */
  274. public function validate ( &$value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
  275. // check file size
  276. // check file extensions
  277. return true;
  278. }
  279. /**
  280. * Change the value or perform actions after validation but before saving to the DB
  281. *
  282. * @param mixed $value
  283. * @param int $id
  284. * @param string $name
  285. * @param array $options
  286. * @param array $fields
  287. * @param array $pod
  288. * @param object $params
  289. *
  290. * @return mixed
  291. * @since 2.0.0
  292. */
  293. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
  294. return $value;
  295. }
  296. /**
  297. * Perform actions after saving to the DB
  298. *
  299. * @param mixed $value
  300. * @param int $id
  301. * @param string $name
  302. * @param array $options
  303. * @param array $fields
  304. * @param array $pod
  305. * @param object $params
  306. *
  307. * @since 2.0.0
  308. */
  309. public function post_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
  310. }
  311. /**
  312. * Perform actions before deleting from the DB
  313. *
  314. * @param int $id
  315. * @param string $name
  316. * @param null $options
  317. * @param string $pod
  318. * @return void
  319. *
  320. * @since 2.0.0
  321. */
  322. public function pre_delete ( $id = null, $name = null, $options = null, $pod = null ) {
  323. }
  324. /**
  325. * Perform actions after deleting from the DB
  326. *
  327. * @param int $id
  328. * @param string $name
  329. * @param array $options
  330. * @param array $pod
  331. *
  332. * @since 2.0.0
  333. */
  334. public function post_delete ( $id = null, $name = null, $options = null, $pod = null ) {
  335. }
  336. /**
  337. * Customize the Pods UI manage table column output
  338. *
  339. * @param int $id
  340. * @param mixed $value
  341. * @param string $name
  342. * @param array $options
  343. * @param array $fields
  344. * @param array $pod
  345. *
  346. * @return mixed|void
  347. * @since 2.0.0
  348. */
  349. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
  350. if ( empty( $value ) )
  351. return;
  352. if ( !empty( $value ) && isset( $value[ 'ID' ] ) )
  353. $value = array( $value );
  354. foreach ( $value as $v ) {
  355. echo pods_image( $v, 'thumbnail' );
  356. }
  357. }
  358. /**
  359. * Handle file row output for uploaders
  360. *
  361. * @param array $attributes
  362. * @param int $limit
  363. * @param bool $editable
  364. * @param int $id
  365. * @param string $icon
  366. * @param string $name
  367. *
  368. * @return string
  369. * @since 2.0.0
  370. */
  371. public function markup ( $attributes, $limit = 1, $editable = true, $id = null, $icon = null, $name = null ) {
  372. // Preserve current file type
  373. $field_type = PodsForm::$field_type;
  374. ob_start();
  375. if ( empty ( $id ) )
  376. $id = '{{id}}';
  377. if ( empty ( $icon ) )
  378. $icon = '{{icon}}';
  379. if ( empty( $name ) )
  380. $name = '{{name}}';
  381. $editable = (boolean) $editable;
  382. ?>
  383. <li class="pods-file hidden" id="pods-file-<?php echo $id ?>">
  384. <?php echo PodsForm::field( $attributes[ 'name' ] . '[' . $id . '][id]', $id, 'hidden' ); ?>
  385. <ul class="pods-file-meta media-item">
  386. <?php if ( 1 != $limit ) { ?>
  387. <li class="pods-file-col pods-file-handle">Handle</li>
  388. <?php } ?>
  389. <li class="pods-file-col pods-file-icon">
  390. <img class="pinkynail" src="<?php echo $icon; ?>" alt="Icon" />
  391. </li>
  392. <li class="pods-file-col pods-file-name">
  393. <?php
  394. if ( $editable )
  395. echo PodsForm::field( $attributes[ 'name' ] . '[' . $id . '][title]', $name, 'text' );
  396. else
  397. echo ( empty( $name ) ? '{{name}}' : $name );
  398. ?>
  399. </li>
  400. <li class="pods-file-col pods-file-delete">Delete</li>
  401. </ul>
  402. </li>
  403. <?php
  404. PodsForm::$field_type = $field_type;
  405. return ob_get_clean();
  406. }
  407. }