-
-
Save brasofilo/5577081 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Plugin Name: T5 Head Meta | |
* Description: Meta data for head element | |
* Plugin URI: | |
* Version: 2013.04.08 | |
* Author: Thomas Scholz | |
* Author URI: http://toscho.de | |
* Licence: MIT | |
* License URI: http://opensource.org/licenses/MIT | |
*/ | |
add_action( 'template_redirect', array ( T5_Head_Meta::get_instance(), 'setup' ) ); | |
class T5_Head_Meta | |
{ | |
/** | |
* Plugin instance. | |
* | |
* @type object | |
*/ | |
protected static $instance = NULL; | |
/** | |
* Used on singular views. Filled with get_post(). | |
* | |
* @type object | |
*/ | |
protected $post = FALSE; | |
/** | |
* Static initializator. | |
* | |
* @wp-hook wp_head | |
* @return object | |
*/ | |
public static function get_instance() | |
{ | |
NULL === self::$instance && self::$instance = new self; | |
return self::$instance; | |
} | |
/** | |
* Set up plugin actions | |
* | |
* @wp-hook template_redirect | |
*/ | |
public function setup() | |
{ | |
add_action( 'wp_head', array ( $this, 'create_description' ) ); | |
do_action( 't5_head_setup_done', $this ); | |
} | |
/** | |
* Meta description. | |
* | |
* @wp-hook wp_head | |
* @return string|boolean | |
*/ | |
public function create_description() | |
{ | |
if ( is_singular() && post_password_required() ) | |
return FALSE; | |
if ( is_singular() && '' === $desc = $this->get_singular_desc() ) | |
return FALSE; | |
if ( ( is_home() || is_front_page() ) | |
&& '' === $desc = $this->get_home_desc() ) | |
return FALSE; | |
if ( ( is_tax() || is_tag() || is_category() ) | |
&& '' === $desc = $this->get_term_desc() ) | |
return FALSE; | |
if ( empty ( $desc ) ) | |
return FALSE; | |
$meta = "<meta name='description' content='$desc' />"; | |
print $meta; | |
return $meta; | |
} | |
/** | |
* Get description for singular view. | |
* | |
* @wp-hook wp_head | |
* @return string | |
*/ | |
protected function get_singular_desc() | |
{ | |
if ( ! $this->post ) | |
$this->post = get_post( get_the_ID() ); | |
if ( ! $this->post ) | |
return ''; | |
$desc = $this->prepare_attr_value( $this->post->post_excerpt ); | |
if ( '' === $desc ) | |
$desc = $this->prepare_attr_value( $this->post->post_content ); | |
if ( '' === $desc ) | |
return ''; | |
return $this->limit_string( $desc ); | |
} | |
/** | |
* Get bloginfo description. | |
* | |
* @return string | |
*/ | |
protected function get_home_desc() | |
{ | |
$desc = get_bloginfo( 'description', 'display' ); | |
$desc = $this->prepare_attr_value( $desc ); | |
return $this->limit_string( $desc ); | |
} | |
/** | |
* Get escaped term description. | |
* | |
* @return string | |
*/ | |
protected function get_term_desc() | |
{ | |
$desc = term_description(); | |
$desc = $this->prepare_attr_value( $desc ); | |
return $this->limit_string( $desc ); | |
} | |
/** | |
* Remove tags and escape for attribute. | |
* | |
* @param string $str | |
* @return string | |
*/ | |
protected function prepare_attr_value( $str ) | |
{ | |
$str = wp_strip_all_tags( $str, TRUE ); | |
$str = strip_shortcodes( $str ); | |
return esc_attr( $str ); | |
} | |
/** | |
* Shorten a string. | |
* | |
* @param string $str strip_tags() should be done before. | |
* @param int $length | |
* @return string | |
*/ | |
protected function limit_string( $str, $length = 160, $end = ' …' ) | |
{ | |
if ( $length >= mb_strlen( $str, 'utf-8' ) ) | |
return $str; | |
$str = $this->trim_punctuation( $str, 'both' ); | |
if ( $length >= mb_strlen( $str, 'utf-8' ) ) | |
return $str; | |
$sub = mb_substr( $str, 0, $length+1 ); | |
$words = explode( " ", $sub ); | |
array_pop( $words ); | |
$str = join( ' ', $words ); | |
$str = $this->trim_punctuation( $str ); | |
$last = mb_substr( $str, -1, 1 ); | |
if ( in_array( $last, array ( '.', '?', '!', '…' ) ) ) | |
return $str; | |
return $str . $end; | |
} | |
/** | |
* Remove punctuation marks from string. | |
* | |
* @param string $str String to shorten | |
* @param string $pos Where to cut: right, left or both. | |
* @return string | |
*/ | |
protected function trim_punctuation( $str, $pos = 'right' ) | |
{ | |
$chars = ';,:-–—»«„“”\'")(][}{><&§'; | |
if ( 'right' === $pos ) | |
return rtrim( $str, $chars ); | |
if ( 'left' === $pos ) | |
return rtrim( $str, $chars ); | |
return trim( $str, $chars ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment