/includes/skip/elements/forms/form.php

  1. <?php 
  2. /** 
  3. * Skip Form 
  4. * @package Skip\Forms 
  5. * @since 1.0 
  6. * @ignore 
  7. */ 
  8.  
  9. namespace skip\v1_0_0; 
  10.  
  11. class Form extends HTML_Element{ 
  12.  
  13. var $option_group; 
  14.  
  15. /** 
  16. * WP Form constructor 
  17. * 
  18. * @package Skip 
  19. * @since 1.0 
  20. *  
  21. * @param string $name The name of the form 
  22. * @param string $args Array of [ 'id' ], [ 'classes' ], [ 'before_element' ], [ 'after_element' ] and [ 'params' ] 
  23. */ 
  24. function __construct( $name, $args = array() ) { 
  25. global $skip_form_name; 
  26.  
  27. /** 
  28. * Additional parent args: 
  29. * 'id' 
  30. * 'classes' 
  31. * 'before_element' 
  32. * 'after_element' 
  33. * 'params' 
  34. */ 
  35. $defaults = array( 
  36. 'enctype' => 'multipart/form-data',  
  37. 'classes' => 'skip_form' 
  38. ); 
  39.  
  40. $args = wp_parse_args( $args, $defaults ); 
  41. extract( $args, EXTR_SKIP ); 
  42.  
  43. if( '' == $name ) 
  44. return; 
  45.  
  46. $method = 'POST'; 
  47. $action = $_SERVER[ 'REQUEST_URI' ]; 
  48.  
  49. $args[ 'close_tag' ] = TRUE; 
  50.  
  51. parent::__construct( 'form', $args ); 
  52.  
  53. $skip_form_name = $name; 
  54. $this->add_param( 'name', $name ); 
  55.  
  56. if( '' != $method ) 
  57. $this->add_param( 'method', $method ); 
  58.  
  59. if( '' != $action ) 
  60. $this->add_param( 'action', $action ); 
  61.  
  62. if( '' != $enctype ) 
  63. $this->add_param( 'enctype', $enctype ); 
  64.  
  65. // Needed Fields for Form 
  66. $needed_fields = wp_nonce_field( 'skip_form_' . $name, $name . '_wpnonce', TRUE , FALSE ) ; 
  67. $needed_fields.= '<input type="hidden" name="MAX_FILE_SIZE" value="' . max_upload() . '" />'; 
  68.  
  69. $this->add_element( $needed_fields ); 
  70.  
  71. /** 
  72. * Skip start form function 
  73. *  
  74. * Use it if you don't want to put all in the content variable 
  75. * 
  76. * @package Skip\Forms 
  77. * @since 1.0 
  78. * @param string $name The name of the form 
  79. * @param string $args Array of [ 'id' ], [ 'classes' ], [ 'before_element' ], [ 'after_element' ] and [ 'params' ] 
  80. * @param string $return How to return 'echo', 'object' or 'html' 
  81. */ 
  82. function form_start( $name, $args = array(), $return = 'echo' ) { 
  83. global $skip_form, $skip_form_args, $skip_form_return; 
  84.  
  85. $skip_form = new Form( $name, $args ); 
  86.  
  87. $skip_form_return = $return; 
  88.  
  89. ob_start( __NAMESPACE__ . '\form_content_buffer' ); 
  90. /** 
  91. * Skip end form function 
  92. *  
  93. * Use it if you don't want to put all in the content variable 
  94. * 
  95. * @package Skip\Forms 
  96. * @since 1.0 
  97. * @param string $return How to return 'echo', 'object' or 'html' 
  98. */ 
  99. function form_end() { 
  100. global $skip_form, $skip_form_buffer, $skip_form_return, $skip_form_save_fields; 
  101. ob_end_flush();  
  102.  
  103. $skip_form->add_element( $skip_form_buffer ); 
  104.  
  105. // Saving form field names for early saving fields 
  106. if( is_array( $skip_form_save_fields ) ) 
  107. update_option( 'skip_form_save_field_array', $skip_form_save_fields ); 
  108.  
  109. echo $skip_form->render(); 
  110.  
  111. /** 
  112. * Skip form content buffer for internal use 
  113. *  
  114. * @package Skip 
  115. * @since 1.0 
  116. * @param string $content Content to buffer 
  117. * @ignore 
  118. */ 
  119. function form_content_buffer( $content ) { 
  120. global $skip_form_buffer; 
  121. $skip_form_buffer = $content; 
  122.  
  123. /** 
  124. * Skip form save function 
  125. *  
  126. * @package Skip 
  127. * @since 1.0 
  128. * @ignore 
  129. */ 
  130. function form_save_fields() { 
  131. global $wpdb; 
  132.  
  133. $skip_form_save_fields = get_option( 'skip_form_save_field_array' ); 
  134.  
  135. if( is_array( $skip_form_save_fields ) ): // If there is any data to save 
  136. foreach( $skip_form_save_fields AS $form_name => $form_fields ): // Getting form name and fields of form 
  137. if( array_key_exists( $form_name . '_value', $_POST ) ): // If there was posted something 
  138. if ( wp_verify_nonce( $_POST[ $form_name . '_wpnonce' ], 'skip_form_' . $form_name ) ): // Verifying form 
  139.  
  140. // Running all fields 
  141. foreach( $form_fields AS $field ): 
  142. // If field name is in _POST data 
  143. if( array_key_exists( $field['name'], $_POST[ $form_name . '_value' ] ) ): 
  144. $new_value = ''; 
  145.  
  146. $value = $_POST[ $form_name . '_value' ][ $field[ 'name' ] ]; 
  147.  
  148. // Getting Value if it's an array 
  149. if( is_array( $value ) && '' != $field[ 'array' ] ): 
  150. $new_value = _cleanup_value( $value, $field[ 'array' ] ); 
  151. elseif ( !is_array( $value ) ): 
  152. $new_value = $value; 
  153. endif; 
  154.  
  155. // Saving if there is a value to save 
  156. if( '' != $new_value ): 
  157. update_option( $field[ 'option_name' ], $new_value ); 
  158. else: 
  159. delete_option( $field[ 'option_name' ] ); 
  160. endif; 
  161. else: 
  162. delete_option( $field[ 'option_name' ] ); 
  163. endif; 
  164. endforeach; 
  165. endif; 
  166. endif; 
  167.  
  168. endforeach; 
  169.  
  170. delete_option( 'skip_form_save_fields' ); 
  171.  
  172. endif; 
  173.  
  174. add_action( 'init', __NAMESPACE__ . '\form_save_fields' ); 
  175.  
  176. /** 
  177. * _cleanup_value() 
  178. * 
  179. * Returns the pure value of value. 
  180. * 
  181. * @package Skip\Tools 
  182. * @param mixed $value The value got from DB 
  183. * @param array $array The array structure 
  184. * @param int $i The key of the array from array structure 
  185. * @since 1.0 
  186. * @ignore 
  187. */ 
  188. function _cleanup_value( $value, $array, $i = 0 ) { 
  189. if( is_array( $array ) ) 
  190. $index = $array[ $i ]; 
  191. else 
  192. $index = $array; 
  193.  
  194. if( is_array( $value[ $index ] ) ): 
  195. return _cleanup_value( $value[ $index ], $array, $i+1 ); 
  196. else: 
  197. return $value[ $index ]; 
  198. endif; 
.