/RCCWP_Post.php

https://github.com/chuyskywalker/Magic-Fields · PHP · 233 lines · 154 code · 41 blank · 38 comment · 29 complexity · 541236c0001bda22c0381b56574aea24 MD5 · raw file

  1. <?php
  2. class RCCWP_Post
  3. {
  4. function SaveCustomFields($postId){
  5. global $flag;
  6. if($flag == 0){
  7. //for this save_post action doesn't execute twice
  8. $flag = 1;
  9. //security
  10. if(!wp_verify_nonce($_REQUEST['rc-custom-write-panel-verify-key'], 'rc-custom-write-panel'))
  11. return $postId;
  12. //the user can edit posts?
  13. if (!current_user_can('edit_post', $postId)){
  14. return $postId;
  15. }
  16. RCCWP_Post::SetCustomWritePanel($postId);
  17. RCCWP_Post::PrepareFieldsValues($postId);
  18. RCCWP_Post::SetMetaValues($postId);
  19. return $postId;
  20. }
  21. }
  22. /*
  23. * Attach a custom write panel to the current post by saving the custom write panel id
  24. * as a meta value for the post
  25. */
  26. function SetCustomWritePanel($postId) {
  27. $customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
  28. if (isset($customWritePanelId))
  29. {
  30. if (!empty($customWritePanelId))
  31. {
  32. if (!update_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, $customWritePanelId))
  33. {
  34. add_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, $customWritePanelId);
  35. }
  36. }
  37. else
  38. {
  39. delete_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY);
  40. }
  41. }
  42. }
  43. /**
  44. * Save all custom field values meta values for the post, this function assumes that
  45. * $_POST['rc_cwp_meta_keys'] contains the names of the fields, while $_POST[{FIELD_NAME}]
  46. * contains the value of the field named {FIELD_NAME}
  47. *
  48. * @param unknown_type $postId
  49. * @return unknown
  50. */
  51. function SetMetaValues($postId){
  52. global $wpdb;
  53. $customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
  54. $customFieldKeys = $_POST['rc_cwp_meta_keys'];
  55. if (!empty($customWritePanelId) && !empty($customFieldKeys) ) {
  56. // --- Delete old values
  57. foreach ($customFieldKeys as $key)
  58. {
  59. if (!empty($key))
  60. {
  61. list($customFieldId, $groupCounter, $fieldCounter, $groupId, $rawCustomFieldName) = split("_", $key, 5);
  62. $customFieldName = $wpdb->escape(stripslashes(trim(RC_Format::GetFieldName($rawCustomFieldName))));
  63. delete_post_meta($postId, $customFieldName);
  64. }
  65. }
  66. if ( $the_post = wp_is_post_revision($postId) )
  67. $postId = $the_post;
  68. $wpdb->query("DELETE FROM ". MF_TABLE_POST_META .
  69. " WHERE post_id=$postId");
  70. $arr = ARRAY();
  71. foreach($customFieldKeys as $key=>$value) {
  72. list($customFieldId, $groupCounter, $fieldCounter, $groupId,$rawCustomFieldName) = split("_", $value, 5);
  73. $arr[$key]->id = $customFieldId ;
  74. $arr[$key]->gc = $groupCounter ;
  75. $arr[$key]->fc = $fieldCounter ;
  76. $arr[$key]->gi = $groupId;
  77. $arr[$key]->fn = $rawCustomFieldName ;
  78. $arr[$key]->ov = $value ;
  79. }
  80. // --- Add new meta data
  81. foreach ($arr as $key)
  82. {
  83. if (!empty($key))
  84. {
  85. //order
  86. if($key->gi == 1){
  87. $order = 1;
  88. }else if (!empty($_POST['order_'.$key->gi.'_'.$key->gc])){
  89. $order = $_POST['order_'.$key->gi.'_'.$key->gc];
  90. }else{
  91. $order = 1;
  92. }
  93. $customFieldValue = $_POST[$key->ov];
  94. $customFieldName = $wpdb->escape(stripslashes(trim(RC_Format::GetFieldName($key->fn))));
  95. // Prepare field value
  96. if (is_array($customFieldValue))
  97. {
  98. $finalValue = array();
  99. foreach ($customFieldValue as $value)
  100. {
  101. $value = stripslashes(trim($value));
  102. array_push($finalValue, $value);
  103. }
  104. }
  105. else
  106. {
  107. $finalValue = stripslashes(trim($customFieldValue));
  108. }
  109. // Add field value meta data
  110. add_post_meta($postId, $customFieldName, $finalValue);
  111. // make sure meta is added to the post, not a revision
  112. if ( $the_post = wp_is_post_revision($postId) )
  113. $postId = $the_post;
  114. $fieldMetaID = $wpdb->insert_id;
  115. // Add field extended properties
  116. $wpdb->query("INSERT INTO ". MF_TABLE_POST_META .
  117. " (id, field_name, group_count, field_count, post_id,order_id) ".
  118. " VALUES ($fieldMetaID, '$customFieldName', ".$key->gc.", ".$key->fc.", $postId,$order)");
  119. }
  120. }
  121. }
  122. }
  123. /**
  124. * This function prepares some custom fields before saving it. It reads $_REQUEST and:
  125. * 1. Adds params to photos uploaded (Image field)
  126. * 2. Formats dates (Date Field)
  127. */
  128. function PrepareFieldsValues($postId) {
  129. global $wpdb;
  130. // Add params to photos
  131. if( isset( $_REQUEST['rc_cwp_meta_photos'])) {
  132. foreach( $_REQUEST['rc_cwp_meta_photos'] as $meta_name ) {
  133. $slashPos = strrpos($_POST[$meta_name], "/");
  134. if (!($slashPos === FALSE))
  135. $_POST[$meta_name] = substr($_POST[$meta_name], $slashPos+1);
  136. //Rename photo if it is edited using editnplace to avoid phpthumb cache
  137. if ($_POST[$meta_name.'_dorename'] == 1){
  138. $oldFilename = $_POST[$meta_name];
  139. $newFilename = time().substr($oldFilename, 10);
  140. rename(MF_UPLOAD_FILES_DIR.$oldFilename, MF_UPLOAD_FILES_DIR.$newFilename);
  141. $_POST[$meta_name] = $newFilename;
  142. }
  143. //if is deleted
  144. if($_POST[$meta_name.'_deleted'] == 1){
  145. $file = $_POST[$meta_name];
  146. //deleting the file
  147. unlink(MF_FILES_PATH.$file);
  148. $_POST[$meta_name] = '';
  149. }
  150. }
  151. }
  152. // Format Dates
  153. if( isset( $_REQUEST['rc_cwp_meta_date'])){
  154. foreach( $_REQUEST['rc_cwp_meta_date'] as $meta_name ) {
  155. $metaDate = strtotime($_POST[$meta_name]);
  156. $formatted_date = date('Y-m-d',$metaDate);
  157. $_POST[$meta_name] = $formatted_date;
  158. }
  159. }
  160. }
  161. /**
  162. * Get a custom write panel by reading $_REQUEST['custom-write-panel-id'] or the
  163. * To see whether $_GET['post'] has a custom write panel associated to it.
  164. *
  165. * @return Custom Write Panel as an object, returns null if there is no write panels.
  166. */
  167. function GetCustomWritePanel()
  168. {
  169. if (isset($_GET['post']))
  170. {
  171. $customWritePanelId = get_post_meta((int)$_GET['post'], RC_CWP_POST_WRITE_PANEL_ID_META_KEY, true);
  172. if (empty($customWritePanelId))
  173. {
  174. $customWritePanelId = (int)$_REQUEST['custom-write-panel-id'];
  175. }
  176. }
  177. else if (isset($_REQUEST['custom-write-panel-id']))
  178. {
  179. $customWritePanelId = (int)$_REQUEST['custom-write-panel-id'];
  180. }
  181. if (isset($customWritePanelId)) {
  182. include_once('RCCWP_Application.php');
  183. $customWritePanel = RCCWP_CustomWritePanel::Get($customWritePanelId);
  184. }
  185. return $customWritePanel;
  186. }
  187. function DeletePostMetaData($postId)
  188. {
  189. global $wpdb;
  190. $wpdb->query("DELETE FROM " . MF_TABLE_POST_META . " WHERE post_id =" . $postId) ;
  191. }
  192. }