Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an endpoints that returns a list of font families used by a theme #648

Merged
merged 5 commits into from
May 30, 2024

Conversation

matiasbenedetto
Copy link
Contributor

@matiasbenedetto matiasbenedetto commented May 22, 2024

What?

  • Adds an endpoint (/wp-json/create-block-theme/v1/font-families) that returns a list of font families used by a theme, including their style variants.

Why?

Example:

With TT4 theme activated you should get an answer like this from the endpoint /wp-json/create-block-theme/v1/font-families:

{
   "status":"SUCCESS",
   "message":"Font Families retrieved.",
   "data":[
      {
         "fontFace":[
            {
               "fontFamily":"Inter",
               "fontStretch":"normal",
               "fontStyle":"normal",
               "fontWeight":"300 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/inter\/Inter-VariableFont_slnt,wght.woff2"
               ]
            }
         ],
         "fontFamily":"\"Inter\", sans-serif",
         "name":"Inter",
         "slug":"body"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Cardo",
               "fontStyle":"normal",
               "fontWeight":"400",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/cardo\/cardo_normal_400.woff2"
               ]
            },
            {
               "fontFamily":"Cardo",
               "fontStyle":"italic",
               "fontWeight":"400",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/cardo\/cardo_italic_400.woff2"
               ]
            },
            {
               "fontFamily":"Cardo",
               "fontStyle":"normal",
               "fontWeight":"700",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/cardo\/cardo_normal_700.woff2"
               ]
            }
         ],
         "fontFamily":"Cardo",
         "name":"Cardo",
         "slug":"heading"
      },
      {
         "fontFamily":"-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif",
         "name":"System Sans-serif",
         "slug":"system-sans-serif"
      },
      {
         "fontFamily":"Iowan Old Style, Apple Garamond, Baskerville, Times New Roman, Droid Serif, Times, Source Serif Pro, serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol",
         "name":"System Serif",
         "slug":"system-serif"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Instrument Sans",
               "fontStyle":"normal",
               "fontWeight":"400 700",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/instrument-sans\/InstrumentSans-VariableFont_wdth,wght.woff2"
               ]
            },
            {
               "fontFamily":"Instrument Sans",
               "fontStyle":"italic",
               "fontWeight":"400 700",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/instrument-sans\/InstrumentSans-Italic-VariableFont_wdth,wght.woff2"
               ]
            }
         ],
         "fontFamily":"\"Instrument Sans\", sans-serif",
         "name":"Instrument Sans",
         "slug":"body"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Jost",
               "fontStyle":"normal",
               "fontWeight":"100 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/jost\/Jost-VariableFont_wght.woff2"
               ]
            },
            {
               "fontFamily":"Jost",
               "fontStyle":"italic",
               "fontWeight":"100 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/jost\/Jost-Italic-VariableFont_wght.woff2"
               ]
            }
         ],
         "fontFamily":"\"Jost\", sans-serif",
         "name":"Jost",
         "slug":"heading"
      },
      {
         "fontFamily":"-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif",
         "name":"System Sans-serif",
         "slug":"system-sans-serif"
      },
      {
         "fontFamily":"Iowan Old Style, Apple Garamond, Baskerville, Times New Roman, Droid Serif, Times, Source Serif Pro, serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol",
         "name":"System Serif",
         "slug":"system-serif"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Inter",
               "fontStretch":"normal",
               "fontStyle":"normal",
               "fontWeight":"300 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/inter\/Inter-VariableFont_slnt,wght.woff2"
               ]
            }
         ],
         "fontFamily":"\"Inter\", sans-serif",
         "name":"Inter",
         "slug":"heading"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Cardo",
               "fontStyle":"normal",
               "fontWeight":"400",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/cardo\/cardo_normal_400.woff2"
               ]
            },
            {
               "fontFamily":"Cardo",
               "fontStyle":"italic",
               "fontWeight":"400",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/cardo\/cardo_italic_400.woff2"
               ]
            },
            {
               "fontFamily":"Cardo",
               "fontStyle":"normal",
               "fontWeight":"700",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/cardo\/cardo_normal_700.woff2"
               ]
            }
         ],
         "fontFamily":"Cardo",
         "name":"Cardo",
         "slug":"body"
      },
      {
         "fontFamily":"-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif",
         "name":"System Sans-serif",
         "slug":"system-sans-serif"
      },
      {
         "fontFamily":"Iowan Old Style, Apple Garamond, Baskerville, Times New Roman, Droid Serif, Times, Source Serif Pro, serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol",
         "name":"System Serif",
         "slug":"system-serif"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Inter",
               "fontStretch":"normal",
               "fontStyle":"normal",
               "fontWeight":"300 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/inter\/Inter-VariableFont_slnt,wght.woff2"
               ]
            }
         ],
         "fontFamily":"\"Inter\", sans-serif",
         "name":"Inter",
         "slug":"heading"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Jost",
               "fontStyle":"normal",
               "fontWeight":"100 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/jost\/Jost-VariableFont_wght.woff2"
               ]
            },
            {
               "fontFamily":"Jost",
               "fontStyle":"italic",
               "fontWeight":"100 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/jost\/Jost-Italic-VariableFont_wght.woff2"
               ]
            }
         ],
         "fontFamily":"\"Jost\", sans-serif",
         "name":"Jost",
         "slug":"body"
      },
      {
         "fontFamily":"-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif",
         "name":"System Sans-serif",
         "slug":"system-sans-serif"
      },
      {
         "fontFamily":"Iowan Old Style, Apple Garamond, Baskerville, Times New Roman, Droid Serif, Times, Source Serif Pro, serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol",
         "name":"System Serif",
         "slug":"system-serif"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Cardo",
               "fontStyle":"normal",
               "fontWeight":"400",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/cardo\/cardo_normal_400.woff2"
               ]
            },
            {
               "fontFamily":"Cardo",
               "fontStyle":"italic",
               "fontWeight":"400",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/cardo\/cardo_italic_400.woff2"
               ]
            },
            {
               "fontFamily":"Cardo",
               "fontStyle":"normal",
               "fontWeight":"700",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/cardo\/cardo_normal_700.woff2"
               ]
            }
         ],
         "fontFamily":"Cardo",
         "name":"Cardo",
         "slug":"body"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Jost",
               "fontStyle":"normal",
               "fontWeight":"100 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/jost\/Jost-VariableFont_wght.woff2"
               ]
            },
            {
               "fontFamily":"Jost",
               "fontStyle":"italic",
               "fontWeight":"100 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/jost\/Jost-Italic-VariableFont_wght.woff2"
               ]
            }
         ],
         "fontFamily":"\"Jost\", sans-serif",
         "name":"Jost",
         "slug":"heading"
      },
      {
         "fontFamily":"-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif",
         "name":"System Sans-serif",
         "slug":"system-sans-serif"
      },
      {
         "fontFamily":"Iowan Old Style, Apple Garamond, Baskerville, Times New Roman, Droid Serif, Times, Source Serif Pro, serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol",
         "name":"System Serif",
         "slug":"system-serif"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Instrument Sans",
               "fontStyle":"normal",
               "fontWeight":"400 700",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/instrument-sans\/InstrumentSans-VariableFont_wdth,wght.woff2"
               ]
            },
            {
               "fontFamily":"Instrument Sans",
               "fontStyle":"italic",
               "fontWeight":"400 700",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/instrument-sans\/InstrumentSans-Italic-VariableFont_wdth,wght.woff2"
               ]
            }
         ],
         "fontFamily":"\"Instrument Sans\", sans-serif",
         "name":"Instrument Sans",
         "slug":"heading"
      },
      {
         "fontFace":[
            {
               "fontFamily":"Jost",
               "fontStyle":"normal",
               "fontWeight":"100 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/jost\/Jost-VariableFont_wght.woff2"
               ]
            },
            {
               "fontFamily":"Jost",
               "fontStyle":"italic",
               "fontWeight":"100 900",
               "src":[
                  "http:\/\/localhost\/wp1\/wp-content\/themes\/twentytwentyfour\/assets\/fonts\/jost\/Jost-Italic-VariableFont_wght.woff2"
               ]
            }
         ],
         "fontFamily":"\"Jost\", sans-serif",
         "name":"Jost",
         "slug":"body"
      },
      {
         "fontFamily":"-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif",
         "name":"System Sans-serif",
         "slug":"system-sans-serif"
      },
      {
         "fontFamily":"Iowan Old Style, Apple Garamond, Baskerville, Times New Roman, Droid Serif, Times, Source Serif Pro, serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol",
         "name":"System Serif",
         "slug":"system-serif"
      }
   ]
}

