Automattic\WooCommerce\Blueprint

ExportSchema::exportpublicWC 1.0

Export the schema steps.

Method of the class: ExportSchema{}

Returns

Array|WP_Error. The exported schema array or a WP_Error if the export fails.

Usage

$ExportSchema = new ExportSchema();
$ExportSchema->export( $steps );
$steps(string[])
Array of step names to export, optional.
Default: array()

ExportSchema::export() code WC 9.9.5

public function export( $steps = array() ) {
	$loading_page_path = $this->wp_apply_filters( 'wooblueprint_export_landingpage', '/' );
	/**
	 * Validate that the landing page path is a valid relative local URL path.
	 *
	 * Accepts:
	 * - /
	 * - /path/to/page
	 *
	 * Rejects:
	 * - http://example.com/path/to/page
	 * - invalid-path
	 */
	if ( ! preg_match( '#^/$|^/[^/].*#', $loading_page_path ) ) {
		return new WP_Error( 'wooblueprint_invalid_landing_page_path', 'Invalid loading page path.' );
	}

	$schema = array(
		'landingPage' => $loading_page_path,
		'steps'       => array(),
	);

	$built_in_exporters = ( new BuiltInExporters() )->get_all();

	/**
	 * Filters the step exporters.
	 *
	 * Allows adding/removing custom step exporters.
	 *
	 * @param StepExporter[] $exporters Array of step exporters.
	 *
	 * @since 0.0.1
	 */
	$exporters = $this->wp_apply_filters( 'wooblueprint_exporters', array_merge( $this->exporters, $built_in_exporters ) );
	// Validate that the exporters are instances of StepExporter.
	$exporters = array_filter(
		$exporters,
		function ( $exporter ) {
			return $exporter instanceof StepExporter;
		}
	);

	// Filter out any exporters that are not in the list of steps to export.
	if ( count( $steps ) ) {
		foreach ( $exporters as $key => $exporter ) {
			$name  = $exporter->get_step_name();
			$alias = $exporter instanceof HasAlias ? $exporter->get_alias() : $name;
			if ( ! in_array( $name, $steps, true ) && ! in_array( $alias, $steps, true ) ) {
				unset( $exporters[ $key ] );
			}
		}
	}

	// Make sure the user has the required capabilities to export the steps.
	foreach ( $exporters as $exporter ) {
		if ( ! $exporter->check_step_capabilities() ) {
			return new WP_Error( 'wooblueprint_insufficient_permissions', 'Insufficient permissions to export for step: ' . $exporter->get_step_name() );
		}
	}

	$logger = new Logger();
	$logger->start_export( $exporters );

	foreach ( $exporters as $exporter ) {
		try {
			$this->publish( 'onBeforeExport', $exporter );
			$step = $exporter->export();
			$this->add_result_to_schema( $schema, $step );

		} catch ( \Throwable $e ) {
			$step_name = $exporter instanceof HasAlias ? $exporter->get_alias() : $exporter->get_step_name();
			$logger->export_step_failed( $step_name, $e );
			return new WP_Error( 'wooblueprint_export_step_failed', 'Export step failed: ' . $e->getMessage() );
		}
	}

	$logger->complete_export( $exporters );

	return $schema;
}