Created
July 12, 2021 19:55
-
-
Save xnau/1b2b9aeea3beb13a78073a8ca7a39783 to your computer and use it in GitHub Desktop.
Shows how to include the absolute URI for image files in a Participants Database CSV export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: PDB Export Absolute Image Paths | |
* Description: Shows how to include the absolute path to image files in a CSV export | |
* | |
*/ | |
class PDb_Image_Paths_Export { | |
/** | |
* @var PDb_Field_Item the field object | |
*/ | |
private $field; | |
/** | |
* sets up the filter | |
*/ | |
public function __construct() | |
{ | |
add_filter( 'pdb-csv_export_value', array( $this, 'alter_export_values' ), 50, 2 ); | |
} | |
/** | |
* alters the CSV export values | |
* | |
* @param string $value | |
* @param PDb_Field_Item $field the field object | |
* @return string the value | |
*/ | |
public function alter_export_values( $value, $field ) | |
{ | |
if ( $field->form_element() === 'image-upload' ) { | |
$this->field = $field; | |
$value = $this->image_path(); | |
} | |
return $value; | |
} | |
/** | |
* provides the absolute path to the image file | |
* | |
* @return string the absolute path | |
*/ | |
private function image_path() | |
{ | |
$image_object = $this->image_object(); | |
return $image_object->image_uri; | |
} | |
/** | |
* provides the image object | |
* | |
* @return PDb_Image instance | |
*/ | |
private function image_object() | |
{ | |
return new PDb_Image( array( | |
'filename' => $this->field->value(), | |
'link' => $this->field->link(), | |
'module' => $this->field->module(), | |
'attributes' => $this->field->attributes(), | |
) ); | |
} | |
} | |
new PDb_Image_Paths_Export(); |
A CSV generated with this plugin active may not be successfully imported back into Participants Database, the plugin is expecting only the file name or media item slug, not a full path for the imported value.
This gist may not work when using the Image Expansion Kit, and certainly won't work for multi-image type fields, it is a demonstration of the simple case of a native Participant Database image field.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Install a WordPress Plugin from a Gist