/wp-includes/post.php

  1. <?php 
  2. /** 
  3. * Core Post API 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Post 
  7. */ 
  8.  
  9. // 
  10. // Post Type Registration 
  11. // 
  12.   
  13. /** 
  14. * Creates the initial post types when 'init' action is fired. 
  15. * 
  16. * See {@see 'init'}. 
  17. * 
  18. * @since 2.9.0 
  19. */ 
  20. function create_initial_post_types() { 
  21. register_post_type( 'post', array( 
  22. 'labels' => array( 
  23. 'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),  
  24. ),  
  25. 'public' => true,  
  26. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  27. '_edit_link' => 'post.php?post=%d', /** internal use only. don't use this when registering your own post type. */ 
  28. 'capability_type' => 'post',  
  29. 'map_meta_cap' => true,  
  30. 'menu_position' => 5,  
  31. 'hierarchical' => false,  
  32. 'rewrite' => false,  
  33. 'query_var' => false,  
  34. 'delete_with_user' => true,  
  35. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),  
  36. ) ); 
  37.  
  38. register_post_type( 'page', array( 
  39. 'labels' => array( 
  40. 'name_admin_bar' => _x( 'Page', 'add new on admin bar' ),  
  41. ),  
  42. 'public' => true,  
  43. 'publicly_queryable' => false,  
  44. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  45. '_edit_link' => 'post.php?post=%d', /** internal use only. don't use this when registering your own post type. */ 
  46. 'capability_type' => 'page',  
  47. 'map_meta_cap' => true,  
  48. 'menu_position' => 20,  
  49. 'hierarchical' => true,  
  50. 'rewrite' => false,  
  51. 'query_var' => false,  
  52. 'delete_with_user' => true,  
  53. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),  
  54. ) ); 
  55.  
  56. register_post_type( 'attachment', array( 
  57. 'labels' => array( 
  58. 'name' => _x('Media', 'post type general name'),  
  59. 'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),  
  60. 'add_new' => _x( 'Add New', 'add new media' ),  
  61. 'edit_item' => __( 'Edit Media' ),  
  62. 'view_item' => __( 'View Attachment Page' ),  
  63. ),  
  64. 'public' => true,  
  65. 'show_ui' => true,  
  66. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  67. '_edit_link' => 'post.php?post=%d', /** internal use only. don't use this when registering your own post type. */ 
  68. 'capability_type' => 'post',  
  69. 'capabilities' => array( 
  70. 'create_posts' => 'upload_files',  
  71. ),  
  72. 'map_meta_cap' => true,  
  73. 'hierarchical' => false,  
  74. 'rewrite' => false,  
  75. 'query_var' => false,  
  76. 'show_in_nav_menus' => false,  
  77. 'delete_with_user' => true,  
  78. 'supports' => array( 'title', 'author', 'comments' ),  
  79. ) ); 
  80. add_post_type_support( 'attachment:audio', 'thumbnail' ); 
  81. add_post_type_support( 'attachment:video', 'thumbnail' ); 
  82.  
  83. register_post_type( 'revision', array( 
  84. 'labels' => array( 
  85. 'name' => __( 'Revisions' ),  
  86. 'singular_name' => __( 'Revision' ),  
  87. ),  
  88. 'public' => false,  
  89. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  90. '_edit_link' => 'revision.php?revision=%d', /** internal use only. don't use this when registering your own post type. */ 
  91. 'capability_type' => 'post',  
  92. 'map_meta_cap' => true,  
  93. 'hierarchical' => false,  
  94. 'rewrite' => false,  
  95. 'query_var' => false,  
  96. 'can_export' => false,  
  97. 'delete_with_user' => true,  
  98. 'supports' => array( 'author' ),  
  99. ) ); 
  100.  
  101. register_post_type( 'nav_menu_item', array( 
  102. 'labels' => array( 
  103. 'name' => __( 'Navigation Menu Items' ),  
  104. 'singular_name' => __( 'Navigation Menu Item' ),  
  105. ),  
  106. 'public' => false,  
  107. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  108. 'hierarchical' => false,  
  109. 'rewrite' => false,  
  110. 'delete_with_user' => false,  
  111. 'query_var' => false,  
  112. ) ); 
  113.  
  114. register_post_status( 'publish', array( 
  115. 'label' => _x( 'Published', 'post status' ),  
  116. 'public' => true,  
  117. '_builtin' => true, /** internal use only. */ 
  118. 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),  
  119. ) ); 
  120.  
  121. register_post_status( 'future', array( 
  122. 'label' => _x( 'Scheduled', 'post status' ),  
  123. 'protected' => true,  
  124. '_builtin' => true, /** internal use only. */ 
  125. 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),  
  126. ) ); 
  127.  
  128. register_post_status( 'draft', array( 
  129. 'label' => _x( 'Draft', 'post status' ),  
  130. 'protected' => true,  
  131. '_builtin' => true, /** internal use only. */ 
  132. 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),  
  133. ) ); 
  134.  
  135. register_post_status( 'pending', array( 
  136. 'label' => _x( 'Pending', 'post status' ),  
  137. 'protected' => true,  
  138. '_builtin' => true, /** internal use only. */ 
  139. 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),  
  140. ) ); 
  141.  
  142. register_post_status( 'private', array( 
  143. 'label' => _x( 'Private', 'post status' ),  
  144. 'private' => true,  
  145. '_builtin' => true, /** internal use only. */ 
  146. 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),  
  147. ) ); 
  148.  
  149. register_post_status( 'trash', array( 
  150. 'label' => _x( 'Trash', 'post status' ),  
  151. 'internal' => true,  
  152. '_builtin' => true, /** internal use only. */ 
  153. 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),  
  154. 'show_in_admin_status_list' => true,  
  155. ) ); 
  156.  
  157. register_post_status( 'auto-draft', array( 
  158. 'label' => 'auto-draft',  
  159. 'internal' => true,  
  160. '_builtin' => true, /** internal use only. */ 
  161. ) ); 
  162.  
  163. register_post_status( 'inherit', array( 
  164. 'label' => 'inherit',  
  165. 'internal' => true,  
  166. '_builtin' => true, /** internal use only. */ 
  167. 'exclude_from_search' => false,  
  168. ) ); 
  169.  
  170. /** 
  171. * Retrieve attached file path based on attachment ID. 
  172. * 
  173. * By default the path will go through the 'get_attached_file' filter, but 
  174. * passing a true to the $unfiltered argument of get_attached_file() will 
  175. * return the file path unfiltered. 
  176. * 
  177. * The function works by getting the single post meta name, named 
  178. * '_wp_attached_file' and returning it. This is a convenience function to 
  179. * prevent looking up the meta name and provide a mechanism for sending the 
  180. * attached filename through a filter. 
  181. * 
  182. * @since 2.0.0 
  183. * 
  184. * @param int $attachment_id Attachment ID. 
  185. * @param bool $unfiltered Optional. Whether to apply filters. Default false. 
  186. * @return string|false The file path to where the attached file should be, false otherwise. 
  187. */ 
  188. function get_attached_file( $attachment_id, $unfiltered = false ) { 
  189. $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); 
  190.  
  191. // If the file is relative, prepend upload dir. 
  192. if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) && ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) ) { 
  193. $file = $uploads['basedir'] . "/$file"; 
  194.  
  195. if ( $unfiltered ) { 
  196. return $file; 
  197.  
  198. /** 
  199. * Filters the attached file based on the given ID. 
  200. * 
  201. * @since 2.1.0 
  202. * 
  203. * @param string $file Path to attached file. 
  204. * @param int $attachment_id Attachment ID. 
  205. */ 
  206. return apply_filters( 'get_attached_file', $file, $attachment_id ); 
  207.  
  208. /** 
  209. * Update attachment file path based on attachment ID. 
  210. * 
  211. * Used to update the file path of the attachment, which uses post meta name 
  212. * '_wp_attached_file' to store the path of the attachment. 
  213. * 
  214. * @since 2.1.0 
  215. * 
  216. * @param int $attachment_id Attachment ID. 
  217. * @param string $file File path for the attachment. 
  218. * @return bool True on success, false on failure. 
  219. */ 
  220. function update_attached_file( $attachment_id, $file ) { 
  221. if ( !get_post( $attachment_id ) ) 
  222. return false; 
  223.  
  224. /** 
  225. * Filters the path to the attached file to update. 
  226. * 
  227. * @since 2.1.0 
  228. * 
  229. * @param string $file Path to the attached file to update. 
  230. * @param int $attachment_id Attachment ID. 
  231. */ 
  232. $file = apply_filters( 'update_attached_file', $file, $attachment_id ); 
  233.  
  234. if ( $file = _wp_relative_upload_path( $file ) ) 
  235. return update_post_meta( $attachment_id, '_wp_attached_file', $file ); 
  236. else 
  237. return delete_post_meta( $attachment_id, '_wp_attached_file' ); 
  238.  
  239. /** 
  240. * Return relative path to an uploaded file. 
  241. * 
  242. * The path is relative to the current upload dir. 
  243. * 
  244. * @since 2.9.0 
  245. * 
  246. * @param string $path Full path to the file. 
  247. * @return string Relative path on success, unchanged path on failure. 
  248. */ 
  249. function _wp_relative_upload_path( $path ) { 
  250. $new_path = $path; 
  251.  
  252. $uploads = wp_get_upload_dir(); 
  253. if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) { 
  254. $new_path = str_replace( $uploads['basedir'], '', $new_path ); 
  255. $new_path = ltrim( $new_path, '/' ); 
  256.  
  257. /** 
  258. * Filters the relative path to an uploaded file. 
  259. * 
  260. * @since 2.9.0 
  261. * 
  262. * @param string $new_path Relative path to the file. 
  263. * @param string $path Full path to the file. 
  264. */ 
  265. return apply_filters( '_wp_relative_upload_path', $new_path, $path ); 
  266.  
  267. /** 
  268. * Retrieve all children of the post parent ID. 
  269. * 
  270. * Normally, without any enhancements, the children would apply to pages. In the 
  271. * context of the inner workings of WordPress, pages, posts, and attachments 
  272. * share the same table, so therefore the functionality could apply to any one 
  273. * of them. It is then noted that while this function does not work on posts, it 
  274. * does not mean that it won't work on posts. It is recommended that you know 
  275. * what context you wish to retrieve the children of. 
  276. * 
  277. * Attachments may also be made the child of a post, so if that is an accurate 
  278. * statement (which needs to be verified), it would then be possible to get 
  279. * all of the attachments for a post. Attachments have since changed since 
  280. * version 2.5, so this is most likely inaccurate, but serves generally as an 
  281. * example of what is possible. 
  282. * 
  283. * The arguments listed as defaults are for this function and also of the 
  284. * get_posts() function. The arguments are combined with the get_children defaults 
  285. * and are then passed to the get_posts() function, which accepts additional arguments. 
  286. * You can replace the defaults in this function, listed below and the additional 
  287. * arguments listed in the get_posts() function. 
  288. * 
  289. * The 'post_parent' is the most important argument and important attention 
  290. * needs to be paid to the $args parameter. If you pass either an object or an 
  291. * integer (number), then just the 'post_parent' is grabbed and everything else 
  292. * is lost. If you don't specify any arguments, then it is assumed that you are 
  293. * in The Loop and the post parent will be grabbed for from the current post. 
  294. * 
  295. * The 'post_parent' argument is the ID to get the children. The 'numberposts' 
  296. * is the amount of posts to retrieve that has a default of '-1', which is 
  297. * used to get all of the posts. Giving a number higher than 0 will only 
  298. * retrieve that amount of posts. 
  299. * 
  300. * The 'post_type' and 'post_status' arguments can be used to choose what 
  301. * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress 
  302. * post types are 'post', 'pages', and 'attachments'. The 'post_status' 
  303. * argument will accept any post status within the write administration panels. 
  304. * 
  305. * @since 2.0.0 
  306. * 
  307. * @see get_posts() 
  308. * @todo Check validity of description. 
  309. * 
  310. * @global WP_Post $post 
  311. * 
  312. * @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. 
  313. * @param string $output Optional. Constant for return type. Accepts OBJECT, ARRAY_A, ARRAY_N. 
  314. * Default OBJECT. 
  315. * @return array Array of children, where the type of each element is determined by $output parameter. 
  316. * Empty array on failure. 
  317. */ 
  318. function get_children( $args = '', $output = OBJECT ) { 
  319. $kids = array(); 
  320. if ( empty( $args ) ) { 
  321. if ( isset( $GLOBALS['post'] ) ) { 
  322. $args = array('post_parent' => (int) $GLOBALS['post']->post_parent ); 
  323. } else { 
  324. return $kids; 
  325. } elseif ( is_object( $args ) ) { 
  326. $args = array('post_parent' => (int) $args->post_parent ); 
  327. } elseif ( is_numeric( $args ) ) { 
  328. $args = array('post_parent' => (int) $args); 
  329.  
  330. $defaults = array( 
  331. 'numberposts' => -1, 'post_type' => 'any',  
  332. 'post_status' => 'any', 'post_parent' => 0,  
  333. ); 
  334.  
  335. $r = wp_parse_args( $args, $defaults ); 
  336.  
  337. $children = get_posts( $r ); 
  338.  
  339. if ( ! $children ) 
  340. return $kids; 
  341.  
  342. if ( ! empty( $r['fields'] ) ) 
  343. return $children; 
  344.  
  345. update_post_cache($children); 
  346.  
  347. foreach ( $children as $key => $child ) 
  348. $kids[$child->ID] = $children[$key]; 
  349.  
  350. if ( $output == OBJECT ) { 
  351. return $kids; 
  352. } elseif ( $output == ARRAY_A ) { 
  353. $weeuns = array(); 
  354. foreach ( (array) $kids as $kid ) { 
  355. $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]); 
  356. return $weeuns; 
  357. } elseif ( $output == ARRAY_N ) { 
  358. $babes = array(); 
  359. foreach ( (array) $kids as $kid ) { 
  360. $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID])); 
  361. return $babes; 
  362. } else { 
  363. return $kids; 
  364.  
  365. /** 
  366. * Get extended entry info (<!--more-->). 
  367. * 
  368. * There should not be any space after the second dash and before the word 
  369. * 'more'. There can be text or space(s) after the word 'more', but won't be 
  370. * referenced. 
  371. * 
  372. * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before 
  373. * the `<!--more-->`. The 'extended' key has the content after the 
  374. * `<!--more-->` comment. The 'more_text' key has the custom "Read More" text. 
  375. * 
  376. * @since 1.0.0 
  377. * 
  378. * @param string $post Post content. 
  379. * @return array Post before ('main'), after ('extended'), and custom read more ('more_text'). 
  380. */ 
  381. function get_extended( $post ) { 
  382. //Match the new style more links. 
  383. if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) { 
  384. list($main, $extended) = explode($matches[0], $post, 2); 
  385. $more_text = $matches[1]; 
  386. } else { 
  387. $main = $post; 
  388. $extended = ''; 
  389. $more_text = ''; 
  390.  
  391. // leading and trailing whitespace. 
  392. $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main); 
  393. $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended); 
  394. $more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text); 
  395.  
  396. return array( 'main' => $main, 'extended' => $extended, 'more_text' => $more_text ); 
  397.  
  398. /** 
  399. * Retrieves post data given a post ID or post object. 
  400. * 
  401. * See sanitize_post() for optional $filter values. Also, the parameter 
  402. * `$post`, must be given as a variable, since it is passed by reference. 
  403. * 
  404. * @since 1.5.1 
  405. * 
  406. * @global WP_Post $post 
  407. * 
  408. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 
  409. * @param string $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N. 
  410. * Default OBJECT. 
  411. * @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',  
  412. * or 'display'. Default 'raw'. 
  413. * @return WP_Post|array|null Type corresponding to $output on success or null on failure. 
  414. * When $output is OBJECT, a `WP_Post` instance is returned. 
  415. */ 
  416. function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { 
  417. if ( empty( $post ) && isset( $GLOBALS['post'] ) ) 
  418. $post = $GLOBALS['post']; 
  419.  
  420. if ( $post instanceof WP_Post ) { 
  421. $_post = $post; 
  422. } elseif ( is_object( $post ) ) { 
  423. if ( empty( $post->filter ) ) { 
  424. $_post = sanitize_post( $post, 'raw' ); 
  425. $_post = new WP_Post( $_post ); 
  426. } elseif ( 'raw' == $post->filter ) { 
  427. $_post = new WP_Post( $post ); 
  428. } else { 
  429. $_post = WP_Post::get_instance( $post->ID ); 
  430. } else { 
  431. $_post = WP_Post::get_instance( $post ); 
  432.  
  433. if ( ! $_post ) 
  434. return null; 
  435.  
  436. $_post = $_post->filter( $filter ); 
  437.  
  438. if ( $output == ARRAY_A ) 
  439. return $_post->to_array(); 
  440. elseif ( $output == ARRAY_N ) 
  441. return array_values( $_post->to_array() ); 
  442.  
  443. return $_post; 
  444.  
  445. /** 
  446. * Retrieve ancestors of a post. 
  447. * 
  448. * @since 2.5.0 
  449. * 
  450. * @param int|WP_Post $post Post ID or post object. 
  451. * @return array Ancestor IDs or empty array if none are found. 
  452. */ 
  453. function get_post_ancestors( $post ) { 
  454. $post = get_post( $post ); 
  455.  
  456. if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) 
  457. return array(); 
  458.  
  459. $ancestors = array(); 
  460.  
  461. $id = $ancestors[] = $post->post_parent; 
  462.  
  463. while ( $ancestor = get_post( $id ) ) { 
  464. // Loop detection: If the ancestor has been seen before, break. 
  465. if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) ) 
  466. break; 
  467.  
  468. $id = $ancestors[] = $ancestor->post_parent; 
  469.  
  470. return $ancestors; 
  471.  
  472. /** 
  473. * Retrieve data from a post field based on Post ID. 
  474. * 
  475. * Examples of the post field will be, 'post_type', 'post_status', 'post_content',  
  476. * etc and based off of the post object property or key names. 
  477. * 
  478. * The context values are based off of the taxonomy filter functions and 
  479. * supported values are found within those functions. 
  480. * 
  481. * @since 2.3.0 
  482. * @since 4.5.0 The `$post` parameter was made optional. 
  483. * 
  484. * @see sanitize_post_field() 
  485. * 
  486. * @param string $field Post field name. 
  487. * @param int|WP_Post $post Optional. Post ID or post object. Defaults to current post. 
  488. * @param string $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',  
  489. * or 'display'. Default 'display'. 
  490. * @return string The value of the post field on success, empty string on failure. 
  491. */ 
  492. function get_post_field( $field, $post = null, $context = 'display' ) { 
  493. $post = get_post( $post ); 
  494.  
  495. if ( !$post ) 
  496. return ''; 
  497.  
  498. if ( !isset($post->$field) ) 
  499. return ''; 
  500.  
  501. return sanitize_post_field($field, $post->$field, $post->ID, $context); 
  502.  
  503. /** 
  504. * Retrieve the mime type of an attachment based on the ID. 
  505. * 
  506. * This function can be used with any post type, but it makes more sense with 
  507. * attachments. 
  508. * 
  509. * @since 2.0.0 
  510. * 
  511. * @param int|WP_Post $ID Optional. Post ID or post object. Default empty. 
  512. * @return string|false The mime type on success, false on failure. 
  513. */ 
  514. function get_post_mime_type( $ID = '' ) { 
  515. $post = get_post($ID); 
  516.  
  517. if ( is_object($post) ) 
  518. return $post->post_mime_type; 
  519.  
  520. return false; 
  521.  
  522. /** 
  523. * Retrieve the post status based on the Post ID. 
  524. * 
  525. * If the post ID is of an attachment, then the parent post status will be given 
  526. * instead. 
  527. * 
  528. * @since 2.0.0 
  529. * 
  530. * @param int|WP_Post $ID Optional. Post ID or post object. Default empty. 
  531. * @return string|false Post status on success, false on failure. 
  532. */ 
  533. function get_post_status( $ID = '' ) { 
  534. $post = get_post($ID); 
  535.  
  536. if ( !is_object($post) ) 
  537. return false; 
  538.  
  539. if ( 'attachment' == $post->post_type ) { 
  540. if ( 'private' == $post->post_status ) 
  541. return 'private'; 
  542.  
  543. // Unattached attachments are assumed to be published. 
  544. if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) ) 
  545. return 'publish'; 
  546.  
  547. // Inherit status from the parent. 
  548. if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) { 
  549. $parent_post_status = get_post_status( $post->post_parent ); 
  550. if ( 'trash' == $parent_post_status ) { 
  551. return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true ); 
  552. } else { 
  553. return $parent_post_status; 
  554.  
  555.  
  556. /** 
  557. * Filters the post status. 
  558. * 
  559. * @since 4.4.0 
  560. * 
  561. * @param string $post_status The post status. 
  562. * @param WP_Post $post The post object. 
  563. */ 
  564. return apply_filters( 'get_post_status', $post->post_status, $post ); 
  565.  
  566. /** 
  567. * Retrieve all of the WordPress supported post statuses. 
  568. * 
  569. * Posts have a limited set of valid status values, this provides the 
  570. * post_status values and descriptions. 
  571. * 
  572. * @since 2.5.0 
  573. * 
  574. * @return array List of post statuses. 
  575. */ 
  576. function get_post_statuses() { 
  577. $status = array( 
  578. 'draft' => __( 'Draft' ),  
  579. 'pending' => __( 'Pending Review' ),  
  580. 'private' => __( 'Private' ),  
  581. 'publish' => __( 'Published' ) 
  582. ); 
  583.  
  584. return $status; 
  585.  
  586. /** 
  587. * Retrieve all of the WordPress support page statuses. 
  588. * 
  589. * Pages have a limited set of valid status values, this provides the 
  590. * post_status values and descriptions. 
  591. * 
  592. * @since 2.5.0 
  593. * 
  594. * @return array List of page statuses. 
  595. */ 
  596. function get_page_statuses() { 
  597. $status = array( 
  598. 'draft' => __( 'Draft' ),  
  599. 'private' => __( 'Private' ),  
  600. 'publish' => __( 'Published' ) 
  601. ); 
  602.  
  603. return $status; 
  604.  
  605. /** 
  606. * Register a post status. Do not use before init. 
  607. * 
  608. * A simple function for creating or modifying a post status based on the 
  609. * parameters given. The function will accept an array (second optional 
  610. * parameter), along with a string for the post status name. 
  611. * 
  612. * Arguments prefixed with an _underscore shouldn't be used by plugins and themes. 
  613. * 
  614. * @since 3.0.0 
  615. * @global array $wp_post_statuses Inserts new post status object into the list 
  616. * 
  617. * @param string $post_status Name of the post status. 
  618. * @param array|string $args { 
  619. * Optional. Array or string of post status arguments. 
  620. * 
  621. * @type bool|string $label A descriptive name for the post status marked 
  622. * for translation. Defaults to value of $post_status. 
  623. * @type bool|array $label_count Descriptive text to use for nooped plurals. 
  624. * Default array of $label, twice 
  625. * @type bool $exclude_from_search Whether to exclude posts with this post status 
  626. * from search results. Default is value of $internal. 
  627. * @type bool $_builtin Whether the status is built-in. Core-use only. 
  628. * Default false. 
  629. * @type bool $public Whether posts of this status should be shown 
  630. * in the front end of the site. Default false. 
  631. * @type bool $internal Whether the status is for internal use only. 
  632. * Default false. 
  633. * @type bool $protected Whether posts with this status should be protected. 
  634. * Default false. 
  635. * @type bool $private Whether posts with this status should be private. 
  636. * Default false. 
  637. * @type bool $publicly_queryable Whether posts with this status should be publicly- 
  638. * queryable. Default is value of $public. 
  639. * @type bool $show_in_admin_all_list Whether to include posts in the edit listing for 
  640. * their post type. Default is value of $internal. 
  641. * @type bool $show_in_admin_status_list Show in the list of statuses with post counts at 
  642. * the top of the edit listings,  
  643. * e.g. All (12) | Published (9) | My Custom Status (2) 
  644. * Default is value of $internal. 
  645. * } 
  646. * @return object 
  647. */ 
  648. function register_post_status( $post_status, $args = array() ) { 
  649. global $wp_post_statuses; 
  650.  
  651. if (!is_array($wp_post_statuses)) 
  652. $wp_post_statuses = array(); 
  653.  
  654. // Args prefixed with an underscore are reserved for internal use. 
  655. $defaults = array( 
  656. 'label' => false,  
  657. 'label_count' => false,  
  658. 'exclude_from_search' => null,  
  659. '_builtin' => false,  
  660. 'public' => null,  
  661. 'internal' => null,  
  662. 'protected' => null,  
  663. 'private' => null,  
  664. 'publicly_queryable' => null,  
  665. 'show_in_admin_status_list' => null,  
  666. 'show_in_admin_all_list' => null,  
  667. ); 
  668. $args = wp_parse_args($args, $defaults); 
  669. $args = (object) $args; 
  670.  
  671. $post_status = sanitize_key($post_status); 
  672. $args->name = $post_status; 
  673.  
  674. // Set various defaults. 
  675. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private ) 
  676. $args->internal = true; 
  677.  
  678. if ( null === $args->public ) 
  679. $args->public = false; 
  680.  
  681. if ( null === $args->private ) 
  682. $args->private = false; 
  683.  
  684. if ( null === $args->protected ) 
  685. $args->protected = false; 
  686.  
  687. if ( null === $args->internal ) 
  688. $args->internal = false; 
  689.  
  690. if ( null === $args->publicly_queryable ) 
  691. $args->publicly_queryable = $args->public; 
  692.  
  693. if ( null === $args->exclude_from_search ) 
  694. $args->exclude_from_search = $args->internal; 
  695.  
  696. if ( null === $args->show_in_admin_all_list ) 
  697. $args->show_in_admin_all_list = !$args->internal; 
  698.  
  699. if ( null === $args->show_in_admin_status_list ) 
  700. $args->show_in_admin_status_list = !$args->internal; 
  701.  
  702. if ( false === $args->label ) 
  703. $args->label = $post_status; 
  704.  
  705. if ( false === $args->label_count ) 
  706. $args->label_count = array( $args->label, $args->label ); 
  707.  
  708. $wp_post_statuses[$post_status] = $args; 
  709.  
  710. return $args; 
  711.  
  712. /** 
  713. * Retrieve a post status object by name. 
  714. * 
  715. * @since 3.0.0 
  716. * 
  717. * @global array $wp_post_statuses List of post statuses. 
  718. * 
  719. * @see register_post_status() 
  720. * 
  721. * @param string $post_status The name of a registered post status. 
  722. * @return object|null A post status object. 
  723. */ 
  724. function get_post_status_object( $post_status ) { 
  725. global $wp_post_statuses; 
  726.  
  727. if ( empty($wp_post_statuses[$post_status]) ) 
  728. return null; 
  729.  
  730. return $wp_post_statuses[$post_status]; 
  731.  
  732. /** 
  733. * Get a list of post statuses. 
  734. * 
  735. * @since 3.0.0 
  736. * 
  737. * @global array $wp_post_statuses List of post statuses. 
  738. * 
  739. * @see register_post_status() 
  740. * 
  741. * @param array|string $args Optional. Array or string of post status arguments to compare against 
  742. * properties of the global `$wp_post_statuses objects`. Default empty array. 
  743. * @param string $output Optional. The type of output to return, either 'names' or 'objects'. Default 'names'. 
  744. * @param string $operator Optional. The logical operation to perform. 'or' means only one element 
  745. * from the array needs to match; 'and' means all elements must match. 
  746. * Default 'and'. 
  747. * @return array A list of post status names or objects. 
  748. */ 
  749. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) { 
  750. global $wp_post_statuses; 
  751.  
  752. $field = ('names' == $output) ? 'name' : false; 
  753.  
  754. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field); 
  755.  
  756. /** 
  757. * Whether the post type is hierarchical. 
  758. * 
  759. * A false return value might also mean that the post type does not exist. 
  760. * 
  761. * @since 3.0.0 
  762. * 
  763. * @see get_post_type_object() 
  764. * 
  765. * @param string $post_type Post type name 
  766. * @return bool Whether post type is hierarchical. 
  767. */ 
  768. function is_post_type_hierarchical( $post_type ) { 
  769. if ( ! post_type_exists( $post_type ) ) 
  770. return false; 
  771.  
  772. $post_type = get_post_type_object( $post_type ); 
  773. return $post_type->hierarchical; 
  774.  
  775. /** 
  776. * Check if a post type is registered. 
  777. * 
  778. * @since 3.0.0 
  779. * 
  780. * @see get_post_type_object() 
  781. * 
  782. * @param string $post_type Post type name. 
  783. * @return bool Whether post type is registered. 
  784. */ 
  785. function post_type_exists( $post_type ) { 
  786. return (bool) get_post_type_object( $post_type ); 
  787.  
  788. /** 
  789. * Retrieves the post type of the current post or of a given post. 
  790. * 
  791. * @since 2.1.0 
  792. * 
  793. * @param int|WP_Post|null $post Optional. Post ID or post object. Default is global $post. 
  794. * @return string|false Post type on success, false on failure. 
  795. */ 
  796. function get_post_type( $post = null ) { 
  797. if ( $post = get_post( $post ) ) 
  798. return $post->post_type; 
  799.  
  800. return false; 
  801.  
  802. /** 
  803. * Retrieves a post type object by name. 
  804. * 
  805. * @since 3.0.0 
  806. * @since 4.6.0 Object returned is now an instance of WP_Post_Type. 
  807. * 
  808. * @global array $wp_post_types List of post types. 
  809. * 
  810. * @see register_post_type() 
  811. * 
  812. * @param string $post_type The name of a registered post type. 
  813. * @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise. 
  814. */ 
  815. function get_post_type_object( $post_type ) { 
  816. global $wp_post_types; 
  817.  
  818. if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) { 
  819. return null; 
  820.  
  821. return $wp_post_types[ $post_type ]; 
  822.  
  823. /** 
  824. * Get a list of all registered post type objects. 
  825. * 
  826. * @since 2.9.0 
  827. * 
  828. * @global array $wp_post_types List of post types. 
  829. * 
  830. * @see register_post_type() for accepted arguments. 
  831. * 
  832. * @param array|string $args Optional. An array of key => value arguments to match against 
  833. * the post type objects. Default empty array. 
  834. * @param string $output Optional. The type of output to return. Accepts post type 'names' 
  835. * or 'objects'. Default 'names'. 
  836. * @param string $operator Optional. The logical operation to perform. 'or' means only one 
  837. * element from the array needs to match; 'and' means all elements 
  838. * must match; 'not' means no elements may match. Default 'and'. 
  839. * @return array A list of post type names or objects. 
  840. */ 
  841. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) { 
  842. global $wp_post_types; 
  843.  
  844. $field = ('names' == $output) ? 'name' : false; 
  845.  
  846. return wp_filter_object_list($wp_post_types, $args, $operator, $field); 
  847.  
  848. /** 
  849. * Registers a post type. 
  850. * 
  851. * Note: Post type registrations should not be hooked before the 
  852. * {@see 'init'} action. Also, any taxonomy connections should be 
  853. * registered via the `$taxonomies` argument to ensure consistency 
  854. * when hooks such as {@see 'parse_query'} or {@see 'pre_get_posts'} 
  855. * are used. 
  856. * 
  857. * Post types can support any number of built-in core features such 
  858. * as meta boxes, custom fields, post thumbnails, post statuses,  
  859. * comments, and more. See the `$supports` argument for a complete 
  860. * list of supported features. 
  861. * 
  862. * @since 2.9.0 
  863. * @since 3.0.0 The `show_ui` argument is now enforced on the new post screen. 
  864. * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing 
  865. * screen and post editing screen. 
  866. * @since 4.6.0 Post type object returned is now an instance of WP_Post_Type. 
  867. * 
  868. * @global array $wp_post_types List of post types. 
  869. * 
  870. * @param string $post_type Post type key. Must not exceed 20 characters and may 
  871. * only contain lowercase alphanumeric characters, dashes,  
  872. * and underscores. See sanitize_key(). 
  873. * @param array|string $args { 
  874. * Array or string of arguments for registering a post type. 
  875. * 
  876. * @type string $label Name of the post type shown in the menu. Usually plural. 
  877. * Default is value of $labels['name']. 
  878. * @type array $labels An array of labels for this post type. If not set, post 
  879. * labels are inherited for non-hierarchical types and page 
  880. * labels for hierarchical ones. See get_post_type_labels() for a full 
  881. * list of supported labels. 
  882. * @type string $description A short descriptive summary of what the post type is. 
  883. * Default empty. 
  884. * @type bool $public Whether a post type is intended for use publicly either via 
  885. * the admin interface or by front-end users. While the default 
  886. * settings of $exclude_from_search, $publicly_queryable, $show_ui,  
  887. * and $show_in_nav_menus are inherited from public, each does not 
  888. * rely on this relationship and controls a very specific intention. 
  889. * Default false. 
  890. * @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false. 
  891. * @type bool $exclude_from_search Whether to exclude posts with this post type from front end search 
  892. * results. Default is the opposite value of $public. 
  893. * @type bool $publicly_queryable Whether queries can be performed on the front end for the post type 
  894. * as part of parse_request(). Endpoints would include: 
  895. * * ?post_type={post_type_key} 
  896. * * ?{post_type_key}={single_post_slug} 
  897. * * ?{post_type_query_var}={single_post_slug} 
  898. * If not set, the default is inherited from $public. 
  899. * @type bool $show_ui Whether to generate and allow a UI for managing this post type in the 
  900. * admin. Default is value of $public. 
  901. * @type bool $show_in_menu Where to show the post type in the admin menu. To work, $show_ui 
  902. * must be true. If true, the post type is shown in its own top level 
  903. * menu. If false, no menu is shown. If a string of an existing top 
  904. * level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post 
  905. * type will be placed as a sub-menu of that. 
  906. * Default is value of $show_ui. 
  907. * @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus. 
  908. * Default is value $public. 
  909. * @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value 
  910. * of $show_in_menu. 
  911. * @type int $menu_position The position in the menu order the post type should appear. To work,  
  912. * $show_in_menu must be true. Default null (at the bottom). 
  913. * @type string $menu_icon The url to the icon to be used for this menu. Pass a base64-encoded 
  914. * SVG using a data URI, which will be colored to match the color scheme 
  915. * -- this should begin with 'data:image/svg+xml;base64, '. Pass the name 
  916. * of a Dashicons helper class to use a font icon, e.g. 
  917. * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty 
  918. * so an icon can be added via CSS. Defaults to use the posts icon. 
  919. * @type string $capability_type The string to use to build the read, edit, and delete capabilities. 
  920. * May be passed as an array to allow for alternative plurals when using 
  921. * this argument as a base to construct the capabilities, e.g. 
  922. * array('story', 'stories'). Default 'post'. 
  923. * @type array $capabilities Array of capabilities for this post type. $capability_type is used 
  924. * as a base to construct capabilities by default. 
  925. * See get_post_type_capabilities(). 
  926. * @type bool $map_meta_cap Whether to use the internal default meta capability handling. 
  927. * Default false. 
  928. * @type array $supports Core feature(s) the post type supports. Serves as an alias for calling 
  929. * add_post_type_support() directly. Core features include 'title',  
  930. * 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt',  
  931. * 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. 
  932. * Additionally, the 'revisions' feature dictates whether the post type 
  933. * will store revisions, and the 'comments' feature dictates whether the 
  934. * comments count will show on the edit screen. Defaults is an array 
  935. * containing 'title' and 'editor'. 
  936. * @type callable $register_meta_box_cb Provide a callback function that sets up the meta boxes for the 
  937. * edit form. Do remove_meta_box() and add_meta_box() calls in the 
  938. * callback. Default null. 
  939. * @type array $taxonomies An array of taxonomy identifiers that will be registered for the 
  940. * post type. Taxonomies can be registered later with register_taxonomy() 
  941. * or register_taxonomy_for_object_type(). 
  942. * Default empty array. 
  943. * @type bool|string $has_archive Whether there should be post type archives, or if a string, the 
  944. * archive slug to use. Will generate the proper rewrite rules if 
  945. * $rewrite is enabled. Default false. 
  946. * @type bool|array $rewrite { 
  947. * Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. 
  948. * Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be 
  949. * passed with any of these keys: 
  950. * 
  951. * @type string $slug Customize the permastruct slug. Defaults to $post_type key. 
  952. * @type bool $with_front Whether the permastruct should be prepended with WP_Rewrite::$front. 
  953. * Default true. 
  954. * @type bool $feeds Whether the feed permastruct should be built for this post type. 
  955. * Default is value of $has_archive. 
  956. * @type bool $pages Whether the permastruct should provide for pagination. Default true. 
  957. * @type const $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set,  
  958. * inherits from $permalink_epmask. If not specified and permalink_epmask 
  959. * is not set, defaults to EP_PERMALINK. 
  960. * } 
  961. * @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type 
  962. * key. If false, a post type cannot be loaded at 
  963. * ?{query_var}={post_slug}. If specified as a string, the query 
  964. * ?{query_var_string}={post_slug} will be valid. 
  965. * @type bool $can_export Whether to allow this post type to be exported. Default true. 
  966. * @type bool $delete_with_user Whether to delete posts of this type when deleting a user. If true,  
  967. * posts of this type belonging to the user will be moved to trash 
  968. * when then user is deleted. If false, posts of this type belonging 
  969. * to the user will *not* be trashed or deleted. If not set (the default),  
  970. * posts are trashed if post_type_supports('author'). Otherwise posts 
  971. * are not trashed or deleted. Default null. 
  972. * @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or 
  973. * "built-in" post_type. Default false. 
  974. * @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of 
  975. * this post type. Default 'post.php?post=%d'. 
  976. * } 
  977. * @return WP_Post_Type|WP_Error The registered post type object, or an error object. 
  978. */ 
  979. function register_post_type( $post_type, $args = array() ) { 
  980. global $wp_post_types; 
  981.  
  982. if ( ! is_array( $wp_post_types ) ) { 
  983. $wp_post_types = array(); 
  984.  
  985. // Sanitize post type name 
  986. $post_type = sanitize_key( $post_type ); 
  987.  
  988. if ( empty( $post_type ) || strlen( $post_type ) > 20 ) { 
  989. _doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' ); 
  990. return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) ); 
  991.  
  992. $post_type_object = new WP_Post_Type( $post_type, $args ); 
  993. $post_type_object->add_supports(); 
  994. $post_type_object->add_rewrite_rules(); 
  995. $post_type_object->register_meta_boxes(); 
  996.  
  997. $wp_post_types[ $post_type ] = $post_type_object; 
  998.  
  999. $post_type_object->add_hooks(); 
  1000. $post_type_object->register_taxonomies(); 
  1001.  
  1002. /** 
  1003. * Fires after a post type is registered. 
  1004. * 
  1005. * @since 3.3.0 
  1006. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object. 
  1007. * 
  1008. * @param string $post_type Post type. 
  1009. * @param WP_Post_Type $post_type_object Arguments used to register the post type. 
  1010. */ 
  1011. do_action( 'registered_post_type', $post_type, $post_type_object ); 
  1012.  
  1013. return $post_type_object; 
  1014.  
  1015. /** 
  1016. * Unregisters a post type. 
  1017. * 
  1018. * Can not be used to unregister built-in post types. 
  1019. * 
  1020. * @since 4.5.0 
  1021. * 
  1022. * @global array $wp_post_types List of post types. 
  1023. * 
  1024. * @param string $post_type Post type to unregister. 
  1025. * @return bool|WP_Error True on success, WP_Error on failure or if the post type doesn't exist. 
  1026. */ 
  1027. function unregister_post_type( $post_type ) { 
  1028. global $wp_post_types; 
  1029.  
  1030. if ( ! post_type_exists( $post_type ) ) { 
  1031. return new WP_Error( 'invalid_post_type', __( 'Invalid post type.' ) ); 
  1032.  
  1033. $post_type_object = get_post_type_object( $post_type ); 
  1034.  
  1035. // Do not allow unregistering internal post types. 
  1036. if ( $post_type_object->_builtin ) { 
  1037. return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) ); 
  1038.  
  1039. $post_type_object->remove_supports(); 
  1040. $post_type_object->remove_rewrite_rules(); 
  1041. $post_type_object->unregister_meta_boxes(); 
  1042. $post_type_object->remove_hooks(); 
  1043. $post_type_object->unregister_taxonomies(); 
  1044.  
  1045. unset( $wp_post_types[ $post_type ] ); 
  1046.  
  1047. /** 
  1048. * Fires after a post type was unregistered. 
  1049. * 
  1050. * @since 4.5.0 
  1051. * 
  1052. * @param string $post_type Post type key. 
  1053. */ 
  1054. do_action( 'unregistered_post_type', $post_type ); 
  1055.  
  1056. return true; 
  1057.  
  1058. /** 
  1059. * Build an object with all post type capabilities out of a post type object 
  1060. * 
  1061. * Post type capabilities use the 'capability_type' argument as a base, if the 
  1062. * capability is not set in the 'capabilities' argument array or if the 
  1063. * 'capabilities' argument is not supplied. 
  1064. * 
  1065. * The capability_type argument can optionally be registered as an array, with 
  1066. * the first value being singular and the second plural, e.g. array('story, 'stories') 
  1067. * Otherwise, an 's' will be added to the value for the plural form. After 
  1068. * registration, capability_type will always be a string of the singular value. 
  1069. * 
  1070. * By default, seven keys are accepted as part of the capabilities array: 
  1071. * 
  1072. * - edit_post, read_post, and delete_post are meta capabilities, which are then 
  1073. * generally mapped to corresponding primitive capabilities depending on the 
  1074. * context, which would be the post being edited/read/deleted and the user or 
  1075. * role being checked. Thus these capabilities would generally not be granted 
  1076. * directly to users or roles. 
  1077. * 
  1078. * - edit_posts - Controls whether objects of this post type can be edited. 
  1079. * - edit_others_posts - Controls whether objects of this type owned by other users 
  1080. * can be edited. If the post type does not support an author, then this will 
  1081. * behave like edit_posts. 
  1082. * - publish_posts - Controls publishing objects of this post type. 
  1083. * - read_private_posts - Controls whether private objects can be read. 
  1084. * 
  1085. * These four primitive capabilities are checked in core in various locations. 
  1086. * There are also seven other primitive capabilities which are not referenced 
  1087. * directly in core, except in map_meta_cap(), which takes the three aforementioned 
  1088. * meta capabilities and translates them into one or more primitive capabilities 
  1089. * that must then be checked against the user or role, depending on the context. 
  1090. * 
  1091. * - read - Controls whether objects of this post type can be read. 
  1092. * - delete_posts - Controls whether objects of this post type can be deleted. 
  1093. * - delete_private_posts - Controls whether private objects can be deleted. 
  1094. * - delete_published_posts - Controls whether published objects can be deleted. 
  1095. * - delete_others_posts - Controls whether objects owned by other users can be 
  1096. * can be deleted. If the post type does not support an author, then this will 
  1097. * behave like delete_posts. 
  1098. * - edit_private_posts - Controls whether private objects can be edited. 
  1099. * - edit_published_posts - Controls whether published objects can be edited. 
  1100. * 
  1101. * These additional capabilities are only used in map_meta_cap(). Thus, they are 
  1102. * only assigned by default if the post type is registered with the 'map_meta_cap' 
  1103. * argument set to true (default is false). 
  1104. * 
  1105. * @since 3.0.0 
  1106. * 
  1107. * @see register_post_type() 
  1108. * @see map_meta_cap() 
  1109. * 
  1110. * @param object $args Post type registration arguments. 
  1111. * @return object object with all the capabilities as member variables. 
  1112. */ 
  1113. function get_post_type_capabilities( $args ) { 
  1114. if ( ! is_array( $args->capability_type ) ) 
  1115. $args->capability_type = array( $args->capability_type, $args->capability_type . 's' ); 
  1116.  
  1117. // Singular base for meta capabilities, plural base for primitive capabilities. 
  1118. list( $singular_base, $plural_base ) = $args->capability_type; 
  1119.  
  1120. $default_capabilities = array( 
  1121. // Meta capabilities 
  1122. 'edit_post' => 'edit_' . $singular_base,  
  1123. 'read_post' => 'read_' . $singular_base,  
  1124. 'delete_post' => 'delete_' . $singular_base,  
  1125. // Primitive capabilities used outside of map_meta_cap(): 
  1126. 'edit_posts' => 'edit_' . $plural_base,  
  1127. 'edit_others_posts' => 'edit_others_' . $plural_base,  
  1128. 'publish_posts' => 'publish_' . $plural_base,  
  1129. 'read_private_posts' => 'read_private_' . $plural_base,  
  1130. ); 
  1131.  
  1132. // Primitive capabilities used within map_meta_cap(): 
  1133. if ( $args->map_meta_cap ) { 
  1134. $default_capabilities_for_mapping = array( 
  1135. 'read' => 'read',  
  1136. 'delete_posts' => 'delete_' . $plural_base,  
  1137. 'delete_private_posts' => 'delete_private_' . $plural_base,  
  1138. 'delete_published_posts' => 'delete_published_' . $plural_base,  
  1139. 'delete_others_posts' => 'delete_others_' . $plural_base,  
  1140. 'edit_private_posts' => 'edit_private_' . $plural_base,  
  1141. 'edit_published_posts' => 'edit_published_' . $plural_base,  
  1142. ); 
  1143. $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping ); 
  1144.  
  1145. $capabilities = array_merge( $default_capabilities, $args->capabilities ); 
  1146.  
  1147. // Post creation capability simply maps to edit_posts by default: 
  1148. if ( ! isset( $capabilities['create_posts'] ) ) 
  1149. $capabilities['create_posts'] = $capabilities['edit_posts']; 
  1150.  
  1151. // Remember meta capabilities for future reference. 
  1152. if ( $args->map_meta_cap ) 
  1153. _post_type_meta_capabilities( $capabilities ); 
  1154.  
  1155. return (object) $capabilities; 
  1156.  
  1157. /** 
  1158. * Store or return a list of post type meta caps for map_meta_cap(). 
  1159. * 
  1160. * @since 3.1.0 
  1161. * @access private 
  1162. * 
  1163. * @global array $post_type_meta_caps Used to store meta capabilities. 
  1164. * 
  1165. * @param array $capabilities Post type meta capabilities. 
  1166. */ 
  1167. function _post_type_meta_capabilities( $capabilities = null ) { 
  1168. global $post_type_meta_caps; 
  1169.  
  1170. foreach ( $capabilities as $core => $custom ) { 
  1171. if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) { 
  1172. $post_type_meta_caps[ $custom ] = $core; 
  1173.  
  1174. /** 
  1175. * Builds an object with all post type labels out of a post type object. 
  1176. * 
  1177. * Accepted keys of the label array in the post type object: 
  1178. * 
  1179. * - `name` - General name for the post type, usually plural. The same and overridden 
  1180. * by `$post_type_object->label`. Default is 'Posts' / 'Pages'. 
  1181. * - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'. 
  1182. * - `add_new` - Default is 'Add New' for both hierarchical and non-hierarchical types. 
  1183. * When internationalizing this string, please use a {@link https://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context gettext context} 
  1184. * matching your post type. Example: `_x( 'Add New', 'product', 'textdomain' );`. 
  1185. * - `add_new_item` - Label for adding a new singular item. Default is 'Add New Post' / 'Add New Page'. 
  1186. * - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'. 
  1187. * - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'. 
  1188. * - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'. 
  1189. * - `search_items` - Label for searching plural items. Default is 'Search Posts' / 'Search Pages'. 
  1190. * - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'. 
  1191. * - `not_found_in_trash` - Label used when no items are in the trash. Default is 'No posts found in Trash' / 
  1192. * 'No pages found in Trash'. 
  1193. * - `parent_item_colon` - Label used to prefix parents of hierarchical items. Not used on non-hierarchical 
  1194. * post types. Default is 'Parent Page:'. 
  1195. * - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'. 
  1196. * - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'. 
  1197. * - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'. 
  1198. * - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' / 
  1199. * 'Uploaded to this page'. 
  1200. * - `featured_image` - Label for the Featured Image meta box title. Default is 'Featured Image'. 
  1201. * - `set_featured_image` - Label for setting the featured image. Default is 'Set featured image'. 
  1202. * - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'. 
  1203. * - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'. 
  1204. * - `menu_name` - Label for the menu name. Default is the same as `name`. 
  1205. * - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' / 
  1206. * 'Filter pages list'. 
  1207. * - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' / 
  1208. * 'Pages list navigation'. 
  1209. * - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'. 
  1210. * 
  1211. * Above, the first default value is for non-hierarchical post types (like posts) 
  1212. * and the second one is for hierarchical post types (like pages). 
  1213. * 
  1214. * Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter. 
  1215. * 
  1216. * @since 3.0.0 
  1217. * @since 4.3.0 Added the `featured_image`, `set_featured_image`, `remove_featured_image`,  
  1218. * and `use_featured_image` labels. 
  1219. * @since 4.4.0 Added the `insert_into_item`, `uploaded_to_this_item`, `filter_items_list`,  
  1220. * `items_list_navigation`, and `items_list` labels. 
  1221. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object. 
  1222. * 
  1223. * @access private 
  1224. * 
  1225. * @param object|WP_Post_Type $post_type_object Post type object. 
  1226. * @return object Object with all the labels as member variables. 
  1227. */ 
  1228. function get_post_type_labels( $post_type_object ) { 
  1229. $nohier_vs_hier_defaults = array( 
  1230. 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),  
  1231. 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),  
  1232. 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),  
  1233. 'add_new_item' => array( __('Add New Post'), __('Add New Page') ),  
  1234. 'edit_item' => array( __('Edit Post'), __('Edit Page') ),  
  1235. 'new_item' => array( __('New Post'), __('New Page') ),  
  1236. 'view_item' => array( __('View Post'), __('View Page') ),  
  1237. 'search_items' => array( __('Search Posts'), __('Search Pages') ),  
  1238. 'not_found' => array( __('No posts found.'), __('No pages found.') ),  
  1239. 'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),  
  1240. 'parent_item_colon' => array( null, __('Parent Page:') ),  
  1241. 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ),  
  1242. 'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ),  
  1243. 'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ),  
  1244. 'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),  
  1245. 'featured_image' => array( __( 'Featured Image' ), __( 'Featured Image' ) ),  
  1246. 'set_featured_image' => array( __( 'Set featured image' ), __( 'Set featured image' ) ),  
  1247. 'remove_featured_image' => array( __( 'Remove featured image' ), __( 'Remove featured image' ) ),  
  1248. 'use_featured_image' => array( __( 'Use as featured image' ), __( 'Use as featured image' ) ),  
  1249. 'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),  
  1250. 'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),  
  1251. 'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ),  
  1252. ); 
  1253. $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; 
  1254.  
  1255. $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); 
  1256.  
  1257. $post_type = $post_type_object->name; 
  1258.  
  1259. $default_labels = clone $labels; 
  1260.  
  1261. /** 
  1262. * Filters the labels of a specific post type. 
  1263. * 
  1264. * The dynamic portion of the hook name, `$post_type`, refers to 
  1265. * the post type slug. 
  1266. * 
  1267. * @since 3.5.0 
  1268. * 
  1269. * @see get_post_type_labels() for the full list of labels. 
  1270. * 
  1271. * @param object $labels Object with labels for the post type as member variables. 
  1272. */ 
  1273. $labels = apply_filters( "post_type_labels_{$post_type}", $labels ); 
  1274.  
  1275. // Ensure that the filtered labels contain all required default values. 
  1276. $labels = (object) array_merge( (array) $default_labels, (array) $labels ); 
  1277.  
  1278. return $labels; 
  1279.  
  1280. /** 
  1281. * Build an object with custom-something object (post type, taxonomy) labels 
  1282. * out of a custom-something object 
  1283. * 
  1284. * @since 3.0.0 
  1285. * @access private 
  1286. * 
  1287. * @param object $object A custom-something object. 
  1288. * @param array $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels. 
  1289. * @return object Object containing labels for the given custom-something object. 
  1290. */ 
  1291. function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { 
  1292. $object->labels = (array) $object->labels; 
  1293.  
  1294. if ( isset( $object->label ) && empty( $object->labels['name'] ) ) 
  1295. $object->labels['name'] = $object->label; 
  1296.  
  1297. if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) 
  1298. $object->labels['singular_name'] = $object->labels['name']; 
  1299.  
  1300. if ( ! isset( $object->labels['name_admin_bar'] ) ) 
  1301. $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name; 
  1302.  
  1303. if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) 
  1304. $object->labels['menu_name'] = $object->labels['name']; 
  1305.  
  1306. if ( !isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) ) 
  1307. $object->labels['all_items'] = $object->labels['menu_name']; 
  1308.  
  1309. if ( !isset( $object->labels['archives'] ) && isset( $object->labels['all_items'] ) ) { 
  1310. $object->labels['archives'] = $object->labels['all_items']; 
  1311.  
  1312. $defaults = array(); 
  1313. foreach ( $nohier_vs_hier_defaults as $key => $value ) { 
  1314. $defaults[$key] = $object->hierarchical ? $value[1] : $value[0]; 
  1315. $labels = array_merge( $defaults, $object->labels ); 
  1316. $object->labels = (object) $object->labels; 
  1317.  
  1318. return (object) $labels; 
  1319.  
  1320. /** 
  1321. * Add submenus for post types. 
  1322. * 
  1323. * @access private 
  1324. * @since 3.1.0 
  1325. */ 
  1326. function _add_post_type_submenus() { 
  1327. foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { 
  1328. $ptype_obj = get_post_type_object( $ptype ); 
  1329. // Sub-menus only. 
  1330. if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true ) 
  1331. continue; 
  1332. add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" ); 
  1333.  
  1334. /** 
  1335. * Register support of certain features for a post type. 
  1336. * 
  1337. * All core features are directly associated with a functional area of the edit 
  1338. * screen, such as the editor or a meta box. Features include: 'title', 'editor',  
  1339. * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes',  
  1340. * 'thumbnail', 'custom-fields', and 'post-formats'. 
  1341. * 
  1342. * Additionally, the 'revisions' feature dictates whether the post type will 
  1343. * store revisions, and the 'comments' feature dictates whether the comments 
  1344. * count will show on the edit screen. 
  1345. * 
  1346. * @since 3.0.0 
  1347. * 
  1348. * @global array $_wp_post_type_features 
  1349. * 
  1350. * @param string $post_type The post type for which to add the feature. 
  1351. * @param string|array $feature The feature being added, accepts an array of 
  1352. * feature strings or a single string. 
  1353. */ 
  1354. function add_post_type_support( $post_type, $feature ) { 
  1355. global $_wp_post_type_features; 
  1356.  
  1357. $features = (array) $feature; 
  1358. foreach ($features as $feature) { 
  1359. if ( func_num_args() == 2 ) 
  1360. $_wp_post_type_features[$post_type][$feature] = true; 
  1361. else 
  1362. $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 ); 
  1363.  
  1364. /** 
  1365. * Remove support for a feature from a post type. 
  1366. * 
  1367. * @since 3.0.0 
  1368. * 
  1369. * @global array $_wp_post_type_features 
  1370. * 
  1371. * @param string $post_type The post type for which to remove the feature. 
  1372. * @param string $feature The feature being removed. 
  1373. */ 
  1374. function remove_post_type_support( $post_type, $feature ) { 
  1375. global $_wp_post_type_features; 
  1376.  
  1377. unset( $_wp_post_type_features[ $post_type ][ $feature ] ); 
  1378.  
  1379. /** 
  1380. * Get all the post type features 
  1381. * 
  1382. * @since 3.4.0 
  1383. * 
  1384. * @global array $_wp_post_type_features 
  1385. * 
  1386. * @param string $post_type The post type. 
  1387. * @return array Post type supports list. 
  1388. */ 
  1389. function get_all_post_type_supports( $post_type ) { 
  1390. global $_wp_post_type_features; 
  1391.  
  1392. if ( isset( $_wp_post_type_features[$post_type] ) ) 
  1393. return $_wp_post_type_features[$post_type]; 
  1394.  
  1395. return array(); 
  1396.  
  1397. /** 
  1398. * Check a post type's support for a given feature. 
  1399. * 
  1400. * @since 3.0.0 
  1401. * 
  1402. * @global array $_wp_post_type_features 
  1403. * 
  1404. * @param string $post_type The post type being checked. 
  1405. * @param string $feature The feature being checked. 
  1406. * @return bool Whether the post type supports the given feature. 
  1407. */ 
  1408. function post_type_supports( $post_type, $feature ) { 
  1409. global $_wp_post_type_features; 
  1410.  
  1411. return ( isset( $_wp_post_type_features[$post_type][$feature] ) ); 
  1412.  
  1413. /** 
  1414. * Retrieves a list of post type names that support a specific feature. 
  1415. * 
  1416. * @since 4.5.0 
  1417. * 
  1418. * @global array $_wp_post_type_features Post type features 
  1419. * 
  1420. * @param array|string $feature Single feature or an array of features the post types should support. 
  1421. * @param string $operator Optional. The logical operation to perform. 'or' means 
  1422. * only one element from the array needs to match; 'and' 
  1423. * means all elements must match; 'not' means no elements may 
  1424. * match. Default 'and'. 
  1425. * @return array A list of post type names. 
  1426. */ 
  1427. function get_post_types_by_support( $feature, $operator = 'and' ) { 
  1428. global $_wp_post_type_features; 
  1429.  
  1430. $features = array_fill_keys( (array) $feature, true ); 
  1431.  
  1432. return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) ); 
  1433.  
  1434. /** 
  1435. * Update the post type for the post ID. 
  1436. * 
  1437. * The page or post cache will be cleaned for the post ID. 
  1438. * 
  1439. * @since 2.5.0 
  1440. * 
  1441. * @global wpdb $wpdb WordPress database abstraction object. 
  1442. * 
  1443. * @param int $post_id Optional. Post ID to change post type. Default 0. 
  1444. * @param string $post_type Optional. Post type. Accepts 'post' or 'page' to 
  1445. * name a few. Default 'post'. 
  1446. * @return int|false Amount of rows changed. Should be 1 for success and 0 for failure. 
  1447. */ 
  1448. function set_post_type( $post_id = 0, $post_type = 'post' ) { 
  1449. global $wpdb; 
  1450.  
  1451. $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db'); 
  1452. $return = $wpdb->update( $wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) ); 
  1453.  
  1454. clean_post_cache( $post_id ); 
  1455.  
  1456. return $return; 
  1457.  
  1458. /** 
  1459. * Determines whether a post type is considered "viewable". 
  1460. * 
  1461. * For built-in post types such as posts and pages, the 'public' value will be evaluated. 
  1462. * For all others, the 'publicly_queryable' value will be used. 
  1463. * 
  1464. * @since 4.4.0 
  1465. * @since 4.5.0 Added the ability to pass a post type name in addition to object. 
  1466. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object. 
  1467. * 
  1468. * @param string|WP_Post_Type $post_type Post type name or object. 
  1469. * @return bool Whether the post type should be considered viewable. 
  1470. */ 
  1471. function is_post_type_viewable( $post_type ) { 
  1472. if ( is_scalar( $post_type ) ) { 
  1473. $post_type = get_post_type_object( $post_type ); 
  1474. if ( ! $post_type ) { 
  1475. return false; 
  1476.  
  1477. return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); 
  1478.  
  1479. /** 
  1480. * Retrieve list of latest posts or posts matching criteria. 
  1481. * 
  1482. * The defaults are as follows: 
  1483. * 
  1484. * @since 1.2.0 
  1485. * 
  1486. * @see WP_Query::parse_query() 
  1487. * 
  1488. * @param array $args { 
  1489. * Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all 
  1490. * available arguments. 
  1491. * 
  1492. * @type int $numberposts Total number of posts to retrieve. Is an alias of $posts_per_page 
  1493. * in WP_Query. Accepts -1 for all. Default 5. 
  1494. * @type int|string $category Category ID or comma-separated list of IDs (this or any children). 
  1495. * Is an alias of $cat in WP_Query. Default 0. 
  1496. * @type array $include An array of post IDs to retrieve, sticky posts will be included. 
  1497. * Is an alias of $post__in in WP_Query. Default empty array. 
  1498. * @type array $exclude An array of post IDs not to retrieve. Default empty array. 
  1499. * @type bool $suppress_filters Whether to suppress filters. Default true. 
  1500. * } 
  1501. * @return array List of posts. 
  1502. */ 
  1503. function get_posts( $args = null ) { 
  1504. $defaults = array( 
  1505. 'numberposts' => 5,  
  1506. 'category' => 0, 'orderby' => 'date',  
  1507. 'order' => 'DESC', 'include' => array(),  
  1508. 'exclude' => array(), 'meta_key' => '',  
  1509. 'meta_value' =>'', 'post_type' => 'post',  
  1510. 'suppress_filters' => true 
  1511. ); 
  1512.  
  1513. $r = wp_parse_args( $args, $defaults ); 
  1514. if ( empty( $r['post_status'] ) ) 
  1515. $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish'; 
  1516. if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) ) 
  1517. $r['posts_per_page'] = $r['numberposts']; 
  1518. if ( ! empty($r['category']) ) 
  1519. $r['cat'] = $r['category']; 
  1520. if ( ! empty($r['include']) ) { 
  1521. $incposts = wp_parse_id_list( $r['include'] ); 
  1522. $r['posts_per_page'] = count($incposts); // only the number of posts included 
  1523. $r['post__in'] = $incposts; 
  1524. } elseif ( ! empty($r['exclude']) ) 
  1525. $r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); 
  1526.  
  1527. $r['ignore_sticky_posts'] = true; 
  1528. $r['no_found_rows'] = true; 
  1529.  
  1530. $get_posts = new WP_Query; 
  1531. return $get_posts->query($r); 
  1532.  
  1533.  
  1534. // 
  1535.  // Post meta functions 
  1536. // 
  1537.   
  1538. /** 
  1539. * Add meta data field to a post. 
  1540. * 
  1541. * Post meta data is called "Custom Fields" on the Administration Screen. 
  1542. * 
  1543. * @since 1.5.0 
  1544. * 
  1545. * @param int $post_id Post ID. 
  1546. * @param string $meta_key Metadata name. 
  1547. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 
  1548. * @param bool $unique Optional. Whether the same key should not be added. 
  1549. * Default false. 
  1550. * @return int|false Meta ID on success, false on failure. 
  1551. */ 
  1552. function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) { 
  1553. // Make sure meta is added to the post, not a revision. 
  1554. if ( $the_post = wp_is_post_revision($post_id) ) 
  1555. $post_id = $the_post; 
  1556.  
  1557. return add_metadata('post', $post_id, $meta_key, $meta_value, $unique); 
  1558.  
  1559. /** 
  1560. * Remove metadata matching criteria from a post. 
  1561. * 
  1562. * You can match based on the key, or key and value. Removing based on key and 
  1563. * value, will keep from removing duplicate metadata with the same key. It also 
  1564. * allows removing all metadata matching key, if needed. 
  1565. * 
  1566. * @since 1.5.0 
  1567. * 
  1568. * @param int $post_id Post ID. 
  1569. * @param string $meta_key Metadata name. 
  1570. * @param mixed $meta_value Optional. Metadata value. Must be serializable if 
  1571. * non-scalar. Default empty. 
  1572. * @return bool True on success, false on failure. 
  1573. */ 
  1574. function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { 
  1575. // Make sure meta is added to the post, not a revision. 
  1576. if ( $the_post = wp_is_post_revision($post_id) ) 
  1577. $post_id = $the_post; 
  1578.  
  1579. return delete_metadata('post', $post_id, $meta_key, $meta_value); 
  1580.  
  1581. /** 
  1582. * Retrieve post meta field for a post. 
  1583. * 
  1584. * @since 1.5.0 
  1585. * 
  1586. * @param int $post_id Post ID. 
  1587. * @param string $key Optional. The meta key to retrieve. By default, returns 
  1588. * data for all keys. Default empty. 
  1589. * @param bool $single Optional. Whether to return a single value. Default false. 
  1590. * @return mixed Will be an array if $single is false. Will be value of meta data 
  1591. * field if $single is true. 
  1592. */ 
  1593. function get_post_meta( $post_id, $key = '', $single = false ) { 
  1594. return get_metadata('post', $post_id, $key, $single); 
  1595.  
  1596. /** 
  1597. * Update post meta field based on post ID. 
  1598. * 
  1599. * Use the $prev_value parameter to differentiate between meta fields with the 
  1600. * same key and post ID. 
  1601. * 
  1602. * If the meta field for the post does not exist, it will be added. 
  1603. * 
  1604. * @since 1.5.0 
  1605. * 
  1606. * @param int $post_id Post ID. 
  1607. * @param string $meta_key Metadata key. 
  1608. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 
  1609. * @param mixed $prev_value Optional. Previous value to check before removing. 
  1610. * Default empty. 
  1611. * @return int|bool Meta ID if the key didn't exist, true on successful update,  
  1612. * false on failure. 
  1613. */ 
  1614. function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { 
  1615. // Make sure meta is added to the post, not a revision. 
  1616. if ( $the_post = wp_is_post_revision($post_id) ) 
  1617. $post_id = $the_post; 
  1618.  
  1619. return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value); 
  1620.  
  1621. /** 
  1622. * Delete everything from post meta matching meta key. 
  1623. * 
  1624. * @since 2.3.0 
  1625. * 
  1626. * @param string $post_meta_key Key to search for when deleting. 
  1627. * @return bool Whether the post meta key was deleted from the database. 
  1628. */ 
  1629. function delete_post_meta_by_key( $post_meta_key ) { 
  1630. return delete_metadata( 'post', null, $post_meta_key, '', true ); 
  1631.  
  1632. /** 
  1633. * Retrieve post meta fields, based on post ID. 
  1634. * 
  1635. * The post meta fields are retrieved from the cache where possible,  
  1636. * so the function is optimized to be called more than once. 
  1637. * 
  1638. * @since 1.2.0 
  1639. * 
  1640. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  1641. * @return array Post meta for the given post. 
  1642. */ 
  1643. function get_post_custom( $post_id = 0 ) { 
  1644. $post_id = absint( $post_id ); 
  1645. if ( ! $post_id ) 
  1646. $post_id = get_the_ID(); 
  1647.  
  1648. return get_post_meta( $post_id ); 
  1649.  
  1650. /** 
  1651. * Retrieve meta field names for a post. 
  1652. * 
  1653. * If there are no meta fields, then nothing (null) will be returned. 
  1654. * 
  1655. * @since 1.2.0 
  1656. * 
  1657. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  1658. * @return array|void Array of the keys, if retrieved. 
  1659. */ 
  1660. function get_post_custom_keys( $post_id = 0 ) { 
  1661. $custom = get_post_custom( $post_id ); 
  1662.  
  1663. if ( !is_array($custom) ) 
  1664. return; 
  1665.  
  1666. if ( $keys = array_keys($custom) ) 
  1667. return $keys; 
  1668.  
  1669. /** 
  1670. * Retrieve values for a custom post field. 
  1671. * 
  1672. * The parameters must not be considered optional. All of the post meta fields 
  1673. * will be retrieved and only the meta field key values returned. 
  1674. * 
  1675. * @since 1.2.0 
  1676. * 
  1677. * @param string $key Optional. Meta field key. Default empty. 
  1678. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  1679. * @return array|null Meta field values. 
  1680. */ 
  1681. function get_post_custom_values( $key = '', $post_id = 0 ) { 
  1682. if ( !$key ) 
  1683. return null; 
  1684.  
  1685. $custom = get_post_custom($post_id); 
  1686.  
  1687. return isset($custom[$key]) ? $custom[$key] : null; 
  1688.  
  1689. /** 
  1690. * Check if post is sticky. 
  1691. * 
  1692. * Sticky posts should remain at the top of The Loop. If the post ID is not 
  1693. * given, then The Loop ID for the current post will be used. 
  1694. * 
  1695. * @since 2.7.0 
  1696. * 
  1697. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  1698. * @return bool Whether post is sticky. 
  1699. */ 
  1700. function is_sticky( $post_id = 0 ) { 
  1701. $post_id = absint( $post_id ); 
  1702.  
  1703. if ( ! $post_id ) 
  1704. $post_id = get_the_ID(); 
  1705.  
  1706. $stickies = get_option( 'sticky_posts' ); 
  1707.  
  1708. if ( ! is_array( $stickies ) ) 
  1709. return false; 
  1710.  
  1711. if ( in_array( $post_id, $stickies ) ) 
  1712. return true; 
  1713.  
  1714. return false; 
  1715.  
  1716. /** 
  1717. * Sanitize every post field. 
  1718. * 
  1719. * If the context is 'raw', then the post object or array will get minimal 
  1720. * sanitization of the integer fields. 
  1721. * 
  1722. * @since 2.3.0 
  1723. * 
  1724. * @see sanitize_post_field() 
  1725. * 
  1726. * @param object|WP_Post|array $post The Post Object or Array 
  1727. * @param string $context Optional. How to sanitize post fields. 
  1728. * Accepts 'raw', 'edit', 'db', or 'display'. 
  1729. * Default 'display'. 
  1730. * @return object|WP_Post|array The now sanitized Post Object or Array (will be the 
  1731. * same type as $post). 
  1732. */ 
  1733. function sanitize_post( $post, $context = 'display' ) { 
  1734. if ( is_object($post) ) { 
  1735. // Check if post already filtered for this context. 
  1736. if ( isset($post->filter) && $context == $post->filter ) 
  1737. return $post; 
  1738. if ( !isset($post->ID) ) 
  1739. $post->ID = 0; 
  1740. foreach ( array_keys(get_object_vars($post)) as $field ) 
  1741. $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context); 
  1742. $post->filter = $context; 
  1743. } elseif ( is_array( $post ) ) { 
  1744. // Check if post already filtered for this context. 
  1745. if ( isset($post['filter']) && $context == $post['filter'] ) 
  1746. return $post; 
  1747. if ( !isset($post['ID']) ) 
  1748. $post['ID'] = 0; 
  1749. foreach ( array_keys($post) as $field ) 
  1750. $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context); 
  1751. $post['filter'] = $context; 
  1752. return $post; 
  1753.  
  1754. /** 
  1755. * Sanitize post field based on context. 
  1756. * 
  1757. * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 
  1758. * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts 
  1759. * are treated like 'display' when calling filters. 
  1760. * 
  1761. * @since 2.3.0 
  1762. * @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'. 
  1763. * 
  1764. * @param string $field The Post Object field name. 
  1765. * @param mixed $value The Post Object value. 
  1766. * @param int $post_id Post ID. 
  1767. * @param string $context Optional. How to sanitize post fields. Looks for 'raw', 'edit',  
  1768. * 'db', 'display', 'attribute' and 'js'. Default 'display'. 
  1769. * @return mixed Sanitized value. 
  1770. */ 
  1771. function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) { 
  1772. $int_fields = array('ID', 'post_parent', 'menu_order'); 
  1773. if ( in_array($field, $int_fields) ) 
  1774. $value = (int) $value; 
  1775.  
  1776. // Fields which contain arrays of integers. 
  1777. $array_int_fields = array( 'ancestors' ); 
  1778. if ( in_array($field, $array_int_fields) ) { 
  1779. $value = array_map( 'absint', $value); 
  1780. return $value; 
  1781.  
  1782. if ( 'raw' == $context ) 
  1783. return $value; 
  1784.  
  1785. $prefixed = false; 
  1786. if ( false !== strpos($field, 'post_') ) { 
  1787. $prefixed = true; 
  1788. $field_no_prefix = str_replace('post_', '', $field); 
  1789.  
  1790. if ( 'edit' == $context ) { 
  1791. $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password'); 
  1792.  
  1793. if ( $prefixed ) { 
  1794.  
  1795. /** 
  1796. * Filters the value of a specific post field to edit. 
  1797. * 
  1798. * The dynamic portion of the hook name, `$field`, refers to the post 
  1799. * field name. 
  1800. * 
  1801. * @since 2.3.0 
  1802. * 
  1803. * @param mixed $value Value of the post field. 
  1804. * @param int $post_id Post ID. 
  1805. */ 
  1806. $value = apply_filters( "edit_{$field}", $value, $post_id ); 
  1807.  
  1808. /** 
  1809. * Filters the value of a specific post field to edit. 
  1810. * 
  1811. * The dynamic portion of the hook name, `$field_no_prefix`, refers to 
  1812. * the post field name. 
  1813. * 
  1814. * @since 2.3.0 
  1815. * 
  1816. * @param mixed $value Value of the post field. 
  1817. * @param int $post_id Post ID. 
  1818. */ 
  1819. $value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id ); 
  1820. } else { 
  1821. $value = apply_filters( "edit_post_{$field}", $value, $post_id ); 
  1822.  
  1823. if ( in_array($field, $format_to_edit) ) { 
  1824. if ( 'post_content' == $field ) 
  1825. $value = format_to_edit($value, user_can_richedit()); 
  1826. else 
  1827. $value = format_to_edit($value); 
  1828. } else { 
  1829. $value = esc_attr($value); 
  1830. } elseif ( 'db' == $context ) { 
  1831. if ( $prefixed ) { 
  1832.  
  1833. /** 
  1834. * Filters the value of a specific post field before saving. 
  1835. * 
  1836. * The dynamic portion of the hook name, `$field`, refers to the post 
  1837. * field name. 
  1838. * 
  1839. * @since 2.3.0 
  1840. * 
  1841. * @param mixed $value Value of the post field. 
  1842. */ 
  1843. $value = apply_filters( "pre_{$field}", $value ); 
  1844.  
  1845. /** 
  1846. * Filters the value of a specific field before saving. 
  1847. * 
  1848. * The dynamic portion of the hook name, `$field_no_prefix`, refers 
  1849. * to the post field name. 
  1850. * 
  1851. * @since 2.3.0 
  1852. * 
  1853. * @param mixed $value Value of the post field. 
  1854. */ 
  1855. $value = apply_filters( "{$field_no_prefix}_save_pre", $value ); 
  1856. } else { 
  1857. $value = apply_filters( "pre_post_{$field}", $value ); 
  1858.  
  1859. /** 
  1860. * Filters the value of a specific post field before saving. 
  1861. * 
  1862. * The dynamic portion of the hook name, `$field`, refers to the post 
  1863. * field name. 
  1864. * 
  1865. * @since 2.3.0 
  1866. * 
  1867. * @param mixed $value Value of the post field. 
  1868. */ 
  1869. $value = apply_filters( "{$field}_pre", $value ); 
  1870. } else { 
  1871.  
  1872. // Use display filters by default. 
  1873. if ( $prefixed ) { 
  1874.  
  1875. /** 
  1876. * Filters the value of a specific post field for display. 
  1877. * 
  1878. * The dynamic portion of the hook name, `$field`, refers to the post 
  1879. * field name. 
  1880. * 
  1881. * @since 2.3.0 
  1882. * 
  1883. * @param mixed $value Value of the prefixed post field. 
  1884. * @param int $post_id Post ID. 
  1885. * @param string $context Context for how to sanitize the field. Possible 
  1886. * values include 'raw', 'edit', 'db', 'display',  
  1887. * 'attribute' and 'js'. 
  1888. */ 
  1889. $value = apply_filters( $field, $value, $post_id, $context ); 
  1890. } else { 
  1891. $value = apply_filters( "post_{$field}", $value, $post_id, $context ); 
  1892.  
  1893. if ( 'attribute' == $context ) { 
  1894. $value = esc_attr( $value ); 
  1895. } elseif ( 'js' == $context ) { 
  1896. $value = esc_js( $value ); 
  1897.  
  1898. return $value; 
  1899.  
  1900. /** 
  1901. * Make a post sticky. 
  1902. * 
  1903. * Sticky posts should be displayed at the top of the front page. 
  1904. * 
  1905. * @since 2.7.0 
  1906. * 
  1907. * @param int $post_id Post ID. 
  1908. */ 
  1909. function stick_post( $post_id ) { 
  1910. $stickies = get_option('sticky_posts'); 
  1911.  
  1912. if ( !is_array($stickies) ) 
  1913. $stickies = array($post_id); 
  1914.  
  1915. if ( ! in_array($post_id, $stickies) ) 
  1916. $stickies[] = $post_id; 
  1917.  
  1918. $updated = update_option( 'sticky_posts', $stickies ); 
  1919.  
  1920. if ( $updated ) { 
  1921. /** 
  1922. * Fires once a post has been added to the sticky list. 
  1923. * 
  1924. * @since 4.6.0 
  1925. * 
  1926. * @param int $post_id ID of the post that was stuck. 
  1927. */ 
  1928. do_action( 'post_stuck', $post_id ); 
  1929.  
  1930. /** 
  1931. * Un-stick a post. 
  1932. * 
  1933. * Sticky posts should be displayed at the top of the front page. 
  1934. * 
  1935. * @since 2.7.0 
  1936. * 
  1937. * @param int $post_id Post ID. 
  1938. */ 
  1939. function unstick_post( $post_id ) { 
  1940. $stickies = get_option('sticky_posts'); 
  1941.  
  1942. if ( !is_array($stickies) ) 
  1943. return; 
  1944.  
  1945. if ( ! in_array($post_id, $stickies) ) 
  1946. return; 
  1947.  
  1948. $offset = array_search($post_id, $stickies); 
  1949. if ( false === $offset ) 
  1950. return; 
  1951.  
  1952. array_splice($stickies, $offset, 1); 
  1953.  
  1954. $updated = update_option( 'sticky_posts', $stickies ); 
  1955.  
  1956. if ( $updated ) { 
  1957. /** 
  1958. * Fires once a post has been removed from the sticky list. 
  1959. * 
  1960. * @since 4.6.0 
  1961. * 
  1962. * @param int $post_id ID of the post that was unstuck. 
  1963. */ 
  1964. do_action( 'post_unstuck', $post_id ); 
  1965.  
  1966. /** 
  1967. * Return the cache key for wp_count_posts() based on the passed arguments. 
  1968. * 
  1969. * @since 3.9.0 
  1970. * 
  1971. * @param string $type Optional. Post type to retrieve count Default 'post'. 
  1972. * @param string $perm Optional. 'readable' or empty. Default empty. 
  1973. * @return string The cache key. 
  1974. */ 
  1975. function _count_posts_cache_key( $type = 'post', $perm = '' ) { 
  1976. $cache_key = 'posts-' . $type; 
  1977. if ( 'readable' == $perm && is_user_logged_in() ) { 
  1978. $post_type_object = get_post_type_object( $type ); 
  1979. if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) { 
  1980. $cache_key .= '_' . $perm . '_' . get_current_user_id(); 
  1981. return $cache_key; 
  1982.  
  1983. /** 
  1984. * Count number of posts of a post type and if user has permissions to view. 
  1985. * 
  1986. * This function provides an efficient method of finding the amount of post's 
  1987. * type a blog has. Another method is to count the amount of items in 
  1988. * get_posts(), but that method has a lot of overhead with doing so. Therefore,  
  1989. * when developing for 2.5+, use this function instead. 
  1990. * 
  1991. * The $perm parameter checks for 'readable' value and if the user can read 
  1992. * private posts, it will display that for the user that is signed in. 
  1993. * 
  1994. * @since 2.5.0 
  1995. * 
  1996. * @global wpdb $wpdb WordPress database abstraction object. 
  1997. * 
  1998. * @param string $type Optional. Post type to retrieve count. Default 'post'. 
  1999. * @param string $perm Optional. 'readable' or empty. Default empty. 
  2000. * @return object Number of posts for each status. 
  2001. */ 
  2002. function wp_count_posts( $type = 'post', $perm = '' ) { 
  2003. global $wpdb; 
  2004.  
  2005. if ( ! post_type_exists( $type ) ) 
  2006. return new stdClass; 
  2007.  
  2008. $cache_key = _count_posts_cache_key( $type, $perm ); 
  2009.  
  2010. $counts = wp_cache_get( $cache_key, 'counts' ); 
  2011. if ( false !== $counts ) { 
  2012. /** This filter is documented in wp-includes/post.php */ 
  2013. return apply_filters( 'wp_count_posts', $counts, $type, $perm ); 
  2014.  
  2015. $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; 
  2016. if ( 'readable' == $perm && is_user_logged_in() ) { 
  2017. $post_type_object = get_post_type_object($type); 
  2018. if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { 
  2019. $query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",  
  2020. get_current_user_id() 
  2021. ); 
  2022. $query .= ' GROUP BY post_status'; 
  2023.  
  2024. $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); 
  2025. $counts = array_fill_keys( get_post_stati(), 0 ); 
  2026.  
  2027. foreach ( $results as $row ) { 
  2028. $counts[ $row['post_status'] ] = $row['num_posts']; 
  2029.  
  2030. $counts = (object) $counts; 
  2031. wp_cache_set( $cache_key, $counts, 'counts' ); 
  2032.  
  2033. /** 
  2034. * Modify returned post counts by status for the current post type. 
  2035. * 
  2036. * @since 3.7.0 
  2037. * 
  2038. * @param object $counts An object containing the current post_type's post 
  2039. * counts by status. 
  2040. * @param string $type Post type. 
  2041. * @param string $perm The permission to determine if the posts are 'readable' 
  2042. * by the current user. 
  2043. */ 
  2044. return apply_filters( 'wp_count_posts', $counts, $type, $perm ); 
  2045.  
  2046. /** 
  2047. * Count number of attachments for the mime type(s). 
  2048. * 
  2049. * If you set the optional mime_type parameter, then an array will still be 
  2050. * returned, but will only have the item you are looking for. It does not give 
  2051. * you the number of attachments that are children of a post. You can get that 
  2052. * by counting the number of children that post has. 
  2053. * 
  2054. * @since 2.5.0 
  2055. * 
  2056. * @global wpdb $wpdb WordPress database abstraction object. 
  2057. * 
  2058. * @param string|array $mime_type Optional. Array or comma-separated list of 
  2059. * MIME patterns. Default empty. 
  2060. * @return object An object containing the attachment counts by mime type. 
  2061. */ 
  2062. function wp_count_attachments( $mime_type = '' ) { 
  2063. global $wpdb; 
  2064.  
  2065. $and = wp_post_mime_type_where( $mime_type ); 
  2066. $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); 
  2067.  
  2068. $counts = array(); 
  2069. foreach ( (array) $count as $row ) { 
  2070. $counts[ $row['post_mime_type'] ] = $row['num_posts']; 
  2071. $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and"); 
  2072.  
  2073. /** 
  2074. * Modify returned attachment counts by mime type. 
  2075. * 
  2076. * @since 3.7.0 
  2077. * 
  2078. * @param object $counts An object containing the attachment counts by 
  2079. * mime type. 
  2080. * @param string $mime_type The mime type pattern used to filter the attachments 
  2081. * counted. 
  2082. */ 
  2083. return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); 
  2084.  
  2085. /** 
  2086. * Get default post mime types. 
  2087. * 
  2088. * @since 2.9.0 
  2089. * 
  2090. * @return array List of post mime types. 
  2091. */ 
  2092. function get_post_mime_types() { 
  2093. $post_mime_types = array( // array( adj, noun ) 
  2094. 'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')),  
  2095. 'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')),  
  2096. 'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')),  
  2097. ); 
  2098.  
  2099. /** 
  2100. * Filters the default list of post mime types. 
  2101. * 
  2102. * @since 2.5.0 
  2103. * 
  2104. * @param array $post_mime_types Default list of post mime types. 
  2105. */ 
  2106. return apply_filters( 'post_mime_types', $post_mime_types ); 
  2107.  
  2108. /** 
  2109. * Check a MIME-Type against a list. 
  2110. * 
  2111. * If the wildcard_mime_types parameter is a string, it must be comma separated 
  2112. * list. If the real_mime_types is a string, it is also comma separated to 
  2113. * create the list. 
  2114. * 
  2115. * @since 2.5.0 
  2116. * 
  2117. * @param string|array $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/**) 
  2118. * or flash (same as *flash*). 
  2119. * @param string|array $real_mime_types Real post mime type values. 
  2120. * @return array array(wildcard=>array(real types)). 
  2121. */ 
  2122. function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { 
  2123. $matches = array(); 
  2124. if ( is_string( $wildcard_mime_types ) ) { 
  2125. $wildcard_mime_types = array_map( 'trim', explode( ', ', $wildcard_mime_types ) ); 
  2126. if ( is_string( $real_mime_types ) ) { 
  2127. $real_mime_types = array_map( 'trim', explode( ', ', $real_mime_types ) ); 
  2128.  
  2129. $patternses = array(); 
  2130. $wild = '[-._a-z0-9]*'; 
  2131.  
  2132. foreach ( (array) $wildcard_mime_types as $type ) { 
  2133. $mimes = array_map( 'trim', explode( ', ', $type ) ); 
  2134. foreach ( $mimes as $mime ) { 
  2135. $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); 
  2136. $patternses[][$type] = "^$regex$"; 
  2137. if ( false === strpos( $mime, '/' ) ) { 
  2138. $patternses[][$type] = "^$regex/"; 
  2139. $patternses[][$type] = $regex; 
  2140. asort( $patternses ); 
  2141.  
  2142. foreach ( $patternses as $patterns ) { 
  2143. foreach ( $patterns as $type => $pattern ) { 
  2144. foreach ( (array) $real_mime_types as $real ) { 
  2145. if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[$type] ) || false === array_search( $real, $matches[$type] ) ) ) { 
  2146. $matches[$type][] = $real; 
  2147. return $matches; 
  2148.  
  2149. /** 
  2150. * Convert MIME types into SQL. 
  2151. * 
  2152. * @since 2.5.0 
  2153. * 
  2154. * @param string|array $post_mime_types List of mime types or comma separated string 
  2155. * of mime types. 
  2156. * @param string $table_alias Optional. Specify a table alias, if needed. 
  2157. * Default empty. 
  2158. * @return string The SQL AND clause for mime searching. 
  2159. */ 
  2160. function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { 
  2161. $where = ''; 
  2162. $wildcards = array('', '%', '%/%'); 
  2163. if ( is_string($post_mime_types) ) 
  2164. $post_mime_types = array_map('trim', explode(', ', $post_mime_types)); 
  2165.  
  2166. $wheres = array(); 
  2167.  
  2168. foreach ( (array) $post_mime_types as $mime_type ) { 
  2169. $mime_type = preg_replace('/\s/', '', $mime_type); 
  2170. $slashpos = strpos($mime_type, '/'); 
  2171. if ( false !== $slashpos ) { 
  2172. $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos)); 
  2173. $mime_subgroup = preg_replace('/[^-*.+a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1)); 
  2174. if ( empty($mime_subgroup) ) 
  2175. $mime_subgroup = '*'; 
  2176. else 
  2177. $mime_subgroup = str_replace('/', '', $mime_subgroup); 
  2178. $mime_pattern = "$mime_group/$mime_subgroup"; 
  2179. } else { 
  2180. $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type); 
  2181. if ( false === strpos($mime_pattern, '*') ) 
  2182. $mime_pattern .= '/*'; 
  2183. } 
  2184.   
  2185. $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern); 
  2186.   
  2187. if ( in_array( $mime_type, $wildcards ) ) 
  2188. return ''; 
  2189.   
  2190. if ( false !== strpos($mime_pattern, '%') ) 
  2191. $wheres[] = empty($table_alias) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; 
  2192. else 
  2193. $wheres[] = empty($table_alias) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; 
  2194. } 
  2195. if ( !empty($wheres) ) 
  2196. $where = ' AND (' . join(' OR ', $wheres) . ') '; 
  2197. return $where; 
  2198. } 
  2199.   
  2200. /** 
  2201. * Trash or delete a post or page. 
  2202. * 
  2203. * When the post and page is permanently deleted, everything that is tied to 
  2204. * it is deleted also. This includes comments, post meta fields, and terms 
  2205. * associated with the post. 
  2206. * 
  2207. * The post or page is moved to trash instead of permanently deleted unless 
  2208. * trash is disabled, item is already in the trash, or $force_delete is true. 
  2209. * 
  2210. * @since 1.0.0 
  2211. * 
  2212. * @global wpdb $wpdb WordPress database abstraction object. 
  2213. * @see wp_delete_attachment() 
  2214. * @see wp_trash_post() 
  2215. * 
  2216. * @param int $postid Optional. Post ID. Default 0. 
  2217. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. 
  2218. * Default false. 
  2219. * @return array|false|WP_Post False on failure. 
  2220. */ 
  2221. function wp_delete_post( $postid = 0, $force_delete = false ) { 
  2222. global $wpdb; 
  2223.  
  2224. if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) ) 
  2225. return $post; 
  2226.  
  2227. if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS ) 
  2228. return wp_trash_post( $postid ); 
  2229.  
  2230. if ( $post->post_type == 'attachment' ) 
  2231. return wp_delete_attachment( $postid, $force_delete ); 
  2232.  
  2233. /** 
  2234. * Filters whether a post deletion should take place. 
  2235. * 
  2236. * @since 4.4.0 
  2237. * 
  2238. * @param bool $delete Whether to go forward with deletion. 
  2239. * @param WP_Post $post Post object. 
  2240. * @param bool $force_delete Whether to bypass the trash. 
  2241. */ 
  2242. $check = apply_filters( 'pre_delete_post', null, $post, $force_delete ); 
  2243. if ( null !== $check ) { 
  2244. return $check; 
  2245.  
  2246. /** 
  2247. * Fires before a post is deleted, at the start of wp_delete_post(). 
  2248. * 
  2249. * @since 3.2.0 
  2250. * 
  2251. * @see wp_delete_post() 
  2252. * 
  2253. * @param int $postid Post ID. 
  2254. */ 
  2255. do_action( 'before_delete_post', $postid ); 
  2256.  
  2257. delete_post_meta($postid, '_wp_trash_meta_status'); 
  2258. delete_post_meta($postid, '_wp_trash_meta_time'); 
  2259.  
  2260. wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type)); 
  2261.  
  2262. $parent_data = array( 'post_parent' => $post->post_parent ); 
  2263. $parent_where = array( 'post_parent' => $postid ); 
  2264.  
  2265. if ( is_post_type_hierarchical( $post->post_type ) ) { 
  2266. // Point children of this page to its parent, also clean the cache of affected children. 
  2267. $children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type ); 
  2268. $children = $wpdb->get_results( $children_query ); 
  2269. if ( $children ) { 
  2270. $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); 
  2271.  
  2272. // Do raw query. wp_get_post_revisions() is filtered. 
  2273. $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); 
  2274. // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. 
  2275. foreach ( $revision_ids as $revision_id ) 
  2276. wp_delete_post_revision( $revision_id ); 
  2277.  
  2278. // Point all attachments to this post up one level. 
  2279. $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); 
  2280.  
  2281. wp_defer_comment_counting( true ); 
  2282.  
  2283. $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid )); 
  2284. foreach ( $comment_ids as $comment_id ) { 
  2285. wp_delete_comment( $comment_id, true ); 
  2286.  
  2287. wp_defer_comment_counting( false ); 
  2288.  
  2289. $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid )); 
  2290. foreach ( $post_meta_ids as $mid ) 
  2291. delete_metadata_by_mid( 'post', $mid ); 
  2292.  
  2293. /** 
  2294. * Fires immediately before a post is deleted from the database. 
  2295. * 
  2296. * @since 1.2.0 
  2297. * 
  2298. * @param int $postid Post ID. 
  2299. */ 
  2300. do_action( 'delete_post', $postid ); 
  2301. $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) ); 
  2302. if ( ! $result ) { 
  2303. return false; 
  2304.  
  2305. /** 
  2306. * Fires immediately after a post is deleted from the database. 
  2307. * 
  2308. * @since 2.2.0 
  2309. * 
  2310. * @param int $postid Post ID. 
  2311. */ 
  2312. do_action( 'deleted_post', $postid ); 
  2313.  
  2314. clean_post_cache( $post ); 
  2315.  
  2316. if ( is_post_type_hierarchical( $post->post_type ) && $children ) { 
  2317. foreach ( $children as $child ) 
  2318. clean_post_cache( $child ); 
  2319.  
  2320. wp_clear_scheduled_hook('publish_future_post', array( $postid ) ); 
  2321.  
  2322. /** 
  2323. * Fires after a post is deleted, at the conclusion of wp_delete_post(). 
  2324. * 
  2325. * @since 3.2.0 
  2326. * 
  2327. * @see wp_delete_post() 
  2328. * 
  2329. * @param int $postid Post ID. 
  2330. */ 
  2331. do_action( 'after_delete_post', $postid ); 
  2332.  
  2333. return $post; 
  2334.  
  2335. /** 
  2336. * Reset the page_on_front, show_on_front, and page_for_post settings when 
  2337. * a linked page is deleted or trashed. 
  2338. * 
  2339. * Also ensures the post is no longer sticky. 
  2340. * 
  2341. * @since 3.7.0 
  2342. * @access private 
  2343. * 
  2344. * @param int $post_id Post ID. 
  2345. */ 
  2346. function _reset_front_page_settings_for_post( $post_id ) { 
  2347. $post = get_post( $post_id ); 
  2348. if ( 'page' == $post->post_type ) { 
  2349. /** 
  2350. * If the page is defined in option page_on_front or post_for_posts,  
  2351. * adjust the corresponding options. 
  2352. */ 
  2353. if ( get_option( 'page_on_front' ) == $post->ID ) { 
  2354. update_option( 'show_on_front', 'posts' ); 
  2355. update_option( 'page_on_front', 0 ); 
  2356. if ( get_option( 'page_for_posts' ) == $post->ID ) { 
  2357. delete_option( 'page_for_posts', 0 ); 
  2358. unstick_post( $post->ID ); 
  2359.  
  2360. /** 
  2361. * Move a post or page to the Trash 
  2362. * 
  2363. * If trash is disabled, the post or page is permanently deleted. 
  2364. * 
  2365. * @since 2.9.0 
  2366. * 
  2367. * @see wp_delete_post() 
  2368. * 
  2369. * @param int $post_id Optional. Post ID. Default is ID of the global $post 
  2370. * if EMPTY_TRASH_DAYS equals true. 
  2371. * @return false|array|WP_Post|null Post data array, otherwise false. 
  2372. */ 
  2373. function wp_trash_post( $post_id = 0 ) { 
  2374. if ( !EMPTY_TRASH_DAYS ) 
  2375. return wp_delete_post($post_id, true); 
  2376.  
  2377. if ( !$post = get_post($post_id, ARRAY_A) ) 
  2378. return $post; 
  2379.  
  2380. if ( $post['post_status'] == 'trash' ) 
  2381. return false; 
  2382.  
  2383. /** 
  2384. * Fires before a post is sent to the trash. 
  2385. * 
  2386. * @since 3.3.0 
  2387. * 
  2388. * @param int $post_id Post ID. 
  2389. */ 
  2390. do_action( 'wp_trash_post', $post_id ); 
  2391.  
  2392. add_post_meta($post_id, '_wp_trash_meta_status', $post['post_status']); 
  2393. add_post_meta($post_id, '_wp_trash_meta_time', time()); 
  2394.  
  2395. $post['post_status'] = 'trash'; 
  2396. wp_insert_post( wp_slash( $post ) ); 
  2397.  
  2398. wp_trash_post_comments($post_id); 
  2399.  
  2400. /** 
  2401. * Fires after a post is sent to the trash. 
  2402. * 
  2403. * @since 2.9.0 
  2404. * 
  2405. * @param int $post_id Post ID. 
  2406. */ 
  2407. do_action( 'trashed_post', $post_id ); 
  2408.  
  2409. return $post; 
  2410.  
  2411. /** 
  2412. * Restore a post or page from the Trash. 
  2413. * 
  2414. * @since 2.9.0 
  2415. * 
  2416. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  2417. * @return WP_Post|false WP_Post object. False on failure. 
  2418. */ 
  2419. function wp_untrash_post( $post_id = 0 ) { 
  2420. if ( !$post = get_post($post_id, ARRAY_A) ) 
  2421. return $post; 
  2422.  
  2423. if ( $post['post_status'] != 'trash' ) 
  2424. return false; 
  2425.  
  2426. /** 
  2427. * Fires before a post is restored from the trash. 
  2428. * 
  2429. * @since 2.9.0 
  2430. * 
  2431. * @param int $post_id Post ID. 
  2432. */ 
  2433. do_action( 'untrash_post', $post_id ); 
  2434.  
  2435. $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true); 
  2436.  
  2437. $post['post_status'] = $post_status; 
  2438.  
  2439. delete_post_meta($post_id, '_wp_trash_meta_status'); 
  2440. delete_post_meta($post_id, '_wp_trash_meta_time'); 
  2441.  
  2442. wp_insert_post( wp_slash( $post ) ); 
  2443.  
  2444. wp_untrash_post_comments($post_id); 
  2445.  
  2446. /** 
  2447. * Fires after a post is restored from the trash. 
  2448. * 
  2449. * @since 2.9.0 
  2450. * 
  2451. * @param int $post_id Post ID. 
  2452. */ 
  2453. do_action( 'untrashed_post', $post_id ); 
  2454.  
  2455. return $post; 
  2456.  
  2457. /** 
  2458. * Moves comments for a post to the trash. 
  2459. * 
  2460. * @since 2.9.0 
  2461. * 
  2462. * @global wpdb $wpdb WordPress database abstraction object. 
  2463. * 
  2464. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 
  2465. * @return mixed|void False on failure. 
  2466. */ 
  2467. function wp_trash_post_comments( $post = null ) { 
  2468. global $wpdb; 
  2469.  
  2470. $post = get_post($post); 
  2471. if ( empty($post) ) 
  2472. return; 
  2473.  
  2474. $post_id = $post->ID; 
  2475.  
  2476. /** 
  2477. * Fires before comments are sent to the trash. 
  2478. * 
  2479. * @since 2.9.0 
  2480. * 
  2481. * @param int $post_id Post ID. 
  2482. */ 
  2483. do_action( 'trash_post_comments', $post_id ); 
  2484.  
  2485. $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) ); 
  2486. if ( empty($comments) ) 
  2487. return; 
  2488.  
  2489. // Cache current status for each comment. 
  2490. $statuses = array(); 
  2491. foreach ( $comments as $comment ) 
  2492. $statuses[$comment->comment_ID] = $comment->comment_approved; 
  2493. add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses); 
  2494.  
  2495. // Set status for all comments to post-trashed. 
  2496. $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id)); 
  2497.  
  2498. clean_comment_cache( array_keys($statuses) ); 
  2499.  
  2500. /** 
  2501. * Fires after comments are sent to the trash. 
  2502. * 
  2503. * @since 2.9.0 
  2504. * 
  2505. * @param int $post_id Post ID. 
  2506. * @param array $statuses Array of comment statuses. 
  2507. */ 
  2508. do_action( 'trashed_post_comments', $post_id, $statuses ); 
  2509.  
  2510. return $result; 
  2511.  
  2512. /** 
  2513. * Restore comments for a post from the trash. 
  2514. * 
  2515. * @since 2.9.0 
  2516. * 
  2517. * @global wpdb $wpdb WordPress database abstraction object. 
  2518. * 
  2519. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 
  2520. * @return true|void 
  2521. */ 
  2522. function wp_untrash_post_comments( $post = null ) { 
  2523. global $wpdb; 
  2524.  
  2525. $post = get_post($post); 
  2526. if ( empty($post) ) 
  2527. return; 
  2528.  
  2529. $post_id = $post->ID; 
  2530.  
  2531. $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true); 
  2532.  
  2533. if ( empty($statuses) ) 
  2534. return true; 
  2535.  
  2536. /** 
  2537. * Fires before comments are restored for a post from the trash. 
  2538. * 
  2539. * @since 2.9.0 
  2540. * 
  2541. * @param int $post_id Post ID. 
  2542. */ 
  2543. do_action( 'untrash_post_comments', $post_id ); 
  2544.  
  2545. // Restore each comment to its original status. 
  2546. $group_by_status = array(); 
  2547. foreach ( $statuses as $comment_id => $comment_status ) 
  2548. $group_by_status[$comment_status][] = $comment_id; 
  2549.  
  2550. foreach ( $group_by_status as $status => $comments ) { 
  2551. // Sanity check. This shouldn't happen. 
  2552. if ( 'post-trashed' == $status ) { 
  2553. $status = '0'; 
  2554. $comments_in = implode( ', ', array_map( 'intval', $comments ) ); 
  2555. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); 
  2556.  
  2557. clean_comment_cache( array_keys($statuses) ); 
  2558.  
  2559. delete_post_meta($post_id, '_wp_trash_meta_comments_status'); 
  2560.  
  2561. /** 
  2562. * Fires after comments are restored for a post from the trash. 
  2563. * 
  2564. * @since 2.9.0 
  2565. * 
  2566. * @param int $post_id Post ID. 
  2567. */ 
  2568. do_action( 'untrashed_post_comments', $post_id ); 
  2569.  
  2570. /** 
  2571. * Retrieve the list of categories for a post. 
  2572. * 
  2573. * Compatibility layer for themes and plugins. Also an easy layer of abstraction 
  2574. * away from the complexity of the taxonomy layer. 
  2575. * 
  2576. * @since 2.1.0 
  2577. * 
  2578. * @see wp_get_object_terms() 
  2579. * 
  2580. * @param int $post_id Optional. The Post ID. Does not default to the ID of the 
  2581. * global $post. Default 0. 
  2582. * @param array $args Optional. Category arguments. See wp_get_object_terms(). Default empty. 
  2583. * @return array List of categories. If the `$fields` argument passed via `$args` is 'all' or 
  2584. * 'all_with_object_id', an array of WP_Term objects will be returned. If `$fields` 
  2585. * is 'ids', an array of category ids. If `$fields` is 'names', an array of category names. 
  2586. */ 
  2587. function wp_get_post_categories( $post_id = 0, $args = array() ) { 
  2588. $post_id = (int) $post_id; 
  2589.  
  2590. $defaults = array('fields' => 'ids'); 
  2591. $args = wp_parse_args( $args, $defaults ); 
  2592.  
  2593. $cats = wp_get_object_terms($post_id, 'category', $args); 
  2594. return $cats; 
  2595.  
  2596. /** 
  2597. * Retrieve the tags for a post. 
  2598. * 
  2599. * There is only one default for this function, called 'fields' and by default 
  2600. * is set to 'all'. There are other defaults that can be overridden in 
  2601. * wp_get_object_terms(). 
  2602. * 
  2603. * @since 2.3.0 
  2604. * 
  2605. * @param int $post_id Optional. The Post ID. Does not default to the ID of the 
  2606. * global $post. Default 0. 
  2607. * @param array $args Optional. Overwrite the defaults 
  2608. * @return array List of post tags. 
  2609. */ 
  2610. function wp_get_post_tags( $post_id = 0, $args = array() ) { 
  2611. return wp_get_post_terms( $post_id, 'post_tag', $args); 
  2612.  
  2613. /** 
  2614. * Retrieve the terms for a post. 
  2615. * 
  2616. * There is only one default for this function, called 'fields' and by default 
  2617. * is set to 'all'. There are other defaults that can be overridden in 
  2618. * wp_get_object_terms(). 
  2619. * 
  2620. * @since 2.8.0 
  2621. * 
  2622. * @param int $post_id Optional. The Post ID. Does not default to the ID of the 
  2623. * global $post. Default 0. 
  2624. * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. 
  2625. * @param array $args Optional. wp_get_object_terms() arguments. Default empty array. 
  2626. * @return array|WP_Error List of post terms or empty array if no terms were found. WP_Error object 
  2627. * if `$taxonomy` doesn't exist. 
  2628. */ 
  2629. function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { 
  2630. $post_id = (int) $post_id; 
  2631.  
  2632. $defaults = array('fields' => 'all'); 
  2633. $args = wp_parse_args( $args, $defaults ); 
  2634.  
  2635. $tags = wp_get_object_terms($post_id, $taxonomy, $args); 
  2636.  
  2637. return $tags; 
  2638.  
  2639. /** 
  2640. * Retrieve a number of recent posts. 
  2641. * 
  2642. * @since 1.0.0 
  2643. * 
  2644. * @see get_posts() 
  2645. * 
  2646. * @param array $args Optional. Arguments to retrieve posts. Default empty array. 
  2647. * @param string $output Optional. Type of output. Accepts ARRAY_A or ''. Default ARRAY_A. 
  2648. * @return array|false Associative array if $output equals ARRAY_A, array or false if no results. 
  2649. */ 
  2650. function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { 
  2651.  
  2652. if ( is_numeric( $args ) ) { 
  2653. _deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); 
  2654. $args = array( 'numberposts' => absint( $args ) ); 
  2655.  
  2656. // Set default arguments. 
  2657. $defaults = array( 
  2658. 'numberposts' => 10, 'offset' => 0,  
  2659. 'category' => 0, 'orderby' => 'post_date',  
  2660. 'order' => 'DESC', 'include' => '',  
  2661. 'exclude' => '', 'meta_key' => '',  
  2662. 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',  
  2663. 'suppress_filters' => true 
  2664. ); 
  2665.  
  2666. $r = wp_parse_args( $args, $defaults ); 
  2667.  
  2668. $results = get_posts( $r ); 
  2669.  
  2670. // Backward compatibility. Prior to 3.1 expected posts to be returned in array. 
  2671. if ( ARRAY_A == $output ) { 
  2672. foreach ( $results as $key => $result ) { 
  2673. $results[$key] = get_object_vars( $result ); 
  2674. return $results ? $results : array(); 
  2675.  
  2676. return $results ? $results : false; 
  2677.  
  2678.  
  2679. /** 
  2680. * Insert or update a post. 
  2681. * 
  2682. * If the $postarr parameter has 'ID' set to a value, then post will be updated. 
  2683. * 
  2684. * You can set the post date manually, by setting the values for 'post_date' 
  2685. * and 'post_date_gmt' keys. You can close the comments or open the comments by 
  2686. * setting the value for 'comment_status' key. 
  2687. * 
  2688. * @since 1.0.0 
  2689. * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt. 
  2690. * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data. 
  2691. * 
  2692. * @see sanitize_post() 
  2693. * @global wpdb $wpdb WordPress database abstraction object. 
  2694. * 
  2695. * @param array $postarr { 
  2696. * An array of elements that make up a post to update or insert. 
  2697. * 
  2698. * @type int $ID The post ID. If equal to something other than 0,  
  2699. * the post with that ID will be updated. Default 0. 
  2700. * @type int $post_author The ID of the user who added the post. Default is 
  2701. * the current user ID. 
  2702. * @type string $post_date The date of the post. Default is the current time. 
  2703. * @type string $post_date_gmt The date of the post in the GMT timezone. Default is 
  2704. * the value of `$post_date`. 
  2705. * @type mixed $post_content The post content. Default empty. 
  2706. * @type string $post_content_filtered The filtered post content. Default empty. 
  2707. * @type string $post_title The post title. Default empty. 
  2708. * @type string $post_excerpt The post excerpt. Default empty. 
  2709. * @type string $post_status The post status. Default 'draft'. 
  2710. * @type string $post_type The post type. Default 'post'. 
  2711. * @type string $comment_status Whether the post can accept comments. Accepts 'open' or 'closed'. 
  2712. * Default is the value of 'default_comment_status' option. 
  2713. * @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'. 
  2714. * Default is the value of 'default_ping_status' option. 
  2715. * @type string $post_password The password to access the post. Default empty. 
  2716. * @type string $post_name The post name. Default is the sanitized post title 
  2717. * when creating a new post. 
  2718. * @type string $to_ping Space or carriage return-separated list of URLs to ping. 
  2719. * Default empty. 
  2720. * @type string $pinged Space or carriage return-separated list of URLs that have 
  2721. * been pinged. Default empty. 
  2722. * @type string $post_modified The date when the post was last modified. Default is 
  2723. * the current time. 
  2724. * @type string $post_modified_gmt The date when the post was last modified in the GMT 
  2725. * timezone. Default is the current time. 
  2726. * @type int $post_parent Set this for the post it belongs to, if any. Default 0. 
  2727. * @type int $menu_order The order the post should be displayed in. Default 0. 
  2728. * @type string $post_mime_type The mime type of the post. Default empty. 
  2729. * @type string $guid Global Unique ID for referencing the post. Default empty. 
  2730. * @type array $post_category Array of category names, slugs, or IDs. 
  2731. * Defaults to value of the 'default_category' option. 
  2732. * @type array $tax_input Array of taxonomy terms keyed by their taxonomy name. Default empty. 
  2733. * @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. 
  2734. * } 
  2735. * @param bool $wp_error Optional. Whether to allow return of WP_Error on failure. Default false. 
  2736. * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. 
  2737. */ 
  2738. function wp_insert_post( $postarr, $wp_error = false ) { 
  2739. global $wpdb; 
  2740.  
  2741. $user_id = get_current_user_id(); 
  2742.  
  2743. $defaults = array( 
  2744. 'post_author' => $user_id,  
  2745. 'post_content' => '',  
  2746. 'post_content_filtered' => '',  
  2747. 'post_title' => '',  
  2748. 'post_excerpt' => '',  
  2749. 'post_status' => 'draft',  
  2750. 'post_type' => 'post',  
  2751. 'comment_status' => '',  
  2752. 'ping_status' => '',  
  2753. 'post_password' => '',  
  2754. 'to_ping' => '',  
  2755. 'pinged' => '',  
  2756. 'post_parent' => 0,  
  2757. 'menu_order' => 0,  
  2758. 'guid' => '',  
  2759. 'import_id' => 0,  
  2760. 'context' => '',  
  2761. ); 
  2762.  
  2763. $postarr = wp_parse_args($postarr, $defaults); 
  2764.  
  2765. unset( $postarr[ 'filter' ] ); 
  2766.  
  2767. $postarr = sanitize_post($postarr, 'db'); 
  2768.  
  2769. // Are we updating or creating? 
  2770. $post_ID = 0; 
  2771. $update = false; 
  2772. $guid = $postarr['guid']; 
  2773.  
  2774. if ( ! empty( $postarr['ID'] ) ) { 
  2775. $update = true; 
  2776.  
  2777. // Get the post ID and GUID. 
  2778. $post_ID = $postarr['ID']; 
  2779. $post_before = get_post( $post_ID ); 
  2780. if ( is_null( $post_before ) ) { 
  2781. if ( $wp_error ) { 
  2782. return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); 
  2783. return 0; 
  2784.  
  2785. $guid = get_post_field( 'guid', $post_ID ); 
  2786. $previous_status = get_post_field('post_status', $post_ID ); 
  2787. } else { 
  2788. $previous_status = 'new'; 
  2789.  
  2790. $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type']; 
  2791.  
  2792. $post_title = $postarr['post_title']; 
  2793. $post_content = $postarr['post_content']; 
  2794. $post_excerpt = $postarr['post_excerpt']; 
  2795. if ( isset( $postarr['post_name'] ) ) { 
  2796. $post_name = $postarr['post_name']; 
  2797. } elseif ( $update ) { 
  2798. // For an update, don't modify the post_name if it wasn't supplied as an argument. 
  2799. $post_name = $post_before->post_name; 
  2800.  
  2801. $maybe_empty = 'attachment' !== $post_type 
  2802. && ! $post_content && ! $post_title && ! $post_excerpt 
  2803. && post_type_supports( $post_type, 'editor' ) 
  2804. && post_type_supports( $post_type, 'title' ) 
  2805. && post_type_supports( $post_type, 'excerpt' ); 
  2806.  
  2807. /** 
  2808. * Filters whether the post should be considered "empty". 
  2809. * 
  2810. * The post is considered "empty" if both: 
  2811. * 1. The post type supports the title, editor, and excerpt fields 
  2812. * 2. The title, editor, and excerpt fields are all empty 
  2813. * 
  2814. * Returning a truthy value to the filter will effectively short-circuit 
  2815. * the new post being inserted, returning 0. If $wp_error is true, a WP_Error 
  2816. * will be returned instead. 
  2817. * 
  2818. * @since 3.3.0 
  2819. * 
  2820. * @param bool $maybe_empty Whether the post should be considered "empty". 
  2821. * @param array $postarr Array of post data. 
  2822. */ 
  2823. if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) { 
  2824. if ( $wp_error ) { 
  2825. return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) ); 
  2826. } else { 
  2827. return 0; 
  2828.  
  2829. $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status']; 
  2830. if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash' ) ) ) { 
  2831. $post_status = 'inherit'; 
  2832.  
  2833. if ( ! empty( $postarr['post_category'] ) ) { 
  2834. // Filter out empty terms. 
  2835. $post_category = array_filter( $postarr['post_category'] ); 
  2836.  
  2837. // Make sure we set a valid category. 
  2838. if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) { 
  2839. // 'post' requires at least one category. 
  2840. if ( 'post' == $post_type && 'auto-draft' != $post_status ) { 
  2841. $post_category = array( get_option('default_category') ); 
  2842. } else { 
  2843. $post_category = array(); 
  2844.  
  2845. // Don't allow contributors to set the post slug for pending review posts. 
  2846. if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) ) { 
  2847. $post_name = ''; 
  2848.  
  2849. /** 
  2850. * Create a valid post name. Drafts and pending posts are allowed to have 
  2851. * an empty post name. 
  2852. */ 
  2853. if ( empty($post_name) ) { 
  2854. if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { 
  2855. $post_name = sanitize_title($post_title); 
  2856. } else { 
  2857. $post_name = ''; 
  2858. } else { 
  2859. // On updates, we need to check to see if it's using the old, fixed sanitization context. 
  2860. $check_name = sanitize_title( $post_name, '', 'old-save' ); 
  2861. if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) { 
  2862. $post_name = $check_name; 
  2863. } else { // new post, or slug has changed. 
  2864. $post_name = sanitize_title($post_name); 
  2865.  
  2866. /** 
  2867. * If the post date is empty (due to having been new or a draft) and status 
  2868. * is not 'draft' or 'pending', set date to now. 
  2869. */ 
  2870. if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) { 
  2871. if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) { 
  2872. $post_date = current_time( 'mysql' ); 
  2873. } else { 
  2874. $post_date = get_date_from_gmt( $postarr['post_date_gmt'] ); 
  2875. } else { 
  2876. $post_date = $postarr['post_date']; 
  2877.  
  2878. // Validate the date. 
  2879. $mm = substr( $post_date, 5, 2 ); 
  2880. $jj = substr( $post_date, 8, 2 ); 
  2881. $aa = substr( $post_date, 0, 4 ); 
  2882. $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date ); 
  2883. if ( ! $valid_date ) { 
  2884. if ( $wp_error ) { 
  2885. return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) ); 
  2886. } else { 
  2887. return 0; 
  2888.  
  2889. if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) { 
  2890. if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { 
  2891. $post_date_gmt = get_gmt_from_date( $post_date ); 
  2892. } else { 
  2893. $post_date_gmt = '0000-00-00 00:00:00'; 
  2894. } else { 
  2895. $post_date_gmt = $postarr['post_date_gmt']; 
  2896.  
  2897. if ( $update || '0000-00-00 00:00:00' == $post_date ) { 
  2898. $post_modified = current_time( 'mysql' ); 
  2899. $post_modified_gmt = current_time( 'mysql', 1 ); 
  2900. } else { 
  2901. $post_modified = $post_date; 
  2902. $post_modified_gmt = $post_date_gmt; 
  2903.  
  2904. if ( 'attachment' !== $post_type ) { 
  2905. if ( 'publish' == $post_status ) { 
  2906. $now = gmdate('Y-m-d H:i:59'); 
  2907. if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) { 
  2908. $post_status = 'future'; 
  2909. } elseif ( 'future' == $post_status ) { 
  2910. $now = gmdate('Y-m-d H:i:59'); 
  2911. if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) { 
  2912. $post_status = 'publish'; 
  2913.  
  2914. // Comment status. 
  2915. if ( empty( $postarr['comment_status'] ) ) { 
  2916. if ( $update ) { 
  2917. $comment_status = 'closed'; 
  2918. } else { 
  2919. $comment_status = get_default_comment_status( $post_type ); 
  2920. } else { 
  2921. $comment_status = $postarr['comment_status']; 
  2922.  
  2923. // These variables are needed by compact() later. 
  2924. $post_content_filtered = $postarr['post_content_filtered']; 
  2925. $post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id; 
  2926. $ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status']; 
  2927. $to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : ''; 
  2928. $pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : ''; 
  2929. $import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0; 
  2930.  
  2931. /** 
  2932. * The 'wp_insert_post_parent' filter expects all variables to be present. 
  2933. * Previously, these variables would have already been extracted 
  2934. */ 
  2935. if ( isset( $postarr['menu_order'] ) ) { 
  2936. $menu_order = (int) $postarr['menu_order']; 
  2937. } else { 
  2938. $menu_order = 0; 
  2939.  
  2940. $post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : ''; 
  2941. if ( 'private' == $post_status ) { 
  2942. $post_password = ''; 
  2943.  
  2944. if ( isset( $postarr['post_parent'] ) ) { 
  2945. $post_parent = (int) $postarr['post_parent']; 
  2946. } else { 
  2947. $post_parent = 0; 
  2948.  
  2949. /** 
  2950. * Filters the post parent -- used to check for and prevent hierarchy loops. 
  2951. * 
  2952. * @since 3.1.0 
  2953. * 
  2954. * @param int $post_parent Post parent ID. 
  2955. * @param int $post_ID Post ID. 
  2956. * @param array $new_postarr Array of parsed post data. 
  2957. * @param array $postarr Array of sanitized, but otherwise unmodified post data. 
  2958. */ 
  2959. $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr ); 
  2960.  
  2961. /** 
  2962. * If the post is being untrashed and it has a desired slug stored in post meta,  
  2963. * reassign it. 
  2964. */ 
  2965. if ( 'trash' === $previous_status && 'trash' !== $post_status ) { 
  2966. $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true ); 
  2967. if ( $desired_post_slug ) { 
  2968. delete_post_meta( $post_ID, '_wp_desired_post_slug' ); 
  2969. $post_name = $desired_post_slug; 
  2970.  
  2971. // If a trashed post has the desired slug, change it and let this post have it. 
  2972. if ( 'trash' !== $post_status && $post_name ) { 
  2973. wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID ); 
  2974.  
  2975. // When trashing an existing post, change its slug to allow non-trashed posts to use it. 
  2976. if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) { 
  2977. $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID ); 
  2978.  
  2979. $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); 
  2980.  
  2981. // Don't unslash. 
  2982. $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; 
  2983.  
  2984. // Expected_slashed (everything!). 
  2985. $data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ); 
  2986.  
  2987. $emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' ); 
  2988.  
  2989. foreach ( $emoji_fields as $emoji_field ) { 
  2990. if ( isset( $data[ $emoji_field ] ) ) { 
  2991. $charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field ); 
  2992. if ( 'utf8' === $charset ) { 
  2993. $data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] ); 
  2994.  
  2995. if ( 'attachment' === $post_type ) { 
  2996. /** 
  2997. * Filters attachment post data before it is updated in or added to the database. 
  2998. * 
  2999. * @since 3.9.0 
  3000. * 
  3001. * @param array $data An array of sanitized attachment post data. 
  3002. * @param array $postarr An array of unsanitized attachment post data. 
  3003. */ 
  3004. $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr ); 
  3005. } else { 
  3006. /** 
  3007. * Filters slashed post data just before it is inserted into the database. 
  3008. * 
  3009. * @since 2.7.0 
  3010. * 
  3011. * @param array $data An array of slashed post data. 
  3012. * @param array $postarr An array of sanitized, but otherwise unmodified post data. 
  3013. */ 
  3014. $data = apply_filters( 'wp_insert_post_data', $data, $postarr ); 
  3015. $data = wp_unslash( $data ); 
  3016. $where = array( 'ID' => $post_ID ); 
  3017.  
  3018. if ( $update ) { 
  3019. /** 
  3020. * Fires immediately before an existing post is updated in the database. 
  3021. * 
  3022. * @since 2.5.0 
  3023. * 
  3024. * @param int $post_ID Post ID. 
  3025. * @param array $data Array of unslashed post data. 
  3026. */ 
  3027. do_action( 'pre_post_update', $post_ID, $data ); 
  3028. if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { 
  3029. if ( $wp_error ) { 
  3030. return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error); 
  3031. } else { 
  3032. return 0; 
  3033. } else { 
  3034. // If there is a suggested ID, use it if not already present. 
  3035. if ( ! empty( $import_id ) ) { 
  3036. $import_id = (int) $import_id; 
  3037. if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) { 
  3038. $data['ID'] = $import_id; 
  3039. if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { 
  3040. if ( $wp_error ) { 
  3041. return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error); 
  3042. } else { 
  3043. return 0; 
  3044. $post_ID = (int) $wpdb->insert_id; 
  3045.  
  3046. // Use the newly generated $post_ID. 
  3047. $where = array( 'ID' => $post_ID ); 
  3048.  
  3049. if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { 
  3050. $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); 
  3051. $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); 
  3052. clean_post_cache( $post_ID ); 
  3053.  
  3054. if ( is_object_in_taxonomy( $post_type, 'category' ) ) { 
  3055. wp_set_post_categories( $post_ID, $post_category ); 
  3056.  
  3057. if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { 
  3058. wp_set_post_tags( $post_ID, $postarr['tags_input'] ); 
  3059.  
  3060. // New-style support for all custom taxonomies. 
  3061. if ( ! empty( $postarr['tax_input'] ) ) { 
  3062. foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { 
  3063. $taxonomy_obj = get_taxonomy($taxonomy); 
  3064. if ( ! $taxonomy_obj ) { 
  3065. /** translators: %s: taxonomy name */ 
  3066. _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid taxonomy: %s.' ), $taxonomy ), '4.4.0' ); 
  3067. continue; 
  3068.  
  3069. // array = hierarchical, string = non-hierarchical. 
  3070. if ( is_array( $tags ) ) { 
  3071. $tags = array_filter($tags); 
  3072. if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { 
  3073. wp_set_post_terms( $post_ID, $tags, $taxonomy ); 
  3074.  
  3075. if ( ! empty( $postarr['meta_input'] ) ) { 
  3076. foreach ( $postarr['meta_input'] as $field => $value ) { 
  3077. update_post_meta( $post_ID, $field, $value ); 
  3078.  
  3079. $current_guid = get_post_field( 'guid', $post_ID ); 
  3080.  
  3081. // Set GUID. 
  3082. if ( ! $update && '' == $current_guid ) { 
  3083. $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); 
  3084.  
  3085. if ( 'attachment' === $postarr['post_type'] ) { 
  3086. if ( ! empty( $postarr['file'] ) ) { 
  3087. update_attached_file( $post_ID, $postarr['file'] ); 
  3088.  
  3089. if ( ! empty( $postarr['context'] ) ) { 
  3090. add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true ); 
  3091.  
  3092. // Set or remove featured image. 
  3093. if ( isset( $postarr['_thumbnail_id'] ) ) { 
  3094. $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type; 
  3095. if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) { 
  3096. if ( wp_attachment_is( 'audio', $post_ID ) ) { 
  3097. $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); 
  3098. } elseif ( wp_attachment_is( 'video', $post_ID ) ) { 
  3099. $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); 
  3100.  
  3101. if ( $thumbnail_support ) { 
  3102. $thumbnail_id = intval( $postarr['_thumbnail_id'] ); 
  3103. if ( -1 === $thumbnail_id ) { 
  3104. delete_post_thumbnail( $post_ID ); 
  3105. } else { 
  3106. set_post_thumbnail( $post_ID, $thumbnail_id ); 
  3107.  
  3108. clean_post_cache( $post_ID ); 
  3109.  
  3110. $post = get_post( $post_ID ); 
  3111.  
  3112. if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) { 
  3113. $post->page_template = $postarr['page_template']; 
  3114. $page_templates = wp_get_theme()->get_page_templates( $post ); 
  3115. if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { 
  3116. if ( $wp_error ) { 
  3117. return new WP_Error('invalid_page_template', __('The page template is invalid.')); 
  3118. update_post_meta( $post_ID, '_wp_page_template', 'default' ); 
  3119. } else { 
  3120. update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] ); 
  3121.  
  3122. if ( 'attachment' !== $postarr['post_type'] ) { 
  3123. wp_transition_post_status( $data['post_status'], $previous_status, $post ); 
  3124. } else { 
  3125. if ( $update ) { 
  3126. /** 
  3127. * Fires once an existing attachment has been updated. 
  3128. * 
  3129. * @since 2.0.0 
  3130. * 
  3131. * @param int $post_ID Attachment ID. 
  3132. */ 
  3133. do_action( 'edit_attachment', $post_ID ); 
  3134. $post_after = get_post( $post_ID ); 
  3135.  
  3136. /** 
  3137. * Fires once an existing attachment has been updated. 
  3138. * 
  3139. * @since 4.4.0 
  3140. * 
  3141. * @param int $post_ID Post ID. 
  3142. * @param WP_Post $post_after Post object following the update. 
  3143. * @param WP_Post $post_before Post object before the update. 
  3144. */ 
  3145. do_action( 'attachment_updated', $post_ID, $post_after, $post_before ); 
  3146. } else { 
  3147.  
  3148. /** 
  3149. * Fires once an attachment has been added. 
  3150. * 
  3151. * @since 2.0.0 
  3152. * 
  3153. * @param int $post_ID Attachment ID. 
  3154. */ 
  3155. do_action( 'add_attachment', $post_ID ); 
  3156.  
  3157. return $post_ID; 
  3158.  
  3159. if ( $update ) { 
  3160. /** 
  3161. * Fires once an existing post has been updated. 
  3162. * 
  3163. * @since 1.2.0 
  3164. * 
  3165. * @param int $post_ID Post ID. 
  3166. * @param WP_Post $post Post object. 
  3167. */ 
  3168. do_action( 'edit_post', $post_ID, $post ); 
  3169. $post_after = get_post($post_ID); 
  3170.  
  3171. /** 
  3172. * Fires once an existing post has been updated. 
  3173. * 
  3174. * @since 3.0.0 
  3175. * 
  3176. * @param int $post_ID Post ID. 
  3177. * @param WP_Post $post_after Post object following the update. 
  3178. * @param WP_Post $post_before Post object before the update. 
  3179. */ 
  3180. do_action( 'post_updated', $post_ID, $post_after, $post_before); 
  3181.  
  3182. /** 
  3183. * Fires once a post has been saved. 
  3184. * 
  3185. * The dynamic portion of the hook name, `$post->post_type`, refers to 
  3186. * the post type slug. 
  3187. * 
  3188. * @since 3.7.0 
  3189. * 
  3190. * @param int $post_ID Post ID. 
  3191. * @param WP_Post $post Post object. 
  3192. * @param bool $update Whether this is an existing post being updated or not. 
  3193. */ 
  3194. do_action( "save_post_{$post->post_type}", $post_ID, $post, $update ); 
  3195.  
  3196. /** 
  3197. * Fires once a post has been saved. 
  3198. * 
  3199. * @since 1.5.0 
  3200. * 
  3201. * @param int $post_ID Post ID. 
  3202. * @param WP_Post $post Post object. 
  3203. * @param bool $update Whether this is an existing post being updated or not. 
  3204. */ 
  3205. do_action( 'save_post', $post_ID, $post, $update ); 
  3206.  
  3207. /** 
  3208. * Fires once a post has been saved. 
  3209. * 
  3210. * @since 2.0.0 
  3211. * 
  3212. * @param int $post_ID Post ID. 
  3213. * @param WP_Post $post Post object. 
  3214. * @param bool $update Whether this is an existing post being updated or not. 
  3215. */ 
  3216. do_action( 'wp_insert_post', $post_ID, $post, $update ); 
  3217.  
  3218. return $post_ID; 
  3219.  
  3220. /** 
  3221. * Update a post with new post data. 
  3222. * 
  3223. * The date does not have to be set for drafts. You can set the date and it will 
  3224. * not be overridden. 
  3225. * 
  3226. * @since 1.0.0 
  3227. * 
  3228. * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped,  
  3229. * objects are not. Default array. 
  3230. * @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false. 
  3231. * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. 
  3232. */ 
  3233. function wp_update_post( $postarr = array(), $wp_error = false ) { 
  3234. if ( is_object($postarr) ) { 
  3235. // Non-escaped post was passed. 
  3236. $postarr = get_object_vars($postarr); 
  3237. $postarr = wp_slash($postarr); 
  3238.  
  3239. // First, get all of the original fields. 
  3240. $post = get_post($postarr['ID'], ARRAY_A); 
  3241.  
  3242. if ( is_null( $post ) ) { 
  3243. if ( $wp_error ) 
  3244. return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); 
  3245. return 0; 
  3246.  
  3247. // Escape data pulled from DB. 
  3248. $post = wp_slash($post); 
  3249.  
  3250. // Passed post category list overwrites existing category list if not empty. 
  3251. if ( isset($postarr['post_category']) && is_array($postarr['post_category']) 
  3252. && 0 != count($postarr['post_category']) ) 
  3253. $post_cats = $postarr['post_category']; 
  3254. else 
  3255. $post_cats = $post['post_category']; 
  3256.  
  3257. // Drafts shouldn't be assigned a date unless explicitly done so by the user. 
  3258. if ( isset( $post['post_status'] ) && in_array($post['post_status'], array('draft', 'pending', 'auto-draft')) && empty($postarr['edit_date']) && 
  3259. ('0000-00-00 00:00:00' == $post['post_date_gmt']) ) 
  3260. $clear_date = true; 
  3261. else 
  3262. $clear_date = false; 
  3263.  
  3264. // Merge old and new fields with new fields overwriting old ones. 
  3265. $postarr = array_merge($post, $postarr); 
  3266. $postarr['post_category'] = $post_cats; 
  3267. if ( $clear_date ) { 
  3268. $postarr['post_date'] = current_time('mysql'); 
  3269. $postarr['post_date_gmt'] = ''; 
  3270.  
  3271. if ($postarr['post_type'] == 'attachment') 
  3272. return wp_insert_attachment($postarr); 
  3273.  
  3274. return wp_insert_post( $postarr, $wp_error ); 
  3275.  
  3276. /** 
  3277. * Publish a post by transitioning the post status. 
  3278. * 
  3279. * @since 2.1.0 
  3280. * 
  3281. * @global wpdb $wpdb WordPress database abstraction object. 
  3282. * 
  3283. * @param int|WP_Post $post Post ID or post object. 
  3284. */ 
  3285. function wp_publish_post( $post ) { 
  3286. global $wpdb; 
  3287.  
  3288. if ( ! $post = get_post( $post ) ) 
  3289. return; 
  3290.  
  3291. if ( 'publish' == $post->post_status ) 
  3292. return; 
  3293.  
  3294. $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) ); 
  3295.  
  3296. clean_post_cache( $post->ID ); 
  3297.  
  3298. $old_status = $post->post_status; 
  3299. $post->post_status = 'publish'; 
  3300. wp_transition_post_status( 'publish', $old_status, $post ); 
  3301.  
  3302. /** This action is documented in wp-includes/post.php */ 
  3303. do_action( 'edit_post', $post->ID, $post ); 
  3304.  
  3305. /** This action is documented in wp-includes/post.php */ 
  3306. do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); 
  3307.  
  3308. /** This action is documented in wp-includes/post.php */ 
  3309. do_action( 'save_post', $post->ID, $post, true ); 
  3310.  
  3311. /** This action is documented in wp-includes/post.php */ 
  3312. do_action( 'wp_insert_post', $post->ID, $post, true ); 
  3313.  
  3314. /** 
  3315. * Publish future post and make sure post ID has future post status. 
  3316. * 
  3317. * Invoked by cron 'publish_future_post' event. This safeguard prevents cron 
  3318. * from publishing drafts, etc. 
  3319. * 
  3320. * @since 2.5.0 
  3321. * 
  3322. * @param int|WP_Post $post_id Post ID or post object. 
  3323. */ 
  3324. function check_and_publish_future_post( $post_id ) { 
  3325. $post = get_post($post_id); 
  3326.  
  3327. if ( empty($post) ) 
  3328. return; 
  3329.  
  3330. if ( 'future' != $post->post_status ) 
  3331. return; 
  3332.  
  3333. $time = strtotime( $post->post_date_gmt . ' GMT' ); 
  3334.  
  3335. // Uh oh, someone jumped the gun! 
  3336. if ( $time > time() ) { 
  3337. wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // clear anything else in the system 
  3338. wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); 
  3339. return; 
  3340.  
  3341. // wp_publish_post() returns no meaningful value. 
  3342. wp_publish_post( $post_id ); 
  3343.  
  3344. /** 
  3345. * Computes a unique slug for the post, when given the desired slug and some post details. 
  3346. * 
  3347. * @since 2.8.0 
  3348. * 
  3349. * @global wpdb $wpdb WordPress database abstraction object. 
  3350. * @global WP_Rewrite $wp_rewrite 
  3351. * 
  3352. * @param string $slug The desired slug (post_name). 
  3353. * @param int $post_ID Post ID. 
  3354. * @param string $post_status No uniqueness checks are made if the post is still draft or pending. 
  3355. * @param string $post_type Post type. 
  3356. * @param int $post_parent Post parent ID. 
  3357. * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) 
  3358. */ 
  3359. function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { 
  3360. if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) ) 
  3361. return $slug; 
  3362.  
  3363. global $wpdb, $wp_rewrite; 
  3364.  
  3365. $original_slug = $slug; 
  3366.  
  3367. $feeds = $wp_rewrite->feeds; 
  3368. if ( ! is_array( $feeds ) ) 
  3369. $feeds = array(); 
  3370.  
  3371. if ( 'attachment' == $post_type ) { 
  3372. // Attachment slugs must be unique across all types. 
  3373. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; 
  3374. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); 
  3375.  
  3376. /** 
  3377. * Filters whether the post slug would make a bad attachment slug. 
  3378. * 
  3379. * @since 3.1.0 
  3380. * 
  3381. * @param bool $bad_slug Whether the slug would be bad as an attachment slug. 
  3382. * @param string $slug The post slug. 
  3383. */ 
  3384. if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) { 
  3385. $suffix = 2; 
  3386. do { 
  3387. $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 
  3388. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); 
  3389. $suffix++; 
  3390. } while ( $post_name_check ); 
  3391. $slug = $alt_post_name; 
  3392. } elseif ( is_post_type_hierarchical( $post_type ) ) { 
  3393. if ( 'nav_menu_item' == $post_type ) 
  3394. return $slug; 
  3395.  
  3396. /** 
  3397. * Page slugs must be unique within their own trees. Pages are in a separate 
  3398. * namespace than posts so page slugs are allowed to overlap post slugs. 
  3399. */ 
  3400. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1"; 
  3401. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); 
  3402.  
  3403. /** 
  3404. * Filters whether the post slug would make a bad hierarchical post slug. 
  3405. * 
  3406. * @since 3.1.0 
  3407. * 
  3408. * @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context. 
  3409. * @param string $slug The post slug. 
  3410. * @param string $post_type Post type. 
  3411. * @param int $post_parent Post parent ID. 
  3412. */ 
  3413. if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) { 
  3414. $suffix = 2; 
  3415. do { 
  3416. $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 
  3417. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); 
  3418. $suffix++; 
  3419. } while ( $post_name_check ); 
  3420. $slug = $alt_post_name; 
  3421. } else { 
  3422. // Post slugs must be unique across all posts. 
  3423. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; 
  3424. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); 
  3425.  
  3426. // Prevent new post slugs that could result in URLs that conflict with date archives. 
  3427. $post = get_post( $post_ID ); 
  3428. $conflicts_with_date_archive = false; 
  3429. if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) { 
  3430. $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); 
  3431. $postname_index = array_search( '%postname%', $permastructs ); 
  3432.  
  3433. /** 
  3434. * Potential date clashes are as follows: 
  3435. * 
  3436. * - Any integer in the first permastruct position could be a year. 
  3437. * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'. 
  3438. * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'. 
  3439. */ 
  3440. if ( 0 === $postname_index || 
  3441. ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) || 
  3442. ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num ) 
  3443. ) { 
  3444. $conflicts_with_date_archive = true; 
  3445.  
  3446. /** 
  3447. * Filters whether the post slug would be bad as a flat slug. 
  3448. * 
  3449. * @since 3.1.0 
  3450. * 
  3451. * @param bool $bad_slug Whether the post slug would be bad as a flat slug. 
  3452. * @param string $slug The post slug. 
  3453. * @param string $post_type Post type. 
  3454. */ 
  3455. if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) { 
  3456. $suffix = 2; 
  3457. do { 
  3458. $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 
  3459. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); 
  3460. $suffix++; 
  3461. } while ( $post_name_check ); 
  3462. $slug = $alt_post_name; 
  3463.  
  3464. /** 
  3465. * Filters the unique post slug. 
  3466. * 
  3467. * @since 3.3.0 
  3468. * 
  3469. * @param string $slug The post slug. 
  3470. * @param int $post_ID Post ID. 
  3471. * @param string $post_status The post status. 
  3472. * @param string $post_type Post type. 
  3473. * @param int $post_parent Post parent ID 
  3474. * @param string $original_slug The original post slug. 
  3475. */ 
  3476. return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ); 
  3477.  
  3478. /** 
  3479. * Truncate a post slug. 
  3480. * 
  3481. * @since 3.6.0 
  3482. * @access private 
  3483. * 
  3484. * @see utf8_uri_encode() 
  3485. * 
  3486. * @param string $slug The slug to truncate. 
  3487. * @param int $length Optional. Max length of the slug. Default 200 (characters). 
  3488. * @return string The truncated slug. 
  3489. */ 
  3490. function _truncate_post_slug( $slug, $length = 200 ) { 
  3491. if ( strlen( $slug ) > $length ) { 
  3492. $decoded_slug = urldecode( $slug ); 
  3493. if ( $decoded_slug === $slug ) 
  3494. $slug = substr( $slug, 0, $length ); 
  3495. else 
  3496. $slug = utf8_uri_encode( $decoded_slug, $length ); 
  3497.  
  3498. return rtrim( $slug, '-' ); 
  3499.  
  3500. /** 
  3501. * Add tags to a post. 
  3502. * 
  3503. * @see wp_set_post_tags() 
  3504. * 
  3505. * @since 2.3.0 
  3506. * 
  3507. * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. 
  3508. * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags 
  3509. * separated by commas. Default empty. 
  3510. * @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure. 
  3511. */ 
  3512. function wp_add_post_tags( $post_id = 0, $tags = '' ) { 
  3513. return wp_set_post_tags($post_id, $tags, true); 
  3514.  
  3515. /** 
  3516. * Set the tags for a post. 
  3517. * 
  3518. * @since 2.3.0 
  3519. * 
  3520. * @see wp_set_object_terms() 
  3521. * 
  3522. * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. 
  3523. * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags 
  3524. * separated by commas. Default empty. 
  3525. * @param bool $append Optional. If true, don't delete existing tags, just add on. If false,  
  3526. * replace the tags with the new tags. Default false. 
  3527. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. 
  3528. */ 
  3529. function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { 
  3530. return wp_set_post_terms( $post_id, $tags, 'post_tag', $append); 
  3531.  
  3532. /** 
  3533. * Set the terms for a post. 
  3534. * 
  3535. * @since 2.8.0 
  3536. * 
  3537. * @see wp_set_object_terms() 
  3538. * 
  3539. * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. 
  3540. * @param string|array $tags Optional. An array of terms to set for the post, or a string of terms 
  3541. * separated by commas. Default empty. 
  3542. * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. 
  3543. * @param bool $append Optional. If true, don't delete existing terms, just add on. If false,  
  3544. * replace the terms with the new terms. Default false. 
  3545. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. 
  3546. */ 
  3547. function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { 
  3548. $post_id = (int) $post_id; 
  3549.  
  3550. if ( !$post_id ) 
  3551. return false; 
  3552.  
  3553. if ( empty($tags) ) 
  3554. $tags = array(); 
  3555.  
  3556. if ( ! is_array( $tags ) ) { 
  3557. $comma = _x( ', ', 'tag delimiter' ); 
  3558. if ( ', ' !== $comma ) 
  3559. $tags = str_replace( $comma, ', ', $tags ); 
  3560. $tags = explode( ', ', trim( $tags, " \n\t\r\0\x0B, " ) ); 
  3561.  
  3562. /** 
  3563. * Hierarchical taxonomies must always pass IDs rather than names so that 
  3564. * children with the same names but different parents aren't confused. 
  3565. */ 
  3566. if ( is_taxonomy_hierarchical( $taxonomy ) ) { 
  3567. $tags = array_unique( array_map( 'intval', $tags ) ); 
  3568.  
  3569. return wp_set_object_terms( $post_id, $tags, $taxonomy, $append ); 
  3570.  
  3571. /** 
  3572. * Set categories for a post. 
  3573. * 
  3574. * If the post categories parameter is not set, then the default category is 
  3575. * going used. 
  3576. * 
  3577. * @since 2.1.0 
  3578. * 
  3579. * @param int $post_ID Optional. The Post ID. Does not default to the ID 
  3580. * of the global $post. Default 0. 
  3581. * @param array|int $post_categories Optional. List of categories or ID of category. 
  3582. * Default empty array. 
  3583. * @param bool $append If true, don't delete existing categories, just add on. 
  3584. * If false, replace the categories with the new categories. 
  3585. * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. 
  3586. */ 
  3587. function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { 
  3588. $post_ID = (int) $post_ID; 
  3589. $post_type = get_post_type( $post_ID ); 
  3590. $post_status = get_post_status( $post_ID ); 
  3591. // If $post_categories isn't already an array, make it one: 
  3592. $post_categories = (array) $post_categories; 
  3593. if ( empty( $post_categories ) ) { 
  3594. if ( 'post' == $post_type && 'auto-draft' != $post_status ) { 
  3595. $post_categories = array( get_option('default_category') ); 
  3596. $append = false; 
  3597. } else { 
  3598. $post_categories = array(); 
  3599. } elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) { 
  3600. return true; 
  3601.  
  3602. return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); 
  3603.  
  3604. /** 
  3605. * Fires actions related to the transitioning of a post's status. 
  3606. * 
  3607. * When a post is saved, the post status is "transitioned" from one status to another,  
  3608. * though this does not always mean the status has actually changed before and after 
  3609. * the save. This function fires a number of action hooks related to that transition: 
  3610. * the generic {@see 'transition_post_status'} action, as well as the dynamic hooks 
  3611. * {@see '$old_status_to_$new_status'} and {@see '$new_status_$post->post_type'}. Note 
  3612. * that the function does not transition the post object in the database. 
  3613. * 
  3614. * For instance: When publishing a post for the first time, the post status may transition 
  3615. * from 'draft' * or some other status * to 'publish'. However, if a post is already 
  3616. * published and is simply being updated, the "old" and "new" statuses may both be 'publish' 
  3617. * before and after the transition. 
  3618. * 
  3619. * @since 2.3.0 
  3620. * 
  3621. * @param string $new_status Transition to this post status. 
  3622. * @param string $old_status Previous post status. 
  3623. * @param WP_Post $post Post data. 
  3624. */ 
  3625. function wp_transition_post_status( $new_status, $old_status, $post ) { 
  3626. /** 
  3627. * Fires when a post is transitioned from one status to another. 
  3628. * 
  3629. * @since 2.3.0 
  3630. * 
  3631. * @param string $new_status New post status. 
  3632. * @param string $old_status Old post status. 
  3633. * @param WP_Post $post Post object. 
  3634. */ 
  3635. do_action( 'transition_post_status', $new_status, $old_status, $post ); 
  3636.  
  3637. /** 
  3638. * Fires when a post is transitioned from one status to another. 
  3639. * 
  3640. * The dynamic portions of the hook name, `$new_status` and `$old status`,  
  3641. * refer to the old and new post statuses, respectively. 
  3642. * 
  3643. * @since 2.3.0 
  3644. * 
  3645. * @param WP_Post $post Post object. 
  3646. */ 
  3647. do_action( "{$old_status}_to_{$new_status}", $post ); 
  3648.  
  3649. /** 
  3650. * Fires when a post is transitioned from one status to another. 
  3651. * 
  3652. * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,  
  3653. * refer to the new post status and post type, respectively. 
  3654. * 
  3655. * Please note: When this action is hooked using a particular post status (like 
  3656. * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is 
  3657. * first transitioned to that status from something else, as well as upon 
  3658. * subsequent post updates (old and new status are both the same). 
  3659. * 
  3660. * Therefore, if you are looking to only fire a callback when a post is first 
  3661. * transitioned to a status, use the {@see 'transition_post_status'} hook instead. 
  3662. * 
  3663. * @since 2.3.0 
  3664. * 
  3665. * @param int $post_id Post ID. 
  3666. * @param WP_Post $post Post object. 
  3667. */ 
  3668. do_action( "{$new_status}_{$post->post_type}", $post->ID, $post ); 
  3669.  
  3670. // 
  3671.  // Comment, trackback, and pingback functions. 
  3672. // 
  3673.   
  3674. /** 
  3675. * Add a URL to those already pinged. 
  3676. * 
  3677. * @since 1.5.0 
  3678. * 
  3679. * @global wpdb $wpdb WordPress database abstraction object. 
  3680. * 
  3681. * @param int $post_id Post ID. 
  3682. * @param string $uri Ping URI. 
  3683. * @return int|false How many rows were updated. 
  3684. */ 
  3685. function add_ping( $post_id, $uri ) { 
  3686. global $wpdb; 
  3687. $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id )); 
  3688. $pung = trim($pung); 
  3689. $pung = preg_split('/\s/', $pung); 
  3690. $pung[] = $uri; 
  3691. $new = implode("\n", $pung); 
  3692.  
  3693. /** 
  3694. * Filters the new ping URL to add for the given post. 
  3695. * 
  3696. * @since 2.0.0 
  3697. * 
  3698. * @param string $new New ping URL to add. 
  3699. */ 
  3700. $new = apply_filters( 'add_ping', $new ); 
  3701.  
  3702. // expected_slashed ($new). 
  3703. $new = wp_unslash($new); 
  3704. return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) ); 
  3705.  
  3706. /** 
  3707. * Retrieve enclosures already enclosed for a post. 
  3708. * 
  3709. * @since 1.5.0 
  3710. * 
  3711. * @param int $post_id Post ID. 
  3712. * @return array List of enclosures. 
  3713. */ 
  3714. function get_enclosed( $post_id ) { 
  3715. $custom_fields = get_post_custom( $post_id ); 
  3716. $pung = array(); 
  3717. if ( !is_array( $custom_fields ) ) 
  3718. return $pung; 
  3719.  
  3720. foreach ( $custom_fields as $key => $val ) { 
  3721. if ( 'enclosure' != $key || !is_array( $val ) ) 
  3722. continue; 
  3723. foreach ( $val as $enc ) { 
  3724. $enclosure = explode( "\n", $enc ); 
  3725. $pung[] = trim( $enclosure[ 0 ] ); 
  3726.  
  3727. /** 
  3728. * Filters the list of enclosures already enclosed for the given post. 
  3729. * 
  3730. * @since 2.0.0 
  3731. * 
  3732. * @param array $pung Array of enclosures for the given post. 
  3733. * @param int $post_id Post ID. 
  3734. */ 
  3735. return apply_filters( 'get_enclosed', $pung, $post_id ); 
  3736.  
  3737. /** 
  3738. * Retrieve URLs already pinged for a post. 
  3739. * 
  3740. * @since 1.5.0 
  3741. * 
  3742. * @global wpdb $wpdb WordPress database abstraction object. 
  3743. * 
  3744. * @param int $post_id Post ID. 
  3745. * @return array 
  3746. */ 
  3747. function get_pung( $post_id ) { 
  3748. global $wpdb; 
  3749. $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id )); 
  3750. $pung = trim($pung); 
  3751. $pung = preg_split('/\s/', $pung); 
  3752.  
  3753. /** 
  3754. * Filters the list of already-pinged URLs for the given post. 
  3755. * 
  3756. * @since 2.0.0 
  3757. * 
  3758. * @param array $pung Array of URLs already pinged for the given post. 
  3759. */ 
  3760. return apply_filters( 'get_pung', $pung ); 
  3761.  
  3762. /** 
  3763. * Retrieve URLs that need to be pinged. 
  3764. * 
  3765. * @since 1.5.0 
  3766. * 
  3767. * @global wpdb $wpdb WordPress database abstraction object. 
  3768. * 
  3769. * @param int $post_id Post ID 
  3770. * @return array 
  3771. */ 
  3772. function get_to_ping( $post_id ) { 
  3773. global $wpdb; 
  3774. $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id )); 
  3775. $to_ping = sanitize_trackback_urls( $to_ping ); 
  3776. $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY); 
  3777.  
  3778. /** 
  3779. * Filters the list of URLs yet to ping for the given post. 
  3780. * 
  3781. * @since 2.0.0 
  3782. * 
  3783. * @param array $to_ping List of URLs yet to ping. 
  3784. */ 
  3785. return apply_filters( 'get_to_ping', $to_ping ); 
  3786.  
  3787. /** 
  3788. * Do trackbacks for a list of URLs. 
  3789. * 
  3790. * @since 1.0.0 
  3791. * 
  3792. * @param string $tb_list Comma separated list of URLs. 
  3793. * @param int $post_id Post ID. 
  3794. */ 
  3795. function trackback_url_list( $tb_list, $post_id ) { 
  3796. if ( ! empty( $tb_list ) ) { 
  3797. // Get post data. 
  3798. $postdata = get_post( $post_id, ARRAY_A ); 
  3799.  
  3800. // Form an excerpt. 
  3801. $excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] ); 
  3802.  
  3803. if ( strlen( $excerpt ) > 255 ) { 
  3804. $excerpt = substr( $excerpt, 0, 252 ) . '…'; 
  3805.  
  3806. $trackback_urls = explode( ', ', $tb_list ); 
  3807. foreach ( (array) $trackback_urls as $tb_url ) { 
  3808. $tb_url = trim( $tb_url ); 
  3809. trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id ); 
  3810.  
  3811. // 
  3812.  // Page functions 
  3813. // 
  3814.   
  3815. /** 
  3816. * Get a list of page IDs. 
  3817. * 
  3818. * @since 2.0.0 
  3819. * 
  3820. * @global wpdb $wpdb WordPress database abstraction object. 
  3821. * 
  3822. * @return array List of page IDs. 
  3823. */ 
  3824. function get_all_page_ids() { 
  3825. global $wpdb; 
  3826.  
  3827. $page_ids = wp_cache_get('all_page_ids', 'posts'); 
  3828. if ( ! is_array( $page_ids ) ) { 
  3829. $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'"); 
  3830. wp_cache_add('all_page_ids', $page_ids, 'posts'); 
  3831.  
  3832. return $page_ids; 
  3833.  
  3834. /** 
  3835. * Retrieves page data given a page ID or page object. 
  3836. * 
  3837. * Use get_post() instead of get_page(). 
  3838. * 
  3839. * @since 1.5.1 
  3840. * @deprecated 3.5.0 Use get_post() 
  3841. * 
  3842. * @param mixed $page Page object or page ID. Passed by reference. 
  3843. * @param string $output Optional. What to output. Accepts OBJECT, ARRAY_A, or ARRAY_N. 
  3844. * Default OBJECT. 
  3845. * @param string $filter Optional. How the return value should be filtered. Accepts 'raw',  
  3846. * 'edit', 'db', 'display'. Default 'raw'. 
  3847. * @return WP_Post|array|null WP_Post on success or null on failure. 
  3848. */ 
  3849. function get_page( $page, $output = OBJECT, $filter = 'raw') { 
  3850. return get_post( $page, $output, $filter ); 
  3851.  
  3852. /** 
  3853. * Retrieves a page given its path. 
  3854. * 
  3855. * @since 2.1.0 
  3856. * 
  3857. * @global wpdb $wpdb WordPress database abstraction object. 
  3858. * 
  3859. * @param string $page_path Page path. 
  3860. * @param string $output Optional. Output type. Accepts OBJECT, ARRAY_N, or ARRAY_A. 
  3861. * Default OBJECT. 
  3862. * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. 
  3863. * @return WP_Post|array|void WP_Post on success. 
  3864. */ 
  3865. function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { 
  3866. global $wpdb; 
  3867.  
  3868. $last_changed = wp_cache_get( 'last_changed', 'posts' ); 
  3869. if ( false === $last_changed ) { 
  3870. $last_changed = microtime(); 
  3871. wp_cache_set( 'last_changed', $last_changed, 'posts' ); 
  3872.  
  3873. $hash = md5( $page_path . serialize( $post_type ) ); 
  3874. $cache_key = "get_page_by_path:$hash:$last_changed"; 
  3875. $cached = wp_cache_get( $cache_key, 'posts' ); 
  3876. if ( false !== $cached ) { 
  3877. // Special case: '0' is a bad `$page_path`. 
  3878. if ( '0' === $cached || 0 === $cached ) { 
  3879. return; 
  3880. } else { 
  3881. return get_post( $cached, $output ); 
  3882.  
  3883. $page_path = rawurlencode(urldecode($page_path)); 
  3884. $page_path = str_replace('%2F', '/', $page_path); 
  3885. $page_path = str_replace('%20', ' ', $page_path); 
  3886. $parts = explode( '/', trim( $page_path, '/' ) ); 
  3887. $parts = esc_sql( $parts ); 
  3888. $parts = array_map( 'sanitize_title_for_query', $parts ); 
  3889.  
  3890. $in_string = "'" . implode( "', '", $parts ) . "'"; 
  3891.  
  3892. if ( is_array( $post_type ) ) { 
  3893. $post_types = $post_type; 
  3894. } else { 
  3895. $post_types = array( $post_type, 'attachment' ); 
  3896.  
  3897. $post_types = esc_sql( $post_types ); 
  3898. $post_type_in_string = "'" . implode( "', '", $post_types ) . "'"; 
  3899. $sql = " 
  3900. SELECT ID, post_name, post_parent, post_type 
  3901. FROM $wpdb->posts 
  3902. WHERE post_name IN ($in_string) 
  3903. AND post_type IN ($post_type_in_string) 
  3904. "; 
  3905.  
  3906. $pages = $wpdb->get_results( $sql, OBJECT_K ); 
  3907.  
  3908. $revparts = array_reverse( $parts ); 
  3909.  
  3910. $foundid = 0; 
  3911. foreach ( (array) $pages as $page ) { 
  3912. if ( $page->post_name == $revparts[0] ) { 
  3913. $count = 0; 
  3914. $p = $page; 
  3915.  
  3916. /** 
  3917. * Loop through the given path parts from right to left,  
  3918. * ensuring each matches the post ancestry. 
  3919. */ 
  3920. while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) { 
  3921. $count++; 
  3922. $parent = $pages[ $p->post_parent ]; 
  3923. if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) 
  3924. break; 
  3925. $p = $parent; 
  3926.  
  3927. if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) { 
  3928. $foundid = $page->ID; 
  3929. if ( $page->post_type == $post_type ) 
  3930. break; 
  3931.  
  3932. // We cache misses as well as hits. 
  3933. wp_cache_set( $cache_key, $foundid, 'posts' ); 
  3934.  
  3935. if ( $foundid ) { 
  3936. return get_post( $foundid, $output ); 
  3937.  
  3938. /** 
  3939. * Retrieve a page given its title. 
  3940. * 
  3941. * @since 2.1.0 
  3942. * 
  3943. * @global wpdb $wpdb WordPress database abstraction object. 
  3944. * 
  3945. * @param string $page_title Page title 
  3946. * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. 
  3947. * Default OBJECT. 
  3948. * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. 
  3949. * @return WP_Post|array|void WP_Post on success or null on failure 
  3950. */ 
  3951. function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { 
  3952. global $wpdb; 
  3953.  
  3954. if ( is_array( $post_type ) ) { 
  3955. $post_type = esc_sql( $post_type ); 
  3956. $post_type_in_string = "'" . implode( "', '", $post_type ) . "'"; 
  3957. $sql = $wpdb->prepare( " 
  3958. SELECT ID 
  3959. FROM $wpdb->posts 
  3960. WHERE post_title = %s 
  3961. AND post_type IN ($post_type_in_string) 
  3962. ", $page_title ); 
  3963. } else { 
  3964. $sql = $wpdb->prepare( " 
  3965. SELECT ID 
  3966. FROM $wpdb->posts 
  3967. WHERE post_title = %s 
  3968. AND post_type = %s 
  3969. ", $page_title, $post_type ); 
  3970.  
  3971. $page = $wpdb->get_var( $sql ); 
  3972.  
  3973. if ( $page ) { 
  3974. return get_post( $page, $output ); 
  3975.  
  3976. /** 
  3977. * Identify descendants of a given page ID in a list of page objects. 
  3978. * 
  3979. * Descendants are identified from the `$pages` array passed to the function. No database queries are performed. 
  3980. * 
  3981. * @since 1.5.1 
  3982. * 
  3983. * @param int $page_id Page ID. 
  3984. * @param array $pages List of page objects from which descendants should be identified. 
  3985. * @return array List of page children. 
  3986. */ 
  3987. function get_page_children( $page_id, $pages ) { 
  3988. // Build a hash of ID -> children. 
  3989. $children = array(); 
  3990. foreach ( (array) $pages as $page ) { 
  3991. $children[ intval( $page->post_parent ) ][] = $page; 
  3992.  
  3993. $page_list = array(); 
  3994.  
  3995. // Start the search by looking at immediate children. 
  3996. if ( isset( $children[ $page_id ] ) ) { 
  3997. // Always start at the end of the stack in order to preserve original `$pages` order. 
  3998. $to_look = array_reverse( $children[ $page_id ] ); 
  3999.  
  4000. while ( $to_look ) { 
  4001. $p = array_pop( $to_look ); 
  4002. $page_list[] = $p; 
  4003. if ( isset( $children[ $p->ID ] ) ) { 
  4004. foreach ( array_reverse( $children[ $p->ID ] ) as $child ) { 
  4005. // Append to the `$to_look` stack to descend the tree. 
  4006. $to_look[] = $child; 
  4007.  
  4008. return $page_list; 
  4009.  
  4010. /** 
  4011. * Order the pages with children under parents in a flat list. 
  4012. * 
  4013. * It uses auxiliary structure to hold parent-children relationships and 
  4014. * runs in O(N) complexity 
  4015. * 
  4016. * @since 2.0.0 
  4017. * 
  4018. * @param array $pages Posts array, passed by reference. 
  4019. * @param int $page_id Optional. Parent page ID. Default 0. 
  4020. * @return array A list arranged by hierarchy. Children immediately follow their parents. 
  4021. */ 
  4022. function get_page_hierarchy( &$pages, $page_id = 0 ) { 
  4023. if ( empty( $pages ) ) { 
  4024. return array(); 
  4025.  
  4026. $children = array(); 
  4027. foreach ( (array) $pages as $p ) { 
  4028. $parent_id = intval( $p->post_parent ); 
  4029. $children[ $parent_id ][] = $p; 
  4030.  
  4031. $result = array(); 
  4032. _page_traverse_name( $page_id, $children, $result ); 
  4033.  
  4034. return $result; 
  4035.  
  4036. /** 
  4037. * Traverse and return all the nested children post names of a root page. 
  4038. * 
  4039. * $children contains parent-children relations 
  4040. * 
  4041. * @since 2.9.0 
  4042. * 
  4043. * @see _page_traverse_name() 
  4044. * 
  4045. * @param int $page_id Page ID. 
  4046. * @param array $children Parent-children relations, passed by reference. 
  4047. * @param array $result Result, passed by reference. 
  4048. */ 
  4049. function _page_traverse_name( $page_id, &$children, &$result ) { 
  4050. if ( isset( $children[ $page_id ] ) ) { 
  4051. foreach ( (array)$children[ $page_id ] as $child ) { 
  4052. $result[ $child->ID ] = $child->post_name; 
  4053. _page_traverse_name( $child->ID, $children, $result ); 
  4054.  
  4055. /** 
  4056. * Build the URI path for a page. 
  4057. * 
  4058. * Sub pages will be in the "directory" under the parent page post name. 
  4059. * 
  4060. * @since 1.5.0 
  4061. * @since 4.6.0 Converted the `$page` parameter to optional. 
  4062. * 
  4063. * @param WP_Post|object|int $page Optional. Page ID or WP_Post object. Default is global $post. 
  4064. * @return string|false Page URI, false on error. 
  4065. */ 
  4066. function get_page_uri( $page = 0 ) { 
  4067. if ( ! $page instanceof WP_Post ) { 
  4068. $page = get_post( $page ); 
  4069.  
  4070. if ( ! $page ) 
  4071. return false; 
  4072.  
  4073. $uri = $page->post_name; 
  4074.  
  4075. foreach ( $page->ancestors as $parent ) { 
  4076. $parent = get_post( $parent ); 
  4077. if ( $parent && $parent->post_name ) { 
  4078. $uri = $parent->post_name . '/' . $uri; 
  4079.  
  4080. /** 
  4081. * Filters the URI for a page. 
  4082. * 
  4083. * @since 4.4.0 
  4084. * 
  4085. * @param string $uri Page URI. 
  4086. * @param WP_Post $page Page object. 
  4087. */ 
  4088. return apply_filters( 'get_page_uri', $uri, $page ); 
  4089.  
  4090. /** 
  4091. * Retrieve a list of pages. 
  4092. * 
  4093. * @global wpdb $wpdb WordPress database abstraction object. 
  4094. * 
  4095. * @since 1.5.0 
  4096. * 
  4097. * @param array|string $args { 
  4098. * Optional. Array or string of arguments to retrieve pages. 
  4099. * 
  4100. * @type int $child_of Page ID to return child and grandchild pages of. Note: The value 
  4101. * of `$hierarchical` has no bearing on whether `$child_of` returns 
  4102. * hierarchical results. Default 0, or no restriction. 
  4103. * @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. 
  4104. * @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author',  
  4105. * 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order',  
  4106. * 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. 
  4107. * 'post_' can be omitted for any values that start with it. 
  4108. * Default 'post_title'. 
  4109. * @type bool $hierarchical Whether to return pages hierarchically. If false in conjunction with 
  4110. * `$child_of` also being false, both arguments will be disregarded. 
  4111. * Default true. 
  4112. * @type array $exclude Array of page IDs to exclude. Default empty array. 
  4113. * @type array $include Array of page IDs to include. Cannot be used with `$child_of`,  
  4114. * `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. 
  4115. * Default empty array. 
  4116. * @type string $meta_key Only include pages with this meta key. Default empty. 
  4117. * @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. 
  4118. * Default empty. 
  4119. * @type string $authors A comma-separated list of author IDs. Default empty. 
  4120. * @type int $parent Page ID to return direct children of. Default -1, or no restriction. 
  4121. * @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude. 
  4122. * Default empty array. 
  4123. * @type int $number The number of pages to return. Default 0, or all pages. 
  4124. * @type int $offset The number of pages to skip before returning. Requires `$number`. 
  4125. * Default 0. 
  4126. * @type string $post_type The post type to query. Default 'page'. 
  4127. * @type string $post_status A comma-separated list of post status types to include. 
  4128. * Default 'publish'. 
  4129. * } 
  4130. * @return array|false List of pages matching defaults or `$args`. 
  4131. */ 
  4132. function get_pages( $args = array() ) { 
  4133. global $wpdb; 
  4134.  
  4135. $defaults = array( 
  4136. 'child_of' => 0, 'sort_order' => 'ASC',  
  4137. 'sort_column' => 'post_title', 'hierarchical' => 1,  
  4138. 'exclude' => array(), 'include' => array(),  
  4139. 'meta_key' => '', 'meta_value' => '',  
  4140. 'authors' => '', 'parent' => -1, 'exclude_tree' => array(),  
  4141. 'number' => '', 'offset' => 0,  
  4142. 'post_type' => 'page', 'post_status' => 'publish',  
  4143. ); 
  4144.  
  4145. $r = wp_parse_args( $args, $defaults ); 
  4146.  
  4147. $number = (int) $r['number']; 
  4148. $offset = (int) $r['offset']; 
  4149. $child_of = (int) $r['child_of']; 
  4150. $hierarchical = $r['hierarchical']; 
  4151. $exclude = $r['exclude']; 
  4152. $meta_key = $r['meta_key']; 
  4153. $meta_value = $r['meta_value']; 
  4154. $parent = $r['parent']; 
  4155. $post_status = $r['post_status']; 
  4156.  
  4157. // Make sure the post type is hierarchical. 
  4158. $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); 
  4159. if ( ! in_array( $r['post_type'], $hierarchical_post_types ) ) { 
  4160. return false; 
  4161.  
  4162. if ( $parent > 0 && ! $child_of ) { 
  4163. $hierarchical = false; 
  4164.  
  4165. // Make sure we have a valid post status. 
  4166. if ( ! is_array( $post_status ) ) { 
  4167. $post_status = explode( ', ', $post_status ); 
  4168. if ( array_diff( $post_status, get_post_stati() ) ) { 
  4169. return false; 
  4170.  
  4171. // $args can be whatever, only use the args defined in defaults to compute the key. 
  4172. $key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) ); 
  4173. $last_changed = wp_cache_get( 'last_changed', 'posts' ); 
  4174. if ( ! $last_changed ) { 
  4175. $last_changed = microtime(); 
  4176. wp_cache_set( 'last_changed', $last_changed, 'posts' ); 
  4177.  
  4178. $cache_key = "get_pages:$key:$last_changed"; 
  4179. if ( $cache = wp_cache_get( $cache_key, 'posts' ) ) { 
  4180. // Convert to WP_Post instances. 
  4181. $pages = array_map( 'get_post', $cache ); 
  4182. /** This filter is documented in wp-includes/post.php */ 
  4183. $pages = apply_filters( 'get_pages', $pages, $r ); 
  4184. return $pages; 
  4185.  
  4186. $inclusions = ''; 
  4187. if ( ! empty( $r['include'] ) ) { 
  4188. $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include 
  4189. $parent = -1; 
  4190. $exclude = ''; 
  4191. $meta_key = ''; 
  4192. $meta_value = ''; 
  4193. $hierarchical = false; 
  4194. $incpages = wp_parse_id_list( $r['include'] ); 
  4195. if ( ! empty( $incpages ) ) { 
  4196. $inclusions = ' AND ID IN (' . implode( ', ', $incpages ) . ')'; 
  4197.  
  4198. $exclusions = ''; 
  4199. if ( ! empty( $exclude ) ) { 
  4200. $expages = wp_parse_id_list( $exclude ); 
  4201. if ( ! empty( $expages ) ) { 
  4202. $exclusions = ' AND ID NOT IN (' . implode( ', ', $expages ) . ')'; 
  4203.  
  4204. $author_query = ''; 
  4205. if ( ! empty( $r['authors'] ) ) { 
  4206. $post_authors = preg_split( '/[\s, ]+/', $r['authors'] ); 
  4207.  
  4208. if ( ! empty( $post_authors ) ) { 
  4209. foreach ( $post_authors as $post_author ) { 
  4210. //Do we have an author id or an author login? 
  4211. if ( 0 == intval($post_author) ) { 
  4212. $post_author = get_user_by('login', $post_author); 
  4213. if ( empty( $post_author ) ) { 
  4214. continue; 
  4215. if ( empty( $post_author->ID ) ) { 
  4216. continue; 
  4217. $post_author = $post_author->ID; 
  4218.  
  4219. if ( '' == $author_query ) { 
  4220. $author_query = $wpdb->prepare(' post_author = %d ', $post_author); 
  4221. } else { 
  4222. $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author); 
  4223. if ( '' != $author_query ) { 
  4224. $author_query = " AND ($author_query)"; 
  4225.  
  4226. $join = ''; 
  4227. $where = "$exclusions $inclusions "; 
  4228. if ( '' !== $meta_key || '' !== $meta_value ) { 
  4229. $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; 
  4230.  
  4231. // meta_key and meta_value might be slashed 
  4232. $meta_key = wp_unslash($meta_key); 
  4233. $meta_value = wp_unslash($meta_value); 
  4234. if ( '' !== $meta_key ) { 
  4235. $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key); 
  4236. if ( '' !== $meta_value ) { 
  4237. $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value); 
  4238.  
  4239.  
  4240. if ( is_array( $parent ) ) { 
  4241. $post_parent__in = implode( ', ', array_map( 'absint', (array) $parent ) ); 
  4242. if ( ! empty( $post_parent__in ) ) { 
  4243. $where .= " AND post_parent IN ($post_parent__in)"; 
  4244. } elseif ( $parent >= 0 ) { 
  4245. $where .= $wpdb->prepare(' AND post_parent = %d ', $parent); 
  4246.  
  4247. if ( 1 == count( $post_status ) ) { 
  4248. $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $r['post_type'], reset( $post_status ) ); 
  4249. } else { 
  4250. $post_status = implode( "', '", $post_status ); 
  4251. $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $r['post_type'] ); 
  4252.  
  4253. $orderby_array = array(); 
  4254. $allowed_keys = array( 'author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified',  
  4255. 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent',  
  4256. 'ID', 'rand', 'comment_count' ); 
  4257.  
  4258. foreach ( explode( ', ', $r['sort_column'] ) as $orderby ) { 
  4259. $orderby = trim( $orderby ); 
  4260. if ( ! in_array( $orderby, $allowed_keys ) ) { 
  4261. continue; 
  4262.  
  4263. switch ( $orderby ) { 
  4264. case 'menu_order': 
  4265. break; 
  4266. case 'ID': 
  4267. $orderby = "$wpdb->posts.ID"; 
  4268. break; 
  4269. case 'rand': 
  4270. $orderby = 'RAND()'; 
  4271. break; 
  4272. case 'comment_count': 
  4273. $orderby = "$wpdb->posts.comment_count"; 
  4274. break; 
  4275. default: 
  4276. if ( 0 === strpos( $orderby, 'post_' ) ) { 
  4277. $orderby = "$wpdb->posts." . $orderby; 
  4278. } else { 
  4279. $orderby = "$wpdb->posts.post_" . $orderby; 
  4280.  
  4281. $orderby_array[] = $orderby; 
  4282.  
  4283. $sort_column = ! empty( $orderby_array ) ? implode( ', ', $orderby_array ) : "$wpdb->posts.post_title"; 
  4284.  
  4285. $sort_order = strtoupper( $r['sort_order'] ); 
  4286. if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) { 
  4287. $sort_order = 'ASC'; 
  4288.  
  4289. $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; 
  4290. $query .= $author_query; 
  4291. $query .= " ORDER BY " . $sort_column . " " . $sort_order ; 
  4292.  
  4293. if ( ! empty( $number ) ) { 
  4294. $query .= ' LIMIT ' . $offset . ', ' . $number; 
  4295.  
  4296. $pages = $wpdb->get_results($query); 
  4297.  
  4298. if ( empty($pages) ) { 
  4299. /** This filter is documented in wp-includes/post.php */ 
  4300. $pages = apply_filters( 'get_pages', array(), $r ); 
  4301. return $pages; 
  4302.  
  4303. // Sanitize before caching so it'll only get done once. 
  4304. $num_pages = count($pages); 
  4305. for ($i = 0; $i < $num_pages; $i++) { 
  4306. $pages[$i] = sanitize_post($pages[$i], 'raw'); 
  4307.  
  4308. // Update cache. 
  4309. update_post_cache( $pages ); 
  4310.  
  4311. if ( $child_of || $hierarchical ) { 
  4312. $pages = get_page_children($child_of, $pages); 
  4313.  
  4314. if ( ! empty( $r['exclude_tree'] ) ) { 
  4315. $exclude = wp_parse_id_list( $r['exclude_tree'] ); 
  4316. foreach ( $exclude as $id ) { 
  4317. $children = get_page_children( $id, $pages ); 
  4318. foreach ( $children as $child ) { 
  4319. $exclude[] = $child->ID; 
  4320.  
  4321. $num_pages = count( $pages ); 
  4322. for ( $i = 0; $i < $num_pages; $i++ ) { 
  4323. if ( in_array( $pages[$i]->ID, $exclude ) ) { 
  4324. unset( $pages[$i] ); 
  4325.  
  4326. $page_structure = array(); 
  4327. foreach ( $pages as $page ) { 
  4328. $page_structure[] = $page->ID; 
  4329.  
  4330. wp_cache_set( $cache_key, $page_structure, 'posts' ); 
  4331.  
  4332. // Convert to WP_Post instances 
  4333. $pages = array_map( 'get_post', $pages ); 
  4334.  
  4335. /** 
  4336. * Filters the retrieved list of pages. 
  4337. * 
  4338. * @since 2.1.0 
  4339. * 
  4340. * @param array $pages List of pages to retrieve. 
  4341. * @param array $r Array of get_pages() arguments. 
  4342. */ 
  4343. return apply_filters( 'get_pages', $pages, $r ); 
  4344.  
  4345. // 
  4346.  // Attachment functions 
  4347. // 
  4348.   
  4349. /** 
  4350. * Check if the attachment URI is local one and is really an attachment. 
  4351. * 
  4352. * @since 2.0.0 
  4353. * 
  4354. * @param string $url URL to check 
  4355. * @return bool True on success, false on failure. 
  4356. */ 
  4357. function is_local_attachment($url) { 
  4358. if (strpos($url, home_url()) === false) 
  4359. return false; 
  4360. if (strpos($url, home_url('/?attachment_id=')) !== false) 
  4361. return true; 
  4362. if ( $id = url_to_postid($url) ) { 
  4363. $post = get_post($id); 
  4364. if ( 'attachment' == $post->post_type ) 
  4365. return true; 
  4366. return false; 
  4367.  
  4368. /** 
  4369. * Insert an attachment. 
  4370. * 
  4371. * If you set the 'ID' in the $args parameter, it will mean that you are 
  4372. * updating and attempt to update the attachment. You can also set the 
  4373. * attachment name or title by setting the key 'post_name' or 'post_title'. 
  4374. * 
  4375. * You can set the dates for the attachment manually by setting the 'post_date' 
  4376. * and 'post_date_gmt' keys' values. 
  4377. * 
  4378. * By default, the comments will use the default settings for whether the 
  4379. * comments are allowed. You can close them manually or keep them open by 
  4380. * setting the value for the 'comment_status' key. 
  4381. * 
  4382. * @since 2.0.0 
  4383. * 
  4384. * @see wp_insert_post() 
  4385. * 
  4386. * @param string|array $args Arguments for inserting an attachment. 
  4387. * @param string $file Optional. Filename. 
  4388. * @param int $parent Optional. Parent post ID. 
  4389. * @return int Attachment ID. 
  4390. */ 
  4391. function wp_insert_attachment( $args, $file = false, $parent = 0 ) { 
  4392. $defaults = array( 
  4393. 'file' => $file,  
  4394. 'post_parent' => 0 
  4395. ); 
  4396.  
  4397. $data = wp_parse_args( $args, $defaults ); 
  4398.  
  4399. if ( ! empty( $parent ) ) { 
  4400. $data['post_parent'] = $parent; 
  4401.  
  4402. $data['post_type'] = 'attachment'; 
  4403.  
  4404. return wp_insert_post( $data ); 
  4405.  
  4406. /** 
  4407. * Trash or delete an attachment. 
  4408. * 
  4409. * When an attachment is permanently deleted, the file will also be removed. 
  4410. * Deletion removes all post meta fields, taxonomy, comments, etc. associated 
  4411. * with the attachment (except the main post). 
  4412. * 
  4413. * The attachment is moved to the trash instead of permanently deleted unless trash 
  4414. * for media is disabled, item is already in the trash, or $force_delete is true. 
  4415. * 
  4416. * @since 2.0.0 
  4417. * 
  4418. * @global wpdb $wpdb WordPress database abstraction object. 
  4419. * 
  4420. * @param int $post_id Attachment ID. 
  4421. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. 
  4422. * Default false. 
  4423. * @return mixed False on failure. Post data on success. 
  4424. */ 
  4425. function wp_delete_attachment( $post_id, $force_delete = false ) { 
  4426. global $wpdb; 
  4427.  
  4428. if ( !$post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ) ) 
  4429. return $post; 
  4430.  
  4431. if ( 'attachment' != $post->post_type ) 
  4432. return false; 
  4433.  
  4434. if ( !$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status ) 
  4435. return wp_trash_post( $post_id ); 
  4436.  
  4437. delete_post_meta($post_id, '_wp_trash_meta_status'); 
  4438. delete_post_meta($post_id, '_wp_trash_meta_time'); 
  4439.  
  4440. $meta = wp_get_attachment_metadata( $post_id ); 
  4441. $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); 
  4442. $file = get_attached_file( $post_id ); 
  4443.  
  4444. if ( is_multisite() ) 
  4445. delete_transient( 'dirsize_cache' ); 
  4446.  
  4447. /** 
  4448. * Fires before an attachment is deleted, at the start of wp_delete_attachment(). 
  4449. * 
  4450. * @since 2.0.0 
  4451. * 
  4452. * @param int $post_id Attachment ID. 
  4453. */ 
  4454. do_action( 'delete_attachment', $post_id ); 
  4455.  
  4456. wp_delete_object_term_relationships($post_id, array('category', 'post_tag')); 
  4457. wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type)); 
  4458.  
  4459. // Delete all for any posts. 
  4460. delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); 
  4461.  
  4462. wp_defer_comment_counting( true ); 
  4463.  
  4464. $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id )); 
  4465. foreach ( $comment_ids as $comment_id ) { 
  4466. wp_delete_comment( $comment_id, true ); 
  4467.  
  4468. wp_defer_comment_counting( false ); 
  4469.  
  4470. $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )); 
  4471. foreach ( $post_meta_ids as $mid ) 
  4472. delete_metadata_by_mid( 'post', $mid ); 
  4473.  
  4474. /** This action is documented in wp-includes/post.php */ 
  4475. do_action( 'delete_post', $post_id ); 
  4476. $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); 
  4477. if ( ! $result ) { 
  4478. return false; 
  4479. /** This action is documented in wp-includes/post.php */ 
  4480. do_action( 'deleted_post', $post_id ); 
  4481.  
  4482. $uploadpath = wp_get_upload_dir(); 
  4483.  
  4484. if ( ! empty($meta['thumb']) ) { 
  4485. // Don't delete the thumb if another attachment uses it. 
  4486. if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) { 
  4487. $thumbfile = str_replace(basename($file), $meta['thumb'], $file); 
  4488. /** This filter is documented in wp-includes/functions.php */ 
  4489. $thumbfile = apply_filters( 'wp_delete_file', $thumbfile ); 
  4490. @ unlink( path_join($uploadpath['basedir'], $thumbfile) ); 
  4491.  
  4492. // Remove intermediate and backup images if there are any. 
  4493. if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) { 
  4494. foreach ( $meta['sizes'] as $size => $sizeinfo ) { 
  4495. $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file ); 
  4496. /** This filter is documented in wp-includes/functions.php */ 
  4497. $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file ); 
  4498. @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) ); 
  4499.  
  4500. if ( is_array($backup_sizes) ) { 
  4501. foreach ( $backup_sizes as $size ) { 
  4502. $del_file = path_join( dirname($meta['file']), $size['file'] ); 
  4503. /** This filter is documented in wp-includes/functions.php */ 
  4504. $del_file = apply_filters( 'wp_delete_file', $del_file ); 
  4505. @ unlink( path_join($uploadpath['basedir'], $del_file) ); 
  4506.  
  4507. wp_delete_file( $file ); 
  4508.  
  4509. clean_post_cache( $post ); 
  4510.  
  4511. return $post; 
  4512.  
  4513. /** 
  4514. * Retrieve attachment meta field for attachment ID. 
  4515. * 
  4516. * @since 2.1.0 
  4517. * 
  4518. * @param int $post_id Attachment ID. Default 0. 
  4519. * @param bool $unfiltered Optional. If true, filters are not run. Default false. 
  4520. * @return mixed Attachment meta field. False on failure. 
  4521. */ 
  4522. function wp_get_attachment_metadata( $post_id = 0, $unfiltered = false ) { 
  4523. $post_id = (int) $post_id; 
  4524. if ( !$post = get_post( $post_id ) ) 
  4525. return false; 
  4526.  
  4527. $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); 
  4528.  
  4529. if ( $unfiltered ) 
  4530. return $data; 
  4531.  
  4532. /** 
  4533. * Filters the attachment meta data. 
  4534. * 
  4535. * @since 2.1.0 
  4536. * 
  4537. * @param array|bool $data Array of meta data for the given attachment, or false 
  4538. * if the object does not exist. 
  4539. * @param int $post_id Attachment ID. 
  4540. */ 
  4541. return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); 
  4542.  
  4543. /** 
  4544. * Update metadata for an attachment. 
  4545. * 
  4546. * @since 2.1.0 
  4547. * 
  4548. * @param int $post_id Attachment ID. 
  4549. * @param array $data Attachment data. 
  4550. * @return int|bool False if $post is invalid. 
  4551. */ 
  4552. function wp_update_attachment_metadata( $post_id, $data ) { 
  4553. $post_id = (int) $post_id; 
  4554. if ( !$post = get_post( $post_id ) ) 
  4555. return false; 
  4556.  
  4557. /** 
  4558. * Filters the updated attachment meta data. 
  4559. * 
  4560. * @since 2.1.0 
  4561. * 
  4562. * @param array $data Array of updated attachment meta data. 
  4563. * @param int $post_id Attachment ID. 
  4564. */ 
  4565. if ( $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ) ) 
  4566. return update_post_meta( $post->ID, '_wp_attachment_metadata', $data ); 
  4567. else 
  4568. return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); 
  4569.  
  4570. /** 
  4571. * Retrieve the URL for an attachment. 
  4572. * 
  4573. * @since 2.1.0 
  4574. * 
  4575. * @global string $pagenow 
  4576. * 
  4577. * @param int $post_id Optional. Attachment ID. Default 0. 
  4578. * @return string|false Attachment URL, otherwise false. 
  4579. */ 
  4580. function wp_get_attachment_url( $post_id = 0 ) { 
  4581. $post_id = (int) $post_id; 
  4582. if ( !$post = get_post( $post_id ) ) 
  4583. return false; 
  4584.  
  4585. if ( 'attachment' != $post->post_type ) 
  4586. return false; 
  4587.  
  4588. $url = ''; 
  4589. // Get attached file. 
  4590. if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true ) ) { 
  4591. // Get upload directory. 
  4592. if ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) { 
  4593. // Check that the upload base exists in the file location. 
  4594. if ( 0 === strpos( $file, $uploads['basedir'] ) ) { 
  4595. // Replace file location with url location. 
  4596. $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); 
  4597. } elseif ( false !== strpos($file, 'wp-content/uploads') ) { 
  4598. // Get the directory name relative to the basedir (back compat for pre-2.7 uploads) 
  4599. $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . basename( $file ); 
  4600. } else { 
  4601. // It's a newly-uploaded file, therefore $file is relative to the basedir. 
  4602. $url = $uploads['baseurl'] . "/$file"; 
  4603.  
  4604. /** 
  4605. * If any of the above options failed, Fallback on the GUID as used pre-2.7,  
  4606. * not recommended to rely upon this. 
  4607. */ 
  4608. if ( empty($url) ) { 
  4609. $url = get_the_guid( $post->ID ); 
  4610.  
  4611. // On SSL front end, URLs should be HTTPS. 
  4612. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) { 
  4613. $url = set_url_scheme( $url ); 
  4614.  
  4615. /** 
  4616. * Filters the attachment URL. 
  4617. * 
  4618. * @since 2.1.0 
  4619. * 
  4620. * @param string $url URL for the given attachment. 
  4621. * @param int $post_id Attachment ID. 
  4622. */ 
  4623. $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); 
  4624.  
  4625. if ( empty( $url ) ) 
  4626. return false; 
  4627.  
  4628. return $url; 
  4629.  
  4630. /** 
  4631. * Retrieves the caption for an attachment. 
  4632. * 
  4633. * @since 4.6.0 
  4634. * 
  4635. * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. 
  4636. * @return string|false False on failure. Attachment caption on success. 
  4637. */ 
  4638. function wp_get_attachment_caption( $post_id = 0 ) { 
  4639. $post_id = (int) $post_id; 
  4640. if ( ! $post = get_post( $post_id ) ) { 
  4641. return false; 
  4642.  
  4643. if ( 'attachment' !== $post->post_type ) { 
  4644. return false; 
  4645.  
  4646. $caption = $post->post_excerpt; 
  4647.  
  4648. /** 
  4649. * Filters the attachment caption. 
  4650. * 
  4651. * @since 4.6.0 
  4652. * 
  4653. * @param string $caption Caption for the given attachment. 
  4654. * @param int $post_id Attachment ID. 
  4655. */ 
  4656. return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID ); 
  4657.  
  4658. /** 
  4659. * Retrieve thumbnail for an attachment. 
  4660. * 
  4661. * @since 2.1.0 
  4662. * 
  4663. * @param int $post_id Optional. Attachment ID. Default 0. 
  4664. * @return string|false False on failure. Thumbnail file path on success. 
  4665. */ 
  4666. function wp_get_attachment_thumb_file( $post_id = 0 ) { 
  4667. $post_id = (int) $post_id; 
  4668. if ( !$post = get_post( $post_id ) ) 
  4669. return false; 
  4670. if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) ) 
  4671. return false; 
  4672.  
  4673. $file = get_attached_file( $post->ID ); 
  4674.  
  4675. if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) ) { 
  4676. /** 
  4677. * Filters the attachment thumbnail file path. 
  4678. * 
  4679. * @since 2.1.0 
  4680. * 
  4681. * @param string $thumbfile File path to the attachment thumbnail. 
  4682. * @param int $post_id Attachment ID. 
  4683. */ 
  4684. return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); 
  4685. return false; 
  4686.  
  4687. /** 
  4688. * Retrieve URL for an attachment thumbnail. 
  4689. * 
  4690. * @since 2.1.0 
  4691. * 
  4692. * @param int $post_id Optional. Attachment ID. Default 0. 
  4693. * @return string|false False on failure. Thumbnail URL on success. 
  4694. */ 
  4695. function wp_get_attachment_thumb_url( $post_id = 0 ) { 
  4696. $post_id = (int) $post_id; 
  4697. if ( !$post = get_post( $post_id ) ) 
  4698. return false; 
  4699. if ( !$url = wp_get_attachment_url( $post->ID ) ) 
  4700. return false; 
  4701.  
  4702. $sized = image_downsize( $post_id, 'thumbnail' ); 
  4703. if ( $sized ) 
  4704. return $sized[0]; 
  4705.  
  4706. if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) ) 
  4707. return false; 
  4708.  
  4709. $url = str_replace(basename($url), basename($thumb), $url); 
  4710.  
  4711. /** 
  4712. * Filters the attachment thumbnail URL. 
  4713. * 
  4714. * @since 2.1.0 
  4715. * 
  4716. * @param string $url URL for the attachment thumbnail. 
  4717. * @param int $post_id Attachment ID. 
  4718. */ 
  4719. return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); 
  4720.  
  4721. /** 
  4722. * Verifies an attachment is of a given type. 
  4723. * 
  4724. * @since 4.2.0 
  4725. * 
  4726. * @param string $type Attachment type. Accepts 'image', 'audio', or 'video'. 
  4727. * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. 
  4728. * @return bool True if one of the accepted types, false otherwise. 
  4729. */ 
  4730. function wp_attachment_is( $type, $post = null ) { 
  4731. if ( ! $post = get_post( $post ) ) { 
  4732. return false; 
  4733.  
  4734. if ( ! $file = get_attached_file( $post->ID ) ) { 
  4735. return false; 
  4736.  
  4737. if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) { 
  4738. return true; 
  4739.  
  4740. $check = wp_check_filetype( $file ); 
  4741. if ( empty( $check['ext'] ) ) { 
  4742. return false; 
  4743.  
  4744. $ext = $check['ext']; 
  4745.  
  4746. if ( 'import' !== $post->post_mime_type ) { 
  4747. return $type === $ext; 
  4748.  
  4749. switch ( $type ) { 
  4750. case 'image': 
  4751. $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); 
  4752. return in_array( $ext, $image_exts ); 
  4753.  
  4754. case 'audio': 
  4755. return in_array( $ext, wp_get_audio_extensions() ); 
  4756.  
  4757. case 'video': 
  4758. return in_array( $ext, wp_get_video_extensions() ); 
  4759.  
  4760. default: 
  4761. return $type === $ext; 
  4762.  
  4763. /** 
  4764. * Checks if the attachment is an image. 
  4765. * 
  4766. * @since 2.1.0 
  4767. * @since 4.2.0 Modified into wrapper for wp_attachment_is() and 
  4768. * allowed WP_Post object to be passed. 
  4769. * 
  4770. * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. 
  4771. * @return bool Whether the attachment is an image. 
  4772. */ 
  4773. function wp_attachment_is_image( $post = null ) { 
  4774. return wp_attachment_is( 'image', $post ); 
  4775.  
  4776. /** 
  4777. * Retrieve the icon for a MIME type. 
  4778. * 
  4779. * @since 2.1.0 
  4780. * 
  4781. * @param string|int $mime MIME type or attachment ID. 
  4782. * @return string|false Icon, false otherwise. 
  4783. */ 
  4784. function wp_mime_type_icon( $mime = 0 ) { 
  4785. if ( !is_numeric($mime) ) 
  4786. $icon = wp_cache_get("mime_type_icon_$mime"); 
  4787.  
  4788. $post_id = 0; 
  4789. if ( empty($icon) ) { 
  4790. $post_mimes = array(); 
  4791. if ( is_numeric($mime) ) { 
  4792. $mime = (int) $mime; 
  4793. if ( $post = get_post( $mime ) ) { 
  4794. $post_id = (int) $post->ID; 
  4795. $file = get_attached_file( $post_id ); 
  4796. $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $file); 
  4797. if ( !empty($ext) ) { 
  4798. $post_mimes[] = $ext; 
  4799. if ( $ext_type = wp_ext2type( $ext ) ) 
  4800. $post_mimes[] = $ext_type; 
  4801. $mime = $post->post_mime_type; 
  4802. } else { 
  4803. $mime = 0; 
  4804. } else { 
  4805. $post_mimes[] = $mime; 
  4806.  
  4807. $icon_files = wp_cache_get('icon_files'); 
  4808.  
  4809. if ( !is_array($icon_files) ) { 
  4810. /** 
  4811. * Filters the icon directory path. 
  4812. * 
  4813. * @since 2.0.0 
  4814. * 
  4815. * @param string $path Icon directory absolute path. 
  4816. */ 
  4817. $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); 
  4818.  
  4819. /** 
  4820. * Filters the icon directory URI. 
  4821. * 
  4822. * @since 2.0.0 
  4823. * 
  4824. * @param string $uri Icon directory URI. 
  4825. */ 
  4826. $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) ); 
  4827.  
  4828. /** 
  4829. * Filters the list of icon directory URIs. 
  4830. * 
  4831. * @since 2.5.0 
  4832. * 
  4833. * @param array $uris List of icon directory URIs. 
  4834. */ 
  4835. $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); 
  4836. $icon_files = array(); 
  4837. while ( $dirs ) { 
  4838. $keys = array_keys( $dirs ); 
  4839. $dir = array_shift( $keys ); 
  4840. $uri = array_shift($dirs); 
  4841. if ( $dh = opendir($dir) ) { 
  4842. while ( false !== $file = readdir($dh) ) { 
  4843. $file = basename($file); 
  4844. if ( substr($file, 0, 1) == '.' ) 
  4845. continue; 
  4846. if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) { 
  4847. if ( is_dir("$dir/$file") ) 
  4848. $dirs["$dir/$file"] = "$uri/$file"; 
  4849. continue; 
  4850. $icon_files["$dir/$file"] = "$uri/$file"; 
  4851. closedir($dh); 
  4852. wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); 
  4853.  
  4854. $types = array(); 
  4855. // Icon basename - extension = MIME wildcard. 
  4856. foreach ( $icon_files as $file => $uri ) 
  4857. $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file]; 
  4858.  
  4859. if ( ! empty($mime) ) { 
  4860. $post_mimes[] = substr($mime, 0, strpos($mime, '/')); 
  4861. $post_mimes[] = substr($mime, strpos($mime, '/') + 1); 
  4862. $post_mimes[] = str_replace('/', '_', $mime); 
  4863.  
  4864. $matches = wp_match_mime_types(array_keys($types), $post_mimes); 
  4865. $matches['default'] = array('default'); 
  4866.  
  4867. foreach ( $matches as $match => $wilds ) { 
  4868. foreach ( $wilds as $wild ) { 
  4869. if ( ! isset( $types[ $wild ] ) ) { 
  4870. continue; 
  4871.  
  4872. $icon = $types[ $wild ]; 
  4873. if ( ! is_numeric( $mime ) ) { 
  4874. wp_cache_add( "mime_type_icon_$mime", $icon ); 
  4875. break 2; 
  4876.  
  4877. /** 
  4878. * Filters the mime type icon. 
  4879. * 
  4880. * @since 2.1.0 
  4881. * 
  4882. * @param string $icon Path to the mime type icon. 
  4883. * @param string $mime Mime type. 
  4884. * @param int $post_id Attachment ID. Will equal 0 if the function passed 
  4885. * the mime type. 
  4886. */ 
  4887. return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); 
  4888.  
  4889. /** 
  4890. * Check for changed slugs for published post objects and save the old slug. 
  4891. * 
  4892. * The function is used when a post object of any type is updated,  
  4893. * by comparing the current and previous post objects. 
  4894. * 
  4895. * If the slug was changed and not already part of the old slugs then it will be 
  4896. * added to the post meta field ('_wp_old_slug') for storing old slugs for that 
  4897. * post. 
  4898. * 
  4899. * The most logically usage of this function is redirecting changed post objects, so 
  4900. * that those that linked to an changed post will be redirected to the new post. 
  4901. * 
  4902. * @since 2.1.0 
  4903. * 
  4904. * @param int $post_id Post ID. 
  4905. * @param WP_Post $post The Post Object 
  4906. * @param WP_Post $post_before The Previous Post Object 
  4907. */ 
  4908. function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { 
  4909. // Don't bother if it hasn't changed. 
  4910. if ( $post->post_name == $post_before->post_name ) { 
  4911. return; 
  4912.  
  4913. // We're only concerned with published, non-hierarchical objects. 
  4914. if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) { 
  4915. return; 
  4916.  
  4917. $old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' ); 
  4918.  
  4919. // If we haven't added this old slug before, add it now. 
  4920. if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs ) ) { 
  4921. add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name ); 
  4922.  
  4923. // If the new slug was used previously, delete it from the list. 
  4924. if ( in_array( $post->post_name, $old_slugs ) ) { 
  4925. delete_post_meta( $post_id, '_wp_old_slug', $post->post_name ); 
  4926.  
  4927. /** 
  4928. * Retrieve the private post SQL based on capability. 
  4929. * 
  4930. * This function provides a standardized way to appropriately select on the 
  4931. * post_status of a post type. The function will return a piece of SQL code 
  4932. * that can be added to a WHERE clause; this SQL is constructed to allow all 
  4933. * published posts, and all private posts to which the user has access. 
  4934. * 
  4935. * @since 2.2.0 
  4936. * @since 4.3.0 Added the ability to pass an array to `$post_type`. 
  4937. * 
  4938. * @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'. 
  4939. * @return string SQL code that can be added to a where clause. 
  4940. */ 
  4941. function get_private_posts_cap_sql( $post_type ) { 
  4942. return get_posts_by_author_sql( $post_type, false ); 
  4943.  
  4944. /** 
  4945. * Retrieve the post SQL based on capability, author, and type. 
  4946. * 
  4947. * @since 3.0.0 
  4948. * @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. 
  4949. * 
  4950. * @see get_private_posts_cap_sql() 
  4951. * @global wpdb $wpdb WordPress database abstraction object. 
  4952. * 
  4953. * @param array|string $post_type Single post type or an array of post types. 
  4954. * @param bool $full Optional. Returns a full WHERE statement instead of just 
  4955. * an 'andalso' term. Default true. 
  4956. * @param int $post_author Optional. Query posts having a single author ID. Default null. 
  4957. * @param bool $public_only Optional. Only return public posts. Skips cap checks for 
  4958. * $current_user. Default false. 
  4959. * @return string SQL WHERE code that can be added to a query. 
  4960. */ 
  4961. function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { 
  4962. global $wpdb; 
  4963.  
  4964. if ( is_array( $post_type ) ) { 
  4965. $post_types = $post_type; 
  4966. } else { 
  4967. $post_types = array( $post_type ); 
  4968.  
  4969. $post_type_clauses = array(); 
  4970. foreach ( $post_types as $post_type ) { 
  4971. $post_type_obj = get_post_type_object( $post_type ); 
  4972. if ( ! $post_type_obj ) { 
  4973. continue; 
  4974.  
  4975. /** 
  4976. * Filters the capability to read private posts for a custom post type 
  4977. * when generating SQL for getting posts by author. 
  4978. * 
  4979. * @since 2.2.0 
  4980. * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless". 
  4981. * 
  4982. * @param string $cap Capability. 
  4983. */ 
  4984. if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) { 
  4985. $cap = current_user_can( $post_type_obj->cap->read_private_posts ); 
  4986.  
  4987. // Only need to check the cap if $public_only is false. 
  4988. $post_status_sql = "post_status = 'publish'"; 
  4989. if ( false === $public_only ) { 
  4990. if ( $cap ) { 
  4991. // Does the user have the capability to view private posts? Guess so. 
  4992. $post_status_sql .= " OR post_status = 'private'"; 
  4993. } elseif ( is_user_logged_in() ) { 
  4994. // Users can view their own private posts. 
  4995. $id = get_current_user_id(); 
  4996. if ( null === $post_author || ! $full ) { 
  4997. $post_status_sql .= " OR post_status = 'private' AND post_author = $id"; 
  4998. } elseif ( $id == (int) $post_author ) { 
  4999. $post_status_sql .= " OR post_status = 'private'"; 
  5000. } // else none 
  5001. } // else none 
  5002.  
  5003. $post_type_clauses[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )"; 
  5004.  
  5005. if ( empty( $post_type_clauses ) ) { 
  5006. return $full ? 'WHERE 1 = 0' : '1 = 0'; 
  5007.  
  5008. $sql = '( '. implode( ' OR ', $post_type_clauses ) . ' )'; 
  5009.  
  5010. if ( null !== $post_author ) { 
  5011. $sql .= $wpdb->prepare( ' AND post_author = %d', $post_author ); 
  5012.  
  5013. if ( $full ) { 
  5014. $sql = 'WHERE ' . $sql; 
  5015.  
  5016. return $sql; 
  5017.  
  5018. /** 
  5019. * Retrieve the date that the last post was published. 
  5020. * 
  5021. * The server timezone is the default and is the difference between GMT and 
  5022. * server time. The 'blog' value is the date when the last post was posted. The 
  5023. * 'gmt' is when the last post was posted in GMT formatted date. 
  5024. * 
  5025. * @since 0.71 
  5026. * @since 4.4.0 The `$post_type` argument was added. 
  5027. * 
  5028. * @param string $timezone Optional. The timezone for the timestamp. Accepts 'server', 'blog', or 'gmt'. 
  5029. * 'server' uses the server's internal timezone. 
  5030. * 'blog' uses the `post_modified` field, which proxies to the timezone set for the site. 
  5031. * 'gmt' uses the `post_modified_gmt` field. 
  5032. * Default 'server'. 
  5033. * @param string $post_type Optional. The post type to check. Default 'any'. 
  5034. * @return string The date of the last post. 
  5035. */ 
  5036. function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) { 
  5037. /** 
  5038. * Filters the date the last post was published. 
  5039. * 
  5040. * @since 2.3.0 
  5041. * 
  5042. * @param string $date Date the last post was published. 
  5043. * @param string $timezone Location to use for getting the post published date. 
  5044. * See get_lastpostdate() for accepted `$timezone` values. 
  5045. */ 
  5046. return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date', $post_type ), $timezone ); 
  5047.  
  5048. /** 
  5049. * Get the timestamp of the last time any post was modified. 
  5050. * 
  5051. * The server timezone is the default and is the difference between GMT and 
  5052. * server time. The 'blog' value is just when the last post was modified. The 
  5053. * 'gmt' is when the last post was modified in GMT time. 
  5054. * 
  5055. * @since 1.2.0 
  5056. * @since 4.4.0 The `$post_type` argument was added. 
  5057. * 
  5058. * @param string $timezone Optional. The timezone for the timestamp. See get_lastpostdate() 
  5059. * for information on accepted values. 
  5060. * Default 'server'. 
  5061. * @param string $post_type Optional. The post type to check. Default 'any'. 
  5062. * @return string The timestamp. 
  5063. */ 
  5064. function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) { 
  5065. /** 
  5066. * Pre-filter the return value of get_lastpostmodified() before the query is run. 
  5067. * 
  5068. * @since 4.4.0 
  5069. * 
  5070. * @param string $lastpostmodified Date the last post was modified. 
  5071. * Returning anything other than false will short-circuit the function. 
  5072. * @param string $timezone Location to use for getting the post modified date. 
  5073. * See get_lastpostdate() for accepted `$timezone` values. 
  5074. * @param string $post_type The post type to check. 
  5075. */ 
  5076. $lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type ); 
  5077. if ( false !== $lastpostmodified ) { 
  5078. return $lastpostmodified; 
  5079.  
  5080. $lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type ); 
  5081.  
  5082. $lastpostdate = get_lastpostdate($timezone); 
  5083. if ( $lastpostdate > $lastpostmodified ) { 
  5084. $lastpostmodified = $lastpostdate; 
  5085.  
  5086. /** 
  5087. * Filters the date the last post was modified. 
  5088. * 
  5089. * @since 2.3.0 
  5090. * 
  5091. * @param string $lastpostmodified Date the last post was modified. 
  5092. * @param string $timezone Location to use for getting the post modified date. 
  5093. * See get_lastpostdate() for accepted `$timezone` values. 
  5094. */ 
  5095. return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone ); 
  5096.  
  5097. /** 
  5098. * Get the timestamp of the last time any post was modified or published. 
  5099. * 
  5100. * @since 3.1.0 
  5101. * @since 4.4.0 The `$post_type` argument was added. 
  5102. * @access private 
  5103. * 
  5104. * @global wpdb $wpdb WordPress database abstraction object. 
  5105. * 
  5106. * @param string $timezone The timezone for the timestamp. See get_lastpostdate(). 
  5107. * for information on accepted values. 
  5108. * @param string $field Post field to check. Accepts 'date' or 'modified'. 
  5109. * @param string $post_type Optional. The post type to check. Default 'any'. 
  5110. * @return string|false The timestamp. 
  5111. */ 
  5112. function _get_last_post_time( $timezone, $field, $post_type = 'any' ) { 
  5113. global $wpdb; 
  5114.  
  5115. if ( ! in_array( $field, array( 'date', 'modified' ) ) ) { 
  5116. return false; 
  5117.  
  5118. $timezone = strtolower( $timezone ); 
  5119.  
  5120. $key = "lastpost{$field}:$timezone"; 
  5121. if ( 'any' !== $post_type ) { 
  5122. $key .= ':' . sanitize_key( $post_type ); 
  5123.  
  5124. $date = wp_cache_get( $key, 'timeinfo' ); 
  5125.  
  5126. if ( ! $date ) { 
  5127. if ( 'any' === $post_type ) { 
  5128. $post_types = get_post_types( array( 'public' => true ) ); 
  5129. array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) ); 
  5130. $post_types = "'" . implode( "', '", $post_types ) . "'"; 
  5131. } else { 
  5132. $post_types = "'" . sanitize_key( $post_type ) . "'"; 
  5133.  
  5134. switch ( $timezone ) { 
  5135. case 'gmt': 
  5136. $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); 
  5137. break; 
  5138. case 'blog': 
  5139. $date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); 
  5140. break; 
  5141. case 'server': 
  5142. $add_seconds_server = date( 'Z' ); 
  5143. $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); 
  5144. break; 
  5145.  
  5146. if ( $date ) { 
  5147. wp_cache_set( $key, $date, 'timeinfo' ); 
  5148.  
  5149. return $date; 
  5150.  
  5151. /** 
  5152. * Updates posts in cache. 
  5153. * 
  5154. * @since 1.5.1 
  5155. * 
  5156. * @param array $posts Array of post objects, passed by reference. 
  5157. */ 
  5158. function update_post_cache( &$posts ) { 
  5159. if ( ! $posts ) 
  5160. return; 
  5161.  
  5162. foreach ( $posts as $post ) 
  5163. wp_cache_add( $post->ID, $post, 'posts' ); 
  5164.  
  5165. /** 
  5166. * Will clean the post in the cache. 
  5167. * 
  5168. * Cleaning means delete from the cache of the post. Will call to clean the term 
  5169. * object cache associated with the post ID. 
  5170. * 
  5171. * This function not run if $_wp_suspend_cache_invalidation is not empty. See 
  5172. * wp_suspend_cache_invalidation(). 
  5173. * 
  5174. * @since 2.0.0 
  5175. * 
  5176. * @global bool $_wp_suspend_cache_invalidation 
  5177. * 
  5178. * @param int|WP_Post $post Post ID or post object to remove from the cache. 
  5179. */ 
  5180. function clean_post_cache( $post ) { 
  5181. global $_wp_suspend_cache_invalidation; 
  5182.  
  5183. if ( ! empty( $_wp_suspend_cache_invalidation ) ) 
  5184. return; 
  5185.  
  5186. $post = get_post( $post ); 
  5187. if ( empty( $post ) ) 
  5188. return; 
  5189.  
  5190. wp_cache_delete( $post->ID, 'posts' ); 
  5191. wp_cache_delete( $post->ID, 'post_meta' ); 
  5192.  
  5193. clean_object_term_cache( $post->ID, $post->post_type ); 
  5194.  
  5195. wp_cache_delete( 'wp_get_archives', 'general' ); 
  5196.  
  5197. /** 
  5198. * Fires immediately after the given post's cache is cleaned. 
  5199. * 
  5200. * @since 2.5.0 
  5201. * 
  5202. * @param int $post_id Post ID. 
  5203. * @param WP_Post $post Post object. 
  5204. */ 
  5205. do_action( 'clean_post_cache', $post->ID, $post ); 
  5206.  
  5207. if ( 'page' == $post->post_type ) { 
  5208. wp_cache_delete( 'all_page_ids', 'posts' ); 
  5209.  
  5210. /** 
  5211. * Fires immediately after the given page's cache is cleaned. 
  5212. * 
  5213. * @since 2.5.0 
  5214. * 
  5215. * @param int $post_id Post ID. 
  5216. */ 
  5217. do_action( 'clean_page_cache', $post->ID ); 
  5218.  
  5219. wp_cache_set( 'last_changed', microtime(), 'posts' ); 
  5220.  
  5221. /** 
  5222. * Call major cache updating functions for list of Post objects. 
  5223. * 
  5224. * @since 1.5.0 
  5225. * 
  5226. * @param array $posts Array of Post objects 
  5227. * @param string $post_type Optional. Post type. Default 'post'. 
  5228. * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. 
  5229. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 
  5230. */ 
  5231. function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { 
  5232. // No point in doing all this work if we didn't match any posts. 
  5233. if ( !$posts ) 
  5234. return; 
  5235.  
  5236. update_post_cache($posts); 
  5237.  
  5238. $post_ids = array(); 
  5239. foreach ( $posts as $post ) 
  5240. $post_ids[] = $post->ID; 
  5241.  
  5242. if ( ! $post_type ) 
  5243. $post_type = 'any'; 
  5244.  
  5245. if ( $update_term_cache ) { 
  5246. if ( is_array($post_type) ) { 
  5247. $ptypes = $post_type; 
  5248. } elseif ( 'any' == $post_type ) { 
  5249. $ptypes = array(); 
  5250. // Just use the post_types in the supplied posts. 
  5251. foreach ( $posts as $post ) { 
  5252. $ptypes[] = $post->post_type; 
  5253. $ptypes = array_unique($ptypes); 
  5254. } else { 
  5255. $ptypes = array($post_type); 
  5256.  
  5257. if ( ! empty($ptypes) ) 
  5258. update_object_term_cache($post_ids, $ptypes); 
  5259.  
  5260. if ( $update_meta_cache ) 
  5261. update_postmeta_cache($post_ids); 
  5262.  
  5263. /** 
  5264. * Updates metadata cache for list of post IDs. 
  5265. * 
  5266. * Performs SQL query to retrieve the metadata for the post IDs and updates the 
  5267. * metadata cache for the posts. Therefore, the functions, which call this 
  5268. * function, do not need to perform SQL queries on their own. 
  5269. * 
  5270. * @since 2.1.0 
  5271. * 
  5272. * @param array $post_ids List of post IDs. 
  5273. * @return array|false Returns false if there is nothing to update or an array 
  5274. * of metadata. 
  5275. */ 
  5276. function update_postmeta_cache( $post_ids ) { 
  5277. return update_meta_cache('post', $post_ids); 
  5278.  
  5279. /** 
  5280. * Will clean the attachment in the cache. 
  5281. * 
  5282. * Cleaning means delete from the cache. Optionally will clean the term 
  5283. * object cache associated with the attachment ID. 
  5284. * 
  5285. * This function will not run if $_wp_suspend_cache_invalidation is not empty. 
  5286. * 
  5287. * @since 3.0.0 
  5288. * 
  5289. * @global bool $_wp_suspend_cache_invalidation 
  5290. * 
  5291. * @param int $id The attachment ID in the cache to clean. 
  5292. * @param bool $clean_terms Optional. Whether to clean terms cache. Default false. 
  5293. */ 
  5294. function clean_attachment_cache( $id, $clean_terms = false ) { 
  5295. global $_wp_suspend_cache_invalidation; 
  5296.  
  5297. if ( !empty($_wp_suspend_cache_invalidation) ) 
  5298. return; 
  5299.  
  5300. $id = (int) $id; 
  5301.  
  5302. wp_cache_delete($id, 'posts'); 
  5303. wp_cache_delete($id, 'post_meta'); 
  5304.  
  5305. if ( $clean_terms ) 
  5306. clean_object_term_cache($id, 'attachment'); 
  5307.  
  5308. /** 
  5309. * Fires after the given attachment's cache is cleaned. 
  5310. * 
  5311. * @since 3.0.0 
  5312. * 
  5313. * @param int $id Attachment ID. 
  5314. */ 
  5315. do_action( 'clean_attachment_cache', $id ); 
  5316.  
  5317. // 
  5318.  // Hooks 
  5319. // 
  5320.   
  5321. /** 
  5322. * Hook for managing future post transitions to published. 
  5323. * 
  5324. * @since 2.3.0 
  5325. * @access private 
  5326. * 
  5327. * @see wp_clear_scheduled_hook() 
  5328. * @global wpdb $wpdb WordPress database abstraction object. 
  5329. * 
  5330. * @param string $new_status New post status. 
  5331. * @param string $old_status Previous post status. 
  5332. * @param WP_Post $post Post object. 
  5333. */ 
  5334. function _transition_post_status( $new_status, $old_status, $post ) { 
  5335. global $wpdb; 
  5336.  
  5337. if ( $old_status != 'publish' && $new_status == 'publish' ) { 
  5338. // Reset GUID if transitioning to publish and it is empty. 
  5339. if ( '' == get_the_guid($post->ID) ) 
  5340. $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); 
  5341.  
  5342. /** 
  5343. * Fires when a post's status is transitioned from private to published. 
  5344. * 
  5345. * @since 1.5.0 
  5346. * @deprecated 2.3.0 Use 'private_to_publish' instead. 
  5347. * 
  5348. * @param int $post_id Post ID. 
  5349. */ 
  5350. do_action('private_to_published', $post->ID); 
  5351.  
  5352. // If published posts changed clear the lastpostmodified cache. 
  5353. if ( 'publish' == $new_status || 'publish' == $old_status) { 
  5354. foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { 
  5355. wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' ); 
  5356. wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' ); 
  5357. wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' ); 
  5358.  
  5359. if ( $new_status !== $old_status ) { 
  5360. wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' ); 
  5361. wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' ); 
  5362.  
  5363. // Always clears the hook in case the post status bounced from future to draft. 
  5364. wp_clear_scheduled_hook('publish_future_post', array( $post->ID ) ); 
  5365.  
  5366. /** 
  5367. * Hook used to schedule publication for a post marked for the future. 
  5368. * 
  5369. * The $post properties used and must exist are 'ID' and 'post_date_gmt'. 
  5370. * 
  5371. * @since 2.3.0 
  5372. * @access private 
  5373. * 
  5374. * @param int $deprecated Not used. Can be set to null. Never implemented. Not marked 
  5375. * as deprecated with _deprecated_argument() as it conflicts with 
  5376. * wp_transition_post_status() and the default filter for _future_post_hook(). 
  5377. * @param WP_Post $post Post object. 
  5378. */ 
  5379. function _future_post_hook( $deprecated, $post ) { 
  5380. wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); 
  5381. wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT') , 'publish_future_post', array( $post->ID ) ); 
  5382.  
  5383. /** 
  5384. * Hook to schedule pings and enclosures when a post is published. 
  5385. * 
  5386. * Uses XMLRPC_REQUEST and WP_IMPORTING constants. 
  5387. * 
  5388. * @since 2.3.0 
  5389. * @access private 
  5390. * 
  5391. * @param int $post_id The ID in the database table of the post being published. 
  5392. */ 
  5393. function _publish_post_hook( $post_id ) { 
  5394. if ( defined( 'XMLRPC_REQUEST' ) ) { 
  5395. /** 
  5396. * Fires when _publish_post_hook() is called during an XML-RPC request. 
  5397. * 
  5398. * @since 2.1.0 
  5399. * 
  5400. * @param int $post_id Post ID. 
  5401. */ 
  5402. do_action( 'xmlrpc_publish_post', $post_id ); 
  5403.  
  5404. if ( defined('WP_IMPORTING') ) 
  5405. return; 
  5406.  
  5407. if ( get_option('default_pingback_flag') ) 
  5408. add_post_meta( $post_id, '_pingme', '1' ); 
  5409. add_post_meta( $post_id, '_encloseme', '1' ); 
  5410.  
  5411. wp_schedule_single_event(time(), 'do_pings'); 
  5412.  
  5413. /** 
  5414. * Return the post's parent's post_ID 
  5415. * 
  5416. * @since 3.1.0 
  5417. * 
  5418. * @param int $post_ID 
  5419. * 
  5420. * @return int|false Post parent ID, otherwise false. 
  5421. */ 
  5422. function wp_get_post_parent_id( $post_ID ) { 
  5423. $post = get_post( $post_ID ); 
  5424. if ( !$post || is_wp_error( $post ) ) 
  5425. return false; 
  5426. return (int) $post->post_parent; 
  5427.  
  5428. /** 
  5429. * Check the given subset of the post hierarchy for hierarchy loops. 
  5430. * 
  5431. * Prevents loops from forming and breaks those that it finds. Attached 
  5432. * to the {@see 'wp_insert_post_parent'} filter. 
  5433. * 
  5434. * @since 3.1.0 
  5435. * 
  5436. * @see wp_find_hierarchy_loop() 
  5437. * 
  5438. * @param int $post_parent ID of the parent for the post we're checking. 
  5439. * @param int $post_ID ID of the post we're checking. 
  5440. * @return int The new post_parent for the post, 0 otherwise. 
  5441. */ 
  5442. function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) { 
  5443. // Nothing fancy here - bail. 
  5444. if ( !$post_parent ) 
  5445. return 0; 
  5446.  
  5447. // New post can't cause a loop. 
  5448. if ( empty( $post_ID ) ) 
  5449. return $post_parent; 
  5450.  
  5451. // Can't be its own parent. 
  5452. if ( $post_parent == $post_ID ) 
  5453. return 0; 
  5454.  
  5455. // Now look for larger loops. 
  5456. if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) ) 
  5457. return $post_parent; // No loop 
  5458.  
  5459. // Setting $post_parent to the given value causes a loop. 
  5460. if ( isset( $loop[$post_ID] ) ) 
  5461. return 0; 
  5462.  
  5463. // There's a loop, but it doesn't contain $post_ID. Break the loop. 
  5464. foreach ( array_keys( $loop ) as $loop_member ) 
  5465. wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) ); 
  5466.  
  5467. return $post_parent; 
  5468.  
  5469. /** 
  5470. * Set a post thumbnail. 
  5471. * 
  5472. * @since 3.1.0 
  5473. * 
  5474. * @param int|WP_Post $post Post ID or post object where thumbnail should be attached. 
  5475. * @param int $thumbnail_id Thumbnail to attach. 
  5476. * @return int|bool True on success, false on failure. 
  5477. */ 
  5478. function set_post_thumbnail( $post, $thumbnail_id ) { 
  5479. $post = get_post( $post ); 
  5480. $thumbnail_id = absint( $thumbnail_id ); 
  5481. if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) { 
  5482. if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) 
  5483. return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id ); 
  5484. else 
  5485. return delete_post_meta( $post->ID, '_thumbnail_id' ); 
  5486. return false; 
  5487.  
  5488. /** 
  5489. * Remove a post thumbnail. 
  5490. * 
  5491. * @since 3.3.0 
  5492. * 
  5493. * @param int|WP_Post $post Post ID or post object where thumbnail should be removed from. 
  5494. * @return bool True on success, false on failure. 
  5495. */ 
  5496. function delete_post_thumbnail( $post ) { 
  5497. $post = get_post( $post ); 
  5498. if ( $post ) 
  5499. return delete_post_meta( $post->ID, '_thumbnail_id' ); 
  5500. return false; 
  5501.  
  5502. /** 
  5503. * Delete auto-drafts for new posts that are > 7 days old. 
  5504. * 
  5505. * @since 3.4.0 
  5506. * 
  5507. * @global wpdb $wpdb WordPress database abstraction object. 
  5508. */ 
  5509. function wp_delete_auto_drafts() { 
  5510. global $wpdb; 
  5511.  
  5512. // Cleanup old auto-drafts more than 7 days old. 
  5513. $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" ); 
  5514. foreach ( (array) $old_posts as $delete ) { 
  5515. // Force delete. 
  5516. wp_delete_post( $delete, true ); 
  5517.  
  5518. /** 
  5519. * Queues posts for lazy-loading of term meta. 
  5520. * 
  5521. * @since 4.5.0 
  5522. * 
  5523. * @param array $posts Array of WP_Post objects. 
  5524. */ 
  5525. function wp_queue_posts_for_term_meta_lazyload( $posts ) { 
  5526. $post_type_taxonomies = $term_ids = array(); 
  5527. foreach ( $posts as $post ) { 
  5528. if ( ! ( $post instanceof WP_Post ) ) { 
  5529. continue; 
  5530.  
  5531. if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) { 
  5532. $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type ); 
  5533.  
  5534. foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { 
  5535. // Term cache should already be primed by `update_post_term_cache()`. 
  5536. $terms = get_object_term_cache( $post->ID, $taxonomy ); 
  5537. if ( false !== $terms ) { 
  5538. foreach ( $terms as $term ) { 
  5539. if ( ! isset( $term_ids[ $term->term_id ] ) ) { 
  5540. $term_ids[] = $term->term_id; 
  5541.  
  5542. if ( $term_ids ) { 
  5543. $lazyloader = wp_metadata_lazyloader(); 
  5544. $lazyloader->queue_objects( 'term', $term_ids ); 
  5545.  
  5546. /** 
  5547. * Update the custom taxonomies' term counts when a post's status is changed. 
  5548. * 
  5549. * For example, default posts term counts (for custom taxonomies) don't include 
  5550. * private / draft posts. 
  5551. * 
  5552. * @since 3.3.0 
  5553. * @access private 
  5554. * 
  5555. * @param string $new_status New post status. 
  5556. * @param string $old_status Old post status. 
  5557. * @param WP_Post $post Post object. 
  5558. */ 
  5559. function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { 
  5560. // Update counts for the post's terms. 
  5561. foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { 
  5562. $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); 
  5563. wp_update_term_count( $tt_ids, $taxonomy ); 
  5564.  
  5565. /** 
  5566. * Adds any posts from the given ids to the cache that do not already exist in cache 
  5567. * 
  5568. * @since 3.4.0 
  5569. * @access private 
  5570. * 
  5571. * @see update_post_caches() 
  5572. * 
  5573. * @global wpdb $wpdb WordPress database abstraction object. 
  5574. * 
  5575. * @param array $ids ID list. 
  5576. * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. 
  5577. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 
  5578. */ 
  5579. function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { 
  5580. global $wpdb; 
  5581.  
  5582. $non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); 
  5583. if ( !empty( $non_cached_ids ) ) { 
  5584. $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ", ", $non_cached_ids ) ) ); 
  5585.  
  5586. update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); 
  5587.  
  5588. /** 
  5589. * Adds a suffix if any trashed posts have a given slug. 
  5590. * 
  5591. * Store its desired (i.e. current) slug so it can try to reclaim it 
  5592. * if the post is untrashed. 
  5593. * 
  5594. * For internal use. 
  5595. * 
  5596. * @since 4.5.0 
  5597. * @access private 
  5598. * 
  5599. * @param string $post_name Slug. 
  5600. * @param string $post_ID Optional. Post ID that should be ignored. Default 0. 
  5601. */ 
  5602. function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) { 
  5603. $trashed_posts_with_desired_slug = get_posts( array( 
  5604. 'name' => $post_name,  
  5605. 'post_status' => 'trash',  
  5606. 'post_type' => 'any',  
  5607. 'nopaging' => true,  
  5608. 'post__not_in' => array( $post_ID ) 
  5609. ) ); 
  5610.  
  5611. if ( ! empty( $trashed_posts_with_desired_slug ) ) { 
  5612. foreach ( $trashed_posts_with_desired_slug as $_post ) { 
  5613. wp_add_trashed_suffix_to_post_name_for_post( $_post ); 
  5614.  
  5615. /** 
  5616. * Adds a trashed suffix for a given post. 
  5617. * 
  5618. * Store its desired (i.e. current) slug so it can try to reclaim it 
  5619. * if the post is untrashed. 
  5620. * 
  5621. * For internal use. 
  5622. * 
  5623. * @since 4.5.0 
  5624. * @access private 
  5625. * 
  5626. * @param WP_Post $post The post. 
  5627. * @return string New slug for the post. 
  5628. */ 
  5629. function wp_add_trashed_suffix_to_post_name_for_post( $post ) { 
  5630. global $wpdb; 
  5631.  
  5632. $post = get_post( $post ); 
  5633.  
  5634. if ( '__trashed' === substr( $post->post_name, -9 ) ) { 
  5635. return $post->post_name; 
  5636. add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name ); 
  5637. $post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed'; 
  5638. $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) ); 
  5639. clean_post_cache( $post->ID ); 
  5640. return $post_name; 
.