PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/validation-rules/class.filesize.php

https://github.com/inc2734/mw-wp-form
PHP | 65 lines | 34 code | 3 blank | 28 comment | 8 complexity | 376203b7183afd1ffaf1625001f5f72d MD5 | raw file
  1. <?php
  2. /**
  3. * Name : MW WP Form Validation Rule FileSize
  4. * Description: ファイルサイズが指定したサイズ以内
  5. * Version : 1.1.0
  6. * Author : Takashi Kitajima
  7. * Author URI : http://2inc.org
  8. * Created : July 21, 2014
  9. * Modified : December 31, 2014
  10. * License : GPLv2
  11. * License URI: http://www.gnu.org/licenses/gpl-2.0.html
  12. */
  13. class MW_WP_Form_Validation_Rule_FileSize extends MW_WP_Form_Abstract_Validation_Rule {
  14. /**
  15. * バリデーションルール名を指定
  16. * @var string
  17. */
  18. protected $name = 'filesize';
  19. /**
  20. * バリデーションチェック
  21. *
  22. * @param string $key name属性
  23. * @param array $option
  24. * @return string エラーメッセージ
  25. */
  26. public function rule( $key, array $options = array() ) {
  27. $data = $this->Data->get_post_value_by_key( MWF_Config::UPLOAD_FILES );
  28. if ( !is_null( $data ) && is_array( $data ) && array_key_exists( $key, $data ) ) {
  29. $file = $data[$key];
  30. if ( !empty( $file['size'] ) ) {
  31. $defaults = array(
  32. 'bytes' => '0',
  33. 'message' => __( 'This file size is too big.', MWF_Config::DOMAIN )
  34. );
  35. $options = array_merge( $defaults, $options );
  36. if ( !( preg_match( '/^[\d]+$/', $options['bytes'] ) && $options['bytes'] >= $file['size'] ) ) {
  37. return $options['message'];
  38. }
  39. }
  40. }
  41. }
  42. /**
  43. * 設定パネルに追加
  44. *
  45. * @param numeric $key バリデーションルールセットの識別番号
  46. * @param array $value バリデーションルールセットの内容
  47. */
  48. public function admin( $key, $value ) {
  49. $bytes = '';
  50. if ( is_array( $value[$this->getName()] ) && isset( $value[$this->getName()]['bytes'] ) ) {
  51. $bytes = $value[$this->getName()]['bytes'];
  52. }
  53. ?>
  54. <table>
  55. <tr>
  56. <td><?php esc_html_e( 'Permitted file size', MWF_Config::DOMAIN ); ?></td>
  57. <td><input type="text" value="<?php echo esc_attr( $bytes ); ?>" name="<?php echo MWF_Config::NAME; ?>[validation][<?php echo $key; ?>][<?php echo esc_attr( $this->getName() ); ?>][bytes]" /> <span class="mwf_note"><?php esc_html_e( 'bytes', MWF_Config::DOMAIN ); ?></span></td>
  58. </tr>
  59. </table>
  60. <?php
  61. }
  62. }