PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/toolset.dev/wp-content/plugins/framework-installer/embedded-types/functions.php

https://github.com/bfay/aie-toolset
PHP | 811 lines | 510 code | 59 blank | 242 comment | 95 complexity | 72fb42b97114acdc549c896f9e6ac6ee MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /*
  3. * Basic and init functions.
  4. * Since Types 1.2 moved from /embedded/types.php
  5. *
  6. * $HeadURL: https://www.onthegosystems.com/misc_svn/cck/tags/1.5.7/embedded/functions.php $
  7. * $LastChangedDate: 2014-05-23 14:25:20 +0000 (Fri, 23 May 2014) $
  8. * $LastChangedRevision: 22686 $
  9. * $LastChangedBy: marcin $
  10. *
  11. */
  12. /**
  13. * Caches get_post_meta() calls.
  14. *
  15. * @staticvar array $cache
  16. * @param type $post_id
  17. * @param type $meta_key
  18. * @param type $single
  19. * @return string
  20. */
  21. function wpcf_get_post_meta( $post_id, $meta_key, $single ) {
  22. static $cache = array();
  23. if ( !isset( $cache[$post_id] ) ) {
  24. $cache[$post_id] = get_post_custom( $post_id );
  25. }
  26. if ( isset( $cache[$post_id][$meta_key] ) ) {
  27. if ( $single && isset( $cache[$post_id][$meta_key][0] ) ) {
  28. return maybe_unserialize( $cache[$post_id][$meta_key][0] );
  29. } else if ( !$single && !empty( $cache[$post_id][$meta_key] ) ) {
  30. return maybe_unserialize( $cache[$post_id][$meta_key] );
  31. }
  32. }
  33. return '';
  34. }
  35. /**
  36. * Calculates relative path for given file.
  37. *
  38. * @param type $file Absolute path to file
  39. * @return string Relative path
  40. */
  41. function wpcf_get_file_relpath( $file ) {
  42. $is_https = isset( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) == 'on';
  43. $http_protocol = $is_https ? 'https' : 'http';
  44. $base_root = $http_protocol . '://' . $_SERVER['HTTP_HOST'];
  45. $base_url = $base_root;
  46. $dir = rtrim( dirname( $file ), '\/' );
  47. if ( $dir ) {
  48. $base_path = $dir;
  49. $base_url .= $base_path;
  50. $base_path .= '/';
  51. } else {
  52. $base_path = '/';
  53. }
  54. $relpath = $base_root
  55. . str_replace(
  56. str_replace( '\\', '/',
  57. realpath( $_SERVER['DOCUMENT_ROOT'] ) )
  58. , '', str_replace( '\\', '/', dirname( $file ) )
  59. );
  60. return $relpath;
  61. }
  62. /**
  63. * after_setup_theme hook.
  64. */
  65. function wpcf_embedded_after_setup_theme_hook() {
  66. $custom_types = get_option( 'wpcf-custom-types', array() );
  67. if ( !empty( $custom_types ) ) {
  68. foreach ( $custom_types as $post_type => $data ) {
  69. if ( !empty( $data['supports']['thumbnail'] ) ) {
  70. if ( !current_theme_supports( 'post-thumbnails' ) ) {
  71. add_theme_support( 'post-thumbnails' );
  72. remove_post_type_support( 'post', 'thumbnail' );
  73. remove_post_type_support( 'page', 'thumbnail' );
  74. } else {
  75. add_post_type_support( $post_type, 'thumbnail' );
  76. }
  77. }
  78. }
  79. }
  80. }
  81. /**
  82. * Inits custom types and taxonomies.
  83. */
  84. function wpcf_init_custom_types_taxonomies() {
  85. $custom_taxonomies = get_option( 'wpcf-custom-taxonomies', array() );
  86. if ( !empty( $custom_taxonomies ) ) {
  87. require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-taxonomies.php';
  88. wpcf_custom_taxonomies_init();
  89. }
  90. $custom_types = get_option( 'wpcf-custom-types', array() );
  91. if ( !empty( $custom_types ) ) {
  92. require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-types.php';
  93. wpcf_custom_types_init();
  94. }
  95. }
  96. /**
  97. * Returns meta_key type for specific field type.
  98. *
  99. * @param type $type
  100. * @return type
  101. */
  102. function types_get_field_type( $type ) {
  103. require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
  104. $data = wpcf_fields_type_action( $type );
  105. if ( !empty( $data['meta_key_type'] ) ) {
  106. return $data['meta_key_type'];
  107. }
  108. return 'CHAR';
  109. }
  110. /**
  111. * Imports settings.
  112. */
  113. function wpcf_embedded_check_import() {
  114. if ( file_exists( WPCF_EMBEDDED_ABSPATH . '/settings.php' ) ) {
  115. require_once WPCF_EMBEDDED_ABSPATH . '/admin.php';
  116. require_once WPCF_EMBEDDED_ABSPATH . '/settings.php';
  117. $dismissed = get_option( 'wpcf_dismissed_messages', array() );
  118. if ( in_array( $timestamp, $dismissed ) ) {
  119. return false;
  120. }
  121. if ( $timestamp > get_option( 'wpcf-types-embedded-import', 0 ) ) {
  122. if ( !$auto_import ) {
  123. $link = "<a href=\"" . admin_url( '?types-embedded-import=1&amp;_wpnonce=' . wp_create_nonce( 'embedded-import' ) ) . "\">";
  124. $text = sprintf( __( 'You have Types import pending. %sClick here to import.%s %sDismiss message.%s',
  125. 'wpcf' ), $link, '</a>',
  126. "<a onclick=\"jQuery(this).parent().parent().fadeOut();\" class=\"wpcf-ajax-link\" href=\""
  127. . admin_url( 'admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=dismiss_message&amp;id='
  128. . $timestamp . '&amp;_wpnonce=' . wp_create_nonce( 'dismiss_message' ) ) . "\">",
  129. '</a>' );
  130. wpcf_admin_message( $text );
  131. }
  132. if ( $auto_import || (isset( $_GET['types-embedded-import'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'],
  133. 'embedded-import' )) ) {
  134. if ( file_exists( WPCF_EMBEDDED_ABSPATH . '/settings.xml' ) ) {
  135. $_POST['overwrite-groups'] = 1;
  136. $_POST['overwrite-fields'] = 1;
  137. $_POST['overwrite-types'] = 1;
  138. $_POST['overwrite-tax'] = 1;
  139. // $_POST['delete-groups'] = 0;
  140. // $_POST['delete-fields'] = 0;
  141. // $_POST['delete-types'] = 0;
  142. // $_POST['delete-tax'] = 0;
  143. $_POST['post_relationship'] = 1;
  144. require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
  145. require_once WPCF_EMBEDDED_INC_ABSPATH . '/import-export.php';
  146. $data = @file_get_contents( WPCF_EMBEDDED_ABSPATH . '/settings.xml' );
  147. wpcf_admin_import_data( $data, false, 'types-auto-import' );
  148. update_option( 'wpcf-types-embedded-import', $timestamp );
  149. wp_redirect( admin_url() );
  150. } else {
  151. $code = __( 'settings.xml file missing', 'wpcf' );
  152. wpcf_admin_message( $code, 'error' );
  153. }
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * Display information about upgrading to the plugin version of types.
  160. *
  161. */
  162. function wpcf_promote_types_admin() {
  163. $custom_types = get_option( 'wpcf-custom-types', array() );
  164. ?>
  165. <?php
  166. if ( sizeof( $custom_types ) > 0 ) {
  167. echo '<p>' . __( 'Types creates Custom Post Types. These are user-defined WordPress content types. On your theme the following Types are defined:',
  168. 'wpcf' ) . "</p>\n";
  169. echo "<ul style='margin-left:20px;'>\n";
  170. foreach ( $custom_types as $type ) {
  171. echo "<li>" . $type['labels']['name'] . "</li>\n";
  172. }
  173. echo "</ul>\n";
  174. }
  175. ?>
  176. <p><?php
  177. echo sprintf( __( 'If you want to edit these or create your own you can download the full version of <strong>Types</strong> from <a href="%s">%s</a>',
  178. 'wpcf' ), 'http://wordpress.org/extend/plugins/types/',
  179. 'http://wordpress.org/extend/plugins/types/' );
  180. ?></p>
  181. <?php
  182. }
  183. /**
  184. * Actions for outside fields control.
  185. *
  186. * @param type $action
  187. */
  188. function wpcf_types_cf_under_control( $action = 'add', $args = array(),
  189. $post_type = 'wp-types-group', $meta_name = 'wpcf-fields' ) {
  190. global $wpcf_types_under_control;
  191. $wpcf_types_under_control['errors'] = array();
  192. switch ( $action ) {
  193. case 'add':
  194. $fields = wpcf_admin_fields_get_fields( false, true, false,
  195. $meta_name, false );
  196. foreach ( $args['fields'] as $field_id ) {
  197. $field_type = !empty( $args['type'] ) ? $args['type'] : 'textfield';
  198. if ( strpos( $field_id, md5( 'wpcf_not_controlled' ) ) !== false ) {
  199. $field_id_name = str_replace( '_' . md5( 'wpcf_not_controlled' ), '', $field_id );
  200. $field_id_add = preg_replace( '/^wpcf\-/', '', $field_id_name );
  201. // Activating field that previously existed in Types
  202. if ( array_key_exists( $field_id_add, $fields ) ) {
  203. $fields[$field_id_add]['data']['disabled'] = 0;
  204. } else { // Adding from outside
  205. $fields[$field_id_add]['id'] = $field_id_add;
  206. $fields[$field_id_add]['type'] = $field_type;
  207. $fields[$field_id_add]['name'] = $field_id_name;
  208. $fields[$field_id_add]['slug'] = $field_id_name;
  209. $fields[$field_id_add]['description'] = '';
  210. $fields[$field_id_add]['data'] = array();
  211. // @TODO WATCH THIS! MUST NOT BE DROPPED IN ANY CASE
  212. $fields[$field_id_add]['data']['controlled'] = 1;
  213. }
  214. $unset_key = array_search( $field_id, $args['fields'] );
  215. if ( $unset_key !== false ) {
  216. unset( $args['fields'][$unset_key] );
  217. $args['fields'][$unset_key] = $field_id_add;
  218. }
  219. }
  220. }
  221. wpcf_admin_fields_save_fields( $fields, true, $meta_name );
  222. return $args['fields'];
  223. break;
  224. case 'check_exists':
  225. $fields = wpcf_admin_fields_get_fields( false, true, false,
  226. $meta_name, false );
  227. $field = $args;
  228. if ( array_key_exists( $field, $fields ) && empty( $fields[$field]['data']['disabled'] ) ) {
  229. return true;
  230. }
  231. return false;
  232. break;
  233. case 'check_outsider':
  234. $fields = wpcf_admin_fields_get_fields( false, true, false,
  235. $meta_name, false );
  236. $field = $args;
  237. if ( array_key_exists( $field, $fields ) && !empty( $fields[$field]['data']['controlled'] ) ) {
  238. return true;
  239. }
  240. return false;
  241. break;
  242. default:
  243. break;
  244. }
  245. }
  246. /**
  247. * Controlls meta prefix.
  248. *
  249. * @param array $field
  250. */
  251. function wpcf_types_get_meta_prefix( $field = array() ) {
  252. if ( empty( $field ) ) {
  253. return WPCF_META_PREFIX;
  254. }
  255. if ( !empty( $field['data']['controlled'] ) ) {
  256. return '';
  257. }
  258. return WPCF_META_PREFIX;
  259. }
  260. /**
  261. * Compares WP versions
  262. * @global type $wp_version
  263. * @param type $version
  264. * @param type $operator
  265. * @return type
  266. */
  267. function wpcf_compare_wp_version( $version = '3.2.1', $operator = '>' ) {
  268. global $wp_version;
  269. return version_compare( $wp_version, $version, $operator );
  270. }
  271. /**
  272. * Gets post type with data to which belongs.
  273. *
  274. * @param type $post_type
  275. * @return type
  276. */
  277. function wpcf_pr_get_belongs( $post_type ) {
  278. require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
  279. return wpcf_pr_admin_get_belongs( $post_type );
  280. }
  281. /**
  282. * Gets all post types and data that owns.
  283. *
  284. * @param type $post_type
  285. * @return type
  286. */
  287. function wpcf_pr_get_has( $post_type ) {
  288. require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
  289. return wpcf_pr_admin_get_has( $post_type );
  290. }
  291. /**
  292. * Gets individual post ID to which queried post belongs.
  293. *
  294. * @param type $post_id
  295. * @param type $post_type Post type of owner
  296. * @return type
  297. */
  298. function wpcf_pr_post_get_belongs( $post_id, $post_type ) {
  299. return get_post_meta( $post_id, '_wpcf_belongs_' . $post_type . '_id', true );
  300. }
  301. /**
  302. * Gets all posts that belong to queried post, grouped by post type.
  303. *
  304. * @param type $post_id
  305. * @param type $post_type
  306. * @return type
  307. */
  308. function wpcf_pr_post_get_has( $post_id, $post_type_q = null ) {
  309. $post_type = get_post_type( $post_id );
  310. $has = array_keys( wpcf_pr_get_has( $post_type ) );
  311. $add = is_null( $post_type_q ) ? '&post_type=any' : '&post_type=' . $post_type_q;
  312. $posts = get_posts( 'numberposts=-1&post_status=null&meta_key=_wpcf_belongs_'
  313. . $post_type . '_id&meta_value=' . $post_id . $add );
  314. $results = array();
  315. foreach ( $posts as $post ) {
  316. if ( !in_array( $post->post_type, $has ) ) {
  317. continue;
  318. }
  319. $results[$post->post_type][] = $post;
  320. }
  321. return is_null( $post_type_q ) ? $results : array_shift( $results );
  322. }
  323. /**
  324. * Gets settings.
  325. */
  326. function wpcf_get_settings( $specific = false ) {
  327. $defaults = array(
  328. 'add_resized_images_to_library' => 0,
  329. 'register_translations_on_import' => 1,
  330. 'images_remote' => 0,
  331. 'images_remote_cache_time' => '36',
  332. 'help_box' => 'by_types',
  333. );
  334. $settings = wp_parse_args( get_option( 'wpcf_settings', array() ), $defaults );
  335. $settings = apply_filters( 'types_settings', $settings );
  336. if ( $specific ) {
  337. return isset( $settings[$specific] ) ? $settings[$specific] : false;
  338. }
  339. return $settings;
  340. }
  341. /**
  342. * Saves settings.
  343. */
  344. function wpcf_save_settings( $settings ) {
  345. update_option( 'wpcf_settings', $settings );
  346. }
  347. /**
  348. * Check if it can be repetitive
  349. * @param type $field
  350. * @return type
  351. */
  352. function wpcf_admin_can_be_repetitive( $type ) {
  353. return !in_array( $type,
  354. array('checkbox', 'checkboxes', 'wysiwyg', 'radio', 'select') );
  355. }
  356. /**
  357. * Check if field is repetitive
  358. * @param type $type
  359. * @return type
  360. */
  361. function wpcf_admin_is_repetitive( $field ) {
  362. if ( !isset( $field['data']['repetitive'] ) || !isset( $field['type'] ) ) {
  363. return false;
  364. }
  365. $check = intval( $field['data']['repetitive'] );
  366. return !empty( $check ) && wpcf_admin_can_be_repetitive( $field['type'] );
  367. }
  368. /**
  369. * Returns unique ID.
  370. *
  371. * @staticvar array $cache
  372. * @param type $cache_key
  373. * @return type
  374. */
  375. function wpcf_unique_id( $cache_key ) {
  376. $cache_key = md5( strval( $cache_key ) . strval( time() ) . rand() );
  377. static $cache = array();
  378. if ( !isset( $cache[$cache_key] ) ) {
  379. $cache[$cache_key] = 1;
  380. } else {
  381. $cache[$cache_key] += 1;
  382. }
  383. return $cache_key . '-' . $cache[$cache_key];
  384. }
  385. /**
  386. * Determine if platform is Win
  387. *
  388. * @return type
  389. */
  390. function wpcf_is_windows() {
  391. global $wpcf;
  392. $is_windows = PHP_OS == "WIN32" || PHP_OS == "WINNT";
  393. if ( isset( $wpcf->debug ) ) {
  394. $wpcf->debug->is_windows = $is_windows;
  395. }
  396. return $is_windows;
  397. }
  398. /**
  399. * Parses array as string
  400. *
  401. * @param type $array
  402. */
  403. function wpcf_parse_array_to_string( $array ) {
  404. $s = '';
  405. foreach ( (array) $array as $param => $value ) {
  406. $s .= strval( $param ) . '=' . urlencode( strval( $value ) ) . '&';
  407. }
  408. return trim( $s, '&' );
  409. }
  410. /**
  411. * Get main post ID.
  412. *
  413. * @param type $context
  414. * @return type
  415. */
  416. function wpcf_get_post_id( $context = 'group' ) {
  417. if ( !is_admin() ) {
  418. /*
  419. *
  420. * TODO Check if frontend is fine (rendering children).
  421. * get_post() previously WP 3.5 requires $post_id
  422. */
  423. $post_id = null;
  424. if ( wpcf_compare_wp_version( '3.5', '<' ) ) {
  425. global $post;
  426. $post_id = !empty( $post->ID ) ? $post->ID : -1;
  427. }
  428. $_post = get_post( $post_id );
  429. return !empty( $_post->ID ) ? $_post->ID : -1;
  430. }
  431. /*
  432. * TODO Explore possible usage for $context
  433. */
  434. $post = wpcf_admin_get_edited_post();
  435. return empty( $post->ID ) ? -1 : $post->ID;
  436. }
  437. /**
  438. * Basic scripts
  439. */
  440. function wpcf_enqueue_scripts() {
  441. if ( !wpcf_is_embedded() ) {
  442. /*
  443. *
  444. * Basic JS
  445. */
  446. wp_enqueue_script( 'wpcf-js', WPCF_RES_RELPATH . '/js/basic.js',
  447. array('jquery', 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-tabs'),
  448. WPCF_VERSION );
  449. /*
  450. *
  451. * Basic CSS
  452. */
  453. wp_enqueue_style( 'wpcf-css', WPCF_RES_RELPATH . '/css/basic.css',
  454. array(), WPCF_VERSION );
  455. }
  456. /*
  457. *
  458. * Basic JS
  459. */
  460. wp_enqueue_script( 'wpcf-js-embedded',
  461. WPCF_EMBEDDED_RES_RELPATH . '/js/basic.js',
  462. array('jquery', 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-tabs'),
  463. WPCF_VERSION );
  464. /*
  465. *
  466. * Basic CSS
  467. */
  468. wp_enqueue_style( 'wpcf-css-embedded',
  469. WPCF_EMBEDDED_RES_RELPATH . '/css/basic.css', array(), WPCF_VERSION );
  470. /*
  471. *
  472. * Other components
  473. */
  474. // Repetitive
  475. wp_enqueue_script(
  476. 'wpcf-repeater', WPCF_EMBEDDED_RES_RELPATH . '/js/repetitive.js',
  477. array('wpcf-js-embedded'), WPCF_VERSION
  478. );
  479. wp_enqueue_style(
  480. 'wpcf-repeater', WPCF_EMBEDDED_RES_RELPATH . '/css/repetitive.css',
  481. array('wpcf-css-embedded'), WPCF_VERSION
  482. );
  483. // Conditional
  484. wp_enqueue_script( 'types-conditional' );
  485. wpcf_admin_add_js_settings( 'wpcfConditionalVerify_nonce',
  486. wp_create_nonce( 'cd_verify' )
  487. );
  488. wpcf_admin_add_js_settings( 'wpcfConditionalVerifyGroup',
  489. wp_create_nonce( 'cd_group_verify' ) );
  490. // RTL
  491. if ( is_rtl() ) {
  492. wp_enqueue_style(
  493. 'wpcf-rtl', WPCF_EMBEDDED_RES_RELPATH . '/css/rtl.css',
  494. array('wpcf-css-embedded'), WPCF_VERSION
  495. );
  496. }
  497. }
  498. /**
  499. * Load all scripts required on edit post screen.
  500. *
  501. * @since 1.2.1
  502. * @todo Make loading JS more clear for all components.
  503. */
  504. function wpcf_edit_post_screen_scripts() {
  505. wpcf_enqueue_scripts();
  506. wp_enqueue_script( 'wpcf-fields-post',
  507. WPCF_EMBEDDED_RES_RELPATH . '/js/fields-post.js', array('jquery'),
  508. WPCF_VERSION );
  509. // TODO Switch to 1.11.1 jQuery Validation
  510. // wp_enqueue_script( 'types-js-validation' );
  511. wp_enqueue_script( 'wpcf-form-validation',
  512. WPCF_EMBEDDED_RES_RELPATH . '/js/'
  513. . 'jquery-form-validation/jquery.validate.js', array('jquery'),
  514. WPCF_VERSION );
  515. wp_enqueue_script( 'wpcf-form-validation-additional',
  516. WPCF_EMBEDDED_RES_RELPATH . '/js/'
  517. . 'jquery-form-validation/additional-methods.min.js',
  518. array('jquery'), WPCF_VERSION );
  519. wp_enqueue_style( 'wpcf-fields-basic',
  520. WPCF_EMBEDDED_RES_RELPATH . '/css/basic.css', array(), WPCF_VERSION );
  521. wp_enqueue_style( 'wpcf-fields-post',
  522. WPCF_EMBEDDED_RES_RELPATH . '/css/fields-post.css',
  523. array('wpcf-fields-basic'), WPCF_VERSION );
  524. wp_enqueue_style( 'wpcf-usermeta',
  525. WPCF_EMBEDDED_RES_RELPATH . '/css/usermeta.css',
  526. array('wpcf-fields-basic'), WPCF_VERSION );
  527. wp_enqueue_script( 'toolset-colorbox' );
  528. wp_enqueue_style( 'toolset-colorbox' );
  529. wp_enqueue_style( 'toolset-font-awesome' );
  530. }
  531. /**
  532. * Check if running embedded version.
  533. *
  534. * @return type
  535. */
  536. function wpcf_is_embedded() {
  537. return defined( 'WPCF_RUNNING_EMBEDDED' ) && WPCF_RUNNING_EMBEDDED;
  538. }
  539. /**
  540. * Returns custom post type settings.
  541. *
  542. * @param type $post_type
  543. * @return type
  544. */
  545. function wpcf_get_custom_post_type_settings( $item ) {
  546. $custom = get_option( 'wpcf-custom-types', array() );
  547. return !empty( $custom[$item] ) ? $custom[$item] : array();
  548. }
  549. /**
  550. * Returns taxonomy settings.
  551. *
  552. * @param type $taxonomy
  553. * @return type
  554. */
  555. function wpcf_get_custom_taxonomy_settings( $item ) {
  556. $custom = get_option( 'wpcf-custom-taxonomies', array() );
  557. return !empty( $custom[$item] ) ? $custom[$item] : array();
  558. }
  559. /**
  560. * Load JS and CSS for field type.
  561. *
  562. * Core function. Works and stable. Do not move or change.
  563. * If required, add hooks only.
  564. *
  565. * @staticvar array $cache
  566. * @param string $type
  567. * @return string
  568. */
  569. function wpcf_field_enqueue_scripts( $type ) {
  570. global $wpcf;
  571. static $cache = array();
  572. $config = wpcf_fields_type_action( $type );
  573. if ( !empty( $config ) ) {
  574. // Check if cached
  575. if ( isset( $cache[$config['id']] ) ) {
  576. return $cache[$config['id']];
  577. }
  578. // Use field object
  579. $wpcf->field->enqueue_script( $config );
  580. $wpcf->field->enqueue_style( $config );
  581. $cache[$config['id']] = $config;
  582. return $config;
  583. } else {
  584. $wpcf->debug->errors['missing_type_config'][] = $type;
  585. return array();
  586. }
  587. }
  588. /**
  589. * Get file URL.
  590. *
  591. * @uses WPCF_Path (functions taken from CRED_Loader)
  592. * @param type $file
  593. * @return type
  594. */
  595. function wpcf_get_file_url( $file, $use_baseurl = true ) {
  596. WPCF_Loader::loadClass( 'path' );
  597. return WPCF_Path::getFileUrl( $file, $use_baseurl );
  598. }
  599. /**
  600. * Checks if timestamp supports negative values.
  601. *
  602. * @return type
  603. */
  604. function fields_date_timestamp_neg_supported() {
  605. return strtotime( 'Fri, 13 Dec 1950 20:45:54 UTC' ) === -601010046;
  606. }
  607. /**
  608. * Returns media size.
  609. *
  610. * @global type $content_width
  611. * @param type $widescreen
  612. * @return type
  613. */
  614. function wpcf_media_size( $widescreen = false ) {
  615. global $content_width;
  616. if ( !empty( $content_width ) ) {
  617. $height = $widescreen ? round( $content_width * 9 / 16 ) : round( $content_width * 3 / 4 );
  618. return array($content_width, $height);
  619. }
  620. return $widescreen ? array(450, 253) : array(450, 320);
  621. }
  622. /**
  623. * Validation wrapper.
  624. *
  625. * @param type $method
  626. * @param type $args
  627. * @return boolean
  628. */
  629. function types_validate( $method, $args ) {
  630. WPCF_Loader::loadClass( 'validation-cakephp' );
  631. if ( is_callable( array('Wpcf_Cake_Validation', $method) ) ) {
  632. if ( !is_array( $args ) ) {
  633. $args = array($args);
  634. }
  635. return @call_user_func_array( array('Wpcf_Cake_Validation', $method),
  636. $args );
  637. }
  638. return false;
  639. }
  640. /**
  641. * Filters field for Toolset forms.
  642. *
  643. * @param type $field
  644. * @param type $post
  645. * @param type $value
  646. * @return type
  647. */
  648. function wptoolset_forms_types_filter_field( $form_id, $field, $value = null ) {
  649. if ( is_array( $value ) ) $value = array_shift( $value );
  650. $value = '$value';
  651. $cond = array();
  652. if ( !empty( $field['data']['conditional_display']['custom_use'] ) ) {
  653. $cond = array(
  654. 'prefix' => WPCF_META_PREFIX,
  655. 'custom' => $field['data']['conditional_display']['custom'],
  656. );
  657. } else if ( isset( $field['data']['conditional_display']['conditions'] ) ) {
  658. $cond = array(
  659. 'relation' => $field['data']['conditional_display']['relation'],
  660. 'conditions' => array()
  661. );
  662. foreach ( $field['data']['conditional_display']['conditions'] as $d ) {
  663. $c_field = types_get_field( $d['field'] );
  664. if ( !empty( $c_field ) ) {
  665. if ( $c_field['type'] == 'date' ) {
  666. $date_format = get_option( 'date_format' );
  667. switch ( $date_format ) {
  668. case 'd/m/Y':
  669. $d['value'] = str_replace( '/', '-', $d['value'] );
  670. break;
  671. default:
  672. break;
  673. }
  674. $d['value'] = strtotime( $d['value'] );
  675. }
  676. $_c = array(WPCF_META_PREFIX . $d['field'], $d['operation'], $d['value'],
  677. $c_field['type']);
  678. $cond['conditions'][] = $_c;
  679. }
  680. }
  681. }
  682. $validation = array();
  683. if ( isset( $field['data']['validate'] ) ) {
  684. foreach ( $field['data']['validate'] as $rule => $d ) {
  685. if ( $d['active'] ) {
  686. $validation[$rule] = array(
  687. 'args' => isset( $d['args'] ) ? array_unshift( $value,
  688. $d['args'] ) : array($value, true),
  689. 'message' => $d['message']
  690. );
  691. }
  692. }
  693. }
  694. $_field = array(
  695. // 'form_id' => $form_id,
  696. 'id' => WPCF_META_PREFIX . $field['id'],
  697. 'meta_key' => $field['meta_key'],
  698. 'meta_type' => 'postmeta',
  699. 'type' => $field['type'],
  700. 'slug' => $field['id'],
  701. 'title' => $field['name'],
  702. 'description' => $field['description'],
  703. 'name' => "wpcf[{$field['id']}]",
  704. // 'value' => $value,
  705. 'repetitive' => (bool) @$field['data']['repetitive'],
  706. 'validation' => $validation,
  707. 'conditional' => $cond,
  708. );
  709. if ( $field['type'] == 'checkbox' ) {
  710. $_field['default_value'] = $field['data']['set_value'];
  711. }
  712. if ( $field['type'] == 'checkboxes' && !empty( $field['data']['options'] ) ) {
  713. global $pagenow;
  714. foreach ( $field['data']['options'] as $option_key => $option ) {
  715. // Set value
  716. $_field['options'][$option_key] = array(
  717. 'value' => $option['set_value'],
  718. 'title' => wpcf_translate( 'field ' . $field['id'] . ' option '
  719. . $option_key . ' title', $option['title'] ),
  720. 'checked' => (!empty( $field['value'][$option_key] )// Also check new post
  721. || ($pagenow == 'post-new.php' && !empty( $option['checked'] ))) ? true : false,
  722. 'name' => 'wpcf[' . $field['id'] . '][' . $option_key . ']',
  723. 'id' => $option_key . '_'
  724. . wpcf_unique_id( serialize( $field ) ),
  725. );
  726. }
  727. }
  728. if ( $field['type'] == 'date' ) {
  729. $_field['add_time'] = !empty( $field['data']['date_and_time'] ) && $field['data']['date_and_time'] == 'and_time';
  730. }
  731. if ( ($field['type'] == 'radio' || $field['type'] == 'select') && !empty( $field['data']['options'] ) ) {
  732. // debug( $field, false );
  733. foreach ( $field['data']['options'] as $k => $option ) {
  734. if ( $k == 'default' ) {
  735. continue;
  736. }
  737. if ( $field['data']['options']['default'] == $k ) {
  738. $_field['default_value'] = $option['value'];
  739. }
  740. $_field['options'][] = array(
  741. 'value' => $option['value'],
  742. 'title' => wpcf_translate( 'field ' . $field['id'] . ' option '
  743. . $k . ' title', $option['title'] ),
  744. );
  745. }
  746. // debug( $_field );
  747. }
  748. if ( $field['type'] == 'radio' ) {
  749. $_field['type'] = 'radios';
  750. }
  751. return $_field;
  752. }