Changes from trunk/wp-includes/post.php at r17429 to branches/3.0/wp-includes/post.php at r16668
- File:
-
- 1 edited
-
branches/3.0/wp-includes/post.php (modified) (83 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/3.0/wp-includes/post.php
r17429 r16668 14 14 /** 15 15 * Creates the initial post types when 'init' action is fired. 16 *17 * @since 2.9.018 16 */ 19 17 function create_initial_post_types() { … … 23 21 '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ 24 22 'capability_type' => 'post', 25 'map_meta_cap' => true,26 23 'hierarchical' => false, 27 24 'rewrite' => false, 28 25 'query_var' => false, 29 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' , 'post-formats'),26 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ), 30 27 ) ); 31 28 … … 35 32 '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ 36 33 'capability_type' => 'page', 37 'map_meta_cap' => true,38 34 'hierarchical' => true, 39 35 'rewrite' => false, … … 51 47 '_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */ 52 48 'capability_type' => 'post', 53 'map_meta_cap' => true,54 49 'hierarchical' => false, 55 50 'rewrite' => false, 56 51 'query_var' => false, 52 'can_export' => false, 57 53 'show_in_nav_menus' => false, 58 54 ) ); … … 67 63 '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */ 68 64 'capability_type' => 'post', 69 'map_meta_cap' => true,70 65 'hierarchical' => false, 71 66 'rewrite' => false, 72 67 'query_var' => false, 73 'can_export' => false,74 68 ) ); 75 69 … … 81 75 'public' => false, 82 76 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ 77 'capability_type' => 'post', 83 78 'hierarchical' => false, 84 79 'rewrite' => false, … … 267 262 * @return array|bool False on failure and the type will be determined by $output parameter. 268 263 */ 269 function get_children($args = '', $output = OBJECT) {264 function &get_children($args = '', $output = OBJECT) { 270 265 $kids = array(); 271 266 if ( empty( $args ) ) { … … 296 291 297 292 foreach ( $children as $key => $child ) 298 $kids[$child->ID] = $children[$key];293 $kids[$child->ID] =& $children[$key]; 299 294 300 295 if ( $output == OBJECT ) { … … 477 472 478 473 /** 479 * Retrieve the format slug for a post480 *481 * @since 3.1.0482 *483 * @param int|object $post A post484 *485 * @return mixed The format if successful. False if no format is set. WP_Error if errors.486 */487 function get_post_format( $post = null ) {488 $post = get_post($post);489 490 if ( ! post_type_supports( $post->post_type, 'post-formats' ) )491 return false;492 493 $_format = get_the_terms( $post->ID, 'post_format' );494 495 if ( empty( $_format ) )496 return false;497 498 $format = array_shift( $_format );499 500 return ( str_replace('post-format-', '', $format->slug ) );501 }502 503 /**504 * Check if a post has a particular format505 *506 * @since 3.1.0507 * @uses has_term()508 *509 * @param string $format The format to check for510 * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.511 * @return bool True if the post has the format, false otherwise.512 */513 function has_post_format( $format, $post = null ) {514 return has_term('post-format-' . sanitize_key($format), 'post_format', $post);515 }516 517 /**518 * Assign a format to a post519 *520 * @since 3.1.0521 *522 * @param int|object $post The post for which to assign a format523 * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.524 * @return mixed WP_Error on error. Array of affected term IDs on success.525 */526 function set_post_format( $post, $format ) {527 $post = get_post($post);528 529 if ( empty($post) )530 return new WP_Error('invalid_post', __('Invalid post'));531 532 if ( !empty($format) ) {533 $format = sanitize_key($format);534 if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )535 $format = '';536 else537 $format = 'post-format-' . $format;538 }539 540 return wp_set_post_terms($post->ID, $format, 'post_format');541 }542 543 /**544 474 * Retrieve the post status based on the Post ID. 545 475 * … … 620 550 * 621 551 * label - A descriptive name for the post status marked for translation. Defaults to $post_status. 622 * public - Whether posts of this status should be shown in the front end of the site. Defaults to true.552 * public - Whether posts of this status should be shown in the admin UI. Defaults to true. 623 553 * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to true. 624 * show_in_admin_all_list - Whether to include posts in the edit listing for their post type625 * show_in_admin_status_list - Show in the list of statuses with post counts at the top of the edit626 * listings, e.g. All (12) | Published (9) | My Custom Status (2) ...627 *628 * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.629 554 * 630 555 * @package WordPress … … 647 572 $args = (object) $args; 648 573 649 $post_status = sanitize_ key($post_status);574 $post_status = sanitize_user($post_status, true); 650 575 $args->name = $post_status; 651 576 … … 701 626 * @see get_post_statuses 702 627 * 703 * @param string $post_ statusThe name of a registered post status628 * @param string $post_type The name of a registered post status 704 629 * @return object A post status object 705 630 */ … … 745 670 * @see get_post_type_object 746 671 * 747 * @param string $post _typePost type name672 * @param string $post Post type name 748 673 * @return bool Whether post type is hierarchical. 749 674 */ … … 762 687 * @uses get_post_type_object() 763 688 * 764 * @param string $post_typePost type name689 * @param string Post type name 765 690 * @return bool Whether post type is registered. 766 691 */ … … 841 766 * Register a post type. Do not use before init. 842 767 * 843 * A function for creating or modifying a post type based on the768 * A simple function for creating or modifying a post type based on the 844 769 * parameters given. The function will accept an array (second optional 845 770 * parameter), along with a string for the post type name. 771 * 846 772 * 847 773 * Optional $args contents: … … 850 776 * - description - A short descriptive summary of what the post type is. Defaults to blank. 851 777 * - public - Whether posts of this type should be shown in the admin UI. Defaults to false. 852 * - exclude_from_search - Whether to exclude posts with this post type from search results. 853 * Defaults to true if the type is not public, false if the type is public. 854 * - publicly_queryable - Whether post_type queries can be performed from the front page. 855 * Defaults to whatever public is set as. 856 * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true 857 * if the type is public, false if the type is not public. 858 * - show_in_menu - Where to show the post type in the admin menu. True for a top level menu, 859 * false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'. 860 * show_ui must be true. 778 * - exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public. 779 * - publicly_queryable - Whether post_type queries can be performed from the front page. Defaults to whatever public is set as. 780 * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public. 861 781 * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom. 862 782 * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon. 863 * - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'. 864 * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the 865 * capabilities, e.g. array('story', 'stories'). 866 * - capabilities - Array of capabilities for this post type. By default the capability_type is used 867 * as a base to construct capabilities. You can see accepted values in {@link get_post_type_capabilities()}. 868 * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false. 783 * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post". 784 * - capabilities - Array of capabilities for this post type. You can see accepted values in {@link get_post_type_capabilities()}. By default the capability_type is used to construct capabilities. 869 785 * - hierarchical - Whether the post type is hierarchical. Defaults to false. 870 * - supports - An alias for calling add_post_type_support() directly. See {@link add_post_type_support()} 871 * for documentation. Defaults to none. 872 * - register_meta_box_cb - Provide a callback function that will be called when setting up the 873 * meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback. 874 * - taxonomies - An array of taxonomy identifiers that will be registered for the post type. 875 * Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or 876 * register_taxonomy_for_object_type(). 877 * - labels - An array of labels for this post type. By default post labels are used for non-hierarchical 878 * types and page labels for hierarchical ones. You can see accepted values in {@link get_post_type_labels()}. 786 * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none. 787 * - register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback. 788 * - taxonomies - An array of taxonomy identifiers that will be registered for the post type. Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type(). 789 * - labels - An array of labels for this post type. You can see accepted values in {@link get_post_type_labels()}. By default post labels are used for non-hierarchical types and page labels for hierarchical ones. 879 790 * - permalink_epmask - The default rewrite endpoint bitmasks. 880 * - has_archive - True to enable post type archives. Will generate the proper rewrite rules if rewrite is enabled. 881 * - rewrite - false to prevent rewrite. Defaults to true. Use array('slug'=>$slug) to customize permastruct; 882 * default will use $post_type as slug. Other options include 'with_front', 'feeds', and 'pages'. 791 * - rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $post_type as slug. 883 792 * - query_var - false to prevent queries, or string to value of the query var to use for this post type 884 793 * - can_export - true allows this post type to be exported. 885 794 * - show_in_nav_menus - true makes this post type available for selection in navigation menus. 886 * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!887 * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!795 * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY! 796 * - _edit_link - URL segement to use for edit link of this post type. Set to 'post.php?post=%d'. THIS IS FOR INTERNAL USE ONLY! 888 797 * 889 798 * @since 2.9.0 … … 892 801 * @param string $post_type Name of the post type. 893 802 * @param array|string $args See above description. 894 * @return object |WP_Error the registered post type object, or an errorobject803 * @return object the registered post type object 895 804 */ 896 805 function register_post_type($post_type, $args = array()) { … … 903 812 $defaults = array( 904 813 'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null, 905 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null, 906 '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false, 907 'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true, 908 'supports' => array(), 'register_meta_box_cb' => null, 814 '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'capabilities' => array(), 'hierarchical' => false, 815 'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null, 909 816 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null, 910 'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null , 'show_in_menu' => null,817 'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null 911 818 ); 912 819 $args = wp_parse_args($args, $defaults); 913 820 $args = (object) $args; 914 821 915 $post_type = sanitize_ key($post_type);822 $post_type = sanitize_user($post_type, true); 916 823 $args->name = $post_type; 917 918 if ( strlen( $post_type ) > 20 )919 return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );920 824 921 825 // If not set, default to the setting for public. … … 927 831 $args->show_ui = $args->public; 928 832 929 // If not set, default to the setting for show_ui.930 if ( null === $args->show_in_menu || ! $args->show_ui )931 $args->show_in_menu = $args->show_ui;932 933 833 // Whether to show this type in nav-menus.php. Defaults to the setting for public. 934 834 if ( null === $args->show_in_nav_menus ) … … 939 839 $args->exclude_from_search = !$args->public; 940 840 941 // Back compat with quirky handling in version 3.0. #14122 942 if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) ) 943 $args->map_meta_cap = true; 944 945 if ( null === $args->map_meta_cap ) 946 $args->map_meta_cap = false; 841 if ( empty($args->capability_type) ) 842 $args->capability_type = 'post'; 947 843 948 844 $args->cap = get_post_type_capabilities( $args ); 949 845 unset($args->capabilities); 950 951 if ( is_array( $args->capability_type ) )952 $args->capability_type = $args->capability_type[0];953 846 954 847 if ( ! empty($args->supports) ) { … … 968 861 969 862 if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) { 970 if ( ! is_array( $args->rewrite) )863 if ( !is_array($args->rewrite) ) 971 864 $args->rewrite = array(); 972 if ( empty( $args->rewrite['slug']) )865 if ( !isset($args->rewrite['slug']) ) 973 866 $args->rewrite['slug'] = $post_type; 974 if ( ! isset( $args->rewrite['with_front']) )867 if ( !isset($args->rewrite['with_front']) ) 975 868 $args->rewrite['with_front'] = true; 976 if ( ! isset( $args->rewrite['pages'] ) )977 $args->rewrite['pages'] = true;978 if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )979 $args->rewrite['feeds'] = (bool) $args->has_archive;980 981 869 if ( $args->hierarchical ) 982 870 $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name="); 983 871 else 984 872 $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name="); 985 986 if ( $args->has_archive ) {987 $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;988 $wp_rewrite->add_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );989 if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {990 $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';991 $wp_rewrite->add_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );992 $wp_rewrite->add_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );993 }994 if ( $args->rewrite['pages'] )995 $wp_rewrite->add_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );996 }997 998 873 $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask); 999 874 } … … 1019 894 * Builds an object with all post type capabilities out of a post type object 1020 895 * 1021 * Post type capabilities use the 'capability_type' argument as a base, if the 1022 * capability is not set in the 'capabilities' argument array or if the 1023 * 'capabilities' argument is not supplied. 1024 * 1025 * The capability_type argument can optionally be registered as an array, with 1026 * the first value being singular and the second plural, e.g. array('story, 'stories') 1027 * Otherwise, an 's' will be added to the value for the plural form. After 1028 * registration, capability_type will always be a string of the singular value. 1029 * 1030 * By default, seven keys are accepted as part of the capabilities array: 1031 * 1032 * - edit_post, read_post, and delete_post are meta capabilities, which are then 1033 * generally mapped to corresponding primitive capabilities depending on the 1034 * context, which would be the post being edited/read/deleted and the user or 1035 * role being checked. Thus these capabilities would generally not be granted 1036 * directly to users or roles. 1037 * 1038 * - edit_posts - Controls whether objects of this post type can be edited. 1039 * - edit_others_posts - Controls whether objects of this type owned by other users 1040 * can be edited. If the post type does not support an author, then this will 1041 * behave like edit_posts. 1042 * - publish_posts - Controls publishing objects of this post type. 1043 * - read_private_posts - Controls whether private objects can be read. 1044 1045 * These four primitive capabilities are checked in core in various locations. 1046 * There are also seven other primitive capabilities which are not referenced 1047 * directly in core, except in map_meta_cap(), which takes the three aforementioned 1048 * meta capabilities and translates them into one or more primitive capabilities 1049 * that must then be checked against the user or role, depending on the context. 1050 * 1051 * - read - Controls whether objects of this post type can be read. 1052 * - delete_posts - Controls whether objects of this post type can be deleted. 1053 * - delete_private_posts - Controls whether private objects can be deleted. 1054 * - delete_published_posts - Controls whether published objects can be deleted. 1055 * - delete_others_posts - Controls whether objects owned by other users can be 1056 * can be deleted. If the post type does not support an author, then this will 1057 * behave like delete_posts. 1058 * - edit_private_posts - Controls whether private objects can be edited. 1059 * - edit_published_posts - Controls whether published objects can be deleted. 1060 * 1061 * These additional capabilities are only used in map_meta_cap(). Thus, they are 1062 * only assigned by default if the post type is registered with the 'map_meta_cap' 1063 * argument set to true (default is false). 1064 * 1065 * @see map_meta_cap() 896 * Accepted keys of the capabilities array in the post type object: 897 * - edit_post - The meta capability that controls editing a particular object of this post type. Defaults to "edit_ . $capability_type" (edit_post). 898 * - edit_posts - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts). 899 * - edit_others_posts - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts). 900 * - publish_posts - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts). 901 * - read_post - The meta capability that controls reading a particular object of this post type. Defaults to "read_ . $capability_type" (read_post). 902 * - read_private_posts - The capability that controls reading private posts. Defaults to "read_private . $capability_type . s" (read_private_posts). 903 * - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_ . $capability_type" (delete_post). 904 * 1066 905 * @since 3.0.0 1067 * 1068 * @param object $args Post type registration arguments 906 * @param object $args 1069 907 * @return object object with all the capabilities as member variables 1070 908 */ 1071 909 function get_post_type_capabilities( $args ) { 1072 if ( ! is_array( $args->capability_type ) ) 1073 $args->capability_type = array( $args->capability_type, $args->capability_type . 's' ); 1074 1075 // Singular base for meta capabilities, plural base for primitive capabilities. 1076 list( $singular_base, $plural_base ) = $args->capability_type; 1077 1078 $default_capabilities = array( 1079 // Meta capabilities 1080 'edit_post' => 'edit_' . $singular_base, 1081 'read_post' => 'read_' . $singular_base, 1082 'delete_post' => 'delete_' . $singular_base, 1083 // Primitive capabilities used outside of map_meta_cap(): 1084 'edit_posts' => 'edit_' . $plural_base, 1085 'edit_others_posts' => 'edit_others_' . $plural_base, 1086 'publish_posts' => 'publish_' . $plural_base, 1087 'read_private_posts' => 'read_private_' . $plural_base, 910 $defaults = array( 911 'edit_post' => 'edit_' . $args->capability_type, 912 'edit_posts' => 'edit_' . $args->capability_type . 's', 913 'edit_others_posts' => 'edit_others_' . $args->capability_type . 's', 914 'publish_posts' => 'publish_' . $args->capability_type . 's', 915 'read_post' => 'read_' . $args->capability_type, 916 'read_private_posts' => 'read_private_' . $args->capability_type . 's', 917 'delete_post' => 'delete_' . $args->capability_type, 1088 918 ); 1089 1090 // Primitive capabilities used within map_meta_cap(): 1091 if ( $args->map_meta_cap ) { 1092 $default_capabilities_for_mapping = array( 1093 'read' => 'read', 1094 'delete_posts' => 'delete_' . $plural_base, 1095 'delete_private_posts' => 'delete_private_' . $plural_base, 1096 'delete_published_posts' => 'delete_published_' . $plural_base, 1097 'delete_others_posts' => 'delete_others_' . $plural_base, 1098 'edit_private_posts' => 'edit_private_' . $plural_base, 1099 'edit_published_posts' => 'edit_published_' . $plural_base, 1100 ); 1101 $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping ); 1102 } 1103 1104 $capabilities = array_merge( $default_capabilities, $args->capabilities ); 1105 1106 // Remember meta capabilities for future reference. 1107 if ( $args->map_meta_cap ) 1108 _post_type_meta_capabilities( $capabilities ); 1109 1110 return (object) $capabilities; 1111 } 1112 1113 /** 1114 * Stores or returns a list of post type meta caps for map_meta_cap(). 1115 * 1116 * @since 3.1.0 1117 * @access private 1118 */ 1119 function _post_type_meta_capabilities( $capabilities = null ) { 1120 static $meta_caps = array(); 1121 if ( null === $capabilities ) 1122 return $meta_caps; 1123 foreach ( $capabilities as $core => $custom ) { 1124 if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) 1125 $meta_caps[ $custom ] = $core; 1126 } 919 $labels = array_merge( $defaults, $args->capabilities ); 920 return (object) $labels; 1127 921 } 1128 922 … … 1143 937 * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page: 1144 938 * 1145 * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages ).939 * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages.) 1146 940 * 1147 941 * @since 3.0.0 … … 1159 953 'view_item' => array( __('View Post'), __('View Page') ), 1160 954 'search_items' => array( __('Search Posts'), __('Search Pages') ), 1161 'not_found' => array( __('No posts found .'), __('No pages found.') ),1162 'not_found_in_trash' => array( __('No posts found in Trash .'), __('No pages found in Trash.') ),1163 'parent_item_colon' => array( null, __('Parent Page:') ) ,955 'not_found' => array( __('No posts found'), __('No pages found') ), 956 'not_found_in_trash' => array( __('No posts found in Trash'), __('No pages found in Trash') ), 957 'parent_item_colon' => array( null, __('Parent Page:') ) 1164 958 ); 1165 $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];1166 959 return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); 1167 960 } … … 1171 964 * 1172 965 * @access private 1173 * @since 3.0.01174 966 */ 1175 967 function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { … … 1181 973 $object->labels['singular_name'] = $object->labels['name']; 1182 974 1183 if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) 1184 $object->labels['menu_name'] = $object->labels['name']; 1185 1186 foreach ( $nohier_vs_hier_defaults as $key => $value ) 1187 $defaults[$key] = $object->hierarchical ? $value[1] : $value[0]; 1188 975 $defaults = array_map( create_function( '$x', $object->hierarchical? 'return $x[1];' : 'return $x[0];' ), $nohier_vs_hier_defaults ); 1189 976 $labels = array_merge( $defaults, $object->labels ); 1190 977 return (object)$labels; 1191 978 } 1192 1193 /**1194 * Adds submenus for post types.1195 *1196 * @access private1197 * @since 3.1.01198 */1199 function _add_post_type_submenus() {1200 foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {1201 $ptype_obj = get_post_type_object( $ptype );1202 // Submenus only.1203 if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true )1204 continue;1205 add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );1206 }1207 }1208 add_action( 'admin_menu', '_add_post_type_submenus' );1209 979 1210 980 /** … … 1342 1112 if ( empty( $r['post_status'] ) ) 1343 1113 $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish'; 1344 if ( ! empty($r['numberposts']) && empty($r['posts_per_page']))1114 if ( ! empty($r['numberposts']) ) 1345 1115 $r['posts_per_page'] = $r['numberposts']; 1346 1116 if ( ! empty($r['category']) ) … … 1353 1123 $r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); 1354 1124 1355 $r['ignore_sticky_posts'] = true; 1356 $r['no_found_rows'] = true; 1125 $r['caller_get_posts'] = true; 1357 1126 1358 1127 $get_posts = new WP_Query; … … 1375 1144 * 1376 1145 * @param int $post_id Post ID. 1377 * @param string $ meta_key Metadata name.1378 * @param mixed $ meta_value Metadata value.1146 * @param string $key Metadata name. 1147 * @param mixed $value Metadata value. 1379 1148 * @param bool $unique Optional, default is false. Whether the same key should not be added. 1380 1149 * @return bool False for failure. True for success. … … 1437 1206 * If the meta field for the post does not exist, it will be added. 1438 1207 * 1439 * @since 1.5 .01208 * @since 1.5 1440 1209 * @uses $wpdb 1441 1210 * @link http://codex.wordpress.org/Function_Reference/update_post_meta 1442 1211 * 1443 1212 * @param int $post_id Post ID. 1444 * @param string $ meta_key Metadata key.1445 * @param mixed $ meta_value Metadata value.1213 * @param string $key Metadata key. 1214 * @param mixed $value Metadata value. 1446 1215 * @param mixed $prev_value Optional. Previous value to check before removing. 1447 1216 * @return bool False on failure, true if success. … … 1498 1267 * @return array 1499 1268 */ 1500 function get_post_custom( $post_id = 0 ) { 1501 $post_id = absint( $post_id ); 1502 1503 if ( ! $post_id ) 1504 $post_id = get_the_ID(); 1505 1506 if ( ! wp_cache_get( $post_id, 'post_meta' ) ) 1507 update_postmeta_cache( $post_id ); 1508 1509 return wp_cache_get( $post_id, 'post_meta' ); 1269 function get_post_custom($post_id = 0) { 1270 global $id; 1271 1272 if ( !$post_id ) 1273 $post_id = (int) $id; 1274 1275 $post_id = (int) $post_id; 1276 1277 if ( ! wp_cache_get($post_id, 'post_meta') ) 1278 update_postmeta_cache($post_id); 1279 1280 return wp_cache_get($post_id, 'post_meta'); 1510 1281 } 1511 1282 … … 1564 1335 * @return bool Whether post is sticky. 1565 1336 */ 1566 function is_sticky( $post_id = 0 ) { 1567 $post_id = absint( $post_id ); 1568 1569 if ( ! $post_id ) 1570 $post_id = get_the_ID(); 1571 1572 $stickies = get_option( 'sticky_posts' ); 1573 1574 if ( ! is_array( $stickies ) ) 1337 function is_sticky($post_id = null) { 1338 global $id; 1339 1340 $post_id = absint($post_id); 1341 1342 if ( !$post_id ) 1343 $post_id = absint($id); 1344 1345 $stickies = get_option('sticky_posts'); 1346 1347 if ( !is_array($stickies) ) 1575 1348 return false; 1576 1349 1577 if ( in_array( $post_id, $stickies) )1350 if ( in_array($post_id, $stickies) ) 1578 1351 return true; 1579 1352 … … 1624 1397 * 1625 1398 * @since 2.3.0 1626 * @uses apply_filters() Calls 'edit_$field' and ' {$field_no_prefix}_edit_pre' passing $value and1399 * @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and 1627 1400 * $post_id if $context == 'edit' and field name prefix == 'post_'. 1628 1401 * 1629 1402 * @uses apply_filters() Calls 'edit_post_$field' passing $value and $post_id if $context == 'db'. 1630 1403 * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'post_'. 1631 * @uses apply_filters() Calls ' {$field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'.1404 * @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'. 1632 1405 * 1633 1406 * @uses apply_filters() Calls '$field' passing $value, $post_id and $context if $context == anything … … 1668 1441 1669 1442 if ( $prefixed ) { 1670 $value = apply_filters("edit_ {$field}", $value, $post_id);1443 $value = apply_filters("edit_$field", $value, $post_id); 1671 1444 // Old school 1672 $value = apply_filters(" {$field_no_prefix}_edit_pre", $value, $post_id);1445 $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id); 1673 1446 } else { 1674 $value = apply_filters("edit_post_ {$field}", $value, $post_id);1447 $value = apply_filters("edit_post_$field", $value, $post_id); 1675 1448 } 1676 1449 … … 1685 1458 } else if ( 'db' == $context ) { 1686 1459 if ( $prefixed ) { 1687 $value = apply_filters("pre_ {$field}", $value);1688 $value = apply_filters(" {$field_no_prefix}_save_pre", $value);1460 $value = apply_filters("pre_$field", $value); 1461 $value = apply_filters("${field_no_prefix}_save_pre", $value); 1689 1462 } else { 1690 $value = apply_filters("pre_post_ {$field}", $value);1691 $value = apply_filters(" {$field}_pre", $value);1463 $value = apply_filters("pre_post_$field", $value); 1464 $value = apply_filters("${field}_pre", $value); 1692 1465 } 1693 1466 } else { … … 1696 1469 $value = apply_filters($field, $value, $post_id, $context); 1697 1470 else 1698 $value = apply_filters("post_ {$field}", $value, $post_id, $context);1471 $value = apply_filters("post_$field", $value, $post_id, $context); 1699 1472 } 1700 1473 … … 1881 1654 * @since 2.5.0 1882 1655 * 1883 * @param string|array $ post_mime_types List of mime types or comma separated string of mime types.1656 * @param string|array $mime_types List of mime types or comma separated string of mime types. 1884 1657 * @param string $table_alias Optional. Specify a table alias, if needed. 1885 1658 * @return string The SQL AND clause for mime searching. … … 2040 1813 * @uses wp_delete_post() if trash is disabled 2041 1814 * 2042 * @param int $post _id Post ID.1815 * @param int $postid Post ID. 2043 1816 * @return mixed False on failure 2044 1817 */ … … 2075 1848 * @uses do_action() on 'untrashed_post' after undeletion 2076 1849 * 2077 * @param int $post _id Post ID.1850 * @param int $postid Post ID. 2078 1851 * @return mixed False on failure 2079 1852 */ … … 2268 2041 * 2269 2042 * @since 1.0.0 2270 * @uses wp_parse_args() 2271 * @uses get_posts() 2272 * 2273 * @param string $deprecated Deprecated. 2274 * @param array $args Optional. Overrides defaults. 2275 * @param string $output Optional. 2276 * @return unknown. 2277 */ 2278 function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { 2279 2280 if ( is_numeric( $args ) ) { 2281 _deprecated_argument( __FUNCTION__, '3.1', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); 2282 $args = array( 'numberposts' => absint( $args ) ); 2283 } 2284 2285 // Set default arguments 2286 $defaults = array( 2287 'numberposts' => 10, 'offset' => 0, 2288 'category' => 0, 'orderby' => 'post_date', 2289 'order' => 'DESC', 'include' => '', 2290 'exclude' => '', 'meta_key' => '', 2291 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private', 2292 'suppress_filters' => true 2293 ); 2294 2295 $r = wp_parse_args( $args, $defaults ); 2296 2297 $results = get_posts( $r ); 2298 2299 // Backward compatibility. Prior to 3.1 expected posts to be returned in array 2300 if ( ARRAY_A == $output ){ 2301 foreach( $results as $key => $result ) { 2302 $results[$key] = get_object_vars( $result ); 2303 } 2304 return $results ? $results : array(); 2305 } 2306 2307 return $results ? $results : false; 2308 2043 * @uses $wpdb 2044 * 2045 * @param int $num Optional, default is 10. Number of posts to get. 2046 * @return array List of posts. 2047 */ 2048 function wp_get_recent_posts($num = 10) { 2049 global $wpdb; 2050 2051 // Set the limit clause, if we got a limit 2052 $num = (int) $num; 2053 if ( $num ) { 2054 $limit = "LIMIT $num"; 2055 } 2056 2057 $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC $limit"; 2058 $result = $wpdb->get_results($sql, ARRAY_A); 2059 2060 return $result ? $result : array(); 2309 2061 } 2310 2062 … … 2326 2078 $post = get_post($postid, $mode); 2327 2079 2328 if ( 2080 if ( 2329 2081 ( OBJECT == $mode && empty( $post->ID ) ) || 2330 2082 ( OBJECT != $mode && empty( $post['ID'] ) ) … … 2377 2129 * 2378 2130 * @since 1.0.0 2131 * @link http://core.trac.wordpress.org/ticket/9084 Bug report on 'wp_insert_post_data' filter. 2379 2132 * @uses $wpdb 2380 2133 * @uses $wp_rewrite 2381 2134 * @uses $user_ID 2135 * 2382 2136 * @uses do_action() Calls 'pre_post_update' on post ID if this is an update. 2383 2137 * @uses do_action() Calls 'edit_post' action on post ID and post data if this is an update. 2384 * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and post data just before returning. 2385 * @uses apply_filters() Calls 'wp_insert_post_data' passing $data, $postarr prior to database update or insert. 2138 * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and post data just before 2139 * returning. 2140 * 2141 * @uses apply_filters() Calls 'wp_insert_post_data' passing $data, $postarr prior to database 2142 * update or insert. 2386 2143 * @uses wp_transition_post_status() 2387 2144 * 2388 * @param array $postarr Elements that make up post to insert.2145 * @param array $postarr Optional. Overrides defaults. 2389 2146 * @param bool $wp_error Optional. Allow return of WP_Error on failure. 2390 2147 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. 2391 2148 */ 2392 function wp_insert_post($postarr , $wp_error = false) {2149 function wp_insert_post($postarr = array(), $wp_error = false) { 2393 2150 global $wpdb, $wp_rewrite, $user_ID; 2394 2151 … … 2517 2274 $post_parent = 0; 2518 2275 2519 // Check the post_parent to see if it will cause a hierarchy loop 2520 $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr ); 2276 if ( !empty($post_ID) ) { 2277 if ( $post_parent == $post_ID ) { 2278 // Post can't be its own parent 2279 $post_parent = 0; 2280 } elseif ( !empty($post_parent) ) { 2281 $parent_post = get_post($post_parent); 2282 // Check for circular dependency 2283 if ( isset( $parent_post->post_parent ) && $parent_post->post_parent == $post_ID ) 2284 $post_parent = 0; 2285 } 2286 } 2521 2287 2522 2288 if ( isset($menu_order) ) … … 2753 2519 * Computes a unique slug for the post, when given the desired slug and some post details. 2754 2520 * 2755 * @since 2.8.02756 *2757 2521 * @global wpdb $wpdb 2758 2522 * @global WP_Rewrite $wp_rewrite … … 2780 2544 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); 2781 2545 2782 if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug )) {2546 if ( $post_name_check || in_array( $slug, $feeds ) ) { 2783 2547 $suffix = 2; 2784 2548 do { … … 2795 2559 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) ); 2796 2560 2797 if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent) ) {2561 if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( '@^(page)?\d+$@', $slug ) ) { 2798 2562 $suffix = 2; 2799 2563 do { … … 2809 2573 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); 2810 2574 2811 if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type )) {2575 if ( $post_name_check || in_array( $slug, $feeds ) ) { 2812 2576 $suffix = 2; 2813 2577 do { … … 2850 2614 * @param string $tags The tags to set for the post, separated by commas. 2851 2615 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags. 2852 * @return mixed Array of affected term IDs. WP_Error or false on failure.2616 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise 2853 2617 */ 2854 2618 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { … … 2865 2629 * @param string $tags The tags to set for the post, separated by commas. 2866 2630 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags. 2867 * @return mixed Array of affected term IDs. WP_Error or false on failure.2631 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise 2868 2632 */ 2869 2633 function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { … … 2885 2649 } 2886 2650 2887 returnwp_set_object_terms($post_id, $tags, $taxonomy, $append);2651 wp_set_object_terms($post_id, $tags, $taxonomy, $append); 2888 2652 } 2889 2653 … … 2941 2705 * @uses do_action() Calls 'transition_post_status' on $new_status, $old_status and 2942 2706 * $post if there is a status change. 2943 * @uses do_action() Calls ' {$old_status}_to_{$new_status}' on $post if there is a status change.2944 * @uses do_action() Calls ' {$new_status}_{$post->post_type}' on post ID and $post.2707 * @uses do_action() Calls '${old_status}_to_$new_status' on $post if there is a status change. 2708 * @uses do_action() Calls '${new_status}_$post->post_type' on post ID and $post. 2945 2709 * 2946 2710 * @param string $new_status Transition to this post status. … … 2950 2714 function wp_transition_post_status($new_status, $old_status, $post) { 2951 2715 do_action('transition_post_status', $new_status, $old_status, $post); 2952 do_action(" {$old_status}_to_{$new_status}", $post);2953 do_action(" {$new_status}_{$post->post_type}", $post->ID, $post);2716 do_action("${old_status}_to_$new_status", $post); 2717 do_action("${new_status}_$post->post_type", $post->ID, $post); 2954 2718 } 2955 2719 … … 3004 2768 } 3005 2769 } 3006 $pung = apply_filters('get_enclosed', $pung , $post_id);2770 $pung = apply_filters('get_enclosed', $pung); 3007 2771 return $pung; 3008 2772 } … … 3126 2890 function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') { 3127 2891 global $wpdb; 3128 $null = null;3129 2892 $page_path = rawurlencode(urldecode($page_path)); 3130 2893 $page_path = str_replace('%2F', '/', $page_path); … … 3140 2903 3141 2904 if ( empty($pages) ) 3142 return $null;2905 return null; 3143 2906 3144 2907 foreach ( $pages as $page ) { … … 3146 2909 $curpage = $page; 3147 2910 while ( $curpage->post_parent != 0 ) { 3148 $post_parent = $curpage->post_parent; 3149 $curpage = wp_cache_get( $post_parent, 'posts' ); 3150 if ( false === $curpage ) 3151 $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $post_parent, $post_type ) ); 2911 $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type )); 3152 2912 $path = '/' . $curpage->post_name . $path; 3153 2913 } … … 3157 2917 } 3158 2918 3159 return $null;2919 return null; 3160 2920 } 3161 2921 … … 3213 2973 * @since 2.0.0 3214 2974 * 3215 * @param array $p ages Posts array.3216 * @param int $pa ge_idParent page ID.2975 * @param array $posts Posts array. 2976 * @param int $parent Parent page ID. 3217 2977 * @return array A list arranged by hierarchy. Children immediately follow their parents. 3218 2978 */ 3219 2979 function &get_page_hierarchy( &$pages, $page_id = 0 ) { 2980 3220 2981 if ( empty( $pages ) ) { 3221 2982 $result = array(); … … 3225 2986 $children = array(); 3226 2987 foreach ( (array) $pages as $p ) { 2988 3227 2989 $parent_id = intval( $p->post_parent ); 3228 2990 $children[ $parent_id ][] = $p; 3229 }3230 3231 $result = array();3232 _page_traverse_name( $page_id, $children, $result );2991 } 2992 2993 $result = array(); 2994 _page_traverse_name( $page_id, $children, $result ); 3233 2995 3234 2996 return $result; … … 3239 3001 * $children contains parent-chilren relations 3240 3002 * 3241 * @since 2.9.03242 3003 */ 3243 3004 function _page_traverse_name( $page_id, &$children, &$result ){ 3005 3244 3006 if ( isset( $children[ $page_id ] ) ){ 3007 3245 3008 foreach( (array)$children[ $page_id ] as $child ) { 3009 3246 3010 $result[ $child->ID ] = $child->post_name; 3247 3011 _page_traverse_name( $child->ID, $children, $result ); … … 3522 3286 * @param string|array $object Arguments to override defaults. 3523 3287 * @param string $file Optional filename. 3524 * @param int $p arent Parent post ID.3288 * @param int $post_parent Parent post ID. 3525 3289 * @return int Attachment ID. 3526 3290 */ … … 3672 3436 * @uses do_action() Calls 'delete_attachment' hook on Attachment ID. 3673 3437 * 3674 * @param int $post _id Attachment ID.3438 * @param int $postid Attachment ID. 3675 3439 * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false. 3676 3440 * @return mixed False on failure. Post data on success. … … 3748 3512 $del_file = path_join( dirname($meta['file']), $size['file'] ); 3749 3513 $del_file = apply_filters('wp_delete_file', $del_file); 3750 @ unlink( path_join($uploadpath['basedir'], $del_file) );3514 @ unlink( path_join($uploadpath['basedir'], $del_file) ); 3751 3515 } 3752 3516 } … … 3821 3585 if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location 3822 3586 $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location 3823 elseif ( false !== strpos($file, 'wp-content/uploads') )3824 $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );3825 else3826 $url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir.3587 elseif ( false !== strpos($file, 'wp-content/uploads') ) 3588 $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 ); 3589 else 3590 $url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir. 3827 3591 } 3828 3592 } … … 3831 3595 $url = get_the_guid( $post->ID ); 3832 3596 3833 $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); 3834 3835 if ( 'attachment' != $post->post_type || empty( $url ) ) 3597 if ( 'attachment' != $post->post_type || empty($url) ) 3836 3598 return false; 3837 3599 3838 return $url;3600 return apply_filters( 'wp_get_attachment_url', $url, $post->ID ); 3839 3601 } 3840 3602 … … 4000 3762 4001 3763 /** 4002 * Checked for changed slugs for published post objects and save the old slug. 4003 * 4004 * The function is used when a post object of any type is updated, 4005 * by comparing the current and previous post objects. 3764 * Checked for changed slugs for published posts and save old slug. 3765 * 3766 * The function is used along with form POST data. It checks for the wp-old-slug 3767 * POST field. Will only be concerned with published posts and the slug actually 3768 * changing. 4006 3769 * 4007 3770 * If the slug was changed and not already part of the old slugs then it will be … … 4009 3772 * post. 4010 3773 * 4011 * The most logically usage of this function is redirecting changed post objects, so3774 * The most logically usage of this function is redirecting changed posts, so 4012 3775 * that those that linked to an changed post will be redirected to the new post. 4013 3776 * … … 4015 3778 * 4016 3779 * @param int $post_id Post ID. 4017 * @param object $post The Post Object4018 * @param object $post_before The Previous Post Object4019 3780 * @return int Same as $post_id 4020 3781 */ … … 4024 3785 return; 4025 3786 4026 // we're only concerned with published , non-hierarchical objects4027 if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ))3787 // we're only concerned with published posts 3788 if ( $post->post_status != 'publish' || $post->post_type != 'post' ) 4028 3789 return; 4029 3790 … … 4031 3792 4032 3793 // if we haven't added this old slug before, add it now 4033 if ( ! empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )3794 if ( !in_array($post_before->post_name, $old_slugs) ) 4034 3795 add_post_meta($post_id, '_wp_old_slug', $post_before->post_name); 4035 3796 … … 4134 3895 * @since 0.71 4135 3896 * 3897 * @uses $wpdb 3898 * @uses $blog_id 4136 3899 * @uses apply_filters() Calls 'get_lastpostdate' filter 3900 * 3901 * @global mixed $cache_lastpostdate Stores the last post date 3902 * @global mixed $pagenow The current page being viewed 4137 3903 * 4138 3904 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'. … … 4140 3906 */ 4141 3907 function get_lastpostdate($timezone = 'server') { 4142 return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone ); 3908 global $cache_lastpostdate, $wpdb, $blog_id; 3909 $add_seconds_server = date('Z'); 3910 if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) { 3911 switch(strtolower($timezone)) { 3912 case 'gmt': 3913 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1"); 3914 break; 3915 case 'blog': 3916 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1"); 3917 break; 3918 case 'server': 3919 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1"); 3920 break; 3921 } 3922 $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate; 3923 } else { 3924 $lastpostdate = $cache_lastpostdate[$blog_id][$timezone]; 3925 } 3926 return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone ); 4143 3927 } 4144 3928 … … 4151 3935 * 4152 3936 * @since 1.2.0 3937 * @uses $wpdb 3938 * @uses $blog_id 4153 3939 * @uses apply_filters() Calls 'get_lastpostmodified' filter 4154 3940 * … … 4157 3943 */ 4158 3944 function get_lastpostmodified($timezone = 'server') { 4159 $lastpostmodified = _get_last_post_time( $timezone, 'modified' ); 3945 global $wpdb; 3946 3947 $add_seconds_server = date('Z'); 3948 $timezone = strtolower( $timezone ); 3949 3950 $lastpostmodified = wp_cache_get( "lastpostmodified:$timezone", 'timeinfo' ); 3951 if ( $lastpostmodified ) 3952 return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone ); 3953 3954 switch ( strtolower($timezone) ) { 3955 case 'gmt': 3956 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1"); 3957 break; 3958 case 'blog': 3959 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1"); 3960 break; 3961 case 'server': 3962 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1"); 3963 break; 3964 } 4160 3965 4161 3966 $lastpostdate = get_lastpostdate($timezone); … … 4163 3968 $lastpostmodified = $lastpostdate; 4164 3969 3970 if ( $lastpostmodified ) 3971 wp_cache_set( "lastpostmodified:$timezone", $lastpostmodified, 'timeinfo' ); 3972 4165 3973 return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone ); 4166 }4167 4168 /**4169 * Retrieve latest post date data based on timezone.4170 *4171 * @access private4172 * @since 3.1.04173 *4174 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.4175 * @param string $field Field to check. Can be 'date' or 'modified'.4176 * @return string The date.4177 */4178 function _get_last_post_time( $timezone, $field ) {4179 global $wpdb;4180 4181 if ( !in_array( $field, array( 'date', 'modified' ) ) )4182 return false;4183 4184 $timezone = strtolower( $timezone );4185 4186 $key = "lastpost{$field}:$timezone";4187 4188 $date = wp_cache_get( $key, 'timeinfo' );4189 4190 if ( !$date ) {4191 $add_seconds_server = date('Z');4192 4193 $post_types = get_post_types( array( 'publicly_queryable' => true ) );4194 array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );4195 $post_types = "'" . implode( "', '", $post_types ) . "'";4196 4197 switch ( $timezone ) {4198 case 'gmt':4199 $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");4200 break;4201 case 'blog':4202 $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");4203 break;4204 case 'server':4205 $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");4206 break;4207 }4208 4209 if ( $date )4210 wp_cache_set( $key, $date, 'timeinfo' );4211 }4212 4213 return $date;4214 3974 } 4215 3975 … … 4260 4020 $id = (int) $id; 4261 4021 4262 if ( 0 === $id )4263 return;4264 4265 4022 wp_cache_delete($id, 'posts'); 4266 4023 wp_cache_delete($id, 'post_meta'); … … 4273 4030 4274 4031 if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) { 4275 foreach ( $children as $cid ) { 4276 // Loop detection 4277 if ( $cid == $id ) 4278 continue; 4032 foreach( $children as $cid ) 4279 4033 clean_post_cache( $cid ); 4280 }4281 4034 } 4282 4035 … … 4354 4107 $post_type = 'post'; 4355 4108 4356 if ( $update_term_cache ) { 4357 if ( is_array($post_type) ) { 4358 $ptypes = $post_type; 4359 } elseif ( 'any' == $post_type ) { 4360 // Just use the post_types in the supplied posts. 4361 foreach ( $posts as $post ) 4362 $ptypes[] = $post->post_type; 4363 $ptypes = array_unique($ptypes); 4364 } else { 4365 $ptypes = array($post_type); 4366 } 4367 4368 if ( ! empty($ptypes) ) 4369 update_object_term_cache($post_ids, $ptypes); 4370 } 4109 if ( !is_array($post_type) && 'any' != $post_type && $update_term_cache ) 4110 update_object_term_cache($post_ids, $post_type); 4371 4111 4372 4112 if ( $update_meta_cache ) … … 4458 4198 // If published posts changed clear the lastpostmodified cache 4459 4199 if ( 'publish' == $new_status || 'publish' == $old_status) { 4460 foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { 4461 wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' ); 4462 wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' ); 4463 } 4200 wp_cache_delete( 'lastpostmodified:server', 'timeinfo' ); 4201 wp_cache_delete( 'lastpostmodified:gmt', 'timeinfo' ); 4202 wp_cache_delete( 'lastpostmodified:blog', 'timeinfo' ); 4464 4203 } 4465 4204 … … 4564 4303 * parent will be an ancestor. There will only be two ancestors at the most. 4565 4304 * 4566 * @since 2.5.04305 * @since unknown 4567 4306 * @access private 4568 4307 * @uses $wpdb … … 4584 4323 $id = $_post->ancestors[] = $_post->post_parent; 4585 4324 while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) { 4586 // Loop detection: If the ancestor has been seen before, break. 4587 if ( ( $ancestor == $_post->ID ) || in_array($ancestor, $_post->ancestors) ) 4325 if ( $id == $ancestor ) 4588 4326 break; 4589 4327 $id = $_post->ancestors[] = $ancestor; … … 4913 4651 * 4914 4652 * @param int|object $revision_id Revision ID or revision object. 4915 * @return mixed Null or WP_Error if error, deleted post if success. 4653 * @param array $fields Optional. What fields to restore from. Defaults to all. 4654 * @return mixed Null if error, false if no fields to restore, (int) post ID if success. 4916 4655 */ 4917 4656 function wp_delete_post_revision( $revision_id ) { … … 4986 4725 } 4987 4726 } 4988 4989 /**4990 * Returns the post's parent's post_ID4991 *4992 * @since 3.1.04993 *4994 * @param int $post_id4995 *4996 * @return int|bool false on error4997 */4998 function wp_get_post_parent_id( $post_ID ) {4999 $post = get_post( $post_ID );5000 if ( !$post || is_wp_error( $post ) )5001 return false;5002 return (int) $post->post_parent;5003 }5004 5005 /**5006 * Checks the given subset of the post hierarchy for hierarchy loops.5007 * Prevents loops from forming and breaks those that it finds.5008 *5009 * Attached to the wp_insert_post_parent filter.5010 *5011 * @since 3.1.05012 * @uses wp_find_hierarchy_loop()5013 *5014 * @param int $post_parent ID of the parent for the post we're checking.5015 * @parem int $post_ID ID of the post we're checking.5016 *5017 * @return int The new post_parent for the post.5018 */5019 function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {5020 // Nothing fancy here - bail5021 if ( !$post_parent )5022 return 0;5023 5024 // New post can't cause a loop5025 if ( empty( $post_ID ) )5026 return $post_parent;5027 5028 // Can't be its own parent5029 if ( $post_parent == $post_ID )5030 return 0;5031 5032 // Now look for larger loops5033 5034 if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )5035 return $post_parent; // No loop5036 5037 // Setting $post_parent to the given value causes a loop5038 if ( isset( $loop[$post_ID] ) )5039 return 0;5040 5041 // There's a loop, but it doesn't contain $post_ID. Break the loop.5042 foreach ( array_keys( $loop ) as $loop_member )5043 wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );5044 5045 return $post_parent;5046 }5047 5048 /**5049 * Returns an array of post format slugs to their translated and pretty display versions5050 *5051 * @since 3.1.05052 *5053 * @return array The array of translations5054 */5055 function get_post_format_strings() {5056 $strings = array(5057 'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard5058 'aside' => _x( 'Aside', 'Post format' ),5059 'chat' => _x( 'Chat', 'Post format' ),5060 'gallery' => _x( 'Gallery', 'Post format' ),5061 'link' => _x( 'Link', 'Post format' ),5062 'image' => _x( 'Image', 'Post format' ),5063 'quote' => _x( 'Quote', 'Post format' ),5064 'status' => _x( 'Status', 'Post format' ),5065 'video' => _x( 'Video', 'Post format' ),5066 'audio' => _x( 'Audio', 'Post format' ),5067 );5068 return $strings;5069 }5070 5071 /**5072 * Retrieves an array of post format slugs.5073 *5074 * @since 3.1.05075 *5076 * @return array The array of post format slugs.5077 */5078 function get_post_format_slugs() {5079 // 3.2-early: use array_combine() and array_keys( get_post_format_strings() )5080 $slugs = array(5081 'standard' => 'standard', // Special case. any value that evals to false will be considered standard5082 'aside' => 'aside',5083 'chat' => 'chat',5084 'gallery' => 'gallery',5085 'link' => 'link',5086 'image' => 'image',5087 'quote' => 'quote',5088 'status' => 'status',5089 'video' => 'video',5090 'audio' => 'audio',5091 );5092 return $slugs;5093 }5094 5095 /**5096 * Returns a pretty, translated version of a post format slug5097 *5098 * @since 3.1.05099 *5100 * @param string $slug A post format slug5101 * @return string The translated post format name5102 */5103 function get_post_format_string( $slug ) {5104 $strings = get_post_format_strings();5105 if ( !$slug )5106 return $strings['standard'];5107 else5108 return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';5109 }5110 5111 /**5112 * Sets a post thumbnail.5113 *5114 * @since 3.1.05115 *5116 * @param int|object $post Post ID or object where thumbnail should be attached.5117 * @param int $thumbnail_id Thumbnail to attach.5118 * @return bool True on success, false on failure.5119 */5120 function set_post_thumbnail( $post, $thumbnail_id ) {5121 $post = get_post( $post );5122 $thumbnail_id = absint( $thumbnail_id );5123 if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {5124 $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );5125 if ( ! empty( $thumbnail_html ) ) {5126 update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );5127 return true;5128 }5129 }5130 return false;5131 }5132 5133 /**5134 * Returns a link to a post format index.5135 *5136 * @since 3.1.05137 *5138 * @param $format string Post format5139 * @return string Link5140 */5141 function get_post_format_link( $format ) {5142 $term = get_term_by('slug', 'post-format-' . $format, 'post_format' );5143 if ( ! $term || is_wp_error( $term ) )5144 return false;5145 return get_term_link( $term );5146 }5147 5148 /**5149 * Filters the request to allow for the format prefix.5150 *5151 * @access private5152 * @since 3.1.05153 */5154 function _post_format_request( $qvs ) {5155 if ( ! isset( $qvs['post_format'] ) )5156 return $qvs;5157 $slugs = get_post_format_slugs();5158 if ( isset( $slugs[ $qvs['post_format'] ] ) )5159 $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];5160 $tax = get_taxonomy( 'post_format' );5161 $qvs['post_type'] = $tax->object_type;5162 return $qvs;5163 }5164 add_filter( 'request', '_post_format_request' );5165 5166 /**5167 * Filters the post format term link to remove the format prefix.5168 *5169 * @access private5170 * @since 3.1.05171 */5172 function _post_format_link( $link, $term, $taxonomy ) {5173 global $wp_rewrite;5174 if ( 'post_format' != $taxonomy )5175 return $link;5176 if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {5177 return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );5178 } else {5179 $link = remove_query_arg( 'post_format', $link );5180 return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );5181 }5182 }5183 add_filter( 'term_link', '_post_format_link', 10, 3 );5184 5185 /**5186 * Remove the post format prefix from the name property of the term object created by get_term().5187 *5188 * @access private5189 * @since 3.1.05190 */5191 function _post_format_get_term( $term ) {5192 if ( isset( $term->slug ) ) {5193 $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );5194 }5195 return $term;5196 }5197 add_filter( 'get_post_format', '_post_format_get_term' );5198 5199 /**5200 * Remove the post format prefix from the name property of the term objects created by get_terms().5201 *5202 * @access private5203 * @since 3.1.05204 */5205 function _post_format_get_terms( $terms, $taxonomies, $args ) {5206 if ( in_array( 'post_format', (array) $taxonomies ) ) {5207 if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {5208 foreach( $terms as $order => $name ) {5209 $terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );5210 }5211 } else {5212 foreach ( (array) $terms as $order => $term ) {5213 if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {5214 $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );5215 }5216 }5217 }5218 }5219 return $terms;5220 }5221 add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );5222 5223 /**5224 * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().5225 *5226 * @access private5227 * @since 3.1.05228 */5229 function _post_format_wp_get_object_terms( $terms ) {5230 foreach ( (array) $terms as $order => $term ) {5231 if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {5232 $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );5233 }5234 }5235 return $terms;5236 }5237 add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );5238 5239 ?>
Note: See TracChangeset
for help on using the changeset viewer.