Skip to content

Commit

Permalink
Add ability to restrict media library by current post ID
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Jul 15, 2024
1 parent 860c9f4 commit d99557c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
27 changes: 27 additions & 0 deletions classes/fields/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ public function options() {
'pick_format_single' => 'dropdown',
'pick_show_select_text' => 0,
),
static::$type . '_attachment_current_post_only' => array(
'label' => __( 'Restrict Media Library to Current Post ID', 'pods' ),
'help' => __( 'The media library will be restricted to only showing attachments that are attached to the current post ID if this field is on a Pod that is a Post Type.', 'pods' ),
'depends-on' => array( static::$type . '_uploader' => 'attachment' ),
'default' => 0,
'type' => 'boolean',
),
static::$type . '_upload_dir' => array(
'label' => __( 'Upload Directory', 'pods' ),
'default' => 'wp',
Expand Down Expand Up @@ -384,6 +391,26 @@ public function input( $name, $value = null, $options = null, $pod = null, $id =
$args = compact( array_keys( get_defined_vars() ) );
$args = (object) $args;

$pod_data = null;

if ( $pod instanceof Pods ) {
$pod_data = $pod->pod_data;
} elseif ( $pod instanceof Pod ) {
$pod_data = $pod;
} elseif ( is_array( $pod ) ) {
$pod_data = $pod;
}

// Get pod type.
$pod_type = $pod_data ? $pod_data['type'] : null;

$args->options['file_post_id'] = null;

// Maybe set post_id based on current post context.
if ( 'post_type' === $pod_type && ! empty( $options[ static::$type . '_attachment_current_post_only' ] ) ) {
$args->options['file_post_id'] = $id;
}

/**
* Access Checking
*/
Expand Down
2 changes: 1 addition & 1 deletion ui/js/blocks/pods-blocks-api.min.asset.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"dependencies":["lodash","react","react-dom","wp-api-fetch","wp-autop","wp-block-editor","wp-blocks","wp-components","wp-compose","wp-date","wp-element","wp-i18n","wp-keycodes","wp-server-side-render","wp-url"],"version":"d2da5610e90fcb8c369b"}
{"dependencies":["lodash","react","react-dom","wp-api-fetch","wp-autop","wp-block-editor","wp-blocks","wp-components","wp-compose","wp-date","wp-element","wp-i18n","wp-keycodes","wp-server-side-render","wp-url"],"version":"3093dd2e3e3359558f91"}
2 changes: 1 addition & 1 deletion ui/js/blocks/pods-blocks-api.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ui/js/dfv/pods-dfv.min.asset.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"dependencies":["lodash","moment","react","react-dom","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-polyfill","wp-primitives","wp-url"],"version":"c0964a4940abf9b41e14"}
{"dependencies":["lodash","moment","react","react-dom","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-polyfill","wp-primitives","wp-url"],"version":"9360c63f30fab3ebc1b5"}
2 changes: 1 addition & 1 deletion ui/js/dfv/pods-dfv.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion ui/js/dfv/src/fields/file/uploaders/media-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const MediaModal = PodsFileUploader.extend( {
multiple: ( 1 !== parseInt( this.fieldConfig.file_limit, 10 ) ),
library: {
type: this.fieldConfig.limit_types,
uploadedTo: this.fieldConfig?.file_post_id,
},
// Customize the submit button.
button: {
Expand Down Expand Up @@ -80,7 +81,7 @@ export const MediaModal = PodsFileUploader.extend( {
name: attachment.attributes.title,
edit_link: attachment.attributes.editLink,
link: attachment.attributes.link,
download: attachment.attributes.url
download: attachment.attributes.url,
} );
} );

Expand Down

0 comments on commit d99557c

Please sign in to comment.