WPSEO_JSON_LD

Class WPSEO_JSON_LD.

Defined (1)

The class is defined in the following location(s).

/frontend/class-json-ld.php  
  1. class WPSEO_JSON_LD { 
  2.  
  3. /** 
  4. * @var array Holds the plugins options. 
  5. */ 
  6. public $options = array(); 
  7.  
  8. /** 
  9. * @var array Holds the social profiles for the entity 
  10. */ 
  11. private $profiles = array(); 
  12.  
  13. /** 
  14. * @var array Holds the data to put out 
  15. */ 
  16. private $data = array(); 
  17.  
  18. /** 
  19. * Class constructor 
  20. */ 
  21. public function __construct() { 
  22. $this->options = WPSEO_Options::get_options( array( 'wpseo', 'wpseo_social' ) ); 
  23.  
  24. add_action( 'wpseo_head', array( $this, 'json_ld' ), 90 ); 
  25. add_action( 'wpseo_json_ld', array( $this, 'website' ), 10 ); 
  26. add_action( 'wpseo_json_ld', array( $this, 'organization_or_person' ), 20 ); 
  27.  
  28. /** 
  29. * JSON LD output function that the functions for specific code can hook into 
  30. * @since 1.8 
  31. */ 
  32. public function json_ld() { 
  33. do_action( 'wpseo_json_ld' ); 
  34.  
  35. /** 
  36. * Outputs code to allow Google to recognize social profiles for use in the Knowledge graph 
  37. * @since 1.8 
  38. */ 
  39. public function organization_or_person() { 
  40. if ( '' === $this->options['company_or_person'] ) { 
  41. return; 
  42.  
  43. $this->prepare_organization_person_markup(); 
  44.  
  45. switch ( $this->options['company_or_person'] ) { 
  46. case 'company': 
  47. $this->organization(); 
  48. break; 
  49. case 'person': 
  50. $this->person(); 
  51. break; 
  52.  
  53. $this->output( $this->options['company_or_person'] ); 
  54.  
  55. /** 
  56. * Outputs code to allow recognition of the internal search engine 
  57. * @since 1.5.7 
  58. * @link https://developers.google.com/structured-data/site-name 
  59. */ 
  60. public function website() { 
  61. $this->data = array( 
  62. '@context' => 'http://schema.org',  
  63. '@type' => 'WebSite',  
  64. '@id' => '#website',  
  65. 'url' => $this->get_home_url(),  
  66. 'name' => $this->get_website_name(),  
  67. ); 
  68.  
  69. $this->add_alternate_name(); 
  70. $this->internal_search_section(); 
  71.  
  72. $this->output( 'website' ); 
  73.  
  74. /** 
  75. * Outputs the JSON LD code in a valid JSON+LD wrapper 
  76. * @since 1.8 
  77. * @param string $context The context of the output, useful for filtering. 
  78. */ 
  79. private function output( $context ) { 
  80. /** 
  81. * Filter: 'wpseo_json_ld_output' - Allows filtering of the JSON+LD output 
  82. * @api array $output The output array, before its JSON encoded 
  83. * @param string $context The context of the output, useful to determine whether to filter or not. 
  84. */ 
  85. $this->data = apply_filters( 'wpseo_json_ld_output', $this->data, $context ); 
  86.  
  87. if ( is_array( $this->data ) && ! empty( $this->data ) ) { 
  88. $json_data = wp_json_encode( $this->data ); 
  89.  
  90. echo "<script type='application/ld+json'>", $json_data, '</script>', "\n"; 
  91.  
  92. // Empty the $data array so we don't output it twice. 
  93. $this->data = array(); 
  94.  
  95. /** 
  96. * Schema for Organization 
  97. */ 
  98. private function organization() { 
  99. if ( '' !== $this->options['company_name'] ) { 
  100. $this->data['@type'] = 'Organization'; 
  101. $this->data['@id'] = '#organization'; 
  102. $this->data['name'] = $this->options['company_name']; 
  103. $this->data['logo'] = $this->options['company_logo']; 
  104. return; 
  105. $this->data = false; 
  106.  
  107. /** 
  108. * Schema for Person 
  109. */ 
  110. private function person() { 
  111. if ( '' !== $this->options['person_name'] ) { 
  112. $this->data['@type'] = 'Person'; 
  113. $this->data['@id'] = '#person'; 
  114. $this->data['name'] = $this->options['person_name']; 
  115. return; 
  116. $this->data = false; 
  117.  
  118. /** 
  119. * Prepares the organization or person markup 
  120. */ 
  121. private function prepare_organization_person_markup() { 
  122. $this->fetch_social_profiles(); 
  123.  
  124. $this->data = array( 
  125. '@context' => 'http://schema.org',  
  126. '@type' => '',  
  127. 'url' => WPSEO_Frontend::get_instance()->canonical( false, true ),  
  128. 'sameAs' => $this->profiles,  
  129. ); 
  130.  
  131. /** 
  132. * Retrieve the social profiles to display in the organization output. 
  133. * @since 1.8 
  134. * @link https://developers.google.com/webmasters/structured-data/customize/social-profiles 
  135. */ 
  136. private function fetch_social_profiles() { 
  137. $social_profiles = array( 
  138. 'facebook_site',  
  139. 'instagram_url',  
  140. 'linkedin_url',  
  141. 'plus-publisher',  
  142. 'myspace_url',  
  143. 'youtube_url',  
  144. 'pinterest_url',  
  145. ); 
  146. foreach ( $social_profiles as $profile ) { 
  147. if ( $this->options[ $profile ] !== '' ) { 
  148. $this->profiles[] = $this->options[ $profile ]; 
  149.  
  150. if ( ! empty( $this->options['twitter_site'] ) ) { 
  151. $this->profiles[] = 'https://twitter.com/' . $this->options['twitter_site']; 
  152.  
  153. /** 
  154. * Retrieves the home URL 
  155. * @return string 
  156. */ 
  157. private function get_home_url() { 
  158. /** 
  159. * Filter: 'wpseo_json_home_url' - Allows filtering of the home URL for Yoast SEO's JSON+LD output 
  160. * @api unsigned string 
  161. */ 
  162. return apply_filters( 'wpseo_json_home_url', WPSEO_Utils::home_url() ); 
  163.  
  164. /** 
  165. * Returns an alternate name if one was specified in the Yoast SEO settings 
  166. */ 
  167. private function add_alternate_name() { 
  168. if ( '' !== $this->options['alternate_website_name'] ) { 
  169. $this->data['alternateName'] = $this->options['alternate_website_name']; 
  170.  
  171. /** 
  172. * Adds the internal search JSON LD code if it's not disabled 
  173. * @link https://developers.google.com/structured-data/slsb-overview 
  174. */ 
  175. private function internal_search_section() { 
  176. /** 
  177. * Filter: 'disable_wpseo_json_ld_search' - Allow disabling of the json+ld output 
  178. * @api bool $display_search Whether or not to display json+ld search on the frontend 
  179. */ 
  180. if ( ! apply_filters( 'disable_wpseo_json_ld_search', false ) ) { 
  181. /** 
  182. * Filter: 'wpseo_json_ld_search_url' - Allows filtering of the search URL for Yoast SEO 
  183. * @api string $search_url The search URL for this site with a `{search_term_string}` variable. 
  184. */ 
  185. $search_url = apply_filters( 'wpseo_json_ld_search_url', $this->get_home_url() . '?s={search_term_string}' ); 
  186.  
  187. $this->data['potentialAction'] = array( 
  188. '@type' => 'SearchAction',  
  189. 'target' => $search_url,  
  190. 'query-input' => 'required name=search_term_string',  
  191. ); 
  192.  
  193. /** 
  194. * Returns the website name either from Yoast SEO's options or from the site settings 
  195. * @since 2.1 
  196. * @return string 
  197. */ 
  198. private function get_website_name() { 
  199. if ( '' !== $this->options['website_name'] ) { 
  200. return $this->options['website_name']; 
  201.  
  202. return get_bloginfo( 'name' ); 
  203.  
  204. /** 
  205. * Renders internal search schema markup 
  206. * @deprecated 2.1 
  207. * @deprecated use WPSEO_JSON_LD::website() 
  208.   
  209. * @codeCoverageIgnore 
  210. */ 
  211. public function internal_search() { 
  212. _deprecated_function( __METHOD__, 'WPSEO 2.1', 'WPSEO_JSON_LD::website()' ); 
  213.  
  214. $this->website();