diff --git a/src/AdvancedCustomFields.php b/src/AdvancedCustomFields.php
index 08c914b..20dd2f0 100644
--- a/src/AdvancedCustomFields.php
+++ b/src/AdvancedCustomFields.php
@@ -25,16 +25,6 @@ public function __construct(Post $post)
$this->post = $post;
}
- /**
- * @return mixed
- */
- public function get($fieldName)
- {
- $field = FieldFactory::make($fieldName, $this->post);
-
- return $field->get();
- }
-
/**
* @param string $name
*
@@ -42,7 +32,9 @@ public function get($fieldName)
*/
public function __get($name)
{
- return $this->get($name);
+ $field = FieldFactory::make($name, $this->post);
+
+ return $field->get();
}
/**
diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php
index 25cca15..a75655d 100644
--- a/src/Field/BasicField.php
+++ b/src/Field/BasicField.php
@@ -42,25 +42,33 @@ abstract class BasicField
*/
protected $value;
+ /**
+ * @var string
+ */
+ protected $connection;
+
/**
* Constructor method.
+ *
+ * @param Post $post
*/
- public function __construct()
+ public function __construct(Post $post)
{
+ $this->post = $post;
$this->postMeta = new PostMeta();
+ $this->postMeta->setConnection($post->getConnectionName());
}
/**
* Get the value of a field according it's post ID.
*
* @param string $field
- * @param Post $post
*
* @return array|string
*/
- public function fetchValue($field, Post $post)
+ public function fetchValue($field)
{
- $postMeta = $this->postMeta->where('post_id', $post->ID)
+ $postMeta = $this->postMeta->where('post_id', $this->post->ID)
->where('meta_key', $field)
->first();
@@ -80,17 +88,15 @@ public function fetchValue($field, Post $post)
/**
* @param string $fieldName
- * @param Post $post
*
* @return string
*/
- public function fetchFieldKey($fieldName, Post $post)
+ public function fetchFieldKey($fieldName)
{
- $this->post = $post;
$this->name = $fieldName;
- $postMeta = $this->postMeta->where('post_id', $post->ID)
- ->where('meta_key', '_'.$fieldName)
+ $postMeta = $this->postMeta->where('post_id', $this->post->ID)
+ ->where('meta_key', '_' . $fieldName)
->first();
if (!$postMeta) {
@@ -105,16 +111,20 @@ public function fetchFieldKey($fieldName, Post $post)
/**
* @param string $fieldKey
*
- * @return string
+ * @return string|null
*/
public function fetchFieldType($fieldKey)
{
$post = $this->post->where('post_name', $fieldKey)->first();
- $fieldData = unserialize($post->post_content);
- $this->type = isset($fieldData['type']) ? $fieldData['type'] : 'text';
+ if ($post) {
+ $fieldData = unserialize($post->post_content);
+ $this->type = isset($fieldData['type']) ? $fieldData['type'] : 'text';
+
+ return $this->type;
+ }
- return $this->type;
+ return null;
}
/**
diff --git a/src/Field/DateTime.php b/src/Field/DateTime.php
index fc3aba1..f75860c 100644
--- a/src/Field/DateTime.php
+++ b/src/Field/DateTime.php
@@ -20,11 +20,10 @@ class DateTime extends BasicField implements FieldInterface
/**
* @param string $fieldName
- * @param Post $post
*/
- public function process($fieldName, Post $post)
+ public function process($fieldName)
{
- $dateString = $this->fetchValue($fieldName, $post);
+ $dateString = $this->fetchValue($fieldName);
$format = $this->getDateFormatFromString($dateString);
$this->date = Carbon::createFromFormat($format, $dateString);
}
diff --git a/src/Field/File.php b/src/Field/File.php
index c7ab0a9..7c03259 100644
--- a/src/Field/File.php
+++ b/src/Field/File.php
@@ -44,12 +44,11 @@ class File extends BasicField implements FieldInterface
/**
* @param string $field
- * @param Post $post
*/
- public function process($field, Post $post)
+ public function process($field)
{
- $value = $this->fetchValue($field, $post);
- $file = $post->find($value);
+ $value = $this->fetchValue($field);
+ $file = $this->post->find($value);
$this->fillFields($file);
}
diff --git a/src/Field/Gallery.php b/src/Field/Gallery.php
index 3ffb9ce..27ef697 100644
--- a/src/Field/Gallery.php
+++ b/src/Field/Gallery.php
@@ -25,16 +25,15 @@ class Gallery extends Image implements FieldInterface
/**
* @param $field
- * @param Post $post
*/
- public function process($field, Post $post)
+ public function process($field)
{
- $ids = $this->fetchValue($field, $post);
- $attachments = $post->whereIn('ID', $ids)->get();
+ $ids = $this->fetchValue($field);
+ $attachments = $this->post->whereIn('ID', $ids)->get();
$metaDataValues = $this->fetchMultipleMetadataValues($attachments);
foreach ($attachments as $attachment) {
- $image = new Image();
+ $image = new Image($this->post);
$image->fillFields($attachment);
$image->fillMetadataFields($metaDataValues[$attachment->ID]);
$this->images[] = $image;
diff --git a/src/Field/Image.php b/src/Field/Image.php
index be19ae6..11fdfa4 100644
--- a/src/Field/Image.php
+++ b/src/Field/Image.php
@@ -55,13 +55,12 @@ class Image extends BasicField implements FieldInterface
/**
* @param string $field
- * @param Post $post
*/
- public function process($field, Post $post)
+ public function process($field)
{
- $attachmentId = $this->fetchValue($field, $post);
+ $attachmentId = $this->fetchValue($field);
- $attachment = $post->find(intval($attachmentId));
+ $attachment = $this->post->find(intval($attachmentId));
$this->fillFields($attachment);
$imageData = $this->fetchMetadataValue($attachment);
@@ -107,7 +106,7 @@ public function size($size)
*/
protected function fillThumbnailFields(array $data)
{
- $size = new static();
+ $size = new static($this->post);
$size->filename = $data['file'];
$size->width = $data['width'];
$size->height = $data['height'];
@@ -133,7 +132,7 @@ protected function fetchMetadataValue(Post $attachment)
/**
* @param Collection $attachments
*
- * @return Collection
+ * @return Collection|array
*/
protected function fetchMultipleMetadataValues(Collection $attachments)
{
diff --git a/src/Field/PostObject.php b/src/Field/PostObject.php
index 523e27f..2516e26 100644
--- a/src/Field/PostObject.php
+++ b/src/Field/PostObject.php
@@ -19,16 +19,15 @@ class PostObject extends BasicField implements FieldInterface
/**
* @param string $fieldName
- * @param Post $post
*/
- public function process($fieldName, Post $post)
+ public function process($fieldName)
{
- $postId = $this->fetchValue($fieldName, $post);
+ $postId = $this->fetchValue($fieldName);
if (is_array($postId)) {
- $this->object = $post->whereIn('ID', $postId)->get();
+ $this->object = $this->post->whereIn('ID', $postId)->get();
} else {
- $this->object = $post->find($postId);
+ $this->object = $this->post->find($postId);
}
}
diff --git a/src/Field/Repeater.php b/src/Field/Repeater.php
index fd1c0ad..ec47626 100644
--- a/src/Field/Repeater.php
+++ b/src/Field/Repeater.php
@@ -22,14 +22,12 @@ class Repeater extends BasicField implements FieldInterface
/**
* @param string $fieldName
- * @param Post $post
*/
- public function process($fieldName, Post $post)
+ public function process($fieldName)
{
$this->name = $fieldName;
- $this->post = $post;
- $builder = $this->fetchPostsMeta($fieldName, $post);
+ $builder = $this->fetchPostsMeta($fieldName, $this->post);
$fields = $this->fetchFields($fieldName, $builder);
$this->fields = new Collection($fields);
diff --git a/src/Field/Term.php b/src/Field/Term.php
index aa40923..531547f 100644
--- a/src/Field/Term.php
+++ b/src/Field/Term.php
@@ -23,19 +23,22 @@ class Term extends BasicField implements FieldInterface
*/
protected $term;
- public function __construct()
+ /**
+ * @param Post $post
+ */
+ public function __construct(Post $post)
{
- parent::__construct();
+ parent::__construct($post);
$this->term = new \Corcel\Term();
+ $this->term->setConnection($post->getConnectionName());
}
/**
* @param string $fieldName
- * @param Post $post
*/
- public function process($fieldName, Post $post)
+ public function process($fieldName)
{
- $value = $this->fetchValue($fieldName, $post);
+ $value = $this->fetchValue($fieldName);
if (is_array($value)) {
$this->items = $this->term->whereIn('term_id', $value)->get(); // ids
} else {
diff --git a/src/Field/Text.php b/src/Field/Text.php
index 3c9ce0d..2b3f50c 100644
--- a/src/Field/Text.php
+++ b/src/Field/Text.php
@@ -17,9 +17,12 @@ class Text extends BasicField implements FieldInterface
*/
protected $value;
- public function process($field, Post $post)
+ /**
+ * @param string $field
+ */
+ public function process($field)
{
- $this->value = $this->fetchValue($field, $post);
+ $this->value = $this->fetchValue($field);
}
/**
diff --git a/src/Field/User.php b/src/Field/User.php
index 5d33ee5..09aada2 100644
--- a/src/Field/User.php
+++ b/src/Field/User.php
@@ -22,19 +22,22 @@ class User extends BasicField implements FieldInterface
*/
protected $value;
- public function __construct()
+ /**
+ * @param Post $post
+ */
+ public function __construct(Post $post)
{
- parent::__construct();
+ parent::__construct($post);
$this->user = new \Corcel\User();
+ $this->user->setConnection($post->getConnectionName());
}
/**
* @param string $fieldName
- * @param Post $post
*/
- public function process($fieldName, Post $post)
+ public function process($fieldName)
{
- $userId = $this->fetchValue($fieldName, $post);
+ $userId = $this->fetchValue($fieldName);
$this->value = $this->user->find($userId);
}
diff --git a/src/FieldFactory.php b/src/FieldFactory.php
index b3081d0..b423ab6 100644
--- a/src/FieldFactory.php
+++ b/src/FieldFactory.php
@@ -29,8 +29,8 @@ private function __construct()
}
/**
- * @param string $name
- * @param Post $post
+ * @param string $name
+ * @param Post $post
* @param null|string $type
*
* @return FieldInterface|Collection|string
@@ -38,8 +38,8 @@ private function __construct()
public static function make($name, Post $post, $type = null)
{
if (null === $type) {
- $fakeText = new Text();
- $key = $fakeText->fetchFieldKey($name, $post);
+ $fakeText = new Text($post);
+ $key = $fakeText->fetchFieldKey($name);
if ($key === null) { // Field does not exist
return null;
@@ -63,48 +63,48 @@ public static function make($name, Post $post, $type = null)
case 'select':
case 'checkbox':
case 'radio':
- $field = new Text();
+ $field = new Text($post);
break;
case 'image':
case 'img':
- $field = new Image();
+ $field = new Image($post);
break;
case 'file':
- $field = new File();
+ $field = new File($post);
break;
case 'gallery':
- $field = new Gallery();
+ $field = new Gallery($post);
break;
case 'true_false':
case 'boolean':
- $field = new Boolean();
+ $field = new Boolean($post);
break;
case 'post_object':
case 'post':
case 'relationship':
- $field = new PostObject();
+ $field = new PostObject($post);
break;
case 'page_link':
- $field = new PageLink();
+ $field = new PageLink($post);
break;
case 'taxonomy':
case 'term':
- $field = new Term();
+ $field = new Term($post);
break;
case 'user':
- $field = new User();
+ $field = new User($post);
break;
case 'date_picker':
case 'date_time_picker':
case 'time_picker':
- $field = new DateTime();
+ $field = new DateTime($post);
break;
case 'repeater':
- $field = new Repeater();
+ $field = new Repeater($post);
break;
}
- $field->process($name, $post);
+ $field->process($name);
return $field;
}
diff --git a/src/FieldInterface.php b/src/FieldInterface.php
index 9d4d280..ae45dab 100644
--- a/src/FieldInterface.php
+++ b/src/FieldInterface.php
@@ -13,9 +13,8 @@ interface FieldInterface
{
/**
* @param string $fieldName
- * @param Post $post
*/
- public function process($fieldName, Post $post);
+ public function process($fieldName);
/**
* @return mixed
diff --git a/tests/BasicFieldsTest.php b/tests/BasicFieldsTest.php
index e62cae6..0b8f11b 100644
--- a/tests/BasicFieldsTest.php
+++ b/tests/BasicFieldsTest.php
@@ -25,48 +25,48 @@ protected function setUp()
public function testTextFieldValue()
{
- $text = new Text();
- $text->process('fake_text', $this->post);
+ $text = new Text($this->post);
+ $text->process('fake_text');
$this->assertEquals('Proin eget tortor risus', $text->get());
}
public function testTextareaFieldValue()
{
- $textarea = new Text();
- $textarea->process('fake_textarea', $this->post);
+ $textarea = new Text($this->post);
+ $textarea->process('fake_textarea');
$this->assertEquals('Praesent sapien massa, convallis a pellentesque nec, egestas non nisi.', $textarea->get());
}
public function testNumberFieldValue()
{
- $number = new Text();
- $number->process('fake_number', $this->post);
+ $number = new Text($this->post);
+ $number->process('fake_number');
$this->assertEquals('1984', $number->get());
}
public function testEmailFieldValue()
{
- $email = new Text();
- $email->process('fake_email', $this->post);
+ $email = new Text($this->post);
+ $email->process('fake_email');
$this->assertEquals('junior@corcel.org', $email->get());
}
public function testUrlFieldValue()
{
- $url = new Text();
- $url->process('fake_url', $this->post);
+ $url = new Text($this->post);
+ $url->process('fake_url');
$this->assertEquals('https://corcel.org', $url->get());
}
public function testPasswordFieldValue()
{
- $password = new Text();
- $password->process('fake_password', $this->post);
+ $password = new Text($this->post);
+ $password->process('fake_password');
$this->assertEquals('123change', $password->get());
}
diff --git a/tests/ChoicesFieldsTest.php b/tests/ChoicesFieldsTest.php
index a905bad..c383ad4 100644
--- a/tests/ChoicesFieldsTest.php
+++ b/tests/ChoicesFieldsTest.php
@@ -23,36 +23,36 @@ public function setUp()
public function testSelectField()
{
- $select = new Text();
- $select->process('fake_select', $this->post);
+ $select = new Text($this->post);
+ $select->process('fake_select');
$this->assertEquals('red', $select->get());
}
public function testSelectMultipleField()
{
- $select = new Text();
- $select->process('fake_select_multiple', $this->post);
+ $select = new Text($this->post);
+ $select->process('fake_select_multiple');
$this->assertEquals(['yellow', 'green'], $select->get());
}
public function testCheckboxField()
{
- $check = new Text();
- $check->process('fake_checkbox', $this->post);
+ $check = new Text($this->post);
+ $check->process('fake_checkbox');
$this->assertEquals(['blue', 'yellow'], $check->get());
}
public function testRadioField()
{
- $radio = new Text();
- $radio->process('fake_radio_button', $this->post);
+ $radio = new Text($this->post);
+ $radio->process('fake_radio_button');
$this->assertEquals('green', $radio->get());
}
public function testTrueFalseField()
{
- $boolean = new Boolean();
- $boolean->process('fake_true_false', $this->post);
+ $boolean = new Boolean($this->post);
+ $boolean->process('fake_true_false');
$this->assertTrue($boolean->get());
}
}
diff --git a/tests/ContentFieldsTest.php b/tests/ContentFieldsTest.php
index ffd601c..b47e38b 100644
--- a/tests/ContentFieldsTest.php
+++ b/tests/ContentFieldsTest.php
@@ -28,8 +28,8 @@ protected function setUp()
public function testEditorFieldValue()
{
- $field = new Text();
- $field->process('fake_editor', $this->post);
+ $field = new Text($this->post);
+ $field->process('fake_editor');
$this->assertEquals(
'Nulla porttitor accumsan tincidunt. Sed porttitor lectus nibh.',
@@ -39,16 +39,16 @@ public function testEditorFieldValue()
public function testOembedFieldValue()
{
- $field = new Text();
- $field->process('fake_oembed', $this->post);
+ $field = new Text($this->post);
+ $field->process('fake_oembed');
$this->assertEquals('https://www.youtube.com/watch?v=LiyQ8bvLzIE', $field->get());
}
public function testImageFieldValue()
{
- $image = new Image();
- $image->process('fake_image', $this->post);
+ $image = new Image($this->post);
+ $image->process('fake_image');
$this->assertEquals('1920', $image->width);
$this->assertEquals('1080', $image->height);
@@ -60,8 +60,8 @@ public function testImageFieldValue()
public function testFileFieldValue()
{
- $file = new File();
- $file->process('fake_file', $this->post);
+ $file = new File($this->post);
+ $file->process('fake_file');
$this->assertEquals('Description here', $file->description);
$this->assertEquals('Title here', $file->title);
@@ -72,8 +72,8 @@ public function testFileFieldValue()
public function testGalleryFieldValue()
{
- $gallery = new Gallery();
- $gallery->process('fake_gallery', $this->post);
+ $gallery = new Gallery($this->post);
+ $gallery->process('fake_gallery');
$this->assertEquals(7, $gallery->get()->count());
diff --git a/tests/CorcelIntegrationTest.php b/tests/CorcelIntegrationTest.php
index a674122..bb6f5f0 100644
--- a/tests/CorcelIntegrationTest.php
+++ b/tests/CorcelIntegrationTest.php
@@ -18,4 +18,11 @@ public function testUsageOfHelperFunctions()
$post = Post::find(56);
$this->assertEquals('admin', $post->acf->user('fake_user')->nickname);
}
+
+ public function testFunctionHelperWithSnakeCaseFieldType()
+ {
+ $post = Post::find(65);
+ $this->assertEquals('10/13/2016', $post->acf->fake_date_picker->format('m/d/Y'));
+ $this->assertEquals('10/13/2016', $post->acf->datePicker('fake_date_picker')->format('m/d/Y'));
+ }
}
diff --git a/tests/JqueryFieldsTest.php b/tests/JqueryFieldsTest.php
index 61df761..d63b8f7 100644
--- a/tests/JqueryFieldsTest.php
+++ b/tests/JqueryFieldsTest.php
@@ -23,29 +23,29 @@ public function testGoogleMapField()
public function testDatePickerField()
{
- $date = new DateTime();
- $date->process('fake_date_picker', $this->post);
+ $date = new DateTime($this->post);
+ $date->process('fake_date_picker');
$this->assertEquals('10/13/2016', $date->get()->format('m/d/Y'));
}
public function testDateTimePickerField()
{
- $dateTime = new DateTime();
- $dateTime->process('fake_date_time_picker', $this->post);
+ $dateTime = new DateTime($this->post);
+ $dateTime->process('fake_date_time_picker');
$this->assertEquals('05:06:08/19-10:2016', $dateTime->get()->format('s:i:H/d-m:Y')); // 2016-10-19 08:06:05
}
public function testTimePickerField()
{
- $time = new DateTime();
- $time->process('fake_time_picker', $this->post);
+ $time = new DateTime($this->post);
+ $time->process('fake_time_picker');
$this->assertEquals('00/17/30', $time->get()->format('s/H/i')); // 17:30:00
}
public function testColorPickerField()
{
- $color = new Text();
- $color->process('fake_color_picker', $this->post);
+ $color = new Text($this->post);
+ $color->process('fake_color_picker');
$this->assertEquals('#7263a8', $color->get());
}
}
diff --git a/tests/LayoutFieldsTest.php b/tests/LayoutFieldsTest.php
index 3283a5b..d5a4d9e 100644
--- a/tests/LayoutFieldsTest.php
+++ b/tests/LayoutFieldsTest.php
@@ -12,8 +12,8 @@ class LayoutFieldsTest extends PHPUnit_Framework_TestCase
public function testRepeaterField()
{
$page = Post::find(73);
- $repeater = new Repeater();
- $repeater->process('fake_repeater', $page);
+ $repeater = new Repeater($page);
+ $repeater->process('fake_repeater');
$fields = $repeater->get()->toArray();
$this->assertEquals('First text', $fields[0]['repeater_text']);
@@ -25,8 +25,8 @@ public function testRepeaterField()
public function testComplexRepeaterField()
{
$page = Post::find(73);
- $repeater = new Repeater();
- $repeater->process('fake_repeater_2', $page);
+ $repeater = new Repeater($page);
+ $repeater->process('fake_repeater_2');
$fields = $repeater->get()->toArray();
$this->assertEquals('admin', $fields[0]['fake_user']->nickname);
diff --git a/tests/RelationalFieldsTest.php b/tests/RelationalFieldsTest.php
index baef2b1..9c9b66c 100644
--- a/tests/RelationalFieldsTest.php
+++ b/tests/RelationalFieldsTest.php
@@ -25,41 +25,41 @@ public function setUp()
public function testPostObjectField()
{
- $object = new PostObject();
- $object->process('fake_post_object', $this->post);
+ $object = new PostObject($this->post);
+ $object->process('fake_post_object');
$this->assertEquals('ACF Basic Fields', $object->get()->post_title);
}
public function testPageLinkField()
{
- $page = new PageLink();
- $page->process('fake_page_link', $this->post);
+ $page = new PageLink($this->post);
+ $page->process('fake_page_link');
$this->assertEquals('http://wordpress.corcel.dev/acf-content-fields/', $page->get());
}
public function testRelationshipField()
{
- $relation = new PostObject();
- $relation->process('fake_relationship', $this->post);
+ $relation = new PostObject($this->post);
+ $relation->process('fake_relationship');
$posts = $relation->get();
$this->assertEquals([44, 56], $posts->pluck('ID')->toArray());
}
public function testTaxonomyField()
{
- $relation = new Term();
+ $relation = new Term($this->post);
- $relation->process('fake_taxonomy', $this->post); // multiple (Collection)
+ $relation->process('fake_taxonomy'); // multiple (Collection)
$this->assertEquals('uncategorized', $relation->get()->first()->slug);
- $relation->process('fake_taxonomy_single', $this->post); // single (Corcel\Term)
+ $relation->process('fake_taxonomy_single'); // single (Corcel\Term)
$this->assertEquals('uncategorized', $relation->get()->slug);
}
public function testUserField()
{
- $user = new User();
- $user->process('fake_user', $this->post);
+ $user = new User($this->post);
+ $user->process('fake_user');
$this->assertEquals('admin', $user->get()->user_login);
$this->assertEquals('admin', $user->get()->nickname);
}