@t-hamano t-hamano self-requested a review May 22, 2024 13:49
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does adding its own endpoint mean that there is data that cannot be obtained using the core API alone? Looks like __experimentalGetCurrentThemeGlobalStylesVariations has the information we need.

Wouldn't it be better to use this to create a JS hook?

image

Comment on lines 62 to 63

return $font_families;
Copy link
Contributor

@pbking pbking May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we drop something like this into this get_all_fonts function then we can eliminate the 'make theme absolute' function (which is really just string/array checks and a str_replace), eliminate the iteration logic in the api class and always ensure that the src attributes are arrays.

		// ensure that src is an array
		foreach( $font_families as &$font_family ) {
			if ( isset( $font_family['fontFace'] ) ) {
				foreach( $font_family['fontFace'] as &$font_face ) {
					if ( ! is_array( $font_face['src'] ) ) {
						$font_face['src'] = array( $font_face['src'] );
					}
					// make the src urls absolute
					if ( $absolute_src ) {
						foreach( $font_face['src'] as &$font_face_src ) {
							if ( str_starts_with( $font_face_src, 'file:./' ) ) {
								return str_replace( 'file:./', get_stylesheet_directory_uri() . '/', $font_face_src );
							}
						}
					}
				}
			}
		}

Copy link
Contributor Author

