/woocommerce-delivery-notes.php

  1. <?php 
  2. /** 
  3. * Print order invoices & delivery notes for WooCommerce shop plugin. 
  4. * You can add company/shop info as well as personal notes & policies to print pages. 
  5. * 
  6. * Plugin Name: WooCommerce Print Invoices & Delivery Notes 
  7. * Plugin URI: https://github.com/piffpaffpuff/woocommerce-delivery-notes 
  8. * Description: Print order invoices & delivery notes for WooCommerce shop plugin. You can add company/shop info as well as personal notes & policies to print pages. 
  9. * Version: 2.0.2 
  10. * Author: Steve Clark, Triggvy Gunderson, David Decker 
  11. * Author URI: https://github.com/piffpaffpuff/woocommerce-delivery-notes 
  12. * License: GPLv3 or later 
  13. * License URI: http://www.opensource.org/licenses/gpl-license.php 
  14. * Text Domain: woocommerce-delivery-notes 
  15. * Domain Path: /languages/ 
  16. * 
  17. * Copyright 2011-2012 Steve Clark, Trigvvy Gunderson, David Decker - DECKERWEB 
  18. *  
  19. * This file is part of WooCommerce Print Invoices & Delivery Notes,  
  20. * a plugin for WordPress. 
  21. * 
  22. * WooCommerce Print Invoices & Delivery Notes is free software: 
  23. * You can redistribute it and/or modify it under the terms of the 
  24. * GNU General Public License as published by the Free Software 
  25. * Foundation, either version 2 of the License, or (at your option) 
  26. * any later version. 
  27. * 
  28. * WooCommerce Print Invoices & Delivery Notes is distributed in the hope that 
  29. * it will be useful, but WITHOUT ANY WARRANTY; without even the 
  30. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  31. * PURPOSE. See the GNU General Public License for more details. 
  32. * 
  33. * You should have received a copy of the GNU General Public License 
  34. * along with WordPress. If not, see <http://www.gnu.org/licenses/>. 
  35. */ 
  36.  
  37. /** 
  38. * Base class 
  39. */ 
  40. if ( !class_exists( 'WooCommerce_Delivery_Notes' ) ) { 
  41.  
  42. class WooCommerce_Delivery_Notes { 
  43.  
  44. public static $plugin_prefix; 
  45. public static $plugin_url; 
  46. public static $plugin_path; 
  47. public static $plugin_basefile; 
  48.  
  49. public $writepanel; 
  50. public $settings; 
  51. public $print; 
  52.  
  53. /** 
  54. * Constructor 
  55. */ 
  56. public function __construct() { 
  57. self::$plugin_prefix = 'wcdn_'; 
  58. self::$plugin_basefile = plugin_basename(__FILE__); 
  59. self::$plugin_url = plugin_dir_url(self::$plugin_basefile); 
  60. self::$plugin_path = trailingslashit(dirname(__FILE__)); 
  61.  
  62. /** 
  63. * Load the hooks 
  64. */ 
  65. public function load() { 
  66. // load the hooks 
  67. add_action( 'plugins_loaded', array($this, 'load_localisation') ); 
  68. add_action( 'init', array( $this, 'load_hooks' ) ); 
  69. add_action( 'admin_init', array( $this, 'load_admin_hooks' ) ); 
  70.  
  71. /** 
  72. * Load the main plugin classes and functions 
  73. */ 
  74. public function includes() { 
  75. include_once( 'classes/class-wcdn-writepanel.php' ); 
  76. include_once( 'classes/class-wcdn-settings.php' ); 
  77. include_once( 'classes/class-wcdn-print.php' ); 
  78.  
  79. /** 
  80. * Load the localisation  
  81. */ 
  82. public function load_localisation() {  
  83. load_plugin_textdomain( 'woocommerce-delivery-notes', false, dirname( self::$plugin_basefile ) . '/../../languages/woocommerce-delivery-notes/' ); 
  84. load_plugin_textdomain( 'woocommerce-delivery-notes', false, dirname( self::$plugin_basefile ) . '/languages' ); 
  85.  
  86. /** 
  87. * Load the init hooks 
  88. */ 
  89. public function load_hooks() {  
  90. if ( $this->is_woocommerce_activated() ) { 
  91. $this->includes(); 
  92. $this->writepanel = new WooCommerce_Delivery_Notes_Writepanel(); 
  93. $this->writepanel->load(); 
  94. $this->settings = new WooCommerce_Delivery_Notes_Settings(); 
  95. $this->settings->load(); 
  96. $this->print = new WooCommerce_Delivery_Notes_Print(); 
  97. $this->print->load(); 
  98.  
  99. /** 
  100. * Load the admin hooks 
  101. */ 
  102. public function load_admin_hooks() { 
  103. if ( $this->is_woocommerce_activated() ) { 
  104. add_filter( 'plugin_row_meta', array( $this, 'add_support_links' ), 10, 2 );  
  105. add_filter( 'plugin_action_links_' . self::$plugin_basefile, array( $this, 'add_settings_link') ); 
  106.  
  107. /** 
  108. * Add various support links to plugin page 
  109. */ 
  110. public function add_support_links( $links, $file ) { 
  111. if ( !current_user_can( 'install_plugins' ) ) { 
  112. return $links; 
  113.  
  114. if ( $file == WooCommerce_Delivery_Notes::$plugin_basefile ) { 
  115. $links[] = '<a href="http://wordpress.org/extend/plugins/woocommerce-delivery-notes/faq/" target="_blank" title="' . __( 'FAQ', 'woocommerce-delivery-notes' ) . '">' . __( 'FAQ', 'woocommerce-delivery-notes' ) . '</a>'; 
  116. $links[] = '<a href="http://wordpress.org/support/plugin/woocommerce-delivery-notes" target="_blank" title="' . __( 'Support', 'woocommerce-delivery-notes' ) . '">' . __( 'Support', 'woocommerce-delivery-notes' ) . '</a>'; 
  117. return $links; 
  118.  
  119. /** 
  120. * Add settings link to plugin page 
  121. */ 
  122. public function add_settings_link( $links ) { 
  123. $settings = sprintf( '<a href="%s" title="%s">%s</a>' , admin_url( 'admin.php?page=woocommerce&tab=' . $this->settings->tab_name ) , __( 'Go to the settings page', 'woocommerce-delivery-notes' ) , __( 'Settings', 'woocommerce-delivery-notes' ) ); 
  124. array_unshift( $links, $settings ); 
  125. return $links;  
  126.  
  127. /** 
  128. * Check if woocommerce is activated 
  129. */ 
  130. public function is_woocommerce_activated() { 
  131. $blog_plugins = get_option( 'active_plugins', array() ); 
  132. $site_plugins = get_site_option( 'active_sitewide_plugins', array() ); 
  133.  
  134. if ( in_array( 'woocommerce/woocommerce.php', $blog_plugins ) || isset( $site_plugins['woocommerce/woocommerce.php'] ) ) { 
  135. return true; 
  136. } else { 
  137. return false; 
  138.  
  139. /** 
  140. * Instance of plugin 
  141. */ 
  142. $wcdn = new WooCommerce_Delivery_Notes(); 
  143. $wcdn->load(); 
  144.  
  145. /** 
  146. * Public functions 
  147. */ 
  148.  
  149. /** 
  150. * Return Type of the print template 
  151. */ 
  152. if ( !function_exists( 'wcdn_get_template_type' ) ) { 
  153. function wcdn_get_template_type() { 
  154. global $wcdn; 
  155. return $wcdn->print->template_type; 
  156.  
  157. /** 
  158. * Show the template part 
  159. */ 
  160. if ( !function_exists( 'wcdn_get_template' ) ) { 
  161. function wcdn_get_template( $name ) { 
  162. global $wcdn; 
  163. $wcdn->print->get_template( $name ); 
  164.  
  165. /** 
  166. * Show template url 
  167. */ 
  168. if ( !function_exists( 'wcdn_stylesheet_url' ) ) { 
  169. function wcdn_stylesheet_url( $name ) { 
  170. global $wcdn; 
  171. echo apply_filters( 'wcdn_stylesheet_url', $wcdn->print->get_template_url( $name ) ); 
  172.  
  173. /** 
  174. * Show the template title depending on type 
  175. */ 
  176. if ( !function_exists( 'wcdn_template_title' ) ) { 
  177. function wcdn_template_title() { 
  178. if( wcdn_get_template_type() == 'invoice' ) { 
  179. echo apply_filters( 'wcdn_template_title', __( 'Invoice', 'woocommerce-delivery-notes' ) ); 
  180. } else { 
  181. echo apply_filters( 'wcdn_template_title', __( 'Delivery Note', 'woocommerce-delivery-notes' ) ); 
  182.  
  183. /** 
  184. * Create header 
  185. */ 
  186. if ( !function_exists( 'wcdn_head' ) ) { 
  187. function wcdn_head() { 
  188. ?> 
  189. <style> 
  190. #navigation { 
  191. font-family: sans-serif; 
  192. background-color: #f7f7f7; 
  193. z-index: 200; 
  194. border-bottom: 1px solid #dfdfdf; 
  195. left: 0; 
  196. right: 0; 
  197. top: 0; 
  198. position: fixed; 
  199. padding: 6px; 
  200.  
  201. #navigation .button { 
  202. display: inline-block; 
  203. text-decoration: none; 
  204. font-size: 12px; 
  205. line-height: 23px; 
  206. height: 24px; 
  207. margin: 0; 
  208. padding: 0 10px 1px; 
  209. cursor: pointer; 
  210. border-width: 1px; 
  211. border-style: solid; 
  212. -webkit-border-radius: 3px; 
  213. -webkit-appearance: none; 
  214. border-radius: 3px; 
  215. white-space: nowrap; 
  216. -webkit-box-sizing: border-box; 
  217. -moz-box-sizing: border-box; 
  218. box-sizing: border-box; 
  219.  
  220. background: #f3f3f3; 
  221. background-image: -webkit-gradient(linear, left top, left bottom, from(#fefefe), to(#f4f4f4)); 
  222. background-image: -webkit-linear-gradient(top, #fefefe, #f4f4f4); 
  223. background-image: -moz-linear-gradient(top, #fefefe, #f4f4f4); 
  224. background-image: -o-linear-gradient(top, #fefefe, #f4f4f4); 
  225. background-image: linear-gradient(to bottom, #fefefe, #f4f4f4); 
  226. border-color: #bbb; 
  227. color: #333; 
  228. text-shadow: 0 1px 0 #fff; 
  229.  
  230. #navigation .button:hover,  
  231. #navigation .button:focus { 
  232. background: #f3f3f3; 
  233. background-image: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#f3f3f3)); 
  234. background-image: -webkit-linear-gradient(top, #fff, #f3f3f3); 
  235. background-image: -moz-linear-gradient(top, #fff, #f3f3f3); 
  236. background-image: -ms-linear-gradient(top, #fff, #f3f3f3); 
  237. background-image: -o-linear-gradient(top, #fff, #f3f3f3); 
  238. background-image: linear-gradient(to bottom, #fff, #f3f3f3); 
  239. border-color: #999; 
  240. color: #222; 
  241.  
  242. #navigation .button:active { 
  243. background: #eee; 
  244. background-image: -webkit-gradient(linear, left top, left bottom, from(#f4f4f4), to(#fefefe)); 
  245. background-image: -webkit-linear-gradient(top, #f4f4f4, #fefefe); 
  246. background-image: -moz-linear-gradient(top, #f4f4f4, #fefefe); 
  247. background-image: -ms-linear-gradient(top, #f4f4f4, #fefefe); 
  248. background-image: -o-linear-gradient(top, #f4f4f4, #fefefe); 
  249. background-image: linear-gradient(to bottom, #f4f4f4, #fefefe); 
  250. border-color: #999; 
  251. color: #333; 
  252. text-shadow: 0 -1px 0 #fff; 
  253. -webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 ); 
  254. box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 ); 
  255.  
  256. @media print {  
  257. #navigation { 
  258. display: none; 
  259. </style> 
  260. <?php 
  261.  
  262. /** 
  263. * Create meta navigation 
  264. */ 
  265. if ( !function_exists( 'wcdn_navigation' ) ) { 
  266. function wcdn_navigation() { 
  267. ?> 
  268. <div id="navigation"> 
  269. <a href="#" class="button" onclick="window.print();return false;"><?php _e( 'Print', 'woocommerce-delivery-notes' ); ?></a> 
  270. </div> 
  271. <?php 
  272.  
  273. /** 
  274. * Return logo id 
  275. */ 
  276. if ( !function_exists( 'wcdn_get_company_logo_id' ) ) { 
  277. function wcdn_get_company_logo_id() { 
  278. global $wcdn; 
  279. return apply_filters( 'wcdn_company_logo_id', $wcdn->settings->get_setting( 'company_logo_image_id' ) ); 
  280.  
  281. /** 
  282. * Show logo html 
  283. */ 
  284. if ( !function_exists( 'wcdn_company_logo' ) ) { 
  285. function wcdn_company_logo() { 
  286. global $wcdn; 
  287. $attachment_id = wcdn_get_company_logo_id(); 
  288. $company = $wcdn->settings->get_setting( 'custom_company_name' ); 
  289. if( $attachment_id ) { 
  290. $attachment_src = wp_get_attachment_image_src( $attachment_id, 'full', false ); 
  291.  
  292. // resize the image to a 1/4 of the original size 
  293. // to have a printing point density of about 288ppi. 
  294. ?> 
  295. <img src="<?php echo $attachment_src[0]; ?>" width="<?php echo $attachment_src[1] / 4; ?>" height="<?php echo $attachment_src[2] / 4; ?>" alt="<?php echo esc_attr( $company ); ?>" /> 
  296. <?php 
  297.  
  298. /** 
  299. * Return default title name of Delivery Note  
  300. */ 
  301. if ( !function_exists( 'wcdn_company_name' ) ) { 
  302. function wcdn_company_name() { 
  303. global $wcdn; 
  304. $name = trim( $wcdn->settings->get_setting( 'custom_company_name' ) ); 
  305. if( !empty( $name ) ) { 
  306. echo apply_filters( 'wcdn_company_name', wpautop( wptexturize( $name ) ) ); 
  307. } else { 
  308. echo apply_filters( 'wcdn_company_name', get_bloginfo( 'name' ) ); 
  309.  
  310. /** 
  311. * Return shop/company info if provided 
  312. */ 
  313. if ( !function_exists( 'wcdn_company_info' ) ) { 
  314. function wcdn_company_info() { 
  315. global $wcdn; 
  316. echo wpautop( wptexturize( $wcdn->settings->get_setting( 'company_address' ) ) ); 
  317.  
  318. /** 
  319. * Show billing phone 
  320. */ 
  321. if ( !function_exists( 'wcdn_billing_phone' ) ) { 
  322. function wcdn_billing_phone() { 
  323. global $wcdn; 
  324. echo $wcdn->print->get_order()->billing_phone; 
  325.  
  326. /** 
  327. * Show billing email 
  328. */ 
  329. if ( !function_exists( 'wcdn_billing_email' ) ) { 
  330. function wcdn_billing_email() { 
  331. global $wcdn; 
  332. echo $wcdn->print->get_order()->billing_email; 
  333.  
  334. /** 
  335. * Show billing address 
  336. */ 
  337. if ( !function_exists( 'wcdn_billing_address' ) ) { 
  338. function wcdn_billing_address() { 
  339. global $wcdn; 
  340. $address = $wcdn->print->get_order()->get_formatted_billing_address(); 
  341. if( !$address ) { 
  342. $address = __('N/A', 'woocommerce-delivery-notes'); 
  343. echo apply_filters( 'wcdn_billing_address', $address ); 
  344.  
  345. /** 
  346. * Show shipping address 
  347. */ 
  348. if ( !function_exists( 'wcdn_shipping_address' ) ) { 
  349. function wcdn_shipping_address() { 
  350. global $wcdn; 
  351. $address = $wcdn->print->get_order()->get_formatted_shipping_address(); 
  352. if( !$address ) { 
  353. $address = __('N/A', 'woocommerce-delivery-notes'); 
  354. echo apply_filters( 'wcdn_shipping_address', $address ); 
  355.  
  356. /** 
  357. * Show the current date 
  358. */ 
  359. if ( !function_exists( 'wcdn_date' ) ) { 
  360. function wcdn_date() { 
  361. echo apply_filters( 'wcdn_date', date_i18n( get_option( 'date_format' ) ) ); 
  362.  
  363. /** 
  364. * Show payment method  
  365. */ 
  366. if ( !function_exists( 'wcdn_payment_method' ) ) { 
  367. function wcdn_payment_method() { 
  368. global $wcdn; 
  369. echo apply_filters( 'wcdn_payment_method', __( $wcdn->print->get_order()->payment_method_title, 'woocommerce' ) ); 
  370.  
  371. /** 
  372. * Get order 
  373. */ 
  374. if ( !function_exists( 'wcdn_get_order' ) ) { 
  375. function wcdn_get_order( $order_id = null ) { 
  376. global $wcdn; 
  377. return $wcdn->print->get_order( $order_id ); 
  378.  
  379. /** 
  380. * Get order custom field 
  381. */ 
  382. if ( !function_exists( 'wcdn_get_order_custom_field' ) ) { 
  383. function wcdn_get_order_custom_field( $field ) { 
  384. global $wcdn; 
  385. return $wcdn->print->get_order_field( $field ); 
  386.  
  387. /** 
  388. * Show order number 
  389. */ 
  390. if ( !function_exists( 'wcdn_order_number' ) ) { 
  391. function wcdn_order_number() { 
  392. global $wcdn; 
  393.  
  394. // Trim the hash to have a clean number but still  
  395. // support any filters that were applied before. 
  396. $order_number = ltrim($wcdn->print->get_order()->get_order_number(), '#'); 
  397. echo $order_number; 
  398.  
  399. /** 
  400. * Show the order date 
  401. */ 
  402. if ( !function_exists( 'wcdn_order_date' ) ) { 
  403. function wcdn_order_date() { 
  404. global $wcdn; 
  405. $order = $wcdn->print->get_order(); 
  406. echo apply_filters( 'wcdn_order_date', date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ) ); 
  407.  
  408. /** 
  409. * Return the order items 
  410. */ 
  411. if ( !function_exists( 'wcdn_get_order_items' ) ) { 
  412. function wcdn_get_order_items() { 
  413. global $wcdn; 
  414. return apply_filters( 'wcdn_order_items', $wcdn->print->get_order_items() ); 
  415.  
  416. /** 
  417. * Return the order totals listing 
  418. */ 
  419. if ( !function_exists( 'wcdn_get_order_totals' ) ) { 
  420. function wcdn_get_order_totals() { 
  421. global $wcdn;  
  422.  
  423. // get totals and remove the semicolon 
  424. $totals = apply_filters( 'wcdn_raw_order_totals', $wcdn->print->get_order()->get_order_item_totals() ); 
  425.  
  426. // remove the colon for every label 
  427. foreach ( $totals as $key => $total ) { 
  428. $label = $total['label']; 
  429. $colon = strrpos( $label, ':' ); 
  430. if( $colon !== false ) { 
  431. $label = substr_replace( $label, '', $colon, 1 ); 
  432. }  
  433. $totals[$key]['label'] = $label; 
  434.  
  435. return apply_filters( 'wcdn_order_totals', $totals ); 
  436.  
  437. /** 
  438. * Return has shipping notes 
  439. */ 
  440. if ( !function_exists( 'wcdn_get_shipping_notes' ) ) { 
  441. function wcdn_get_shipping_notes() { 
  442. global $wcdn; 
  443. return wpautop( wptexturize( $wcdn->print->get_order()->customer_note ) ); 
  444.  
  445. /** 
  446. * Show shipping notes 
  447. */ 
  448. if ( !function_exists( 'wcdn_shipping_notes' ) ) { 
  449. function wcdn_shipping_notes() { 
  450. global $wcdn; 
  451. echo wcdn_get_shipping_notes(); 
  452.  
  453. /** 
  454. * Return personal notes, season greetings etc. 
  455. */ 
  456. if ( !function_exists( 'wcdn_get_personal_notes' ) ) { 
  457. function wcdn_get_personal_notes() { 
  458. global $wcdn; 
  459. return wpautop( wptexturize( $wcdn->settings->get_setting( 'personal_notes' ) ) ); 
  460.  
  461. /** 
  462. * Show personal notes, season greetings etc. 
  463. */ 
  464. if ( !function_exists( 'wcdn_personal_notes' ) ) { 
  465. function wcdn_personal_notes() { 
  466. global $wcdn; 
  467. echo wcdn_get_personal_notes(); 
  468.  
  469. /** 
  470. * Return policy for returns 
  471. */ 
  472. if ( !function_exists( 'wcdn_get_policies_conditions' ) ) { 
  473. function wcdn_get_policies_conditions() { 
  474. global $wcdn; 
  475. return wpautop( wptexturize( $wcdn->settings->get_setting( 'policies_conditions' ) ) ); 
  476.  
  477. /** 
  478. * Show policy for returns 
  479. */ 
  480. if ( !function_exists( 'wcdn_policies_conditions' ) ) { 
  481. function wcdn_policies_conditions() { 
  482. global $wcdn; 
  483. echo wcdn_get_policies_conditions(); 
  484.  
  485. /** 
  486. * Return shop/company footer imprint, copyright etc. 
  487. */ 
  488. if ( !function_exists( 'wcdn_get_footer_imprint' ) ) { 
  489. function wcdn_get_footer_imprint() { 
  490. global $wcdn; 
  491. return wpautop( wptexturize( $wcdn->settings->get_setting( 'footer_imprint' ) ) ); 
  492.  
  493. /** 
  494. * Show shop/company footer imprint, copyright etc. 
  495. */ 
  496. if ( !function_exists( 'wcdn_footer_imprint' ) ) { 
  497. function wcdn_footer_imprint() { 
  498. global $wcdn; 
  499. echo wcdn_get_footer_imprint(); 
  500.  
  501. ?> 
.