| 1 |
<?php |
| 2 |
/** |
| 3 |
* Global SEO Post Type Policy |
| 4 |
* |
| 5 |
* Single source of truth for which post types are valid Global SEO targets, |
| 6 |
* shared by the admin post-type discovery (which populates the UI), the REST |
| 7 |
* validator, and the MCP ability validator so all three agree. Previously the |
| 8 |
* UI hid ineligible types while the write paths accepted any public type, |
| 9 |
* letting direct REST/ability calls persist settings for types the UI |
| 10 |
* classifies as unsuitable. |
| 11 |
* |
| 12 |
* @package ThinkRank |
| 13 |
* @subpackage SEO |
| 14 |
* @since 1.20.1 |
| 15 |
*/ |
| 16 |
|
| 17 |
declare(strict_types=1); |
| 18 |
|
| 19 |
namespace ThinkRank\SEO; |
| 20 |
|
| 21 |
// Prevent direct access. |
| 22 |
if (!defined('ABSPATH')) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Global SEO post-type eligibility policy. |
| 28 |
* |
| 29 |
* @since 1.20.1 |
| 30 |
*/ |
| 31 |
class Global_SEO_Post_Types { |
| 32 |
|
| 33 |
/** |
| 34 |
* Whether a post type may receive Global SEO settings. |
| 35 |
* |
| 36 |
* Policy: the type must be public; a non-built-in type must also be |
| 37 |
* front-end viewable (excludes builder/utility CPTs that register |
| 38 |
* `public => true` only for previews); WordPress must actually render a page |
| 39 |
* for it; and it must not be on the filterable |
| 40 |
* `thinkrank_global_seo_excluded_post_types` deny list. |
| 41 |
* |
| 42 |
* @param \WP_Post_Type|string $post_type Post type object or name. |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
public static function is_allowed($post_type): bool { |
| 46 |
$object = is_string($post_type) ? get_post_type_object($post_type) : $post_type; |
| 47 |
|
| 48 |
if (!$object instanceof \WP_Post_Type || empty($object->public)) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
// Builder/utility CPTs that register public => true for preview purposes |
| 53 |
// but aren't front-end viewable content (e.g. Elementor's "Floating |
| 54 |
// Elements"). Built-in types (post/page) are always kept. |
| 55 |
if ($object->_builtin === false && !is_post_type_viewable($object)) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
// Attachment is built-in and public, so the rule above keeps it, but on |
| 60 |
// a site that does not serve attachment pages there is no document for |
| 61 |
// any of these settings to reach (#843). |
| 62 |
if ('attachment' === $object->name && !self::renders_a_page($object)) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
return !in_array($object->name, self::excluded_post_types($object), true); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Whether WordPress renders an HTML page for this post type on this site. |
| 71 |
* |
| 72 |
* Only `attachment` can answer no. Core has redirected attachment URLs |
| 73 |
* straight to the file since WordPress 6.4, and does so by default on a new |
| 74 |
* install: the request 301s to the uploads path, so there is no <head> for a |
| 75 |
* title, a robots meta, a canonical or JSON-LD to appear in. Every Global |
| 76 |
* SEO control for the type is therefore inert, while the screen presents |
| 77 |
* itself exactly like the ones that work. |
| 78 |
* |
| 79 |
* Read from the option core itself consults rather than hardcoded, and kept |
| 80 |
* in one place so the admin nav, the REST validator and the ability |
| 81 |
* validators cannot disagree about it. A site with attachment pages enabled |
| 82 |
* is unaffected and keeps every setting it has. |
| 83 |
* |
| 84 |
* Deliberately not applied to any other type: `is_post_type_viewable()` |
| 85 |
* already covers the general case, and this is a core quirk specific to |
| 86 |
* attachments. |
| 87 |
* |
| 88 |
* @since 2.14.0 |
| 89 |
* |
| 90 |
* @param \WP_Post_Type|string $post_type Post type object or name. |
| 91 |
* @return bool |
| 92 |
*/ |
| 93 |
public static function renders_a_page($post_type): bool { |
| 94 |
$object = is_string($post_type) ? get_post_type_object($post_type) : $post_type; |
| 95 |
|
| 96 |
if (!$object instanceof \WP_Post_Type) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
if ('attachment' !== $object->name) { |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
// The same test core's own redirect makes, in wp-includes/canonical.php: |
| 105 |
// if ( is_attachment() && ! get_option( 'wp_attachment_pages_enabled' ) ) |
| 106 |
// so this agrees with what actually happens to the request. Core has no |
| 107 |
// accessor for it; several core files read the option directly, and two |
| 108 |
// of them compare against '1' rather than testing truthiness. The |
| 109 |
// redirect uses the truthy form, and the redirect is what decides |
| 110 |
// whether a document exists, so that is the form matched here. |
| 111 |
// |
| 112 |
// The default is false because core's is: schema.php seeds a fresh |
| 113 |
// install with 0, while upgrade.php sets 1 for a site upgrading from |
| 114 |
// before 6.4, which keeps its attachment pages and is unaffected. |
| 115 |
return (bool) get_option('wp_attachment_pages_enabled', false); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Every post type that passes {@see self::is_allowed()}. |
| 120 |
* |
| 121 |
* The sitewide half of duplicate snippet detection needs the whole list |
| 122 |
* rather than one name, because a duplicate title is a duplicate whichever |
| 123 |
* post type the other page happens to be (#564). |
| 124 |
* |
| 125 |
* @since 2.10.0 |
| 126 |
* |
| 127 |
* @return string[] Post type names. |
| 128 |
*/ |
| 129 |
public static function allowed(): array { |
| 130 |
$allowed = []; |
| 131 |
|
| 132 |
foreach (get_post_types(['public' => true], 'objects') as $object) { |
| 133 |
if (self::is_allowed($object)) { |
| 134 |
$allowed[] = $object->name; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return $allowed; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Builder and utility CPTs that are not optimizable content. |
| 143 |
* |
| 144 |
* These register `public => true` for preview purposes but are layout |
| 145 |
* fragments, not pages anyone gives a title and a meta description. |
| 146 |
* |
| 147 |
* Shared rather than duplicated: the per-post SEO metabox discovers post |
| 148 |
* types by the same public/`show_ui` rule and used to keep its own |
| 149 |
* exclusion list, which named only WordPress internals. The two policies |
| 150 |
* disagreed — a builder template CPT was denied Global SEO while still |
| 151 |
* getting a full metabox — so both now read this one list (#621). |
| 152 |
* |
| 153 |
* @since 2.2.1 |
| 154 |
* |
| 155 |
* Deliberately untyped: this is passed straight to a public filter whose |
| 156 |
* existing contract accepts whatever the caller has (a `WP_Post_Type`, or |
| 157 |
* null when the list is wanted without a subject). |
| 158 |
* |
| 159 |
* @param mixed $post_type_object Post type being judged, passed to the filter. |
| 160 |
* @return string[] Post type names to exclude. |
| 161 |
*/ |
| 162 |
public static function excluded_post_types($post_type_object = null): array { |
| 163 |
// Filterable so integrators can tune it without patching core. The |
| 164 |
// post-type object is passed as the second argument (matches the admin |
| 165 |
// discovery filter usage). |
| 166 |
$excluded = apply_filters('thinkrank_global_seo_excluded_post_types', [ |
| 167 |
'elementor_library', 'oceanwp_library', 'ae_global_templates', |
| 168 |
'e-floating-buttons', 'elementor_component', |
| 169 |
// Divi: the library plus every Theme Builder template type. |
| 170 |
'et_pb_layout', 'et_theme_builder', 'et_template', |
| 171 |
'et_header_layout', 'et_body_layout', 'et_footer_layout', |
| 172 |
// Beaver Builder: saved templates (rows, columns, modules) and |
| 173 |
// Beaver Themer's layouts. Same clutter Divi's Theme Builder |
| 174 |
// templates caused before they were listed here (#449). |
| 175 |
'fl-builder-template', 'fl-theme-layout', |
| 176 |
// Bricks: saved templates plus the header/footer/section layouts |
| 177 |
// its Theme Builder stores as the same CPT (#257). |
| 178 |
'bricks_template', |
| 179 |
], $post_type_object); |
| 180 |
|
| 181 |
return array_values(array_filter((array) $excluded, 'is_string')); |
| 182 |
} |
| 183 |
} |
| 184 |
|