Skip to content

Commit

Permalink
some code style cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftner committed Nov 3, 2016
1 parent 18f3e39 commit 694da8a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
60 changes: 28 additions & 32 deletions src/Models/UnderscoreTemplateEnqueuer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class UnderscoreTemplateEnqueuer
* @param string $url
* @param string $path
*/
public function __construct($url, $path)
public function __construct( $url, $path )
{
$this->url = trailingslashit($url);
$this->path = trailingslashit($path);
$this->url = trailingslashit( $url );
$this->path = trailingslashit( $path );
}

/**
Expand All @@ -58,27 +58,27 @@ public function basePath()
* @param string $template_name
* @return bool
*/
public function enqueue($template_name)
public function enqueue( $template_name )
{
$path = $this->templatePath($template_name);
if (!$path || !is_readable($path)) {
return false;
$path = $this->templatePath( $template_name );
if ( !$path || !is_readable( $path ) ) {
return false;
}

$filter = function ($script_tag_markup = '', $handle = '') use ($template_name) {
$filter = function ( $script_tag_markup = '', $handle = '' ) use ( $template_name ) {

if( $handle !== "tmpl-{$template_name}" ){
return $script_tag_markup;
}

return $this->makeTemplateInline($script_tag_markup, $handle, $template_name);
return $this->makeTemplateInline( $script_tag_markup, $handle, $template_name );
};

add_filter('script_loader_tag', $filter, 20, 2);
add_filter( 'script_loader_tag', $filter, 20, 2 );

wp_enqueue_script(
"tmpl-{$template_name}",
$this->templateUrl($template_name),
$this->templateUrl( $template_name ),
[],
@filemtime($path) ?: null,
true
Expand All @@ -92,7 +92,7 @@ public function enqueue($template_name)
* @param $template
* @return string
*/
private function templatePath($template)
private function templatePath( $template )
{
return $this->path . 'assets/templates/' . $template . '.tmpl';
}
Expand All @@ -101,18 +101,18 @@ private function templatePath($template)
* @param $template
* @return string
*/
private function templateUrl($template){
private function templateUrl( $template ){
return $this->url. 'assets/templates/' . $template . '.tmpl';
}

/**
* @param $template_path
* @return string
*/
private function loadTemplateContent($template_path)
private function loadTemplateContent( $template_path )
{
if (is_readable($template_path)) {
return file_get_contents($template_path);
if ( is_readable( $template_path ) ) {
return file_get_contents( $template_path );
}

return '';
Expand All @@ -124,36 +124,32 @@ private function loadTemplateContent($template_path)
* @param string $template_name
* @return string
*/
private function makeTemplateInline(
$script_tag_markup = '',
$handle = '',
$template_name = ''
) {
private function makeTemplateInline( $script_tag_markup = '', $handle = '', $template_name = '' ) {
$dom = new \DOMDocument;
$dom->loadHTML($script_tag_markup);
$tags = $dom->getElementsByTagName('script');
$dom->loadHTML( $script_tag_markup );
$tags = $dom->getElementsByTagName( 'script' );
$tag = $tags->length ? $tags->item(0) : null;

if (!$tag || !$template_name) {
if ( !$tag || !$template_name ) {
return $script_tag_markup;
}

$template_path = $this->templatePath($template_name);
$template_content = $this->loadTemplateContent($template_path);
$template_path = $this->templatePath( $template_name );
$template_content = $this->loadTemplateContent( $template_path );

if (!$template_content) {
if ( !$template_content ) {
return $script_tag_markup;
}

/** @var \DOMElement $tag */
$tag->setAttribute('type', 'text/template');
$tag->setAttribute('id', $handle);
$tag->appendChild($dom->createTextNode($template_content));
$tag->setAttribute( 'type', 'text/template' );
$tag->setAttribute( 'id', $handle );
$tag->appendChild( $dom->createTextNode( $template_content ) );
# new node: 25% faster than
# @link http://chat.stackexchange.com/transcript/message/19567599#19567599
//$tag->nodeValue = esc_html( file_get_contents( $src ) );
$tag->removeAttribute('src');
$tag->removeAttribute( 'src' );

return $dom->saveHTML($tag);
return $dom->saveHTML( $tag );
}
}
2 changes: 1 addition & 1 deletion src/Services/AvatarScriptsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function addTemplates()
'caption',
];

array_walk($templates, [$this->templateEnqueuer, 'enqueue']);
array_walk( $templates, [ $this->templateEnqueuer, 'enqueue' ] );
}


Expand Down

0 comments on commit 694da8a

Please sign in to comment.