-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-pdf-invoices-packingslips.php
executable file
·358 lines (297 loc) · 11.7 KB
/
woocommerce-pdf-invoices-packingslips.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/**
* Plugin Name: WooCommerce PDF Invoices & Packing Slips
* Plugin URI: http://www.wpovernight.com
* Description: Create, print & email PDF invoices & packing slips for WooCommerce orders.
* Version: 2.2.4
* Author: Ewout Fernhout
* Author URI: http://www.wpovernight.com
* License: GPLv2 or later
* License URI: http://www.opensource.org/licenses/gpl-license.php
* Text Domain: woocommerce-pdf-invoices-packing-slips
* WC requires at least: 2.2.0
* WC tested up to: 3.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( !class_exists( 'WPO_WCPDF' ) ) :
class WPO_WCPDF {
public $version = '2.2.4';
public $plugin_basename;
public $legacy_mode;
protected static $_instance = null;
/**
* Main Plugin Instance
*
* Ensures only one instance of plugin is loaded or can be loaded.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor
*/
public function __construct() {
$this->plugin_basename = plugin_basename(__FILE__);
$this->define( 'WPO_WCPDF_VERSION', $this->version );
// load the localisation & classes
add_action( 'plugins_loaded', array( $this, 'translations' ) );
add_filter( 'load_textdomain_mofile', array( $this, 'textdomain_fallback' ), 10, 2 );
add_action( 'plugins_loaded', array( $this, 'load_classes' ), 9 );
add_action( 'in_plugin_update_message-'.$this->plugin_basename, array( $this, 'in_plugin_update_message' ) );
}
/**
* Define constant if not already set
* @param string $name
* @param string|bool $value
*/
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Load the translation / textdomain files
*
* Note: the first-loaded translation file overrides any following ones if the same translation is present
*/
public function translations() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-pdf-invoices-packing-slips' );
$dir = trailingslashit( WP_LANG_DIR );
$textdomains = array( 'woocommerce-pdf-invoices-packing-slips' );
if ( $this->legacy_mode_enabled() === true ) {
$textdomains[] = 'wpo_wcpdf';
}
/**
* Frontend/global Locale. Looks in:
*
* - WP_LANG_DIR/woocommerce-pdf-invoices-packing-slips/woocommerce-pdf-invoices-packing-slips-LOCALE.mo
* - WP_LANG_DIR/plugins/woocommerce-pdf-invoices-packing-slips-LOCALE.mo
* - woocommerce-pdf-invoices-packing-slips-pro/languages/woocommerce-pdf-invoices-packing-slips-LOCALE.mo (which if not found falls back to:)
* - WP_LANG_DIR/plugins/woocommerce-pdf-invoices-packing-slips-LOCALE.mo
*/
foreach ( $textdomains as $textdomain ) {
unload_textdomain( $textdomain );
load_textdomain( $textdomain, $dir . 'woocommerce-pdf-invoices-packing-slips/woocommerce-pdf-invoices-packing-slips-' . $locale . '.mo' );
load_textdomain( $textdomain, $dir . 'plugins/woocommerce-pdf-invoices-packing-slips-' . $locale . '.mo' );
load_plugin_textdomain( $textdomain, false, dirname( plugin_basename(__FILE__) ) . '/languages' );
}
}
/**
* Maintain backwards compatibility with old translation files
* Uses old .mo file if it exists in any of the override locations
*/
public function textdomain_fallback( $mofile, $textdomain ) {
$plugin_domain = 'woocommerce-pdf-invoices-packing-slips';
$old_domain = 'wpo_wcpdf';
if ($textdomain == $old_domain) {
$textdomain = $plugin_domain;
$mofile = str_replace( "{$old_domain}-", "{$textdomain}-", $mofile ); // with trailing dash to target file and not folder
}
if ( $textdomain === $plugin_domain ) {
$old_mofile = str_replace( "{$textdomain}-", "{$old_domain}-", $mofile ); // with trailing dash to target file and not folder
if ( file_exists( $old_mofile ) ) {
// we have an old override - use it
return $old_mofile;
}
// prevent loading outdated language packs
$pofile = str_replace('.mo', '.po', $mofile);
if ( file_exists( $pofile ) ) {
// load po file
$podata = file_get_contents($pofile);
// set revision date threshold
$block_before = strtotime( '2017-05-15' );
// read revision date
preg_match('~PO-Revision-Date: (.*?)\\\n~s',$podata,$matches);
if (isset($matches[1])) {
$revision_date = $matches[1];
if ( $revision_timestamp = strtotime($revision_date) ) {
// check if revision is before threshold date
if ( $revision_timestamp < $block_before ) {
// try bundled
$bundled_file = $this->plugin_path() . '/languages/'. basename( $mofile );
if (file_exists($bundled_file)) {
return $bundled_file;
} else {
return '';
}
// delete po & mo file if possible
// @unlink($pofile);
// @unlink($mofile);
}
}
}
}
}
return $mofile;
}
/**
* Load the main plugin classes and functions
*/
public function includes() {
// WooCommerce compatibility classes
include_once( $this->plugin_path() . '/includes/compatibility/abstract-wc-data-compatibility.php' );
include_once( $this->plugin_path() . '/includes/compatibility/class-wc-date-compatibility.php' );
include_once( $this->plugin_path() . '/includes/compatibility/class-wc-core-compatibility.php' );
include_once( $this->plugin_path() . '/includes/compatibility/class-wc-order-compatibility.php' );
include_once( $this->plugin_path() . '/includes/compatibility/class-wc-product-compatibility.php' );
include_once( $this->plugin_path() . '/includes/compatibility/wc-datetime-functions-compatibility.php' );
// Third party compatibility
include_once( $this->plugin_path() . '/includes/compatibility/class-wcpdf-compatibility-third-party-plugins.php' );
// Plugin classes
include_once( $this->plugin_path() . '/includes/wcpdf-functions.php' );
$this->settings = include_once( $this->plugin_path() . '/includes/class-wcpdf-settings.php' );
$this->documents = include_once( $this->plugin_path() . '/includes/class-wcpdf-documents.php' );
$this->main = include_once( $this->plugin_path() . '/includes/class-wcpdf-main.php' );
include_once( $this->plugin_path() . '/includes/class-wcpdf-assets.php' );
include_once( $this->plugin_path() . '/includes/class-wcpdf-admin.php' );
include_once( $this->plugin_path() . '/includes/class-wcpdf-frontend.php' );
include_once( $this->plugin_path() . '/includes/class-wcpdf-install.php' );
// Backwards compatibility with self
include_once( $this->plugin_path() . '/includes/legacy/class-wcpdf-legacy.php' );
include_once( $this->plugin_path() . '/includes/legacy/class-wcpdf-legacy-deprecated-hooks.php' );
// PHP MB String fallback functions
include_once( $this->plugin_path() . '/includes/compatibility/mb-string-compatibility.php' );
}
/**
* Instantiate classes when woocommerce is activated
*/
public function load_classes() {
if ( $this->is_woocommerce_activated() === false ) {
add_action( 'admin_notices', array ( $this, 'need_woocommerce' ) );
return;
}
if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
add_action( 'admin_notices', array ( $this, 'required_php_version' ) );
return;
}
// all systems ready - GO!
$this->includes();
}
/**
* Check if legacy mode is enabled
*/
public function legacy_mode_enabled() {
if (!isset($this->legacy_mode)) {
$debug_settings = get_option( 'wpo_wcpdf_settings_debug' );
$this->legacy_mode = isset($debug_settings['legacy_mode']);
}
return $this->legacy_mode;
}
/**
* Check if woocommerce is activated
*/
public function is_woocommerce_activated() {
$blog_plugins = get_option( 'active_plugins', array() );
$site_plugins = is_multisite() ? (array) maybe_unserialize( get_site_option('active_sitewide_plugins' ) ) : array();
if ( in_array( 'woocommerce/woocommerce.php', $blog_plugins ) || isset( $site_plugins['woocommerce/woocommerce.php'] ) ) {
return true;
} else {
return false;
}
}
/**
* WooCommerce not active notice.
*
* @return string Fallack notice.
*/
public function need_woocommerce() {
$error = sprintf( __( 'WooCommerce PDF Invoices & Packing Slips requires %sWooCommerce%s to be installed & activated!' , 'woocommerce-pdf-invoices-packing-slips' ), '<a href="http://wordpress.org/extend/plugins/woocommerce/">', '</a>' );
$message = '<div class="error"><p>' . $error . '</p></div>';
echo $message;
}
/**
* PHP version requirement notice
*/
public function required_php_version() {
$error = __( 'WooCommerce PDF Invoices & Packing Slips requires PHP 5.3 or higher (5.6 or higher recommended).', 'woocommerce-pdf-invoices-packing-slips' );
$how_to_update = __( 'How to update your PHP version', 'woocommerce-pdf-invoices-packing-slips' );
$message = sprintf('<div class="error"><p>%s</p><p><a href="%s">%s</a></p></div>', $error, 'http://docs.wpovernight.com/general/how-to-update-your-php-version/', $how_to_update);
echo $message;
}
/**
* Show plugin changes. Code adapted from W3 Total Cache.
*/
public function in_plugin_update_message( $args ) {
$transient_name = 'wpo_wcpdf_upgrade_notice_' . $args['Version'];
if ( false === ( $upgrade_notice = get_transient( $transient_name ) ) ) {
$response = wp_safe_remote_get( 'https://plugins.svn.wordpress.org/woocommerce-pdf-invoices-packing-slips/trunk/readme.txt' );
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
$upgrade_notice = self::parse_update_notice( $response['body'], $args['new_version'] );
set_transient( $transient_name, $upgrade_notice, DAY_IN_SECONDS );
}
}
echo wp_kses_post( $upgrade_notice );
}
/**
* Parse update notice from readme file.
*
* @param string $content
* @param string $new_version
* @return string
*/
private function parse_update_notice( $content, $new_version ) {
// Output Upgrade Notice.
$matches = null;
$regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote( $new_version ) . '\s*=|$)~Uis';
$upgrade_notice = '';
if ( preg_match( $regexp, $content, $matches ) ) {
$notices = (array) preg_split( '~[\r\n]+~', trim( $matches[2] ) );
// Convert the full version strings to minor versions.
$notice_version_parts = explode( '.', trim( $matches[1] ) );
$current_version_parts = explode( '.', $this->version );
if ( 3 !== sizeof( $notice_version_parts ) ) {
return;
}
$notice_version = $notice_version_parts[0] . '.' . $notice_version_parts[1];
$current_version = $current_version_parts[0] . '.' . $current_version_parts[1];
// Check the latest stable version and ignore trunk.
if ( version_compare( $current_version, $notice_version, '<' ) ) {
$upgrade_notice .= '</p><p class="wpo_wcpdf_upgrade_notice">';
foreach ( $notices as $index => $line ) {
$upgrade_notice .= preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line );
}
}
}
return wp_kses_post( $upgrade_notice );
}
/**
* Get the plugin url.
* @return string
*/
public function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Get the plugin path.
* @return string
*/
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
} // class WPO_WCPDF
endif; // class_exists
/**
* Returns the main instance of WooCommerce PDF Invoices & Packing Slips to prevent the need to use globals.
*
* @since 1.6
* @return WPO_WCPDF
*/
function WPO_WCPDF() {
return WPO_WCPDF::instance();
}
WPO_WCPDF(); // load plugin
// legacy class for plugin detecting
if ( !class_exists( 'WooCommerce_PDF_Invoices' ) ) {
class WooCommerce_PDF_Invoices{
public static $version;
public function __construct() {
self::$version = WPO_WCPDF()->version;
}
}
new WooCommerce_PDF_Invoices();
}