Opened 6 years ago
Last modified 6 years ago
#51318 new enhancement
WP_Sitemaps_Provider protected $name property
| Reported by: | Tkama | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Sitemaps | Version: | 5.5.1 |
| Severity: | major | Keywords: | |
| Cc: | Focuses: |
Description
See: https://developer.wordpress.org/reference/classes/wp_sitemaps_provider/
Why We need $name property to be protected? It is just name. This is inconvenient and can cause bugs. For example I create my Sitemaps Provider like this:
<?php add_filter( 'init', 'wpkama_register_sitemap_providers' ) function wpkama_register_sitemap_providers(){ require_once __DIR__ .'/class-Similar_Posts_Sitemaps_Provider.php'; $provider = new Similar_Posts_Sitemaps_Provider(); wp_register_sitemap_provider( 'similarposts', $provider ); }
And I can't get the value of $name property like this:
<?php $provider = new Similar_Posts_Sitemaps_Provider(); wp_register_sitemap_provider( $provider->name, $provider );
But if first argument of wp_register_sitemap_provider() function and the $name prop are different the Sitemap Provider won't run at all!
So all I can to do is copy $name prop value and past it as parameter. And if I will change the name I need to remember to copy because they must be the same (and code itself doesn't tell me this dependency).
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
While I'm not sure why it's marked protected, I'm just noting that if you want to make the
$nameproperty public, you can do so in the subclassed provider as PHP allows you to change the visibility, for example: https://3v4l.org/QIVCgYou could also call
wp_register_sitemap_provider( $this->name, $this )within the class through something like$provider = new Similar_Posts_Sitemaps_Provider(); $provider->register_provider();I believe.