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>
This commit is contained in:
Miravia Connector Bot
2025-07-17 08:11:23 +02:00
co-authored by Claude
commit a7d7dbb164
61 changed files with 8501 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
=== Woo for Miravia ===
Contributors: wecommsolutions
Tags: miravia, woocommerce miravia, alibaba, aliexpress
Requires at least: 5.0
Tested up to: 6.3
Requires PHP: 5.0
Stable tag: 1.0.0
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
== Description ==
Connect your store with Miravia, upload your products to Miravia and Download orders.
== Installation ==
* Upload the add-in folder to the "/ wp-content / plugins /" directory or install the add-in directly from the WordPress add-ons screen.
* Activate the plugin from the «Add-ons» screen in WordPress
* Enjoy!
== FAQ ==
Visit wecomm.es
== Changelog ==
= 1.0.0 =
* Initial version
== Localization ==
English (US).
@@ -0,0 +1,148 @@
/*
PRIMARY: #4c45e6
accent: #6ee786
*/
.showHover {
visibility: hidden;
}
tr:hover .showHover {
visibility: visible;
}
span.minidate {
font-size: 10px;
line-height: 10px;
}
/*
th#miravia_data {
width: 180px;
} */
td.miravia_data.column-miravia_data {
width: 200px !important;
}
th#miravia_data {
width: 200px !important;
}
table.table-view-list.toplevel_page_miravia_settings {
table-layout: inherit !important;
}
span.created_via {
float: right;
font-size: 12px;
color: #08a1de;
}
.btn_config {
text-decoration: none;
color: #4c45e6;
border: 1px solid #dfdfdf;
background: #dfdfdf;
width: 45%;
padding: 0.5rem;
float: left;
margin: 0.5rem;
cursor: pointer;
height: 140px;
display: block;
border-radius: 10px;
transition: all 0.5s ease;
}
.btn_config:hover {
background-color: #b0aed2;
/* color: #FFF; */
}
.btn_config h4 {
color: #4c45e6;
font-size: 30px;
margin: 0;
font-weight: bold;
text-align: center;
}
.btn_config:hover h4 {
/* color: #FFF */
}
.stats {
/* border: 1px solid #4c45e6; */
padding: 0.5rem;
margin: 0.5rem;
height: 140px;
}
.stats-1 {
width: 23%;
float: left;
}
.stats-2 {
width: 48%;
float: left;
}
.stats-3 {
width: 73%;
float: left;
}
.stats-4 {
clear: both;
}
.wrap.miravia-pane {
max-width: 1024px;
margin: auto;
}
.miravia-pane .description {
font-size: 10px;
display: block;
font-weight: lighter;
}
ul.miravia_categories_select {
background-color: '#FFF';
}
ul.miravia_categories_select ul.children {
margin-left: 25px;
}
.region_config_dashboard {
max-width: 1024px;
margin: auto;
left: 0;
right: 0;
position: relative;
display: block;
}
/* tabs */
a.mv-bt-tab {
padding: 10px;
display: block;
border-radius: 4px 4px 0 0;
border: 1px solid #CCC;
border-bottom: 0px solid;
text-decoration: none;
color: #333;
font-weight: bold;
outline: none;
}
.mv-tab-nav {
border-bottom: 1px solid #CCC;
}
.mv-tab-nav li {
margin: 0;
}
a.mv-bt-tab.active {
background: #CCC;
}
.mv-tab {
display:none;
}
.mv-tab-active {
display:block;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 431 B

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 6.4 KiB

+448
View File
@@ -0,0 +1,448 @@
jQuery(document).ready(function($) {
(function($) {
var Defaults = $.fn.select2.amd.require('select2/defaults');
$.extend(Defaults.defaults, {
searchInputPlaceholder: ''
});
var SearchDropdown = $.fn.select2.amd.require('select2/dropdown/search');
var _renderSearchDropdown = SearchDropdown.prototype.render;
SearchDropdown.prototype.render = function(decorated) {
// invoke parent method
var $rendered = _renderSearchDropdown.apply(this, Array.prototype.slice.apply(arguments));
this.$search.attr('placeholder', this.options.get('searchInputPlaceholder'));
return $rendered;
};
})(window.jQuery);
$(".search-select-miravia").select2({ width: '100%', searchInputPlaceholder: 'Search Miravia Category' })
var select2Element = $(".search-select-miravia-brand");
var options = {
searchInputPlaceholder: 'Search Brand',
width: '100%',
ajax: {
url: MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=miravia_get_brands',
dataType: 'json',
cache: true,
data: function (params) {
return {
brand_name: params.term,
};
},
processResults: function (data) {
final_data = {
results: $.map(data, function (item) {
return {
text: item.brand_name,
id: item.brand_name
}
})
};
console.log(final_data)
delete options.ajax;
options.data = final_data.results;
select2Element.select2( options ).select2( 'open' );
return final_data;
},
success: function( data ) {
},
}
};
select2Element.select2( options );
$(document).on("click", "a.sendProductsMiravia_BACK", function(){
var id = $(this).attr("data-id");
var control = $(".miravia_controls[data-id='"+id+"']");
var preHTML = control.html();
control.html("Uploading...");
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=miravia_upload_product',
data : {
id: id
},
dataType: 'JSON',
error: function(response){
control.html(preHTML);
},
success: function(response) {
if(response['error']) {
alert(response['message']);
return;
}
control.html("<a href='javascript:void(0)' class='button viewProduct' data-id='"+response['id']+"'>View</a> | <a href='javascript:void(0)' class='button updateProduct' data-id='"+id+"'>Update</a>");
}
})
});
$(document).on("click", "a.updateProduct", function(){
var id = $(this).attr("data-id");
var control = $(".miravia_controls[data-id='"+id+"']");
var preHTML = control.html();
control.html("Updating...");
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=miravia_update_product',
data : {
id: id
},
dataType: 'JSON',
error: function(response){
control.html(preHTML);
},
success: function(response) {
if(response['error']) {
alert(response['message']);
return;
}
control.html(preHTML);
}
})
});
$(document).on("click", "a.viewProduct, button.viewProduct", function(){
var id = $(this).attr("data-id");
window.open('https://www.miravia.es/p/i'+id+'.html', '_blank');
});
$(document).on("click", ".connectProductRemote", function(){
var control = $(this);
if(control.attr('disabled') == 'disabled') {
return;
}
var id = $(this).attr("data-id");
var sku = $(this).attr("data-sku");
var preHTML = control.html();
var preColor = control.css('color');
control.text("Connecting...");
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=miravia_connect_product',
data : {
id_remote: id,
sku: sku
},
dataType: 'JSON',
error: function(response){
alert('Error, please contact with support');
control.html(preHTML);
Swal.close();
},
success: function(response) {
if(response['ok']) {
control.attr('disabled', 'disabled').html('Connected').css('color', 'green').css('border-color', 'green');
}else{
if(response['alert']) {
alert(response['alert']);
control.html(preHTML).css('color', preColor).css('border-color', preColor);
}else{
control.html('No Profile / Product').css('color', 'red').css('border-color', 'red');
setTimeout(() => {
control.html(preHTML).css('color', preColor).css('border-color', preColor);
}, 5000);
}
}
}
})
});
$(document).on("click", ".disconnectProduct", function(){
var id = $(this).attr("data-id");
var control = $(this);
var preHTML = control.html();
control.text("Disconnecting...");
Swal.fire({
title: 'Disconnecting product...',
html: 'Please wait...',
didOpen: () => {
Swal.showLoading()
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=disconnect_product_miravia',
data : {
id: id
},
dataType: 'JSON',
error: function(response){
alert('Error, please contact with support');
control.html(preHTML);
Swal.close();
},
success: function(response) {
if(response['error']) {
Swal.fire(
'Error',
response['message'],
'error'
)
}else{
Swal.fire(
'Success',
'Product disconnected from Miravia',
'success'
)
}
control.html(preHTML);
}
})
},
});
});
$(document).on("click", ".sendProductsMiravia", function(){
console.log("ENVIANDO");
var profile = $(this).attr("data-profile");
var control = $(this);
var preHTML = control.html();
control.text("Updating...");
Swal.fire({
title: 'Uploading products...',
html: 'Please wait...',
didOpen: () => {
Swal.showLoading()
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=send_products_miravia',
data : {
profile: profile
},
dataType: 'JSON',
error: function(response){
alert('Error, please contact with support');
control.html(preHTML);
Swal.close();
},
success: function(response) {
if(response['error']) {
Swal.fire(
'Error',
response['message'],
'error'
)
}else{
Swal.fire(
'Success',
'Send ' + response.length + ' products to Miravia',
'success'
)
}
control.html(preHTML);
}
})
},
});
});
$(document).on("click",".download_order", function(){
var id = $(this).attr("data-id");
var token = $(this).attr("data-token");
var account_id = $(this).attr("data-account");
var control = $(this);
var preHTML = control.html();
control.text("Downloading...");
Swal.fire({
title: 'Download order...',
html: 'Please wait...',
didOpen: () => {
Swal.showLoading()
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=miravia_download_order',
data : {
id: id,
token: token,
account_id: account_id
},
dataType: 'JSON',
error: function(response){
control.html(preHTML);
Swal.close();
},
success: function(response) {
if(response['error']) {
Swal.fire(
'Error',
response['message'],
'error'
)
return;
}
Swal.close();
control.html(preHTML);
}
})
},
});
});
$(document).on("click",".checkJob", function(){
var id = $(this).attr("data-id");
var token = $(this).attr("data-token");
var control = $(this).closest('tr');
$(".status_result", control).text('Checking...');
Swal.fire({
title: 'Check Job...',
html: 'Please wait...',
didOpen: () => {
Swal.showLoading()
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=miravia_check_job',
data : {
id: id,
token: token
},
dataType: 'JSON',
error: function(response){
$(".status_result", control).text('Error to check status, please try again');
Swal.close();
},
success: function(response) {
if(response['error']) {
Swal.fire(
'Error',
response['message'],
'error'
)
return;
}
Swal.close();
$(".status_result", control).text(response['status']);
}
})
},
});
});
$(document).on("click",".cancelJob", function(){
var id = $(this).attr("data-id");
var token = $(this).attr("data-token");
var control = $(this);
var preHTML = control.html();
if(confirm('You want cancel this job?')) {
control.text("Checking...");
Swal.fire({
title: 'Cancel Job...',
html: '<b>Please wait...</b>',
didOpen: () => {
Swal.showLoading()
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=miravia_cancel_job',
data : {
id: id,
token: token
},
dataType: 'JSON',
error: function(response){
control.html(preHTML);
Swal.close();
},
success: function(response) {
control.html(preHTML);
if(response['error']) {
Swal.fire(
'Error',
response['message'],
'error'
)
}else{
Swal.fire(
'Success',
'Job Cancel Success',
'success'
)
}
}
})
},
});
}
});
$(document).on("click",".mv-bt-tab", function(){
var tab = $(this).attr("data-tab");
$(".mv-bt-tab").removeClass("active");
$(this).addClass("active");
$(".mv-tab").removeClass("mv-tab-active");
$(".mv-tab[data-tab='"+tab+"']").addClass('mv-tab-active');
});
$(document).on("click","#packOrderButton", function(){
var id = $(this).attr("data-id");
console.log("Packed Order", id);
$(this).text('Waiting...').prop('disabled', true);
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=miravia_packed_order',
data : {
id: id,
},
dataType: 'JSON',
error: function(response){
console.log(response);
$(this).text('Pack Order').prop('disabled', false);
},
success: function(response) {
if(response['error']) {
alert(response['message']);
$(this).text('Pack Order').prop('disabled', false);
return;
}
window.location.reload();
}
})
});
$(document).on("click", ".miraviaprintLabel", function() {
var id = $(this).attr("data-id");
var package_id = $(this).attr("data-package");
$.ajax({
method : "post",
url : MIRAVIA_DATA_JAVASCRIPT.adminAjaxURL+'?action=miravia_print_label',
data : {
id: id,
package_id: package_id,
},
dataType: 'JSON',
error: function(response){
},
success: function(response) {
if(response && response.data) {
if(response.data.pdf_url) {
console.log(response.data);
window.open(response.data.pdf_url, '_blank');
}
}
}
})
});
});
+495
View File
@@ -0,0 +1,495 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('APIMIRAVIA') ) {
class APIMIRAVIA {
function __construct() {
$actionsPrivate = array(
'miravia_upload_product',
'miravia_update_product',
'miravia_authorize',
'miravia_create_profile',
'miravia_download_order',
'send_products_miravia',
'miravia_check_job',
'miravia_cancel_job',
'miravia_notify',
'miravia_request_notify',
'miravia_packed_order',
'miravia_print_label',
'miravia_get_brands',
'miravia_connect_product',
'disconnect_product_miravia'
);
foreach( $actionsPrivate as $action ){
add_action( 'wp_ajax_'.$action, array( $this, $action ) );
add_action( 'wp_ajax_nopriv_'.$action, array( $this, $action ) );
}
}
function miravia_connect_product(){
global $wpdb;
if ( !current_user_can( 'manage_woocommerce' ) ) { exit; }
$res = array('ok' => false);
$sku = sanitize_text_field($_POST['sku']);
$profile_id = MiraviaCore::get_profile_by_product($sku);
LOG::add("Profile encontrado {$profile_id} -> {$sku}");
if($profile_id) {
$id_remote = sanitize_text_field($_POST['id_remote']);
$id_local = wc_get_product_id_by_sku($sku);
$existe = MiraviaCore::get_product_miravia($id_local);
if(!$existe) {
$_product = wc_get_product($id_local);
//Save product data
$wpdb->insert($wpdb->prefix.'miravia_products', array(
'id_woocommerce' => $id_local,
'sku' => $_product->get_sku() ?: $id_local,
'id_miravia' => $id_remote,
'profile_id' => $profile_id,
'stock' => $_product->get_regular_price(),
'price' => $_product->get_regular_price(),
'sale_price' => $_product->get_sale_price(),
));
LOG::add("Conectando producto ({$id_local}) remoto {$sku} con {$id_remote} con el profile {$profile_id}");
update_post_meta($id_local, '_miravia_product_id', $id_remote);
update_post_meta($id_local, '_miravia_sync_date', time());
$res['ok'] = true;
}else{
$res['alert'] = "This product exist";
}
}
wp_send_json($res);
}
function miravia_print_label() {
$id = sanitize_text_field($_POST['id']);
$package_id = sanitize_text_field($_POST['package_id']);
$account_id = get_post_meta($id, '_miravia_account_id', true);
$labelResult = false;
$account = MiraviaCore::get_accounts($account_id);
$link = new MiraviaLink($account['token']);
if($account_id) {
$labelResult = $link->getShippingLabel($package_id);
}
wp_send_json($labelResult);
}
function miravia_get_brands() {
$token = MiraviaCore::get_miravia_account_default();
if($token) {
$token = $token['token'];
$link = new MiraviaCategory($token);
$brands = $link->getBrands();
}else{
$brands = [];
}
wp_send_json($brands);
}
function miravia_packed_order(){
$id = sanitize_text_field($_POST['id']);
$account_id = get_post_meta($id, '_miravia_account_id', true);
$order_miravia_id = get_post_meta($id, '_miravia_order_id', true);
if($account_id) {
$account = MiraviaCore::get_accounts($account_id);
$link = new MiraviaLink($account['token']);
$resultPack = false;
list($order, $items) = MiraviaCore::get_order_woocommerce($id);
$resultPack = $link->orderPack($order_miravia_id, array('products' => $items));
update_post_meta($id, '_miravia_packed_result', $resultPack);
}else{
LOG::add("{$id} no ha sido importado, no tiene ID de Cuenta", false, 'pack_order');
LOG::add([$_POST, $account_id], false, 'pack_order');
}
wp_send_json($resultPack);
}
function miravia_check_job() {
$id = sanitize_text_field($_POST['id']);
if($id) {
$apiKey = sanitize_text_field($_POST['token']);
$link = new MiraviaLink($apiKey);
$result = $link->getFeedInfo($id);
// LOG::add($result, false, 'check_job');
if($result and $result['result']['processing_status'] == 'DONE') {
foreach($result['response'] as $sku => $value) {
if($value['status'] == 'FAIL') {
if($value['detail']['message']['errorDetail'] and count($value['detail']['message']['errorDetail']) > 0) {
LOG::add('SET JOB Detail' . $id . ' -> ' . $value['detail']['message']['errorDetail'][0]['message'] . ' -- ' . $sku, false, 'check_job');
MiraviaCore::set_error_product_job($sku, $id, $value['detail']['message']['errorDetail'][0]['message']);
}else{
LOG::add('SET JOB MSG' . $id . ' -> ' . $value['detail']['message']['errorMsg'] . ' -- ' . $sku, false, 'check_job');
MiraviaCore::set_error_product_job($sku, $id, $value['detail']['message']['errorMsg']);
}
}else{
//Controlar los updates
if(!isset($value['id'])) {
$value['id'] = false;
}
MiraviaCore::set_id_miravia_product_job($sku, $id, $value['id']);
}
}
}
if($result) {
// MiraviaCore::set_status_job($id, $result['result']['processing_status']);
wp_send_json(array('status' => $result['result']['processing_status']));
}else{
wp_send_json(array('status' => false));
}
}
}function miravia_cancel_job() {
$id = sanitize_text_field($_POST['id']);
if($id) {
$apiKey = sanitize_text_field($_POST['token']);
$link = new MiraviaLink($apiKey);
$result = $link->cancelFeed($id);
LOG::add($result, false, 'result_cancel_job');
if($result and $result['success']) {
MiraviaCore::clear_job($id);
wp_send_json(array('success' => true));
}else{
MiraviaCore::clear_job($id);
wp_send_json(array('error' => true, 'message' => 'You can cancel this job, products unlock locally'));
}
}
}
function miravia_request_notify() {
MiraviaCore::request_notify(sanitize_text_field($_POST['token']), sanitize_text_field($_POST['message']));
}
function miravia_download_order($id = false, $token = false) {
$id = $id ?: sanitize_text_field($_POST['id']);
$apiKey = $token ?: sanitize_text_field($_POST['token']);
$existe = MiraviaCore::order_exist($id);
if(count($existe) > 0) {
LOG::add("Order {$id} exists");
return;
}
$link = new MiraviaLink($apiKey);
$order_from_miravia = $link->getOrder($id);
$order_from_miravia['data']['account_id'] = sanitize_text_field($_POST['account_id']);
$miravia_order = new MVOrder();
$miravia_order->create($order_from_miravia['data']);
}
function miravia_notify(){
LOG::add('Datos recibidos de notificación', false, 'notify');
// LOG::add($_REQUEST, false, 'notify');
if(isset($_GET['seller'])) {
$idPedido = false;
$action = sanitize_text_field($_GET['message']);
if(sanitize_text_field($_GET['miravia_action']) == 'notify') {
if(str_contains($action, 'neworder-')) {
$idPedido = substr($action, 9);
$action = 'neworder';
}
switch($action) {
case 'update_stock':
$profiles = MiraviaCore::get_profiles_by_seller(sanitize_text_field($_GET['seller']));
foreach($profiles as $k => $p) {
$this->send_stock_price_miravia($p['id'], false);
}
//update_option('miravia_notify_' . $action . '_in', time());
break;
case 'neworder':
if($idPedido) {
if(isset($_GET['status']) and sanitize_text_field($_GET['status']) == 'pending') {
$account = MiraviaCore::get_miravia_account_default(sanitize_text_field($_GET['seller']), 'userid');
$this->miravia_download_order($idPedido, $account['token']);
}else{
LOG::add("Order status is ".sanitize_text_field($_GET['status'])." => {$idPedido}");
}
}
break;
default:
LOG::add("Action notify no recognized");
break;
}
}elseif(sanitize_text_field($_GET['miravia_action']) == 'feed') {
MiraviaCore::procesarFeed();
}elseif(sanitize_text_field($_GET['miravia_action']) == 'stock_sresync') {
$accounts = MiraviaCore::resync_stock();
if($accounts) {
foreach($accounts as $a) {
MiraviaCore::request_notify($a['token'], 'update_stock');
}
}
}
wp_send_json(array('success' => true), 200);
die();
}
wp_send_json(array('success' => false), 400);
die();
}
function send_products_miravia() {
$profile = sanitize_text_field($_POST['profile']);
LOG::add("Enviando productos del perfil {$profile}");
if ( !current_user_can( 'manage_woocommerce' ) ) { exit; }
$result = array(
'id' => 0,
'error' => false,
'message' => ''
);
$accounts = MiraviaCore::accounts_by_profile($profile);
$product = MiraviaCore::get_products_by_profile($profile);
if($product) {
foreach($accounts as $a) {
//Enviar los productos con cada una de las cuentas de usuario registrados en el profile.
//Comprobar el producto si no se ha enviado
// LOG::add("Comprobando producto en job");
// LOG::add($product);
$productsToSend = array(
'update' => array(),
'create' => array()
);
if($product){
foreach($product as $k => $p) {
if(MiraviaCore::check_product_onjob($p->id)) {
unset($product[$k]);
}else{
if($product and $product[$k]->id_miravia != 0 and $product[$k]->id_miravia != '' and $product[$k]->id_miravia != '0') {
$product[$k]->created = 1;
array_push($productsToSend['update'], $product[$k]);
}else{
array_push($productsToSend['create'], $product[$k]);
}
}
}
}
//Check after check on job
// LOG::add("PRODUCTOS DESPUES");
// LOG::add($product);
if(count($product) == 0) {
wp_send_json(array('error' => true, 'message' => 'All products is on job, please wait to complete this before send again.'));
wp_die();
}
if(count($productsToSend['create']) > 0) {
$link = new MiraviaLink($a['token']);
$feed = new MiraviaFeed();
$feed->setProducts($productsToSend['create']);
//Apply Rules
$feed = MiraviaCore::applyFilter($feed, $a['id'], $profile);
$productJson = $feed->getJsonCreate();
if(MIRAVIA_DEBUG == '0') {
$result = $link->sendFeed($productJson);
if(isset($result['feed_result']) and $result['feed_result']['success']) {
MiraviaCore::set_job_product(array_column($productsToSend['create'], 'id'), $profile, $result['feed_result']['result']);
}
}
}
if(count($productsToSend['update']) > 0) {
$link = new MiraviaLink($a['token']);
$feed = new MiraviaFeed();
$feed->setProducts($productsToSend['update']);
//Apply Rules
$feed = MiraviaCore::applyFilter($feed, $a['id'], $profile);
$productJsonUpdate = $feed->getJsonUpdate();
if(MIRAVIA_DEBUG == '0') {
$result = $link->sendFeed($productJsonUpdate, 'update');
if(isset($result['feed_result']) and $result['feed_result']['success']) {
MiraviaCore::set_job_product(array_column($productsToSend['update'], 'id'), $profile, $result['feed_result']['result']);
}
}
}
if(MIRAVIA_DEBUG == '1') {
wp_send_json(array('error' => true, 'message' => 'Debug Active', 'update' => $productJsonUpdate, 'create' => $productJson, 'initData' => $productsToSend));
die();
}
LOG::add("Enviando " . count($product) . " productos con token {$a['token']}");
LOG::add($result);
}
}
wp_send_json($product);
wp_die();
}
function send_stock_price_miravia($profile = false, $returnValue = true) {
if(!$profile) {
$profile = sanitize_text_field($_POST['profile']);
}
LOG::add("Enviando productos del perfil {$profile}");
if ($returnValue and !current_user_can( 'manage_woocommerce' ) ) { exit; }
$result = array(
'id' => 0,
'error' => false,
'message' => ''
);
$accounts = MiraviaCore::accounts_by_profile($profile);
$product = MiraviaCore::get_products_by_profile($profile, true);
$isOnlyStock = get_option('miravia_only_stock', '0') == '1';
if($product) {
foreach($accounts as $a) {
//Enviar los productos con cada una de las cuentas de usuario registrados en el profile.
//Comprobar el producto si no se ha enviado
foreach($product as $k => $p) {
if(MiraviaCore::check_product_onjob($p->id)) {
unset($product[$k]);
}
}
//Check after check on job
if(count($product) == 0) {
wp_send_json(array('error' => true, 'message' => 'All products is on job, please wait to complete this before send again.'));
wp_die();
}
$link = new MiraviaLink($a['token']);
$feed = new MiraviaFeed();
$feed->setProducts($product);
//Apply Rules
$feed = MiraviaCore::applyFilter($feed, $a['id'], $profile);
$stockJson = $feed->getJsonUpdateStock($isOnlyStock);
if(MIRAVIA_DEBUG == '1') {
wp_send_json($stockJson);
die();
}
$result = $link->updateStock($stockJson);
if(isset($result['feed_result']) and $result['feed_result']['success']) {
MiraviaCore::set_job_product(array_column($product, 'id'), $profile, $result['feed_result']['result']);
}
}
}
if($returnValue) {
wp_send_json($product);
wp_die();
}
}
function miravia_create_profile() {
$apiKey = sanitize_text_field($_REQUEST['api_key']);
$link = new MiraviaLink($apiKey);
$sellerInfo = $link->getSellerInfo(admin_url('admin-ajax.php?action=miravia_notify'));
if($sellerInfo) {
if(!MiraviaCore::get_accounts($sellerInfo['seller_id'], 'userid')) {
$profile = MiraviaCore::add_account(array(
'name' => $sellerInfo['seller_name'],
'token' => $apiKey,
'userid' => $sellerInfo['seller_id'],
'lang' => $sellerInfo['country'],
'email' => $sellerInfo['email'],
'config' => '{short_code: "'.$sellerInfo['short_code'].'"}',
));
}
}
wp_redirect( admin_url('admin.php?page=miravia_settings&subpage=accounts') );
wp_send_json(array('ok' => true));
}
function disconnect_product_miravia(){
if ( !current_user_can( 'manage_woocommerce' ) ) { exit; }
$id = $_POST['id'];
LOG::add("Desconectando producto {$id} de miravia");
update_post_meta($id, '_miravia_product_id', 0);
update_post_meta($id, '_miravia_sync_date', 0);
wp_send_json(array('ok' => true));
}
function miravia_authorize() {
LOG::add("Solicitando autorización a Miravia");
$link = new MiraviaLink();
$register_link = $link->getRegisterUrl(admin_url('admin-ajax.php?action=miravia_create_profile'));
LOG::add("Register link is " . $register_link);
wp_redirect( $register_link );
wp_send_json(array('ok' => true));
}
function miravia_upload_product() {
if ( !current_user_can( 'manage_woocommerce' ) ) { exit; }
$result = array(
'id' => 0,
'error' => false,
'message' => ''
);
$id = sanitize_text_field($_POST['id']);
$product = new MVProduct($id, 2);
wp_send_json($product);
wp_die();
$response = $product->send();
if(isset($response['item_id'])) {
//Producto subido
update_post_meta($id, '_miravia_product_id',$response['item_id']);
update_post_meta($id, '_miravia_sync_date',time());
$result['id'] = $response['item_id'];
}else{
$result['error'] = true;
$result['message'] = $response['errors'][0]['message'];
}
wp_send_json($result);
wp_die();
}
function miravia_update_product() {
if ( !current_user_can( 'manage_woocommerce' ) ) { exit; }
$result = array(
'id' => 0,
'error' => false,
'message' => ''
);
$id = sanitize_text_field($_POST['id']);
$product = new MVProduct($id);
$response = $product;
if(is_array($response) and count($response) == 0) {
//Producto subido
update_post_meta($id, '_miravia_sync_date',time());
$result['id'] = $id;
}else{
$result['error'] = true;
$result['message'] = $response['errors'][0]['message'];
}
wp_send_json($result);
wp_die();
}
}
$APIMIRAVIA = new APIMIRAVIA();
}
@@ -0,0 +1,66 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('MIRAVIA_Category') ) {
class MIRAVIA_Category {
private $lang = "es_ES";
function __construct($lang = ''){
if($lang != '') {
$this->lang = $lang;
}
}
function get_category_tree() {
global $MIRAVIAWOO;
$categories = $MIRAVIAWOO->client->get('/category/tree/get', array(
'language_code' => $this->lang
));
return $categories;
}
function get_attributes($id) {
global $MIRAVIAWOO;
$attributes = $MIRAVIAWOO->client->get('/category/attributes/get', array(
'primary_category_id' => $id,
'language_code' => $this->lang
));
return $attributes;
}
function get_input_type_attr($attr, $args = array('value' => '')) {
$result = 'Esta propiedad no puede ser establecida por defecto';
if($attr['input_type'] == 'enumInput' or $attr['input_type'] == 'singleSelect' or $attr['input_type'] == 'multiSelect') {
if(isset($attr['options'])) {
$result = "<select name='attr[{$attr['name']}]'>";
$result .= "<option value=''>Seleccione</option>";
foreach($attr['options'] as $opt) {
$selected_html = '';
if($opt['name'] == $args['value']) { $selected_html = "selected='selected'"; }
$result .= "<option {$selected_html} value='{$opt['name']}'>{$opt['name']}</option>";
}
$result .= "</select>";
}else{
$result = "<input type='text' name='attr[{$attr['name']}]' value='{$args['value']}' />";
}
}elseif($attr['input_type'] == 'numeric') {
$result = "<input type='number' name='attr[{$attr['name']}]' value='{$args['value']}' />";
}elseif($attr['input_type'] == 'text') {
$result = "<input type='text' name='attr[{$attr['name']}]' value='{$args['value']}' />";
}elseif($attr['input_type'] == 'richText') {
$result = "<textarea name='attr[{$attr['name']}]'>{$args['value']}</textarea>";
}
return esc_html($result);
}
}
}
+775
View File
@@ -0,0 +1,775 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('MiraviaCore') ) {
class MiraviaCore {
/**
* Add account miravia
*
* @param array $data
* @return bool
*
* name tinytext NOT NULL,
token varchar(200) NOT NULL,
userid varchar(30) DEFAULT '' NOT NULL,
config TEXT NOT NULL DEFAULT '{}',
lang varchar(10) NOT NULL
*/
static function add_account($args) {
global $wpdb;
$default = array(
'name' => '',
'token' => '',
'userid' => '',
'lang' => '',
'email' => '',
'config' => '{}',
);
$args = wp_parse_args( $args, $default );
if($args['token'] == '') {
return false;
}
$wpdb->insert($wpdb->prefix.'miravia_accounts',$args);
$my_id = $wpdb->insert_id;
return $my_id;
}
static function delete_account($id = false) {
global $wpdb;
if($id and current_user_can( 'manage_options' )) {
return $wpdb->delete($wpdb->prefix.'miravia_accounts', array('id' => $id));
}
return false;
}
static function delete_profile($id = false) {
global $wpdb;
if($id and current_user_can( 'manage_options' )) {
return $wpdb->delete($wpdb->prefix.'miravia_profiles', array('id' => $id));
}
return false;
}
static function delete_rule($id = false) {
global $wpdb;
if($id and current_user_can( 'manage_options' )) {
return $wpdb->delete($wpdb->prefix.'miravia_rules', array('id' => $id));
}
return false;
}
static function debug($v, $json = true) {
if($json) {
die(json_encode(($v)));
}
die("<pre>".print_r($v, true)."</pre>");
}
static function add_profile($args) {
global $wpdb;
$default = array(
'name' => '',
'accounts_id' => '',
'categories' => '',
'miravia_category' => '',
'config' => '{}',
);
$args = wp_parse_args( $args, $default );
$wpdb->insert($wpdb->prefix.'miravia_profiles',$args);
$my_id = $wpdb->insert_id;
return $my_id;
}
static function request_notify($apiKey, $message, $customSeconds = false){
$secondsNotify = !$customSeconds ? get_option('miravia_delay_time', 300) : $customSeconds;
$notify_actual = intval(get_option('miravia_notify_' . $message . '_in', 0));
$time_lost = ($notify_actual + ($secondsNotify * 1.1));
$time_now = time();
if($notify_actual >= 0 and $time_now < $time_lost ) {
LOG::add("El notify actual está establecido y es superior a {$time_now} > {$time_lost}");
return -1;
}
LOG::add("Estableciendo info notify {$secondsNotify}");
update_option('miravia_notify_' . $message . '_in', time());
$link = new MiraviaLink($apiKey);
$result = $link->subscribe($secondsNotify, $message);
if($result) {
return true;
}else{
return false;
}
}
static function add_rule($args) {
global $wpdb;
$default = array(
'name_rule' => 'No name',
'accounts' => 0,
'profile_id' => 0,
'rules_json' => '[]',
'action_json' => '[]'
);
$args = wp_parse_args( $args, $default );
$wpdb->insert($wpdb->prefix.'miravia_rules',$args);
LOG::add($wpdb->last_error);
$my_id = $wpdb->insert_id;
return $my_id;
}
static function check_product_onjob($product) {
global $wpdb;
$check = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}miravia_products WHERE id_woocommerce = {$product}");
LOG::add("Comprobando si el producto {$product} esta en job {$check->status_text}");
if($check->status_text == 'IN_QUEUE') {
return true;
}
return false;
}
static function update_rule($args, $id) {
global $wpdb;
$default = array(
'name_rule' => 'No name',
'accounts' => 0,
'profile_id' => 0,
'rules_json' => '[]',
'action_json' => '[]'
);
$args = wp_parse_args( $args, $default );
if($id) {
$wpdb->update($wpdb->prefix.'miravia_rules',$args, $id);
return $id;
}
return false;
}
static function update_profile($args, $id = false) {
global $wpdb;
if($id) {
$wpdb->update($wpdb->prefix.'miravia_profiles',$args, $id);
}
return $id;
}
static function get_products_by_profile($profile = false, $need = false) {
if(!$profile) {
return false;
}
$profile = self::get_profiles($profile);
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array_map('intval', explode(',', $profile['categories'])),
'operator' => 'IN',
'include_children' => false
)
),
'post_type' => 'product',
);
if($need) {
$args['meta_query'] = array(
array(
'key' => '_miravia_need_update',
'compare' => 'EXISTS'
),
array(
'key' => '_miravia_need_update',
'compare' => '!=',
'value' => '0'
)
);
}
$saved = array();
$products = new WP_Query($args);
if($products->have_posts()) {
//Preparare products
foreach($products->posts as $p) {
$prod = new MVProduct($p->ID, $profile);
$pro = $prod->getData();
if($pro) {
$saved[] = $pro;
}
}
return $saved;
}
return false;
}
static function accounts_by_profile($profile) {
global $wpdb;
$profile = self::get_profiles($profile);
if($profile) {
return $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}miravia_accounts WHERE id IN({$profile['accounts_id']})", ARRAY_A );
}
return [];
}
static function get_local_products($page = 1, $limit = 20) {
global $wpdb;
$ofset = ($limit * $page) - $limit;
$query = "SELECT p.*,
IFNULL(COUNT(pw.ID), 0) as variationsTotal,
pp.post_title as `name`
FROM {$wpdb->prefix}miravia_products AS p
LEFT JOIN {$wpdb->prefix}posts AS pp ON pp.ID = p.id_woocommerce
LEFT JOIN {$wpdb->prefix}posts AS pw ON pw.post_parent = p.id_woocommerce AND pw.post_type = 'product_variation'
GROUP BY p.ID
LIMIT {$ofset},{$limit}";
return $wpdb->get_results( $query, ARRAY_A );
}
static function get_profiles($id = false) {
global $wpdb;
if($id) {
return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}miravia_profiles WHERE id = '{$id}'", ARRAY_A );
}
$profiles = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}miravia_profiles", ARRAY_A );
if($profiles) {
foreach($profiles as &$prof){
$prof['sync'] = self::get_products_by_profile_total($prof['categories']);
}
return $profiles;
}
return [];
}
static function get_products_by_profile_total($categories) {
global $wpdb;
if($categories == "") {
return "No categories selected";
}
$created = (array) $wpdb->get_row("SELECT COUNT(DISTINCT p.ID) AS total_created
FROM {$wpdb->prefix}posts p
INNER JOIN {$wpdb->prefix}term_relationships tr ON p.ID = tr.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms t ON tt.term_id = t.term_id
WHERE tt.taxonomy = 'product_cat'
AND t.term_id IN ({$categories})");
$sync = (array) $wpdb->get_row("SELECT COUNT(DISTINCT mp.id_woocommerce) AS total_sync
FROM {$wpdb->prefix}posts p
INNER JOIN {$wpdb->prefix}term_relationships tr ON p.ID = tr.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms t ON tt.term_id = t.term_id
INNER JOIN {$wpdb->prefix}miravia_products mp ON p.ID = mp.id_woocommerce AND mp.id_miravia != 0
WHERE tt.taxonomy = 'product_cat'
AND t.term_id IN ({$categories})");
// MiraviaCore::debug($created);
if($sync) {
$string = "{$sync['total_sync']} / ";
}else{
$string = "0 /";
}
if($created) {
$string .= $created['total_created'];
}else{
$string .= "0";
}
return $string;
}
static function get_profiles_by_seller($account) {
global $wpdb;
return $wpdb->get_results("SELECT
p.id
FROM
{$wpdb->prefix}miravia_profiles AS p
LEFT JOIN {$wpdb->prefix}miravia_accounts AS ma ON ma.id = p.accounts_id
WHERE
ma.userid = '{$account}'", ARRAY_A );
}
static function get_rules($id = false, $customWhere = false) {
global $wpdb;
if($id) {
return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}miravia_rules WHERE id = '{$id}'", ARRAY_A );
}else{
if($customWhere) {
return $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}miravia_rules WHERE {$customWhere}", ARRAY_A );
}
}
return $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}miravia_rules", ARRAY_A );
}
static function get_profile_by_product($sku = false) {
global $wpdb;
if($sku) {
$_product_id = wc_get_product_id_by_sku($sku);
$categories = get_the_terms( $_product_id, 'product_cat' );
foreach($categories as $c) {
$profiles = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}miravia_profiles WHERE FIND_IN_SET('{$c->term_id}', categories) > 0", ARRAY_A );
if(is_array($profiles) and count($profiles) > 0) {
return $profiles[0]['id'];
}
}
}
return false;
}
static function get_product_miravia($id = false) {
global $wpdb;
if($id) {
return $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}miravia_products WHERE id_woocommerce = '{$id}'", ARRAY_A );
}
//Is a variation...
// $parent =
return false;
}
static function get_accounts($id = false, $field = 'id') {
global $wpdb;
if($id) {
return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}miravia_accounts WHERE {$field} = '{$id}'", ARRAY_A );
}
return $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}miravia_accounts", ARRAY_A );
}
static function get_miravia_account_default($id = false, $field = 'id') {
global $wpdb;
if($id) {
return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}miravia_accounts WHERE `{$field}` = '{$id}'", ARRAY_A );
}
return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}miravia_accounts LIMIT 1", ARRAY_A );
}
static function order_exist($id) {
global $wpdb;
if($id) {
return $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE `meta_value` = '{$id}'", ARRAY_A );
}
return true;
}
static function get_product($id, $profile = 0) {
global $wpdb;
$queryProfile = "";
if($profile) {
$queryProfile = " AND profile_id = '{$profile}'";
}
return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}miravia_products WHERE id_woocommerce = '{$id}' {$queryProfile}" );
}
static function set_job_product($id, $profile, $job = 0) {
global $wpdb;
if(is_array($id)) {
$where = "id_woocommerce IN(".implode(',', $id).")";
}else{
$where = "id_woocommerce = '$id'";
}
$query = "UPDATE {$wpdb->prefix}miravia_products SET job_id='{$job}', status_text='IN_QUEUE' WHERE profile_id = {$profile} AND ". $where;
$result = $wpdb->query($query);
LOG::add("SET JOB ON PRODUCTS -> " . $query);
LOG::add($result);
}
static function set_job_product_error($id, $profile, $job = 0, $errorText = 'Generic Error') {
global $wpdb;
if(is_array($id)) {
$where = "id_woocommerce IN(".implode(',', $id).")";
}else{
$where = "id_woocommerce = '$id'";
}
$query = "UPDATE {$wpdb->prefix}miravia_products SET job_id='{$job}', lastError='{$errorText}' status_text='FAIL' WHERE profile_id = {$profile} AND ". $where;
$result = $wpdb->query($query);
LOG::add("SET JOB ON PRODUCTS -> " . $query);
LOG::add($result);
}
static function get_order_woocommerce($id) {
$order = wc_get_order($id);
$order_data = new stdClass();
$items_data = [];
$items = false;
if($order) {
$order_data->id = $id;
$order_data->total = $order->get_total();
$items = $order->get_items();
foreach($items as $item) {
array_push($items_data, wc_get_order_item_meta($item->get_id(), '_miravia_order_item_id', true));
}
}
return [$order_data, $items_data];
}
static function getCurrentRules($account_id = 0, $profile_id = 0){
$rules = self::get_rules(false, "(accounts = {$account_id} OR accounts = 0) AND (profile_id = {$profile_id} OR profile_id = '0')");
return $rules;
}
static function applyFilter($feed, $account_id, $profile_id) {
$rules = self::getCurrentRules($account_id, $profile_id);
foreach ($rules as $rule){
$jsonfilter = $rule['rules_json'];
if($jsonfilter){
if($ids = $feed->applyFilter($jsonfilter)){
switch($rule['action_type']){
case 'remove':
$feed->removeProductsById($ids);
break;
case 'only':
$feed->keepProductsById($ids);
break;
case 'price_stock':
$detail = json_decode($rule['action_json'], true);
if(is_array($detail)){
$field = $detail['field'];
$operator = $detail['operator'];
$value = $detail['value'];
$feed->applyNumericFieldAction($ids, $field, $operator, $value);
}
break;
case 'name':
$detail = json_decode($rule['action_json'], true);
if(is_array($detail)){
$field = $detail['field'];
$stringvalue = $detail['stringvalue'];
$feed->applyTextFieldAction($ids, $field, $stringvalue);
}
break;
case 'logistics':
$detail = json_decode($rule['action_json'], true);
if(is_array($detail)){
$delivery = $detail['delivery'];
$warehouse = $detail['warehouse'];
$feed->applyLogisticsAction($ids, $delivery, $warehouse);
}
break;
}
}
}
}
return $feed;
}
static function set_error_product_job($sku, $job = 0, $error = '') {
global $wpdb;
$query = "UPDATE {$wpdb->prefix}miravia_products SET lastError='{$error}', status_text='ERROR' WHERE sku = '{$sku}' AND job_id='{$job}'";
$result = $wpdb->query($query);
}
static function set_status_job($job = 0, $status = '') {
global $wpdb;
if($job) {
$query = "UPDATE {$wpdb->prefix}miravia_products SET status_text='{$status}' WHERE job_id='{$job}'";
$result = $wpdb->query($query);
}
}
static function clear_job($job = 0) {
global $wpdb;
if($job) {
$query = "UPDATE {$wpdb->prefix}miravia_products SET status_text='ND', job_id='', lastError='' WHERE job_id='{$job}'";
$result = $wpdb->query($query);
}
}
static function disconnect_product($id) {
global $wpdb;
$query = "UPDATE {$wpdb->prefix}miravia_products SET status_text='Disconected', lastError='', last_updated='NOW()', id_miravia='' WHERE id_woocommerce = '{$id}'";
$wpdb->query($query);
}
static function set_id_miravia_product_job($sku, $job = 0, $id = '') {
global $wpdb;
if($id === false) {
$query = "UPDATE {$wpdb->prefix}miravia_products SET status_text='DONE', lastError='', last_updated='NOW()' WHERE sku = '{$sku}' AND job_id='{$job}'";
}else{
$query = "UPDATE {$wpdb->prefix}miravia_products SET id_miravia='{$id}',status_text='DONE', lastError='', last_updated='NOW()' WHERE sku = '{$sku}' AND job_id='{$job}'";
}
$result = $wpdb->query($query);
}
static function get_miravia_category($product, $profile) {
$cat = $product->get_category_ids();
$category_profile_search = explode(',', $profile['categories']);
foreach($cat as $c) {
if(in_array($c, $category_profile_search)) {
return $c;
}
}
return 0;
}
static function get_attributes(WC_Product $_product, $attrs) {
$attrs = $_product->get_attributes();
// (
// [color] => WC_Product_Attribute Object
// (
// [data:protected] => Array
// (
// [id] => 0
// [name] => Color
// [options] => Array
// (
// [0] => Rojo
// )
// [position] => 0
// [visible] => 1
// [variation] =>
// )
// )
// )
// var_dump($attrs);
// die();
$attributes = [];
foreach($attrs as $key => $v) {
if(count($v['options']) == 1) {
$value = $v['options'][0];
}else{
$value = $v['options'];
}
$attributes[self::get_key_miravia_attr($key, $attrs)] = $value;
}
return $attributes;
// $map_attrs = get_term_meta($_product->get_category_ids(), "_miravia_attr", true);
}
static function get_key_miravia_attr($key, $attrs) {
if(isset($attrs->attr)) {
$attrs = $attrs->attr;
}
foreach($attrs as $k => $v) {
// MiraviaCore::debug(array($key, $attrs, $k, 'attribute_pa_'.$k == $key));
if($k == $key or 'pa_'.$k == $key or 'attribute_pa_'.$k == $key) {
return $v;
}
}
return false;
}
static function select_category($categories, $args = array('select' => '0', 'name' => 'category')) {
$html = "<select name='{$args['name']}'>";
$html .= "<option value='0'>Seleccione</option>";
foreach($categories as $c) {
// die(var_dump($c['children']));
if(isset($c['children'])) {
$html .= self::get_children_category_select($c, $args);
}else{
$selected_html = "";
if($args['select'] == $c['category_id']) { $selected_html = "selected='selected'"; }
$html .= "<option {$selected_html} value='{$c['category_id']}'>{$c['name']}</option>";
}
}
$html .= "</select>";
return esc_html($html);
}
static function get_children_category_select($categories, $args = array('select' => '0', 'name' => 'category')) {
// die(var_dump($categories));
$html = "<optgroup label='{$categories['name']}'>";
foreach($categories['children'] as $c) {
// die(var_dump($c));
if(isset($c['children'])) {
$html .= self::get_children_category_select($c, $args);
}else{
$selected_html = "";
if($args['select'] == $c['category_id']) { $selected_html = "selected='selected'"; }
$html .= "<option {$selected_html} value='{$c['category_id']}'>{$c['name']}</option>";
}
}
$html .= "</optgroup>";
return esc_html($html);
}
static function get_jobs() {
global $wpdb;
$query = "SELECT
DISTINCT(p.job_id),
COUNT(DISTINCT(p.id_woocommerce)) as total,
ac.token,
f.id,
p.updated
FROM {$wpdb->prefix}miravia_products AS p
INNER JOIN {$wpdb->prefix}miravia_profiles AS f ON p.profile_id = f.id
INNER JOIN {$wpdb->prefix}miravia_accounts AS ac ON f.accounts_id = ac.id
WHERE
p.job_id != '0' and p.job_id != ''
GROUP BY p.job_id, ac.token, f.id";
return $wpdb->get_results( $query, ARRAY_A );
}
static function get_job_detail($id = false) {
global $wpdb;
if(!$id) {
return [];
}
$query = "SELECT
p.*
FROM {$wpdb->prefix}miravia_products AS p
INNER JOIN {$wpdb->prefix}miravia_profiles AS f ON p.profile_id = f.id
INNER JOIN {$wpdb->prefix}miravia_accounts AS ac ON f.accounts_id = ac.id
WHERE
p.job_id = '{$id}'";
return $wpdb->get_results( $query, ARRAY_A );
}
static function resync_stock() {
global $wpdb;
//Actualizar todos los stocks como que necesitan actualizar y responder con un array de tokens de cuentas
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}postmeta AS pm1
INNER JOIN {$wpdb->prefix}miravia_products AS pm2
ON pm1.post_id = pm2.id_woocommerce
SET pm1.meta_value = %s
WHERE pm1.meta_key = %s",
'1',
'_miravia_need_update'
)
);
return MiraviaCore::get_accounts();
}
static function procesarFeed() {
$document = sanitize_text_field($_REQUEST['document']);
$seller = sanitize_text_field($_REQUEST['seller']);
$account = MiraviaCore::get_accounts($seller,'userid');
if(!$account) {
LOG::add("Accont not found on process feed {$document}", false, 'feeds');
return false;
}
$id_seller = $account['id'];
if($id_seller) {
$link = new MiraviaLink($account['token']);
$data = $link->getFeedResult($document);
if (!empty($data)) {
$info = json_decode($data,true);
if(is_array($info) && isset($info['success']) && $info['success'] == false){
$ret['success'] = false;
$ret['error'] = $info['error'];
$txlog .= $info['error'];
}else {
$r = self::processFeedResult($seller, $data);
if ($r != 'ok') {
LOG::add("Error when process feed {$document}: {$r}", false, 'feeds');
return false;
}
}
}
}else{
LOG::add("ID Accont not found on process feed {$document}", false, 'feeds');
return false;
}
}
static public function processFeedResult($seller, $data){
if(!is_array($data)){
$data = json_decode($data, true);
}
$account = MiraviaCore::get_accounts($seller,'userid');
if(!$account){
return 'error: invalid seller';
}
if(!is_array($data)){
return 'error: invalid data';
}
foreach($data as $id => $info){
global $wpdb;
$status = isset($info['status']) ? $info['status'] : false;
$message = '?';
$item_id = '';
if($status){
$message='';
if($status=='FAIL'){
$status = 'error';
$detail = isset($info['detail']) ? $info['detail'] : false;
$message = self::getFeedDetail($detail);
}elseif($status=='SUCCESS'){
$status = 'created';
$item_id = $info['id'];
}
}else{
$status = 'error';
$message = 'Invalid response from server: ' . json_encode($info);
}
$update_fields = [
'status_text' => $status,
'lastError' => $message,
'job_id' => '',
];
if(!empty($item_id)){
$update_fields['id_miravia'] = $item_id;
}
$wpdb->update($wpdb->prefix.'miravia_products', $update_fields, array('sku' => $id,'job_id' => $_GET['feed']));
}
return 'ok';
}
static protected function getFeedDetail($tx)
{
if(is_array($tx)){
$data = $tx;
}else {
$data = json_decode($tx, true);
}
$msg = isset($data['message']) ? $data['message'] : false;
if($msg){
$data = is_array($msg) ? $msg : json_decode($msg, true);
$errorCode = isset($data['errorCode']) ? $data['errorCode'] : '0';
$errorMsg = isset($data['errorMsg']) ? $data['errorMsg'] : '0';
$errorFirst = isset($data['errors'][0]['code']) ? $data['errors'][0]['code'] : '0';
$detail = "[$errorCode] $errorMsg ($errorFirst)";
return $detail;
}
$detail = $tx;
return $detail;
}
}
}
+77
View File
@@ -0,0 +1,77 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
class MIRAVIADB {
static function install() {
global $wpdb;
$installed_ver = get_option( "miravia_db_version", 0 );
if ($installed_ver != MIRAVIA_DB_VERSION) {
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$wpdb->prefix}miravia_accounts (
id mediumint(9) NOT NULL AUTO_INCREMENT,
created datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated datetime DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
name tinytext NOT NULL,
token varchar(200) NOT NULL,
userid varchar(30) DEFAULT '' NOT NULL,
config TEXT,
lang varchar(10) NOT NULL,
email varchar(100) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
$sql .= "CREATE TABLE {$wpdb->prefix}miravia_profiles (
id mediumint(9) NOT NULL AUTO_INCREMENT,
created datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated datetime DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
name tinytext NOT NULL,
accounts_id varchar(200) NOT NULL,
categories varchar(250) DEFAULT '' NOT NULL,
miravia_category bigint NOT NULL DEFAULT 0,
config TEXT,
PRIMARY KEY (id)
) $charset_collate;";
$sql .= "CREATE TABLE {$wpdb->prefix}miravia_products (
id mediumint(9) NOT NULL AUTO_INCREMENT,
created datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated datetime DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
last_updated datetime DEFAULT '0000-00-00 00:00:00',
id_woocommerce BIGINT NOT NULL,
sku varchar(100) NOT NULL,
id_miravia BIGINT NOT NULL,
job_id VARCHAR(100) NOT NULL DEFAULT '0',
stock INT NOT NULL DEFAULT 0,
price FLOAT(50,2) NULL DEFAULT NULL,
sale_price FLOAT(50,2) NULL DEFAULT NULL,
profile_id INT DEFAULT 0 NOT NULL,
lastError varchar(255) DEFAULT '',
status_text varchar(100) DEFAULT '',
PRIMARY KEY (id)
) $charset_collate;";
$sql .= "CREATE TABLE {$wpdb->prefix}miravia_rules (
id mediumint(9) NOT NULL AUTO_INCREMENT,
created datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated datetime DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
name_rule VARCHAR(100) NOT NULL,
accounts INT NOT NULL DEFAULT 0,
profile_id INT NOT NULL DEFAULT 0,
rules_json TEXT,
action_json TEXT,
action_type VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
//Run SQL
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
//Update bd version plugin
update_option( 'miravia_db_version', MIRAVIA_DB_VERSION );
LOG::add("La base de datos ha sido actualizada a la versión " . MIRAVIA_DB_VERSION);
}
}
}
@@ -0,0 +1,11 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if(!class_exists('MIRAVIA_LOCAL')) {
class MIRAVIA_LOCAL {
function __construct() {
}
}
}
+60
View File
@@ -0,0 +1,60 @@
<?php
if(!class_exists('LOG')) {
class LOG {
/**
* add message to file
*
* @param string $msg String to register
* @param int|string $element Any ID or String
* @param string $type Default: user
* @return int|boolean
*/
static function add($msg, $element = false, $type = false ) {
if(!$type) {
$type = defined('LOG_DEFAULT_FILE') ? LOG_DEFAULT_FILE : 'user';
}
$root = defined('LOG_FOLDER') ? LOG_FOLDER : '/';
//If an element is defined and provided, it will be separated into files by element
$divide = defined('LOG_DIVIDE_ELEMENT');
$y = date('Y');
$m = date('m');
//Create htaccess file if no exist
if(!file_exists($root . "logs/.htaccess")) {
file_put_contents($root . "logs/.htaccess", 'Deny from all');
}
$fileDest = $root . "logs/{$y}/{$m}/";
if(!file_exists($fileDest)) {
$dir = mkdir($fileDest, 0775, true);
}
if($element && $divide) {
return file_put_contents($fileDest.$type."_{$element}.log", self::data($msg, $element, $type),FILE_APPEND);
}
return file_put_contents($fileDest.$type.".log", self::data($msg, $element, $type),FILE_APPEND);
}
/**
* Structure data
*
* @param string $msg
* @param int|string $element
* @param string $type
* @return string
*/
private static function data($msg, $element = '0', $type) {
if(is_array($msg) || is_object($msg)) {
return "[".date('Y-m-d H:i:s') . "] ". json_encode($msg, JSON_PRETTY_PRINT) . " \r\n";
}
return "[".date('Y-m-d H:i:s') . "] $msg \r\n";
}
}
}
@@ -0,0 +1,120 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('MIRAVIABase') ) {
class MIRAVIABase {
private $url = "https://api.miravia.es/rest";
private $token = "50000701b37tSEfdXdiuAq0yiimwLWEyVIf3lsRgyp2yLqwX61b895957sebFoS";
private $appSecret = "8nxjS2TDTevc0AwmXh7AbKQL4XnuaE7t";
private $appKey = "500406";
private $client = false;
function __construct(){
if(!$this->client) {
$this->client = new IopClient($this->url,$this->appKey,$this->appSecret);
}
}
function secure(){
if(!$this->client) {
die(json_encode(array('error' => true, 'message' => 'No se ha establecido la conexión')));
}
}
function get($endpoint = '', $params = array()) {
$this->secure();
$request = new IopRequest($endpoint,'GET');
if($params) {
foreach($params as $k => $v) {
$request->addApiParam($k,$v);
}
}
try{
$result = $this->client->execute($request,$this->token);
}catch(Exception $e) {
return $this->error_control($e);
}
$resp = json_decode($result, true);
return $this->response($resp);
}
function post($endpoint = '', $data = array()) {
$this->secure();
$request = new IopRequest($endpoint);
if($data) {
foreach($data as $k => $v) {
$request->addApiParam($k,$v);
}
}
try{
$result = $this->client->execute($request, $this->token);
}catch(Exception $e) {
return $this->error_control($e);
}
$resp = json_decode($result, true);
return $this->response($resp);
}
function response($result) {
if(isset($result['data'])) {
return $result['data'];
}else{
//Control de errores
return $this->error_control($result);
}
}
function error_control($resp) {
// die('<pre>'.print_r($resp, true).'</pre>');
if($resp->code != '0') {
if(isset($resp->detail)) {
$errors = $resp->detail;
}else{
$errors = [$resp->message];
}
return array(
'code' => $resp->code,
'errors' => $errors
);
}else{
return array(
'code' => '-1',
'errors' => array($resp)
);
}
}
function get_images_batch($batch_id) {
return $this->get('/image/response/get', array('batch_id' => $batch_id));
}
function get_brands($page) {
$to = $page * 20;
$from = $to - 20;
return $this->get('/category/brands/query', array('start_row' => $from, "page_size" => $to));
}
function get_orders($args = array()) {
return $this->get('/orders/get', $args);
}
function get_order($args = array()) {
return $this->get('/order/get', $args);
}
function get_orders_items($args = array()) {
return $this->get('/order/items/get', $args);
}
function get_products($args = array()) {
return $this->get('/products/get', $args);
}
}
}
+128
View File
@@ -0,0 +1,128 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('MVOrder') ) {
class MVOrder {
function __construct(){
}
function create($order_miravia = false) {
//Obtener datos del pedido
$defaultStatus = get_option('miravia_default_status', 'wc-processing');
$order = wc_create_order();
if($order_miravia['address_shipping']['customerEmail']) {
$username = $order_miravia['address_shipping']['customerEmail'];
}else{
$username = $order_miravia['order_number'];
}
$user_id = username_exists( $username );
$billing_address = array(
'first_name' => $order_miravia['address_billing']['first_name'],
'last_name' => $order_miravia['address_billing']['last_name'],
'email' => $order_miravia['address_shipping']['customerEmail'],
'phone' => $order_miravia['address_billing']['phone'],
'address_1' => "{$order_miravia['address_billing']['address1']} {$order_miravia['address_billing']['address2']} {$order_miravia['address_billing']['address3']}",
'address_2' => "{$order_miravia['address_billing']['address4']} {$order_miravia['address_billing']['address5']}",
'city' => $order_miravia['address_billing']['city'],
'state' => $order_miravia['address_billing']['country'], //Ver esto
'postcode' => $order_miravia['address_billing']['post_code'],
'country' => $order_miravia['address_billing']['country'],
);
$shipping_address = array(
'first_name' => $order_miravia['address_shipping']['firstName'],
'last_name' => $order_miravia['address_shipping']['lastName'],
'address_1' => "{$order_miravia['address_shipping']['address1']} {$order_miravia['address_shipping']['address2']} {$order_miravia['address_shipping']['address3']}",
'address_2' => "{$order_miravia['address_shipping']['address4']} {$order_miravia['address_shipping']['address5']}",
'city' => $order_miravia['address_shipping']['city'],
'state' => $order_miravia['address_shipping']['country'], //Ver esto
'postcode' => $order_miravia['address_shipping']['postCode'],
'country' => $order_miravia['address_shipping']['country'],
);
$order->set_address( $billing_address, 'billing' );
$order->set_address( $shipping_address, 'shipping' );
update_post_meta($order->ID, '_miravia_order_id', $order_miravia['order_number']);
update_post_meta($order->ID, '_miravia_account_id', $order_miravia['account_id']);
// update_post_meta($order->ID, '_miravia_order_id', $order_miravia['order_number']);
// 1. User doesn't exist - Create it - send email - set address and define
if ( ! $user_id && $user_id == false ) {
$email = $username;
$password = wp_generate_password( 12, false );
$first_name = $order_miravia['address_shipping']['firstName'];
$last_name = $order_miravia['address_shipping']['lastName'];
$user_data = array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
'role' => 'customer',
);
$user_id = wp_insert_user( $user_data );
// Update Billing and shipping user data
foreach( $billing_address as $key => $value ) {
update_user_meta( $user_id, 'billing_' . $key, $value );
}
foreach( $shipping_address as $key => $value ) {
update_user_meta( $user_id, 'shipping_' . $key, $value );
}
// No send notification on create user
// WC()->mailer()->get_emails()['WC_Email_Customer_New_Account']->trigger( $user_id, $password, true );
}
// For calculating taxes on items
$calculate_taxes_for = array(
'country' => ! empty($shipping_address['country']) ? $shipping_address['country'] : $billing_address['country'],
'state' => ! empty($shipping_address['state']) ? $shipping_address['state'] : $billing_address['state'],
'postcode' => ! empty($shipping_address['postcode']) ? $shipping_address['postcode'] : $billing_address['postcode'],
'city' => ! empty($shipping_address['city']) ? $shipping_address['city'] : $billing_address['city'],
);
foreach($order_miravia['order_items'] as $k => $item) {
$this->add_product($order, $item, $calculate_taxes_for);
}
$order->set_customer_id( $user_id );
$order->set_currency( get_woocommerce_currency() );
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
$note = 'From MIRAVIA Order';
$order->add_order_note( $note );
$order->set_customer_note( $note );
$order->set_status($defaultStatus, "Created from Miravia");
$order->calculate_totals();
$order->update_status('autoquote', true); // $order->save() is already included with update_status() method
}
function add_product($order, $product, $calculate_taxes_for) {
$product_sku = wc_get_product_id_by_sku($product['sku']);
$_product = $product_sku ? wc_get_product( $product_sku ) : false;
$cantidad = 1; //TODO ver esto
$priceWithOutTax = $product['item_price'] - (($product['item_price'] - $product['tax_amount']) / 100);
$dataLine = [
'name' => $product['name'],
'subtotal' => $priceWithOutTax,
'total' => $product['item_price']
];
$item_id = $order->add_product($_product, $cantidad, $dataLine);
wc_update_order_item_meta($item_id, '_miravia_delivery_option', $product['delivery_option_sof']);
wc_update_order_item_meta($item_id, '_miravia_order_item_id', $product['order_item_id']);
$line_item = $order->get_item( $item_id, false ); // Get the WC_Order_Item_Product Object instance from the Item Id
$line_item->calculate_taxes($calculate_taxes_for); // <== Calculating taxes
$line_item->save(); // Save data to WC_Order_Item_Product Object
}
}
}
@@ -0,0 +1,125 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('ARIProduct') ) {
class ARIProduct {
private $product = false;
private $defaul_image = '';
private $category = 0;
public $ItemId = 0;
public $Images = array('Image' => array());
public $Attributes = array();
public $Skus = array('Sku' => array());
public $PrimaryCategory = 0;
function __construct($id){
$this->product = wc_get_product($id);
//Por defecto de la categoria
$terms = get_the_terms( $this->product->get_id(), 'product_cat' );
$this->category = $terms[0];
//Exist Product
$miraviaId = get_post_meta($id, '_miravia_product_id', true);
if($miraviaId) {
$this->ItemId = $miraviaId;
}
$atributos_guardados = get_term_meta($this->category->term_id, "_miravia_attr", true);
if($atributos_guardados) {
$this->Attributes = $atributos_guardados;
}
//Completar los datos básicos
$this->PrimaryCategory = get_term_meta($this->category->term_id, "_miravia_category", true);
$this->Attributes['name'] = $this->product->get_name();
$this->Attributes['description'] = $this->product->get_description();
//Cargar imagenes
$attachment_ids = $this->product->get_gallery_image_ids();
foreach( $attachment_ids as $attachment_id ) {
$this->Images['Image'][] = $this->defaul_image;
}
$this->get_skus();
}
function get_skus() {
if($this->product->is_type('simple')) {
$skus = [$this->product];
}else{
$skus = $this->product->get_available_variations();
}
foreach($skus as $index => $sku) {
$this->Skus['Sku'][$index]["SellerSku"] = $sku->get_sku();
$this->Skus['Sku'][$index]["quantity"] = $sku->get_stock_quantity();
$this->Skus['Sku'][$index]["price"] = $sku->get_regular_price();
if($sku->is_on_sale()) {
$this->Skus['Sku'][$index]["special_price"] = $sku->get_sale_price();
}
$this->Skus['Sku'][$index]["price"] = $sku->get_regular_price();
if($sku->get_manage_stock() === false) {
if($sku->get_stock_status() == "instock") {
$stock_available = intval(100); //Stock por defecto
}else{
$stock_available = 0;
}
}else{
$stock_available = $sku->get_stock_quantity();
}
$this->Skus['Sku'][$index]["quantity"] = $stock_available;
$this->Skus['Sku'][$index]["package_height"] = $sku->get_height();
$this->Skus['Sku'][$index]["package_length"] = $sku->get_length();
$this->Skus['Sku'][$index]["ean_code"] = '0'; //Implementar
$this->Skus['Sku'][$index]["package_width"] = $sku->get_width();
$this->Skus['Sku'][$index]["package_weight"] = $sku->get_weight();
$this->Skus['Sku'][$index]["package_content"] = ''; //Implementar
$attachment_ids = $sku->get_gallery_image_ids();
foreach( $attachment_ids as $attachment_id ) {
$this->Skus['Sku'][$index]["Images"]["Image"][] = $this->defaul_image;
}
}
}
function send() {
global $MIRAVIAWOO;
$send_product = json_encode(array("Request" => array("Product" => array(
'Attributes' => $this->Attributes,
'PrimaryCategory' => $this->PrimaryCategory,
'Skus' => $this->Skus,
'Images' => $this->Images
))));
$result_product = $MIRAVIAWOO->client->post('/product/create', array(
'body' => $send_product
));
return $result_product;
}
function update() {
global $MIRAVIAWOO;
$send_product = json_encode(array("Request" => array("Product" => array(
'ItemId' => $this->ItemId,
'Attributes' => $this->Attributes,
'PrimaryCategory' => $this->PrimaryCategory,
'Skus' => $this->Skus,
'Images' => $this->Images
))));
$result_product = $MIRAVIAWOO->client->post('/product/update', array(
'payload' => $send_product
));
error_log(json_encode($result_product));
return $result_product;
}
}
}
+211
View File
@@ -0,0 +1,211 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('MVProduct') ) {
class MVProduct {
public $product = false;
public $id = 0;
public $id_woocommerce = 0;
public $id_miravia = 0;
public $profile = 0;
public $defaul_image = '';
public $last_updated = '0000-00-00 00:00:00';
function __construct($id, $profile = 0) {
global $wpdb;
$saved = MiraviaCore::get_product($id, $profile['id']);
$ean = '';
$keyEAN = get_option('miravia_ean_key', '');
if($keyEAN != '') {
$ean = get_post_meta($id, $keyEAN, true);
}
$defaultBrand = get_option('miravia_default_brand', 'No Brand');
$productBrand = get_post_meta($id, '_miravia_brand_product', true);
if($productBrand) {
$defaultBrand = $productBrand;
}
//Units
$defaultUnit = get_option('miravia_default_unit', 'units');
$defaultUnitValue = get_option('miravia_default_unit_value', '1');
$productUnit = get_post_meta($id, '_miravia_unit', true);
$productUnitValue = get_post_meta($id, '_miravia_unit_value', true);
if($productUnit) {
$defaultUnit = $productUnit;
}
if($productUnitValue) {
$defaultUnitValue = $productUnitValue;
}
// LOG::add($saved);
if($saved) {
$this->id = $saved->id;
$this->id_woocommerce = $saved->id_woocommerce;
$this->id_miravia = $saved->id_miravia;
$this->profile = $saved->profile_id;
$this->last_updated = $saved->last_updated;
$_product = wc_get_product(intval($this->id_woocommerce));
}else{
$this->id_woocommerce = $id;
//Load product
$_product = wc_get_product(intval($this->id_woocommerce));
$this->profile = $profile['id'];
$wpdb->insert($wpdb->prefix.'miravia_products', array(
'id_woocommerce' => $this->id_woocommerce,
'sku' => $_product->get_sku() ?: $id,
'id_miravia' => $this->id_miravia,
'profile_id' => $this->profile,
'stock' => $_product->get_regular_price(),
'price' => $_product->get_regular_price(),
'sale_price' => $_product->get_sale_price(),
));
$this->id = $wpdb->insert_id;
}
if($_product->get_manage_stock() === false) {
if($_product->get_stock_status() == "instock") {
$stock_available = get_option('_miravia_default_stock', 100); //Stock por defecto
}else{
$stock_available = 0;
}
}else{
$stock_available = $_product->get_stock_quantity();
}
$local_category = MiraviaCore::get_miravia_category($_product, $profile);
// $attributes_product = MiraviaCore::get_attributes($_product, $local_category);
// $attributes_product = $_product->get_attributes();
// die("<pre>".print_r($attributes_product, true)."</pre>");
$this->product = new MiraviaProduct();
$this->product->id = $this->id_woocommerce;
$this->product->id_miravia = $this->id_miravia;
$this->product->sku = $_product->get_sku() ?: $id;
$this->product->lang = 'es-ES'; // ISO lang cod
$this->product->name = $_product->get_name();
$this->product->short_description = $_product->get_short_description();
$this->product->description = $_product->get_description();
$this->product->brand = $defaultBrand;
$this->product->model = '';
$this->product->ean_code = $ean;
$this->product->warranty = '';
$this->product->id_category = $profile['miravia_category'];
$this->product->id_brand = '';
$this->product->images = []; // Se completa después
$this->product->video = '';
$this->product->price = $_product->get_regular_price();
$this->product->special_price = $_product->get_sale_price();
$this->product->width = $_product->get_width();
$this->product->height = $_product->get_height();
$this->product->length = $_product->get_length();
$this->product->weight = $_product->get_weight();
$this->product->delivery = get_option('miravia_transport_mode', 'dbm');
$this->product->info = array(
'id_category' => $local_category
);
//$this->product->extra_attributes = []; # TODO agregar caracteristicas
//Attrs
$this->product->addAttr("unit_count_type", $defaultUnit);
$this->product->addAttr("Unit_Count", $defaultUnitValue);
if($_product->is_type( 'variable' )) {
$combinations = [];
$variations = $_product->get_available_variations();
// MiraviaCore::debug($variations);
foreach($variations as $v) {
if($v['is_in_stock'] == true) {
if($v['max_qty'] == "") {
$stock_available = get_option('_miravia_default_stock', 100); //Stock por defecto
}else{
$stock_available = $v["max_qty"];
}
}else{
$stock_available = 0; //Stock por defecto
}
$ean = '';
$keyEAN = get_option('miravia_ean_key', '');
if($keyEAN != '') {
$ean = get_post_meta($v['variation_id'], $keyEAN, true);
}
$com = new MiraviaCombination();
$com->sku = $v['sku'];
$com->ean_code = $ean;
$com->price = $v['display_regular_price'];
$com->special_price = $v['display_price'];
$com->quantity = intval($stock_available);
$com->images = [];
$com->width = $v['dimensions']['width'];
$com->height = $v['dimensions']['height'];
$com->length = $v['dimensions']['length'];
$com->weight = $v['weight'];
$com->variation = [];
$encontrados = [];
$labels = [];
$lastAttribute = "";
foreach($v['attributes'] as $k => $a) {
$term_name = ( $term = get_term_by( 'slug', $a, str_replace("attribute_", "", $k) ) ) ? $term->name : $a;
$nameWithOutAttribute = str_replace("attribute_", "", $k);
if(isset($encontrados[$nameWithOutAttribute])) {
$encontrados[$nameWithOutAttribute] .= $term_name;
}else{
$encontrados[$nameWithOutAttribute] = $term_name;
}
}
foreach($encontrados as $b => $c) {
$com->addVariation($b, $c);
}
$combinations[] = $com;
}
$this->product->combinations = $combinations;
}
//Set stock
$this->product->quantity = $stock_available;
//Load images
$attachment_ids = $_product->get_gallery_image_ids();
$sizeImage = get_option('miravia_size_image', 'single-post-thumbnail');
$featured = wp_get_attachment_image_src( get_post_thumbnail_id( $this->product->id ), $sizeImage );
if($featured and is_array($featured)) {
$this->product->images[] = $featured[0];
}
foreach( $attachment_ids as $attachment_id ) {
$this->product->images[] = wp_get_attachment_image_src( $attachment_id, $sizeImage )[0];
}
}
public function getData() {
$error = false;
//Control de productos sin variaciones ni precio
if(($this->product->price == 0 or $this->product->price == "") and count($this->product->combinations) == 0) {
LOG::add("Producto {$this->product->id} no agregado por no tener precio y no tener variaciones", false, 'errors_products_send');
$error = true;
}
if(count($this->product->images) == 0) {
LOG::add("Producto {$this->product->id} no tiene imagenes, no enviado", false, 'errors_products_send');
$error = true;
}
if($error) {
return false;
}
return $this->product;
}
function send() {
return false;
}
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('ARIUi') ) {
class ARIUi {
function __construct(){
}
}
}
@@ -0,0 +1,90 @@
<?php
require_once dirname(__FILE__) . '/MiraviaLink.php';
class MiraviaCategory extends MiraviaLink
{
public function getCategories($treeFormat = false)
{
$url = $this->api_url . '/category/tree';
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
$this->last_error = $resp['error'];
return false;
}
if(isset($resp['data'])){
if($treeFormat) {
return $resp['data'];
}else{
return $this->treeToList($resp['data']);
}
}
return false;
}
public function getBrands()
{
$url = $this->api_url . '/category/brands';
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
$this->last_error = $resp['error'];
return false;
}
if(isset($resp['data'])){
return $resp['data'];
}
return false;
}
public function getAttributes($id)
{
$url = $this->api_url . '/category/' . $id . '/attributes';
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
$this->last_error = isset($resp['error']) ? $resp['error'] : $ret;
return false;
}
if(isset($resp['data'])){
return $resp['data'];
}
return false;
}
private function treeToList($tree = [], $prefix = '')
{
$ret = [];
$separator = $prefix=='' ? '' : ' / ';
foreach ($tree as $item) {
$name = $item['name'];
if(isset($item['children'])) {
$children = $this->treeToList($item['children'], $prefix . $separator . $name);
foreach($children as $key => $child){
$ret[] = [
'id' => $child['id'],
'name' => $child['name']
];
}
}else{
$ret[] = [
'id' => $item['category_id'],
'name' => $prefix . $separator . $name
];
}
}
return $ret;
}
}
@@ -0,0 +1,59 @@
<?php
class MiraviaCombination
{
public $sku = '';
public $ean_code = '';
public $price = 0;
public $special_price = 0;
public $quantity = 0;
public $images = [];
public $width = 0;
public $height = 0;
public $length = 0;
public $weight = 0;
public $variation = [];
public function addVariation($name, $value)
{
$this->variation[$name] = $value;
}
public function getData()
{
$combination = [];
$this->weight = round((float)$this->weight, 2);
foreach ($this as $key => $value){
if($key=='ean_code' && empty($value)){
continue;
}
if($key === 'images' && is_array($value) && !empty($value)){
$combination['Images'] = ['Image' => $value];
} else if($key !== 'images') {
$combination[$key] = $value;
}
}
return $combination;
}
public function getStockData()
{
$combination = [
'sku' => $this->sku,
'price' => $this->price,
'special_price' => $this->special_price,
'quantity' => $this->quantity >= 0 ? $this->quantity : 0
];
return $combination;
}
public function getOnlyStockData()
{
$combination = [
'sku' => $this->sku,
'quantity' => $this->quantity >= 0 ? $this->quantity : 0
];
return $combination;
}
}
@@ -0,0 +1,243 @@
<?php
class MiraviaFeed
{
private $products = [];
public $miravia_feed_id = '';
public $debug = false;
public $default_width = 0;
public $default_height = 0;
public $default_length = 0;
public $default_weight = 0;
public function __construct()
{
//
}
public function setProducts($products)
{
foreach($products as $p) {
$this->products[$p->id] = $p;
}
}
public function addProduct(MiraviaProduct $product)
{
$this->products[$product->id] = $product;
}
public function getProducts()
{
return $this->products;
}
public function getProductsIds()
{
return array_keys($this->products);
}
public function getJsonCreate($offset = 0, $limit = 200)
{
$ret = [];
$prods = array_slice($this->products, $offset, $limit);
foreach ($prods as $p) {
if($p->created==0){
$this->setDefaultValues($p);
$ret[] = $p->getData();
}
}
if(empty($ret)){
return false;
}
$ret = [
'products' => $ret
];
$flags = $this->debug ? JSON_PRETTY_PRINT : 0;
return json_encode($ret, $flags);
}
public function getJsonUpdate($offset = 0, $limit = 200)
{
$ret = [];
$prods = array_slice($this->products, $offset, $limit);
foreach ($prods as $p) {
if($p->created==1 && strlen($p->id_miravia)>14){
$this->setDefaultValues($p);
$ret[] = $p->getData();
}
}
if(empty($ret)){
return false;
}
$ret = [
'products' => $ret
];
$flags = $this->debug ? JSON_PRETTY_PRINT : 0;
return json_encode($ret, $flags);
}
public function setDefaultValues(MiraviaProduct &$product)
{
if((float)$product->weight == 0) $product->weight = $this->default_weight;
if((int)$product->height == 0) $product->height = $this->default_height;
if((int)$product->width == 0) $product->width = $this->default_width;
if((int)$product->length == 0) $product->length = $this->default_length;
}
public function getJsonUpdateStock($onlyStock = false, $offset = 0, $limit = 200)
{
$ret = [];
$prods = array_slice($this->products, $offset, $limit);
foreach ($prods as $p) {
if(!empty($p->id_miravia)){
if($onlyStock) {
$ret[] = $p->getOnlyStockData();
}else{
$ret[] = $p->getStockData();
}
}
}
if(empty($ret)){
return false;
}
$ret = [
'products' => $ret
];
$flags = $this->debug ? JSON_PRETTY_PRINT : 0;
return json_encode($ret, $flags);
}
public function applyFilter($jsonFilter)
{
if(empty($jsonFilter)){
return false;
}
$filter = new MiraviaFilter($jsonFilter);
if($ids = $filter->parseRecords($this->products)){
return $ids;
}
return false;
}
public function removeProductsById($ids)
{
if(!is_array($ids)){
$ids = [$ids];
}
foreach ($ids as $id){
unset($this->products[$id]);
}
}
public function keepProductsById($ids)
{
if(!is_array($ids)){
$ids = [$ids];
}
$keys = array_keys($this->products);
$ids = array_diff($keys, $ids);
foreach ($ids as $id){
unset($this->products[$id]);
}
}
public function applyNumericFieldAction($ids, $fields, $action, $value)
{
$afields = explode(',',$fields);
foreach ($afields as $field){
foreach ($ids as $id){
if(isset($this->products[$id])){
switch ($action){
case 'increment':
if($this->products[$id]->{$field}) {
$this->products[$id]->{$field} = (float)$this->products[$id]->{$field} + (float)$value;
}
foreach($this->products[$id]->combinations as &$combi){
$combi->{$field} += (float)$value;
}
break;
case 'decrement':
if($this->products[$id]->{$field}) {
$this->products[$id]->{$field} -= (float)$value;
}
foreach($this->products[$id]->combinations as &$combi){
$combi->{$field} -= (float)$value;
}
break;
case 'increment_percent':
if($this->products[$id]->{$field}) {
$this->products[$id]->{$field} += ((float)$this->products[$id]->{$field} * (float)$value / 100);
}
foreach($this->products[$id]->combinations as &$combi){
$combi->{$field} += ((float)$combi->{$field} * (float)$value / 100);
}
break;
case 'decrement_percent':
if($this->products[$id]->{$field}) {
$this->products[$id]->{$field} -= ((float)$this->products[$id]->{$field} * (float)$value / 100);
}
foreach($this->products[$id]->combinations as &$combi){
$combi->{$field} -= ((float)$combi->{$field} * (float)$value / 100);
}
break;
case 'multiply':
if($this->products[$id]->{$field}) {
$this->products[$id]->{$field} = ((float)$this->products[$id]->{$field} * (float)$value );
}
foreach($this->products[$id]->combinations as &$combi){
$combi->{$field} = ((float)$combi->{$field} * (float)$value );
}
break;
case 'set':
if($this->products[$id]->{$field}) {
$this->products[$id]->{$field} = (float)$value;
}
foreach($this->products[$id]->combinations as &$combi){
$combi->{$field} = (float)$value;
}
break;
}
}
}
}
}
public function applyTextFieldAction($ids, $field, $template)
{
foreach ($ids as $id) {
if (isset($this->products[$id])) {
$replaces = [
'name' => $this->products[$id]->name,
'description' => $this->products[$id]->description,
'short_description' => $this->products[$id]->short_description,
'manufacturer' => $this->products[$id]->getInfoValue('manufacturer'),
'supplier' => $this->products[$id]->getInfoValue('supplier'),
'category' => $this->products[$id]->getInfoValue('category'),
];
$this->products[$id]->{$field} = $template;
foreach($replaces as $search => $replace){
$this->products[$id]->{$field} = str_replace('%' . $search .'%', $replace, $this->products[$id]->{$field});
}
}
}
}
public function applyLogisticsAction($ids, $delivery, $warehouse)
{
foreach ($ids as $id) {
if (isset($this->products[$id])) {
$this->products[$id]->delivery = $delivery;
$this->products[$id]->warehouse = $warehouse;
}
}
}
public function getProductCount()
{
return sizeof($this->products);
}
}
@@ -0,0 +1,134 @@
<?php
class MiraviaFilter
{
private $definition;
public function __construct($json)
{
$this->definition = json_decode($json, true);
}
/*
* Return a list of keys
*/
public function parseRecords($products)
{
if(!is_array($this->definition)){
return false;
}
$ret = [];
if(is_array($products)) {
foreach ($products as $key => $product) {
if ($this->parseGroup($this->definition, $product)) {
// condition is met
$ret[] = $product->id;
}else{
//$ret[] = "NO: " . $key;
}
}
}
return $ret;
}
private function parseGroup($condition, $product)
{
$final_result = false;
$rules_parsed = 0;
foreach ($condition as $rule){
$operator = $rule['logical_operator'];
if(!isset($rule['element_rule_id'])){
// group
$result = $this->parseGroup($rule, $product);
}else{
$result = $this->parseCondition($rule['condition'], $product);
}
if($rules_parsed==0){
$final_result = $result;
}else{
switch ($operator){
case 'OR':
$final_result = $final_result || $result;
break;
case 'AND':
$final_result = $final_result && $result;
break;
}
}
}
return $final_result;
}
private function parseCondition($condition, $product)
{
switch ($condition['field']){
case 'product_name':
$field = $product->name;
break;
case 'price':
$field = $product->price;
break;
case 'specific_price':
$field = $product->special_price;
break;
case 'stock':
$field = $product->quantity;
break;
case 'category':
$field = $product->info['id_category'];
break;
case 'supplier':
$field = $product->info['id_supplier'];
break;
case 'manufacturer':
$field = $product->info['id_manufacturer'];
break;
}
switch($condition['operator']){
case 'equal':
$value = $condition['filterValue'][0];
return $field==$value;
case 'not_equal':
$value = $condition['filterValue'][0];
return $field!=$value;
case 'less':
$value = $condition['filterValue'][0];
return $field<$value;
case 'less_or_equal':
$value = $condition['filterValue'][0];
return $field<=$value;
case 'greater':
$value = $condition['filterValue'][0];
return $field>$value;
case 'greater_or_equal':
$value = $condition['filterValue'][0];
return $field>=$value;
case 'begins_with':
$value = $condition['filterValue'][0];
return (substr($field, 0, strlen($value)) === $value);
case 'contains':
$value = $condition['filterValue'][0];
return (stripos($field, $value)!=false);
case 'ends_with':
$value = $condition['filterValue'][0];
return (substr($field, -1 * strlen($value)) === $value);
case 'not_begins_with':
$value = $condition['filterValue'][0];
return !(substr($field, 0, strlen($value)) === $value);
case 'not_contains':
$value = $condition['filterValue'][0];
return !(stripos($field, $value)!=false);
case 'not_ends_with':
$value = $condition['filterValue'][0];
return !(substr($field, -1 * strlen($value)) === $value);
case 'is_null':
case 'is_empty':
return empty($field);
case 'is_not_null':
case 'is_not_empty':
return !empty($field);
}
return false;
}
}
@@ -0,0 +1,63 @@
<?php
class MiraviaLang
{
const ENGLISH = 'en';
const ARABIC = 'ar';
const GERMAN = 'de';
const SPANISH = 'es';
const FRENCH = 'fr';
const INDONESIAN = 'in';
const ITALIAN = 'it';
const HEBREW = 'iw';
const JAPANESE = 'ja';
const KOREAN = 'ko';
const DUTCH = 'nl';
const POLISH = 'pl';
const PORTUGUESE = 'pt';
const RUSSIAN = 'ru';
const THAI = 'th';
const TURKISH = 'tr';
const VIETNAMESE = 'vi';
public static $LanguagesISO = [
'ENGLISH' => 'en',
'ARABIC' => 'ar',
'GERMAN' => 'de',
'SPANISH' => 'es',
'FRENCH' => 'fr',
'INDONESIAN' => 'in',
'ITALIAN' => 'it',
'HEBREW' => 'iw',
'JAPANESE' => 'ja',
'KOREAN' => 'ko',
'DUTCH' => 'nl',
'POLISH' => 'pl',
'PORTUGUESE' => 'pt',
'RUSSIAN' => 'ru',
'THAI' => 'th',
'TURKISH' => 'tr',
'VIETNAMESE' => 'vi',
];
public static $Languages = [
'en' => 'en_US',
'ar' => 'ar_MA',
'de' => 'de_DE',
'es' => 'es_ES',
'fr' => 'fr_FR',
'in' => 'in_ID',
'it' => 'it_IT',
'iw' => 'iw_IL',
'ja' => 'ja_JP',
'ko' => 'ko_KR',
'nl' => 'nl_NL',
'pl' => 'pl_PL',
'pt' => 'pt_BR',
'ru' => 'ru_RU',
'th' => 'th_TH',
'tr' => 'tr_TR',
'vi' => 'vi_VN',
];
}
@@ -0,0 +1,541 @@
<?php
class MiraviaLink
{
protected $api_url = 'https://miravia.wecomm.es';
protected $api_key = '';
public $last_error = '';
public $request_id = '';
public $insecure_mode = true;
public $sandbox_mode = false;
public function __construct($api_key = '')
{
$this->api_key = $api_key;
if( strpos($api_key, '_f6649cb881216ce050bd0e3') ){
$this->sandbox_mode = true;
$this->api_url = 'https://sandbox.miravia.wecomm.es';
}
}
public function getRegisterUrl($callback_url)
{
$url = $this->api_url . '/token';
$ret = $this->CallAPI($url, 'POST',
array('callback_url' => $callback_url));
if($ret === false){
return '';
}
$response = json_decode($ret, true);
if(!is_array($response)){
$last_error = $ret;
return '';
}
if(isset($response['error'])){
$this->last_error = $response['error'];
}
$url = isset($response['url']) ? $response['url'] : '';
$this->request_id = isset($response['request_id']) ? $response['request_id'] : '';
return $url;
}
public function getSellerInfo($notify_endpoint = '')
{
$url = $this->api_url . '/seller/info';
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
//$this->last_error = print_r($resp,true);
return false;
}
// update data
$url = $this->api_url . '/seller/update';
if(!empty($notify_endpoint)){
$data = [ 'notification_url' => $notify_endpoint ];
}
$data = json_encode($data);
$ret = $this->CallAPI($url, 'POST', $data);
if(isset($resp['seller'])){
return $resp['seller'];
}
//$this->last_error = print_r($resp,true);
return false;
}
public function getWarehouses()
{
$url = $this->api_url . '/seller/warehouses';
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
$this->last_error = print_r($resp,true);
return false;
}
if(isset($resp['module'])){
return $resp['module'];
}
return false;
}
public function getFeedResult($document)
{
$url = $this->api_url . '/feed/result/' . $document;
$ret = $this->CallAPI($url);
return $ret;
}
public function createProduct($json_product)
{
$url = $this->api_url . '/product/create';
$ret = $this->CallAPI($url, 'POST', $json_product);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success'])){
return false;
}else if(!$resp['success']){
$this->last_error = $resp['message'];
if(isset($resp['detail'])){
foreach ($resp['detail'] as $det){
$this->last_error .= "\r" . $det['field'] . ': ' . $det['message'];
}
}
}
return $resp;
}
public function updateProduct($miravia_id, $json_product)
{
$url = $this->api_url . '/product/' . $miravia_id . '/update';
$ret = $this->CallAPI($url, 'POST', $json_product);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success'])){
return false;
}else if(!$resp['success']){
$this->last_error = $resp['message'];
if(isset($resp['detail'])){
foreach ($resp['detail'] as $det){
$this->last_error .= "\r" . $det['field'] . ': ' . $det['message'];
}
}
}
return $resp;
}
public function getProductList($page = 1, $pagesize = 50)
{
$url = $this->api_url . '/product/list?pagesize=' . $pagesize . '&page=' . $page;
$ret = $this->CallAPI($url);
return json_decode($ret, true);
}
public function getProductBySku($sku)
{
$url = $this->api_url . '/product/' . $sku . '/find';
$ret = $this->CallAPI($url);
return json_decode($ret);
}
public function getProductById($id)
{
$url = $this->api_url . '/product/' . $id . '/get';
$ret = $this->CallAPI($url);
return json_decode($ret);
}
public function updateStock($json_stock, $use_feed = false)
{
if($use_feed){
$url = $this->api_url . '/feed/stock';
}else{
$url = $this->api_url . '/product/update_stock';
}
$ret = $this->CallAPI($url, 'POST', $json_stock);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success'])){
return false;
}else if(!$resp['success']){
$this->last_error = $resp['message'];
if(isset($resp['detail'])){
foreach ($resp['detail'] as $det){
$this->last_error .= "\r" . $det['field'] . ': ' . $det['message'];
}
}
}
return $resp;
}
public function disableProducts($itemids)
{
if(!is_array($itemids)){
$itemids = [$itemids];
}
$itemids = json_encode([
'item_ids' => $itemids
]);
$url = $this->api_url . '/product/disable';
$ret = $this->CallAPI($url, 'POST', $itemids);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success'])){
return false;
}else {
return (bool) $resp['success'];
}
}
public function enableProducts($itemids)
{
if(!is_array($itemids)){
$itemids = [$itemids];
}
$itemids = json_encode([
'item_ids' => $itemids
]);
$url = $this->api_url . '/product/enable';
$ret = $this->CallAPI($url, 'POST', $itemids);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success'])){
return false;
}else {
return (bool) $resp['success'];
}
}
public function deleteProduct($itemid)
{
$url = $this->api_url . '/product/' . $itemid . '/delete';
$ret = $this->CallAPI($url, 'GET');
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success'])){
return false;
}else {
return (bool) $resp['success'];
}
}
public function getFeeds()
{
$url = $this->api_url . '/feed/list';
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
return false;
}
if(isset($resp['feeds'])){
return $resp['feeds'];
}
//$this->last_error = print_r($resp,true);
return false;
}
public function getFeedInfo($id)
{
$url = $this->api_url . '/feed/' . $id . '/get' ;
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(isset($resp['feed_result'])) {
$response = [];
if(isset($resp['response'])) {
$response = $resp['response'];
}
$resp = $resp['feed_result'];
$resp['response'] = $response;
}else{
if(isset($resp['code'])){
$this->last_error = $resp['code'] . ': ' .
@$resp['message'] ?: '';
}
}
if(!isset($resp['success']) || !$resp['success']){
return false;
}
return $resp;
}
public function cancelFeed($id)
{
$url = $this->api_url . '/feed/' . $id . '/cancel' ;
$ret = $this->CallAPI($url, 'POST');
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(isset($resp['feed_result'])) {
$resp = $resp['feed_result'];
}
if(!isset($resp['success']) || !$resp['success']){
return false;
}
return $resp;
}
public function sendFeed($data, $type = 'create')
{
$url = $this->api_url . '/feed/' . $type;
$ret = $this->CallAPI($url, 'POST', $data);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success'])){
return false;
}else if(!$resp['success']){
// $this->last_error = $resp['message'];
if(isset($resp['detail'])){
foreach ($resp['detail'] as $det){
$this->last_error .= "\r" . $det['field'] . ': ' . $det['message'];
}
}
}
return $resp;
}
/*
* $fromDate format: YYYY-MM-DD ('Y-m-d')
*/
public function getOrders(string $fromDate)
{
if($d = date_parse_from_format('Y-m-d', $fromDate)){
$fromDate = date('Y-m-d', mktime(0, 0, 0, $d['month'], $d['day'], $d['year']));
}else{
$this->last_error= 'Invalid date';
return false;
}
$url = $this->api_url . '/order/list';
$url .= '?from_date=' . $fromDate;
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
return false;
}
return $resp;
}
public function getOrder($order_number)
{
$url = $this->api_url . '/order/' . $order_number . '/get';
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
return false;
}
return $resp;
}
public function orderConfirmation($order_number, $status)
{
$url = $this->api_url . '/order/confirmation/' . $order_number . '/' . $status;
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
return false;
}
return $resp;
}
public function orderPack($id_order, $products)
{
$url = $this->api_url . '/order/' . $id_order . '/pack';
if(is_array($products)){
$data = json_encode($products);
}else{
$data = $products;
}
$ret = $this->CallAPI($url , 'POST', $data);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
return false;
}
return $resp;
}
public function getShippingLabel($package_id)
{
$url = $this->api_url . '/order/label/' . $package_id;
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
return false;
}
return $resp;
}
public function getDBSCarriers()
{
$url = $this->api_url . '/order/dbs/providers';
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(isset($resp['result'])){
$resp = $resp['result'];
}
if(!isset($resp['success']) || !$resp['success']){
return false;
}
return $resp;
}
public function setDBSTracking($packages, $tracking, $provider)
{
if(!is_array($packages)){
$packages = [$packages];
}
$payload = [];
foreach ($packages as $package){
$payload['packages'][] = [
'package_id' => $package,
'tracking_number' => $tracking,
'shipment_provider_code' => $provider
];
}
$url = $this->api_url . '/order/dbs/update_tracking';
$data = json_encode($payload);
$ret = $this->CallAPI($url, 'POST', $data);
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
$this->last_error = $ret;
return false;
}
return true;
}
public function subscribe($seconds, $message='')
{
$url = $this->api_url . '/seller/notify/' . (int)$seconds;
if(!empty($message)){
$url .= '?message=' . urlencode($message);
}
$ret = $this->CallAPI($url);
if($ret === false){
return false;
}
$resp = json_decode($ret, true);
if(!isset($resp['success']) || !$resp['success']){
return false;
}
return true;
}
public function purgeImageCache()
{
$url = $this->api_url . '/image/cache/purge';
$ret = $this->CallAPI($url);
return json_decode($ret, true);
}
protected function CallAPI($url, $method='GET', $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($this->insecure_mode) {
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
}
if(!empty($this->api_key)){
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Api-Token: ' . $this->api_key
));
}
$result = curl_exec($curl);
if($result === false){
$this->last_error = curl_error($curl);
}
curl_close($curl);
return $result;
}
}
@@ -0,0 +1,186 @@
<?php
class MiraviaProduct
{
public $id = 0;
public $id_miravia = '';
public $miravia_status = '';
public $created = 0;
public $sku = '';
public $reference_sku = '';
public $lang = 'es-ES'; // ISO lang code
public $name = '';
public $short_description = '';
public $description = '';
public $brand = 'no brand';
public $model = '';
public $ean_code = '';
public $warranty = '';
public $id_category = '';
public $id_brand = '';
public $images = []; // Public URLs
public $video = '';
public $price = 0;
public $special_price = 0;
public $quantity = 0;
public $width = 0;
public $height = 0;
public $length = 0;
public $weight = 0;
public $warehouse = '';
public $delivery = '';
public $extra_attributes = []; // [ "id_attribute" => "value", ... ]
public $combinations = []; // Array of MiraviaCombination class
public $info = []; // Internal info (for filters)
protected $private_vars = ['private_vars', 'error', 'id', 'combinations', 'info'];
public $error = '';
public function __construct()
{
//
}
public function addAttr($key = false, $value = false) {
if($key and $value) {
$this->extra_attributes[$key] = $value;
}
}
public function getData()
{
$product = [];
foreach ($this as $key => $value){
if(!in_array($key, $this->private_vars)){
if($key === 'images' && is_array($value) && !empty($value)){
$product['Images'] = ['Image' => $value];
} else if($key !== 'images') {
$product[$key] = $value;
}
}
}
$combinations = $this->getCombinations();
$product['combinations'] = $combinations;
return $product;
}
public function getStockData()
{
$combis = [];
foreach ($this->combinations as $combination){
$combis[] = $combination->getStockData();
}
if(count($combis)==0){
$combis =[[
'sku' => $this->sku,
'price' => $this->price,
'special_price' => $this->special_price,
'quantity' => $this->quantity >= 0 ? $this->quantity : 0
]];
$this->reference_sku = $this->sku;
}
$ret = [
'id' => $this->id,
'id_miravia' => $this->id_miravia,
'sku' => $this->sku,
'combinations' => $combis
];
if(!empty($this->warehouse)){
$ret['warehouse'] = $this->warehouse;
}
return $ret;
}
public function getOnlyStockData()
{
$combis = [];
foreach ($this->combinations as $combination){
$combis[] = $combination->getOnlyStockData();
}
if(count($combis)==0){
$combis =[[
'sku' => $this->sku,
'quantity' => $this->quantity >= 0 ? $this->quantity : 0
]];
$this->reference_sku = $this->sku;
}
$ret = [
'id' => $this->id,
'id_miravia' => $this->id_miravia,
'sku' => $this->sku,
'combinations' => $combis
];
if(!empty($this->warehouse)){
$ret['warehouse'] = $this->warehouse;
}
return $ret;
}
public function getJSON($pretty = false)
{
$flags = $pretty ? JSON_PRETTY_PRINT : 0;
return json_encode($this->getData(), $flags);
}
public function getCombinations()
{
$ret = [];
foreach ($this->combinations as $combination){
$ret[] = $combination->getData();
}
return $ret;
}
public function createProduct($apiKey)
{
if(!empty($this->id_miravia)){
return $this->updateProduct($apiKey);
}
$jreq = json_encode($this->getData());
$link = new MiraviaLink($apiKey);
$ret = $link->createProduct($jreq);
if($ret == false) {
$this->error = $link->last_error;
}
if(isset($ret['data'])){
if(isset($ret['data']['item_id'])){
$this->id_miravia = $ret['data']['item_id'];
}
}
return true;
}
public function updateProduct($apiKey)
{
if(empty($this->id_miravia)){
$this->error = 'Invalid Miravia Id Product';
return false;
}
$jreq = json_encode($this->getData());
$link = new MiraviaLink($apiKey);
$ret = $link->updateProduct($this->id_miravia, $jreq);
if($ret == false) {
$this->error = $link->last_error;
}
if(isset($ret['data'])){
if(isset($ret['data']['item_id'])){
$this->id_miravia = $ret['data']['item_id'];
}
return true;
}
if(isset($ret['message'])){
$this->error = $ret['message'];
}
return false;
}
public function getInfoValue($field)
{
if(isset($this->info[$field])){
return $this->info[$field];
}
return '';
}
}
@@ -0,0 +1,173 @@
<?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;
}
}
+357
View File
@@ -0,0 +1,357 @@
<?php
/**
* Plugin Name: Connector for Miravia
* Description: Upload your products and download orders from Miravia
* Version: 1.0.0
* Author: WeComm Solutions
* Author URI: https://wecomm.es
* Plugin URI: https://wecomm.es/miravia-woocommerce
* Text Domain: miraviawoo
* WC requires at least: 3.0
* WC tested up to: 7.7.2
* Required WP: 5.0
* Tested WP: 6.3
*/
if ( ! defined( 'ABSPATH' ) ) { exit; }
define('MIRAVIA_CLASSES_PATH', plugin_dir_path( __FILE__ ) . 'classes/');
define('MIRAVIA_SHARED_CLASSES_PATH', MIRAVIA_CLASSES_PATH . 'shared/');
define('MIRAVIA_VIEWS_PATH', plugin_dir_path( __FILE__ ) . 'views/');
define('MIRAVIA_ASSETS_PATH', plugin_dir_path( __FILE__ ) . 'assets/');
define('MIRAVIA_WOO_VERSION', '1.0.0');
define('MIRAVIA_BUILD_VERSION', '14');
define('MIRAVIA_DB_VERSION', 101);
define('MIRAVIA_DEBUG', get_option('miravia_debug_mode', '0'));
define('LOG_FOLDER', plugin_dir_path( __FILE__ ));
$isSetter = false;
require_once(MIRAVIA_CLASSES_PATH . 'class.log.php'); //Log CLASS
require_once(MIRAVIA_CLASSES_PATH . 'class.core.php'); //Core CLASS
require_once(MIRAVIA_CLASSES_PATH . 'class.db.php'); //DB CLASS
//require_once(MIRAVIA_CLASSES_PATH . 'class.miravia.base.php'); //BASE SDK
require_once(MIRAVIA_CLASSES_PATH . 'class.categories.php'); //ORDER CLASS
require_once(MIRAVIA_CLASSES_PATH . 'class.product.php'); //PRODUCT CLASS
require_once(MIRAVIA_CLASSES_PATH . 'class.order.php'); //ORDER CLASS
require_once(MIRAVIA_CLASSES_PATH . 'tables/class.table.php'); //TABLE CLASS
require_once(MIRAVIA_CLASSES_PATH . 'class.api.php'); //API CLASS
//Wecomm Server
require_once(MIRAVIA_SHARED_CLASSES_PATH . 'MiraviaFilter.php');
require_once(MIRAVIA_SHARED_CLASSES_PATH . 'MiraviaFeed.php');
require_once(MIRAVIA_SHARED_CLASSES_PATH . 'MiraviaLink.php');
require_once(MIRAVIA_SHARED_CLASSES_PATH . 'MiraviaCategory.php');
require_once(MIRAVIA_SHARED_CLASSES_PATH . 'MiraviaCombination.php');
require_once(MIRAVIA_SHARED_CLASSES_PATH . 'MiraviaProduct.php');
if(is_admin()) {
$iconUrl = plugins_url('/assets/img/icon.png', __FILE__);
}
if( !class_exists('MiraviaWOO') ) {
class MiraviaWOO {
function __construct(){
//Menu
add_action('admin_menu', array(&$this, 'admin_menu'));
add_action('admin_init', array(&$this, 'admin_init'));
add_action( 'admin_enqueue_scripts', array(&$this, 'load_style') );
add_action( 'manage_product_posts_custom_column' , array(&$this, 'colum_product'), 10, 2 );
add_filter( 'manage_product_posts_columns' , array(&$this, 'add_colum_products') );
// register_activation_hook( __FILE__, array('MIRAVIADB', 'install_db') );
add_action( 'plugins_loaded', array('MIRAVIADB', 'install') );
//Actions Table
add_filter( 'miravia_profiles_row_actions', array( &$this, 'miravia_profiles_actions' ), 10, 2 );
add_action( 'add_meta_boxes', array($this, 'create_miravia_order_metabox') );
register_deactivation_hook(
__FILE__,
array($this, 'miravia_desactivation')
);
add_action( 'manage_shop_order_posts_custom_column' , array($this, 'colum_orders'), 10, 2 );
add_action( 'woocommerce_update_product', array($this, 'miravia_update_product'), 10, 1 );
add_action('woocommerce_variation_set_stock', array($this, 'miravia_update_product'), 10, 1);
add_action('woocommerce_product_set_stock', array($this, 'miravia_update_product'), 10, 1);
add_action('woocommerce_variation_set_stock_status', array($this, 'miravia_update_product'), 10, 1);
add_action('woocommerce_product_set_stock_status', array($this, 'miravia_update_product'), 10, 1);
add_action( 'woocommerce_save_product_variation', array($this, 'miravia_update_product'), 10, 1 );
add_action( 'woocommerce_product_options_general_product_data', array($this, 'miravia_product_metabox') );
add_action( 'woocommerce_process_product_meta', array($this, 'miravia_save_metadata_product') );
add_action( 'woocommerce_variation_set_stock', array($this, 'miravia_change_stock_hook'), 15, 1);
add_action( 'woocommerce_product_set_stock', array($this, 'miravia_change_stock_hook'), 15, 1);
}
function miravia_change_stock_hook($product) {
if(get_option('miravia_only_stock', '0') == '1') {
LOG::add("Cambio de stock para el producto {$product}");
$profile = MiraviaCore::get_profile_by_product($product->sku);
$accounts = MiraviaCore::accounts_by_profile($profile);
if(count($accounts) > 0) {
MiraviaCore::request_notify($accounts[0]['token'], 'update_stock');
}
}
return true;
}
function miravia_save_metadata_product( $id ){
if(isset($_POST['miravia_brand_product'])) {
$product = wc_get_product( $id );
$product->update_meta_data( '_miravia_brand_product', sanitize_text_field( $_POST[ 'miravia_brand_product' ] ) );
$product->update_meta_data( '_miravia_unit', sanitize_text_field( $_POST[ 'miravia_unit' ] ) );
$product->update_meta_data( '_miravia_unit_value', sanitize_text_field( $_POST[ 'miravia_unit_value' ] ) );
$product->save_meta_data();
}
}
function miravia_product_metabox() {
require_once(MIRAVIA_VIEWS_PATH . "/metabox/miravia-product.php");
}
function colum_orders( $column, $post_id ) {
if ($column == 'order_status'){
$orderID = get_post_meta($post_id, '_miravia_order_id', true);
if($orderID != '' and $orderID != 0) {
echo '<span class="created_via">'.sprintf(__('Created Vía %s','aliexpress'), 'Miravia').'</span>';
}
}
}
function miravia_update_product( $product_id ) {
if(get_option('miravia_only_stock', '0') == '1') {
return;
}
global $isSetter;
if($product_id instanceof WC_Product) {
$product_id = $product_id->ID;
}elseif(isset($product_id['id'])) {
$product_id = $product_id['id'];
}
if(get_post_meta($product_id, '_miravia_need_update', true) == '1') {
LOG::add("{$product_id} no necesita notificación, ya programada.");
return;
}
LOG::add("Estableciendo notificación para el producto {$product_id}");
update_post_meta($product_id, '_miravia_need_update', '1');
if($isSetter) {
LOG::add("No es necesario establecer aviso");
return;
}
if($product_id) {
$sync = MiraviaCore::get_product_miravia($product_id);
if($sync) {
//ESTABLECER NOTIFICACIÓN
$apiKey = MiraviaCore::get_miravia_account_default();
$resultNotify = MiraviaCore::request_notify($apiKey['token'], 'update_stock');
if($resultNotify === -1) {
LOG::add("Existe una notificación pendiente");
return;
}
if($resultNotify) {
LOG::add("Seteada notificación correctamente");
$isSetter = true;
}
}
}
}
function miravia_desactivation() {
update_option('miravia_db_version', 0);
}
public function miravia_profiles_actions( $actions, $post ) {
if ( current_user_can( 'edit_posts' ) ) {
$actions['miravia'] = '<a href="'.admin_url('admin.php?page=miravia_settings&subpage=edit_profile&nonce=' . wp_create_nonce( 'miravia_edit_profile_' . $post->ID )) . '" rel="permalink">' . __('Edit') . '</a>';
}
return $actions;
}
function colum_product($column, $post_id){
if($column == 'miravia_data') {
$html = '<div class="miravia_controls" data-id="'.$post_id.'">';
$sync = MiraviaCore::get_product_miravia($post_id);
$dateUpdated = 'ND';
if($sync and count($sync) > 0) {
if($sync[0]['last_updated'] == '0000-00-00 00:00:0') {
$dateUpdated = date('d-m-Y H:i:s', strtotime($sync[0]['last_updated']));
}
//MiraviaCore::debug($sync);
if(count($sync) > 1) {
$html .= __('Uploaded with multiple accounts','miraviawoo');
}else{
if($sync[0]['id_miravia'] != '' and $sync[0]['id_miravia'] != 0) {
$html .= "<a class='button viewProduct' data-id='{$sync[0]['id_miravia']}'>View</a><span class='minidate'><br>".__('Last Update:','miraviawoo')." ".$dateUpdated."</span>";
$html .= "<p>Status: {$sync[0]['status_text']}</p>";
}
}
if(trim($sync[0]['lastError']) != '') {
$html .= '<i class="fa fa-info" style="border-radius:100px;padding:5px;background-color:#CCC;margin-left:5px" title="'.$sync[0]['lastError'].'"></i>';
}
}else{
}
$html .= '</div>';
echo wp_kses($html, array(
'a' => array(
'href' => array(),
'class' => array(),
'data-id' => array(),
),
'span' => array(
'class' => array()
),
'i' => array('class' => array(),),
'p' => array()
));
}
}
function add_colum_products( $columns ) {
return array_merge( $columns,
array( 'miravia_data' => __( 'Miravia', 'miraviawoo' ) ) );
}
function load_style() {
if(is_admin()) {
wp_enqueue_style( 'cssmiravia', plugins_url('assets/css/miravia-admin.css?v='.time(), __FILE__) );
wp_enqueue_script( 'productmiravia', plugins_url('assets/js/products.js?v='.time(), __FILE__), array( 'wp-i18n' ) );
wp_localize_script( 'productmiravia', 'MIRAVIA_DATA_JAVASCRIPT', array(
'adminAjaxURL' => admin_url('admin-ajax.php'),
));
wp_enqueue_script( 'jquery-ui-core', false, array('jquery') );
wp_enqueue_script('jquery');
wp_enqueue_script( 'filter_rules_plugin_miravia_localization', plugins_url('lib/filter_rules/localization/es.min.js', __FILE__));
wp_enqueue_script( 'filter_rules_plugin_miravia', plugins_url('lib/filter_rules/jquery.jui_filter_rules.min.js', __FILE__),array('jquery'));
wp_enqueue_script( 'filter_rules_miravia', plugins_url('lib/filter_rules_miravia.js?v='.time(), __FILE__), array('jquery'));
wp_enqueue_style( 'fontawesome_miravia_css', plugins_url('lib/fontawesome-6.1.2.all.min.css', __FILE__) );
if(isset($_GET['page']) and sanitize_text_field($_GET['page']) == 'miravia_settings') {
wp_enqueue_script( 'sweetalert_lib', plugins_url('lib/sweetalert2.min.js', __FILE__) );
wp_enqueue_script( 'select2_lib', plugins_url('lib/select2.min.js', __FILE__) );
wp_enqueue_style( 'select2_css', plugins_url('lib/select2.min.css', __FILE__) );
}
wp_enqueue_style( 'filter_rules_css', plugins_url('lib/filter_rules/jquery.jui_filter_rules.bs.min.css', __FILE__) );
wp_enqueue_script( 'fontawesome_miravia_js', plugins_url('lib/fontawesome-6.1.2.all.min.js', __FILE__) );
}
}
function admin_menu() {
global $iconUrl;
add_menu_page(
esc_html__( 'Miravia', 'miraviawoo' ),
esc_html__( 'Miravia', 'miraviawoo'),
'manage_woocommerce',
'miravia_settings',
array($this, 'settings_page'),
$iconUrl,
55.5
);
// add_submenu_page(
// 'miravia_settings',
// esc_html__( 'Brands', 'miraviawoo' ),
// esc_html__( 'Brands', 'miraviawoo'),
// 'manage_woocommerce',
// 'miravia_settings&subpage=brands',
// array($this, 'settings_page'),
// 10
// );
add_submenu_page(
'miravia_settings',
esc_html__( 'Orders', 'miraviawoo' ),
esc_html__( 'Orders', 'miraviawoo'),
'manage_woocommerce',
'miravia_settings&subpage=orders',
array($this, 'settings_page'),
10
);
add_submenu_page(
'miravia_settings',
esc_html__( 'Jobs', 'miraviawoo' ),
esc_html__( 'Jobs', 'miraviawoo'),
'manage_woocommerce',
'miravia_settings&subpage=jobs',
array($this, 'settings_page'),
10
);
add_submenu_page(
'miravia_settings',
esc_html__( 'Accounts', 'miraviawoo' ),
esc_html__( 'Accounts', 'miraviawoo'),
'manage_woocommerce',
'miravia_settings&subpage=accounts',
array($this, 'settings_page'),
10
);
add_submenu_page(
'miravia_settings',
esc_html__( 'Profiles', 'miraviawoo' ),
esc_html__( 'Profiles', 'miraviawoo'),
'manage_woocommerce',
'miravia_settings&subpage=profiles',
array($this, 'settings_page'),
10
);
add_submenu_page(
'miravia_settings',
esc_html__( 'Products', 'miraviawoo' ),
esc_html__( 'Products', 'miraviawoo'),
'manage_woocommerce',
'miravia_settings&subpage=products',
array($this, 'settings_page'),
10
);
add_submenu_page(
'miravia_settings',
esc_html__( 'Miravia Products', 'miraviawoo' ),
esc_html__( 'Miravia Products', 'miraviawoo'),
'manage_woocommerce',
'miravia_settings&subpage=products_remote',
array($this, 'settings_page'),
10
);
add_submenu_page(
'miravia_settings',
esc_html__( 'Rules', 'miraviawoo' ),
esc_html__( 'Rules', 'miraviawoo'),
'manage_woocommerce',
'miravia_settings&subpage=rules',
array($this, 'settings_page'),
10
);
}
function create_miravia_order_metabox() {
global $post;
if(get_post_meta($post->ID, '_miravia_order_id', true)){
add_meta_box( 'aew_number_shipping_metabox', __('Miravia Tracker','miraviawoo'), array($this, 'content_metabox_miravia_order'), 'shop_order', 'normal', 'high' );
}
}
function content_metabox_miravia_order($post) {
require_once(MIRAVIA_VIEWS_PATH . '/metabox/miravia-order.php');
}
public function settings_page() {
include MIRAVIA_VIEWS_PATH . 'settings.php';
}
public function brands_page() {
include MIRAVIA_VIEWS_PATH . 'brands.php';
}
function admin_init() {
}
}
}
$MIRAVIAWOO = new MIRAVIAWOO();
@@ -0,0 +1,181 @@
/*
* jui_filter_rules v1.0.7 CSS (for Bootstrap) *********************************
*/
/*
DO NOT CHANGE this file, as it will be overwritten in next update.
Write your own classes in other css file.
*/
/* container ---------------------------------------------------------------- */
.filter_rules_container {
padding: 10px 0;
}
/* rules group -------------------------------------------------------------- */
.rules_group_container {
padding: 0;
margin: 0;
}
.rules_group_header {
padding: 5px 0;
}
.rules_group_body {
padding: 0;
margin: 0;
}
.rules_group_condition_container {
display: inline-block;
padding: 0;
}
.rules_group_condition_list {
border: 1px solid;
}
.rules_group_tools_container {
display: inline-block;
padding: 0;
margin-left: 5px;
}
.rules_group_tools_list {
border: 1px solid;
padding: 2px;
margin: 0;
width: 35px;
}
/* rules list --------------------------------------------------------------- */
.rules_list {
width: auto !important;
list-style-type: none;
padding: 0 0 0 25px;
border-left: dotted 1px !important;
border-right: none !important;
border-top: none !important;
border-bottom: none !important;
}
.rules_list_li {
margin: 0 0 2px 0 !important;
padding: 0 !important;
display: block !important;
}
.rules_list_applied_li {
}
.rules_list_error_li {
background-color: #df0202 !important;
}
/* filter name -------------------------------------------------------------- */
.filter_container {
display: inline-block;
padding: 5px 5px 5px 0;
vertical-align: top;
}
.filter_list {
}
/* operator ----------------------------------------------------------------- */
.operators_list_container {
display: inline-block;
padding: 5px;
vertical-align: top;
}
.operators_list {
}
/* filter value ------------------------------------------------------------- */
.filter_value_container {
display: inline-block;
padding: 5px;
}
.filter_input_text {
}
.filter_input_number {
width: 80px;
}
.filter_input_date {
}
.filter_input_checkbox {
margin: 0 5px 0 0;
display: inline !important;
}
.filter_input_radio {
margin: 0 5px 0 0;
}
.filter_select {
margin: 0 5px;
}
.filter_group_list {
list-style-type: none;
padding: 0;
}
.filter_group_list_item_horizontal {
display: inline !important;
margin: 0 5px;
padding: 0;
}
.filter_group_list_item_vertical {
display: block !important;
margin: 0 0 5px 5px !important;
padding: 0 !important;
}
/* filter tools ------------------------------------------------------------- */
.rule_tools_container {
display: inline-block !important;
padding: 5px !important;
vertical-align: top !important;
}
.rule_tools_list {
padding: 0;
margin: 0;
width: 35px;
}
/* no filters div ----------------------------------------------------------- */
.no_filters_found{
text-align: center;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
}
/* IE8 CSS hack ------------------------------------------------------------- */
/* http://dimox.net/personal-css-hacks-for-ie6-ie7-ie8/ */
@media \0screen {
.rules_group_tools_list {
border: 1px solid;
padding: 2px;
margin: 0;
width: auto;
}
.rule_tools_list {
padding: 0;
margin: 0;
width: auto;
}
}
/* -------------------------------------------------------------------------- */
@@ -0,0 +1,5 @@
/*
* jui_filter_rules v1.0.7 CSS *************************************************
* Minified using http://gpbmike.github.io/refresh-sf/
*/
.filter_rules_container{padding:5px 10px 10px}.rules_group_container{padding:0;margin:0}.rules_group_header{padding:5px 0}.rules_group_body{padding:0;margin:0}.rules_group_condition_container{display:inline-block;padding:0}.rules_group_condition_list{border:1px solid;padding:2px 0;margin:0}.rules_group_tools_container{display:inline-block;padding:0;margin-left:5px}.rules_group_tools_list{border:1px solid;padding:2px;margin:0;width:35px}.rules_list{width:auto!important;list-style-type:none;padding:0 0 0 25px;border-left:dotted 1px!important;border-right:none!important;border-top:none!important;border-bottom:none!important}.rules_list_li{margin:0 0 2px!important;padding:0!important;background-color:#f3f3f3!important;display:block!important}.rules_list_applied_li{background-color:#CF9!important}.rules_list_error_li{background-color:#df0202!important}.filter_container{display:inline-block;padding:5px 5px 5px 0;vertical-align:top}.filter_list{padding:0;margin:0;background:0 0}.operators_list_container{display:inline-block;padding:5px;width:180px;vertical-align:top}.operators_list{padding:0;margin:0;background:0 0}.filter_value_container{display:inline-block;padding:5px}.filter_input_text{width:180px;margin:0 5px;padding:0}.filter_input_number{width:75px;margin:0 5px;padding:0}.filter_input_date{width:150px;margin:0 5px;padding:0}.filter_input_checkbox,.filter_input_radio{margin:0 5px 0 0}.filter_select{margin:0 5px}.filter_group_list{list-style-type:none;padding:0}.filter_group_list_item_horizontal{display:inline!important;margin:0 5px;padding:0}.filter_group_list_item_vertical{display:block!important;margin:0 0 5px 5px!important;padding:0!important}.rule_tools_container{display:inline-block!important;padding:5px!important;float:right!important;vertical-align:top!important}.rule_tools_list{padding:0;margin:0;width:35px}.no_filters_found{text-align:center;margin-top:10px;margin-left:auto;margin-right:auto}@media \0screen{.rules_group_tools_list{border:1px solid;padding:2px;margin:0;width:auto}.rule_tools_list{padding:0;margin:0;width:auto}}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,4 @@
/**
* jui_filter_rules v1.0.7 simple localization - GREEK *************************
*/
var rsc_jui_fr={no_filters_found:"Δεν έχουν ορισθεί φίλτρα",rules_group_AND:"σε ίσχύ όλα",rules_group_OR:"σε ίσχύ ένα τουλάχιστον",rule:"κριτήριο",group:"ομάδα",tools_please_select:"&raquo;",rule_insert_before:"εισαγωγή πριν",rule_insert_after:"εισαγωγή μετά",rule_insert_inside:"εισαγωγή εντός",rule_clear:"καθαρισμός",rule_delete:"διαγραφή",group_insert_before:"εισαγωγή πριν",group_insert_after:"εισαγωγή μετά",group_insert_inside:"εισαγωγή εντός",group_delete:"διαγραφή",filter_please_select:"&raquo; επιλογή",operator_equal:"ισούται",operator_not_equal:"δεν ισούται",operator_in:"περιλαμβάνεται σε",operator_not_in:"δεν περιλαμβάνεται σε",operator_less:"μικρότερο",operator_less_or_equal:"μικρότερο είτε ίσο",operator_greater:"μεγαλύτερο",operator_greater_or_equal:"μεγαλύτερο είτε ίσο",operator_between:"ανάμεσα",operator_not_between:"όχι ανάμεσα",operator_begins_with:"ξεκινά με",operator_not_begins_with:"δεν ξεκινά με",operator_contains:"περιέχει",operator_not_contains:"δεν περιέχει",operator_ends_with:"καταλήγει σε",operator_not_ends_with:"δεν καταλήγει σε",operator_is_empty:"είναι κενό",operator_is_not_empty:"δεν είναι κενό",operator_is_null:"δεν έχει δοθεί",operator_is_not_null:"έχει δοθεί",error_no_value_given:"Δεν δόθηκε τιμή",error_invalid_number:"Η τιμή δεν είναι αριθμός",error_invalid_datetime:"Η ημερομηνία δεν είναι σωστή",error_converting_value:"Λάθος κατά τη μετατροπή της τιμής που δόθηκε"};
@@ -0,0 +1,4 @@
/**
* jui_filter_rules v1.0.7 simple localization - ENGLISH ***********************
*/
var rsc_jui_fr={no_filters_found:"No filters defined",rules_group_AND:"all rules",rules_group_OR:"any rule",rule:"rule",group:"group",tools_please_select:"&raquo;",rule_insert_before:"insert before",rule_insert_after:"insert after",rule_insert_inside:"insert inside",rule_clear:"clear",rule_delete:"delete",group_insert_before:"insert before",group_insert_after:"insert after",group_insert_inside:"insert inside",group_delete:"delete",filter_please_select:"&raquo; select",operator_equal:"equal",operator_not_equal:"not_equal",operator_in:"in",operator_not_in:"not in",operator_less:"less",operator_less_or_equal:"less or equal",operator_greater:"greater",operator_greater_or_equal:"greater or equal",operator_between:"between",operator_not_between:"not between",operator_begins_with:"begins with",operator_not_begins_with:"does not begin with",operator_contains:"contains",operator_not_contains:"does not contain",operator_ends_with:"ends with",operator_not_ends_with:"does not end with",operator_is_empty:"is empty",operator_is_not_empty:"is not empty",operator_is_null:"is null",operator_is_not_null:"is not null",error_no_value_given:"Value not given",error_invalid_number:"Value is not numeric",error_invalid_datetime:"Invalid date format",error_converting_value:"Error converting value"};
@@ -0,0 +1,55 @@
/**
* jui_filter_rules v1.0.7 simple localization - ENGLISH
*
* DO NOT CHANGE this file, as it will be overwritten in next update.
* To use different values, write and use a similar structure js file.
*
*/
var rsc_jui_fr = {
no_filters_found: "No hay filtros definidos",
rules_group_AND: "Todas las reglas",
rules_group_OR: "Cualquier regla",
rule: "regla",
group: "grupo",
tools_please_select: "Añadir",
rule_insert_before: "Insertar antes",
rule_insert_after: "Insertar después",
rule_insert_inside: "Insertar dentro",
rule_clear: "limpiar",
rule_delete: "borrar",
group_insert_before: "Insertar antes",
group_insert_after: "Insertar después",
group_insert_inside: "Insertar dentro",
group_delete: "borrar",
filter_please_select: 'Seleccione',
operator_equal: 'igual',
operator_not_equal: 'no igual',
operator_in: 'sea',
operator_not_in: 'no sea',
operator_less: 'menor',
operator_less_or_equal: 'menor o igual',
operator_greater: 'mayor',
operator_greater_or_equal: 'mayor o igual',
operator_between: 'entre',
operator_not_between: 'no esté entre',
operator_begins_with: 'empiece por',
operator_not_begins_with: 'no empiece por',
operator_contains: 'contenga',
operator_not_contains: 'no contenga',
operator_ends_with: 'termine en',
operator_not_ends_with: 'no termine en',
operator_is_empty: 'esté vacio',
operator_is_not_empty: 'no esté vacio',
operator_is_null: 'sea nulo',
operator_is_not_null: 'no sea nulo',
error_no_value_given: "Valor no declarado",
error_invalid_number: "Valor no numerico",
error_invalid_datetime: "Formato de fecha incorrecto",
error_converting_value: "Error al convertir el valor"
};
@@ -0,0 +1,55 @@
/**
* jui_filter_rules v1.0.7 simple localization - ENGLISH
*
* DO NOT CHANGE this file, as it will be overwritten in next update.
* To use different values, write and use a similar structure js file.
*
*/
var rsc_jui_fr = {
no_filters_found: "No filters defined",
rules_group_AND: "all rules",
rules_group_OR: "any rule",
rule: "rule",
group: "group",
tools_please_select: "&raquo;",
rule_insert_before: "insert before",
rule_insert_after: "insert after",
rule_insert_inside: "insert inside",
rule_clear: "clear",
rule_delete: "delete",
group_insert_before: "insert before",
group_insert_after: "insert after",
group_insert_inside: "insert inside",
group_delete: "delete",
filter_please_select: '&raquo; select',
operator_equal: 'equal',
operator_not_equal: 'not_equal',
operator_in: 'in',
operator_not_in: 'not in',
operator_less: 'less',
operator_less_or_equal: 'less or equal',
operator_greater: 'greater',
operator_greater_or_equal: 'greater or equal',
operator_between: 'between',
operator_not_between: 'not between',
operator_begins_with: 'begins with',
operator_not_begins_with: 'does not begin with',
operator_contains: 'contains',
operator_not_contains: 'does not contain',
operator_ends_with: 'ends with',
operator_not_ends_with: 'does not end with',
operator_is_empty: 'is empty',
operator_is_not_empty: 'is not empty',
operator_is_null: 'is null',
operator_is_not_null: 'is not null',
error_no_value_given: "Value not given",
error_invalid_number: "Value is not numeric",
error_invalid_datetime: "Invalid date format",
error_converting_value: "Error converting value"
};
@@ -0,0 +1,235 @@
jQuery(document).ready(function($) {
if(typeof categories == 'undefined') {
return;
}
$("#filter_editor").jui_filter_rules( getFilterConfig() );
if(filter !== false){
setRules(filter);
}
$("#miravia_rules_form").submit(function(){
var data = '';
data = getRulesJson();
$("#miravia_rules_form").append('<input type="hidden" name="filter_data" value="'+encodeURIComponent(data)+'">');
data = getActionDetailJson();
$("#miravia_rules_form").append('<input type="hidden" name="action_detail_data" value="'+encodeURIComponent(data)+'">');
});
$("#action_type").change(function(){
setActionDetail();
});
setActionDetail();
function getFilterConfig() {
var filter_config = {
bootstrap_version: "3",
filters: [
{
filterName: "Name", "filterType": "text", field: "product_name", filterLabel: "Product name",
excluded_operators: ["in", "not_in"],
filter_interface: [
{
filter_element: "input",
filter_element_attributes: {"type": "text", "value": ""}
}
]
},
{
filterName: "Price",
"filterType": "number",
"numberType": "double",
field: "price",
filterLabel: "Product price",
excluded_operators: ["in", "not_in"]
},
{
filterName: "SpecificPrice",
"filterType": "number",
"numberType": "double",
field: "specific_price",
filterLabel: "Product specific price",
excluded_operators: ["in", "not_in"]
},
{
filterName: "Stock",
"filterType": "number",
"numberType": "integer",
field: "stock",
filterLabel: "Stock",
excluded_operators: ["in", "not_in"]
},
{
filterName: "Category",
"filterType": "number",
"numberType": "integer",
field: "category",
filterLabel: "Category",
excluded_operators: ["in", "not_in", "less", "less_or_equal", "greater", "greater_or_equal"],
filter_interface: [
{
filter_element: "select"
}
],
lookup_values: categories
},
// {
// filterName: "Supplier",
// "filterType": "number",
// "numberType": "integer",
// field: "supplier",
// filterLabel: "Supplier",
// excluded_operators: ["in", "not_in", "less", "less_or_equal", "greater", "greater_or_equal"],
// filter_interface: [
// {
// filter_element: "select"
// }
// ],
// lookup_values: suppliers
// },
// {
// filterName: "Manufacturer",
// "filterType": "number",
// "numberType": "integer",
// field: "manufacturer",
// filterLabel: "Manufacturer",
// excluded_operators: ["in", "not_in", "less", "less_or_equal", "greater", "greater_or_equal"],
// filter_interface: [
// {
// filter_element: "select"
// }
// ],
// lookup_values: manufacturers
// },
],
onValidationError: function (event, data) {
alert(data["err_description"] + ' (' + data["err_code"] + ')');
if (data.hasOwnProperty("elem_filter")) {
data.elem_filter.focus();
}
}
};
return filter_config;
}
function setActionDetail()
{
var action_type = $("#action_type").val();
$("#action_detail").html(getActionDetail(action_type));
}
function getActionDetail(action_type)
{
switch (action_type) {
case 'price_stock':
return getActionDetailModifyPriceStock();
case 'name':
return getActionDetailModifyName();
case 'logistics':
return getActionDetailLogistics();
default:
return '';
}
}
function getActionDetailModifyPriceStock()
{
var v =action_detail.field || '';
var html = '<select id="action_detail_field" class="fixed-width-xl pull-left mr-1">';
html += '<option value="price,special_price" ' + ((v=='price,special_price')?'selected':'') + '>Price & Specific price</option>';
html += '<option value="price" ' + ((v=='price')?'selected':'') + '>Price</option>';
html += '<option value="special_price" ' + ((v=='special_price')?'selected':'') + '>Specific price</option>';
html += '<option value="stock" ' + ((v=='stock')?'selected':'') + '>Stock</option>';
html += '</select>';
v = action_detail.operator || '';
html += '<select id="action_detail_operator" class="fixed-width-xl pull-left">';
html += '<option value="increment" ' + ((v=='increment')?'selected':'') + '>Increment €</option>';
html += '<option value="decrement" ' + ((v=='decrement')?'selected':'') + '>Decrement €</option>';
html += '<option value="increment_percent" ' + ((v=='increment_percent')?'selected':'') + '>Increment %</option>';
html += '<option value="decrement_percent" ' + ((v=='decrement_percent')?'selected':'') + '>Decrement %</option>';
html += '<option value="multiply" ' + ((v=='multiply')?'selected':'') + '>Multiply</option>';
html += '<option value="set" ' + ((v=='set')?'selected':'') + '>Set to</option>';
html += '</select> &nbsp; ';
v = action_detail.value || '';
v = v.replaceAll('"', '');
html += '<input type="text" id="action_detail_value" value="' + v + '" class="fixed-width-xl pull-left">';
return html;
}
function getActionDetailModifyName()
{
var v =action_detail.field || '';
var html = '<select id="action_detail_field" class="fixed-width-xl pull-left mr-1">';
html += '<option value="name" ' + ((v=='name')?'selected':'') + '>Mofify "Name"</option>';
html += '<option value="description" ' + ((v=='description')?'selected':'') + '>Modify "Description"</option>';
html += '<option value="short_description" ' + ((v=='short_description')?'selected':'') + '>Modify "Short description"</option>';
html += '</select><br><br>';
v = action_detail.stringvalue || '';
html += '<textarea id="action_detail_value">' + v;
html += '</textarea>';
html += '<div>';
html += 'You can use these variables:<strong> %name%, %description%, %short_description%, %manufacturer%, %supplier%, %category%</strong>';
html += '</div>';
return html;
}
function getActionDetailLogistics()
{
var v = action_detail.delivery || '';
var html = '<select id="action_detail_field" class="fixed-width-xl pull-left mr-1">';
html += '<option value="dbs" ' + ((v=='dbs')?'selected':'') + '>Delivery by Seller</option>';
html += '<option value="dbm" ' + ((v=='dbm')?'selected':'') + '>Delivery by Miravia</option>';
html += '<option value="" ' + ((v=='')?'selected':'') + '>Use defaults</option>';
html += '</select><br><br>';
v = action_detail.warehouse || '';
html += '<input type="text" id="action_detail_value" placeholder="Warehouse code" ';
html += 'value="' + v + '" class="input fixed-width-xl pull-left mr-1">';
html += '<br><br>';
return html;
}
function getActionDetailJson()
{
var action_type = $("#action_type").val();
switch (action_type) {
case 'price_stock':
var ret = {
field: $('#action_detail_field').val(),
operator: $('#action_detail_operator').val(),
value: $('#action_detail_value').val()
};
return JSON.stringify(ret);
case 'name':
var ret = {
field: $('#action_detail_field').val(),
stringvalue: $('#action_detail_value').val()
};
return JSON.stringify(ret);
case 'logistics':
var ret = {
delivery: $('#action_detail_field').val(),
warehouse: $('#action_detail_value').val()
}
return JSON.stringify(ret);
default:
return '';
}
}
function getRulesJson()
{
return JSON.stringify($("#filter_editor").jui_filter_rules('getRules', 0, []));
}
function setRules(a_rules)
{
$("#filter_editor").jui_filter_rules('setRules', a_rules);
}
})
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,33 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
list($order, $items) = MiraviaCore::get_order_woocommerce($post->ID);
?>
<div id="miravia_order_gestion">
<?php
$package_data = get_post_meta($post->ID, '_miravia_packed_result', true);
if(isset($package_data['data']['pack_order_list'])){
$packages = [];
foreach ($package_data['data']['pack_order_list'] as $item){
if(isset($item['order_item_list'])){
foreach($item['order_item_list'] as $package){
$packages[$package['package_id']] = true;
}
}
}
foreach($packages as $p => $v) {
if($v) {
echo wp_kses('<a href="javascript:void(0)" class="miraviaprintLabel" data-id="'.$post->ID.'" data-package="'.$p.'"><i class="fa fa-print"></i> Print Shipping Label ['.$p.']</a>', wp_kses_allowed_html());
}
}
}else{
echo wp_kses(' <p>'.__('Ready to ship?','miraviawoo').'</p>
<button type="button" id="packOrderButton" data-id="'.$post->ID.'">Pack Order</button>', wp_kses_allowed_html());
}
?>
</div>
@@ -0,0 +1,43 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $post;
$defaultBrand = get_option('miravia_default_brand', 'No Brand');
$productBrand = get_post_meta($post->ID, '_miravia_brand_product', true);
$productUnit = get_post_meta($post->ID, '_miravia_unit', true);
$productUnitValue = get_post_meta($post->ID, '_miravia_unit_value', true);
$defaultUnit = get_option('miravia_default_unit', 'units');
$defaultUnitValue = get_option('miravia_default_unit_value', '1');
if($productBrand) {
$defaultBrand = $productBrand;
}
?>
<div class="options_group show_if_simple show_if_external show_if_variable" style="">
<p class=" form-field miravia_brand_product_field">
<label for="miravia_brand_product"><?php echo __('Product Brand', 'miraviawoo') ?></label>
<select id="miravia_brand_product" name="miravia_brand_product" class="search-select-miravia-brand">
<option value="<?php echo $defaultBrand ?>" <?php selected($defaultBrand, $defaultBrand) ?>><?php echo $defaultBrand ?></option>
</select>
</p>
<p class=" form-field miravia_unit_type_field">
<label for="miravia_unit_type"><?php echo __('Unit Type', 'miraviawoo') ?></label>
<select id="miravia_unit_type" name="miravia_unit_type">
<option value="">Default: <?php echo $defaultUnit ?></option>
<option <?php echo selected('Units',$productUnit)?> value="Units">Units</option>
<option <?php echo selected('Liters',$productUnit)?> value="Liters">Liters</option>
<option <?php echo selected('Kilograms',$productUnit)?> value="Kilograms">Kilograms</option>
<option <?php echo selected('Grams',$productUnit)?> value="Grams">Grams</option>
<option <?php echo selected('Milliliters',$productUnit)?> value="Milliliters">Milliliters</option>
</select>
</p>
<p class=" form-field miravia_brand_product_field">
<label for="miravia_brand_product"><?php echo __('Unit Value', 'miraviawoo') ?></label>
<input type="text" name="miravia_unit_value" value="<?php echo $productUnitValue ?>" placeholder="Default: <?php echo $defaultUnitValue?>" />
</p>
</div>
@@ -0,0 +1,38 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
if(isset($_GET['action']) and $_GET['action'] == 'delete') {
if(current_user_can( 'manage_options' )){
MiraviaCore::delete_account(sanitize_text_field( $_GET['id'] ));
wp_safe_redirect(admin_url("admin.php?page=miravia_settings&subpage=accounts"));
}
}
$miraviaTable = new MiraviaTable();
$miraviaTable->custom_actions = array(
'delete' => sprintf('<a href="admin.php?page=%s&subpage=%s&action=%s&id=[id]">Delete</a>', sanitize_text_field($_REQUEST['page']), sanitize_text_field($_REQUEST['subpage']), 'delete'),
);
$miraviaTable->columns = [
'name' => 'Name',
'lang' => 'Language',
'userid' => 'User ID',
'created' => 'Created',
'updated' => 'Updated'
];
$miraviaTable->data_table = $accounts = MiraviaCore::get_accounts();
$miraviaTable->total_elements = count($accounts);
$miraviaTable->prepare_items();
?>
<div class="wrap">
<form method="post" action="<?php echo admin_url('admin-ajax.php?action=miravia_authorize')?>">
<h1 class="wp-heading-inline">Miravia Seller Accounts</h1>
<input type="submit" name="authorize" class="page-title-action" value="Add Account" />
</form>
<hr class="wp-header-end">
<?php
$miraviaTable->display();
?>
</div>
+109
View File
@@ -0,0 +1,109 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
$categories_end = array();
$categories = get_terms(array('taxonomy' => 'product_cat'));
foreach($categories as $cat) {
$categories_end[] = (object) array('lk_option' => $cat->name, 'lk_value' => $cat->term_id);
}
$profiles = MiraviaCore::get_profiles();
$accounts = MiraviaCore::get_accounts();
if(isset($_POST['action']) and sanitize_text_field($_POST['action']) == 'save_rule') {
if(sanitize_text_field($_POST['name_rule']) != "") {
$account = sanitize_text_field($_POST['apply_accounts']);
$profiles = sanitize_text_field($_POST['apply_profile']);
$filterDecoded = urldecode($_POST['filter_data']);
$actionsDecoded = urldecode($_POST['action_detail_data']);
$saveProfile = MiraviaCore::add_rule(array(
'name_rule' => sanitize_text_field($_POST['name_rule']),
'accounts' => $account,
'profile_id' => $profiles,
'rules_json' => $filterDecoded,
'action_json' => $actionsDecoded
));
if($saveProfile) {
wp_safe_redirect('?page=miravia_settings&subpage=rules');
}
}else{
echo "Error: Rule need Name";
}
}
?>
<script>
const filter = [];
const categories = <?php echo json_encode($categories_end)?>;
</script>
<div class="wrap miravia-pane">
<h1 class="wp-heading-inline">Add Rule to Miravia</h1>
<hr class="wp-header-end">
<form id="miravia_rules_form" method="post">
<input type="hidden" name="action" value="save_rule" />
<table class="form-table">
<tr valign="top">
<th scope="row"><?php echo __('Name of Rule', 'miraviawoo')?>
<span class="description"><?php echo __('Describe your rule', 'miraviawoo')?></span>
</th>
<td>
<input type="text" name="name_rule" placeholder="Rule Name..." />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Accounts', 'miraviawoo')?>
<span class="description"><?php echo __('Select all or one account to apply this rule', 'miraviawoo')?></span>
</th>
<td>
<select name="apply_accounts">
<option value="0">Apply all accounts</option>
<?php
foreach($accounts as $acc) {
echo " <option value='{$acc['id']}'>{$acc['name']}</option>";
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Profile', 'miraviawoo')?>
<span class="description"><?php echo __('Select all or one profile to apply this rule', 'miraviawoo')?></span>
</th>
<td>
<select name="apply_profile">
<option value="0">Select one profile</option>
<?php
foreach($profiles as $pro) {
echo "<option value='{$pro['id']}'>{$pro['name']}</option>";
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Rules', 'miraviawoo')?>
<span class="description"><?php echo __('Create a conditionals to apply this rule', 'miraviawoo')?></span>
</th>
<td>
<div id="filter_editor" style="border:1px solid #CCC; padding:10px; background-color:#FFF;border-radius:5px"></div>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Actions', 'miraviawoo')?>
<span class="description"><?php echo __('If conditionals is true, what happens?', 'miraviawoo')?></span>
</th>
<td>
<div style="border:1px solid #CCC; padding:10px; margin-top:20px; background-color:#FFF; border-radius:5px">
<select id="action_type" style="margin-bottom:10px">
<option value="remove">Do not send these products</option>
<option value="only" >Only send these products</option>
<option value="price_stock">Change price or stock</option>
<option value="name">Name</option>
</select>
<div id="action_detail"></div>
</div>
</td>
</tr>
</table>
<input type="submit" name="submit" class="button" value="Save Rule" />
</form>
</div>
@@ -0,0 +1,65 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
$category = get_term(sanitize_text_field($_GET['cat_id']));
if(isset($_POST['action']) and sanitize_text_field($_POST['action']) == 'save_miravia_attr_default') {
update_term_meta($category->term_id, "_miravia_attr", sanitize_text_field($_POST['attr']));
}
$miravia_category = get_term_meta($category->term_id, '_miravia_category', true);
$token = MiraviaCore::get_miravia_account_default();
if($token) {
$token = $token['token'];
}else{
die('Error: Account is not ready');
}
$link = new MiraviaCategory($token);
$attributes = $link->getAttributes($miravia_category);
$default_attributes = get_term_meta($category->term_id, "_miravia_attr", true);
$attributes_woocommerce = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' );
?>
<h1>MIRAVIA - Attributes <?php echo $category->name ?> Configuration</h1>
<form method="post">
<table width="100%">
<?php
if($attributes_woocommerce) {
foreach($attributes_woocommerce as $slug => $attr) {
// if(isset($default_attributes[$attr['name']])) {
// $value = $default_attributes[$attr['name']];
// }else{
// $value = '';
// }
// $obligatorio = '';
// if($attr['is_mandatory'] == '1') {
// $obligatorio = '*';
// }
$options = '';
if (!empty ($attributes ) ) {
foreach ( $attributes as $key => $v ) {
if($v['is_sale_prop'] == 0) {
continue;
}
$selected = '';
if($default_attributes != '' and $v['name'] == $default_attributes[$slug]) {
$selected = 'selected="selected"';
}
$options .= "<option value='{$v['name']}' {$selected}>{$v['label']}</option>";
}
}
echo wp_kses("<tr>
<td style='text-transform: capitalize;'>{$attr}</td>
<td><select class='attribute' name='attr[{$slug}]'>
<option disabled value='0'> - Select - </option>{$options}</select></td>
</tr>", wp_kses_allowed_html());
}
echo wp_kses("<button type='submit' name='action' value='save_miravia_attr_default'>Guardar Atributos</button>",wp_kses_allowed_html());
}else{
echo esc_html('No hay atributos en el id ' . $category->term_id);
}
?>
</table>
</form>
+10
View File
@@ -0,0 +1,10 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
// $brands = $MIRAVIAWOO->client->get_brands(1);
// die('<pre>'.print_r($brands, true).'</pre>');
?>
<div class="wrap">
<h2>Brands</h2>
</div>
@@ -0,0 +1,45 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
?>
<h1>MIRAVIA - Category Configuration</h1>
<?php
$token = MiraviaCore::get_miravia_account_default();
if($token) {
$token = $token['token'];
}else{
die('Error: Account is not ready');
}
$link = new MiraviaCategory($token);
$miravia_categories = $link->getCategories(true);
if(isset($_POST['action']) and sanitize_text_field($_POST['action']) == 'save_miravia_categories') {
if(is_array($_POST['miravia_category'])) {
foreach($_POST['miravia_category'] as $woocommerce_cat_id => $miravia_cat) {
update_term_meta(sanitize_text_field($woocommerce_cat_id), "_miravia_category", sanitize_text_field($miravia_cat));
}
}
}
$woocommerce_categories = get_terms('product_cat');
?>
<form method="post">
<table width="100%">
<thead>
<td>WooCommerce Category</td>
<td>MIRAVIA Category</td>
<td>Opciones</td>
</thead>
<?php
foreach($woocommerce_categories as $category) {
$selected = get_term_meta($category->term_id, "_miravia_category", true);
echo wp_kses("<tr>
<td>{$category->name}</td>
<td>".MiraviaCore::select_category($miravia_categories, array('select' => $selected, 'name' => 'miravia_category['.$category->term_id.']'))."</td>
<td>
<a href='?page=miravia_settings&subpage=attributes&cat_id={$category->term_id}'>Atributos por defecto</a>
</td>
</tr>", wp_kses_allowed_html());
}
?>
<button type="submit" name="action" value="save_miravia_categories">Guardar Categorias</button>
</table>
</form>
@@ -0,0 +1,175 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $wpdb;
if(isset($_POST['miravia_action_nonce'])) {
if(wp_verify_nonce(sanitize_text_field($_POST['miravia_action_nonce']), 'save_miravia_conf')) {
update_option('miravia_delay_time', sanitize_text_field($_POST['secondsNotify']));
update_option('miravia_default_status', sanitize_text_field($_POST['miravia_order_defuault_status']));
update_option('_miravia_default_stock', sanitize_text_field($_POST['_miravia_default_stock']));
update_option('miravia_transport_mode', sanitize_text_field($_POST['miravia_transport_mode']));
update_option('miravia_ean_key', sanitize_text_field($_POST['miravia_ean_key']));
update_option('miravia_default_brand', sanitize_text_field($_POST['miravia_default_brand']));
update_option('miravia_default_unit', sanitize_text_field($_POST['miravia_default_unit']));
update_option('miravia_default_unit_value', sanitize_text_field($_POST['miravia_default_unit_value']));
if(isset($_POST['miraviaDebugMode']) and sanitize_text_field($_POST['miraviaDebugMode']) == '1') {
update_option('miravia_debug_mode', '1');
}else{
update_option('miravia_debug_mode', '0');
}
if(isset($_POST['miravia_only_stock']) and sanitize_text_field($_POST['miravia_only_stock']) == '1') {
update_option('miravia_only_stock', '1');
}else{
update_option('miravia_only_stock', '0');
}
}
}
$defaultUnit = get_option('miravia_default_unit', 'units');
$defaultUnitValue = get_option('miravia_default_unit_value', '1');
$timeDelay = get_option('miravia_delay_time', 300);
$debugMode = get_option('miravia_debug_mode', '0');
$transportMode = get_option('miravia_transport_mode', 'dbm');
$defaultBrand = get_option('miravia_default_brand', 'No Brand');
$statuses = wc_get_order_statuses();
$statusDefaultSelected = get_option('miravia_default_status', 'wc-processing');
$categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
// MiraviaCore::debug($categories);
// $notifies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_name LIKE '%miravia_notify%'", ARRAY_A );
// // delete_option('miravia_notify_update_stock_in');
// LOG::add("Get notifies");
// LOG::add($notifies);
?>
<div class="wrap">
<h2>Configuration</h2>
<form method="post">
<?php wp_nonce_field( 'save_miravia_conf', 'miravia_action_nonce' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php echo __('Time Delay', 'miraviawoo')?>
</th>
<td>
<select name="secondsNotify">
<option value="30" <?php echo selected($timeDelay, 30,false)?>>30 secs</option>
<option value="60" <?php echo selected($timeDelay, 60,false)?>>1 min</option>
<option value="120" <?php echo selected($timeDelay, 120,false)?>>2 min</option>
<option value="240" <?php echo selected($timeDelay, 240,false)?>>4 min</option>
<option value="300" <?php echo selected($timeDelay, 300,false)?>>5 min</option>
<option value="300" <?php echo selected($timeDelay, 600,false)?>>10 min</option>
<option value="300" <?php echo selected($timeDelay, 3600,false)?>>1 h</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Debug Mode', 'miraviawoo')?>
<p class="description"><?php echo __('This active debug mode, developer support','miraviawoo')?></p>
</th>
<td>
<input type="checkbox" value="1" name="miraviaDebugMode" <?php echo checked($debugMode, '1', false)?> />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Status Orders', 'miraviawoo')?>
<p class="description"><?php echo __('Set default status for Miravia Orders','miraviawoo')?></p>
</th>
<td>
<select name="miravia_order_defuault_status">
<?php
foreach($statuses as $key => $status) {
if($key == 'wc-completed' || $key == 'wc-refunded' || $key == 'wc-failed' || $key == 'wc-cancelled') { continue; }
$selected = selected($key, $statusDefaultSelected, false);
echo '<option value="'.$key.'" '.$selected.'>'.$status.'</option>';
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Transport Mode', 'miraviawoo')?>
<p class="description"><?php echo __('Select DBM or DBS','miraviawoo')?></p>
</th>
<td>
<select name="miravia_transport_mode">
<option value="dbm" <?php selected($transportMode, 'dbm') ?>>DBM</option>
<option value="dbs" <?php selected($transportMode, 'dbs') ?>>DBS</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Stock Default', 'miraviawoo')?>
<p class="description"><?php echo __('When products have "In Stock" without quantity, set stock to send Miravia','miraviawoo')?></p>
</th>
<td>
<input type="number" value="<?php echo get_option('_miravia_default_stock', 100)?>" name="_miravia_default_stock" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('EAN Key', 'miraviawoo')?>
<p class="description"><?php echo __('This use for extract EAN code from products, enter the meta key for your EAN code','miraviawoo')?></p>
</th>
<td>
<input type="text" name="miravia_ean_key" value="<?php echo get_option('miravia_ean_key', '')?>" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Brand', 'miraviawoo')?>
<p class="description"><?php echo __('Set default Brand for your products','miraviawoo')?></p>
</th>
<td>
<select name="miravia_default_brand" class="search-select-miravia-brand">
<option value="<?php echo $defaultBrand ?>" <?php selected($defaultBrand, $defaultBrand) ?>><?php echo $defaultBrand ?></option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Unit', 'miraviawoo')?>
<p class="description"><?php echo __('Set default unit for all products','miraviawoo')?></p>
</th>
<td>
<select name="miravia_default_unit">
<option <?php echo selected('Units',$defaultUnit)?>value="Units">Units</option>
<option <?php echo selected('Liters',$defaultUnit)?>value="Liters">Liters</option>
<option <?php echo selected('Kilograms',$defaultUnit)?>value="Kilograms">Kilograms</option>
<option <?php echo selected('Grams',$defaultUnit)?>value="Grams">Grams</option>
<option <?php echo selected('Milliliters',$defaultUnit)?>value="Milliliters">Milliliters</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Unit Value', 'miraviawoo')?>
<p class="description"><?php echo __('Set default unit value for all products','miraviawoo')?></p>
</th>
<td>
<input type="text" name="miravia_default_unit_value" value="<?php echo $defaultUnitValue ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Category', 'miraviawoo')?>
<p class="description"><?php echo __('Set default unit value for all products','miraviawoo')?></p>
</th>
<td>
<select name="miravia_default_unit">
<?php
foreach($categories as $cat) {
echo "<option value='{$cat->term_id}'>{$cat->name}</option>";
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Only Stock', 'miraviawoo')?>
<p class="description"><?php echo __('Check this if you want sync only stock','miraviawoo')?></p>
</th>
<td>
<input type="checkbox" value="1" name="miravia_only_stock" <?php echo checked(get_option('miravia_only_stock', '0'), '1', false)?>" />
</td>
</tr>
</table>
<input type="submit" class="button" value="<?php echo __('Save')?>" />
</form>
</div>
@@ -0,0 +1,81 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
$profiles = MiraviaCore::get_accounts();
if($profiles) {
$category = new MiraviaCategory($profiles[0]['token']);
$tree = $category->getCategories();
}
//Save data
if(isset($_POST['action']) and sanitize_text_field($_POST['action']) == 'save_profile') {
$categories_recibed = implode(",", array_map('sanitize_text_field', $_POST['tax_input']['product_cat']));
$acounts_recibed = implode(",", array_map('sanitize_text_field', $_POST['profile']));
// die(var_dump($categories_recibed));
if($_POST['description'] == '') {
echo "<p>Profile need description</p>";
}else{
$saveProfile = MiraviaCore::add_profile(array(
'name' => sanitize_text_field($_POST['description']),
'accounts_id' => $acounts_recibed,
'categories' => $categories_recibed,
'miravia_category' => sanitize_text_field($_POST['miravia_category'])
));
if($saveProfile) {
wp_safe_redirect(admin_url("admin.php?page=miravia_settings&subpage=edit_profile&action=edit&id={$saveProfile}"));
}
}
}
?>
<div class="wrap miravia-pane">
<h1 class="wp-heading-inline">Create new profile</h1>
<hr class="wp-header-end">
<form method="post">
<input type="hidden" name="action" value="save_profile" />
<table class="form-table">
<tr valign="top">
<th scope="row"><?php echo __('Description Profile', 'miraviawoo')?></th>
<td>
<input required type="text" name="description" value="" />
</td>
</tr>
<tr>
<td>Categories
<span class="description">Select the categories with this profile</span>
</td>
<td>
<ul class="miravia_categories_select" style="height: 400px; overflow: auto;">
<?php wp_terms_checklist(0, array(
'taxonomy' => 'product_cat'
));?>
</ul>
</td>
</tr>
<tr>
<td>Miravia Category</td>
<td>
<select name="miravia_category" class="search-select-miravia">
<?php
foreach($tree as $cat) {
echo "<option value='{$cat['id']}'>{$cat['name']}</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Seller Accounts</td>
<td>
<?php
foreach($profiles as $profile) {
echo "<p><input type='checkbox' name='profile[]' value='{$profile['id']}' /> {$profile['name']}</p>";
}
?>
</td>
</tr>
</table>
<input type="submit" name="submit" class="button" value="Save profile" />
</form>
</div>
@@ -0,0 +1,29 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
?>
<div class="wrap">
<div class="region_config_dashboard">
<img style="width:300px" src="<?php echo plugins_url('assets/img/miravia-logo-new.svg', MIRAVIA_ASSETS_PATH)?>" />
<h2>Dashboard</h2>
<p>Welcome to Miravia Dashboard for WooCommerce, manage your products on Miravia Store and download orders on your WooCommerce site.</p>
<a href="admin.php?page=miravia_settings&subpage=orders" class="btn_config">
<h4><i class="fa fa-file"></i> Orders</h4>
<p>Manage orders from Miravia, download and change status from your WooCommerce</p>
</a>
<a href="admin.php?page=miravia_settings&subpage=profiles" class="btn_config">
<h4><i class="fa-solid fa-tree"></i> Profiles</h4>
<p>Create o Edit your profiles to send products to Miravia</p>
</a>
<a href="admin.php?page=miravia_settings&subpage=products" class="btn_config">
<h4><i class="fa-solid fa-store"></i> Products</h4>
<p>View Miravia products, status, stock and connect with your WooCommerce products</p>
</a>
<a href="admin.php?page=miravia_settings&subpage=configuration" class="btn_config">
<h4><i class="fa-solid fa-gear"></i> Configuration</h4>
<p>Change plugin configuration for attributes, default values, etc.</p>
</a>
<div class="clear"></div>
<p>Connector version <?php echo MIRAVIA_WOO_VERSION ?> (Build <?php echo MIRAVIA_BUILD_VERSION ?>)</p>
</div>
</div>
@@ -0,0 +1,49 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
$id = sanitize_text_field($_GET['id']);
$jobs = MiraviaCore::get_job_detail($id);
$miraviaTable = new MiraviaTable();
$data = array();
// MiraviaCore::debug($jobs);
if($jobs) {
$token = $jobs[0];
$miraviaTable->custom_actions = array(
'download' => sprintf('<a href="/wp-admin/post.php?post=[id_woocommerce]&action=edit" target="_blank">Edit</a>' ),
'view' => '<a href="https://www.miravia.es/p/i[id_miravia].html" target="_blank">View on Miravia</a>',
);
$miraviaTable->columns = [
'name' => 'SKU',
'id_miravia' => 'ID Miravia',
'id_woocommerce' => 'ID WooCommerce',
'lastError' => 'Last Error',
'created' => 'Created',
'updated' => 'Updated',
'status_text' => 'Status'
];
foreach($jobs as $k => $p) {
$data[] = array(
'name' => $p['sku'],
'id_miravia' => $p['id_miravia'],
'id_woocommerce' => $p['id_woocommerce'],
'lastError' => $p['lastError'],
'created' => date('d-m-Y H:i:s', strtotime($p['created'])),
'updated' => date('d-m-Y H:i:s', strtotime($p['updated'])),
'status_text' => $p['status_text']
);
}
}
// die('<pre>' . print_r($data, true) . '</pre>');
$miraviaTable->data_table = $data;
$miraviaTable->total_elements = count($data);
$miraviaTable->prepare_items();
?>
<div class="wrap">
<h2>Job Detail</h2>
<?php echo $miraviaTable->display(); ?>
</div>
@@ -0,0 +1,208 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
if($_GET['id']) {
$id = sanitize_text_field($_GET['id']);
$prof =MiraviaCore::get_profiles($id);
}else{
exit('Validation error');
}
$token = MiraviaCore::get_miravia_account_default();
if($token) {
$token = $token['token'];
}else{
die('Error: Account is not ready');
}
$link = new MiraviaCategory($token);
$profiles = MiraviaCore::get_accounts();
if($profiles) {
$category = new MiraviaCategory($token);
$tree = $category->getCategories();
}
$attributes = $link->getAttributes($prof['miravia_category']);
// var_dump($prof['config']);
try {
$default_attributes = $prof['config'] ? json_decode($prof['config'], true) : [];
}catch(Exception $e) {
$default_attributes = [];
}
// MiraviaCore::debug($default_attributes);
$attributes_woocommerce = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' );
if(isset($_POST['action']) and sanitize_text_field($_POST['action']) == 'save_miravia_attr_default') {
$default_attributes = sanitize_text_field($_POST['attr']);
$args = array('config' => json_encode(array('attr' => sanitize_text_field($_POST['attr']))));
LOG::add($args);
if(MiraviaCore::update_profile($args, array('id' => $id))) {
echo esc_html('Attributes profile saved');
}
}
//Save data
if(isset($_POST['action']) and sanitize_text_field($_POST['action']) == 'update_profile') {
if($_POST['description'] == '') {
echo "<p>Profile need description</p>";
}elseif ($_POST['tax_input']['product_cat'] == null) {
echo "<p>Profile need categories</p>";
}else{
$categories_recibed = implode(",", array_map('sanitize_text_field', $_POST['tax_input']['product_cat']));
$acounts_recibed = implode(",", array_map('sanitize_text_field', $_POST['profile']));
// die(var_dump($categories_recibed));
$saveProfile = MiraviaCore::update_profile(array(
'name' => sanitize_text_field($_POST['description']),
'accounts_id' => $acounts_recibed,
'categories' => $categories_recibed,
'miravia_category' => sanitize_text_field($_POST['miravia_category'])
), array('id' => $id));
if($saveProfile) {
wp_safe_redirect('?page=miravia_settings&subpage=profiles');
}
}
}
//Rules
$miraviaTable = new MiraviaTable([
'sccren' => 'miravia_rules'
]);
$miraviaTable->custom_actions = array(
'edit' => sprintf('<a href="?page=%s&subpage=%s&action=%s&id=[id]">Editar</a>', sanitize_text_field($_REQUEST['page']), 'edit_rule', 'edit', ),
'delete' => sprintf('<a href="?page=%s&subpage=%s&action=%s&id=[id]">Eliminar</a>', sanitize_text_field($_REQUEST['page']), sanitize_text_field($_REQUEST['subpage']), 'delete'),
);
$miraviaTable->columns = [
'id' => "ID",
'name' => "Name",
'created' => 'Created',
'updated' => 'Updated'
];
$miraviaTable->default_column_name = 'name_rule';
$rules = MiraviaCore::get_rules(false, "profile_id = {$id} or profile_id = '0'");
$miraviaTable->data_table = $rules;
$miraviaTable->total_elements = count($rules);
$miraviaTable->prepare_items();
?>
<div class="wrap miravia-pane">
<h1 class="wp-heading-inline">Edit Profile</h1>
<hr class="wp-header-end">
<div class="text-sm font-medium text-center text-gray-500">
<ul style="display:flex" class="mv-tab-nav">
<li>
<a href="javascript:void(0);" data-tab="profile" class="mv-bt-tab active" style="margin-right:10px">Profile</a>
</li>
<li>
<a href="javascript:void(0);" data-tab="attributes" class="mv-bt-tab" style="margin-right:10px">Attributes</a>
</li>
<li>
<a href="javascript:void(0);" data-tab="rules" class="mv-bt-tab" style="margin-right:10px">Rules</a>
</li>
</ul>
</div>
<div class="mv-tabs">
<div class="mv-tab mv-tab-active" data-tab="profile">
<form method="post">
<input type="hidden" name="action" value="update_profile" />
<table class="form-table">
<tr valign="top">
<td scope="row"><?php echo __('Description Profile', 'miraviawoo')?>
<span class="description"><?php echo __('Describe your Profile, this use on admin site only') ?></span>
</td>
<td>
<input type="text" name="description" value="<?php echo $prof['name']?>" />
</td>
</tr>
<tr>
<td>Categories
<span class="description">Select the categories with this profile</span>
</td>
<td>
<ul class="miravia_categories_select" style="height: 350px; overflow: auto; border:1px solid #ccc; padding: 15px;">
<?php wp_terms_checklist(0, array(
'taxonomy' => 'product_cat',
'selected_cats' => explode(",", $prof['categories'])
));?>
</ul>
</td>
</tr>
<tr>
<td>Miravia Category</td>
<td>
<select name="miravia_category" class="search-select-miravia">
<?php
foreach($tree as $cat) {
$selected = "";
if($cat['id'] == $prof['miravia_category']) {
$selected = 'selected="selected"';
}
echo "<option value='{$cat['id']}' {$selected}>{$cat['name']}</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Seller Accounts</td>
<td>
<?php
$profiles_array = explode(',', $prof['accounts_id']);
foreach($profiles as $profile) {
$selected = '';
if(in_array($profile['id'], $profiles_array)) {
$selected = 'checked="checked"';
}
echo "<p><input type='checkbox' name='profile[]' {$selected} value='{$profile['id']}' /> {$profile['name']}</p>";
}
?>
</td>
</tr>
</table>
<input type="submit" name="submit" class="button" value="Update profile" />
<button type="button" class="button sendProductsMiravia" data-profile="<?php echo $id?>">Send Products</button>
</form>
</div>
<div class="mv-tab" data-tab="attributes">
<form method="post">
<input type="hidden" name="action" value="save_miravia_attr_default" />
<table width="100%">
<?php
if($attributes_woocommerce) {
foreach($attributes_woocommerce as $slug => $attr) {
$options = '';
if (!empty ($attributes ) ) {
foreach ( $attributes as $key => $v ) {
if($v['is_sale_prop'] == 0) {
continue;
}
$selected = '';
if($default_attributes != '' and isset($default_attributes[$slug]) and $v['name'] == $default_attributes[$slug]) {
$selected = 'selected="selected"';
}
$options .= "<option value='{$v['name']}' {$selected}>{$v['label']}</option>";
}
}
echo "<tr>
<td style='text-transform: capitalize;'>{$attr}</td>
<td><select class='attribute' name='attr[{$slug}]'>
<option value='0'> - Select - </option>{$options}</select></td>
</tr>";
}
}
?>
</table>
<input type="submit" name="submit" class="button" value="Save Attributes" />
</form>
</div>
<div class="mv-tab" data-tab="rules">
<?php
$miraviaTable->display();
?>
</div>
</div>
</div>
+129
View File
@@ -0,0 +1,129 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
$categories_end = array();
if($_GET['id']) {
$id = sanitize_text_field($_GET['id']);
$rule =MiraviaCore::get_rules($id);
}else{
exit('Validation error');
}
$profiles = MiraviaCore::get_profiles();
$accounts = MiraviaCore::get_accounts();
$actions_json = json_decode($rule['action_json']);
$categories = get_terms(array('taxonomy' => 'product_cat'));
foreach($categories as $cat) {
$categories_end[] = (object) array('lk_option' => $cat->name, 'lk_value' => $cat->term_id);
}
if(isset($_POST['action']) and sanitize_text_field($_POST['action']) == 'save_rule') {
$filterDecoded = urldecode($_POST['filter_data']);
$actionsDecoded = urldecode($_POST['action_detail_data']);
$account = sanitize_text_field($_POST['apply_accounts']);
$profile = sanitize_text_field($_POST['apply_profile']);
$arrayDatosRule = array(
'name_rule' => sanitize_text_field($_POST['name_rule']),
'accounts' => intval($account),
'profile_id' => intval($profile),
'rules_json' => $filterDecoded,
'action_json' => $actionsDecoded,
'action_type' => sanitize_text_field($_POST['action_type'])
);
$saveProfile = MiraviaCore::update_rule($arrayDatosRule, array('id' => $id));
if($saveProfile) {
$rule = $arrayDatosRule;
echo esc_html('Rule saved');
}
}
// var_dump($rule);
?>
<script>
const action_detail = <?php echo $rule['action_json']?>;
const filter = <?php echo $rule['rules_json']?>;
const categories = <?php echo json_encode($categories_end)?>;
</script>
<div class="wrap miravia-pane">
<h1 class="wp-heading-inline">Edit Rule to Miravia</h1>
<hr class="wp-header-end">
<form id="miravia_rules_form" method="post">
<input type="hidden" name="action" value="save_rule" />
<table class="form-table">
<tr valign="top">
<th scope="row"><?php echo __('Name of Rule', 'miraviawoo')?>
<span class="description"><?php echo __('Describe your rule', 'miraviawoo')?></span>
</th>
<td>
<input type="text" name="name_rule" placeholder="Rule Name..." value="<?php echo $rule['name_rule']?>" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Accounts', 'miraviawoo')?>
<span class="description"><?php echo __('Select all or one account to apply this rule', 'miraviawoo')?></span>
</th>
<td>
<select name="apply_accounts">
<option value="0">Apply all accounts</option>
<?php
$accounts_array = explode(',', $rule['accounts']);
foreach($accounts as $acc) {
$selected = '';
if(in_array($acc['id'], $accounts_array)) {
$selected = 'selected="selected"';
}
echo "<option value='{$acc['id']}' {$selected}>{$acc['name']}</option>";
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Profile', 'miraviawoo')?>
<span class="description"><?php echo __('Select all or one profile to apply this rule', 'miraviawoo')?></span>
</th>
<td>
<select name="apply_profile">
<option value="0">Apply all profiles</option>
<?php
foreach($profiles as $pro) {
$selected = '';
if($pro['id'] == $rule['profile_id']) {
$selected = 'selected="selected"';
}
echo " <option value='{$pro['id']}' {$selected}>{$pro['name']}</option>";
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Rules', 'miraviawoo')?>
<span class="description"><?php echo __('Create a conditionals to apply this rule', 'miraviawoo')?></span>
</th>
<td>
<div id="filter_editor" style="border:1px solid #CCC; padding:10px; background-color:#FFF;border-radius:5px"></div>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Actions', 'miraviawoo')?>
<span class="description"><?php echo __('If conditionals is true, what happens?', 'miraviawoo')?></span>
</th>
<td>
<div style="border:1px solid #CCC; padding:10px; margin-top:20px; background-color:#FFF; border-radius:5px">
<select id="action_type" name="action_type" style="margin-bottom:10px">
<option value="remove" <?php echo $rule['action_type'] == 'remove' ? 'selected="selected"' : ''?>>Do not send these products</option>
<option value="only" <?php echo $rule['action_type'] == 'only' ? 'selected="selected"' : ''?>>Only send these products</option>
<option value="price_stock" <?php echo $rule['action_type'] == 'price_stock' ? 'selected="selected"' : ''?>>Change price or stock</option>
<option value="name" <?php echo $rule['action_type'] == 'name' ? 'selected="selected"' : ''?>>Name</option>
</select>
<div id="action_detail"></div>
</div>
</td>
</tr>
</table>
<input type="submit" name="submit" class="button" value="Save Rule" />
</form>
</div>
+45
View File
@@ -0,0 +1,45 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
$jobs = MiraviaCore::get_jobs();
$miraviaTable = new MiraviaTable();
$data = array();
if($jobs) {
$token = $jobs[0];
$miraviaTable->custom_actions = array(
'detail' => sprintf('<a href="?page=%s&subpage=%s&id=[name]">Detail</a>', sanitize_text_field($_REQUEST['page']), 'detail-job' ),
'download' => sprintf('<a href="javascript:void(0);" class="checkJob" data-token="[token]" data-id="[name]">Check Status</a>', sanitize_text_field($_REQUEST['page']), sanitize_text_field($_REQUEST['subpage']), 'download', ),
'cancel' => sprintf('<a href="javascript:void(0);" class="cancelJob" data-token="[token]" data-id="[name]">Cancel Job</a>', sanitize_text_field($_REQUEST['page']), sanitize_text_field($_REQUEST['subpage']), 'download', ),
);
$miraviaTable->columns = [
'name' => 'Job',
'status' => 'Status',
'total' => 'Total Products',
'updated' => 'Updated',
];
foreach($jobs as $k => $p) {
$data[] = array(
'name' => $p['job_id'],
'status' => '<span class="status_result">...</span>',
'token' => $p['token'],
'total' => $p['total'],
'updated' => date('d-m-Y H:i:s', strtotime($p['updated'])),
);
}
}
// die('<pre>' . print_r($data, true) . '</pre>');
$miraviaTable->data_table = $data;
$miraviaTable->total_elements = count($data);
$miraviaTable->prepare_items();
?>
<div class="wrap">
<h2>Miravia Jobs</h2>
<?php echo $miraviaTable->display(); ?>
</div>
+63
View File
@@ -0,0 +1,63 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
if(isset($_GET['account'])) {
$link = new MiraviaLink(sanitize_text_field($_GET['token']));
$orders = $link->getOrders('2022-09-01');
$miraviaTable = new MiraviaTable();
// die('<pre>'.print_r($orders,true).'</pre>');
$data = array();
$miraviaTable->custom_actions = array(
'download' => sprintf('<a href="javascript:void(0);" class="download_order" data-account="'.sanitize_text_field($_GET['account_id']).'" data-token="'.sanitize_text_field($_GET['token']).'" data-id="[id]">Download</a>', sanitize_text_field($_REQUEST['page']), sanitize_text_field($_REQUEST['subpage']), 'download', ),
);
$miraviaTable->columns = [
'id' => "Order Number",
'name' => 'Customer Name',
'price' => "Price",
'statuses' => "Status",
'created_at' => 'Created',
'updated_at' => 'Updated',
];
foreach($orders['data']['orders'] as $k => $p) {
$data[] = array(
'id' => $p['order_number'],
'name' => $p['customer_first_name'],
'price' => number_format($p['price'], 2) . '€',
'statuses' => $p['statuses'][0],
'created_at' => date('d-m-Y H:i', strtotime($p['created_at'])),
'updated_at' => date('d-m-Y H:i', strtotime($p['updated_at'])),
);
}
// die('<pre>' . print_r($data, true) . '</pre>');
$miraviaTable->data_table = $data;
$miraviaTable->total_elements = $orders['data']['count'];
$miraviaTable->prepare_items();
}else{
$accounts = MiraviaCore::get_accounts();
$html_accounts = "";
if(count($accounts) == 1) {
wp_safe_redirect( "?page=miravia_settings&subpage=orders&account={$accounts[0]['name']}&token={$accounts[0]['token']}&account_id={$accounts[0]['id']}", 301);
die();
}
foreach($accounts as $a) {
$html_accounts .= "<a href='?page=miravia_settings&subpage=orders&account={$a['name']}&token={$a['token']}&account_id={$a['id']}'>{$a['name']} ({$a['email']})</a>";
}
}
?>
<div class="wrap">
<h2>Miravia Orders</h2>
<?php if(!isset($_GET['account'])) {
echo wp_kses('<p>'.__('Select an account', 'miravia').'</p>', array('p' => array()));
echo wp_kses($html_accounts, array('a' => array()));
}else{
echo wp_kses('<p>'.__('Orders by account', 'miravia').' '.sanitize_text_field($_GET['account']).'</p>', array('p' => array()));
$miraviaTable->display();
} ?>
</div>
@@ -0,0 +1,7 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
$result = MiraviaCore::get_profile_by_product('LO52027');
MiraviaCore::debug($result);
@@ -0,0 +1,72 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
$page = isset($_GET['paged']) ? intval($_GET['paged']) - 1 : 1;
$pagesize = 10;
$token = MiraviaCore::get_miravia_account_default();
if($token) {
$token = $token['token'];
}else{
die('Error: Account is not ready');
}
if(isset($_GET['action']) and $_GET['action'] == 'disconect') {
if(current_user_can( 'manage_options' )){
MiraviaCore::disconnect_product(sanitize_text_field( $_GET['id'] ));
wp_safe_redirect(admin_url("admin.php?page=miravia_settings&subpage=products"));
}
}
$link = new MiraviaLink($token);
$products = MiraviaCore::get_local_products($page, $pagesize);
// MiraviaCore::debug([$page, $pagesize, $products], false);
$miraviaTable = new MiraviaTable();
$miraviaTable->custom_actions = array(
'woocommerce' => '<a href="admin.php?page=miravia_settings&subpage=products&action=disconect&id=[id_woocommerce]">Disconect</a>',
//'upload' => '<a href="javascript:void(0)" data-profile="[ID]" class="sendProductsMiravia">Upload products</a>'
);
$data = array();
$miraviaTable->columns = [
'sku' => "SKU",
'name' => "Title",
'variationsTotal' => 'Total Variations',
'price' => "Price",
'lastError' => "Last Error",
'status_text' => "Status",
'created' => 'Created',
'updated' => 'Updated',
'actions' => 'Actions',
];
foreach($products as $k => $p) {
try {
$_product = new WC_Product($p['id_woocommerce']);
$data[] = array(
'id_woocommerce' => $p['id_woocommerce'],
'sku' => $p['sku'],
'name' => $p['name'],
'variationsTotal' => $p['variationsTotal'],
'price' => $_product->get_price_html(),
'lastError' =>$p['lastError'],
'status_text' => $p['status_text'],
'created' => $p['created'],
'updated' => $p['updated'],
'actions' => $p['id_miravia'] != 0 ? "<a target='_blank' href='https://www.miravia.es/p/i{$p['id_miravia']}.html' class='button'>View on Miravia</a>" : 'Not on Miravia',
);
}catch(Exception $e){
continue;
}
}
$miraviaTable->data_table = $data;
$miraviaTable->total_elements = count($data);
$miraviaTable->prepare_items();
?>
<div class="wrap">
<h2>Products</h2>
<p><?php echo __('Your products for Miravia', 'miravia')?></p>
<?php $miraviaTable->display(); ?>
</div>
@@ -0,0 +1,58 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
$page = isset($_GET['paged']) ? intval($_GET['paged']) - 1 : '0';
$pagesize = 10;
$token = MiraviaCore::get_miravia_account_default();
if($token) {
$token = $token['token'];
}else{
die('Error: Account is not ready');
}
$link = new MiraviaLink($token);
$products = $link->getProductList($page);
// MiraviaCore::debug($products['data'], false);
$miraviaTable = new MiraviaTable();
$data = array();
$miraviaTable->custom_actions = array(
'view' => '<a href="https://www.miravia.es/p/i[id].html">View</a>',
//'upload' => '<a href="javascript:void(0)" data-profile="[ID]" class="sendProductsMiravia">Upload products</a>'
);
$miraviaTable->columns = [
'id' => "ID",
'image' => "Image",
'name' => "Name",
'price' => "Price",
'status' => "Status",
'created' => 'Created',
'updated' => 'Updated',
'connect' => 'Connect'
];
foreach($products['data']['products'] as $k => $p) {
$image = count($p['images']) > 0 ? $p['images'][0] : false;
$sku = $p['skus'][0]['SellerSku'];
$id_remote = $p['item_id'];
$data[] = array(
'id' => $id_remote,
'image' => $image ? "<img width='50px' src='{$image}' />" : '',
'name' => $p['attributes']['name'],
'price' => number_format($p['skus'][0]['price'], 2) . '€',
'status' => $p['status'],
'created' => date('d-m-Y H:i', intval($p['created_time'])),
'updated' => date('d-m-Y H:i', intval($p['updated_time'])),
'connect' => "<a href='javascript:void(0);' class='button connectProductRemote' data-sku='{$sku}' data-id='{$id_remote}'>Connect</a>"
);
}
// die('<pre>' . print_r($data, true) . '</pre>');
$miraviaTable->data_table = $data;
$miraviaTable->total_elements = $products['data']['total_products'];
$miraviaTable->prepare_items();
?>
<div class="wrap">
<h2>Miravia Products</h2>
<p><?php echo __('Your remote products created in Miravia account', 'miravia')?></p>
<?php $miraviaTable->display(); ?>
</div>
@@ -0,0 +1,39 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
if(isset($_GET['action']) and $_GET['action'] == 'delete') {
if(current_user_can( 'manage_options' )){
MiraviaCore::delete_profile(sanitize_text_field( $_GET['id'] ));
wp_safe_redirect(admin_url("admin.php?page=miravia_settings&subpage=profiles"));
}
}
$miraviaTable = new MiraviaTable([
'sccren' => 'miravia_profiles'
]);
$miraviaTable->custom_actions = array(
'edit' => sprintf('<a href="?page=%s&subpage=%s&action=%s&id=[id]">Editar</a>', sanitize_text_field($_REQUEST['page']), 'edit_profile', 'edit', ),
'delete' => sprintf('<a href="?page=%s&subpage=%s&action=%s&id=[id]">Eliminar</a>', sanitize_text_field($_REQUEST['page']), sanitize_text_field($_REQUEST['subpage']), 'delete'),
'upload' => '<a href="javascript:void(0)" data-profile="[id]" class="sendProductsMiravia">Upload products</a>'
);
$miraviaTable->columns = [
'id' => "ID",
'name' => "Name",
'created' => 'Created',
'updated' => 'Updated',
'sync' => 'Status Sync'
];
$profiles = MiraviaCore::get_profiles();
$miraviaTable->data_table = $profiles;
$miraviaTable->total_elements = count($profiles);
$miraviaTable->prepare_items();
?>
<div class="wrap">
<h1 class="wp-heading-inline">Mapping Profiles</h1>
<a href="<?php echo admin_url('admin.php?page=miravia_settings&subpage=create_profile')?>" class="page-title-action">Create Profile</a>
<hr class="wp-header-end">
<?php
$miraviaTable->display();
?>
</div>
+37
View File
@@ -0,0 +1,37 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
$miraviaTable = new MiraviaTable([
'sccren' => 'miravia_rules'
]);
if(isset($_GET['action']) and $_GET['action'] == 'delete') {
if(current_user_can( 'manage_options' )){
MiraviaCore::delete_rule(sanitize_text_field( $_GET['id'] ));
wp_safe_redirect(admin_url("admin.php?page=miravia_settings&subpage=rules"));
}
}
$miraviaTable->custom_actions = array(
'edit' => sprintf('<a href="?page=%s&subpage=%s&action=%s&id=[id]">Editar</a>', sanitize_text_field($_REQUEST['page']), 'edit_rule', 'edit', ),
'delete' => sprintf('<a href="?page=%s&subpage=%s&action=%s&id=[id]">Eliminar</a>', sanitize_text_field($_REQUEST['page']), sanitize_text_field($_REQUEST['subpage']), 'delete'),
);
$miraviaTable->columns = [
'id' => "ID",
'name' => "Name",
'created' => 'Created',
'updated' => 'Updated'
];
$miraviaTable->default_column_name = 'name_rule';
$rules = MiraviaCore::get_rules();
$miraviaTable->data_table = $rules;
$miraviaTable->total_elements = count($rules);
$miraviaTable->prepare_items();
?>
<div class="wrap">
<h1 class="wp-heading-inline">Rules</h1>
<a href="<?php echo admin_url('admin.php?page=miravia_settings&subpage=add-rule')?>" class="page-title-action">Create Rule</a>
<hr class="wp-header-end">
<?php
$miraviaTable->display();
?>
</div>
+11
View File
@@ -0,0 +1,11 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $MIRAVIAWOO;
if(!isset($_GET['subpage'])) {
$page = 'dashboard';
}else{
$page = sanitize_text_field($_GET['subpage']);
}
require_once(MIRAVIA_VIEWS_PATH . 'pages/' . $page . '.php');