2018-09-23 23:29:20 +00:00
|
|
|
<?php
|
|
|
|
namespace followers;
|
|
|
|
|
|
|
|
require_once plugin_dir_path( __FILE__ ) . '/actors.php';
|
|
|
|
require_once plugin_dir_path( __FILE__ ) . '/objects.php';
|
|
|
|
|
|
|
|
function add_follower( $actor_slug, $follower ) {
|
|
|
|
global $wpdb;
|
|
|
|
$actor_id = \actors\get_actor_id( $actor_slug );
|
|
|
|
if ( !$actor_id ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'not_found',
|
|
|
|
__( 'Actor not found', 'pterotype' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( !array_key_exists( 'id', $follower ) ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_object',
|
|
|
|
__( 'Object must have an "id" field', 'pterotype' ),
|
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$object_id = \objects\get_object_id( $follower['id'] );
|
|
|
|
if ( !$object_id ) {
|
|
|
|
$row = \objects\upsert_object( $follower );
|
|
|
|
$object_id = $row->id;
|
|
|
|
}
|
|
|
|
$wpdb->insert(
|
2018-09-26 03:46:31 +00:00
|
|
|
'pterotype_followers',
|
|
|
|
array(
|
|
|
|
'actor_id' => $actor_id,
|
2018-09-26 04:07:41 +00:00
|
|
|
'object_id' => $object_id,
|
|
|
|
)
|
2018-09-26 03:46:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_follower( $actor_slug, $follower ) {
|
|
|
|
global $wpdb;
|
|
|
|
$actor_id = \actors\get_actor_id( $actor_slug );
|
|
|
|
if ( !$actor_id ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'not_found',
|
|
|
|
__( 'Actor not found', 'pterotype' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( !array_key_exists( 'id', $follower ) ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_object',
|
|
|
|
__( 'Object must have an "id" field', 'pterotype' ),
|
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$object_id = \objects\get_object_id( $follower['id'] );
|
|
|
|
if ( !$object_id ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'not_found',
|
|
|
|
__( 'Object not found', 'pterotype' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$wpdb->delete(
|
2018-09-23 23:29:20 +00:00
|
|
|
'pterotype_followers',
|
|
|
|
array(
|
|
|
|
'actor_id' => $actor_id,
|
2018-09-26 04:07:41 +00:00
|
|
|
'object_id' => $object_id,
|
|
|
|
)
|
2018-09-23 23:29:20 +00:00
|
|
|
);
|
|
|
|
}
|
2018-09-24 12:51:50 +00:00
|
|
|
|
|
|
|
function get_followers_collection( $actor_slug ) {
|
|
|
|
global $wpdb;
|
|
|
|
$actor_id = \actors\get_actor_id( $actor_slug );
|
|
|
|
if ( !$actor_id ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'not_found',
|
|
|
|
__( 'Actor not found', 'pterotype' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$followers = $wpdb->get_results(
|
|
|
|
$wpdb->prepare(
|
|
|
|
'
|
|
|
|
SELECT object FROM pterotype_followers
|
|
|
|
JOIN pterotype_objects ON object_id = pterotype_objects.id
|
|
|
|
WHERE actor_id = %d
|
|
|
|
',
|
|
|
|
$actor_id
|
|
|
|
),
|
|
|
|
ARRAY_A
|
|
|
|
);
|
|
|
|
if ( !$followers ) {
|
|
|
|
$followers = array();
|
|
|
|
}
|
|
|
|
$collection = \collections\make_ordered_collection( $followers );
|
|
|
|
$collection['id'] = get_rest_url( null, sprintf(
|
|
|
|
'/pterotype/v1/actor/%s/followers', $actor_slug
|
|
|
|
) );
|
|
|
|
return $collection;
|
|
|
|
}
|
2018-09-23 23:29:20 +00:00
|
|
|
?>
|