-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3425 abstract out and test the client assets registration
- Loading branch information
Showing
2,066 changed files
with
170,026 additions
and
1,763 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,107 @@ | ||
<?php | ||
|
||
|
||
namespace calderawp\calderaforms\cf2\Asset; | ||
|
||
|
||
use calderawp\calderaforms\cf2\CalderaFormsV2Contract; | ||
|
||
class Hooks | ||
{ | ||
|
||
/** | ||
* @var Register[] | ||
*/ | ||
protected $handlers = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $handles; | ||
/** | ||
* @var array | ||
*/ | ||
protected $manifest; | ||
|
||
/** | ||
* @var CalderaFormsV2Contract | ||
*/ | ||
protected $container; | ||
|
||
public function __construct(array $handles, CalderaFormsV2Contract $container, array $manifest = []) | ||
{ | ||
$this->handles = $handles; | ||
$this->manifest = $manifest; | ||
$this->container = $container; | ||
|
||
} | ||
|
||
public function subscribe() | ||
{ | ||
|
||
$this->maybeUseManifest(); | ||
add_action('wp_register_scripts', [$this, 'registerAssets']); | ||
add_action('admin_enqueue_scripts', [$this, 'enqueueAdminAssets']); | ||
} | ||
|
||
/** | ||
* If a webpack asset-manifest.json was used, substitute URLs from that array | ||
*/ | ||
protected function maybeUseManifest() | ||
{ | ||
if (!empty($this->manifest) && !empty($this->handles)) { | ||
foreach ($this->handles as $handle) { | ||
$assetFilePath = isset($this->manifest["{$handle}.json"]) ? $this->manifest["{$handle}.json"] : null; | ||
if (!is_null($assetFilePath)) { | ||
$this->getHandler($handle)->setAssetsFilePath($assetFilePath); | ||
} | ||
$scriptsUrl = isset($this->manifest["{$handle}.js"]) ? $this->manifest["{$handle}.js"] : null; | ||
if (!is_null($scriptsUrl)) { | ||
$this->getHandler($handle)->setScriptUrl($scriptsUrl); | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
* @param $handle | ||
* @return Register|null | ||
*/ | ||
public function getHandler($handle) | ||
{ | ||
if (in_array($handle, $this->handles)) { | ||
if (!array_key_exists($handle, $this->handlers)) { | ||
$this->handlers[$handle] = new Register( | ||
$handle, | ||
$this->container->getCoreUrl(), | ||
$this->container->getCoreDir(), | ||
[] | ||
); | ||
} | ||
return $this->handlers[$handle]; | ||
} | ||
} | ||
|
||
/** | ||
* Register all assets | ||
*/ | ||
public function registerAssets() | ||
{ | ||
$this->getHandler('form-builder')->register(); | ||
} | ||
|
||
public function enqueueAdminAssets($hook) | ||
{ | ||
if ('toplevel_page_caldera-forms' !== $hook) { | ||
return; | ||
} | ||
|
||
if (\Caldera_Forms_Admin::is_edit()) { | ||
$this->getHandler('form-builder')->enqueue(); | ||
} | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace calderawp\calderaforms\Tests\Unit\Asset; | ||
|
||
use calderawp\calderaforms\cf2\Asset\Hooks; | ||
use calderawp\calderaforms\cf2\Asset\Register; | ||
use calderawp\calderaforms\Tests\Unit\TestCase; | ||
|
||
class HooksTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @var \calderawp\calderaforms\cf2\CalderaFormsV2 | ||
*/ | ||
protected $container; | ||
|
||
/** @inheritDoc */ | ||
public function setUp() | ||
{ | ||
$this->container = $this->getContainer(); | ||
$this->container->setCoreDir(dirname(__DIR__, 3)); | ||
parent::setUp(); | ||
} | ||
|
||
public function testRegisterAssets() | ||
{ | ||
$hooks = new Hooks(['form-builder'], $this->container); | ||
\Brain\Monkey\Functions\expect('wp_register_script')->once(); | ||
\Brain\Monkey\Functions\expect('wp_localize_script')->once(); | ||
$hooks->registerAssets(); | ||
|
||
} | ||
|
||
public function testSubscribe() | ||
{ | ||
$hooks = new Hooks(['form-builder'], $this->container); | ||
$hooks->subscribe(); | ||
$this->assertTrue(has_action('admin_enqueue_scripts'), [$hooks, 'enqueueAdminAssets']); | ||
$this->assertTrue(has_action('wp_register_scripts'), [$hooks, 'registerAssets']); | ||
} | ||
|
||
|
||
public function testGetHandler() | ||
{ | ||
$hooks = new Hooks(['form-builder'], $this->container); | ||
$this->assertInstanceOf(Register::class, $hooks->getHandler('form-builder')); | ||
$this->assertStringEndsWith( | ||
'/clients/form-builder/build/index.min.asset.json', | ||
$hooks->getHandler('form-builder')->getAssetFilePath() | ||
); | ||
} | ||
} |
Oops, something went wrong.