Merge pull request #340 from Automattic/fix/wp-sweep

prevent sweeping of followers taxonomies
This commit is contained in:
Matthias Pfefferle 2023-05-22 14:34:04 +02:00 committed by GitHub
commit 45ab002550
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 5 deletions

View file

@ -27,6 +27,7 @@ function init() {
\defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=<p>)|(?<=<br>)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' );
\defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' );
\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<strong>[ap_title]</strong>\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" );
\define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) );
@ -139,11 +140,25 @@ function plugin_settings_link( $actions ) {
/**
* Only load code that needs BuddyPress to run once BP is loaded and initialized.
*/
function enable_buddypress_features() {
add_action(
'bp_include',
function() {
require_once \dirname( __FILE__ ) . '/integration/class-buddypress.php';
Integration\Buddypress::init();
},
0
);
add_action(
'plugins_loaded',
function() {
if ( defined( 'WP_SWEEP_VERSION' ) ) {
require_once \dirname( __FILE__ ) . '/integration/class-wp-sweep.php';
Integration\Wp_Sweep::init();
}
add_action( 'bp_include', __NAMESPACE__ . '\enable_buddypress_features' );
},
0
);
/**
* `get_plugin_data` wrapper

View file

@ -338,6 +338,25 @@ class Followers {
return $items;
}
/**
* Get all Followers
*
* @param array $args The WP_Term_Query arguments.
*
* @return array The Term list of Followers.
*/
public static function get_all_followers( $args = array() ) {
$defaults = array(
'taxonomy' => self::TAXONOMY,
'hide_empty' => false,
);
$args = wp_parse_args( $args, $defaults );
$terms = new WP_Term_Query( $args );
return $terms->get_terms();
}
/**
* Count the total number of followers
*

View file

@ -0,0 +1,50 @@
<?php
namespace Activitypub\Integration;
use Activitypub\Collection\Followers;
/**
* Manages the compatibility with WP Sweep.
*
* @link https://wordpress.org/plugins/wp-sweep/
* @link https://github.com/polylang/polylang/tree/master/integrations/wp-sweep
*/
class Wp_Sweep {
/**
* Setups actions.
*
* @return void
*/
public static function init() {
add_filter( 'wp_sweep_excluded_taxonomies', array( self::class, 'excluded_taxonomies' ) );
add_filter( 'wp_sweep_excluded_termids', array( self::class, 'excluded_termids' ), 0 );
}
/**
* Add 'activitypub-followers' to excluded taxonomies otherwise terms loose their language
* and translation group.
*
* @param array $excluded_taxonomies List of taxonomies excluded from sweeping.
*
* @return array The list of taxonomies excluded from sweeping.
*/
public static function excluded_taxonomies( $excluded_taxonomies ) {
return array_merge( $excluded_taxonomies, array( Followers::TAXONOMY ) );
}
/**
* Add the translation of the default taxonomy terms and our language terms to the excluded terms.
*
* @param array $excluded_term_ids List of term ids excluded from sweeping.
*
* @return array The list of term ids excluded from sweeping.
*/
public static function excluded_termids( $excluded_term_ids ) {
// We got a list of excluded terms (defaults and parents). Let exclude their translations too.
$followers = Followers::get_all_followers( array( 'fields' => 'ids' ) );
$excluded_term_ids = array_merge( $excluded_term_ids, $followers );
return array_unique( $excluded_term_ids );
}
}