WC_REST_Products_Catalog_Controller::generate_catalog_file
Generate catalog file and save it to the specified file path.
Method of the class: WC_REST_Products_Catalog_Controller{}
No Hooks.
Returns
true|WP_Error. True on success, WP_Error on failure.
Usage
// private - for code of main (parent) class only $result = $this->generate_catalog_file( $file_info );
- $file_info(array) (required)
- File information with 'filepath', 'url', and 'directory' keys.
WC_REST_Products_Catalog_Controller::generate_catalog_file() WC REST Products Catalog Controller::generate catalog file code WC 10.4.3
private function generate_catalog_file( $file_info ) {
// Ensure directory exists and is not indexable.
try {
FilesystemUtil::mkdir_p_not_indexable( $file_info['directory'], true );
} catch ( \Exception $exception ) {
return new WP_Error( 'catalog_dir_creation_failed', $exception->getMessage(), array( 'status' => 500 ) );
}
// Generate empty catalog file.
$catalog_data = array();
// Write to file.
$json = wp_json_encode( $catalog_data );
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
$result = file_put_contents( $file_info['filepath'], $json, LOCK_EX );
if ( false === $result ) {
return new WP_Error( 'catalog_generation_failed', __( 'Failed to generate catalog file.', 'woocommerce' ), array( 'status' => 500 ) );
}
return true;
}