WC_Naguro_Product_Meta_Box

The WooCommerce Naguro Connect WC Naguro Product Meta Box class.

Defined (1)

The class is defined in the following location(s).

/includes/woocommerce/admin/class-wc-naguro-product-meta-box.php  
  1. class WC_Naguro_Product_Meta_Box { 
  2. /** @var string */ 
  3. private $units = "mm"; 
  4.  
  5. public function __construct() { 
  6. add_action( 'admin_enqueue_scripts' , array( $this, 'add_assets' ) ); 
  7. add_action( 'post_edit_form_tag' , array( $this, 'post_edit_form_tag' ) ); 
  8.  
  9. $options = get_option('naguro_settings'); 
  10. $this->units = ( $options['dimension_unit'] ? $options['dimension_unit'] : get_option('woocommerce_dimension_unit', $this->units) ); 
  11.  
  12. public function add_assets() { 
  13. wp_enqueue_script("wc-naguro", NAGURO_PLUGIN_URL . "assets/js/wc-naguro.js", array("jquery")); 
  14. wp_enqueue_style("wc-naguro", NAGURO_PLUGIN_URL . "assets/css/wc-naguro.css"); 
  15.  
  16. wp_enqueue_script("imgareaselect", NAGURO_PLUGIN_URL . "assets/imgareaselect/jquery.imgareaselect.min.js", array("jquery")); 
  17. wp_enqueue_style("imgareaselect", NAGURO_PLUGIN_URL . "assets/imgareaselect/imgareaselect-default.css"); 
  18.  
  19. wp_enqueue_style('thickbox'); 
  20. wp_enqueue_script('thickbox'); 
  21.  
  22. public function post_edit_form_tag() { 
  23. echo ' enctype="multipart/form-data"'; 
  24.  
  25. public function output( $post ) { 
  26. echo '<div id="woocommerce_naguro_settings" class="panel woocommerce_options_panel show_if_naguro">'; 
  27.  
  28. wp_nonce_field( 'woocommerce_naguro_product_meta_box', 'woocommerce_naguro_product_meta_box_nonce' ); 
  29.  
  30. echo '<div class="options_group">'; 
  31. $this->add_enable_checkbox(); 
  32. echo '</div>'; 
  33.  
  34. echo '<div class="options_group">'; 
  35. $this->add_design_areas(); 
  36. echo '</div>'; 
  37.  
  38. echo '</div>'; 
  39.  
  40. public function add_enable_checkbox() { 
  41. global $post; 
  42. $name = WC_Naguro::$prefix . "exists"; 
  43.  
  44. woocommerce_wp_checkbox(array( 
  45. "id" => $name,  
  46. "label" => "Naguro product?",  
  47. "name" => $name,  
  48. "value" => ( 'yes' == get_post_meta( $post->ID, 'naguro_product_active', true ) ? 'yes' : 'no' ),  
  49. "description" => "Enable the customer to configure this product with the Naguro designer" 
  50. )); 
  51.  
  52. private function get_design_areas() { 
  53. global $post; 
  54. $design_areas = get_post_meta($post->ID, 'naguro_design_area', false); 
  55.  
  56. foreach ( $design_areas as $key => $design_area ) { 
  57. if ( isset( $design_area['product_image_id'] ) ) { 
  58. $image_src = wp_get_attachment_image_src( $design_area['product_image_id'], 'full' ); 
  59. $design_areas[ $key ]['product_image'] = $image_src[0]; 
  60.  
  61. $design_areas[ $key ] = apply_filters("naguro_woocommerce_design_area_data", $design_areas[ $key ]); 
  62.  
  63. return $design_areas; 
  64.  
  65. public function add_design_areas() { 
  66. $design_areas = $this->get_design_areas(); 
  67.  
  68. echo "<div class='wc-metaboxes naguro-design-areas'>"; 
  69.  
  70. if (0 === sizeof( $design_areas )) { 
  71. echo "<p>No design areas found for this product.</p>"; 
  72.  
  73. echo '<section class="naguro-design-areas-container-ghost">'; 
  74. $this->add_design_area(array()); 
  75. echo '</section>'; 
  76.  
  77. echo '<button type="button" class="button button-primary" id="naguro-add-new-design-area">Add new design area</button>'; 
  78.  
  79. echo '<section class="naguro-design-areas-container">'; 
  80.  
  81. foreach ( $design_areas as $design_area ) { 
  82. $this->add_design_area($design_area); 
  83.  
  84. echo '</section>'; 
  85.  
  86. echo "</div>"; 
  87.  
  88. public function add_design_area($design_area = array()) { 
  89. echo '<article class="naguro-design-area">'; 
  90.  
  91. $this->add_remove_button(); 
  92.  
  93. $this->add_design_area_name($design_area); 
  94. $this->add_design_area_output_width($design_area); 
  95. $this->add_design_area_output_height($design_area); 
  96. $this->add_design_area_background($design_area); 
  97.  
  98. echo '</article>'; 
  99.  
  100. public function add_remove_button() { 
  101. echo '<button type="button" class="remove_row button">Remove</button>'; 
  102.  
  103. public function add_design_area_name($design_area = array()) { 
  104. $name = WC_Naguro::$prefix . "designarea[name][]"; 
  105.  
  106. woocommerce_wp_text_input(array( 
  107. "id" => $name,  
  108. "label" => "Name",  
  109. "placeholder" => "Name of the design area",  
  110. "name" => $name,  
  111. "class" => "",  
  112. "value" => (isset($design_area["name"]) ? $design_area["name"] : "" ) 
  113. )); 
  114.  
  115. public function add_design_area_output_width($design_area = array()) { 
  116. $name = WC_Naguro::$prefix . "designarea[output_width][]"; 
  117.  
  118. woocommerce_wp_text_input(array( 
  119. "id" => $name,  
  120. "label" => "Print width",  
  121. "placeholder" => "Width of the printable area",  
  122. "description" => "Width of the printable area in " . $this->units . " without the unit (eg '25')",  
  123. "name" => $name,  
  124. "class" => "naguro-float-val",  
  125. "value" => (isset($design_area["output_width"]) ? $design_area["output_width"] : "10" ) 
  126. )); 
  127.  
  128. public function add_design_area_output_height($design_area = array()) { 
  129. $name = WC_Naguro::$prefix . "designarea[output_height][]"; 
  130.  
  131. woocommerce_wp_text_input(array( 
  132. "id" => $name,  
  133. "label" => "Print height",  
  134. "placeholder" => "Height of the printable area",  
  135. "description" => "Height of the printable area in " . $this->units . " without the unit (eg '12.5')",  
  136. "name" => $name,  
  137. "class" => "naguro-float-val",  
  138. "value" => (isset($design_area["output_height"]) ? $design_area["output_height"] : "10" ) 
  139. )); 
  140.  
  141. public function add_design_area_background($design_area = array()) { 
  142. $rand = rand(10000, 99999); 
  143. $this->add_design_area_upload_key($rand); 
  144. $this->add_design_area_background_upload($rand, ( isset( $design_area['product_image_id'] ) ? $design_area['product_image_id'] : "" )); 
  145.  
  146. do_action("naguro_woocommerce_before_printable_area_button", $rand, $design_area); 
  147.  
  148. echo "<p class='form-field'><a class='button naguro-define-image-area' data-id='" . $rand . "'>Edit printable area</a></p>"; 
  149.  
  150. $this->add_design_area_printable_area($design_area, $rand); 
  151.  
  152. public function add_design_area_background_upload($rand, $image) { 
  153. $name = WC_Naguro::$prefix . "designarea[image][" . $rand . "]"; 
  154.  
  155. $this->upload_field($name, "Design area image", "Upload an image that will serve as the image that will be designed on", $image, "naguro_designarea[product_image_id][]"); 
  156.  
  157. public function add_design_area_upload_key($rand) { 
  158. $this->hidden_input( 
  159. WC_Naguro::$prefix . "designarea[upload_key][]",  
  160. $rand,  
  161. WC_Naguro::$prefix . "designarea_upload_key" 
  162. ); 
  163.  
  164. public function add_design_area_printable_area($design_area = array(), $rand) { 
  165. echo '<div class="naguro-printable-product" id="' . $rand . '">'; 
  166.  
  167. $this->add_design_area_print_width($design_area); 
  168. $this->add_design_area_print_height($design_area); 
  169. $this->add_design_area_left($design_area); 
  170. $this->add_design_area_top($design_area); 
  171.  
  172. if ( isset( $design_area['product_image_id'] ) ) { 
  173. $this->add_design_area_image_id($design_area['product_image_id']); 
  174. } else { 
  175. $this->add_design_area_image_id(0); 
  176.  
  177. if ( isset( $design_area['product_image'] ) ) { 
  178. echo '<img class="background-image" src="' . $design_area['product_image'] . '" />'; 
  179. } else { 
  180. echo '<img class="background-image" src="" />'; 
  181.  
  182. do_action("naguro_woocommerce_after_printable_area_image", $design_area); 
  183.  
  184. echo '<a href="#" class="button naguro-printable-area-save-button">OK</a>'; 
  185.  
  186. echo '</div>'; 
  187.  
  188. public function add_design_area_print_width($design_area = array()) { 
  189. $this->hidden_input( 
  190. WC_Naguro::$prefix . "designarea[print_width][]",  
  191. (isset($design_area["print_width"]) ? $design_area["print_width"] : "0" ),  
  192. WC_Naguro::$prefix . "designarea_print_width" 
  193. ); 
  194.  
  195. public function add_design_area_print_height($design_area = array()) { 
  196. $this->hidden_input( 
  197. WC_Naguro::$prefix . "designarea[print_height][]",  
  198. (isset($design_area["print_height"]) ? $design_area["print_height"] : "0" ),  
  199. WC_Naguro::$prefix . "designarea_print_height" 
  200. ); 
  201.  
  202. public function add_design_area_left($design_area = array()) { 
  203. $this->hidden_input( 
  204. WC_Naguro::$prefix . "designarea[left][]",  
  205. (isset($design_area["left"]) ? $design_area["left"] : "0" ),  
  206. WC_Naguro::$prefix . "designarea_left" 
  207. ); 
  208.  
  209. public function add_design_area_top($design_area = array()) { 
  210. $this->hidden_input( 
  211. WC_Naguro::$prefix . "designarea[top][]",  
  212. (isset($design_area["top"]) ? $design_area["top"] : "0" ),  
  213. WC_Naguro::$prefix . "designarea_top" 
  214. ); 
  215.  
  216. public function add_design_area_image_id($design_area_product_image_id) { 
  217. $this->hidden_input( 
  218. WC_Naguro::$prefix . "designarea[product_image_id][]",  
  219. $design_area_product_image_id,  
  220. WC_Naguro::$prefix . "product_image_id" 
  221. ); 
  222.  
  223. public function hidden_input($name, $value, $class = "") { 
  224. echo '<input type="hidden" name="' . $name . '" value="' . $value . '" class="' . $class . '" />'; 
  225.  
  226. public function save( $post_id ) { 
  227. // Check if our nonce is set. 
  228. if ( ! isset( $_POST['woocommerce_naguro_product_meta_box_nonce'] ) ) { 
  229. return; 
  230.  
  231. // Verify that the nonce is valid. 
  232. if ( ! wp_verify_nonce( $_POST['woocommerce_naguro_product_meta_box_nonce'], 'woocommerce_naguro_product_meta_box' ) ) { 
  233. return; 
  234.  
  235. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 
  236. return; 
  237.  
  238. // START checkbox save 
  239. if ( isset( $_POST[ WC_Naguro::$prefix . "exists" ] ) && 'yes' == $_POST[ WC_Naguro::$prefix . "exists" ] ) { 
  240. $checkbox_value = 'yes'; 
  241. } else { 
  242. $checkbox_value = 'no'; 
  243.  
  244. update_post_meta( $post_id, 'naguro_product_active', $checkbox_value ); 
  245. // END checkbox save 
  246.  
  247. // START file upload handler 
  248. $stack = $_FILES['naguro_designarea']; 
  249. $files = array(); 
  250.  
  251. $keys = array( 'name', 'type', 'tmp_name', 'error', 'size' ); 
  252. $file_keys = apply_filters("naguro_woocommerce_file_keys", array('image')); 
  253. // Loop through the posted keys and collect them per design area 
  254. foreach ( $keys as $key ) { 
  255. foreach( $file_keys as $file_key ) { 
  256. foreach( $stack[ $key ][ $file_key ] as $item_key => $item ) { 
  257. $files[ $item_key ][$file_key][ $key ] = $item; 
  258.  
  259. foreach ( $files as $key => $file ) { 
  260. foreach( $file_keys as $file_key ) { 
  261. if ( isset( $file[$file_key] ) && 0 == $file[$file_key]['size'] && 4 == $file[$file_key]['error'] ) { 
  262. unset( $files[ $key ][$file_key] ); 
  263.  
  264. $i = 0; 
  265. $image_ids = array(); 
  266. foreach ( $files as $key => $file ) { 
  267. foreach( $file_keys as $file_key ) { 
  268. if ( isset( $file[$file_key] ) ) { 
  269. if ( empty( $file[ $file_key ]['name'] ) && 4 == $file[ $file_key ]['error'] ) { 
  270. $image_ids[ $file_key ][ $key ] = 0; 
  271. } else { 
  272. $_FILES[ 'naguro_designarea_' . $i ] = $file[ $file_key ]; 
  273. $image_ids[ $file_key ][ $key ] = media_handle_upload( 'naguro_designarea_' . $i, $post_id ); 
  274. $i++; 
  275. // END file upload handler 
  276.  
  277. $stack = $_POST['naguro_designarea']; 
  278. $design_areas = array(); 
  279.  
  280. $keys = apply_filters("naguro_woocommerce_save_keys", array( 
  281. 'name',  
  282. 'output_width',  
  283. 'output_height',  
  284. 'print_width',  
  285. 'print_height',  
  286. 'left',  
  287. 'top',  
  288. 'product_image_id',  
  289. 'upload_key' 
  290. )); 
  291.  
  292. // Loop through the posted keys and collect them per design area 
  293. foreach ( $keys as $key ) { 
  294. if ( isset( $stack[ $key ] ) ) { 
  295. foreach ( $stack[ $key ] as $item_key => $item ) { 
  296. $design_areas[ $item_key ][ $key ] = $item; 
  297.  
  298. // Remove the first item off the array, as that's the empty ghost 
  299. array_shift( $design_areas ); 
  300.  
  301. $this->remove_old_meta_fields($post_id); 
  302.  
  303. $options = get_option('naguro_settings'); 
  304. $unit = $options['dimension_unit']; 
  305.  
  306. // Save each design area as separate post meta objects 
  307. foreach ( $design_areas as $design_area ) { 
  308. if ( isset( $image_ids['image'][ $design_area['upload_key'] ] ) ) { 
  309. $image_id = $image_ids['image'][ $design_area['upload_key']]; 
  310. } elseif ( isset( $design_area['product_image_id'] ) ) { 
  311. $image_id = $design_area['product_image_id']; 
  312. } else { 
  313. $image_id = 0; 
  314.  
  315. $design_area['size_description'] = $design_area['output_width'] . $unit . ' x ' . $design_area['output_height'] . $unit; 
  316.  
  317. if ( 0 != $image_id ) { 
  318. $design_area['product_image_id'] = $image_id; 
  319.  
  320. $design_area = apply_filters( "naguro_woocommerce_filter_save_image", $design_area, $image_ids ); 
  321.  
  322. add_post_meta( $post_id, 'naguro_design_area', $design_area, false ); 
  323.  
  324. private function remove_old_meta_fields( $post_id ) { 
  325. delete_post_meta($post_id, 'naguro_design_area'); 
  326.  
  327. static function upload_field($name, $label, $description, $file_id, $hidden_name) { 
  328. $image_src = wp_get_attachment_image_src( $file_id, 'full' ); 
  329. $image_src = $image_src[0]; 
  330. $filename = basename($image_src); 
  331.  
  332. ?> 
  333. <section class="naguro-upload <?php echo ($image_src ? "opened" : "closed"); ?>" data-hidden-name="<?php echo $hidden_name; ?>"> 
  334. <div class="current-file"> 
  335. <span class="filename"><?php echo $filename; ?></span> 
  336. <a><?php echo __("Change file"); ?></a> 
  337. </div> 
  338. <div class="upload-file"></div> 
  339. </section> 
  340. <?php 
  341. woocommerce_wp_text_input(array( 
  342. "id" => $name,  
  343. "label" => $label,  
  344. "description" => $description,  
  345. "name" => $name,  
  346. "value" => "",  
  347. "class" => "",  
  348. "type" => "file" 
  349. )); 
  350.