Merge pull request #347 from Automattic/try/posts-for-followers
Followers: use custom post types and postmeta to store
This commit is contained in:
commit
a69afb5f89
8 changed files with 331 additions and 262 deletions
|
@ -151,17 +151,6 @@ add_action(
|
|||
0
|
||||
);
|
||||
|
||||
add_action(
|
||||
'plugins_loaded',
|
||||
function() {
|
||||
if ( defined( 'WP_SWEEP_VERSION' ) ) {
|
||||
require_once \dirname( __FILE__ ) . '/integration/class-wp-sweep.php';
|
||||
Integration\Wp_Sweep::init();
|
||||
}
|
||||
},
|
||||
0
|
||||
);
|
||||
|
||||
/**
|
||||
* `get_plugin_data` wrapper
|
||||
*
|
||||
|
|
|
@ -6,11 +6,19 @@ services:
|
|||
environment:
|
||||
MYSQL_DATABASE: activitypub-test
|
||||
MYSQL_ROOT_PASSWORD: activitypub-test
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:3306"]
|
||||
interval: 5s
|
||||
timeout: 2s
|
||||
retries: 5
|
||||
|
||||
test-php:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
depends_on:
|
||||
test-db:
|
||||
condition: service_healthy
|
||||
links:
|
||||
- test-db
|
||||
volumes:
|
||||
|
|
|
@ -83,7 +83,7 @@ class Migration {
|
|||
|
||||
$follower->upsert();
|
||||
|
||||
$result = wp_set_object_terms( $user_id, $follower->get_actor(), Followers::TAXONOMY, true );
|
||||
add_post_meta( $follower->get_id(), 'user_id', $user_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ namespace Activitypub\Collection;
|
|||
|
||||
use WP_Error;
|
||||
use Exception;
|
||||
use WP_Term_Query;
|
||||
use WP_Query;
|
||||
use Activitypub\Http;
|
||||
use Activitypub\Webfinger;
|
||||
use Activitypub\Model\Activity;
|
||||
|
@ -15,10 +15,11 @@ use function Activitypub\get_remote_metadata_by_actor;
|
|||
/**
|
||||
* ActivityPub Followers Collection
|
||||
*
|
||||
* @author Matt Wiebe
|
||||
* @author Matthias Pfefferle
|
||||
*/
|
||||
class Followers {
|
||||
const TAXONOMY = 'activitypub-followers';
|
||||
const POST_TYPE = 'ap_follower';
|
||||
const CACHE_KEY_INBOXES = 'follower_inboxes_%s';
|
||||
|
||||
/**
|
||||
|
@ -27,8 +28,8 @@ class Followers {
|
|||
* @return void
|
||||
*/
|
||||
public static function init() {
|
||||
// register "followers" taxonomy
|
||||
self::register_taxonomy();
|
||||
// register "followers" post_type
|
||||
self::register_post_type();
|
||||
|
||||
\add_action( 'activitypub_inbox_follow', array( self::class, 'handle_follow_request' ), 10, 2 );
|
||||
\add_action( 'activitypub_inbox_undo', array( self::class, 'handle_undo_request' ), 10, 2 );
|
||||
|
@ -41,31 +42,26 @@ class Followers {
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function register_taxonomy() {
|
||||
$args = array(
|
||||
private static function register_post_type() {
|
||||
register_post_type(
|
||||
self::POST_TYPE,
|
||||
array(
|
||||
'labels' => array(
|
||||
'name' => _x( 'Followers', 'taxonomy general name', 'activitypub' ),
|
||||
'singular_name' => _x( 'Followers', 'taxonomy singular name', 'activitypub' ),
|
||||
'menu_name' => __( 'Followers', 'activitypub' ),
|
||||
'name' => _x( 'Followers', 'post_type plural name', 'activitypub' ),
|
||||
'singular_name' => _x( 'Follower', 'post_type single name', 'activitypub' ),
|
||||
),
|
||||
'hierarchical' => false,
|
||||
'show_ui' => false,
|
||||
'show_in_menu' => false,
|
||||
'show_in_nav_menus' => false,
|
||||
'show_admin_column' => false,
|
||||
'query_var' => false,
|
||||
'rewrite' => false,
|
||||
'public' => false,
|
||||
'capabilities' => array(
|
||||
'edit_terms' => null,
|
||||
),
|
||||
'hierarchical' => false,
|
||||
'rewrite' => false,
|
||||
'query_var' => false,
|
||||
'delete_with_user' => false,
|
||||
'can_export' => true,
|
||||
'supports' => array(),
|
||||
)
|
||||
);
|
||||
|
||||
register_taxonomy( self::TAXONOMY, 'user', $args );
|
||||
register_taxonomy_for_object_type( self::TAXONOMY, 'user' );
|
||||
|
||||
register_term_meta(
|
||||
self::TAXONOMY,
|
||||
register_post_meta(
|
||||
self::POST_TYPE,
|
||||
'name',
|
||||
array(
|
||||
'type' => 'string',
|
||||
|
@ -76,8 +72,8 @@ class Followers {
|
|||
)
|
||||
);
|
||||
|
||||
register_term_meta(
|
||||
self::TAXONOMY,
|
||||
register_post_meta(
|
||||
self::POST_TYPE,
|
||||
'username',
|
||||
array(
|
||||
'type' => 'string',
|
||||
|
@ -88,56 +84,48 @@ class Followers {
|
|||
)
|
||||
);
|
||||
|
||||
register_term_meta(
|
||||
self::TAXONOMY,
|
||||
register_post_meta(
|
||||
self::POST_TYPE,
|
||||
'avatar',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => true,
|
||||
'sanitize_callback' => function( $value ) {
|
||||
if ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return esc_url_raw( $value );
|
||||
},
|
||||
'sanitize_callback' => array( self::class, 'sanitize_url' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_term_meta(
|
||||
self::TAXONOMY,
|
||||
register_post_meta(
|
||||
self::POST_TYPE,
|
||||
'url',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => false,
|
||||
'sanitize_callback' => array( self::class, 'sanitize_url' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_post_meta(
|
||||
self::POST_TYPE,
|
||||
'inbox',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => true,
|
||||
'sanitize_callback' => function( $value ) {
|
||||
if ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
|
||||
throw new Exception( '"inbox" has to be a valid URL' );
|
||||
}
|
||||
|
||||
return esc_url_raw( $value );
|
||||
},
|
||||
'sanitize_callback' => array( self::class, 'sanitize_url' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_term_meta(
|
||||
self::TAXONOMY,
|
||||
register_post_meta(
|
||||
self::POST_TYPE,
|
||||
'shared_inbox',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => true,
|
||||
'sanitize_callback' => function( $value ) {
|
||||
if ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return esc_url_raw( $value );
|
||||
},
|
||||
'sanitize_callback' => array( self::class, 'sanitize_url' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_term_meta(
|
||||
self::TAXONOMY,
|
||||
register_post_meta(
|
||||
self::POST_TYPE,
|
||||
'updated_at',
|
||||
array(
|
||||
'type' => 'string',
|
||||
|
@ -152,8 +140,8 @@ class Followers {
|
|||
)
|
||||
);
|
||||
|
||||
register_term_meta(
|
||||
self::TAXONOMY,
|
||||
register_post_meta(
|
||||
self::POST_TYPE,
|
||||
'errors',
|
||||
array(
|
||||
'type' => 'string',
|
||||
|
@ -168,7 +156,15 @@ class Followers {
|
|||
)
|
||||
);
|
||||
|
||||
do_action( 'activitypub_after_register_taxonomy' );
|
||||
do_action( 'activitypub_after_register_post_type' );
|
||||
}
|
||||
|
||||
public static function sanitize_url( $value ) {
|
||||
if ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return esc_url_raw( $value );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -221,14 +217,14 @@ class Followers {
|
|||
$follower->from_meta( $meta );
|
||||
$follower->upsert();
|
||||
|
||||
$result = wp_set_object_terms( $user_id, $follower->get_actor(), self::TAXONOMY, true );
|
||||
$meta = get_post_meta( $follower->get_id(), 'user_id' );
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
return $result;
|
||||
} else {
|
||||
if ( is_array( $meta ) && ! in_array( $user_id, $meta, true ) ) {
|
||||
add_post_meta( $follower->get_id(), 'user_id', $user_id );
|
||||
wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
|
||||
return $follower;
|
||||
}
|
||||
|
||||
return $follower;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -241,11 +237,18 @@ class Followers {
|
|||
*/
|
||||
public static function remove_follower( $user_id, $actor ) {
|
||||
wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
|
||||
return wp_remove_object_terms( $user_id, $actor, self::TAXONOMY );
|
||||
|
||||
$follower = self::get_follower( $user_id, $actor );
|
||||
|
||||
if ( ! $follower ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return delete_post_meta( $follower->get_id(), 'user_id', $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a Follower
|
||||
* Get a Follower
|
||||
*
|
||||
* @param int $user_id The ID of the WordPress User
|
||||
* @param string $actor The Actor URL
|
||||
|
@ -253,21 +256,22 @@ class Followers {
|
|||
* @return \Activitypub\Model\Follower The Follower object
|
||||
*/
|
||||
public static function get_follower( $user_id, $actor ) {
|
||||
$terms = new WP_Term_Query(
|
||||
global $wpdb;
|
||||
|
||||
$post_id = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id WHERE p.post_type = %s AND pm.meta_key = 'user_id' AND pm.meta_value = %d AND p.guid = %s",
|
||||
array(
|
||||
'name' => $actor,
|
||||
'taxonomy' => self::TAXONOMY,
|
||||
'hide_empty' => false,
|
||||
'object_ids' => $user_id,
|
||||
'number' => 1,
|
||||
esc_sql( self::POST_TYPE ),
|
||||
esc_sql( $user_id ),
|
||||
esc_sql( $actor ),
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$term = $terms->get_terms();
|
||||
|
||||
if ( is_array( $term ) && ! empty( $term ) ) {
|
||||
$term = reset( $term );
|
||||
return new Follower( $term->name );
|
||||
if ( $post_id ) {
|
||||
$post = get_post( $post_id );
|
||||
return new Follower( $post );
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -321,21 +325,25 @@ class Followers {
|
|||
*/
|
||||
public static function get_followers( $user_id, $number = null, $offset = null, $args = array() ) {
|
||||
$defaults = array(
|
||||
'taxonomy' => self::TAXONOMY,
|
||||
'hide_empty' => false,
|
||||
'object_ids' => $user_id,
|
||||
'number' => $number,
|
||||
'post_type' => self::POST_TYPE,
|
||||
'posts_per_page' => $number,
|
||||
'offset' => $offset,
|
||||
'orderby' => 'id',
|
||||
'order' => 'ASC',
|
||||
'orderby' => 'ID',
|
||||
'order' => 'DESC',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'user_id',
|
||||
'value' => $user_id,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
$terms = new WP_Term_Query( $args );
|
||||
$query = new WP_Query( $args );
|
||||
$items = array();
|
||||
|
||||
foreach ( $terms->get_terms() as $follower ) {
|
||||
$items[] = new Follower( $follower->name ); // phpcs:ignore
|
||||
foreach ( $query->get_posts() as $post ) {
|
||||
$items[] = new Follower( $post ); // phpcs:ignore
|
||||
}
|
||||
|
||||
return $items;
|
||||
|
@ -344,20 +352,15 @@ class Followers {
|
|||
/**
|
||||
* Get all Followers
|
||||
*
|
||||
* @param array $args The WP_Term_Query arguments.
|
||||
* @param array $args The WP_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,
|
||||
public static function get_all_followers( $user_id = null ) {
|
||||
$args = array(
|
||||
'meta_query' => array(),
|
||||
);
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
$terms = new WP_Term_Query( $args );
|
||||
|
||||
return $terms->get_terms();
|
||||
return self::get_followers( $user_id, null, null, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -368,7 +371,20 @@ class Followers {
|
|||
* @return int The number of Followers
|
||||
*/
|
||||
public static function count_followers( $user_id ) {
|
||||
return count( self::get_followers( $user_id ) );
|
||||
$query = new WP_Query(
|
||||
array(
|
||||
'post_type' => self::POST_TYPE,
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'user_id',
|
||||
'value' => $user_id,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
return $query->found_posts;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -387,35 +403,37 @@ class Followers {
|
|||
}
|
||||
|
||||
// get all Followers of a ID of the WordPress User
|
||||
$terms = new WP_Term_Query(
|
||||
$posts = new WP_Query(
|
||||
array(
|
||||
'taxonomy' => self::TAXONOMY,
|
||||
'hide_empty' => false,
|
||||
'object_ids' => $user_id,
|
||||
'post_type' => self::POST_TYPE,
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'inbox',
|
||||
'compare' => 'EXISTS',
|
||||
),
|
||||
array(
|
||||
'key' => 'user_id',
|
||||
'value' => $user_id,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$terms = $terms->get_terms();
|
||||
$posts = $posts->get_posts();
|
||||
|
||||
if ( ! $terms ) {
|
||||
if ( ! $posts ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$results = $wpdb->get_col(
|
||||
$wpdb->prepare(
|
||||
"SELECT DISTINCT meta_value FROM {$wpdb->termmeta}
|
||||
WHERE term_id IN (" . implode( ', ', array_fill( 0, count( $terms ), '%d' ) ) . ")
|
||||
"SELECT DISTINCT meta_value FROM {$wpdb->postmeta}
|
||||
WHERE post_id IN (" . implode( ', ', array_fill( 0, count( $posts ), '%d' ) ) . ")
|
||||
AND meta_key = 'shared_inbox'
|
||||
AND meta_value IS NOT NULL",
|
||||
$terms
|
||||
$posts
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -436,26 +454,24 @@ class Followers {
|
|||
*/
|
||||
public static function get_outdated_followers( $number = 50, $older_than = 604800 ) {
|
||||
$args = array(
|
||||
'taxonomy' => self::TAXONOMY,
|
||||
'number' => $number,
|
||||
'meta_key' => 'updated_at',
|
||||
'orderby' => 'meta_value_num',
|
||||
'post_type' => self::POST_TYPE,
|
||||
'posts_per_page' => $number,
|
||||
'orderby' => 'modified',
|
||||
'order' => 'DESC',
|
||||
'meta_query' => array(
|
||||
'post_status' => 'any', // 'any' includes 'trash
|
||||
'date_query' => array(
|
||||
array(
|
||||
'key' => 'updated_at',
|
||||
'value' => time() - $older_than,
|
||||
'type' => 'numeric',
|
||||
'compare' => '<=',
|
||||
'column' => 'post_modified_gmt',
|
||||
'before' => gmdate( 'Y-m-d', \time() - $older_than ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$terms = new WP_Term_Query( $args );
|
||||
$posts = new WP_Query( $args );
|
||||
$items = array();
|
||||
|
||||
foreach ( $terms->get_terms() as $follower ) {
|
||||
$items[] = new Follower( $follower->name ); // phpcs:ignore
|
||||
foreach ( $posts->get_posts() as $follower ) {
|
||||
$items[] = new Follower( $follower ); // phpcs:ignore
|
||||
}
|
||||
|
||||
return $items;
|
||||
|
@ -471,8 +487,8 @@ class Followers {
|
|||
*/
|
||||
public static function get_faulty_followers( $number = 10 ) {
|
||||
$args = array(
|
||||
'taxonomy' => self::TAXONOMY,
|
||||
'number' => $number,
|
||||
'post_type' => self::POST_TYPE,
|
||||
'posts_per_page' => $number,
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'errors',
|
||||
|
@ -481,11 +497,11 @@ class Followers {
|
|||
),
|
||||
);
|
||||
|
||||
$terms = new WP_Term_Query( $args );
|
||||
$posts = new WP_Query( $args );
|
||||
$items = array();
|
||||
|
||||
foreach ( $terms->get_terms() as $follower ) {
|
||||
$items[] = new Follower( $follower->name ); // phpcs:ignore
|
||||
foreach ( $posts->get_posts() as $follower ) {
|
||||
$items[] = new Follower( $follower ); // phpcs:ignore
|
||||
}
|
||||
|
||||
return $items;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
namespace Activitypub\Model;
|
||||
|
||||
use WP_Query;
|
||||
use Activitypub\Collection\Followers;
|
||||
|
||||
/**
|
||||
|
@ -9,6 +10,7 @@ use Activitypub\Collection\Followers;
|
|||
* This Object represents a single Follower.
|
||||
* There is no direct reference to a WordPress User here.
|
||||
*
|
||||
* @author Matt Wiebe
|
||||
* @author Matthias Pfefferle
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitypub/#follow-activity-inbox
|
||||
|
@ -28,16 +30,6 @@ class Follower {
|
|||
*/
|
||||
private $actor;
|
||||
|
||||
/**
|
||||
* The Object slug
|
||||
*
|
||||
* This is a requirement of the Term-Meta but will not
|
||||
* be actively used in the ActivityPub context.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $slug;
|
||||
|
||||
/**
|
||||
* The Object Name
|
||||
*
|
||||
|
@ -108,6 +100,12 @@ class Follower {
|
|||
*/
|
||||
private $errors;
|
||||
|
||||
/**
|
||||
* The WordPress User ID, or 0 for whole site.
|
||||
* @var int
|
||||
*/
|
||||
private $user_id;
|
||||
|
||||
/**
|
||||
* Maps the meta fields to the local db fields
|
||||
*
|
||||
|
@ -122,17 +120,35 @@ class Follower {
|
|||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param WP_Post $post
|
||||
* @param string|WP_Post $actor The Actor-URL or WP_Post Object.
|
||||
* @param int $user_id The WordPress User ID. 0 Represents the whole site.
|
||||
*/
|
||||
public function __construct( $actor ) {
|
||||
$post = null;
|
||||
|
||||
if ( \is_a( $actor, 'WP_Post' ) ) {
|
||||
$post = $actor;
|
||||
} else {
|
||||
global $wpdb;
|
||||
|
||||
$post_id = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT ID FROM $wpdb->posts WHERE guid=%s",
|
||||
esc_sql( $actor )
|
||||
)
|
||||
);
|
||||
|
||||
if ( $post_id ) {
|
||||
$post = get_post( $post_id );
|
||||
} else {
|
||||
$this->actor = $actor;
|
||||
}
|
||||
}
|
||||
|
||||
$term = get_term_by( 'name', $actor, Followers::TAXONOMY );
|
||||
|
||||
if ( $term ) {
|
||||
$this->id = $term->term_id;
|
||||
$this->slug = $term->slug;
|
||||
$this->meta = json_decode( $term->meta );
|
||||
if ( $post ) {
|
||||
$this->id = $post->ID;
|
||||
$this->actor = $post->guid;
|
||||
$this->updated_at = $post->post_modified;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -205,20 +221,21 @@ class Follower {
|
|||
* @return mixed The attribute value.
|
||||
*/
|
||||
public function get( $attribute ) {
|
||||
if ( $this->$attribute ) {
|
||||
if ( ! is_null( $this->$attribute ) ) {
|
||||
return $this->$attribute;
|
||||
}
|
||||
|
||||
$attribute = get_term_meta( $this->id, $attribute, true );
|
||||
if ( $attribute ) {
|
||||
$this->$attribute = $attribute;
|
||||
return $attribute;
|
||||
$attribute_value = get_post_meta( $this->id, $attribute, true );
|
||||
|
||||
if ( $attribute_value ) {
|
||||
$this->$attribute = $attribute_value;
|
||||
return $attribute_value;
|
||||
}
|
||||
|
||||
$attribute = $this->get_meta_by( $attribute );
|
||||
if ( $attribute ) {
|
||||
$this->$attribute = $attribute;
|
||||
return $attribute;
|
||||
$attribute_value = $this->get_meta_by( $attribute );
|
||||
if ( $attribute_value ) {
|
||||
$this->$attribute = $attribute_value;
|
||||
return $attribute_value;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -246,7 +263,7 @@ class Follower {
|
|||
return $this->errors;
|
||||
}
|
||||
|
||||
$this->errors = get_term_meta( $this->id, 'errors' );
|
||||
$this->errors = get_post_meta( $this->id, 'errors' );
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
|
@ -256,7 +273,7 @@ class Follower {
|
|||
* @return void
|
||||
*/
|
||||
public function reset_errors() {
|
||||
delete_term_meta( $this->id, 'errors' );
|
||||
delete_post_meta( $this->id, 'errors' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -298,7 +315,9 @@ class Follower {
|
|||
*/
|
||||
public function get_meta_by( $attribute ) {
|
||||
$meta = $this->get_meta();
|
||||
|
||||
if ( ! is_array( $meta ) ) {
|
||||
return null;
|
||||
}
|
||||
// try mapped data (see $this->map_meta)
|
||||
foreach ( $this->map_meta as $remote => $local ) {
|
||||
if ( $attribute === $local && isset( $meta[ $remote ] ) ) {
|
||||
|
@ -306,11 +325,6 @@ class Follower {
|
|||
}
|
||||
}
|
||||
|
||||
// try ActivityPub attribtes
|
||||
if ( ! empty( $this->map_meta[ $attribute ] ) ) {
|
||||
return $this->map_meta[ $attribute ];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -333,16 +347,8 @@ class Follower {
|
|||
* @return void
|
||||
*/
|
||||
public function update() {
|
||||
$term = wp_update_term(
|
||||
$this->id,
|
||||
Followers::TAXONOMY,
|
||||
array(
|
||||
'description' => wp_json_encode( $this->get_meta( true ) ),
|
||||
)
|
||||
);
|
||||
|
||||
$this->updated_at = \time();
|
||||
$this->update_term_meta();
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -351,18 +357,19 @@ class Follower {
|
|||
* @return void
|
||||
*/
|
||||
public function save() {
|
||||
$term = wp_insert_term(
|
||||
$this->actor,
|
||||
Followers::TAXONOMY,
|
||||
array(
|
||||
'slug' => sanitize_title( $this->get_actor() ),
|
||||
'description' => wp_json_encode( $this->get_meta() ),
|
||||
)
|
||||
$args = array(
|
||||
'ID' => $this->id,
|
||||
'guid' => $this->actor,
|
||||
'post_title' => $this->get_name(),
|
||||
'post_author' => 0,
|
||||
'post_type' => Followers::POST_TYPE,
|
||||
'post_content' => wp_json_encode( $this->meta ),
|
||||
'post_status' => 'publish',
|
||||
'post_modified' => gmdate( 'Y-m-d H:i:s', $this->updated_at ),
|
||||
'meta_input' => $this->get_post_meta_input(),
|
||||
);
|
||||
|
||||
$this->id = $term['term_id'];
|
||||
|
||||
$this->update_term_meta();
|
||||
$post_id = wp_insert_post( $args );
|
||||
$this->id = $post_id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -384,20 +391,22 @@ class Follower {
|
|||
* @return void
|
||||
*/
|
||||
public function delete() {
|
||||
wp_delete_term( $this->id, Followers::TAXONOMY );
|
||||
wp_delete_post( $this->id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the term meta.
|
||||
* Update the post meta.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function update_term_meta() {
|
||||
$attributes = array( 'inbox', 'shared_inbox', 'avatar', 'updated_at', 'name', 'username' );
|
||||
protected function get_post_meta_input() {
|
||||
$attributes = array( 'inbox', 'shared_inbox', 'avatar', 'name', 'username' );
|
||||
|
||||
$meta_input = array();
|
||||
|
||||
foreach ( $attributes as $attribute ) {
|
||||
if ( $this->get( $attribute ) ) {
|
||||
update_term_meta( $this->id, $attribute, $this->get( $attribute ) );
|
||||
$meta_input[ $attribute ] = $this->get( $attribute );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -410,7 +419,9 @@ class Follower {
|
|||
$error = __( 'Unknown Error or misconfigured Error-Message', 'activitypub' );
|
||||
}
|
||||
|
||||
add_term_meta( $this->id, 'errors', $error );
|
||||
}
|
||||
$meta_input['errors'] = array( $error );
|
||||
}
|
||||
|
||||
return $meta_input;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ class Followers extends WP_List_Table {
|
|||
'name' => \__( 'Name', 'activitypub' ),
|
||||
'username' => \__( 'Username', 'activitypub' ),
|
||||
'identifier' => \__( 'Identifier', 'activitypub' ),
|
||||
'updated_at' => \__( 'Last updated', 'activitypub' ),
|
||||
'errors' => \__( 'Errors', 'activitypub' ),
|
||||
'latest-error' => \__( 'Latest Error Message', 'activitypub' ),
|
||||
);
|
||||
|
@ -35,7 +36,7 @@ class Followers extends WP_List_Table {
|
|||
$page_num = $this->get_pagenum();
|
||||
$per_page = 20;
|
||||
|
||||
$follower = FollowerCollection::get_followers( \get_current_user_id(), $per_page, ( $page_num - 1 ) * $per_page );
|
||||
$followers = FollowerCollection::get_followers( \get_current_user_id(), $per_page, ( $page_num - 1 ) * $per_page );
|
||||
$counter = FollowerCollection::count_followers( \get_current_user_id() );
|
||||
|
||||
$this->items = array();
|
||||
|
@ -47,12 +48,13 @@ class Followers extends WP_List_Table {
|
|||
)
|
||||
);
|
||||
|
||||
foreach ( $follower as $follower ) {
|
||||
foreach ( $followers as $follower ) {
|
||||
$item = array(
|
||||
'avatar' => esc_attr( $follower->get_avatar() ),
|
||||
'name' => esc_attr( $follower->get_name() ),
|
||||
'username' => esc_attr( $follower->get_username() ),
|
||||
'identifier' => esc_attr( $follower->get_actor() ),
|
||||
'updated_at' => esc_attr( $follower->get_updated_at() ),
|
||||
'errors' => $follower->count_errors(),
|
||||
'latest-error' => $follower->get_latest_error_message(),
|
||||
);
|
||||
|
@ -110,7 +112,12 @@ class Followers extends WP_List_Table {
|
|||
|
||||
switch ( $this->current_action() ) {
|
||||
case 'delete':
|
||||
FollowerCollection::remove_follower( \get_current_user_id(), $followers );
|
||||
if ( ! is_array( $followers ) ) {
|
||||
$followers = array( $followers );
|
||||
}
|
||||
foreach ( $followers as $follower ) {
|
||||
FollowerCollection::remove_follower( \get_current_user_id(), $follower );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
<?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 );
|
||||
}
|
||||
}
|
|
@ -31,6 +31,12 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
|
|||
'name' => '12345',
|
||||
'prefferedUsername' => '12345',
|
||||
),
|
||||
'user2@example.com' => array(
|
||||
'url' => 'https://user2.example.com',
|
||||
'inbox' => 'https://user2.example.com/inbox',
|
||||
'name' => 'user2',
|
||||
'prefferedUsername' => 'user2',
|
||||
),
|
||||
);
|
||||
|
||||
public function set_up() {
|
||||
|
@ -65,7 +71,7 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
|
|||
$db_followers
|
||||
);
|
||||
|
||||
$this->assertSame( array( 'https://example.com/author/jon', 'https://example.org/author/doe', 'http://sally.example.org' ), $db_followers );
|
||||
$this->assertEquals( array( 'http://sally.example.org', 'https://example.org/author/doe', 'https://example.com/author/jon' ), $db_followers );
|
||||
}
|
||||
|
||||
public function test_add_follower() {
|
||||
|
@ -73,15 +79,21 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
|
|||
add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 );
|
||||
|
||||
$follower = 'https://12345.example.com';
|
||||
$follower2 = 'https://user2.example.com';
|
||||
\Activitypub\Collection\Followers::add_follower( 1, $follower );
|
||||
\Activitypub\Collection\Followers::add_follower( 2, $follower );
|
||||
\Activitypub\Collection\Followers::add_follower( 2, $follower2 );
|
||||
|
||||
$db_followers = \Activitypub\Collection\Followers::get_followers( 1 );
|
||||
$db_followers2 = \Activitypub\Collection\Followers::get_followers( 2 );
|
||||
|
||||
$this->assertContains( $follower, $db_followers );
|
||||
$this->assertContains( $follower2, $db_followers2 );
|
||||
}
|
||||
|
||||
public function test_get_follower() {
|
||||
$followers = array( 'https://example.com/author/jon' );
|
||||
$followers2 = array( 'https://user2.example.com' );
|
||||
|
||||
$pre_http_request = new MockAction();
|
||||
add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 );
|
||||
|
@ -90,11 +102,66 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
|
|||
\Activitypub\Collection\Followers::add_follower( 1, $follower );
|
||||
}
|
||||
|
||||
foreach ( $followers2 as $follower ) {
|
||||
\Activitypub\Collection\Followers::add_follower( 2, $follower );
|
||||
}
|
||||
|
||||
$follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://example.com/author/jon' );
|
||||
$this->assertEquals( 'https://example.com/author/jon', $follower->get_actor() );
|
||||
|
||||
$follower = \Activitypub\Collection\Followers::get_follower( 1, 'http://sally.example.org' );
|
||||
$this->assertNull( $follower );
|
||||
|
||||
$follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://user2.example.com' );
|
||||
$this->assertNull( $follower );
|
||||
|
||||
$follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://example.com/author/jon' );
|
||||
$this->assertEquals( 'https://example.com/author/jon', $follower->get_actor() );
|
||||
|
||||
$follower2 = \Activitypub\Collection\Followers::get_follower( 2, 'https://user2.example.com' );
|
||||
$this->assertEquals( 'https://user2.example.com', $follower2->get_actor() );
|
||||
}
|
||||
|
||||
public function test_delete_follower() {
|
||||
$followers = array(
|
||||
'https://example.com/author/jon',
|
||||
'https://example.org/author/doe',
|
||||
);
|
||||
$followers2 = array( 'https://user2.example.com' );
|
||||
|
||||
$pre_http_request = new MockAction();
|
||||
add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 );
|
||||
|
||||
foreach ( $followers as $follower ) {
|
||||
\Activitypub\Collection\Followers::add_follower( 1, $follower );
|
||||
\Activitypub\Collection\Followers::add_follower( 1, $follower );
|
||||
\Activitypub\Collection\Followers::add_follower( 1, $follower );
|
||||
\Activitypub\Collection\Followers::add_follower( 2, $follower );
|
||||
}
|
||||
|
||||
foreach ( $followers2 as $follower2 ) {
|
||||
\Activitypub\Collection\Followers::add_follower( 2, $follower2 );
|
||||
}
|
||||
|
||||
$follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://example.com/author/jon' );
|
||||
$this->assertEquals( 'https://example.com/author/jon', $follower->get_actor() );
|
||||
|
||||
$followers = \Activitypub\Collection\Followers::get_followers( 1 );
|
||||
$this->assertEquals( 2, count( $followers ) );
|
||||
|
||||
$follower2 = \Activitypub\Collection\Followers::get_follower( 2, 'https://example.com/author/jon' );
|
||||
$this->assertEquals( 'https://example.com/author/jon', $follower2->get_actor() );
|
||||
|
||||
\Activitypub\Collection\Followers::remove_follower( 1, 'https://example.com/author/jon' );
|
||||
|
||||
$follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://example.com/author/jon' );
|
||||
$this->assertNull( $follower );
|
||||
|
||||
$follower2 = \Activitypub\Collection\Followers::get_follower( 2, 'https://example.com/author/jon' );
|
||||
$this->assertEquals( 'https://example.com/author/jon', $follower2->get_actor() );
|
||||
|
||||
$followers = \Activitypub\Collection\Followers::get_followers( 1 );
|
||||
$this->assertEquals( 1, count( $followers ) );
|
||||
}
|
||||
|
||||
public function test_get_outdated_followers() {
|
||||
|
@ -109,7 +176,28 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
|
|||
|
||||
$follower = new \Activitypub\Model\Follower( 'https://example.com/author/jon' );
|
||||
|
||||
update_term_meta( $follower->get_id(), 'updated_at', \time() - 804800 );
|
||||
global $wpdb;
|
||||
|
||||
//eg. time one year ago..
|
||||
$time = time() - 804800;
|
||||
$mysql_time_format = 'Y-m-d H:i:s';
|
||||
|
||||
$post_modified = gmdate( $mysql_time_format, $time );
|
||||
$post_modified_gmt = gmdate( $mysql_time_format, ( $time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
|
||||
$post_id = $follower->get_id();
|
||||
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"UPDATE $wpdb->posts SET post_modified = %s, post_modified_gmt = %s WHERE ID = %s",
|
||||
array(
|
||||
$post_modified,
|
||||
$post_modified_gmt,
|
||||
$post_id,
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
clean_post_cache( $post_id );
|
||||
|
||||
$followers = \Activitypub\Collection\Followers::get_outdated_followers();
|
||||
$this->assertEquals( 1, count( $followers ) );
|
||||
|
@ -129,7 +217,7 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
|
|||
$follower = new \Activitypub\Model\Follower( 'http://sally.example.org' );
|
||||
|
||||
for ( $i = 1; $i <= 15; $i++ ) {
|
||||
add_term_meta( $follower->get_id(), 'errors', 'error ' . $i );
|
||||
add_post_meta( $follower->get_id(), 'errors', 'error ' . $i );
|
||||
}
|
||||
|
||||
$follower = new \Activitypub\Model\Follower( 'http://sally.example.org' );
|
||||
|
|
Loading…
Reference in a new issue