Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce meta description presenter #13411

Closed
wants to merge 11 commits into from
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -139,7 +139,8 @@
],
"post-install-cmd": [
"xrstf\\Composer52\\Generator::onPostInstallCmd",
"Yoast\\WP\\Free\\Composer\\Actions::prefix_dependencies"
"Yoast\\WP\\Free\\Composer\\Actions::prefix_dependencies",
"Yoast\\WP\\Free\\Composer\\Actions::compile_dependency_injection_container"
],
"post-update-cmd": [
"xrstf\\Composer52\\Generator::onPostInstallCmd"
Expand Down
5 changes: 4 additions & 1 deletion config/dependency-injection/services.php
Expand Up @@ -8,6 +8,7 @@
namespace Yoast\WP\Free\Dependency_Injection;

use Symfony\Component\DependencyInjection\Definition;
use WPSEO_Replace_Vars;
use Yoast\WP\Free\Repositories\Indexable_Repository;
use Yoast\WP\Free\Repositories\Primary_Term_Repository;
use Yoast\WP\Free\Repositories\SEO_Links_Repository;
Expand All @@ -18,7 +19,9 @@

// WordPress factory functions.
$container->register( 'wpdb', 'wpdb' )->setFactory( [ Wrapper::class, 'get_wpdb' ] );
$container->register( 'wp_query', 'WP_Query' )->setFactory( [ Wrapper::class, 'get_wp_query' ] );

// Legacy classes
$container->register( WPSEO_Replace_Vars::class, WPSEO_Replace_Vars::class )->setFactory( [ Wrapper::class, 'get_replace_vars' ] );

// Model repository factory functions.
$container->register( Indexable_Repository::class, Indexable_Repository::class )->setFactory( [ Indexable_Repository::class, 'get_instance' ] )->setAutowired( true );
Expand Down
4 changes: 3 additions & 1 deletion frontend/class-frontend.php
Expand Up @@ -11,6 +11,8 @@
*/
class WPSEO_Frontend {

const METADESC_PRIORITY = 6;

/**
* Instance of this class.
*
Expand Down Expand Up @@ -93,7 +95,7 @@ protected function __construct() {

// The head function here calls action wpseo_head, to which we hook all our functionality.
add_action( 'wpseo_head', array( $this, 'debug_mark' ), 2 );
add_action( 'wpseo_head', array( $this, 'metadesc' ), 6 );
add_action( 'wpseo_head', array( $this, 'metadesc' ), self::METADESC_PRIORITY );
add_action( 'wpseo_head', array( $this, 'robots' ), 10 );
add_action( 'wpseo_head', array( $this, 'canonical' ), 20 );
add_action( 'wpseo_head', array( $this, 'adjacent_rel_links' ), 21 );
Expand Down
21 changes: 21 additions & 0 deletions src/conditionals/front-end-conditional.php
@@ -0,0 +1,21 @@
<?php
/**
* Yoast SEO plugin file.
*
* @package Yoast\YoastSEO\Conditionals
*/

namespace Yoast\WP\Free\Conditionals;

/**
* Conditional that is only met when in the admin.
*/
class Front_End_Conditional implements Conditional {

/**
* @inheritdoc
*/
public function is_met() {
return ! \is_admin();
}
}
72 changes: 72 additions & 0 deletions src/helpers/current-post-helper.php
@@ -0,0 +1,72 @@
<?php
/**
* A helper object for WordPress posts.
*
* @package Yoast\YoastSEO\Helpers
*/

namespace Yoast\WP\Free\Helpers;

/**
* Class Current_Post_Helper
*/
class Current_Post_Helper {

/**
* Checks if the currently opened page is a simple page.
*
* @return bool Whether the currently opened page is a simple page.
*/
public function is_simple_page() {
return self::get_simple_page_id() > 0;
}

/**
* Returns the id of the currently opened page.
*
* @return int The id of the currently opened page.
*/
public function get_simple_page_id() {
if ( \is_singular() ) {
return \get_the_ID();
}

if ( self::is_posts_page() ) {
return \get_option( 'page_for_posts' );
}

/**
* Filter: Allow changing the default page id.
*
* @api int $page_id The default page id.
*/
return \apply_filters( 'wpseo_frontend_page_type_simple_page_id', 0 );
}

/**
* Determine whether this is the homepage and shows posts.
*
* @return bool Whether or not the current page is the homepage that displays posts.
*/
public function is_home_posts_page() {
return ( \is_home() && \get_option( 'show_on_front' ) === 'posts' );
}

/**
* Determine whether this is the static frontpage.
*
* @return bool Whether or not the current page is a static frontpage.
*/
public function is_home_static_page() {
return ( \is_front_page() && \get_option( 'show_on_front' ) === 'page' && \is_page( \get_option( 'page_on_front' ) ) );
}

/**
* Determine whether this is the statically set posts page, when it's not the frontpage.
*
* @return bool Whether or not it's a non-frontpage, statically set posts page.
*/
public function is_posts_page() {
return ( \is_home() && \get_option( 'show_on_front' ) === 'page' );
}
}
106 changes: 106 additions & 0 deletions src/integrations/front-end-integration.php
@@ -0,0 +1,106 @@
<?php

namespace Yoast\WP\Free\Integrations;

use Yoast\WP\Free\Conditionals\Front_End_Conditional;
use Yoast\WP\Free\Conditionals\Indexables_Feature_Flag_Conditional;
use Yoast\WP\Free\Helpers\Current_Post_Helper;
use Yoast\WP\Free\Presenters\Head_Presenter;
use Yoast\WP\Free\Repositories\Indexable_Repository;
use Yoast\WP\Free\WordPress\Integration;
use Yoast\WP\Free\Wrappers\WP_Query_Wrapper;
use Yoast\WP\Free\Presenters\Post_Type\Head_Presenter as Post_Type_Head_Presenter;
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;

class Front_End_Integration implements Integration {

/**
* @inheritDoc
*/
public static function get_conditionals() {
return [ Front_End_Conditional::class, Indexables_Feature_Flag_Conditional::class ];
}

/**
* @var Current_Post_Helper
*/
protected $current_post_helper;

/**
* @var Indexable_Repository
*/
protected $indexable_repository;

/**
* @var WP_Query_Wrapper
*/
protected $wp_query_wrapper;

/**
* @var ContainerInterface
*/
protected $container;

/**
* Front_End_Integration constructor.
*
* @param Indexable_Repository $indexable_repository
* @param Current_Post_Helper $current_post_helper
* @param WP_Query_Wrapper $wp_query_wrapper
* @param Head_Presenter $head_presenter
*/
public function __construct(
Indexable_Repository $indexable_repository,
Current_Post_Helper $current_post_helper,
WP_Query_Wrapper $wp_query_wrapper,
ContainerInterface $container
) {
$this->indexable_repository = $indexable_repository;
$this->current_post_helper = $current_post_helper;
$this->wp_query_wrapper = $wp_query_wrapper;
$this->container = $container;
}

/**
* @inheritDoc
*/
public function register_hooks() {
add_action( 'wp_head', [ $this, 'present_head' ], 1 );

remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
remove_action( 'wp_head', 'noindex', 1 );
}

/**
* Presents the head in the front-end. Resets wp_query if it's not the main query.
*/
public function present_head() {
$wp_query = $this->wp_query_wrapper->get_query();
$old_wp_query = null;

if ( ! $wp_query->is_main_query() ) {
$old_wp_query = $wp_query;
\wp_reset_query();
}

$indexable = $this->indexable_repository->for_current_page();
$this->get_head_presenter()->present( $indexable );
/**
* Action: 'wpseo_head' - Allow other plugins to output inside the Yoast SEO section of the head section.
*/
do_action( 'wpseo_head' );

if ( ! empty( $old_wp_query ) ) {
$this->wp_query_wrapper->set_query( $old_wp_query );
}
}

public function get_head_presenter() {
if ( $this->current_post_helper->is_simple_page() ) {
return $this->container->get( Post_Type_Head_Presenter::class );
}
}
}
Expand Up @@ -5,7 +5,7 @@
* @package Yoast\YoastSEO\Watchers
*/

namespace Yoast\WP\Free\Watchers;
namespace Yoast\WP\Free\Integrations\Watchers;

use Yoast\WP\Free\Conditionals\Indexables_Feature_Flag_Conditional;
use Yoast\WP\Free\Builders\Indexable_Author_Builder;
Expand Down
Expand Up @@ -5,7 +5,7 @@
* @package Yoast\YoastSEO\Watchers
*/

namespace Yoast\WP\Free\Watchers;
namespace Yoast\WP\Free\Integrations\Watchers;

use Yoast\WP\Free\Conditionals\Indexables_Feature_Flag_Conditional;
use Yoast\WP\Free\Builders\Indexable_Post_Builder;
Expand Down
Expand Up @@ -5,7 +5,7 @@
* @package Yoast\YoastSEO\Watchers
*/

namespace Yoast\WP\Free\Watchers;
namespace Yoast\WP\Free\Integrations\Watchers;

use Yoast\WP\Free\Conditionals\Indexables_Feature_Flag_Conditional;
use Yoast\WP\Free\Builders\Indexable_Term_Builder;
Expand Down
Expand Up @@ -5,7 +5,7 @@
* @package Yoast\YoastSEO\Watchers
*/

namespace Yoast\WP\Free\Watchers;
namespace Yoast\WP\Free\Integrations\Watchers;

use WPSEO_Meta;
use Yoast\WP\Free\Conditionals\Indexables_Feature_Flag_Conditional;
Expand Down
1 change: 1 addition & 0 deletions src/loader.php
Expand Up @@ -42,6 +42,7 @@ class Loader {
*/
public function __construct( ContainerInterface $container ) {
$this->container = $container;
add_filter( 'wpseo_get_class', [ $this->container, 'get' ] );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/loggers/logger.php
Expand Up @@ -12,7 +12,7 @@
use YoastSEO_Vendor\Psr\Log\NullLogger;

/**
* Creates an instance of a logger object.
* Our logger class.
*/
class Logger implements LoggerInterface {
use LoggerTrait;
Expand Down
78 changes: 63 additions & 15 deletions src/main.php
Expand Up @@ -16,23 +16,71 @@
exit();
}

$development = \defined( 'YOAST_ENVIRONMENT' ) && \YOAST_ENVIRONMENT === 'development';
if ( $development && \class_exists( '\Yoast\WP\Free\Dependency_Injection\Container_Compiler' ) ) {
// Exception here is unhandled as it will only occur in development.
Container_Compiler::compile( $development );
}
class Main {

/**
* Initializes the plugin.
*/
public function initialize() {
$dependency_management = new Dependency_Management();
$dependency_management->initialize();
$this->load();
}

/**
* Loads the plugin.
*
* @throws \Exception If loading fails and YOAST_ENVIRONMENT is development.
*/
public function load() {
try {
$container = $this->get_container();

if ( ! $container ) {
return;
}

$container->get( Loader::class )->load();
} catch ( \Exception $e ) {
if ( $this->is_development() ) {
throw $e;
}
// Don't crash the entire site, simply don't load.
// TODO: Add error notifications here.
}
}

/**
* Loads the DI container.
*
* @return null|Cached_Container The DI container.
*
* @throws \Exception If something goes wrong generating the DI container.
*/
private function get_container() {
if ( $this->is_development() && \class_exists( '\Yoast\WP\Free\Dependency_Injection\Container_Compiler' ) ) {
// Exception here is unhandled as it will only occur in development.
Container_Compiler::compile( $this->is_development() );
}

if ( \file_exists( __DIR__ . '/generated/container.php' ) ) {
require_once __DIR__ . '/generated/container.php';
$container = new Cached_Container();
try {
$container->get( Loader::class )->load();
} catch ( \Exception $e ) {
if ( $development ) {
throw $e;
if ( \file_exists( __DIR__ . '/generated/container.php' ) ) {
require_once __DIR__ . '/generated/container.php';
return new Cached_Container();
}
// Don't crash the entire site, simply don't load.
// TODO: Add error notifications here.

return null;
}

/**
* Returns whether or not we're in an environment for Yoast development.
*
* @return bool Whether or not to load in development mode.
*/
private function is_development() {
return \defined( 'YOAST_ENVIRONMENT' ) && \YOAST_ENVIRONMENT === 'development';
}
}

$main = new Main();
$main->initialize();