meta-tag-manager
Last commit date
css
5 years ago
js
5 years ago
languages
10 years ago
meta-tag-manager-admin.php
4 years ago
meta-tag-manager.php
4 years ago
mtm-admin-settings.php
4 years ago
mtm-builder.php
4 years ago
mtm-encodings.php
10 years ago
mtm-tag-admin.php
4 years ago
mtm-tag.php
4 years ago
mtm-update.php
9 years ago
readme.txt
4 years ago
mtm-tag.php
84 lines
| 1 | <?php |
| 2 | class MTM_Tag { |
| 3 | public $reference; |
| 4 | public $type; |
| 5 | public $value; |
| 6 | public $content; |
| 7 | public $context = false; |
| 8 | |
| 9 | public static $types = array(); |
| 10 | public static $types_with_content = array(); |
| 11 | |
| 12 | public function __construct($values){ |
| 13 | $this->reference = !empty($values['reference']) ? $values['reference']:''; |
| 14 | $this->type = !empty($values['type']) ? $values['type']:''; |
| 15 | $this->value = !empty($values['value']) ? $values['value']:''; |
| 16 | $this->content = !empty($values['content']) ? $values['content']:''; |
| 17 | if( !empty($values['context']) ){ |
| 18 | $this->context = explode(',', $values['context']); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | public function output(){ |
| 23 | $output = apply_filters('mtm_tag_output_pre', null, $this); |
| 24 | if( $output !== null ){ |
| 25 | return apply_filters('mtm_tag_output', $output, $this); |
| 26 | } |
| 27 | $tag_string = '<meta '.esc_attr($this->type).'="'.esc_attr($this->value).'"'; |
| 28 | if( $this->has_content() ){ |
| 29 | if( $this->type == 'http-equiv' && $this->value == 'Link' ){ |
| 30 | //escape the attribute but allow for the <url>; format to pass through |
| 31 | $tag_string .= ' content="'.preg_replace('/<(.+)>;/', '<$1>;', esc_attr($this->get_content())).'"'; |
| 32 | }else{ |
| 33 | $tag_string .= ' content="'.esc_attr($this->get_content()).'"'; |
| 34 | } |
| 35 | } |
| 36 | $tag_string .= ' />'; |
| 37 | return apply_filters('mtm_tag_output', $tag_string, $this); |
| 38 | } |
| 39 | |
| 40 | public function to_array(){ |
| 41 | $array = array( |
| 42 | 'reference' => $this->reference, |
| 43 | 'type' => $this->type, |
| 44 | 'value' => $this->value |
| 45 | ); |
| 46 | if( $this->has_content() ){ |
| 47 | $array['content'] = $this->content; |
| 48 | } |
| 49 | if( $this->context !== false && is_array($this->context) ){ |
| 50 | $array['context'] = implode(',', $this->context); |
| 51 | } |
| 52 | return $array; |
| 53 | } |
| 54 | |
| 55 | public function is_valid(){ |
| 56 | if( in_array($this->type, $this->get_types()) ){ //pass |
| 57 | if( !empty($this->value) ){ //pass |
| 58 | if( $this->has_content() && empty($this->content) ){ //fail |
| 59 | return false; //empty content, no point in outputting |
| 60 | } |
| 61 | return true; //if we get here, we're good |
| 62 | } |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | public function get_content(){ |
| 68 | return apply_filters('mtm_tag_get_content', $this->content, $this); |
| 69 | } |
| 70 | |
| 71 | public static function get_types(){ |
| 72 | if( empty(self::$types) ){ |
| 73 | self::$types = apply_filters('mtm_tag_get_types', array('name','http-equiv','charset','itemprop','property')); |
| 74 | } |
| 75 | return self::$types; |
| 76 | } |
| 77 | |
| 78 | public function has_content(){ |
| 79 | if( empty(self::$types_with_content) ){ |
| 80 | self::$types_with_content = apply_filters('mtm_types_with_content', array('name','http-equiv','itemprop','property')); |
| 81 | } |
| 82 | return in_array($this->type, self::$types_with_content); |
| 83 | } |
| 84 | } |