plugin installs

This commit is contained in:
Tony Volpe
2024-09-25 09:25:31 -04:00
parent 65a07c8d4d
commit cc870f301f
2953 changed files with 514886 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?php
namespace Yoast\WP\SEO\Helpers\Schema;
/**
* Class Article_Helper.
*/
class Article_Helper {
/**
* Determines whether a given post type should have Article schema.
*
* @param string|null $post_type Post type to check.
*
* @return bool True if it has Article schema, false if not.
*/
public function is_article_post_type( $post_type = null ) {
if ( \is_null( $post_type ) ) {
$post_type = \get_post_type();
}
return $this->is_author_supported( $post_type );
}
/**
* Checks whether author is supported for the passed object sub type.
*
* @param string $object_sub_type The sub type of the object to check author support for.
*
* @return bool True if author is supported for the passed object sub type.
*/
public function is_author_supported( $object_sub_type ) {
return \post_type_supports( $object_sub_type, 'author' );
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Yoast\WP\SEO\Helpers\Schema;
/**
* Class HTML_Helper.
*/
class HTML_Helper {
/**
* Sanitizes a HTML string by stripping all tags except headings, breaks, lists, links, paragraphs and formatting.
*
* @param string $html The original HTML.
*
* @return string The sanitized HTML.
*/
public function sanitize( $html ) {
if ( ! $this->is_non_empty_string_or_stringable( $html ) ) {
if ( \is_int( $html ) || \is_float( $html ) ) {
return (string) $html;
}
return '';
}
return \strip_tags( $html, '<h1><h2><h3><h4><h5><h6><br><ol><ul><li><a><p><b><strong><i><em>' );
}
/**
* Strips the tags in a smart way.
*
* @param string $html The original HTML.
*
* @return string The sanitized HTML.
*/
public function smart_strip_tags( $html ) {
if ( ! $this->is_non_empty_string_or_stringable( $html ) ) {
if ( \is_int( $html ) || \is_float( $html ) ) {
return (string) $html;
}
return '';
}
// Replace all new lines with spaces.
$html = \preg_replace( '/(\r|\n)/', ' ', $html );
// Replace <br> tags with spaces.
$html = \preg_replace( '/<br(\s*)?\/?>/i', ' ', $html );
// Replace closing </p> and other tags with the same tag with a space after it, so we don't end up connecting words when we remove them later.
$html = \preg_replace( '/<\/(p|div|h\d)>/i', '</$1> ', $html );
// Replace list items with list identifiers so it still looks natural.
$html = \preg_replace( '/(<li[^>]*>)/i', '$1• ', $html );
// Strip tags.
$html = \wp_strip_all_tags( $html );
// Replace multiple spaces with one space.
$html = \preg_replace( '!\s+!', ' ', $html );
return \trim( $html );
}
/**
* Verifies that the received input is either a string or stringable object.
*
* @param string $html The original HTML.
*
* @return bool
*/
private function is_non_empty_string_or_stringable( $html ) {
return ( \is_string( $html ) || ( \is_object( $html ) && \method_exists( $html, '__toString' ) ) ) && ! empty( $html );
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Yoast\WP\SEO\Helpers\Schema;
use Yoast\WP\SEO\Config\Schema_IDs;
use Yoast\WP\SEO\Context\Meta_Tags_Context;
/**
* Schema utility functions.
*/
class ID_Helper {
/**
* Retrieve a users Schema ID.
*
* @param int $user_id The ID of the User you need a Schema ID for.
* @param Meta_Tags_Context $context A value object with context variables.
*
* @return string The user's schema ID.
*/
public function get_user_schema_id( $user_id, $context ) {
$user = \get_userdata( $user_id );
if ( \is_a( $user, 'WP_User' ) ) {
return $context->site_url . Schema_IDs::PERSON_HASH . \wp_hash( $user->user_login . $user_id );
}
return '';
}
}

View File

@@ -0,0 +1,205 @@
<?php
namespace Yoast\WP\SEO\Helpers\Schema;
use Yoast\WP\SEO\Helpers\Image_Helper as Main_Image_Helper;
/**
* Class Image_Helper.
*/
class Image_Helper {
/**
* The HTML helper.
*
* @var HTML_Helper
*/
private $html;
/**
* The language helper.
*
* @var Language_Helper
*/
private $language;
/**
* Represents the main image helper.
*
* @var Main_Image_Helper
*/
private $image;
/**
* Image_Helper constructor.
*
* @codeCoverageIgnore It handles dependencies.
*
* @param HTML_Helper $html The HTML helper.
* @param Language_Helper $language The language helper.
* @param Main_Image_Helper $image The 'main' image helper.
*/
public function __construct( HTML_Helper $html, Language_Helper $language, Main_Image_Helper $image ) {
$this->html = $html;
$this->language = $language;
$this->image = $image;
}
/**
* Find an image based on its URL and generate a Schema object for it.
*
* @param string $schema_id The `@id` to use for the returned image.
* @param string $url The image URL to base our object on.
* @param string $caption An optional caption.
* @param bool $add_hash Whether a hash will be added as a suffix in the @id.
* @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id.
*
* @return array Schema ImageObject array.
*/
public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = false, $use_link_table = true ) {
$attachment_id = $this->image->get_attachment_by_url( $url, $use_link_table );
if ( $attachment_id > 0 ) {
return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption, $add_hash );
}
return $this->simple_image_object( $schema_id, $url, $caption, $add_hash );
}
/**
* Retrieve data about an image from the database and use it to generate a Schema object.
*
* @param string $schema_id The `@id` to use for the returned image.
* @param int $attachment_id The attachment to retrieve data from.
* @param string $caption The caption string, if there is one.
* @param bool $add_hash Whether a hash will be added as a suffix in the @id.
*
* @return array Schema ImageObject array.
*/
public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '', $add_hash = false ) {
$data = $this->generate_object();
$url = $this->image->get_attachment_image_url( $attachment_id, 'full' );
$id_suffix = ( $add_hash ) ? \md5( $url ) : '';
$data['@id'] = $schema_id . $id_suffix;
$data['url'] = $url;
$data['contentUrl'] = $url;
$data = $this->add_image_size( $data, $attachment_id );
$data = $this->add_caption( $data, $attachment_id, $caption );
return $data;
}
/**
* Retrieve data about an image from the database and use it to generate a Schema object.
*
* @param string $schema_id The `@id` to use for the returned image.
* @param array $attachment_meta The attachment metadata.
* @param string $caption The caption string, if there is one.
* @param bool $add_hash Whether a hash will be added as a suffix in the @id.
*
* @return array Schema ImageObject array.
*/
public function generate_from_attachment_meta( $schema_id, $attachment_meta, $caption = '', $add_hash = false ) {
$data = $this->generate_object();
$id_suffix = ( $add_hash ) ? \md5( $attachment_meta['url'] ) : '';
$data['@id'] = $schema_id . $id_suffix;
$data['url'] = $attachment_meta['url'];
$data['contentUrl'] = $data['url'];
$data['width'] = $attachment_meta['width'];
$data['height'] = $attachment_meta['height'];
if ( ! empty( $caption ) ) {
$data['caption'] = $this->html->smart_strip_tags( $caption );
}
return $data;
}
/**
* If we can't find $url in our database, we output a simple ImageObject.
*
* @param string $schema_id The `@id` to use for the returned image.
* @param string $url The image URL.
* @param string $caption A caption, if set.
* @param bool $add_hash Whether a hash will be added as a suffix in the @id.
*
* @return array Schema ImageObject array.
*/
public function simple_image_object( $schema_id, $url, $caption = '', $add_hash = false ) {
$data = $this->generate_object();
$id_suffix = ( $add_hash ) ? \md5( $url ) : '';
$data['@id'] = $schema_id . $id_suffix;
$data['url'] = $url;
$data['contentUrl'] = $url;
if ( ! empty( $caption ) ) {
$data['caption'] = $this->html->smart_strip_tags( $caption );
}
return $data;
}
/**
* Retrieves an image's caption if set, or uses the alt tag if that's set.
*
* @param array $data An ImageObject Schema array.
* @param int $attachment_id Attachment ID.
* @param string $caption The caption string, if there is one.
*
* @return array An imageObject with width and height set if available.
*/
private function add_caption( $data, $attachment_id, $caption = '' ) {
if ( $caption !== '' ) {
$data['caption'] = $caption;
return $data;
}
$caption = $this->image->get_caption( $attachment_id );
if ( ! empty( $caption ) ) {
$data['caption'] = $this->html->smart_strip_tags( $caption );
return $data;
}
return $data;
}
/**
* Generates our bare bone ImageObject.
*
* @return array an empty ImageObject
*/
private function generate_object() {
$data = [
'@type' => 'ImageObject',
];
$data = $this->language->add_piece_language( $data );
return $data;
}
/**
* Adds image's width and height.
*
* @param array $data An ImageObject Schema array.
* @param int $attachment_id Attachment ID.
*
* @return array An imageObject with width and height set if available.
*/
private function add_image_size( $data, $attachment_id ) {
$image_meta = $this->image->get_metadata( $attachment_id );
if ( empty( $image_meta['width'] ) || empty( $image_meta['height'] ) ) {
return $data;
}
$data['width'] = $image_meta['width'];
$data['height'] = $image_meta['height'];
return $data;
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Yoast\WP\SEO\Helpers\Schema;
/**
* Class Language_Helper.
*/
class Language_Helper {
/**
* Adds the `inLanguage` property to a Schema piece.
*
* Must use one of the language codes from the IETF BCP 47 standard. The
* language tag syntax is made of one or more subtags separated by a hyphen
* e.g. "en", "en-US", "zh-Hant-CN".
*
* @param array $data The Schema piece data.
*
* @return array The Schema piece data with added language property
*/
public function add_piece_language( $data ) {
/**
* Filter: 'wpseo_schema_piece_language' - Allow changing the Schema piece language.
*
* @param string $type The Schema piece language.
* @param array $data The Schema piece data.
*/
$data['inLanguage'] = \apply_filters( 'wpseo_schema_piece_language', \get_bloginfo( 'language' ), $data );
return $data;
}
}

View File

@@ -0,0 +1,135 @@
<?php
namespace Yoast\WP\SEO\Helpers\Schema;
use Closure;
use WPSEO_Replace_Vars;
use Yoast\WP\SEO\Conditionals\No_Conditionals;
use Yoast\WP\SEO\Config\Schema_IDs;
use Yoast\WP\SEO\Context\Meta_Tags_Context;
use Yoast\WP\SEO\Helpers\Date_Helper;
use Yoast\WP\SEO\Presentations\Indexable_Presentation;
/**
* Registers the Schema replace variables and exposes a method to replace variables on a Schema graph.
*/
class Replace_Vars_Helper {
use No_Conditionals;
/**
* The replace vars.
*
* @var WPSEO_Replace_Vars
*/
protected $replace_vars;
/**
* The Schema ID helper.
*
* @var ID_Helper
*/
protected $id_helper;
/**
* The date helper.
*
* @var Date_Helper
*/
protected $date_helper;
/**
* Replace_Vars_Helper constructor.
*
* @param WPSEO_Replace_Vars $replace_vars The replace vars.
* @param ID_Helper $id_helper The Schema ID helper.
* @param Date_Helper $date_helper The date helper.
*/
public function __construct(
WPSEO_Replace_Vars $replace_vars,
ID_Helper $id_helper,
Date_Helper $date_helper
) {
$this->replace_vars = $replace_vars;
$this->id_helper = $id_helper;
$this->date_helper = $date_helper;
}
/**
* Replaces the variables.
*
* @param array $schema_data The Schema data.
* @param Indexable_Presentation $presentation The indexable presentation.
*
* @return array The array with replaced vars.
*/
public function replace( array $schema_data, Indexable_Presentation $presentation ) {
foreach ( $schema_data as $key => $value ) {
if ( \is_array( $value ) ) {
$schema_data[ $key ] = $this->replace( $value, $presentation );
continue;
}
$schema_data[ $key ] = $this->replace_vars->replace( $value, $presentation->source );
}
return $schema_data;
}
/**
* Registers the Schema-related replace vars.
*
* @param Meta_Tags_Context $context The meta tags context.
*
* @return void
*/
public function register_replace_vars( $context ) {
$replace_vars = [
'main_schema_id' => $context->main_schema_id,
'author_id' => $this->id_helper->get_user_schema_id( $context->indexable->author_id, $context ),
'person_id' => $context->site_url . Schema_IDs::PERSON_HASH,
'primary_image_id' => $context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH,
'webpage_id' => $context->main_schema_id,
'website_id' => $context->site_url . Schema_IDs::WEBSITE_HASH,
'organization_id' => $context->site_url . Schema_IDs::ORGANIZATION_HASH,
];
if ( $context->post ) {
// Post does not always exist, e.g. on term pages.
$replace_vars['post_date'] = $this->date_helper->format( $context->post->post_date, \DATE_ATOM );
}
foreach ( $replace_vars as $var => $value ) {
$this->register_replacement( $var, $value );
}
}
/**
* Registers a replace var and its value.
*
* @param string $variable The replace variable.
* @param string $value The value that the variable should be replaced with.
*
* @return void
*/
protected function register_replacement( $variable, $value ) {
$this->replace_vars->safe_register_replacement(
$variable,
$this->get_identity_function( $value )
);
}
/**
* Returns an anonymous function that in turn just returns the given value.
*
* @param mixed $value The value that the function should return.
*
* @return Closure A function that returns the given value.
*/
protected function get_identity_function( $value ) {
return static function () use ( $value ) {
return $value;
};
}
}