open-graph-admin.php
4 years ago
open-graph.php
4 years ago
schema-admin.php
4 years ago
schema.php
4 years ago
verify-sites-admin.php
4 years ago
verify-sites.php
4 years ago
open-graph.php
88 lines
| 1 | <?php |
| 2 | namespace Meta_Tag_Manager; |
| 3 | use Meta_Tag_Manager, MTM_Tag; |
| 4 | |
| 5 | class Open_Graph { |
| 6 | public static function init(){ |
| 7 | if( is_admin() ){ |
| 8 | include('open-graph-admin.php'); |
| 9 | } |
| 10 | add_filter('mtm_head_meta_tags', 'Meta_Tag_Manager\Open_Graph::add_tags', 10); |
| 11 | } |
| 12 | |
| 13 | public static function add_tags( $mtm_tags ){ |
| 14 | $og = static::get_options(); |
| 15 | if( empty($og['enabled']) ) return $mtm_tags; |
| 16 | if( is_front_page() ){ |
| 17 | // add front page tags |
| 18 | $this_page = array(); |
| 19 | if( !empty($og['home']['image']) ) $og['home']['image'] = wp_get_attachment_url($og['home']['image']); |
| 20 | foreach( $og['home'] as $k => $v ){ |
| 21 | if( empty($v) ) continue; |
| 22 | $this_page[] = array('type' => 'name', 'value' => 'og:'.$k, 'content' => $v, 'context' => 'home'); |
| 23 | } |
| 24 | if( !empty($og['twitter']['enabled']) ){ |
| 25 | $this_page[] = array('type' => 'name', 'value' => 'twitter:card', 'content' => 'summary', 'context' => 'home'); |
| 26 | if( !empty($og['twitter']['site']) ) $this_page[] = array('type' => 'name', 'value' => 'twitter:site', 'content' => $og['twitter']['site'], 'context' => 'home'); |
| 27 | if( !empty($og['twitter']['creator']) ) $this_page[] = array('type' => 'name', 'value' => 'twitter:creator', 'content' => $og['twitter']['creator'], 'context' => 'home'); |
| 28 | } |
| 29 | foreach( array_reverse($this_page) as $tag_array ){ |
| 30 | $meta_tag = new MTM_Tag($tag_array); |
| 31 | array_unshift($mtm_tags, $meta_tag); |
| 32 | } |
| 33 | }elseif( !empty($og['generate_singular']) && is_singular() ){ |
| 34 | // generate og tags based on current page |
| 35 | $this_page = array( |
| 36 | 'og:type' => 'webssite', |
| 37 | 'og:title' => get_the_title(), |
| 38 | 'og:description' => get_the_excerpt(), |
| 39 | 'og:image' => get_the_post_thumbnail_url(), |
| 40 | 'og:locale' => determine_locale(), |
| 41 | 'og:site_name' => $og['home']['site_name'], |
| 42 | ); |
| 43 | if( !empty($og['twitter']['enabled']) ){ |
| 44 | $this_page['twitter:card'] = 'summary'; |
| 45 | if( !empty($og['twitter']['site']) ) $this_page['twitter:site'] = $og['twitter']['site']; |
| 46 | if( !empty($og['twitter']['creator']) ) $this_page['twitter:site'] = $og['twitter']['creator']; |
| 47 | } |
| 48 | foreach( array_reverse( $this_page ) as $k => $v ){ |
| 49 | $tag_array = array('type' => 'name', 'value' => $k, 'content' => $v, 'context' => 'post-type_'.get_post_type() ); |
| 50 | $meta_tag = new MTM_Tag($tag_array); |
| 51 | array_unshift($mtm_tags, $meta_tag); |
| 52 | } |
| 53 | } |
| 54 | return $mtm_tags; |
| 55 | } |
| 56 | |
| 57 | public static function get_options( $defaults = null ){ |
| 58 | $mtm_custom = get_option('mtm_custom'); |
| 59 | if( $defaults === null ) { |
| 60 | return !empty($mtm_custom['og']) ? $mtm_custom['og'] : false; |
| 61 | }else{ |
| 62 | $locale = (function_exists('determine_locale')) ? determine_locale() : get_locale(); |
| 63 | $og = array( |
| 64 | 'enabled' => false, |
| 65 | 'generate_singular' => false, |
| 66 | 'home' => array( |
| 67 | 'type' => 'website', |
| 68 | 'title' => get_bloginfo('name'), |
| 69 | 'description' => get_bloginfo('description'), |
| 70 | 'image' => get_theme_mod( 'custom_logo' ), |
| 71 | 'locale' => $locale, |
| 72 | 'site_name' => get_bloginfo('name'), |
| 73 | ), |
| 74 | 'twitter' => array( |
| 75 | 'enabled' => true, // enabled if og is enabled, no harm |
| 76 | 'site' => '', |
| 77 | 'creator' => '', |
| 78 | ) |
| 79 | ); |
| 80 | if( !$defaults ){ |
| 81 | $og = empty($mtm_custom['og']) ? $og : Meta_Tag_Manager::array_merge($og, $mtm_custom['og']); |
| 82 | $og['home']['site_name'] = $og['home']['title']; |
| 83 | } |
| 84 | } |
| 85 | return $og; |
| 86 | } |
| 87 | } |
| 88 | Open_Graph::init(); |