| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Presenters; |
| 4 |
|
| 5 |
/** |
| 6 |
* Abstract presenter class for indexable tag presentations. |
| 7 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 8 |
* @phpcs:disable Yoast.Files.FileName.InvalidClassFileName |
| 9 |
*/ |
| 10 |
abstract class Abstract_Indexable_Tag_Presenter extends Abstract_Indexable_Presenter { |
| 11 |
|
| 12 |
const META_PROPERTY_CONTENT = '<meta property="%2$s" content="%1$s" />'; |
| 13 |
const META_NAME_CONTENT = '<meta name="%2$s" content="%1$s" />'; |
| 14 |
const LINK_REL_HREF = '<link rel="%2$s" href="%1$s" />'; |
| 15 |
const DEFAULT_TAG_FORMAT = self::META_NAME_CONTENT; |
| 16 |
|
| 17 |
/** |
| 18 |
* The tag format including placeholders. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $tag_format = self::DEFAULT_TAG_FORMAT; |
| 23 |
|
| 24 |
/** |
| 25 |
* The method of escaping to use. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $escaping = 'attribute'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns a tag in the head. |
| 33 |
* |
| 34 |
* @return string The tag. |
| 35 |
*/ |
| 36 |
public function present() { |
| 37 |
$value = $this->get(); |
| 38 |
|
| 39 |
if ( ! \is_string( $value ) || $value === '' ) { |
| 40 |
return ''; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* There may be some classes that are derived from this class that do not use the $key property |
| 45 |
* in their $tag_format string. In that case the key property will simply not be used. |
| 46 |
*/ |
| 47 |
return \sprintf( $this->tag_format, $this->escape_value( $value ), $this->key ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Escaped the output. |
| 52 |
* |
| 53 |
* @param string $value The desired method of escaping; 'html', 'url' or 'attribute'. |
| 54 |
* |
| 55 |
* @return string The escaped value. |
| 56 |
*/ |
| 57 |
protected function escape_value( $value ) { |
| 58 |
switch ( $this->escaping ) { |
| 59 |
case 'html': |
| 60 |
return \esc_html( $value ); |
| 61 |
case 'url': |
| 62 |
return \esc_url( $value ); |
| 63 |
case 'attribute': |
| 64 |
default: |
| 65 |
return \esc_attr( $value ); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|