alleyinteractive / apple-news

The Publish to Apple News plugin enables your WordPress blog content to be published to your Apple News channel.
https://wordpress.org/plugins/publish-to-apple-news/
GNU General Public License v3.0
152 stars 70 forks source link

Default styling for programmatically-added components? #1105

Closed ashicus closed 2 months ago

ashicus commented 2 months ago

Description

I have a use case where I need to programmatically add various components to AN articles. Currently I am doing this via the apple_news_generate_json filter. The problem is that these newly added components do not get any styling by default, unless there is a component of the same type already in the post content.

While I could totally add the styling directly as part of the component, I would much prefer to be able to reference the pre-defined styles that are part of the AN theme itself, so as not to have two sets of styling rules to maintain.

For example, if I add an H2, I want to be able to do something like this:

Array
(
    [role] => heading2
    [text] => Heading 2 Test
    [format] => html
    [textStyle] => default-heading-2
    [layout] => heading-layout
)

However, unless there is already an H2 within the original post content, the default-heading-2 and heading-layout definitions will be missing from the componentTextStyles/componentLayouts sections in the JSON, which will cause an error in News Preview.

Is there a way to have the JSON include the pre-defined styling rules for programmatically-added components? Or even just have the JSON include all styling rules for all components by default? I tried experimenting with the various hooks in the wiki, but couldn’t manage to find anything that fits the bill.

Use Case

When I add a component via apple_news_generate_json, I would like the default styling/layout rules to be added automatically.

kevinfodness commented 2 months ago

The simplest path to success here is to use either the apple_news_exporter_content or the apple_news_exporter_content_pre filter instead, which adds the content before it is sent to the plugin, which results in the plugin adding the proper layouts and text styles for the components you are adding automatically. Otherwise, you're into adding those layouts and text styles yourself, which as you've pointed out, is a pain to do.

ashicus commented 2 months ago

Thanks Kevin, the approach you suggested is working for me, so will close this issue. Appreciate the advice!