bobbingwide / sb-field-block

Field block to display post meta data
GNU General Public License v3.0
3 stars 0 forks source link

Extend the generic control to support post fields #5

Open bobbingwide opened 3 years ago

bobbingwide commented 3 years ago

The [bw_field] and [bw_fields] shortcodes support both post meta fields and fields which are properties of the post. eg post_date, post_modified.

These are used in Fizzie to display a series of inline fields, see block-template-parts/metadates.html

<p>Published: [bw_field post_date] | Last updated: [bw_field post_modified] [post-edit]</p>

I could use the same solution in Written, but think it would be better to have a block based solution.

Options are:

bobbingwide commented 3 years ago

Which fields are needed?

See also the Gutenberg issue https://github.com/WordPress/gutenberg/issues/22724, which has been Closed even though the work is not complete. Note that the_modified_date was originally going to implemented as part of the Post Date block.

Field Example value Needed?
ID 1332 Nice to have for debugging ( NTHFD )
post_author 1 Yes - converted to the author name
post_date 2020-11-21 20:55:22 Yes. post-date block
post_date_gmt 0000-00-00 00:00:00 Probably not
post_content post content post-content
post_title archive-block post-title block
post_excerpt post excerpt post-excerpt block
post_status auto-draft NTHFD
comment_status closed See Comments blocks?
ping_status closed ?
post_password No
post_name archive-block NTHFD
to_ping ?
pinged ?
post_modified 2020-11-21 20:55:22 Yes. Display as for post-date
post_modified_gmt 0000-00-00 00:00:00 Probably not
post_content_filtered Probably not
post_parent 0 See sb-parent-block
guid https://s.b/wp56/?post_type=wp_template&p=1332 NTHFD
menu_order 0 NTHFD
post_type wp_template See oik-bob-bing-wide oik-fields block
post_mime_type See oik-bob=bing-wide oik-fields block
comment_count 0 See Comments blocks
filter raw Probably not
bobbingwide commented 3 years ago

Develop a styling solution that will support inlining the blocks in a group using grid / flex

The metadates.html block template part in Written is currently coded as:

<!-- wp:paragraph -->
<p>Published:</p>
<!-- /wp:paragraph -->

<!-- wp:post-date /-->

<!-- wp:paragraph -->
<p>Last updated:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>[bw_field post_modified]</p>
<!-- /wp:paragraph -->

Here's the output from the metadates.html block template part

<div class="wp-block-template-part">
<p>Published:</p>
<div class="wp-block-post-date"><time datetime="2021-07-06T16:23:21+00:00">July 6, 2021</time></div>
<p>Last updated:</p>
<span class="post_modified">July 6, 2021</span>
</div>
bobbingwide commented 3 years ago

Need to explain why the paragraph tag surrounding the output of the [bw_field post_modified] shortcode has been stripped.

Explanation

It's the shortcode_unautop() function call in gutenberg_render_block_core_template_part() that strips the <p> tag.

I believe this is undesirable processing... a feechur.

I don't imagine I'll experience the problem when the shortcode has been converted to a block. But what about other people expecting to see the paragraph if they use this construct?

bobbingwide commented 3 years ago

It's the shortcode_unautop() function call in gutenberg_render_block_core_template_part() that strips the <p> tag. I believe this is undesirable processing... a feechur.

I raised an issue https://github.com/wordpress/gutenberg/issues/33345

bobbingwide commented 3 years ago

Options are: develop a post-modified-date block for the other one extend the sb-field-block to support post fields

I found it easier to extend the Fields block in oik-blocks.

C:\apache\htdocs\wordpress\wp-content\plugins\oik-blocks>git diff blocks\oik-fields
diff --git a/blocks/oik-fields/index.js b/blocks/oik-fields/index.js
index a7a2437..2aada86 100644
--- a/blocks/oik-fields/index.js
+++ b/blocks/oik-fields/index.js
@@ -8,7 +8,7 @@
  * - Not yet aware of the Fields associated with a CPT
  * - Does not require fields to be exposed in the REST API
  *
- * @copyright (C) Copyright Bobbing Wide 2018-2020
+ * @copyright (C) Copyright Bobbing Wide 2018-2021
  * @author Herb Miller @bobbingwide
  */
 //import './style.scss';
@@ -51,6 +51,10 @@ const fieldsOptions =
         "thumbnail": "Thumbnail",
         "googlemap": "Google Maps Map",
         "template": "Page template name",
+        "post_date": "Post date",
+        "post_modified": "Post modified date",
+        //"author": "Author",
+        "author_name": "Author name"
     };

For post_date and post_modified the code change was as simple as adding two new entries in the fieldOptions select list. The Server Side Rendering logic for the block already used bw_metadata() and there were already implementing functions bw_theme_field_post_date() and bw_theme_field_post_modified(). Note: oik-blocks is dependent upon oik-fields for this logic.

I needed to write new logic to register the author_name virtual field. Its callback function bw_fields_get_author_name() is a wrapper to get_the_author().