Skip to content

Commit

Permalink
Merge pull request #1 from mindkomm/feat-timber-2
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr authored Oct 17, 2023
2 parents e4ca5b5 + 8cc0208 commit 323291b
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 98 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
composer.lock
vendor
7 changes: 4 additions & 3 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

declare(strict_types=1);

require_once 'lib/functions.php';

/**
* Require functionality in filter only if WordPress is loaded.
*/
if ( function_exists( 'add_filter' ) ) {
require_once 'lib/twig.php';
if (function_exists('add_filter')) {
require_once 'lib/twig.php';
}

54 changes: 42 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
{
"name": "mindkomm/theme-lib-links",
"type": "library",
"description": "Collection of link helper functions for WordPress themes",
"license": "MIT",
"require": {
"php": ">=7.0.0"
},
"autoload": {
"files": [
"autoload.php"
]
},
"type": "library",
"authors": [
{
"name": "Lukas Gaechter",
"email": "[email protected]",
"homepage": "https://www.mind.ch"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=7.0.0"
},
"require-dev": {
"mindkomm/qa": "^0.4.1"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"files": [
"autoload.php"
]
},
"config": {
"allow-plugins": {
"phpro/grumphp-shim": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true,
"ergebnis/composer-normalize": true
}
},
"extra": {
"grumphp": {
"config-default-path": "vendor/mindkomm/qa/config/grumphp.yml"
}
},
"scripts": {
"analyse": [
"phpstan analyse --memory-limit=1G"
],
"cs": "phpcs --colors",
"cs:fix": "phpcbf --filter=GitModified",
"grump": [
"grumphp git:pre-commit"
],
"lint-composer": "@composer normalize --dry-run",
"lint-composer:fix": "@composer normalize",
"qa": [
"@lint-composer",
"@cs",
"@analyse"
]
}
}
}
86 changes: 46 additions & 40 deletions lib/functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Converts an URL to an anchor link.
*
Expand All @@ -15,8 +17,9 @@
* @param string $url URL to turn into an anchor link.
* @return string The updated URL.
*/
function make_anchor_link( $url ) {
return untrailingslashit( preg_replace( '/[^\/]+(?=\/$|$)/i', '#$0', $url ) );
function make_anchor_link($url)
{
return untrailingslashit(preg_replace('/[^\/]+(?=\/$|$)/i', '#$0', $url));
}

/**
Expand All @@ -39,15 +42,16 @@ function make_anchor_link( $url ) {
* @param string $url URL to be parsed.
* @return bool Whether the URL is external.
*/
function is_external_url( $url ) {
$link_url = wp_parse_url( $url );
$home_url = wp_parse_url( home_url() );
function is_external_url($url)
{
$link_url = wp_parse_url($url);
$home_url = wp_parse_url(home_url());

if ( $link_url['host'] === $home_url['host'] ) {
return false;
}
if ($link_url['host'] === $home_url['host']) {
return false;
}

return true;
return true;
}

/**
Expand All @@ -71,31 +75,32 @@ function is_external_url( $url ) {
*
* @return string The domain part of the URL.
*/
function url_to_domain( $url, $strip_www = false, $limit = false ) {
$host = wp_parse_url( $url, PHP_URL_HOST );
function url_to_domain($url, $strip_www = false, $limit = false)
{
$host = wp_parse_url($url, PHP_URL_HOST);

/**
* If the URL can't be parsed, use the original URL.
* Change to "return false" if you don't want that.
*/
if ( ! $host ) {
$host = $url;
}
/**
* If the URL can't be parsed, use the original URL.
* Change to "return false" if you don't want that.
*/
if (! $host) {
$host = $url;
}

/**
* The "www." prefix isn't really needed if you're just using
* this to display the domain to the user.
*/
if ( 'www.' === substr( $host, 0, 4 ) && $strip_www ) {
$host = substr( $host, 4 );
}
/**
* The "www." prefix isn't really needed if you're just using
* this to display the domain to the user.
*/
if ('www.' === substr($host, 0, 4) && $strip_www) {
$host = substr($host, 4);
}

// You might also want to limit the length if screen space is limited
if ( $limit && strlen( $host ) > $limit ) {
$host = substr( $host, 0, $limit - 3 ) . '&hellip;';
}
// You might also want to limit the length if screen space is limited
if ($limit && strlen($host) > $limit) {
$host = substr($host, 0, $limit - 3) . '&hellip;';
}

return $host;
return $host;
}

/**
Expand All @@ -120,15 +125,16 @@ function url_to_domain( $url, $strip_www = false, $limit = false ) {
* @return string|bool An href attribute with target and rel attributes, when necessary. Returns `false` if input
* is empty.
*/
function get_link_attributes( $url ) {
// Bail out if parameter URL is empty
if ( empty( $url ) ) {
return false;
}
function get_link_attributes($url)
{
// Bail out if parameter URL is empty
if (empty($url)) {
return false;
}

if ( is_external_url( $url ) ) {
return 'href="' . $url . '" target="_blank" rel="noopener noreferrer"';
} else {
return 'href="' . $url . '"';
}
if (is_external_url($url)) {
return 'href="' . $url . '" target="_blank" rel="noopener noreferrer"';
} else {
return 'href="' . $url . '"';
}
}
88 changes: 46 additions & 42 deletions lib/twig.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
<?php

use Timber\Twig_Function as Timber_Twig_Function;
declare(strict_types=1);

/**
* Customize Twig
*
* @param Twig_Environment $twig
* @return $twig
* Add Twig functions.
*/
add_filter( 'timber/twig', function( Twig_Environment $twig ) {
/**
* Checks if a URL is external or internal.
*
* Usage:
* {% if is_external_url(partner.url) %}
* {# Do something special #}
* {% endif %}
*
* @since 1.0.0
*
* @see is_external_url()
*/
$twig->addFunction( new Timber_Twig_Function( 'is_external_url', 'is_external_url' ) );

/**
* Get href attribute for an <a> tag with proper target and rel attributes.
*
* Usage:
* <a {{ get_link_attributes( post.link ) }}>Your link text</a>
*
* @since 1.0.0
*
* @see get_link_attributes()
*/
$twig->addFunction( new Timber_Twig_Function( 'get_link_attributes', 'get_link_attributes' ) );
add_filter('timber/twig/functions', static function (array $functions): array {
/**
* Checks if a URL is external or internal.
*
* Usage:
* {% if is_external_url(partner.url) %}
* {# Do something special #}
* {% endif %}
*
* @since 1.0.0
*
* @see is_external_url()
*/
$functions['is_external_url'] = [
'callable' => 'is_external_url',
];

/**
* Convert a URL to just the domain name together with the TLD.
*
* Usage:
* <a href={{ url }}>{{ url_to_domain(url) }}</a>
*
* @since 1.0.0
*
* @see url_to_domain()
*/
$twig->addFunction( new Timber_Twig_Function( 'url_to_domain', 'url_to_domain' ) );
/**
* Get href attribute for an <a> tag with proper target and rel attributes.
*
* Usage:
* <a {{ get_link_attributes( post.link ) }}>Your link text</a>
*
* @since 1.0.0
*
* @see get_link_attributes()
*/
$functions['get_link_attributes'] = [
'callable' => 'get_link_attributes',
];

return $twig;
/**
* Convert a URL to just the domain name together with the TLD.
*
* Usage:
* <a href={{ url }}>{{ url_to_domain(url) }}</a>
*
* @since 1.0.0
*
* @see url_to_domain()
*/
$functions['url_to_domain'] = [
'callable' => 'url_to_domain',
];

return $functions;
});
5 changes: 5 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<ruleset>
<rule ref="MIND"></rule>
<file>.</file>
</ruleset>
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- vendor/mindkomm/qa/phpstan.neon.dist
parameters:
level: 5
paths:
- lib
- autoload.php

0 comments on commit 323291b

Please sign in to comment.