WordPressを読む 38-1 /blog/wp-includes/post.php 1
2014/12/18
目次
/blog/wp-includes/post.php
関数 create_initial_post_types()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | <?php /** * Post functions and post utility function. * * @package WordPress * @subpackage Post * @since 1.5.0 */ // // Post Type Registration // /** * Creates the initial post types when 'init' action is fired. * * @since 2.9.0 */ function create_initial_post_types() { register_post_type( 'post', array( 'labels' => array( 'name_admin_bar' => _x( 'Post', 'add new on admin bar' ), ), 'public' => true, '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ 'capability_type' => 'post', 'map_meta_cap' => true, 'hierarchical' => false, 'rewrite' => false, 'query_var' => false, 'delete_with_user' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ), ) ); register_post_type( 'page', array( 'labels' => array( 'name_admin_bar' => _x( 'Page', 'add new on admin bar' ), ), 'public' => true, 'publicly_queryable' => false, '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ 'capability_type' => 'page', 'map_meta_cap' => true, 'hierarchical' => true, 'rewrite' => false, 'query_var' => false, 'delete_with_user' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ), ) ); register_post_type( 'attachment', array( 'labels' => array( 'name' => _x('Media', 'post type general name'), 'name_admin_bar' => _x( 'Media', 'add new from admin bar' ), 'add_new' => _x( 'Add New', 'add new media' ), 'edit_item' => __( 'Edit Media' ), 'view_item' => __( 'View Attachment Page' ), ), 'public' => true, 'show_ui' => true, '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ 'capability_type' => 'post', 'capabilities' => array( 'create_posts' => 'upload_files', ), 'map_meta_cap' => true, 'hierarchical' => false, 'rewrite' => false, 'query_var' => false, 'show_in_nav_menus' => false, 'delete_with_user' => true, 'supports' => array( 'title', 'author', 'comments' ), ) ); add_post_type_support( 'attachment:audio', 'thumbnail' ); add_post_type_support( 'attachment:video', 'thumbnail' ); register_post_type( 'revision', array( 'labels' => array( 'name' => __( 'Revisions' ), 'singular_name' => __( 'Revision' ), ), 'public' => false, '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */ 'capability_type' => 'post', 'map_meta_cap' => true, 'hierarchical' => false, 'rewrite' => false, 'query_var' => false, 'can_export' => false, 'delete_with_user' => true, 'supports' => array( 'author' ), ) ); register_post_type( 'nav_menu_item', array( 'labels' => array( 'name' => __( 'Navigation Menu Items' ), 'singular_name' => __( 'Navigation Menu Item' ), ), 'public' => false, '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ 'hierarchical' => false, 'rewrite' => false, 'delete_with_user' => false, 'query_var' => false, ) ); register_post_status( 'publish', array( 'label' => _x( 'Published', 'post' ), 'public' => true, '_builtin' => true, /* internal use only. */ 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ), ) ); register_post_status( 'future', array( 'label' => _x( 'Scheduled', 'post' ), 'protected' => true, '_builtin' => true, /* internal use only. */ 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ), ) ); register_post_status( 'draft', array( 'label' => _x( 'Draft', 'post' ), 'protected' => true, '_builtin' => true, /* internal use only. */ 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ), ) ); register_post_status( 'pending', array( 'label' => _x( 'Pending', 'post' ), 'protected' => true, '_builtin' => true, /* internal use only. */ 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ), ) ); register_post_status( 'private', array( 'label' => _x( 'Private', 'post' ), 'private' => true, '_builtin' => true, /* internal use only. */ 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ), ) ); register_post_status( 'trash', array( 'label' => _x( 'Trash', 'post' ), 'internal' => true, '_builtin' => true, /* internal use only. */ 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ), 'show_in_admin_status_list' => true, ) ); register_post_status( 'auto-draft', array( 'label' => 'auto-draft', 'internal' => true, '_builtin' => true, /* internal use only. */ ) ); register_post_status( 'inherit', array( 'label' => 'inherit', 'internal' => true, '_builtin' => true, /* internal use only. */ 'exclude_from_search' => false, ) ); } |
関数定義の間の処理
1 | add_action( 'init', 'create_initial_post_types', 0 ); // highest priority |
関数 get_attached_file()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * Retrieve attached file path based on attachment ID. * * By default the path will go through the 'get_attached_file' filter, but * passing a true to the $unfiltered argument of get_attached_file() will * return the file path unfiltered. * * The function works by getting the single post meta name, named * '_wp_attached_file' and returning it. This is a convenience function to * prevent looking up the meta name and provide a mechanism for sending the * attached filename through a filter. * * @since 2.0.0 * * @param int $attachment_id Attachment ID. * @param bool $unfiltered Optional. Whether to apply filters. Default false. * @return string|bool The file path to where the attached file should be, false otherwise. */ function get_attached_file( $attachment_id, $unfiltered = false ) { $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); // If the file is relative, prepend upload dir. if ( $file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) ) $file = $uploads['basedir'] . "/$file"; if ( $unfiltered ) return $file; /** * Filter the attached file based on the given ID. * * @since 2.1.0 * * @param string $file Path to attached file. * @param int $attachment_id Attachment ID. */ return apply_filters( 'get_attached_file', $file, $attachment_id ); } |
関数 update_attached_file()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * Update attachment file path based on attachment ID. * * Used to update the file path of the attachment, which uses post meta name * '_wp_attached_file' to store the path of the attachment. * * @since 2.1.0 * * @param int $attachment_id Attachment ID. * @param string $file File path for the attachment. * @return bool True on success, false on failure. */ function update_attached_file( $attachment_id, $file ) { if ( !get_post( $attachment_id ) ) return false; /** * Filter the path to the attached file to update. * * @since 2.1.0 * * @param string $file Path to the attached file to update. * @param int $attachment_id Attachment ID. */ $file = apply_filters( 'update_attached_file', $file, $attachment_id ); if ( $file = _wp_relative_upload_path( $file ) ) return update_post_meta( $attachment_id, '_wp_attached_file', $file ); else return delete_post_meta( $attachment_id, '_wp_attached_file' ); } |
関数 _wp_relative_upload_path()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /** * Return relative path to an uploaded file. * * The path is relative to the current upload dir. * * @since 2.9.0 * * @param string $path Full path to the file. * @return string Relative path on success, unchanged path on failure. */ function _wp_relative_upload_path( $path ) { $new_path = $path; $uploads = wp_upload_dir(); if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) { $new_path = str_replace( $uploads['basedir'], '', $new_path ); $new_path = ltrim( $new_path, '/' ); } /** * Filter the relative path to an uploaded file. * * @since 2.9.0 * * @param string $new_path Relative path to the file. * @param string $path Full path to the file. */ return apply_filters( '_wp_relative_upload_path', $new_path, $path ); } |
関数 get_children()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | /** * Retrieve all children of the post parent ID. * * Normally, without any enhancements, the children would apply to pages. In the * context of the inner workings of WordPress, pages, posts, and attachments * share the same table, so therefore the functionality could apply to any one * of them. It is then noted that while this function does not work on posts, it * does not mean that it won't work on posts. It is recommended that you know * what context you wish to retrieve the children of. * * Attachments may also be made the child of a post, so if that is an accurate * statement (which needs to be verified), it would then be possible to get * all of the attachments for a post. Attachments have since changed since * version 2.5, so this is most likely unaccurate, but serves generally as an * example of what is possible. * * The arguments listed as defaults are for this function and also of the * {@link get_posts()} function. The arguments are combined with the * get_children defaults and are then passed to the {@link get_posts()} * function, which accepts additional arguments. You can replace the defaults in * this function, listed below and the additional arguments listed in the * {@link get_posts()} function. * * The 'post_parent' is the most important argument and important attention * needs to be paid to the $args parameter. If you pass either an object or an * integer (number), then just the 'post_parent' is grabbed and everything else * is lost. If you don't specify any arguments, then it is assumed that you are * in The Loop and the post parent will be grabbed for from the current post. * * The 'post_parent' argument is the ID to get the children. The 'numberposts' * is the amount of posts to retrieve that has a default of '-1', which is * used to get all of the posts. Giving a number higher than 0 will only * retrieve that amount of posts. * * The 'post_type' and 'post_status' arguments can be used to choose what * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress * post types are 'post', 'pages', and 'attachments'. The 'post_status' * argument will accept any post status within the write administration panels. * * @internal Claims made in the long description might be inaccurate. * @since 2.0.0 * * @see get_posts() * * @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. * @param string $output Optional. Constant for return type. Accepts OBJECT, ARRAY_A, ARRAY_N. * Default OBJECt. * @return array Array of children, where the type of each element is determined by $output parameter. * Empty array on failure. */ function get_children( $args = '', $output = OBJECT ) { $kids = array(); if ( empty( $args ) ) { if ( isset( $GLOBALS['post'] ) ) { $args = array('post_parent' => (int) $GLOBALS['post']->post_parent ); } else { return $kids; } } elseif ( is_object( $args ) ) { $args = array('post_parent' => (int) $args->post_parent ); } elseif ( is_numeric( $args ) ) { $args = array('post_parent' => (int) $args); } $defaults = array( 'numberposts' => -1, 'post_type' => 'any', 'post_status' => 'any', 'post_parent' => 0, ); $r = wp_parse_args( $args, $defaults ); $children = get_posts( $r ); if ( ! $children ) return $kids; if ( ! empty( $r['fields'] ) ) return $children; update_post_cache($children); foreach ( $children as $key => $child ) $kids[$child->ID] = $children[$key]; if ( $output == OBJECT ) { return $kids; } elseif ( $output == ARRAY_A ) { foreach ( (array) $kids as $kid ) $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]); return $weeuns; } elseif ( $output == ARRAY_N ) { foreach ( (array) $kids as $kid ) $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID])); return $babes; } else { return $kids; } } |
関数 get_extended()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * Get extended entry info (<!--more-->). * * There should not be any space after the second dash and before the word * 'more'. There can be text or space(s) after the word 'more', but won't be * referenced. * * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before * the <code><!--more--></code>. The 'extended' key has the content after the * <code><!--more--></code> comment. The 'more_text' key has the custom "Read More" text. * * @since 1.0.0 * * @param string $post Post content. * @return array Post before ('main'), after ('extended'), and custom readmore ('more_text'). */ function get_extended( $post ) { //Match the new style more links. if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) { list($main, $extended) = explode($matches[0], $post, 2); $more_text = $matches[1]; } else { $main = $post; $extended = ''; $more_text = ''; } // leading and trailing whitespace. $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main); $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended); $more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text); return array( 'main' => $main, 'extended' => $extended, 'more_text' => $more_text ); } |
関数 get_post()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /** * Retrieves post data given a post ID or post object. * * See {@link sanitize_post()} for optional $filter values. Also, the parameter * $post, must be given as a variable, since it is passed by reference. * * @since 1.5.1 * * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. * @param string $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N. * Default OBJECT. * @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db', * or 'display'. Default 'raw'. * @return WP_Post|null WP_Post on success or null on failure. */ function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { if ( empty( $post ) && isset( $GLOBALS['post'] ) ) $post = $GLOBALS['post']; if ( is_a( $post, 'WP_Post' ) ) { $_post = $post; } elseif ( is_object( $post ) ) { if ( empty( $post->filter ) ) { $_post = sanitize_post( $post, 'raw' ); $_post = new WP_Post( $_post ); } elseif ( 'raw' == $post->filter ) { $_post = new WP_Post( $post ); } else { $_post = WP_Post::get_instance( $post->ID ); } } else { $_post = WP_Post::get_instance( $post ); } if ( ! $_post ) return null; $_post = $_post->filter( $filter ); if ( $output == ARRAY_A ) return $_post->to_array(); elseif ( $output == ARRAY_N ) return array_values( $_post->to_array() ); return $_post; } |