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

Issue-99: Prevent Alley Users from Appearing On the Site #108

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
ae56b67
WIP: issue-99
anubisthejackle Aug 27, 2024
9f96109
Merge branch 'main' into feature/issue-99/prevent-alley-users-from-ap…
anubisthejackle Oct 25, 2024
aaa1e79
Merge branch 'main' into feature/issue-99/prevent-alley-users-from-ap…
anubisthejackle Oct 25, 2024
58e87de
Scaffold initial feature, and build search method to search for users…
anubisthejackle Oct 25, 2024
8a06a6c
Do domain check against strings instead of database query.
anubisthejackle Oct 28, 2024
622a45d
404 author archives for filtered accounts
anubisthejackle Oct 28, 2024
ac009a4
Consolidate user accounts to setup method
anubisthejackle Oct 28, 2024
33af499
Separate tests by product
anubisthejackle Oct 28, 2024
03d1087
Add properties to define if Byline Manager and / or Co-Authors Plus a…
anubisthejackle Oct 28, 2024
097b857
Filter the author name and the author link
anubisthejackle Oct 28, 2024
ed5c11b
Add a filter to allow loading Alleyvate features by environment type
anubisthejackle Oct 28, 2024
70d0482
Add test to confirm ability to disable features based on environment.
anubisthejackle Oct 28, 2024
f81e58f
Define the WP_ENVIRONMENT_TYPE to be development for PHPUnit
anubisthejackle Oct 28, 2024
6b7bf30
Test to confirm we can filter based on environment type, as well as b…
anubisthejackle Oct 28, 2024
135ab45
Make environment hook dynamic to restrict it to a specific feature
anubisthejackle Oct 28, 2024
f2e3263
Add ability to filter the allowed environments for this feature
anubisthejackle Oct 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<testsuite name="unit">
<directory suffix="Test.php">tests/Alley/WP/</directory>
</testsuite>
<php>
<env name="WP_ENVIRONMENT_TYPE" value="development" force="true"/>
</php>
</phpunit>
12 changes: 12 additions & 0 deletions src/alley/wp/alleyvate/class-feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public function filtered_boot(): void {
*/
$load = apply_filters( "alleyvate_load_{$this->handle}", $load );

/**
* Filters whether to load the given Alleyvate feature.
*
* The dynamic portion of the hook name, `$this->$this->handle`, refers to the
* machine name for the feature. Filtering in this instance is based on the
* secondary parameter, environment,which should be used to determine whether
* to load or not.
*
* @param bool $load Whether to load the feature. Default true.
*/
$load = apply_filters( "alleyvate_load_{$this->handle}_in_environment", $load, wp_get_environment_type() );

if ( $load ) {
$this->booted = true;
$this->origin->boot();
Expand Down
173 changes: 173 additions & 0 deletions src/alley/wp/alleyvate/features/class-disable-alley-authors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* Class file for Disable_Alley_Authors
*
* (c) Alley <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package wp-alleyvate
*/

namespace Alley\WP\Alleyvate\Features;

use Alley\WP\Types\Feature;

/**
* Removes the impact of Alley accounts on the frontend of client websites by:
* - Ensuring Alley users do not have author archive pages.
* - Ensuring Byline Manager and Co-Authors Plus profiles linked to Alley users do not have author archive pages.
* - Filtering Alley account usernames to display as "Staff" on the frontend.
*/
final class Disable_Alley_Authors implements Feature {

/**
* Add an early hook to decide if this feature should load or not, based on the environment.
*/
public function __construct() {
add_filter( 'alleyvate_load_disable_alley_authors_in_environment', [ self::class, 'restrict_to_environment' ], 999, 2 );
}

/**
* Accepts whether or not the feature should load, as well as the current environment,
* to allow for disabling this feature on certain environments.
*
* @param bool $load Whether or not to load the feature.
* @param string $environment The loaded environment.
* @return bool
*/
public static function restrict_to_environment( $load, $environment ): bool {
if ( ! $load ) {
return $load;
}

$allowed_environments = apply_filters( 'alleyvate_disable_alley_authors_environments', [ 'production' ] );

return in_array( $environment, $allowed_environments, true );
}

/**
* Boot the feature.
*/
public function boot(): void {
add_action( 'template_include', [ self::class, 'disable_staff_archives' ], 999 );
add_filter( 'get_the_author_display_name', [ self::class, 'filter__get_the_author_display_name' ], 999, 2 );
add_filter( 'author_link', [ self::class, 'filter__author_link' ], 999, 2 );
}

/**
* Filters the author archive URL.
*
* @param string $link The author link.
* @param int $author_id The author ID.
* @return string
*/
public static function filter__author_link( $link, $author_id ): string {
if ( ! is_singular() ) {
return $link;
}

$author = get_user_by( 'ID', $author_id );
if ( ! self::is_staff_author( $author->user_email ) ) {
return $link;
}

return get_home_url();
}

/**
* Action fired once the post data has been set up. Passes the post and query by
* reference, so we can filter the objects directly.
*
* @param string $display_name The current post we are filtering.
* @param int $author_id The author ID.
* @return string
*/
public static function filter__get_the_author_display_name( $display_name, $author_id ): string {
$author = get_user_by( 'ID', $author_id );

if ( ! self::is_staff_author( $author->user_email ) ) {
return $display_name;
}

return __( 'Staff', 'alley' );
}

/**
* Disable author archives for Alley--and other "general staff"--accounts.
*
* @param string $template The template currently being included.
* @return string The template to ultimately include.
*/
public static function disable_staff_archives( string $template ): string {
global $wp_query;
// If this isn't an author archive, skip it.
if ( ! is_author() ) {
return $template;
}

$author = $wp_query->get_queried_object();

if ( ! self::is_staff_author( $author->user_email ) ) {
return $template;
}
$wp_query->set_404();
status_header( 404 );
nocache_headers();
return get_404_template();
}

/**
* Generate an array of authors in the database that are to be defined as "Staff"
* authors and not attributable authors.
*
* @param string $email The email address to compare against.
* @return int[]
*/
public static function is_staff_author( string $email ): bool {
/**
* Filters which domains to use for defining staff users in the user database.
*
* @param string[] $domains The array of domains. Defaults to alley domains.
*/
$domains = apply_filters(
'alleyvate_staff_author_domains',
[
'alley.com',
'alley.co',
'local.test',
]
);

$domains = array_map(
/**
* Force domains to take the format of an email domain, to avoid
* false positives like `[email protected]` which is a valid
* email.
*
* @param string $domain The domain to filter.
* @return string The filtered domain value.
*/
function ( $domain ) {
if ( trim( $domain, '@' ) !== $domain ) {
return $domain;
}

return '@' . $domain;
},
$domains
);

return (bool) array_reduce(
$domains,
function ( $carry, $domain ) use ( $email ) {
if ( ! $carry ) {
$carry = str_contains( $email, $domain );
}
return $carry;
},
false
);
}
}
4 changes: 4 additions & 0 deletions src/alley/wp/alleyvate/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ function load(): void {
'disable_block_editor_rest_api_preload_paths',
new Features\Disable_Block_Editor_Rest_Api_Preload_Paths(),
),
new Feature(
'disable_alley_authors',
new Features\Disable_Alley_Authors(),
),
);

$plugin->boot();
Expand Down
Loading