PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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