I am relatively new to php and wordpress. I'm trying to make a custom widget to display all the folders and files in my bucket in a sidebar. I've tested the code outside of the widget, but when I put it inside a class object and try to drag it into my page with elementor in wordpress, I get a 500 error and I can't update the page. I feel like I'm missing something very obvious, but I've been scratching my head today.
This is running on a qa server using the aws php sdk.
<?php
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
class S3_Widget extends WP_Widget {
// php classnames and widget name/description added
function __construct() {
$widget_options = array(
'classname' => 's3_widget',
'description' => 'Add a formatted list of s3 folders & files to the sidebar.'
);
parent::__construct(
's3_widget',
'S3 Widget',
$widget_options
);
}
// create the widget output
function widget( $args, $instance ) {
require_once 'vendor/aws.phar';
$bucket = 'gravityforms-bucket';
// Instantiate the client.
$s3 = new S3Client([
'profile' => 'formsS3',
'version' => 'latest',
'region' => 'us-west-2'
]);
echo "<ul>";
// Use the high-level iterators (returns ALL of your objects).
try {
$results = $s3->getPaginator('ListObjects', [
'Bucket' => $bucket
]);
foreach ($results as $result) {
foreach ($result['Contents'] as $object) {
echo "<li>" . $object['Key'] . PHP_EOL . "</li>";
}
}
} catch (S3Exception $e) {
echo $e->getMessage();
} catch (Exception $e) {
echo "<pre>errorMessage::"; echo print_r($e->getMessage(), true); echo "</pre>";
}
echo "</ul>";
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
return $instance;
}
}
// register the widget
function s3_register_widgets() {
register_widget( 'S3_Widget' );
}
add_action( 'widgets_init', 's3_register_widgets' );
?>