ption( 'woocommerce_attribute_lookup_regeneration_aborted', 'yes' ); } /** * Remove the flag indicating that the last lookup table regeneration process started was aborted. */ public function unset_regeneration_aborted_flag() { delete_option( 'woocommerce_attribute_lookup_regeneration_aborted' ); } /** * Tells if the last lookup table regeneration process started was aborted * (via deleting the 'woocommerce_attribute_lookup_regeneration_in_progress' option). * * @return bool True if the last lookup table regeneration process was aborted. */ public function regeneration_was_aborted(): bool { return get_option( 'woocommerce_attribute_lookup_regeneration_aborted' ) === 'yes'; } /** * Check if the lookup table contains any entry at all. * * @return bool True if the table contains entries, false if the table is empty. */ public function lookup_table_has_data(): bool { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared return ( (int) $wpdb->get_var( "SELECT EXISTS (SELECT 1 FROM {$this->lookup_table_name})" ) ) !== 0; } /** * Handler for 'woocommerce_get_sections_products', adds the "Advanced" section to the product settings. * * @param array $products Original array of settings sections. * @return array New array of settings sections. */ private function add_advanced_section_to_product_settings( array $products ): array { if ( $this->check_lookup_table_exists() ) { $products['advanced'] = __( 'Advanced', 'woocommerce' ); } return $products; } /** * Handler for 'woocommerce_get_settings_products', adds the settings related to the product attributes lookup table. * * @param array $settings Original settings configuration array. * @param string $section_id Settings section identifier. * @return array New settings configuration array. */ private function add_product_attributes_lookup_table_settings( array $settings, string $section_id ): array { if ( $section_id === 'advanced' && $this->check_lookup_table_exists() ) { $title_item = array( 'title' => __( 'Product attributes lookup table', 'woocommerce' ), 'type' => 'title', ); $regeneration_is_in_progress = $this->regeneration_is_in_progress(); if ( $regeneration_is_in_progress ) { $title_item['desc'] = __( 'These settings are not available while the lookup table regeneration is in progress.', 'woocommerce' ); } $settings[] = $title_item; if ( ! $regeneration_is_in_progress ) { $regeneration_aborted_warning = $this->regeneration_was_aborted() ? sprintf( "

%s

%s

", __( 'WARNING: The product attributes lookup table regeneration process was aborted.', 'woocommerce' ), __( 'This means that the table is probably in an inconsistent state. It\'s recommended to run a new regeneration process or to resume the aborted process (Status - Tools - Regenerate the product attributes lookup table/Resume the product attributes lookup table regeneration) before enabling the table usage.', 'woocommerce' ) ) : null; $settings[] = array( 'title' => __( 'Enable table usage', 'woocommerce' ), 'desc' => __( 'Use the product attributes lookup table for catalog filtering.', 'woocommerce' ), 'desc_tip' => $regeneration_aborted_warning, 'id' => 'woocommerce_attribute_lookup_enabled', 'default' => 'no', 'type' => 'checkbox', 'checkboxgroup' => 'start', ); $settings[] = array( 'title' => __( 'Direct updates', 'woocommerce' ), 'desc' => __( 'Update the table directly upon product changes, instead of scheduling a deferred update.', 'woocommerce' ), 'id' => 'woocommerce_attribute_lookup_direct_updates', 'default' => 'no', 'type' => 'checkbox', 'checkboxgroup' => 'start', ); } $settings[] = array( 'type' => 'sectionend' ); } return $settings; } }