Polylang > Blog > Let’s OOP with YOAST: don’t inherit from classes you don’t own

Let’s OOP with YOAST: don’t inherit from classes you don’t own

Sylvain Schellenberger

Polylang

1 January 1970

Yoast SEO 14.0 RC2: Abstract indexable presenter

/**
* Class Abstract_Indexable_Presenter
*/
abstract class Abstract_Indexable_Presenter {

/**
* The WPSEO Replace Vars object.
*
* @var WPSEO_Replace_Vars
*/
public $replace_vars;

/**
* The indexable presentation.
*
* @var Indexable_Presentation
*/
public $presentation;

/**
* The helpers surface
*
* @var Helpers_Surface
*/
public $helpers;

/**
* Presents a presentation.
*
* @codeCoverageIgnore There is nothing to test.
*
* @return string The template.
*/
public abstract function present();

/**
* Replace replacement variables in a string.
*
* @param string $string The string.
*
* @codeCoverageIgnore Wrapper method.
*
* @return string The string with replacement variables replaced.
*/
protected function replace_vars( $string ) {
return $this->replace_vars->replace( $string, $this->presentation->source );
}
}

Polylang presenter


final class PLL_WPSEO_OGP extends Abstract_Indexable_Presenter {
	/**
	 * Facebook locale
	 *
	 * @var string $locale
	 */
	private $locale;

	/**
	 * Constructor
	 *
	 * @since 2.7
	 *
	 * @param string $locale Facebook locale.
	 */
	public function __construct( $locale ) {
		$this->locale = $locale;
	}

	/**
	 * Returns the meta Opengraph alternate locale meta tag
	 *
	 * @since 2.7
	 *
	 * @return string
	 */
	public function present() {
		return sprintf( '', esc_attr( $this->locale ) );
	}
}

Yoast SEO 14.0 RC3 Abstract indexable presenter:


/**
 * Class Abstract_Indexable_Presenter
 */
abstract class Abstract_Indexable_Presenter {

	/**
	 * The WPSEO Replace Vars object.
	 *
	 * @var WPSEO_Replace_Vars
	 */
	public $replace_vars;

	/**
	 * The indexable presentation.
	 *
	 * @var Indexable_Presentation
	 */
	public $presentation;

	/**
	 * The helpers surface
	 *
	 * @var Helpers_Surface
	 */
	public $helpers;

	/**
	 * Presents a presentation.
	 *
	 * @codeCoverageIgnore There is nothing to test.
	 *
	 * @return string The template.
	 */
	public abstract function present();

	/**
	 * Gets the raw value of a presentation.
	 *
	 * @return string|array The raw value.
	 */
	public abstract function get();

	/**
	 * Replace replacement variables in a string.
	 *
	 * @param string $string The string.
	 *
	 * @codeCoverageIgnore Wrapper method.
	 *
	 * @return string The string with replacement variables replaced.
	 */
	protected function replace_vars( $string ) {
		return $this->replace_vars->replace( $string, $this->presentation->source );
	}
}

Polylang Locale_Presenter Decorator:


class PLL_WPSEO_Locale_Alternates_Presenter
{

    /**
     * PLL_WPSEO_Locale_With_Alternate_Presenter constructor.
     * @param mixed|YoastWPSEOPresentersOpen_GraphLocale_Presenter $presenter
     */
    public function __construct(YoastWPSEOPresentersOpen_GraphLocale_Presenter $presenter, $alternate_locales)
    {
    	$this->presenter = $presenter;
    	$this->alternate_locales = $alternate_locales;
    }

	public function __get($name)
	{
		return $this->presenter->$name;
	}

	public function __call($name, $arguments)
	{
		return call_user_func_array($this->presenter->$name, $arguments);
	}

	public function present() {
    	$presentation = $this->presenter->get();
    	foreach( $this->alternate_locales as $alternate_locale ) {
    		$presentation .= sprintf( '', esc_attr( $alternate_locale ) );
		}
	}


}

Yoast filters the presenters: here


		$presenter_instances = array_filter( $presenter_instances, function ( $presenter_instance ) {
			return $presenter_instance instanceof Abstract_Indexable_Presenter;
		} );

Author

Sylvain Schellenberger