@matiasbenedetto matiasbenedetto May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we drop something like this into this get_all_fonts function, we can eliminate the 'make theme absolute' .

I don't see the need to grow one function by absorbing the same code as another. The separation of concerns is clear. What's the benefit?

ensure that the src attributes are arrays

Why should we ensure that?

Copy link
Contributor

@pbking pbking May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you have already moved half of it in from the API class; that was most of the concern and that's enough that you can resolve this discussion if you want.

But I find the make_theme_font_src_absolute function overly complicated when the same could be completed using fewer, easier to understand lines in the get_all_fonts function. The make_theme_font_src_absolute logic is not re-used.

By ensuring that the response to the API always returns the src in the form of an array the client won't have to execute the "is it a string or an array" check when it works with this data; it will always be an array (and makes the processing of that easier here too).

@matiasbenedetto
Copy link
Contributor Author

Does adding its own endpoint mean that there is data that cannot be obtained using the core API alone? Looks like __experimentalGetCurrentThemeGlobalStylesVariations has the information we need.

Something that is different in this endpoint than in __experimentalGetCurrentThemeGlobalStylesVariations is that the font paths are absolute. I haven't found a good way to guess the theme URL from the frontend.

@pbking
Copy link
Contributor

pbking commented May 22, 2024

Does adding its own endpoint mean that there is data that cannot be obtained using the core API alone?

I think the frontend could cobble together the data using that and other APIs but I agree it's nice to have everything packaged nicely and ready-to-go with this one API addition.

@pbking
Copy link
Contributor

pbking commented May 28, 2024

I added some unit tests and merged trunk to fix some conflicts.

I think this is good to bring in and focus on the last branch.

@matiasbenedetto matiasbenedetto merged commit 5470e48 into trunk May 30, 2024
2 checks passed
@matiasbenedetto matiasbenedetto deleted the add/endpoint-to-get-all-theme-fonts branch May 30, 2024 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants