Skip to content

Commit

Permalink
Add fields for image and video attribution (resolve #32) (#33)
Browse files Browse the repository at this point in the history
* Add fields for image and video attribution (resolve #32)
* Fix coding standards error
  • Loading branch information
Ned Zimmerman authored Jun 6, 2019
1 parent 5de2615 commit 4b6735e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Utilities, custom post types and blocks for the Platform Cooperativism Consortiu
None yet.

## Changelog ##
### 0.7.0 ###
* Add fields for image and video attribution (#32): #33

### 0.6.0 ###
* Rename plugin: #16
* Add configuration for automatic deployment: #18
Expand Down
83 changes: 83 additions & 0 deletions lib/posttypes/pcc-attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace PCCFramework\PostTypes\Attachment;

/**
* Registers the Attachment Data meta fields.
*
* @return null
*/
function data($form_fields, $post)
{
$prefix = 'pcc_attachment_';
$creator_name = get_post_meta($post->ID, $prefix . 'creator_name', true);
$creator_link = get_post_meta($post->ID, $prefix . 'creator_link', true);
$organization_name = get_post_meta($post->ID, $prefix . 'organization_name', true);
$organization_link = get_post_meta($post->ID, $prefix . 'organization_link', true);

$form_fields[$prefix . 'creator_name'] = [
'value' => $creator_name ?? '',
'label' => __('Creator Name', 'pcc-framework'),
'input' => 'text',
'helps' => __('The name of the person who created this image or video.', 'pcc-framework'),
];
$form_fields[$prefix . 'creator_link'] = [
'value' => $creator_link ?? '',
'label' => __('Creator Link', 'pcc-framework'),
'input' => 'html',
'helps' => __('A hyperlink to the website of the person who created this image or video.', 'pcc-framework'),
'html' => sprintf(
'<input type="url" id="%1$s" class="regular-text code" name="%2$s" value="%3$s" />',
"attachments-$post->ID-{$prefix}creator_link",
"attachments[$post->ID][{$prefix}creator_link]",
esc_url($creator_link),
),
];
$form_fields[$prefix . 'organization_name'] = [
'value' => $organization_name ?? '',
'label' => __('Organization Name', 'pcc-framework'),
'input' => 'text',
'helps' => __('The name of the organization who created this image or video.', 'pcc-framework'),
];
$form_fields[$prefix . 'organization_link'] = [
'value' => $organization_link ?? '',
'label' => __('Organization Link', 'pcc-framework'),
'input' => 'html',
'helps' =>
__('A hyperlink to the website of the organization who created this image or video.', 'pcc-framework'),
'html' =>sprintf(
'<input type="url" id="%1$s" class="regular-text code" name="%2$s" value="%3$s" />',
"attachments-$post->ID-{$prefix}organization_link",
"attachments[$post->ID][{$prefix}organization_link]",
esc_url($organization_link),
),
];


return $form_fields;
}

/**
* Handle saving attachment data.
*
* @param int $post_id
*/
function save($post_id)
{
$prefix = 'pcc_attachment_';
$fields = [
$prefix . 'creator_name' => 'sanitize_text_field',
$prefix . 'creator_link' => 'esc_url',
$prefix . 'organization_name' => 'sanitize_text_field',
$prefix . 'organization_link' => 'esc_url',
];

$data = [];

foreach ($fields as $key => $cb) {
if (isset($_REQUEST['attachments'][$post_id][$key])) {
$data[ $key ] = $cb($_REQUEST['attachments'][$post_id][$key]);
update_post_meta($post_id, $key, $data[ $key ]);
}
}
}
7 changes: 6 additions & 1 deletion pcc-framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
}

foreach ([
'attachment',
'event',
'person',
] as $posttype) {
require_once dirname(__FILE__) . "/lib/posttypes/pcc-$posttype.php";
add_action('init', '\\PCCFramework\\PostTypes\\' . ucfirst($posttype) . '\\init');
if ($posttype !== 'attachment') {
add_action('init', '\\PCCFramework\\PostTypes\\' . ucfirst($posttype) . '\\init');
}
}

foreach ([
Expand Down Expand Up @@ -73,4 +76,6 @@
add_action('cmb2_admin_init', '\\PCCFramework\\PostTypes\\Event\\sponsors');
add_action('cmb2_admin_init', '\\PCCFramework\\PostTypes\\Person\\data');
add_action('cmb2_admin_init', '\\PCCFramework\\Settings\\page');
add_filter('attachment_fields_to_edit', '\\PCCFramework\\PostTypes\\Attachment\\data', 10, 2);
add_action('edit_attachment', '\\PCCFramework\\PostTypes\\Attachment\\save');
}
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Utilities, custom post types and blocks for the Platform Cooperativism Consortiu
None yet.

== Changelog ==
= 0.7.0 =
* Add fields for image and video attribution (#32): #33

= 0.6.0 =
* Rename plugin: #16
* Add configuration for automatic deployment: #18
Expand Down

0 comments on commit 4b6735e

Please sign in to comment.