PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/cisash/fananeen
PHP | 1546 lines | 1192 code | 132 blank | 222 comment | 372 complexity | 8fb09199c43dd62e694e2e2b3580d4e4 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  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. && !defined('WPCF_RUNNING_EMBEDDED')) {
  42. $hide_help_box = true;
  43. $help_box = wpcf_get_settings('help_box');
  44. $custom_types = get_option('wpcf-custom-types', array());
  45. if ($help_box != 'no') {
  46. if ($help_box == 'by_types' && array_key_exists($post_type,
  47. $custom_types)) {
  48. $hide_help_box = false;
  49. }
  50. if (function_exists('wprc_is_logged_to_repo') && wprc_is_logged_to_repo(WPCF_REPOSITORY)) {
  51. $hide_help_box = true;
  52. }
  53. if ($help_box == 'all') {
  54. $hide_help_box = false;
  55. }
  56. if (!$hide_help_box) {
  57. add_meta_box('wpcf-marketing',
  58. __('How-To Display Custom Content', 'wpcf'),
  59. 'wpcf_admin_post_marketing_meta_box', $post_type,
  60. 'side', 'high');
  61. }
  62. }
  63. }
  64. // Get groups
  65. $groups = wpcf_admin_post_get_post_groups_fields($post);
  66. $wpcf_active = false;
  67. foreach ($groups as $key => $group) {
  68. if (!empty($group['fields'])) {
  69. $wpcf_active = true;
  70. // Process fields
  71. $group['fields'] = wpcf_admin_post_process_fields($post,
  72. $group['fields'], true);
  73. }
  74. // Add meta boxes
  75. add_meta_box($group['slug'],
  76. wpcf_translate('group ' . $group['id'] . ' name', $group['name']),
  77. 'wpcf_admin_post_meta_box', $post_type,
  78. $group['meta_box_context'], 'high', $group);
  79. }
  80. // Activate scripts
  81. if ($wpcf_active) {
  82. wp_enqueue_script('wpcf-fields-post',
  83. WPCF_EMBEDDED_RES_RELPATH . '/js/fields-post.js',
  84. array('jquery'), WPCF_VERSION);
  85. wp_enqueue_script('wpcf-form-validation',
  86. WPCF_EMBEDDED_RES_RELPATH . '/js/'
  87. . 'jquery-form-validation/jquery.validate.min.js',
  88. array('jquery'), WPCF_VERSION);
  89. wp_enqueue_script('wpcf-form-validation-additional',
  90. WPCF_EMBEDDED_RES_RELPATH . '/js/'
  91. . 'jquery-form-validation/additional-methods.min.js',
  92. array('jquery'), WPCF_VERSION);
  93. wp_enqueue_style('wpcf-fields-basic',
  94. WPCF_EMBEDDED_RES_RELPATH . '/css/basic.css', array(),
  95. WPCF_VERSION);
  96. wp_enqueue_style('wpcf-fields-post',
  97. WPCF_EMBEDDED_RES_RELPATH . '/css/fields-post.css',
  98. array('wpcf-fields-basic'), WPCF_VERSION);
  99. add_action('admin_footer', 'wpcf_admin_post_js_validation');
  100. }
  101. do_action('wpcf_admin_post_init', $post_type, $post, $groups, $wpcf_active);
  102. }
  103. /**
  104. * Renders meta box content.
  105. *
  106. * @param type $post
  107. * @param type $group
  108. */
  109. function wpcf_admin_post_meta_box($post, $group) {
  110. static $nonce_added = false;
  111. if (!$nonce_added) {
  112. $nonce_action = 'update-' . $post->post_type . '_' . $post->ID;
  113. wp_nonce_field($nonce_action, '_wpcf_post_wpnonce');
  114. $nonce_added = true;
  115. }
  116. if (!empty($group['args']['_conditional_display'])) {
  117. if ($group['args']['_conditional_display'] == 'failed') {
  118. echo '<div class="wpcf-cd-group wpcf-cd-group-failed" style="display:none;">';
  119. } else {
  120. echo '<div class="wpcf-cd-group wpcf-cd-group-passed">';
  121. }
  122. }
  123. if (!empty($group['args']['fields'])) {
  124. // Display description
  125. if (!empty($group['args']['description'])) {
  126. echo '<div class="wpcf-meta-box-description">'
  127. . wpautop(wpcf_translate('group ' . $group['args']['id'] . ' description',
  128. $group['args']['description'])) . '</div>';
  129. }
  130. foreach ($group['args']['fields'] as $field_slug => $field) {
  131. if (empty($field)) {
  132. continue;
  133. }
  134. // Render form elements
  135. if (wpcf_compare_wp_version() && $field['#type'] == 'wysiwyg') {
  136. // Especially for WYSIWYG
  137. echo '<div class="wpcf-wysiwyg">';
  138. echo '<div id="wpcf-textarea-textarea-wrapper" class="form-item form-item-textarea wpcf-form-item wpcf-form-item-textarea">';
  139. echo isset($field['#before']) ? $field['#before'] : '';
  140. echo '
  141. <label class="wpcf-form-label wpcf-form-textarea-label">' . $field['#title'] . '</label>';
  142. echo '<div class="description wpcf-form-description wpcf-form-description-textarea description-textarea">
  143. ' . wpautop($field['#description']) . '</div>';
  144. wp_editor($field['#value'], $field['#id'],
  145. $field['#editor_settings']);
  146. $field['slug'] = str_replace(WPCF_META_PREFIX . 'wysiwyg-', '',
  147. $field_slug);
  148. $field['type'] = 'wysiwyg';
  149. echo '</div>';
  150. echo isset($field['#after']) ? $field['#after'] : '';
  151. echo '</div><br /><br />';
  152. } else {
  153. if ($field['#type'] == 'wysiwyg') {
  154. $field['#type'] = 'textarea';
  155. }
  156. echo wpcf_form_simple(array($field['#id'] => $field));
  157. }
  158. do_action('wpcf_fields_' . $field_slug . '_meta_box_form', $field);
  159. if (isset($field['wpcf-type'])) { // May be ignored
  160. do_action('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form',
  161. $field);
  162. }
  163. }
  164. }
  165. if (!empty($group['args']['_conditional_display'])) {
  166. echo '</div>';
  167. }
  168. }
  169. /**
  170. * save_post hook.
  171. *
  172. * @param type $post_ID
  173. * @param type $post
  174. */
  175. function wpcf_admin_post_save_post_hook($post_ID, $post) {
  176. if (!isset($_POST['_wpcf_post_wpnonce']) || !wp_verify_nonce($_POST['_wpcf_post_wpnonce'],
  177. 'update-' . $post->post_type . '_' . $post_ID)) {
  178. return false;
  179. }
  180. if (in_array($post->post_type,
  181. array('revision', 'attachment', 'wp-types-group', 'view',
  182. 'view-template'))) {
  183. return false;
  184. }
  185. // Get groups
  186. $groups = wpcf_admin_post_get_post_groups_fields($post);
  187. if (empty($groups)) {
  188. return false;
  189. }
  190. $all_fields = array();
  191. foreach ($groups as $group) {
  192. // Process fields
  193. $fields = wpcf_admin_post_process_fields($post, $group['fields'], true,
  194. false, 'validation');
  195. // Validate fields
  196. $form = wpcf_form_simple_validate($fields);
  197. $all_fields = $all_fields + $fields;
  198. $error = $form->isError();
  199. // Trigger form error
  200. if ($error) {
  201. wpcf_admin_message_store(
  202. __('Please check your input data', 'wpcf'), 'error');
  203. }
  204. }
  205. // Save invalid elements so user can be informed after redirect
  206. if (!empty($all_fields)) {
  207. update_post_meta($post_ID, 'wpcf-invalid-fields', $all_fields);
  208. }
  209. // Save meta fields
  210. if (!empty($_POST['wpcf'])) {
  211. foreach ($_POST['wpcf'] as $field_slug => $field_value) {
  212. // Skip copied fields
  213. if (isset($_POST['wpcf_repetitive_copy'][$field_slug])) {
  214. continue;
  215. }
  216. $field = wpcf_fields_get_field_by_slug($field_slug);
  217. if (empty($field)) {
  218. continue;
  219. }
  220. $meta_key = wpcf_types_get_meta_prefix($field) . $field_slug;
  221. // Don't save invalid
  222. if (isset($all_fields[$meta_key]['#error'])) {
  223. continue;
  224. }
  225. // Repetitive fields
  226. if (isset($_POST['wpcf_repetitive'][$field_slug])) {
  227. delete_post_meta($post_ID, $meta_key);
  228. foreach ($field_value as $temp_id => $repetitive_data) {
  229. // Skype specific
  230. if ($field['type'] == 'skype') {
  231. unset($repetitive_data['old_value']);
  232. wpcf_admin_post_save_field($post_ID, $meta_key, $field,
  233. $repetitive_data, true);
  234. } else {
  235. wpcf_admin_post_save_field($post_ID, $meta_key, $field,
  236. $repetitive_data['new_value'], true);
  237. }
  238. }
  239. } else {
  240. wpcf_admin_post_save_field($post_ID, $meta_key, $field,
  241. $field_value);
  242. }
  243. }
  244. }
  245. // Process checkboxes
  246. foreach ($all_fields as $field) {
  247. if (!isset($field['#type'])) {
  248. continue;
  249. }
  250. if ($field['#type'] == 'checkbox'
  251. && !isset($_POST['wpcf'][$field['wpcf-slug']])) {
  252. $field_data = wpcf_admin_fields_get_field($field['wpcf-id']);
  253. if (isset($field_data['data']['save_empty'])
  254. && $field_data['data']['save_empty'] == 'yes') {
  255. update_post_meta($post_ID,
  256. wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'],
  257. 0);
  258. } else {
  259. delete_post_meta($post_ID,
  260. wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']);
  261. }
  262. }
  263. if ($field['#type'] == 'checkboxes') {
  264. $field_data = wpcf_admin_fields_get_field($field['wpcf-id']);
  265. if (!empty($field_data['data']['options'])) {
  266. $update_data = array();
  267. foreach ($field_data['data']['options'] as $option_id => $option_data) {
  268. if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) {
  269. if (isset($field_data['data']['save_empty'])
  270. && $field_data['data']['save_empty'] == 'yes') {
  271. $update_data[$option_id] = 0;
  272. }
  273. } else {
  274. $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id];
  275. }
  276. }
  277. update_post_meta($post_ID,
  278. wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'],
  279. $update_data);
  280. }
  281. }
  282. }
  283. }
  284. /**
  285. * Saves single field.
  286. *
  287. * @param type $post_ID
  288. * @param type $all_fields
  289. * @param type $field_slug
  290. * @param type $field_value
  291. * @param string $old_value
  292. * @return boolean
  293. */
  294. function wpcf_admin_post_save_field($post_ID, $meta_key, $field, $field_value,
  295. $add = false) {
  296. // Apply filters
  297. if (is_string($field_value))
  298. $field_value = trim($field_value);
  299. $field_value = apply_filters('wpcf_fields_value_save', $field_value,
  300. $field['type'], $field['slug'], $field);
  301. $field_value = apply_filters('wpcf_fields_slug_' . $field['slug']
  302. . '_value_save', $field_value, $field);
  303. $field_value = apply_filters('wpcf_fields_type_' . $field['type']
  304. . '_value_save', $field_value, $field);
  305. // Save field
  306. if ($add) {
  307. add_post_meta($post_ID, $meta_key, $field_value);
  308. } else {
  309. update_post_meta($post_ID, $meta_key, $field_value);
  310. }
  311. do_action('wpcf_fields_save', $field_value, $field);
  312. do_action('wpcf_fields_slug_' . $field['slug'] . '_save', $field_value,
  313. $field);
  314. do_action('wpcf_fields_type_' . $field['type'] . '_save', $field_value,
  315. $field);
  316. }
  317. /**
  318. * Renders JS validation script.
  319. */
  320. function wpcf_admin_post_js_validation() {
  321. wpcf_form_render_js_validation('#post');
  322. ?>
  323. <script type="text/javascript">
  324. //<![CDATA[
  325. function wpcfFieldsEditorCallback(field_id) {
  326. 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";
  327. tb_show("<?php
  328. _e('Insert field', 'wpcf');
  329. ?>", url);
  330. }
  331. var wpcfFieldsEditorCallback_redirect = null;
  332. function wpcfFieldsEditorCallback_set_redirect(function_name, params) {
  333. wpcfFieldsEditorCallback_redirect = {'function' : function_name, 'params' : params};
  334. }
  335. //]]>
  336. </script>
  337. <?php
  338. }
  339. /**
  340. * Creates form elements.
  341. *
  342. * @param type $post
  343. * @param type $fields
  344. * @return type
  345. */
  346. function wpcf_admin_post_process_fields($post = false, $fields = array(),
  347. $use_cache = true, $add_to_editor = true, $context = 'group') {
  348. // Get cached
  349. static $cache = array();
  350. $cache_key = !empty($post->ID) ? $post->ID . md5(serialize($fields)) : false;
  351. if ($use_cache && $cache_key && isset($cache[$cache_key])) {
  352. return $cache[$cache_key];
  353. }
  354. $fields_processed = array();
  355. // Get invalid fields (if submitted)
  356. $invalid_fields = array();
  357. if (!empty($post->ID)) {
  358. $invalid_fields = get_post_meta($post->ID, 'wpcf-invalid-fields', true);
  359. delete_post_meta($post->ID, 'wpcf-invalid-fields');
  360. }
  361. $original_cf = array();
  362. if (function_exists('wpml_get_copied_fields_for_post_edit')) {
  363. $original_cf = wpml_get_copied_fields_for_post_edit();
  364. }
  365. foreach ($fields as $field) {
  366. // Repetitive fields
  367. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
  368. $temp_flag = false;
  369. // First check if repetitive fields are copied using WPML
  370. if (!empty($original_cf['fields'])) {
  371. // Check if marked
  372. if (in_array(wpcf_types_get_meta_prefix($field) . $field['slug'],
  373. $original_cf['fields'])) {
  374. // Get original post field values
  375. $temp_fields = get_post_meta($original_cf['original_post_id'],
  376. wpcf_types_get_meta_prefix($field) . $field['slug'],
  377. false);
  378. // If there are original field values stored
  379. if (!empty($temp_fields)) {
  380. foreach ($temp_fields as $temp_key => $temp_field) {
  381. $single_field = $field;
  382. $single_field['wpml_action'] = 'copy';
  383. $single_field['value'] = $temp_field;
  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,
  394. $post, $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,
  401. $invalid_fields);
  402. }
  403. }
  404. }
  405. // If there are no original fields stored
  406. // display empty element
  407. } else {
  408. $single_field = $field;
  409. $single_field['wpml_action'] = 'copy';
  410. $single_field['value'] = null;
  411. $temp_processed = wpcf_admin_post_process_field($post,
  412. $single_field, $use_cache, $add_to_editor,
  413. $context);
  414. if ($temp_processed) {
  415. $single_field = $temp_processed['field'];
  416. $element = $temp_processed['element'];
  417. if ($field['type'] == 'skype') {
  418. foreach ($element as $temp_element_key => $temp_element_value) {
  419. $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field',
  420. $temp_element_value, $field, $post,
  421. $context, $original_cf,
  422. $invalid_fields);
  423. }
  424. } else {
  425. $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field',
  426. $element, $single_field, $post,
  427. $context, $original_cf, $invalid_fields);
  428. }
  429. }
  430. }
  431. } else {
  432. $temp_flag = true;
  433. }
  434. } else {
  435. $temp_flag = true;
  436. }
  437. // Get repetitive fields values
  438. if ($temp_flag && !empty($post->ID)) {
  439. $temp_flag = false;
  440. $temp_fields = get_post_meta($post->ID,
  441. wpcf_types_get_meta_prefix($field) . $field['slug'],
  442. false);
  443. if (!empty($temp_fields)) {
  444. $temp_start = true;
  445. $fields_processed[$field['id'] . '_repetitive_wrapper_open'] = array(
  446. '#type' => 'markup',
  447. '#markup' => '<div id="wpcf_'
  448. . $field['id']
  449. . '_repetitive_wrapper_' . wpcf_unique_id(serialize($field))
  450. . '" class="wpcf-repetitive-wrapper">',
  451. '#id' => $field['id'] . '_repetitive_wrapper_open',
  452. );
  453. foreach ($temp_fields as $temp_key => $temp_field) {
  454. $single_field = $field;
  455. $single_field['value'] = $temp_field;
  456. $temp_processed = wpcf_admin_post_process_field($post,
  457. $single_field, $use_cache, $add_to_editor,
  458. $context);
  459. if ($temp_processed) {
  460. $single_field = $temp_processed['field'];
  461. $element = $temp_processed['element'];
  462. if ($field['type'] == 'skype') {
  463. foreach ($element as $temp_element_key => $temp_element_value) {
  464. if (!$temp_start && !in_array($field['type'],
  465. array('checkbox'))) {
  466. unset($temp_element_value['#title']);
  467. }
  468. $temp_start = false;
  469. $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field',
  470. $temp_element_value, $field, $post,
  471. $context, $original_cf,
  472. $invalid_fields);
  473. }
  474. } else {
  475. if (!$temp_start && !in_array($field['type'],
  476. array('checkbox'))) {
  477. unset($element['#title']);
  478. }
  479. $temp_start = false;
  480. $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field',
  481. $element, $single_field, $post,
  482. $context, $original_cf, $invalid_fields);
  483. }
  484. }
  485. }
  486. $fields_processed[$field['id'] . '_repetitive_wrapper_close'] = array(
  487. '#type' => 'markup',
  488. '#markup' => '</div>',
  489. '#id' => $field['id'] . '_repetitive_wrapper_close',
  490. );
  491. } else {
  492. $temp_flag = true;
  493. }
  494. }
  495. // Temp flag for repetitive field is triggered if post is new
  496. // and field is not marked to be copied
  497. if ($temp_flag == true) {
  498. $temp_processed = wpcf_admin_post_process_field($post, $field,
  499. $use_cache, $add_to_editor, $context);
  500. if ($temp_processed) {
  501. $fields_processed[$field['id'] . '_repetitive_wrapper_open'] = array(
  502. '#type' => 'markup',
  503. '#markup' => '<div id="wpcf_'
  504. . $field['id']
  505. . '_repetitive_wrapper_' . wpcf_unique_id(serialize($field))
  506. . '" class="wpcf-repetitive-wrapper">',
  507. '#id' => $field['id'] . '_repetitive_wrapper_open',
  508. );
  509. $field = $temp_processed['field'];
  510. $element = $temp_processed['element'];
  511. if ($field['type'] == 'skype') {
  512. foreach ($element as $temp_element_key => $temp_element_value) {
  513. $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field',
  514. $temp_element_value, $field, $post,
  515. $context, $original_cf, $invalid_fields);
  516. }
  517. } else {
  518. $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field',
  519. $element, $field, $post, $context, $original_cf,
  520. $invalid_fields);
  521. }
  522. $fields_processed[$field['id'] . '_repetitive_wrapper_close'] = array(
  523. '#type' => 'markup',
  524. '#markup' => '</div>',
  525. '#id' => $field['id'] . '_repetitive_wrapper_close',
  526. );
  527. }
  528. }
  529. // Non-repetitive fields
  530. } else {
  531. if (!empty($post->ID)) {
  532. $field['value'] = get_post_meta($post->ID,
  533. wpcf_types_get_meta_prefix($field) . $field['slug'],
  534. true);
  535. }
  536. // Check if repetitive field is copied using WPML
  537. if (!empty($original_cf['fields'])) {
  538. if (in_array(wpcf_types_get_meta_prefix($field) . $field['slug'],
  539. $original_cf['fields'])) {
  540. $field['wpml_action'] = 'copy';
  541. $field['value'] = get_post_meta($original_cf['original_post_id'],
  542. wpcf_types_get_meta_prefix($field) . $field['slug'],
  543. true);
  544. }
  545. }
  546. $temp_processed = wpcf_admin_post_process_field($post, $field,
  547. $use_cache, $add_to_editor, $context);
  548. if ($temp_processed) {
  549. $field = $temp_processed['field'];
  550. $element = $temp_processed['element'];
  551. if ($field['type'] == 'skype') {
  552. foreach ($element as $temp_element_key => $temp_element_value) {
  553. $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field',
  554. $temp_element_value, $field, $post, $context,
  555. $original_cf, $invalid_fields);
  556. }
  557. } else {
  558. $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field',
  559. $element, $field, $post, $context, $original_cf,
  560. $invalid_fields);
  561. }
  562. }
  563. }
  564. }
  565. // Cache results
  566. if ($cache_key) {
  567. $cache[$cache_key] = $fields_processed;
  568. }
  569. return $fields_processed;
  570. }
  571. /**
  572. * Processes single field.
  573. *
  574. * @staticvar array $repetitive_started
  575. * @param type $post
  576. * @param type $field
  577. * @param type $use_cache
  578. * @param type $add_to_editor
  579. * @param type $context
  580. * @param type $original_cf
  581. * @param type $invalid_fields
  582. * @return mixed boolean|array
  583. */
  584. function wpcf_admin_post_process_field($post = false, $field_unedited = array(),
  585. $use_cache = true, $add_to_editor = true, $context = 'group',
  586. $original_cf = array(), $invalid_fields = array()) {
  587. $field = wpcf_admin_fields_get_field($field_unedited['id']);
  588. if (!empty($field)) {
  589. // Set values
  590. $field['value'] = isset($field_unedited['value']) ? maybe_unserialize($field_unedited['value']) : '';
  591. $field['wpml_action'] = isset($field_unedited['wpml_action']) ? $field_unedited['wpml_action'] : '';
  592. $field_id = 'wpcf-' . $field['type'] . '-' . $field['slug'] . '-'
  593. . wpcf_unique_id(serialize($field));
  594. $field_init_data = wpcf_fields_type_action($field['type']);
  595. // Get inherited field
  596. $inherited_field_data = false;
  597. if (isset($field_init_data['inherited_field_type'])) {
  598. $inherited_field_data = wpcf_fields_type_action($field_init_data['inherited_field_type']);
  599. }
  600. // Apply filters
  601. $field['value'] = apply_filters('wpcf_fields_value_get',
  602. $field['value'], $field, $field_init_data);
  603. $field['value'] = apply_filters('wpcf_fields_slug_' . $field['slug']
  604. . '_value_get', $field['value'], $field, $field_init_data);
  605. $field['value'] = apply_filters('wpcf_fields_type_' . $field['type']
  606. . '_value_get', $field['value'], $field, $field_init_data);
  607. wpcf_admin_post_field_load_js_css($field_init_data);
  608. $element = array();
  609. // Set generic values
  610. $element = array(
  611. '#type' => isset($field_init_data['inherited_field_type']) ? $field_init_data['inherited_field_type'] : $field['type'],
  612. '#id' => $field_id,
  613. '#title' => wpcf_translate('field ' . $field['id'] . ' name',
  614. $field['name']),
  615. '#description' => wpautop(wpcf_translate('field ' . $field['id'] . ' description',
  616. $field['description'])),
  617. '#name' => 'wpcf[' . $field['slug'] . ']',
  618. '#value' => isset($field['value']) ? $field['value'] : '',
  619. 'wpcf-id' => $field['id'],
  620. 'wpcf-slug' => $field['slug'],
  621. 'wpcf-type' => $field['type'],
  622. );
  623. // Set inherited values
  624. $element_inherited = array();
  625. if ($inherited_field_data) {
  626. if (function_exists('wpcf_fields_'
  627. . $field_init_data['inherited_field_type']
  628. . '_meta_box_form')) {
  629. $element_inherited = call_user_func_array('wpcf_fields_'
  630. . $field_init_data['inherited_field_type']
  631. . '_meta_box_form', array($field, $element));
  632. }
  633. }
  634. $element = array_merge($element, $element_inherited);
  635. if (isset($field['description_extra'])) {
  636. $element['#description'] .= wpautop($field['description_extra']);
  637. }
  638. // Set atributes #1
  639. if (isset($field['disable'])) {
  640. $field['#disable'] = $field['disable'];
  641. }
  642. if (!empty($field['disable'])) {
  643. $field['#attributes']['disabled'] = 'disabled';
  644. }
  645. if (!empty($field['readonly'])) {
  646. $field['#attributes']['readonly'] = 'readonly';
  647. }
  648. // Set specific values
  649. if (defined('WPCF_INC_ABSPATH')
  650. && file_exists(WPCF_INC_ABSPATH . '/fields/' . $field['type']
  651. . '.php')) {
  652. require_once WPCF_INC_ABSPATH . '/fields/' . $field['type']
  653. . '.php';
  654. }
  655. // Load field
  656. if (function_exists('wpcf_fields_' . $field['type']
  657. . '_meta_box_form')) {
  658. $element_specific = call_user_func_array('wpcf_fields_'
  659. . $field['type'] . '_meta_box_form', array($field, $element));
  660. // Check if it's single
  661. if (isset($element_specific['#type'])) {
  662. // Format description
  663. if (!empty($element_specific['#description'])) {
  664. $element_specific['#description'] = wpautop($element_specific['#description']);
  665. }
  666. $element = array_merge($element, $element_specific);
  667. // Set validation element
  668. if (isset($field['data']['validate'])) {
  669. $element['#validate'] = $field['data']['validate'];
  670. }
  671. // Repetitive fields
  672. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
  673. $element = wpcf_admin_post_process_repetitive_field($post,
  674. $field, $element);
  675. }
  676. } else { // More fields, loop all
  677. // Only Skype for now have multiple fields, so process only that
  678. if ($field['type'] == 'skype') {
  679. $skype_element = array();
  680. foreach ($element_specific as $element_specific_fields_key => $element_specific_fields_value) {
  681. $element_specific_fields_value['__element_key'] = $element_specific_fields_key;
  682. // Format description
  683. if (!empty($element_specific_fields_value['#description'])) {
  684. $element_specific_fields_value['#description'] = wpautop($element_specific_fields_value['#description']);
  685. }
  686. // If no ID
  687. if (!isset($element_specific_fields_value['#id'])) {
  688. $element_specific_fields_value['#id'] = 'wpcf-'
  689. . $field['slug'] . '-'
  690. . wpcf_unique_id(serialize($field));
  691. }
  692. // Set validation element
  693. if (!empty($element_specific_fields_value['#_validate_this']) && isset($field['data']['validate'])) {
  694. $element_specific_fields_value['#validate'] = $field['data']['validate'];
  695. }
  696. if ($element_specific_fields_key != 'skypename') {
  697. if (!isset($element_specific_fields_value['#name'])) {
  698. $element_specific_fields_value['#name'] = 'wpcf[ignore]['
  699. . wpcf_unique_id(serialize($element_specific_fields_value))
  700. . ']';
  701. }
  702. $skype_element[$element_specific_fields_value['#id']] = $element_specific_fields_value;
  703. continue;
  704. }
  705. // This one is actually value and keep it (#name is required)
  706. $element = array_merge($element,
  707. $element_specific_fields_value);
  708. // Add it here to keep order
  709. $skype_element[$element['#id']] = $element;
  710. }
  711. // Repetitive fields
  712. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
  713. list($element, $skype_element) = wpcf_admin_post_process_repetitive_field_skype($post,
  714. $field, $skype_element);
  715. }
  716. }
  717. }
  718. } else {
  719. // Repetitive fields
  720. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
  721. $element = wpcf_admin_post_process_repetitive_field($post,
  722. $field, $element);
  723. }
  724. }
  725. // Set atributes #2 (override)
  726. if (isset($field['disable'])) {
  727. $element['#disable'] = $field['disable'];
  728. }
  729. if (!empty($field['disable'])) {
  730. $element['#attributes']['disabled'] = 'disabled';
  731. }
  732. if (!empty($field['readonly'])) {
  733. $element['#attributes']['readonly'] = 'readonly';
  734. if (!empty($element['#options'])) {
  735. foreach ($element['#options'] as $key => $option) {
  736. if (!is_array($option)) {
  737. $element['#options'][$key] = array(
  738. '#title' => $key,
  739. '#value' => $option,
  740. );
  741. }
  742. $element['#options'][$key]['#attributes']['readonly'] = 'readonly';
  743. if ($element['#type'] == 'select') {
  744. $element['#options'][$key]['#attributes']['disabled'] = 'disabled';
  745. }
  746. }
  747. }
  748. if ($element['#type'] == 'select') {
  749. $element['#attributes']['disabled'] = 'disabled';
  750. }
  751. }
  752. // Set validation element
  753. if ($field['type'] != 'skype' && empty($element['#validate']) && isset($field['data']['validate'])) {
  754. $element['#validate'] = $field['data']['validate'];
  755. }
  756. // Check if it was invalid on submit and add error message
  757. if ($post && !empty($invalid_fields)) {
  758. if (isset($invalid_fields[$element['#id']]['#error'])) {
  759. $element['#error'] = $invalid_fields[$element['#id']]['#error'];
  760. }
  761. }
  762. // Set WPML locked icon
  763. if (isset($field['wpml_action']) && $field['wpml_action'] == 'copy') {
  764. $element['#title'] .= '<img src="' . WPCF_EMBEDDED_RES_RELPATH . '/images/locked.png" alt="'
  765. . __('This field is locked for editing because WPML will copy its value from the original language.',
  766. 'wpcf') . '" title="'
  767. . __('This field is locked for editing because WPML will copy its value from the original language.',
  768. 'wpcf') . '" style="position:relative;left:2px;top:2px;" />';
  769. }
  770. // Add to editor
  771. if ($add_to_editor) {
  772. wpcf_admin_post_add_to_editor($field);
  773. }
  774. // Add repetitive class
  775. // @TODO Why not add repetitive class if copied?
  776. if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship'
  777. && (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy')) {
  778. if (!empty($element['#options']) && $element['#type'] != 'select') {
  779. foreach ($element['#options'] as $temp_key => $temp_value) {
  780. $element['#options'][$temp_key]['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
  781. }
  782. } else {
  783. $element['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
  784. }
  785. wpcf_admin_add_js_settings('wpcfFormRepetitiveUniqueValuesCheckText',
  786. '\'' . __('Warning: same values set', 'wpcf') . '\'');
  787. }
  788. // Set read-only if copied by WPML
  789. if (isset($field['wpml_action']) && $field['wpml_action'] == 'copy') {
  790. if (isset($element['#options'])) {
  791. foreach ($element['#options'] as $temp_key => $temp_value) {
  792. if (isset($temp_value['#attributes'])) {
  793. $element['#options'][$temp_key]['#attributes']['readonly'] = 'readonly';
  794. } else {
  795. $element['#options'][$temp_key]['#attributes'] = array('readonly' => 'readonly');
  796. }
  797. }
  798. }
  799. if ($field['type'] == 'select') {
  800. if (isset($element['#attributes'])) {
  801. $element['#attributes']['disabled'] = 'disabled';
  802. } else {
  803. $element['#attributes'] = array('disabled' => 'disabled');
  804. }
  805. } else {
  806. if (isset($element['#attributes'])) {
  807. $element['#attributes']['readonly'] = 'readonly';
  808. } else {
  809. $element['#attributes'] = array('readonly' => 'readonly');
  810. }
  811. }
  812. }
  813. // Specific for Skype
  814. if ($field['type'] == 'skype') {
  815. $skype_element[$element['#id']] = $element;
  816. $element = $skype_element;
  817. }
  818. return array('field' => $field, 'element' => $element);
  819. }
  820. return false;
  821. }
  822. /**
  823. * Gets all groups and fields for post.
  824. *
  825. * @param type $post_ID
  826. * @return type
  827. */
  828. function wpcf_admin_post_get_post_groups_fields($post = false,
  829. $context = 'group') {
  830. // Get post_type
  831. if (!empty($post)) {
  832. $post_type = get_post_type($post);
  833. } else {
  834. if (!isset($_GET['post_type'])) {
  835. $post_type = 'post';
  836. } else if (in_array($_GET['post_type'],
  837. get_post_types(array('show_ui' => true)))) {
  838. $post_type = $_GET['post_type'];
  839. } else {
  840. $post_type = 'post';
  841. }
  842. }
  843. // Get post terms
  844. $support_terms = false;
  845. if (!empty($post)) {
  846. $post->_wpcf_post_terms = array();
  847. $taxonomies = get_taxonomies('', 'objects');
  848. if (!empty($taxonomies)) {
  849. foreach ($taxonomies as $tax_slug => $tax) {
  850. $temp_tax = get_taxonomy($tax_slug);
  851. if (!in_array($post_type, $temp_tax->object_type)) {
  852. continue;
  853. }
  854. $support_terms = true;
  855. $terms = wp_get_post_terms($post->ID, $tax_slug,
  856. array('fields' => 'all'));
  857. foreach ($terms as $term_id) {
  858. $post->_wpcf_post_terms[] = $term_id->term_taxonomy_id;
  859. }
  860. }
  861. }
  862. }
  863. // Get post template
  864. if (empty($post)) {
  865. $post = new stdClass();
  866. $post->_wpcf_post_template = false;
  867. $post->_wpcf_post_views_template = false;
  868. } else {
  869. $post->_wpcf_post_template = get_post_meta($post->ID,
  870. '_wp_page_template', true);
  871. $post->_wpcf_post_views_template = get_post_meta($post->ID,
  872. '_views_template', true);
  873. }
  874. if (empty($post->_wpcf_post_terms)) {
  875. $post->_wpcf_post_terms = array();
  876. }
  877. // Filter groups
  878. $groups = array();
  879. $groups_all = wpcf_admin_fields_get_groups();
  880. foreach ($groups_all as $temp_key => $temp_group) {
  881. if (empty($temp_group['is_active'])) {
  882. unset($groups_all[$temp_key]);
  883. continue;
  884. }
  885. // Get filters
  886. $groups_all[$temp_key]['_wp_types_group_post_types'] = explode(',',
  887. trim(get_post_meta($temp_group['id'],
  888. '_wp_types_group_post_types', true), ','));
  889. $groups_all[$temp_key]['_wp_types_group_terms'] = explode(',',
  890. trim(get_post_meta($temp_group['id'], '_wp_types_group_terms',
  891. true), ','));
  892. $groups_all[$temp_key]['_wp_types_group_templates'] = explode(',',
  893. trim(get_post_meta($temp_group['id'],
  894. '_wp_types_group_templates', true), ','));
  895. $post_type_filter = $groups_all[$temp_key]['_wp_types_group_post_types'][0] == 'all' ? -1 : 0;
  896. $taxonomy_filter = $groups_all[$temp_key]['_wp_types_group_terms'][0] == 'all' ? -1 : 0;
  897. $template_filter = $groups_all[$temp_key]['_wp_types_group_templates'][0] == 'all' ? -1 : 0;
  898. // See if post type matches
  899. if ($post_type_filter == 0 && in_array($post_type,
  900. $groups_all[$temp_key]['_wp_types_group_post_types'])) {
  901. $post_type_filter = 1;
  902. }
  903. // See if terms match
  904. if ($taxonomy_filter == 0) {
  905. foreach ($post->_wpcf_post_terms as $temp_post_term) {
  906. if (in_array($temp_post_term,
  907. $groups_all[$temp_key]['_wp_types_group_terms'])) {
  908. $taxonomy_filter = 1;
  909. }
  910. }
  911. }
  912. // See if template match
  913. if ($template_filter == 0) {
  914. if ((!empty($post->_wpcf_post_template) && in_array($post->_wpcf_post_template,
  915. $groups_all[$temp_key]['_wp_types_group_templates']))
  916. || (!empty($post->_wpcf_post_views_template) && in_array($post->_wpcf_post_views_template,
  917. $groups_all[$temp_key]['_wp_types_group_templates']))) {
  918. $template_filter = 1;
  919. }
  920. }
  921. // Filter by association
  922. if (empty($groups_all[$temp_key]['filters_association'])) {
  923. $groups_all[$temp_key]['filters_association'] = 'any';
  924. }
  925. // If context is post_relationship allow all groups that match post type
  926. if ($context == 'post_relationships_header') {
  927. $groups_all[$temp_key]['filters_association'] = 'any';
  928. }
  929. if ($post_type_filter == -1 && $taxonomy_filter == -1 && $template_filter == -1) {
  930. $passed = 1;
  931. } else if ($groups_all[$temp_key]['filters_association'] == 'any') {
  932. $passed = $post_type_filter == 1 || $taxonomy_filter == 1 || $template_filter == 1;
  933. } else {
  934. $passed = $post_type_filter != 0 && $taxonomy_filter != 0 && $template_filter != 0;
  935. }
  936. if (!$passed) {
  937. unset($groups_all[$temp_key]);
  938. } else {
  939. $groups_all[$temp_key]['fields'] = wpcf_admin_fields_get_fields_by_group($temp_group['id'],
  940. 'slug', true, false, true);
  941. }
  942. }
  943. $groups = apply_filters('wpcf_post_groups', $groups_all, $post, $context);
  944. return $groups;
  945. }
  946. /**
  947. * Stores fields for editor menu.
  948. *
  949. * @staticvar array $fields
  950. * @param type $field
  951. * @return array
  952. */
  953. function wpcf_admin_post_add_to_editor($field) {
  954. static $fields = array();
  955. if ($field == 'get') {
  956. return $fields;
  957. }
  958. if (empty($fields)) {
  959. add_action('admin_enqueue_scripts', 'wpcf_admin_post_add_to_editor_js');
  960. }
  961. $fields[$field['id']] = $field;
  962. }
  963. /**
  964. * Renders JS for editor menu.
  965. *
  966. * @return type
  967. */
  968. function wpcf_admin_post_add_to_editor_js() {
  969. global $post;
  970. $fields = wpcf_admin_post_add_to_editor('get');
  971. $groups = wpcf_admin_post_get_post_groups_fields($post);
  972. if (empty($fields) || empty($groups)) {
  973. return false;
  974. }
  975. $editor_addon = new Editor_addon('types',
  976. __('Insert Types Shortcode', 'wpcf'),
  977. WPCF_EMBEDDED_RES_RELPATH . '/js/types_editor_plugin.js',
  978. WPCF_EMBEDDED_RES_RELPATH . '/images/bw-logo-16.png');
  979. foreach ($groups as $group) {
  980. if (empty($group['fields'])) {
  981. continue;
  982. }
  983. foreach ($group['fields'] as $group_field_id => $group_field) {
  984. if (!isset($fields[$group_field_id])) {
  985. continue;
  986. }
  987. $field = $fields[$group_field_id];
  988. $data = wpcf_fields_type_action($field['type']);
  989. $callback = '';
  990. if (isset($data['editor_callback'])) {
  991. $callback = sprintf($data['editor_callback'], $field['id']);
  992. } else {
  993. // Set callback if function exists
  994. $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
  995. $callback = function_exists($function) ? 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\')' : '';
  996. }
  997. $editor_addon->add_insert_shortcode_menu(stripslashes($field['name']),
  998. trim(wpcf_fields_get_shortcode($field), '[]'),
  999. $group['name'], $callback);
  1000. }
  1001. }
  1002. }
  1003. /**
  1004. * Adds items to view dropdown.
  1005. *
  1006. * @param type $items
  1007. * @return type
  1008. */
  1009. function wpcf_admin_post_editor_addon_menus_filter($items) {
  1010. $groups = wpcf_admin_fields_get_groups();
  1011. $all_post_types = implode(' ', get_post_types(array('public' => true)));
  1012. $add = array();
  1013. if (!empty($groups)) {
  1014. // $group_id is blank therefore not equal to $group['id']
  1015. // use array for item key and CSS class
  1016. $item_styles = array();
  1017. foreach ($groups as $group_id => $group) {
  1018. $fields = wpcf_admin_fields_get_fields_by_group($group['id'],
  1019. 'slug', true, false, true);
  1020. if (!empty($fields)) {
  1021. // code from Types used here without breaking the flow
  1022. // get post types list for every group or apply all
  1023. $post_types = get_post_meta($group['id'],
  1024. '_wp_types_group_post_types', true);
  1025. if ($post_types == 'all') {
  1026. $post_types = $all_post_types;
  1027. }
  1028. $post_types = trim(str_replace(',', ' ', $post_types));
  1029. $item_styles[$group['name']] = $post_types;
  1030. foreach ($fields as $field_id => $field) {
  1031. // Get field data
  1032. $data = wpcf_fields_type_action($field['type']);
  1033. // Get inherited field
  1034. if (isset($data['inherited_field_type'])) {
  1035. $inherited_field_data = wpcf_fields_type_action($data['inherited_field_type']);
  1036. }
  1037. $callback = '';
  1038. if (isset($data['editor_callback'])) {
  1039. $callback = sprintf($data['editor_callback'],
  1040. $field['id']);
  1041. } else {
  1042. // Set callback if function exists
  1043. $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
  1044. $callback = function_exists($function) ? 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\')' : '';
  1045. }
  1046. $add[$group['name']][stripslashes($field['name'])] = array(stripslashes($field['name']), trim(wpcf_fields_get_shortcode($field),
  1047. '[]'), $group['name'], $callback);
  1048. // Process JS
  1049. if (!empty($data['meta_box_js'])) {
  1050. foreach ($data['meta_box_js'] as $handle => $data_script) {
  1051. if (isset($data_script['inline'])) {
  1052. add_action('admin_footer',
  1053. $data_script['inline']);
  1054. continue;
  1055. }
  1056. $deps = !empty($data_script['deps']) ? $data_script['deps'] : array();
  1057. wp_enqueue_script($handle, $data_script['src'],
  1058. $deps, WPCF_VERSION);
  1059. }
  1060. }
  1061. // Process CSS
  1062. if (!empty($data['meta_box_css'])) {
  1063. foreach ($data['meta_box_css'] as $handle => $data_script) {
  1064. $deps = !empty($data_script['deps']) ? $data_script['deps'] : array();
  1065. if (isset($data_script['inline'])) {
  1066. add_action('admin_header',
  1067. $

Large files files are truncated, but you can click here to view the full file