require_manage(); $this->require_connected(); $result = $this->plugin->api()->custom_fields( $this->plugin->settings()->get_store_id() ); if ( is_wp_error( $result ) ) { wp_send_json_error( $this->error_payload( $result ) ); } wp_send_json_success( array( 'fields' => isset( $result['fields'] ) && is_array( $result['fields'] ) ? $result['fields'] : array(), ) ); } /** * Leave a field out, use it again, or allow or stop rewriting it. */ public function update_field() { check_ajax_referer( self::NONCE_ACTION, 'nonce' ); $this->require_manage(); $this->require_connected(); $intent = isset( $_POST['intent'] ) ? sanitize_key( wp_unslash( $_POST['intent'] ) ) : ''; $id = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : ''; $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : ''; if ( ! in_array( $intent, self::INTENTS, true ) || '' === $id ) { wp_send_json_error( array( 'message' => __( 'That change was not recognised.', 'agentic-commerce-engine' ) ) ); } $patch = array(); if ( 'exclude' === $intent ) { $patch['exclude'] = array( array( 'id' => $id, 'label' => $label, ), ); } elseif ( 'include' === $intent ) { $patch['include'] = array( $id ); } elseif ( 'rewrite-on' === $intent ) { $patch['rewriteOn'] = array( $id ); } else { $patch['rewriteOff'] = array( $id ); } $settings = $this->plugin->settings(); $result = $this->plugin->api()->update_custom_fields( $settings->get_store_id(), $patch ); if ( is_wp_error( $result ) ) { wp_send_json_error( $this->error_payload( $result ) ); } // Mirror the exclusions locally so the next sync honours them, and // re-send a field brought back: it was stripped when it was left out. $changed = $settings->save_custom_fields( array( 'revision' => isset( $result['revision'] ) ? $result['revision'] : 0, 'excluded' => isset( $result['excluded'] ) && is_array( $result['excluded'] ) ? $result['excluded'] : array(), ) ); if ( 'include' === $intent && $changed && ! wp_next_scheduled( Cron::CATALOG_RESYNC ) ) { wp_schedule_single_event( time() + MINUTE_IN_SECONDS, Cron::CATALOG_RESYNC ); } wp_send_json_success( array( 'fields' => isset( $result['fields'] ) && is_array( $result['fields'] ) ? $result['fields'] : array(), 'resyncNeeded' => ! empty( $result['resyncNeeded'] ), ) ); } }