🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2016-10-11 22:52:53.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2016-10-11 22:52:53.0 +00:00:00
commit
53bb53191b4e689266ec839bd02a7a3cc009153f [patch]
tree
81e9f687cd18c5aa96088e3d30c70c20ff2e9147
parent
439d86cc5220b692d71313016d6cb84e920fc390
download
53bb53191b4e689266ec839bd02a7a3cc009153f.tar.gz

ACF formatted fields



Diff

 src/Exceptions/UnsatisfiedDependencyException.php | 15 ++++++-
 src/Models/Meta.php                               | 60 ++++++++++++++++++++++--
 2 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/src/Exceptions/UnsatisfiedDependencyException.php b/src/Exceptions/UnsatisfiedDependencyException.php
new file mode 100644
index 0000000..fa4b67c
--- /dev/null
+++ b/src/Exceptions/UnsatisfiedDependencyException.php
@@ -0,0 +1,15 @@
<?php
namespace Koselig\Exceptions;

use RuntimeException;

/**
 * This exception is thrown when a method depends on a dependency that
 * has not been met.
 *
 * @author Jordan Doyle <jordan@doyle.wf>
 */
class UnsatisfiedDependencyException extends RuntimeException
{
    //
}
diff --git a/src/Models/Meta.php b/src/Models/Meta.php
index 03c6973..e11de6b 100644
--- a/src/Models/Meta.php
+++ b/src/Models/Meta.php
@@ -3,6 +3,8 @@ namespace Koselig\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Koselig\Exceptions\UnsatisfiedDependencyException;
use Koselig\Support\Action;
use Koselig\Support\Wordpress;

/**
@@ -44,24 +46,74 @@ class Meta extends Model
            $page = Wordpress::id();
        }

        if (!isset(self::$cache[$page])) {
        if (!isset(static::$cache[$page])) {
            // get all the meta values for a post, it's more than likely we're going to
            // need this again query, so we'll just grab all the results and cache them.
            self::$cache[$page] = self::where('post_id', $page)->get();
            static::$cache[$page] = static::where('post_id', $page)->get();
        }

        if ($name === null) {
            return self::$cache[$page]->mapWithKeys(function ($item) {
            return static::$cache[$page]->mapWithKeys(function ($item) {
                return [$item->meta_key => $item->meta_value];
            })->all();
        }

        $value = self::$cache[$page]->where('meta_key', $name)->first();
        $value = static::$cache[$page]->where('meta_key', $name)->first();

        return empty($value) ? null : $value->meta_value;
    }

    /**
     * Grab an ACF field from the database.
     *
     * @see Meta::get()
     * @param int|string|null $page page to get meta for (or name of the meta item to get
     *                              if you want to get the current page's meta)
     * @param string|null $name
     * @param bool $format whether to format this field or not
     * @return mixed
     * @throws UnsatisfiedDependencyException
     */
    public static function field($page = null, $name = null, $format = true)
    {
        if (!ctype_digit((string) $page) && $name === null) {
            $name = $page;
            $page = null;
        }

        if ($page === null) {
            $page = Wordpress::id();
        }

        if (!function_exists('acf_format_value')) {
            throw new UnsatisfiedDependencyException('Advanced Custom Fields must be installed to use field');
        }

        $value = static::get($page, $name);

        if (is_serialized($value))
            $value = @unserialize($value);

        $field = static::get($page, '_' . $name);

        if (!acf_is_field_key($field)) {
            return null;
        }

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

        if ($format) {
            $value = acf_format_value($value, $page, $field);
        }

        return $value;
    }

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