wp_rewrite_rules(); // not using rewrite rules, and 'author=N' method failed, so we're out of options if ( empty( $rewrite ) ) { return 0; } // generate rewrite rule for the author url $author_rewrite = $wp_rewrite->get_author_permastruct(); $author_regexp = \str_replace( '%author%', '', $author_rewrite ); // match the rewrite rule with the passed url if ( \preg_match( '/https?:\/\/(.+)' . \preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) { $user = \get_user_by( 'slug', $match[2] ); if ( $user ) { return $user->ID; } } return 0; } /** * Check for Tombstone Objects * * @see https://www.w3.org/TR/activitypub/#delete-activity-outbox * * @param WP_Error $wp_error A WP_Error-Response of an HTTP-Request * * @return boolean true if HTTP-Code is 410 or 404 */ function is_tombstone( $wp_error ) { if ( ! is_wp_error( $wp_error ) ) { return false; } if ( in_array( (int) $wp_error->get_error_code(), array( 404, 410 ), true ) ) { return true; } return false; } /** * Get the REST URL relative to this plugin's namespace. * * @param string $path Optional. REST route path. Otherwise this plugin's namespaced root. * * @return string REST URL relative to this plugin's namespace. */ function get_rest_url_by_path( $path = '' ) { // we'll handle the leading slash. $path = ltrim( $path, '/' ); $namespaced_path = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path ); return \get_rest_url( null, $namespaced_path ); } /** * Convert a string from camelCase to snake_case. * * @param string $string The string to convert. * * @return string The converted string. */ function camel_to_snake_case( $string ) { return strtolower( preg_replace( '/(?query_vars['activitypub'] ) ) { return true; } /* * The other (more common) option to make an ActivityPub request * is to send an Accept header. */ if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) { $accept = sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) ); /* * $accept can be a single value, or a comma separated list of values. * We want to support both scenarios, * and return true when the header includes at least one of the following: * - application/activity+json * - application/ld+json */ if ( preg_match( '/(application\/(ld\+json|activity\+json))/', $accept ) ) { return true; } } return false; } /** * This function checks if a user is disabled for ActivityPub. * * @param int $user_id The User-ID. * * @return boolean True if the user is disabled, false otherwise. */ function is_user_disabled( $user_id ) { $return = false; switch ( $user_id ) { // if the user is the application user, it's always enabled. case \Activitypub\Collection\Users::APPLICATION_USER_ID: $return = false; break; // if the user is the blog user, it's only enabled in single-user mode. case \Activitypub\Collection\Users::BLOG_USER_ID: if ( is_user_type_disabled( 'blog' ) ) { $return = true; break; } $return = false; break; // if the user is any other user, it's enabled if it can publish posts. default: if ( ! \get_user_by( 'id', $user_id ) ) { $return = true; break; } if ( is_user_type_disabled( 'user' ) ) { $return = true; break; } if ( ! \user_can( $user_id, 'publish_posts' ) ) { $return = true; break; } $return = false; break; } return apply_filters( 'activitypub_is_user_disabled', $return, $user_id ); } /** * Checks if a User-Type is disabled for ActivityPub. * * This function is used to check if the 'blog' or 'user' * type is disabled for ActivityPub. * * @param enum $type Can be 'blog' or 'user'. * * @return boolean True if the user type is disabled, false otherwise. */ function is_user_type_disabled( $type ) { switch ( $type ) { case 'blog': if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) { if ( ACTIVITYPUB_SINGLE_USER_MODE ) { $return = false; break; } } if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) ) { $return = ACTIVITYPUB_DISABLE_BLOG_USER; break; } if ( '1' !== \get_option( 'activitypub_enable_blog_user', '0' ) ) { $return = true; break; } $return = false; break; case 'user': if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) { if ( ACTIVITYPUB_SINGLE_USER_MODE ) { $return = true; break; } } if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) ) { $return = ACTIVITYPUB_DISABLE_USER; break; } if ( '1' !== \get_option( 'activitypub_enable_users', '1' ) ) { $return = true; break; } $return = false; break; default: $return = new WP_Error( 'activitypub_wrong_user_type', __( 'Wrong user type', 'activitypub' ) ); break; } return apply_filters( 'activitypub_is_user_type_disabled', $return, $type ); } /** * Check if the blog is in single-user mode. * * @return boolean True if the blog is in single-user mode, false otherwise. */ function is_single_user() { $return = false; if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) { if ( ACTIVITYPUB_SINGLE_USER_MODE ) { $return = true; } } elseif ( false === is_user_type_disabled( 'blog' ) && true === is_user_type_disabled( 'user' ) ) { $return = true; } return $return; } if ( ! function_exists( 'get_self_link' ) ) { /** * Returns the link for the currently displayed feed. * * @return string Correct link for the atom:self element. */ function get_self_link() { $host = wp_parse_url( home_url() ); $path = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; return esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . $path ) ) ); } }