abstract-wc-product-importer.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. <?php
  2. /**
  3. * Abstract Product importer
  4. *
  5. * @package WooCommerce/Import
  6. * @version 3.1.0
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. /**
  12. * Include dependencies.
  13. */
  14. if ( ! class_exists( 'WC_Importer_Interface', false ) ) {
  15. include_once WC_ABSPATH . 'includes/interfaces/class-wc-importer-interface.php';
  16. }
  17. /**
  18. * WC_Product_Importer Class.
  19. */
  20. abstract class WC_Product_Importer implements WC_Importer_Interface {
  21. /**
  22. * CSV file.
  23. *
  24. * @var string
  25. */
  26. protected $file = '';
  27. /**
  28. * The file position after the last read.
  29. *
  30. * @var int
  31. */
  32. protected $file_position = 0;
  33. /**
  34. * Importer parameters.
  35. *
  36. * @var array
  37. */
  38. protected $params = array();
  39. /**
  40. * Raw keys - CSV raw headers.
  41. *
  42. * @var array
  43. */
  44. protected $raw_keys = array();
  45. /**
  46. * Mapped keys - CSV headers.
  47. *
  48. * @var array
  49. */
  50. protected $mapped_keys = array();
  51. /**
  52. * Raw data.
  53. *
  54. * @var array
  55. */
  56. protected $raw_data = array();
  57. /**
  58. * Raw data.
  59. *
  60. * @var array
  61. */
  62. protected $file_positions = array();
  63. /**
  64. * Parsed data.
  65. *
  66. * @var array
  67. */
  68. protected $parsed_data = array();
  69. /**
  70. * Start time of current import.
  71. *
  72. * (default value: 0)
  73. *
  74. * @var int
  75. * @access protected
  76. */
  77. protected $start_time = 0;
  78. /**
  79. * Get file raw headers.
  80. *
  81. * @return array
  82. */
  83. public function get_raw_keys() {
  84. return $this->raw_keys;
  85. }
  86. /**
  87. * Get file mapped headers.
  88. *
  89. * @return array
  90. */
  91. public function get_mapped_keys() {
  92. return ! empty( $this->mapped_keys ) ? $this->mapped_keys : $this->raw_keys;
  93. }
  94. /**
  95. * Get raw data.
  96. *
  97. * @return array
  98. */
  99. public function get_raw_data() {
  100. return $this->raw_data;
  101. }
  102. /**
  103. * Get parsed data.
  104. *
  105. * @return array
  106. */
  107. public function get_parsed_data() {
  108. return apply_filters( 'woocommerce_product_importer_parsed_data', $this->parsed_data, $this->get_raw_data() );
  109. }
  110. /**
  111. * Get importer parameters.
  112. *
  113. * @return array
  114. */
  115. public function get_params() {
  116. return $this->params;
  117. }
  118. /**
  119. * Get file pointer position from the last read.
  120. *
  121. * @return int
  122. */
  123. public function get_file_position() {
  124. return $this->file_position;
  125. }
  126. /**
  127. * Get file pointer position as a percentage of file size.
  128. *
  129. * @return int
  130. */
  131. public function get_percent_complete() {
  132. $size = filesize( $this->file );
  133. if ( ! $size ) {
  134. return 0;
  135. }
  136. return absint( min( round( ( $this->file_position / $size ) * 100 ), 100 ) );
  137. }
  138. /**
  139. * Prepare a single product for create or update.
  140. *
  141. * @param array $data Item data.
  142. * @return WC_Product|WP_Error
  143. */
  144. protected function get_product_object( $data ) {
  145. $id = isset( $data['id'] ) ? absint( $data['id'] ) : 0;
  146. // Type is the most important part here because we need to be using the correct class and methods.
  147. if ( isset( $data['type'] ) ) {
  148. $types = array_keys( wc_get_product_types() );
  149. $types[] = 'variation';
  150. if ( ! in_array( $data['type'], $types, true ) ) {
  151. return new WP_Error( 'woocommerce_product_importer_invalid_type', __( 'Invalid product type.', 'woocommerce' ), array( 'status' => 401 ) );
  152. }
  153. $classname = WC_Product_Factory::get_classname_from_product_type( $data['type'] );
  154. if ( ! class_exists( $classname ) ) {
  155. $classname = 'WC_Product_Simple';
  156. }
  157. $product = new $classname( $id );
  158. } elseif ( ! empty( $data['id'] ) ) {
  159. $product = wc_get_product( $id );
  160. if ( ! $product ) {
  161. return new WP_Error(
  162. 'woocommerce_product_csv_importer_invalid_id',
  163. /* translators: %d: product ID */
  164. sprintf( __( 'Invalid product ID %d.', 'woocommerce' ), $id ),
  165. array(
  166. 'id' => $id,
  167. 'status' => 401,
  168. )
  169. );
  170. }
  171. } else {
  172. $product = new WC_Product_Simple( $id );
  173. }
  174. return apply_filters( 'woocommerce_product_import_get_product_object', $product, $data );
  175. }
  176. /**
  177. * Process a single item and save.
  178. *
  179. * @throws Exception If item cannot be processed.
  180. * @param array $data Raw CSV data.
  181. * @return array|WC_Error
  182. */
  183. protected function process_item( $data ) {
  184. try {
  185. do_action( 'woocommerce_product_import_before_process_item', $data );
  186. // Get product ID from SKU if created during the importation.
  187. if ( empty( $data['id'] ) && ! empty( $data['sku'] ) ) {
  188. $product_id = wc_get_product_id_by_sku( $data['sku'] );
  189. if ( $product_id ) {
  190. $data['id'] = $product_id;
  191. }
  192. }
  193. $object = $this->get_product_object( $data );
  194. $updating = false;
  195. if ( is_wp_error( $object ) ) {
  196. return $object;
  197. }
  198. if ( $object->get_id() && 'importing' !== $object->get_status() ) {
  199. $updating = true;
  200. }
  201. if ( 'external' === $object->get_type() ) {
  202. unset( $data['manage_stock'], $data['stock_status'], $data['backorders'] );
  203. }
  204. if ( 'importing' === $object->get_status() ) {
  205. $object->set_status( 'publish' );
  206. $object->set_slug( '' );
  207. }
  208. $result = $object->set_props( array_diff_key( $data, array_flip( array( 'meta_data', 'raw_image_id', 'raw_gallery_image_ids', 'raw_attributes' ) ) ) );
  209. if ( is_wp_error( $result ) ) {
  210. throw new Exception( $result->get_error_message() );
  211. }
  212. if ( 'variation' === $object->get_type() ) {
  213. $this->set_variation_data( $object, $data );
  214. } else {
  215. $this->set_product_data( $object, $data );
  216. }
  217. $this->set_image_data( $object, $data );
  218. $this->set_meta_data( $object, $data );
  219. $object = apply_filters( 'woocommerce_product_import_pre_insert_product_object', $object, $data );
  220. $object->save();
  221. do_action( 'woocommerce_product_import_inserted_product_object', $object, $data );
  222. return array(
  223. 'id' => $object->get_id(),
  224. 'updated' => $updating,
  225. );
  226. } catch ( Exception $e ) {
  227. return new WP_Error( 'woocommerce_product_importer_error', $e->getMessage(), array( 'status' => $e->getCode() ) );
  228. }
  229. }
  230. /**
  231. * Convert raw image URLs to IDs and set.
  232. *
  233. * @param WC_Product $product Product instance.
  234. * @param array $data Item data.
  235. */
  236. protected function set_image_data( &$product, $data ) {
  237. // Image URLs need converting to IDs before inserting.
  238. if ( isset( $data['raw_image_id'] ) ) {
  239. $product->set_image_id( $this->get_attachment_id_from_url( $data['raw_image_id'], $product->get_id() ) );
  240. }
  241. // Gallery image URLs need converting to IDs before inserting.
  242. if ( isset( $data['raw_gallery_image_ids'] ) ) {
  243. $gallery_image_ids = array();
  244. foreach ( $data['raw_gallery_image_ids'] as $image_id ) {
  245. $gallery_image_ids[] = $this->get_attachment_id_from_url( $image_id, $product->get_id() );
  246. }
  247. $product->set_gallery_image_ids( $gallery_image_ids );
  248. }
  249. }
  250. /**
  251. * Append meta data.
  252. *
  253. * @param WC_Product $product Product instance.
  254. * @param array $data Item data.
  255. */
  256. protected function set_meta_data( &$product, $data ) {
  257. if ( isset( $data['meta_data'] ) ) {
  258. foreach ( $data['meta_data'] as $meta ) {
  259. $product->update_meta_data( $meta['key'], $meta['value'] );
  260. }
  261. }
  262. }
  263. /**
  264. * Set product data.
  265. *
  266. * @param WC_Product $product Product instance.
  267. * @param array $data Item data.
  268. * @throws Exception If data cannot be set.
  269. */
  270. protected function set_product_data( &$product, $data ) {
  271. if ( isset( $data['raw_attributes'] ) ) {
  272. $attributes = array();
  273. $default_attributes = array();
  274. $existing_attributes = $product->get_attributes();
  275. foreach ( $data['raw_attributes'] as $position => $attribute ) {
  276. $attribute_id = 0;
  277. // Get ID if is a global attribute.
  278. if ( ! empty( $attribute['taxonomy'] ) ) {
  279. $attribute_id = $this->get_attribute_taxonomy_id( $attribute['name'] );
  280. }
  281. // Set attribute visibility.
  282. if ( isset( $attribute['visible'] ) ) {
  283. $is_visible = $attribute['visible'];
  284. } else {
  285. $is_visible = 1;
  286. }
  287. // Get name.
  288. $attribute_name = $attribute_id ? wc_attribute_taxonomy_name_by_id( $attribute_id ) : $attribute['name'];
  289. // Set if is a variation attribute based on existing attributes if possible so updates via CSV do not change this.
  290. $is_variation = 0;
  291. if ( $existing_attributes ) {
  292. foreach ( $existing_attributes as $existing_attribute ) {
  293. if ( $existing_attribute->get_name() === $attribute_name ) {
  294. $is_variation = $existing_attribute->get_variation();
  295. break;
  296. }
  297. }
  298. }
  299. if ( $attribute_id ) {
  300. if ( isset( $attribute['value'] ) ) {
  301. $options = array_map( 'wc_sanitize_term_text_based', $attribute['value'] );
  302. $options = array_filter( $options, 'strlen' );
  303. } else {
  304. $options = array();
  305. }
  306. // Check for default attributes and set "is_variation".
  307. if ( ! empty( $attribute['default'] ) && in_array( $attribute['default'], $options, true ) ) {
  308. $default_term = get_term_by( 'name', $attribute['default'], $attribute_name );
  309. if ( $default_term && ! is_wp_error( $default_term ) ) {
  310. $default = $default_term->slug;
  311. } else {
  312. $default = sanitize_title( $attribute['default'] );
  313. }
  314. $default_attributes[ $attribute_name ] = $default;
  315. $is_variation = 1;
  316. }
  317. if ( ! empty( $options ) ) {
  318. $attribute_object = new WC_Product_Attribute();
  319. $attribute_object->set_id( $attribute_id );
  320. $attribute_object->set_name( $attribute_name );
  321. $attribute_object->set_options( $options );
  322. $attribute_object->set_position( $position );
  323. $attribute_object->set_visible( $is_visible );
  324. $attribute_object->set_variation( $is_variation );
  325. $attributes[] = $attribute_object;
  326. }
  327. } elseif ( isset( $attribute['value'] ) ) {
  328. // Check for default attributes and set "is_variation".
  329. if ( ! empty( $attribute['default'] ) && in_array( $attribute['default'], $attribute['value'], true ) ) {
  330. $default_attributes[ sanitize_title( $attribute['name'] ) ] = $attribute['default'];
  331. $is_variation = 1;
  332. }
  333. $attribute_object = new WC_Product_Attribute();
  334. $attribute_object->set_name( $attribute['name'] );
  335. $attribute_object->set_options( $attribute['value'] );
  336. $attribute_object->set_position( $position );
  337. $attribute_object->set_visible( $is_visible );
  338. $attribute_object->set_variation( $is_variation );
  339. $attributes[] = $attribute_object;
  340. }
  341. }
  342. $product->set_attributes( $attributes );
  343. // Set variable default attributes.
  344. if ( $product->is_type( 'variable' ) ) {
  345. $product->set_default_attributes( $default_attributes );
  346. }
  347. }
  348. }
  349. /**
  350. * Set variation data.
  351. *
  352. * @param WC_Product $variation Product instance.
  353. * @param array $data Item data.
  354. * @return WC_Product|WP_Error
  355. * @throws Exception If data cannot be set.
  356. */
  357. protected function set_variation_data( &$variation, $data ) {
  358. $parent = false;
  359. // Check if parent exist.
  360. if ( isset( $data['parent_id'] ) ) {
  361. $parent = wc_get_product( $data['parent_id'] );
  362. if ( $parent ) {
  363. $variation->set_parent_id( $parent->get_id() );
  364. }
  365. }
  366. // Stop if parent does not exists.
  367. if ( ! $parent ) {
  368. return new WP_Error( 'woocommerce_product_importer_missing_variation_parent_id', __( 'Variation cannot be imported: Missing parent ID or parent does not exist yet.', 'woocommerce' ), array( 'status' => 401 ) );
  369. }
  370. if ( isset( $data['raw_attributes'] ) ) {
  371. $attributes = array();
  372. $parent_attributes = $this->get_variation_parent_attributes( $data['raw_attributes'], $parent );
  373. foreach ( $data['raw_attributes'] as $attribute ) {
  374. $attribute_id = 0;
  375. // Get ID if is a global attribute.
  376. if ( ! empty( $attribute['taxonomy'] ) ) {
  377. $attribute_id = $this->get_attribute_taxonomy_id( $attribute['name'] );
  378. }
  379. if ( $attribute_id ) {
  380. $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id );
  381. } else {
  382. $attribute_name = sanitize_title( $attribute['name'] );
  383. }
  384. if ( ! isset( $parent_attributes[ $attribute_name ] ) || ! $parent_attributes[ $attribute_name ]->get_variation() ) {
  385. continue;
  386. }
  387. $attribute_key = sanitize_title( $parent_attributes[ $attribute_name ]->get_name() );
  388. $attribute_value = isset( $attribute['value'] ) ? current( $attribute['value'] ) : '';
  389. if ( $parent_attributes[ $attribute_name ]->is_taxonomy() ) {
  390. // If dealing with a taxonomy, we need to get the slug from the name posted to the API.
  391. $term = get_term_by( 'name', $attribute_value, $attribute_name );
  392. if ( $term && ! is_wp_error( $term ) ) {
  393. $attribute_value = $term->slug;
  394. } else {
  395. $attribute_value = sanitize_title( $attribute_value );
  396. }
  397. }
  398. $attributes[ $attribute_key ] = $attribute_value;
  399. }
  400. $variation->set_attributes( $attributes );
  401. }
  402. }
  403. /**
  404. * Get variation parent attributes and set "is_variation".
  405. *
  406. * @param array $attributes Attributes list.
  407. * @param WC_Product $parent Parent product data.
  408. * @return array
  409. */
  410. protected function get_variation_parent_attributes( $attributes, $parent ) {
  411. $parent_attributes = $parent->get_attributes();
  412. $require_save = false;
  413. foreach ( $attributes as $attribute ) {
  414. $attribute_id = 0;
  415. // Get ID if is a global attribute.
  416. if ( ! empty( $attribute['taxonomy'] ) ) {
  417. $attribute_id = $this->get_attribute_taxonomy_id( $attribute['name'] );
  418. }
  419. if ( $attribute_id ) {
  420. $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id );
  421. } else {
  422. $attribute_name = sanitize_title( $attribute['name'] );
  423. }
  424. // Check if attribute handle variations.
  425. if ( isset( $parent_attributes[ $attribute_name ] ) && ! $parent_attributes[ $attribute_name ]->get_variation() ) {
  426. // Re-create the attribute to CRUD save and generate again.
  427. $parent_attributes[ $attribute_name ] = clone $parent_attributes[ $attribute_name ];
  428. $parent_attributes[ $attribute_name ]->set_variation( 1 );
  429. $require_save = true;
  430. }
  431. }
  432. // Save variation attributes.
  433. if ( $require_save ) {
  434. $parent->set_attributes( array_values( $parent_attributes ) );
  435. $parent->save();
  436. }
  437. return $parent_attributes;
  438. }
  439. /**
  440. * Get attachment ID.
  441. *
  442. * @param string $url Attachment URL.
  443. * @param int $product_id Product ID.
  444. * @return int
  445. * @throws Exception If attachment cannot be loaded.
  446. */
  447. public function get_attachment_id_from_url( $url, $product_id ) {
  448. if ( empty( $url ) ) {
  449. return 0;
  450. }
  451. $id = 0;
  452. $upload_dir = wp_upload_dir( null, false );
  453. $base_url = $upload_dir['baseurl'] . '/';
  454. // Check first if attachment is inside the WordPress uploads directory, or we're given a filename only.
  455. if ( false !== strpos( $url, $base_url ) || false === strpos( $url, '://' ) ) {
  456. // Search for yyyy/mm/slug.extension or slug.extension - remove the base URL.
  457. $file = str_replace( $base_url, '', $url );
  458. $args = array(
  459. 'post_type' => 'attachment',
  460. 'post_status' => 'any',
  461. 'fields' => 'ids',
  462. 'meta_query' => array( // @codingStandardsIgnoreLine.
  463. 'relation' => 'OR',
  464. array(
  465. 'key' => '_wp_attached_file',
  466. 'value' => '^' . $file,
  467. 'compare' => 'REGEXP',
  468. ),
  469. array(
  470. 'key' => '_wp_attached_file',
  471. 'value' => '/' . $file,
  472. 'compare' => 'LIKE',
  473. ),
  474. array(
  475. 'key' => '_wc_attachment_source',
  476. 'value' => '/' . $file,
  477. 'compare' => 'LIKE',
  478. ),
  479. ),
  480. );
  481. } else {
  482. // This is an external URL, so compare to source.
  483. $args = array(
  484. 'post_type' => 'attachment',
  485. 'post_status' => 'any',
  486. 'fields' => 'ids',
  487. 'meta_query' => array( // @codingStandardsIgnoreLine.
  488. array(
  489. 'value' => $url,
  490. 'key' => '_wc_attachment_source',
  491. ),
  492. ),
  493. );
  494. }
  495. $ids = get_posts( $args ); // @codingStandardsIgnoreLine.
  496. if ( $ids ) {
  497. $id = current( $ids );
  498. }
  499. // Upload if attachment does not exists.
  500. if ( ! $id && stristr( $url, '://' ) ) {
  501. $upload = wc_rest_upload_image_from_url( $url );
  502. if ( is_wp_error( $upload ) ) {
  503. throw new Exception( $upload->get_error_message(), 400 );
  504. }
  505. $id = wc_rest_set_uploaded_image_as_attachment( $upload, $product_id );
  506. if ( ! wp_attachment_is_image( $id ) ) {
  507. /* translators: %s: image URL */
  508. throw new Exception( sprintf( __( 'Not able to attach "%s".', 'woocommerce' ), $url ), 400 );
  509. }
  510. // Save attachment source for future reference.
  511. update_post_meta( $id, '_wc_attachment_source', $url );
  512. }
  513. if ( ! $id ) {
  514. /* translators: %s: image URL */
  515. throw new Exception( sprintf( __( 'Unable to use image "%s".', 'woocommerce' ), $url ), 400 );
  516. }
  517. return $id;
  518. }
  519. /**
  520. * Get attribute taxonomy ID from the imported data.
  521. * If does not exists register a new attribute.
  522. *
  523. * @param string $raw_name Attribute name.
  524. * @return int
  525. * @throws Exception If taxonomy cannot be loaded.
  526. */
  527. public function get_attribute_taxonomy_id( $raw_name ) {
  528. global $wpdb, $wc_product_attributes;
  529. // These are exported as labels, so convert the label to a name if possible first.
  530. $attribute_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' );
  531. $attribute_name = array_search( $raw_name, $attribute_labels, true );
  532. if ( ! $attribute_name ) {
  533. $attribute_name = wc_sanitize_taxonomy_name( $raw_name );
  534. }
  535. $attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name );
  536. // Get the ID from the name.
  537. if ( $attribute_id ) {
  538. return $attribute_id;
  539. }
  540. // If the attribute does not exist, create it.
  541. $attribute_id = wc_create_attribute( array(
  542. 'name' => $raw_name,
  543. 'slug' => $attribute_name,
  544. 'type' => 'select',
  545. 'order_by' => 'menu_order',
  546. 'has_archives' => false,
  547. ) );
  548. if ( is_wp_error( $attribute_id ) ) {
  549. throw new Exception( $attribute_id->get_error_message(), 400 );
  550. }
  551. // Register as taxonomy while importing.
  552. $taxonomy_name = wc_attribute_taxonomy_name( $attribute_name );
  553. register_taxonomy(
  554. $taxonomy_name,
  555. apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, array( 'product' ) ),
  556. apply_filters( 'woocommerce_taxonomy_args_' . $taxonomy_name, array(
  557. 'labels' => array(
  558. 'name' => $raw_name,
  559. ),
  560. 'hierarchical' => true,
  561. 'show_ui' => false,
  562. 'query_var' => true,
  563. 'rewrite' => false,
  564. ) )
  565. );
  566. // Set product attributes global.
  567. $wc_product_attributes = array();
  568. foreach ( wc_get_attribute_taxonomies() as $taxonomy ) {
  569. $wc_product_attributes[ wc_attribute_taxonomy_name( $taxonomy->attribute_name ) ] = $taxonomy;
  570. }
  571. return $attribute_id;
  572. }
  573. /**
  574. * Memory exceeded
  575. *
  576. * Ensures the batch process never exceeds 90%
  577. * of the maximum WordPress memory.
  578. *
  579. * @return bool
  580. */
  581. protected function memory_exceeded() {
  582. $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory
  583. $current_memory = memory_get_usage( true );
  584. $return = false;
  585. if ( $current_memory >= $memory_limit ) {
  586. $return = true;
  587. }
  588. return apply_filters( 'woocommerce_product_importer_memory_exceeded', $return );
  589. }
  590. /**
  591. * Get memory limit
  592. *
  593. * @return int
  594. */
  595. protected function get_memory_limit() {
  596. if ( function_exists( 'ini_get' ) ) {
  597. $memory_limit = ini_get( 'memory_limit' );
  598. } else {
  599. // Sensible default.
  600. $memory_limit = '128M';
  601. }
  602. if ( ! $memory_limit || -1 === intval( $memory_limit ) ) {
  603. // Unlimited, set to 32GB.
  604. $memory_limit = '32000M';
  605. }
  606. return intval( $memory_limit ) * 1024 * 1024;
  607. }
  608. /**
  609. * Time exceeded.
  610. *
  611. * Ensures the batch never exceeds a sensible time limit.
  612. * A timeout limit of 30s is common on shared hosting.
  613. *
  614. * @return bool
  615. */
  616. protected function time_exceeded() {
  617. $finish = $this->start_time + apply_filters( 'woocommerce_product_importer_default_time_limit', 20 ); // 20 seconds
  618. $return = false;
  619. if ( time() >= $finish ) {
  620. $return = true;
  621. }
  622. return apply_filters( 'woocommerce_product_importer_time_exceeded', $return );
  623. }
  624. /**
  625. * Explode CSV cell values using commas by default, and handling escaped
  626. * separators.
  627. *
  628. * @since 3.2.0
  629. * @param string $value Value to explode.
  630. * @return array
  631. */
  632. protected function explode_values( $value ) {
  633. $value = str_replace( '\\,', '::separator::', $value );
  634. $values = explode( ',', $value );
  635. $values = array_map( array( $this, 'explode_values_formatter' ), $values );
  636. return $values;
  637. }
  638. /**
  639. * Remove formatting and trim each value.
  640. *
  641. * @since 3.2.0
  642. * @param string $value Value to format.
  643. * @return string
  644. */
  645. protected function explode_values_formatter( $value ) {
  646. return trim( str_replace( '::separator::', ',', $value ) );
  647. }
  648. /**
  649. * The exporter prepends a ' to fields that start with a - which causes
  650. * issues with negative numbers. This removes the ' if the input is still a valid
  651. * number after removal.
  652. *
  653. * @since 3.3.0
  654. * @param string $value A numeric string that may or may not have ' prepended.
  655. * @return string
  656. */
  657. protected function unescape_negative_number( $value ) {
  658. if ( 0 === strpos( $value, "'-" ) ) {
  659. $unescaped = trim( $value, "'" );
  660. if ( is_numeric( $unescaped ) ) {
  661. return $unescaped;
  662. }
  663. }
  664. return $value;
  665. }
  666. }