NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. <?php
  2. /**
  3. * Data-aware form generator.
  4. */
  5. class scbForms {
  6. const TOKEN = '%input%';
  7. /**
  8. * Generates form field.
  9. *
  10. * @param array|scbFormField_I $args
  11. * @param mixed $value
  12. *
  13. * @return string
  14. */
  15. public static function input_with_value( $args, $value ) {
  16. $field = scbFormField::create( $args );
  17. return $field->render( $value );
  18. }
  19. /**
  20. * Generates form field.
  21. *
  22. * @param array|scbFormField_I $args
  23. * @param array $formdata (optional)
  24. *
  25. * @return string
  26. */
  27. public static function input( $args, $formdata = null ) {
  28. $field = scbFormField::create( $args );
  29. return $field->render( scbForms::get_value( $args['name'], $formdata ) );
  30. }
  31. /**
  32. * Generates a table wrapped in a form.
  33. *
  34. * @param array $rows
  35. * @param array $formdata (optional)
  36. *
  37. * @return string
  38. */
  39. public static function form_table( $rows, $formdata = null ) {
  40. $output = '';
  41. foreach ( $rows as $row ) {
  42. $output .= self::table_row( $row, $formdata );
  43. }
  44. $output = self::form_table_wrap( $output );
  45. return $output;
  46. }
  47. /**
  48. * Generates a form.
  49. *
  50. * @param array $inputs
  51. * @param array $formdata (optional)
  52. * @param string $nonce
  53. *
  54. * @return string
  55. */
  56. public static function form( $inputs, $formdata = null, $nonce ) {
  57. $output = '';
  58. foreach ( $inputs as $input ) {
  59. $output .= self::input( $input, $formdata );
  60. }
  61. $output = self::form_wrap( $output, $nonce );
  62. return $output;
  63. }
  64. /**
  65. * Generates a table.
  66. *
  67. * @param array $rows
  68. * @param array $formdata (optional)
  69. *
  70. * @return string
  71. */
  72. public static function table( $rows, $formdata = null ) {
  73. $output = '';
  74. foreach ( $rows as $row ) {
  75. $output .= self::table_row( $row, $formdata );
  76. }
  77. $output = self::table_wrap( $output );
  78. return $output;
  79. }
  80. /**
  81. * Generates a table row.
  82. *
  83. * @param array $args
  84. * @param array $formdata (optional)
  85. *
  86. * @return string
  87. */
  88. public static function table_row( $args, $formdata = null ) {
  89. return self::row_wrap( $args['title'], self::input( $args, $formdata ) );
  90. }
  91. // ____________WRAPPERS____________
  92. /**
  93. * Wraps a table in a form.
  94. *
  95. * @param string $content
  96. * @param string $nonce (optional)
  97. *
  98. * @return string
  99. */
  100. public static function form_table_wrap( $content, $nonce = 'update_options' ) {
  101. return self::form_wrap( self::table_wrap( $content ), $nonce );
  102. }
  103. /**
  104. * Wraps a content in a form.
  105. *
  106. * @param string $content
  107. * @param string $nonce (optional)
  108. *
  109. * @return string
  110. */
  111. public static function form_wrap( $content, $nonce = 'update_options' ) {
  112. return html( "form method='post' action=''",
  113. $content,
  114. wp_nonce_field( $nonce, '_wpnonce', $referer = true, $echo = false )
  115. );
  116. }
  117. /**
  118. * Wraps a content in a table.
  119. *
  120. * @param string $content
  121. *
  122. * @return string
  123. */
  124. public static function table_wrap( $content ) {
  125. return html( "table class='form-table'", $content );
  126. }
  127. /**
  128. * Wraps a content in a table row.
  129. *
  130. * @param string $title
  131. * @param string $content
  132. *
  133. * @return string
  134. */
  135. public static function row_wrap( $title, $content ) {
  136. return html( 'tr',
  137. html( "th scope='row'", $title ),
  138. html( 'td', $content )
  139. );
  140. }
  141. // ____________PRIVATE METHODS____________
  142. // Utilities
  143. /**
  144. * Generates the proper string for a name attribute.
  145. *
  146. * @param array|string $name The raw name
  147. *
  148. * @return string
  149. */
  150. public static function get_name( $name ) {
  151. $name = (array) $name;
  152. $name_str = array_shift( $name );
  153. foreach ( $name as $key ) {
  154. $name_str .= '[' . esc_attr( $key ) . ']';
  155. }
  156. return $name_str;
  157. }
  158. /**
  159. * Traverses the formdata and retrieves the correct value.
  160. *
  161. * @param string $name The name of the value
  162. * @param array $value The data that will be traversed
  163. * @param mixed $fallback (optional) The value returned when the key is not found
  164. *
  165. * @return mixed
  166. */
  167. public static function get_value( $name, $value, $fallback = null ) {
  168. foreach ( (array) $name as $key ) {
  169. if ( ! isset( $value[ $key ] ) ) {
  170. return $fallback;
  171. }
  172. $value = $value[ $key ];
  173. }
  174. return $value;
  175. }
  176. /**
  177. * Given a list of fields, validate some data.
  178. *
  179. * @param array $fields List of args that would be sent to scbForms::input()
  180. * @param array $data (optional) The data to validate. Defaults to $_POST
  181. * @param array $to_update (optional) Existing data to populate. Necessary for nested values
  182. *
  183. * @return array
  184. */
  185. public static function validate_post_data( $fields, $data = null, $to_update = array() ) {
  186. if ( null === $data ) {
  187. $data = stripslashes_deep( $_POST );
  188. }
  189. foreach ( $fields as $field ) {
  190. $value = scbForms::get_value( $field['name'], $data );
  191. $fieldObj = scbFormField::create( $field );
  192. $value = $fieldObj->validate( $value );
  193. if ( null !== $value ) {
  194. self::set_value( $to_update, $field['name'], $value );
  195. }
  196. }
  197. return $to_update;
  198. }
  199. /**
  200. * For multiple-choice fields, we can never distinguish between "never been set" and "set to none".
  201. * For single-choice fields, we can't distinguish either, because of how self::update_meta() works.
  202. * Therefore, the 'default' parameter is always ignored.
  203. *
  204. * @param array $args Field arguments.
  205. * @param int $object_id The object ID the metadata is attached to
  206. * @param string $meta_type (optional)
  207. *
  208. * @return string
  209. */
  210. public static function input_from_meta( $args, $object_id, $meta_type = 'post' ) {
  211. $single = ( 'checkbox' != $args['type'] );
  212. $key = (array) $args['name'];
  213. $key = end( $key );
  214. $value = get_metadata( $meta_type, $object_id, $key, $single );
  215. return self::input_with_value( $args, $value );
  216. }
  217. /**
  218. * Updates metadata for passed list of fields.
  219. *
  220. * @param array $fields
  221. * @param array $data
  222. * @param int $object_id The object ID the metadata is attached to
  223. * @param string $meta_type (optional) Defaults to 'post'
  224. *
  225. * @return void
  226. */
  227. public static function update_meta( $fields, $data, $object_id, $meta_type = 'post' ) {
  228. foreach ( $fields as $field_args ) {
  229. $key = $field_args['name'];
  230. if ( 'checkbox' == $field_args['type'] ) {
  231. $new_values = isset( $data[ $key ] ) ? $data[ $key ] : array();
  232. $old_values = get_metadata( $meta_type, $object_id, $key );
  233. foreach ( array_diff( $new_values, $old_values ) as $value ) {
  234. add_metadata( $meta_type, $object_id, $key, $value );
  235. }
  236. foreach ( array_diff( $old_values, $new_values ) as $value ) {
  237. delete_metadata( $meta_type, $object_id, $key, $value );
  238. }
  239. } else {
  240. $value = isset( $data[ $key ] ) ? $data[ $key ] : '';
  241. if ( '' === $value ) {
  242. delete_metadata( $meta_type, $object_id, $key );
  243. } else {
  244. update_metadata( $meta_type, $object_id, $key, $value );
  245. }
  246. }
  247. }
  248. }
  249. /**
  250. * Sets value using a reference.
  251. *
  252. * @param array $arr
  253. * @param string $name
  254. * @param mixed $value
  255. *
  256. * @return void
  257. */
  258. private static function set_value( &$arr, $name, $value ) {
  259. $name = (array) $name;
  260. $final_key = array_pop( $name );
  261. while ( ! empty( $name ) ) {
  262. $key = array_shift( $name );
  263. if ( ! isset( $arr[ $key ] ) ) {
  264. $arr[ $key ] = array();
  265. }
  266. $arr =& $arr[ $key ];
  267. }
  268. $arr[ $final_key ] = $value;
  269. }
  270. }
  271. /**
  272. * A wrapper for scbForms, containing the formdata.
  273. */
  274. class scbForm {
  275. protected $data = array();
  276. protected $prefix = array();
  277. /**
  278. * Constructor.
  279. *
  280. * @param array $data
  281. * @param string|boolean $prefix (optional)
  282. *
  283. * @return void
  284. */
  285. public function __construct( $data, $prefix = false ) {
  286. if ( is_array( $data ) ) {
  287. $this->data = $data;
  288. }
  289. if ( $prefix ) {
  290. $this->prefix = (array) $prefix;
  291. }
  292. }
  293. /**
  294. * Traverses the form.
  295. *
  296. * @param string $path
  297. *
  298. * @return object A scbForm
  299. */
  300. public function traverse_to( $path ) {
  301. $data = scbForms::get_value( $path, $this->data );
  302. $prefix = array_merge( $this->prefix, (array) $path );
  303. return new scbForm( $data, $prefix );
  304. }
  305. /**
  306. * Generates form field.
  307. *
  308. * @param array $args
  309. *
  310. * @return string
  311. */
  312. public function input( $args ) {
  313. $value = scbForms::get_value( $args['name'], $this->data );
  314. if ( ! empty( $this->prefix ) ) {
  315. $args['name'] = array_merge( $this->prefix, (array) $args['name'] );
  316. }
  317. return scbForms::input_with_value( $args, $value );
  318. }
  319. }
  320. /**
  321. * Interface for form fields.
  322. */
  323. interface scbFormField_I {
  324. /**
  325. * Generate the corresponding HTML for a field.
  326. *
  327. * @param mixed $value (optional) The value to use.
  328. *
  329. * @return string
  330. */
  331. function render( $value = null );
  332. /**
  333. * Validates a value against a field.
  334. *
  335. * @param mixed $value The value to check.
  336. *
  337. * @return mixed null if the validation failed, sanitized value otherwise.
  338. */
  339. function validate( $value );
  340. }
  341. /**
  342. * Base class for form fields implementations.
  343. */
  344. abstract class scbFormField implements scbFormField_I {
  345. protected $args;
  346. /**
  347. * Creates form field.
  348. *
  349. * @param array|scbFormField_I $args
  350. *
  351. * @return mixed false on failure or instance of form class
  352. */
  353. public static function create( $args ) {
  354. if ( is_a( $args, 'scbFormField_I' ) ) {
  355. return $args;
  356. }
  357. if ( empty( $args['name'] ) ) {
  358. return trigger_error( 'Empty name', E_USER_WARNING );
  359. }
  360. if ( isset( $args['value'] ) && is_array( $args['value'] ) ) {
  361. $args['choices'] = $args['value'];
  362. unset( $args['value'] );
  363. }
  364. if ( isset( $args['values'] ) ) {
  365. $args['choices'] = $args['values'];
  366. unset( $args['values'] );
  367. }
  368. if ( isset( $args['extra'] ) && ! is_array( $args['extra'] ) ) {
  369. $args['extra'] = shortcode_parse_atts( $args['extra'] );
  370. }
  371. $args = wp_parse_args( $args, array(
  372. 'desc' => '',
  373. 'desc_pos' => 'after',
  374. 'wrap' => scbForms::TOKEN,
  375. 'wrap_each' => scbForms::TOKEN,
  376. ) );
  377. // depends on $args['desc']
  378. if ( isset( $args['choices'] ) ) {
  379. self::_expand_choices( $args );
  380. }
  381. switch ( $args['type'] ) {
  382. case 'radio':
  383. return new scbRadiosField( $args );
  384. case 'select':
  385. return new scbSelectField( $args );
  386. case 'checkbox':
  387. if ( isset( $args['choices'] ) ) {
  388. return new scbMultipleChoiceField( $args );
  389. } else {
  390. return new scbSingleCheckboxField( $args );
  391. }
  392. case 'custom':
  393. return new scbCustomField( $args );
  394. default:
  395. return new scbTextField( $args );
  396. }
  397. }
  398. /**
  399. * Constructor.
  400. *
  401. * @param array $args
  402. *
  403. * @return void
  404. */
  405. protected function __construct( $args ) {
  406. $this->args = $args;
  407. }
  408. /**
  409. * Magic method: $field->arg
  410. *
  411. * @param string $key
  412. *
  413. * @return mixed
  414. */
  415. public function __get( $key ) {
  416. return $this->args[ $key ];
  417. }
  418. /**
  419. * Magic method: isset( $field->arg )
  420. *
  421. * @param string $key
  422. *
  423. * @return bool
  424. */
  425. public function __isset( $key ) {
  426. return isset( $this->args[ $key ] );
  427. }
  428. /**
  429. * Generate the corresponding HTML for a field.
  430. *
  431. * @param mixed $value (optional)
  432. *
  433. * @return string
  434. */
  435. public function render( $value = null ) {
  436. if ( null === $value && isset( $this->default ) ) {
  437. $value = $this->default;
  438. }
  439. $args = $this->args;
  440. if ( null !== $value ) {
  441. $this->_set_value( $args, $value );
  442. }
  443. $args['name'] = scbForms::get_name( $args['name'] );
  444. return str_replace( scbForms::TOKEN, $this->_render( $args ), $this->wrap );
  445. }
  446. /**
  447. * Mutate the field arguments so that the value passed is rendered.
  448. *
  449. * @param array $args
  450. * @param mixed $value
  451. */
  452. abstract protected function _set_value( &$args, $value );
  453. /**
  454. * The actual rendering.
  455. *
  456. * @param array $args
  457. */
  458. abstract protected function _render( $args );
  459. /**
  460. * Handle args for a single checkbox or radio input.
  461. *
  462. * @param array $args
  463. *
  464. * @return string
  465. */
  466. protected static function _checkbox( $args ) {
  467. $args = wp_parse_args( $args, array(
  468. 'value' => true,
  469. 'desc' => null,
  470. 'checked' => false,
  471. 'extra' => array(),
  472. ) );
  473. $args['extra']['checked'] = $args['checked'];
  474. if ( is_null( $args['desc'] ) && ! is_bool( $args['value'] ) ) {
  475. $args['desc'] = str_replace( '[]', '', $args['value'] );
  476. }
  477. return self::_input_gen( $args );
  478. }
  479. /**
  480. * Generate html with the final args.
  481. *
  482. * @param array $args
  483. *
  484. * @return string
  485. */
  486. protected static function _input_gen( $args ) {
  487. $args = wp_parse_args( $args, array(
  488. 'value' => null,
  489. 'desc' => null,
  490. 'extra' => array(),
  491. ) );
  492. $args['extra']['name'] = $args['name'];
  493. if ( 'textarea' == $args['type'] ) {
  494. $input = html( 'textarea', $args['extra'], esc_textarea( $args['value'] ) );
  495. } else {
  496. $args['extra']['value'] = $args['value'];
  497. $args['extra']['type'] = $args['type'];
  498. $input = html( 'input', $args['extra'] );
  499. }
  500. return self::add_label( $input, $args['desc'], $args['desc_pos'] );
  501. }
  502. /**
  503. * Wraps a form field in a label, and position field description.
  504. *
  505. * @param string $input
  506. * @param string $desc
  507. * @param string $desc_pos
  508. *
  509. * @return string
  510. */
  511. protected static function add_label( $input, $desc, $desc_pos ) {
  512. return html( 'label', self::add_desc( $input, $desc, $desc_pos ) ) . "\n";
  513. }
  514. /**
  515. * Adds description before/after the form field.
  516. *
  517. * @param string $input
  518. * @param string $desc
  519. * @param string $desc_pos
  520. *
  521. * @return string
  522. */
  523. protected static function add_desc( $input, $desc, $desc_pos ) {
  524. if ( empty( $desc ) ) {
  525. return $input;
  526. }
  527. if ( 'before' == $desc_pos ) {
  528. return $desc . ' ' . $input;
  529. } else {
  530. return $input . ' ' . $desc;
  531. }
  532. }
  533. /**
  534. * @param array $args
  535. */
  536. private static function _expand_choices( &$args ) {
  537. $choices =& $args['choices'];
  538. if ( ! empty( $choices ) && ! self::is_associative( $choices ) ) {
  539. if ( is_array( $args['desc'] ) ) {
  540. $choices = array_combine( $choices, $args['desc'] ); // back-compat
  541. $args['desc'] = false;
  542. } else if ( ! isset( $args['numeric'] ) || ! $args['numeric'] ) {
  543. $choices = array_combine( $choices, $choices );
  544. }
  545. }
  546. }
  547. /**
  548. * Checks if passed array is associative.
  549. *
  550. * @param array $array
  551. *
  552. * @return bool
  553. */
  554. private static function is_associative( $array ) {
  555. $keys = array_keys( $array );
  556. return array_keys( $keys ) !== $keys;
  557. }
  558. }
  559. /**
  560. * Text form field.
  561. */
  562. class scbTextField extends scbFormField {
  563. /**
  564. * Sanitizes value.
  565. *
  566. * @param string $value
  567. *
  568. * @return string
  569. */
  570. public function validate( $value ) {
  571. $sanitize = isset( $this->sanitize ) ? $this->sanitize : 'wp_kses_data';
  572. return call_user_func( $sanitize, $value, $this );
  573. }
  574. /**
  575. * Generate the corresponding HTML for a field.
  576. *
  577. * @param array $args
  578. *
  579. * @return string
  580. */
  581. protected function _render( $args ) {
  582. $args = wp_parse_args( $args, array(
  583. 'value' => '',
  584. 'desc_pos' => 'after',
  585. 'extra' => array( 'class' => 'regular-text' ),
  586. ) );
  587. if ( ! isset( $args['extra']['id'] ) && ! is_array( $args['name'] ) && false === strpos( $args['name'], '[' ) ) {
  588. $args['extra']['id'] = $args['name'];
  589. }
  590. return scbFormField::_input_gen( $args );
  591. }
  592. /**
  593. * Sets value using a reference.
  594. *
  595. * @param array $args
  596. * @param string $value
  597. *
  598. * @return void
  599. */
  600. protected function _set_value( &$args, $value ) {
  601. $args['value'] = $value;
  602. }
  603. }
  604. /**
  605. * Base class for form fields with single choice.
  606. */
  607. abstract class scbSingleChoiceField extends scbFormField {
  608. /**
  609. * Validates a value against a field.
  610. *
  611. * @param mixed $value
  612. *
  613. * @return mixed|null
  614. */
  615. public function validate( $value ) {
  616. if ( isset( $this->choices[ $value ] ) ) {
  617. return $value;
  618. }
  619. return null;
  620. }
  621. /**
  622. * Generate the corresponding HTML for a field.
  623. *
  624. * @param array $args
  625. *
  626. * @return string
  627. */
  628. protected function _render( $args ) {
  629. $args = wp_parse_args( $args, array(
  630. 'numeric' => false, // use numeric array instead of associative
  631. ) );
  632. if ( isset( $args['selected'] ) ) {
  633. $args['selected'] = (string) $args['selected'];
  634. } else {
  635. $args['selected'] = array( 'foo' ); // hack to make default blank
  636. }
  637. return $this->_render_specific( $args );
  638. }
  639. /**
  640. * Sets value using a reference.
  641. *
  642. * @param array $args
  643. * @param string $value
  644. *
  645. * @return void
  646. */
  647. protected function _set_value( &$args, $value ) {
  648. $args['selected'] = $value;
  649. }
  650. /**
  651. * Generate the corresponding HTML for a field.
  652. *
  653. * @param array $args
  654. *
  655. * @return string
  656. */
  657. abstract protected function _render_specific( $args );
  658. }
  659. /**
  660. * Dropdown field.
  661. */
  662. class scbSelectField extends scbSingleChoiceField {
  663. /**
  664. * Generate the corresponding HTML for a field.
  665. *
  666. * @param array $args
  667. *
  668. * @return string
  669. */
  670. protected function _render_specific( $args ) {
  671. $args = wp_parse_args( $args, array(
  672. 'text' => false,
  673. 'extra' => array(),
  674. ) );
  675. $options = array();
  676. if ( false !== $args['text'] ) {
  677. $options[] = array(
  678. 'value' => '',
  679. 'selected' => ( $args['selected'] === array( 'foo' ) ),
  680. 'title' => $args['text'],
  681. );
  682. }
  683. foreach ( $args['choices'] as $value => $title ) {
  684. $value = (string) $value;
  685. $options[] = array(
  686. 'value' => $value,
  687. 'selected' => ( $value == $args['selected'] ),
  688. 'title' => $title,
  689. );
  690. }
  691. $opts = '';
  692. foreach ( $options as $option ) {
  693. $opts .= html( 'option', array( 'value' => $option['value'], 'selected' => $option['selected'] ), $option['title'] );
  694. }
  695. $args['extra']['name'] = $args['name'];
  696. $input = html( 'select', $args['extra'], $opts );
  697. return scbFormField::add_label( $input, $args['desc'], $args['desc_pos'] );
  698. }
  699. }
  700. /**
  701. * Radio field.
  702. */
  703. class scbRadiosField extends scbSelectField {
  704. /**
  705. * Generate the corresponding HTML for a field.
  706. *
  707. * @param array $args
  708. *
  709. * @return string
  710. */
  711. protected function _render_specific( $args ) {
  712. if ( array( 'foo' ) === $args['selected'] ) {
  713. // radio buttons should always have one option selected
  714. $args['selected'] = key( $args['choices'] );
  715. }
  716. $opts = '';
  717. foreach ( $args['choices'] as $value => $title ) {
  718. $value = (string) $value;
  719. $single_input = scbFormField::_checkbox( array(
  720. 'name' => $args['name'],
  721. 'type' => 'radio',
  722. 'value' => $value,
  723. 'checked' => ( $value == $args['selected'] ),
  724. 'desc' => $title,
  725. 'desc_pos' => 'after',
  726. ) );
  727. $opts .= str_replace( scbForms::TOKEN, $single_input, $args['wrap_each'] );
  728. }
  729. return scbFormField::add_desc( $opts, $args['desc'], $args['desc_pos'] );
  730. }
  731. }
  732. /**
  733. * Checkbox field with multiple choices.
  734. */
  735. class scbMultipleChoiceField extends scbFormField {
  736. /**
  737. * Validates a value against a field.
  738. *
  739. * @param mixed $value
  740. *
  741. * @return array
  742. */
  743. public function validate( $value ) {
  744. return array_intersect( array_keys( $this->choices ), (array) $value );
  745. }
  746. /**
  747. * Generate the corresponding HTML for a field.
  748. *
  749. * @param array $args
  750. *
  751. * @return string
  752. */
  753. protected function _render( $args ) {
  754. $args = wp_parse_args( $args, array(
  755. 'numeric' => false, // use numeric array instead of associative
  756. 'checked' => null,
  757. ) );
  758. if ( ! is_array( $args['checked'] ) ) {
  759. $args['checked'] = array();
  760. }
  761. $opts = '';
  762. foreach ( $args['choices'] as $value => $title ) {
  763. $single_input = scbFormField::_checkbox( array(
  764. 'name' => $args['name'] . '[]',
  765. 'type' => 'checkbox',
  766. 'value' => $value,
  767. 'checked' => in_array( $value, $args['checked'] ),
  768. 'desc' => $title,
  769. 'desc_pos' => 'after',
  770. ) );
  771. $opts .= str_replace( scbForms::TOKEN, $single_input, $args['wrap_each'] );
  772. }
  773. return scbFormField::add_desc( $opts, $args['desc'], $args['desc_pos'] );
  774. }
  775. /**
  776. * Sets value using a reference.
  777. *
  778. * @param array $args
  779. * @param string $value
  780. *
  781. * @return void
  782. */
  783. protected function _set_value( &$args, $value ) {
  784. $args['checked'] = (array) $value;
  785. }
  786. }
  787. /**
  788. * Checkbox field.
  789. */
  790. class scbSingleCheckboxField extends scbFormField {
  791. /**
  792. * Validates a value against a field.
  793. *
  794. * @param mixed $value
  795. *
  796. * @return boolean
  797. */
  798. public function validate( $value ) {
  799. return (bool) $value;
  800. }
  801. /**
  802. * Generate the corresponding HTML for a field.
  803. *
  804. * @param array $args
  805. *
  806. * @return string
  807. */
  808. protected function _render( $args ) {
  809. $args = wp_parse_args( $args, array(
  810. 'value' => true,
  811. 'desc' => null,
  812. 'checked' => false,
  813. 'extra' => array(),
  814. ) );
  815. $args['extra']['checked'] = $args['checked'];
  816. if ( is_null( $args['desc'] ) && ! is_bool( $args['value'] ) ) {
  817. $args['desc'] = str_replace( '[]', '', $args['value'] );
  818. }
  819. return scbFormField::_input_gen( $args );
  820. }
  821. /**
  822. * Sets value using a reference.
  823. *
  824. * @param array $args
  825. * @param string $value
  826. *
  827. * @return void
  828. */
  829. protected function _set_value( &$args, $value ) {
  830. $args['checked'] = ( $value || ( isset( $args['value'] ) && $value == $args['value'] ) );
  831. }
  832. }
  833. /**
  834. * Wrapper field for custom callbacks.
  835. */
  836. class scbCustomField implements scbFormField_I {
  837. protected $args;
  838. /**
  839. * Constructor.
  840. *
  841. * @param array $args
  842. *
  843. * @return void
  844. */
  845. function __construct( $args ) {
  846. $this->args = wp_parse_args( $args, array(
  847. 'render' => 'var_dump',
  848. 'sanitize' => 'wp_filter_kses',
  849. ) );
  850. }
  851. /**
  852. * Magic method: $field->arg
  853. *
  854. * @param string $key
  855. *
  856. * @return mixed
  857. */
  858. public function __get( $key ) {
  859. return $this->args[ $key ];
  860. }
  861. /**
  862. * Magic method: isset( $field->arg )
  863. *
  864. * @param string $key
  865. *
  866. * @return boolean
  867. */
  868. public function __isset( $key ) {
  869. return isset( $this->args[ $key ] );
  870. }
  871. /**
  872. * Generate the corresponding HTML for a field.
  873. *
  874. * @param mixed $value (optional)
  875. *
  876. * @return string
  877. */
  878. public function render( $value = null ) {
  879. return call_user_func( $this->render, $value, $this );
  880. }
  881. /**
  882. * Sanitizes value.
  883. *
  884. * @param mixed $value
  885. *
  886. * @return mixed
  887. */
  888. public function validate( $value ) {
  889. return call_user_func( $this->sanitize, $value, $this );
  890. }
  891. }