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

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

https://bitbucket.org/JacobTyler/fame4good-wp
PHP | 1501 lines | 1154 code | 125 blank | 222 comment | 357 complexity | 554142e0ac907bf37d4fbd91cfe4b7be 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. * Edit post page functions
  4. */
  5. require_once WPCF_EMBEDDED_ABSPATH . '/includes/conditional-display.php';
  6. /**
  7. * Init functions for post edit pages.
  8. *
  9. * @param type $upgrade
  10. */
  11. function wpcf_admin_post_init($post = false) {
  12. wpcf_admin_add_js_settings('wpcf_nonce_toggle_group',
  13. '\'' . wp_create_nonce('group_form_collapsed') . '\'');
  14. wpcf_admin_add_js_settings('wpcf_nonce_toggle_fieldset',
  15. '\'' . wp_create_nonce('form_fieldset_toggle') . '\'');
  16. // Get post_type
  17. if ($post) {
  18. $post_type = get_post_type($post);
  19. } else {
  20. if (!isset($_GET['post_type'])) {
  21. $post_type = 'post';
  22. } else if (in_array($_GET['post_type'],
  23. get_post_types(array('show_ui' => true)))) {
  24. $post_type = $_GET['post_type'];
  25. } else {
  26. return false;
  27. }
  28. }
  29. // Add items to View dropdown
  30. if (in_array($post_type, array('view', 'view-template'))) {
  31. add_filter('editor_addon_menus_wpv-views',
  32. 'wpcf_admin_post_editor_addon_menus_filter');
  33. add_action('admin_footer', 'wpcf_admin_post_js_validation');
  34. }
  35. // Never show on 'Views' and 'View Templates'
  36. if (in_array($post_type, array('view', 'view-template'))) {
  37. return false;
  38. }
  39. // Add marketing box
  40. if (!in_array($post_type, array('post', 'page'))) {
  41. add_meta_box('wpcf-marketing',
  42. __('How-To Display Custom Content', 'wpcf'),
  43. 'wpcf_admin_post_marketing_meta_box', $post_type, 'side', 'high');
  44. }
  45. // Get groups
  46. $groups = wpcf_admin_post_get_post_groups_fields($post);
  47. $wpcf_active = false;
  48. foreach ($groups as $key => $group) {
  49. if (!empty($group['fields'])) {
  50. $wpcf_active = true;
  51. // Process fields
  52. $group['fields'] = wpcf_admin_post_process_fields($post,
  53. $group['fields'], true);
  54. }
  55. // Add meta boxes
  56. add_meta_box($group['slug'],
  57. wpcf_translate('group ' . $group['id'] . ' name', $group['name']),
  58. 'wpcf_admin_post_meta_box', $post_type,
  59. $group['meta_box_context'], 'high', $group);
  60. }
  61. // Activate scripts
  62. if ($wpcf_active) {
  63. wp_enqueue_script('wpcf-fields-post',
  64. WPCF_EMBEDDED_RES_RELPATH . '/js/fields-post.js',
  65. array('jquery'), WPCF_VERSION);
  66. wp_enqueue_script('wpcf-form-validation',
  67. WPCF_EMBEDDED_RES_RELPATH . '/js/'
  68. . 'jquery-form-validation/jquery.validate.min.js',
  69. array('jquery'), WPCF_VERSION);
  70. wp_enqueue_script('wpcf-form-validation-additional',
  71. WPCF_EMBEDDED_RES_RELPATH . '/js/'
  72. . 'jquery-form-validation/additional-methods.min.js',
  73. array('jquery'), WPCF_VERSION);
  74. wp_enqueue_style('wpcf-fields-basic',
  75. WPCF_EMBEDDED_RES_RELPATH . '/css/basic.css', array(),
  76. WPCF_VERSION);
  77. wp_enqueue_style('wpcf-fields-post',
  78. WPCF_EMBEDDED_RES_RELPATH . '/css/fields-post.css',
  79. array('wpcf-fields-basic'), WPCF_VERSION);
  80. add_action('admin_footer', 'wpcf_admin_post_js_validation');
  81. }
  82. do_action('wpcf_admin_post_init', $post_type, $post, $groups, $wpcf_active);
  83. }
  84. /**
  85. * Renders meta box content.
  86. *
  87. * @param type $post
  88. * @param type $group
  89. */
  90. function wpcf_admin_post_meta_box($post, $group) {
  91. if (!empty($group['args']['_conditional_display'])) {
  92. if ($group['args']['_conditional_display'] == 'failed') {
  93. echo '<div class="wpcf-cd-group wpcf-cd-group-failed" style="display:none;">';
  94. } else {
  95. echo '<div class="wpcf-cd-group wpcf-cd-group-passed">';
  96. }
  97. }
  98. if (!empty($group['args']['fields'])) {
  99. // Display description
  100. if (!empty($group['args']['description'])) {
  101. echo '<div class="wpcf-meta-box-description">'
  102. . wpautop(wpcf_translate('group ' . $group['args']['id'] . ' description',
  103. $group['args']['description'])) . '</div>';
  104. }
  105. foreach ($group['args']['fields'] as $field_slug => $field) {
  106. if (empty($field)) {
  107. continue;
  108. }
  109. // Render form elements
  110. if (wpcf_compare_wp_version() && $field['#type'] == 'wysiwyg') {
  111. // Especially for WYSIWYG
  112. echo '<div class="wpcf-wysiwyg">';
  113. echo '<div id="wpcf-textarea-textarea-wrapper" class="form-item form-item-textarea wpcf-form-item wpcf-form-item-textarea">';
  114. echo isset($field['#before']) ? $field['#before'] : '';
  115. echo '
  116. <label class="wpcf-form-label wpcf-form-textarea-label">' . $field['#title'] . '</label>';
  117. echo '<div class="description wpcf-form-description wpcf-form-description-textarea description-textarea">
  118. ' . wpautop($field['#description']) . '</div>';
  119. wp_editor($field['#value'], $field['#id'],
  120. $field['#editor_settings']);
  121. $field['slug'] = str_replace(WPCF_META_PREFIX . 'wysiwyg-', '',
  122. $field_slug);
  123. $field['type'] = 'wysiwyg';
  124. echo '</div>';
  125. echo isset($field['#after']) ? $field['#after'] : '';
  126. echo '</div><br /><br />';
  127. } else {
  128. if ($field['#type'] == 'wysiwyg') {
  129. $field['#type'] = 'textarea';
  130. }
  131. echo wpcf_form_simple(array($field['#id'] => $field));
  132. }
  133. do_action('wpcf_fields_' . $field_slug . '_meta_box_form', $field);
  134. if (isset($field['wpcf-type'])) { // May be ignored
  135. do_action('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form',
  136. $field);
  137. }
  138. }
  139. }
  140. if (!empty($group['args']['_conditional_display'])) {
  141. echo '</div>';
  142. }
  143. }
  144. /**
  145. * save_post hook.
  146. *
  147. * @param type $post_ID
  148. * @param type $post
  149. */
  150. function wpcf_admin_post_save_post_hook($post_ID, $post) {
  151. if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'],
  152. 'update-' . $post->post_type . '_' . $post_ID)) {
  153. return false;
  154. }
  155. if (in_array($post->post_type,
  156. array('revision', 'attachment', 'wp-types-group', 'view',
  157. 'view-template'))) {
  158. return false;
  159. }
  160. // Get groups
  161. $groups = wpcf_admin_post_get_post_groups_fields($post);
  162. if (empty($groups)) {
  163. return false;
  164. }
  165. $all_fields = array();
  166. foreach ($groups as $group) {
  167. // Process fields
  168. $fields = wpcf_admin_post_process_fields($post, $group['fields'], true,
  169. false, 'validation');
  170. // Validate fields
  171. $form = wpcf_form_simple_validate($fields);
  172. $all_fields = $all_fields + $fields;
  173. $error = $form->isError();
  174. // Trigger form error
  175. if ($error) {
  176. wpcf_admin_message_store(
  177. __('Please check your input data', 'wpcf'), 'error');
  178. }
  179. }
  180. // Save invalid elements so user can be informed after redirect
  181. if (!empty($all_fields)) {
  182. update_post_meta($post_ID, 'wpcf-invalid-fields', $all_fields);
  183. }
  184. // Save meta fields
  185. if (!empty($_POST['wpcf'])) {
  186. foreach ($_POST['wpcf'] as $field_slug => $field_value) {
  187. // Skip copied fields
  188. if (isset($_POST['wpcf_repetitive_copy'][$field_slug])) {
  189. continue;
  190. }
  191. $field = wpcf_fields_get_field_by_slug($field_slug);
  192. if (empty($field)) {
  193. continue;
  194. }
  195. $meta_key = wpcf_types_get_meta_prefix($field) . $field_slug;
  196. // Don't save invalid
  197. if (isset($all_fields[$meta_key]['#error'])) {
  198. continue;
  199. }
  200. // Repetitive fields
  201. if (isset($_POST['wpcf_repetitive'][$field_slug])) {
  202. delete_post_meta($post_ID, $meta_key);
  203. foreach ($field_value as $temp_id => $repetitive_data) {
  204. // Skype specific
  205. if ($field['type'] == 'skype') {
  206. unset($repetitive_data['old_value']);
  207. wpcf_admin_post_save_field($post_ID, $meta_key, $field,
  208. $repetitive_data, true);
  209. } else {
  210. wpcf_admin_post_save_field($post_ID, $meta_key, $field,
  211. $repetitive_data['new_value'], true);
  212. }
  213. }
  214. } else {
  215. wpcf_admin_post_save_field($post_ID, $meta_key, $field,
  216. $field_value);
  217. }
  218. }
  219. }
  220. // Process checkboxes
  221. foreach ($all_fields as $field) {
  222. if (!isset($field['#type'])) {
  223. continue;
  224. }
  225. if ($field['#type'] == 'checkbox'
  226. && !isset($_POST['wpcf'][$field['wpcf-slug']])) {
  227. $field_data = wpcf_admin_fields_get_field($field['wpcf-id']);
  228. if (isset($field_data['data']['save_empty'])
  229. && $field_data['data']['save_empty'] == 'yes') {
  230. update_post_meta($post_ID,
  231. wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'],
  232. 0);
  233. } else {
  234. delete_post_meta($post_ID,
  235. wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']);
  236. }
  237. }
  238. if ($field['#type'] == 'checkboxes') {
  239. $field_data = wpcf_admin_fields_get_field($field['wpcf-id']);
  240. if (!empty($field_data['data']['options'])) {
  241. $update_data = array();
  242. foreach ($field_data['data']['options'] as $option_id => $option_data) {
  243. if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) {
  244. if (isset($field_data['data']['save_empty'])
  245. && $field_data['data']['save_empty'] == 'yes') {
  246. $update_data[$option_id] = 0;
  247. }
  248. } else {
  249. $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id];
  250. }
  251. }
  252. update_post_meta($post_ID,
  253. wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'],
  254. $update_data);
  255. }
  256. }
  257. }
  258. }
  259. /**
  260. * Saves single field.
  261. *
  262. * @param type $post_ID
  263. * @param type $all_fields
  264. * @param type $field_slug
  265. * @param type $field_value
  266. * @param string $old_value
  267. * @return boolean
  268. */
  269. function wpcf_admin_post_save_field($post_ID, $meta_key, $field, $field_value,
  270. $add = false) {
  271. // Apply filters
  272. $field_value = apply_filters('wpcf_fields_value_save', $field_value,
  273. $field['type'], $field['slug'], $field);
  274. $field_value = apply_filters('wpcf_fields_slug_' . $field['slug']
  275. . '_value_save', $field_value, $field);
  276. $field_value = apply_filters('wpcf_fields_type_' . $field['type']
  277. . '_value_save', $field_value, $field);
  278. // Save field
  279. if ($add) {
  280. add_post_meta($post_ID, $meta_key, $field_value);
  281. } else {
  282. update_post_meta($post_ID, $meta_key, $field_value);
  283. }
  284. do_action('wpcf_fields_save', $field_value, $field);
  285. do_action('wpcf_fields_slug_' . $field['slug'] . '_save', $field_value,
  286. $field);
  287. do_action('wpcf_fields_type_' . $field['type'] . '_save', $field_value,
  288. $field);
  289. }
  290. /**
  291. * Renders JS validation script.
  292. */
  293. function wpcf_admin_post_js_validation() {
  294. wpcf_form_render_js_validation('#post');
  295. ?>
  296. <script type="text/javascript">
  297. //<![CDATA[
  298. function wpcfFieldsEditorCallback(field_id) {
  299. var url = "<?php echo admin_url('admin-ajax.php'); ?>?action=wpcf_ajax&wpcf_action=editor_callback&_wpnonce=<?php echo wp_create_nonce('editor_callback'); ?>&field_id="+field_id+"&keepThis=true&TB_iframe=true&height=400&width=400";
  300. tb_show("<?php
  301. _e('Insert field', 'wpcf');
  302. ?>", url);
  303. }
  304. var wpcfFieldsEditorCallback_redirect = null;
  305. function wpcfFieldsEditorCallback_set_redirect(function_name, params) {
  306. wpcfFieldsEditorCallback_redirect = {'function' : function_name, 'params' : params};
  307. }
  308. //]]>
  309. </script>
  310. <?php
  311. }
  312. /**
  313. * Creates form elements.
  314. *
  315. * @param type $post
  316. * @param type $fields
  317. * @return type
  318. */
  319. function wpcf_admin_post_process_fields($post = false, $fields = array(),
  320. $use_cache = true, $add_to_editor = true, $context = 'group') {
  321. // Get cached
  322. static $cache = array();
  323. $cache_key = !empty($post->ID) ? $post->ID . md5(serialize($fields)) : false;
  324. if ($use_cache && $cache_key && isset($cache[$cache_key])) {
  325. return $cache[$cache_key];
  326. }
  327. $fields_processed = array();
  328. // Get invalid fields (if submitted)
  329. $invalid_fields = array();
  330. if (!empty($post->ID)) {
  331. $invalid_fields = get_post_meta($post->ID, 'wpcf-invalid-fields', true);
  332. delete_post_meta($post->ID, 'wpcf-invalid-fields');
  333. }
  334. $original_cf = array();
  335. if (function_exists('wpml_get_copied_fields_for_post_edit')) {
  336. $original_cf = wpml_get_copied_fields_for_post_edit();
  337. }
  338. foreach ($fields as $field) {
  339. // Repetitive fields
  340. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
  341. $temp_flag = false;
  342. // First check if repetitive fields are copied using WPML
  343. if (!empty($original_cf['fields'])) {
  344. // Check if marked
  345. if (in_array(wpcf_types_get_meta_prefix($field) . $field['slug'],
  346. $original_cf['fields'])) {
  347. // Get original post field values
  348. $temp_fields = get_post_meta($original_cf['original_post_id'],
  349. wpcf_types_get_meta_prefix($field) . $field['slug'],
  350. false);
  351. // If there are original field values stored
  352. if (!empty($temp_fields)) {
  353. foreach ($temp_fields as $temp_key => $temp_field) {
  354. $single_field = $field;
  355. $single_field['wpml_action'] = 'copy';
  356. $single_field['value'] = $temp_field;
  357. $temp_processed = wpcf_admin_post_process_field($post,
  358. $single_field, $use_cache, $add_to_editor,
  359. $context);
  360. if ($temp_processed) {
  361. $single_field = $temp_processed['field'];
  362. $element = $temp_processed['element'];
  363. if ($field['type'] == 'skype') {
  364. foreach ($element as $temp_element_key => $temp_element_value) {
  365. $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field',
  366. $temp_element_value, $field,
  367. $post, $context, $original_cf,
  368. $invalid_fields);
  369. }
  370. } else {
  371. $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field',
  372. $element, $single_field, $post,
  373. $context, $original_cf,
  374. $invalid_fields);
  375. }
  376. }
  377. }
  378. // If there are no original fields stored
  379. // display empty element
  380. } else {
  381. $single_field = $field;
  382. $single_field['wpml_action'] = 'copy';
  383. $single_field['value'] = null;
  384. $temp_processed = wpcf_admin_post_process_field($post,
  385. $single_field, $use_cache, $add_to_editor,
  386. $context);
  387. if ($temp_processed) {
  388. $single_field = $temp_processed['field'];
  389. $element = $temp_processed['element'];
  390. if ($field['type'] == 'skype') {
  391. foreach ($element as $temp_element_key => $temp_element_value) {
  392. $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field',
  393. $temp_element_value, $field, $post,
  394. $context, $original_cf,
  395. $invalid_fields);
  396. }
  397. } else {
  398. $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field',
  399. $element, $single_field, $post,
  400. $context, $original_cf, $invalid_fields);
  401. }
  402. }
  403. }
  404. } else {
  405. $temp_flag = true;
  406. }
  407. } else {
  408. $temp_flag = true;
  409. }
  410. // Get repetitive fields values
  411. if ($temp_flag && !empty($post->ID)) {
  412. $temp_flag = false;
  413. $temp_fields = get_post_meta($post->ID,
  414. wpcf_types_get_meta_prefix($field) . $field['slug'],
  415. false);
  416. if (!empty($temp_fields)) {
  417. $temp_start = true;
  418. $fields_processed[$field['id'] . '_repetitive_wrapper_open'] = array(
  419. '#type' => 'markup',
  420. '#markup' => '<div id="wpcf_'
  421. . $field['id']
  422. . '_repetitive_wrapper_' . mt_rand()
  423. . '" class="wpcf-repetitive-wrapper">',
  424. '#id' => $field['id'] . '_repetitive_wrapper_open',
  425. );
  426. foreach ($temp_fields as $temp_key => $temp_field) {
  427. $single_field = $field;
  428. $single_field['value'] = $temp_field;
  429. $temp_processed = wpcf_admin_post_process_field($post,
  430. $single_field, $use_cache, $add_to_editor,
  431. $context);
  432. if ($temp_processed) {
  433. $single_field = $temp_processed['field'];
  434. $element = $temp_processed['element'];
  435. if ($field['type'] == 'skype') {
  436. foreach ($element as $temp_element_key => $temp_element_value) {
  437. if (!$temp_start && !in_array($field['type'],
  438. array('checkbox'))) {
  439. unset($temp_element_value['#title']);
  440. }
  441. $temp_start = false;
  442. $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field',
  443. $temp_element_value, $field, $post,
  444. $context, $original_cf,
  445. $invalid_fields);
  446. }
  447. } else {
  448. if (!$temp_start && !in_array($field['type'],
  449. array('checkbox'))) {
  450. unset($element['#title']);
  451. }
  452. $temp_start = false;
  453. $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field',
  454. $element, $single_field, $post,
  455. $context, $original_cf, $invalid_fields);
  456. }
  457. }
  458. }
  459. $fields_processed[$field['id'] . '_repetitive_wrapper_close'] = array(
  460. '#type' => 'markup',
  461. '#markup' => '</div>',
  462. '#id' => $field['id'] . '_repetitive_wrapper_close',
  463. );
  464. } else {
  465. $temp_flag = true;
  466. }
  467. }
  468. // Temp flag for repetitive field is triggered if post is new
  469. // and field is not marked to be copied
  470. if ($temp_flag == true) {
  471. $temp_processed = wpcf_admin_post_process_field($post, $field,
  472. $use_cache, $add_to_editor, $context);
  473. if ($temp_processed) {
  474. $fields_processed[$field['id'] . '_repetitive_wrapper_open'] = array(
  475. '#type' => 'markup',
  476. '#markup' => '<div id="wpcf_'
  477. . $field['id']
  478. . '_repetitive_wrapper_' . mt_rand()
  479. . '" class="wpcf-repetitive-wrapper">',
  480. '#id' => $field['id'] . '_repetitive_wrapper_open',
  481. );
  482. $field = $temp_processed['field'];
  483. $element = $temp_processed['element'];
  484. if ($field['type'] == 'skype') {
  485. foreach ($element as $temp_element_key => $temp_element_value) {
  486. $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field',
  487. $temp_element_value, $field, $post,
  488. $context, $original_cf, $invalid_fields);
  489. }
  490. } else {
  491. $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field',
  492. $element, $field, $post, $context, $original_cf,
  493. $invalid_fields);
  494. }
  495. $fields_processed[$field['id'] . '_repetitive_wrapper_close'] = array(
  496. '#type' => 'markup',
  497. '#markup' => '</div>',
  498. '#id' => $field['id'] . '_repetitive_wrapper_close',
  499. );
  500. }
  501. }
  502. // Non-repetitive fields
  503. } else {
  504. if (!empty($post->ID)) {
  505. $field['value'] = get_post_meta($post->ID,
  506. wpcf_types_get_meta_prefix($field) . $field['slug'],
  507. true);
  508. }
  509. // Check if repetitive field is copied using WPML
  510. if (!empty($original_cf['fields'])) {
  511. if (in_array(wpcf_types_get_meta_prefix($field) . $field['slug'],
  512. $original_cf['fields'])) {
  513. $field['wpml_action'] = 'copy';
  514. $field['value'] = get_post_meta($original_cf['original_post_id'],
  515. wpcf_types_get_meta_prefix($field) . $field['slug'],
  516. true);
  517. }
  518. }
  519. $temp_processed = wpcf_admin_post_process_field($post, $field,
  520. $use_cache, $add_to_editor, $context);
  521. if ($temp_processed) {
  522. $field = $temp_processed['field'];
  523. $element = $temp_processed['element'];
  524. if ($field['type'] == 'skype') {
  525. foreach ($element as $temp_element_key => $temp_element_value) {
  526. $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field',
  527. $temp_element_value, $field, $post, $context,
  528. $original_cf, $invalid_fields);
  529. }
  530. } else {
  531. $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field',
  532. $element, $field, $post, $context, $original_cf,
  533. $invalid_fields);
  534. }
  535. }
  536. }
  537. }
  538. // Cache results
  539. if ($cache_key) {
  540. $cache[$cache_key] = $fields_processed;
  541. }
  542. return $fields_processed;
  543. }
  544. /**
  545. * Processes single field.
  546. *
  547. * @staticvar array $repetitive_started
  548. * @param type $post
  549. * @param type $field
  550. * @param type $use_cache
  551. * @param type $add_to_editor
  552. * @param type $context
  553. * @param type $original_cf
  554. * @param type $invalid_fields
  555. * @return mixed boolean|array
  556. */
  557. function wpcf_admin_post_process_field($post = false, $field_unedited = array(),
  558. $use_cache = true, $add_to_editor = true, $context = 'group',
  559. $original_cf = array(), $invalid_fields = array()) {
  560. $field = wpcf_admin_fields_get_field($field_unedited['id']);
  561. if (!empty($field)) {
  562. // Set values
  563. $field['value'] = isset($field_unedited['value']) ? maybe_unserialize($field_unedited['value']) : '';
  564. $field['wpml_action'] = isset($field_unedited['wpml_action']) ? $field_unedited['wpml_action'] : '';
  565. $field_id = 'wpcf-' . $field['type'] . '-' . $field['slug'] . '-' . mt_rand();
  566. $field_init_data = wpcf_fields_type_action($field['type']);
  567. // Get inherited field
  568. $inherited_field_data = false;
  569. if (isset($field_init_data['inherited_field_type'])) {
  570. $inherited_field_data = wpcf_fields_type_action($field_init_data['inherited_field_type']);
  571. }
  572. // Apply filters
  573. $field['value'] = apply_filters('wpcf_fields_value_get',
  574. $field['value'], $field, $field_init_data);
  575. $field['value'] = apply_filters('wpcf_fields_slug_' . $field['slug']
  576. . '_value_get', $field['value'], $field, $field_init_data);
  577. $field['value'] = apply_filters('wpcf_fields_type_' . $field['type']
  578. . '_value_get', $field['value'], $field, $field_init_data);
  579. wpcf_admin_post_field_load_js_css($field_init_data);
  580. $element = array();
  581. // Set generic values
  582. $element = array(
  583. '#type' => isset($field_init_data['inherited_field_type']) ? $field_init_data['inherited_field_type'] : $field['type'],
  584. '#id' => $field_id,
  585. '#title' => wpcf_translate('field ' . $field['id'] . ' name',
  586. $field['name']),
  587. '#description' => wpautop(wpcf_translate('field ' . $field['id'] . ' description',
  588. $field['description'])),
  589. '#name' => 'wpcf[' . $field['slug'] . ']',
  590. '#value' => isset($field['value']) ? $field['value'] : '',
  591. 'wpcf-id' => $field['id'],
  592. 'wpcf-slug' => $field['slug'],
  593. 'wpcf-type' => $field['type'],
  594. );
  595. // Set inherited values
  596. $element_inherited = array();
  597. if ($inherited_field_data) {
  598. if (function_exists('wpcf_fields_'
  599. . $field_init_data['inherited_field_type']
  600. . '_meta_box_form')) {
  601. $element_inherited = call_user_func_array('wpcf_fields_'
  602. . $field_init_data['inherited_field_type']
  603. . '_meta_box_form', array($field, $element));
  604. }
  605. }
  606. $element = array_merge($element, $element_inherited);
  607. if (isset($field['description_extra'])) {
  608. $element['#description'] .= wpautop($field['description_extra']);
  609. }
  610. // Set atributes #1
  611. if (isset($field['disable'])) {
  612. $field['#disable'] = $field['disable'];
  613. }
  614. if (!empty($field['disable'])) {
  615. $field['#attributes']['disabled'] = 'disabled';
  616. }
  617. if (!empty($field['readonly'])) {
  618. $field['#attributes']['readonly'] = 'readonly';
  619. }
  620. // Set specific values
  621. if (defined('WPCF_INC_ABSPATH')
  622. && file_exists(WPCF_INC_ABSPATH . '/fields/' . $field['type']
  623. . '.php')) {
  624. require_once WPCF_INC_ABSPATH . '/fields/' . $field['type']
  625. . '.php';
  626. }
  627. // Load field
  628. if (function_exists('wpcf_fields_' . $field['type']
  629. . '_meta_box_form')) {
  630. $element_specific = call_user_func_array('wpcf_fields_'
  631. . $field['type'] . '_meta_box_form', array($field, $element));
  632. // Check if it's single
  633. if (isset($element_specific['#type'])) {
  634. // Format description
  635. if (!empty($element_specific['#description'])) {
  636. $element_specific['#description'] = wpautop($element_specific['#description']);
  637. }
  638. $element = array_merge($element, $element_specific);
  639. // Set validation element
  640. if (isset($field['data']['validate'])) {
  641. $element['#validate'] = $field['data']['validate'];
  642. }
  643. // Repetitive fields
  644. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
  645. $element = wpcf_admin_post_process_repetitive_field($post,
  646. $field, $element);
  647. }
  648. } else { // More fields, loop all
  649. // Only Skype for now have multiple fields, so process only that
  650. if ($field['type'] == 'skype') {
  651. $skype_element = array();
  652. foreach ($element_specific as $element_specific_fields_key => $element_specific_fields_value) {
  653. $element_specific_fields_value['__element_key'] = $element_specific_fields_key;
  654. // Format description
  655. if (!empty($element_specific_fields_value['#description'])) {
  656. $element_specific_fields_value['#description'] = wpautop($element_specific_fields_value['#description']);
  657. }
  658. // If no ID
  659. if (!isset($element_specific_fields_value['#id'])) {
  660. $element_specific_fields_value['#id'] = 'wpcf-'
  661. . $field['slug'] . '-' . mt_rand();
  662. }
  663. // Set validation element
  664. if (!empty($element_specific_fields_value['#_validate_this']) && isset($field['data']['validate'])) {
  665. $element_specific_fields_value['#validate'] = $field['data']['validate'];
  666. }
  667. if ($element_specific_fields_key != 'skypename') {
  668. if (!isset($element_specific_fields_value['#name'])) {
  669. $element_specific_fields_value['#name'] = 'wpcf[ignore]['
  670. . mt_rand() . ']';
  671. }
  672. $skype_element[$element_specific_fields_value['#id']] = $element_specific_fields_value;
  673. continue;
  674. }
  675. // This one is actually value and keep it (#name is required)
  676. $element = array_merge($element,
  677. $element_specific_fields_value);
  678. // Add it here to keep order
  679. $skype_element[$element['#id']] = $element;
  680. }
  681. // Repetitive fields
  682. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
  683. list($element, $skype_element) = wpcf_admin_post_process_repetitive_field_skype($post,
  684. $field, $skype_element);
  685. }
  686. }
  687. }
  688. } else {
  689. // Repetitive fields
  690. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
  691. $element = wpcf_admin_post_process_repetitive_field($post,
  692. $field, $element);
  693. }
  694. }
  695. // Set atributes #2 (override)
  696. if (isset($field['disable'])) {
  697. $element['#disable'] = $field['disable'];
  698. }
  699. if (!empty($field['disable'])) {
  700. $element['#attributes']['disabled'] = 'disabled';
  701. }
  702. if (!empty($field['readonly'])) {
  703. $element['#attributes']['readonly'] = 'readonly';
  704. if (!empty($element['#options'])) {
  705. foreach ($element['#options'] as $key => $option) {
  706. if (!is_array($option)) {
  707. $element['#options'][$key] = array(
  708. '#title' => $key,
  709. '#value' => $option,
  710. );
  711. }
  712. $element['#options'][$key]['#attributes']['readonly'] = 'readonly';
  713. if ($element['#type'] == 'select') {
  714. $element['#options'][$key]['#attributes']['disabled'] = 'disabled';
  715. }
  716. }
  717. }
  718. if ($element['#type'] == 'select') {
  719. $element['#attributes']['disabled'] = 'disabled';
  720. }
  721. }
  722. // Set validation element
  723. if ($field['type'] != 'skype' && empty($element['#validate']) && isset($field['data']['validate'])) {
  724. $element['#validate'] = $field['data']['validate'];
  725. }
  726. // Check if it was invalid on submit and add error message
  727. if ($post && !empty($invalid_fields)) {
  728. if (isset($invalid_fields[$element['#id']]['#error'])) {
  729. $element['#error'] = $invalid_fields[$element['#id']]['#error'];
  730. }
  731. }
  732. // Set WPML locked icon
  733. if (isset($field['wpml_action']) && $field['wpml_action'] == 'copy') {
  734. $element['#title'] .= '<img src="' . WPCF_EMBEDDED_RES_RELPATH . '/images/locked.png" alt="'
  735. . __('This field is locked for editing because WPML will copy its value from the original language.',
  736. 'wpcf') . '" title="'
  737. . __('This field is locked for editing because WPML will copy its value from the original language.',
  738. 'wpcf') . '" style="position:relative;left:2px;top:2px;" />';
  739. }
  740. // Add to editor
  741. if ($add_to_editor) {
  742. wpcf_admin_post_add_to_editor($field);
  743. }
  744. // Add repetitive class
  745. // @TODO Why not add repetitive class if copied?
  746. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship'
  747. && (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy')) {
  748. if (!empty($element['#options']) && $element['#type'] != 'select') {
  749. foreach ($element['#options'] as $temp_key => $temp_value) {
  750. $element['#options'][$temp_key]['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
  751. }
  752. } else {
  753. $element['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
  754. }
  755. wpcf_admin_add_js_settings('wpcfFormRepetitiveUniqueValuesCheckText',
  756. '\'' . __('Warning: same values set', 'wpcf') . '\'');
  757. }
  758. // Set read-only if copied by WPML
  759. if (isset($field['wpml_action']) && $field['wpml_action'] == 'copy') {
  760. if (isset($element['#options'])) {
  761. foreach ($element['#options'] as $temp_key => $temp_value) {
  762. if (isset($temp_value['#attributes'])) {
  763. $element['#options'][$temp_key]['#attributes']['readonly'] = 'readonly';
  764. } else {
  765. $element['#options'][$temp_key]['#attributes'] = array('readonly' => 'readonly');
  766. }
  767. }
  768. }
  769. if ($field['type'] == 'select') {
  770. if (isset($element['#attributes'])) {
  771. $element['#attributes']['disabled'] = 'disabled';
  772. } else {
  773. $element['#attributes'] = array('disabled' => 'disabled');
  774. }
  775. } else {
  776. if (isset($element['#attributes'])) {
  777. $element['#attributes']['readonly'] = 'readonly';
  778. } else {
  779. $element['#attributes'] = array('readonly' => 'readonly');
  780. }
  781. }
  782. }
  783. // Specific for Skype
  784. if ($field['type'] == 'skype') {
  785. $skype_element[$element['#id']] = $element;
  786. $element = $skype_element;
  787. }
  788. return array('field' => $field, 'element' => $element);
  789. }
  790. return false;
  791. }
  792. /**
  793. * Gets all groups and fields for post.
  794. *
  795. * @param type $post_ID
  796. * @return type
  797. */
  798. function wpcf_admin_post_get_post_groups_fields($post = false,
  799. $context = 'group') {
  800. // Get post_type
  801. if (!empty($post)) {
  802. $post_type = get_post_type($post);
  803. } else {
  804. if (!isset($_GET['post_type'])) {
  805. $post_type = 'post';
  806. } else if (in_array($_GET['post_type'],
  807. get_post_types(array('show_ui' => true)))) {
  808. $post_type = $_GET['post_type'];
  809. } else {
  810. $post_type = 'post';
  811. }
  812. }
  813. // Get post terms
  814. $support_terms = false;
  815. if (!empty($post)) {
  816. $post->_wpcf_post_terms = array();
  817. $taxonomies = get_taxonomies('', 'objects');
  818. if (!empty($taxonomies)) {
  819. foreach ($taxonomies as $tax_slug => $tax) {
  820. $temp_tax = get_taxonomy($tax_slug);
  821. if (!in_array($post_type, $temp_tax->object_type)) {
  822. continue;
  823. }
  824. $support_terms = true;
  825. $terms = wp_get_post_terms($post->ID, $tax_slug,
  826. array('fields' => 'ids'));
  827. foreach ($terms as $term_id) {
  828. $post->_wpcf_post_terms[] = $term_id;
  829. }
  830. }
  831. }
  832. }
  833. // Get post template
  834. if (empty($post)) {
  835. $post = new stdClass();
  836. $post->_wpcf_post_template = false;
  837. $post->_wpcf_post_views_template = false;
  838. } else {
  839. $post->_wpcf_post_template = get_post_meta($post->ID,
  840. '_wp_page_template', true);
  841. $post->_wpcf_post_views_template = get_post_meta($post->ID,
  842. '_views_template', true);
  843. }
  844. if (empty($post->_wpcf_post_terms)) {
  845. $post->_wpcf_post_terms = array();
  846. }
  847. // Filter groups
  848. $groups = array();
  849. $groups_all = wpcf_admin_fields_get_groups();
  850. foreach ($groups_all as $temp_key => $temp_group) {
  851. if (empty($temp_group['is_active'])) {
  852. unset($groups_all[$temp_key]);
  853. continue;
  854. }
  855. // Get filters
  856. $groups_all[$temp_key]['_wp_types_group_post_types'] = explode(',',
  857. trim(get_post_meta($temp_group['id'],
  858. '_wp_types_group_post_types', true), ','));
  859. $groups_all[$temp_key]['_wp_types_group_terms'] = explode(',',
  860. trim(get_post_meta($temp_group['id'], '_wp_types_group_terms',
  861. true), ','));
  862. $groups_all[$temp_key]['_wp_types_group_templates'] = explode(',',
  863. trim(get_post_meta($temp_group['id'],
  864. '_wp_types_group_templates', true), ','));
  865. $post_type_filter = $groups_all[$temp_key]['_wp_types_group_post_types'][0] == 'all' ? -1 : 0;
  866. $taxonomy_filter = $groups_all[$temp_key]['_wp_types_group_terms'][0] == 'all' ? -1 : 0;
  867. $template_filter = $groups_all[$temp_key]['_wp_types_group_templates'][0] == 'all' ? -1 : 0;
  868. // See if post type matches
  869. if ($post_type_filter == 0 && in_array($post_type,
  870. $groups_all[$temp_key]['_wp_types_group_post_types'])) {
  871. $post_type_filter = 1;
  872. }
  873. // See if terms match
  874. if ($taxonomy_filter == 0) {
  875. foreach ($post->_wpcf_post_terms as $temp_post_term) {
  876. if (in_array($temp_post_term,
  877. $groups_all[$temp_key]['_wp_types_group_terms'])) {
  878. $taxonomy_filter = 1;
  879. }
  880. }
  881. }
  882. // See if template match
  883. if ($template_filter == 0) {
  884. if ((!empty($post->_wpcf_post_template) && in_array($post->_wpcf_post_template,
  885. $groups_all[$temp_key]['_wp_types_group_templates']))
  886. || (!empty($post->_wpcf_post_views_template) && in_array($post->_wpcf_post_views_template,
  887. $groups_all[$temp_key]['_wp_types_group_templates']))) {
  888. $template_filter = 1;
  889. }
  890. }
  891. // Filter by association
  892. if (empty($groups_all[$temp_key]['filters_association'])) {
  893. $groups_all[$temp_key]['filters_association'] = 'any';
  894. }
  895. // If context is post_relationship allow all groups that match post type
  896. if ($context == 'post_relationships_header') {
  897. $groups_all[$temp_key]['filters_association'] = 'any';
  898. }
  899. if ($post_type_filter == -1 && $taxonomy_filter == -1 && $template_filter == -1) {
  900. $passed = 1;
  901. } else if ($groups_all[$temp_key]['filters_association'] == 'any') {
  902. $passed = $post_type_filter == 1 || $taxonomy_filter == 1 || $template_filter == 1;
  903. } else {
  904. $passed = $post_type_filter != 0 && $taxonomy_filter != 0 && $template_filter != 0;
  905. }
  906. if (!$passed) {
  907. unset($groups_all[$temp_key]);
  908. } else {
  909. $groups_all[$temp_key]['fields'] = wpcf_admin_fields_get_fields_by_group($temp_group['id'],
  910. 'slug', true, false, true);
  911. }
  912. }
  913. $groups = apply_filters('wpcf_post_groups', $groups_all, $post, $context);
  914. return $groups;
  915. }
  916. /**
  917. * Stores fields for editor menu.
  918. *
  919. * @staticvar array $fields
  920. * @param type $field
  921. * @return array
  922. */
  923. function wpcf_admin_post_add_to_editor($field) {
  924. static $fields = array();
  925. if ($field == 'get') {
  926. return $fields;
  927. }
  928. if (empty($fields)) {
  929. add_action('admin_enqueue_scripts', 'wpcf_admin_post_add_to_editor_js');
  930. }
  931. $fields[$field['id']] = $field;
  932. }
  933. /**
  934. * Renders JS for editor menu.
  935. *
  936. * @return type
  937. */
  938. function wpcf_admin_post_add_to_editor_js() {
  939. global $post;
  940. $fields = wpcf_admin_post_add_to_editor('get');
  941. $groups = wpcf_admin_post_get_post_groups_fields($post);
  942. if (empty($fields) || empty($groups)) {
  943. return false;
  944. }
  945. $editor_addon = new Editor_addon('types',
  946. __('Insert Types Shortcode', 'wpcf'),
  947. WPCF_EMBEDDED_RES_RELPATH . '/js/types_editor_plugin.js',
  948. WPCF_EMBEDDED_RES_RELPATH . '/images/bw-logo-16.png');
  949. foreach ($groups as $group) {
  950. if (empty($group['fields'])) {
  951. continue;
  952. }
  953. foreach ($group['fields'] as $group_field_id => $group_field) {
  954. if (!isset($fields[$group_field_id])) {
  955. continue;
  956. }
  957. $field = $fields[$group_field_id];
  958. $data = wpcf_fields_type_action($field['type']);
  959. $callback = '';
  960. if (isset($data['editor_callback'])) {
  961. $callback = sprintf($data['editor_callback'], $field['id']);
  962. } else {
  963. // Set callback if function exists
  964. $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
  965. $callback = function_exists($function) ? 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\')' : '';
  966. }
  967. $editor_addon->add_insert_shortcode_menu(stripslashes($field['name']),
  968. trim(wpcf_fields_get_shortcode($field), '[]'),
  969. $group['name'], $callback);
  970. }
  971. }
  972. }
  973. /**
  974. * Adds items to view dropdown.
  975. *
  976. * @param type $items
  977. * @return type
  978. */
  979. function wpcf_admin_post_editor_addon_menus_filter($items) {
  980. $groups = wpcf_admin_fields_get_groups();
  981. $all_post_types = implode(' ', get_post_types(array('public' => true)));
  982. $add = array();
  983. if (!empty($groups)) {
  984. // $group_id is blank therefore not equal to $group['id']
  985. // use array for item key and CSS class
  986. $item_styles = array();
  987. foreach ($groups as $group_id => $group) {
  988. $fields = wpcf_admin_fields_get_fields_by_group($group['id'],
  989. 'slug', true, false, true);
  990. if (!empty($fields)) {
  991. // code from Types used here without breaking the flow
  992. // get post types list for every group or apply all
  993. $post_types = get_post_meta($group['id'],
  994. '_wp_types_group_post_types', true);
  995. if ($post_types == 'all') {
  996. $post_types = $all_post_types;
  997. }
  998. $post_types = trim(str_replace(',', ' ', $post_types));
  999. $item_styles[$group['name']] = $post_types;
  1000. foreach ($fields as $field_id => $field) {
  1001. // Get field data
  1002. $data = wpcf_fields_type_action($field['type']);
  1003. // Get inherited field
  1004. if (isset($data['inherited_field_type'])) {
  1005. $inherited_field_data = wpcf_fields_type_action($data['inherited_field_type']);
  1006. }
  1007. $callback = '';
  1008. if (isset($data['editor_callback'])) {
  1009. $callback = sprintf($data['editor_callback'],
  1010. $field['id']);
  1011. } else {
  1012. // Set callback if function exists
  1013. $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
  1014. $callback = function_exists($function) ? 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\')' : '';
  1015. }
  1016. $add[$group['name']][stripslashes($field['name'])] = array(stripslashes($field['name']), trim(wpcf_fields_get_shortcode($field),
  1017. '[]'), $group['name'], $callback);
  1018. // Process JS
  1019. if (!empty($data['meta_box_js'])) {
  1020. foreach ($data['meta_box_js'] as $handle => $data_script) {
  1021. if (isset($data_script['inline'])) {
  1022. add_action('admin_footer',
  1023. $data_script['inline']);
  1024. continue;
  1025. }
  1026. $deps = !empty($data_script['deps']) ? $data_script['deps'] : array();
  1027. wp_enqueue_script($handle, $data_script['src'],
  1028. $deps, WPCF_VERSION);
  1029. }
  1030. }
  1031. // Process CSS
  1032. if (!empty($data['meta_box_css'])) {
  1033. foreach ($data['meta_box_css'] as $handle => $data_script) {
  1034. $deps = !empty($data_script['deps']) ? $data_script['deps'] : array();
  1035. if (isset($data_script['inline'])) {
  1036. add_action('admin_header',
  1037. $data_script['inline']);
  1038. continue;
  1039. }
  1040. wp_enqueue_style($handle, $data_script['src'],
  1041. $deps, WPCF_VERSION);
  1042. }
  1043. }
  1044. }
  1045. }
  1046. }
  1047. }
  1048. $search_key = '';
  1049. // Iterate all items to be displayed in the "V" menu
  1050. foreach ($items as $key => $item) {
  1051. if ($key == __('Basic', 'wpv-views')) {
  1052. $search_key = 'found';
  1053. continue;
  1054. }
  1055. if ($search_key == 'found') {
  1056. $search_key = $key;
  1057. }
  1058. if ($key == __('Field', 'wpv-views') && isset($item[trim(wpcf_types_get_meta_prefix(),
  1059. '-')])) {
  1060. unset($items[$key][trim(wpcf_types_get_meta_prefix(), '-')]);
  1061. }
  1062. }
  1063. if (empty($search_key) || $search_key == 'found') {
  1064. $search_key = count($items);
  1065. }
  1066. $insert_position = array_search($search_key, array_keys($items));
  1067. $part_one = array_slice($items, 0, $insert_position);
  1068. $part_two = array_slice($items, $insert_position);
  1069. $items = $part_one + $add + $part_two;
  1070. // apply CSS styles to each item based on post types
  1071. foreach ($items as $key => $value) {
  1072. if (isset($item_styles[$key])) {
  1073. $items[$key]['css'] = $item_styles[$key];
  1074. } else {
  1075. $items[$key]['css'] = $all_post_types;
  1076. }
  1077. }
  1078. return $items;
  1079. }
  1080. /**
  1081. * Load JS and CSS for field type.
  1082. *
  1083. * @staticvar array $cache
  1084. * @param type $field_init_data
  1085. * @return string
  1086. */
  1087. function wpcf_admin_post_field_load_js_css($field_init_data) {
  1088. static $cache = array();
  1089. if (isset($cache[$field_init_data['id']])) {
  1090. return '';
  1091. }
  1092. // Process JS
  1093. if (!empty($field_init_data['meta_box_js'])) {
  1094. foreach ($field_init_data['meta_box_js'] as $handle => $data) {
  1095. if (isset($data['inline'])) {
  1096. add_action('admin_footer', $data['inline']);
  1097. continue;
  1098. }
  1099. $deps = !empty($data['deps']) ? $data['deps'] : array();
  1100. $in_footer = !empty($data['in_footer']) ? $data['in_footer'] : false;
  1101. wp_register_script($handle, $data['src'], $deps, WPCF_VERSION,
  1102. $in_footer);
  1103. wp_enqueue_script($handle);
  1104. }
  1105. }
  1106. // Process CSS
  1107. if (!empty($field_init_data['meta_box_css'])) {
  1108. foreach ($field_init_data['meta_box_css'] as $handle => $data) {
  1109. if (isset($data['src'])) {
  1110. $deps = !empty($data['deps']) ? $data['deps'] : array();
  1111. wp_enqueue_style($handle, $data['src'], $deps, WPCF_VERSION);
  1112. } else if (isset($data['inline'])) {
  1113. add_action('admin_head', $data['inline']);
  1114. }
  1115. }
  1116. }
  1117. $cache[$field_init_data['id']] = 1;
  1118. }
  1119. /**
  1120. * Processes repetitive field.
  1121. *
  1122. * @staticvar array $repetitive_started
  1123. * @staticvar array $repetitive_index
  1124. * @param string $field
  1125. * @param type $element
  1126. * @return string
  1127. */
  1128. function wpcf_admin_post_process_repetitive_field($post, $field, $element) {
  1129. static $repetitive_started = array();
  1130. static $repetitive_index = array();
  1131. if (defined('DOING_AJAX')) {
  1132. if ($field['type'] == 'skype') {
  1133. $field['value'] = array();
  1134. } else {
  1135. $field['value'] = '__wpcf_repetitive_new_field';
  1136. }
  1137. }
  1138. $field['value'] = apply_filters('wpcf_repetitive_field_old_value',
  1139. $field['value'], $field, $post, $element);
  1140. if (!isset($repetitive_index[$field['id']])) {
  1141. if (defined('DOING_AJAX') && isset($_POST['count'])) {
  1142. $repetitive_index[$field['id']] = $_POST['count'];
  1143. } else {
  1144. $repetitive_index[$field['id']] = 1;
  1145. }
  1146. }
  1147. // Add hidden fields old_value and mark field repetitive
  1148. $repetitive_form = '<input type="hidden" name="wpcf_repetitive['
  1149. . $field['id'] . '][' . $repetitive_index[$field['id']]
  1150. . ']" value="1" />';
  1151. $repetitive_form .= '<input type="hidden" name="' . $element['#name']
  1152. . '[' . $repetitive_index[$field['id']] . '][old_value]" value="'
  1153. . base64_encode($field['value']) . '" />';
  1154. // Alter element name
  1155. $element['#name'] = $element['#name'] . '['
  1156. . $repetitive_index[$field['id']] . '][new_value]';
  1157. // Add repetitive control buttons if not copied by WPML
  1158. if (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy') {
  1159. if (!isset($repetitive_started[$field['id']]) && !defined('DOING_AJAX')) {
  1160. // Add 'Add' button
  1161. $repetitive_form .= '<div class="wpcf-repetitive-buttons" style="margin-top:10px;">';
  1162. $repetitive_form .= '<a href="'
  1163. . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=repetitive_add&amp;_wpnonce=' . wp_create_nonce('repetitive_add'))
  1164. . '&amp;field_id=' . $field['id'] . '&amp;field_id_md5='
  1165. . md5($field['id'])
  1166. . '" class="wpcf-repetitive-add button-primary">Add Another Field</a>';
  1167. $repetitive_form .= '</div>';
  1168. } else {
  1169. $repetitive_form .= '<div class="wpcf-repetitive-buttons" style="margin-top:10px;">';
  1170. // Add 'Delete' button
  1171. if (!empty($post->ID) && !defined('DOING_AJAX')) {
  1172. $repetitive_form .= '&nbsp;<a href="'
  1173. . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=repetitive_delete&amp;_wpnonce=' . wp_create_nonce('repetitive_delete')
  1174. . '&amp;post_id=' . $post->ID . '&amp;field_id='
  1175. . $field['id'] . '&amp;old_value='
  1176. . base64_encode($field['value']))
  1177. . '&amp;wpcf_warning=' . __('Are you sure?', 'wpcf')
  1178. . '&amp;field_id_md5='
  1179. . md5($field['id'])
  1180. . '" class="wpcf-repetitive-delete button-secondary">Delete Field</a>';
  1181. } else {
  1182. $repetitive_form .= '&nbsp;<a href="javascript:void(0);" onclick="jQuery(this).parent().parent().fadeOut(function(){jQuery(this).remove();});" class="wpcf-repetitive-delete button-secondary">Delete Field</a>';
  1183. }
  1184. $repetitive_form .= '</div>';
  1185. }
  1186. // Prepend to form element
  1187. if (isset($element['#before'])) {
  1188. $element['#before'] .= $repetitive_form;
  1189. } else {
  1190. $element['#before'] = $repetitive_form;
  1191. }
  1192. // Append AJAX response area to element
  1193. if (!isset($repetitive_started[$field['id']]) && !defined('DOING_AJAX')) {
  1194. if (isset($element['#after'])) {
  1195. $element['#after'] .= '<div class="wpcf-repetitive-response"></div>';
  1196. } else {
  1197. $element['#after'] = '<div class="wpcf-repetitive-response"></div>';
  1198. }
  1199. }
  1200. // Date trigger
  1201. if (defined('DOING_AJAX') && $field['type'] == 'date') {
  1202. $date_trigger = '<script type="text/javascript">
  1203. wpcfFieldsDateInit("#"+jQuery("#' . $element['#id'] . '").parent().attr("id"));
  1204. </script>';
  1205. if (isset($element['#after'])) {
  1206. $element['#after'] .= $date_trigger;
  1207. } else {
  1208. $element['#after'] = $date_trigger;
  1209. }
  1210. }
  1211. // If copied with WPML add hidden element that will be used to skip field
  1212. } else if (isset($field['wpml_action']) && $field['wpml_action'] == 'copy') {
  1213. if (isset($element['#after'])) {
  1214. $element['#after'] .= '<input type="hidden" name="wpcf_repetitive_copy['
  1215. . $field['id'] . '][' . $repetitive_index[$field['id']]
  1216. . ']" value="1" />';
  1217. } else {
  1218. $element['#after'] = '<input type="hidden" name="wpcf_repetitive_copy['
  1219. . $field['id'] . '][' . $repetitive_index[$field['id']]
  1220. . ']" value="1" />';
  1221. }
  1222. }
  1223. // Mark that first repetitive field is processed
  1224. $repetitive_started[$field['id']] = true;
  1225. $repetitive_index[$field['id']] += 1;
  1226. // Set JS var for counting repetitive fields
  1227. wpcf_admin_add_js_settings('wpcf_repetitive_count_' . md5($field['id']),
  1228. $repetitive_index[$field['id']]);
  1229. return $element;
  1230. }
  1231. /**
  1232. * Processes repetitive Skype field.
  1233. *
  1234. * @staticvar array $repetitive_started
  1235. * @staticvar array $repetitive_index
  1236. * @param type $post
  1237. * @param string $field
  1238. * @param type $skype_element
  1239. * @return string
  1240. */
  1241. function wpcf_admin_post_process_repetitive_field_skype($post, $field,
  1242. $skype_element) {
  1243. static $repetitive_started = array();
  1244. static $repetitive_index = array();
  1245. $repetitive_form = '';
  1246. if (defined('DOING_AJAX')) {
  1247. $field['value'] = '__wpcf_repetitive_new_field';
  1248. }
  1249. // Set index
  1250. if (!isset($repetitive_index[$field['id']])) {
  1251. if (defined('DOING_AJAX') && isset($_POST['count'])) {
  1252. $repetitive_index[$field['id']] = $_POST['count'];
  1253. } else {
  1254. $repetitive_index[$field['id']] = 1;
  1255. }
  1256. }
  1257. foreach ($skype_element as $element_key_temp => &$element) {
  1258. $element_key = $element['__element_key'];
  1259. if (!isset($field['value'][$element_key])) {
  1260. $field['value'][$element_key] = '';
  1261. }
  1262. if ($element_key != 'markup') {
  1263. // Alter element name
  1264. $element['#name'] = 'wpcf[' . $field['slug'] . ']['
  1265. . $repetitive_index[$field['id']] . ']['
  1266. . $element_key . ']';
  1267. }
  1268. // If not marked as copied by WPML add control buttons
  1269. if (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy') {
  1270. if ($element_key == 'skypename') {
  1271. // Add hidden fields old_value and mark field repetitive
  1272. $repetitive_form .= '<input type="hidden" name="wpcf_repetitive['
  1273. . $field['id'] . '][' . $repetitive_index[$field['id']]
  1274. . ']" value="1" />';
  1275. $repetitive_form .= '<input type="hidden" name="wpcf[' . $field['slug']
  1276. . '][' . $repetitive_index[$field['id']] . '][old_value]" value="'
  1277. . base64_encode(serialize($field['value'])) . '" />';
  1278. }
  1279. if ($element_key == 'skypename' && !isset($repetitive_started[$field['id']]) && !defined('DOING_AJAX')) {
  1280. // Add 'Add' button
  1281. $repetitive_form .= '<div class="wpcf-repetitive-buttons" style="margin-top:10px;">';
  1282. $repetitive_form .= '<a href="'
  1283. . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=repetitive_add&amp;_wpnonce=' . wp_create_nonce('repetitive_add'))
  1284. . '&amp;field_id=' . $field['id'] . '&amp;field_id_md5='
  1285. . md5($field['id'])
  1286. . '" class="wpcf-repetitive-add button-primary">Add Another Field</a>';
  1287. $repetitive_form .= '</div>';
  1288. } else if ($element_key == 'skypename') {
  1289. $repetitive_form .= '<div class="wpcf-repetitive-buttons" style="margin-top:10px;">';
  1290. // Add 'Delete' button
  1291. if (!empty($post->ID)) {
  1292. $repetitive_form .= '&nbsp;<a href="'
  1293. . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=repetitive_delete&amp;_wpnonce=' . wp_create_nonce('repetitive_delete')
  1294. . '&amp;post_id=' . $post->ID . '&amp;field_id='
  1295. . $field['id'] . '&amp;field_id_md5='
  1296. . md5($field['id']) . '&amp;old_value='
  1297. . base64_encode(serialize($field['value'])))
  1298. . '&amp;wpcf_warning=' . __('Are you sure?', 'wpcf')
  1299. . '" class="wpcf-repetitive-delete button-secondary">Delete Field</a>';
  1300. } else {
  1301. $repetitive_form .= '&nbsp;<a href="javascript:void(0);" onclick="jQuery(this).parent().parent().fadeOut(function(){jQuery(this).remove();});" class="wpcf-repetitive-delete button-secondary">Delete Field</a>';
  1302. }
  1303. $repetitive_form .= '</div>';
  1304. }
  1305. // Prepend to form element
  1306. if ($element_key == 'skypename' && isset($element['#before'])) {
  1307. $element['#before'] .= $repetitive_form;
  1308. } else if ($element_key == 'skypename') {
  1309. $element['#before'] = $repetitive_form;
  1310. }
  1311. // Append AJAX response area to element
  1312. if ($element_key == 'markup'
  1313. && !isset($repetitive_started[$field['id']])
  1314. && !defined('DOING_AJAX')) {
  1315. $element['#markup'] .= '<div class="wpcf-repetitive-response"></div>';
  1316. }
  1317. // If marked as copied by WPML add hidden field
  1318. } else if ($element_key == 'skypename'
  1319. && isset($field['wpml_action']) && $field['wpml_action'] == 'copy') {
  1320. if (isset($element['#after'])) {
  1321. $element['#after'] .= '<input type="hidden" name="wpcf_repetitive_copy['
  1322. . $field['id'] . '][' . $repetitive_index[$field['id']]
  1323. . ']" value="1" />';
  1324. } else {
  1325. $element['#after'] = '<input type="hidden" name="wpcf_repetitive_copy['
  1326. . $field['id'] . '][' . $repetitive_index[$field['id']]
  1327. . ']" value="1" />';
  1328. }
  1329. }
  1330. if ($element_key == 'skypename') {
  1331. $main_element = $element;
  1332. }
  1333. }
  1334. // Mark that first repetitive field is processed
  1335. $repetitive_started[$field['id']] = true;
  1336. $repetitive_index[$field['id']] += 1;
  1337. // Set JS var for counting repetitive fields
  1338. wpcf_admin_add_js_settings('wpcf_repetitive_count_' . md5($field['id']),
  1339. $repetitive_index[$field['id']]);
  1340. return array($main_element, $skype_element);
  1341. }
  1342. /**
  1343. * Marketing meta-box
  1344. */
  1345. function wpcf_admin_post_marketing_meta_box() {
  1346. $output = '';
  1347. if (defined('WPV_VERSION')) {
  1348. $output .= '<p>' . sprintf(__("%sViews%s let's you create templates, query content from the database and display it.",
  1349. 'wpcf'),
  1350. '<a href="http://wp-types.com/home/views-create-elegant-displays-for-your-content/?utm_source=types&utm_medium=plugin&utm_term=views&utm_content=promobox&utm_campaign=types" title="Views" target="_blank">',
  1351. '</a>') . '</p>';
  1352. $output .= '<p><a href="' . admin_url('edit.php?post_type=view-template') . '">' . __('Create <strong>View Templates</strong> for single pages &raquo;',
  1353. 'wpcf') . '</a></p>';
  1354. $output .= '<p><a href="' . admin_url('edit.php?post_type=view') . '">' . __('Create <strong>Views</strong> for content lists &raquo;',
  1355. 'wpcf') . '</a></p>';
  1356. } else {
  1357. $output .= '<p>' . __('Views makes it easy to display custom posts and fields on your site.',
  1358. 'wpcf') . '</p>'
  1359. . '<p>' . __('Learn how to:', 'wpcf') . '</p>'
  1360. . '<ul>'
  1361. . '<li><a href="http://wp-types.com/documentation/user-guides/view-templates/?utm_source=types&utm_medium=plugin&utm_term=view-templates&utm_content=post-edit-sidebar&utm_campaign=types" target="_blank">'
  1362. . __('Display custom fields in templates &raquo;', 'wpcf')
  1363. . '</a></li>'
  1364. . '<li><a href="http://wp-types.com/documentation/user-guides/views/?utm_source=types&utm_medium=plugin&utm_term=views&utm_content=post-edit-sidebar&utm_campaign=types" target="_blank">'
  1365. . __('Show custom content anywhere in the site &raquo;', 'wpcf')
  1366. . '</a></li>'
  1367. . '</ul>'
  1368. . '<p style="margin-top:2em;"><a class="button button-highlighted" href="http://wp-types.com/buy/?utm_source=types&utm_medium=plugin&utm_term=buy&utm_content=post-edit-sidebar&utm_campaign=types" target="_blank">'
  1369. . __('Buy Views ($49)', 'wpcf')
  1370. . '</a></p>'
  1371. . '<p style="font-size: 90%;">'
  1372. . __('Risk free - 30 days money back guarantee', 'wpcf')
  1373. . '</p>';
  1374. }
  1375. echo $output;
  1376. }