validation.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. class WPCF7_Validation implements ArrayAccess {
  3. private $invalid_fields = array();
  4. private $container = array();
  5. public function __construct() {
  6. $this->container = array(
  7. 'valid' => true,
  8. 'reason' => array(),
  9. 'idref' => array(),
  10. );
  11. }
  12. public function invalidate( $context, $message ) {
  13. if ( $context instanceof WPCF7_FormTag ) {
  14. $tag = $context;
  15. } elseif ( is_array( $context ) ) {
  16. $tag = new WPCF7_FormTag( $context );
  17. } elseif ( is_string( $context ) ) {
  18. $tags = wpcf7_scan_form_tags( array( 'name' => trim( $context ) ) );
  19. $tag = $tags ? new WPCF7_FormTag( $tags[0] ) : null;
  20. }
  21. $name = ! empty( $tag ) ? $tag->name : null;
  22. if ( empty( $name )
  23. or ! wpcf7_is_name( $name ) ) {
  24. return;
  25. }
  26. if ( $this->is_valid( $name ) ) {
  27. $id = $tag->get_id_option();
  28. if ( empty( $id )
  29. or ! wpcf7_is_name( $id ) ) {
  30. $id = null;
  31. }
  32. $this->invalid_fields[$name] = array(
  33. 'reason' => (string) $message,
  34. 'idref' => $id,
  35. );
  36. }
  37. }
  38. public function is_valid( $name = null ) {
  39. if ( ! empty( $name ) ) {
  40. return ! isset( $this->invalid_fields[$name] );
  41. } else {
  42. return empty( $this->invalid_fields );
  43. }
  44. }
  45. public function get_invalid_fields() {
  46. return $this->invalid_fields;
  47. }
  48. public function offsetSet( $offset, $value ) {
  49. if ( isset( $this->container[$offset] ) ) {
  50. $this->container[$offset] = $value;
  51. }
  52. if ( 'reason' == $offset
  53. and is_array( $value ) ) {
  54. foreach ( $value as $k => $v ) {
  55. $this->invalidate( $k, $v );
  56. }
  57. }
  58. }
  59. public function offsetGet( $offset ) {
  60. if ( isset( $this->container[$offset] ) ) {
  61. return $this->container[$offset];
  62. }
  63. }
  64. public function offsetExists( $offset ) {
  65. return isset( $this->container[$offset] );
  66. }
  67. public function offsetUnset( $offset ) {
  68. }
  69. }