Skip to content

Commit

Permalink
Archive: Redirect default archive page to custom one (GoogleForCreato…
Browse files Browse the repository at this point in the history
…rs#9221)

Co-authored-by: Pascal Birchler <[email protected]>
  • Loading branch information
spacedmonkey and swissspidy authored Oct 12, 2021
1 parent bb9128f commit c280df1
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 58 deletions.
59 changes: 56 additions & 3 deletions includes/Story_Post_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* Class Story_Post_Type.
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class Story_Post_Type extends Service_Base implements PluginDeactivationAware, SiteInitializationAware, HasRequirements {
use Post_Type;
Expand Down Expand Up @@ -78,17 +79,26 @@ class Story_Post_Type extends Service_Base implements PluginDeactivationAware, S
*/
private $settings;

/**
* Experiments instance.
*
* @var Experiments Experiments instance.
*/
private $experiments;

/**
* Analytics constructor.
*
* @since 1.12.0
*
* @param Settings $settings Settings instance.
* @param Settings $settings Settings instance.
* @param Experiments $experiments Experiments instance.
*
* @return void
*/
public function __construct( Settings $settings ) {
$this->settings = $settings;
public function __construct( Settings $settings, Experiments $experiments ) {
$this->settings = $settings;
$this->experiments = $experiments;
}

/**
Expand All @@ -110,6 +120,7 @@ public function register() {
add_filter( 'wp_insert_post_data', [ $this, 'change_default_title' ] );
add_filter( 'bulk_post_updated_messages', [ $this, 'bulk_post_updated_messages' ], 10, 2 );
add_action( 'clean_post_cache', [ $this, 'clear_user_posts_count' ], 10, 2 );
add_filter( 'pre_handle_404', [ $this, 'redirect_post_type_archive_urls' ], 10, 2 );

add_action( 'add_option_' . $this->settings::SETTING_NAME_ARCHIVE, [ $this, 'update_archive_setting' ] );
add_action( 'update_option_' . $this->settings::SETTING_NAME_ARCHIVE, [ $this, 'update_archive_setting' ] );
Expand Down Expand Up @@ -349,6 +360,44 @@ public function change_default_title( $data ) {
return $data;
}

/**
* Handles redirects to the post type archive.
*
* @since 1.13.0
*
* @param bool|mixed $bypass Pass-through of the pre_handle_404 filter value.
* @param \WP_Query $query The WP_Query object.
* @return bool|mixed Whether to pass-through or not.
*/
public function redirect_post_type_archive_urls( $bypass, $query ) {
global $wp_rewrite;

if ( ! $this->experiments->is_experiment_enabled( 'archivePageCustomization' ) ) {
return $bypass;
}

if ( $bypass || ! is_string( $this->get_has_archive() ) || ( ! $wp_rewrite instanceof \WP_Rewrite || ! $wp_rewrite->using_permalinks() ) ) {
return $bypass;
}

// 'pagename' is for most permalink types, name is for when the %postname% is used as a top-level field.
if ( self::REWRITE_SLUG === $query->get( 'pagename' ) || self::REWRITE_SLUG === $query->get( 'name' ) ) {
$redirect_url = get_post_type_archive_link( self::POST_TYPE_SLUG );

if ( ! $redirect_url ) {
return $bypass;
}

// Only exit if there was actually a location to redirect to.
// Allows filtering location in tests to verify behavior.
if ( wp_safe_redirect( $redirect_url, 301 ) ) {
exit;
}
}

return $bypass;
}

/**
* Invalid cache.
*
Expand Down Expand Up @@ -392,6 +441,10 @@ public function update_archive_setting() {
* @return bool|string Whether the post type should have an archive, or archive slug.
*/
private function get_has_archive() {
if ( ! $this->experiments->is_experiment_enabled( 'archivePageCustomization' ) ) {
return true;
}

$archive_page_option = $this->settings->get_setting( $this->settings::SETTING_NAME_ARCHIVE );
$custom_archive_page_id = (int) $this->settings->get_setting( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID );
$has_archive = true;
Expand Down
6 changes: 4 additions & 2 deletions tests/phpunit/integration/includes/Kses_Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ trait Kses_Setup {
* Setup KSES init class.
*/
protected function kses_int() {
$this->kses = new \Google\Web_Stories\KSES(
new \Google\Web_Stories\Story_Post_Type( new \Google\Web_Stories\Settings() )
$settings = new \Google\Web_Stories\Settings();
$experiments = new \Google\Web_Stories\Experiments( $settings );
$this->kses = new \Google\Web_Stories\KSES(
new \Google\Web_Stories\Story_Post_Type( $settings, $experiments )
);
$this->kses->register();
}
Expand Down
9 changes: 7 additions & 2 deletions tests/phpunit/integration/tests/REST_API/Embed_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Google\Web_Stories\Tests\Integration\REST_API;

use Google\Web_Stories\Experiments;
use Google\Web_Stories\Settings;
use Google\Web_Stories\Story_Post_Type;
use Google\Web_Stories\Tests\Integration\Test_REST_TestCase;
Expand Down Expand Up @@ -297,11 +298,13 @@ public function test_local_url_pretty_permalinks() {

$this->set_permalink_structure( '/%postname%/' );

$settings = new Settings();

// Without (re-)registering the post type here there won't be any rewrite rules for it
// and get_permalink() will return "http://example.org/?web-story=embed-controller-test-story"
// instead of "http://example.org/web-stories/embed-controller-test-story/".
// @todo Investigate why this is needed (leakage between tests?)
$story_post_type = new Story_Post_Type( new Settings() );
$story_post_type = new Story_Post_Type( $settings, new Experiments( $settings ) );
$story_post_type->register();

flush_rewrite_rules( false );
Expand Down Expand Up @@ -329,11 +332,13 @@ public function test_local_url_pretty_permalinks_multisite() {

$this->set_permalink_structure( '/%postname%/' );

$settings = new Settings();

// Without (re-)registering the post type here there won't be any rewrite rules for it
// and get_permalink() will return "http://example.org/?web-story=embed-controller-test-story"
// instead of "http://example.org/web-stories/embed-controller-test-story/".
// @todo Investigate why this is needed (leakage between tests?).
$story_post_type = new Story_Post_Type( new Settings() );
$story_post_type = new Story_Post_Type( $settings, new Experiments( $settings ) );
$story_post_type->register();

flush_rewrite_rules( false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Google\Web_Stories\Tests\Integration\REST_API;

use Google\Web_Stories\Experiments;
use Google\Web_Stories\Settings;
use Google\Web_Stories\Tests\Integration\Test_REST_TestCase;

Expand Down Expand Up @@ -84,8 +85,8 @@ public function test_register() {
*/
public function test_count_user_posts() {
$this->controller->register();

$post_type = new \Google\Web_Stories\Story_Post_Type( new Settings() );
$settings = new Settings();
$post_type = new \Google\Web_Stories\Story_Post_Type( $settings, new Experiments( $settings ) );
$post_type->register();

$result1 = $this->call_private_method(
Expand Down Expand Up @@ -138,7 +139,8 @@ public function test_count_user_posts_invalid() {
$this->controller->register();

$controller = new \Google\Web_Stories\REST_API\Stories_Users_Controller();
$post_type = new \Google\Web_Stories\Story_Post_Type( new Settings() );
$settings = new Settings();
$post_type = new \Google\Web_Stories\Story_Post_Type( $settings, new Experiments( $settings ) );
$post_type->register();
$result1 = $this->call_private_method(
$controller,
Expand Down
Loading

0 comments on commit c280df1

Please sign in to comment.