WooCommerce_Order_Notes_Window_t4m

The WooCommerce Order Notes Window WooCommerce Order Notes Window t4m class.

Defined (1)

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

/woocommerce-order-notes-window.php  
  1. class WooCommerce_Order_Notes_Window_t4m { 
  2.  
  3. public function __construct() { 
  4. $this->current_tab = (isset($_GET['tab'])) ? $_GET['tab'] : 'general'; 
  5.  
  6. $this->settings_tabs = array( 
  7. 'order_notes_window' => __('Order Notes Window', 'wc_order_notes_window') 
  8. ); 
  9.  
  10. add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'action_links')); 
  11.  
  12. add_action('woocommerce_settings_tabs', array($this, 'add_tab'), 10); 
  13.  
  14. foreach ($this->settings_tabs as $name => $label) { 
  15. add_action('woocommerce_settings_tabs_' . $name, array($this, 'settings_tab_action'), 10); 
  16. add_action('woocommerce_update_options_' . $name, array($this, 'save_settings'), 10); 
  17.  
  18. add_action('woocommerce_order_notes_window_settings', array($this, 'add_settings_fields'), 10); 
  19. add_action('manage_shop_order_posts_custom_column', array($this, 'custom_order_columns'), 10); 
  20. add_action('admin_enqueue_scripts', array($this, 'enqueue_dependencies_admin')); 
  21. remove_action('wp_ajax_woocommerce_add_order_note', array($this, 'woocommerce_add_order_note')); 
  22. add_action('wp_ajax_woocommerce_add_order_note', array($this, 'custom_add_order_note')); 
  23. add_action('wp_insert_comment', array($this, 'custom_wp_insert_comment'), 10, 2); 
  24.  
  25. /** 
  26. * Add action links under WordPress > Plugins 
  27. * @param $links 
  28. * @return array 
  29. */ 
  30. public function action_links($links) { 
  31.  
  32. $plugin_links = array( 
  33. '<a href="' . admin_url('admin.php?page=woocommerce&tab=order_notes_window') . '">' . __('Settings', 'woocommerce') . '</a>',  
  34. ); 
  35.  
  36. return array_merge($plugin_links, $links); 
  37.  
  38. protected function order_columns_script($post) { 
  39. global $woocommerce; 
  40. ?> 
  41.  
  42. <script 
  43. type='text/javascript'>jQuery('#the-list #post-<?php echo $post->ID; ?>').find('.order_comments a.post-com-count').attr("href", "#TB_inline?width=600&height=550&inlineId=order-notes-<?php echo $post->ID; ?>").addClass("thickbox"); 
  44.  
  45. jQuery(function () { 
  46.  
  47. jQuery('#order-notes-inside-<?php echo $post->ID; ?>') 
  48.  
  49. .on('click', 'a.add_note', function () { 
  50.  
  51. if (!jQuery('#order-notes-inside-<?php echo $post->ID; ?> textarea#add_order_note').val()) return; 
  52.  
  53. jQuery('#order-notes-inside-<?php echo $post->ID; ?>').block({ message: null, overlayCSS: { background: '#fff url(<?php echo $woocommerce->plugin_url(); ?>/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } }); 
  54.  
  55. var data = { 
  56. action: 'woocommerce_add_order_note',  
  57. post_id: '<?php echo $post->ID; ?>',  
  58. note: jQuery('#order-notes-inside-<?php echo $post->ID; ?> textarea#add_order_note').val(),  
  59. note_type: jQuery('#order-notes-inside-<?php echo $post->ID; ?> select#order_note_type').val(),  
  60. security: '<?php echo wp_create_nonce("add-order-note"); ?>' 
  61. }; 
  62.  
  63. jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function (response) { 
  64.  
  65. jQuery('#order-notes-inside-<?php echo $post->ID; ?> ul.order_notes').prepend(response); 
  66. jQuery('#order-notes-inside-<?php echo $post->ID; ?>').unblock(); 
  67. jQuery('#order-notes-inside-<?php echo $post->ID; ?> #add_order_note').val(''); 
  68.  
  69. countEl = jQuery('#the-list #post-<?php echo $post->ID; ?>').find('.comment-count'); 
  70. countEl.html(parseInt(countEl.html()) + 1); 
  71.  
  72. }); 
  73.  
  74. return false; 
  75.  
  76. }) 
  77.  
  78. .on('click', 'a.delete_note', function () { 
  79.  
  80. var note = jQuery(this).closest('li.note'); 
  81.  
  82. jQuery(note).block({ message: null, overlayCSS: { background: '#fff url(<?php echo $woocommerce->plugin_url(); ?>/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } }); 
  83.  
  84. var data = { 
  85. action: 'woocommerce_delete_order_note',  
  86. note_id: jQuery(note).attr('rel'),  
  87. security: '<?php echo wp_create_nonce("delete-order-note"); ?>' 
  88. }; 
  89.  
  90. jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function (response) { 
  91.  
  92. jQuery(note).remove(); 
  93.  
  94. countEl = jQuery('#the-list #post-<?php echo $post->ID; ?>').find('.comment-count'); 
  95. countEl.html(parseInt(countEl.html()) - 1); 
  96.  
  97. }); 
  98.  
  99. return false; 
  100.  
  101. }); 
  102.  
  103. }); 
  104.  
  105. </script> 
  106.  
  107. <?php 
  108.  
  109.  
  110. public function custom_order_columns($column) { 
  111. if ($column == 'order_comments') { 
  112.  
  113. global $post; 
  114.  
  115. $this->order_columns_script($post); 
  116.  
  117. ?> 
  118.  
  119. <div id="order-notes-<?php echo $post->ID; ?>" style="display:none;"> 
  120. <div> 
  121. <div id="order-notes-inside-<?php echo $post->ID; ?>"> 
  122. <?php $this->show_note($post->ID); ?> 
  123. </div> 
  124. </div> 
  125. </div> 
  126. <?php 
  127.  
  128.  
  129.  
  130. protected function show_note($id) { 
  131.  
  132. global $woocommerce; 
  133.  
  134. $args = array( 
  135. 'post_id' => $id,  
  136. 'approve' => 'approve',  
  137. 'type' => 'order_note' 
  138. ); 
  139.  
  140. $notes = get_comments($args); 
  141.  
  142. echo '<ul class="order_notes">'; 
  143.  
  144. if ($notes) { 
  145. foreach ($notes as $note) { 
  146. $note_classes = get_comment_meta($note->comment_ID, 'is_customer_note', true) ? array('customer-note', 'note') : array('note'); 
  147.  
  148. ?> 
  149. <li rel="<?php echo absint($note->comment_ID); ?>" 
  150. class="<?php echo implode(' ', $note_classes); ?>"> 
  151. <div class="note_content"> 
  152. <?php echo wpautop(wptexturize(wp_kses_post($note->comment_content))); ?> 
  153. </div> 
  154. <p class="meta"> 
  155. <?php printf(__('added %s ago', 'woocommerce'), human_time_diff(strtotime($note->comment_date_gmt), current_time('timestamp', 1))); ?> 
  156. by <?php echo $note->comment_author; ?> 
  157. <a href="#" class="delete_note"><?php _e('Delete note', 'woocommerce'); ?></a> 
  158. </p> 
  159. </li> 
  160. <?php 
  161. } else { 
  162. echo '<li>' . __('There are no notes for this order yet.', 'woocommerce') . '</li>'; 
  163.  
  164. echo '</ul>'; 
  165.  
  166. ?> 
  167.  
  168.  
  169. <div class="add_note"> 
  170. <h4><?php _e('Add note', 'woocommerce'); ?> <img class="help_tip" 
  171. data-tip='<?php esc_attr_e('Add a note for your reference, or add a customer note (the user will be notified).', 'woocommerce'); ?>' 
  172. src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" 
  173. height="16" width="16"/></h4> 
  174.  
  175. <p> 
  176. <textarea type="text" name="order_note" id="add_order_note" class="input-text" cols="20" 
  177. rows="5"></textarea> 
  178. </p> 
  179.  
  180. <p> 
  181. <select name="order_note_type" id="order_note_type"> 
  182. <option 
  183. value="customer" <?php if (get_option('t4m_default_note_type') == 'customer') echo 'selected' ?>><?php _e('Customer note', 'woocommerce'); ?></option> 
  184. <option 
  185. value="" <?php if (get_option('t4m_default_note_type') == '') echo 'selected' ?>><?php _e('Private note', 'woocommerce'); ?></option> 
  186. </select> 
  187. <a href="#" class="add_note button"><?php _e('Add', 'woocommerce'); ?></a> 
  188. </p> 
  189. </div> 
  190.  
  191. <?php 
  192.  
  193. public function custom_wp_insert_comment($id, $comment) { 
  194. if ($comment->comment_type == 'order_note') { 
  195. $current_user = wp_get_current_user(); 
  196. if (empty($current_user->user_firstname) && empty($current_user->user_lastname)) { 
  197. $comment->comment_author = $current_user->user_login; 
  198. } else { 
  199. $comment->comment_author = $current_user->user_firstname . (!empty($current_user->user_lastname) ? " " . $current_user->user_lastname : ""); 
  200. wp_update_comment(get_object_vars($comment)); 
  201.  
  202. public function custom_add_order_note() { 
  203. global $woocommerce; 
  204.  
  205. check_ajax_referer('add-order-note', 'security'); 
  206.  
  207. $post_id = (int)$_POST['post_id']; 
  208. $note = wp_kses_post(trim(stripslashes($_POST['note']))); 
  209. $note_type = $_POST['note_type']; 
  210.  
  211. $is_customer_note = $note_type == 'customer' ? 1 : 0; 
  212.  
  213. if ($post_id > 0) { 
  214. $order = new WC_Order($post_id); 
  215. $comment_id = $order->add_order_note($note, $is_customer_note); 
  216.  
  217. echo '<li rel="' . $comment_id . '" class="note '; 
  218. if ($is_customer_note) echo 'customer-note'; 
  219. echo '"><div class="note_content">'; 
  220. echo wpautop(wptexturize($note)); 
  221. echo '</div><p class="meta"> 
  222. by ' . get_comment_author($comment_id) . ' 
  223. <a href="#" class="delete_note">' . __('Delete note', 'woocommerce') . '</a></p>'; 
  224. echo '</li>'; 
  225.  
  226.  
  227. // Quit out 
  228. die(); 
  229.  
  230.  
  231. public function enqueue_dependencies_admin() { 
  232. wp_enqueue_script('jquery'); 
  233. wp_enqueue_style('woocommerce-order-notes-style-admin', plugins_url('assets/admin.css', __FILE__)); 
  234. add_thickbox(); 
  235.  
  236. /** 
  237. * @access public 
  238. * @return void 
  239. */ 
  240. public function add_tab() { 
  241. foreach ($this->settings_tabs as $name => $label) { 
  242. $class = 'nav-tab'; 
  243. if ($this->current_tab == $name) 
  244. $class .= ' nav-tab-active'; 
  245. echo '<a href="' . admin_url('admin.php?page=woocommerce&tab=' . $name) . '" class="' . $class . '">' . $label . '</a>'; 
  246.  
  247. /** 
  248. * @access public 
  249. * @return void 
  250. */ 
  251. public function settings_tab_action() { 
  252. global $woocommerce_settings; 
  253.  
  254. // Determine the current tab 
  255. $current_tab = $this->get_tab_in_view(current_filter(), 'woocommerce_settings_tabs_'); 
  256.  
  257. // Hook onto this from another function to keep things clean. 
  258. do_action('woocommerce_order_notes_window_settings'); 
  259.  
  260. woocommerce_admin_fields($woocommerce_settings[$current_tab]); 
  261.  
  262. /** 
  263. * Save settings in a single field in the database for each tab's fields (one field per tab). 
  264. */ 
  265. public function save_settings() { 
  266. global $woocommerce_settings; 
  267.  
  268. $this->add_settings_fields(); 
  269.  
  270. $current_tab = $this->get_tab_in_view(current_filter(), 'woocommerce_update_options_'); 
  271. woocommerce_update_options($woocommerce_settings[$current_tab]); 
  272.  
  273. /** 
  274. * Get the tab current in view/processing. 
  275. */ 
  276. public function get_tab_in_view($current_filter, $filter_base) { 
  277. return str_replace($filter_base, '', $current_filter); 
  278.  
  279.  
  280. /** 
  281. * Add settings fields for each tab. 
  282. */ 
  283. public function add_settings_fields() { 
  284. global $woocommerce_settings; 
  285.  
  286. // Load the prepared form fields. 
  287. $this->init_form_fields(); 
  288.  
  289. if (is_array($this->fields)) 
  290. foreach ($this->fields as $k => $v) 
  291. $woocommerce_settings[$k] = $v; 
  292.  
  293. /** 
  294. * Prepare form fields to be used in the various tabs. 
  295. */ 
  296. public function init_form_fields() { 
  297.  
  298. $this->fields['order_notes_window'] = apply_filters('woocommerce_bulk_discount_settings_fields', array( 
  299.  
  300. array('name' => __('Order notes', 'wc_order_notes_window'), 'type' => 'title', 'desc' => '', 'id' => 't4m_order_notes_options'),  
  301.  
  302. array( 
  303. 'title' => __('Default note type', 'wc_order_notes_window'),  
  304. 'id' => 't4m_default_note_type',  
  305. 'desc' => __('Note type which will be selected in the Notes order modal window by default.', 'wc_order_notes_window'),  
  306. 'desc_tip' => true,  
  307. 'std' => 'yes',  
  308. 'type' => 'select',  
  309. 'css' => 'min-width:200px;',  
  310. 'class' => 'chosen_select',  
  311. 'options' => array( 
  312. 'customer' => __('Customer note', 'woocommerce'),  
  313. '' => __('Private note', 'woocommerce') 
  314. ),  
  315.  
  316. array('type' => 'sectionend', 'id' => 't4m_order_notes_options'),  
  317.  
  318. array( 
  319. 'desc' => 'If you find the WooCommerce Order Notes Window extension useful, please rate it <a target="_blank" href="http://wordpress.org/support/view/plugin-reviews/woocommerce-order-notes-window#postform">★★★★★</a>.',  
  320. 'id' => 'wc_order_notes_window_notice_text',  
  321. 'type' => 'title' 
  322. ),  
  323.  
  324. array( 'type' => 'sectionend', 'id' => 'wc_order_notes_window_notice_text' ) 
  325.  
  326. ); 
  327.  
  328.