Skip to content

Commit

Permalink
Merge pull request #164 from PRX/fix-image-credit
Browse files Browse the repository at this point in the history
Add ACF Credit Field and display credits on images
  • Loading branch information
gtenaschuk authored Feb 29, 2024
2 parents a8cff1f + e4238f7 commit 55994c7
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'tw-episode-importer/tw-episode-importer.php',
'tw-episodes/tw-episodes.php',
'tw-graphql/tw-graphql.php',
'tw-image-credit/tw-image-credit.php',
'tw-import-post-types/tw-import-post-types.php', // This is the plugin that creates the custom post types for the import. Can be removed after the import is complete.
'tw-media/tw-media.php',
'tw-menus/tw-menus.php',
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function pri_get_post_meta_keys_and_values( $type = null, $field = null ) {
'_fid' => 'field_62b48652faff2',
'_image_title' => 'field_62d671c653456',
'_hide_image' => 'field_622a6312b1b5d',
'__media_credit' => 'field_65cdf8ee238b7',
),
'audio' => array(
'_original_uri' => 'field_62b4862efaff1',
Expand Down
66 changes: 66 additions & 0 deletions wp-content/plugins/tw-image-credit/tw-image-credit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Plugin Name: TW Image Credit
* Plugin URI: https://www.dinkuminteractive.com/
* Description: Add image credit and credit field.
* Version: 1.0.0
*/

/**
* Adds the images media credit to rendered `core/image` blocks.
*
* @param string $block_content The block content about to be appended.
* @param mixed[] $block The full block, including name and attributes.
*
* @return string
*
* @phpstan-param BlockMetaData $block
*/
function tw_add_media_credit_to_image_blocks( $block_content, array $block ) {

// We only target standard images, and only when the credits are not displayed after the post content.
if ( 'core/image' !== $block['blockName'] || ! isset( $block['attrs']['id'] ) || stripos( $block_content, 'class="media-credit"' ) ) {
return $block_content;
}

// Get attachment credit using the block id.
$attachment_credit = get_post_meta( $block['attrs']['id'], '_media_credit', true );
if ( empty( $attachment_credit ) ) {
// Not a valid attachment, let's bail.
return $block_content;
}

// Filter if needed.
$block_content = apply_filters( 'tw_add_media_credit_to_image_blocks', $block_content, $attachment_credit );

return $block_content;
}
add_filter( 'render_block', 'tw_add_media_credit_to_image_blocks', 10, 2 );

/**
* Injects the credit into the caption markup of a `core/image` block.
*
* @param string $block_content The block content.
* @param string $credit The credit markup.
*
* @return string
*/
function tw_inject_credit_into_caption( $block_content, $credit ) {

// If we have a credit, inject it into the caption markup.
if ( $credit ) {

// Replacement parts.
$pattern = '</figcaption>';
$open = '<span class="media-credit">';
$credit = trim( $credit );
$close = '</span>';

// Inject the credit into the caption markup.
$block_content = str_replace( $pattern, "{$open}{$credit}{$close}{$pattern}", $block_content );
}

// Return the modified block content.
return $block_content;
}
add_filter( 'tw_add_media_credit_to_image_blocks', 'tw_inject_credit_into_caption', 10, 2 );
44 changes: 0 additions & 44 deletions wp-content/themes/the-world/acf-json/group_622a629eb83b7.json

This file was deleted.

132 changes: 68 additions & 64 deletions wp-content/themes/the-world/acf-json/group_62b485f48c100.json
Original file line number Diff line number Diff line change
@@ -1,65 +1,69 @@
{
"key": "group_62b485f48c100",
"title": "File Meta - Migration",
"fields": [
{
"key": "field_62b4862efaff1",
"label": "Original URI",
"name": "original_uri",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
},
{
"key": "field_62b48652faff2",
"label": "Drupal ID",
"name": "fid",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
}
],
"location": [
[
{
"param": "attachment",
"operator": "==",
"value": "all"
}
]
],
"menu_order": 999,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": true,
"description": "",
"show_in_rest": 0,
"modified": 1684260351
}
"key": "group_62b485f48c100",
"title": "File Meta - Migration",
"fields": [
{
"key": "field_62b4862efaff1",
"label": "Original URI",
"name": "original_uri",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
},
{
"key": "field_62b48652faff2",
"label": "Drupal ID",
"name": "fid",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
}
],
"location": [
[
{
"param": "attachment",
"operator": "==",
"value": "all"
}
]
],
"menu_order": 10,
"position": "normal",
"style": "seamless",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": true,
"description": "",
"show_in_rest": 0,
"show_in_graphql": 0,
"graphql_field_name": "fileMeta",
"map_graphql_types_from_location_rules": 0,
"graphql_types": "",
"modified": 1709041316
}
Loading

0 comments on commit 55994c7

Please sign in to comment.