PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/types/includes/conditional-display.php

https://bitbucket.org/JacobTyler/fame4good-wp
PHP | 400 lines | 343 code | 9 blank | 48 comment | 35 complexity | c048d573bc6b27cfb70b1bb07e72e328 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, AGPL-1.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /*
  3. * Conditional display code.
  4. */
  5. require_once WPCF_EMBEDDED_ABSPATH . '/includes/conditional-display.php';
  6. add_filter('wpcf_form_field', 'wpcf_cd_form_field_filter', 10, 2);
  7. add_filter('wpcf_field_pre_save', 'wpcf_cd_field_pre_save_filter');
  8. add_filter('wpcf_fields_form_additional_filters',
  9. 'wpcf_cd_fields_form_additional_filters', 10, 2);
  10. add_action('wpcf_save_group', 'wpcf_cd_save_group_action');
  11. add_action('admin_footer', 'wpcf_cd_admin_form_js');
  12. /**
  13. * Filters group field form.
  14. *
  15. * @param type $form
  16. * @param type $data
  17. * @return type
  18. */
  19. function wpcf_cd_form_field_filter($form, $data) {
  20. if (defined('DOING_AJAX') && isset($_SERVER['HTTP_REFERER'])) {
  21. parse_str($_SERVER['HTTP_REFERER'], $vars);
  22. } else if (isset($_GET['group_id'])) {
  23. $vars = array();
  24. $vars['group_id'] = $_GET['group_id'];
  25. }
  26. if (!isset($vars['group_id'])) {
  27. return $form + array(
  28. 'cd_not_available' => array(
  29. '#type' => 'markup',
  30. '#markup' => '<p>' . __('You will be able to set conditional field display once this group is saved.',
  31. 'wpcf') . '</p>',
  32. ),
  33. );
  34. }
  35. $form = $form + wpcf_cd_admin_form_filter($data);
  36. return $form;
  37. }
  38. /**
  39. * Field pre-save filter.
  40. *
  41. * @param array $data
  42. * @return array
  43. */
  44. function wpcf_cd_field_pre_save_filter($data) {
  45. if (empty($data['conditional_display'])) {
  46. $data['conditional_display'] = array();
  47. }
  48. return $data;
  49. }
  50. /**
  51. * Conditional display form.
  52. *
  53. * @param type $data
  54. * @param type $group
  55. * @return type
  56. */
  57. function wpcf_cd_admin_form_filter($data, $group = false) {
  58. if ($group) {
  59. $name = 'wpcf[group][conditional_display]';
  60. } else {
  61. $name = 'wpcf[fields][' . $data['id'] . '][conditional_display]';
  62. }
  63. $form = array();
  64. if (!$group) {
  65. $form['cd'] = array(
  66. '#type' => 'fieldset',
  67. '#title' => __('Conditional display', 'wpcf'),
  68. '#collapsed' => true,
  69. '#id' => $data['id'] . '_conditional_display',
  70. '#attributes' => array('class' => 'wpcf-cd-fieldset'),
  71. );
  72. } else {
  73. $form['cd']['wrap'] = array(
  74. '#type' => 'markup',
  75. '#markup' => '<strong>' . __('Data-dependent display filters',
  76. 'wpcf') . '</strong><br />'
  77. . __("Specify additional filters that control this group's display, based on values of custom fields.",
  78. 'wpcf')
  79. . '<br /><a class="button-secondary" onclick="jQuery(this).css(\'visibility\',\'hidden\').next().slideToggle();" style="line-height: 30px;" href="javascript:void(0);">'
  80. . __('Edit', 'wpcf') . '</a><div id="wpcf-cd-group" style="display:none;">',
  81. );
  82. }
  83. if (!empty($data['data']['conditional_display']['conditions'])) {
  84. $conditions = $data['data']['conditional_display']['conditions'];
  85. $count = count($conditions);
  86. } else {
  87. $count = 1;
  88. }
  89. $add = $group ? 'true' : 'false';
  90. $form['cd']['add'] = array(
  91. '#type' => 'markup',
  92. '#markup' => '<br /><a class="wpcf-ajax-link button-secondary" onclick="wpcfCdAddCondition(jQuery(this),' . $add . ');" href="javascript:void(0);">'
  93. . __('Add condition', 'wpcf') . '</a><br /><br /><div class="wpcf-cd-entries">',
  94. );
  95. if (!empty($data['data']['conditional_display']['conditions'])) {
  96. foreach ($data['data']['conditional_display']['conditions'] as $key => $condition) {
  97. $form['cd'] += wpcf_cd_admin_form_single_filter($data, $condition,
  98. $key, $group);
  99. }
  100. }
  101. $form['cd']['add_close'] = array(
  102. '#type' => 'markup',
  103. '#markup' => '</div>',
  104. );
  105. $form['cd']['relation'] = array(
  106. '#type' => 'radios',
  107. '#name' => $name . '[relation]',
  108. '#options' => array(
  109. 'AND' => array(
  110. '#title' => 'AND',
  111. '#attributes' => array('onclick' => 'wpcfCdCreateSummary(\'' . md5($data['id']) . '_cd_summary\')'),
  112. '#inline' => true,
  113. '#value' => 'AND',
  114. '#after' => '<br />',
  115. ),
  116. 'OR' => array(
  117. '#title' => 'OR',
  118. '#attributes' => array('onclick' => 'wpcfCdCreateSummary(\'' . md5($data['id']) . '_cd_summary\')'),
  119. '#inline' => true,
  120. '#value' => 'OR'
  121. ),
  122. ),
  123. '#default_value' => isset($data['data']['conditional_display']['relation']) ? $data['data']['conditional_display']['relation'] : 'AND',
  124. '#inline' => true,
  125. '#before' => '<br /><div class="wpcf-cd-relation" style="display:none;">',
  126. '#after' => '</div><br />',
  127. );
  128. $form['cd']['toggle_open'] = array(
  129. '#type' => 'markup',
  130. '#markup' => '<div class="toggle-cd" style="display:none;">',
  131. );
  132. $prepopulate = !empty($data['data']['conditional_display']['custom']) ? ' jQuery(\'#' . md5($data['id']) . '_cd_summary\').val();' : ' wpcfCdCreateSummary(\'' . md5($data['id']) . '_cd_summary\');';
  133. $form['cd']['customize_display_logic_link'] = array(
  134. '#type' => 'markup',
  135. '#markup' => '<a href="javascript:void(0);" class="button-secondary wpcf-cd-enable-custom-mode" onclick="window.wpcfCdState_' . md5($data['id'])
  136. . ' = jQuery(\'#' . md5($data['id']) . '_cd_summary\').val();' . $prepopulate . ' jQuery(\'#' . md5($data['id']) . '_cd_summary\').parent().slideDown(); jQuery(this).hide().next().show();wpcfCdCheckDateCustomized(jQuery(this));">'
  137. . __('Customize the display logic', 'wpcf')
  138. . '</a>',
  139. );
  140. $form['cd']['revert_display_logic_link'] = array(
  141. '#type' => 'markup',
  142. '#markup' => '<a href="javascript:void(0);" class="button-secondary wpcf-cd-enable-custom-mode hidden" onclick="jQuery(\'#' . md5($data['id']) . '_cd_summary\').parent().slideUp().find(\'.checkbox\').removeAttr(\'checked\'); jQuery(this).hide().prev().show();">'
  143. . __('Go back to simple logic', 'wpcf')
  144. . '</a>',
  145. );
  146. $form['cd']['toggle_open_area'] = array(
  147. '#type' => 'markup',
  148. '#markup' => '<div class="area-toggle-cd" style="margin-top:10px;display:none;">',
  149. );
  150. $form['cd']['custom'] = array(
  151. '#type' => 'textarea',
  152. '#name' => $name . '[custom]',
  153. '#title' => __('Customize conditions', 'wpcf'),
  154. '#id' => md5($data['id']) . '_cd_summary',
  155. '#after' => '<br /><a href="javascript:void(0);" onclick="wpcfCdCreateSummary(\''
  156. . md5($data['id']) . '_cd_summary\');">'
  157. . __('Re-read structure', 'wpcf') . '</a><br />',
  158. '#inline' => true,
  159. '#value' => isset($data['data']['conditional_display']['custom']) ? $data['data']['conditional_display']['custom'] : '',
  160. );
  161. $form['cd']['custom_use'] = array(
  162. '#type' => 'checkbox',
  163. '#name' => $name . '[custom_use]',
  164. '#title' => __('Use customized conditions', 'wpcf'),
  165. '#inline' => true,
  166. '#default_value' => isset($data['data']['conditional_display']['custom_use']),
  167. '#after' => '',
  168. );
  169. $form['cd']['date_notice'] = array(
  170. '#type' => 'markup',
  171. '#markup' => '<div style="display:none; margin-top:15px;" class="wpcf-cd-notice-date">'
  172. . sprintf(__('%sDates can be entered using the date filters &raquo;%s', 'wpcf'), '<a href="http://wp-types.com/documentation/user-guides/date-filters/" target="_blank">', '</a>') . '</div>',
  173. );
  174. $form['cd']['apply_display_logic_link'] = array(
  175. '#type' => 'markup',
  176. '#markup' => '<br /><br /><a href="javascript:void(0);" class="button-primary" onclick="window.wpcfCdState_' . md5($data['id'])
  177. . ' = jQuery(\'#' . md5($data['id']) . '_cd_summary\').parent().slideUp().prev().hide().prev().show();">'
  178. . __('Apply', 'wpcf')
  179. . '</a>&nbsp&nbsp;',
  180. );
  181. $form['cd']['cancel_display_logic_link'] = array(
  182. '#type' => 'markup',
  183. '#markup' => '<a href="javascript:void(0);" class="button-primary" onclick="jQuery(\'#' . md5($data['id']) . '_cd_summary\').val(window.wpcfCdState_' . md5($data['id']) . ').parent().slideUp().prev().hide().prev().show();">'
  184. . __('Cancel', 'wpcf')
  185. . '</a>',
  186. );
  187. $form['cd']['toggle_close'] = array(
  188. '#type' => 'markup',
  189. '#markup' => '</div>',
  190. );
  191. $form['cd']['toggle_close_area'] = array(
  192. '#type' => 'markup',
  193. '#markup' => '</div>',
  194. );
  195. $form['cd']['count'] = array(
  196. '#type' => 'hidden',
  197. '#name' => '_wpcf_cd_count_' . $data['id'],
  198. '#value' => $count,
  199. );
  200. if ($group) {
  201. $form['cd']['wrap_close'] = array(
  202. '#type' => 'markup',
  203. '#markup' => '<br /><a class="button-primary" onclick="jQuery(this).parent().slideUp().prev().css(\'visibility\',\'visible\');" style="line-height: 30px;" href="javascript:void(0);">'
  204. . __('OK', 'wpcf') . '</a></div>',
  205. );
  206. }
  207. return $group ? $form['cd'] : $form;
  208. }
  209. /**
  210. * Single condition form elements.
  211. *
  212. * @param type $data
  213. * @param type $condition
  214. * @param type $key
  215. * @return string
  216. */
  217. function wpcf_cd_admin_form_single_filter($data, $condition, $key = null,
  218. $group = false, $force_multi = false) {
  219. if ($group) {
  220. $name = 'wpcf[group][conditional_display]';
  221. } else {
  222. $name = 'wpcf[fields][' . $data['id'] . '][conditional_display]';
  223. }
  224. $group_id = isset($_GET['group_id']) ? intval($_GET['group_id']) : false;
  225. if ($group_id && !$group) {// Allow group to use other fields
  226. $fields = wpcf_admin_fields_get_fields_by_group($group_id);
  227. } else {
  228. $fields = wpcf_admin_fields_get_fields();
  229. }
  230. $options = array();
  231. foreach ($fields as $field_id => $field) {
  232. if (!$group && $data['id'] == $field_id) {
  233. continue;
  234. }
  235. if (wpcf_admin_is_repetitive($field)) {
  236. continue;
  237. }
  238. $options[$field_id] = array(
  239. '#value' => $field_id,
  240. '#title' => $field['name'],
  241. '#attributes' => array('class' => 'wpcf-conditional-select-' . $field['type']),
  242. );
  243. }
  244. if (!$group && empty($options)) {
  245. return array(
  246. 'cd' => array(
  247. '#type' => 'markup',
  248. '#markup' => '<p>' . __('You will be able to set conditional field display when you save more fields.',
  249. 'wpcf') . '</p>',
  250. )
  251. );
  252. }
  253. $id = !is_null($key) ? $key : strval('condition_' . mt_rand());
  254. $form = array();
  255. $before = '<div class="wpcf-cd-entry"><br />';
  256. $form['cd']['field_' . $id] = array(
  257. '#type' => 'select',
  258. '#name' => $name . '[conditions][' . $id . '][field]',
  259. '#options' => $options,
  260. '#inline' => true,
  261. '#before' => $before,
  262. '#default_value' => isset($condition['field']) ? $condition['field'] : null,
  263. '#attributes' => array('class' => 'wpcf-cd-field'),
  264. );
  265. $form['cd']['operation_' . $id] = array(
  266. '#type' => 'select',
  267. '#name' => $name . '[conditions][' . $id . '][operation]',
  268. '#options' => array_flip(wpcf_cd_admin_operations()),
  269. '#inline' => true,
  270. '#default_value' => isset($condition['operation']) ? $condition['operation'] : null,
  271. '#attributes' => array('class' => 'wpcf-cd-operation'),
  272. );
  273. $form['cd']['value_' . $id] = array(
  274. '#type' => 'textfield',
  275. '#name' => $name . '[conditions][' . $id . '][value]',
  276. '#options' => wpcf_cd_admin_operations(),
  277. '#inline' => true,
  278. '#value' => isset($condition['value']) ? $condition['value'] : '',
  279. '#attributes' => array('class' => 'wpcf-cd-value'),
  280. );
  281. $form['cd']['remove_' . $id] = array(
  282. '#type' => 'button',
  283. '#name' => 'remove',
  284. '#value' => __('Remove condition', 'wpcf'),
  285. '#attributes' => array('onclick' => 'wpcfCdRemoveCondition(jQuery(this));', 'class' => 'wpcf-add-condition'),
  286. '#before' => '<br />',
  287. '#after' => '</div>',
  288. );
  289. return $form['cd'];
  290. }
  291. /**
  292. * Group coditional display filter.
  293. *
  294. * @param type $filters
  295. * @param type $update
  296. * @return type
  297. */
  298. function wpcf_cd_fields_form_additional_filters($filters, $update) {
  299. $data = array();
  300. $data['id'] = !empty($update) ? $update['name'] : mt_rand();
  301. if ($update) {
  302. $data['data']['conditional_display'] = get_post_meta($update['id'],
  303. '_wpcf_conditional_display', true);
  304. } else {
  305. $data['data']['conditional_display'] = array();
  306. }
  307. $filters = $filters + wpcf_cd_admin_form_filter($data, true);
  308. return $filters;
  309. }
  310. /**
  311. * Save group action hook.
  312. *
  313. * @param type $group
  314. */
  315. function wpcf_cd_save_group_action($group) {
  316. if (!empty($group['conditional_display'])) {
  317. update_post_meta($group['id'], '_wpcf_conditional_display',
  318. $group['conditional_display']);
  319. } else {
  320. update_post_meta($group['id'], '_wpcf_conditional_display', array());
  321. }
  322. }
  323. /**
  324. * Inline JS.
  325. */
  326. function wpcf_cd_admin_form_js() {
  327. ?>
  328. <script type="text/javascript">
  329. jQuery(document).ready(function(){
  330. jQuery('.wpcf-cd-fieldset, #wpcf-cd-group').each(function(){
  331. if (jQuery(this).find('.wpcf-cd-entry').length > 1) {
  332. jQuery(this).find('.toggle-cd').show();
  333. jQuery(this).find('.wpcf-cd-relation').show();
  334. }
  335. });
  336. });
  337. function wpcfCdCreateSummary(id) {
  338. var condition = '';
  339. var skip = true;
  340. jQuery('#'+id).parents('fieldset, #wpcf-cd-group').find('.wpcf-cd-entry').each(function(){
  341. // if (jQuery(this).parent().find('.wpcf-cd-relation').length > 0) {
  342. if (!skip) {
  343. condition += jQuery(this).parent().parent().find('input[type=radio]:checked').val() + ' ';
  344. }
  345. skip = false;
  346. // }
  347. condition += '($'+jQuery(this).find('.wpcf-cd-field').val();
  348. condition += ' ' + jQuery(this).find('.wpcf-cd-operation').val();
  349. condition += ' ' + jQuery(this).find('.wpcf-cd-value').val() + ') ';
  350. });
  351. jQuery('#'+id).val(condition);
  352. }
  353. function wpcfCdAddCondition(object, isGroup) {
  354. if (object.parent().parent().find('.wpcf-cd-entry').length > 0) {
  355. object.parent().parent().find('.toggle-cd').show();
  356. object.parent().parent().find('.wpcf-cd-relation').show();
  357. }
  358. var url = '<?php
  359. $group_id = isset($_GET['group_id']) ? '&group_id=' . $_GET['group_id'] : '';
  360. echo admin_url('admin-ajax.php?action=wpcf_ajax&wpcf_action=add_condition'
  361. . $group_id . '&_wpnonce='
  362. . wp_create_nonce('add_condition'));
  363. ?>&count='+object.parent().parent().find('input[type=hidden]').val();
  364. if (isGroup) {
  365. url += '&group=1';
  366. } else {
  367. url += '&field='+object.parent().parent().attr('id');
  368. }
  369. jQuery.get(url, function(data) {
  370. if (typeof data.output != 'undefined') {
  371. object.parent().find('.wpcf-cd-entries').append(data.output);
  372. var count = object.parent().find('input[type=hidden]').val();
  373. object.parent().find('input[type=hidden]').val(parseInt(count)+1);
  374. }
  375. }, "json");
  376. }
  377. function wpcfCdRemoveCondition(object) {
  378. object.parent().fadeOut(function(){jQuery(this).remove();});
  379. var count = object.parent().parent().parent().find('input[type=hidden]').val();
  380. object.parent().parent().parent().find('input[type=hidden]').val(parseInt(count)-1);
  381. if (object.parent().parent().find('.wpcf-cd-entry').length < 3) {
  382. var customConditions = object.parent().parent().parent().find('.toggle-cd');
  383. customConditions.hide().find('.checkbox').removeAttr('checked');
  384. customConditions.find('.textarea').val('');
  385. object.parent().parent().parent().find('.wpcf-cd-relation').hide();
  386. }
  387. }
  388. </script>
  389. <?php
  390. }