| 1 |
<?php |
| 2 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 |
namespace Yoast\WP\SEO\Schema_Aggregator\Infrastructure; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
/** |
| 8 |
* Configuration for the Schema Aggregator. |
| 9 |
*/ |
| 10 |
class Config { |
| 11 |
|
| 12 |
/** |
| 13 |
* Default items per page |
| 14 |
* |
| 15 |
* @var int |
| 16 |
*/ |
| 17 |
private const DEFAULT_PER_PAGE = 1000; |
| 18 |
/** |
| 19 |
* Default items per page for post types that come with lots of schema pieces. |
| 20 |
* |
| 21 |
* @var int |
| 22 |
*/ |
| 23 |
private const DEFAULT_PER_PAGE_BIG_SCHEMA = 100; |
| 24 |
|
| 25 |
private const BIG_SCHEMA_POST_TYPES = [ |
| 26 |
'product', |
| 27 |
]; |
| 28 |
|
| 29 |
/** |
| 30 |
* Maximum items per page |
| 31 |
* |
| 32 |
* @var int |
| 33 |
*/ |
| 34 |
private const MAX_PER_PAGE = 1000; |
| 35 |
/** |
| 36 |
* Default cache TTL in seconds |
| 37 |
* |
| 38 |
* @var int |
| 39 |
*/ |
| 40 |
private const DEFAULT_CACHE_TTL = ( 60 * 60 ); |
| 41 |
|
| 42 |
/** |
| 43 |
* Get default items per page |
| 44 |
* |
| 45 |
* @param string $post_type The post type to determine the max page size for. |
| 46 |
* |
| 47 |
* @return int |
| 48 |
*/ |
| 49 |
public function get_per_page( string $post_type ): int { |
| 50 |
/** |
| 51 |
* Filter: 'wpseo_schema_aggregator_big_schema_post_types' Determines the list of post types we want to have a smaller per page count. |
| 52 |
* |
| 53 |
* @param bool $default_schema_post_types Determines the default list of big schema post types. |
| 54 |
*/ |
| 55 |
$big_schema_post_types = \apply_filters( 'wpseo_schema_aggregator_big_schema_post_types', self::BIG_SCHEMA_POST_TYPES ); |
| 56 |
if ( ! \is_array( $big_schema_post_types ) ) { |
| 57 |
$big_schema_post_types = self::BIG_SCHEMA_POST_TYPES; |
| 58 |
} |
| 59 |
|
| 60 |
$per_page = \in_array( $post_type, $big_schema_post_types, true ) ? $this->get_big_per_post_type() : $this->get_default_per_post_type(); |
| 61 |
|
| 62 |
if ( $per_page > self::MAX_PER_PAGE ) { |
| 63 |
$per_page = self::MAX_PER_PAGE; |
| 64 |
} |
| 65 |
|
| 66 |
return $per_page; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get maximum items per page |
| 71 |
* |
| 72 |
* @return int |
| 73 |
*/ |
| 74 |
public function get_max_per_page(): int { |
| 75 |
return self::MAX_PER_PAGE; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get expiration time based on data size. |
| 80 |
* |
| 81 |
* @param array<string> $data Data to cache. |
| 82 |
* |
| 83 |
* @return int Expiration in seconds. |
| 84 |
*/ |
| 85 |
public function get_expiration( array $data ): int { |
| 86 |
$cache_ttl = self::DEFAULT_CACHE_TTL; |
| 87 |
try { |
| 88 |
$serialized = \serialize( $data ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Needed for size calculation. |
| 89 |
|
| 90 |
$size = \strlen( $serialized ); |
| 91 |
|
| 92 |
// Large payloads: cache longer. |
| 93 |
if ( $size > 1_048_576 ) { |
| 94 |
$cache_ttl = ( 6 * \HOUR_IN_SECONDS ); |
| 95 |
} |
| 96 |
|
| 97 |
// Small payloads: cache shorter. |
| 98 |
if ( $size < 102_400 ) { |
| 99 |
$cache_ttl = ( 30 * \MINUTE_IN_SECONDS ); |
| 100 |
} |
| 101 |
|
| 102 |
$cache_ttl = \apply_filters( 'wpseo_schema_aggregator_cache_ttl', $cache_ttl ); |
| 103 |
|
| 104 |
if ( ! \is_int( $cache_ttl ) || $cache_ttl <= 0 ) { |
| 105 |
return self::DEFAULT_CACHE_TTL; |
| 106 |
} |
| 107 |
|
| 108 |
return $cache_ttl; |
| 109 |
|
| 110 |
} catch ( Exception $e ) { |
| 111 |
return self::DEFAULT_CACHE_TTL; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Check if caching is enabled. |
| 117 |
* |
| 118 |
* @return bool True if caching is enabled, false otherwise. |
| 119 |
*/ |
| 120 |
public function cache_enabled(): bool { |
| 121 |
$enabled = \apply_filters( 'wpseo_schema_aggregator_cache_enabled', true ); |
| 122 |
|
| 123 |
if ( \is_bool( $enabled ) ) { |
| 124 |
return $enabled; |
| 125 |
} |
| 126 |
else { |
| 127 |
return true; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Gets the per post type for post types with lots of schema nodes. |
| 133 |
* |
| 134 |
* @return int |
| 135 |
*/ |
| 136 |
public function get_big_per_post_type(): int { |
| 137 |
/** |
| 138 |
* Filter: 'wpseo_schema_aggregator_per_page_big' Determines the page count for post types with lots of schema nodes. |
| 139 |
* |
| 140 |
* @param bool $default_count The default amount of posts per page. |
| 141 |
*/ |
| 142 |
$per_page = (int) \apply_filters( 'wpseo_schema_aggregator_per_page_big', self::DEFAULT_PER_PAGE_BIG_SCHEMA ); |
| 143 |
|
| 144 |
if ( $per_page > 0 ) { |
| 145 |
return $per_page; |
| 146 |
} |
| 147 |
|
| 148 |
return self::DEFAULT_PER_PAGE_BIG_SCHEMA; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Gets the per page for smaller post types. |
| 153 |
* |
| 154 |
* @return int |
| 155 |
*/ |
| 156 |
public function get_default_per_post_type(): int { |
| 157 |
/** |
| 158 |
* Filter: 'wpseo_schema_aggregator_per_page' Determines the page count for post types. |
| 159 |
* |
| 160 |
* @param bool $default_count The default amount of posts per page. |
| 161 |
*/ |
| 162 |
$per_page = (int) \apply_filters( 'wpseo_schema_aggregator_per_page', self::DEFAULT_PER_PAGE ); |
| 163 |
|
| 164 |
if ( $per_page > 0 ) { |
| 165 |
return $per_page; |
| 166 |
} |
| 167 |
|
| 168 |
return self::DEFAULT_PER_PAGE; |
| 169 |
} |
| 170 |
} |
| 171 |
|