@@ -68,7 +68,7 @@ function safe_remote_get( $url, $user_id ) {
6868 $ wp_version = \get_bloginfo ( 'version ' );
6969 $ user_agent = \apply_filters ( 'http_headers_useragent ' , 'WordPress/ ' . $ wp_version . '; ' . \get_bloginfo ( 'url ' ) );
7070 $ args = array (
71- 'timeout ' => 100 ,
71+ 'timeout ' => apply_filters ( ' activitypub_remote_get_timeout ' , 100 ) ,
7272 'limit_response_size ' => 1048576 ,
7373 'redirection ' => 3 ,
7474 'user-agent ' => "$ user_agent; ActivityPub " ,
@@ -110,8 +110,8 @@ function get_remote_metadata_by_actor( $actor ) {
110110 if ( $ pre ) {
111111 return $ pre ;
112112 }
113- if ( preg_match ( '/^@?[^@]+@((?:[a-z0-9-]+\.)+[a-z]+) $/i ' , $ actor ) ) {
114- $ actor = \ Activitypub \ Webfinger::resolve ( $ actor );
113+ if ( preg_match ( '/^@? ' . ACTIVITYPUB_USERNAME_REGEXP . ' $/i ' , $ actor ) ) {
114+ $ actor = Webfinger::resolve ( $ actor );
115115 }
116116
117117 if ( ! $ actor ) {
@@ -122,41 +122,50 @@ function get_remote_metadata_by_actor( $actor ) {
122122 return $ actor ;
123123 }
124124
125- $ metadata = \get_transient ( 'activitypub_ ' . $ actor );
125+ $ transient_key = 'activitypub_ ' . $ actor ;
126+ $ metadata = \get_transient ( $ transient_key );
126127
127128 if ( $ metadata ) {
128129 return $ metadata ;
129130 }
130131
131132 if ( ! \wp_http_validate_url ( $ actor ) ) {
132- return new \WP_Error ( 'activitypub_no_valid_actor_url ' , \__ ( 'The "actor" is no valid URL ' , 'activitypub ' ), $ actor );
133+ $ metadata = new \WP_Error ( 'activitypub_no_valid_actor_url ' , \__ ( 'The "actor" is no valid URL ' , 'activitypub ' ), $ actor );
134+ \set_transient ( $ transient_key , $ metadata , HOUR_IN_SECONDS ); // Cache the error for a shorter period.
135+ return $ metadata ;
133136 }
134137
135138 $ user = \get_users (
136139 array (
137140 'number ' => 1 ,
138- 'who ' => ' authors ' ,
141+ 'capability__in ' => array ( ' publish_posts ' ) ,
139142 'fields ' => 'ID ' ,
140143 )
141144 );
142145
143146 // we just need any user to generate a request signature
144147 $ user_id = \reset ( $ user );
145-
148+ $ short_timeout = function () {
149+ return 3 ;
150+ };
151+ add_filter ( 'activitypub_remote_get_timeout ' , $ short_timeout );
146152 $ response = \Activitypub \safe_remote_get ( $ actor , $ user_id );
147-
153+ remove_filter ( ' activitypub_remote_get_timeout ' , $ short_timeout );
148154 if ( \is_wp_error ( $ response ) ) {
155+ \set_transient ( $ transient_key , $ response , HOUR_IN_SECONDS ); // Cache the error for a shorter period.
149156 return $ response ;
150157 }
151158
152159 $ metadata = \wp_remote_retrieve_body ( $ response );
153160 $ metadata = \json_decode ( $ metadata , true );
154161
155162 if ( ! $ metadata ) {
156- return new \WP_Error ( 'activitypub_invalid_json ' , \__ ( 'No valid JSON data ' , 'activitypub ' ), $ actor );
163+ $ metadata = new \WP_Error ( 'activitypub_invalid_json ' , \__ ( 'No valid JSON data ' , 'activitypub ' ), $ actor );
164+ \set_transient ( $ transient_key , $ metadata , HOUR_IN_SECONDS ); // Cache the error for a shorter period.
165+ return $ metadata ;
157166 }
158167
159- \set_transient ( ' activitypub_ ' . $ actor , $ metadata , WEEK_IN_SECONDS );
168+ \set_transient ( $ transient_key , $ metadata , WEEK_IN_SECONDS );
160169
161170 return $ metadata ;
162171}
0 commit comments