WordPressを読む 38-5 /blog/wp-includes/post.php 5
2014/12/25
目次
- 1 /blog/wp-includes/post.php 5
- 2 関数 _add_post_type_submenus()
- 3 関数定義の間の処理
- 4 関数 add_post_type_support()
- 5 関数 remove_post_type_support()
- 6 関数 get_all_post_type_supports()
- 7 関数 post_type_supports()
- 8 関数 set_post_type()
- 9 関数 get_posts()
- 10 関数 add_post_meta()
- 11 関数 delete_post_meta()
- 12 関数 get_post_meta()
- 13 関数 delete_post_meta_by_key()
- 14 関数 get_post_custom()
- 15 関数 get_post_custom_keys()
- 16 関数 get_post_custom_values()
- 17 関数 is_sticky()
- 18 関数 sanitize_post()
- 19 関数 sanitize_post_field()
/blog/wp-includes/post.php 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Add submenus for post types. * * @access private * @since 3.1.0 */ function _add_post_type_submenus() { foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { $ptype_obj = get_post_type_object( $ptype ); // Sub-menus only. if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true ) continue; add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" ); } } |
関数定義の間の処理
1 | add_action( 'admin_menu', '_add_post_type_submenus' ); |
関数 add_post_type_support()
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 | /** * Register support of certain features for a post type. * * All core features are directly associated with a functional area of the edit * screen, such as the editor or a meta box. Features include: 'title', 'editor', * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', * 'thumbnail', 'custom-fields', and 'post-formats'. * * Additionally, the 'revisions' feature dictates whether the post type will * store revisions, and the 'comments' feature dictates whether the comments * count will show on the edit screen. * * @since 3.0.0 * * @param string $post_type The post type for which to add the feature. * @param string|array $feature The feature being added, accepts an array of * feature strings or a single string. */ function add_post_type_support( $post_type, $feature ) { global $_wp_post_type_features; $features = (array) $feature; foreach ($features as $feature) { if ( func_num_args() == 2 ) $_wp_post_type_features[$post_type][$feature] = true; else $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 ); } } |
関数 remove_post_type_support()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Remove support for a feature from a post type. * * @since 3.0.0 * * @param string $post_type The post type for which to remove the feature. * @param string $feature The feature being removed. */ function remove_post_type_support( $post_type, $feature ) { global $_wp_post_type_features; if ( isset( $_wp_post_type_features[$post_type][$feature] ) ) unset( $_wp_post_type_features[$post_type][$feature] ); } |
関数 get_all_post_type_supports()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Get all the post type features * * @since 3.4.0 * * @param string $post_type The post type. * @return array Post type supports list. */ function get_all_post_type_supports( $post_type ) { global $_wp_post_type_features; if ( isset( $_wp_post_type_features[$post_type] ) ) return $_wp_post_type_features[$post_type]; return array(); } |
関数 post_type_supports()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Check a post type's support for a given feature. * * @since 3.0.0 * * @param string $post_type The post type being checked. * @param string $feature the feature being checked. * @return bool Whether the post type supports the given feature. */ function post_type_supports( $post_type, $feature ) { global $_wp_post_type_features; return ( isset( $_wp_post_type_features[$post_type][$feature] ) ); } |
関数 set_post_type()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Update the post type for the post ID. * * The page or post cache will be cleaned for the post ID. * * @since 2.5.0 * * @global wpdb $wpdb WordPress database access abstraction object. * * @param int $post_id Optional. Post ID to change post type. Default 0. * @param string $post_type Optional. Post type. Accepts 'post' or 'page' to * name a few. Default 'post'. * @return int Amount of rows changed. Should be 1 for success and 0 for failure. */ function set_post_type( $post_id = 0, $post_type = 'post' ) { global $wpdb; $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db'); $return = $wpdb->update( $wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) ); clean_post_cache( $post_id ); return $return; } |
関数 get_posts()
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 | /** * Retrieve list of latest posts or posts matching criteria. * * The defaults are as follows: * * @since 1.2.0 * * @see WP_Query::parse_query() * * @param array $args { * Optional. Arguments to retrieve posts. {@see WP_Query::parse_query()} for more * available arguments. * * @type int $numberposts Total number of posts to retrieve. Is an alias of $posts_per_page * in WP_Query. Accepts 1+ and -1 for all. Default 5. * @type int $offset The number of posts to offset before retrieval. Default 0. * @type int|string $category Category ID or comma-separated list of IDs (this or any children). * Is an alias of $cat in WP_Query. Default 0. * @type string $orderby Which field to order posts by. Accepts post fields. Default 'date'. * @type array $include An array of post IDs to retrieve, sticky posts will be included. * Is an alias of $post__in in WP_Query. Default empty array. * @type array $exclude An array of post IDs not to retrieve. Default empty array. * @type string $meta_key Custom field key. Default empty. * @type mixed $meta_value Custom field value. Default empty string. * @type string $post_type Post type. Default 'post'. * @type bool $suppress_filters Whether to suppress filters. Default true. * } * @return array List of posts. */ function get_posts( $args = null ) { $defaults = array( 'numberposts' => 5, 'offset' => 0, 'category' => 0, 'orderby' => 'date', 'order' => 'DESC', 'include' => array(), 'exclude' => array(), 'meta_key' => '', 'meta_value' =>'', 'post_type' => 'post', 'suppress_filters' => true ); $r = wp_parse_args( $args, $defaults ); if ( empty( $r['post_status'] ) ) $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish'; if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) ) $r['posts_per_page'] = $r['numberposts']; if ( ! empty($r['category']) ) $r['cat'] = $r['category']; if ( ! empty($r['include']) ) { $incposts = wp_parse_id_list( $r['include'] ); $r['posts_per_page'] = count($incposts); // only the number of posts included $r['post__in'] = $incposts; } elseif ( ! empty($r['exclude']) ) $r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); $r['ignore_sticky_posts'] = true; $r['no_found_rows'] = true; $get_posts = new WP_Query; return $get_posts->query($r); } |
関数 add_post_meta()
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 | // // Post meta functions // /** * Add meta data field to a post. * * Post meta data is called "Custom Fields" on the Administration Screen. * * @since 1.5.0 * * @param int $post_id Post ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. * @param bool $unique Optional. Whether the same key should not be added. * Default false. * @return int|bool Meta ID on success, false on failure. */ function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) { // Make sure meta is added to the post, not a revision. if ( $the_post = wp_is_post_revision($post_id) ) $post_id = $the_post; return add_metadata('post', $post_id, $meta_key, $meta_value, $unique); } |
関数 delete_post_meta()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Remove metadata matching criteria from a post. * * You can match based on the key, or key and value. Removing based on key and * value, will keep from removing duplicate metadata with the same key. It also * allows removing all metadata matching key, if needed. * * @since 1.5.0 * * @param int $post_id Post ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Optional. Metadata value. Must be serializable if * non-scalar. Default empty. * @return bool True on success, false on failure. */ function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { // Make sure meta is added to the post, not a revision. if ( $the_post = wp_is_post_revision($post_id) ) $post_id = $the_post; return delete_metadata('post', $post_id, $meta_key, $meta_value); } |
関数 get_post_meta()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Retrieve post meta field for a post. * * @since 1.5.0 * * @param int $post_id Post ID. * @param string $key Optional. The meta key to retrieve. By default, returns * data for all keys. Default empty. * @param bool $single Optional. Whether to return a single value. Default false. * @return mixed Will be an array if $single is false. Will be value of meta data * field if $single is true. */ function get_post_meta( $post_id, $key = '', $single = false ) { return get_metadata('post', $post_id, $key, $single); } |
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 | /** * Update post meta field based on post ID. * * Use the $prev_value parameter to differentiate between meta fields with the * same key and post ID. * * If the meta field for the post does not exist, it will be added. * * @since 1.5.0 * * @param int $post_id Post ID. * @param string $meta_key Metadata key. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. * @param mixed $prev_value Optional. Previous value to check before removing. * Default empty. * @return int|bool Meta ID if the key didn't exist, true on successful update, * false on failure. */ function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { // Make sure meta is added to the post, not a revision. if ( $the_post = wp_is_post_revision($post_id) ) $post_id = $the_post; return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value); } |
関数 delete_post_meta_by_key()
1 2 3 4 5 6 7 8 9 10 11 | /** * Delete everything from post meta matching meta key. * * @since 2.3.0 * * @param string $post_meta_key Key to search for when deleting. * @return bool Whether the post meta key was deleted from the database. */ function delete_post_meta_by_key( $post_meta_key ) { return delete_metadata( 'post', null, $post_meta_key, '', true ); } |
関数 get_post_custom()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Retrieve post meta fields, based on post ID. * * The post meta fields are retrieved from the cache where possible, * so the function is optimized to be called more than once. * * @since 1.2.0 * * @param int $post_id Optional. Post ID. Default is ID of the global $post. * @return array Post meta for the given post. */ function get_post_custom( $post_id = 0 ) { $post_id = absint( $post_id ); if ( ! $post_id ) $post_id = get_the_ID(); return get_post_meta( $post_id ); } |
関数 get_post_custom_keys()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Retrieve meta field names for a post. * * If there are no meta fields, then nothing (null) will be returned. * * @since 1.2.0 * * @param int $post_id Optional. Post ID. Default is ID of the global $post. * @return array|null Either array of the keys, or null if keys could not be * retrieved. */ function get_post_custom_keys( $post_id = 0 ) { $custom = get_post_custom( $post_id ); if ( !is_array($custom) ) return; if ( $keys = array_keys($custom) ) return $keys; } |
関数 get_post_custom_values()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Retrieve values for a custom post field. * * The parameters must not be considered optional. All of the post meta fields * will be retrieved and only the meta field key values returned. * * @since 1.2.0 * * @param string $key Optional. Meta field key. Default empty. * @param int $post_id Optional. Post ID. Default is ID of the global $post. * @return array Meta field values. */ function get_post_custom_values( $key = '', $post_id = 0 ) { if ( !$key ) return null; $custom = get_post_custom($post_id); return isset($custom[$key]) ? $custom[$key] : null; } |
関数 is_sticky()
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 | /** * Check if post is sticky. * * Sticky posts should remain at the top of The Loop. If the post ID is not * given, then The Loop ID for the current post will be used. * * @since 2.7.0 * * @param int $post_id Optional. Post ID. Default is ID of the global $post. * @return bool Whether post is sticky. */ function is_sticky( $post_id = 0 ) { $post_id = absint( $post_id ); if ( ! $post_id ) $post_id = get_the_ID(); $stickies = get_option( 'sticky_posts' ); if ( ! is_array( $stickies ) ) return false; if ( in_array( $post_id, $stickies ) ) return true; return false; } |
関数 sanitize_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 | /** * Sanitize every post field. * * If the context is 'raw', then the post object or array will get minimal * sanitization of the integer fields. * * @since 2.3.0 * * @see sanitize_post_field() * * @param object|WP_Post|array $post The Post Object or Array * @param string $context Optional. How to sanitize post fields. * Accepts 'raw', 'edit', 'db', or 'display'. * Default 'display'. * @return object|WP_Post|array The now sanitized Post Object or Array (will be the * same type as $post). */ function sanitize_post( $post, $context = 'display' ) { if ( is_object($post) ) { // Check if post already filtered for this context. if ( isset($post->filter) && $context == $post->filter ) return $post; if ( !isset($post->ID) ) $post->ID = 0; foreach ( array_keys(get_object_vars($post)) as $field ) $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context); $post->filter = $context; } else { // Check if post already filtered for this context. if ( isset($post['filter']) && $context == $post['filter'] ) return $post; if ( !isset($post['ID']) ) $post['ID'] = 0; foreach ( array_keys($post) as $field ) $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context); $post['filter'] = $context; } return $post; } |
関数 sanitize_post_field()
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 | /** * Sanitize post field based on context. * * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts * are treated like 'display' when calling filters. * * @since 2.3.0 * * @param string $field The Post Object field name. * @param mixed $value The Post Object value. * @param int $post_id Post ID. * @param string $context How to sanitize post fields. Looks for 'raw', 'edit', * 'db', 'display', 'attribute' and 'js'. * @return mixed Sanitized value. */ function sanitize_post_field($field, $value, $post_id, $context) { $int_fields = array('ID', 'post_parent', 'menu_order'); if ( in_array($field, $int_fields) ) $value = (int) $value; // Fields which contain arrays of integers. $array_int_fields = array( 'ancestors' ); if ( in_array($field, $array_int_fields) ) { $value = array_map( 'absint', $value); return $value; } if ( 'raw' == $context ) return $value; $prefixed = false; if ( false !== strpos($field, 'post_') ) { $prefixed = true; $field_no_prefix = str_replace('post_', '', $field); } if ( 'edit' == $context ) { $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password'); if ( $prefixed ) { /** * Filter the value of a specific post field to edit. * * The dynamic portion of the hook name, $field, refers to the post * field name. * * @since 2.3.0 * * @param mixed $value Value of the post field. * @param int $post_id Post ID. */ $value = apply_filters( "edit_{$field}", $value, $post_id ); /** * Filter the value of a specific post field to edit. * * The dynamic portion of the hook name, $field_no_prefix, refers to * the post field name. * * @since 2.3.0 * * @param mixed $value Value of the post field. * @param int $post_id Post ID. */ $value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id ); } else { $value = apply_filters( "edit_post_{$field}", $value, $post_id ); } if ( in_array($field, $format_to_edit) ) { if ( 'post_content' == $field ) $value = format_to_edit($value, user_can_richedit()); else $value = format_to_edit($value); } else { $value = esc_attr($value); } } else if ( 'db' == $context ) { if ( $prefixed ) { /** * Filter the value of a specific post field before saving. * * The dynamic portion of the hook name, $field, refers to the post * field name. * * @since 2.3.0 * * @param mixed $value Value of the post field. */ $value = apply_filters( "pre_{$field}", $value ); /** * Filter the value of a specific field before saving. * * The dynamic portion of the hook name, $field_no_prefix, refers * to the post field name. * * @since 2.3.0 * * @param mixed $value Value of the post field. */ $value = apply_filters( "{$field_no_prefix}_save_pre", $value ); } else { $value = apply_filters( "pre_post_{$field}", $value ); /** * Filter the value of a specific post field before saving. * * The dynamic portion of the hook name, $field, refers to the post * field name. * * @since 2.3.0 * * @param mixed $value Value of the post field. */ $value = apply_filters( "{$field}_pre", $value ); } } else { // Use display filters by default. if ( $prefixed ) { /** * Filter the value of a specific post field for display. * * The dynamic portion of the hook name, $field, refers to the post * field name. * * @since 2.3.0 * * @param mixed $value Value of the prefixed post field. * @param int $post_id Post ID. * @param string $context Context for how to sanitize the field. Possible * values include 'raw', 'edit', 'db', 'display', * 'attribute' and 'js'. */ $value = apply_filters( $field, $value, $post_id, $context ); } else { $value = apply_filters( "post_{$field}", $value, $post_id, $context ); } } if ( 'attribute' == $context ) $value = esc_attr($value); else if ( 'js' == $context ) $value = esc_js($value); return $value; } |