` argument. * * @param string $file_name Name of the file. * @param bool $template Optional. Use template theme directory. Default false. * @return string The whole file path or empty if the file doesn't exist. */ protected static function get_file_path_from_theme( $file_name, $template = false ) { $path = $template ? get_template_directory() : get_stylesheet_directory(); $candidate = $path . '/' . $file_name; return is_readable( $candidate ) ? $candidate : ''; } /** * Cleans the cached data so it can be recalculated. * * @since 5.8.0 * @since 5.9.0 Added the `$user`, `$user_custom_post_type_id`, * and `$i18n_schema` variables to reset. * @since 6.1.0 Added the `$blocks` and `$blocks_cache` variables * to reset. */ public static function clean_cached_data() { static::$core = null; static::$blocks = null; static::$blocks_cache = array( 'core' => array(), 'blocks' => array(), 'theme' => array(), 'user' => array(), ); static::$theme = null; static::$user = null; static::$user_custom_post_type_id = null; static::$i18n_schema = null; } /** * Returns an array of all nested JSON files within a given directory. * * @since 6.2.0 * * @param string $dir The directory to recursively iterate and list files of. * @return array The merged array. */ private static function recursively_iterate_json( $dir ) { $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) ); $nested_json_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) ); return $nested_json_files; } /** * Returns the style variations defined by the theme. * * @since 6.0.0 * @since 6.2.0 Returns parent theme variations if theme is a child. * * @return array */ public static function get_style_variations() { $variation_files = array(); $variations = array(); $base_directory = get_stylesheet_directory() . '/styles'; $template_directory = get_template_directory() . '/styles'; if ( is_dir( $base_directory ) ) { $variation_files = static::recursively_iterate_json( $base_directory ); } if ( is_dir( $template_directory ) && $template_directory !== $base_directory ) { $variation_files_parent = static::recursively_iterate_json( $template_directory ); // If the child and parent variation file basename are the same, only include the child theme's. foreach ( $variation_files_parent as $parent_path => $parent ) { foreach ( $variation_files as $child_path => $child ) { if ( basename( $parent_path ) === basename( $child_path ) ) { unset( $variation_files_parent[ $parent_path ] ); } } } $variation_files = array_merge( $variation_files, $variation_files_parent ); } ksort( $variation_files ); foreach ( $variation_files as $path => $file ) { $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); if ( is_array( $decoded_file ) ) { $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); $variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data(); if ( empty( $variation['title'] ) ) { $variation['title'] = basename( $path, '.json' ); } $variations[] = $variation; } } return $variations; } } 'woocommerce_attribute_lookup_processed_count' ); update_option( 'woocommerce_attribute_lookup_enabled', $enable_usage ? 'yes' : 'no' ); $this->data_store->unset_regeneration_in_progress_flag(); } /** * Add a 'Regenerate product attributes lookup table' entry to the Status - Tools page. * * @param array $tools_array The tool definitions array that is passed ro the woocommerce_debug_tools filter. * @return array The tools array with the entry added. */ private function add_initiate_regeneration_entry_to_tools_array( array $tools_array ) { if ( ! $this->data_store->check_lookup_table_exists() ) { return $tools_array; } $generation_is_in_progress = $this->data_store->regeneration_is_in_progress(); $generation_was_aborted = $this->data_store->regeneration_was_aborted(); $entry = array( 'name' => __( 'Regenerate the product attributes lookup table', 'woocommerce' ), 'desc' => __( 'This tool will regenerate the product attributes lookup table data from existing product(s) data. This process may take a while.', 'woocommerce' ), 'requires_refresh' => true, 'callback' => function() { $this->initiate_regeneration_from_tools_page(); return __( 'Product attributes lookup table data is regenerating', 'woocommerce' ); }, 'selector' => array( 'description' => __( 'Select a product to regenerate the data for, or leave empty for a full table regeneration:', 'woocommerce' ), 'class' => 'wc-product-search', 'search_action' => 'woocommerce_json_search_products', 'name' => 'regenerate_product_attribute_lookup_data_product_id', 'placeholder' => esc_attr__( 'Search for a product…', 'woocommerce' ), ), ); if ( $generation_is_in_progress ) { $entry['button'] = sprintf( /* translators: %d: How many products have been processed so far. */ __( 'Filling in progress (%d)', 'woocommerce' ), get_option( 'woocommerce_attribute_lookup_processed_count', 0 ) ); $entry['disabled'] = true; } else { $entry['button'] = __( 'Regenerate', 'woocommerce' ); } $tools_array['regenerate_product_attributes_lookup_table'] = $entry; if ( $generation_is_in_progress ) { $entry = array( 'name' => __( 'Abort the product attributes lookup table regeneration', 'woocommerce' ), 'desc' => __( 'This tool will abort the regenerate product attributes lookup table regeneration. After this is done the process can be either started over, or resumed to continue where it stopped.', 'woocommerce' ), 'requires_refresh' => true, 'callback' => function() { $this->abort_regeneration_from_tools_page(); return __( 'Product attributes lookup table regeneration process has been aborted.', 'woocommerce' ); }, 'button' => __( 'Abort', 'woocommerce' ), ); $tools_array['abort_product_attributes_lookup_table_regeneration'] = $entry; } elseif ( $generation_was_aborted ) { $processed_count = get_option( 'woocommerce_attribute_lookup_processed_count', 0 ); $entry = array( 'name' => __( 'Resume the product attributes lookup table regeneration', 'woocommerce' ), 'desc' => sprintf( /* translators: %1$s = count of products already processed. */ __( 'This tool will resume the product attributes lookup table regeneration at the point in which it was aborted (%1$s products were already processed).', 'woocommerce' ), $processed_count ), 'requires_refresh' => true, 'callback' => function() { $this->resume_regeneration_from_tools_page(); return __( 'Product attributes lookup table regeneration process has been resumed.', 'woocommerce' ); }, 'button' => __( 'Resume', 'woocommerce' ), ); $tools_array['resume_product_attributes_lookup_table_regeneration'] = $entry; } return $tools_array; } /** * Callback to initiate the regeneration process from the Status - Tools page. * * @throws \Exception The regeneration is already in progress. */ private function initiate_regeneration_from_tools_page() { $this->verify_tool_execution_nonce(); //phpcs:disable WordPress.Security.NonceVerification.Recommended if ( isset( $_REQUEST['regenerate_product_attribute_lookup_data_product_id'] ) ) { $product_id = (int) $_REQUEST['regenerate_product_attribute_lookup_data_product_id']; $this->check_can_do_lookup_table_regeneration( $product_id ); $this->data_store->create_data_for_product( $product_id ); } else { $this->check_can_do_lookup_table_regeneration(); $this->initiate_regeneration(); } //phpcs:enable WordPress.Security.NonceVerification.Recommended } /** * Enable or disable the actual lookup table usage. * * @param bool $enable True to enable, false to disable. * @throws \Exception A lookup table regeneration is currently in progress. */ private function enable_or_disable_lookup_table_usage( $enable ) { if ( $this->data_store->regeneration_is_in_progress() ) { throw new \Exception( "Can't enable or disable the attributes lookup table usage while it's regenerating." ); } update_option( 'woocommerce_attribute_lookup_enabled', $enable ? 'yes' : 'no' ); } /** * Check if everything is good to go to perform a complete or per product lookup table data regeneration * and throw an exception if not. * * @param mixed $product_id The product id to check the regeneration viability for, or null to check if a complete regeneration is possible. * @throws \Exception Something prevents the regeneration from starting. */ private function check_can_do_lookup_table_regeneration( $product_id = null ) { if ( $product_id && ! $this->data_store->check_lookup_table_exists() ) { throw new \Exception( "Can't do product attribute lookup data regeneration: lookup table doesn't exist" ); } if ( $this->data_store->regeneration_is_in_progress() ) { throw new \Exception( "Can't do product attribute lookup data regeneration: regeneration is already in progress" ); } if ( $product_id && ! wc_get_product( $product_id ) ) { throw new \Exception( "Can't do product attribute lookup data regeneration: product doesn't exist" ); } } /** * Callback to abort the regeneration process from the Status - Tools page. * * @throws \Exception The lookup table doesn't exist, or there's no regeneration process in progress to abort. */ private function abort_regeneration_from_tools_page() { $this->verify_tool_execution_nonce(); if ( ! $this->data_store->check_lookup_table_exists() ) { throw new \Exception( "Can't abort the product attribute lookup data regeneration process: lookup table doesn't exist" ); } if ( ! $this->data_store->regeneration_is_in_progress() ) { throw new \Exception( "Can't abort the product attribute lookup data regeneration process since it's not currently in progress" ); } $queue = WC()->get_instance_of( \WC_Queue::class ); $queue->cancel_all( 'woocommerce_run_product_attribute_lookup_regeneration_callback' ); $this->data_store->unset_regeneration_in_progress_flag(); $this->data_store->set_regeneration_aborted_flag(); $this->enable_or_disable_lookup_table_usage( false ); // Note that we are NOT deleting the options that track the regeneration progress (processed count, last product id to process). // This is on purpose so that the regeneration can be resumed where it stopped. } /** * Callback to resume the regeneration process from the Status - Tools page. * * @throws \Exception The lookup table doesn't exist, or a regeneration process is already in place. */ private function resume_regeneration_from_tools_page() { $this->verify_tool_execution_nonce(); if ( ! $this->data_store->check_lookup_table_exists() ) { throw new \Exception( "Can't resume the product attribute lookup data regeneration process: lookup table doesn't exist" ); } if ( $this->data_store->regeneration_is_in_progress() ) { throw new \Exception( "Can't resume the product attribute lookup data regeneration process: regeneration is already in progress" ); } $this->data_store->unset_regeneration_aborted_flag(); $this->data_store->set_regeneration_in_progress_flag(); $this->enqueue_regeneration_step_run(); } /** * Verify the validity of the nonce received when executing a tool from the Status - Tools page. * * @throws \Exception Missing or invalid nonce received. */ private function verify_tool_execution_nonce() { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput if ( ! isset( $_REQUEST['_wpnonce'] ) || false === wp_verify_nonce( $_REQUEST['_wpnonce'], 'debug_action' ) ) { throw new \Exception( 'Invalid nonce' ); } } /** * Get the name of the product attributes lookup table. * * @return string */ public function get_lookup_table_name() { return $this->lookup_table_name; } /** * Get the SQL statement that creates the product attributes lookup table, including the indices. * * @return string */ public function get_table_creation_sql() { global $wpdb; $collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : ''; return "CREATE TABLE {$this->lookup_table_name} ( product_id bigint(20) NOT NULL, product_or_parent_id bigint(20) NOT NULL, taxonomy varchar(32) NOT NULL, term_id bigint(20) NOT NULL, is_variation_attribute tinyint(1) NOT NULL, in_stock tinyint(1) NOT NULL, INDEX is_variation_attribute_term_id (is_variation_attribute, term_id), PRIMARY KEY ( `product_or_parent_id`, `term_id`, `product_id`, `taxonomy` ) ) $collate;"; } /** * Create the primary key for the table if it doesn't exist already. * It also deletes the product_or_parent_id_term_id index if it exists, since it's now redundant. * * @return void */ public function create_table_primary_index() { $database_util = wc_get_container()->get( DatabaseUtil::class ); $database_util->create_primary_key( $this->lookup_table_name, array( 'product_or_parent_id', 'term_id', 'product_id', 'taxonomy' ) ); $database_util->drop_table_index( $this->lookup_table_name, 'product_or_parent_id_term_id' ); if ( empty( $database_util->get_index_columns( $this->lookup_table_name ) ) ) { wc_get_logger()->error( "The creation of the primary key for the {$this->lookup_table_name} table failed" ); } if ( ! empty( $database_util->get_index_columns( $this->lookup_table_name, 'product_or_parent_id_term_id' ) ) ) { wc_get_logger()->error( "Dropping the product_or_parent_id_term_id index from the {$this->lookup_table_name} table failed" ); } } /** * Run additional setup needed after a WooCommerce install or update finishes. */ private function run_woocommerce_installed_callback() { // The table must exist at this point (created via dbDelta), but we check just in case. if ( ! $this->data_store->check_lookup_table_exists() ) { return; } // If a table regeneration is in progress, leave it alone. if ( $this->data_store->regeneration_is_in_progress() ) { return; } // If the lookup table has data, or if it's empty because there are no products yet, we're good. // Otherwise (lookup table is empty but products exist) we need to initiate a regeneration if one isn't already in progress. if ( $this->data_store->lookup_table_has_data() || ! $this->get_last_existing_product_id() ) { $this->finalize_regeneration( true ); } else { $this->initiate_regeneration(); } } }