_wp_translate_postdata

Rename $_POST data from form names to DB post columns.

Description

(object|bool) _wp_translate_postdata( (constant) $update = false, (null) $post_data = null ); 

Manipulates $_POST directly.

Returns (object|bool)

WP_Error on failure, true on success.

Parameters (2)

0. $update — Optional. (constant) => false
Are we updating a pre-existing post?
1. $post_data — Optional. (null) => null
Array of post data. Defaults to the contents of $_POST.

Usage

  1. if ( !function_exists( '_wp_translate_postdata' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // Are we updating a pre-existing post? 
  5. $update = false; 
  6.  
  7. // Array of post data. Defaults to the contents of $_POST. 
  8. $post_data = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _wp_translate_postdata($update, $post_data); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/post.php  
  1. function _wp_translate_postdata( $update = false, $post_data = null ) { 
  2.  
  3. if ( empty($post_data) ) 
  4. $post_data = &$_POST; 
  5.  
  6. if ( $update ) 
  7. $post_data['ID'] = (int) $post_data['post_ID']; 
  8.  
  9. $ptype = get_post_type_object( $post_data['post_type'] ); 
  10.  
  11. if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) { 
  12. if ( 'page' == $post_data['post_type'] ) 
  13. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); 
  14. else 
  15. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); 
  16. } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) { 
  17. if ( 'page' == $post_data['post_type'] ) 
  18. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); 
  19. else 
  20. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); 
  21.  
  22. if ( isset( $post_data['content'] ) ) 
  23. $post_data['post_content'] = $post_data['content']; 
  24.  
  25. if ( isset( $post_data['excerpt'] ) ) 
  26. $post_data['post_excerpt'] = $post_data['excerpt']; 
  27.  
  28. if ( isset( $post_data['parent_id'] ) ) 
  29. $post_data['post_parent'] = (int) $post_data['parent_id']; 
  30.  
  31. if ( isset($post_data['trackback_url']) ) 
  32. $post_data['to_ping'] = $post_data['trackback_url']; 
  33.  
  34. $post_data['user_ID'] = get_current_user_id(); 
  35.  
  36. if (!empty ( $post_data['post_author_override'] ) ) { 
  37. $post_data['post_author'] = (int) $post_data['post_author_override']; 
  38. } else { 
  39. if (!empty ( $post_data['post_author'] ) ) { 
  40. $post_data['post_author'] = (int) $post_data['post_author']; 
  41. } else { 
  42. $post_data['post_author'] = (int) $post_data['user_ID']; 
  43.  
  44. if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] ) 
  45. && ! current_user_can( $ptype->cap->edit_others_posts ) ) { 
  46. if ( $update ) { 
  47. if ( 'page' == $post_data['post_type'] ) 
  48. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); 
  49. else 
  50. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); 
  51. } else { 
  52. if ( 'page' == $post_data['post_type'] ) 
  53. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); 
  54. else 
  55. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); 
  56.  
  57. if ( ! empty( $post_data['post_status'] ) ) { 
  58. $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); 
  59.  
  60. // No longer an auto-draft 
  61. if ( 'auto-draft' === $post_data['post_status'] ) { 
  62. $post_data['post_status'] = 'draft'; 
  63.  
  64. if ( ! get_post_status_object( $post_data['post_status'] ) ) { 
  65. unset( $post_data['post_status'] ); 
  66.  
  67. // What to do based on which button they pressed 
  68. if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] ) 
  69. $post_data['post_status'] = 'draft'; 
  70. if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] ) 
  71. $post_data['post_status'] = 'private'; 
  72. if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) ) 
  73. $post_data['post_status'] = 'publish'; 
  74. if ( isset($post_data['advanced']) && '' != $post_data['advanced'] ) 
  75. $post_data['post_status'] = 'draft'; 
  76. if ( isset($post_data['pending']) && '' != $post_data['pending'] ) 
  77. $post_data['post_status'] = 'pending'; 
  78.  
  79. if ( isset( $post_data['ID'] ) ) 
  80. $post_id = $post_data['ID']; 
  81. else 
  82. $post_id = false; 
  83. $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false; 
  84.  
  85. if ( isset( $post_data['post_status'] ) && 'private' == $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) { 
  86. $post_data['post_status'] = $previous_status ? $previous_status : 'pending'; 
  87.  
  88. $published_statuses = array( 'publish', 'future' ); 
  89.  
  90. // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published. 
  91. // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts. 
  92. if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) ) 
  93. if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) ) 
  94. $post_data['post_status'] = 'pending'; 
  95.  
  96. if ( ! isset( $post_data['post_status'] ) ) { 
  97. $post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status; 
  98.  
  99. if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) { 
  100. unset( $post_data['post_password'] ); 
  101.  
  102. if (!isset( $post_data['comment_status'] )) 
  103. $post_data['comment_status'] = 'closed'; 
  104.  
  105. if (!isset( $post_data['ping_status'] )) 
  106. $post_data['ping_status'] = 'closed'; 
  107.  
  108. foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) { 
  109. if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) { 
  110. $post_data['edit_date'] = '1'; 
  111. break; 
  112.  
  113. if ( !empty( $post_data['edit_date'] ) ) { 
  114. $aa = $post_data['aa']; 
  115. $mm = $post_data['mm']; 
  116. $jj = $post_data['jj']; 
  117. $hh = $post_data['hh']; 
  118. $mn = $post_data['mn']; 
  119. $ss = $post_data['ss']; 
  120. $aa = ($aa <= 0 ) ? date('Y') : $aa; 
  121. $mm = ($mm <= 0 ) ? date('n') : $mm; 
  122. $jj = ($jj > 31 ) ? 31 : $jj; 
  123. $jj = ($jj <= 0 ) ? date('j') : $jj; 
  124. $hh = ($hh > 23 ) ? $hh -24 : $hh; 
  125. $mn = ($mn > 59 ) ? $mn -60 : $mn; 
  126. $ss = ($ss > 59 ) ? $ss -60 : $ss; 
  127. $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss ); 
  128. $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] ); 
  129. if ( !$valid_date ) { 
  130. return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); 
  131. $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] ); 
  132.  
  133. if ( isset( $post_data['post_category'] ) ) { 
  134. $category_object = get_taxonomy( 'category' ); 
  135. if ( ! current_user_can( $category_object->cap->assign_terms ) ) { 
  136. unset( $post_data['post_category'] ); 
  137.  
  138. return $post_data;