| | |
| | | use JVBase\managers\SEO\render\Thing\Intangible\OfferCatalog; |
| | | use JVBase\managers\SEO\render\Thing\Intangible\Service; |
| | | use JVBase\managers\SEO\render\Thing\Organization\LocalBusiness\LocalBusiness; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\registrar\config\seo\Resolver; |
| | | use JVBase\registrar\Registrar; |
| | | |
| | |
| | | public function buildSchema():array |
| | | { |
| | | $schema = []; |
| | | if (!is_front_page()) { |
| | | $schema[] = $this->buildBasicWebsiteSchema(); |
| | | |
| | | $schema[] = $this->buildWebsiteSchema(false); |
| | | if (is_front_page()) { |
| | | $test = $this->buildOrganizationSchema(); |
| | | if (!empty($test)) { |
| | | $schema[] = $test; |
| | | } |
| | | } |
| | | if (is_singular($this->types)) { |
| | | $type = get_post_type(); |
| | |
| | | } |
| | | |
| | | if (!empty($schema)) { |
| | | $website = get_option(BASE.'WebsiteSchema'); |
| | | $website = JVB()->schemaHelper()::schema('website'); |
| | | if (!empty($website)) { |
| | | if (JVB_TESTING) { |
| | | Cache::for('websiteSchema')->flush(); |
| | |
| | | |
| | | protected function websiteSchema():array |
| | | { |
| | | $stored = get_option(BASE.'WebsiteSchema', apply_filters(BASE.'WebsiteSchema', [])); |
| | | $stored = JVB()->schemaHelper()::schema('website'); |
| | | $website = JVB()->schemaHelper()::classFromConfig($stored); |
| | | |
| | | $seo = new WebSite(); |
| | | foreach ($stored as $property => $value) { |
| | | $method = 'set'.ucfirst($property); |
| | | $seo->$method($value); |
| | | if (!$website->getUrl() || empty($website->getUrl())){ |
| | | $website->setUrl(get_home_url()); |
| | | } |
| | | $seo->setUrl(get_home_url()); |
| | | $seo->setName(get_bloginfo('name')); |
| | | |
| | | $seo->setCreator($this->getCreator()); |
| | | return $seo->outputSchema(); |
| | | if (!$website->getName() || empty($website->getName())){ |
| | | $website->setName(get_bloginfo('name')); |
| | | } |
| | | if(!$website->getCreator() || empty($website->getCreator())) { |
| | | $website->setCreator($this->getCreator()); |
| | | } |
| | | return $website->outputSchema(); |
| | | } |
| | | |
| | | public function outputSchema():void |
| | |
| | | return BreadcrumbManager::getInstance()->toSchema(); |
| | | } |
| | | |
| | | public function buildBasicWebsiteSchema():array |
| | | public function buildWebsiteSchema(bool $full = true):array |
| | | { |
| | | if (JVB_TESTING){ |
| | | Cache::for('websiteSchema')->flush(); |
| | | } |
| | | $storedWebsite = JVB()->schemaHelper()::schema('website'); |
| | | |
| | | if (!$full) { |
| | | return Cache::for('websiteSchema')->remember( |
| | | 'reference', |
| | | function () use ($storedWebsite) { |
| | | $allowed = ['type','name', 'url', 'description', 'inLanguage']; |
| | | $storedWebsite = array_filter($storedWebsite, function ($key) use ($allowed) { |
| | | return in_array($key, $allowed); |
| | | }, ARRAY_FILTER_USE_KEY); |
| | | |
| | | $website = JVB()->schemaHelper()::classFromConfig($storedWebsite); |
| | | if (!$website->getName() || empty($website->getName())) { |
| | | $website->setName(get_bloginfo('name')); |
| | | } |
| | | if (!$website->getUrl() || empty($website->getUrl())) { |
| | | $website->setUrl(get_home_url()); |
| | | } |
| | | if (!$website->getId() || empty($website->getId())) { |
| | | $website->setId(get_home_url().'/#website'); |
| | | } |
| | | if (!$website->getDescription() || empty($website->getDescription())) { |
| | | $website->setDescription(get_bloginfo('description')); |
| | | } |
| | | if (!$website->getInLanguage() || empty($website->getInLanguage())) { |
| | | $website->setInLanguage('en-CA'); |
| | | } |
| | | if (!$website->getPublisher() || empty($website->getPublisher())) { |
| | | $publisher = $this->getOptionSchemaReference('organization'); |
| | | if ($publisher){ |
| | | $website->setPublisher($publisher); |
| | | } |
| | | } |
| | | |
| | | $website->setCreator($this->getCreator(true)); |
| | | |
| | | return $website->outputSchema(); |
| | | } |
| | | ); |
| | | } |
| | | return Cache::for('websiteSchema')->remember( |
| | | 'reference', |
| | | function () { |
| | | $website = new WebSite(); |
| | | $website->setName(get_bloginfo('name')); |
| | | $website->setUrl(get_home_url()); |
| | | $website->setId(get_home_url().'/#website'); |
| | | $website->setDescription(get_bloginfo('description')); |
| | | $website->setInLanguage('en-CA'); |
| | | $publisher = $this->getOptionSchemaReference('organization'); |
| | | if ($publisher){ |
| | | $website->setPublisher($publisher); |
| | | 'full', |
| | | function () use ($storedWebsite) { |
| | | error_log('StoredWebsite: '.print_r($storedWebsite, true)); |
| | | $website = JVB()->schemaHelper()::classFromConfig($storedWebsite); |
| | | if (!$website->getName() || empty($website->getName())) { |
| | | $website->setName(get_bloginfo('name')); |
| | | } |
| | | if (!$website->getUrl() || empty($website->getUrl())) { |
| | | $website->setUrl(get_home_url()); |
| | | } |
| | | if (!$website->getId() || empty($website->getId())) { |
| | | $website->setId(get_home_url().'/#website'); |
| | | } |
| | | if (!$website->getDescription() || empty($website->getDescription())) { |
| | | $website->setDescription(get_bloginfo('description')); |
| | | } |
| | | if (!$website->getInLanguage() || empty($website->getInLanguage())) { |
| | | $website->setInLanguage('en-CA'); |
| | | } |
| | | if (!$website->getPublisher() || empty($website->getPublisher())) { |
| | | $publisher = $this->getOptionSchemaReference('organization'); |
| | | if ($publisher){ |
| | | $website->setPublisher($publisher); |
| | | } |
| | | } |
| | | |
| | | $website->setCreator($this->getCreator(true)); |
| | |
| | | return null; |
| | | } |
| | | $action = BASE.ucfirst($option).'Schema'; |
| | | $stored = get_option($action, apply_filters($action, [])); |
| | | // $stored = get_option($action, apply_filters($action, jvbDefaultSchema($option))); |
| | | $stored = JVB()->schemaHelper()::reference($action); |
| | | |
| | | if (empty($stored)){ |
| | | error_log('Attempted to get schema reference for: '.$option.', but defaults not set.'); |
| | | return null; |
| | |
| | | $value = array_key_exists($property, $stored) ? $stored[$property] : null; |
| | | if (!$value) {continue;} |
| | | |
| | | $class->$method(Resolver::resolve($property, $value)); |
| | | if (str_contains($value, '{{')) { |
| | | $value = Resolver::resolve($property, $value); |
| | | } |
| | | $class->$method($value); |
| | | } |
| | | return $class; |
| | | } |
| | | |
| | | public function buildOrganizationSchema():array |
| | | { |
| | | $config = JVB()->schemaHelper()::schema('organization'); |
| | | $class = JVB()->schemaHelper()::classFromConfig($config); |
| | | return ($class)? $class->outputSchema() : []; |
| | | } |
| | | } |
| | | |