· 8 years ago · Dec 07, 2016, 08:26 PM
1<?php
2 /**
3 * Plugin Name: Linear Netsuite Import
4 * Description: This plugin is used to import from Netsuite into Wordpress
5 * Version: 1.0.0
6 * Author:
7 * Author URI:
8 *
9 */
10
11 $is_sandbox = false;
12 if($is_sandbox) {
13 $host_to_use = "rest.sandbox.netsuite.com";
14 $script_id = '1679';
15 $acct = "3468268_SB4";
16
17 define( "NETSUITE_URL", 'https://' . $host_to_use . '/app/site/hosting/restlet.nl' );
18 define( "NETSUITE_SCRIPT_ID", $script_id );
19 define( "NETSUITE_DEPLOY_ID", '1' );
20 define( "NETSUITE_ACCOUNT", $acct );
21 define( "NETSUITE_CONSUMER_KEY", '0367d98cbe2f859880164eb2b500e116a9af8692d6dba57b4144e78c04db0f18' );
22 define( "NETSUITE_CONSUMER_SECRET", '5d4dd3a951eff187f793339fb7af291bf633bdea5253acd3a0715d6915ede189' );
23 define( "NETSUITE_TOKEN_ID", '492f18a8006702c11bba47d285977090382b5f19d13b18dda27c2aec4c668008' );
24 define( "NETSUITE_TOKEN_SECRET", 'ac3b6f30dd680233b440dfa9fdd802a42e4492e6113e617be1c4e264066f95c4' );
25
26 https://rest.na2.netsuite.com/app/site/hosting/restlet.nl?script=1769&deploy=1
27 }else{
28 $host_to_use = "rest.na2.netsuite.com";
29 $script_id = '1769';
30 $acct = "3468268";
31
32 define( "NETSUITE_URL", 'https://' . $host_to_use . '/app/site/hosting/restlet.nl' );
33 define( "NETSUITE_SCRIPT_ID", $script_id );
34 define( "NETSUITE_DEPLOY_ID", '1' );
35 define( "NETSUITE_ACCOUNT", $acct );
36 define( "NETSUITE_CONSUMER_KEY", '5f35ff4e9e3acf378dfe3d1e0b77cb883a48025d575dfbeffe2dbc15aca06e85' );
37 define( "NETSUITE_CONSUMER_SECRET", '9c82a24484a3060caac4d4a0e492d09ea58081d06b08ca7928c2bc62fccadd3c' );
38 define( "NETSUITE_TOKEN_ID", '160d802edc696089c6a652f59cee4810f9ce4877b537e774b9f98e95ac2df5d7' );
39 define( "NETSUITE_TOKEN_SECRET", '3a21946c39a565c725d48cc3d1dbff04d6ccc8b113b31742b33bde3a5b918f88' );
40 }
41
42
43 /* No direct access. */
44 if ( !function_exists( 'add_action' ) ) {
45 exit( 0 );
46 }
47
48 /*
49 * Register custom post type
50 */
51
52 if ( !function_exists( 'linear_netsuite_import_custom_post_type' ) ) {
53
54 // Register Custom Post Type
55 function linear_netsuite_import_custom_post_type() {
56
57 $labels = array(
58 'name' => _x( 'Products', 'Post Type General Name', 'linear_netsuite_import' ),
59 'singular_name' => _x( 'Product', 'Post Type Singular Name', 'linear_netsuite_import' ),
60 'menu_name' => __( 'Products', 'linear_netsuite_import' ),
61 'name_admin_bar' => __( 'Product', 'linear_netsuite_import' ),
62 'archives' => __( 'Item Archives', 'linear_netsuite_import' ),
63 'parent_item_colon' => __( 'Parent Product', 'linear_netsuite_import' ),
64 'all_items' => __( 'All Products', 'linear_netsuite_import' ),
65 'add_new_item' => __( 'Add New Product', 'linear_netsuite_import' ),
66 'add_new' => __( 'Add New', 'linear_netsuite_import' ),
67 'new_item' => __( 'New Product', 'linear_netsuite_import' ),
68 'edit_item' => __( 'Edit Product', 'linear_netsuite_import' ),
69 'update_item' => __( 'Update Product', 'linear_netsuite_import' ),
70 'view_item' => __( 'View Product', 'linear_netsuite_import' ),
71 'search_items' => __( 'Search Product', 'linear_netsuite_import' ),
72 'not_found' => __( 'Not found', 'linear_netsuite_import' ),
73 'not_found_in_trash' => __( 'Not found in Trash', 'linear_netsuite_import' ),
74 'featured_image' => __( 'Featured Image', 'linear_netsuite_import' ),
75 'set_featured_image' => __( 'Set featured image', 'linear_netsuite_import' ),
76 'remove_featured_image' => __( 'Remove featured image', 'linear_netsuite_import' ),
77 'use_featured_image' => __( 'Use as featured image', 'linear_netsuite_import' ),
78 'insert_into_item' => __( 'Insert into item', 'linear_netsuite_import' ),
79 'uploaded_to_this_item' => __( 'Uploaded to this item', 'linear_netsuite_import' ),
80 'items_list' => __( 'Items list', 'linear_netsuite_import' ),
81 'items_list_navigation' => __( 'Items list navigation', 'linear_netsuite_import' ),
82 'filter_items_list' => __( 'Filter items list', 'linear_netsuite_import' ),
83 );
84 $args = array(
85 'label' => __( 'Product', 'linear_netsuite_import' ),
86 'description' => __( 'Product imported from Netsuite', 'linear_netsuite_import' ),
87 'labels' => $labels,
88 'supports' => array(
89 'title',
90 'editor',
91 'author',
92 'thumbnail',
93 'revisions',
94 'custom-fields',
95 ),
96 'taxonomies' => array(
97 'custom_class',
98 'brand',
99 'nsc_family',
100 ),
101 'hierarchical' => false,
102 'public' => true,
103 'show_ui' => true,
104 'show_in_menu' => true,
105 'menu_position' => 5,
106 'show_in_admin_bar' => true,
107 'show_in_nav_menus' => true,
108 'can_export' => true,
109 'has_archive' => true,
110 'exclude_from_search' => false,
111 'publicly_queryable' => true,
112 'capability_type' => 'page',
113 );
114 register_post_type( 'linear_product', $args );
115
116 }
117
118 add_action( 'init', 'linear_netsuite_import_custom_post_type', 0 );
119 }
120
121 // options page
122 if ( function_exists( 'acf_add_options_page' ) ) {
123
124 acf_add_options_sub_page( array(
125 'page_title' => 'Product Options',
126 'menu_title' => 'Product Options',
127 'parent_slug' => 'edit.php?post_type=linear_product',
128 ) );
129 }
130
131 if ( !function_exists( 'linear_netsuite_import_custom_class_taxonomy' ) ) {
132
133 // Register Custom Taxonomy
134 function linear_netsuite_import_custom_class_taxonomy() {
135
136 $labels = array(
137 'name' => _x( 'Classes', 'Taxonomy General Name', 'linear_netsuite_import' ),
138 'singular_name' => _x( 'Class', 'Taxonomy Singular Name', 'linear_netsuite_import' ),
139 'menu_name' => __( 'Class', 'linear_netsuite_import' ),
140 'all_items' => __( 'All Items', 'linear_netsuite_import' ),
141 'parent_item' => __( 'Parent Item', 'linear_netsuite_import' ),
142 'parent_item_colon' => __( 'Parent Item:', 'linear_netsuite_import' ),
143 'new_item_name' => __( 'New Item Name', 'linear_netsuite_import' ),
144 'add_new_item' => __( 'Add New Item', 'linear_netsuite_import' ),
145 'edit_item' => __( 'Edit Item', 'linear_netsuite_import' ),
146 'update_item' => __( 'Update Item', 'linear_netsuite_import' ),
147 'view_item' => __( 'View Item', 'linear_netsuite_import' ),
148 'separate_items_with_commas' => __( 'Separate items with commas', 'linear_netsuite_import' ),
149 'add_or_remove_items' => __( 'Add or remove items', 'linear_netsuite_import' ),
150 'choose_from_most_used' => __( 'Choose from the most used', 'linear_netsuite_import' ),
151 'popular_items' => __( 'Popular Items', 'linear_netsuite_import' ),
152 'search_items' => __( 'Search Items', 'linear_netsuite_import' ),
153 'not_found' => __( 'Not Found', 'linear_netsuite_import' ),
154 'no_terms' => __( 'No items', 'linear_netsuite_import' ),
155 'items_list' => __( 'Items list', 'linear_netsuite_import' ),
156 'items_list_navigation' => __( 'Items list navigation', 'linear_netsuite_import' ),
157 );
158 $args = array(
159 'labels' => $labels,
160 'hierarchical' => true,
161 'public' => true,
162 'show_ui' => true,
163 'show_admin_column' => true,
164 'show_in_nav_menus' => true,
165 'show_tagcloud' => true,
166 );
167 register_taxonomy( 'custom_class', array('linear_product'), $args );
168
169 }
170
171 add_action( 'init', 'linear_netsuite_import_custom_class_taxonomy', 0 );
172
173 }
174
175 if ( !function_exists( 'linear_netsuite_import_brand_taxonomy' ) ) {
176
177 // Register Custom Taxonomy
178 function linear_netsuite_import_brand_taxonomy() {
179
180 $labels = array(
181 'name' => _x( 'Brands', 'Taxonomy General Name', 'linear_netsuite_import' ),
182 'singular_name' => _x( 'Brand', 'Taxonomy Singular Name', 'linear_netsuite_import' ),
183 'menu_name' => __( 'Brand', 'linear_netsuite_import' ),
184 'all_items' => __( 'All Items', 'linear_netsuite_import' ),
185 'parent_item' => __( 'Parent Item', 'linear_netsuite_import' ),
186 'parent_item_colon' => __( 'Parent Item:', 'linear_netsuite_import' ),
187 'new_item_name' => __( 'New Item Name', 'linear_netsuite_import' ),
188 'add_new_item' => __( 'Add New Item', 'linear_netsuite_import' ),
189 'edit_item' => __( 'Edit Item', 'linear_netsuite_import' ),
190 'update_item' => __( 'Update Item', 'linear_netsuite_import' ),
191 'view_item' => __( 'View Item', 'linear_netsuite_import' ),
192 'separate_items_with_commas' => __( 'Separate items with commas', 'linear_netsuite_import' ),
193 'add_or_remove_items' => __( 'Add or remove items', 'linear_netsuite_import' ),
194 'choose_from_most_used' => __( 'Choose from the most used', 'linear_netsuite_import' ),
195 'popular_items' => __( 'Popular Items', 'linear_netsuite_import' ),
196 'search_items' => __( 'Search Items', 'linear_netsuite_import' ),
197 'not_found' => __( 'Not Found', 'linear_netsuite_import' ),
198 'no_terms' => __( 'No items', 'linear_netsuite_import' ),
199 'items_list' => __( 'Items list', 'linear_netsuite_import' ),
200 'items_list_navigation' => __( 'Items list navigation', 'linear_netsuite_import' ),
201 );
202 $args = array(
203 'labels' => $labels,
204 'hierarchical' => false,
205 'public' => true,
206 'show_ui' => true,
207 'show_admin_column' => true,
208 'show_in_nav_menus' => true,
209 'show_tagcloud' => true,
210 );
211 register_taxonomy( 'brand', array('linear_product'), $args );
212
213 }
214
215 add_action( 'init', 'linear_netsuite_import_brand_taxonomy', 0 );
216
217 }
218
219 if ( !function_exists( 'linear_netsuite_import_nsc_family_taxonomy' ) ) {
220
221 // Register Custom Taxonomy
222 function linear_netsuite_import_nsc_family_taxonomy() {
223
224 $labels = array(
225 'name' => _x( 'NSC Family', 'Taxonomy General Name', 'linear_netsuite_import' ),
226 'singular_name' => _x( 'NSC Family', 'Taxonomy Singular Name', 'linear_netsuite_import' ),
227 'menu_name' => __( 'NSC Family', 'linear_netsuite_import' ),
228 'all_items' => __( 'All Items', 'linear_netsuite_import' ),
229 'parent_item' => __( 'Parent Item', 'linear_netsuite_import' ),
230 'parent_item_colon' => __( 'Parent Item:', 'linear_netsuite_import' ),
231 'new_item_name' => __( 'New Item Name', 'linear_netsuite_import' ),
232 'add_new_item' => __( 'Add New Item', 'linear_netsuite_import' ),
233 'edit_item' => __( 'Edit Item', 'linear_netsuite_import' ),
234 'update_item' => __( 'Update Item', 'linear_netsuite_import' ),
235 'view_item' => __( 'View Item', 'linear_netsuite_import' ),
236 'separate_items_with_commas' => __( 'Separate items with commas', 'linear_netsuite_import' ),
237 'add_or_remove_items' => __( 'Add or remove items', 'linear_netsuite_import' ),
238 'choose_from_most_used' => __( 'Choose from the most used', 'linear_netsuite_import' ),
239 'popular_items' => __( 'Popular Items', 'linear_netsuite_import' ),
240 'search_items' => __( 'Search Items', 'linear_netsuite_import' ),
241 'not_found' => __( 'Not Found', 'linear_netsuite_import' ),
242 'no_terms' => __( 'No items', 'linear_netsuite_import' ),
243 'items_list' => __( 'Items list', 'linear_netsuite_import' ),
244 'items_list_navigation' => __( 'Items list navigation', 'linear_netsuite_import' ),
245 );
246 $args = array(
247 'labels' => $labels,
248 'hierarchical' => false,
249 'public' => true,
250 'show_ui' => true,
251 'show_admin_column' => true,
252 'show_in_nav_menus' => true,
253 'show_tagcloud' => true,
254 );
255 register_taxonomy( 'nsc_family', array('linear_product'), $args );
256
257 }
258
259 add_action( 'init', 'linear_netsuite_import_nsc_family_taxonomy', 0 );
260
261 }
262
263 /*
264 * New Low Joe categories
265 */
266 if ( !function_exists( 'lowjoe_categories' ) ) {
267
268 // Register Custom Taxonomy
269 function lowjoe_categories() {
270
271 $labels = array(
272 'name' => _x( 'Product Categories', 'Taxonomy General Name', 'text_domain' ),
273 'singular_name' => _x( 'Product Category', 'Taxonomy Singular Name', 'text_domain' ),
274 'menu_name' => __( 'NS Categories', 'text_domain' ),
275 'all_items' => __( 'All NS Categories', 'text_domain' ),
276 'parent_item' => __( 'Parent NS Category', 'text_domain' ),
277 'parent_item_colon' => __( 'Parent NS Category:', 'text_domain' ),
278 'new_item_name' => __( 'New NS Category Name', 'text_domain' ),
279 'add_new_item' => __( 'Add New NS Category', 'text_domain' ),
280 'edit_item' => __( 'Edit NS Category', 'text_domain' ),
281 'update_item' => __( 'Update NS Category', 'text_domain' ),
282 'view_item' => __( 'View NS Category', 'text_domain' ),
283 'separate_items_with_commas' => __( 'Separate NS Categories with commas', 'text_domain' ),
284 'add_or_remove_items' => __( 'Add or remove NS Categories', 'text_domain' ),
285 'choose_from_most_used' => __( 'Choose from the most used NS Categories', 'text_domain' ),
286 'popular_items' => __( 'Popular NS Categories', 'text_domain' ),
287 'search_items' => __( 'Search NS Categories', 'text_domain' ),
288 'not_found' => __( 'NS Category Not Found', 'text_domain' ),
289 'no_terms' => __( 'No NS Categories', 'text_domain' ),
290 'items_list' => __( 'NS Categories list', 'text_domain' ),
291 'items_list_navigation' => __( 'NS Categories list navigation', 'text_domain' ),
292 );
293 $args = array(
294 'labels' => $labels,
295 'hierarchical' => true,
296 'public' => true,
297 'show_ui' => true,
298 'show_admin_column' => true,
299 'show_in_nav_menus' => true,
300 'show_tagcloud' => true,
301 );
302 register_taxonomy( 'ns_category', array('linear_product'), $args );
303
304 }
305
306 add_action( 'init', 'lowjoe_categories', 0 );
307
308 }
309
310 add_action('admin_menu', 'linear_netsuite_import_admin_menu');
311 function linear_netsuite_import_admin_menu() {
312 add_menu_page('Linear Netsuite Import', 'Linear Netsuite Import', 'activate_plugins', 'linearnetsuite', 'linear_netsuite_import_admin_page');
313 }
314
315 function linear_netsuite_import_admin_page() {
316 linear_netsuite_import_fetch();
317 }
318
319 register_activation_hook( __FILE__, 'linear_netsuite_import_daily_schedule' );
320
321 function linear_netsuite_import_daily_schedule() {
322 $timestamp = wp_next_scheduled( 'linear_netsuite_import_daily_fetch' );
323 if ( $timestamp == false ) {
324 wp_schedule_event( strtotime( '16:00' ), 'daily', 'linear_netsuite_import_daily_fetch' );
325 }
326 }
327
328 register_deactivation_hook( __FILE__, 'linear_netsuite_import_remove_daily_schedule' );
329
330 function linear_netsuite_import_remove_daily_schedule() {
331 wp_clear_scheduled_hook( 'linear_netsuite_import_daily_fetch' );
332 }
333
334 /*
335 * Function to fetch from Netsuite into Wordpress
336 */
337 add_action( 'linear_netsuite_import_daily_fetch', 'linear_netsuite_import_fetch' );
338 function linear_netsuite_import_fetch() {
339 //Getting result from Netsuite
340 $result = netsuite_get_data();
341
342 $php_result = json_decode( $result );
343
344
345 //Prepare all of the linear posts in Wordpress
346 $linear_products = get_posts( array(
347 'post_type' => 'linear_product',
348 'posts_per_page' => -1,
349 ) );
350
351 // first, cycle and see if we already have the item, and if so
352 // add it to a "update" list, and add the rest of the WP products
353 // to the "delete" list
354
355 $incoming_products = $php_result->result;
356 $incoming_itemids = array();
357 foreach ( $incoming_products as $incoming_product ) {
358 $incoming_itemids[] = $incoming_product->internalid;
359 }
360 foreach ( $incoming_itemids as $incoming_id ) {
361 $check_args = array(
362 'post_type' => 'linear_product',
363 'meta_value' => $incoming_id,
364 'meta_key' => 'internalid',
365 );
366 $check = get_posts( $check_args );
367 if ( !empty( $check ) ) {
368 $update_products[] = $check[0]->ID;
369 $update_linear_product[] = $incoming_id;
370 }
371 }
372
373 // get the posts to delete
374 foreach ( $linear_products as $possibly_delete ) {
375 if ( !in_array( $possibly_delete->ID, $update_products ) ) {
376 wp_delete_post( $possibly_delete->ID, true );
377 }
378 }
379
380 if ( $result ) { //If result is not false
381
382
383 $result = json_decode( $result, true ); //force convert everything to array
384
385 $new_fetched_posts = $result['result'];
386
387 foreach ( $new_fetched_posts as $post ) {
388 if ( isset( $post['custitem_prod_cat'] ) ) {
389 $custom_class_parent_term = term_exists( $post['custitem_prod_cat']['text'], 'custom_class' );
390 if ( $custom_class_parent_term === 0 || $custom_class_parent_term === NULL ) {
391 $custom_class_parent_term = wp_insert_term( $post['custitem_prod_cat']['text'], 'custom_class' );
392
393 if ( isset( $post['custitem_prod_subcat'] ) ) {
394 $custom_class_child_term = term_exists( $post['custitem_prod_subcat']['text'], 'custom_class' );
395 if ( $custom_class_child_term === 0 || $custom_class_child_term === NULL ) {
396 $custom_class_child_term = wp_insert_term( $post['custitem_prod_subcat']['text'], 'custom_class', array(
397 'parent' => $custom_class_parent_term['term_id'],
398 ) );
399 }
400 }
401 } else {
402 $custom_class_parent_term_id = $custom_class_parent_term['term_id'];
403 if ( isset( $post['custitem_prod_subcat'] ) ) {
404 $custom_class_child_term = term_exists( $post['custitem_prod_subcat']['text'], 'custom_class' );
405 if ( $custom_class_child_term === 0 || $custom_class_child_term === NULL ) {
406 $custom_class_child_term = wp_insert_term( $post['custitem_prod_subcat']['text'], 'custom_class', array(
407 'parent' => $custom_class_parent_term_id,
408 ) );
409 }
410 }
411 }
412 }
413
414 /*
415 * New Low Joe Cats
416 */
417 /*if ( 1 == 1 ) {
418 if ( isset( $post['custitem_nsc_legacy_productcat_code'] ) ) {
419 $ns_category_parent_term = term_exists( $post['custitem_nsc_legacy_productcat_code']['text'], 'ns_category' );
420 if ( $ns_category_parent_term === 0 || $ns_category_parent_term === NULL ) {
421 $ns_category_parent_term = wp_insert_term( $post['custitem_nsc_legacy_productcat_code']['text'], 'ns_category' );
422
423 if ( isset( $post['custitem_nsc_legacy_produsctsub_codes'] ) ) {
424 $ns_category_child_term = term_exists( $post['custitem_nsc_legacy_produsctsub_codes']['text'], 'ns_category' );
425 if ( $ns_category_child_term === 0 || $ns_category_child_term === NULL ) {
426 $ns_category_child_term = wp_insert_term( $post['custitem_nsc_legacy_produsctsub_codes']['text'], 'ns_category', array(
427 'parent' => $ns_category_parent_term['term_id'],
428 ) );
429 }
430 }
431 } else {
432
433$array_map = array(
434 '27' => 'Access Control Systems'
435);
436
437
438
439 $ns_category_parent_term_id = $ns_category_parent_term['term_id'];
440 if ( isset( $post['custitem_nsc_legacy_produsctsub_codes'] ) ) {
441 $ns_category_child_term = term_exists( $post['custitem_nsc_legacy_produsctsub_codes']['text'], 'ns_category' );
442 if ( $ns_category_child_term === 0 || $ns_category_child_term === NULL ) {
443 $ns_category_child_term = wp_insert_term( $post['custitem_nsc_legacy_produsctsub_codes']['text'], 'ns_category', array(
444 'parent' => $ns_category_parent_term_id,
445 ) );
446 }
447 }
448 }
449 }
450 }*/
451 // end new low joe cats
452
453
454 if ( isset( $post['custitem_brand'] ) ) {
455 $brand_term = term_exists( $post['custitem_brand']['text'], 'brand' );
456 if ( $brand_term === 0 || $brand_term === NULL ) {
457 $brand_term = wp_insert_term( $post['custitem_brand']['text'], 'brand' );
458 }
459 }
460
461 if ( isset( $post['custitem_nsc_item_family'] ) ) {
462 $nsc_item_family_term = term_exists( $post['custitem_nsc_item_family']['text'], 'nsc_family' );
463 if ( $nsc_item_family_term === 0 || $nsc_item_family_term === NULL ) {
464 $nsc_item_family_term = wp_insert_term( $post['custitem_nsc_item_family']['text'], 'nsc_family' );
465 }
466 }
467
468
469 //check if product exists again
470 $check_args = array(
471 'post_type' => 'linear_product',
472 'meta_value' => $post['internalid'],
473 'meta_key' => 'internalid',
474 );
475 $check = get_posts( $check_args );
476 $update = false;
477 if ( !empty( $check ) ) {
478 $update = true;
479 $post_id = $check[0]->ID;
480 $new_wp_post = array(
481 'ID' => $post_id,
482 'post_title' => $post['displayname']['value'],
483 'post_type' => 'linear_product',
484 'post_content' => $post['storedetaileddescription']['value'],
485 'post_status' => 'publish',
486 );
487 $post_id = wp_update_post( $new_wp_post );
488 } else {
489
490 $new_wp_post = array(
491 'post_title' => $post['displayname']['value'],
492 'post_type' => 'linear_product',
493 'post_content' => $post['storedetaileddescription']['value'],
494 'post_status' => 'publish',
495 );
496 //insert new wordpress post
497 $post_id = wp_insert_post( $new_wp_post );
498 }
499
500
501 // related items
502 if ( !empty( $post['relatedItems'] ) ) {
503 $related_product_ids = array();
504 foreach ( $post['relatedItems'] as $related_item ) {
505 $related_product_ids[] = $related_item['internalid'];
506 }
507 update_post_meta( $post_id, 'related_items', json_encode( $related_product_ids ) );
508 }
509
510 // set terms for product
511 if ( isset( $custom_class_parent_term ) ) {
512 wp_set_object_terms( $post_id, array(
513 intval( $custom_class_parent_term['term_id'] ),
514 intval( $custom_class_child_term['term_id'] ),
515 ), 'custom_class' );
516 }
517
518 // low joe terms
519 if ( isset( $ns_category_parent_term ) ) {
520 wp_set_object_terms( $post_id, array(
521 intval( $ns_category_parent_term['term_id'] ),
522 intval( $ns_category_child_term['term_id'] ),
523 ), 'ns_category' );
524 }
525
526 if ( isset( $brand_term ) && !array_key_exists('errors', $brand_term)) {
527 wp_set_object_terms( $post_id, intval( $brand_term['term_id'] ), 'brand' );
528 }
529
530
531 if ( isset( $nsc_item_family_term )&& !array_key_exists('errors', $nsc_item_family_term) ) wp_set_object_terms( $post_id, intval( $nsc_item_family_term['term_id'] ), 'nsc_family' );
532
533 if ( isset( $post['custitemmodel_number'] ) ) {
534 update_field( 'custom_item_model_number', $post['custitemmodel_number']['value'], $post_id );
535 }
536
537 if ( isset( $post['class'] ) ) {
538 update_field( 'class', $post['class']['text'], $post_id );
539 }
540
541 if(isset($post['featureddescription'])){
542 update_post_meta($post_id, 'product_features',$post['featureddescription']['value'] );
543 }
544
545 // add the internal ID to the post
546 update_post_meta( $post_id, 'internalid', $post['internalid'] );
547
548 $upload_dir = wp_upload_dir();
549 $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
550 if ( isset( $post['storedisplayimage']['file']['content'] ) ) {
551 $img = $post['storedisplayimage']['file']['content'];
552 $img = str_replace( ' ', '+', $img );
553 $decoded_img = base64_decode( $img );
554
555 $file_name = $post['storedisplayimage']['file']['name'];
556 $hashed_file_name = $file_name;
557
558 $existing_attachment_id = get_attachment_id($upload_dir['url'] . DIRECTORY_SEPARATOR . $hashed_file_name);
559 if (isset($existing_attachment_id))
560 {
561 set_post_thumbnail( $post_id, $existing_attachment_id );
562 } else {
563 if(!file_exists($upload_path . $hashed_file_name)) {
564 $image_upload = file_put_contents( $upload_path . $hashed_file_name, $decoded_img );
565 }
566
567 if ( !function_exists( 'wp_handle_sideload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
568
569 $file = array();
570 $file['error'] = '';
571 $file['tmp_name'] = $upload_path . $hashed_file_name;
572 $file['name'] = $hashed_file_name;
573 if ( $post['storedisplayimage']['file']['fileType'] == 'JPGIMAGE' ) $file['type'] = 'image/jpg'; else
574 $file['type'] = 'image/png';
575
576 $file['size'] = filesize( $upload_path . $hashed_file_name );
577 $file_return = wp_handle_sideload( $file, array('test_form' => false) );
578
579 $file_name = $file_return['file'];
580 $attachment = array(
581 'post_mime_type' => $file_return['type'],
582 'post_title' => sanitize_file_name( $file_name ),
583 'post_content' => '',
584 'post_status' => 'inherit',
585 'guid' => $upload_dir['url'] . '/' . basename( $file_name ),
586 );
587 $attach_id = wp_insert_attachment( $attachment, $file_name, $post_id );
588 require_once( ABSPATH . 'wp-admin/includes/image.php' );
589 $attach_data = wp_generate_attachment_metadata( $attach_id, $file_name );
590 wp_update_attachment_metadata( $attach_id, $attach_data );
591 set_post_thumbnail( $post_id, $attach_id );
592 }
593
594 }
595 }
596 }
597 }
598
599
600 function netsuite_get_data() {
601
602 $details = array(
603 "func" => "getCatalogItems",
604 "webid" => 3,
605 "getImages" => true,
606 "imageContent" => true,
607 "relatedItems" => true,
608
609 );
610
611 $data_string = json_encode( $details );
612
613 $oauth_nonce = md5( mt_rand() );
614 $oauth_timestamp = time();
615 $oauth_signature_method = 'HMAC-SHA1';
616 $oauth_version = "1.0";
617
618 $base_string = "POST&" . urlencode( NETSUITE_URL ) . "&" . urlencode( "deploy=" . NETSUITE_DEPLOY_ID . "&oauth_consumer_key=" . NETSUITE_CONSUMER_KEY . "&oauth_nonce=" . $oauth_nonce . "&oauth_signature_method=" . $oauth_signature_method . "&oauth_timestamp=" . $oauth_timestamp . "&oauth_token=" . NETSUITE_TOKEN_ID . "&oauth_version=" . $oauth_version . "&realm=" . NETSUITE_ACCOUNT . "&script=" . NETSUITE_SCRIPT_ID );
619
620 $sig_string = urlencode( NETSUITE_CONSUMER_SECRET ) . '&' . urlencode( NETSUITE_TOKEN_SECRET );
621 $signature = base64_encode( hash_hmac( "sha1", $base_string, $sig_string, true ) );
622
623 $auth_header = "OAuth " . 'oauth_signature="' . rawurlencode( $signature ) . '", ' . 'oauth_version="' . rawurlencode( $oauth_version ) . '", ' . 'oauth_nonce="' . rawurlencode( $oauth_nonce ) . '", ' . 'oauth_signature_method="' . rawurlencode( $oauth_signature_method ) . '", ' . 'oauth_consumer_key="' . rawurlencode( NETSUITE_CONSUMER_KEY ) . '", ' . 'oauth_token="' . rawurlencode( NETSUITE_TOKEN_ID ) . '", ' . 'oauth_timestamp="' . rawurlencode( $oauth_timestamp ) . '", ' . 'realm="' . rawurlencode( NETSUITE_ACCOUNT ) . '"';
624
625 $ch = curl_init();
626 curl_setopt( $ch, CURLOPT_URL, NETSUITE_URL . '?&script=' . NETSUITE_SCRIPT_ID . '&deploy=' . NETSUITE_DEPLOY_ID . '&realm=' . NETSUITE_ACCOUNT );
627 curl_setopt( $ch, CURLOPT_POST, "POST" );
628 curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
629 // dev localhost
630 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
631 curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
632
633 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
634 curl_setopt( $ch, CURLOPT_HTTPHEADER, [
635 'Authorization: ' . $auth_header,
636 'Content-Type: application/json',
637 'Content-Length: ' . strlen( $data_string ),
638 ] );
639
640 $result = curl_exec( $ch );
641 if ( curl_error( $ch ) ) {
642 error_log( curl_error( $ch ) );
643 curl_close( $ch );
644
645 return false;
646 } else {
647 curl_close( $ch );
648
649 return $result;
650 }
651 }
652
653 function get_attachment_id ($url)
654 {
655 $attachment_id = 0;
656
657 $dir = wp_upload_dir();
658
659 if (strpos($url, $dir['url']) . '/' !== FALSE)
660 {
661 $file = basename($url);
662 $query_args = array(
663 'post_type' => 'attachment',
664 'post_status' => 'inherit',
665 'fields' => 'ids',
666 'meta_query' => array(
667 array(
668 'value' => $file,
669 'compare' => 'LIKE',
670 'key' => '_wp_attachment_metadata'
671 ),
672 )
673 );
674
675 $query = new WP_Query($query_args);
676 if ($query->have_posts())
677 {
678 foreach ($query->posts as $post_id)
679 {
680 $meta = wp_get_attachment_metadata($post_id);
681 $original_file = basename($meta['file']);
682 $cropped_image_files = wp_list_pluck($meta['sizes'], 'file');
683
684 if ($original_file === $file || in_array($file, $cropped_image_files))
685 {
686 $attachment_id = $post_id;
687 break;
688 }
689 }
690 }
691 }
692
693 return $attachment_id;
694 }