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

/wp-content/plugins/types/embedded/common/toolset-forms/classes/class.types.php

https://github.com/ngocphuuit/wordpress-tax
PHP | 496 lines | 218 code | 35 blank | 243 comment | 35 complexity | 518919b18aee46260a122c04496d0148 MD5 | raw file
Possible License(s): GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Types fields specific
  4. *
  5. * $HeadURL: https://www.onthegosystems.com/misc_svn/common/tags/Views-1.6.1-Types-1.5.7/toolset-forms/classes/class.types.php $
  6. * $LastChangedDate: 2014-04-16 10:42:22 +0200 (Wed, 16 Apr 2014) $
  7. * $LastChangedRevision: 21561 $
  8. * $LastChangedBy: marcin $
  9. *
  10. */
  11. /**
  12. * Class description
  13. *
  14. * @author Srdjan
  15. */
  16. class WPToolset_Types
  17. {
  18. /**
  19. * Filters Types field to match data structure needed for shared code.
  20. *
  21. * @global type $pagenow
  22. * @staticvar array $cache
  23. * @param type $field array|string $field settings array (as stored in DB) or field ID
  24. * @param type $post_id Post or user ID used for conditional
  25. * @return array
  26. */
  27. static function filterField($field, $post_id = null)
  28. {
  29. // Get field settings (as when using get_option('wpcf-fields') from DB)
  30. $field = self::getConfig( $field );
  31. if ( is_null( $field ) ) return array();
  32. // Caching
  33. static $cache = array();
  34. $cache_key = md5( serialize( $field ) . $post_id );
  35. if ( isset( $cache[$cache_key] ) ) {
  36. return $cache[$cache_key];
  37. }
  38. // Prefix - used to construct ID of field @see at bottom 'General field settings'
  39. $prefix = self::getPrefix( $field );
  40. /* Suffix - used to construct ID of this field and other fields connected
  41. * to it via conditionals.
  42. *
  43. * @see at bottom 'General field settings'
  44. *
  45. * Reason to use it - Types have child posts forms inside same form as
  46. * main fields. It's like having same sets of fields inside same form.
  47. * Main post fields do not have suffix.
  48. *
  49. * Example main field:
  50. * ID: wpcf-text
  51. * conditional: '$wpcf-date > DATE(01,02,2014)'
  52. *
  53. * Example child field:
  54. * ID: wpcf-text-123
  55. * conditional: '$wpcf-date-123 > DATE(01,02,2014)'
  56. * Suffix is child post ID (wpcf-text-$child_post_id).
  57. *
  58. * That way right triggers and conditional fields are mapped.
  59. */
  60. $suffix = isset( $field['suffix'] ) ? $field['suffix'] : '';
  61. /* General field settings
  62. *
  63. * Main settings that are returned.
  64. */
  65. $_field = array(
  66. 'id' => $prefix . $field['id'] . $suffix, // Used as main ID (raw date wpt-id), used to connect conditional relations
  67. 'meta_key' => $prefix . $field['id'], // Used by Types (meta key of field saved to DB)
  68. 'meta_type' => isset( $field['meta_type'] ) ? $field['meta_type'] : 'postmeta', // Used by Types (postmeta|usermeta)
  69. 'type' => $field['type'], // Type of field (textfield, skype, email etc)
  70. 'slug' => $field['id'], // Not sure if used by Types TODO REVISE
  71. 'title' => self::translate( "field {$field['id']} name",
  72. $field['name'] ), // Translated title
  73. 'description' => !empty( $field['description'] ) ? self::translate( "field {$field['id']} description",
  74. $field['description'] ) : '', // Translated description
  75. 'name' => isset( $field['form_name'] ) ? $field['form_name'] : "wpcf[{$field['id']}]", // Form element name, default wpcf[$no-prefix-slug]
  76. 'repetitive' => self::isRepetitive( $field ), // Is repetitive?
  77. 'validation' => self::filterValidation( $field ), // Validation settings
  78. 'conditional' => self::filterConditional( $field, $post_id ), // Conditional settings
  79. );
  80. /* Specific field settings
  81. *
  82. * There are some field types that needs extra data or adjusted data
  83. * for readibility.
  84. */
  85. switch( $field['type'] ) {
  86. // Checkbox set default_value
  87. case 'checkbox':
  88. $_field['default_value'] = $field['data']['set_value'];
  89. $_field['checked'] = array_key_exists( 'checked', $field['data'] )? $field['data']['checked']:false;
  90. break;
  91. // Checkboxes set field [options]
  92. case 'checkboxes':
  93. if ( !empty( $field['data']['options'] ) ) {
  94. global $pagenow;
  95. foreach ( $field['data']['options'] as $option_key => $option ) {
  96. // Set value
  97. $_field['options'][$option_key] = array(
  98. 'value' => $option['set_value'],
  99. 'title' => self::translate( 'field ' . $field['id'] . ' option '
  100. . $option_key . ' title', $option['title'] ),
  101. 'name' => 'wpcf[' . $field['id'] . '][' . $option_key . ']',
  102. );
  103. if ( $pagenow == 'post-new.php' && !empty( $option['checked'] ) ) {
  104. $_field['options'][$option_key]['checked'] = true;
  105. }
  106. }
  107. }
  108. break;
  109. case 'date':
  110. $_field['add_time'] = !empty( $field['data']['date_and_time'] ) && $field['data']['date_and_time'] == 'and_time';
  111. break;
  112. // Radio and Select set field [options]
  113. case 'radio':
  114. case 'select':
  115. if ( !empty( $field['data']['options'] ) ) {
  116. foreach ( $field['data']['options'] as $k => $option ) {
  117. if ( $k == 'default' ) {
  118. continue;
  119. }
  120. if ( array_key_exists( 'default', $field['data']['options'] ) && $field['data']['options']['default'] == $k ) {
  121. $_field['default_value'] = $option['value'];
  122. }
  123. $_field['options'][] = array(
  124. 'value' => $option['value'],
  125. 'title' => self::translate( 'field ' . $field['id'] . ' option '
  126. . $k . ' title', $option['title'] ),
  127. );
  128. }
  129. }
  130. break;
  131. }
  132. // Radio adjust type name because form_factory class name contains 'Radios'
  133. if ( $field['type'] == 'radio' ) {
  134. $_field['type'] = 'radios';
  135. }
  136. return $cache[$cache_key] = $_field;
  137. }
  138. /**
  139. * Filters validation.
  140. *
  141. * Loop over validation settings and create array of validation rules.
  142. * array( $rule => array( 'args' => array, 'message' => string ), ... )
  143. *
  144. * @param array|string $field settings array (as stored in DB) or field ID
  145. * @return array array( $rule => array( 'args' => array, 'message' => string ), ... )
  146. */
  147. public static function filterValidation($config)
  148. {
  149. $config = self::getConfig( $config );
  150. if ( is_null( $config ) ) return array();
  151. /* Placeholder for field value '$value'.
  152. *
  153. * Used for validation settings.
  154. * Field value is not processed here, instead string '$value' is used
  155. * to be replaced with actual value when needed.
  156. *
  157. * For example:
  158. * validation['rangelength'] = array(
  159. * 'args' => array( '$value', 5, 12 ),
  160. * 'message' => 'Value length between %s and %s required'
  161. * );
  162. * validation['reqiuired'] = array(
  163. * 'args' => array( '$value', true ),
  164. * 'message' => 'This field is required'
  165. * );
  166. *
  167. * Types have default and custom messages defined on it's side.
  168. */
  169. $value = '$value';
  170. $validation = array();
  171. if ( isset( $config['data']['validate'] ) ) {
  172. foreach ( $config['data']['validate'] as $rule => $settings ) {
  173. if ( $settings['active'] ) {
  174. $validation[$rule] = array(
  175. 'args' => isset( $settings['args'] ) ? array_unshift( $value,
  176. $settings['args'] ) : array($value, true),
  177. 'message' => $settings['message']
  178. );
  179. }
  180. }
  181. }
  182. return $validation;
  183. }
  184. /**
  185. * Filters conditional.
  186. *
  187. * There are two types of conditionals:
  188. * 1. Regular conditionals created using Types GUI
  189. * 2. Custom onditionals (user entered manually)
  190. *
  191. * 1. Regular conditional
  192. *
  193. * Main properties:
  194. * [relation] - AND|OR evaluate as true if all or any condition is TRUE
  195. * [conditions] - array of conditions
  196. * [values] - values to check against (used only by PHP), evaluate method
  197. * should not be aware if it's checking post meta or user meta,
  198. * instead array of needed values (or missing) are passed to method.
  199. * Types use filteres get_post_meta() and get_user_meta().
  200. *
  201. * [conditions]
  202. * Each conditions is array with properties:
  203. * id: ID of trigger field (this field value is checked) to evaluate
  204. * this field as TRUE or FALSE (corresponds to main IDs set here)
  205. * type: type of trigger field. JS and PHP needs at least this information
  206. * when processing condtitional evaluation
  207. * operator: which operation to perform (=|>|<|>=|<=|!=|between)
  208. * args: arguments passed to checking functions
  209. *
  210. * Example of reguar conditional
  211. *
  212. * [conditional] => Array(
  213. [relation] => OR
  214. [conditions] => Array(
  215. [0] => Array(
  216. [id] => wpcf-my-date
  217. [type] => date
  218. [operator] => =
  219. [args] => Array(
  220. [0] => 02/01/2014
  221. )
  222. )
  223. [1] => Array(
  224. [id] => wpcf-my-date
  225. [type] => date
  226. [operator] => between
  227. [args] => Array(
  228. [0] => 02/07/2014
  229. [1] => 02/10/2014
  230. )
  231. )
  232. )
  233. [values] => Array(
  234. [wpcf-my-date] => 32508691200
  235. )
  236. )
  237. *
  238. *
  239. * 2. Custom conditional
  240. * Main properties:
  241. * [custom] - custom statement made by user, note that $xxxx should match
  242. * IDs of fields that passed this filter.
  243. * [values] - same as for regular conditional
  244. *
  245. * [conditional] => Array(
  246. [custom] => ($wpcf-my-date = DATE(01,02,2014)) OR ($wpcf-my-date > DATE(07,02,2014))
  247. [values] => Array(
  248. [wpcf-my-date] => 32508691200
  249. )
  250. )
  251. *
  252. * @param array|string $field settings array (as stored in DB) or field ID
  253. * @param int $post_id Post or user ID to fetch meta data to check against
  254. * @return array
  255. */
  256. public static function filterConditional($field, $post_id)
  257. {
  258. $field = self::getConfig( $field );
  259. if ( is_null( $field ) ) return array();
  260. // Caching
  261. static $cache = array();
  262. $cache_key = md5( serialize( $field ) . $post_id );
  263. if ( isset( $cache[$cache_key] ) ) {
  264. return $cache[$cache_key];
  265. }
  266. /* Suffix - used to construct ID of this field and other fields connected
  267. * to it via conditionals.
  268. *
  269. * @see at bottom 'General field settings'
  270. *
  271. * Reason to use it - Types have child posts forms inside same form as
  272. * main fields. It's like having same sets of fields inside same form.
  273. * Main post fields do not have suffix.
  274. *
  275. * Example main field:
  276. * ID: wpcf-text
  277. * conditional: '$wpcf-date > DATE(01,02,2014)'
  278. *
  279. * Example child field:
  280. * ID: wpcf-text-123
  281. * conditional: '$wpcf-date-123 > DATE(01,02,2014)'
  282. * Suffix is child post ID (wpcf-text-$child_post_id).
  283. *
  284. * That way right triggers and conditional fields are mapped.
  285. */
  286. $suffix = isset( $field['suffix'] ) ? $field['suffix'] : '';
  287. $cond = array();
  288. $cond_values = array();
  289. if ( empty( $field['meta_type'] ) ) {
  290. $field['meta_type'] = 'postmeta';
  291. }
  292. // Get [values]
  293. if ( !empty( $post_id ) ) {
  294. $cond_values = $field['meta_type'] == 'usermeta' ? get_user_meta( $post_id ) : get_post_custom( $post_id );
  295. }
  296. // Unserialize [values] and do not allow array (take first value from array
  297. if ( is_array( $cond_values ) ) {
  298. foreach ( $cond_values as $k => &$v ) {
  299. $v = maybe_unserialize( $v[0] );
  300. $v = self::getStringFromArray($v);
  301. }
  302. }
  303. // Set custom conditional
  304. if ( !empty( $field['data']['conditional_display']['custom_use'] ) ) {
  305. require_once 'class.conditional.php';
  306. $custom = $field['data']['conditional_display']['custom'];
  307. // Extract field values ('${$field_name}')
  308. $c_fields = WPToolset_Forms_Conditional::extractFields( $custom );
  309. $c_values = array();
  310. // Loop over extracted fields and adjust custom statement
  311. foreach ( $c_fields as $c_field_id ) {
  312. // Get field settings
  313. $c_field = self::getConfig( $c_field_id );
  314. // If it's Types field
  315. if ( !empty( $c_field ) ) {
  316. // Adjust custom statement
  317. $custom = preg_replace( '/\$' . $c_field_id . '[\s\)]/',
  318. "\${$c_field['meta_key']}{$suffix} ", $custom );
  319. // Apply filters from field (that is why we set 'type' property)
  320. wptoolset_form_field_add_filters( $c_field['type'] );
  321. $c_key = $c_field['meta_key'];
  322. if ( isset( $cond_values[$c_key] ) ) {
  323. $c_values[$c_key . $suffix] = apply_filters( 'wptoolset_conditional_value_php',
  324. $cond_values[$c_key], $c_field['type'] );
  325. }
  326. // Otherwise do nothing (leave statement as it is and just add [values])
  327. // That allows checking for non-Types field
  328. } elseif ( isset( $cond_values[$c_field_id] ) ) {
  329. $c_values[$c_field_id . $suffix] = $cond_values[$c_field_id];
  330. }
  331. }
  332. // Set conditional setting
  333. $cond = array(
  334. 'custom' => $custom,
  335. 'values' => $c_values,
  336. );
  337. // Regular conditional
  338. } elseif ( !empty( $field['data']['conditional_display']['conditions'] ) ) {
  339. $cond = array(
  340. 'relation' => $field['data']['conditional_display']['relation'],
  341. 'conditions' => array(),
  342. 'values' => array(),
  343. );
  344. // Loop over conditions and collect settings
  345. foreach ( $field['data']['conditional_display']['conditions'] as $d ) {
  346. // Get field settings
  347. $c_field = self::getConfig( $d['field'] );
  348. // If it's Types field
  349. if ( !empty( $c_field ) ) {
  350. $_c = array(
  351. 'id' => self::getPrefix( $c_field ) . $d['field'] . $suffix,
  352. 'type' => $c_field['type'],
  353. 'operator' => $d['operation'],
  354. 'args' => array($d['value']),
  355. );
  356. $cond['conditions'][] = $_c;
  357. // Apply filters from field (that is why we set 'type' property)
  358. wptoolset_form_field_add_filters( $field['type'] );
  359. $key = $c_field['meta_key'];
  360. if ( isset( $cond_values[$key] ) ) {
  361. $cond['values'][$key . $suffix] = apply_filters( 'wptoolset_conditional_value_php',
  362. $cond_values[$key], $c_field['type'] );
  363. }
  364. // Otherwise do nothing add [values]
  365. // That allows checking for non-Types field
  366. // TODO REVIEW THIS
  367. } elseif ( isset( $cond_values[$d['field']] ) ) {
  368. $cond['values'][$d['field'] . $suffix] = $cond_values[$d['field']];
  369. }
  370. }
  371. }
  372. unset( $cond_values, $c_values, $c_field );
  373. return $cache[$cache_key] = $cond;
  374. }
  375. /**
  376. * Checks if field is repetitive.
  377. *
  378. * @param type $field
  379. * @return null|boolean
  380. */
  381. public static function isRepetitive($config)
  382. {
  383. $config = self::getConfig( $config );
  384. if ( is_null( $config ) ) return null;
  385. return !empty( $config['data']['repetitive'] );
  386. }
  387. /**
  388. * Returns all fields configurations created by Types.
  389. *
  390. * @return array Array of field settings
  391. */
  392. public static function getFields()
  393. {
  394. return get_option( 'wpcf-fields', array() );
  395. }
  396. /**
  397. * Get specific field configuration created by Types.
  398. *
  399. * @param type $field_id
  400. * @return array|null
  401. */
  402. public static function getConfig($field_id)
  403. {
  404. if ( !is_string( $field_id ) ) {
  405. return is_array( $field_id ) ? $field_id : null;
  406. }
  407. $fields = self::getFields();
  408. return isset( $fields[$field_id] ) ? $fields[$field_id] : null;
  409. }
  410. /**
  411. * Returns prefix for specific field meta_key or ID.
  412. *
  413. * @param type $field
  414. * @return string
  415. */
  416. public static function getPrefix($config)
  417. {
  418. return !empty( $config['data']['controlled'] ) ? '' : 'wpcf-';
  419. }
  420. /**
  421. * Translates various strings connected to Types using WPML icl_t().
  422. *
  423. * @param type $name
  424. * @param type $string
  425. * @param type $context
  426. * @return string
  427. */
  428. public static function translate($name, $string, $context = 'plugin Types')
  429. {
  430. if ( !function_exists( 'icl_t' ) ) {
  431. return $string;
  432. }
  433. return icl_t( $context, $name, stripslashes( $string ) );
  434. }
  435. /**
  436. * Returns field meta key.
  437. *
  438. * @param type $config
  439. * @return type
  440. */
  441. public static function getMetakey($config)
  442. {
  443. return self::getPrefix( $config ) . $config['id'];
  444. }
  445. /**
  446. * return first string value
  447. *
  448. * @param type $string
  449. * @return string
  450. */
  451. private static function getStringFromArray($array)
  452. {
  453. if ( is_object( $array ) ) {
  454. return $array;
  455. }
  456. if ( is_array( $array ) ) {
  457. return self::getStringFromArray(array_shift($array));
  458. }
  459. return strval( $array );
  460. }
  461. }