-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
250 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Corcel\Acf\Field; | ||
|
||
use Corcel\Acf\FieldFactory; | ||
use Corcel\Acf\FieldInterface; | ||
use Corcel\Post; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* Class Flexible Content. | ||
* | ||
* @author Marco Boom <[email protected]> | ||
*/ | ||
class FlexibleContent extends BasicField implements FieldInterface | ||
{ | ||
/** | ||
* @var Collection | ||
*/ | ||
protected $fields; | ||
|
||
/** | ||
* @param string $fieldName | ||
*/ | ||
public function process($fieldName) | ||
{ | ||
$this->name = $fieldName; | ||
|
||
$builder = $this->fetchPostsMeta($fieldName, $this->post); | ||
$fields = $this->fetchFields($fieldName, $builder); | ||
|
||
$this->fields = new Collection($fields); | ||
} | ||
|
||
/** | ||
* @return Collection | ||
*/ | ||
public function get() | ||
{ | ||
return $this->fields; | ||
} | ||
|
||
/** | ||
* @param string $metaKey | ||
* @param string $fieldName | ||
* | ||
* @return int | ||
*/ | ||
protected function retrieveIdFromFieldName($metaKey, $fieldName) | ||
{ | ||
return (int) str_replace("{$fieldName}_", '', $metaKey); | ||
} | ||
|
||
/** | ||
* @param string $metaKey | ||
* @param string $fieldName | ||
* @param int $id | ||
* | ||
* @return string | ||
*/ | ||
protected function retrieveFieldName($metaKey, $fieldName, $id) | ||
{ | ||
$pattern = "{$fieldName}_{$id}_"; | ||
|
||
return str_replace($pattern, '', $metaKey); | ||
} | ||
|
||
/** | ||
* @param $fieldName | ||
* @param Post $post | ||
* | ||
* @return mixed | ||
*/ | ||
protected function fetchPostsMeta($fieldName, Post $post) | ||
{ | ||
$builder = $this->postMeta->where('post_id', $post->ID); | ||
$builder->where(function ($query) use ($fieldName) { | ||
$query->orWhere('meta_key', 'like', "{$fieldName}_%"); | ||
}); | ||
|
||
return $builder; | ||
} | ||
|
||
/** | ||
* @param $fieldName | ||
* @param $builder | ||
* | ||
* @return mixed | ||
*/ | ||
protected function fetchFields($fieldName, Builder $builder) | ||
{ | ||
$fields = []; | ||
$blocks = $this->fetchValue($fieldName, $this->post); | ||
|
||
foreach ($builder->get() as $meta) { | ||
$id = $this->retrieveIdFromFieldName($meta->meta_key, $fieldName); | ||
|
||
$name = $this->retrieveFieldName($meta->meta_key, $fieldName, $id); | ||
$field = FieldFactory::make($meta->meta_key, $this->post->find($meta->post_id)); | ||
|
||
if (!array_key_exists($id, $blocks)) { | ||
continue; | ||
} | ||
|
||
if (empty($fields[$id])) { | ||
$fields[$id] = new \stdClass; | ||
$fields[$id]->type = $blocks[$id]; | ||
$fields[$id]->fields = new \stdClass; | ||
} | ||
|
||
$fields[$id]->fields->$name = $field->get(); | ||
} | ||
|
||
return $fields; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
use Corcel\Acf\Field\Text; | ||
use Corcel\Acf\Field\BasicField; | ||
use Corcel\Post; | ||
|
||
/** | ||
* Class BasicFieldTest. | ||
* | ||
* @author Junior Grossi <[email protected]> | ||
*/ | ||
class EmptyBasicFieldsTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Post | ||
*/ | ||
protected $post; | ||
|
||
/** | ||
* Setup a base $this->post object to represent the page with the basic fields. | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->post = Post::find(91); // it' a page with empty custom fields | ||
} | ||
|
||
public function testTextFieldValue() | ||
{ | ||
$text = new Text($this->post); | ||
$text->process('fake_text'); | ||
|
||
$this->assertSame('', $text->get()); | ||
} | ||
|
||
public function testTextareaFieldValue() | ||
{ | ||
$textarea = new Text($this->post); | ||
$textarea->process('fake_textarea'); | ||
|
||
$this->assertSame('', $textarea->get()); | ||
} | ||
|
||
public function testNumberFieldValue() | ||
{ | ||
$number = new Text($this->post); | ||
$number->process('fake_number'); | ||
|
||
$this->assertSame('', $number->get()); | ||
} | ||
|
||
public function testEmailFieldValue() | ||
{ | ||
$email = new Text($this->post); | ||
$email->process('fake_email'); | ||
|
||
$this->assertSame('', $email->get()); | ||
} | ||
|
||
public function testUrlFieldValue() | ||
{ | ||
$url = new Text($this->post); | ||
$url->process('fake_url'); | ||
|
||
$this->assertSame('', $url->get()); | ||
} | ||
|
||
public function testPasswordFieldValue() | ||
{ | ||
$password = new Text($this->post); | ||
$password->process('fake_password'); | ||
|
||
$this->assertSame('', $password->get()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.