class-schema-context.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend\Schema
  6. */
  7. /**
  8. * Context variables for Schema generation.
  9. *
  10. * @property string $canonical The current page's canonical.
  11. * @property string $company_name Holds the company name, if the site represents a company.
  12. * @property int $company_logo_id Holds the company logo's ID, if the site represents a company.
  13. * @property int $id The post ID, if there is one.
  14. * @property string $site_name The site's name.
  15. * @property string $site_description The site's tagline.
  16. * @property string $site_represents Whether this site represents a `company` or a `person`.
  17. * @property string $site_url The site's URL.
  18. * @property int $site_user_id The site's user ID if a site represents a `person`.
  19. * @property string $title Page title.
  20. * @property string $description Page description.
  21. * @property bool $breadcrumbs_enabled Whether or not this site has breadcrumbs enabled.
  22. * @property array $site_represents_reference A schema @id reference to the piece the site represents.
  23. * @property bool $has_image A boolean that determines whether the current URL has a primary image.
  24. *
  25. * @since 10.2
  26. */
  27. class WPSEO_Schema_Context {
  28. /**
  29. * The current page's canonical.
  30. *
  31. * @var string
  32. */
  33. public $canonical;
  34. /**
  35. * Holds the company name, if the site represents a company.
  36. *
  37. * @var string
  38. */
  39. public $company_name;
  40. /**
  41. * Holds the company logo's ID, if the site represents a company.
  42. *
  43. * @var int
  44. */
  45. public $company_logo_id;
  46. /**
  47. * The queried object ID, if there is one.
  48. *
  49. * @var int
  50. */
  51. public $id;
  52. /**
  53. * Whether this site represents a `company` or a `person`.
  54. *
  55. * @var string
  56. */
  57. public $site_represents;
  58. /**
  59. * The site's Name.
  60. *
  61. * @var string
  62. */
  63. public $site_name;
  64. /**
  65. * The site's tagline.
  66. *
  67. * @var string
  68. */
  69. public $site_description;
  70. /**
  71. * The site's URL.
  72. *
  73. * @var string
  74. */
  75. public $site_url;
  76. /**
  77. * Page title.
  78. *
  79. * @var string
  80. */
  81. public $title;
  82. /**
  83. * User ID when the site represents a Person.
  84. *
  85. * @var int
  86. */
  87. public $site_user_id;
  88. /**
  89. * Page description.
  90. *
  91. * @var string
  92. */
  93. public $description;
  94. /**
  95. * Whether or not this site has breadcrumbs enabled.
  96. *
  97. * @var bool
  98. */
  99. public $breadcrumbs_enabled;
  100. /**
  101. * A schema @id reference to the piece the site represents.
  102. *
  103. * @var array
  104. */
  105. public $site_represents_reference;
  106. /**
  107. * A boolean that determines whether the current URL has a primary image.
  108. *
  109. * @var bool
  110. */
  111. public $has_image = false;
  112. /**
  113. * WPSEO_Schema_Context constructor.
  114. */
  115. public function __construct() {
  116. $this->build_data();
  117. }
  118. /**
  119. * Builds all the required data for the context object.
  120. */
  121. private function build_data() {
  122. // Page level variables.
  123. $front = WPSEO_Frontend::get_instance();
  124. $this->canonical = $front->canonical( false, false, true );
  125. $this->title = $front->title( '' );
  126. $this->description = $front->metadesc( false );
  127. $this->id = get_queried_object_id();
  128. // Site level variables.
  129. $this->site_name = $this->set_site_name();
  130. $this->site_description = get_bloginfo( 'description' );
  131. $this->site_url = trailingslashit( WPSEO_Utils::home_url() );
  132. $this->set_breadcrumbs_variables();
  133. $this->set_site_represents_variables();
  134. $this->set_site_represents_reference();
  135. }
  136. /**
  137. * Retrieves the site's name from settings.
  138. *
  139. * @return string
  140. */
  141. private function set_site_name() {
  142. if ( '' !== WPSEO_Options::get( 'website_name', '' ) ) {
  143. return WPSEO_Options::get( 'website_name' );
  144. }
  145. return get_bloginfo( 'name' );
  146. }
  147. /**
  148. * Sets our site represents reference for easy use.
  149. */
  150. private function set_site_represents_reference() {
  151. $this->site_represents_reference = false;
  152. if ( $this->site_represents === 'person' ) {
  153. $this->site_represents_reference = [ '@id' => WPSEO_Schema_Utils::get_user_schema_id( $this->site_user_id, $this ) ];
  154. }
  155. if ( $this->site_represents === 'company' ) {
  156. $this->site_represents_reference = [ '@id' => $this->site_url . WPSEO_Schema_IDs::ORGANIZATION_HASH ];
  157. }
  158. }
  159. /**
  160. * Determines what our site represents, and grabs their values.
  161. */
  162. private function set_site_represents_variables() {
  163. $this->site_represents = WPSEO_Options::get( 'company_or_person', false );
  164. switch ( $this->site_represents ) {
  165. case 'company':
  166. $company_name = WPSEO_Options::get( 'company_name' );
  167. /**
  168. * Filter: 'wpseo_schema_company_name' - Allows filtering company name
  169. *
  170. * @api string $company_name.
  171. */
  172. $this->company_name = apply_filters( 'wpseo_schema_company_name', $company_name );
  173. // Do not use a non-named company.
  174. if ( empty( $this->company_name ) ) {
  175. $this->site_represents = false;
  176. break;
  177. }
  178. $company_logo_id = WPSEO_Image_Utils::get_attachment_id_from_settings( 'company_logo' );
  179. /**
  180. * Filter: 'wpseo_schema_company_logo_id' - Allows filtering company logo id
  181. *
  182. * @api integer $company_logo_id.
  183. */
  184. $this->company_logo_id = apply_filters( 'wpseo_schema_company_logo_id', $company_logo_id );
  185. /*
  186. * Do not use a company without a logo.
  187. * This is not a false check due to how `get_attachment_id_from_settings` works.
  188. */
  189. if ( $this->company_logo_id < 1 ) {
  190. $this->site_represents = false;
  191. }
  192. break;
  193. case 'person':
  194. $this->site_user_id = WPSEO_Options::get( 'company_or_person_user_id', false );
  195. // Do not use a non-existing user.
  196. if ( $this->site_user_id !== false && get_user_by( 'id', $this->site_user_id ) === false ) {
  197. $this->site_represents = false;
  198. }
  199. break;
  200. }
  201. }
  202. /**
  203. * Determines whether the site uses Yoast SEO breadcrumbs.
  204. */
  205. private function set_breadcrumbs_variables() {
  206. $this->breadcrumbs_enabled = current_theme_supports( 'yoast-seo-breadcrumbs' );
  207. if ( ! $this->breadcrumbs_enabled ) {
  208. $this->breadcrumbs_enabled = WPSEO_Options::get( 'breadcrumbs-enable', false );
  209. }
  210. }
  211. }