wp_xmlrpc_server::_insert_post() WordPress Method

The wp_xmlrpc_server::_insert_post() method is used to insert a new post into a WordPress site. This is done by sending a request to the WordPress XML-RPC server with the required information for the new post. The method can be used to insert a post into a WordPress site from any external application that supports XML-RPC.

wp_xmlrpc_server::_insert_post( WP_User $user, array|IXR_Error $content_struct ) #

Helper method for wp_newPost() and wp_editPost(), containing shared logic.


Description

Top ↑

See also


Top ↑

Parameters

$user

(WP_User)(Required)The post author if post_author isn't set in $content_struct.

$content_struct

(array|IXR_Error)(Required)Post data to insert.


Top ↑

Return

(IXR_Error|string)


Top ↑

Source

File: wp-includes/class-wp-xmlrpc-server.php

1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
protected function _insert_post( $user, $content_struct ) {
    $defaults = array(
        'post_status'    => 'draft',
        'post_type'      => 'post',
        'post_author'    => null,
        'post_password'  => null,
        'post_excerpt'   => null,
        'post_content'   => null,
        'post_title'     => null,
        'post_date'      => null,
        'post_date_gmt'  => null,
        'post_format'    => null,
        'post_name'      => null,
        'post_thumbnail' => null,
        'post_parent'    => null,
        'ping_status'    => null,
        'comment_status' => null,
        '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.' ) );
        }
 
        $author = get_userdata( $post_data['post_author'] );
 
        if ( ! $author ) {
            return new IXR_Error( 404, __( 'Invalid author ID.' ) );
        }
    } else {
        $post_data['post_author'] = $user->ID;
    }
 
    if ( isset( $post_data['comment_status'] ) && 'open' !== $post_data['comment_status'] && 'closed' !== $post_data['comment_status'] ) {
        unset( $post_data['comment_status'] );
    }
 
    if ( isset( $post_data['ping_status'] ) && 'open' !== $post_data['ping_status'] && 'closed' !== $post_data['ping_status'] ) {
        unset( $post_data['ping_status'] );
    }
 
    // Do some timestamp voodoo.
    if ( ! empty( $post_data['post_date_gmt'] ) ) {
        // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
        $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
    } elseif ( ! empty( $post_data['post_date'] ) ) {
        $dateCreated = $post_data['post_date']->getIso();
    }
 
    // Default to not flagging the post date to be edited unless it's intentional.
    $post_data['edit_date'] = false;
 
    if ( ! empty( $dateCreated ) ) {
        $post_data['post_date']     = iso8601_to_datetime( $dateCreated );
        $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'gmt' );
 
        // Flag the post date to be edited.
        $post_data['edit_date'] = true;
    }
 
    if ( ! isset( $post_data['ID'] ) ) {
        $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
    }
    $post_ID = $post_data['ID'];
 
    if ( 'post' === $post_data['post_type'] ) {
        $error = $this->_toggle_sticky( $post_data, $update );
        if ( $error ) {
            return $error;
        }
    }
 
    if ( isset( $post_data['post_thumbnail'] ) ) {
        // Empty value deletes, non-empty value adds/updates.
        if ( ! $post_data['post_thumbnail'] ) {
            delete_post_thumbnail( $post_ID );
        } elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) ) {
            return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
        }
        set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
        unset( $content_struct['post_thumbnail'] );
    }
 
    if ( isset( $post_data['custom_fields'] ) ) {
        $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
    }
 
    if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
        $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
 
        // Accumulate term IDs from terms and terms_names.
        $terms = array();
 
        // First validate the terms specified by ID.
        if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
            $taxonomies = array_keys( $post_data['terms'] );
 
            // Validating term IDs.
            foreach ( $taxonomies as $taxonomy ) {
                if ( ! array_key_exists( $taxonomy, $post_type_taxonomies ) ) {
                    return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
                }
 
                if ( ! current_user_can( $post_type_taxonomies[ $taxonomy ]->cap->assign_terms ) ) {
                    return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
                }
 
                $term_ids           = $post_data['terms'][ $taxonomy ];
                $terms[ $taxonomy ] = array();
                foreach ( $term_ids as $term_id ) {
                    $term = get_term_by( 'id', $term_id, $taxonomy );
 
                    if ( ! $term ) {
                        return new IXR_Error( 403, __( 'Invalid term ID.' ) );
                    }
 
                    $terms[ $taxonomy ][] = (int) $term_id;
                }
            }
        }
 
        // Now validate terms specified by name.
        if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
            $taxonomies = array_keys( $post_data['terms_names'] );
 
            foreach ( $taxonomies as $taxonomy ) {
                if ( ! array_key_exists( $taxonomy, $post_type_taxonomies ) ) {
                    return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
                }
 
                if ( ! current_user_can( $post_type_taxonomies[ $taxonomy ]->cap->assign_terms ) ) {
                    return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
                }
 
                /*
                 * For hierarchical taxonomies, we can't assign a term when multiple terms
                 * in the hierarchy share the same name.
                 */
                $ambiguous_terms = array();
                if ( is_taxonomy_hierarchical( $taxonomy ) ) {
                    $tax_term_names = get_terms(
                        array(
                            'taxonomy'   => $taxonomy,
                            'fields'     => 'names',
                            'hide_empty' => false,
                        )
                    );
 
                    // Count the number of terms with the same name.
                    $tax_term_names_count = array_count_values( $tax_term_names );
 
                    // Filter out non-ambiguous term names.
                    $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one' ) );
 
                    $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
                }
 
                $term_names = $post_data['terms_names'][ $taxonomy ];
                foreach ( $term_names as $term_name ) {
                    if ( in_array( $term_name, $ambiguous_terms, true ) ) {
                        return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
                    }
 
                    $term = get_term_by( 'name', $term_name, $taxonomy );
 
                    if ( ! $term ) {
                        // Term doesn't exist, so check that the user is allowed to create new terms.
                        if ( ! current_user_can( $post_type_taxonomies[ $taxonomy ]->cap->edit_terms ) ) {
                            return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
                        }
 
                        // Create the new term.
                        $term_info = wp_insert_term( $term_name, $taxonomy );
                        if ( is_wp_error( $term_info ) ) {
                            return new IXR_Error( 500, $term_info->get_error_message() );
                        }
 
                        $terms[ $taxonomy ][] = (int) $term_info['term_id'];
                    } else {
                        $terms[ $taxonomy ][] = (int) $term->term_id;
                    }
                }
            }
        }
 
        $post_data['tax_input'] = $terms;
        unset( $post_data['terms'], $post_data['terms_names'] );
    }
 
    if ( isset( $post_data['post_format'] ) ) {
        $format = set_post_format( $post_ID, $post_data['post_format'] );
 
        if ( is_wp_error( $format ) ) {
            return new IXR_Error( 500, $format->get_error_message() );
        }
 
        unset( $post_data['post_format'] );
    }
 
    // Handle enclosures.
    $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
    $this->add_enclosure_if_new( $post_ID, $enclosure );
 
    $this->attach_uploads( $post_ID, $post_data['post_content'] );
 
    /**
     * Filters post data array to be inserted via XML-RPC.
     *
     * @since 3.4.0
     *
     * @param array $post_data      Parsed array of post data.
     * @param array $content_struct Post data array.
     */
    $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
 
    $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
    if ( is_wp_error( $post_ID ) ) {
        return new IXR_Error( 500, $post_ID->get_error_message() );
    }
 
    if ( ! $post_ID ) {
        if ( $update ) {
            return new IXR_Error( 401, __( 'Sorry, the post could not be updated.' ) );
        } else {
            return new IXR_Error( 401, __( 'Sorry, the post could not be created.' ) );
        }
    }
 
    return (string) $post_ID;
}


Top ↑

Changelog

Changelog
VersionDescription
3.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More