PageRenderTime 64ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/post.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 5846 lines | 2473 code | 714 blank | 2659 comment | 684 complexity | 15e634132196410e34dcf50820fcd942 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0

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

  1. <?php
  2. /**
  3. * Post functions and post utility function.
  4. *
  5. * @package WordPress
  6. * @subpackage Post
  7. * @since 1.5.0
  8. */
  9. //
  10. // Post Type Registration
  11. //
  12. /**
  13. * Creates the initial post types when 'init' action is fired.
  14. *
  15. * @since 2.9.0
  16. */
  17. function create_initial_post_types() {
  18. register_post_type( 'post', array(
  19. 'labels' => array(
  20. 'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
  21. ),
  22. 'public' => true,
  23. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  24. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  25. 'capability_type' => 'post',
  26. 'map_meta_cap' => true,
  27. 'hierarchical' => false,
  28. 'rewrite' => false,
  29. 'query_var' => false,
  30. 'delete_with_user' => true,
  31. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
  32. ) );
  33. register_post_type( 'page', array(
  34. 'labels' => array(
  35. 'name_admin_bar' => _x( 'Page', 'add new on admin bar' ),
  36. ),
  37. 'public' => true,
  38. 'publicly_queryable' => false,
  39. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  40. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  41. 'capability_type' => 'page',
  42. 'map_meta_cap' => true,
  43. 'hierarchical' => true,
  44. 'rewrite' => false,
  45. 'query_var' => false,
  46. 'delete_with_user' => true,
  47. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
  48. ) );
  49. register_post_type( 'attachment', array(
  50. 'labels' => array(
  51. 'name' => _x('Media', 'post type general name'),
  52. 'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),
  53. 'add_new' => _x( 'Add New', 'add new media' ),
  54. 'edit_item' => __( 'Edit Media' ),
  55. 'view_item' => __( 'View Attachment Page' ),
  56. ),
  57. 'public' => true,
  58. 'show_ui' => true,
  59. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  60. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  61. 'capability_type' => 'post',
  62. 'capabilities' => array(
  63. 'create_posts' => 'upload_files',
  64. ),
  65. 'map_meta_cap' => true,
  66. 'hierarchical' => false,
  67. 'rewrite' => false,
  68. 'query_var' => false,
  69. 'show_in_nav_menus' => false,
  70. 'delete_with_user' => true,
  71. 'supports' => array( 'title', 'author', 'comments' ),
  72. ) );
  73. add_post_type_support( 'attachment:audio', 'thumbnail' );
  74. add_post_type_support( 'attachment:video', 'thumbnail' );
  75. register_post_type( 'revision', array(
  76. 'labels' => array(
  77. 'name' => __( 'Revisions' ),
  78. 'singular_name' => __( 'Revision' ),
  79. ),
  80. 'public' => false,
  81. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  82. '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
  83. 'capability_type' => 'post',
  84. 'map_meta_cap' => true,
  85. 'hierarchical' => false,
  86. 'rewrite' => false,
  87. 'query_var' => false,
  88. 'can_export' => false,
  89. 'delete_with_user' => true,
  90. 'supports' => array( 'author' ),
  91. ) );
  92. register_post_type( 'nav_menu_item', array(
  93. 'labels' => array(
  94. 'name' => __( 'Navigation Menu Items' ),
  95. 'singular_name' => __( 'Navigation Menu Item' ),
  96. ),
  97. 'public' => false,
  98. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  99. 'hierarchical' => false,
  100. 'rewrite' => false,
  101. 'delete_with_user' => false,
  102. 'query_var' => false,
  103. ) );
  104. register_post_status( 'publish', array(
  105. 'label' => _x( 'Published', 'post' ),
  106. 'public' => true,
  107. '_builtin' => true, /* internal use only. */
  108. 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),
  109. ) );
  110. register_post_status( 'future', array(
  111. 'label' => _x( 'Scheduled', 'post' ),
  112. 'protected' => true,
  113. '_builtin' => true, /* internal use only. */
  114. 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),
  115. ) );
  116. register_post_status( 'draft', array(
  117. 'label' => _x( 'Draft', 'post' ),
  118. 'protected' => true,
  119. '_builtin' => true, /* internal use only. */
  120. 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
  121. ) );
  122. register_post_status( 'pending', array(
  123. 'label' => _x( 'Pending', 'post' ),
  124. 'protected' => true,
  125. '_builtin' => true, /* internal use only. */
  126. 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),
  127. ) );
  128. register_post_status( 'private', array(
  129. 'label' => _x( 'Private', 'post' ),
  130. 'private' => true,
  131. '_builtin' => true, /* internal use only. */
  132. 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),
  133. ) );
  134. register_post_status( 'trash', array(
  135. 'label' => _x( 'Trash', 'post' ),
  136. 'internal' => true,
  137. '_builtin' => true, /* internal use only. */
  138. 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
  139. 'show_in_admin_status_list' => true,
  140. ) );
  141. register_post_status( 'auto-draft', array(
  142. 'label' => 'auto-draft',
  143. 'internal' => true,
  144. '_builtin' => true, /* internal use only. */
  145. ) );
  146. register_post_status( 'inherit', array(
  147. 'label' => 'inherit',
  148. 'internal' => true,
  149. '_builtin' => true, /* internal use only. */
  150. 'exclude_from_search' => false,
  151. ) );
  152. }
  153. add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
  154. /**
  155. * Retrieve attached file path based on attachment ID.
  156. *
  157. * By default the path will go through the 'get_attached_file' filter, but
  158. * passing a true to the $unfiltered argument of get_attached_file() will
  159. * return the file path unfiltered.
  160. *
  161. * The function works by getting the single post meta name, named
  162. * '_wp_attached_file' and returning it. This is a convenience function to
  163. * prevent looking up the meta name and provide a mechanism for sending the
  164. * attached filename through a filter.
  165. *
  166. * @since 2.0.0
  167. *
  168. * @param int $attachment_id Attachment ID.
  169. * @param bool $unfiltered Optional. Whether to apply filters. Default false.
  170. * @return string|bool The file path to where the attached file should be, false otherwise.
  171. */
  172. function get_attached_file( $attachment_id, $unfiltered = false ) {
  173. $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
  174. // If the file is relative, prepend upload dir.
  175. if ( $file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
  176. $file = $uploads['basedir'] . "/$file";
  177. if ( $unfiltered )
  178. return $file;
  179. /**
  180. * Filter the attached file based on the given ID.
  181. *
  182. * @since 2.1.0
  183. *
  184. * @param string $file Path to attached file.
  185. * @param int $attachment_id Attachment ID.
  186. */
  187. return apply_filters( 'get_attached_file', $file, $attachment_id );
  188. }
  189. /**
  190. * Update attachment file path based on attachment ID.
  191. *
  192. * Used to update the file path of the attachment, which uses post meta name
  193. * '_wp_attached_file' to store the path of the attachment.
  194. *
  195. * @since 2.1.0
  196. *
  197. * @param int $attachment_id Attachment ID.
  198. * @param string $file File path for the attachment.
  199. * @return bool True on success, false on failure.
  200. */
  201. function update_attached_file( $attachment_id, $file ) {
  202. if ( !get_post( $attachment_id ) )
  203. return false;
  204. /**
  205. * Filter the path to the attached file to update.
  206. *
  207. * @since 2.1.0
  208. *
  209. * @param string $file Path to the attached file to update.
  210. * @param int $attachment_id Attachment ID.
  211. */
  212. $file = apply_filters( 'update_attached_file', $file, $attachment_id );
  213. if ( $file = _wp_relative_upload_path( $file ) )
  214. return update_post_meta( $attachment_id, '_wp_attached_file', $file );
  215. else
  216. return delete_post_meta( $attachment_id, '_wp_attached_file' );
  217. }
  218. /**
  219. * Return relative path to an uploaded file.
  220. *
  221. * The path is relative to the current upload dir.
  222. *
  223. * @since 2.9.0
  224. *
  225. * @param string $path Full path to the file.
  226. * @return string Relative path on success, unchanged path on failure.
  227. */
  228. function _wp_relative_upload_path( $path ) {
  229. $new_path = $path;
  230. $uploads = wp_upload_dir();
  231. if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) {
  232. $new_path = str_replace( $uploads['basedir'], '', $new_path );
  233. $new_path = ltrim( $new_path, '/' );
  234. }
  235. /**
  236. * Filter the relative path to an uploaded file.
  237. *
  238. * @since 2.9.0
  239. *
  240. * @param string $new_path Relative path to the file.
  241. * @param string $path Full path to the file.
  242. */
  243. return apply_filters( '_wp_relative_upload_path', $new_path, $path );
  244. }
  245. /**
  246. * Retrieve all children of the post parent ID.
  247. *
  248. * Normally, without any enhancements, the children would apply to pages. In the
  249. * context of the inner workings of WordPress, pages, posts, and attachments
  250. * share the same table, so therefore the functionality could apply to any one
  251. * of them. It is then noted that while this function does not work on posts, it
  252. * does not mean that it won't work on posts. It is recommended that you know
  253. * what context you wish to retrieve the children of.
  254. *
  255. * Attachments may also be made the child of a post, so if that is an accurate
  256. * statement (which needs to be verified), it would then be possible to get
  257. * all of the attachments for a post. Attachments have since changed since
  258. * version 2.5, so this is most likely unaccurate, but serves generally as an
  259. * example of what is possible.
  260. *
  261. * The arguments listed as defaults are for this function and also of the
  262. * {@link get_posts()} function. The arguments are combined with the
  263. * get_children defaults and are then passed to the {@link get_posts()}
  264. * function, which accepts additional arguments. You can replace the defaults in
  265. * this function, listed below and the additional arguments listed in the
  266. * {@link get_posts()} function.
  267. *
  268. * The 'post_parent' is the most important argument and important attention
  269. * needs to be paid to the $args parameter. If you pass either an object or an
  270. * integer (number), then just the 'post_parent' is grabbed and everything else
  271. * is lost. If you don't specify any arguments, then it is assumed that you are
  272. * in The Loop and the post parent will be grabbed for from the current post.
  273. *
  274. * The 'post_parent' argument is the ID to get the children. The 'numberposts'
  275. * is the amount of posts to retrieve that has a default of '-1', which is
  276. * used to get all of the posts. Giving a number higher than 0 will only
  277. * retrieve that amount of posts.
  278. *
  279. * The 'post_type' and 'post_status' arguments can be used to choose what
  280. * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
  281. * post types are 'post', 'pages', and 'attachments'. The 'post_status'
  282. * argument will accept any post status within the write administration panels.
  283. *
  284. * @internal Claims made in the long description might be inaccurate.
  285. * @since 2.0.0
  286. *
  287. * @see get_posts()
  288. *
  289. * @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty.
  290. * @param string $output Optional. Constant for return type. Accepts OBJECT, ARRAY_A, ARRAY_N.
  291. * Default OBJECt.
  292. * @return array Array of children, where the type of each element is determined by $output parameter.
  293. * Empty array on failure.
  294. */
  295. function get_children( $args = '', $output = OBJECT ) {
  296. $kids = array();
  297. if ( empty( $args ) ) {
  298. if ( isset( $GLOBALS['post'] ) ) {
  299. $args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
  300. } else {
  301. return $kids;
  302. }
  303. } elseif ( is_object( $args ) ) {
  304. $args = array('post_parent' => (int) $args->post_parent );
  305. } elseif ( is_numeric( $args ) ) {
  306. $args = array('post_parent' => (int) $args);
  307. }
  308. $defaults = array(
  309. 'numberposts' => -1, 'post_type' => 'any',
  310. 'post_status' => 'any', 'post_parent' => 0,
  311. );
  312. $r = wp_parse_args( $args, $defaults );
  313. $children = get_posts( $r );
  314. if ( ! $children )
  315. return $kids;
  316. if ( ! empty( $r['fields'] ) )
  317. return $children;
  318. update_post_cache($children);
  319. foreach ( $children as $key => $child )
  320. $kids[$child->ID] = $children[$key];
  321. if ( $output == OBJECT ) {
  322. return $kids;
  323. } elseif ( $output == ARRAY_A ) {
  324. foreach ( (array) $kids as $kid )
  325. $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
  326. return $weeuns;
  327. } elseif ( $output == ARRAY_N ) {
  328. foreach ( (array) $kids as $kid )
  329. $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
  330. return $babes;
  331. } else {
  332. return $kids;
  333. }
  334. }
  335. /**
  336. * Get extended entry info (<!--more-->).
  337. *
  338. * There should not be any space after the second dash and before the word
  339. * 'more'. There can be text or space(s) after the word 'more', but won't be
  340. * referenced.
  341. *
  342. * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before
  343. * the `<!--more-->`. The 'extended' key has the content after the
  344. * `<!--more-->` comment. The 'more_text' key has the custom "Read More" text.
  345. *
  346. * @since 1.0.0
  347. *
  348. * @param string $post Post content.
  349. * @return array Post before ('main'), after ('extended'), and custom readmore ('more_text').
  350. */
  351. function get_extended( $post ) {
  352. //Match the new style more links.
  353. if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
  354. list($main, $extended) = explode($matches[0], $post, 2);
  355. $more_text = $matches[1];
  356. } else {
  357. $main = $post;
  358. $extended = '';
  359. $more_text = '';
  360. }
  361. // leading and trailing whitespace.
  362. $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
  363. $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
  364. $more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text);
  365. return array( 'main' => $main, 'extended' => $extended, 'more_text' => $more_text );
  366. }
  367. /**
  368. * Retrieves post data given a post ID or post object.
  369. *
  370. * See {@link sanitize_post()} for optional $filter values. Also, the parameter
  371. * $post, must be given as a variable, since it is passed by reference.
  372. *
  373. * @since 1.5.1
  374. *
  375. * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
  376. * @param string $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N.
  377. * Default OBJECT.
  378. * @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
  379. * or 'display'. Default 'raw'.
  380. * @return WP_Post|null WP_Post on success or null on failure.
  381. */
  382. function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
  383. if ( empty( $post ) && isset( $GLOBALS['post'] ) )
  384. $post = $GLOBALS['post'];
  385. if ( is_a( $post, 'WP_Post' ) ) {
  386. $_post = $post;
  387. } elseif ( is_object( $post ) ) {
  388. if ( empty( $post->filter ) ) {
  389. $_post = sanitize_post( $post, 'raw' );
  390. $_post = new WP_Post( $_post );
  391. } elseif ( 'raw' == $post->filter ) {
  392. $_post = new WP_Post( $post );
  393. } else {
  394. $_post = WP_Post::get_instance( $post->ID );
  395. }
  396. } else {
  397. $_post = WP_Post::get_instance( $post );
  398. }
  399. if ( ! $_post )
  400. return null;
  401. $_post = $_post->filter( $filter );
  402. if ( $output == ARRAY_A )
  403. return $_post->to_array();
  404. elseif ( $output == ARRAY_N )
  405. return array_values( $_post->to_array() );
  406. return $_post;
  407. }
  408. /**
  409. * WordPress Post class.
  410. *
  411. * @since 3.5.0
  412. *
  413. */
  414. final class WP_Post {
  415. /**
  416. * Post ID.
  417. *
  418. * @var int
  419. */
  420. public $ID;
  421. /**
  422. * ID of post author.
  423. *
  424. * A numeric string, for compatibility reasons.
  425. *
  426. * @var string
  427. */
  428. public $post_author = 0;
  429. /**
  430. * The post's local publication time.
  431. *
  432. * @var string
  433. */
  434. public $post_date = '0000-00-00 00:00:00';
  435. /**
  436. * The post's GMT publication time.
  437. *
  438. * @var string
  439. */
  440. public $post_date_gmt = '0000-00-00 00:00:00';
  441. /**
  442. * The post's content.
  443. *
  444. * @var string
  445. */
  446. public $post_content = '';
  447. /**
  448. * The post's title.
  449. *
  450. * @var string
  451. */
  452. public $post_title = '';
  453. /**
  454. * The post's excerpt.
  455. *
  456. * @var string
  457. */
  458. public $post_excerpt = '';
  459. /**
  460. * The post's status.
  461. *
  462. * @var string
  463. */
  464. public $post_status = 'publish';
  465. /**
  466. * Whether comments are allowed.
  467. *
  468. * @var string
  469. */
  470. public $comment_status = 'open';
  471. /**
  472. * Whether pings are allowed.
  473. *
  474. * @var string
  475. */
  476. public $ping_status = 'open';
  477. /**
  478. * The post's password in plain text.
  479. *
  480. * @var string
  481. */
  482. public $post_password = '';
  483. /**
  484. * The post's slug.
  485. *
  486. * @var string
  487. */
  488. public $post_name = '';
  489. /**
  490. * URLs queued to be pinged.
  491. *
  492. * @var string
  493. */
  494. public $to_ping = '';
  495. /**
  496. * URLs that have been pinged.
  497. *
  498. * @var string
  499. */
  500. public $pinged = '';
  501. /**
  502. * The post's local modified time.
  503. *
  504. * @var string
  505. */
  506. public $post_modified = '0000-00-00 00:00:00';
  507. /**
  508. * The post's GMT modified time.
  509. *
  510. * @var string
  511. */
  512. public $post_modified_gmt = '0000-00-00 00:00:00';
  513. /**
  514. * A utility DB field for post content.
  515. *
  516. *
  517. * @var string
  518. */
  519. public $post_content_filtered = '';
  520. /**
  521. * ID of a post's parent post.
  522. *
  523. * @var int
  524. */
  525. public $post_parent = 0;
  526. /**
  527. * The unique identifier for a post, not necessarily a URL, used as the feed GUID.
  528. *
  529. * @var string
  530. */
  531. public $guid = '';
  532. /**
  533. * A field used for ordering posts.
  534. *
  535. * @var int
  536. */
  537. public $menu_order = 0;
  538. /**
  539. * The post's type, like post or page.
  540. *
  541. * @var string
  542. */
  543. public $post_type = 'post';
  544. /**
  545. * An attachment's mime type.
  546. *
  547. * @var string
  548. */
  549. public $post_mime_type = '';
  550. /**
  551. * Cached comment count.
  552. *
  553. * A numeric string, for compatibility reasons.
  554. *
  555. * @var string
  556. */
  557. public $comment_count = 0;
  558. /**
  559. * Stores the post object's sanitization level.
  560. *
  561. * Does not correspond to a DB field.
  562. *
  563. * @var string
  564. */
  565. public $filter;
  566. /**
  567. * Retrieve WP_Post instance.
  568. *
  569. * @static
  570. * @access public
  571. *
  572. * @param int $post_id Post ID.
  573. * @return WP_Post|bool Post object, false otherwise.
  574. */
  575. public static function get_instance( $post_id ) {
  576. global $wpdb;
  577. $post_id = (int) $post_id;
  578. if ( ! $post_id )
  579. return false;
  580. $_post = wp_cache_get( $post_id, 'posts' );
  581. if ( ! $_post ) {
  582. $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
  583. if ( ! $_post )
  584. return false;
  585. $_post = sanitize_post( $_post, 'raw' );
  586. wp_cache_add( $_post->ID, $_post, 'posts' );
  587. } elseif ( empty( $_post->filter ) ) {
  588. $_post = sanitize_post( $_post, 'raw' );
  589. }
  590. return new WP_Post( $_post );
  591. }
  592. /**
  593. * Constructor.
  594. *
  595. * @param WP_Post $post Post object.
  596. */
  597. public function __construct( $post ) {
  598. foreach ( get_object_vars( $post ) as $key => $value )
  599. $this->$key = $value;
  600. }
  601. /**
  602. * Isset-er.
  603. *
  604. * @param string $key Property to check if set.
  605. * @return bool
  606. */
  607. public function __isset( $key ) {
  608. if ( 'ancestors' == $key )
  609. return true;
  610. if ( 'page_template' == $key )
  611. return ( 'page' == $this->post_type );
  612. if ( 'post_category' == $key )
  613. return true;
  614. if ( 'tags_input' == $key )
  615. return true;
  616. return metadata_exists( 'post', $this->ID, $key );
  617. }
  618. /**
  619. * Getter.
  620. *
  621. * @param string $key Key to get.
  622. * @return array|mixed
  623. */
  624. public function __get( $key ) {
  625. if ( 'page_template' == $key && $this->__isset( $key ) ) {
  626. return get_post_meta( $this->ID, '_wp_page_template', true );
  627. }
  628. if ( 'post_category' == $key ) {
  629. if ( is_object_in_taxonomy( $this->post_type, 'category' ) )
  630. $terms = get_the_terms( $this, 'category' );
  631. if ( empty( $terms ) )
  632. return array();
  633. return wp_list_pluck( $terms, 'term_id' );
  634. }
  635. if ( 'tags_input' == $key ) {
  636. if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) )
  637. $terms = get_the_terms( $this, 'post_tag' );
  638. if ( empty( $terms ) )
  639. return array();
  640. return wp_list_pluck( $terms, 'name' );
  641. }
  642. // Rest of the values need filtering.
  643. if ( 'ancestors' == $key )
  644. $value = get_post_ancestors( $this );
  645. else
  646. $value = get_post_meta( $this->ID, $key, true );
  647. if ( $this->filter )
  648. $value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
  649. return $value;
  650. }
  651. /**
  652. * {@Missing Summary}
  653. *
  654. * @param string $filter Filter.
  655. * @return $this|array|bool|object|WP_Post
  656. */
  657. public function filter( $filter ) {
  658. if ( $this->filter == $filter )
  659. return $this;
  660. if ( $filter == 'raw' )
  661. return self::get_instance( $this->ID );
  662. return sanitize_post( $this, $filter );
  663. }
  664. /**
  665. * Convert object to array.
  666. *
  667. * @return array Object as array.
  668. */
  669. public function to_array() {
  670. $post = get_object_vars( $this );
  671. foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
  672. if ( $this->__isset( $key ) )
  673. $post[ $key ] = $this->__get( $key );
  674. }
  675. return $post;
  676. }
  677. }
  678. /**
  679. * Retrieve ancestors of a post.
  680. *
  681. * @since 2.5.0
  682. *
  683. * @param int|WP_Post $post Post ID or post object.
  684. * @return array Ancestor IDs or empty array if none are found.
  685. */
  686. function get_post_ancestors( $post ) {
  687. $post = get_post( $post );
  688. if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID )
  689. return array();
  690. $ancestors = array();
  691. $id = $ancestors[] = $post->post_parent;
  692. while ( $ancestor = get_post( $id ) ) {
  693. // Loop detection: If the ancestor has been seen before, break.
  694. if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
  695. break;
  696. $id = $ancestors[] = $ancestor->post_parent;
  697. }
  698. return $ancestors;
  699. }
  700. /**
  701. * Retrieve data from a post field based on Post ID.
  702. *
  703. * Examples of the post field will be, 'post_type', 'post_status', 'post_content',
  704. * etc and based off of the post object property or key names.
  705. *
  706. * The context values are based off of the taxonomy filter functions and
  707. * supported values are found within those functions.
  708. *
  709. * @since 2.3.0
  710. *
  711. * @see sanitize_post_field()
  712. *
  713. * @param string $field Post field name.
  714. * @param int|WP_Post $post Post ID or post object.
  715. * @param string $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
  716. * or 'display'. Default 'display'.
  717. * @return string The value of the post field on success, empty string on failure.
  718. */
  719. function get_post_field( $field, $post, $context = 'display' ) {
  720. $post = get_post( $post );
  721. if ( !$post )
  722. return '';
  723. if ( !isset($post->$field) )
  724. return '';
  725. return sanitize_post_field($field, $post->$field, $post->ID, $context);
  726. }
  727. /**
  728. * Retrieve the mime type of an attachment based on the ID.
  729. *
  730. * This function can be used with any post type, but it makes more sense with
  731. * attachments.
  732. *
  733. * @since 2.0.0
  734. *
  735. * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
  736. * @return string|false The mime type on success, false on failure.
  737. */
  738. function get_post_mime_type( $ID = '' ) {
  739. $post = get_post($ID);
  740. if ( is_object($post) )
  741. return $post->post_mime_type;
  742. return false;
  743. }
  744. /**
  745. * Retrieve the post status based on the Post ID.
  746. *
  747. * If the post ID is of an attachment, then the parent post status will be given
  748. * instead.
  749. *
  750. * @since 2.0.0
  751. *
  752. * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
  753. * @return string|false Post status on success, false on failure.
  754. */
  755. function get_post_status( $ID = '' ) {
  756. $post = get_post($ID);
  757. if ( !is_object($post) )
  758. return false;
  759. if ( 'attachment' == $post->post_type ) {
  760. if ( 'private' == $post->post_status )
  761. return 'private';
  762. // Unattached attachments are assumed to be published.
  763. if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) )
  764. return 'publish';
  765. // Inherit status from the parent.
  766. if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) {
  767. $parent_post_status = get_post_status( $post->post_parent );
  768. if ( 'trash' == $parent_post_status ) {
  769. return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
  770. } else {
  771. return $parent_post_status;
  772. }
  773. }
  774. }
  775. return $post->post_status;
  776. }
  777. /**
  778. * Retrieve all of the WordPress supported post statuses.
  779. *
  780. * Posts have a limited set of valid status values, this provides the
  781. * post_status values and descriptions.
  782. *
  783. * @since 2.5.0
  784. *
  785. * @return array List of post statuses.
  786. */
  787. function get_post_statuses() {
  788. $status = array(
  789. 'draft' => __( 'Draft' ),
  790. 'pending' => __( 'Pending Review' ),
  791. 'private' => __( 'Private' ),
  792. 'publish' => __( 'Published' )
  793. );
  794. return $status;
  795. }
  796. /**
  797. * Retrieve all of the WordPress support page statuses.
  798. *
  799. * Pages have a limited set of valid status values, this provides the
  800. * post_status values and descriptions.
  801. *
  802. * @since 2.5.0
  803. *
  804. * @return array List of page statuses.
  805. */
  806. function get_page_statuses() {
  807. $status = array(
  808. 'draft' => __( 'Draft' ),
  809. 'private' => __( 'Private' ),
  810. 'publish' => __( 'Published' )
  811. );
  812. return $status;
  813. }
  814. /**
  815. * Register a post status. Do not use before init.
  816. *
  817. * A simple function for creating or modifying a post status based on the
  818. * parameters given. The function will accept an array (second optional
  819. * parameter), along with a string for the post status name.
  820. *
  821. * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
  822. *
  823. * @since 3.0.0
  824. * @uses $wp_post_statuses Inserts new post status object into the list
  825. *
  826. * @param string $post_status Name of the post status.
  827. * @param array|string $args {
  828. * Optional. Array or string of post status arguments.
  829. *
  830. * @type bool|string $label A descriptive name for the post status marked
  831. * for translation. Defaults to value of $post_status.
  832. * @type bool|array $label_count Descriptive text to use for nooped plurals.
  833. * Default array of $label, twice
  834. * @type bool $exclude_from_search Whether to exclude posts with this post status
  835. * from search results. Default is value of $internal.
  836. * @type bool $_builtin Whether the status is built-in. Core-use only.
  837. * Default false.
  838. * @type bool $public Whether posts of this status should be shown
  839. * in the front end of the site. Default true.
  840. * @type bool $internal Whether the status is for internal use only.
  841. * Default false.
  842. * @type bool $protected Whether posts with this status should be protected.
  843. * Default false.
  844. * @type bool $private Whether posts with this status should be private.
  845. * Default false.
  846. * @type bool $publicly_queryable Whether posts with this status should be publicly-
  847. * queryable. Default is value of $public.
  848. * @type bool $show_in_admin_all_list Whether to include posts in the edit listing for
  849. * their post type. Default is value of $internal.
  850. * @type bool $show_in_admin_status_list Show in the list of statuses with post counts at
  851. * the top of the edit listings,
  852. * e.g. All (12) | Published (9) | My Custom Status (2)
  853. * Default is value of $internal.
  854. * }
  855. */
  856. function register_post_status( $post_status, $args = array() ) {
  857. global $wp_post_statuses;
  858. if (!is_array($wp_post_statuses))
  859. $wp_post_statuses = array();
  860. // Args prefixed with an underscore are reserved for internal use.
  861. $defaults = array(
  862. 'label' => false,
  863. 'label_count' => false,
  864. 'exclude_from_search' => null,
  865. '_builtin' => false,
  866. 'public' => null,
  867. 'internal' => null,
  868. 'protected' => null,
  869. 'private' => null,
  870. 'publicly_queryable' => null,
  871. 'show_in_admin_status_list' => null,
  872. 'show_in_admin_all_list' => null,
  873. );
  874. $args = wp_parse_args($args, $defaults);
  875. $args = (object) $args;
  876. $post_status = sanitize_key($post_status);
  877. $args->name = $post_status;
  878. // Set various defaults.
  879. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
  880. $args->internal = true;
  881. if ( null === $args->public )
  882. $args->public = false;
  883. if ( null === $args->private )
  884. $args->private = false;
  885. if ( null === $args->protected )
  886. $args->protected = false;
  887. if ( null === $args->internal )
  888. $args->internal = false;
  889. if ( null === $args->publicly_queryable )
  890. $args->publicly_queryable = $args->public;
  891. if ( null === $args->exclude_from_search )
  892. $args->exclude_from_search = $args->internal;
  893. if ( null === $args->show_in_admin_all_list )
  894. $args->show_in_admin_all_list = !$args->internal;
  895. if ( null === $args->show_in_admin_status_list )
  896. $args->show_in_admin_status_list = !$args->internal;
  897. if ( false === $args->label )
  898. $args->label = $post_status;
  899. if ( false === $args->label_count )
  900. $args->label_count = array( $args->label, $args->label );
  901. $wp_post_statuses[$post_status] = $args;
  902. return $args;
  903. }
  904. /**
  905. * Retrieve a post status object by name.
  906. *
  907. * @since 3.0.0
  908. *
  909. * @global array $wp_post_statuses List of post statuses.
  910. *
  911. * @see register_post_status()
  912. *
  913. * @param string $post_status The name of a registered post status.
  914. * @return object A post status object.
  915. */
  916. function get_post_status_object( $post_status ) {
  917. global $wp_post_statuses;
  918. if ( empty($wp_post_statuses[$post_status]) )
  919. return null;
  920. return $wp_post_statuses[$post_status];
  921. }
  922. /**
  923. * Get a list of post statuses.
  924. *
  925. * @since 3.0.0
  926. *
  927. * @global array $wp_post_statuses List of post statuses.
  928. *
  929. * @see register_post_status()
  930. *
  931. * @param array|string $args Optional. Array or string of post status arguments to compare against
  932. * properties of the global `$wp_post_statuses objects`. Default empty array.
  933. * @param string $output Optional. The type of output to return, either 'names' or 'objects'. Default 'names'.
  934. * @param string $operator Optional. The logical operation to perform. 'or' means only one element
  935. * from the array needs to match; 'and' means all elements must match.
  936. * Default 'and'.
  937. * @return array A list of post status names or objects.
  938. */
  939. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
  940. global $wp_post_statuses;
  941. $field = ('names' == $output) ? 'name' : false;
  942. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
  943. }
  944. /**
  945. * Whether the post type is hierarchical.
  946. *
  947. * A false return value might also mean that the post type does not exist.
  948. *
  949. * @since 3.0.0
  950. *
  951. * @see get_post_type_object()
  952. *
  953. * @param string $post_type Post type name
  954. * @return bool Whether post type is hierarchical.
  955. */
  956. function is_post_type_hierarchical( $post_type ) {
  957. if ( ! post_type_exists( $post_type ) )
  958. return false;
  959. $post_type = get_post_type_object( $post_type );
  960. return $post_type->hierarchical;
  961. }
  962. /**
  963. * Check if a post type is registered.
  964. *
  965. * @since 3.0.0
  966. *
  967. * @see get_post_type_object()
  968. *
  969. * @param string $post_type Post type name.
  970. * @return bool Whether post type is registered.
  971. */
  972. function post_type_exists( $post_type ) {
  973. return (bool) get_post_type_object( $post_type );
  974. }
  975. /**
  976. * Retrieve the post type of the current post or of a given post.
  977. *
  978. * @since 2.1.0
  979. *
  980. * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
  981. * @return string|false Post type on success, false on failure.
  982. */
  983. function get_post_type( $post = null ) {
  984. if ( $post = get_post( $post ) )
  985. return $post->post_type;
  986. return false;
  987. }
  988. /**
  989. * Retrieve a post type object by name.
  990. *
  991. * @since 3.0.0
  992. *
  993. * @global array $wp_post_types List of post types.
  994. *
  995. * @see register_post_type()
  996. *
  997. * @param string $post_type The name of a registered post type.
  998. * @return object A post type object.
  999. */
  1000. function get_post_type_object( $post_type ) {
  1001. global $wp_post_types;
  1002. if ( empty($wp_post_types[$post_type]) )
  1003. return null;
  1004. return $wp_post_types[$post_type];
  1005. }
  1006. /**
  1007. * Get a list of all registered post type objects.
  1008. *
  1009. * @since 2.9.0
  1010. *
  1011. * @global array $wp_post_types List of post types.
  1012. *
  1013. * @see register_post_type() for accepted arguments.
  1014. *
  1015. * @param array|string $args Optional. An array of key => value arguments to match against
  1016. * the post type objects. Default empty array.
  1017. * @param string $output Optional. The type of output to return. Accepts post type 'names'
  1018. * or 'objects'. Default 'names'.
  1019. * @param string $operator Optional. The logical operation to perform. 'or' means only one
  1020. * element from the array needs to match; 'and' means all elements
  1021. * must match. Accepts 'or' or 'and'. Default 'and'.
  1022. * @return array A list of post type names or objects.
  1023. */
  1024. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
  1025. global $wp_post_types;
  1026. $field = ('names' == $output) ? 'name' : false;
  1027. return wp_filter_object_list($wp_post_types, $args, $operator, $field);
  1028. }
  1029. /**
  1030. * Register a post type. Do not use before init.
  1031. *
  1032. * A function for creating or modifying a post type based on the
  1033. * parameters given. The function will accept an array (second optional
  1034. * parameter), along with a string for the post type name.
  1035. *
  1036. * @since 2.9.0
  1037. *
  1038. * @global array $wp_post_types List of post types.
  1039. * @global WP_Rewrite $wp_rewrite Used for default feeds.
  1040. * @global WP $wp Used to add query vars.
  1041. *
  1042. * @param string $post_type Post type key, must not exceed 20 characters.
  1043. * @param array|string $args {
  1044. * Array or string of arguments for registering a post type.
  1045. *
  1046. * @type string $label Name of the post type shown in the menu. Usually plural.
  1047. * Default is value of $labels['name'].
  1048. * @type array $labels An array of labels for this post type. If not set, post
  1049. * labels are inherited for non-hierarchical types and page
  1050. * labels for hierarchical ones. {@see get_post_type_labels()}.
  1051. * @type string $description A short descriptive summary of what the post type is.
  1052. * Default empty.
  1053. * @type bool $public Whether a post type is intended for use publicly either via
  1054. * the admin interface or by front-end users. While the default
  1055. * settings of $exclude_from_search, $publicly_queryable, $show_ui,
  1056. * and $show_in_nav_menus are inherited from public, each does not
  1057. * rely on this relationship and controls a very specific intention.
  1058. * Default false.
  1059. * @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false.
  1060. * @type bool $exclude_from_search Whether to exclude posts with this post type from front end search
  1061. * results. Default is the opposite value of $public.
  1062. * @type bool $publicly_queryable Whether queries can be performed on the front end for the post type
  1063. * as part of {@see parse_request()}. Endpoints would include:
  1064. * * ?post_type={post_type_key}
  1065. * * ?{post_type_key}={single_post_slug}
  1066. * * ?{post_type_query_var}={single_post_slug}
  1067. * If not set, the default is inherited from $public.
  1068. * @type bool $show_ui Whether to generate a default UI for managing this post type in the
  1069. * admin. Default is value of $public.
  1070. * @type bool $show_in_menu Where to show the post type in the admin menu. To work, $show_ui
  1071. * must be true. If true, the post type is shown in its own top level
  1072. * menu. If false, no menu is shown. If a string of an existing top
  1073. * level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post
  1074. * type will be placed as a sub-menu of that.
  1075. * Default is value of $show_ui.
  1076. * @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus.
  1077. * Default is value $public.
  1078. * @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value
  1079. * of $show_in_menu.
  1080. * @type int $menu_position The position in the menu order the post type should appear. To work,
  1081. * $show_in_menu must be true. Default null (at the bottom).
  1082. * @type string $menu_icon The url to the icon to be used for this menu. Pass a base64-encoded
  1083. * SVG using a data URI, which will be colored to match the color scheme
  1084. * -- this should begin with 'data:image/svg+xml;base64,'. Pass the name
  1085. * of a Dashicons helper class to use a font icon, e.g.
  1086. * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
  1087. * so an icon can be added via CSS. Defaults to use the posts icon.
  1088. * @type string $capability_type The string to use to build the read, edit, and delete capabilities.
  1089. * May be passed as an array to allow for alternative plurals when using
  1090. * this argument as a base to construct the capabilities, e.g.
  1091. * array('story', 'stories'). Default 'post'.
  1092. * @type array $capabilities Array of capabilities for this post type. $capability_type is used
  1093. * as a base to construct capabilities by default.
  1094. * {@see get_post_type_capabilities()}.
  1095. * @type bool $map_meta_cap Whether to use the internal default meta capability handling.
  1096. * Default false.
  1097. * @type array $supports An alias for calling {@see add_post_type_support()} directly.
  1098. * Defaults to array containing 'title' & 'editor'.
  1099. * @type callback $register_meta_box_cb Provide a callback function that sets up the meta boxes for the
  1100. * edit form. Do remove_meta_box() and add_meta_box() calls in the
  1101. * callback. Default null.
  1102. * @type array $taxonomies An array of taxonomy identifiers that will be registered for the
  1103. * post type. Taxonomies can be registered later with
  1104. * {@see register_taxonomy()} or {@see register_taxonomy_for_object_type()}.
  1105. * Default empty array.
  1106. * @type bool|string $has_archive Whether there should be post type archives, or if a string, the
  1107. * archive slug to use. Will generate the proper rewrite rules if
  1108. * $rewrite is enabled. Default false.
  1109. * @type bool|array $rewrite {
  1110. * Triggers the handling of rewrites for this post type. To prevent rewrite, set to false.
  1111. * Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be
  1112. * passed with any of these keys:
  1113. *
  1114. * @type string $slug Customize the permastruct slug. Defaults to $post_type key.
  1115. * @type bool $with_front Whether the permastruct should be prepended with WP_Rewrite::$front.
  1116. * Default true.
  1117. * @type bool $feeds Whether the feed permastruct should be built for this post type.
  1118. * Default is value of $has_archive.
  1119. * @type bool $pages Whether the permastruct should provide for pagination. Default true.
  1120. * @type const $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set,
  1121. * inherits from $permalink_epmask. If not specified and permalink_epmask
  1122. * is not set, defaults to EP_PERMALINK.
  1123. * }
  1124. * @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type
  1125. * key. If false, a post type cannot be loaded at
  1126. * ?{query_var}={post_slug}. If specified as a string, the query
  1127. * ?{query_var_string}={post_slug} will be valid.
  1128. * @type bool $can_export Whether to allow this post type to be exported. Default true.
  1129. * @type bool $delete_with_user Whether to delete posts of this type when deleting a user. If true,
  1130. * posts of this type belonging to the user will be moved to trash
  1131. * when then user is deleted. If false, posts of this type belonging
  1132. * to the user will *not* be trashed or deleted. If not set (the default),
  1133. * posts are trashed if post_type_supports('author'). Otherwise posts
  1134. * are not trashed or deleted. Default null.
  1135. * @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or
  1136. * "built-in" post_type. Default false.
  1137. * @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of
  1138. * this post type. Default 'post.php?post=%d'.
  1139. * }
  1140. * @return object|WP_Error The registered post type object, or an error object.
  1141. */
  1142. function register_post_type( $post_type, $args = array() ) {
  1143. global $wp_post_types, $wp_rewrite, $wp;
  1144. if ( ! is_array( $wp_post_types ) )
  1145. $wp_post_types = array();
  1146. // Args prefixed with an underscore are reserved for internal use.
  1147. $defaults = array(
  1148. 'labels' => array(),
  1149. 'description' => '',
  1150. 'public' => false,
  1151. 'hierarchical' => false,
  1152. 'exclude_from_search' => null,
  1153. 'publicly_queryable' => null,
  1154. 'show_ui' => null,
  1155. 'show_in_menu' => null,
  1156. 'show_in_nav_menus' => null,
  1157. 'show_in_admin_bar' => null,
  1158. 'menu_position' => null,
  1159. 'menu_icon' => null,
  1160. 'capability_type' => 'post',
  1161. 'capabilities' => array(),
  1162. 'map_meta_cap' => null,
  1163. 'supports' => array(),
  1164. 'register_meta_box_cb' => null,
  1165. 'taxonomies' => array(),
  1166. 'has_archive' => false,
  1167. 'rewrite' => true,
  1168. 'query_var' => true,
  1169. 'can_export' => true,
  1170. 'delete_with_user' => null,
  1171. '_builtin' => false,
  1172. '_edit_link' => 'post.php?post=%d',
  1173. );
  1174. $args = wp_parse_args( $args, $defaults );
  1175. $args = (object) $args;
  1176. $post_type = sanitize_key( $post_type );
  1177. $args->name = $post_type;
  1178. if ( strlen( $post_type ) > 20 ) {
  1179. _doing_it_wrong( __FUNCTION__, __( 'Post types cannot exceed 20 characters in length' ), '4.0' );
  1180. return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
  1181. }
  1182. // If not set, default to the setting for public.
  1183. if ( null === $args->publicly_queryable )
  1184. $args->publicly_queryable = $args->public;
  1185. // If not set, default to the setting for public.
  1186. if ( null === $args->show_ui )
  1187. $args->show_ui = $args->public;
  1188. // If not set, default to the setting for show_ui.
  1189. if ( null === $args->show_in_menu || ! $args->show_ui )
  1190. $args->show_in_menu = $args->show_ui;
  1191. // If not set, default to the whether the full UI is shown.
  1192. if ( null === $args->show_in_admin_bar )
  1193. $args->show_in_admin_bar = (bool) $args->show_in_menu;
  1194. // If not set, default to the setting for public.
  1195. if ( null === $args->show_in_nav_menus )
  1196. $args->show_in_nav_menus = $args->public;
  1197. // If not set, default to true if not public, false if public.
  1198. if ( null === $args->exclude_from_search )
  1199. $args->exclude_from_search = !$args->public;
  1200. // Back compat with quirky handling in version 3.0. #14122.
  1201. if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
  1202. $args->map_meta_cap = true;
  1203. // If not set, default to false.
  1204. if ( null === $args->map_meta_cap )
  1205. $args->map_meta_cap = false;
  1206. $args->cap = get_post_type_capabilities( $args );
  1207. unset( $args->capabilities );
  1208. if ( is_array( $args->capability_type ) )
  1209. $args->capability_type = $args->capability_type[0];
  1210. if ( ! empty( $args->supports ) ) {
  1211. add_post_type_support( $post_type, $args->supports );
  1212. unset( $args->supports );
  1213. } elseif ( false !== $args->supports ) {
  1214. // Add default features
  1215. add_post_type_support( $post_type, array( 'title', 'editor' ) );
  1216. }
  1217. if ( false !== $args->query_var && ! empty( $wp ) ) {
  1218. if ( true === $args->query_var )
  1219. $args->query_var = $post_type;
  1220. else
  1221. $args->query_var = sanitize_title_with_dashes( $args->query_var );
  1222. $wp->add_query_var( $args->query_var );
  1223. }
  1224. if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
  1225. if ( ! is_array( $args->rewrite ) )
  1226. $args->rewrite = array();
  1227. if ( empty( $args->rewrite['slug'] ) )
  1228. $args->rewrite['slug'] = $post_type;
  1229. if ( ! isset( $args->rewrite['with_front'] ) )
  1230. $args->rewrite['with_front'] = true;
  1231. if ( ! isset( $args->rewrite['pages'] ) )
  1232. $args->rewrite['pages'] = true;
  1233. if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
  1234. $args->rewrite['feeds'] = (bool) $args->has_archive;
  1235. if ( ! isset( $args->rewrite['ep_mask'] ) ) {
  1236. if ( isset( $args->permalink_epmask ) )
  1237. $args->rewrite['ep_mask'] = $args->permalink_epmask;
  1238. else
  1239. $args->rewrite['ep_mask'] = EP_PERMALINK;
  1240. }
  1241. if ( $args->hierarchical )
  1242. add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" );
  1243. else
  1244. add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" );
  1245. if ( $args->has_archive ) {
  1246. $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
  1247. if ( $args->rewrite['with_front'] )
  1248. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  1249. else
  1250. $archive_slug = $wp_rewrite->root . $archive_slug;
  1251. add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
  1252. if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
  1253. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
  1254. add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  1255. add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  1256. }
  1257. if ( $args->rewrite['pages'] )
  1258. add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
  1259. }
  1260. $permastruct_args = $args->rewrite;
  1261. $permastruct_args['feed'] = $permastruct_args['feeds'];
  1262. add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
  1263. }
  1264. // Register the post type meta box if a custom callback was specified.
  1265. if ( $args->register_meta_box_cb )
  1266. add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );
  1267. $args->labels = get_post_type_labels( $args );
  1268. $args->label = $args->labels->name;
  1269. $wp_post_types[ $post_type ] = $args;
  1270. add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
  1271. foreach ( $args->taxonomies as $taxonomy ) {
  1272. register_taxonomy_for_object_type( $taxonomy, $post_type );
  1273. }
  1274. /**
  1275. * Fires after a post type is registered.
  1276. *
  1277. * @since 3.3.0
  1278. *
  1279. * @param string $post_type Post type.
  1280. * @param object $args Arguments used to register the post type.
  1281. */
  1282. do_action( 'registered_post_type', $post_type, $args );
  1283. return $args;
  1284. }
  1285. /**
  1286. * Build an object with all …

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