Cli
1 year ago
Exporters
1 year ago
Importers
11 months ago
ResourceStorages
1 year ago
ResultFormatters
1 year ago
Schemas
1 year ago
Steps
1 year ago
docs
1 year ago
BuiltInExporters.php
1 year ago
BuiltInStepProcessors.php
1 year ago
ClassExtractor.php
1 year ago
Cli.php
1 year ago
ExportSchema.php
1 year ago
ImportSchema.php
1 year ago
ImportStep.php
1 year ago
Logger.php
1 year ago
ResourceStorages.php
1 year ago
StepProcessor.php
1 year ago
StepProcessorResult.php
1 year ago
UsePluginHelpers.php
1 year ago
UsePubSub.php
1 year ago
UseWPFunctions.php
1 year ago
Util.php
1 year ago
ExportSchema.php
160 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blueprint\Exporters\StepExporter; |
| 6 | use Automattic\WooCommerce\Blueprint\Exporters\HasAlias; |
| 7 | use Automattic\WooCommerce\Blueprint\Logger; |
| 8 | use Automattic\WooCommerce\Blueprint\Steps\Step; |
| 9 | use WP_Error; |
| 10 | |
| 11 | /** |
| 12 | * Class ExportSchema |
| 13 | * |
| 14 | * Handles the export schema functionality for WooCommerce. |
| 15 | * |
| 16 | * @package Automattic\WooCommerce\Blueprint |
| 17 | */ |
| 18 | class ExportSchema { |
| 19 | use UseWPFunctions; |
| 20 | use UsePubSub; |
| 21 | |
| 22 | /** |
| 23 | * Step exporters. |
| 24 | * |
| 25 | * @var StepExporter[] Array of step exporters. |
| 26 | */ |
| 27 | protected array $exporters = array(); |
| 28 | |
| 29 | /** |
| 30 | * ExportSchema constructor. |
| 31 | * |
| 32 | * @param StepExporter[] $exporters Array of step exporters. |
| 33 | */ |
| 34 | public function __construct( $exporters = array() ) { |
| 35 | $this->exporters = $exporters; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Export the schema steps. |
| 40 | * |
| 41 | * @param string[] $steps Array of step names to export, optional. |
| 42 | * |
| 43 | * @return array|WP_Error The exported schema array or a WP_Error if the export fails. |
| 44 | */ |
| 45 | public function export( $steps = array() ) { |
| 46 | $loading_page_path = $this->wp_apply_filters( 'wooblueprint_export_landingpage', '/' ); |
| 47 | /** |
| 48 | * Validate that the landing page path is a valid relative local URL path. |
| 49 | * |
| 50 | * Accepts: |
| 51 | * - / |
| 52 | * - /path/to/page |
| 53 | * |
| 54 | * Rejects: |
| 55 | * - http://example.com/path/to/page |
| 56 | * - invalid-path |
| 57 | */ |
| 58 | if ( ! preg_match( '#^/$|^/[^/].*#', $loading_page_path ) ) { |
| 59 | return new WP_Error( 'wooblueprint_invalid_landing_page_path', 'Invalid loading page path.' ); |
| 60 | } |
| 61 | |
| 62 | $schema = array( |
| 63 | 'landingPage' => $loading_page_path, |
| 64 | 'steps' => array(), |
| 65 | ); |
| 66 | |
| 67 | $built_in_exporters = ( new BuiltInExporters() )->get_all(); |
| 68 | |
| 69 | /** |
| 70 | * Filters the step exporters. |
| 71 | * |
| 72 | * Allows adding/removing custom step exporters. |
| 73 | * |
| 74 | * @param StepExporter[] $exporters Array of step exporters. |
| 75 | * |
| 76 | * @since 0.0.1 |
| 77 | */ |
| 78 | $exporters = $this->wp_apply_filters( 'wooblueprint_exporters', array_merge( $this->exporters, $built_in_exporters ) ); |
| 79 | // Validate that the exporters are instances of StepExporter. |
| 80 | $exporters = array_filter( |
| 81 | $exporters, |
| 82 | function ( $exporter ) { |
| 83 | return $exporter instanceof StepExporter; |
| 84 | } |
| 85 | ); |
| 86 | |
| 87 | // Filter out any exporters that are not in the list of steps to export. |
| 88 | if ( count( $steps ) ) { |
| 89 | foreach ( $exporters as $key => $exporter ) { |
| 90 | $name = $exporter->get_step_name(); |
| 91 | $alias = $exporter instanceof HasAlias ? $exporter->get_alias() : $name; |
| 92 | if ( ! in_array( $name, $steps, true ) && ! in_array( $alias, $steps, true ) ) { |
| 93 | unset( $exporters[ $key ] ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Make sure the user has the required capabilities to export the steps. |
| 99 | foreach ( $exporters as $exporter ) { |
| 100 | if ( ! $exporter->check_step_capabilities() ) { |
| 101 | return new WP_Error( 'wooblueprint_insufficient_permissions', 'Insufficient permissions to export for step: ' . $exporter->get_step_name() ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | $logger = new Logger(); |
| 106 | $logger->start_export( $exporters ); |
| 107 | |
| 108 | foreach ( $exporters as $exporter ) { |
| 109 | try { |
| 110 | $this->publish( 'onBeforeExport', $exporter ); |
| 111 | $step = $exporter->export(); |
| 112 | $this->add_result_to_schema( $schema, $step ); |
| 113 | |
| 114 | } catch ( \Throwable $e ) { |
| 115 | $step_name = $exporter instanceof HasAlias ? $exporter->get_alias() : $exporter->get_step_name(); |
| 116 | $logger->export_step_failed( $step_name, $e ); |
| 117 | return new WP_Error( 'wooblueprint_export_step_failed', 'Export step failed: ' . $e->getMessage() ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | $logger->complete_export( $exporters ); |
| 122 | |
| 123 | return $schema; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Subscribe to the onBeforeExport event. |
| 128 | * |
| 129 | * @param string $step_name The step name to subscribe to. |
| 130 | * @param callable $callback The callback to execute. |
| 131 | */ |
| 132 | public function on_before_export( $step_name, $callback ) { |
| 133 | $this->subscribe( |
| 134 | 'onBeforeExport', |
| 135 | function ( $exporter ) use ( $step_name, $callback ) { |
| 136 | if ( $step_name === $exporter->get_step_name() ) { |
| 137 | $callback( $exporter ); |
| 138 | } |
| 139 | } |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Add export result to the schema array. |
| 145 | * |
| 146 | * @param array $schema Schema array to add steps to. |
| 147 | * @param array|Step $step Step or array of steps to add. |
| 148 | */ |
| 149 | private function add_result_to_schema( array &$schema, $step ): void { |
| 150 | if ( is_array( $step ) ) { |
| 151 | foreach ( $step as $_step ) { |
| 152 | $schema['steps'][] = $_step->get_json_array(); |
| 153 | } |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | $schema['steps'][] = $step->get_json_array(); |
| 158 | } |
| 159 | } |
| 160 |