PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/APP/wp-admin/includes/post.php

https://bitbucket.org/AFelipeTrujillo/goblog
PHP | 1659 lines | 957 code | 268 blank | 434 comment | 337 complexity | 8e2d522e701c275eb8f291c822143072 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * WordPress Post Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Rename $_POST data from form names to DB post columns.
  10. *
  11. * Manipulates $_POST directly.
  12. *
  13. * @package WordPress
  14. * @since 2.6.0
  15. *
  16. * @param bool $update Are we updating a pre-existing post?
  17. * @param array $post_data Array of post data. Defaults to the contents of $_POST.
  18. * @return object|bool WP_Error on failure, true on success.
  19. */
  20. function _wp_translate_postdata( $update = false, $post_data = null ) {
  21. if ( empty($post_data) )
  22. $post_data = &$_POST;
  23. if ( $update )
  24. $post_data['ID'] = (int) $post_data['post_ID'];
  25. $ptype = get_post_type_object( $post_data['post_type'] );
  26. if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
  27. if ( 'page' == $post_data['post_type'] )
  28. return new WP_Error( 'edit_others_pages', __( 'You are not allowed to edit pages as this user.' ) );
  29. else
  30. return new WP_Error( 'edit_others_posts', __( 'You are not allowed to edit posts as this user.' ) );
  31. } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
  32. if ( 'page' == $post_data['post_type'] )
  33. return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
  34. else
  35. return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
  36. }
  37. if ( isset( $post_data['content'] ) )
  38. $post_data['post_content'] = $post_data['content'];
  39. if ( isset( $post_data['excerpt'] ) )
  40. $post_data['post_excerpt'] = $post_data['excerpt'];
  41. if ( isset( $post_data['parent_id'] ) )
  42. $post_data['post_parent'] = (int) $post_data['parent_id'];
  43. if ( isset($post_data['trackback_url']) )
  44. $post_data['to_ping'] = $post_data['trackback_url'];
  45. $post_data['user_ID'] = get_current_user_id();
  46. if (!empty ( $post_data['post_author_override'] ) ) {
  47. $post_data['post_author'] = (int) $post_data['post_author_override'];
  48. } else {
  49. if (!empty ( $post_data['post_author'] ) ) {
  50. $post_data['post_author'] = (int) $post_data['post_author'];
  51. } else {
  52. $post_data['post_author'] = (int) $post_data['user_ID'];
  53. }
  54. }
  55. if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] )
  56. && ! current_user_can( $ptype->cap->edit_others_posts ) ) {
  57. if ( $update ) {
  58. if ( 'page' == $post_data['post_type'] )
  59. return new WP_Error( 'edit_others_pages', __( 'You are not allowed to edit pages as this user.' ) );
  60. else
  61. return new WP_Error( 'edit_others_posts', __( 'You are not allowed to edit posts as this user.' ) );
  62. } else {
  63. if ( 'page' == $post_data['post_type'] )
  64. return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
  65. else
  66. return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
  67. }
  68. }
  69. if ( ! empty( $post_data['post_status'] ) ) {
  70. $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  71. // No longer an auto-draft
  72. if ( 'auto-draft' === $post_data['post_status'] ) {
  73. $post_data['post_status'] = 'draft';
  74. }
  75. if ( ! get_post_status_object( $post_data['post_status'] ) ) {
  76. unset( $post_data['post_status'] );
  77. }
  78. }
  79. // What to do based on which button they pressed
  80. if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
  81. $post_data['post_status'] = 'draft';
  82. if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
  83. $post_data['post_status'] = 'private';
  84. if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) )
  85. $post_data['post_status'] = 'publish';
  86. if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
  87. $post_data['post_status'] = 'draft';
  88. if ( isset($post_data['pending']) && '' != $post_data['pending'] )
  89. $post_data['post_status'] = 'pending';
  90. if ( isset( $post_data['ID'] ) )
  91. $post_id = $post_data['ID'];
  92. else
  93. $post_id = false;
  94. $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
  95. if ( isset( $post_data['post_status'] ) && 'private' == $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) {
  96. $post_data['post_status'] = $previous_status ? $previous_status : 'pending';
  97. }
  98. $published_statuses = array( 'publish', 'future' );
  99. // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
  100. // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
  101. if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) )
  102. if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) )
  103. $post_data['post_status'] = 'pending';
  104. if ( ! isset( $post_data['post_status'] ) ) {
  105. $post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status;
  106. }
  107. if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) {
  108. unset( $post_data['post_password'] );
  109. }
  110. if (!isset( $post_data['comment_status'] ))
  111. $post_data['comment_status'] = 'closed';
  112. if (!isset( $post_data['ping_status'] ))
  113. $post_data['ping_status'] = 'closed';
  114. foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
  115. if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) {
  116. $post_data['edit_date'] = '1';
  117. break;
  118. }
  119. }
  120. if ( !empty( $post_data['edit_date'] ) ) {
  121. $aa = $post_data['aa'];
  122. $mm = $post_data['mm'];
  123. $jj = $post_data['jj'];
  124. $hh = $post_data['hh'];
  125. $mn = $post_data['mn'];
  126. $ss = $post_data['ss'];
  127. $aa = ($aa <= 0 ) ? date('Y') : $aa;
  128. $mm = ($mm <= 0 ) ? date('n') : $mm;
  129. $jj = ($jj > 31 ) ? 31 : $jj;
  130. $jj = ($jj <= 0 ) ? date('j') : $jj;
  131. $hh = ($hh > 23 ) ? $hh -24 : $hh;
  132. $mn = ($mn > 59 ) ? $mn -60 : $mn;
  133. $ss = ($ss > 59 ) ? $ss -60 : $ss;
  134. $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
  135. $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
  136. if ( !$valid_date ) {
  137. return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
  138. }
  139. $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
  140. }
  141. return $post_data;
  142. }
  143. /**
  144. * Update an existing post with values provided in $_POST.
  145. *
  146. * @since 1.5.0
  147. *
  148. * @param array $post_data Optional.
  149. * @return int Post ID.
  150. */
  151. function edit_post( $post_data = null ) {
  152. if ( empty($post_data) )
  153. $post_data = &$_POST;
  154. // Clear out any data in internal vars.
  155. unset( $post_data['filter'] );
  156. $post_ID = (int) $post_data['post_ID'];
  157. $post = get_post( $post_ID );
  158. $post_data['post_type'] = $post->post_type;
  159. $post_data['post_mime_type'] = $post->post_mime_type;
  160. if ( ! empty( $post_data['post_status'] ) ) {
  161. $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  162. if ( 'inherit' == $post_data['post_status'] ) {
  163. unset( $post_data['post_status'] );
  164. }
  165. }
  166. $ptype = get_post_type_object($post_data['post_type']);
  167. if ( !current_user_can( 'edit_post', $post_ID ) ) {
  168. if ( 'page' == $post_data['post_type'] )
  169. wp_die( __('You are not allowed to edit this page.' ));
  170. else
  171. wp_die( __('You are not allowed to edit this post.' ));
  172. }
  173. if ( post_type_supports( $ptype->name, 'revisions' ) ) {
  174. $revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC', 'posts_per_page' => 1 ) );
  175. $revision = current( $revisions );
  176. // Check if the revisions have been upgraded
  177. if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 )
  178. _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
  179. }
  180. if ( isset($post_data['visibility']) ) {
  181. switch ( $post_data['visibility'] ) {
  182. case 'public' :
  183. $post_data['post_password'] = '';
  184. break;
  185. case 'password' :
  186. unset( $post_data['sticky'] );
  187. break;
  188. case 'private' :
  189. $post_data['post_status'] = 'private';
  190. $post_data['post_password'] = '';
  191. unset( $post_data['sticky'] );
  192. break;
  193. }
  194. }
  195. $post_data = _wp_translate_postdata( true, $post_data );
  196. if ( is_wp_error($post_data) )
  197. wp_die( $post_data->get_error_message() );
  198. // Post Formats
  199. if ( isset( $post_data['post_format'] ) )
  200. set_post_format( $post_ID, $post_data['post_format'] );
  201. $format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
  202. foreach ( $format_meta_urls as $format_meta_url ) {
  203. $keyed = '_format_' . $format_meta_url;
  204. if ( isset( $post_data[ $keyed ] ) )
  205. update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) );
  206. }
  207. $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' );
  208. foreach ( $format_keys as $key ) {
  209. $keyed = '_format_' . $key;
  210. if ( isset( $post_data[ $keyed ] ) ) {
  211. if ( current_user_can( 'unfiltered_html' ) )
  212. update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
  213. else
  214. update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
  215. }
  216. }
  217. if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
  218. $id3data = wp_get_attachment_metadata( $post_ID );
  219. if ( ! is_array( $id3data ) ) {
  220. $id3data = array();
  221. }
  222. foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) {
  223. if ( isset( $post_data[ 'id3_' . $key ] ) ) {
  224. $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
  225. }
  226. }
  227. wp_update_attachment_metadata( $post_ID, $id3data );
  228. }
  229. // Meta Stuff
  230. if ( isset($post_data['meta']) && $post_data['meta'] ) {
  231. foreach ( $post_data['meta'] as $key => $value ) {
  232. if ( !$meta = get_post_meta_by_id( $key ) )
  233. continue;
  234. if ( $meta->post_id != $post_ID )
  235. continue;
  236. if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) )
  237. continue;
  238. update_meta( $key, $value['key'], $value['value'] );
  239. }
  240. }
  241. if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {
  242. foreach ( $post_data['deletemeta'] as $key => $value ) {
  243. if ( !$meta = get_post_meta_by_id( $key ) )
  244. continue;
  245. if ( $meta->post_id != $post_ID )
  246. continue;
  247. if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) )
  248. continue;
  249. delete_meta( $key );
  250. }
  251. }
  252. // Attachment stuff
  253. if ( 'attachment' == $post_data['post_type'] ) {
  254. if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) {
  255. $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
  256. if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) {
  257. $image_alt = wp_strip_all_tags( $image_alt, true );
  258. // update_meta expects slashed
  259. update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
  260. }
  261. }
  262. $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
  263. /** This filter is documented in wp-admin/includes/media.php */
  264. $post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data );
  265. }
  266. add_meta( $post_ID );
  267. update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
  268. wp_update_post( $post_data );
  269. // Now that we have an ID we can fix any attachment anchor hrefs
  270. _fix_attachment_links( $post_ID );
  271. wp_set_post_lock( $post_ID );
  272. if ( current_user_can( $ptype->cap->edit_others_posts ) ) {
  273. if ( ! empty( $post_data['sticky'] ) )
  274. stick_post( $post_ID );
  275. else
  276. unstick_post( $post_ID );
  277. }
  278. return $post_ID;
  279. }
  280. /**
  281. * Process the post data for the bulk editing of posts.
  282. *
  283. * Updates all bulk edited posts/pages, adding (but not removing) tags and
  284. * categories. Skips pages when they would be their own parent or child.
  285. *
  286. * @since 2.7.0
  287. *
  288. * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
  289. * @return array
  290. */
  291. function bulk_edit_posts( $post_data = null ) {
  292. global $wpdb;
  293. if ( empty($post_data) )
  294. $post_data = &$_POST;
  295. if ( isset($post_data['post_type']) )
  296. $ptype = get_post_type_object($post_data['post_type']);
  297. else
  298. $ptype = get_post_type_object('post');
  299. if ( !current_user_can( $ptype->cap->edit_posts ) ) {
  300. if ( 'page' == $ptype->name )
  301. wp_die( __('You are not allowed to edit pages.'));
  302. else
  303. wp_die( __('You are not allowed to edit posts.'));
  304. }
  305. if ( -1 == $post_data['_status'] ) {
  306. $post_data['post_status'] = null;
  307. unset($post_data['post_status']);
  308. } else {
  309. $post_data['post_status'] = $post_data['_status'];
  310. }
  311. unset($post_data['_status']);
  312. if ( ! empty( $post_data['post_status'] ) ) {
  313. $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  314. if ( 'inherit' == $post_data['post_status'] ) {
  315. unset( $post_data['post_status'] );
  316. }
  317. }
  318. $post_IDs = array_map( 'intval', (array) $post_data['post'] );
  319. $reset = array(
  320. 'post_author', 'post_status', 'post_password',
  321. 'post_parent', 'page_template', 'comment_status',
  322. 'ping_status', 'keep_private', 'tax_input',
  323. 'post_category', 'sticky', 'post_format',
  324. );
  325. foreach ( $reset as $field ) {
  326. if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) )
  327. unset($post_data[$field]);
  328. }
  329. if ( isset($post_data['post_category']) ) {
  330. if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) )
  331. $new_cats = array_map( 'absint', $post_data['post_category'] );
  332. else
  333. unset($post_data['post_category']);
  334. }
  335. $tax_input = array();
  336. if ( isset($post_data['tax_input'])) {
  337. foreach ( $post_data['tax_input'] as $tax_name => $terms ) {
  338. if ( empty($terms) )
  339. continue;
  340. if ( is_taxonomy_hierarchical( $tax_name ) ) {
  341. $tax_input[ $tax_name ] = array_map( 'absint', $terms );
  342. } else {
  343. $comma = _x( ',', 'tag delimiter' );
  344. if ( ',' !== $comma )
  345. $terms = str_replace( $comma, ',', $terms );
  346. $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
  347. }
  348. }
  349. }
  350. if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) {
  351. $pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'");
  352. $children = array();
  353. for ( $i = 0; $i < 50 && $parent > 0; $i++ ) {
  354. $children[] = $parent;
  355. foreach ( $pages as $page ) {
  356. if ( $page->ID == $parent ) {
  357. $parent = $page->post_parent;
  358. break;
  359. }
  360. }
  361. }
  362. }
  363. $updated = $skipped = $locked = array();
  364. $shared_post_data = $post_data;
  365. foreach ( $post_IDs as $post_ID ) {
  366. // Start with fresh post data with each iteration.
  367. $post_data = $shared_post_data;
  368. $post_type_object = get_post_type_object( get_post_type( $post_ID ) );
  369. if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( 'edit_post', $post_ID ) ) {
  370. $skipped[] = $post_ID;
  371. continue;
  372. }
  373. if ( wp_check_post_lock( $post_ID ) ) {
  374. $locked[] = $post_ID;
  375. continue;
  376. }
  377. $post = get_post( $post_ID );
  378. $tax_names = get_object_taxonomies( $post );
  379. foreach ( $tax_names as $tax_name ) {
  380. $taxonomy_obj = get_taxonomy($tax_name);
  381. if ( isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) )
  382. $new_terms = $tax_input[$tax_name];
  383. else
  384. $new_terms = array();
  385. if ( $taxonomy_obj->hierarchical )
  386. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') );
  387. else
  388. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') );
  389. $post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms );
  390. }
  391. if ( isset($new_cats) && in_array( 'category', $tax_names ) ) {
  392. $cats = (array) wp_get_post_categories($post_ID);
  393. $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
  394. unset( $post_data['tax_input']['category'] );
  395. }
  396. $post_data['post_type'] = $post->post_type;
  397. $post_data['post_mime_type'] = $post->post_mime_type;
  398. $post_data['guid'] = $post->guid;
  399. foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) {
  400. if ( ! isset( $post_data[ $field ] ) ) {
  401. $post_data[ $field ] = $post->$field;
  402. }
  403. }
  404. $post_data['ID'] = $post_ID;
  405. $post_data['post_ID'] = $post_ID;
  406. $post_data = _wp_translate_postdata( true, $post_data );
  407. if ( is_wp_error( $post_data ) ) {
  408. $skipped[] = $post_ID;
  409. continue;
  410. }
  411. $updated[] = wp_update_post( $post_data );
  412. if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
  413. if ( 'sticky' == $post_data['sticky'] )
  414. stick_post( $post_ID );
  415. else
  416. unstick_post( $post_ID );
  417. }
  418. if ( isset( $post_data['post_format'] ) )
  419. set_post_format( $post_ID, $post_data['post_format'] );
  420. }
  421. return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
  422. }
  423. /**
  424. * Default post information to use when populating the "Write Post" form.
  425. *
  426. * @since 2.0.0
  427. *
  428. * @param string $post_type A post type string, defaults to 'post'.
  429. * @return WP_Post Post object containing all the default post data as attributes
  430. */
  431. function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
  432. global $wpdb;
  433. $post_title = '';
  434. if ( !empty( $_REQUEST['post_title'] ) )
  435. $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ));
  436. $post_content = '';
  437. if ( !empty( $_REQUEST['content'] ) )
  438. $post_content = esc_html( wp_unslash( $_REQUEST['content'] ));
  439. $post_excerpt = '';
  440. if ( !empty( $_REQUEST['excerpt'] ) )
  441. $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ));
  442. if ( $create_in_db ) {
  443. $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
  444. $post = get_post( $post_id );
  445. if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
  446. set_post_format( $post, get_option( 'default_post_format' ) );
  447. } else {
  448. $post = new stdClass;
  449. $post->ID = 0;
  450. $post->post_author = '';
  451. $post->post_date = '';
  452. $post->post_date_gmt = '';
  453. $post->post_password = '';
  454. $post->post_type = $post_type;
  455. $post->post_status = 'draft';
  456. $post->to_ping = '';
  457. $post->pinged = '';
  458. $post->comment_status = get_option( 'default_comment_status' );
  459. $post->ping_status = get_option( 'default_ping_status' );
  460. $post->post_pingback = get_option( 'default_pingback_flag' );
  461. $post->post_category = get_option( 'default_category' );
  462. $post->page_template = 'default';
  463. $post->post_parent = 0;
  464. $post->menu_order = 0;
  465. $post = new WP_Post( $post );
  466. }
  467. /**
  468. * Filter the default post content initially used in the "Write Post" form.
  469. *
  470. * @since 1.5.0
  471. *
  472. * @param string $post_content Default post content.
  473. * @param WP_Post $post Post object.
  474. */
  475. $post->post_content = apply_filters( 'default_content', $post_content, $post );
  476. /**
  477. * Filter the default post title initially used in the "Write Post" form.
  478. *
  479. * @since 1.5.0
  480. *
  481. * @param string $post_title Default post title.
  482. * @param WP_Post $post Post object.
  483. */
  484. $post->post_title = apply_filters( 'default_title', $post_title, $post );
  485. /**
  486. * Filter the default post excerpt initially used in the "Write Post" form.
  487. *
  488. * @since 1.5.0
  489. *
  490. * @param string $post_excerpt Default post excerpt.
  491. * @param WP_Post $post Post object.
  492. */
  493. $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post );
  494. $post->post_name = '';
  495. return $post;
  496. }
  497. /**
  498. * Determine if a post exists based on title, content, and date
  499. *
  500. * @since 2.0.0
  501. *
  502. * @param string $title Post title
  503. * @param string $content Optional post content
  504. * @param string $date Optional post date
  505. * @return int Post ID if post exists, 0 otherwise.
  506. */
  507. function post_exists($title, $content = '', $date = '') {
  508. global $wpdb;
  509. $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
  510. $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
  511. $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
  512. $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
  513. $args = array();
  514. if ( !empty ( $date ) ) {
  515. $query .= ' AND post_date = %s';
  516. $args[] = $post_date;
  517. }
  518. if ( !empty ( $title ) ) {
  519. $query .= ' AND post_title = %s';
  520. $args[] = $post_title;
  521. }
  522. if ( !empty ( $content ) ) {
  523. $query .= 'AND post_content = %s';
  524. $args[] = $post_content;
  525. }
  526. if ( !empty ( $args ) )
  527. return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
  528. return 0;
  529. }
  530. /**
  531. * Creates a new post from the "Write Post" form using $_POST information.
  532. *
  533. * @since 2.1.0
  534. *
  535. * @return unknown
  536. */
  537. function wp_write_post() {
  538. if ( isset($_POST['post_type']) )
  539. $ptype = get_post_type_object($_POST['post_type']);
  540. else
  541. $ptype = get_post_type_object('post');
  542. if ( !current_user_can( $ptype->cap->edit_posts ) ) {
  543. if ( 'page' == $ptype->name )
  544. return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );
  545. else
  546. return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );
  547. }
  548. $_POST['post_mime_type'] = '';
  549. // Clear out any data in internal vars.
  550. unset( $_POST['filter'] );
  551. // Edit don't write if we have a post id.
  552. if ( isset( $_POST['post_ID'] ) )
  553. return edit_post();
  554. if ( isset($_POST['visibility']) ) {
  555. switch ( $_POST['visibility'] ) {
  556. case 'public' :
  557. $_POST['post_password'] = '';
  558. break;
  559. case 'password' :
  560. unset( $_POST['sticky'] );
  561. break;
  562. case 'private' :
  563. $_POST['post_status'] = 'private';
  564. $_POST['post_password'] = '';
  565. unset( $_POST['sticky'] );
  566. break;
  567. }
  568. }
  569. $translated = _wp_translate_postdata( false );
  570. if ( is_wp_error($translated) )
  571. return $translated;
  572. // Create the post.
  573. $post_ID = wp_insert_post( $_POST );
  574. if ( is_wp_error( $post_ID ) )
  575. return $post_ID;
  576. if ( empty($post_ID) )
  577. return 0;
  578. add_meta( $post_ID );
  579. add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
  580. // Now that we have an ID we can fix any attachment anchor hrefs
  581. _fix_attachment_links( $post_ID );
  582. wp_set_post_lock( $post_ID );
  583. return $post_ID;
  584. }
  585. /**
  586. * Calls wp_write_post() and handles the errors.
  587. *
  588. * @since 2.0.0
  589. * @uses wp_write_post()
  590. * @uses is_wp_error()
  591. * @uses wp_die()
  592. * @return unknown
  593. */
  594. function write_post() {
  595. $result = wp_write_post();
  596. if ( is_wp_error( $result ) )
  597. wp_die( $result->get_error_message() );
  598. else
  599. return $result;
  600. }
  601. //
  602. // Post Meta
  603. //
  604. /**
  605. * {@internal Missing Short Description}}
  606. *
  607. * @since 1.2.0
  608. *
  609. * @param unknown_type $post_ID
  610. * @return unknown
  611. */
  612. function add_meta( $post_ID ) {
  613. global $wpdb;
  614. $post_ID = (int) $post_ID;
  615. $metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
  616. $metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
  617. $metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : '';
  618. if ( is_string( $metavalue ) )
  619. $metavalue = trim( $metavalue );
  620. if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) {
  621. // We have a key/value pair. If both the select and the
  622. // input for the key have data, the input takes precedence:
  623. if ( '#NONE#' != $metakeyselect )
  624. $metakey = $metakeyselect;
  625. if ( $metakeyinput )
  626. $metakey = $metakeyinput; // default
  627. if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) )
  628. return false;
  629. $metakey = wp_slash( $metakey );
  630. return add_post_meta( $post_ID, $metakey, $metavalue );
  631. }
  632. return false;
  633. } // add_meta
  634. /**
  635. * {@internal Missing Short Description}}
  636. *
  637. * @since 1.2.0
  638. *
  639. * @param unknown_type $mid
  640. * @return unknown
  641. */
  642. function delete_meta( $mid ) {
  643. return delete_metadata_by_mid( 'post' , $mid );
  644. }
  645. /**
  646. * Get a list of previously defined keys.
  647. *
  648. * @since 1.2.0
  649. *
  650. * @return unknown
  651. */
  652. function get_meta_keys() {
  653. global $wpdb;
  654. $keys = $wpdb->get_col( "
  655. SELECT meta_key
  656. FROM $wpdb->postmeta
  657. GROUP BY meta_key
  658. ORDER BY meta_key" );
  659. return $keys;
  660. }
  661. /**
  662. * {@internal Missing Short Description}}
  663. *
  664. * @since 2.1.0
  665. *
  666. * @param unknown_type $mid
  667. * @return unknown
  668. */
  669. function get_post_meta_by_id( $mid ) {
  670. return get_metadata_by_mid( 'post', $mid );
  671. }
  672. /**
  673. * {@internal Missing Short Description}}
  674. *
  675. * Some postmeta stuff.
  676. *
  677. * @since 1.2.0
  678. *
  679. * @param unknown_type $postid
  680. * @return unknown
  681. */
  682. function has_meta( $postid ) {
  683. global $wpdb;
  684. return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
  685. FROM $wpdb->postmeta WHERE post_id = %d
  686. ORDER BY meta_key,meta_id", $postid), ARRAY_A );
  687. }
  688. /**
  689. * {@internal Missing Short Description}}
  690. *
  691. * @since 1.2.0
  692. *
  693. * @param unknown_type $meta_id
  694. * @param unknown_type $meta_key Expect Slashed
  695. * @param unknown_type $meta_value Expect Slashed
  696. * @return unknown
  697. */
  698. function update_meta( $meta_id, $meta_key, $meta_value ) {
  699. $meta_key = wp_unslash( $meta_key );
  700. $meta_value = wp_unslash( $meta_value );
  701. return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
  702. }
  703. //
  704. // Private
  705. //
  706. /**
  707. * Replace hrefs of attachment anchors with up-to-date permalinks.
  708. *
  709. * @since 2.3.0
  710. * @access private
  711. *
  712. * @param int|object $post Post ID or post object.
  713. * @return void|int|WP_Error Void if nothing fixed. 0 or WP_Error on update failure. The post ID on update success.
  714. */
  715. function _fix_attachment_links( $post ) {
  716. $post = get_post( $post, ARRAY_A );
  717. $content = $post['post_content'];
  718. // Don't run if no pretty permalinks or post is not published, scheduled, or privately published.
  719. if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ) ) )
  720. return;
  721. // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero)
  722. if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) )
  723. return;
  724. $site_url = get_bloginfo('url');
  725. $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
  726. $replace = '';
  727. foreach ( $link_matches[1] as $key => $value ) {
  728. if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-')
  729. || !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
  730. || !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) )
  731. continue;
  732. $quote = $url_match[1]; // the quote (single or double)
  733. $url_id = (int) $url_match[2];
  734. $rel_id = (int) $rel_match[1];
  735. if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false )
  736. continue;
  737. $link = $link_matches[0][$key];
  738. $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
  739. $content = str_replace( $link, $replace, $content );
  740. }
  741. if ( $replace ) {
  742. $post['post_content'] = $content;
  743. // Escape data pulled from DB.
  744. $post = add_magic_quotes($post);
  745. return wp_update_post($post);
  746. }
  747. }
  748. /**
  749. * Get all the possible statuses for a post_type
  750. *
  751. * @since 2.5.0
  752. *
  753. * @param string $type The post_type you want the statuses for
  754. * @return array As array of all the statuses for the supplied post type
  755. */
  756. function get_available_post_statuses($type = 'post') {
  757. $stati = wp_count_posts($type);
  758. return array_keys(get_object_vars($stati));
  759. }
  760. /**
  761. * Run the wp query to fetch the posts for listing on the edit posts page
  762. *
  763. * @since 2.5.0
  764. *
  765. * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
  766. * @return array
  767. */
  768. function wp_edit_posts_query( $q = false ) {
  769. if ( false === $q )
  770. $q = $_GET;
  771. $q['m'] = isset($q['m']) ? (int) $q['m'] : 0;
  772. $q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
  773. $post_stati = get_post_stati();
  774. if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) )
  775. $post_type = $q['post_type'];
  776. else
  777. $post_type = 'post';
  778. $avail_post_stati = get_available_post_statuses($post_type);
  779. if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
  780. $post_status = $q['post_status'];
  781. $perm = 'readable';
  782. }
  783. if ( isset($q['orderby']) )
  784. $orderby = $q['orderby'];
  785. elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) )
  786. $orderby = 'modified';
  787. if ( isset($q['order']) )
  788. $order = $q['order'];
  789. elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
  790. $order = 'ASC';
  791. $per_page = 'edit_' . $post_type . '_per_page';
  792. $posts_per_page = (int) get_user_option( $per_page );
  793. if ( empty( $posts_per_page ) || $posts_per_page < 1 )
  794. $posts_per_page = 20;
  795. /**
  796. * Filter the number of items per page to show for a specific 'per_page' type.
  797. *
  798. * The dynamic hook name, $per_page, refers to a hook name comprised of the post type,
  799. * preceded by 'edit_', and succeeded by '_per_page', e.g. 'edit_$post_type_per_page'.
  800. *
  801. * Some examples of filter hooks generated here include: 'edit_attachment_per_page',
  802. * 'edit_post_per_page', 'edit_page_per_page', etc.
  803. *
  804. * @since 3.0.0
  805. *
  806. * @param int $posts_per_page Number of posts to display per page for the given 'per_page'
  807. * type. Default 20.
  808. */
  809. $posts_per_page = apply_filters( $per_page, $posts_per_page );
  810. /**
  811. * Filter the number of posts displayed per page when specifically listing "posts".
  812. *
  813. * @since 2.8.0
  814. *
  815. * @param int $per_page Number of posts to be displayed. Default 20.
  816. * @param string $post_type The post type.
  817. */
  818. $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
  819. $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
  820. // Hierarchical types require special args.
  821. if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
  822. $query['orderby'] = 'menu_order title';
  823. $query['order'] = 'asc';
  824. $query['posts_per_page'] = -1;
  825. $query['posts_per_archive_page'] = -1;
  826. }
  827. if ( ! empty( $q['show_sticky'] ) )
  828. $query['post__in'] = (array) get_option( 'sticky_posts' );
  829. wp( $query );
  830. return $avail_post_stati;
  831. }
  832. /**
  833. * {@internal Missing Short Description}}
  834. *
  835. * @since 2.5.0
  836. *
  837. * @param unknown_type $type
  838. * @return unknown
  839. */
  840. function get_available_post_mime_types($type = 'attachment') {
  841. global $wpdb;
  842. $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
  843. return $types;
  844. }
  845. /**
  846. * Executes a query for attachments. An array of WP_Query arguments
  847. * can be passed in, which will override the arguments set by this function.
  848. *
  849. * @since 2.5.0
  850. *
  851. * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
  852. * @return array
  853. */
  854. function wp_edit_attachments_query( $q = false ) {
  855. if ( false === $q )
  856. $q = $_GET;
  857. $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0;
  858. $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
  859. $q['post_type'] = 'attachment';
  860. $post_type = get_post_type_object( 'attachment' );
  861. $states = 'inherit';
  862. if ( current_user_can( $post_type->cap->read_private_posts ) )
  863. $states .= ',private';
  864. $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states;
  865. $media_per_page = (int) get_user_option( 'upload_per_page' );
  866. if ( empty( $media_per_page ) || $media_per_page < 1 )
  867. $media_per_page = 20;
  868. /**
  869. * Filter the number of items to list per page when listing media items.
  870. *
  871. * @since 2.9.0
  872. *
  873. * @param int $media_per_page Number of media to list. Default 20.
  874. */
  875. $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
  876. $post_mime_types = get_post_mime_types();
  877. $avail_post_mime_types = get_available_post_mime_types('attachment');
  878. if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
  879. unset($q['post_mime_type']);
  880. if ( isset($q['detached']) )
  881. $q['post_parent'] = 0;
  882. wp( $q );
  883. return array($post_mime_types, $avail_post_mime_types);
  884. }
  885. /**
  886. * Returns the list of classes to be used by a metabox
  887. *
  888. * @uses get_user_option()
  889. * @since 2.5.0
  890. *
  891. * @param unknown_type $id
  892. * @param unknown_type $page
  893. * @return unknown
  894. */
  895. function postbox_classes( $id, $page ) {
  896. if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
  897. $classes = array( '' );
  898. } elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
  899. if ( !is_array( $closed ) ) {
  900. $classes = array( '' );
  901. } else {
  902. $classes = in_array( $id, $closed ) ? array( 'closed' ) : array( '' );
  903. }
  904. } else {
  905. $classes = array( '' );
  906. }
  907. /**
  908. * Filter the postbox classes for a specific screen and screen ID combo.
  909. *
  910. * The dynamic portions of the hook name, $page, and $id, refer to
  911. * the screen, and screen ID, respectively.
  912. *
  913. * @since 3.2.0
  914. *
  915. * @param array $classes An array of postbox classes.
  916. */
  917. $classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
  918. return implode( ' ', $classes );
  919. }
  920. /**
  921. * {@internal Missing Short Description}}
  922. *
  923. * @since 2.5.0
  924. *
  925. * @param int|object $id Post ID or post object.
  926. * @param string $title (optional) Title
  927. * @param string $name (optional) Name
  928. * @return array With two entries of type string
  929. */
  930. function get_sample_permalink($id, $title = null, $name = null) {
  931. $post = get_post( $id );
  932. if ( ! $post )
  933. return array( '', '' );
  934. $ptype = get_post_type_object($post->post_type);
  935. $original_status = $post->post_status;
  936. $original_date = $post->post_date;
  937. $original_name = $post->post_name;
  938. // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
  939. if ( in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
  940. $post->post_status = 'publish';
  941. $post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
  942. }
  943. // If the user wants to set a new name -- override the current one
  944. // Note: if empty name is supplied -- use the title instead, see #6072
  945. if ( !is_null($name) )
  946. $post->post_name = sanitize_title($name ? $name : $title, $post->ID);
  947. $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
  948. $post->filter = 'sample';
  949. $permalink = get_permalink($post, true);
  950. // Replace custom post_type Token with generic pagename token for ease of use.
  951. $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
  952. // Handle page hierarchy
  953. if ( $ptype->hierarchical ) {
  954. $uri = get_page_uri($post);
  955. $uri = untrailingslashit($uri);
  956. $uri = strrev( stristr( strrev( $uri ), '/' ) );
  957. $uri = untrailingslashit($uri);
  958. /** This filter is documented in wp-admin/edit-tag-form.php */
  959. $uri = apply_filters( 'editable_slug', $uri );
  960. if ( !empty($uri) )
  961. $uri .= '/';
  962. $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
  963. }
  964. /** This filter is documented in wp-admin/edit-tag-form.php */
  965. $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name ) );
  966. $post->post_status = $original_status;
  967. $post->post_date = $original_date;
  968. $post->post_name = $original_name;
  969. unset($post->filter);
  970. return $permalink;
  971. }
  972. /**
  973. * Returns the HTML of the sample permalink slug editor.
  974. *
  975. * @since 2.5.0
  976. *
  977. * @param int|object $id Post ID or post object.
  978. * @param string $new_title Optional. New title.
  979. * @param string $new_slug Optional. New slug.
  980. * @return string The HTML of the sample permalink slug editor.
  981. */
  982. function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
  983. $post = get_post( $id );
  984. if ( ! $post )
  985. return '';
  986. list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  987. if ( current_user_can( 'read_post', $post->ID ) ) {
  988. $ptype = get_post_type_object( $post->post_type );
  989. $view_post = $ptype->labels->view_item;
  990. }
  991. if ( 'publish' == get_post_status( $post ) ) {
  992. $title = __('Click to edit this part of the permalink');
  993. } else {
  994. $title = __('Temporary permalink. Click to edit this part.');
  995. }
  996. if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) {
  997. $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n";
  998. if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) )
  999. $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
  1000. if ( isset( $view_post ) )
  1001. $return .= "<span id='view-post-btn'><a href='$permalink' class='button button-small'>$view_post</a></span>\n";
  1002. /**
  1003. * Filter the sample permalink HTML markup.
  1004. *
  1005. * @since 2.9.0
  1006. *
  1007. * @param string $return Sample permalink HTML markup.
  1008. * @param int|WP_Post $id Post object or ID.
  1009. * @param string $new_title New sample permalink title.
  1010. * @param string $new_slug New sample permalink slug.
  1011. */
  1012. $return = apply_filters( 'get_sample_permalink_html', $return, $id, $new_title, $new_slug );
  1013. return $return;
  1014. }
  1015. if ( function_exists('mb_strlen') ) {
  1016. if ( mb_strlen($post_name) > 30 ) {
  1017. $post_name_abridged = mb_substr($post_name, 0, 14). '&hellip;' . mb_substr($post_name, -14);
  1018. } else {
  1019. $post_name_abridged = $post_name;
  1020. }
  1021. } else {
  1022. if ( strlen($post_name) > 30 ) {
  1023. $post_name_abridged = substr($post_name, 0, 14). '&hellip;' . substr($post_name, -14);
  1024. } else {
  1025. $post_name_abridged = $post_name;
  1026. }
  1027. }
  1028. $post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
  1029. $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
  1030. $view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
  1031. $return = '<strong>' . __('Permalink:') . "</strong>\n";
  1032. $return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n";
  1033. $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
  1034. $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button button-small hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n";
  1035. $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
  1036. if ( isset( $view_post ) ) {
  1037. $return .= "<span id='view-post-btn'><a href='" . get_permalink( $post ) . "' class='button button-small'>$view_post</a></span>\n";
  1038. }
  1039. /** This filter is documented in wp-admin/includes/post.php */
  1040. $return = apply_filters( 'get_sample_permalink_html', $return, $id, $new_title, $new_slug );
  1041. return $return;
  1042. }
  1043. /**
  1044. * Output HTML for the post thumbnail meta-box.
  1045. *
  1046. * @since 2.9.0
  1047. *
  1048. * @param int $thumbnail_id ID of the attachment used for thumbnail
  1049. * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
  1050. * @return string html
  1051. */
  1052. function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
  1053. global $content_width, $_wp_additional_image_sizes;
  1054. $post = get_post( $post );
  1055. $upload_iframe_src = esc_url( get_upload_iframe_src('image', $post->ID ) );
  1056. $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>';
  1057. $content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) );
  1058. if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
  1059. $old_content_width = $content_width;
  1060. $content_width = 266;
  1061. if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
  1062. $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
  1063. else
  1064. $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
  1065. if ( !empty( $thumbnail_html ) ) {
  1066. $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
  1067. $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
  1068. $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>';
  1069. }
  1070. $content_width = $old_content_width;
  1071. }
  1072. /**
  1073. * Filter the admin post thumbnail HTML markup to return.
  1074. *
  1075. * @since 2.9.0
  1076. *
  1077. * @param string $content Admin post thumbnail HTML markup.
  1078. * @param int $post_id Post ID.
  1079. */
  1080. return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID );
  1081. }
  1082. /**
  1083. * Check to see if the post is currently being edited by another user.
  1084. *
  1085. * @since 2.5.0
  1086. *
  1087. * @param int $post_id ID of the post to check for editing
  1088. * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
  1089. */
  1090. function wp_check_post_lock( $post_id ) {
  1091. if ( !$post = get_post( $post_id ) )
  1092. return false;
  1093. if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) )
  1094. return false;
  1095. $lock = explode( ':', $lock );
  1096. $time = $lock[0];
  1097. $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  1098. /** This filter is documented in wp-admin/includes/ajax-actions.php */
  1099. $time_window = apply_filters( 'wp_check_post_lock_window', 150 );
  1100. if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
  1101. return $user;
  1102. return false;
  1103. }
  1104. /**
  1105. * Mark the post as currently being edited by the current user
  1106. *
  1107. * @since 2.5.0
  1108. *
  1109. * @param int $post_id ID of the post to being edited
  1110. * @return bool|array Returns false if the post doesn't exist of there is no current user, or
  1111. * an array of the lock time and the user ID.
  1112. */
  1113. function wp_set_post_lock( $post_id ) {
  1114. if ( !$post = get_post( $post_id ) )
  1115. return false;
  1116. if ( 0 == ($user_id = get_current_user_id()) )
  1117. return false;
  1118. $now = time();
  1119. $lock = "$now:$user_id";
  1120. update_post_meta( $post->ID, '_edit_lock', $lock );
  1121. return array( $now, $user_id );
  1122. }
  1123. /**
  1124. * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
  1125. *
  1126. * @since 2.8.5
  1127. * @return none
  1128. */
  1129. function _admin_notice_post_locked() {
  1130. if ( ! $post = get_post() )
  1131. return;
  1132. $user = null;
  1133. if ( $user_id = wp_check_post_lock( $post->ID ) )
  1134. $user = get_userdata( $user_id );
  1135. if ( $user ) {
  1136. /**
  1137. * Filter whether to show the post locked dialog.
  1138. *
  1139. * Returning a falsey value to the filter will short-circuit displaying the dialog.
  1140. *
  1141. * @since 3.6.0
  1142. *
  1143. * @param bool $display Whether to display the dialog. Default true.
  1144. * @param WP_User|bool $user WP_User object on success, false otherwise.
  1145. */
  1146. if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) )
  1147. return;
  1148. $locked = true;
  1149. } else {
  1150. $locked = false;
  1151. }
  1152. if ( $locked && ( $sendback = wp_get_referer() ) &&
  1153. false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) {
  1154. $sendback_text = __('Go back');
  1155. } else {
  1156. $sendback = admin_url( 'edit.php' );
  1157. if ( 'post' != $post->post_type )
  1158. $sendback = add_query_arg( 'post_type', $post->post_type, $sendback );
  1159. $sendback_text = get_post_type_object( $post->post_type )->labels->all_items;
  1160. }
  1161. $hidden = $locked ? '' : ' hidden';
  1162. ?>
  1163. <div id="post-lock-dialog" class="notification-dialog-wrap<?php echo $hidden; ?>">
  1164. <div class="notification-dialog-background"></div>
  1165. <div class="notification-dialog">
  1166. <?php
  1167. if ( $locked ) {
  1168. if ( get_post_type_object( $post->post_type )->public ) {
  1169. $preview_link = set_url_scheme( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) );
  1170. if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
  1171. // Latest content is in autosave
  1172. $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
  1173. $preview_link = add_query_arg( array( 'preview_id' => $post->ID, 'preview_nonce' => $nonce ), $preview_link );
  1174. }
  1175. } else {
  1176. $preview_link = '';
  1177. }
  1178. /** This filter is documented in wp-admin/includes/meta-boxes.php */
  1179. $preview_link = apply_filters( 'preview_post_link', $preview_link );
  1180. /**
  1181. * Filter whether to allow the post lock to be overridden.
  1182. *
  1183. * Returning a falsey value to the filter will disable the ability
  1184. * to override the post lock.
  1185. *
  1186. * @since 3.6.0
  1187. *
  1188. * @param bool $override Whether to allow overriding post locks. Default true.
  1189. * @param WP_Post $post Post object.
  1190. * @param WP_User $user User object.
  1191. */
  1192. $override = apply_filters( 'override_post_lock', true, $post, $user );
  1193. $tab_last = $override ? '' : ' wp-tab-last';
  1194. ?>
  1195. <div class="post-locked-message">
  1196. <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
  1197. <p class="currently-editing wp-tab-first" tabindex="0">
  1198. <?php
  1199. _e( 'This content is currently locked.' );
  1200. if ( $override )
  1201. printf( ' ' . __( 'If you take over, %s will be blocked from continuing to edit.' ), esc_html( $user->display_name ) );
  1202. ?>
  1203. </p>
  1204. <?php
  1205. /**
  1206. * Fires inside the post locked dialog before the buttons are displayed.
  1207. *
  1208. * @since 3.6.0
  1209. *
  1210. * @param WP_Post $post Post object.
  1211. */
  1212. do_action( 'post_locked_dialog', $post );
  1213. ?>
  1214. <p>
  1215. <a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a>
  1216. <?php if ( $preview_link ) { ?>
  1217. <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e('Preview'); ?></a>
  1218. <?php
  1219. }
  1220. // Allow plugins to prevent some users overriding the post lock
  1221. if ( $override ) {
  1222. ?>
  1223. <a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', get_edit_post_link( $post->ID, 'url' ) ) ); ?>"><?php _e('Take over'); ?></a>
  1224. <?php
  1225. }
  1226. ?>
  1227. </p>
  1228. </div>
  1229. <?php
  1230. } else {
  1231. ?>
  1232. <div class="post-taken-over">
  1233. <div class="post-locked-avatar"></div>
  1234. <p class="wp-tab-first" tabindex="0">
  1235. <span class="currently-editing"></span><br>
  1236. <span class="locked-saving hidden"><img src="images/wpspin_light-2x.gif" width="16" height="16" /> <?php _e('Saving revision...'); ?></span>
  1237. <span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span>
  1238. </p>
  1239. <?php
  1240. /**
  1241. * Fires inside the dialog displayed when a user has lost the post lock.
  1242. *
  1243. * @since 3.6.0
  1244. *
  1245. * @param WP_Post $post Post object.
  1246. */
  1247. do_action( 'post_lock_lost_dialog', $post );
  1248. ?>
  1249. <p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a></p>
  1250. </div>
  1251. <?php
  1252. }
  1253. ?>
  1254. </div>
  1255. </div>
  1256. <?php
  1257. }
  1258. /**
  1259. * Creates autosave data for the specified post from $_POST data.
  1260. *
  1261. * @package WordPress
  1262. * @subpackage Post_Revisions
  1263. * @since 2.6.0
  1264. *
  1265. * @uses _wp_translate_postdata()
  1266. * @uses _wp_post_revision_fields()
  1267. *
  1268. * @param mixed $post_data Associative array containing the post data or int post ID.
  1269. * @return mixed The autosave revision ID. WP_Error or 0 on error.
  1270. */
  1271. function wp_create_post_autosave( $post_data ) {
  1272. if ( is_numeric( $post_data ) ) {
  1273. $post_id = $post_data;
  1274. $post_data = &$_POST;
  1275. } else {
  1276. $post_id = (int) $post_data['post_ID'];
  1277. }
  1278. $post_data = _wp_translate_postdata( true, $post_data );
  1279. if ( is_wp_error( $post_data ) )
  1280. return $post_data;
  1281. $post_author = get_current_user_id();
  1282. // Store one autosave per author. If there is already an autosave, overwrite it.
  1283. if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
  1284. $new_autosave = _wp_post_revision_fields( $post_data, true );
  1285. $new_autosave['ID'] = $old_autosave->ID;
  1286. $new_autosave['post_author'] = $post_author;
  1287. // If the new autosave has the same content as the post, delete the autosave.
  1288. $post = get_post( $post_id );
  1289. $autosave_is_different = false;
  1290. foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields() ) ) as $field ) {
  1291. if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
  1292. $autosave_is_different = true;
  1293. break;
  1294. }
  1295. }
  1296. if ( ! $autosave_is_different ) {
  1297. wp_delete_post_revision( $old_autosave->ID );
  1298. return 0;
  1299. }
  1300. return wp_update_post( $new_autosave );
  1301. }
  1302. // _wp_put_post_revision() expects unescaped.
  1303. $post_data = wp_unslash( $post_data );
  1304. // Otherwise create the new autosave as a special post revision
  1305. return _wp_put_post_revision( $post_data, true );
  1306. }
  1307. /**
  1308. * Save draft or manually autosave for showing preview.
  1309. *
  1310. * @package WordPress
  1311. * @since 2.7.0
  1312. *
  1313. * @uses get_post_status()
  1314. * @uses edit_post()
  1315. * @uses get_post()
  1316. * @uses current_user_can()
  1317. * @uses wp_die()
  1318. * @uses wp_create_post_autosave()
  1319. * @uses add_query_arg()
  1320. * @uses wp_create_nonce()
  1321. *
  1322. * @return str URL to redirect to show the preview
  1323. */
  1324. function post_preview() {
  1325. $post_ID = (int) $_POST['post_ID'];
  1326. $_POST['ID'] = $post_ID;
  1327. if ( ! $post = get_post( $post_ID ) ) {
  1328. wp_die( __( 'You are not allowed to edit this post.' ) );
  1329. }
  1330. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1331. wp_die( __( 'You are not allowed to edit this post.' ) );
  1332. }
  1333. $is_autosave = false;
  1334. if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) {
  1335. $saved_post_id = edit_post();
  1336. } else {
  1337. $is_autosave = true;
  1338. if ( 'auto-draft' == $_POST['post_status'] )
  1339. $_POST['post_status'] = 'dra…

Large files files are truncated, but you can click here to view the full file