Skip to content

Commit

Permalink
working on HPOS compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
debba committed Oct 7, 2024
1 parent d264f53 commit 411421f
Show file tree
Hide file tree
Showing 8 changed files with 890 additions and 830 deletions.
5 changes: 4 additions & 1 deletion .wordpress-org/readme/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributors: dueclic
Tags: emailchef,newsletter,woocommerce,ecommerce,email
Requires at least:5.0.0
Tested up to: 6.6
Stable tag: 5.0
Stable tag: 5.1
WC requires at least: 5.0.0
WC tested up to: 8.3.1
License: GPLv2 or later
Expand Down Expand Up @@ -71,6 +71,9 @@ add_filter('emailchef_abandoned_cart_image_size', 'emailchef_abandoned_cart_imag

== Changelog ==

= 5.1 =
* HPos Compatibility

= 5.0 =
* REST API - bugfixes

Expand Down
19 changes: 12 additions & 7 deletions includes/class-wc-emailchef-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class WC_Emailchef_Api {
protected $api_url = "https://app.emailchef.com";
public $lastError;
private $isLogged = false;
private $authkey = false;
protected $authkey = false;

public function __construct( $username, $password ) {
$this->process_login( $username, $password );
Expand All @@ -20,12 +20,15 @@ public function isLogged() {
}

private function authkey_name(){
return apply_filters('emailchef_api_authkey_name', 'authkey');
return defined("EMAILCHEF_API_AUTHKEY_NAME") ? EMAILCHEF_API_AUTHKEY_NAME : 'authkey';
}

public function getApiUrl(){
return defined("EMAILCHEF_API_URL") ? EMAILCHEF_API_URL : $this->api_url;
}

private function process_login( $username, $password ) {

private function process_login( $username, $password ) {
if (!$authkey = get_transient('ecwc_authkey')) {

$response = $this->getDecodedJson( "/login", array(
Expand All @@ -51,16 +54,18 @@ private function process_login( $username, $password ) {

protected function get( $route, $args = array(), $type = "POST", $prefix = "/apps/api/v1" ) {

$url = apply_filters("emailchef_api_url", $this->api_url) . $prefix . $route;
$url = $this->getApiUrl() . $prefix . $route;

$auth = array();

if ( $this->authkey !== false ) {
$auth[$this->authkey_name()] = $this->authkey;
}

$args = array(
'body' => array_merge( $auth, $args ),
'method' => strtoupper( $type )
'body' => $args,
'method' => strtoupper( $type ),
'headers' => $auth
);

$args = apply_filters( "ec_wc_get_args", $args );
Expand All @@ -80,4 +85,4 @@ protected function getDecodedJson( $route, $args = array(), $type = "POST", $pre
return json_decode( $this->get( $route, $args, $type, $prefix ), true );
}

}
}
24 changes: 7 additions & 17 deletions includes/class-wc-emailchef-customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ public function lastOrder()
return $this->last_order;
}

public function lastShippedOrder()
{

if ($this->last_ship_order === false) {
$this->last_ship_order = wc_get_customer_last_order($this->id);
}

return $this->last_ship_order;
}

public function get($meta)
{

Expand Down Expand Up @@ -126,16 +116,16 @@ public function sync_array($newsletter = "no")
$this->queue['all_ordered_product_ids'] = wc_ec_get_all_products($this->id);
$this->queue['latest_order_product_ids'] = wc_ec_get_all_products($this->id, 1);

$this->queue['latest_order_id'] = $last_order_not_shipped !== FALSE ? $last_order_not_shipped->get_id() : 0;
$this->queue['latest_order_date'] = $last_order_not_shipped !== FALSE ? $last_order_not_shipped->get_date_modified()->date_i18n() : '';
$this->queue['latest_order_id'] = $last_order_not_shipped ? $last_order_not_shipped->get_id() : 0;
$this->queue['latest_order_date'] = $last_order_not_shipped ? $last_order_not_shipped->get_date_modified()->date_i18n() : '';

if ($this->queue['latest_order_id'] != null) {
$this->queue['latest_order_amount'] = $last_order_not_shipped !== FALSE ? $last_order_not_shipped->get_total() : 0;
$this->queue['latest_order_amount'] = $last_order_not_shipped ? $last_order_not_shipped->get_total() : 0;
} else {
$this->queue['latest_order_amount'] = 0;
}

if ($last_order_not_shipped !== FALSE) {
if ($last_order_not_shipped) {
$this->queue['latest_order_status'] = wc_ec_get_order_status_name("wc-" . $last_order_not_shipped->get_status());
}

Expand All @@ -146,9 +136,9 @@ public function sync_array($newsletter = "no")
$this->queue['all_ordered_product_ids'] = wc_ec_get_all_products($this->id);
$this->queue['latest_order_product_ids'] = wc_ec_get_all_products($this->id, 1);

$this->queue['latest_shipped_order_id'] = $last_order !== FALSE ? $last_order->get_id() : 0;
$this->queue['latest_shipped_order_date'] = $last_order !== FALSE ? $last_order->get_date_modified()->date_i18n() : '';
$this->queue['latest_shipped_order_status'] = wc_ec_get_order_status_name($last_order !== FALSE ? "wc-" . $last_order->get_status() : '');
$this->queue['latest_shipped_order_id'] = $last_order ? $last_order->get_id() : 0;
$this->queue['latest_shipped_order_date'] = $last_order ? $last_order->get_date_modified()->date_i18n() : '';
$this->queue['latest_shipped_order_status'] = wc_ec_get_order_status_name($last_order ? "wc-" . $last_order->get_status() : '');

return $this->queue;

Expand Down
25 changes: 25 additions & 0 deletions includes/class-wc-emailchef-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,10 @@ public function hooks() {
$this,
'sync_abandoned_carts'
) );
add_action( 'wp_ajax_' . $this->namespace . '_rebuild_customfields', array(
$this,
'rebuild_customfields'
) );
//add_action( 'upgrader_process_complete', array( $this, 'upgrade_also_list' ), 10, 2 );
add_action(
'wp_ajax_nopriv_' . $this->namespace . '_sync_abandoned_carts',
Expand Down Expand Up @@ -838,6 +842,27 @@ public function get_abandoned_carts( $limit = true, $where = "" ) {
);
}

public function rebuild_customfields(){
if (!current_user_can('manage_options')) {
wp_send_json_error(__('Permissions are not valid for this action', 'emailchef-for-woocommerce'));
}

$list_id = get_option( $this->prefixed_setting( "list" ) );

if ($list_id) {
WCEC()->log( sprintf( __( "[START] Custom fields rebuild for Emailchef list %d",
"emailchef-for-woocommerce" ), $list_id ) );

$this->wcec->emailchef()->initialize_custom_fields(
$list_id
);
wp_send_json_success( __( 'Custom fields had been rebuilt succesfully', 'emailchef-for-woocommerce' ) );

WCEC()->log( sprintf( __( "[END] Custom fields ad been rebuilt succesfully for list %d",
"emailchef-for-woocommerce" ), $list_id ) );
}
}

public function sync_abandoned_carts() {

global $wpdb;
Expand Down
30 changes: 27 additions & 3 deletions includes/class-wc-emailchef-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ private function add_hooks() {

add_action( "woocommerce_loaded", array( $this, "set_logger" ), 10 );

add_action( 'before_woocommerce_init', function() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', WC_EMAILCHEF_FILE, true );
}
} );

add_action( "ec_footer_copyright",
array( $this, 'dueclic_copyright' ) );

Expand All @@ -236,10 +242,10 @@ private function add_hooks() {
}
} );

add_action( 'admin_footer', array( $this, 'abcart_js' ) );
add_action( 'admin_footer', array( $this, 'emailchef_debug_js' ) );
}

public function abcart_js() {
public function emailchef_debug_js() {
$screen = get_current_screen();
if ( $screen->id === 'admin_page_emailchef-debug' ) {
?>
Expand All @@ -265,6 +271,24 @@ function (response) {
});
});

$(document).ready(function () {
$('.button-rebuild-customfields').on('click', function (evt) {
evt.preventDefault();
var userId = $(this).data('user-id');
var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
$.post(
ajaxurl,
{
'action': '<?php echo $this->namespace; ?>_rebuild_customfields'
},
function (response) {
console.log("Recover custom fields successfully");
location.reload();
}
);
});
});

})(jQuery);
</script>
<?php
Expand Down Expand Up @@ -503,7 +527,7 @@ public function settings( $fetch = false ) {
foreach ( $initial as $key => $init_value ) {
$value = get_option( $this->prefixed_setting( $key ) );

$settings[ $key ] = $value ? $value : $init_value;
$settings[ $key ] = $value ?: $init_value;
}

$settings = apply_filters( 'wc_emailchef_settings',
Expand Down
Loading

0 comments on commit 411421f

Please sign in to comment.