Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Feb 13, 2018
0 parents commit 4628ded
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 MIND Kommunikation GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
146 changes: 146 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Links

Collection of link helper functions for WordPress themes.

## Installation

You can install the package via Composer:

```bash
composer require mindkomm/theme-lib-links
```

## Functions

| Name | Summary | Type | Returns/Description |
| --- | --- | --- | --- |
| [get_link_attributes](#get_link_attributes) | Gets href attribute for a link tag with proper target and rel attributes. | `string` or `bool` | An href attribute with target and rel attributes, when necessary. Returns `false` if input is empty. |
| [is_external_url](#is_external_url) | Checks if a URL is external or internal. | `bool` | Whether the URL is external. |
| [make_anchor_link](#make_anchor_link) | Converts an URL to an anchor link. | `string` | The updated URL. |
| [url_to_domain](#url_to_domain) | Converts an URL to just the domain name together with the TLD. | `string` | The domain part of the URL. |

### make\_anchor\_link

<p class="summary">Converts an URL to an anchor link.</p>

Prefix the last segment of a URL with a # to use it as an anchor link.

`make_anchor_link( string $url )`

**Returns:** `string` The updated URL.

| Name | Type | Description |
| --- | --- | --- |
| $url | `string` | URL to turn into an anchor link. |

**PHP**

```php
// Output: https://www.mind.ch/onepager#subsection
echo make_anchor_link( 'https://www.mind.ch/onepager/subsection' );
```

---

### is\_external\_url

<p class="summary">Checks if a URL is external or internal.</p>

`is_external_url( string $url )`

**Returns:** `bool` Whether the URL is external.

| Name | Type | Description |
| --- | --- | --- |
| $url | `string` | URL to be parsed. |

**PHP**

```php
if ( is_external_url( 'https://example.org' ) ) {
// Do something
}
```

**Twig**

```twig
{% if is_external_url('https://www.example.org') %}
{# Do something #}
{% endif %}
```

---

### url\_to\_domain

<p class="summary">Converts an URL to just the domain name together with the TLD.</p>

`url_to_domain( string $url, bool $strip_www = false, int|bool $limit = false )`

**Returns:** `string` The domain part of the URL.

| Name | Type | Description |
| --- | --- | --- |
| $url | `string` | The URL to convert. |
| $strip_www | `bool` | Optional. Whether to strip the "www" part of the domain. Default false. |
| $limit | `int&#124;bool` | Optional. Limit to a certain amount of characters. Default false. |

**PHP**

```php
<?php $domain = 'https://www.mind.ch/post/blablabla?param=wow'; ?>
<a href="<?php echo $url; ?>"><?php echo url_to_domain( $domain ); ?></a>
```

**Twig**

```twig
{# url = 'https://www.mind.ch/post/blablabla?param=wow' #}
<a href="{{ url }}">{{ url_to_domain(url) }}</a>
```

---

### get\_link\_attributes

<p class="summary">Gets href attribute for a link tag with proper target and rel attributes.</p>

Checks if the URL is internal or external. Adds a `target="_blank"` for external urls. Inspired by
<http://stackoverflow.com/a/25090564/1059980>. To catch a security vulnerability, the attribute
`rel="noopener noreferrer"` is added, see <https://mathiasbynens.github.io/rel-noopener/> for more info.

`get_link_attributes( string $url )`

**Returns:** `string|bool` An href attribute with target and rel attributes, when necessary. Returns `false` if input
is empty.

| Name | Type | Description |
| --- | --- | --- |
| $url | `string` | URL to be parsed. |

**PHP**

```php
<a <?php echo get_link_attributes( 'https://www.mind.ch/blog' ); ?>>MIND Blog</a>
```

**Twig**

```twig
<a {{ get_link_attributes(url) }}>MIND Blog</a>
```

---

## Twig functions

You need [Timber](https://github.com/timber/timber) to use these functions.

- [is_external_url](#is_external_url)
- [get_link_attributes](#get_link_attributes)
- [url_to_domain](#url_to_domain)

## Support

This is a library that we use at MIND to develop WordPress themes. You’re free to use it, but currently, we don’t provide any support.
11 changes: 11 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once 'lib/functions.php';

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

21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"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"
]
},
"authors": [
{
"name": "Lukas Gaechter",
"email": "[email protected]",
"homepage": "https://www.mind.ch"
}
]
}
134 changes: 134 additions & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

/**
* Converts an URL to an anchor link.
*
* Prefix the last segment of a URL with a # to use it as an anchor link.
*
* @since 1.0.0
* @example
* ```php
* // Output: https://www.mind.ch/onepager#subsection
* echo make_anchor_link( 'https://www.mind.ch/onepager/subsection' );
* ```
*
* @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 ) );
}

/**
* Checks if a URL is external or internal.
*
* @since 1.0.0
* @example
* ```php
* if ( is_external_url( 'https://example.org' ) ) {
* // Do something
* }
* ```
*
* ```twig
* {% if is_external_url('https://www.example.org') %}
* {# Do something #}
* {% endif %}
* ```
*
* @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() );

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

return true;
}

/**
* Converts an URL to just the domain name together with the TLD.
*
* @since 1.0.0
* @example
* ```php
* <?php $domain = 'https://www.mind.ch/post/blablabla?param=wow'; ?>
* <a href="<?php echo $url; ?>"><?php echo url_to_domain( $domain ); ?></a>
* ```
*
* ```twig
* {# url = 'https://www.mind.ch/post/blablabla?param=wow' #}
* <a href="{{ url }}">{{ url_to_domain(url) }}</a>
* ```
*
* @param string $url The URL to convert.
* @param bool $strip_www Optional. Whether to strip the "www" part of the domain. Default false.
* @param int|bool $limit Optional. Limit to a certain amount of characters. Default false.
*
* @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 );

/**
* 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 );
}

// 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;
}

/**
* Gets href attribute for a link tag with proper target and rel attributes.
*
* Checks if the URL is internal or external. Adds a `target="_blank"` for external urls. Inspired by
* <http://stackoverflow.com/a/25090564/1059980>. To catch a security vulnerability, the attribute
* `rel="noopener noreferrer"` is added, see <https://mathiasbynens.github.io/rel-noopener/> for more info.
*
* @since 1.0.0
* @example
* ```php
* <a <?php echo get_link_attributes( 'https://www.mind.ch/blog' ); ?>>MIND Blog</a>
* ```
*
* ```twig
* <a {{ get_link_attributes(url) }}>MIND Blog</a>
* ```
*
* @param string $url URL to be parsed.
*
* @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;
}

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

use Timber\Twig_Function as Timber_Twig_Function;

/**
* Customize Twig
*
* @param Twig_Environment $twig
* @return $twig
*/
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' ) );

/**
* 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' ) );

return $twig;
});

0 comments on commit 4628ded

Please sign in to comment.