-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathclass-factory.php
More file actions
125 lines (112 loc) · 3.29 KB
/
class-factory.php
File metadata and controls
125 lines (112 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Transformer Factory Class file.
*
* @package Activitypub
*/
namespace Activitypub\Transformer;
use Activitypub\Activity\Base_Object;
use Activitypub\Comment as Comment_Helper;
use Activitypub\Http;
use function Activitypub\get_user_id;
use function Activitypub\is_post_disabled;
use function Activitypub\user_can_activitypub;
/**
* Transformer Factory.
*/
class Factory {
/**
* Get the transformer for a given object.
*
* @param mixed $data The object to transform.
*
* @return Base|\WP_Error The transformer to use, or an error.
*/
public static function get_transformer( $data ) {
// Early return for WP_Error objects.
if ( \is_wp_error( $data ) ) {
return $data;
}
if ( \is_string( $data ) && \filter_var( $data, FILTER_VALIDATE_URL ) ) {
$response = Http::get_remote_object( $data );
if ( \is_wp_error( $response ) ) {
return $response;
}
$class = 'json';
$data = $response;
} elseif ( \is_array( $data ) || \is_string( $data ) ) {
$class = 'json';
} elseif ( \is_object( $data ) ) {
$class = \get_class( $data );
} else {
return new \WP_Error( 'invalid_object', __( 'Invalid object', 'activitypub' ) );
}
/**
* Filter the transformer for a given object.
*
* Add your own transformer based on the object class or the object type.
*
* Example usage:
*
* // Filter be object class
* add_filter( 'activitypub_transformer', function( $transformer, $object, $object_class ) {
* if ( $object_class === 'WP_Post' ) {
* return new My_Post_Transformer( $object );
* }
* return $transformer;
* }, 10, 3 );
*
* // Filter be object type
* add_filter( 'activitypub_transformer', function( $transformer, $object, $object_class ) {
* if ( $object->post_type === 'event' ) {
* return new My_Event_Transformer( $object );
* }
* return $transformer;
* }, 10, 3 );
*
* @param null|Base $transformer The transformer to use. Default null.
* @param mixed $data The object to transform.
* @param string $object_class The class of the object to transform.
*
* @return mixed The transformer to use.
*/
$transformer = \apply_filters( 'activitypub_transformer', null, $data, $class );
if ( $transformer ) {
if (
! \is_object( $transformer ) ||
! $transformer instanceof Base
) {
return new \WP_Error( 'invalid_transformer', __( 'Invalid transformer', 'activitypub' ) );
}
return $transformer;
}
// Use default transformer.
switch ( $class ) {
case 'WP_Post':
if ( 'attachment' === $data->post_type && ! is_post_disabled( $data ) ) {
return new Attachment( $data );
} elseif ( ! is_post_disabled( $data ) && get_user_id( $data->post_author ) ) {
return new Post( $data );
}
break;
case 'WP_Comment':
if ( Comment_Helper::should_be_federated( $data ) ) {
return new Comment( $data );
}
break;
case 'WP_User':
if ( user_can_activitypub( $data->ID ) ) {
return new User( $data );
}
break;
case 'WP_Term':
return new Tag( $data );
case 'json':
return new Json( $data );
}
if ( $data instanceof Base_Object ) {
return new Activity_Object( $data );
}
return new \WP_Error( 'invalid_object', __( 'Invalid object', 'activitypub' ) );
}
}