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

https://gitlab.com/endomorphosis/reservationtelco · PHP · 201 lines · 114 code · 36 blank · 51 comment · 22 complexity · 75b8b2e8765e9f3aa5bd63c7f9530c25 MD5 · raw file

  1. <?php
  2. /**
  3. * When a posts is saved this class is called for check if exists a write panel with custom fields
  4. * if exists then this processes all the custom fields and save his values in the database
  5. */
  6. class RCCWP_Post {
  7. /**
  8. * This function is called when a post is saves
  9. */
  10. function SaveCustomFields($postId){
  11. global $flag;
  12. if($flag == 0){
  13. //with this the save_post action don't will be execute twice
  14. $flag = 1;
  15. //security
  16. if(!wp_verify_nonce($_REQUEST['rc-custom-write-panel-verify-key'], 'rc-custom-write-panel'))
  17. return $postId;
  18. //the user can edit posts?
  19. if (!current_user_can('edit_post', $postId)){
  20. return $postId;
  21. }
  22. RCCWP_Post::SetCustomWritePanel($postId);
  23. RCCWP_Post::PrepareFieldsValues($postId);
  24. RCCWP_Post::SetMetaValues($postId);
  25. return $postId;
  26. }
  27. }
  28. /**
  29. * Attach a custom write panel to the current post by saving the custom write panel id
  30. * as a meta value for the post
  31. *
  32. * @param integer $postId
  33. */
  34. function SetCustomWritePanel($postId) {
  35. $customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
  36. if (isset($customWritePanelId)) {
  37. if (!empty($customWritePanelId)) {
  38. if (!update_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, $customWritePanelId)) {
  39. add_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, $customWritePanelId);
  40. }
  41. } else {
  42. delete_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY);
  43. }
  44. }
  45. }
  46. /**
  47. * Save all custom field values meta values for the post, this function assumes that
  48. * $_POST['rc_cwp_meta_keys'] contains the names of the fields, while $_POST[{FIELD_NAME}]
  49. * contains the value of the field named {FIELD_NAME}
  50. *
  51. * @param integer $postId
  52. * @return void
  53. */
  54. function SetMetaValues($postId){
  55. global $wpdb;
  56. $customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
  57. //delete file
  58. if(!empty($_POST['magicfields_remove_files'])){
  59. $files = preg_split('/\|\|\|/', $_POST['magicfields_remove_files']);
  60. foreach($files as $file){
  61. @unlink(MF_FILES_PATH.$file);
  62. }
  63. }
  64. if(empty($_POST['magicfields'])){
  65. return true;
  66. }
  67. $customfields = $_POST['magicfields'];
  68. if ( $the_post = wp_is_post_revision($postId))
  69. $postId = $the_post;
  70. if (!empty($customWritePanelId)) {
  71. // --- Delete old values
  72. foreach($customfields as $name => $field){
  73. delete_post_meta($postId,$name);
  74. }
  75. $wpdb->query("DELETE FROM ". MF_TABLE_POST_META .
  76. " WHERE post_id=$postId");
  77. //Creating the new values
  78. //Iterating the custom fields
  79. foreach($customfields as $name => $groups){
  80. $groups_index = 1;
  81. //Iterating the groups
  82. foreach($groups as $group_id => $fields){
  83. $index = 1;
  84. //Iterating the duplicates
  85. foreach($fields as $value){
  86. // Adding field value meta data
  87. add_post_meta($postId, $name, $value);
  88. $fieldMetaID = $wpdb->insert_id;
  89. // Adding the referencie in the magic fields post meta table
  90. $wpdb->query("INSERT INTO ". MF_TABLE_POST_META .
  91. " (id, field_name, group_count, field_count, post_id,order_id) ".
  92. " VALUES ({$fieldMetaID}, '{$name}',{$groups_index},{$index},{$postId},{$groups_index})"
  93. );
  94. $index++;
  95. }
  96. $groups_index++;
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * This function prepares some custom fields before saving it. It reads $_REQUEST and:
  103. * 1. Adds params to photos uploaded (Image field)
  104. * 2. Formats dates (Date Field)
  105. *
  106. * @param integer postId
  107. */
  108. function PrepareFieldsValues($postId) {
  109. global $wpdb;
  110. // Format Dates
  111. if( isset( $_REQUEST['rc_cwp_meta_date'])){
  112. foreach( $_REQUEST['rc_cwp_meta_date'] as $meta_name ) {
  113. $metaDate = strtotime($_POST[$meta_name]);
  114. $formatted_date = date('Y-m-d',$metaDate);
  115. $_POST[$meta_name] = $formatted_date;
  116. }
  117. }
  118. }
  119. /**
  120. * Get a custom write panel by reading $_REQUEST['custom-write-panel-id'] or the
  121. * To see whether $_GET['post'] has a custom write panel associated to it.
  122. *
  123. * @return Custom Write Panel as an object, returns null if there is no write panels.
  124. */
  125. function GetCustomWritePanel()
  126. {
  127. global $wpdb;
  128. if (isset($_GET['post']))
  129. {
  130. $customWritePanelId = get_post_meta((int)$_GET['post'], RC_CWP_POST_WRITE_PANEL_ID_META_KEY, true);
  131. if (empty($customWritePanelId))
  132. {
  133. $customWritePanelId = (int)isset($_REQUEST['custom-write-panel-id']);
  134. }
  135. }
  136. else if (function_exists('icl_t') && isset($_GET['trid']) )
  137. {
  138. $element_id = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type='post' AND trid = ".intval($_GET['trid']));
  139. $customWritePanelId = get_post_meta((int)$element_id, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, true);
  140. if (empty($customWritePanelId))
  141. {
  142. $customWritePanelId = (int)$_REQUEST['custom-write-panel-id'];
  143. }
  144. }
  145. else if (isset($_REQUEST['custom-write-panel-id']))
  146. {
  147. $customWritePanelId = (int)$_REQUEST['custom-write-panel-id'];
  148. }
  149. $customWritePanel = FALSE;
  150. if (isset($customWritePanelId)) {
  151. include_once('RCCWP_Application.php');
  152. $customWritePanel = RCCWP_CustomWritePanel::Get($customWritePanelId);
  153. }
  154. return $customWritePanel;
  155. }
  156. /**
  157. * This Method is Executed when a post is deleted
  158. * @param integer $postId
  159. * @TODO check if is deleted the values in wp_postmeta too
  160. */
  161. function DeletePostMetaData($postId) {
  162. global $wpdb;
  163. $wpdb->query("DELETE FROM " . MF_TABLE_POST_META . " WHERE post_id =" . $postId) ;
  164. }
  165. }