diff --git a/includes/sync/class-instawp-sync-ajax.php b/includes/sync/class-instawp-sync-ajax.php index a7a46873..7de9c674 100644 --- a/includes/sync/class-instawp-sync-ajax.php +++ b/includes/sync/class-instawp-sync-ajax.php @@ -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 ) { @@ -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'] ) ) : []; @@ -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 ) ) { diff --git a/includes/sync/class-instawp-sync-apis.php b/includes/sync/class-instawp-sync-apis.php index 9bf4e995..4b629dbf 100644 --- a/includes/sync/class-instawp-sync-apis.php +++ b/includes/sync/class-instawp-sync-apis.php @@ -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 ); diff --git a/includes/sync/class-instawp-sync-wc.php b/includes/sync/class-instawp-sync-wc.php index 70d63fd3..e7a589c6 100644 --- a/includes/sync/class-instawp-sync-wc.php +++ b/includes/sync/class-instawp-sync-wc.php @@ -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 @@ -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 ) { @@ -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 ) ) { @@ -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' ] ); } } @@ -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; diff --git a/languages/instawp-connect.pot b/languages/instawp-connect.pot index 47885a67..be4938d8 100644 --- a/languages/instawp-connect.pot +++ b/languages/instawp-connect.pot @@ -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" @@ -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 "" @@ -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 ""