PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 292 lines | 207 code | 30 blank | 55 comment | 91 complexity | 5dbc831f42b88f99d85eafca8f8b5bd6 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. *
  4. * $HeadURL: https://www.onthegosystems.com/misc_svn/cck/tags/1.6.3/embedded/includes/fields/checkbox.php $
  5. * $LastChangedDate: 2014-08-28 09:37:36 +0000 (Thu, 28 Aug 2014) $
  6. * $LastChangedRevision: 26512 $
  7. * $LastChangedBy: bruce $
  8. *
  9. */
  10. add_action( 'save_post', 'wpcf_fields_checkbox_save_check', 15, 1 );
  11. add_action( 'edit_attachment', 'wpcf_fields_checkbox_save_check', 15, 1 );
  12. /**
  13. * Register data (called automatically).
  14. *
  15. * @return type
  16. */
  17. function wpcf_fields_checkbox() {
  18. return array(
  19. 'id' => 'wpcf-checkbox',
  20. 'title' => __( 'Checkbox', 'wpcf' ),
  21. 'description' => __( 'Checkbox', 'wpcf' ),
  22. 'validate' => array('required'),
  23. 'meta_key_type' => 'BINARY',
  24. );
  25. }
  26. /**
  27. * Form data for post edit page.
  28. *
  29. * @param type $field
  30. */
  31. function wpcf_fields_checkbox_meta_box_form( $field, $field_object ) {
  32. global $wpcf;
  33. $checked = false;
  34. $field['data']['set_value'] = stripslashes( $field['data']['set_value'] );
  35. if ( $field['value'] == $field['data']['set_value'] ) {
  36. $checked = true;
  37. }
  38. // If post is new check if it's checked by default
  39. global $pagenow;
  40. if ( $pagenow == 'post-new.php' && !empty( $field['data']['checked'] ) ) {
  41. $checked = true;
  42. }
  43. // This means post is new
  44. if ( !isset( $field_object->post->ID ) ) {
  45. $field_object->post = (object) array('ID' => 0);
  46. }
  47. return array(
  48. '#type' => 'checkbox',
  49. '#value' => $field['data']['set_value'],
  50. '#default_value' => $checked,
  51. '#after' => '<input type="hidden" name="_wpcf_check_checkbox['
  52. . $field_object->post->ID . '][' . $field_object->slug
  53. . ']" value="1" />',
  54. );
  55. }
  56. /**
  57. * Editor callback form.
  58. */
  59. function wpcf_fields_checkbox_editor_callback( $field, $settings ) {
  60. $value_not_selected = '';
  61. $value_selected = '';
  62. if ( isset( $field['data']['display_value_not_selected'] ) ) {
  63. $value_not_selected = $field['data']['display_value_not_selected'];
  64. }
  65. if ( isset( $field['data']['display_value_selected'] ) ) {
  66. $value_selected = $field['data']['display_value_selected'];
  67. }
  68. $data = array_merge( array(
  69. 'selected' => WPCF_Editor::sanitizeParams( $value_selected ),
  70. 'not_selected' => WPCF_Editor::sanitizeParams( $value_not_selected ),
  71. ), $settings );
  72. return array(
  73. 'supports' => array('style'),
  74. 'tabs' => array(
  75. 'display' => array(
  76. 'menu_title' => __( 'Display', 'wpcf' ),
  77. 'title' => __( 'Display', 'wpcf' ),
  78. 'content' => WPCF_Loader::template( 'editor-modal-checkbox',
  79. $data ),
  80. )
  81. ),
  82. );
  83. }
  84. /**
  85. * Editor callback form submit.
  86. */
  87. function wpcf_fields_checkbox_editor_submit( $data, $field, $context ) {
  88. $add = '';
  89. $types_attr = 'field';
  90. if ( $context == 'usermeta' ) {
  91. $add .= wpcf_get_usermeta_form_addon_submit();
  92. $types_attr = 'usermeta';
  93. }
  94. if ( isset($data['display']) && $data['display'] == 'value' ) {
  95. $checked_add = $add . ' state="checked"';
  96. $unchecked_add = $add . ' state="unchecked"';
  97. if ( $context == 'usermeta' ) {
  98. $shortcode_checked = wpcf_usermeta_get_shortcode( $field,
  99. $checked_add, $data['selected'] );
  100. $shortcode_unchecked = wpcf_usermeta_get_shortcode( $field,
  101. $unchecked_add, $data['not_selected'] );
  102. } else {
  103. $shortcode_checked = wpcf_fields_get_shortcode( $field,
  104. $checked_add, $data['selected'] );
  105. $shortcode_unchecked = wpcf_fields_get_shortcode( $field,
  106. $unchecked_add, $data['not_selected'] );
  107. }
  108. $shortcode = $shortcode_checked . $shortcode_unchecked;
  109. } else {
  110. if ( $context == 'usermeta' ) {
  111. $shortcode = wpcf_usermeta_get_shortcode( $field, $add );
  112. } else {
  113. $shortcode = wpcf_fields_get_shortcode( $field, $add );
  114. }
  115. }
  116. return $shortcode;
  117. }
  118. /**
  119. * View function.
  120. *
  121. * @param type $params
  122. */
  123. function wpcf_fields_checkbox_view( $params ) {
  124. $output = '';
  125. $option_name = 'wpcf-fields';
  126. if ( isset( $params['usermeta'] ) && !empty( $params['usermeta'] ) ) {
  127. $option_name = 'wpcf-usermeta';
  128. }
  129. if ( isset( $params['option_name'] ) ) {
  130. $option_name = $params['option_name'];
  131. }
  132. if ( isset( $params['state'] )
  133. && $params['state'] == 'unchecked'
  134. && empty( $params['field_value'] ) ) {
  135. if ( empty( $params['#content'] ) ) {
  136. return '__wpcf_skip_empty';
  137. }
  138. return htmlspecialchars_decode( $params['#content'] );
  139. } else if ( isset( $params['state'] ) && $params['state'] == 'unchecked' ) {
  140. return '__wpcf_skip_empty';
  141. }
  142. if ( isset( $params['state'] ) && $params['state'] == 'checked' && !empty( $params['field_value'] ) ) {
  143. if ( empty( $params['#content'] ) ) {
  144. return '__wpcf_skip_empty';
  145. }
  146. return htmlspecialchars_decode( $params['#content'] );
  147. } else if ( isset( $params['state'] ) && $params['state'] == 'checked' ) {
  148. return '__wpcf_skip_empty';
  149. }
  150. if ( !empty( $params['#content'] )
  151. && !empty( $params['field_value'] ) ) {
  152. return htmlspecialchars_decode( $params['#content'] );
  153. }
  154. // Check if 'save_empty' is yes and if value is 0 - set value to empty string
  155. if ( isset( $params['field']['data']['save_empty'] )
  156. && $params['field']['data']['save_empty'] == 'yes'
  157. && $params['field_value'] == '0' ) {
  158. $params['field_value'] = '';
  159. }
  160. if ( $params['field']['data']['display'] == 'db' && $params['field_value'] != '' ) {
  161. $field = wpcf_fields_get_field_by_slug( $params['field']['slug'],
  162. $option_name );
  163. $output = $field['data']['set_value'];
  164. // Show the translated value if we have one.
  165. $output = wpcf_translate( 'field ' . $field['id'] . ' checkbox value',
  166. $output );
  167. } else if ( $params['field']['data']['display'] == 'value'
  168. && $params['field_value'] != '' ) {
  169. if ( !empty( $params['field']['data']['display_value_selected'] ) ) {
  170. $output = $params['field']['data']['display_value_selected'];
  171. $output = wpcf_translate( 'field ' . $params['field']['id'] . ' checkbox value selected',
  172. $output );
  173. }
  174. } else if ( $params['field']['data']['display'] == 'value'
  175. && !empty( $params['field']['data']['display_value_not_selected'] ) ) {
  176. $output = $params['field']['data']['display_value_not_selected'];
  177. $output = wpcf_translate( 'field ' . $params['field']['id'] . ' checkbox value not selected', $output );
  178. } else {
  179. return '__wpcf_skip_empty';
  180. }
  181. return $output;
  182. }
  183. /**
  184. * Check if checkbox is submitted.
  185. *
  186. * Currently used on Relationship saving. May be expanded to general code.
  187. *
  188. * @param type $post_id
  189. */
  190. function wpcf_fields_checkbox_save_check( $post_id ) {
  191. $meta_to_unset = array();
  192. $cf = new WPCF_Field();
  193. /*
  194. *
  195. * We hve several calls on this:
  196. * 1. Saving post with Update
  197. * 2. Saving all children
  198. * 3. Saving child
  199. */
  200. $mode = 'save_main';
  201. if ( defined( 'DOING_AJAX' ) ) {
  202. if ( isset( $_GET['wpcf_action'] )
  203. && $_GET['wpcf_action'] == 'pr_save_all' ) {
  204. $mode = 'save_all';
  205. } else if ( isset( $_GET['wpcf_action'] )
  206. && $_GET['wpcf_action'] == 'pr_save_child_post' ) {
  207. $mode = 'save_child';
  208. }
  209. }
  210. // See if any marked for checking
  211. if ( isset( $_POST['_wpcf_check_checkbox'] ) ) {
  212. // Loop and search in $_POST
  213. foreach ( $_POST['_wpcf_check_checkbox'] as $child_id => $slugs ) {
  214. foreach ( $slugs as $slug => $true ) {
  215. $cf->set( $child_id, $cf->__get_slug_no_prefix( $slug ) );
  216. // First check main post
  217. if ( $mode == 'save_main'
  218. && intval( $child_id ) == wpcf_get_post_id() ) {
  219. if ( !isset( $_POST['wpcf'][$cf->cf['slug']] ) ) {
  220. $meta_to_unset[intval( $child_id )][$cf->slug] = true;
  221. }
  222. continue;
  223. }
  224. // If new post
  225. if ( $mode == 'save_main' && $child_id == 0 ) {
  226. if ( !isset( $_POST['wpcf'][$cf->cf['slug']] ) ) {
  227. $meta_to_unset[$post_id][$cf->slug] = true;
  228. }
  229. continue;
  230. }
  231. /**
  232. * Relationship check
  233. */
  234. if ( $mode == 'save_main' ) {
  235. if ( !isset( $_POST['wpcf'][$cf->cf['slug']] ) ) {
  236. $meta_to_unset[$post_id][$cf->slug] = true;
  237. }
  238. continue;
  239. } else if ( !empty( $_POST['wpcf_post_relationship'] ) ) {
  240. foreach ( $_POST['wpcf_post_relationship'] as $_parent => $_children ) {
  241. foreach ( $_children as $_child_id => $_slugs ) {
  242. if ( !isset( $_slugs[$slug] ) ) {
  243. $meta_to_unset[$_child_id][$cf->slug] = true;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. // After collected - delete them
  252. foreach ( $meta_to_unset as $child_id => $slugs ) {
  253. foreach ( $slugs as $slug => $true ) {
  254. $cf->set( $child_id, $cf->__get_slug_no_prefix( $slug ) );
  255. if ( $cf->cf['data']['save_empty'] != 'no' ) {
  256. update_post_meta( $child_id, $slug, 0 );
  257. } else {
  258. delete_post_meta( $child_id, $slug );
  259. }
  260. }
  261. }
  262. }