method. Anything not listed (Pro / unknown) resolves to ''. */ const RESOLVERS = array( 'post_url' => 'tag_post_url', 'author_url' => 'tag_author_url', ); /** * @param array $config `dynamicLink` block attribute. * @param WP_Post|null $post Post to read from (block context or current post). * @return string Sanitized URL, or '' when there is none. */ public static function resolve( array $config, $post ) { $type = isset( $config['type'] ) && is_string( $config['type'] ) ? $config['type'] : ''; if ( ! $post instanceof WP_Post || ! isset( self::RESOLVERS[ $type ] ) || ! self::can_read( $post ) ) { return ''; } $method = self::RESOLVERS[ $type ]; $url = trim( (string) self::$method( $config, $post ) ); return '' === $url ? '' : esc_url_raw( $url ); } /** Published, or the current user may read it (previews, private posts). */ private static function can_read( WP_Post $post ) { return 'publish' === $post->post_status || current_user_can( 'read_post', $post->ID ); } /* ------------------------------------------------------------------ */ /* Type resolvers: ( $config, $post ) => raw URL */ /* ------------------------------------------------------------------ */ private static function tag_post_url( array $config, WP_Post $post ) { return (string) get_permalink( $post ); } private static function tag_author_url( array $config, WP_Post $post ) { $author_id = (int) $post->post_author; if ( ! $author_id ) { return ''; } // Undefined -> 'author_archive', same default as the editor. $type = isset( $config['authorUrlType'] ) ? $config['authorUrlType'] : 'author_archive'; return 'author_website' === $type ? (string) get_the_author_meta( 'url', $author_id ) : (string) get_author_posts_url( $author_id ); } } endif;