Files
MiraviaConnector/connector-miravia/classes/tables/class.table.php
Miravia Connector Bot a7d7dbb164 Fix image upload structure for Miravia API compliance
🔧 Bug Fixes:
- Fixed product image structure to match Miravia API requirements
- Updated MiraviaProduct.php getData() method to wrap images in {"Image": [...]} format
- Updated MiraviaCombination.php getData() method to wrap SKU images properly
- Resolved error "[4224] The Main image of the product is required"

📋 Changes:
- Modified getData() methods to transform flat image arrays to nested structure
- Product images: images[] → Images: {"Image": [...]}
- SKU images: images[] → Images: {"Image": [...]}
- Maintains backward compatibility for empty image arrays

🎯 Impact:
- Product uploads will now pass Miravia's image validation
- Both product-level and SKU-level images properly formatted
- Complies with official Miravia API documentation structure

🤖 Generated with Claude Code (https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-17 08:11:23 +02:00

174 lines
4.3 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
// WP_List_Table is not loaded automatically so we need to load it in our application
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
/**
* Create a new table class that will extend the WP_List_Table
*/
class MiraviaTable extends WP_List_Table
{
/**
* Prepare the items for the table to process
*
* @return Void
*/
public $data_table = array();
public $total_elements = 0;
public $perPage = 10;
public $columns = [];
public $custom_actions = [];
public $sortable_custom_columns = [];
public $default_column_name = 'name';
function extra_tablenav( $which ) {
// if ( $which == "top" ){
// echo '<h2>Letter Templates <a href="admin.php?page=letter-template&type=new">Add New</a></h2>';
// }
}
public function prepare_items()
{
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$data = $this->table_data();
usort( $data, array( &$this, 'sort_data' ) );
$perPage = $this->perPage;
$currentPage = $this->get_pagenum();
$totalItems = $this->total_elements;
$this->set_pagination_args( array(
'total_items' => $totalItems,
'per_page' => $perPage
) );
$data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
$this->_column_headers = array($this->columns, $hidden, $sortable);
$this->items = $data;
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns()
{
return $this->columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns()
{
return array();
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns()
{
$sortable = array();
// if($this->total_elements and count($this->data_table) > 0) {
// foreach($this->data_table[0] as $k => $v) {
// $sortable[$k] = array($k, false);
// }
// }
return $this->sortable_custom_columns;
}
/**
* Get the table data
*
* @return Array
*/
private function table_data()
{
return $this->data_table;
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default( $item, $column_name )
{
return $item[ $column_name ];
}
public function column_name($item){
$item_json = json_decode(json_encode($item), true);
$copy = $this->custom_actions;
foreach($this->custom_actions as $k => $action) {
foreach($item_json as $key => $d) {
$action = str_replace("[".$key."]", $item_json[$key], $action);
}
$this->custom_actions[$k] = $action;
}
$result = sprintf('%s %s', $item_json[$this->default_column_name], $this->row_actions($this->custom_actions));
$this->custom_actions = $copy;
return $result;
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function sort_data( $a, $b )
{
// Set defaults
$orderby = 'title';
$order = 'asc';
// If orderby is set, use this as the sort column
if(!empty($_GET['orderby']))
{
$orderby = sanitize_text_field($_GET['orderby']);
}
// If order is set use this as the order
if(!empty($_GET['order']))
{
$order = sanitize_text_field($_GET['order']);
}
$result = '';
if(isset($a[$orderby])) {
$result = strcmp( $a[$orderby], $b[$orderby] );
}
if($order === 'asc')
{
return $result;
}
return -$result;
}
}