-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic Generate Blocks block type support
- Loading branch information
1 parent
a30e0e4
commit 9d6752e
Showing
7 changed files
with
221 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license MIT | ||
*/ | ||
|
||
namespace craft\wpimport\blocktransformers; | ||
|
||
use craft\elements\Entry; | ||
use craft\wpimport\BaseBlockTransformer; | ||
|
||
/** | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
*/ | ||
class GBElement extends BaseBlockTransformer | ||
{ | ||
public static function blockName(): string | ||
{ | ||
return 'generateblocks/element'; | ||
} | ||
|
||
public function render(array $data, Entry $entry): string | ||
{ | ||
if (empty($data['innerBlocks'])) { | ||
return ''; | ||
} | ||
|
||
return $this->command->renderBlocks($data['innerBlocks'], $entry); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license MIT | ||
*/ | ||
|
||
namespace craft\wpimport\blocktransformers; | ||
|
||
use craft\elements\Entry; | ||
use craft\helpers\Html as HtmlHelper; | ||
use craft\wpimport\BaseBlockTransformer; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
/** | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
*/ | ||
class GBHeadline extends BaseBlockTransformer | ||
{ | ||
public static function blockName(): string | ||
{ | ||
return 'generateblocks/headline'; | ||
} | ||
|
||
public function render(array $data, Entry $entry): string | ||
{ | ||
$node = (new Crawler($data['innerHTML']))->filter('h1,h2,h3,h4,h5,h6,p,div'); | ||
if (!$node->count()) { | ||
return ''; | ||
} | ||
|
||
$tag = $node->nodeName(); | ||
if ($tag === 'div') { | ||
$tag = 'p'; | ||
} | ||
|
||
return HtmlHelper::tag($tag, $node->text()); | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license MIT | ||
*/ | ||
|
||
namespace craft\wpimport\blocktransformers; | ||
|
||
use craft\elements\Entry; | ||
use craft\wpimport\BaseBlockTransformer; | ||
use craft\wpimport\importers\Media; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Throwable; | ||
|
||
/** | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
*/ | ||
class GBImage extends BaseBlockTransformer | ||
{ | ||
public static function blockName(): string | ||
{ | ||
return 'generateblocks/image'; | ||
} | ||
|
||
public function render(array $data, Entry $entry): string | ||
{ | ||
$id = $data['attrs']['mediaId'] ?? null; | ||
if (!$id) { | ||
return ''; | ||
} | ||
|
||
$img = (new Crawler($data['innerHTML']))->filter('img'); | ||
$src = $img->attr('src'); | ||
|
||
try { | ||
if ($src) { | ||
$assetId = $this->command->import(Media::SLUG, [ | ||
'id' => $id, | ||
'source_url' => $src, | ||
'title' => $img->attr('title'), | ||
'alt_text' => $img->attr('alt'), | ||
]); | ||
} else { | ||
$assetId = $this->command->import(Media::SLUG, $id); | ||
} | ||
} catch (Throwable) { | ||
return ''; | ||
} | ||
|
||
return $this->command->createNestedMediaEntry($entry, $assetId); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license MIT | ||
*/ | ||
|
||
namespace craft\wpimport\blocktransformers; | ||
|
||
use craft\elements\Entry; | ||
use craft\wpimport\BaseBlockTransformer; | ||
use craft\wpimport\importers\Media; | ||
use Throwable; | ||
|
||
/** | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
*/ | ||
class GBMedia extends BaseBlockTransformer | ||
{ | ||
public static function blockName(): string | ||
{ | ||
return 'generateblocks/media'; | ||
} | ||
|
||
public function render(array $data, Entry $entry): string | ||
{ | ||
$id = $data['attrs']['mediaId'] ?? null; | ||
if (!$id) { | ||
return ''; | ||
} | ||
|
||
try { | ||
if (!empty($data['attrs']['htmlAttributes']['src'])) { | ||
$assetId = $this->command->import(Media::SLUG, [ | ||
'id' => $id, | ||
'source_url' => $data['attrs']['htmlAttributes']['src'], | ||
'title' => ['raw' => $data['attrs']['htmlAttributes']['title'] ?? null], | ||
'alt_text' => $data['attrs']['htmlAttributes']['alt'] ?? null, | ||
]); | ||
} else { | ||
$assetId = $this->command->import(Media::SLUG, $id); | ||
} | ||
} catch (Throwable) { | ||
return ''; | ||
} | ||
|
||
return $this->command->createNestedMediaEntry($entry, $assetId); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license MIT | ||
*/ | ||
|
||
namespace craft\wpimport\blocktransformers; | ||
|
||
use craft\elements\Entry; | ||
use craft\wpimport\BaseBlockTransformer; | ||
|
||
/** | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
*/ | ||
class GBText extends BaseBlockTransformer | ||
{ | ||
public static function blockName(): string | ||
{ | ||
return 'generateblocks/text'; | ||
} | ||
|
||
public function render(array $data, Entry $entry): string | ||
{ | ||
return $data['innerHTML'] ?? ''; | ||
} | ||
} |
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