PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wr-contactform/libraries/form/field.php

https://gitlab.com/redring-co-in/redring_website
PHP | 298 lines | 120 code | 43 blank | 135 comment | 24 complexity | 9eecece58366f0a25677cd45dd98fe20 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package WR_Library
  5. * @author WooRockets Team <support@woorockets.com>
  6. * @copyright Copyright (C) 2012 WooRockets.com. All Rights Reserved.
  7. * @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
  8. *
  9. * Websites: http://www.woorockets.com
  10. */
  11. /**
  12. * Form field renderer.
  13. *
  14. * @package WR_Library
  15. * @since 1.0.0
  16. */
  17. class WR_CF_Form_Field {
  18. /**
  19. * Array of instantiated object.
  20. *
  21. * @var array
  22. */
  23. protected static $instances = array();
  24. /**
  25. * Field type.
  26. *
  27. * @var string
  28. */
  29. protected $type = 'text';
  30. /**
  31. * Field identification.
  32. *
  33. * @var string
  34. */
  35. protected $id = null;
  36. /**
  37. * Field name.
  38. *
  39. * @var string
  40. */
  41. protected $name = null;
  42. /**
  43. * Field label.
  44. *
  45. * @var string
  46. */
  47. protected $label = null;
  48. /**
  49. * Field description.
  50. *
  51. * @var string
  52. */
  53. protected $desc = null;
  54. /**
  55. * Field placeholder.
  56. *
  57. * @var string
  58. */
  59. protected $placeholder = null;
  60. /**
  61. * Default value.
  62. *
  63. * @var mixed
  64. */
  65. protected $default = null;
  66. /**
  67. * Current field value.
  68. *
  69. * @var mixed
  70. */
  71. protected $value = null;
  72. /**
  73. * Additional HTML attributes for input field element.
  74. *
  75. * @var array
  76. */
  77. protected $attributes = array(
  78. 'autocomplete' => 'off',
  79. 'class' => 'form-control',
  80. );
  81. /**
  82. * Text domain.
  83. *
  84. * @var string
  85. */
  86. protected $text_domain = WR_CF_LIBRARY_TEXTDOMAIN;
  87. /**
  88. * Instantiate a form field object.
  89. *
  90. * @param array $config Field declaration.
  91. * @param array $merge Array of property should be merged.
  92. *
  93. * @return WR_CF_Form_Field
  94. */
  95. public static function get_instance( $config, $merge = null ) {
  96. // Generate object id
  97. $id = md5( json_encode( array( $config, $merge ) ) );
  98. if ( ! isset( self::$instances[$id] ) ) {
  99. $class = __CLASS__;
  100. if ( isset( $config['type'] ) ) {
  101. // Generate name of form field class to be instantiated
  102. $class = explode( '-', $config['type'] );
  103. $class = array_map( 'ucfirst', $class );
  104. $class = __CLASS__ . '_' . implode( '_', $class );
  105. // Try to auto-load the class
  106. class_exists( $class, true ) || $class = __CLASS__;
  107. }
  108. // Instantiate a new field object
  109. if ( $merge && is_array( $merge ) ) {
  110. self::$instances[ $id ] = new $class( $config, $merge );
  111. } else {
  112. self::$instances[ $id ] = new $class( $config );
  113. }
  114. }
  115. return self::$instances[ $id ];
  116. }
  117. /**
  118. * Constructor.
  119. *
  120. * @param array $config Field declaration.
  121. * @param array $merge Array of property should be merged.
  122. *
  123. * @return void
  124. */
  125. public function __construct( $config, $merge = array( 'attributes' ) ) {
  126. $props = array_keys( get_object_vars( $this ) );
  127. foreach ( $config as $key => $value ) {
  128. if ( in_array( $key, $props ) ) {
  129. $this->{$key} = in_array( $key, $merge ) ? array_merge( $this->{$key}, ( array ) $value ) : $value;
  130. }
  131. }
  132. if ( is_null( $this->value ) && ! is_null( $this->default ) ) {
  133. $this->value = $this->default;
  134. }
  135. }
  136. /**
  137. * Get value for a property.
  138. *
  139. * @param string $prop Property to get value for.
  140. * @param mixed $default Default value if property is not set.
  141. * @param boolean $return Whether to return or print the value?
  142. *
  143. * @return mixed
  144. */
  145. public function get( $prop, $default = null, $return = false ) {
  146. if ( method_exists( $this, $prop ) ) {
  147. // Prepare arguments
  148. $args = func_get_args();
  149. // Remove first argument
  150. array_shift( $args );
  151. $default = call_user_func_array( array( $this, $prop ), $args );
  152. } elseif ( array_key_exists( $prop, get_object_vars( $this ) ) ) {
  153. $default = $this->{$prop};
  154. }
  155. if ( $return ) {
  156. return $default;
  157. } else {
  158. if ( preg_match( '/<[^\>]+>/', $default ) ) {
  159. echo '' . $default;
  160. } else {
  161. esc_attr_e( $default, $this->text_domain );
  162. }
  163. }
  164. }
  165. /**
  166. * Generate HTML element id from field name.
  167. *
  168. * @return string
  169. */
  170. protected function id() {
  171. if ( ! isset( $this->id ) ) {
  172. $this->id = isset( $this->name ) ? trim( preg_replace( '/[^a-zA-Z0-9\-_]+/', '_', $this->name ), '_' ) : null;
  173. }
  174. return $this->id;
  175. }
  176. /**
  177. * Translate label then return.
  178. *
  179. * @return string
  180. */
  181. protected function label() {
  182. return esc_html__( $this->label );
  183. }
  184. /**
  185. * Render input field for user to interact with.
  186. *
  187. * @param string $tpl Render field using this template.
  188. *
  189. * @return string
  190. */
  191. protected function input( $tpl = null ) {
  192. // Prepare field template
  193. if ( empty( $tpl ) ) {
  194. $tpl = $this->type;
  195. }
  196. // Preset HTML code
  197. $html = '';
  198. // Backup current field attributes to allow recursive call
  199. $backup = $this->attributes;
  200. // Get path to template file
  201. $file = WR_CF_Loader::get_path( "form/field/tmpl/{$tpl}.php" );
  202. if ( empty( $file ) ) {
  203. $file = WR_CF_Loader::get_path( 'form/field/tmpl/text.php' );
  204. }
  205. // Load field template
  206. ob_start();
  207. include $file;
  208. $html = ob_get_clean();
  209. // Restore field attributes
  210. $this->attributes = $backup;
  211. return $html;
  212. }
  213. /**
  214. * Generate HTML attributes.
  215. *
  216. * @param array $ignore Array of attribute should not be rendered.
  217. * @param boolean $return Whether to return or print the value?
  218. *
  219. * @return string
  220. */
  221. protected function html_attributes( $ignore = array(), $return = false ) {
  222. // Filter attributes
  223. $default = array(
  224. 'id' => $this->id(),
  225. 'name' => $this->name,
  226. 'value' => $this->value,
  227. 'placeholder' => $this->placeholder,
  228. );
  229. if ( in_array( $this->type, array( 'checkbox', 'number', 'password', 'radio', 'text' ) ) && ! isset( $this->attributes['type'] ) ) {
  230. $this->attributes['type'] = $this->type;
  231. }
  232. $attrs = apply_filters( 'wr_cf_form_field_attributes', array_merge( $default, $this->attributes ) );
  233. // Clean unnecessary attributes
  234. if ( ! is_array( $ignore ) ) {
  235. $ignore = array( $ignore );
  236. }
  237. if ( count( $ignore ) ) {
  238. foreach ( array_keys( $attrs ) as $key ) {
  239. if ( in_array( $key, $ignore ) ) {
  240. unset( $attrs[ $key ] );
  241. }
  242. }
  243. }
  244. // Generate HTML attributes
  245. $html = array();
  246. foreach ( $attrs as $key => $value ) {
  247. $html[] = esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
  248. }
  249. if ( $return ) {
  250. return implode( ' ', $html );
  251. } else {
  252. echo '' . implode( ' ', $html );
  253. }
  254. }
  255. }