| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Bulk_Editor\Infrastructure\Updates; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Bulk_Editor\Application\Updates\Field_Renderer_Interface; |
| 7 |
use Yoast\WP\SEO\Helpers\Meta_Helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Renders a stored meta field to its display value, resolving replacement variables the same way |
| 11 |
* the post editor does, so the bulk editor can re-score a field on the value users actually see. |
| 12 |
*/ |
| 13 |
class Field_Renderer implements Field_Renderer_Interface { |
| 14 |
|
| 15 |
/** |
| 16 |
* The meta helper. |
| 17 |
* |
| 18 |
* @var Meta_Helper |
| 19 |
*/ |
| 20 |
private $meta_helper; |
| 21 |
|
| 22 |
/** |
| 23 |
* The constructor. |
| 24 |
* |
| 25 |
* @param Meta_Helper $meta_helper The meta helper. |
| 26 |
*/ |
| 27 |
public function __construct( Meta_Helper $meta_helper ) { |
| 28 |
$this->meta_helper = $meta_helper; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Renders the current value of a meta field for a post, with replacement variables resolved. |
| 33 |
* |
| 34 |
* @param int $post_id The ID of the post. |
| 35 |
* @param string $meta_key The meta key (without prefix) to render. |
| 36 |
* |
| 37 |
* @return string The rendered value. |
| 38 |
*/ |
| 39 |
public function render( int $post_id, string $meta_key ): string { |
| 40 |
$value = (string) $this->meta_helper->get_value( $meta_key, $post_id ); |
| 41 |
|
| 42 |
return (string) \wpseo_replace_vars( $value, \get_post( $post_id ) ); |
| 43 |
} |
| 44 |
} |
| 45 |
|