PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/magic-fields/RCCWP_CustomField.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 508 lines | 295 code | 61 blank | 152 comment | 33 complexity | ff876b0204f77f8bc0c6d659bca8304c MD5 | raw file
  1. <?php
  2. /**
  3. * In this Class can be found it the methods for work with CustomFields.
  4. *
  5. * - Create a Custom Field
  6. * - Delete a Field
  7. * - Get a Custom Field
  8. * - Get Info from the types of custom fields
  9. * - Get the postmeta ID with the value of a custom field
  10. * - Get a custom field Value
  11. */
  12. class RCCWP_CustomField {
  13. /**
  14. * Create a new custom field
  15. *
  16. * @param id $customGroupId the id of the group that will contain the field
  17. * @param string $name the name of the field, the name is used to uniquely identify the field
  18. * when retrieving its value.
  19. * @param string $label the label of the field, the label is displayed beside the field
  20. * in Write tab.
  21. * @param integer $order the order of the field when it is displayed in
  22. * the Write tab.
  23. * @param integer $required_field whether this field is a required field. Required fields
  24. * doesn't allow users to save a post if they are null.
  25. *
  26. * @param integer $type the type of the field. Use $FIELD_TYPES defined in RCCWP_Constant.php
  27. * @param array $options array of strings that represent the list of the field if
  28. * its type is list.
  29. * @param array $default_value array of strings that represent default value(s) of
  30. * of the field if its type is list.
  31. * @param array $properties an array containing extra properties of the field.
  32. * @return the new field id
  33. */
  34. function Create($customGroupId, $name, $label, $order = 1, $required_field = 0, $type, $options = null, $default_value = null, $properties = null,$duplicate,$helptext = null,$css = null) {
  35. global $wpdb;
  36. $name = stripslashes(stripslashes($name));
  37. $name = addslashes($name);
  38. $name = str_replace(" ","_",$name);
  39. $label = stripslashes(stripslashes($label));
  40. $label = addslashes($label);
  41. $helptext = stripslashes(stripslashes($helptext));
  42. $helptext = addslashes($helptext);
  43. if(isset($_POST['custom-field-css'])) $css = $_POST['custom-field-css'];
  44. $sql = sprintf(
  45. "INSERT INTO " . MF_TABLE_GROUP_FIELDS .
  46. " (group_id, name, description, display_order, required_field, type, CSS, duplicate,help_text) values (%d, %s, %s, %d, %d, %d, %s, %d, %s)",
  47. $customGroupId,
  48. RC_Format::TextToSql($name),
  49. RC_Format::TextToSql($label),
  50. $order,
  51. $required_field,
  52. $type,
  53. "'".$css."'",
  54. $duplicate,
  55. RC_Format::TextToSql($helptext)
  56. );
  57. $wpdb->query($sql);
  58. $customFieldId = $wpdb->insert_id;
  59. $field_type = RCCWP_CustomField::GetCustomFieldTypes($type);
  60. if ($field_type->has_options == "true"){
  61. if (!is_array($options)) {
  62. $options = stripslashes($options);
  63. $options = explode("\n", $options);
  64. }
  65. array_walk($options, array("RC_Format", "TrimArrayValues"));
  66. $options = addslashes(serialize($options));
  67. if (!is_array($default_value)) {
  68. $default_value = stripslashes($default_value);
  69. $default_value = explode("\n", $default_value);
  70. }
  71. array_walk($default_value, array("RC_Format", "TrimArrayValues"));
  72. $default_value = addslashes(serialize($default_value));
  73. $sql = sprintf(
  74. "INSERT INTO " . MF_TABLE_CUSTOM_FIELD_OPTIONS .
  75. " (custom_field_id, options, default_option) values (%d, %s, %s)",
  76. $customFieldId,
  77. RC_Format::TextToSql($options),
  78. RC_Format::TextToSql($default_value)
  79. );
  80. $wpdb->query($sql);
  81. }
  82. if ($field_type->has_properties == "true"){
  83. $sql = sprintf(
  84. "INSERT INTO " . MF_TABLE_CUSTOM_FIELD_PROPERTIES .
  85. " (custom_field_id, properties) values (%d, %s)",
  86. $customFieldId,
  87. RC_Format::TextToSql(serialize($properties))
  88. );
  89. $wpdb->query($sql);
  90. }
  91. return $customFieldId;
  92. }
  93. /**
  94. * Delete a field
  95. *
  96. * @param integer $customFieldId field id
  97. */
  98. function Delete($customFieldId = null)
  99. {
  100. global $wpdb;
  101. $customField = RCCWP_CustomField::Get($customFieldId);
  102. $sql = sprintf(
  103. "DELETE FROM " . MF_TABLE_GROUP_FIELDS .
  104. " WHERE id = %d",
  105. $customFieldId
  106. );
  107. $wpdb->query($sql);
  108. if ($customField->has_options == "true")
  109. {
  110. $sql = sprintf(
  111. "DELETE FROM " . MF_TABLE_CUSTOM_FIELD_OPTIONS .
  112. " WHERE custom_field_id = %d",
  113. $customFieldId
  114. );
  115. $wpdb->query($sql);
  116. }
  117. }
  118. /**
  119. * Get the field information including properties, options and default value(s)
  120. *
  121. * @param integer $customFieldId field id
  122. * @return an object containing information about fields. The object contains
  123. * 3 objects: properties, options and default_value
  124. */
  125. function Get($customFieldId)
  126. {
  127. global $wpdb;
  128. $sql = "SELECT cf.group_id, cf.id, cf.name, cf.CSS, tt.id AS type_id, tt.name AS type, cf.description, cf.display_order, cf.required_field, co.options, co.default_option AS default_value, tt.has_options, cp.properties, tt.has_properties, tt.allow_multiple_values, duplicate,cf.help_text FROM " . MF_TABLE_GROUP_FIELDS .
  129. " cf LEFT JOIN " . MF_TABLE_CUSTOM_FIELD_OPTIONS . " co ON cf.id = co.custom_field_id" .
  130. " LEFT JOIN " . MF_TABLE_CUSTOM_FIELD_PROPERTIES . " cp ON cf.id = cp.custom_field_id" .
  131. " JOIN " . MF_TABLE_CUSTOM_FIELD_TYPES . " tt ON cf.type = tt.id" .
  132. " WHERE cf.id = " . $customFieldId;
  133. $results = $wpdb->get_row($sql);
  134. $results->options = unserialize($results->options);
  135. $results->properties = unserialize($results->properties);
  136. $results->default_value = unserialize($results->default_value);
  137. return $results;
  138. }
  139. /**
  140. * Retrieves information about a specified type
  141. *
  142. * @param integer $customFieldTypeId the type id, if null, a list of all types will be returned
  143. * @return a list/object containing information about the specified type. The information
  144. * includes id, name, description, has_options, has_properties, and
  145. * allow_multiple_values (whether fields of that type can have more than one default value)
  146. */
  147. function GetCustomFieldTypes($customFieldTypeId = null)
  148. {
  149. global $wpdb;
  150. if (isset($customFieldTypeId))
  151. {
  152. $sql = "SELECT id, name, description, has_options, has_properties, allow_multiple_values FROM " . MF_TABLE_CUSTOM_FIELD_TYPES .
  153. " WHERE id = " . (int)$customFieldTypeId;
  154. $results = $wpdb->get_row($sql);
  155. }
  156. else
  157. {
  158. $sql = "SELECT id, name, description, has_options, has_properties, allow_multiple_values FROM " . MF_TABLE_CUSTOM_FIELD_TYPES;
  159. $results = $wpdb->get_results($sql);
  160. if (!isset($results))
  161. $results = array();
  162. }
  163. return $results;
  164. }
  165. /**
  166. * Get the Meta ID from a custom field with this id is possible get the value of the custom field
  167. * from the Post Meta table of wordpress
  168. *
  169. * @param integer $postId Post id
  170. * @param string $customFieldNamethe name of the custom field
  171. * @param integer $groupIndex the index of the group (if the field is content into a group)
  172. * @param integer $fieldIndex the index of the field
  173. * @return integer Return the id from the postmeta table of wordpress
  174. * who contain the value of the custom field
  175. */
  176. function GetMetaID($postId, $customFieldName, $groupIndex=1, $fieldIndex=1) {
  177. global $wpdb;
  178. // Given $postId, $customFieldName, $groupIndex and $fieldIndex get meta_id
  179. return $wpdb->get_var("SELECT id FROM " . MF_TABLE_POST_META .
  180. " WHERE field_name = '$customFieldName' AND group_count = $groupIndex ".
  181. " AND field_count = $fieldIndex AND post_id = $postId" );
  182. }
  183. /**
  184. * Retrieves the value of a custom field for a specified post
  185. *
  186. * @param boolean $single
  187. * @param integer $postId
  188. * @param string $customFieldName
  189. * @param integer $groupIndex
  190. * @param integer $fieldIndex
  191. * @return mixed
  192. * @TODO review if is still necessary save the "backward compatibility"
  193. */
  194. function GetCustomFieldValues($single, $postId, $customFieldName, $groupIndex=1, $fieldIndex=1) {
  195. global $wpdb;
  196. $customFieldName = str_replace(" ","_",$customFieldName);
  197. $fieldMetaID = RCCWP_CustomField::GetMetaID($postId, $customFieldName, $groupIndex, $fieldIndex);
  198. // for backward compatability, if no associated row was found, use old method
  199. if (!$fieldMetaID){
  200. return get_post_meta($postId, $customFieldName, $single);
  201. }
  202. // Get meta value
  203. $mid = (int) $fieldMetaID;
  204. $meta = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_id = '$mid'" );
  205. if (!$single) return unserialize($meta->meta_value);
  206. return $meta->meta_value;
  207. }
  208. /**
  209. * Get number of group duplicates given field name. The function returns 1
  210. * if there are no duplicates (just the original group), 2 if there is one
  211. * duplicate and so on.
  212. *
  213. * @param integer $postId post id
  214. * @param integer $fieldID the name of any field in the group
  215. * @return number of groups
  216. */
  217. function GetFieldGroupDuplicates($postId, $fieldName){
  218. global $wpdb;
  219. return $wpdb->get_var("SELECT count(DISTINCT group_count) FROM " . MF_TABLE_POST_META .
  220. " WHERE field_name = '$fieldName' AND post_id = $postId");
  221. }
  222. /**
  223. * Get number of group duplicates given field name. The function returns 1
  224. * if there are no duplicates (just he original group), 2 if there is one
  225. * duplicate and so on.
  226. *
  227. * @param integer $postId post id
  228. * @param integer $fieldID the name of any field in the group
  229. * @return number of groups
  230. */
  231. function GetFieldDuplicates($postId, $fieldName, $groupIndex){
  232. global $wpdb;
  233. return $wpdb->get_var("SELECT count(DISTINCT field_count) FROM " . MF_TABLE_POST_META .
  234. " WHERE field_name = '$fieldName' AND post_id = $postId AND group_count = $groupIndex");
  235. }
  236. /**
  237. * Get field duplicates
  238. *
  239. * @param $postId the id of the post
  240. * @param $fieldName the name of the field
  241. * @param $groupId the groupId
  242. * @return array return the order of the field sorted
  243. */
  244. function GetFieldsOrder($postId,$fieldName,$groupId){
  245. global $wpdb;
  246. $tmp = $wpdb->get_col("SELECT field_count FROM ".MF_TABLE_POST_META." WHERE field_name = '{$fieldName}' AND post_id = {$postId} AND group_count = {$groupId} GROUP BY field_count ORDER BY field_count ASC");
  247. // if the array is empty is because this field is new and don't have
  248. // a data related with this post
  249. // then we just create with the index 1
  250. if(empty($tmp)){
  251. $tmp[0] = 1;
  252. }
  253. return $tmp;
  254. }
  255. /**
  256. * Get the order of group duplicates given the field name. The function returns a
  257. * array with the orden
  258. *
  259. * @param integer $postId post id
  260. * @param integer $fieldID the name of any field in the group
  261. * @return order of one group
  262. */
  263. function GetOrderDuplicates($postId,$fieldName){
  264. global $wpdb;
  265. $tmp = $wpdb->get_col("SELECT group_count FROM ".MF_TABLE_POST_META." WHERE field_name = '{$fieldName}' AND post_id = {$postId} GROUP BY group_count ORDER BY order_id asc");
  266. // if the array is empty is because this field is new and don't have
  267. // a data related with this post
  268. // then we just create with the index 1
  269. if(empty($tmp)){
  270. $tmp[0] = 1;
  271. }
  272. // the order start to 1 and the arrays start to 0
  273. // then i just sum one element in each array key for
  274. // the order and the array keys be the same
  275. $order = array();
  276. foreach($tmp as $key => $value){
  277. $order[$key+1] = $value;
  278. }
  279. return $order;
  280. }
  281. /**
  282. * Retrieves the id and type of a custom field given field name for the current post.
  283. *
  284. * @param string $customFieldName
  285. * @return array with custom field id and custom field type
  286. * @author Edgar GarcĂ­a - hunk <ing.edgar@gmail.com>
  287. */
  288. function GetInfoByName($customFieldName,$post_id){
  289. global $wpdb, $FIELD_TYPES;
  290. $customFieldvalues = $wpdb->get_row(
  291. "SELECT cf.id, cf.type,cf.CSS,fp.properties,cf.description
  292. FROM ". MF_TABLE_GROUP_FIELDS . " cf
  293. LEFT JOIN ".MF_TABLE_CUSTOM_FIELD_PROPERTIES." fp ON fp.custom_field_id = cf.id
  294. WHERE cf.name = '$customFieldName'
  295. AND cf.group_id in (
  296. SELECT mg.id
  297. FROM ". MF_TABLE_PANEL_GROUPS . " mg, ".$wpdb->postmeta." pm
  298. WHERE mg.panel_id = pm.meta_value
  299. AND pm.meta_key = '".RC_CWP_POST_WRITE_PANEL_ID_META_KEY."'
  300. AND pm.post_id = $post_id)",ARRAY_A);
  301. if (empty($customFieldvalues))
  302. return false;
  303. if($customFieldvalues['type'] == $FIELD_TYPES["date"] OR $customFieldvalues['type'] == $FIELD_TYPES["image"] )
  304. $customFieldvalues['properties'] = unserialize($customFieldvalues['properties']);
  305. else
  306. $customFieldvalues['properties']=null;
  307. return $customFieldvalues;
  308. }
  309. /**
  310. * Updates the properties of a custom field.
  311. *
  312. * @param integer $customFieldId the id of the field to be updated
  313. * @param string $name the name of the field, the name is used to uniquely identify the field
  314. * when retrieving its value.
  315. * @param string $label the label of the field, the label is displayed beside the field
  316. * in Write tab.
  317. * @param integer $order the order of the field when it is displayed in
  318. * the Write tab.
  319. * @param integer $required_field whether this field is a required field. Required fields
  320. * doesn't allow users to save a post if they are null.
  321. * @param integer $type the type of the field. Use $FIELD_TYPES defined in RCCWP_Constant.php
  322. * @param array $options array of strings that represent the list of the field if
  323. * its type is list.
  324. * @param array $default_value array of strings that represent default value(s) of
  325. * of the field if its type is list.
  326. * @param array $properties an array containing extra properties of the field.
  327. */
  328. function Update($customFieldId, $name, $label, $order = 1, $required_field = 0, $type, $options = null, $default_value = null, $properties = null, $duplicate,$helptext = null) {
  329. global $wpdb;
  330. $name = str_replace(" ","_",$name);
  331. $oldCustomField = RCCWP_CustomField::Get($customFieldId);
  332. if ($oldCustomField->name != $name)
  333. {
  334. $sql = sprintf(
  335. "UPDATE $wpdb->postmeta" .
  336. " SET meta_key = %s" .
  337. " WHERE meta_key = %s",
  338. RC_Format::TextToSql($name),
  339. RC_Format::TextToSql($oldCustomField->name)
  340. );
  341. $wpdb->query($sql);
  342. }
  343. $css = NULL;
  344. if(isset($_POST['custom-field-css'])) $css = $_POST['custom-field-css'];
  345. $sql = sprintf(
  346. "UPDATE " . MF_TABLE_GROUP_FIELDS .
  347. " SET name = %s" .
  348. " , description = %s" .
  349. " , display_order = %d" .
  350. " , required_field = %d" .
  351. " , type = %d" .
  352. " , CSS = '%s'" .
  353. " , duplicate = %d" .
  354. " , help_text = %s" .
  355. " WHERE id = %d",
  356. RC_Format::TextToSql($name),
  357. RC_Format::TextToSql($label),
  358. $order,
  359. $required_field,
  360. $type,
  361. $css,
  362. $duplicate,
  363. RC_Format::TextToSql($helptext),
  364. $customFieldId
  365. );
  366. $wpdb->query($sql);
  367. $field_type = RCCWP_CustomField::GetCustomFieldTypes($type);
  368. if ($field_type->has_options == "true")
  369. {
  370. if (!is_array($options)) {
  371. $options = stripslashes($options);
  372. $options = explode("\n", $options);
  373. }
  374. array_walk($options, array("RC_Format", "TrimArrayValues"));
  375. $options = addslashes(serialize($options));
  376. if (!is_array($default_value)) {
  377. $default_value = stripslashes($default_value);
  378. $default_value = explode("\n", $default_value);
  379. }
  380. array_walk($default_value, array("RC_Format", "TrimArrayValues"));
  381. $default_value = addslashes(serialize($default_value));
  382. $sql = sprintf(
  383. "INSERT INTO " . MF_TABLE_CUSTOM_FIELD_OPTIONS .
  384. " (custom_field_id, options, default_option) values (%d, %s, %s)" .
  385. " ON DUPLICATE KEY UPDATE options = %s, default_option = %s",
  386. $customFieldId,
  387. RC_Format::TextToSql($options),
  388. RC_Format::TextToSql($default_value),
  389. RC_Format::TextToSql($options),
  390. RC_Format::TextToSql($default_value)
  391. );
  392. $wpdb->query($sql);
  393. }
  394. else
  395. {
  396. $sql = sprintf(
  397. "DELETE FROM " . MF_TABLE_CUSTOM_FIELD_OPTIONS .
  398. " WHERE custom_field_id = %d",
  399. $customFieldId
  400. );
  401. $wpdb->query($sql);
  402. }
  403. if ($field_type->has_properties == "true")
  404. {
  405. $sql = sprintf(
  406. "INSERT INTO " . MF_TABLE_CUSTOM_FIELD_PROPERTIES .
  407. " (custom_field_id, properties) values (%d, %s)" .
  408. " ON DUPLICATE KEY UPDATE properties = %s",
  409. $customFieldId,
  410. RC_Format::TextToSql(serialize($properties)),
  411. RC_Format::TextToSql(serialize($properties))
  412. );
  413. $wpdb->query($sql);
  414. }
  415. else
  416. {
  417. $sql = sprintf(
  418. "DELETE FROM " . MF_TABLE_CUSTOM_FIELD_PROPERTIES .
  419. " WHERE custom_field_id = %d",
  420. $customFieldId
  421. );
  422. $wpdb->query($sql);
  423. }
  424. }
  425. /**
  426. * Get Data Field
  427. * @param string $customFieldName
  428. * @param integer $groupIndex
  429. * @param integer $fieldIndex
  430. * @param integer $postId
  431. */
  432. function GetDataField($customFieldName, $groupIndex=1, $fieldIndex=1,$postId){
  433. global $wpdb, $FIELD_TYPES;
  434. $customFieldName = str_replace(" ","_",$customFieldName);
  435. $customFieldvalues = $wpdb->get_row(
  436. "SELECT pm.meta_id,pm.meta_value, cf.id, cf.type,cf.CSS,fp.properties,cf.description
  437. FROM ".MF_TABLE_POST_META." pm_mf, ".$wpdb->postmeta." pm, ".MF_TABLE_GROUP_FIELDS." cf LEFT JOIN ".MF_TABLE_CUSTOM_FIELD_PROPERTIES." fp ON fp.custom_field_id = cf.id
  438. WHERE cf.name = '$customFieldName' AND cf.name = pm_mf.field_name AND group_count = $groupIndex AND field_count = $fieldIndex AND pm_mf.post_id= $postId AND pm_mf.id = pm.meta_id AND cf.group_id in ( SELECT mg.id FROM ".MF_TABLE_PANEL_GROUPS." mg, ".$wpdb->postmeta." pm WHERE mg.panel_id = pm.meta_value AND pm.meta_key = '_mf_write_panel_id' AND pm.post_id = $postId)
  439. ",ARRAY_A);
  440. if (empty($customFieldvalues))
  441. return false;
  442. $customFieldvalues['properties'] = unserialize($customFieldvalues['properties']);
  443. if($customFieldvalues['type'] == $FIELD_TYPES["checkbox_list"] OR $customFieldvalues['type'] == $FIELD_TYPES["listbox"] )
  444. $customFieldvalues['meta_value'] = unserialize($customFieldvalues['meta_value']);
  445. return $customFieldvalues;
  446. }
  447. }