wp_xmlrpc_server::_insert_post( WP_User $user, array|IXR_Error $content_struct ): IXR_Error|string
- Since
- 3.4.0
- Source
wp-includes/class-wp-xmlrpc-server.php:1433
Compatibility
- WordPress
- since 3.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$userWP_User- The post author if post_author isn't set in $content_struct.
$content_structarray|IXR_Error- Post data to insert.
Return value
IXR_Error|string
Performance profile
How much work a call to wp_xmlrpc_server::_insert_post() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 3
- Plugin surface
- 1 hook
- Called by
- 2
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 651.
Third-party callbacks on 'xmlrpc_wp_insert_post_data' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below wp_xmlrpc_server::_insert_post()
Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_xmlrpc_server::_insert_post() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 3 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 649 | 3 | 67 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 651 | 3 | 67 | |
| 8.4 | 651 | 3 | 67 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 654 | 3 | 67 | |
| 8.2 | 654 | 3 | 67 | 1 more instruction than PHP 8.1 |
| 8.1 | 653 | 3 | 67 | 3 more instructions than PHP 7.4 |
| 7.4 | 650 | 37–194 | 67 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wp_xmlrpc_server::_insert_post() runs, in this order:
- apply_filters( xmlrpc_wp_insert_post_data )filterline 1704 (+271 into the body)
Filters post data array to be inserted via XML-RPC.
Uses · 28
- wp_parse_args()Merges user defined arguments into defaults array.
- get_post_type_object()Retrieves a post type object by name.
- __()Retrieves the translation of $text.
- get_post()Retrieves post data given a post ID or post object.
- current_user_can()Returns whether the current user has the specified capability.
- get_post_type()Retrieves the post type of the current post or of a given post.
- get_post_status_object()Retrieves a post status object by name.
- absint()Converts a value to non-negative integer.
- get_userdata()Retrieves user info by user ID.
- iso8601_to_datetime()Given an ISO 8601 (Ymd\TH:i:sO) date, returns a MySQL DateTime (Y-m-d H:i:s) format used by post_date[_gmt].
- get_default_post_to_edit()Returns default post information to use when populating the "Write Post" form.
- delete_post_thumbnail()Removes the thumbnail (featured image) from the given post.
Show all 28
- set_post_thumbnail()Sets the post thumbnail (featured image) for the given post.
- get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
- get_term_by()Gets all term data from database by term field and data.
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.
- wp_insert_term()Adds a new term to the database.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- set_post_format()Assign a format to a post
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_update_post()Updates a post with new post data.
- wp_insert_post()Inserts or updates a post in the database.
- IXR_Error::__construct()PHP5 constructor.
- wp_xmlrpc_server::_toggle_sticky()Encapsulates the logic for sticking a post and determining if the user has permission to do so.
- wp_xmlrpc_server::set_custom_fields()Sets custom fields for post.
- wp_xmlrpc_server::add_enclosure_if_new()Adds an enclosure to a post if it's new.
- wp_xmlrpc_server::attach_uploads()Attaches an upload to a post.
Used by · 2
- wp_xmlrpc_server::wp_editPost()Edits a post for any registered post type.
- wp_xmlrpc_server::wp_newPost()Creates a new post for any registered post type.
Source code
protected function _insert_post( $user, $content_struct ) { $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0, 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '', 'post_date' => '', 'post_date_gmt' => '', 'post_format' => null, 'post_name' => null, 'post_thumbnail' => null, 'post_parent' => 0, 'ping_status' => '', 'comment_status' => '', 'custom_fields' => null, 'terms_names' => null, 'terms' => null, 'sticky' => null, 'enclosure' => null, 'ID' => null, ); $post_data = wp_parse_args( array_intersect_key( $content_struct, $defaults ), $defaults ); $post_type = get_post_type_object( $post_data['post_type'] ); if ( ! $post_type ) { return new IXR_Error( 403, __( 'Invalid post type.' ) ); } $update = ! empty( $post_data['ID'] ); if ( $update ) { if ( ! get_post( $post_data['ID'] ) ) { return new IXR_Error( 401, __( 'Invalid post ID.' ) ); } if ( ! current_user_can( 'edit_post', $post_data['ID'] ) ) { return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) ); } if ( get_post_type( $post_data['ID'] ) !== $post_data['post_type'] ) { return new IXR_Error( 401, __( 'The post type may not be changed.' ) ); } } else { if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) ) { return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) ); } } switch ( $post_data['post_status'] ) { case 'draft': case 'pending': break; case 'private': if ( ! current_user_can( $post_type->cap->publish_posts ) ) { return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type.' ) ); } break; case 'publish': case 'future': if ( ! current_user_can( $post_type->cap->publish_posts ) ) { return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type.' ) ); } break; default: if ( ! get_post_status_object( $post_data['post_status'] ) ) { $post_data['post_status'] = 'draft'; } break; } if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) ) { return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type.' ) ); } $post_data['post_author'] = absint( $post_data['post_author'] ); if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] !== $user->ID ) { if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) { return new IXR_Error( 401, __( 'Sorry, you are not allowed to create posts as this user.' ) );Changelog
Introduced in 3.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-xmlrpc-server.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.