fix follower creation

This commit is contained in:
Matthias Pfefferle 2023-06-15 11:48:09 +02:00
parent 8b7744a5ea
commit fc0fc295bb

View file

@ -1,6 +1,7 @@
<?php <?php
namespace Activitypub\Model; namespace Activitypub\Model;
use WP_Query;
use Activitypub\Collection\Followers; use Activitypub\Collection\Followers;
/** /**
@ -29,16 +30,6 @@ class Follower {
*/ */
private $actor; 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 * The Object Name
* *
@ -138,20 +129,32 @@ class Follower {
* @param string|WP_Post $actor The Actor-URL or WP_Post Object. * @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. * @param int $user_id The WordPress User ID. 0 Represents the whole site.
*/ */
public function __construct( $actor, $user_id = 0 ) { public function __construct( $actor ) {
$this->user_id = $user_id; $post = null;
if ( is_a( $actor, 'WP_Post' ) ) {
if ( \is_a( $actor, 'WP_Post' ) ) {
$post = $actor; $post = $actor;
$this->actor = $post->post_name;
$this->user_id = $post->post_author;
} else { } else {
$this->actor = $actor; global $wpdb;
$post = Followers::get_follower( $user_id, $actor );
$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;
}
} }
if ( $post ) { if ( $post ) {
$this->id = $term->post_id; $this->id = $post->ID;
$this->slug = $term->post_name; $this->actor = $post->guid;
$this->updated_at = $post->post_modified;
} }
} }
@ -227,7 +230,9 @@ class Follower {
if ( ! is_null( $this->$attribute ) ) { if ( ! is_null( $this->$attribute ) ) {
return $this->$attribute; return $this->$attribute;
} }
$attribute_value = get_post_meta( $this->id, $attribute, true ); $attribute_value = get_post_meta( $this->id, $attribute, true );
if ( $attribute_value ) { if ( $attribute_value ) {
$this->$attribute = $attribute_value; $this->$attribute = $attribute_value;
return $attribute_value; return $attribute_value;
@ -288,7 +293,7 @@ class Follower {
* @return void * @return void
*/ */
public function reset_errors() { public function reset_errors() {
delete_term_meta( $this->id, 'errors' ); delete_post_meta( $this->id, 'errors' );
} }
/** /**
@ -363,7 +368,7 @@ class Follower {
*/ */
public function update() { public function update() {
$this->updated_at = \time(); $this->updated_at = \time();
$this->save( $this->id ); $this->save();
} }
/** /**
@ -371,16 +376,19 @@ class Follower {
* *
* @return void * @return void
*/ */
public function save( $post_id = null ) { public function save() {
$args = array( $args = array(
'ID' => $post_id, 'ID' => $this->id,
'post_name' => $this->actor, 'guid' => $this->actor,
'post_author' => $this->user_id, 'post_title' => $this->get_name(),
'post_type' => Followers::POST_TYPE, 'post_author' => 0,
'meta_input' => $this->get_post_meta_input(), 'post_type' => Followers::POST_TYPE,
'post_content' => wp_json_encode( $this->meta ),
'post_modified' => gmdate( 'Y-m-d H:i:s', $this->updated_at ),
'meta_input' => $this->get_post_meta_input(),
); );
$post = wp_insert_post( $args ); $post_id = wp_insert_post( $args );
$this->id = $post->ID; $this->id = $post_id;
} }
/** /**
@ -406,12 +414,12 @@ class Follower {
} }
/** /**
* Update the term meta. * Update the post meta.
* *
* @return void * @return void
*/ */
protected function get_post_meta_input() { protected function get_post_meta_input() {
$attributes = array( 'inbox', 'shared_inbox', 'avatar', 'updated_at', 'name', 'username', 'url' ); $attributes = array( 'inbox', 'shared_inbox', 'avatar', 'name', 'username' );
$meta_input = array(); $meta_input = array();