Skip to content

Commit

Permalink
Merge pull request #239 from InstaWP/two-way-sync
Browse files Browse the repository at this point in the history
Two way sync
  • Loading branch information
iamsayan committed Jan 3, 2024
2 parents 7c374dc + d80a6e6 commit 4abd9b0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
11 changes: 6 additions & 5 deletions includes/sync/class-instawp-sync-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ public function sync_changes() {
public function get_events_summary() {
check_ajax_referer( 'instawp-tws', 'nonce' );

$where = "1=1";
$where = "`status`='completed'";
$where2 = [];
$connect_id = ! empty( $_POST['connect_id'] ) ? intval( $_POST['connect_id'] ) : 0;
$entry_ids = ! empty( $_POST['ids'] ) ? array_map( 'intval', explode( ',', $_POST['ids'] ) ) : [];

if ( $connect_id > 0 ) {
$where .= " AND connect_id=" . $connect_id;
$where .= " AND `connect_id`=" . $connect_id;
$staging_site = get_connect_detail_by_connect_id( $connect_id );

if ( ! empty( $staging_site ) && isset( $staging_site['created_at'] ) && ! instawp()->is_staging ) {
Expand Down Expand Up @@ -355,7 +355,7 @@ private function get_events_sync_list_pagination( $total, $items_per_page, $page
}

private function get_total_pending_events_count() {
$where = "1=1";
$where = "`status`='completed'";
$where2 = [];
$connect_id = ! empty( $_POST['connect_id'] ) ? intval( $_POST['connect_id'] ) : 0;
$entry_ids = ! empty( $_POST['ids'] ) ? array_map( 'intval', explode( ',', $_POST['ids'] ) ) : [];
Expand Down Expand Up @@ -410,12 +410,13 @@ private function get_connect_quota_remaining_limit() {
}

private function pack_pending_sync_events() {
$where = $where2 = "1=1";
$where = "`status`='completed'";
$where2 = "1=1";
$connect_id = ! empty( $_POST['dest_connect_id'] ) ? intval( $_POST['dest_connect_id'] ) : 0;
$entry_ids = ! empty( $_POST['ids'] ) ? array_map( 'intval', explode( ',', $_POST['ids'] ) ) : [];

if ( $connect_id > 0 ) {
$where .= " AND connect_id=" . $connect_id;
$where .= " AND `connect_id`=" . $connect_id;
}

if ( ! empty( $entry_ids ) ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/sync/class-instawp-sync-apis.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function events_receiver( WP_REST_Request $req ) {
continue;
}

$source_id = ( ! empty( $v->source_id ) ) ? intval( $v->source_id ) : null;
$source_id = ( ! empty( $v->source_id ) ) ? sanitize_text_field( $v->source_id ) : null;
$v->source_id = $source_id;

$response_data = apply_filters( 'INSTAWP_CONNECT/Filters/process_two_way_sync', [], $v );
Expand Down
65 changes: 43 additions & 22 deletions includes/sync/class-instawp-sync-wc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct() {

// Attributes actions
add_action( 'woocommerce_attribute_added', [ $this,'attribute_added' ], 10, 2 );
add_action( 'woocommerce_attribute_updated', [ $this, 'attribute_updated' ], 10, 2 );
add_action( 'woocommerce_attribute_updated', [ $this, 'attribute_updated' ], 10, 3 );
add_action( 'woocommerce_attribute_deleted', [ $this, 'attribute_deleted' ], 10, 2 );

// Process event
Expand Down Expand Up @@ -55,39 +55,43 @@ public function attribute_added( $id, $data ) {
}

$event_name = __( 'Woocommerce attribute added', 'instawp-connect' );
$this->add_event( $event_name, 'woocommerce_attribute_added', $data, $id );

$data['attribute_id'] = $id;
$this->add_event( $event_name, 'woocommerce_attribute_added', $data, $data['attribute_name'] );
}

/**
* Attribute Updated (hook).
*
* @param int $id Updated attribute ID.
* @param array $data Attribute data.
* @param int $id Added attribute ID.
* @param array $data Attribute data.
* @param string $old_slug Attribute old name.
*/
public function attribute_updated( $id, $data ) {
public function attribute_updated( $id, $data, $old_slug ) {
if ( ! $this->can_sync() ) {
return;
}

$event_name = __('Woocommerce attribute updated', 'instawp-connect' );
$event_id = InstaWP_Sync_DB::existing_update_events(INSTAWP_DB_TABLE_EVENTS, 'woocommerce_attribute_updated', $id );
$event_id = InstaWP_Sync_DB::existing_update_events(INSTAWP_DB_TABLE_EVENTS, 'woocommerce_attribute_updated', $old_slug );

$this->add_event( $event_name, 'woocommerce_attribute_updated', $data, $id, $event_id );
$data['attribute_id'] = $id;
$this->add_event( $event_name, 'woocommerce_attribute_updated', $data, $old_slug, $event_id );
}

/**
* Attribute Deleted (hook).
*
* @param int $id Deleted attribute ID.
* @param array $data Attribute data.
* @param int $id Attribute ID.
* @param string $name Attribute name.
*/
public function attribute_deleted( $id, $data ) {
public function attribute_deleted( $id, $name ) {
if ( ! $this->can_sync() ) {
return;
}

$event_name = __( 'Woocommerce attribute deleted', 'instawp-connect' );
$this->add_event( $event_name, 'woocommerce_attribute_deleted', $data, $id );
$this->add_event( $event_name, 'woocommerce_attribute_deleted', [ 'attribute_id' => $id ], $name );
}

public function parse_event( $response, $v ) {
Expand All @@ -100,13 +104,19 @@ public function parse_event( $response, $v ) {

// add or update attribute
if ( in_array( $v->event_slug, [ 'woocommerce_attribute_added', 'woocommerce_attribute_updated' ], true ) ) {
$attribute = wc_get_attribute( $v->source_id );

if ( ! empty( $attribute ) ) {
unset( $details['id'] );
$attribute = wc_update_attribute( $v->source_id, $details );
$attribute_id = wc_attribute_taxonomy_id_by_name( $v->source_id );
$attribute_data = [
'name' => $details['attribute_label'],
'slug' => $details['attribute_name'],
'type' => $details['attribute_type'],
'orderby' => $details['attribute_orderby'],
'has_archives' => isset( $details['attribute_public'] ) ? (int) $details['attribute_public'] : 0,
];

if ( $attribute_id ) {
$attribute = wc_update_attribute( $attribute_id, $attribute_data );
} else {
$attribute = $this->create_attribute( $v->source_id, $details );
$attribute = wc_create_attribute( $attribute_data );
}

if ( is_wp_error( $attribute ) ) {
Expand All @@ -120,12 +130,21 @@ public function parse_event( $response, $v ) {
}

if ( $v->event_slug === 'woocommerce_attribute_deleted' ) {
$response = wc_delete_attribute( $v->source_id );
$attribute_id = wc_attribute_taxonomy_id_by_name( $v->source_id );

if ( ! $response ) {
if ( $attribute_id ) {
$response = wc_delete_attribute( $attribute_id );

if ( ! $response ) {
return InstaWP_Sync_Helpers::sync_response( $v, [], [
'status' => 'pending',
'message' => 'Failed'
] );
}
} else {
return InstaWP_Sync_Helpers::sync_response( $v, [], [
'status' => 'pending',
'message' => 'Failed'
'message' => 'Attribute not found'
] );
}
}
Expand All @@ -146,8 +165,10 @@ private function add_event( $event_name, $event_slug, $details, $source_id, $eve
switch ( $event_slug ) {
case 'woocommerce_attribute_added':
case 'woocommerce_attribute_updated':
$title = $details['attribute_label'];
$details = ( array ) wc_get_attribute( $source_id );
$title = $details['attribute_label'];
break;
case 'woocommerce_attribute_deleted':
$title = ucfirst( str_replace( [ '-', '_' ], ' ', $source_id ) );
break;
default:
$title = $details;
Expand Down
20 changes: 10 additions & 10 deletions languages/instawp-connect.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2024-01-02 11:41+0000\n"
"POT-Creation-Date: 2024-01-03 15:10+0000\n"
"X-Poedit-Basepath: ..\n"
"X-Poedit-KeywordsList: __;_e;_ex:1,2c;_n:1,2;_n_noop:1,2;_nx:1,2,4c;_nx_noop:1,2,3c;_x:1,2c;esc_attr__;esc_attr_e;esc_attr_x:1,2c;esc_html__;esc_html_e;esc_html_x:1,2c\n"
"X-Poedit-SearchPath-0: .\n"
Expand Down Expand Up @@ -465,31 +465,31 @@ msgstr ""
msgid "Enabling this option will allow reading WordPress error logs on this website remotely using the REST API."
msgstr ""

#: includes/class-instawp-tools.php:397
#: includes/class-instawp-tools.php:403
msgid "Tracking database could not found."
msgstr ""

#: includes/class-instawp-tools.php:407
#: includes/class-instawp-tools.php:413
msgid "API Signature and others data could not set properly"
msgstr ""

#: includes/class-instawp-tools.php:416
#: includes/class-instawp-tools.php:422
msgid "Could not create the forwarded file."
msgstr ""

#: includes/class-instawp-tools.php:420
#: includes/class-instawp-tools.php:426
msgid "InstaWP could not access the forwarded file due to security issue."
msgstr ""

#: includes/class-instawp-tools.php:429
#: includes/class-instawp-tools.php:435
msgid "InstaWP could not access or read required files from your WordPress directory due to file permission issue."
msgstr ""

#: includes/class-instawp-tools.php:431
#: includes/class-instawp-tools.php:437
msgid "Learn more."
msgstr ""

#: includes/class-instawp-tools.php:743
#: includes/class-instawp-tools.php:749
msgid "Launch %s"
msgstr ""

Expand Down Expand Up @@ -621,10 +621,10 @@ msgstr ""
msgid "Woocommerce attribute added"
msgstr ""

#: includes/sync/class-instawp-sync-wc.php:72
#: includes/sync/class-instawp-sync-wc.php:75
msgid "Woocommerce attribute updated"
msgstr ""

#: includes/sync/class-instawp-sync-wc.php:89
#: includes/sync/class-instawp-sync-wc.php:93
msgid "Woocommerce attribute deleted"
msgstr ""

0 comments on commit 4abd9b0

Please sign in to comment.