| 1 |
<?php |
| 2 |
/** |
| 3 |
* Front Page Metadata Builder class |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.4.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Metadata; |
| 12 |
|
| 13 |
use Parsely\Parsely; |
| 14 |
|
| 15 |
/** |
| 16 |
* Abstract class that implements modular builders for Metadata. |
| 17 |
* |
| 18 |
* It should be extended in subclasses that implement a particular page view to render |
| 19 |
* metadata for. Specifically, the `get_metadata()` method should be implemented. |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
* @since 3.3.0 Logic extracted from Parsely\Parsely class to separate file/class. |
| 23 |
*/ |
| 24 |
abstract class Metadata_Builder { |
| 25 |
/** |
| 26 |
* Instance of Parsely class. |
| 27 |
* |
| 28 |
* @var Parsely |
| 29 |
*/ |
| 30 |
protected $parsely; |
| 31 |
|
| 32 |
/** |
| 33 |
* Array holding the metadata keys and values. |
| 34 |
* |
| 35 |
* @var array<string, mixed> |
| 36 |
*/ |
| 37 |
protected $metadata = array(); |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor. |
| 41 |
* |
| 42 |
* @param Parsely $parsely Instance of Parsely class. |
| 43 |
*/ |
| 44 |
public function __construct( Parsely $parsely ) { |
| 45 |
$this->parsely = $parsely; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Generates the metadata object by calling the build_* methods and |
| 50 |
* returns the value. |
| 51 |
* |
| 52 |
* @since 3.4.0 |
| 53 |
* |
| 54 |
* @return array<string, mixed> |
| 55 |
*/ |
| 56 |
abstract public function get_metadata(): array; |
| 57 |
|
| 58 |
/** |
| 59 |
* Builds basic metadata present in all pages. |
| 60 |
* |
| 61 |
* @since 3.4.0 |
| 62 |
*/ |
| 63 |
protected function build_basic(): void { |
| 64 |
$this->metadata['@context'] = 'https://schema.org'; |
| 65 |
$this->metadata['@type'] = 'WebPage'; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Populates the `url` field in the metadata object by getting the current page's URL. |
| 70 |
* |
| 71 |
* @since 3.4.0 |
| 72 |
*/ |
| 73 |
protected function build_url(): void { |
| 74 |
$this->metadata['url'] = $this->get_current_url(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Sanitizes string content. |
| 79 |
* |
| 80 |
* @since 2.6.0 |
| 81 |
* @since 3.4.0 Moved to class-metadata-builder |
| 82 |
* |
| 83 |
* @param string|null $val The content you'd like sanitized. |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
protected function clean_value( ?string $val ): string { |
| 87 |
if ( null === $val ) { |
| 88 |
return ''; |
| 89 |
} |
| 90 |
|
| 91 |
return trim( wp_strip_all_tags( str_replace( array( "\n", "\r" ), '', $val ) ) ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Gets the URL of the current PHP script. |
| 96 |
* |
| 97 |
* A fall-back implementation to determine permalink. |
| 98 |
* |
| 99 |
* @since 3.0.0 $parsely_type Default parameter changed to `non-post`. |
| 100 |
* @since 3.4.0 Moved to class-metadata-builder |
| 101 |
* |
| 102 |
* @param string $parsely_type Optional. Parse.ly post type you're interested in, either 'post' |
| 103 |
* or 'non-post'. Default is 'non-post'. |
| 104 |
* @param int $post_id Optional. ID of the post you want to get the URL for. Default is |
| 105 |
* 0, which means the global `$post` is used. |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
protected function get_current_url( string $parsely_type = 'non-post', int $post_id = 0 ): string { |
| 109 |
if ( 'post' === $parsely_type ) { |
| 110 |
$permalink = (string) get_permalink( $post_id ); |
| 111 |
|
| 112 |
/** |
| 113 |
* Filters the permalink for a post. |
| 114 |
* |
| 115 |
* @since 1.14.0 |
| 116 |
* @since 2.5.0 Added $post_id. |
| 117 |
* |
| 118 |
* @param string $permalink The permalink URL or false if post does not exist. |
| 119 |
* @param string $parsely_type Parse.ly type ("post" or "non-post"). |
| 120 |
* @param int $post_id ID of the post you want to get the URL for. May be 0, so |
| 121 |
* $permalink will be for the global $post. |
| 122 |
*/ |
| 123 |
$url = apply_filters( 'wp_parsely_permalink', $permalink, $parsely_type, $post_id ); |
| 124 |
} else { |
| 125 |
$request_uri = isset( $_SERVER['REQUEST_URI'] ) |
| 126 |
? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) |
| 127 |
: ''; |
| 128 |
|
| 129 |
$url = home_url( $request_uri ); |
| 130 |
} |
| 131 |
|
| 132 |
$options = $this->parsely->get_options(); |
| 133 |
return $options['force_https_canonicals'] |
| 134 |
? str_replace( 'http://', 'https://', $url ) |
| 135 |
: str_replace( 'https://', 'http://', $url ); |
| 136 |
} |
| 137 |
} |
| 138 |
|