PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/types/embedded/includes/fields/checkboxes.php

https://github.com/ngocphuuit/wordpress-tax
PHP | 359 lines | 238 code | 18 blank | 103 comment | 80 complexity | 0c0fa5746a885e6fd71b09ae2d6a8d85 MD5 | raw file
Possible License(s): GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. add_filter( 'wpcf_relationship_meta_form',
  3. 'wpcf_filds_checkboxes_relationship_form_filter' );
  4. /**
  5. * Register data (called automatically).
  6. *
  7. * @return type
  8. */
  9. function wpcf_fields_checkboxes() {
  10. return array(
  11. 'id' => 'wpcf-checkboxes',
  12. 'title' => __( 'Checkboxes', 'wpcf' ),
  13. 'description' => __( 'Checkboxes', 'wpcf' ),
  14. // 'validate' => array('required'),
  15. 'meta_key_type' => 'BINARY',
  16. );
  17. }
  18. // Add filter when using wpv_condition()
  19. add_filter( 'wpv_condition', 'wpcf_fields_checkboxes_wpv_conditional_trigger' );
  20. add_filter( 'wpv_condition_end',
  21. 'wpcf_fields_checkboxes_wpv_conditional_trigger_end' );
  22. /**
  23. * Form data for post edit page.
  24. *
  25. * @param type $field
  26. */
  27. function wpcf_fields_checkboxes_meta_box_form( $field, $field_object ) {
  28. $options = array();
  29. if ( !empty( $field['data']['options'] ) ) {
  30. global $pagenow;
  31. foreach ( $field['data']['options'] as $option_key => $option ) {
  32. // Set value
  33. $options[$option_key] = array(
  34. '#value' => $option['set_value'],
  35. '#title' => wpcf_translate( 'field ' . $field['id'] . ' option '
  36. . $option_key . ' title', $option['title'] ),
  37. '#default_value' => (!empty( $field['value'][$option_key] )// Also check new post
  38. || ($pagenow == 'post-new.php' && !empty( $option['checked'] ))) ? 1 : 0,
  39. '#name' => 'wpcf[' . $field['id'] . '][' . $option_key . ']',
  40. '#id' => $option_key . '_'
  41. . wpcf_unique_id( serialize( $field ) ),
  42. '#inline' => true,
  43. '#after' => '<br />',
  44. );
  45. }
  46. }
  47. return array(
  48. '#type' => 'checkboxes',
  49. '#options' => $options,
  50. );
  51. }
  52. /**
  53. * Editor callback form.
  54. */
  55. function wpcf_fields_checkboxes_editor_callback( $field, $settings ) {
  56. $data = array();
  57. if ( !empty( $field['data']['options'] ) ) {
  58. $index = 0;
  59. foreach ( $field['data']['options'] as $option_key => $option ) {
  60. $data['checkboxes'][$option_key] = array(
  61. 'id' => $option_key,
  62. 'title' => $option['title'],
  63. 'selected' => isset( $settings['options'][$index]['selected'] ) ? $settings['options'][$index]['selected'] : htmlspecialchars( stripslashes( strval( ( $option['display_value_selected'] )))),
  64. 'not_selected' => isset( $settings['options'][$index]['not_selected'] ) ? $settings['options'][$index]['not_selected'] : htmlspecialchars(stripslashes( strval( $option['display_value_not_selected'] ))),
  65. );
  66. $index++;
  67. }
  68. }
  69. return array(
  70. 'supports' => array('style'),
  71. 'tabs' => array(
  72. 'display' => array(
  73. 'menu_title' => __( 'Display', 'wpcf' ),
  74. 'title' => __( 'Display', 'wpcf' ),
  75. 'content' => WPCF_Loader::template( 'editor-modal-checkboxes',
  76. $data ),
  77. )
  78. )
  79. );
  80. }
  81. /**
  82. * Editor callback form submit.
  83. */
  84. function wpcf_fields_checkboxes_editor_submit( $data, $field, $context ) {
  85. $add = '';
  86. $types_attr = $context == 'usermeta' ? 'usermeta' : 'field';
  87. $shortcode = '';
  88. if ( $context == 'usermeta' ) {
  89. $add .= wpcf_get_usermeta_form_addon_submit();
  90. }
  91. if ( !empty( $data['options'] ) ) {
  92. if ( $data['display'] == 'display_all' ) {
  93. $separator = !empty( $data['cbs_separator'] ) ? $data['cbs_separator'] : '';
  94. $_add = $add . ' separator="' . $separator . '"';
  95. if ( $context == 'usermeta' ) {
  96. $shortcode .= wpcf_usermeta_get_shortcode( $field, $_add );
  97. } else {
  98. $shortcode .= wpcf_fields_get_shortcode( $field, $_add );
  99. }
  100. } else {
  101. $i = 0;
  102. foreach ( $data['options'] as $option ) {
  103. if ( $data['display'] == 'value' ) {
  104. $checked_add = $add . ' option="' . $i . '" state="checked"';
  105. $unchecked_add = $add . ' option="' . $i . '" state="unchecked"';
  106. if ( $context == 'usermeta' ) {
  107. $shortcode_checked = wpcf_usermeta_get_shortcode( $field,
  108. $checked_add, $option['selected'] );
  109. $shortcode_unchecked = wpcf_usermeta_get_shortcode( $field,
  110. $unchecked_add, $option['not_selected'] );
  111. } else {
  112. $shortcode_checked = wpcf_fields_get_shortcode( $field,
  113. $checked_add, $option['selected'] );
  114. $shortcode_unchecked = wpcf_fields_get_shortcode( $field,
  115. $unchecked_add, $option['not_selected'] );
  116. }
  117. $shortcode .= $shortcode_checked . $shortcode_unchecked;
  118. } else {
  119. $add = ' option="' . $i . '"';
  120. if ( $context == 'usermeta' ) {
  121. $add .= wpcf_get_usermeta_form_addon_submit();
  122. }
  123. if ( $types_attr == 'usermeta' ) {
  124. $shortcode .= wpcf_usermeta_get_shortcode( $field, $add );
  125. } else {
  126. $shortcode .= wpcf_fields_get_shortcode( $field, $add );
  127. }
  128. }
  129. $i++;
  130. }
  131. }
  132. } else {
  133. if ( $types_attr == 'usermeta' ) {
  134. $shortcode .= wpcf_usermeta_get_shortcode( $field, $add );
  135. } else {
  136. $shortcode .= wpcf_fields_get_shortcode( $field, $add );
  137. }
  138. }
  139. return $shortcode;
  140. }
  141. /**
  142. * View function.
  143. *
  144. * @param type $params
  145. */
  146. function wpcf_fields_checkboxes_view( $params ) {
  147. $option = array();
  148. // Basic checks
  149. if ( empty( $params['field']['data']['options'] )
  150. || !is_array( $params['field_value'] ) ) {
  151. return '__wpcf_skip_empty';
  152. }
  153. /*
  154. *
  155. * NO OPTION specified
  156. * loop over all options and display all of them
  157. */
  158. if ( !isset( $params['option'] ) ) {
  159. $separator = isset( $params['separator'] ) ? html_entity_decode( $params['separator'] ) : ', ';
  160. foreach ( $params['field_value'] as $name => &$value ) {
  161. /*
  162. *
  163. * Set option
  164. */
  165. if ( isset( $params['field']['data']['options'][$name] ) ) {
  166. $option = $params['field']['data']['options'][$name];
  167. } else {
  168. // Unset if not valid
  169. unset( $params['field_value'][$name] );
  170. continue;
  171. }
  172. /*
  173. *
  174. * Set output according to settings.
  175. * 'db' or 'value'
  176. */
  177. if ( $option['display'] == 'db'
  178. && !empty( $option['set_value'] ) && !empty( $value ) ) {
  179. $value = $option['set_value'];
  180. $value = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $name . ' value',
  181. $value );
  182. } else if ( $option['display'] == 'value' ) {
  183. if ( isset( $option['display_value_selected'] ) && !empty( $value ) ) {
  184. $value = $option['display_value_selected'];
  185. $value = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $name . ' display value selected',
  186. $value );
  187. } else {
  188. $value = $option['display_value_not_selected'];
  189. $value = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $name . ' display value not selected',
  190. $value );
  191. }
  192. } else {
  193. unset( $params['field_value'][$name] );
  194. }
  195. }
  196. $output = implode( array_values( $params['field_value'] ), $separator );
  197. return empty( $output ) ? '__wpcf_skip_empty' : $output;
  198. }
  199. /*
  200. *
  201. *
  202. * OPTION specified - set required option.
  203. */
  204. $i = 0;
  205. foreach ( $params['field']['data']['options'] as $option_key => $option_value ) {
  206. if ( intval( $params['option'] ) == $i ) {
  207. $option['key'] = $option_key;
  208. $option['data'] = $option_value;
  209. $option['value'] = !empty( $params['field_value'][$option_key] ) ? $params['field_value'][$option_key] : '__wpcf_unchecked';
  210. break;
  211. }
  212. $i++;
  213. }
  214. $output = '';
  215. /*
  216. * STATE set - use #content is as render value.
  217. * If setings are faulty - return '__wpcf_skip_empty'.
  218. */
  219. if ( isset( $params['state'] ) ) {
  220. $content = !empty( $params['#content'] ) ? htmlspecialchars_decode( $params['#content'] ) : '__wpcf_skip_empty';
  221. if ( $params['state'] == 'checked'
  222. && $option['value'] != '__wpcf_unchecked' ) {
  223. return $content;
  224. } else if ( $params['state'] == 'unchecked'
  225. && $option['value'] == '__wpcf_unchecked' ) {
  226. return $content;
  227. } else if ( isset( $params['state'] ) ) {
  228. return '__wpcf_skip_empty';
  229. }
  230. }
  231. /*
  232. *
  233. * MAIN settings
  234. * 'db' - Use 'set_value' as render value
  235. * 'value' - Use values set in Group form data 'display_value_selected'
  236. * or 'display_value_not_selected'
  237. *
  238. * Only set if it matches settings.
  239. * Otherwise leave empty and '__wpcf_skip_empty' will be returned.
  240. */
  241. if ( $option['data']['display'] == 'db' ) {
  242. /*
  243. *
  244. * Only if NOT unchecked!
  245. */
  246. if ( !empty( $option['data']['set_value'] )
  247. && $option['value'] != '__wpcf_unchecked' ) {
  248. $output = $option['data']['set_value'];
  249. $output = wpcf_translate( 'field ' . $params['field']['id']
  250. . ' option ' . $option['key'] . ' value', $output );
  251. }
  252. } else if ( $option['data']['display'] == 'value' ) {
  253. /*
  254. *
  255. * Checked
  256. */
  257. if ( $option['value'] != '__wpcf_unchecked' ) {
  258. if ( isset( $option['data']['display_value_selected'] ) ) {
  259. $output = $option['data']['display_value_selected'];
  260. $output = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $option['key'] . ' display value selected',
  261. $output );
  262. }
  263. /*
  264. *
  265. *
  266. * Un-checked
  267. */
  268. } else if ( isset( $option['data']['display_value_not_selected'] ) ) {
  269. $output = $option['data']['display_value_not_selected'];
  270. $output = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $option['key'] . ' display value not selected',
  271. $output );
  272. }
  273. }
  274. if ( empty( $output ) ) {
  275. return '__wpcf_skip_empty';
  276. }
  277. return $output;
  278. }
  279. /**
  280. * This marks child posts checkboxes.
  281. *
  282. * Because if all unchecked, on submit there won't be any data.
  283. *
  284. * @param string $form
  285. * @param type $cf
  286. * @return string
  287. */
  288. function wpcf_filds_checkboxes_relationship_form_filter( $form, $cf ) {
  289. if ( $cf->cf['type'] == 'checkboxes' ) {
  290. $form[wpcf_unique_id( serialize( $cf ) . 'rel_child' )] = array(
  291. '#type' => 'hidden',
  292. '#name' => '_wpcf_check_checkboxes[' . $cf->post->ID . ']['
  293. . $cf->slug . ']',
  294. '#value' => '1'
  295. );
  296. }
  297. return $form;
  298. }
  299. /**
  300. * Triggers post_meta filter.
  301. *
  302. * @param type $post
  303. * @return type
  304. */
  305. function wpcf_fields_checkboxes_wpv_conditional_trigger( $post ) {
  306. add_filter( 'get_post_metadata',
  307. 'wpcf_fields_checkboxes_conditional_filter_post_meta', 10, 4 );
  308. }
  309. /**
  310. * Returns string.
  311. *
  312. * @global type $wpcf
  313. * @param type $null
  314. * @param type $object_id
  315. * @param type $meta_key
  316. * @param type $single
  317. * @return type
  318. */
  319. function wpcf_fields_checkboxes_conditional_filter_post_meta( $null, $object_id,
  320. $meta_key, $single ) {
  321. global $wpcf;
  322. $field = wpcf_admin_fields_get_field( $wpcf->field->__get_slug_no_prefix( $meta_key ) );
  323. if ( !empty( $field ) && $field['type'] == 'checkboxes' ) {
  324. $_meta = maybe_unserialize( wpcf_get_post_meta( $object_id, $meta_key,
  325. $single ) );
  326. if ( is_array( $_meta ) ) {
  327. $null = empty( $_meta ) ? '1' : '';
  328. }
  329. }
  330. return $null;
  331. }
  332. /**
  333. * Triggers post_meta filter.
  334. *
  335. * @param type $post
  336. * @return type
  337. */
  338. function wpcf_fields_checkboxes_wpv_conditional_trigger_end( $post ) {
  339. remove_filter( 'get_post_metadata',
  340. 'wpcf_fields_checkboxes_conditional_filter_post_meta', 10, 4 );
  341. }