PluginProbe ʕ •ᴥ•ʔ
Meta Tag Manager / trunk
Meta Tag Manager vtrunk
trunk 0.2 0.3 0.4 0.5 1.0 1.1 1.2 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 3.0 3.0.2 3.1 3.2 3.3
meta-tag-manager / classes / schema.php
meta-tag-manager / classes Last commit date
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
schema.php
162 lines
1 <?php
2 namespace Meta_Tag_Manager;
3 use Meta_Tag_Manager;
4
5 class Schema {
6
7 public static function init(){
8 if( is_admin() ){
9 include('schema-admin.php');
10 }
11 add_action('mtm_head', 'Meta_Tag_Manager\Schema::output');
12 }
13
14 public static function output(){
15 if( !is_front_page() ) return; // for now, only front page stuff
16 $schema_options = static::get_options();
17 ?>
18 <!-- Meta Tag Manager - Schema -->
19 <script type="application/ld+json">
20 <?php echo static::output_general() ."\n" ?>
21 </script>
22 <?php
23 if( !empty($schema_options['sitelinks']['search']) ){
24 if( $schema_options['sitelinks']['search'] == 1 ){
25 echo '<script type="application/ld+json">'."\n\t\t\t";
26 echo static::output_sitelinks_search();
27 echo "\n\t\t".'</script>';
28 }else{
29 echo '<meta name="google" content="nositelinkssearchbox" />';
30 }
31 }
32 if( !empty($schema_options['sitelinks']['menu']) ){
33 $sitelinks = static::output_sitelinks();
34 if( !empty($sitelinks) ){
35 echo "\n\t\t".'<script type="application/ld+json">'."\n\t\t\t";
36 echo static::output_sitelinks();
37 echo "\n\t\t".'</script>';
38 }
39 }
40 echo "\n";
41 ?>
42 <!-- / Meta Tag Manager - Schema -->
43 <?php
44 }
45
46 public static function output_general(){
47 $schema_options = static::get_options();
48 $schema = array('@context' => 'https://schema.org');
49 // get the type of
50 $schema['@type'] = esc_js($schema_options['type']);
51 $schema['name'] = esc_js($schema_options['name']);
52 $schema['url'] = get_site_url();
53 if( $schema_options['type'] == 'Organization' && !empty($schema_options['Organization']['subtype']) ){
54 $schema['@type'] = esc_js($schema_options['Organization']['subtype']);
55 }
56 if( !empty($schema_options['logo']) ){
57 $image = wp_get_attachment_image_src( $schema_options['logo'], 'full' );
58 if( $image ){
59 $schema['logo'] = array(
60 '@type' => 'ImageObject',
61 'url' => $image[0],
62 'width' => $image[1],
63 'height' => $image[2],
64 );
65 }
66 }
67 if( !empty($schema_options['Contact']['enabled']) ){
68 $schema['ContactPoint'] = array(
69 '@type' => 'ContactPoint',
70 'contactType' => esc_js($schema_options['Contact']['name']),
71 );
72 if( !empty($schema_options['Contact']['telephone']) ) $schema['ContactPoint']['telephone'] = esc_js($schema_options['Contact']['telephone']);
73 if( !empty($schema_options['Contact']['email']) ) $schema['ContactPoint']['email'] = esc_js($schema_options['Contact']['email']);
74 if( !empty($schema_options['Contact']['url']) ){
75 if( is_numeric($schema_options['Contact']['url']) ){
76 $schema['ContactPoint']['url'] = get_permalink($schema_options['Contact']['url']);
77 }else{
78 $schema['ContactPoint']['url'] = esc_url($schema_options['Contact']['url']);
79 }
80 }
81 }
82 if( !empty($schema_options['profiles']) ){
83 $schema['sameAs'] = array();
84 foreach( $schema_options['profiles'] as $profile ){
85 $schema['sameAs'][] = esc_url($profile);
86 }
87 }
88 return json_encode($schema);
89 }
90
91 public static function output_sitelinks(){
92 $schema_options = static::get_options();
93 if( !empty($schema_options['sitelinks']['menu']) ){
94 $menu_items = wp_get_nav_menu_items($schema_options['sitelinks']['menu']);
95 if( !empty($menu_items) ){
96 $schema = array (
97 '@context' => 'https://schema.org',
98 '@graph' => array (),
99 );
100 foreach( $menu_items as $menu_item ){
101 $schema['@graph'][] = array (
102 '@context' => 'https://schema.org',
103 '@type' => 'SiteNavigationElement',
104 'id' => 'site-navigation',
105 'name' => $menu_item->title,
106 'url' => esc_js(esc_url($menu_item->url)),
107 );
108 }
109 return json_encode($schema);
110 }
111 }
112 return '';
113 }
114
115 public static function output_sitelinks_search(){
116 $schema = array (
117 '@context' => 'https://schema.org',
118 '@type' => 'WebSite',
119 'name' => get_bloginfo('name'),
120 'url' => get_site_url(),
121 'potentialAction' =>
122 array (
123 0 =>
124 array (
125 '@type' => 'SearchAction',
126 'target' => home_url( '?s={search_term_string}' ),
127 'query-input' => 'required name=search_term_string',
128 ),
129 ),
130 );
131 return json_encode($schema);
132 }
133
134 public static function get_options( $defaults = false ){
135 $mtm_custom = get_option('mtm_custom');
136 $schema_default = array(
137 'enabled' => false,
138 'type' => '',
139 'logo' => 0,
140 'name' => '',
141 'Organization' => array(
142 'type' => 'Organization',
143 'subtype' => 'Organization',
144 ),
145 'Contact' => array(
146 'enabled' => 0,
147 'name' => '',
148 'telephone' => '',
149 'url' => '',
150 'email' => '',
151 ),
152 'sitelinks' => array(
153 'menu' => null,
154 'search' => null,
155 ),
156 'profiles' => array(),
157 );
158 return empty($mtm_custom['schema']) || $defaults ? $schema_default : Meta_Tag_Manager::array_merge($schema_default, $mtm_custom['schema']);
159 }
160
161 }
162 Schema::init();