ProductTemplates
4 weeks ago
BlockRegistry.php
4 weeks ago
BlockTemplateUtils.php
4 weeks ago
Init.php
4 weeks ago
ProductFormsController.php
4 weeks ago
ProductTemplate.php
4 weeks ago
RedirectionController.php
4 weeks ago
Tracks.php
4 weeks ago
ProductFormsController.php
155 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Product Forms Controller |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor; |
| 7 | |
| 8 | /** |
| 9 | * Handle retrieval of product forms. |
| 10 | * |
| 11 | * @deprecated 10.9.0 Product editor extension APIs will be removed in WooCommerce 11.0. |
| 12 | */ |
| 13 | class ProductFormsController { |
| 14 | |
| 15 | /** |
| 16 | * Version that product editor APIs were deprecated in. |
| 17 | */ |
| 18 | const DEPRECATED_SINCE = '10.9.0'; |
| 19 | |
| 20 | /** |
| 21 | * Product form templates. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | private $product_form_templates = array( |
| 26 | 'simple', |
| 27 | ); |
| 28 | |
| 29 | /** |
| 30 | * Set up the product forms controller. |
| 31 | */ |
| 32 | public function init() { // phpcs:ignore WooCommerce.Functions.InternalInjectionMethod.MissingFinal, WooCommerce.Functions.InternalInjectionMethod.MissingInternalTag -- Not an injection. |
| 33 | add_action( 'upgrader_process_complete', array( $this, 'migrate_templates_when_plugin_updated' ), 10, 2 ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Migrate form templates after WooCommerce plugin update. |
| 38 | * |
| 39 | * @param \WP_Upgrader $upgrader The WP_Upgrader instance. |
| 40 | * @param array $hook_extra Extra arguments passed to hooked filters. |
| 41 | * @return void |
| 42 | */ |
| 43 | public function migrate_templates_when_plugin_updated( \WP_Upgrader $upgrader, array $hook_extra ) { |
| 44 | // If it is not a plugin hook type, bail early. |
| 45 | $type = isset( $hook_extra['type'] ) ? $hook_extra['type'] : ''; |
| 46 | if ( 'plugin' !== $type ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // If it is not the WooCommerce plugin, bail early. |
| 51 | $plugins = isset( $hook_extra['plugins'] ) ? $hook_extra['plugins'] : array(); |
| 52 | if ( |
| 53 | ! in_array( 'woocommerce/woocommerce.php', $plugins, true ) |
| 54 | ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // If the action is not install or update, bail early. |
| 59 | $action = isset( $hook_extra['action'] ) ? $hook_extra['action'] : ''; |
| 60 | if ( 'install' !== $action && 'update' !== $action ) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // Trigger the migration process. |
| 65 | $this->migrate_product_form_posts( $action ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Create or update a product_form post for each product form template. |
| 70 | * If the post already exists, it will be updated. |
| 71 | * If the post does not exist, it will be created even if the action is `update`. |
| 72 | * |
| 73 | * @param string $action - The action to perform. `insert` | `update`. |
| 74 | * @return void |
| 75 | */ |
| 76 | public function migrate_product_form_posts( $action ) { |
| 77 | if ( has_filter( 'woocommerce_product_form_templates' ) ) { |
| 78 | wc_deprecated_hook( |
| 79 | 'woocommerce_product_form_templates', |
| 80 | $this::DEPRECATED_SINCE, |
| 81 | null, |
| 82 | 'This product editor extension filter will be removed in WooCommerce 11.0.' |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Allow extend the list of templates that should be auto-generated. |
| 88 | * |
| 89 | * @since 9.1.0 |
| 90 | * @deprecated 10.9.0 Product editor extension APIs will be removed in WooCommerce 11.0. |
| 91 | * @param array $templates List of templates to auto-generate. |
| 92 | */ |
| 93 | $templates = apply_filters( |
| 94 | 'woocommerce_product_form_templates', |
| 95 | $this->product_form_templates |
| 96 | ); |
| 97 | |
| 98 | foreach ( $templates as $slug ) { |
| 99 | $file_path = BlockTemplateUtils::get_block_template_path( $slug ); |
| 100 | |
| 101 | if ( ! $file_path ) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | $file_data = BlockTemplateUtils::get_template_file_data( $file_path ); |
| 106 | |
| 107 | $posts = get_posts( |
| 108 | array( |
| 109 | 'name' => $slug, |
| 110 | 'post_type' => 'product_form', |
| 111 | 'post_status' => 'any', |
| 112 | 'posts_per_page' => 1, |
| 113 | ) |
| 114 | ); |
| 115 | |
| 116 | /* |
| 117 | * Update the the CPT post if it already exists, |
| 118 | * and the action is `update`. |
| 119 | */ |
| 120 | if ( 'update' === $action ) { |
| 121 | $post = $posts[0] ?? null; |
| 122 | |
| 123 | if ( ! empty( $post ) ) { |
| 124 | wp_update_post( |
| 125 | array( |
| 126 | 'ID' => $post->ID, |
| 127 | 'post_title' => $file_data['title'], |
| 128 | 'post_content' => BlockTemplateUtils::get_template_content( $file_path ), |
| 129 | 'post_excerpt' => $file_data['description'], |
| 130 | ) |
| 131 | ); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Skip the post creation if the post already exists. |
| 137 | */ |
| 138 | if ( ! empty( $posts ) ) { |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | $post = wp_insert_post( |
| 143 | array( |
| 144 | 'post_title' => $file_data['title'], |
| 145 | 'post_name' => $slug, |
| 146 | 'post_status' => 'publish', |
| 147 | 'post_type' => 'product_form', |
| 148 | 'post_content' => BlockTemplateUtils::get_template_content( $file_path ), |
| 149 | 'post_excerpt' => $file_data['description'], |
| 150 | ) |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 |