LLMS_Post_Model::toArray()

Coverts the object to an associative array


Description Description

Any property returned by $this->get_properties() will be retrieved via $this->get() and added to the array.

Extending classes can add additional properties to the array by overriding $this->toArrayAfter().

This function is also utilized to serialize the object to JSON.


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/abstracts/abstract.llms.post.model.php

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
public function toArray() {
 
    $arr = array(
        'id' => $this->get( 'id' ),
    );
 
    foreach ( $this->get_to_array_properties() as $prop ) {
 
        if ( in_array( $prop, array( 'content', 'excerpt', 'title' ), true ) ) {
            $post_prop    = "post_{$prop}";
            $arr[ $prop ] = $this->post->$post_prop;
        } else {
            $arr[ $prop ] = $this->get( $prop );
        }
    }
 
    // Add the featured image if the post type supports it.
    if ( post_type_supports( $this->db_post_type, 'thumbnail' ) ) {
        $arr['featured_image'] = $this->get_image( 'full', 'thumbnail' );
    }
 
    // Expand instructors if instructors are supported.
    if ( ! empty( $arr['instructors'] ) && method_exists( $this, 'instructors' ) ) {
 
        foreach ( $arr['instructors'] as &$data ) {
            $instructor = llms_get_instructor( $data['id'] );
            if ( $instructor ) {
                $data = array_merge( $data, $instructor->toArray() );
            }
        }
    } elseif ( ! empty( $arr['author'] ) ) {
 
        $instructor = llms_get_instructor( $arr['author'] );
        if ( $instructor ) {
            $arr['author'] = $instructor->toArray();
        }
    }
 
    /**
     * Filter whether or not "extra" content should be included in the post array
     *
     * `__return_true` (with priority 99) is used to force the filter on during exports.
     *
     * @since 4.8.0
     *
     * @param boolean         $include Whether or not to include extra data. Default is `false`, except on during exports.
     * @param LLMS_Post_Model $model   Post model instance.
     */
    $add_array_extra = apply_filters( 'llms_post_model_to_array_add_extras', false, $this );
 
    /**
     * Filter whether or not "extra" content should be included in the post array
     *
     * The dynamic portion of this hook, `$this->model_post_type`, refers to the model's post type. For example "course",
     * "lesson", "membership", etc...
     *
     * @since 4.7.0
     *
     * @param boolean         $include Whether or not to include extra data.
     * @param LLMS_Post_Model $model   Post model instance.
     */
    $add_array_extra = apply_filters( "llms_{$this->model_post_type}_to_array_add_extras", $add_array_extra, $this );
 
    if ( $add_array_extra ) {
        $arr = $this->to_array_extra( $arr );
    }
 
    // Add custom fields.
    $arr = $this->toArrayCustom( $arr );
 
    // Allow extending classes to add properties easily without overriding the class.
    $arr = $this->toArrayAfter( $arr );
 
    $cpt_data = $this->get_post_type_data();
    if ( $cpt_data->public ) {
        $arr['permalink'] = get_permalink( $this->get( 'id' ) );
    }
 
    ksort( $arr ); // Because i'm anal...
 
    /**
     * Filter the final post array created when converting the object to an array
     *
     * The dynamic portion of this hook, `$this->model_post_type`, refers to the model's post type. For example "course",
     * "lesson", "membership", etc...
     *
     * @since 4.7.0
     *
     * @param array           $arr   Associative array of the model.
     * @param LLMS_Post_Model $model Post model instance.
     */
    return apply_filters( "llms_{$this->model_post_type}_to_array", $arr, $this );
 
}

Top ↑

Changelog Changelog

Changelog
Version Description
5.4.1 Load properties to be used to generate the array from the new get_to_array_properties() method.
4.8.0 Exclude extra data by default. Added llms_post_model_to_array_add_extras filter.
4.7.0 Add exporting of extra data (images and blocks).
3.3.0
3.17.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.