🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2016-10-16 9:32:16.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2016-10-16 9:32:16.0 +00:00:00
commit
8af304d353e005fbeba97c92ee4e3f8c066c67ab [patch]
tree
b3715d00eec44880bc96dd98bca3fedd3455900d
parent
a97c0cedf07f2d4abad27f9d31faee92b43e0377
download
8af304d353e005fbeba97c92ee4e3f8c066c67ab.tar.gz

Ability to get ACF meta from a post object



Diff

 src/Models/Post.php | 52 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 49 insertions(+), 3 deletions(-)

diff --git a/src/Models/Post.php b/src/Models/Post.php
index 9a9071f..155909d 100644
--- a/src/Models/Post.php
+++ b/src/Models/Post.php
@@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Koselig\Exceptions\UnsatisfiedDependencyException;
use Koselig\Support\Action;

/**
 * Table containing all the items within the CMS.
@@ -62,9 +64,6 @@ class Post extends Model
     */
    public function getMeta($key = null)
    {
        /*
         * @var Collection
         */
        $meta = $this->meta;

        if (is_array($key)) {
@@ -81,6 +80,53 @@ class Post extends Model
    }

    /**
     * Get meta values for this Post.
     *
     * @param array|string|null $key key (or keys) to get or null for all.
     * @param bool $format whether to format this field or not
     * @return array array of ACF values.
     */
    public function getACF($key = null, $format = true)
    {
        if (!function_exists('acf_format_value')) {
            throw new UnsatisfiedDependencyException('Advanced Custom Fields must be installed to use field');
        }

        $meta = $this->getMeta($key);

        foreach ($meta as $key => $value)
        {
            if (is_serialized($value)) {
                $value = @unserialize($value);
            } else {
                unset($meta[$key]);
                continue;
            }

            $field = $this->getMeta('_' . $key);

            if (!acf_is_field_key($field)) {
                unset($meta[$key]);
                continue;
            }

            $field = get_field_object($field, $key, false, false);
            $value = Action::filter('acf/load_value', $value, $key, $field);
            $value = Action::filter('acf/load_value/type=' . $field['type'], $value, $this->ID, $field);
            $value = Action::filter('acf/load_value/name=' . $field['_name'], $value, $this->ID, $field);
            $value = Action::filter('acf/load_value/key=' . $field['key'], $value, $this->ID, $field);

            if ($format) {
                $value = acf_format_value($value, $this->ID, $field);
            }

            $meta[$key] = $value;
        }

        return $meta;
    }

    /**
     * Get the author that this post belongs to.
     *
     * @return BelongsTo