@@ -76,7 +76,7 @@ |
||
76 | 76 | // This is for backwards compatibility. Do not remove except for a |
77 | 77 | // major version bump. |
78 | 78 | if (is_string($options)) { |
79 | - $options = array( 'host' => $options ); |
|
79 | + $options = array('host' => $options); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | if (!isset($options['host'])) { |
@@ -5,12 +5,12 @@ |
||
5 | 5 | // @codingStandardsIgnoreFile |
6 | 6 | |
7 | 7 | /** |
8 | - * This interface exists to provide backwards compatibility with PHP 5.3 |
|
9 | - * |
|
10 | - * This should _not_ be used by any third-party code. |
|
11 | - * |
|
12 | - * @ignore |
|
13 | - */ |
|
8 | + * This interface exists to provide backwards compatibility with PHP 5.3 |
|
9 | + * |
|
10 | + * This should _not_ be used by any third-party code. |
|
11 | + * |
|
12 | + * @ignore |
|
13 | + */ |
|
14 | 14 | if (interface_exists('JsonSerializable')) { |
15 | 15 | interface JsonSerializable extends \JsonSerializable |
16 | 16 | { |
@@ -9,780 +9,780 @@ |
||
9 | 9 | * @author Roman Ožana <[email protected]> |
10 | 10 | */ |
11 | 11 | class Emogrifier { |
12 | - /** |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - const ENCODING = 'UTF-8'; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var integer |
|
19 | - */ |
|
20 | - const CACHE_KEY_CSS = 0; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var integer |
|
24 | - */ |
|
25 | - const CACHE_KEY_SELECTOR = 1; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var integer |
|
29 | - */ |
|
30 | - const CACHE_KEY_XPATH = 2; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var integer |
|
34 | - */ |
|
35 | - const CACHE_KEY_CSS_DECLARATION_BLOCK = 3; |
|
36 | - |
|
37 | - /** |
|
38 | - * for calculating nth-of-type and nth-child selectors. |
|
39 | - * |
|
40 | - * @var integer |
|
41 | - */ |
|
42 | - const INDEX = 0; |
|
43 | - |
|
44 | - /** |
|
45 | - * for calculating nth-of-type and nth-child selectors. |
|
46 | - * |
|
47 | - * @var integer |
|
48 | - */ |
|
49 | - const MULTIPLIER = 1; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/'; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/'; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var string |
|
63 | - */ |
|
64 | - private $html = ''; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var string |
|
68 | - */ |
|
69 | - private $css = ''; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var array<string> |
|
73 | - */ |
|
74 | - private $unprocessableHtmlTags = array('wbr'); |
|
75 | - |
|
76 | - /** |
|
77 | - * @var array<array> |
|
78 | - */ |
|
79 | - private $caches = array( |
|
80 | - self::CACHE_KEY_CSS => array(), |
|
81 | - self::CACHE_KEY_SELECTOR => array(), |
|
82 | - self::CACHE_KEY_XPATH => array(), |
|
83 | - self::CACHE_KEY_CSS_DECLARATION_BLOCK => array(), |
|
84 | - ); |
|
85 | - |
|
86 | - /** |
|
87 | - * the visited nodes with the XPath paths as array keys. |
|
88 | - * |
|
89 | - * @var array<\DOMNode> |
|
90 | - */ |
|
91 | - private $visitedNodes = array(); |
|
92 | - |
|
93 | - /** |
|
94 | - * the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values. |
|
95 | - * as key/value pairs for the inner array. |
|
96 | - * |
|
97 | - * @var array<array><string> |
|
98 | - */ |
|
99 | - private $styleAttributesForNodes = array(); |
|
100 | - |
|
101 | - /** |
|
102 | - * This attribute applies to the case where you want to preserve your original text encoding. |
|
103 | - * |
|
104 | - * By default, emogrifier translates your text into HTML entities for two reasons: |
|
105 | - * |
|
106 | - * 1. Because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email. |
|
107 | - * |
|
108 | - * 2. It translates any illegal XML characters that DOMDocument cannot work with. |
|
109 | - * |
|
110 | - * If you would like to preserve your original encoding, set this attribute to TRUE. |
|
111 | - * |
|
112 | - * @var boolean |
|
113 | - */ |
|
114 | - public $preserveEncoding = false; |
|
115 | - |
|
116 | - public static $_media = ''; |
|
117 | - |
|
118 | - /** |
|
119 | - * The constructor. |
|
120 | - * |
|
121 | - * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
122 | - * @param string $css the CSS to merge, must be UTF-8-encoded |
|
123 | - */ |
|
124 | - public function __construct($html = '', $css = '') { |
|
125 | - $this->setHtml($html); |
|
126 | - $this->setCss($css); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * The destructor. |
|
131 | - */ |
|
132 | - public function __destruct() { |
|
133 | - $this->purgeVisitedNodes(); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Sets the HTML to emogrify. |
|
138 | - * |
|
139 | - * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
140 | - */ |
|
141 | - public function setHtml($html = '') { |
|
142 | - $this->html = $html; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Sets the CSS to merge with the HTML. |
|
147 | - * |
|
148 | - * @param string $css the CSS to merge, must be UTF-8-encoded |
|
149 | - */ |
|
150 | - public function setCss($css = '') { |
|
151 | - $this->css = $css; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Clears all caches. |
|
156 | - */ |
|
157 | - private function clearAllCaches() { |
|
158 | - $this->clearCache(self::CACHE_KEY_CSS); |
|
159 | - $this->clearCache(self::CACHE_KEY_SELECTOR); |
|
160 | - $this->clearCache(self::CACHE_KEY_XPATH); |
|
161 | - $this->clearCache(self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Clears a single cache by key. |
|
166 | - * |
|
167 | - * @param integer $key the cache key, must be CACHE_KEY_CSS, CACHE_KEY_SELECTOR, CACHE_KEY_XPATH or CACHE_KEY_CSS_DECLARATION_BLOCK |
|
168 | - * |
|
169 | - * @throws InvalidArgumentException |
|
170 | - */ |
|
171 | - private function clearCache($key) { |
|
172 | - $allowedCacheKeys = array(self::CACHE_KEY_CSS, self::CACHE_KEY_SELECTOR, self::CACHE_KEY_XPATH, self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
173 | - if (!in_array($key, $allowedCacheKeys, true)) { |
|
174 | - throw new InvalidArgumentException('Invalid cache key: ' . $key, 1391822035); |
|
175 | - } |
|
176 | - |
|
177 | - $this->caches[$key] = array(); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Purges the visited nodes. |
|
182 | - */ |
|
183 | - private function purgeVisitedNodes() { |
|
184 | - $this->visitedNodes = array(); |
|
185 | - $this->styleAttributesForNodes = array(); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Marks a tag for removal. |
|
190 | - * |
|
191 | - * There are some HTML tags that DOMDocument cannot process, and it will throw an error if it encounters them. |
|
192 | - * In particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document. |
|
193 | - * |
|
194 | - * Note: The tags will not be removed if they have any content. |
|
195 | - * |
|
196 | - * @param string $tagName the tag name, e.g., "p" |
|
197 | - */ |
|
198 | - public function addUnprocessableHtmlTag($tagName) { |
|
199 | - $this->unprocessableHtmlTags[] = $tagName; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Drops a tag from the removal list. |
|
204 | - * |
|
205 | - * @param string $tagName the tag name, e.g., "p" |
|
206 | - */ |
|
207 | - public function removeUnprocessableHtmlTag($tagName) { |
|
208 | - $key = array_search($tagName, $this->unprocessableHtmlTags, true); |
|
209 | - if ($key !== false) { |
|
210 | - unset($this->unprocessableHtmlTags[$key]); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Applies the CSS you submit to the HTML you submit. |
|
216 | - * |
|
217 | - * This method places the CSS inline. |
|
218 | - * |
|
219 | - * @return string |
|
220 | - * |
|
221 | - * @throws BadMethodCallException |
|
222 | - */ |
|
223 | - public function emogrify() { |
|
224 | - if ($this->html === '') { |
|
225 | - throw new BadMethodCallException('Please set some HTML first before calling emogrify.', 1390393096); |
|
226 | - } |
|
227 | - |
|
228 | - $xmlDocument = $this->createXmlDocument(); |
|
229 | - $xpath = new DOMXPath($xmlDocument); |
|
230 | - $this->clearAllCaches(); |
|
231 | - |
|
232 | - // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none'); |
|
233 | - // we wouldn't have to do this if DOMXPath supported XPath 2.0. |
|
234 | - // also store a reference of nodes with existing inline styles so we don't overwrite them |
|
235 | - $this->purgeVisitedNodes(); |
|
236 | - |
|
237 | - $nodesWithStyleAttributes = $xpath->query('//*[@style]'); |
|
238 | - if ($nodesWithStyleAttributes !== false) { |
|
239 | - /** @var $nodeWithStyleAttribute DOMNode */ |
|
240 | - foreach ($nodesWithStyleAttributes as $node) { |
|
241 | - $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
242 | - |
|
243 | - // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
|
244 | - $nodePath = $node->getNodePath(); |
|
245 | - if (!isset($this->styleAttributesForNodes[$nodePath])) { |
|
246 | - $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationBlock($normalizedOriginalStyle); |
|
247 | - $this->visitedNodes[$nodePath] = $node; |
|
248 | - } |
|
249 | - |
|
250 | - $node->setAttribute('style', $normalizedOriginalStyle); |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - // grab any existing style blocks from the html and append them to the existing CSS |
|
255 | - // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS) |
|
256 | - $allCss = $this->css; |
|
257 | - |
|
258 | - $allCss .= $this->getCssFromAllStyleNodes($xpath); |
|
259 | - |
|
260 | - $cssParts = $this->splitCssAndMediaQuery($allCss); |
|
261 | - self::$_media = ''; // reset |
|
262 | - |
|
263 | - $cssKey = md5($cssParts['css']); |
|
264 | - if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) { |
|
265 | - // process the CSS file for selectors and definitions |
|
266 | - preg_match_all('/(?:^|[\\s^{}]*)([^{]+){([^}]*)}/mis', $cssParts['css'], $matches, PREG_SET_ORDER); |
|
267 | - |
|
268 | - $allSelectors = array(); |
|
269 | - foreach ($matches as $key => $selectorString) { |
|
270 | - // if there is a blank definition, skip |
|
271 | - if (!strlen(trim($selectorString[2]))) { |
|
272 | - continue; |
|
273 | - } |
|
274 | - |
|
275 | - // else split by commas and duplicate attributes so we can sort by selector precedence |
|
276 | - $selectors = explode(',', $selectorString[1]); |
|
277 | - foreach ($selectors as $selector) { |
|
278 | - // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes |
|
279 | - if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) { |
|
280 | - continue; |
|
281 | - } |
|
282 | - |
|
283 | - $allSelectors[] = array('selector' => trim($selector), |
|
284 | - 'attributes' => trim($selectorString[2]), |
|
285 | - // keep track of where it appears in the file, since order is important |
|
286 | - 'line' => $key, |
|
287 | - ); |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - // now sort the selectors by precedence |
|
292 | - usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
293 | - |
|
294 | - $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
|
295 | - } |
|
296 | - |
|
297 | - foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) { |
|
298 | - // query the body for the xpath selector |
|
299 | - $nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector'])); |
|
300 | - |
|
301 | - /** @var $node \DOMNode */ |
|
302 | - foreach ($nodesMatchingCssSelectors as $node) { |
|
303 | - // if it has a style attribute, get it, process it, and append (overwrite) new stuff |
|
304 | - if ($node->hasAttribute('style')) { |
|
305 | - // break it up into an associative array |
|
306 | - $oldStyleDeclarations = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
307 | - } else { |
|
308 | - $oldStyleDeclarations = array(); |
|
309 | - } |
|
310 | - $newStyleDeclarations = $this->parseCssDeclarationBlock($value['attributes']); |
|
311 | - $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)); |
|
312 | - } |
|
313 | - } |
|
314 | - |
|
315 | - // now iterate through the nodes that contained inline styles in the original HTML |
|
316 | - foreach ($this->styleAttributesForNodes as $nodePath => $styleAttributesForNode) { |
|
317 | - $node = $this->visitedNodes[$nodePath]; |
|
318 | - $currentStyleAttributes = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
319 | - $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($currentStyleAttributes, $styleAttributesForNode)); |
|
320 | - } |
|
321 | - |
|
322 | - // This removes styles from your email that contain display:none. |
|
323 | - // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0, |
|
324 | - // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need |
|
325 | - // to translate() the letters that would be in 'NONE' ("NOE") to lowercase. |
|
326 | - $nodesWithStyleDisplayNone = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]'); |
|
327 | - // The checks on parentNode and is_callable below ensure that if we've deleted the parent node, |
|
328 | - // we don't try to call removeChild on a nonexistent child node |
|
329 | - if ($nodesWithStyleDisplayNone->length > 0) { |
|
330 | - /** @var $node \DOMNode */ |
|
331 | - foreach ($nodesWithStyleDisplayNone as $node) { |
|
332 | - if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
333 | - $node->parentNode->removeChild($node); |
|
334 | - } |
|
335 | - } |
|
336 | - } |
|
337 | - |
|
338 | - $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
|
339 | - |
|
340 | - if ($this->preserveEncoding) { |
|
341 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
342 | - return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
343 | - } else { |
|
344 | - return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
345 | - } |
|
346 | - } else { |
|
347 | - return $xmlDocument->saveHTML(); |
|
348 | - } |
|
349 | - } |
|
350 | - |
|
351 | - public function strtolower(array $m) { |
|
352 | - return strtolower($m[0]); |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * This method merges old or existing name/value array with new name/value array. |
|
358 | - * and then generates a string of the combined style suitable for placing inline. |
|
359 | - * This becomes the single point for CSS string generation allowing for consistent. |
|
360 | - * CSS output no matter where the CSS originally came from. |
|
361 | - * @param array $oldStyles |
|
362 | - * @param array $newStyles |
|
363 | - * @return string |
|
364 | - */ |
|
365 | - private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles) { |
|
366 | - $combinedStyles = array_merge($oldStyles, $newStyles); |
|
367 | - $style = ''; |
|
368 | - foreach ($combinedStyles as $attributeName => $attributeValue) { |
|
369 | - $style .= (strtolower(trim($attributeName)) . ': ' . trim($attributeValue) . '; '); |
|
370 | - } |
|
371 | - return trim($style); |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * Copies the media part from CSS array parts to $xmlDocument. |
|
377 | - * |
|
378 | - * @param array $cssParts |
|
379 | - * @param DOMDocument $xmlDocument |
|
380 | - */ |
|
381 | - public function copyCssWithMediaToStyleNode(array $cssParts, DOMDocument $xmlDocument) { |
|
382 | - if (isset($cssParts['media']) && $cssParts['media'] !== '') { |
|
383 | - $this->addStyleElementToDocument($xmlDocument, $cssParts['media']); |
|
384 | - } |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Returns CSS content. |
|
389 | - * |
|
390 | - * @param DOMXPath $xpath |
|
391 | - * @return string |
|
392 | - */ |
|
393 | - private function getCssFromAllStyleNodes(DOMXPath $xpath) { |
|
394 | - $styleNodes = $xpath->query('//style'); |
|
395 | - |
|
396 | - if ($styleNodes === false) { |
|
397 | - return ''; |
|
398 | - } |
|
399 | - |
|
400 | - $css = ''; |
|
401 | - /** @var $styleNode DOMNode */ |
|
402 | - foreach ($styleNodes as $styleNode) { |
|
403 | - $css .= "\n\n" . $styleNode->nodeValue; |
|
404 | - $styleNode->parentNode->removeChild($styleNode); |
|
405 | - } |
|
406 | - |
|
407 | - return $css; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Adds a style element with $css to $document. |
|
412 | - * |
|
413 | - * @param DOMDocument $document |
|
414 | - * @param string $css |
|
415 | - */ |
|
416 | - private function addStyleElementToDocument(DOMDocument $document, $css) { |
|
417 | - $styleElement = $document->createElement('style', $css); |
|
418 | - $styleAttribute = $document->createAttribute('type'); |
|
419 | - $styleAttribute->value = 'text/css'; |
|
420 | - $styleElement->appendChild($styleAttribute); |
|
421 | - |
|
422 | - $head = $this->getOrCreateHeadElement($document); |
|
423 | - $head->appendChild($styleElement); |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * Returns the existing or creates a new head element in $document. |
|
428 | - * |
|
429 | - * @param DOMDocument $document |
|
430 | - * @return DOMNode the head element |
|
431 | - */ |
|
432 | - private function getOrCreateHeadElement(DOMDocument $document) { |
|
433 | - $head = $document->getElementsByTagName('head')->item(0); |
|
434 | - |
|
435 | - if ($head === null) { |
|
436 | - $head = $document->createElement('head'); |
|
437 | - $html = $document->getElementsByTagName('html')->item(0); |
|
438 | - $html->insertBefore($head, $document->getElementsByTagName('body')->item(0)); |
|
439 | - } |
|
440 | - |
|
441 | - return $head; |
|
442 | - } |
|
443 | - |
|
444 | - /** |
|
445 | - * Splits input CSS code to an array where: |
|
446 | - * |
|
447 | - * - key "css" will be contains clean CSS code. |
|
448 | - * - key "media" will be contains all valuable media queries. |
|
449 | - * |
|
450 | - * Example: |
|
451 | - * |
|
452 | - * The CSS code. |
|
453 | - * |
|
454 | - * "@import "file.css"; h1 { color:red; } @media { h1 {}} @media tv { h1 {}}" |
|
455 | - * |
|
456 | - * will be parsed into the following array: |
|
457 | - * |
|
458 | - * "css" => "h1 { color:red; }" |
|
459 | - * "media" => "@media { h1 {}}" |
|
460 | - * |
|
461 | - * @param string $css |
|
462 | - * @return array |
|
463 | - */ |
|
464 | - private function splitCssAndMediaQuery($css) { |
|
465 | - $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
466 | - |
|
467 | - // filter the CSS |
|
468 | - $search = array( |
|
469 | - // get rid of css comment code |
|
470 | - '/\\/\\*.*\\*\\//sU', |
|
471 | - // strip out any import directives |
|
472 | - '/^\\s*@import\\s[^;]+;/misU', |
|
473 | - // strip remains media enclosures |
|
474 | - '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU', |
|
475 | - ); |
|
476 | - |
|
477 | - $replace = array( |
|
478 | - '', |
|
479 | - '', |
|
480 | - '', |
|
481 | - ); |
|
482 | - |
|
483 | - // clean CSS before output |
|
484 | - $css = preg_replace($search, $replace, $css); |
|
485 | - |
|
486 | - return array('css' => $css, 'media' => self::$_media); |
|
487 | - } |
|
488 | - |
|
489 | - private function _media_concat( $matches ) { |
|
490 | - self::$_media .= $matches[0]; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * Creates a DOMDocument instance with the current HTML. |
|
495 | - * |
|
496 | - * @return DOMDocument |
|
497 | - */ |
|
498 | - private function createXmlDocument() { |
|
499 | - $xmlDocument = new DOMDocument; |
|
500 | - $xmlDocument->encoding = self::ENCODING; |
|
501 | - $xmlDocument->strictErrorChecking = false; |
|
502 | - $xmlDocument->formatOutput = true; |
|
503 | - $libXmlState = libxml_use_internal_errors(true); |
|
504 | - $xmlDocument->loadHTML($this->getUnifiedHtml()); |
|
505 | - libxml_clear_errors(); |
|
506 | - libxml_use_internal_errors($libXmlState); |
|
507 | - $xmlDocument->normalizeDocument(); |
|
508 | - |
|
509 | - return $xmlDocument; |
|
510 | - } |
|
511 | - |
|
512 | - /** |
|
513 | - * Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed. |
|
514 | - * |
|
515 | - * @return string the unified HTML |
|
516 | - * |
|
517 | - * @throws BadMethodCallException |
|
518 | - */ |
|
519 | - private function getUnifiedHtml() { |
|
520 | - if (!empty($this->unprocessableHtmlTags)) { |
|
521 | - $unprocessableHtmlTags = implode('|', $this->unprocessableHtmlTags); |
|
522 | - $bodyWithoutUnprocessableTags = preg_replace('/<\\/?(' . $unprocessableHtmlTags . ')[^>]*>/i', '', $this->html); |
|
523 | - } else { |
|
524 | - $bodyWithoutUnprocessableTags = $this->html; |
|
525 | - } |
|
526 | - |
|
527 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
528 | - return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
529 | - } else { |
|
530 | - return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
531 | - } |
|
532 | - } |
|
533 | - |
|
534 | - /** |
|
535 | - * @param array $a |
|
536 | - * @param array $b |
|
537 | - * |
|
538 | - * @return integer |
|
539 | - */ |
|
540 | - private function sortBySelectorPrecedence(array $a, array $b) { |
|
541 | - $precedenceA = $this->getCssSelectorPrecedence($a['selector']); |
|
542 | - $precedenceB = $this->getCssSelectorPrecedence($b['selector']); |
|
543 | - |
|
544 | - // We want these sorted in ascending order so selectors with lesser precedence get processed first and |
|
545 | - // selectors with greater precedence get sorted last. |
|
546 | - // The parenthesis around the -1 are necessary to avoid a PHP_CodeSniffer warning about missing spaces around |
|
547 | - // arithmetic operators. |
|
548 | - // @see http://forge.typo3.org/issues/55605 |
|
549 | - $precedenceForEquals = ($a['line'] < $b['line'] ? (-1) : 1); |
|
550 | - $precedenceForNotEquals = ($precedenceA < $precedenceB ? (-1) : 1); |
|
551 | - return ($precedenceA === $precedenceB) ? $precedenceForEquals : $precedenceForNotEquals; |
|
552 | - } |
|
553 | - |
|
554 | - /** |
|
555 | - * @param string $selector |
|
556 | - * |
|
557 | - * @return integer |
|
558 | - */ |
|
559 | - private function getCssSelectorPrecedence($selector) { |
|
560 | - $selectorKey = md5($selector); |
|
561 | - if (!isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) { |
|
562 | - $precedence = 0; |
|
563 | - $value = 100; |
|
564 | - // ids: worth 100, classes: worth 10, elements: worth 1 |
|
565 | - $search = array('\\#','\\.',''); |
|
566 | - |
|
567 | - foreach ($search as $s) { |
|
568 | - if (trim($selector == '')) { |
|
569 | - break; |
|
570 | - } |
|
571 | - $number = 0; |
|
572 | - $selector = preg_replace('/' . $s . '\\w+/', '', $selector, -1, $number); |
|
573 | - $precedence += ($value * $number); |
|
574 | - $value /= 10; |
|
575 | - } |
|
576 | - $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey] = $precedence; |
|
577 | - } |
|
578 | - |
|
579 | - return $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey]; |
|
580 | - } |
|
581 | - |
|
582 | - /** |
|
583 | - * Right now, we support all CSS 1 selectors and most CSS2/3 selectors. |
|
584 | - * |
|
585 | - * @see http://plasmasturm.org/log/444/ |
|
586 | - * |
|
587 | - * @param string $paramCssSelector |
|
588 | - * |
|
589 | - * @return string |
|
590 | - */ |
|
591 | - private function translateCssToXpath($paramCssSelector) { |
|
592 | - $cssSelector = ' ' . $paramCssSelector . ' '; |
|
593 | - $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
594 | - $cssSelector = trim($cssSelector); |
|
595 | - $xpathKey = md5($cssSelector); |
|
596 | - if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
|
597 | - // returns an Xpath selector |
|
598 | - $search = array( |
|
599 | - // Matches any element that is a child of parent. |
|
600 | - '/\\s+>\\s+/', |
|
601 | - // Matches any element that is an adjacent sibling. |
|
602 | - '/\\s+\\+\\s+/', |
|
603 | - // Matches any element that is a descendant of an parent element element. |
|
604 | - '/\\s+/', |
|
605 | - // first-child pseudo-selector |
|
606 | - '/([^\\/]+):first-child/i', |
|
607 | - // last-child pseudo-selector |
|
608 | - '/([^\\/]+):last-child/i', |
|
609 | - // Matches attribute only selector |
|
610 | - '/^\\[(\\w+)\\]/', |
|
611 | - // Matches element with attribute |
|
612 | - '/(\\w)\\[(\\w+)\\]/', |
|
613 | - // Matches element with EXACT attribute |
|
614 | - '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/', |
|
615 | - ); |
|
616 | - $replace = array( |
|
617 | - '/', |
|
618 | - '/following-sibling::*[1]/self::', |
|
619 | - '//', |
|
620 | - '*[1]/self::\\1', |
|
621 | - '*[last()]/self::\\1', |
|
622 | - '*[@\\1]', |
|
623 | - '\\1[@\\2]', |
|
624 | - '\\1[@\\2="\\3"]', |
|
625 | - ); |
|
626 | - |
|
627 | - $cssSelector = '//' . preg_replace($search, $replace, $cssSelector); |
|
628 | - |
|
629 | - $cssSelector = preg_replace_callback(self::ID_ATTRIBUTE_MATCHER, array($this, 'matchIdAttributes'), $cssSelector); |
|
630 | - $cssSelector = preg_replace_callback(self::CLASS_ATTRIBUTE_MATCHER, array($this, 'matchClassAttributes'), $cssSelector); |
|
631 | - |
|
632 | - // Advanced selectors are going to require a bit more advanced emogrification. |
|
633 | - // When we required PHP 5.3, we could do this with closures. |
|
634 | - $cssSelector = preg_replace_callback( |
|
635 | - '/([^\\/]+):nth-child\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
636 | - array($this, 'translateNthChild'), $cssSelector |
|
637 | - ); |
|
638 | - $cssSelector = preg_replace_callback( |
|
639 | - '/([^\\/]+):nth-of-type\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
640 | - array($this, 'translateNthOfType'), $cssSelector |
|
641 | - ); |
|
642 | - |
|
643 | - $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey] = $cssSelector; |
|
644 | - } |
|
645 | - return $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey]; |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * @param array $match |
|
650 | - * |
|
651 | - * @return string |
|
652 | - */ |
|
653 | - private function matchIdAttributes(array $match) { |
|
654 | - return (strlen($match[1]) ? $match[1] : '*') . '[@id="' . $match[2] . '"]'; |
|
655 | - } |
|
656 | - |
|
657 | - /** |
|
658 | - * @param array $match |
|
659 | - * |
|
660 | - * @return string |
|
661 | - */ |
|
662 | - private function matchClassAttributes(array $match) { |
|
663 | - return (strlen($match[1]) ? $match[1] : '*') . '[contains(concat(" ",@class," "),concat(" ","' . |
|
664 | - implode( |
|
665 | - '"," "))][contains(concat(" ",@class," "),concat(" ","', |
|
666 | - explode('.', substr($match[2], 1)) |
|
667 | - ) . '"," "))]'; |
|
668 | - } |
|
669 | - |
|
670 | - /** |
|
671 | - * @param array $match |
|
672 | - * |
|
673 | - * @return string |
|
674 | - */ |
|
675 | - private function translateNthChild(array $match) { |
|
676 | - $result = $this->parseNth($match); |
|
677 | - |
|
678 | - if (isset($result[self::MULTIPLIER])) { |
|
679 | - if ($result[self::MULTIPLIER] < 0) { |
|
680 | - $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
681 | - return sprintf('*[(last() - position()) mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
682 | - } else { |
|
683 | - return sprintf('*[position() mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
684 | - } |
|
685 | - } else { |
|
686 | - return sprintf('*[%u]/self::%s', $result[self::INDEX], $match[1]); |
|
687 | - } |
|
688 | - } |
|
689 | - |
|
690 | - /** |
|
691 | - * @param array $match |
|
692 | - * |
|
693 | - * @return string |
|
694 | - */ |
|
695 | - private function translateNthOfType(array $match) { |
|
696 | - $result = $this->parseNth($match); |
|
697 | - |
|
698 | - if (isset($result[self::MULTIPLIER])) { |
|
699 | - if ($result[self::MULTIPLIER] < 0) { |
|
700 | - $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
701 | - return sprintf('%s[(last() - position()) mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
702 | - } else { |
|
703 | - return sprintf('%s[position() mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
704 | - } |
|
705 | - } else { |
|
706 | - return sprintf('%s[%u]', $match[1], $result[self::INDEX]); |
|
707 | - } |
|
708 | - } |
|
709 | - |
|
710 | - /** |
|
711 | - * @param array $match |
|
712 | - * |
|
713 | - * @return array |
|
714 | - */ |
|
715 | - private function parseNth(array $match) { |
|
716 | - if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
717 | - $index = strtolower($match[2]) == 'even' ? 0 : 1; |
|
718 | - return array(self::MULTIPLIER => 2, self::INDEX => $index); |
|
719 | - } elseif (stripos($match[2], 'n') === false) { |
|
720 | - // if there is a multiplier |
|
721 | - $index = intval(str_replace(' ', '', $match[2])); |
|
722 | - return array(self::INDEX => $index); |
|
723 | - } else { |
|
724 | - if (isset($match[3])) { |
|
725 | - $multipleTerm = str_replace($match[3], '', $match[2]); |
|
726 | - $index = intval(str_replace(' ', '', $match[3])); |
|
727 | - } else { |
|
728 | - $multipleTerm = $match[2]; |
|
729 | - $index = 0; |
|
730 | - } |
|
731 | - |
|
732 | - $multiplier = str_ireplace('n', '', $multipleTerm); |
|
733 | - |
|
734 | - if (!strlen($multiplier)) { |
|
735 | - $multiplier = 1; |
|
736 | - } elseif ($multiplier == 0) { |
|
737 | - return array(self::INDEX => $index); |
|
738 | - } else { |
|
739 | - $multiplier = intval($multiplier); |
|
740 | - } |
|
741 | - |
|
742 | - while ($index < 0) { |
|
743 | - $index += abs($multiplier); |
|
744 | - } |
|
745 | - |
|
746 | - return array(self::MULTIPLIER => $multiplier, self::INDEX => $index); |
|
747 | - } |
|
748 | - } |
|
749 | - |
|
750 | - /** |
|
751 | - * Parses a CSS declaration block into property name/value pairs. |
|
752 | - * |
|
753 | - * Example: |
|
754 | - * |
|
755 | - * The declaration block. |
|
756 | - * |
|
757 | - * "color: #000; font-weight: bold;". |
|
758 | - * |
|
759 | - * will be parsed into the following array: |
|
760 | - * |
|
761 | - * "color" => "#000" |
|
762 | - * "font-weight" => "bold" |
|
763 | - * |
|
764 | - * @param string $cssDeclarationBlock the CSS declaration block without the curly braces, may be empty |
|
765 | - * |
|
766 | - * @return array the CSS declarations with the property names as array keys and the property values as array values |
|
767 | - */ |
|
768 | - private function parseCssDeclarationBlock($cssDeclarationBlock) { |
|
769 | - if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock])) { |
|
770 | - return $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock]; |
|
771 | - } |
|
772 | - |
|
773 | - $properties = array(); |
|
774 | - $declarations = explode(';', $cssDeclarationBlock); |
|
775 | - foreach ($declarations as $declaration) { |
|
776 | - $matches = array(); |
|
777 | - if (!preg_match('/ *([A-Za-z\\-]+) *: *([^;]+) */', $declaration, $matches)) { |
|
778 | - continue; |
|
779 | - } |
|
780 | - $propertyName = strtolower($matches[1]); |
|
781 | - $propertyValue = $matches[2]; |
|
782 | - $properties[$propertyName] = $propertyValue; |
|
783 | - } |
|
784 | - $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock] = $properties; |
|
785 | - |
|
786 | - return $properties; |
|
787 | - } |
|
12 | + /** |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + const ENCODING = 'UTF-8'; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var integer |
|
19 | + */ |
|
20 | + const CACHE_KEY_CSS = 0; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var integer |
|
24 | + */ |
|
25 | + const CACHE_KEY_SELECTOR = 1; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var integer |
|
29 | + */ |
|
30 | + const CACHE_KEY_XPATH = 2; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var integer |
|
34 | + */ |
|
35 | + const CACHE_KEY_CSS_DECLARATION_BLOCK = 3; |
|
36 | + |
|
37 | + /** |
|
38 | + * for calculating nth-of-type and nth-child selectors. |
|
39 | + * |
|
40 | + * @var integer |
|
41 | + */ |
|
42 | + const INDEX = 0; |
|
43 | + |
|
44 | + /** |
|
45 | + * for calculating nth-of-type and nth-child selectors. |
|
46 | + * |
|
47 | + * @var integer |
|
48 | + */ |
|
49 | + const MULTIPLIER = 1; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/'; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/'; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var string |
|
63 | + */ |
|
64 | + private $html = ''; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var string |
|
68 | + */ |
|
69 | + private $css = ''; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var array<string> |
|
73 | + */ |
|
74 | + private $unprocessableHtmlTags = array('wbr'); |
|
75 | + |
|
76 | + /** |
|
77 | + * @var array<array> |
|
78 | + */ |
|
79 | + private $caches = array( |
|
80 | + self::CACHE_KEY_CSS => array(), |
|
81 | + self::CACHE_KEY_SELECTOR => array(), |
|
82 | + self::CACHE_KEY_XPATH => array(), |
|
83 | + self::CACHE_KEY_CSS_DECLARATION_BLOCK => array(), |
|
84 | + ); |
|
85 | + |
|
86 | + /** |
|
87 | + * the visited nodes with the XPath paths as array keys. |
|
88 | + * |
|
89 | + * @var array<\DOMNode> |
|
90 | + */ |
|
91 | + private $visitedNodes = array(); |
|
92 | + |
|
93 | + /** |
|
94 | + * the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values. |
|
95 | + * as key/value pairs for the inner array. |
|
96 | + * |
|
97 | + * @var array<array><string> |
|
98 | + */ |
|
99 | + private $styleAttributesForNodes = array(); |
|
100 | + |
|
101 | + /** |
|
102 | + * This attribute applies to the case where you want to preserve your original text encoding. |
|
103 | + * |
|
104 | + * By default, emogrifier translates your text into HTML entities for two reasons: |
|
105 | + * |
|
106 | + * 1. Because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email. |
|
107 | + * |
|
108 | + * 2. It translates any illegal XML characters that DOMDocument cannot work with. |
|
109 | + * |
|
110 | + * If you would like to preserve your original encoding, set this attribute to TRUE. |
|
111 | + * |
|
112 | + * @var boolean |
|
113 | + */ |
|
114 | + public $preserveEncoding = false; |
|
115 | + |
|
116 | + public static $_media = ''; |
|
117 | + |
|
118 | + /** |
|
119 | + * The constructor. |
|
120 | + * |
|
121 | + * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
122 | + * @param string $css the CSS to merge, must be UTF-8-encoded |
|
123 | + */ |
|
124 | + public function __construct($html = '', $css = '') { |
|
125 | + $this->setHtml($html); |
|
126 | + $this->setCss($css); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * The destructor. |
|
131 | + */ |
|
132 | + public function __destruct() { |
|
133 | + $this->purgeVisitedNodes(); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Sets the HTML to emogrify. |
|
138 | + * |
|
139 | + * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
140 | + */ |
|
141 | + public function setHtml($html = '') { |
|
142 | + $this->html = $html; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Sets the CSS to merge with the HTML. |
|
147 | + * |
|
148 | + * @param string $css the CSS to merge, must be UTF-8-encoded |
|
149 | + */ |
|
150 | + public function setCss($css = '') { |
|
151 | + $this->css = $css; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Clears all caches. |
|
156 | + */ |
|
157 | + private function clearAllCaches() { |
|
158 | + $this->clearCache(self::CACHE_KEY_CSS); |
|
159 | + $this->clearCache(self::CACHE_KEY_SELECTOR); |
|
160 | + $this->clearCache(self::CACHE_KEY_XPATH); |
|
161 | + $this->clearCache(self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Clears a single cache by key. |
|
166 | + * |
|
167 | + * @param integer $key the cache key, must be CACHE_KEY_CSS, CACHE_KEY_SELECTOR, CACHE_KEY_XPATH or CACHE_KEY_CSS_DECLARATION_BLOCK |
|
168 | + * |
|
169 | + * @throws InvalidArgumentException |
|
170 | + */ |
|
171 | + private function clearCache($key) { |
|
172 | + $allowedCacheKeys = array(self::CACHE_KEY_CSS, self::CACHE_KEY_SELECTOR, self::CACHE_KEY_XPATH, self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
173 | + if (!in_array($key, $allowedCacheKeys, true)) { |
|
174 | + throw new InvalidArgumentException('Invalid cache key: ' . $key, 1391822035); |
|
175 | + } |
|
176 | + |
|
177 | + $this->caches[$key] = array(); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Purges the visited nodes. |
|
182 | + */ |
|
183 | + private function purgeVisitedNodes() { |
|
184 | + $this->visitedNodes = array(); |
|
185 | + $this->styleAttributesForNodes = array(); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Marks a tag for removal. |
|
190 | + * |
|
191 | + * There are some HTML tags that DOMDocument cannot process, and it will throw an error if it encounters them. |
|
192 | + * In particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document. |
|
193 | + * |
|
194 | + * Note: The tags will not be removed if they have any content. |
|
195 | + * |
|
196 | + * @param string $tagName the tag name, e.g., "p" |
|
197 | + */ |
|
198 | + public function addUnprocessableHtmlTag($tagName) { |
|
199 | + $this->unprocessableHtmlTags[] = $tagName; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Drops a tag from the removal list. |
|
204 | + * |
|
205 | + * @param string $tagName the tag name, e.g., "p" |
|
206 | + */ |
|
207 | + public function removeUnprocessableHtmlTag($tagName) { |
|
208 | + $key = array_search($tagName, $this->unprocessableHtmlTags, true); |
|
209 | + if ($key !== false) { |
|
210 | + unset($this->unprocessableHtmlTags[$key]); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Applies the CSS you submit to the HTML you submit. |
|
216 | + * |
|
217 | + * This method places the CSS inline. |
|
218 | + * |
|
219 | + * @return string |
|
220 | + * |
|
221 | + * @throws BadMethodCallException |
|
222 | + */ |
|
223 | + public function emogrify() { |
|
224 | + if ($this->html === '') { |
|
225 | + throw new BadMethodCallException('Please set some HTML first before calling emogrify.', 1390393096); |
|
226 | + } |
|
227 | + |
|
228 | + $xmlDocument = $this->createXmlDocument(); |
|
229 | + $xpath = new DOMXPath($xmlDocument); |
|
230 | + $this->clearAllCaches(); |
|
231 | + |
|
232 | + // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none'); |
|
233 | + // we wouldn't have to do this if DOMXPath supported XPath 2.0. |
|
234 | + // also store a reference of nodes with existing inline styles so we don't overwrite them |
|
235 | + $this->purgeVisitedNodes(); |
|
236 | + |
|
237 | + $nodesWithStyleAttributes = $xpath->query('//*[@style]'); |
|
238 | + if ($nodesWithStyleAttributes !== false) { |
|
239 | + /** @var $nodeWithStyleAttribute DOMNode */ |
|
240 | + foreach ($nodesWithStyleAttributes as $node) { |
|
241 | + $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
242 | + |
|
243 | + // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
|
244 | + $nodePath = $node->getNodePath(); |
|
245 | + if (!isset($this->styleAttributesForNodes[$nodePath])) { |
|
246 | + $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationBlock($normalizedOriginalStyle); |
|
247 | + $this->visitedNodes[$nodePath] = $node; |
|
248 | + } |
|
249 | + |
|
250 | + $node->setAttribute('style', $normalizedOriginalStyle); |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + // grab any existing style blocks from the html and append them to the existing CSS |
|
255 | + // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS) |
|
256 | + $allCss = $this->css; |
|
257 | + |
|
258 | + $allCss .= $this->getCssFromAllStyleNodes($xpath); |
|
259 | + |
|
260 | + $cssParts = $this->splitCssAndMediaQuery($allCss); |
|
261 | + self::$_media = ''; // reset |
|
262 | + |
|
263 | + $cssKey = md5($cssParts['css']); |
|
264 | + if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) { |
|
265 | + // process the CSS file for selectors and definitions |
|
266 | + preg_match_all('/(?:^|[\\s^{}]*)([^{]+){([^}]*)}/mis', $cssParts['css'], $matches, PREG_SET_ORDER); |
|
267 | + |
|
268 | + $allSelectors = array(); |
|
269 | + foreach ($matches as $key => $selectorString) { |
|
270 | + // if there is a blank definition, skip |
|
271 | + if (!strlen(trim($selectorString[2]))) { |
|
272 | + continue; |
|
273 | + } |
|
274 | + |
|
275 | + // else split by commas and duplicate attributes so we can sort by selector precedence |
|
276 | + $selectors = explode(',', $selectorString[1]); |
|
277 | + foreach ($selectors as $selector) { |
|
278 | + // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes |
|
279 | + if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) { |
|
280 | + continue; |
|
281 | + } |
|
282 | + |
|
283 | + $allSelectors[] = array('selector' => trim($selector), |
|
284 | + 'attributes' => trim($selectorString[2]), |
|
285 | + // keep track of where it appears in the file, since order is important |
|
286 | + 'line' => $key, |
|
287 | + ); |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + // now sort the selectors by precedence |
|
292 | + usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
293 | + |
|
294 | + $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
|
295 | + } |
|
296 | + |
|
297 | + foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) { |
|
298 | + // query the body for the xpath selector |
|
299 | + $nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector'])); |
|
300 | + |
|
301 | + /** @var $node \DOMNode */ |
|
302 | + foreach ($nodesMatchingCssSelectors as $node) { |
|
303 | + // if it has a style attribute, get it, process it, and append (overwrite) new stuff |
|
304 | + if ($node->hasAttribute('style')) { |
|
305 | + // break it up into an associative array |
|
306 | + $oldStyleDeclarations = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
307 | + } else { |
|
308 | + $oldStyleDeclarations = array(); |
|
309 | + } |
|
310 | + $newStyleDeclarations = $this->parseCssDeclarationBlock($value['attributes']); |
|
311 | + $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)); |
|
312 | + } |
|
313 | + } |
|
314 | + |
|
315 | + // now iterate through the nodes that contained inline styles in the original HTML |
|
316 | + foreach ($this->styleAttributesForNodes as $nodePath => $styleAttributesForNode) { |
|
317 | + $node = $this->visitedNodes[$nodePath]; |
|
318 | + $currentStyleAttributes = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
319 | + $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($currentStyleAttributes, $styleAttributesForNode)); |
|
320 | + } |
|
321 | + |
|
322 | + // This removes styles from your email that contain display:none. |
|
323 | + // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0, |
|
324 | + // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need |
|
325 | + // to translate() the letters that would be in 'NONE' ("NOE") to lowercase. |
|
326 | + $nodesWithStyleDisplayNone = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]'); |
|
327 | + // The checks on parentNode and is_callable below ensure that if we've deleted the parent node, |
|
328 | + // we don't try to call removeChild on a nonexistent child node |
|
329 | + if ($nodesWithStyleDisplayNone->length > 0) { |
|
330 | + /** @var $node \DOMNode */ |
|
331 | + foreach ($nodesWithStyleDisplayNone as $node) { |
|
332 | + if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
333 | + $node->parentNode->removeChild($node); |
|
334 | + } |
|
335 | + } |
|
336 | + } |
|
337 | + |
|
338 | + $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
|
339 | + |
|
340 | + if ($this->preserveEncoding) { |
|
341 | + if ( function_exists( 'mb_convert_encoding' ) ) { |
|
342 | + return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
343 | + } else { |
|
344 | + return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
345 | + } |
|
346 | + } else { |
|
347 | + return $xmlDocument->saveHTML(); |
|
348 | + } |
|
349 | + } |
|
350 | + |
|
351 | + public function strtolower(array $m) { |
|
352 | + return strtolower($m[0]); |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * This method merges old or existing name/value array with new name/value array. |
|
358 | + * and then generates a string of the combined style suitable for placing inline. |
|
359 | + * This becomes the single point for CSS string generation allowing for consistent. |
|
360 | + * CSS output no matter where the CSS originally came from. |
|
361 | + * @param array $oldStyles |
|
362 | + * @param array $newStyles |
|
363 | + * @return string |
|
364 | + */ |
|
365 | + private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles) { |
|
366 | + $combinedStyles = array_merge($oldStyles, $newStyles); |
|
367 | + $style = ''; |
|
368 | + foreach ($combinedStyles as $attributeName => $attributeValue) { |
|
369 | + $style .= (strtolower(trim($attributeName)) . ': ' . trim($attributeValue) . '; '); |
|
370 | + } |
|
371 | + return trim($style); |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * Copies the media part from CSS array parts to $xmlDocument. |
|
377 | + * |
|
378 | + * @param array $cssParts |
|
379 | + * @param DOMDocument $xmlDocument |
|
380 | + */ |
|
381 | + public function copyCssWithMediaToStyleNode(array $cssParts, DOMDocument $xmlDocument) { |
|
382 | + if (isset($cssParts['media']) && $cssParts['media'] !== '') { |
|
383 | + $this->addStyleElementToDocument($xmlDocument, $cssParts['media']); |
|
384 | + } |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Returns CSS content. |
|
389 | + * |
|
390 | + * @param DOMXPath $xpath |
|
391 | + * @return string |
|
392 | + */ |
|
393 | + private function getCssFromAllStyleNodes(DOMXPath $xpath) { |
|
394 | + $styleNodes = $xpath->query('//style'); |
|
395 | + |
|
396 | + if ($styleNodes === false) { |
|
397 | + return ''; |
|
398 | + } |
|
399 | + |
|
400 | + $css = ''; |
|
401 | + /** @var $styleNode DOMNode */ |
|
402 | + foreach ($styleNodes as $styleNode) { |
|
403 | + $css .= "\n\n" . $styleNode->nodeValue; |
|
404 | + $styleNode->parentNode->removeChild($styleNode); |
|
405 | + } |
|
406 | + |
|
407 | + return $css; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Adds a style element with $css to $document. |
|
412 | + * |
|
413 | + * @param DOMDocument $document |
|
414 | + * @param string $css |
|
415 | + */ |
|
416 | + private function addStyleElementToDocument(DOMDocument $document, $css) { |
|
417 | + $styleElement = $document->createElement('style', $css); |
|
418 | + $styleAttribute = $document->createAttribute('type'); |
|
419 | + $styleAttribute->value = 'text/css'; |
|
420 | + $styleElement->appendChild($styleAttribute); |
|
421 | + |
|
422 | + $head = $this->getOrCreateHeadElement($document); |
|
423 | + $head->appendChild($styleElement); |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * Returns the existing or creates a new head element in $document. |
|
428 | + * |
|
429 | + * @param DOMDocument $document |
|
430 | + * @return DOMNode the head element |
|
431 | + */ |
|
432 | + private function getOrCreateHeadElement(DOMDocument $document) { |
|
433 | + $head = $document->getElementsByTagName('head')->item(0); |
|
434 | + |
|
435 | + if ($head === null) { |
|
436 | + $head = $document->createElement('head'); |
|
437 | + $html = $document->getElementsByTagName('html')->item(0); |
|
438 | + $html->insertBefore($head, $document->getElementsByTagName('body')->item(0)); |
|
439 | + } |
|
440 | + |
|
441 | + return $head; |
|
442 | + } |
|
443 | + |
|
444 | + /** |
|
445 | + * Splits input CSS code to an array where: |
|
446 | + * |
|
447 | + * - key "css" will be contains clean CSS code. |
|
448 | + * - key "media" will be contains all valuable media queries. |
|
449 | + * |
|
450 | + * Example: |
|
451 | + * |
|
452 | + * The CSS code. |
|
453 | + * |
|
454 | + * "@import "file.css"; h1 { color:red; } @media { h1 {}} @media tv { h1 {}}" |
|
455 | + * |
|
456 | + * will be parsed into the following array: |
|
457 | + * |
|
458 | + * "css" => "h1 { color:red; }" |
|
459 | + * "media" => "@media { h1 {}}" |
|
460 | + * |
|
461 | + * @param string $css |
|
462 | + * @return array |
|
463 | + */ |
|
464 | + private function splitCssAndMediaQuery($css) { |
|
465 | + $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
466 | + |
|
467 | + // filter the CSS |
|
468 | + $search = array( |
|
469 | + // get rid of css comment code |
|
470 | + '/\\/\\*.*\\*\\//sU', |
|
471 | + // strip out any import directives |
|
472 | + '/^\\s*@import\\s[^;]+;/misU', |
|
473 | + // strip remains media enclosures |
|
474 | + '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU', |
|
475 | + ); |
|
476 | + |
|
477 | + $replace = array( |
|
478 | + '', |
|
479 | + '', |
|
480 | + '', |
|
481 | + ); |
|
482 | + |
|
483 | + // clean CSS before output |
|
484 | + $css = preg_replace($search, $replace, $css); |
|
485 | + |
|
486 | + return array('css' => $css, 'media' => self::$_media); |
|
487 | + } |
|
488 | + |
|
489 | + private function _media_concat( $matches ) { |
|
490 | + self::$_media .= $matches[0]; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * Creates a DOMDocument instance with the current HTML. |
|
495 | + * |
|
496 | + * @return DOMDocument |
|
497 | + */ |
|
498 | + private function createXmlDocument() { |
|
499 | + $xmlDocument = new DOMDocument; |
|
500 | + $xmlDocument->encoding = self::ENCODING; |
|
501 | + $xmlDocument->strictErrorChecking = false; |
|
502 | + $xmlDocument->formatOutput = true; |
|
503 | + $libXmlState = libxml_use_internal_errors(true); |
|
504 | + $xmlDocument->loadHTML($this->getUnifiedHtml()); |
|
505 | + libxml_clear_errors(); |
|
506 | + libxml_use_internal_errors($libXmlState); |
|
507 | + $xmlDocument->normalizeDocument(); |
|
508 | + |
|
509 | + return $xmlDocument; |
|
510 | + } |
|
511 | + |
|
512 | + /** |
|
513 | + * Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed. |
|
514 | + * |
|
515 | + * @return string the unified HTML |
|
516 | + * |
|
517 | + * @throws BadMethodCallException |
|
518 | + */ |
|
519 | + private function getUnifiedHtml() { |
|
520 | + if (!empty($this->unprocessableHtmlTags)) { |
|
521 | + $unprocessableHtmlTags = implode('|', $this->unprocessableHtmlTags); |
|
522 | + $bodyWithoutUnprocessableTags = preg_replace('/<\\/?(' . $unprocessableHtmlTags . ')[^>]*>/i', '', $this->html); |
|
523 | + } else { |
|
524 | + $bodyWithoutUnprocessableTags = $this->html; |
|
525 | + } |
|
526 | + |
|
527 | + if ( function_exists( 'mb_convert_encoding' ) ) { |
|
528 | + return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
529 | + } else { |
|
530 | + return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
531 | + } |
|
532 | + } |
|
533 | + |
|
534 | + /** |
|
535 | + * @param array $a |
|
536 | + * @param array $b |
|
537 | + * |
|
538 | + * @return integer |
|
539 | + */ |
|
540 | + private function sortBySelectorPrecedence(array $a, array $b) { |
|
541 | + $precedenceA = $this->getCssSelectorPrecedence($a['selector']); |
|
542 | + $precedenceB = $this->getCssSelectorPrecedence($b['selector']); |
|
543 | + |
|
544 | + // We want these sorted in ascending order so selectors with lesser precedence get processed first and |
|
545 | + // selectors with greater precedence get sorted last. |
|
546 | + // The parenthesis around the -1 are necessary to avoid a PHP_CodeSniffer warning about missing spaces around |
|
547 | + // arithmetic operators. |
|
548 | + // @see http://forge.typo3.org/issues/55605 |
|
549 | + $precedenceForEquals = ($a['line'] < $b['line'] ? (-1) : 1); |
|
550 | + $precedenceForNotEquals = ($precedenceA < $precedenceB ? (-1) : 1); |
|
551 | + return ($precedenceA === $precedenceB) ? $precedenceForEquals : $precedenceForNotEquals; |
|
552 | + } |
|
553 | + |
|
554 | + /** |
|
555 | + * @param string $selector |
|
556 | + * |
|
557 | + * @return integer |
|
558 | + */ |
|
559 | + private function getCssSelectorPrecedence($selector) { |
|
560 | + $selectorKey = md5($selector); |
|
561 | + if (!isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) { |
|
562 | + $precedence = 0; |
|
563 | + $value = 100; |
|
564 | + // ids: worth 100, classes: worth 10, elements: worth 1 |
|
565 | + $search = array('\\#','\\.',''); |
|
566 | + |
|
567 | + foreach ($search as $s) { |
|
568 | + if (trim($selector == '')) { |
|
569 | + break; |
|
570 | + } |
|
571 | + $number = 0; |
|
572 | + $selector = preg_replace('/' . $s . '\\w+/', '', $selector, -1, $number); |
|
573 | + $precedence += ($value * $number); |
|
574 | + $value /= 10; |
|
575 | + } |
|
576 | + $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey] = $precedence; |
|
577 | + } |
|
578 | + |
|
579 | + return $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey]; |
|
580 | + } |
|
581 | + |
|
582 | + /** |
|
583 | + * Right now, we support all CSS 1 selectors and most CSS2/3 selectors. |
|
584 | + * |
|
585 | + * @see http://plasmasturm.org/log/444/ |
|
586 | + * |
|
587 | + * @param string $paramCssSelector |
|
588 | + * |
|
589 | + * @return string |
|
590 | + */ |
|
591 | + private function translateCssToXpath($paramCssSelector) { |
|
592 | + $cssSelector = ' ' . $paramCssSelector . ' '; |
|
593 | + $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
594 | + $cssSelector = trim($cssSelector); |
|
595 | + $xpathKey = md5($cssSelector); |
|
596 | + if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
|
597 | + // returns an Xpath selector |
|
598 | + $search = array( |
|
599 | + // Matches any element that is a child of parent. |
|
600 | + '/\\s+>\\s+/', |
|
601 | + // Matches any element that is an adjacent sibling. |
|
602 | + '/\\s+\\+\\s+/', |
|
603 | + // Matches any element that is a descendant of an parent element element. |
|
604 | + '/\\s+/', |
|
605 | + // first-child pseudo-selector |
|
606 | + '/([^\\/]+):first-child/i', |
|
607 | + // last-child pseudo-selector |
|
608 | + '/([^\\/]+):last-child/i', |
|
609 | + // Matches attribute only selector |
|
610 | + '/^\\[(\\w+)\\]/', |
|
611 | + // Matches element with attribute |
|
612 | + '/(\\w)\\[(\\w+)\\]/', |
|
613 | + // Matches element with EXACT attribute |
|
614 | + '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/', |
|
615 | + ); |
|
616 | + $replace = array( |
|
617 | + '/', |
|
618 | + '/following-sibling::*[1]/self::', |
|
619 | + '//', |
|
620 | + '*[1]/self::\\1', |
|
621 | + '*[last()]/self::\\1', |
|
622 | + '*[@\\1]', |
|
623 | + '\\1[@\\2]', |
|
624 | + '\\1[@\\2="\\3"]', |
|
625 | + ); |
|
626 | + |
|
627 | + $cssSelector = '//' . preg_replace($search, $replace, $cssSelector); |
|
628 | + |
|
629 | + $cssSelector = preg_replace_callback(self::ID_ATTRIBUTE_MATCHER, array($this, 'matchIdAttributes'), $cssSelector); |
|
630 | + $cssSelector = preg_replace_callback(self::CLASS_ATTRIBUTE_MATCHER, array($this, 'matchClassAttributes'), $cssSelector); |
|
631 | + |
|
632 | + // Advanced selectors are going to require a bit more advanced emogrification. |
|
633 | + // When we required PHP 5.3, we could do this with closures. |
|
634 | + $cssSelector = preg_replace_callback( |
|
635 | + '/([^\\/]+):nth-child\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
636 | + array($this, 'translateNthChild'), $cssSelector |
|
637 | + ); |
|
638 | + $cssSelector = preg_replace_callback( |
|
639 | + '/([^\\/]+):nth-of-type\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
640 | + array($this, 'translateNthOfType'), $cssSelector |
|
641 | + ); |
|
642 | + |
|
643 | + $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey] = $cssSelector; |
|
644 | + } |
|
645 | + return $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey]; |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * @param array $match |
|
650 | + * |
|
651 | + * @return string |
|
652 | + */ |
|
653 | + private function matchIdAttributes(array $match) { |
|
654 | + return (strlen($match[1]) ? $match[1] : '*') . '[@id="' . $match[2] . '"]'; |
|
655 | + } |
|
656 | + |
|
657 | + /** |
|
658 | + * @param array $match |
|
659 | + * |
|
660 | + * @return string |
|
661 | + */ |
|
662 | + private function matchClassAttributes(array $match) { |
|
663 | + return (strlen($match[1]) ? $match[1] : '*') . '[contains(concat(" ",@class," "),concat(" ","' . |
|
664 | + implode( |
|
665 | + '"," "))][contains(concat(" ",@class," "),concat(" ","', |
|
666 | + explode('.', substr($match[2], 1)) |
|
667 | + ) . '"," "))]'; |
|
668 | + } |
|
669 | + |
|
670 | + /** |
|
671 | + * @param array $match |
|
672 | + * |
|
673 | + * @return string |
|
674 | + */ |
|
675 | + private function translateNthChild(array $match) { |
|
676 | + $result = $this->parseNth($match); |
|
677 | + |
|
678 | + if (isset($result[self::MULTIPLIER])) { |
|
679 | + if ($result[self::MULTIPLIER] < 0) { |
|
680 | + $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
681 | + return sprintf('*[(last() - position()) mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
682 | + } else { |
|
683 | + return sprintf('*[position() mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
684 | + } |
|
685 | + } else { |
|
686 | + return sprintf('*[%u]/self::%s', $result[self::INDEX], $match[1]); |
|
687 | + } |
|
688 | + } |
|
689 | + |
|
690 | + /** |
|
691 | + * @param array $match |
|
692 | + * |
|
693 | + * @return string |
|
694 | + */ |
|
695 | + private function translateNthOfType(array $match) { |
|
696 | + $result = $this->parseNth($match); |
|
697 | + |
|
698 | + if (isset($result[self::MULTIPLIER])) { |
|
699 | + if ($result[self::MULTIPLIER] < 0) { |
|
700 | + $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
701 | + return sprintf('%s[(last() - position()) mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
702 | + } else { |
|
703 | + return sprintf('%s[position() mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
704 | + } |
|
705 | + } else { |
|
706 | + return sprintf('%s[%u]', $match[1], $result[self::INDEX]); |
|
707 | + } |
|
708 | + } |
|
709 | + |
|
710 | + /** |
|
711 | + * @param array $match |
|
712 | + * |
|
713 | + * @return array |
|
714 | + */ |
|
715 | + private function parseNth(array $match) { |
|
716 | + if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
717 | + $index = strtolower($match[2]) == 'even' ? 0 : 1; |
|
718 | + return array(self::MULTIPLIER => 2, self::INDEX => $index); |
|
719 | + } elseif (stripos($match[2], 'n') === false) { |
|
720 | + // if there is a multiplier |
|
721 | + $index = intval(str_replace(' ', '', $match[2])); |
|
722 | + return array(self::INDEX => $index); |
|
723 | + } else { |
|
724 | + if (isset($match[3])) { |
|
725 | + $multipleTerm = str_replace($match[3], '', $match[2]); |
|
726 | + $index = intval(str_replace(' ', '', $match[3])); |
|
727 | + } else { |
|
728 | + $multipleTerm = $match[2]; |
|
729 | + $index = 0; |
|
730 | + } |
|
731 | + |
|
732 | + $multiplier = str_ireplace('n', '', $multipleTerm); |
|
733 | + |
|
734 | + if (!strlen($multiplier)) { |
|
735 | + $multiplier = 1; |
|
736 | + } elseif ($multiplier == 0) { |
|
737 | + return array(self::INDEX => $index); |
|
738 | + } else { |
|
739 | + $multiplier = intval($multiplier); |
|
740 | + } |
|
741 | + |
|
742 | + while ($index < 0) { |
|
743 | + $index += abs($multiplier); |
|
744 | + } |
|
745 | + |
|
746 | + return array(self::MULTIPLIER => $multiplier, self::INDEX => $index); |
|
747 | + } |
|
748 | + } |
|
749 | + |
|
750 | + /** |
|
751 | + * Parses a CSS declaration block into property name/value pairs. |
|
752 | + * |
|
753 | + * Example: |
|
754 | + * |
|
755 | + * The declaration block. |
|
756 | + * |
|
757 | + * "color: #000; font-weight: bold;". |
|
758 | + * |
|
759 | + * will be parsed into the following array: |
|
760 | + * |
|
761 | + * "color" => "#000" |
|
762 | + * "font-weight" => "bold" |
|
763 | + * |
|
764 | + * @param string $cssDeclarationBlock the CSS declaration block without the curly braces, may be empty |
|
765 | + * |
|
766 | + * @return array the CSS declarations with the property names as array keys and the property values as array values |
|
767 | + */ |
|
768 | + private function parseCssDeclarationBlock($cssDeclarationBlock) { |
|
769 | + if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock])) { |
|
770 | + return $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock]; |
|
771 | + } |
|
772 | + |
|
773 | + $properties = array(); |
|
774 | + $declarations = explode(';', $cssDeclarationBlock); |
|
775 | + foreach ($declarations as $declaration) { |
|
776 | + $matches = array(); |
|
777 | + if (!preg_match('/ *([A-Za-z\\-]+) *: *([^;]+) */', $declaration, $matches)) { |
|
778 | + continue; |
|
779 | + } |
|
780 | + $propertyName = strtolower($matches[1]); |
|
781 | + $propertyValue = $matches[2]; |
|
782 | + $properties[$propertyName] = $propertyValue; |
|
783 | + } |
|
784 | + $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock] = $properties; |
|
785 | + |
|
786 | + return $properties; |
|
787 | + } |
|
788 | 788 | } |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | if ($nodesWithStyleAttributes !== false) { |
239 | 239 | /** @var $nodeWithStyleAttribute DOMNode */ |
240 | 240 | foreach ($nodesWithStyleAttributes as $node) { |
241 | - $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
241 | + $normalizedOriginalStyle = preg_replace_callback('/[A-z\\-]+(?=\\:)/S', array($this, 'strtolower'), $node->getAttribute('style')); |
|
242 | 242 | |
243 | 243 | // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
244 | 244 | $nodePath = $node->getNodePath(); |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | } |
290 | 290 | |
291 | 291 | // now sort the selectors by precedence |
292 | - usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
292 | + usort($allSelectors, array($this, 'sortBySelectorPrecedence')); |
|
293 | 293 | |
294 | 294 | $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
295 | 295 | } |
@@ -329,7 +329,7 @@ discard block |
||
329 | 329 | if ($nodesWithStyleDisplayNone->length > 0) { |
330 | 330 | /** @var $node \DOMNode */ |
331 | 331 | foreach ($nodesWithStyleDisplayNone as $node) { |
332 | - if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
332 | + if ($node->parentNode && is_callable(array($node->parentNode, 'removeChild'))) { |
|
333 | 333 | $node->parentNode->removeChild($node); |
334 | 334 | } |
335 | 335 | } |
@@ -338,10 +338,10 @@ discard block |
||
338 | 338 | $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
339 | 339 | |
340 | 340 | if ($this->preserveEncoding) { |
341 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
342 | - return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
341 | + if (function_exists('mb_convert_encoding')) { |
|
342 | + return mb_convert_encoding($xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES'); |
|
343 | 343 | } else { |
344 | - return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
344 | + return htmlspecialchars_decode(utf8_encode(html_entity_decode($xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING))); |
|
345 | 345 | } |
346 | 346 | } else { |
347 | 347 | return $xmlDocument->saveHTML(); |
@@ -462,7 +462,7 @@ discard block |
||
462 | 462 | * @return array |
463 | 463 | */ |
464 | 464 | private function splitCssAndMediaQuery($css) { |
465 | - $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
465 | + $css = preg_replace_callback('#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array($this, '_media_concat'), $css); |
|
466 | 466 | |
467 | 467 | // filter the CSS |
468 | 468 | $search = array( |
@@ -486,7 +486,7 @@ discard block |
||
486 | 486 | return array('css' => $css, 'media' => self::$_media); |
487 | 487 | } |
488 | 488 | |
489 | - private function _media_concat( $matches ) { |
|
489 | + private function _media_concat($matches) { |
|
490 | 490 | self::$_media .= $matches[0]; |
491 | 491 | } |
492 | 492 | |
@@ -524,10 +524,10 @@ discard block |
||
524 | 524 | $bodyWithoutUnprocessableTags = $this->html; |
525 | 525 | } |
526 | 526 | |
527 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
528 | - return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
527 | + if (function_exists('mb_convert_encoding')) { |
|
528 | + return mb_convert_encoding($bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING); |
|
529 | 529 | } else { |
530 | - return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
530 | + return htmlspecialchars_decode(utf8_decode(htmlentities($bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false))); |
|
531 | 531 | } |
532 | 532 | } |
533 | 533 | |
@@ -562,7 +562,7 @@ discard block |
||
562 | 562 | $precedence = 0; |
563 | 563 | $value = 100; |
564 | 564 | // ids: worth 100, classes: worth 10, elements: worth 1 |
565 | - $search = array('\\#','\\.',''); |
|
565 | + $search = array('\\#', '\\.', ''); |
|
566 | 566 | |
567 | 567 | foreach ($search as $s) { |
568 | 568 | if (trim($selector == '')) { |
@@ -590,7 +590,7 @@ discard block |
||
590 | 590 | */ |
591 | 591 | private function translateCssToXpath($paramCssSelector) { |
592 | 592 | $cssSelector = ' ' . $paramCssSelector . ' '; |
593 | - $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
593 | + $cssSelector = preg_replace_callback('/\s+\w+\s+/', array($this, 'strtolower'), $cssSelector); |
|
594 | 594 | $cssSelector = trim($cssSelector); |
595 | 595 | $xpathKey = md5($cssSelector); |
596 | 596 | if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
@@ -713,7 +713,7 @@ discard block |
||
713 | 713 | * @return array |
714 | 714 | */ |
715 | 715 | private function parseNth(array $match) { |
716 | - if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
716 | + if (in_array(strtolower($match[2]), array('even', 'odd'))) { |
|
717 | 717 | $index = strtolower($match[2]) == 'even' ? 0 : 1; |
718 | 718 | return array(self::MULTIPLIER => 2, self::INDEX => $index); |
719 | 719 | } elseif (stripos($match[2], 'n') === false) { |
@@ -1,6 +1,8 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly. |
3 | -if (!defined( 'ABSPATH' ) ) exit; |
|
3 | +if (!defined( 'ABSPATH' ) ) { |
|
4 | + exit; |
|
5 | +} |
|
4 | 6 | |
5 | 7 | class WPInv_EUVat { |
6 | 8 | private static $is_ajax = false; |
@@ -1445,16 +1447,18 @@ discard block |
||
1445 | 1447 | |
1446 | 1448 | if ( !empty( $tax_rates ) ) { |
1447 | 1449 | foreach ( $tax_rates as $key => $tax_rate ) { |
1448 | - if ( $country != $tax_rate['country'] ) |
|
1449 | - continue; |
|
1450 | + if ( $country != $tax_rate['country'] ) { |
|
1451 | + continue; |
|
1452 | + } |
|
1450 | 1453 | |
1451 | 1454 | if ( !empty( $tax_rate['global'] ) ) { |
1452 | 1455 | if ( 0 !== $tax_rate['rate'] || !empty( $tax_rate['rate'] ) ) { |
1453 | 1456 | $rate = number_format( $tax_rate['rate'], 4 ); |
1454 | 1457 | } |
1455 | 1458 | } else { |
1456 | - if ( empty( $tax_rate['state'] ) || strtolower( $state ) != strtolower( $tax_rate['state'] ) ) |
|
1457 | - continue; |
|
1459 | + if ( empty( $tax_rate['state'] ) || strtolower( $state ) != strtolower( $tax_rate['state'] ) ) { |
|
1460 | + continue; |
|
1461 | + } |
|
1458 | 1462 | |
1459 | 1463 | $state_rate = $tax_rate['rate']; |
1460 | 1464 | if ( 0 !== $state_rate || !empty( $state_rate ) ) { |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly. |
3 | -if (!defined( 'ABSPATH' ) ) exit; |
|
3 | +if (!defined('ABSPATH')) exit; |
|
4 | 4 | |
5 | 5 | class WPInv_EUVat { |
6 | 6 | private static $is_ajax = false; |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | private static $instance = false; |
9 | 9 | |
10 | 10 | public static function get_instance() { |
11 | - if ( !self::$instance ) { |
|
11 | + if (!self::$instance) { |
|
12 | 12 | self::$instance = new self(); |
13 | 13 | self::$instance->actions(); |
14 | 14 | } |
@@ -17,137 +17,137 @@ discard block |
||
17 | 17 | } |
18 | 18 | |
19 | 19 | public function __construct() { |
20 | - self::$is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX; |
|
20 | + self::$is_ajax = defined('DOING_AJAX') && DOING_AJAX; |
|
21 | 21 | self::$default_country = wpinv_get_default_country(); |
22 | 22 | } |
23 | 23 | |
24 | 24 | public static function actions() { |
25 | - if ( is_admin() ) { |
|
26 | - add_action( 'admin_enqueue_scripts', array( self::$instance, 'enqueue_admin_scripts' ) ); |
|
27 | - add_action( 'wpinv_settings_sections_taxes', array( self::$instance, 'section_vat_settings' ) ); |
|
28 | - add_action( 'wpinv_settings_taxes', array( self::$instance, 'vat_settings' ) ); |
|
29 | - add_filter( 'wpinv_settings_taxes-vat_sanitize', array( self::$instance, 'sanitize_vat_settings' ) ); |
|
30 | - add_filter( 'wpinv_settings_taxes-vat_rates_sanitize', array( self::$instance, 'sanitize_vat_rates' ) ); |
|
31 | - add_action( 'wp_ajax_wpinv_add_vat_class', array( self::$instance, 'add_class' ) ); |
|
32 | - add_action( 'wp_ajax_nopriv_wpinv_add_vat_class', array( self::$instance, 'add_class' ) ); |
|
33 | - add_action( 'wp_ajax_wpinv_delete_vat_class', array( self::$instance, 'delete_class' ) ); |
|
34 | - add_action( 'wp_ajax_nopriv_wpinv_delete_vat_class', array( self::$instance, 'delete_class' ) ); |
|
35 | - add_action( 'wp_ajax_wpinv_update_vat_rates', array( self::$instance, 'update_eu_rates' ) ); |
|
36 | - add_action( 'wp_ajax_nopriv_wpinv_update_vat_rates', array( self::$instance, 'update_eu_rates' ) ); |
|
37 | - add_action( 'wp_ajax_wpinv_geoip2', array( self::$instance, 'geoip2_download_database' ) ); |
|
38 | - add_action( 'wp_ajax_nopriv_wpinv_geoip2', array( self::$instance, 'geoip2_download_database' ) ); |
|
39 | - } |
|
40 | - |
|
41 | - add_action( 'wp_enqueue_scripts', array( self::$instance, 'enqueue_vat_scripts' ) ); |
|
42 | - add_filter( 'wpinv_default_billing_country', array( self::$instance, 'get_user_country' ), 10 ); |
|
43 | - add_filter( 'wpinv_get_user_country', array( self::$instance, 'set_user_country' ), 10 ); |
|
44 | - add_action( 'wp_ajax_wpinv_vat_validate', array( self::$instance, 'ajax_vat_validate' ) ); |
|
45 | - add_action( 'wp_ajax_nopriv_wpinv_vat_validate', array( self::$instance, 'ajax_vat_validate' ) ); |
|
46 | - add_action( 'wp_ajax_wpinv_vat_reset', array( self::$instance, 'ajax_vat_reset' ) ); |
|
47 | - add_action( 'wp_ajax_nopriv_wpinv_vat_reset', array( self::$instance, 'ajax_vat_reset' ) ); |
|
48 | - add_action( 'wpinv_invoice_print_after_line_items', array( self::$instance, 'show_vat_notice' ), 999, 1 ); |
|
49 | - if ( wpinv_use_taxes() ) { |
|
50 | - add_action( 'wpinv_after_billing_fields', array( self::$instance, 'checkout_vat_fields' ) ); |
|
51 | - if ( self::allow_vat_rules() ) { |
|
52 | - add_action( 'wpinv_checkout_error_checks', array( self::$instance, 'checkout_vat_validate' ), 10, 2 ); |
|
53 | - add_filter( 'wpinv_tax_rate', array( self::$instance, 'get_rate' ), 10, 4 ); |
|
25 | + if (is_admin()) { |
|
26 | + add_action('admin_enqueue_scripts', array(self::$instance, 'enqueue_admin_scripts')); |
|
27 | + add_action('wpinv_settings_sections_taxes', array(self::$instance, 'section_vat_settings')); |
|
28 | + add_action('wpinv_settings_taxes', array(self::$instance, 'vat_settings')); |
|
29 | + add_filter('wpinv_settings_taxes-vat_sanitize', array(self::$instance, 'sanitize_vat_settings')); |
|
30 | + add_filter('wpinv_settings_taxes-vat_rates_sanitize', array(self::$instance, 'sanitize_vat_rates')); |
|
31 | + add_action('wp_ajax_wpinv_add_vat_class', array(self::$instance, 'add_class')); |
|
32 | + add_action('wp_ajax_nopriv_wpinv_add_vat_class', array(self::$instance, 'add_class')); |
|
33 | + add_action('wp_ajax_wpinv_delete_vat_class', array(self::$instance, 'delete_class')); |
|
34 | + add_action('wp_ajax_nopriv_wpinv_delete_vat_class', array(self::$instance, 'delete_class')); |
|
35 | + add_action('wp_ajax_wpinv_update_vat_rates', array(self::$instance, 'update_eu_rates')); |
|
36 | + add_action('wp_ajax_nopriv_wpinv_update_vat_rates', array(self::$instance, 'update_eu_rates')); |
|
37 | + add_action('wp_ajax_wpinv_geoip2', array(self::$instance, 'geoip2_download_database')); |
|
38 | + add_action('wp_ajax_nopriv_wpinv_geoip2', array(self::$instance, 'geoip2_download_database')); |
|
39 | + } |
|
40 | + |
|
41 | + add_action('wp_enqueue_scripts', array(self::$instance, 'enqueue_vat_scripts')); |
|
42 | + add_filter('wpinv_default_billing_country', array(self::$instance, 'get_user_country'), 10); |
|
43 | + add_filter('wpinv_get_user_country', array(self::$instance, 'set_user_country'), 10); |
|
44 | + add_action('wp_ajax_wpinv_vat_validate', array(self::$instance, 'ajax_vat_validate')); |
|
45 | + add_action('wp_ajax_nopriv_wpinv_vat_validate', array(self::$instance, 'ajax_vat_validate')); |
|
46 | + add_action('wp_ajax_wpinv_vat_reset', array(self::$instance, 'ajax_vat_reset')); |
|
47 | + add_action('wp_ajax_nopriv_wpinv_vat_reset', array(self::$instance, 'ajax_vat_reset')); |
|
48 | + add_action('wpinv_invoice_print_after_line_items', array(self::$instance, 'show_vat_notice'), 999, 1); |
|
49 | + if (wpinv_use_taxes()) { |
|
50 | + add_action('wpinv_after_billing_fields', array(self::$instance, 'checkout_vat_fields')); |
|
51 | + if (self::allow_vat_rules()) { |
|
52 | + add_action('wpinv_checkout_error_checks', array(self::$instance, 'checkout_vat_validate'), 10, 2); |
|
53 | + add_filter('wpinv_tax_rate', array(self::$instance, 'get_rate'), 10, 4); |
|
54 | 54 | } |
55 | 55 | } |
56 | 56 | } |
57 | 57 | |
58 | - public static function get_eu_states( $sort = true ) { |
|
59 | - $eu_states = array( 'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GB', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE' ); |
|
60 | - if ( $sort ) { |
|
61 | - $sort = sort( $eu_states ); |
|
58 | + public static function get_eu_states($sort = true) { |
|
59 | + $eu_states = array('AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GB', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE'); |
|
60 | + if ($sort) { |
|
61 | + $sort = sort($eu_states); |
|
62 | 62 | } |
63 | 63 | |
64 | - return apply_filters( 'wpinv_get_eu_states', $eu_states, $sort ); |
|
64 | + return apply_filters('wpinv_get_eu_states', $eu_states, $sort); |
|
65 | 65 | } |
66 | 66 | |
67 | - public static function get_gst_countries( $sort = true ) { |
|
68 | - $gst_countries = array( 'AU', 'NZ', 'CA', 'CN' ); |
|
67 | + public static function get_gst_countries($sort = true) { |
|
68 | + $gst_countries = array('AU', 'NZ', 'CA', 'CN'); |
|
69 | 69 | |
70 | - if ( $sort ) { |
|
71 | - $sort = sort( $gst_countries ); |
|
70 | + if ($sort) { |
|
71 | + $sort = sort($gst_countries); |
|
72 | 72 | } |
73 | 73 | |
74 | - return apply_filters( 'wpinv_get_gst_countries', $gst_countries, $sort ); |
|
74 | + return apply_filters('wpinv_get_gst_countries', $gst_countries, $sort); |
|
75 | 75 | } |
76 | 76 | |
77 | - public static function is_eu_state( $country_code ) { |
|
78 | - $return = !empty( $country_code ) && in_array( strtoupper( $country_code ), self::get_eu_states() ) ? true : false; |
|
77 | + public static function is_eu_state($country_code) { |
|
78 | + $return = !empty($country_code) && in_array(strtoupper($country_code), self::get_eu_states()) ? true : false; |
|
79 | 79 | |
80 | - return apply_filters( 'wpinv_is_eu_state', $return, $country_code ); |
|
80 | + return apply_filters('wpinv_is_eu_state', $return, $country_code); |
|
81 | 81 | } |
82 | 82 | |
83 | - public static function is_gst_country( $country_code ) { |
|
84 | - $return = !empty( $country_code ) && in_array( strtoupper( $country_code ), self::get_gst_countries() ) ? true : false; |
|
83 | + public static function is_gst_country($country_code) { |
|
84 | + $return = !empty($country_code) && in_array(strtoupper($country_code), self::get_gst_countries()) ? true : false; |
|
85 | 85 | |
86 | - return apply_filters( 'wpinv_is_gst_country', $return, $country_code ); |
|
86 | + return apply_filters('wpinv_is_gst_country', $return, $country_code); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | public static function enqueue_vat_scripts() { |
90 | - if( wpinv_use_taxes() && wpinv_get_option( 'apply_vat_rules' ) ) { |
|
90 | + if (wpinv_use_taxes() && wpinv_get_option('apply_vat_rules')) { |
|
91 | 91 | self::load_vat_scripts(); |
92 | 92 | } |
93 | 93 | } |
94 | 94 | |
95 | - public static function load_vat_scripts(){ |
|
96 | - $suffix = '';//defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
95 | + public static function load_vat_scripts() { |
|
96 | + $suffix = ''; //defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
97 | 97 | |
98 | - wp_register_script( 'wpinv-vat-validation-script', WPINV_PLUGIN_URL . 'assets/js/jsvat' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION ); |
|
99 | - wp_register_script( 'wpinv-vat-script', WPINV_PLUGIN_URL . 'assets/js/euvat' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION ); |
|
98 | + wp_register_script('wpinv-vat-validation-script', WPINV_PLUGIN_URL . 'assets/js/jsvat' . $suffix . '.js', array('jquery'), WPINV_VERSION); |
|
99 | + wp_register_script('wpinv-vat-script', WPINV_PLUGIN_URL . 'assets/js/euvat' . $suffix . '.js', array('jquery'), WPINV_VERSION); |
|
100 | 100 | |
101 | - $vat_name = self::get_vat_name(); |
|
101 | + $vat_name = self::get_vat_name(); |
|
102 | 102 | |
103 | 103 | $vars = array(); |
104 | 104 | $vars['UseTaxes'] = wpinv_use_taxes(); |
105 | 105 | $vars['EUStates'] = self::get_eu_states(); |
106 | - $vars['NoRateSet'] = __( 'You have not set a rate. Do you want to continue?', 'invoicing' ); |
|
107 | - $vars['EmptyCompany'] = __( 'Please enter your registered company name!', 'invoicing' ); |
|
108 | - $vars['EmptyVAT'] = wp_sprintf( __( 'Please enter your %s number!', 'invoicing' ), $vat_name ); |
|
109 | - $vars['TotalsRefreshed'] = wp_sprintf( __( 'The invoice totals will be refreshed to update the %s.', 'invoicing' ), $vat_name ); |
|
110 | - $vars['ErrValidateVAT'] = wp_sprintf( __( 'Fail to validate the %s number!', 'invoicing' ), $vat_name ); |
|
111 | - $vars['ErrResetVAT'] = wp_sprintf( __( 'Fail to reset the %s number!', 'invoicing' ), $vat_name ); |
|
112 | - $vars['ErrInvalidVat'] = wp_sprintf( __( 'The %s number supplied does not have a valid format!', 'invoicing' ), $vat_name ); |
|
113 | - $vars['ErrInvalidResponse'] = __( 'An invalid response has been received from the server!', 'invoicing' ); |
|
106 | + $vars['NoRateSet'] = __('You have not set a rate. Do you want to continue?', 'invoicing'); |
|
107 | + $vars['EmptyCompany'] = __('Please enter your registered company name!', 'invoicing'); |
|
108 | + $vars['EmptyVAT'] = wp_sprintf(__('Please enter your %s number!', 'invoicing'), $vat_name); |
|
109 | + $vars['TotalsRefreshed'] = wp_sprintf(__('The invoice totals will be refreshed to update the %s.', 'invoicing'), $vat_name); |
|
110 | + $vars['ErrValidateVAT'] = wp_sprintf(__('Fail to validate the %s number!', 'invoicing'), $vat_name); |
|
111 | + $vars['ErrResetVAT'] = wp_sprintf(__('Fail to reset the %s number!', 'invoicing'), $vat_name); |
|
112 | + $vars['ErrInvalidVat'] = wp_sprintf(__('The %s number supplied does not have a valid format!', 'invoicing'), $vat_name); |
|
113 | + $vars['ErrInvalidResponse'] = __('An invalid response has been received from the server!', 'invoicing'); |
|
114 | 114 | $vars['ApplyVATRules'] = $vars['UseTaxes'] ? self::allow_vat_rules() : false; |
115 | 115 | $vars['HideVatFields'] = $vars['ApplyVATRules'] ? self::hide_vat_fields() : true; |
116 | - $vars['ErrResponse'] = __( 'The request response is invalid!', 'invoicing' ); |
|
117 | - $vars['ErrRateResponse'] = __( 'The get rate request response is invalid', 'invoicing' ); |
|
118 | - $vars['PageRefresh'] = __( 'The page will be refreshed in 10 seconds to show the new options.', 'invoicing' ); |
|
119 | - $vars['RequestResponseNotValidJSON'] = __( 'The get rate request response is not valid JSON', 'invoicing' ); |
|
120 | - $vars['GetRateRequestFailed'] = __( 'The get rate request failed: ', 'invoicing' ); |
|
121 | - $vars['NoRateInformationInResponse'] = __( 'The get rate request response does not contain any rate information', 'invoicing' ); |
|
122 | - $vars['RatesUpdated'] = __( 'The rates have been updated. Press the save button to record these new rates.', 'invoicing' ); |
|
123 | - $vars['IPAddressInformation'] = __( 'IP Address Information', 'invoicing' ); |
|
124 | - $vars['VatValidating'] = wp_sprintf( __( 'Validating %s number...', 'invoicing' ), $vat_name ); |
|
125 | - $vars['VatReseting'] = __( 'Reseting...', 'invoicing' ); |
|
126 | - $vars['VatValidated'] = wp_sprintf( __( '%s number validated', 'invoicing' ), $vat_name ); |
|
127 | - $vars['VatNotValidated'] = wp_sprintf( __( '%s number not validated', 'invoicing' ), $vat_name ); |
|
128 | - $vars['ConfirmDeleteClass'] = __( 'Are you sure you wish to delete this rates class?', 'invoicing' ); |
|
116 | + $vars['ErrResponse'] = __('The request response is invalid!', 'invoicing'); |
|
117 | + $vars['ErrRateResponse'] = __('The get rate request response is invalid', 'invoicing'); |
|
118 | + $vars['PageRefresh'] = __('The page will be refreshed in 10 seconds to show the new options.', 'invoicing'); |
|
119 | + $vars['RequestResponseNotValidJSON'] = __('The get rate request response is not valid JSON', 'invoicing'); |
|
120 | + $vars['GetRateRequestFailed'] = __('The get rate request failed: ', 'invoicing'); |
|
121 | + $vars['NoRateInformationInResponse'] = __('The get rate request response does not contain any rate information', 'invoicing'); |
|
122 | + $vars['RatesUpdated'] = __('The rates have been updated. Press the save button to record these new rates.', 'invoicing'); |
|
123 | + $vars['IPAddressInformation'] = __('IP Address Information', 'invoicing'); |
|
124 | + $vars['VatValidating'] = wp_sprintf(__('Validating %s number...', 'invoicing'), $vat_name); |
|
125 | + $vars['VatReseting'] = __('Reseting...', 'invoicing'); |
|
126 | + $vars['VatValidated'] = wp_sprintf(__('%s number validated', 'invoicing'), $vat_name); |
|
127 | + $vars['VatNotValidated'] = wp_sprintf(__('%s number not validated', 'invoicing'), $vat_name); |
|
128 | + $vars['ConfirmDeleteClass'] = __('Are you sure you wish to delete this rates class?', 'invoicing'); |
|
129 | 129 | $vars['isFront'] = is_admin() ? false : true; |
130 | 130 | $vars['baseCountry'] = wpinv_get_default_country(); |
131 | - $vars['disableVATSameCountry'] = ( self::same_country_rule() == 'no' ? true : false ); |
|
132 | - $vars['disableVATSimpleCheck'] = wpinv_get_option( 'vat_offline_check' ) ? true : false; |
|
131 | + $vars['disableVATSameCountry'] = (self::same_country_rule() == 'no' ? true : false); |
|
132 | + $vars['disableVATSimpleCheck'] = wpinv_get_option('vat_offline_check') ? true : false; |
|
133 | 133 | |
134 | - wp_enqueue_script( 'wpinv-vat-validation-script' ); |
|
135 | - wp_enqueue_script( 'wpinv-vat-script' ); |
|
136 | - wp_localize_script( 'wpinv-vat-script', 'WPInv_VAT_Vars', $vars ); |
|
134 | + wp_enqueue_script('wpinv-vat-validation-script'); |
|
135 | + wp_enqueue_script('wpinv-vat-script'); |
|
136 | + wp_localize_script('wpinv-vat-script', 'WPInv_VAT_Vars', $vars); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | public static function enqueue_admin_scripts() { |
140 | - if( isset( $_GET['page'] ) && 'wpinv-settings' == $_GET['page'] ) { |
|
140 | + if (isset($_GET['page']) && 'wpinv-settings' == $_GET['page']) { |
|
141 | 141 | self::load_vat_scripts(); |
142 | 142 | } |
143 | 143 | } |
144 | 144 | |
145 | - public static function section_vat_settings( $sections ) { |
|
146 | - if ( !empty( $sections ) ) { |
|
147 | - $sections['vat'] = __( 'EU VAT Settings', 'invoicing' ); |
|
145 | + public static function section_vat_settings($sections) { |
|
146 | + if (!empty($sections)) { |
|
147 | + $sections['vat'] = __('EU VAT Settings', 'invoicing'); |
|
148 | 148 | |
149 | - if ( self::allow_vat_classes() ) { |
|
150 | - $sections['vat_rates'] = __( 'EU VAT Rates', 'invoicing' ); |
|
149 | + if (self::allow_vat_classes()) { |
|
150 | + $sections['vat_rates'] = __('EU VAT Rates', 'invoicing'); |
|
151 | 151 | } |
152 | 152 | } |
153 | 153 | return $sections; |
@@ -156,52 +156,52 @@ discard block |
||
156 | 156 | public static function vat_rates_settings() { |
157 | 157 | $vat_classes = self::get_rate_classes(); |
158 | 158 | $vat_rates = array(); |
159 | - $vat_class = isset( $_REQUEST['wpi_sub'] ) && $_REQUEST['wpi_sub'] !== '' && isset( $vat_classes[$_REQUEST['wpi_sub']] )? sanitize_text_field( $_REQUEST['wpi_sub'] ) : '_new'; |
|
160 | - $current_url = remove_query_arg( 'wpi_sub' ); |
|
159 | + $vat_class = isset($_REQUEST['wpi_sub']) && $_REQUEST['wpi_sub'] !== '' && isset($vat_classes[$_REQUEST['wpi_sub']]) ? sanitize_text_field($_REQUEST['wpi_sub']) : '_new'; |
|
160 | + $current_url = remove_query_arg('wpi_sub'); |
|
161 | 161 | |
162 | 162 | $vat_rates['vat_rates_header'] = array( |
163 | 163 | 'id' => 'vat_rates_header', |
164 | - 'name' => '<h3>' . __( 'Manage VAT Rates', 'invoicing' ) . '</h3>', |
|
164 | + 'name' => '<h3>' . __('Manage VAT Rates', 'invoicing') . '</h3>', |
|
165 | 165 | 'desc' => '', |
166 | 166 | 'type' => 'header', |
167 | 167 | 'size' => 'regular' |
168 | 168 | ); |
169 | 169 | $vat_rates['vat_rates_class'] = array( |
170 | 170 | 'id' => 'vat_rates_class', |
171 | - 'name' => __( 'Edit VAT Rates', 'invoicing' ), |
|
172 | - 'desc' => __( 'The standard rate will apply where no explicit rate is provided.', 'invoicing' ), |
|
171 | + 'name' => __('Edit VAT Rates', 'invoicing'), |
|
172 | + 'desc' => __('The standard rate will apply where no explicit rate is provided.', 'invoicing'), |
|
173 | 173 | 'type' => 'select', |
174 | - 'options' => array_merge( $vat_classes, array( '_new' => __( 'Add New Rate Class', 'invoicing' ) ) ), |
|
175 | - 'placeholder' => __( 'Select a VAT Rate', 'invoicing' ), |
|
174 | + 'options' => array_merge($vat_classes, array('_new' => __('Add New Rate Class', 'invoicing'))), |
|
175 | + 'placeholder' => __('Select a VAT Rate', 'invoicing'), |
|
176 | 176 | 'selected' => $vat_class, |
177 | 177 | 'class' => 'wpi_select2', |
178 | 178 | 'onchange' => 'document.location.href="' . $current_url . '&wpi_sub=" + this.value;', |
179 | 179 | ); |
180 | 180 | |
181 | - if ( $vat_class != '_standard' && $vat_class != '_new' ) { |
|
181 | + if ($vat_class != '_standard' && $vat_class != '_new') { |
|
182 | 182 | $vat_rates['vat_rate_delete'] = array( |
183 | 183 | 'id' => 'vat_rate_delete', |
184 | 184 | 'type' => 'vat_rate_delete', |
185 | 185 | ); |
186 | 186 | } |
187 | 187 | |
188 | - if ( $vat_class == '_new' ) { |
|
188 | + if ($vat_class == '_new') { |
|
189 | 189 | $vat_rates['vat_rates_settings'] = array( |
190 | 190 | 'id' => 'vat_rates_settings', |
191 | - 'name' => '<h3>' . __( 'Add New Rate Class', 'invoicing' ) . '</h3>', |
|
191 | + 'name' => '<h3>' . __('Add New Rate Class', 'invoicing') . '</h3>', |
|
192 | 192 | 'type' => 'header', |
193 | 193 | ); |
194 | 194 | $vat_rates['vat_rate_name'] = array( |
195 | 195 | 'id' => 'vat_rate_name', |
196 | - 'name' => __( 'Name', 'invoicing' ), |
|
197 | - 'desc' => __( 'A short name for the new VAT Rate class', 'invoicing' ), |
|
196 | + 'name' => __('Name', 'invoicing'), |
|
197 | + 'desc' => __('A short name for the new VAT Rate class', 'invoicing'), |
|
198 | 198 | 'type' => 'text', |
199 | 199 | 'size' => 'regular', |
200 | 200 | ); |
201 | 201 | $vat_rates['vat_rate_desc'] = array( |
202 | 202 | 'id' => 'vat_rate_desc', |
203 | - 'name' => __( 'Description', 'invoicing' ), |
|
204 | - 'desc' => __( 'Manage VAT Rate class', 'invoicing' ), |
|
203 | + 'name' => __('Description', 'invoicing'), |
|
204 | + 'desc' => __('Manage VAT Rate class', 'invoicing'), |
|
205 | 205 | 'type' => 'text', |
206 | 206 | 'size' => 'regular', |
207 | 207 | ); |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | $vat_rates['vat_rates'] = array( |
214 | 214 | 'id' => 'vat_rates', |
215 | 215 | 'name' => '<h3>' . $vat_classes[$vat_class] . '</h3>', |
216 | - 'desc' => self::get_class_desc( $vat_class ), |
|
216 | + 'desc' => self::get_class_desc($vat_class), |
|
217 | 217 | 'type' => 'vat_rates', |
218 | 218 | ); |
219 | 219 | } |
@@ -221,12 +221,12 @@ discard block |
||
221 | 221 | return $vat_rates; |
222 | 222 | } |
223 | 223 | |
224 | - public static function vat_settings( $settings ) { |
|
225 | - if ( !empty( $settings ) ) { |
|
224 | + public static function vat_settings($settings) { |
|
225 | + if (!empty($settings)) { |
|
226 | 226 | $vat_settings = array(); |
227 | 227 | $vat_settings['vat_company_title'] = array( |
228 | 228 | 'id' => 'vat_company_title', |
229 | - 'name' => '<h3>' . __( 'Your Company Details', 'invoicing' ) . '</h3>', |
|
229 | + 'name' => '<h3>' . __('Your Company Details', 'invoicing') . '</h3>', |
|
230 | 230 | 'desc' => '', |
231 | 231 | 'type' => 'header', |
232 | 232 | 'size' => 'regular' |
@@ -234,22 +234,22 @@ discard block |
||
234 | 234 | |
235 | 235 | $vat_settings['vat_company_name'] = array( |
236 | 236 | 'id' => 'vat_company_name', |
237 | - 'name' => __( 'Your Company Name', 'invoicing' ), |
|
238 | - 'desc' => wp_sprintf(__( 'Your company name as it appears on your VAT return, you can verify it via your VAT ID on the %sEU VIES System.%s', 'invoicing' ), '<a href="http://ec.europa.eu/taxation_customs/vies/" target="_blank">', '</a>' ), |
|
237 | + 'name' => __('Your Company Name', 'invoicing'), |
|
238 | + 'desc' => wp_sprintf(__('Your company name as it appears on your VAT return, you can verify it via your VAT ID on the %sEU VIES System.%s', 'invoicing'), '<a href="http://ec.europa.eu/taxation_customs/vies/" target="_blank">', '</a>'), |
|
239 | 239 | 'type' => 'text', |
240 | 240 | 'size' => 'regular', |
241 | 241 | ); |
242 | 242 | |
243 | 243 | $vat_settings['vat_number'] = array( |
244 | 244 | 'id' => 'vat_number', |
245 | - 'name' => __( 'Your VAT Number', 'invoicing' ), |
|
245 | + 'name' => __('Your VAT Number', 'invoicing'), |
|
246 | 246 | 'type' => 'vat_number', |
247 | 247 | 'size' => 'regular', |
248 | 248 | ); |
249 | 249 | |
250 | 250 | $vat_settings['vat_settings_title'] = array( |
251 | 251 | 'id' => 'vat_settings_title', |
252 | - 'name' => '<h3>' . __( 'Apply VAT Settings', 'invoicing' ) . '</h3>', |
|
252 | + 'name' => '<h3>' . __('Apply VAT Settings', 'invoicing') . '</h3>', |
|
253 | 253 | 'desc' => '', |
254 | 254 | 'type' => 'header', |
255 | 255 | 'size' => 'regular' |
@@ -257,8 +257,8 @@ discard block |
||
257 | 257 | |
258 | 258 | $vat_settings['apply_vat_rules'] = array( |
259 | 259 | 'id' => 'apply_vat_rules', |
260 | - 'name' => __( 'Enable VAT Rules', 'invoicing' ), |
|
261 | - 'desc' => __( 'Apply VAT to consumer sales from IP addresses within the EU, even if the billing address is outside the EU.', 'invoicing' ) . '<br><font style="color:red">' . __( 'Do not disable unless you know what you are doing.', 'invoicing' ) . '</font>', |
|
260 | + 'name' => __('Enable VAT Rules', 'invoicing'), |
|
261 | + 'desc' => __('Apply VAT to consumer sales from IP addresses within the EU, even if the billing address is outside the EU.', 'invoicing') . '<br><font style="color:red">' . __('Do not disable unless you know what you are doing.', 'invoicing') . '</font>', |
|
262 | 262 | 'type' => 'checkbox', |
263 | 263 | 'std' => '1' |
264 | 264 | ); |
@@ -274,8 +274,8 @@ discard block |
||
274 | 274 | |
275 | 275 | $vat_settings['vat_prevent_b2c_purchase'] = array( |
276 | 276 | 'id' => 'vat_prevent_b2c_purchase', |
277 | - 'name' => __( 'Prevent EU B2C Sales', 'invoicing' ), |
|
278 | - 'desc' => __( 'Enable this option if you are not registered for VAT in the EU.', 'invoicing' ), |
|
277 | + 'name' => __('Prevent EU B2C Sales', 'invoicing'), |
|
278 | + 'desc' => __('Enable this option if you are not registered for VAT in the EU.', 'invoicing'), |
|
279 | 279 | 'type' => 'checkbox' |
280 | 280 | ); |
281 | 281 | |
@@ -283,22 +283,22 @@ discard block |
||
283 | 283 | |
284 | 284 | $vat_settings['vat_same_country_rule'] = array( |
285 | 285 | 'id' => 'vat_same_country_rule', |
286 | - 'name' => __( 'Same Country Rule', 'invoicing' ), |
|
287 | - 'desc' => __( 'Select how you want to handle VAT charge if sales are in the same country as the base country.', 'invoicing' ), |
|
286 | + 'name' => __('Same Country Rule', 'invoicing'), |
|
287 | + 'desc' => __('Select how you want to handle VAT charge if sales are in the same country as the base country.', 'invoicing'), |
|
288 | 288 | 'type' => 'select', |
289 | 289 | 'options' => array( |
290 | - '' => __( 'Normal', 'invoicing' ), |
|
291 | - 'no' => __( 'No VAT', 'invoicing' ), |
|
292 | - 'always' => __( 'Always apply VAT', 'invoicing' ), |
|
290 | + '' => __('Normal', 'invoicing'), |
|
291 | + 'no' => __('No VAT', 'invoicing'), |
|
292 | + 'always' => __('Always apply VAT', 'invoicing'), |
|
293 | 293 | ), |
294 | - 'placeholder' => __( 'Select an option', 'invoicing' ), |
|
294 | + 'placeholder' => __('Select an option', 'invoicing'), |
|
295 | 295 | 'std' => '', |
296 | 296 | 'class' => 'wpi_select2', |
297 | 297 | ); |
298 | 298 | |
299 | 299 | $vat_settings['vat_checkout_title'] = array( |
300 | 300 | 'id' => 'vat_checkout_title', |
301 | - 'name' => '<h3>' . __( 'Checkout Fields', 'invoicing' ) . '</h3>', |
|
301 | + 'name' => '<h3>' . __('Checkout Fields', 'invoicing') . '</h3>', |
|
302 | 302 | 'desc' => '', |
303 | 303 | 'type' => 'header', |
304 | 304 | 'size' => 'regular' |
@@ -306,14 +306,14 @@ discard block |
||
306 | 306 | |
307 | 307 | $vat_settings['vat_disable_fields'] = array( |
308 | 308 | 'id' => 'vat_disable_fields', |
309 | - 'name' => __( 'Disable VAT Fields', 'invoicing' ), |
|
310 | - 'desc' => __( 'Disable VAT fields if Invoicing is being used for GST.', 'invoicing' ) . '<br><font style="color:red">' . __( 'Do not disable if you have enabled Prevent EU B2C sales, otherwise Prevent EU B2C sales setting will not work.', 'invoicing' ) . '</font>', |
|
309 | + 'name' => __('Disable VAT Fields', 'invoicing'), |
|
310 | + 'desc' => __('Disable VAT fields if Invoicing is being used for GST.', 'invoicing') . '<br><font style="color:red">' . __('Do not disable if you have enabled Prevent EU B2C sales, otherwise Prevent EU B2C sales setting will not work.', 'invoicing') . '</font>', |
|
311 | 311 | 'type' => 'checkbox' |
312 | 312 | ); |
313 | 313 | |
314 | 314 | $vat_settings['vat_ip_lookup'] = array( |
315 | 315 | 'id' => 'vat_ip_lookup', |
316 | - 'name' => __( 'IP Country Look-up', 'invoicing' ), |
|
316 | + 'name' => __('IP Country Look-up', 'invoicing'), |
|
317 | 317 | 'type' => 'vat_ip_lookup', |
318 | 318 | 'size' => 'regular', |
319 | 319 | 'std' => 'default', |
@@ -322,21 +322,21 @@ discard block |
||
322 | 322 | |
323 | 323 | $vat_settings['hide_ip_address'] = array( |
324 | 324 | 'id' => 'hide_ip_address', |
325 | - 'name' => __( 'Hide IP Info at Checkout', 'invoicing' ), |
|
326 | - 'desc' => __( 'Hide the user IP info at checkout.', 'invoicing' ), |
|
325 | + 'name' => __('Hide IP Info at Checkout', 'invoicing'), |
|
326 | + 'desc' => __('Hide the user IP info at checkout.', 'invoicing'), |
|
327 | 327 | 'type' => 'checkbox' |
328 | 328 | ); |
329 | 329 | |
330 | 330 | $vat_settings['vat_ip_country_default'] = array( |
331 | 331 | 'id' => 'vat_ip_country_default', |
332 | - 'name' => __( 'Enable IP Country as Default', 'invoicing' ), |
|
333 | - 'desc' => __( 'Show the country of the users IP as the default country, otherwise the site default country will be used.', 'invoicing' ), |
|
332 | + 'name' => __('Enable IP Country as Default', 'invoicing'), |
|
333 | + 'desc' => __('Show the country of the users IP as the default country, otherwise the site default country will be used.', 'invoicing'), |
|
334 | 334 | 'type' => 'checkbox' |
335 | 335 | ); |
336 | 336 | |
337 | 337 | $vat_settings['vies_validation_title'] = array( |
338 | 338 | 'id' => 'vies_validation_title', |
339 | - 'name' => '<h3>' . __( 'VIES Validation', 'invoicing' ) . '</h3>', |
|
339 | + 'name' => '<h3>' . __('VIES Validation', 'invoicing') . '</h3>', |
|
340 | 340 | 'desc' => '', |
341 | 341 | 'type' => 'header', |
342 | 342 | 'size' => 'regular' |
@@ -344,37 +344,37 @@ discard block |
||
344 | 344 | |
345 | 345 | $vat_settings['vat_vies_check'] = array( |
346 | 346 | 'id' => 'vat_vies_check', |
347 | - 'name' => __( 'Disable VIES VAT ID Check', 'invoicing' ), |
|
348 | - 'desc' => wp_sprintf( __( 'Prevent VAT numbers from being validated by the %sEU VIES System.%s', 'invoicing' ), '<a href="http://ec.europa.eu/taxation_customs/vies/" target="_blank">', '</a>' ), |
|
347 | + 'name' => __('Disable VIES VAT ID Check', 'invoicing'), |
|
348 | + 'desc' => wp_sprintf(__('Prevent VAT numbers from being validated by the %sEU VIES System.%s', 'invoicing'), '<a href="http://ec.europa.eu/taxation_customs/vies/" target="_blank">', '</a>'), |
|
349 | 349 | 'type' => 'checkbox' |
350 | 350 | ); |
351 | 351 | |
352 | 352 | $vat_settings['vat_disable_company_name_check'] = array( |
353 | 353 | 'id' => 'vat_disable_company_name_check', |
354 | - 'name' => __( 'Disable VIES Name Check', 'invoicing' ), |
|
355 | - 'desc' => wp_sprintf( __( 'Prevent company name from being validated by the %sEU VIES System.%s', 'invoicing' ), '<a href="http://ec.europa.eu/taxation_customs/vies/" target="_blank">', '</a>' ), |
|
354 | + 'name' => __('Disable VIES Name Check', 'invoicing'), |
|
355 | + 'desc' => wp_sprintf(__('Prevent company name from being validated by the %sEU VIES System.%s', 'invoicing'), '<a href="http://ec.europa.eu/taxation_customs/vies/" target="_blank">', '</a>'), |
|
356 | 356 | 'type' => 'checkbox' |
357 | 357 | ); |
358 | 358 | |
359 | 359 | $vat_settings['vat_offline_check'] = array( |
360 | 360 | 'id' => 'vat_offline_check', |
361 | - 'name' => __( 'Disable Basic Checks', 'invoicing' ), |
|
362 | - 'desc' => __( 'Disable basic JS checks for correct format of VAT number. (Not Recommended)', 'invoicing' ), |
|
361 | + 'name' => __('Disable Basic Checks', 'invoicing'), |
|
362 | + 'desc' => __('Disable basic JS checks for correct format of VAT number. (Not Recommended)', 'invoicing'), |
|
363 | 363 | 'type' => 'checkbox' |
364 | 364 | ); |
365 | 365 | |
366 | 366 | |
367 | 367 | $settings['vat'] = $vat_settings; |
368 | 368 | |
369 | - if ( self::allow_vat_classes() ) { |
|
369 | + if (self::allow_vat_classes()) { |
|
370 | 370 | $settings['vat_rates'] = self::vat_rates_settings(); |
371 | 371 | } |
372 | 372 | |
373 | 373 | $eu_fallback_rate = array( |
374 | 374 | 'id' => 'eu_fallback_rate', |
375 | - 'name' => '<h3>' . __( 'VAT rate for EU member states', 'invoicing' ) . '</h3>', |
|
375 | + 'name' => '<h3>' . __('VAT rate for EU member states', 'invoicing') . '</h3>', |
|
376 | 376 | 'type' => 'eu_fallback_rate', |
377 | - 'desc' => __( 'Enter the VAT rate to be charged for EU member states. You can edit the rates for each member state when a country rate has been set up by pressing this button.', 'invoicing' ), |
|
377 | + 'desc' => __('Enter the VAT rate to be charged for EU member states. You can edit the rates for each member state when a country rate has been set up by pressing this button.', 'invoicing'), |
|
378 | 378 | 'std' => '20', |
379 | 379 | 'size' => 'small' |
380 | 380 | ); |
@@ -390,11 +390,11 @@ discard block |
||
390 | 390 | $database_url = 'http' . (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] === 'on' ? 's' : '') . '://geolite.maxmind.com/download/geoip/database/'; |
391 | 391 | $destination_dir = $upload_dir['basedir'] . '/invoicing'; |
392 | 392 | |
393 | - if ( !is_dir( $destination_dir ) ) { |
|
394 | - mkdir( $destination_dir ); |
|
393 | + if (!is_dir($destination_dir)) { |
|
394 | + mkdir($destination_dir); |
|
395 | 395 | } |
396 | 396 | |
397 | - $database_files = array( |
|
397 | + $database_files = array( |
|
398 | 398 | 'country' => array( |
399 | 399 | 'source' => $database_url . 'GeoLite2-Country.mmdb.gz', |
400 | 400 | 'destination' => $destination_dir . '/GeoLite2-Country.mmdb', |
@@ -405,57 +405,57 @@ discard block |
||
405 | 405 | ) |
406 | 406 | ); |
407 | 407 | |
408 | - foreach( $database_files as $database => $files ) { |
|
409 | - $result = self::geoip2_download_file( $files['source'], $files['destination'] ); |
|
408 | + foreach ($database_files as $database => $files) { |
|
409 | + $result = self::geoip2_download_file($files['source'], $files['destination']); |
|
410 | 410 | |
411 | - if ( empty( $result['success'] ) ) { |
|
411 | + if (empty($result['success'])) { |
|
412 | 412 | echo $result['message']; |
413 | 413 | exit; |
414 | 414 | } |
415 | 415 | |
416 | - wpinv_update_option( 'wpinv_geoip2_date_updated', current_time( 'timestamp' ) ); |
|
417 | - echo sprintf(__( 'GeoIP2 %s database updated successfully.', 'invoicing' ), $database ) . ' '; |
|
416 | + wpinv_update_option('wpinv_geoip2_date_updated', current_time('timestamp')); |
|
417 | + echo sprintf(__('GeoIP2 %s database updated successfully.', 'invoicing'), $database) . ' '; |
|
418 | 418 | } |
419 | 419 | |
420 | 420 | exit; |
421 | 421 | } |
422 | 422 | |
423 | - public static function geoip2_download_file( $source_url, $destination_file ) { |
|
423 | + public static function geoip2_download_file($source_url, $destination_file) { |
|
424 | 424 | $success = false; |
425 | 425 | $message = ''; |
426 | 426 | |
427 | - if ( !function_exists( 'download_url' ) ) { |
|
428 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
427 | + if (!function_exists('download_url')) { |
|
428 | + require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
429 | 429 | } |
430 | 430 | |
431 | - $temp_file = download_url( $source_url ); |
|
431 | + $temp_file = download_url($source_url); |
|
432 | 432 | |
433 | - if ( is_wp_error( $temp_file ) ) { |
|
434 | - $message = sprintf( __( 'Error while downloading GeoIp2 database( %s ): %s', 'invoicing' ), $source_url, $temp_file->get_error_message() ); |
|
433 | + if (is_wp_error($temp_file)) { |
|
434 | + $message = sprintf(__('Error while downloading GeoIp2 database( %s ): %s', 'invoicing'), $source_url, $temp_file->get_error_message()); |
|
435 | 435 | } else { |
436 | - $handle = gzopen( $temp_file, 'rb' ); |
|
436 | + $handle = gzopen($temp_file, 'rb'); |
|
437 | 437 | |
438 | - if ( $handle ) { |
|
439 | - $fopen = fopen( $destination_file, 'wb' ); |
|
440 | - if ( $fopen ) { |
|
441 | - while ( ( $data = gzread( $handle, 4096 ) ) != false ) { |
|
442 | - fwrite( $fopen, $data ); |
|
438 | + if ($handle) { |
|
439 | + $fopen = fopen($destination_file, 'wb'); |
|
440 | + if ($fopen) { |
|
441 | + while (($data = gzread($handle, 4096)) != false) { |
|
442 | + fwrite($fopen, $data); |
|
443 | 443 | } |
444 | 444 | |
445 | - gzclose( $handle ); |
|
446 | - fclose( $fopen ); |
|
445 | + gzclose($handle); |
|
446 | + fclose($fopen); |
|
447 | 447 | |
448 | 448 | $success = true; |
449 | 449 | } else { |
450 | - gzclose( $handle ); |
|
451 | - $message = sprintf( __( 'Error could not open destination GeoIp2 database file for writing: %s', 'invoicing' ), $destination_file ); |
|
450 | + gzclose($handle); |
|
451 | + $message = sprintf(__('Error could not open destination GeoIp2 database file for writing: %s', 'invoicing'), $destination_file); |
|
452 | 452 | } |
453 | 453 | } else { |
454 | - $message = sprintf( __( 'Error could not open GeoIp2 database file for reading: %s', 'invoicing' ), $temp_file ); |
|
454 | + $message = sprintf(__('Error could not open GeoIp2 database file for reading: %s', 'invoicing'), $temp_file); |
|
455 | 455 | } |
456 | 456 | |
457 | - if ( file_exists( $temp_file ) ) { |
|
458 | - unlink( $temp_file ); |
|
457 | + if (file_exists($temp_file)) { |
|
458 | + unlink($temp_file); |
|
459 | 459 | } |
460 | 460 | } |
461 | 461 | |
@@ -467,11 +467,11 @@ discard block |
||
467 | 467 | } |
468 | 468 | |
469 | 469 | public static function load_geoip2() { |
470 | - if ( defined( 'WPINV_GEOIP2_LODDED' ) ) { |
|
470 | + if (defined('WPINV_GEOIP2_LODDED')) { |
|
471 | 471 | return; |
472 | 472 | } |
473 | 473 | |
474 | - if ( !class_exists( '\MaxMind\Db\Reader' ) ) { |
|
474 | + if (!class_exists('\MaxMind\Db\Reader')) { |
|
475 | 475 | $maxmind_db_files = array( |
476 | 476 | 'Reader/Decoder.php', |
477 | 477 | 'Reader/InvalidDatabaseException.php', |
@@ -480,12 +480,12 @@ discard block |
||
480 | 480 | 'Reader.php', |
481 | 481 | ); |
482 | 482 | |
483 | - foreach ( $maxmind_db_files as $key => $file ) { |
|
484 | - require_once( WPINV_PLUGIN_DIR . 'includes/libraries/MaxMind/Db/' . $file ); |
|
483 | + foreach ($maxmind_db_files as $key => $file) { |
|
484 | + require_once(WPINV_PLUGIN_DIR . 'includes/libraries/MaxMind/Db/' . $file); |
|
485 | 485 | } |
486 | 486 | } |
487 | 487 | |
488 | - if ( !class_exists( '\GeoIp2\Database\Reader' ) ) { |
|
488 | + if (!class_exists('\GeoIp2\Database\Reader')) { |
|
489 | 489 | $geoip2_files = array( |
490 | 490 | 'ProviderInterface.php', |
491 | 491 | 'Compat/JsonSerializable.php', |
@@ -519,23 +519,23 @@ discard block |
||
519 | 519 | 'WebService/Client.php', |
520 | 520 | ); |
521 | 521 | |
522 | - foreach ( $geoip2_files as $key => $file ) { |
|
523 | - require_once( WPINV_PLUGIN_DIR . 'includes/libraries/GeoIp2/' . $file ); |
|
522 | + foreach ($geoip2_files as $key => $file) { |
|
523 | + require_once(WPINV_PLUGIN_DIR . 'includes/libraries/GeoIp2/' . $file); |
|
524 | 524 | } |
525 | 525 | } |
526 | 526 | |
527 | - define( 'WPINV_GEOIP2_LODDED', true ); |
|
527 | + define('WPINV_GEOIP2_LODDED', true); |
|
528 | 528 | } |
529 | 529 | |
530 | 530 | public static function geoip2_country_dbfile() { |
531 | 531 | $upload_dir = wp_upload_dir(); |
532 | 532 | |
533 | - if ( !isset( $upload_dir['basedir'] ) ) { |
|
533 | + if (!isset($upload_dir['basedir'])) { |
|
534 | 534 | return false; |
535 | 535 | } |
536 | 536 | |
537 | 537 | $filename = $upload_dir['basedir'] . '/invoicing/GeoLite2-Country.mmdb'; |
538 | - if ( !file_exists( $filename ) ) { |
|
538 | + if (!file_exists($filename)) { |
|
539 | 539 | return false; |
540 | 540 | } |
541 | 541 | |
@@ -545,12 +545,12 @@ discard block |
||
545 | 545 | public static function geoip2_city_dbfile() { |
546 | 546 | $upload_dir = wp_upload_dir(); |
547 | 547 | |
548 | - if ( !isset( $upload_dir['basedir'] ) ) { |
|
548 | + if (!isset($upload_dir['basedir'])) { |
|
549 | 549 | return false; |
550 | 550 | } |
551 | 551 | |
552 | 552 | $filename = $upload_dir['basedir'] . '/invoicing/GeoLite2-City.mmdb'; |
553 | - if ( !file_exists( $filename ) ) { |
|
553 | + if (!file_exists($filename)) { |
|
554 | 554 | return false; |
555 | 555 | } |
556 | 556 | |
@@ -561,10 +561,10 @@ discard block |
||
561 | 561 | try { |
562 | 562 | self::load_geoip2(); |
563 | 563 | |
564 | - if ( $filename = self::geoip2_country_dbfile() ) { |
|
565 | - return new \GeoIp2\Database\Reader( $filename ); |
|
564 | + if ($filename = self::geoip2_country_dbfile()) { |
|
565 | + return new \GeoIp2\Database\Reader($filename); |
|
566 | 566 | } |
567 | - } catch( Exception $e ) { |
|
567 | + } catch (Exception $e) { |
|
568 | 568 | return false; |
569 | 569 | } |
570 | 570 | |
@@ -575,173 +575,173 @@ discard block |
||
575 | 575 | try { |
576 | 576 | self::load_geoip2(); |
577 | 577 | |
578 | - if ( $filename = self::geoip2_city_dbfile() ) { |
|
579 | - return new \GeoIp2\Database\Reader( $filename ); |
|
578 | + if ($filename = self::geoip2_city_dbfile()) { |
|
579 | + return new \GeoIp2\Database\Reader($filename); |
|
580 | 580 | } |
581 | - } catch( Exception $e ) { |
|
581 | + } catch (Exception $e) { |
|
582 | 582 | return false; |
583 | 583 | } |
584 | 584 | |
585 | 585 | return false; |
586 | 586 | } |
587 | 587 | |
588 | - public static function geoip2_country_record( $ip_address ) { |
|
588 | + public static function geoip2_country_record($ip_address) { |
|
589 | 589 | try { |
590 | 590 | $reader = self::geoip2_country_reader(); |
591 | 591 | |
592 | - if ( $reader ) { |
|
593 | - $record = $reader->country( $ip_address ); |
|
592 | + if ($reader) { |
|
593 | + $record = $reader->country($ip_address); |
|
594 | 594 | |
595 | - if ( !empty( $record->country->isoCode ) ) { |
|
595 | + if (!empty($record->country->isoCode)) { |
|
596 | 596 | return $record; |
597 | 597 | } |
598 | 598 | } |
599 | - } catch(\InvalidArgumentException $e) { |
|
600 | - wpinv_error_log( $e->getMessage(), 'GeoIp2 Lookup( ' . $ip_address . ' )' ); |
|
599 | + } catch (\InvalidArgumentException $e) { |
|
600 | + wpinv_error_log($e->getMessage(), 'GeoIp2 Lookup( ' . $ip_address . ' )'); |
|
601 | 601 | |
602 | 602 | return false; |
603 | - } catch(\GeoIp2\Exception\AddressNotFoundException $e) { |
|
604 | - wpinv_error_log( $e->getMessage(), 'GeoIp2 Lookup( ' . $ip_address . ' )' ); |
|
603 | + } catch (\GeoIp2\Exception\AddressNotFoundException $e) { |
|
604 | + wpinv_error_log($e->getMessage(), 'GeoIp2 Lookup( ' . $ip_address . ' )'); |
|
605 | 605 | |
606 | 606 | return false; |
607 | - } catch( Exception $e ) { |
|
607 | + } catch (Exception $e) { |
|
608 | 608 | return false; |
609 | 609 | } |
610 | 610 | |
611 | 611 | return false; |
612 | 612 | } |
613 | 613 | |
614 | - public static function geoip2_city_record( $ip_address ) { |
|
614 | + public static function geoip2_city_record($ip_address) { |
|
615 | 615 | try { |
616 | 616 | $reader = self::geoip2_city_reader(); |
617 | 617 | |
618 | - if ( $reader ) { |
|
619 | - $record = $reader->city( $ip_address ); |
|
618 | + if ($reader) { |
|
619 | + $record = $reader->city($ip_address); |
|
620 | 620 | |
621 | - if ( !empty( $record->country->isoCode ) ) { |
|
621 | + if (!empty($record->country->isoCode)) { |
|
622 | 622 | return $record; |
623 | 623 | } |
624 | 624 | } |
625 | - } catch(\InvalidArgumentException $e) { |
|
626 | - wpinv_error_log( $e->getMessage(), 'GeoIp2 Lookup( ' . $ip_address . ' )' ); |
|
625 | + } catch (\InvalidArgumentException $e) { |
|
626 | + wpinv_error_log($e->getMessage(), 'GeoIp2 Lookup( ' . $ip_address . ' )'); |
|
627 | 627 | |
628 | 628 | return false; |
629 | - } catch(\GeoIp2\Exception\AddressNotFoundException $e) { |
|
630 | - wpinv_error_log( $e->getMessage(), 'GeoIp2 Lookup( ' . $ip_address . ' )' ); |
|
629 | + } catch (\GeoIp2\Exception\AddressNotFoundException $e) { |
|
630 | + wpinv_error_log($e->getMessage(), 'GeoIp2 Lookup( ' . $ip_address . ' )'); |
|
631 | 631 | |
632 | 632 | return false; |
633 | - } catch( Exception $e ) { |
|
633 | + } catch (Exception $e) { |
|
634 | 634 | return false; |
635 | 635 | } |
636 | 636 | |
637 | 637 | return false; |
638 | 638 | } |
639 | 639 | |
640 | - public static function geoip2_country_code( $ip_address ) { |
|
641 | - $record = self::geoip2_country_record( $ip_address ); |
|
642 | - return !empty( $record->country->isoCode ) ? $record->country->isoCode : wpinv_get_default_country(); |
|
640 | + public static function geoip2_country_code($ip_address) { |
|
641 | + $record = self::geoip2_country_record($ip_address); |
|
642 | + return !empty($record->country->isoCode) ? $record->country->isoCode : wpinv_get_default_country(); |
|
643 | 643 | } |
644 | 644 | |
645 | 645 | // Find country by IP address. |
646 | - public static function get_country_by_ip( $ip = '' ) { |
|
646 | + public static function get_country_by_ip($ip = '') { |
|
647 | 647 | global $wpinv_ip_address_country; |
648 | 648 | |
649 | - if ( !empty( $wpinv_ip_address_country ) ) { |
|
649 | + if (!empty($wpinv_ip_address_country)) { |
|
650 | 650 | return $wpinv_ip_address_country; |
651 | 651 | } |
652 | 652 | |
653 | - if ( empty( $ip ) ) { |
|
653 | + if (empty($ip)) { |
|
654 | 654 | $ip = wpinv_get_ip(); |
655 | 655 | } |
656 | 656 | |
657 | - $ip_country_service = wpinv_get_option( 'vat_ip_lookup' ); |
|
658 | - $is_default = empty( $ip_country_service ) || $ip_country_service === 'default' ? true : false; |
|
657 | + $ip_country_service = wpinv_get_option('vat_ip_lookup'); |
|
658 | + $is_default = empty($ip_country_service) || $ip_country_service === 'default' ? true : false; |
|
659 | 659 | |
660 | - if ( !empty( $ip ) && $ip !== '127.0.0.1' ) { // For 127.0.0.1(localhost) use default country. |
|
661 | - if ( function_exists( 'geoip_country_code_by_name') && ( $ip_country_service === 'geoip' || $is_default ) ) { |
|
660 | + if (!empty($ip) && $ip !== '127.0.0.1') { // For 127.0.0.1(localhost) use default country. |
|
661 | + if (function_exists('geoip_country_code_by_name') && ($ip_country_service === 'geoip' || $is_default)) { |
|
662 | 662 | try { |
663 | - $wpinv_ip_address_country = geoip_country_code_by_name( $ip ); |
|
664 | - } catch( Exception $e ) { |
|
665 | - wpinv_error_log( $e->getMessage(), 'GeoIP Lookup( ' . $ip . ' )' ); |
|
663 | + $wpinv_ip_address_country = geoip_country_code_by_name($ip); |
|
664 | + } catch (Exception $e) { |
|
665 | + wpinv_error_log($e->getMessage(), 'GeoIP Lookup( ' . $ip . ' )'); |
|
666 | 666 | } |
667 | - } else if ( self::geoip2_country_dbfile() && ( $ip_country_service === 'geoip2' || $is_default ) ) { |
|
668 | - $wpinv_ip_address_country = self::geoip2_country_code( $ip ); |
|
669 | - } else if ( function_exists( 'simplexml_load_file' ) && ( $ip_country_service === 'geoplugin' || $is_default ) ) { |
|
670 | - $load_xml = simplexml_load_file( 'http://www.geoplugin.net/xml.gp?ip=' . $ip ); |
|
667 | + } else if (self::geoip2_country_dbfile() && ($ip_country_service === 'geoip2' || $is_default)) { |
|
668 | + $wpinv_ip_address_country = self::geoip2_country_code($ip); |
|
669 | + } else if (function_exists('simplexml_load_file') && ($ip_country_service === 'geoplugin' || $is_default)) { |
|
670 | + $load_xml = simplexml_load_file('http://www.geoplugin.net/xml.gp?ip=' . $ip); |
|
671 | 671 | |
672 | - if ( !empty( $load_xml ) && !empty( $load_xml->geoplugin_countryCode ) ) { |
|
673 | - $wpinv_ip_address_country = (string)$load_xml->geoplugin_countryCode; |
|
672 | + if (!empty($load_xml) && !empty($load_xml->geoplugin_countryCode)) { |
|
673 | + $wpinv_ip_address_country = (string) $load_xml->geoplugin_countryCode; |
|
674 | 674 | } |
675 | 675 | } |
676 | 676 | } |
677 | 677 | |
678 | - if ( empty( $wpinv_ip_address_country ) ) { |
|
678 | + if (empty($wpinv_ip_address_country)) { |
|
679 | 679 | $wpinv_ip_address_country = wpinv_get_default_country(); |
680 | 680 | } |
681 | 681 | |
682 | 682 | return $wpinv_ip_address_country; |
683 | 683 | } |
684 | 684 | |
685 | - public static function sanitize_vat_settings( $input ) { |
|
685 | + public static function sanitize_vat_settings($input) { |
|
686 | 686 | global $wpinv_options; |
687 | 687 | |
688 | 688 | $valid = false; |
689 | 689 | $message = ''; |
690 | 690 | |
691 | - if ( !empty( $wpinv_options['vat_vies_check'] ) ) { |
|
692 | - if ( empty( $wpinv_options['vat_offline_check'] ) ) { |
|
693 | - $valid = self::offline_check( $input['vat_number'] ); |
|
691 | + if (!empty($wpinv_options['vat_vies_check'])) { |
|
692 | + if (empty($wpinv_options['vat_offline_check'])) { |
|
693 | + $valid = self::offline_check($input['vat_number']); |
|
694 | 694 | } else { |
695 | 695 | $valid = true; |
696 | 696 | } |
697 | 697 | |
698 | - $message = $valid ? '' : __( 'VAT number not validated', 'invoicing' ); |
|
698 | + $message = $valid ? '' : __('VAT number not validated', 'invoicing'); |
|
699 | 699 | } else { |
700 | - $result = self::check_vat( $input['vat_number'] ); |
|
700 | + $result = self::check_vat($input['vat_number']); |
|
701 | 701 | |
702 | - if ( empty( $result['valid'] ) ) { |
|
702 | + if (empty($result['valid'])) { |
|
703 | 703 | $valid = false; |
704 | 704 | $message = $result['message']; |
705 | 705 | } else { |
706 | - $valid = ( isset( $result['company'] ) && ( $result['company'] == '---' || ( strcasecmp( trim( $result['company'] ), trim( $input['vat_company_name'] ) ) == 0 ) ) ) || !empty( $wpinv_options['vat_disable_company_name_check'] ); |
|
707 | - $message = $valid ? '' : __( 'The company name associated with the VAT number provided is not the same as the company name provided.', 'invoicing' ); |
|
706 | + $valid = (isset($result['company']) && ($result['company'] == '---' || (strcasecmp(trim($result['company']), trim($input['vat_company_name'])) == 0))) || !empty($wpinv_options['vat_disable_company_name_check']); |
|
707 | + $message = $valid ? '' : __('The company name associated with the VAT number provided is not the same as the company name provided.', 'invoicing'); |
|
708 | 708 | } |
709 | 709 | } |
710 | 710 | |
711 | - if ( $message && self::is_vat_validated() != $valid ) { |
|
712 | - add_settings_error( 'wpinv-notices', '', $message, ( $valid ? 'updated' : 'error' ) ); |
|
711 | + if ($message && self::is_vat_validated() != $valid) { |
|
712 | + add_settings_error('wpinv-notices', '', $message, ($valid ? 'updated' : 'error')); |
|
713 | 713 | } |
714 | 714 | |
715 | 715 | $input['vat_valid'] = $valid; |
716 | 716 | return $input; |
717 | 717 | } |
718 | 718 | |
719 | - public static function sanitize_vat_rates( $input ) { |
|
720 | - if( !wpinv_current_user_can_manage_invoicing() ) { |
|
721 | - add_settings_error( 'wpinv-notices', '', __( 'Your account does not have permission to add rate classes.', 'invoicing' ), 'error' ); |
|
719 | + public static function sanitize_vat_rates($input) { |
|
720 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
721 | + add_settings_error('wpinv-notices', '', __('Your account does not have permission to add rate classes.', 'invoicing'), 'error'); |
|
722 | 722 | return $input; |
723 | 723 | } |
724 | 724 | |
725 | 725 | $vat_classes = self::get_rate_classes(); |
726 | - $vat_class = !empty( $_REQUEST['wpi_vat_class'] ) && isset( $vat_classes[$_REQUEST['wpi_vat_class']] )? sanitize_text_field( $_REQUEST['wpi_vat_class'] ) : ''; |
|
726 | + $vat_class = !empty($_REQUEST['wpi_vat_class']) && isset($vat_classes[$_REQUEST['wpi_vat_class']]) ? sanitize_text_field($_REQUEST['wpi_vat_class']) : ''; |
|
727 | 727 | |
728 | - if ( empty( $vat_class ) ) { |
|
729 | - add_settings_error( 'wpinv-notices', '', __( 'No valid VAT rates class contained in the request to save rates.', 'invoicing' ), 'error' ); |
|
728 | + if (empty($vat_class)) { |
|
729 | + add_settings_error('wpinv-notices', '', __('No valid VAT rates class contained in the request to save rates.', 'invoicing'), 'error'); |
|
730 | 730 | |
731 | 731 | return $input; |
732 | 732 | } |
733 | 733 | |
734 | - $new_rates = ! empty( $_POST['vat_rates'] ) ? array_values( $_POST['vat_rates'] ) : array(); |
|
734 | + $new_rates = !empty($_POST['vat_rates']) ? array_values($_POST['vat_rates']) : array(); |
|
735 | 735 | |
736 | - if ( $vat_class === '_standard' ) { |
|
736 | + if ($vat_class === '_standard') { |
|
737 | 737 | // Save the active rates in the invoice settings |
738 | - update_option( 'wpinv_tax_rates', $new_rates ); |
|
738 | + update_option('wpinv_tax_rates', $new_rates); |
|
739 | 739 | } else { |
740 | 740 | // Get the existing set of rates |
741 | 741 | $rates = self::get_non_standard_rates(); |
742 | 742 | $rates[$vat_class] = $new_rates; |
743 | 743 | |
744 | - update_option( 'wpinv_vat_rates', $rates ); |
|
744 | + update_option('wpinv_vat_rates', $rates); |
|
745 | 745 | } |
746 | 746 | |
747 | 747 | return $input; |
@@ -751,71 +751,71 @@ discard block |
||
751 | 751 | $response = array(); |
752 | 752 | $response['success'] = false; |
753 | 753 | |
754 | - if ( !wpinv_current_user_can_manage_invoicing() ) { |
|
755 | - $response['error'] = __( 'Invalid access!', 'invoicing' ); |
|
756 | - wp_send_json( $response ); |
|
754 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
755 | + $response['error'] = __('Invalid access!', 'invoicing'); |
|
756 | + wp_send_json($response); |
|
757 | 757 | } |
758 | 758 | |
759 | - $vat_class_name = !empty( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : false; |
|
760 | - $vat_class_desc = !empty( $_POST['desc'] ) ? sanitize_text_field( $_POST['desc'] ) : false; |
|
759 | + $vat_class_name = !empty($_POST['name']) ? sanitize_text_field($_POST['name']) : false; |
|
760 | + $vat_class_desc = !empty($_POST['desc']) ? sanitize_text_field($_POST['desc']) : false; |
|
761 | 761 | |
762 | - if ( empty( $vat_class_name ) ) { |
|
763 | - $response['error'] = __( 'Select the VAT rate name', 'invoicing' ); |
|
764 | - wp_send_json( $response ); |
|
762 | + if (empty($vat_class_name)) { |
|
763 | + $response['error'] = __('Select the VAT rate name', 'invoicing'); |
|
764 | + wp_send_json($response); |
|
765 | 765 | } |
766 | 766 | |
767 | - $vat_classes = (array)self::get_rate_classes(); |
|
767 | + $vat_classes = (array) self::get_rate_classes(); |
|
768 | 768 | |
769 | - if ( !empty( $vat_classes ) && in_array( strtolower( $vat_class_name ), array_map( 'strtolower', array_values( $vat_classes ) ) ) ) { |
|
770 | - $response['error'] = wp_sprintf( __( 'A VAT Rate name "%s" already exists', 'invoicing' ), $vat_class_name ); |
|
771 | - wp_send_json( $response ); |
|
769 | + if (!empty($vat_classes) && in_array(strtolower($vat_class_name), array_map('strtolower', array_values($vat_classes)))) { |
|
770 | + $response['error'] = wp_sprintf(__('A VAT Rate name "%s" already exists', 'invoicing'), $vat_class_name); |
|
771 | + wp_send_json($response); |
|
772 | 772 | } |
773 | 773 | |
774 | - $rate_class_key = normalize_whitespace( 'wpi-' . $vat_class_name ); |
|
775 | - $rate_class_key = sanitize_key( str_replace( " ", "-", $rate_class_key ) ); |
|
774 | + $rate_class_key = normalize_whitespace('wpi-' . $vat_class_name); |
|
775 | + $rate_class_key = sanitize_key(str_replace(" ", "-", $rate_class_key)); |
|
776 | 776 | |
777 | - $vat_classes = (array)self::get_rate_classes( true ); |
|
778 | - $vat_classes[$rate_class_key] = array( 'name' => $vat_class_name, 'desc' => $vat_class_desc ); |
|
777 | + $vat_classes = (array) self::get_rate_classes(true); |
|
778 | + $vat_classes[$rate_class_key] = array('name' => $vat_class_name, 'desc' => $vat_class_desc); |
|
779 | 779 | |
780 | - update_option( '_wpinv_vat_rate_classes', $vat_classes ); |
|
780 | + update_option('_wpinv_vat_rate_classes', $vat_classes); |
|
781 | 781 | |
782 | 782 | $response['success'] = true; |
783 | - $response['redirect'] = admin_url( 'admin.php?page=wpinv-settings&tab=taxes§ion=vat_rates&wpi_sub=' . $rate_class_key ); |
|
783 | + $response['redirect'] = admin_url('admin.php?page=wpinv-settings&tab=taxes§ion=vat_rates&wpi_sub=' . $rate_class_key); |
|
784 | 784 | |
785 | - wp_send_json( $response ); |
|
785 | + wp_send_json($response); |
|
786 | 786 | } |
787 | 787 | |
788 | 788 | public static function delete_class() { |
789 | 789 | $response = array(); |
790 | 790 | $response['success'] = false; |
791 | 791 | |
792 | - if ( !wpinv_current_user_can_manage_invoicing() || !isset( $_POST['class'] ) ) { |
|
793 | - $response['error'] = __( 'Invalid access!', 'invoicing' ); |
|
794 | - wp_send_json( $response ); |
|
792 | + if (!wpinv_current_user_can_manage_invoicing() || !isset($_POST['class'])) { |
|
793 | + $response['error'] = __('Invalid access!', 'invoicing'); |
|
794 | + wp_send_json($response); |
|
795 | 795 | } |
796 | 796 | |
797 | - $vat_class = isset( $_POST['class'] ) && $_POST['class'] !== '' ? sanitize_text_field( $_POST['class'] ) : false; |
|
798 | - $vat_classes = (array)self::get_rate_classes(); |
|
797 | + $vat_class = isset($_POST['class']) && $_POST['class'] !== '' ? sanitize_text_field($_POST['class']) : false; |
|
798 | + $vat_classes = (array) self::get_rate_classes(); |
|
799 | 799 | |
800 | - if ( !isset( $vat_classes[$vat_class] ) ) { |
|
801 | - $response['error'] = __( 'Requested class does not exists', 'invoicing' ); |
|
802 | - wp_send_json( $response ); |
|
800 | + if (!isset($vat_classes[$vat_class])) { |
|
801 | + $response['error'] = __('Requested class does not exists', 'invoicing'); |
|
802 | + wp_send_json($response); |
|
803 | 803 | } |
804 | 804 | |
805 | - if ( $vat_class == '_new' || $vat_class == '_standard' ) { |
|
806 | - $response['error'] = __( 'You can not delete standard rates class', 'invoicing' ); |
|
807 | - wp_send_json( $response ); |
|
805 | + if ($vat_class == '_new' || $vat_class == '_standard') { |
|
806 | + $response['error'] = __('You can not delete standard rates class', 'invoicing'); |
|
807 | + wp_send_json($response); |
|
808 | 808 | } |
809 | 809 | |
810 | - $vat_classes = (array)self::get_rate_classes( true ); |
|
811 | - unset( $vat_classes[$vat_class] ); |
|
810 | + $vat_classes = (array) self::get_rate_classes(true); |
|
811 | + unset($vat_classes[$vat_class]); |
|
812 | 812 | |
813 | - update_option( '_wpinv_vat_rate_classes', $vat_classes ); |
|
813 | + update_option('_wpinv_vat_rate_classes', $vat_classes); |
|
814 | 814 | |
815 | 815 | $response['success'] = true; |
816 | - $response['redirect'] = admin_url( 'admin.php?page=wpinv-settings&tab=taxes§ion=vat_rates&wpi_sub=_new' ); |
|
816 | + $response['redirect'] = admin_url('admin.php?page=wpinv-settings&tab=taxes§ion=vat_rates&wpi_sub=_new'); |
|
817 | 817 | |
818 | - wp_send_json( $response ); |
|
818 | + wp_send_json($response); |
|
819 | 819 | } |
820 | 820 | |
821 | 821 | public static function update_eu_rates() { |
@@ -824,72 +824,72 @@ discard block |
||
824 | 824 | $response['error'] = null; |
825 | 825 | $response['data'] = null; |
826 | 826 | |
827 | - if ( !wpinv_current_user_can_manage_invoicing() ) { |
|
828 | - $response['error'] = __( 'Invalid access!', 'invoicing' ); |
|
829 | - wp_send_json( $response ); |
|
827 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
828 | + $response['error'] = __('Invalid access!', 'invoicing'); |
|
829 | + wp_send_json($response); |
|
830 | 830 | } |
831 | 831 | |
832 | - $group = !empty( $_POST['group'] ) ? sanitize_text_field( $_POST['group'] ) : ''; |
|
833 | - $euvatrates = self::request_euvatrates( $group ); |
|
832 | + $group = !empty($_POST['group']) ? sanitize_text_field($_POST['group']) : ''; |
|
833 | + $euvatrates = self::request_euvatrates($group); |
|
834 | 834 | |
835 | - if ( !empty( $euvatrates ) ) { |
|
836 | - if ( !empty( $euvatrates['success'] ) && !empty( $euvatrates['rates'] ) ) { |
|
835 | + if (!empty($euvatrates)) { |
|
836 | + if (!empty($euvatrates['success']) && !empty($euvatrates['rates'])) { |
|
837 | 837 | $response['success'] = true; |
838 | 838 | $response['data']['rates'] = $euvatrates['rates']; |
839 | - } else if ( !empty( $euvatrates['error'] ) ) { |
|
839 | + } else if (!empty($euvatrates['error'])) { |
|
840 | 840 | $response['error'] = $euvatrates['error']; |
841 | 841 | } |
842 | 842 | } |
843 | 843 | |
844 | - wp_send_json( $response ); |
|
844 | + wp_send_json($response); |
|
845 | 845 | } |
846 | 846 | |
847 | 847 | public static function hide_vat_fields() { |
848 | - $hide = wpinv_get_option( 'vat_disable_fields' ); |
|
848 | + $hide = wpinv_get_option('vat_disable_fields'); |
|
849 | 849 | |
850 | - return apply_filters( 'wpinv_hide_vat_fields', $hide ); |
|
850 | + return apply_filters('wpinv_hide_vat_fields', $hide); |
|
851 | 851 | } |
852 | 852 | |
853 | 853 | public static function same_country_rule() { |
854 | - $same_country_rule = wpinv_get_option( 'vat_same_country_rule' ); |
|
854 | + $same_country_rule = wpinv_get_option('vat_same_country_rule'); |
|
855 | 855 | |
856 | - return apply_filters( 'wpinv_vat_same_country_rule', $same_country_rule ); |
|
856 | + return apply_filters('wpinv_vat_same_country_rule', $same_country_rule); |
|
857 | 857 | } |
858 | 858 | |
859 | 859 | public static function get_vat_name() { |
860 | - $vat_name = wpinv_get_option( 'vat_name' ); |
|
861 | - $vat_name = !empty( $vat_name ) ? $vat_name : 'VAT'; |
|
860 | + $vat_name = wpinv_get_option('vat_name'); |
|
861 | + $vat_name = !empty($vat_name) ? $vat_name : 'VAT'; |
|
862 | 862 | |
863 | - return apply_filters( 'wpinv_get_owner_vat_name', $vat_name ); |
|
863 | + return apply_filters('wpinv_get_owner_vat_name', $vat_name); |
|
864 | 864 | } |
865 | 865 | |
866 | 866 | public static function get_company_name() { |
867 | - $company_name = wpinv_get_option( 'vat_company_name' ); |
|
867 | + $company_name = wpinv_get_option('vat_company_name'); |
|
868 | 868 | |
869 | - return apply_filters( 'wpinv_get_owner_company_name', $company_name ); |
|
869 | + return apply_filters('wpinv_get_owner_company_name', $company_name); |
|
870 | 870 | } |
871 | 871 | |
872 | 872 | public static function get_vat_number() { |
873 | - $vat_number = wpinv_get_option( 'vat_number' ); |
|
873 | + $vat_number = wpinv_get_option('vat_number'); |
|
874 | 874 | |
875 | - return apply_filters( 'wpinv_get_owner_vat_number', $vat_number ); |
|
875 | + return apply_filters('wpinv_get_owner_vat_number', $vat_number); |
|
876 | 876 | } |
877 | 877 | |
878 | 878 | public static function is_vat_validated() { |
879 | - $validated = self::get_vat_number() && wpinv_get_option( 'vat_valid' ); |
|
879 | + $validated = self::get_vat_number() && wpinv_get_option('vat_valid'); |
|
880 | 880 | |
881 | - return apply_filters( 'wpinv_is_owner_vat_validated', $validated ); |
|
881 | + return apply_filters('wpinv_is_owner_vat_validated', $validated); |
|
882 | 882 | } |
883 | 883 | |
884 | - public static function sanitize_vat( $vat_number, $country_code = '' ) { |
|
885 | - $vat_number = str_replace( array(' ', '.', '-', '_', ',' ), '', strtoupper( trim( $vat_number ) ) ); |
|
884 | + public static function sanitize_vat($vat_number, $country_code = '') { |
|
885 | + $vat_number = str_replace(array(' ', '.', '-', '_', ','), '', strtoupper(trim($vat_number))); |
|
886 | 886 | |
887 | - if ( empty( $country_code ) ) { |
|
888 | - $country_code = substr( $vat_number, 0, 2 ); |
|
887 | + if (empty($country_code)) { |
|
888 | + $country_code = substr($vat_number, 0, 2); |
|
889 | 889 | } |
890 | 890 | |
891 | - if ( strpos( $vat_number , $country_code ) === 0 ) { |
|
892 | - $vat = str_replace( $country_code, '', $vat_number ); |
|
891 | + if (strpos($vat_number, $country_code) === 0) { |
|
892 | + $vat = str_replace($country_code, '', $vat_number); |
|
893 | 893 | } else { |
894 | 894 | $vat = $country_code . $vat_number; |
895 | 895 | } |
@@ -902,140 +902,140 @@ discard block |
||
902 | 902 | return $return; |
903 | 903 | } |
904 | 904 | |
905 | - public static function offline_check( $vat_number, $country_code = '', $formatted = false ) { |
|
906 | - $vat = self::sanitize_vat( $vat_number, $country_code ); |
|
905 | + public static function offline_check($vat_number, $country_code = '', $formatted = false) { |
|
906 | + $vat = self::sanitize_vat($vat_number, $country_code); |
|
907 | 907 | $vat_number = $vat['vat_number']; |
908 | 908 | $country_code = $vat['iso']; |
909 | 909 | $regex = array(); |
910 | 910 | |
911 | - switch ( $country_code ) { |
|
911 | + switch ($country_code) { |
|
912 | 912 | case 'AT': |
913 | - $regex[] = '/^(AT)U(\d{8})$/'; // Austria |
|
913 | + $regex[] = '/^(AT)U(\d{8})$/'; // Austria |
|
914 | 914 | break; |
915 | 915 | case 'BE': |
916 | - $regex[] = '/^(BE)(0?\d{9})$/'; // Belgium |
|
916 | + $regex[] = '/^(BE)(0?\d{9})$/'; // Belgium |
|
917 | 917 | break; |
918 | 918 | case 'BG': |
919 | - $regex[] = '/^(BG)(\d{9,10})$/'; // Bulgaria |
|
919 | + $regex[] = '/^(BG)(\d{9,10})$/'; // Bulgaria |
|
920 | 920 | break; |
921 | 921 | case 'CH': |
922 | 922 | case 'CHE': |
923 | - $regex[] = '/^(CHE)(\d{9})MWST$/'; // Switzerland (Not EU) |
|
923 | + $regex[] = '/^(CHE)(\d{9})MWST$/'; // Switzerland (Not EU) |
|
924 | 924 | break; |
925 | 925 | case 'CY': |
926 | - $regex[] = '/^(CY)([0-5|9]\d{7}[A-Z])$/'; // Cyprus |
|
926 | + $regex[] = '/^(CY)([0-5|9]\d{7}[A-Z])$/'; // Cyprus |
|
927 | 927 | break; |
928 | 928 | case 'CZ': |
929 | - $regex[] = '/^(CZ)(\d{8,13})$/'; // Czech Republic |
|
929 | + $regex[] = '/^(CZ)(\d{8,13})$/'; // Czech Republic |
|
930 | 930 | break; |
931 | 931 | case 'DE': |
932 | - $regex[] = '/^(DE)([1-9]\d{8})$/'; // Germany |
|
932 | + $regex[] = '/^(DE)([1-9]\d{8})$/'; // Germany |
|
933 | 933 | break; |
934 | 934 | case 'DK': |
935 | - $regex[] = '/^(DK)(\d{8})$/'; // Denmark |
|
935 | + $regex[] = '/^(DK)(\d{8})$/'; // Denmark |
|
936 | 936 | break; |
937 | 937 | case 'EE': |
938 | - $regex[] = '/^(EE)(10\d{7})$/'; // Estonia |
|
938 | + $regex[] = '/^(EE)(10\d{7})$/'; // Estonia |
|
939 | 939 | break; |
940 | 940 | case 'EL': |
941 | - $regex[] = '/^(EL)(\d{9})$/'; // Greece |
|
941 | + $regex[] = '/^(EL)(\d{9})$/'; // Greece |
|
942 | 942 | break; |
943 | 943 | case 'ES': |
944 | - $regex[] = '/^(ES)([A-Z]\d{8})$/'; // Spain (National juridical entities) |
|
945 | - $regex[] = '/^(ES)([A-H|N-S|W]\d{7}[A-J])$/'; // Spain (Other juridical entities) |
|
946 | - $regex[] = '/^(ES)([0-9|Y|Z]\d{7}[A-Z])$/'; // Spain (Personal entities type 1) |
|
947 | - $regex[] = '/^(ES)([K|L|M|X]\d{7}[A-Z])$/'; // Spain (Personal entities type 2) |
|
944 | + $regex[] = '/^(ES)([A-Z]\d{8})$/'; // Spain (National juridical entities) |
|
945 | + $regex[] = '/^(ES)([A-H|N-S|W]\d{7}[A-J])$/'; // Spain (Other juridical entities) |
|
946 | + $regex[] = '/^(ES)([0-9|Y|Z]\d{7}[A-Z])$/'; // Spain (Personal entities type 1) |
|
947 | + $regex[] = '/^(ES)([K|L|M|X]\d{7}[A-Z])$/'; // Spain (Personal entities type 2) |
|
948 | 948 | break; |
949 | 949 | case 'EU': |
950 | - $regex[] = '/^(EU)(\d{9})$/'; // EU-type |
|
950 | + $regex[] = '/^(EU)(\d{9})$/'; // EU-type |
|
951 | 951 | break; |
952 | 952 | case 'FI': |
953 | - $regex[] = '/^(FI)(\d{8})$/'; // Finland |
|
953 | + $regex[] = '/^(FI)(\d{8})$/'; // Finland |
|
954 | 954 | break; |
955 | 955 | case 'FR': |
956 | - $regex[] = '/^(FR)(\d{11})$/'; // France (1) |
|
957 | - $regex[] = '/^(FR)[(A-H)|(J-N)|(P-Z)](\d{10})$/'; // France (2) |
|
958 | - $regex[] = '/^(FR)\d[(A-H)|(J-N)|(P-Z)](\d{9})$/'; // France (3) |
|
959 | - $regex[] = '/^(FR)[(A-H)|(J-N)|(P-Z)]{2}(\d{9})$/'; // France (4) |
|
956 | + $regex[] = '/^(FR)(\d{11})$/'; // France (1) |
|
957 | + $regex[] = '/^(FR)[(A-H)|(J-N)|(P-Z)](\d{10})$/'; // France (2) |
|
958 | + $regex[] = '/^(FR)\d[(A-H)|(J-N)|(P-Z)](\d{9})$/'; // France (3) |
|
959 | + $regex[] = '/^(FR)[(A-H)|(J-N)|(P-Z)]{2}(\d{9})$/'; // France (4) |
|
960 | 960 | break; |
961 | 961 | case 'GB': |
962 | - $regex[] = '/^(GB)?(\d{9})$/'; // UK (Standard) |
|
963 | - $regex[] = '/^(GB)?(\d{12})$/'; // UK (Branches) |
|
964 | - $regex[] = '/^(GB)?(GD\d{3})$/'; // UK (Government) |
|
965 | - $regex[] = '/^(GB)?(HA\d{3})$/'; // UK (Health authority) |
|
962 | + $regex[] = '/^(GB)?(\d{9})$/'; // UK (Standard) |
|
963 | + $regex[] = '/^(GB)?(\d{12})$/'; // UK (Branches) |
|
964 | + $regex[] = '/^(GB)?(GD\d{3})$/'; // UK (Government) |
|
965 | + $regex[] = '/^(GB)?(HA\d{3})$/'; // UK (Health authority) |
|
966 | 966 | break; |
967 | 967 | case 'GR': |
968 | - $regex[] = '/^(GR)(\d{8,9})$/'; // Greece |
|
968 | + $regex[] = '/^(GR)(\d{8,9})$/'; // Greece |
|
969 | 969 | break; |
970 | 970 | case 'HR': |
971 | - $regex[] = '/^(HR)(\d{11})$/'; // Croatia |
|
971 | + $regex[] = '/^(HR)(\d{11})$/'; // Croatia |
|
972 | 972 | break; |
973 | 973 | case 'HU': |
974 | - $regex[] = '/^(HU)(\d{8})$/'; // Hungary |
|
974 | + $regex[] = '/^(HU)(\d{8})$/'; // Hungary |
|
975 | 975 | break; |
976 | 976 | case 'IE': |
977 | - $regex[] = '/^(IE)(\d{7}[A-W])$/'; // Ireland (1) |
|
978 | - $regex[] = '/^(IE)([7-9][A-Z\*\+)]\d{5}[A-W])$/'; // Ireland (2) |
|
979 | - $regex[] = '/^(IE)(\d{7}[A-Z][AH])$/'; // Ireland (3) (new format from 1 Jan 2013) |
|
977 | + $regex[] = '/^(IE)(\d{7}[A-W])$/'; // Ireland (1) |
|
978 | + $regex[] = '/^(IE)([7-9][A-Z\*\+)]\d{5}[A-W])$/'; // Ireland (2) |
|
979 | + $regex[] = '/^(IE)(\d{7}[A-Z][AH])$/'; // Ireland (3) (new format from 1 Jan 2013) |
|
980 | 980 | break; |
981 | 981 | case 'IT': |
982 | - $regex[] = '/^(IT)(\d{11})$/'; // Italy |
|
982 | + $regex[] = '/^(IT)(\d{11})$/'; // Italy |
|
983 | 983 | break; |
984 | 984 | case 'LV': |
985 | - $regex[] = '/^(LV)(\d{11})$/'; // Latvia |
|
985 | + $regex[] = '/^(LV)(\d{11})$/'; // Latvia |
|
986 | 986 | break; |
987 | 987 | case 'LT': |
988 | - $regex[] = '/^(LT)(\d{9}|\d{12})$/'; // Lithuania |
|
988 | + $regex[] = '/^(LT)(\d{9}|\d{12})$/'; // Lithuania |
|
989 | 989 | break; |
990 | 990 | case 'LU': |
991 | - $regex[] = '/^(LU)(\d{8})$/'; // Luxembourg |
|
991 | + $regex[] = '/^(LU)(\d{8})$/'; // Luxembourg |
|
992 | 992 | break; |
993 | 993 | case 'MT': |
994 | - $regex[] = '/^(MT)([1-9]\d{7})$/'; // Malta |
|
994 | + $regex[] = '/^(MT)([1-9]\d{7})$/'; // Malta |
|
995 | 995 | break; |
996 | 996 | case 'NL': |
997 | - $regex[] = '/^(NL)(\d{9})B\d{2}$/'; // Netherlands |
|
997 | + $regex[] = '/^(NL)(\d{9})B\d{2}$/'; // Netherlands |
|
998 | 998 | break; |
999 | 999 | case 'NO': |
1000 | - $regex[] = '/^(NO)(\d{9})$/'; // Norway (Not EU) |
|
1000 | + $regex[] = '/^(NO)(\d{9})$/'; // Norway (Not EU) |
|
1001 | 1001 | break; |
1002 | 1002 | case 'PL': |
1003 | - $regex[] = '/^(PL)(\d{10})$/'; // Poland |
|
1003 | + $regex[] = '/^(PL)(\d{10})$/'; // Poland |
|
1004 | 1004 | break; |
1005 | 1005 | case 'PT': |
1006 | - $regex[] = '/^(PT)(\d{9})$/'; // Portugal |
|
1006 | + $regex[] = '/^(PT)(\d{9})$/'; // Portugal |
|
1007 | 1007 | break; |
1008 | 1008 | case 'RO': |
1009 | - $regex[] = '/^(RO)([1-9]\d{1,9})$/'; // Romania |
|
1009 | + $regex[] = '/^(RO)([1-9]\d{1,9})$/'; // Romania |
|
1010 | 1010 | break; |
1011 | 1011 | case 'RS': |
1012 | - $regex[] = '/^(RS)(\d{9})$/'; // Serbia (Not EU) |
|
1012 | + $regex[] = '/^(RS)(\d{9})$/'; // Serbia (Not EU) |
|
1013 | 1013 | break; |
1014 | 1014 | case 'SI': |
1015 | - $regex[] = '/^(SI)([1-9]\d{7})$/'; // Slovenia |
|
1015 | + $regex[] = '/^(SI)([1-9]\d{7})$/'; // Slovenia |
|
1016 | 1016 | break; |
1017 | 1017 | case 'SK': |
1018 | - $regex[] = '/^(SK)([1-9]\d[(2-4)|(6-9)]\d{7})$/'; // Slovakia Republic |
|
1018 | + $regex[] = '/^(SK)([1-9]\d[(2-4)|(6-9)]\d{7})$/'; // Slovakia Republic |
|
1019 | 1019 | break; |
1020 | 1020 | case 'SE': |
1021 | - $regex[] = '/^(SE)(\d{10}01)$/'; // Sweden |
|
1021 | + $regex[] = '/^(SE)(\d{10}01)$/'; // Sweden |
|
1022 | 1022 | break; |
1023 | 1023 | default: |
1024 | 1024 | $regex = array(); |
1025 | 1025 | break; |
1026 | 1026 | } |
1027 | 1027 | |
1028 | - if ( empty( $regex ) ) { |
|
1028 | + if (empty($regex)) { |
|
1029 | 1029 | return false; |
1030 | 1030 | } |
1031 | 1031 | |
1032 | - foreach ( $regex as $pattern ) { |
|
1032 | + foreach ($regex as $pattern) { |
|
1033 | 1033 | $matches = null; |
1034 | - preg_match_all( $pattern, $vat_number, $matches ); |
|
1034 | + preg_match_all($pattern, $vat_number, $matches); |
|
1035 | 1035 | |
1036 | - if ( !empty( $matches[1][0] ) && !empty( $matches[2][0] ) ) { |
|
1037 | - if ( $formatted ) { |
|
1038 | - return array( 'code' => $matches[1][0], 'number' => $matches[2][0] ); |
|
1036 | + if (!empty($matches[1][0]) && !empty($matches[2][0])) { |
|
1037 | + if ($formatted) { |
|
1038 | + return array('code' => $matches[1][0], 'number' => $matches[2][0]); |
|
1039 | 1039 | } else { |
1040 | 1040 | return true; |
1041 | 1041 | } |
@@ -1045,75 +1045,75 @@ discard block |
||
1045 | 1045 | return false; |
1046 | 1046 | } |
1047 | 1047 | |
1048 | - public static function vies_check( $vat_number, $country_code = '', $result = false ) { |
|
1049 | - $vat = self::sanitize_vat( $vat_number, $country_code ); |
|
1048 | + public static function vies_check($vat_number, $country_code = '', $result = false) { |
|
1049 | + $vat = self::sanitize_vat($vat_number, $country_code); |
|
1050 | 1050 | $vat_number = $vat['vat']; |
1051 | 1051 | $iso = $vat['iso']; |
1052 | 1052 | |
1053 | - $url = 'http://ec.europa.eu/taxation_customs/vies/viesquer.do?ms=' . urlencode( $iso ) . '&iso=' . urlencode( $iso ) . '&vat=' . urlencode( $vat_number ); |
|
1053 | + $url = 'http://ec.europa.eu/taxation_customs/vies/viesquer.do?ms=' . urlencode($iso) . '&iso=' . urlencode($iso) . '&vat=' . urlencode($vat_number); |
|
1054 | 1054 | |
1055 | - if ( ini_get( 'allow_url_fopen' ) ) { |
|
1056 | - $response = file_get_contents( $url ); |
|
1057 | - } else if ( function_exists( 'curl_init' ) ) { |
|
1055 | + if (ini_get('allow_url_fopen')) { |
|
1056 | + $response = file_get_contents($url); |
|
1057 | + } else if (function_exists('curl_init')) { |
|
1058 | 1058 | $ch = curl_init(); |
1059 | 1059 | |
1060 | - curl_setopt( $ch, CURLOPT_URL, $url ); |
|
1061 | - curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 30 ); |
|
1062 | - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
|
1063 | - curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 ); |
|
1064 | - curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); |
|
1060 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
1061 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); |
|
1062 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
1063 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
|
1064 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
|
1065 | 1065 | |
1066 | - $response = curl_exec( $ch ); |
|
1066 | + $response = curl_exec($ch); |
|
1067 | 1067 | |
1068 | - if ( curl_errno( $ch ) ) { |
|
1069 | - wpinv_error_log( curl_error( $ch ), 'VIES CHECK ERROR' ); |
|
1068 | + if (curl_errno($ch)) { |
|
1069 | + wpinv_error_log(curl_error($ch), 'VIES CHECK ERROR'); |
|
1070 | 1070 | $response = ''; |
1071 | 1071 | } |
1072 | 1072 | |
1073 | - curl_close( $ch ); |
|
1073 | + curl_close($ch); |
|
1074 | 1074 | } else { |
1075 | - wpinv_error_log( 'To use VIES CHECK you must have allow_url_fopen is ON or cURL installed & active on your server.', 'VIES CHECK ERROR' ); |
|
1075 | + wpinv_error_log('To use VIES CHECK you must have allow_url_fopen is ON or cURL installed & active on your server.', 'VIES CHECK ERROR'); |
|
1076 | 1076 | } |
1077 | 1077 | |
1078 | - if ( empty( $response ) ) { |
|
1078 | + if (empty($response)) { |
|
1079 | 1079 | return $result; |
1080 | 1080 | } |
1081 | 1081 | |
1082 | - if ( preg_match( '/invalid VAT number/i', $response ) ) { |
|
1082 | + if (preg_match('/invalid VAT number/i', $response)) { |
|
1083 | 1083 | return false; |
1084 | - } else if ( preg_match( '/valid VAT number/i', $response, $matches ) ) { |
|
1085 | - $content = explode( "valid VAT number", htmlentities( $response ) ); |
|
1084 | + } else if (preg_match('/valid VAT number/i', $response, $matches)) { |
|
1085 | + $content = explode("valid VAT number", htmlentities($response)); |
|
1086 | 1086 | |
1087 | - if ( !empty( $content[1] ) ) { |
|
1088 | - preg_match_all( '/<tr>(.*?)<td.*?>(.*?)<\/td>(.*?)<\/tr>/si', html_entity_decode( $content[1] ), $matches ); |
|
1087 | + if (!empty($content[1])) { |
|
1088 | + preg_match_all('/<tr>(.*?)<td.*?>(.*?)<\/td>(.*?)<\/tr>/si', html_entity_decode($content[1]), $matches); |
|
1089 | 1089 | |
1090 | - if ( !empty( $matches[2] ) && $matches[3] ) { |
|
1090 | + if (!empty($matches[2]) && $matches[3]) { |
|
1091 | 1091 | $return = array(); |
1092 | 1092 | |
1093 | - foreach ( $matches[2] as $key => $label ) { |
|
1094 | - $label = trim( $label ); |
|
1093 | + foreach ($matches[2] as $key => $label) { |
|
1094 | + $label = trim($label); |
|
1095 | 1095 | |
1096 | - switch ( strtolower( $label ) ) { |
|
1096 | + switch (strtolower($label)) { |
|
1097 | 1097 | case 'member state': |
1098 | - $return['state'] = trim( strip_tags( $matches[3][$key] ) ); |
|
1098 | + $return['state'] = trim(strip_tags($matches[3][$key])); |
|
1099 | 1099 | break; |
1100 | 1100 | case 'vat number': |
1101 | - $return['number'] = trim( strip_tags( $matches[3][$key] ) ); |
|
1101 | + $return['number'] = trim(strip_tags($matches[3][$key])); |
|
1102 | 1102 | break; |
1103 | 1103 | case 'name': |
1104 | - $return['company'] = trim( strip_tags( $matches[3][$key] ) ); |
|
1104 | + $return['company'] = trim(strip_tags($matches[3][$key])); |
|
1105 | 1105 | break; |
1106 | 1106 | case 'address': |
1107 | - $address = str_replace( array( "<br><br>", "<br /><br />", "<br/><br/>" ), "<br>", html_entity_decode( trim( $matches[3][$key] ) ) ); |
|
1108 | - $return['address'] = trim( strip_tags( $address, '<br>' ) ); |
|
1107 | + $address = str_replace(array("<br><br>", "<br /><br />", "<br/><br/>"), "<br>", html_entity_decode(trim($matches[3][$key]))); |
|
1108 | + $return['address'] = trim(strip_tags($address, '<br>')); |
|
1109 | 1109 | break; |
1110 | 1110 | case 'consultation number': |
1111 | - $return['consultation'] = trim( strip_tags( $matches[3][$key] ) ); |
|
1111 | + $return['consultation'] = trim(strip_tags($matches[3][$key])); |
|
1112 | 1112 | break; |
1113 | 1113 | } |
1114 | 1114 | } |
1115 | 1115 | |
1116 | - if ( !empty( $return ) ) { |
|
1116 | + if (!empty($return)) { |
|
1117 | 1117 | return $return; |
1118 | 1118 | } |
1119 | 1119 | } |
@@ -1125,63 +1125,63 @@ discard block |
||
1125 | 1125 | } |
1126 | 1126 | } |
1127 | 1127 | |
1128 | - public static function check_vat( $vat_number, $country_code = '' ) { |
|
1128 | + public static function check_vat($vat_number, $country_code = '') { |
|
1129 | 1129 | $vat_name = self::get_vat_name(); |
1130 | 1130 | |
1131 | 1131 | $return = array(); |
1132 | 1132 | $return['valid'] = false; |
1133 | - $return['message'] = wp_sprintf( __( '%s number not validated', 'invoicing' ), $vat_name ); |
|
1133 | + $return['message'] = wp_sprintf(__('%s number not validated', 'invoicing'), $vat_name); |
|
1134 | 1134 | |
1135 | - if ( !wpinv_get_option( 'vat_offline_check' ) && !self::offline_check( $vat_number, $country_code ) ) { |
|
1135 | + if (!wpinv_get_option('vat_offline_check') && !self::offline_check($vat_number, $country_code)) { |
|
1136 | 1136 | return $return; |
1137 | 1137 | } |
1138 | 1138 | |
1139 | - $response = self::vies_check( $vat_number, $country_code ); |
|
1139 | + $response = self::vies_check($vat_number, $country_code); |
|
1140 | 1140 | |
1141 | - if ( $response ) { |
|
1142 | - $return['valid'] = true; |
|
1141 | + if ($response) { |
|
1142 | + $return['valid'] = true; |
|
1143 | 1143 | |
1144 | - if ( is_array( $response ) ) { |
|
1145 | - $return['company'] = isset( $response['company'] ) ? $response['company'] : ''; |
|
1146 | - $return['address'] = isset( $response['address'] ) ? $response['address'] : ''; |
|
1144 | + if (is_array($response)) { |
|
1145 | + $return['company'] = isset($response['company']) ? $response['company'] : ''; |
|
1146 | + $return['address'] = isset($response['address']) ? $response['address'] : ''; |
|
1147 | 1147 | $return['message'] = $return['company'] . '<br/>' . $return['address']; |
1148 | 1148 | } |
1149 | 1149 | } else { |
1150 | 1150 | $return['valid'] = false; |
1151 | - $return['message'] = wp_sprintf( __( 'Fail to validate the %s number: EU Commission VAT server (VIES) check fails.', 'invoicing' ), $vat_name ); |
|
1151 | + $return['message'] = wp_sprintf(__('Fail to validate the %s number: EU Commission VAT server (VIES) check fails.', 'invoicing'), $vat_name); |
|
1152 | 1152 | } |
1153 | 1153 | |
1154 | 1154 | return $return; |
1155 | 1155 | } |
1156 | 1156 | |
1157 | - public static function request_euvatrates( $group ) { |
|
1157 | + public static function request_euvatrates($group) { |
|
1158 | 1158 | $response = array(); |
1159 | 1159 | $response['success'] = false; |
1160 | 1160 | $response['error'] = null; |
1161 | 1161 | $response['eurates'] = null; |
1162 | 1162 | |
1163 | 1163 | $euvatrates_url = 'https://euvatrates.com/rates.json'; |
1164 | - $euvatrates_url = apply_filters( 'wpinv_euvatrates_url', $euvatrates_url ); |
|
1165 | - $api_response = wp_remote_get( $euvatrates_url ); |
|
1164 | + $euvatrates_url = apply_filters('wpinv_euvatrates_url', $euvatrates_url); |
|
1165 | + $api_response = wp_remote_get($euvatrates_url); |
|
1166 | 1166 | |
1167 | 1167 | try { |
1168 | - if ( is_wp_error( $api_response ) ) { |
|
1169 | - $response['error'] = __( $api_response->get_error_message(), 'invoicing' ); |
|
1168 | + if (is_wp_error($api_response)) { |
|
1169 | + $response['error'] = __($api_response->get_error_message(), 'invoicing'); |
|
1170 | 1170 | } else { |
1171 | - $body = json_decode( $api_response['body'] ); |
|
1171 | + $body = json_decode($api_response['body']); |
|
1172 | 1172 | |
1173 | - if ( isset( $body->rates ) ) { |
|
1173 | + if (isset($body->rates)) { |
|
1174 | 1174 | $rates = array(); |
1175 | 1175 | |
1176 | - foreach ( $body->rates as $country_code => $rate ) { |
|
1176 | + foreach ($body->rates as $country_code => $rate) { |
|
1177 | 1177 | $vat_rate = array(); |
1178 | 1178 | $vat_rate['country'] = $rate->country; |
1179 | - $vat_rate['standard'] = (float)$rate->standard_rate; |
|
1180 | - $vat_rate['reduced'] = (float)$rate->reduced_rate; |
|
1181 | - $vat_rate['superreduced'] = (float)$rate->super_reduced_rate; |
|
1182 | - $vat_rate['parking'] = (float)$rate->parking_rate; |
|
1179 | + $vat_rate['standard'] = (float) $rate->standard_rate; |
|
1180 | + $vat_rate['reduced'] = (float) $rate->reduced_rate; |
|
1181 | + $vat_rate['superreduced'] = (float) $rate->super_reduced_rate; |
|
1182 | + $vat_rate['parking'] = (float) $rate->parking_rate; |
|
1183 | 1183 | |
1184 | - if ( $group !== '' && in_array( $group, array( 'standard', 'reduced', 'superreduced', 'parking' ) ) ) { |
|
1184 | + if ($group !== '' && in_array($group, array('standard', 'reduced', 'superreduced', 'parking'))) { |
|
1185 | 1185 | $vat_rate_group = array(); |
1186 | 1186 | $vat_rate_group['country'] = $rate->country; |
1187 | 1187 | $vat_rate_group[$group] = $vat_rate[$group]; |
@@ -1193,79 +1193,79 @@ discard block |
||
1193 | 1193 | } |
1194 | 1194 | |
1195 | 1195 | $response['success'] = true; |
1196 | - $response['rates'] = apply_filters( 'wpinv_process_euvatrates', $rates, $api_response, $group ); |
|
1196 | + $response['rates'] = apply_filters('wpinv_process_euvatrates', $rates, $api_response, $group); |
|
1197 | 1197 | } else { |
1198 | - $response['error'] = __( 'No EU rates found!', 'invoicing' ); |
|
1198 | + $response['error'] = __('No EU rates found!', 'invoicing'); |
|
1199 | 1199 | } |
1200 | 1200 | } |
1201 | - } catch ( Exception $e ) { |
|
1202 | - $response['error'] = __( $e->getMessage(), 'invoicing' ); |
|
1201 | + } catch (Exception $e) { |
|
1202 | + $response['error'] = __($e->getMessage(), 'invoicing'); |
|
1203 | 1203 | } |
1204 | 1204 | |
1205 | - return apply_filters( 'wpinv_response_euvatrates', $response, $group ); |
|
1205 | + return apply_filters('wpinv_response_euvatrates', $response, $group); |
|
1206 | 1206 | } |
1207 | 1207 | |
1208 | - public static function requires_vat( $requires_vat = false, $user_id = 0, $is_digital = null ) { |
|
1208 | + public static function requires_vat($requires_vat = false, $user_id = 0, $is_digital = null) { |
|
1209 | 1209 | global $wpi_item_id, $wpi_country; |
1210 | 1210 | |
1211 | - if ( !empty( $_POST['wpinv_country'] ) ) { |
|
1212 | - $country_code = trim( $_POST['wpinv_country'] ); |
|
1213 | - } else if ( !empty( $_POST['country'] ) ) { |
|
1214 | - $country_code = trim( $_POST['country'] ); |
|
1215 | - } else if ( !empty( $wpi_country ) ) { |
|
1211 | + if (!empty($_POST['wpinv_country'])) { |
|
1212 | + $country_code = trim($_POST['wpinv_country']); |
|
1213 | + } else if (!empty($_POST['country'])) { |
|
1214 | + $country_code = trim($_POST['country']); |
|
1215 | + } else if (!empty($wpi_country)) { |
|
1216 | 1216 | $country_code = $wpi_country; |
1217 | 1217 | } else { |
1218 | - $country_code = self::get_user_country( '', $user_id ); |
|
1218 | + $country_code = self::get_user_country('', $user_id); |
|
1219 | 1219 | } |
1220 | 1220 | |
1221 | - if ( $is_digital === null && $wpi_item_id ) { |
|
1222 | - $is_digital = $wpi_item_id ? self::item_has_digital_rule( $wpi_item_id ) : self::allow_vat_rules(); |
|
1221 | + if ($is_digital === null && $wpi_item_id) { |
|
1222 | + $is_digital = $wpi_item_id ? self::item_has_digital_rule($wpi_item_id) : self::allow_vat_rules(); |
|
1223 | 1223 | } |
1224 | 1224 | |
1225 | - if ( !empty( $country_code ) ) { |
|
1226 | - $requires_vat = ( self::is_eu_state( $country_code ) && ( self::is_eu_state( self::$default_country ) || $is_digital ) ) || ( self::is_gst_country( $country_code ) && self::is_gst_country( self::$default_country ) ); |
|
1225 | + if (!empty($country_code)) { |
|
1226 | + $requires_vat = (self::is_eu_state($country_code) && (self::is_eu_state(self::$default_country) || $is_digital)) || (self::is_gst_country($country_code) && self::is_gst_country(self::$default_country)); |
|
1227 | 1227 | } |
1228 | 1228 | |
1229 | - return apply_filters( 'wpinv_requires_vat', $requires_vat, $user_id ); |
|
1229 | + return apply_filters('wpinv_requires_vat', $requires_vat, $user_id); |
|
1230 | 1230 | } |
1231 | 1231 | |
1232 | - public static function tax_label( $label = '' ) { |
|
1232 | + public static function tax_label($label = '') { |
|
1233 | 1233 | global $wpi_requires_vat; |
1234 | 1234 | |
1235 | - if ( !( $wpi_requires_vat !== 0 && $wpi_requires_vat ) ) { |
|
1236 | - $wpi_requires_vat = self::requires_vat( 0, false ); |
|
1235 | + if (!($wpi_requires_vat !== 0 && $wpi_requires_vat)) { |
|
1236 | + $wpi_requires_vat = self::requires_vat(0, false); |
|
1237 | 1237 | } |
1238 | 1238 | |
1239 | - return $wpi_requires_vat ? __( self::get_vat_name(), 'invoicing' ) : ( $label ? $label : __( 'Tax', 'invoicing' ) ); |
|
1239 | + return $wpi_requires_vat ? __(self::get_vat_name(), 'invoicing') : ($label ? $label : __('Tax', 'invoicing')); |
|
1240 | 1240 | } |
1241 | 1241 | |
1242 | 1242 | public static function standard_rates_label() { |
1243 | - return __( 'Standard Rates', 'invoicing' ); |
|
1243 | + return __('Standard Rates', 'invoicing'); |
|
1244 | 1244 | } |
1245 | 1245 | |
1246 | - public static function get_rate_classes( $with_desc = false ) { |
|
1247 | - $rate_classes_option = get_option( '_wpinv_vat_rate_classes', true ); |
|
1248 | - $classes = maybe_unserialize( $rate_classes_option ); |
|
1246 | + public static function get_rate_classes($with_desc = false) { |
|
1247 | + $rate_classes_option = get_option('_wpinv_vat_rate_classes', true); |
|
1248 | + $classes = maybe_unserialize($rate_classes_option); |
|
1249 | 1249 | |
1250 | - if ( empty( $classes ) || !is_array( $classes ) ) { |
|
1250 | + if (empty($classes) || !is_array($classes)) { |
|
1251 | 1251 | $classes = array(); |
1252 | 1252 | } |
1253 | 1253 | |
1254 | 1254 | $rate_classes = array(); |
1255 | - if ( !array_key_exists( '_standard', $classes ) ) { |
|
1256 | - if ( $with_desc ) { |
|
1257 | - $rate_classes['_standard'] = array( 'name' => self::standard_rates_label(), 'desc' => __( 'EU member states standard VAT rates', 'invoicing' ) ); |
|
1255 | + if (!array_key_exists('_standard', $classes)) { |
|
1256 | + if ($with_desc) { |
|
1257 | + $rate_classes['_standard'] = array('name' => self::standard_rates_label(), 'desc' => __('EU member states standard VAT rates', 'invoicing')); |
|
1258 | 1258 | } else { |
1259 | 1259 | $rate_classes['_standard'] = self::standard_rates_label(); |
1260 | 1260 | } |
1261 | 1261 | } |
1262 | 1262 | |
1263 | - foreach ( $classes as $key => $class ) { |
|
1264 | - $name = !empty( $class['name'] ) ? __( $class['name'], 'invoicing' ) : $key; |
|
1265 | - $desc = !empty( $class['desc'] ) ? __( $class['desc'], 'invoicing' ) : ''; |
|
1263 | + foreach ($classes as $key => $class) { |
|
1264 | + $name = !empty($class['name']) ? __($class['name'], 'invoicing') : $key; |
|
1265 | + $desc = !empty($class['desc']) ? __($class['desc'], 'invoicing') : ''; |
|
1266 | 1266 | |
1267 | - if ( $with_desc ) { |
|
1268 | - $rate_classes[$key] = array( 'name' => $name, 'desc' => $desc ); |
|
1267 | + if ($with_desc) { |
|
1268 | + $rate_classes[$key] = array('name' => $name, 'desc' => $desc); |
|
1269 | 1269 | } else { |
1270 | 1270 | $rate_classes[$key] = $name; |
1271 | 1271 | } |
@@ -1276,15 +1276,15 @@ discard block |
||
1276 | 1276 | |
1277 | 1277 | public static function get_all_classes() { |
1278 | 1278 | $classes = self::get_rate_classes(); |
1279 | - $classes['_exempt'] = __( 'Exempt (0%)', 'invoicing' ); |
|
1279 | + $classes['_exempt'] = __('Exempt (0%)', 'invoicing'); |
|
1280 | 1280 | |
1281 | - return apply_filters( 'wpinv_vat_get_all_classes', $classes ); |
|
1281 | + return apply_filters('wpinv_vat_get_all_classes', $classes); |
|
1282 | 1282 | } |
1283 | 1283 | |
1284 | - public static function get_class_desc( $rate_class ) { |
|
1285 | - $rate_classes = self::get_rate_classes( true ); |
|
1284 | + public static function get_class_desc($rate_class) { |
|
1285 | + $rate_classes = self::get_rate_classes(true); |
|
1286 | 1286 | |
1287 | - if ( !empty( $rate_classes ) && isset( $rate_classes[$rate_class] ) && isset( $rate_classes[$rate_class]['desc'] ) ) { |
|
1287 | + if (!empty($rate_classes) && isset($rate_classes[$rate_class]) && isset($rate_classes[$rate_class]['desc'])) { |
|
1288 | 1288 | return $rate_classes[$rate_class]['desc']; |
1289 | 1289 | } |
1290 | 1290 | |
@@ -1300,106 +1300,106 @@ discard block |
||
1300 | 1300 | 'increased' => 'Increased' |
1301 | 1301 | ); |
1302 | 1302 | |
1303 | - return apply_filters( 'wpinv_get_vat_groups', $vat_groups ); |
|
1303 | + return apply_filters('wpinv_get_vat_groups', $vat_groups); |
|
1304 | 1304 | } |
1305 | 1305 | |
1306 | 1306 | public static function get_rules() { |
1307 | 1307 | $vat_rules = array( |
1308 | - 'digital' => __( 'Digital Product', 'invoicing' ), |
|
1309 | - 'physical' => __( 'Physical Product', 'invoicing' ) |
|
1308 | + 'digital' => __('Digital Product', 'invoicing'), |
|
1309 | + 'physical' => __('Physical Product', 'invoicing') |
|
1310 | 1310 | ); |
1311 | - return apply_filters( 'wpinv_get_vat_rules', $vat_rules ); |
|
1311 | + return apply_filters('wpinv_get_vat_rules', $vat_rules); |
|
1312 | 1312 | } |
1313 | 1313 | |
1314 | - public static function get_vat_rates( $class ) { |
|
1315 | - if ( $class === '_standard' ) { |
|
1314 | + public static function get_vat_rates($class) { |
|
1315 | + if ($class === '_standard') { |
|
1316 | 1316 | return wpinv_get_tax_rates(); |
1317 | 1317 | } |
1318 | 1318 | |
1319 | 1319 | $rates = self::get_non_standard_rates(); |
1320 | 1320 | |
1321 | - return array_key_exists( $class, $rates ) ? $rates[$class] : array(); |
|
1321 | + return array_key_exists($class, $rates) ? $rates[$class] : array(); |
|
1322 | 1322 | } |
1323 | 1323 | |
1324 | 1324 | public static function get_non_standard_rates() { |
1325 | - $option = get_option( 'wpinv_vat_rates', array()); |
|
1326 | - return is_array( $option ) ? $option : array(); |
|
1325 | + $option = get_option('wpinv_vat_rates', array()); |
|
1326 | + return is_array($option) ? $option : array(); |
|
1327 | 1327 | } |
1328 | 1328 | |
1329 | 1329 | public static function allow_vat_rules() { |
1330 | - return ( wpinv_use_taxes() && wpinv_get_option( 'apply_vat_rules' ) ? true : false ); |
|
1330 | + return (wpinv_use_taxes() && wpinv_get_option('apply_vat_rules') ? true : false); |
|
1331 | 1331 | } |
1332 | 1332 | |
1333 | 1333 | public static function allow_vat_classes() { |
1334 | 1334 | return false; // TODO |
1335 | - return ( wpinv_get_option( 'vat_allow_classes' ) ? true : false ); |
|
1335 | + return (wpinv_get_option('vat_allow_classes') ? true : false); |
|
1336 | 1336 | } |
1337 | 1337 | |
1338 | - public static function get_item_class( $postID ) { |
|
1339 | - $class = get_post_meta( $postID, '_wpinv_vat_class', true ); |
|
1338 | + public static function get_item_class($postID) { |
|
1339 | + $class = get_post_meta($postID, '_wpinv_vat_class', true); |
|
1340 | 1340 | |
1341 | - if ( empty( $class ) ) { |
|
1341 | + if (empty($class)) { |
|
1342 | 1342 | $class = '_standard'; |
1343 | 1343 | } |
1344 | 1344 | |
1345 | - return apply_filters( 'wpinv_get_item_vat_class', $class, $postID ); |
|
1345 | + return apply_filters('wpinv_get_item_vat_class', $class, $postID); |
|
1346 | 1346 | } |
1347 | 1347 | |
1348 | - public static function item_class_label( $postID ) { |
|
1348 | + public static function item_class_label($postID) { |
|
1349 | 1349 | $vat_classes = self::get_all_classes(); |
1350 | 1350 | |
1351 | - $class = self::get_item_class( $postID ); |
|
1352 | - $class = isset( $vat_classes[$class] ) ? $vat_classes[$class] : __( $class, 'invoicing' ); |
|
1351 | + $class = self::get_item_class($postID); |
|
1352 | + $class = isset($vat_classes[$class]) ? $vat_classes[$class] : __($class, 'invoicing'); |
|
1353 | 1353 | |
1354 | - return apply_filters( 'wpinv_item_class_label', $class, $postID ); |
|
1354 | + return apply_filters('wpinv_item_class_label', $class, $postID); |
|
1355 | 1355 | } |
1356 | 1356 | |
1357 | - public static function get_item_rule( $postID ) { |
|
1358 | - $rule_type = get_post_meta( $postID, '_wpinv_vat_rule', true ); |
|
1357 | + public static function get_item_rule($postID) { |
|
1358 | + $rule_type = get_post_meta($postID, '_wpinv_vat_rule', true); |
|
1359 | 1359 | |
1360 | - if ( empty( $rule_type ) ) { |
|
1360 | + if (empty($rule_type)) { |
|
1361 | 1361 | $rule_type = self::allow_vat_rules() ? 'digital' : 'physical'; |
1362 | 1362 | } |
1363 | 1363 | |
1364 | - return apply_filters( 'wpinv_item_get_vat_rule', $rule_type, $postID ); |
|
1364 | + return apply_filters('wpinv_item_get_vat_rule', $rule_type, $postID); |
|
1365 | 1365 | } |
1366 | 1366 | |
1367 | - public static function item_rule_label( $postID ) { |
|
1367 | + public static function item_rule_label($postID) { |
|
1368 | 1368 | $vat_rules = self::get_rules(); |
1369 | - $vat_rule = self::get_item_rule( $postID ); |
|
1370 | - $vat_rule = isset( $vat_rules[$vat_rule] ) ? $vat_rules[$vat_rule] : $vat_rule; |
|
1369 | + $vat_rule = self::get_item_rule($postID); |
|
1370 | + $vat_rule = isset($vat_rules[$vat_rule]) ? $vat_rules[$vat_rule] : $vat_rule; |
|
1371 | 1371 | |
1372 | - return apply_filters( 'wpinv_item_rule_label', $vat_rule, $postID ); |
|
1372 | + return apply_filters('wpinv_item_rule_label', $vat_rule, $postID); |
|
1373 | 1373 | } |
1374 | 1374 | |
1375 | - public static function item_has_digital_rule( $item_id = 0 ) { |
|
1376 | - return self::get_item_rule( $item_id ) == 'digital' ? true : false; |
|
1375 | + public static function item_has_digital_rule($item_id = 0) { |
|
1376 | + return self::get_item_rule($item_id) == 'digital' ? true : false; |
|
1377 | 1377 | } |
1378 | 1378 | |
1379 | - public static function invoice_has_digital_rule( $invoice = 0 ) { |
|
1380 | - if ( !self::allow_vat_rules() ) { |
|
1379 | + public static function invoice_has_digital_rule($invoice = 0) { |
|
1380 | + if (!self::allow_vat_rules()) { |
|
1381 | 1381 | return false; |
1382 | 1382 | } |
1383 | 1383 | |
1384 | - if ( empty( $invoice ) ) { |
|
1384 | + if (empty($invoice)) { |
|
1385 | 1385 | return true; |
1386 | 1386 | } |
1387 | 1387 | |
1388 | - if ( is_int( $invoice ) ) { |
|
1389 | - $invoice = new WPInv_Invoice( $invoice ); |
|
1388 | + if (is_int($invoice)) { |
|
1389 | + $invoice = new WPInv_Invoice($invoice); |
|
1390 | 1390 | } |
1391 | 1391 | |
1392 | - if ( !( is_object( $invoice ) && is_a( $invoice, 'WPInv_Invoice' ) ) ) { |
|
1392 | + if (!(is_object($invoice) && is_a($invoice, 'WPInv_Invoice'))) { |
|
1393 | 1393 | return true; |
1394 | 1394 | } |
1395 | 1395 | |
1396 | - $cart_items = $invoice->get_cart_details(); |
|
1396 | + $cart_items = $invoice->get_cart_details(); |
|
1397 | 1397 | |
1398 | - if ( !empty( $cart_items ) ) { |
|
1398 | + if (!empty($cart_items)) { |
|
1399 | 1399 | $has_digital_rule = false; |
1400 | 1400 | |
1401 | - foreach ( $cart_items as $key => $item ) { |
|
1402 | - if ( self::item_has_digital_rule( $item['id'] ) ) { |
|
1401 | + foreach ($cart_items as $key => $item) { |
|
1402 | + if (self::item_has_digital_rule($item['id'])) { |
|
1403 | 1403 | $has_digital_rule = true; |
1404 | 1404 | break; |
1405 | 1405 | } |
@@ -1411,67 +1411,67 @@ discard block |
||
1411 | 1411 | return $has_digital_rule; |
1412 | 1412 | } |
1413 | 1413 | |
1414 | - public static function item_is_taxable( $item_id = 0, $country = false, $state = false ) { |
|
1415 | - if ( !wpinv_use_taxes() ) { |
|
1414 | + public static function item_is_taxable($item_id = 0, $country = false, $state = false) { |
|
1415 | + if (!wpinv_use_taxes()) { |
|
1416 | 1416 | return false; |
1417 | 1417 | } |
1418 | 1418 | |
1419 | 1419 | $is_taxable = true; |
1420 | 1420 | |
1421 | - if ( !empty( $item_id ) && self::get_item_class( $item_id ) == '_exempt' ) { |
|
1421 | + if (!empty($item_id) && self::get_item_class($item_id) == '_exempt') { |
|
1422 | 1422 | $is_taxable = false; |
1423 | 1423 | } |
1424 | 1424 | |
1425 | - return apply_filters( 'wpinv_item_is_taxable', $is_taxable, $item_id, $country , $state ); |
|
1425 | + return apply_filters('wpinv_item_is_taxable', $is_taxable, $item_id, $country, $state); |
|
1426 | 1426 | } |
1427 | 1427 | |
1428 | - public static function find_rate( $country, $state, $rate, $class ) { |
|
1428 | + public static function find_rate($country, $state, $rate, $class) { |
|
1429 | 1429 | global $wpi_zero_tax; |
1430 | 1430 | |
1431 | - if ( $class === '_exempt' || $wpi_zero_tax ) { |
|
1431 | + if ($class === '_exempt' || $wpi_zero_tax) { |
|
1432 | 1432 | return 0; |
1433 | 1433 | } |
1434 | 1434 | |
1435 | - $tax_rates = wpinv_get_tax_rates(); |
|
1435 | + $tax_rates = wpinv_get_tax_rates(); |
|
1436 | 1436 | |
1437 | - if ( $class !== '_standard' ) { |
|
1438 | - $class_rates = self::get_vat_rates( $class ); |
|
1437 | + if ($class !== '_standard') { |
|
1438 | + $class_rates = self::get_vat_rates($class); |
|
1439 | 1439 | |
1440 | - if ( is_array( $class_rates ) ) { |
|
1440 | + if (is_array($class_rates)) { |
|
1441 | 1441 | $indexed_class_rates = array(); |
1442 | 1442 | |
1443 | - foreach ( $class_rates as $key => $cr ) { |
|
1443 | + foreach ($class_rates as $key => $cr) { |
|
1444 | 1444 | $indexed_class_rates[$cr['country']] = $cr; |
1445 | 1445 | } |
1446 | 1446 | |
1447 | - $tax_rates = array_map( function( $tr ) use( $indexed_class_rates ) { |
|
1447 | + $tax_rates = array_map(function($tr) use($indexed_class_rates) { |
|
1448 | 1448 | $tr_country = $tr['country']; |
1449 | - if ( !isset( $indexed_class_rates[$tr_country] ) ) { |
|
1449 | + if (!isset($indexed_class_rates[$tr_country])) { |
|
1450 | 1450 | return $tr; |
1451 | 1451 | } |
1452 | 1452 | $icr = $indexed_class_rates[$tr_country]; |
1453 | - return ( empty( $icr['rate'] ) && $icr['rate'] !== '0' ) ? $tr : $icr; |
|
1453 | + return (empty($icr['rate']) && $icr['rate'] !== '0') ? $tr : $icr; |
|
1454 | 1454 | |
1455 | - }, $tax_rates, $class_rates ); |
|
1455 | + }, $tax_rates, $class_rates); |
|
1456 | 1456 | } |
1457 | 1457 | } |
1458 | 1458 | |
1459 | - if ( !empty( $tax_rates ) ) { |
|
1460 | - foreach ( $tax_rates as $key => $tax_rate ) { |
|
1461 | - if ( $country != $tax_rate['country'] ) |
|
1459 | + if (!empty($tax_rates)) { |
|
1460 | + foreach ($tax_rates as $key => $tax_rate) { |
|
1461 | + if ($country != $tax_rate['country']) |
|
1462 | 1462 | continue; |
1463 | 1463 | |
1464 | - if ( !empty( $tax_rate['global'] ) ) { |
|
1465 | - if ( 0 !== $tax_rate['rate'] || !empty( $tax_rate['rate'] ) ) { |
|
1466 | - $rate = number_format( $tax_rate['rate'], 4 ); |
|
1464 | + if (!empty($tax_rate['global'])) { |
|
1465 | + if (0 !== $tax_rate['rate'] || !empty($tax_rate['rate'])) { |
|
1466 | + $rate = number_format($tax_rate['rate'], 4); |
|
1467 | 1467 | } |
1468 | 1468 | } else { |
1469 | - if ( empty( $tax_rate['state'] ) || strtolower( $state ) != strtolower( $tax_rate['state'] ) ) |
|
1469 | + if (empty($tax_rate['state']) || strtolower($state) != strtolower($tax_rate['state'])) |
|
1470 | 1470 | continue; |
1471 | 1471 | |
1472 | 1472 | $state_rate = $tax_rate['rate']; |
1473 | - if ( 0 !== $state_rate || !empty( $state_rate ) ) { |
|
1474 | - $rate = number_format( $state_rate, 4 ); |
|
1473 | + if (0 !== $state_rate || !empty($state_rate)) { |
|
1474 | + $rate = number_format($state_rate, 4); |
|
1475 | 1475 | } |
1476 | 1476 | } |
1477 | 1477 | } |
@@ -1480,84 +1480,84 @@ discard block |
||
1480 | 1480 | return $rate; |
1481 | 1481 | } |
1482 | 1482 | |
1483 | - public static function get_rate( $rate = 1, $country = '', $state = '', $item_id = 0 ) { |
|
1483 | + public static function get_rate($rate = 1, $country = '', $state = '', $item_id = 0) { |
|
1484 | 1484 | global $wpinv_options, $wpi_session, $wpi_item_id, $wpi_zero_tax; |
1485 | 1485 | |
1486 | 1486 | $item_id = $item_id > 0 ? $item_id : $wpi_item_id; |
1487 | 1487 | $allow_vat_classes = self::allow_vat_classes(); |
1488 | - $class = $item_id ? self::get_item_class( $item_id ) : '_standard'; |
|
1488 | + $class = $item_id ? self::get_item_class($item_id) : '_standard'; |
|
1489 | 1489 | |
1490 | - if ( $class === '_exempt' || $wpi_zero_tax ) { |
|
1490 | + if ($class === '_exempt' || $wpi_zero_tax) { |
|
1491 | 1491 | return 0; |
1492 | - } else if ( !$allow_vat_classes ) { |
|
1492 | + } else if (!$allow_vat_classes) { |
|
1493 | 1493 | $class = '_standard'; |
1494 | 1494 | } |
1495 | 1495 | |
1496 | - if( !empty( $_POST['wpinv_country'] ) ) { |
|
1496 | + if (!empty($_POST['wpinv_country'])) { |
|
1497 | 1497 | $post_country = $_POST['wpinv_country']; |
1498 | - } elseif( !empty( $_POST['wpinv_country'] ) ) { |
|
1498 | + } elseif (!empty($_POST['wpinv_country'])) { |
|
1499 | 1499 | $post_country = $_POST['wpinv_country']; |
1500 | - } elseif( !empty( $_POST['country'] ) ) { |
|
1500 | + } elseif (!empty($_POST['country'])) { |
|
1501 | 1501 | $post_country = $_POST['country']; |
1502 | 1502 | } else { |
1503 | 1503 | $post_country = ''; |
1504 | 1504 | } |
1505 | 1505 | |
1506 | - $country = !empty( $post_country ) ? $post_country : wpinv_default_billing_country( $country ); |
|
1507 | - $base_country = wpinv_is_base_country( $country ); |
|
1506 | + $country = !empty($post_country) ? $post_country : wpinv_default_billing_country($country); |
|
1507 | + $base_country = wpinv_is_base_country($country); |
|
1508 | 1508 | |
1509 | - $requires_vat = self::requires_vat( 0, false ); |
|
1510 | - $is_digital = self::get_item_rule( $item_id ) == 'digital' ; |
|
1511 | - $rate = $requires_vat && isset( $wpinv_options['eu_fallback_rate'] ) ? $wpinv_options['eu_fallback_rate'] : $rate; |
|
1509 | + $requires_vat = self::requires_vat(0, false); |
|
1510 | + $is_digital = self::get_item_rule($item_id) == 'digital'; |
|
1511 | + $rate = $requires_vat && isset($wpinv_options['eu_fallback_rate']) ? $wpinv_options['eu_fallback_rate'] : $rate; |
|
1512 | 1512 | |
1513 | - if ( self::same_country_rule() == 'no' && $base_country ) { // Disable VAT to same country |
|
1513 | + if (self::same_country_rule() == 'no' && $base_country) { // Disable VAT to same country |
|
1514 | 1514 | $rate = 0; |
1515 | - } else if ( $requires_vat ) { |
|
1516 | - $vat_number = self::get_user_vat_number( '', 0, true ); |
|
1515 | + } else if ($requires_vat) { |
|
1516 | + $vat_number = self::get_user_vat_number('', 0, true); |
|
1517 | 1517 | $vat_info = self::current_vat_data(); |
1518 | 1518 | |
1519 | - if ( is_array( $vat_info ) ) { |
|
1520 | - $vat_number = isset( $vat_info['number'] ) && !empty( $vat_info['valid'] ) ? $vat_info['number'] : ""; |
|
1519 | + if (is_array($vat_info)) { |
|
1520 | + $vat_number = isset($vat_info['number']) && !empty($vat_info['valid']) ? $vat_info['number'] : ""; |
|
1521 | 1521 | } |
1522 | 1522 | |
1523 | - if ( $country == 'UK' ) { |
|
1523 | + if ($country == 'UK') { |
|
1524 | 1524 | $country = 'GB'; |
1525 | 1525 | } |
1526 | 1526 | |
1527 | - if ( !empty( $vat_number ) ) { |
|
1527 | + if (!empty($vat_number)) { |
|
1528 | 1528 | $rate = 0; |
1529 | 1529 | } else { |
1530 | - $rate = self::find_rate( $country, $state, $rate, $class ); // Fix if there are no tax rated and you try to pay an invoice it does not add the fallback tax rate |
|
1530 | + $rate = self::find_rate($country, $state, $rate, $class); // Fix if there are no tax rated and you try to pay an invoice it does not add the fallback tax rate |
|
1531 | 1531 | } |
1532 | 1532 | |
1533 | - if ( empty( $vat_number ) && !$is_digital ) { |
|
1534 | - if ( $base_country ) { |
|
1535 | - $rate = self::find_rate( $country, null, $rate, $class ); |
|
1533 | + if (empty($vat_number) && !$is_digital) { |
|
1534 | + if ($base_country) { |
|
1535 | + $rate = self::find_rate($country, null, $rate, $class); |
|
1536 | 1536 | } else { |
1537 | - if ( empty( $country ) && isset( $wpinv_options['eu_fallback_rate'] ) ) { |
|
1537 | + if (empty($country) && isset($wpinv_options['eu_fallback_rate'])) { |
|
1538 | 1538 | $rate = $wpinv_options['eu_fallback_rate']; |
1539 | - } else if( !empty( $country ) ) { |
|
1540 | - $rate = self::find_rate( $country, $state, $rate, $class ); |
|
1539 | + } else if (!empty($country)) { |
|
1540 | + $rate = self::find_rate($country, $state, $rate, $class); |
|
1541 | 1541 | } |
1542 | 1542 | } |
1543 | - } else if ( empty( $vat_number ) || ( self::same_country_rule() == 'always' && $base_country ) ) { |
|
1544 | - if ( empty( $country ) && isset( $wpinv_options['eu_fallback_rate'] ) ) { |
|
1543 | + } else if (empty($vat_number) || (self::same_country_rule() == 'always' && $base_country)) { |
|
1544 | + if (empty($country) && isset($wpinv_options['eu_fallback_rate'])) { |
|
1545 | 1545 | $rate = $wpinv_options['eu_fallback_rate']; |
1546 | - } else if( !empty( $country ) ) { |
|
1547 | - $rate = self::find_rate( $country, $state, $rate, $class ); |
|
1546 | + } else if (!empty($country)) { |
|
1547 | + $rate = self::find_rate($country, $state, $rate, $class); |
|
1548 | 1548 | } |
1549 | 1549 | } |
1550 | 1550 | } else { |
1551 | - if ( $is_digital ) { |
|
1551 | + if ($is_digital) { |
|
1552 | 1552 | $ip_country_code = self::get_country_by_ip(); |
1553 | 1553 | |
1554 | - if ( $ip_country_code && self::is_eu_state( $ip_country_code ) ) { |
|
1555 | - $rate = self::find_rate( $ip_country_code, '', 0, $class ); |
|
1554 | + if ($ip_country_code && self::is_eu_state($ip_country_code)) { |
|
1555 | + $rate = self::find_rate($ip_country_code, '', 0, $class); |
|
1556 | 1556 | } else { |
1557 | - $rate = self::find_rate( $country, $state, $rate, $class ); |
|
1557 | + $rate = self::find_rate($country, $state, $rate, $class); |
|
1558 | 1558 | } |
1559 | 1559 | } else { |
1560 | - $rate = self::find_rate( $country, $state, $rate, $class ); |
|
1560 | + $rate = self::find_rate($country, $state, $rate, $class); |
|
1561 | 1561 | } |
1562 | 1562 | } |
1563 | 1563 | |
@@ -1567,48 +1567,48 @@ discard block |
||
1567 | 1567 | public static function current_vat_data() { |
1568 | 1568 | global $wpi_session; |
1569 | 1569 | |
1570 | - return $wpi_session->get( 'user_vat_data' ); |
|
1570 | + return $wpi_session->get('user_vat_data'); |
|
1571 | 1571 | } |
1572 | 1572 | |
1573 | - public static function get_user_country( $country = '', $user_id = 0 ) { |
|
1574 | - $user_address = wpinv_get_user_address( $user_id, false ); |
|
1573 | + public static function get_user_country($country = '', $user_id = 0) { |
|
1574 | + $user_address = wpinv_get_user_address($user_id, false); |
|
1575 | 1575 | |
1576 | - if ( wpinv_get_option( 'vat_ip_country_default' ) ) { |
|
1576 | + if (wpinv_get_option('vat_ip_country_default')) { |
|
1577 | 1577 | $country = ''; |
1578 | 1578 | } |
1579 | 1579 | |
1580 | - $country = empty( $user_address ) || !isset( $user_address['country'] ) || empty( $user_address['country'] ) ? $country : $user_address['country']; |
|
1581 | - $result = apply_filters( 'wpinv_get_user_country', $country, $user_id ); |
|
1580 | + $country = empty($user_address) || !isset($user_address['country']) || empty($user_address['country']) ? $country : $user_address['country']; |
|
1581 | + $result = apply_filters('wpinv_get_user_country', $country, $user_id); |
|
1582 | 1582 | |
1583 | - if ( empty( $result ) ) { |
|
1583 | + if (empty($result)) { |
|
1584 | 1584 | $result = self::get_country_by_ip(); |
1585 | 1585 | } |
1586 | 1586 | |
1587 | 1587 | return $result; |
1588 | 1588 | } |
1589 | 1589 | |
1590 | - public static function set_user_country( $country = '', $user_id = 0 ) { |
|
1590 | + public static function set_user_country($country = '', $user_id = 0) { |
|
1591 | 1591 | global $wpi_userID; |
1592 | 1592 | |
1593 | - if ( empty($country) && !empty($wpi_userID) && get_current_user_id() != $wpi_userID ) { |
|
1593 | + if (empty($country) && !empty($wpi_userID) && get_current_user_id() != $wpi_userID) { |
|
1594 | 1594 | $country = wpinv_get_default_country(); |
1595 | 1595 | } |
1596 | 1596 | |
1597 | 1597 | return $country; |
1598 | 1598 | } |
1599 | 1599 | |
1600 | - public static function get_user_vat_number( $vat_number = '', $user_id = 0, $is_valid = false ) { |
|
1600 | + public static function get_user_vat_number($vat_number = '', $user_id = 0, $is_valid = false) { |
|
1601 | 1601 | global $wpi_current_id, $wpi_userID; |
1602 | 1602 | |
1603 | - if ( !empty( $_POST['new_user'] ) ) { |
|
1603 | + if (!empty($_POST['new_user'])) { |
|
1604 | 1604 | return ''; |
1605 | 1605 | } |
1606 | 1606 | |
1607 | - if ( empty( $user_id ) ) { |
|
1608 | - $user_id = !empty( $wpi_userID ) ? $wpi_userID : ( $wpi_current_id ? wpinv_get_user_id( $wpi_current_id ) : get_current_user_id() ); |
|
1607 | + if (empty($user_id)) { |
|
1608 | + $user_id = !empty($wpi_userID) ? $wpi_userID : ($wpi_current_id ? wpinv_get_user_id($wpi_current_id) : get_current_user_id()); |
|
1609 | 1609 | } |
1610 | 1610 | |
1611 | - $vat_number = empty( $user_id ) ? '' : get_user_meta( $user_id, '_wpinv_vat_number', true ); |
|
1611 | + $vat_number = empty($user_id) ? '' : get_user_meta($user_id, '_wpinv_vat_number', true); |
|
1612 | 1612 | |
1613 | 1613 | /* TODO |
1614 | 1614 | if ( $is_valid && $vat_number ) { |
@@ -1619,38 +1619,38 @@ discard block |
||
1619 | 1619 | } |
1620 | 1620 | */ |
1621 | 1621 | |
1622 | - return apply_filters('wpinv_get_user_vat_number', $vat_number, $user_id, $is_valid ); |
|
1622 | + return apply_filters('wpinv_get_user_vat_number', $vat_number, $user_id, $is_valid); |
|
1623 | 1623 | } |
1624 | 1624 | |
1625 | - public static function get_user_company( $company = '', $user_id = 0 ) { |
|
1625 | + public static function get_user_company($company = '', $user_id = 0) { |
|
1626 | 1626 | global $wpi_current_id, $wpi_userID; |
1627 | 1627 | |
1628 | - if ( empty( $user_id ) ) { |
|
1629 | - $user_id = !empty( $wpi_userID ) ? $wpi_userID : ( $wpi_current_id ? wpinv_get_user_id( $wpi_current_id ) : get_current_user_id() ); |
|
1628 | + if (empty($user_id)) { |
|
1629 | + $user_id = !empty($wpi_userID) ? $wpi_userID : ($wpi_current_id ? wpinv_get_user_id($wpi_current_id) : get_current_user_id()); |
|
1630 | 1630 | } |
1631 | 1631 | |
1632 | - $company = empty( $user_id ) ? "" : get_user_meta( $user_id, '_wpinv_company', true ); |
|
1632 | + $company = empty($user_id) ? "" : get_user_meta($user_id, '_wpinv_company', true); |
|
1633 | 1633 | |
1634 | - return apply_filters( 'wpinv_user_company', $company, $user_id ); |
|
1634 | + return apply_filters('wpinv_user_company', $company, $user_id); |
|
1635 | 1635 | } |
1636 | 1636 | |
1637 | - public static function save_user_vat_details( $company = '', $vat_number = '' ) { |
|
1638 | - $save = apply_filters( 'wpinv_allow_save_user_vat_details', true ); |
|
1637 | + public static function save_user_vat_details($company = '', $vat_number = '') { |
|
1638 | + $save = apply_filters('wpinv_allow_save_user_vat_details', true); |
|
1639 | 1639 | |
1640 | - if ( is_user_logged_in() && $save ) { |
|
1640 | + if (is_user_logged_in() && $save) { |
|
1641 | 1641 | $user_id = get_current_user_id(); |
1642 | 1642 | |
1643 | - if ( !empty( $vat_number ) ) { |
|
1644 | - update_user_meta( $user_id, '_wpinv_vat_number', $vat_number ); |
|
1643 | + if (!empty($vat_number)) { |
|
1644 | + update_user_meta($user_id, '_wpinv_vat_number', $vat_number); |
|
1645 | 1645 | } else { |
1646 | - delete_user_meta( $user_id, '_wpinv_vat_number'); |
|
1646 | + delete_user_meta($user_id, '_wpinv_vat_number'); |
|
1647 | 1647 | } |
1648 | 1648 | |
1649 | - if ( !empty( $company ) ) { |
|
1650 | - update_user_meta( $user_id, '_wpinv_company', $company ); |
|
1649 | + if (!empty($company)) { |
|
1650 | + update_user_meta($user_id, '_wpinv_company', $company); |
|
1651 | 1651 | } else { |
1652 | - delete_user_meta( $user_id, '_wpinv_company'); |
|
1653 | - delete_user_meta( $user_id, '_wpinv_vat_number'); |
|
1652 | + delete_user_meta($user_id, '_wpinv_company'); |
|
1653 | + delete_user_meta($user_id, '_wpinv_vat_number'); |
|
1654 | 1654 | } |
1655 | 1655 | } |
1656 | 1656 | |
@@ -1660,113 +1660,113 @@ discard block |
||
1660 | 1660 | public static function ajax_vat_validate() { |
1661 | 1661 | global $wpinv_options, $wpi_session; |
1662 | 1662 | |
1663 | - $is_checkout = ( !empty( $_POST['source'] ) && $_POST['source'] == 'checkout' ) ? true : false; |
|
1663 | + $is_checkout = (!empty($_POST['source']) && $_POST['source'] == 'checkout') ? true : false; |
|
1664 | 1664 | $response = array(); |
1665 | 1665 | $response['success'] = false; |
1666 | 1666 | |
1667 | - if ( empty( $_REQUEST['_wpi_nonce'] ) || ( !empty( $_REQUEST['_wpi_nonce'] ) && !wp_verify_nonce( $_REQUEST['_wpi_nonce'], 'vat_validation' ) ) ) { |
|
1668 | - $response['error'] = __( 'Invalid security nonce', 'invoicing' ); |
|
1669 | - wp_send_json( $response ); |
|
1667 | + if (empty($_REQUEST['_wpi_nonce']) || (!empty($_REQUEST['_wpi_nonce']) && !wp_verify_nonce($_REQUEST['_wpi_nonce'], 'vat_validation'))) { |
|
1668 | + $response['error'] = __('Invalid security nonce', 'invoicing'); |
|
1669 | + wp_send_json($response); |
|
1670 | 1670 | } |
1671 | 1671 | |
1672 | - $vat_name = self::get_vat_name(); |
|
1672 | + $vat_name = self::get_vat_name(); |
|
1673 | 1673 | |
1674 | - if ( $is_checkout ) { |
|
1674 | + if ($is_checkout) { |
|
1675 | 1675 | $invoice = wpinv_get_invoice_cart(); |
1676 | 1676 | |
1677 | - if ( !self::requires_vat( false, 0, self::invoice_has_digital_rule( $invoice ) ) ) { |
|
1677 | + if (!self::requires_vat(false, 0, self::invoice_has_digital_rule($invoice))) { |
|
1678 | 1678 | $vat_info = array(); |
1679 | - $wpi_session->set( 'user_vat_data', $vat_info ); |
|
1679 | + $wpi_session->set('user_vat_data', $vat_info); |
|
1680 | 1680 | |
1681 | 1681 | self::save_user_vat_details(); |
1682 | 1682 | |
1683 | 1683 | $response['success'] = true; |
1684 | - $response['message'] = wp_sprintf( __( 'Ignore %s', 'invoicing' ), $vat_name ); |
|
1685 | - wp_send_json( $response ); |
|
1684 | + $response['message'] = wp_sprintf(__('Ignore %s', 'invoicing'), $vat_name); |
|
1685 | + wp_send_json($response); |
|
1686 | 1686 | } |
1687 | 1687 | } |
1688 | 1688 | |
1689 | - $company = !empty( $_POST['company'] ) ? sanitize_text_field( $_POST['company'] ) : ''; |
|
1690 | - $vat_number = !empty( $_POST['number'] ) ? sanitize_text_field( $_POST['number'] ) : ''; |
|
1689 | + $company = !empty($_POST['company']) ? sanitize_text_field($_POST['company']) : ''; |
|
1690 | + $vat_number = !empty($_POST['number']) ? sanitize_text_field($_POST['number']) : ''; |
|
1691 | 1691 | |
1692 | - $vat_info = $wpi_session->get( 'user_vat_data' ); |
|
1693 | - if ( !is_array( $vat_info ) || empty( $vat_info ) ) { |
|
1694 | - $vat_info = array( 'company'=> $company, 'number' => '', 'valid' => true ); |
|
1692 | + $vat_info = $wpi_session->get('user_vat_data'); |
|
1693 | + if (!is_array($vat_info) || empty($vat_info)) { |
|
1694 | + $vat_info = array('company'=> $company, 'number' => '', 'valid' => true); |
|
1695 | 1695 | } |
1696 | 1696 | |
1697 | - if ( empty( $vat_number ) ) { |
|
1698 | - if ( $is_checkout ) { |
|
1697 | + if (empty($vat_number)) { |
|
1698 | + if ($is_checkout) { |
|
1699 | 1699 | $response['success'] = true; |
1700 | - $response['message'] = wp_sprintf( __( 'No %s number has been applied. %s will be added to invoice totals', 'invoicing' ), $vat_name, $vat_name ); |
|
1700 | + $response['message'] = wp_sprintf(__('No %s number has been applied. %s will be added to invoice totals', 'invoicing'), $vat_name, $vat_name); |
|
1701 | 1701 | |
1702 | - $vat_info = $wpi_session->get( 'user_vat_data' ); |
|
1702 | + $vat_info = $wpi_session->get('user_vat_data'); |
|
1703 | 1703 | $vat_info['number'] = ""; |
1704 | 1704 | $vat_info['valid'] = true; |
1705 | 1705 | |
1706 | - self::save_user_vat_details( $company ); |
|
1706 | + self::save_user_vat_details($company); |
|
1707 | 1707 | } else { |
1708 | - $response['error'] = wp_sprintf( __( 'Please enter your %s number!', 'invoicing' ), $vat_name ); |
|
1708 | + $response['error'] = wp_sprintf(__('Please enter your %s number!', 'invoicing'), $vat_name); |
|
1709 | 1709 | |
1710 | 1710 | $vat_info['valid'] = false; |
1711 | 1711 | } |
1712 | 1712 | |
1713 | - $wpi_session->set( 'user_vat_data', $vat_info ); |
|
1714 | - wp_send_json( $response ); |
|
1713 | + $wpi_session->set('user_vat_data', $vat_info); |
|
1714 | + wp_send_json($response); |
|
1715 | 1715 | } |
1716 | 1716 | |
1717 | - if ( empty( $company ) ) { |
|
1717 | + if (empty($company)) { |
|
1718 | 1718 | $vat_info['valid'] = false; |
1719 | - $wpi_session->set( 'user_vat_data', $vat_info ); |
|
1719 | + $wpi_session->set('user_vat_data', $vat_info); |
|
1720 | 1720 | |
1721 | - $response['error'] = __( 'Please enter your registered company name!', 'invoicing' ); |
|
1722 | - wp_send_json( $response ); |
|
1721 | + $response['error'] = __('Please enter your registered company name!', 'invoicing'); |
|
1722 | + wp_send_json($response); |
|
1723 | 1723 | } |
1724 | 1724 | |
1725 | - if ( !empty( $wpinv_options['vat_vies_check'] ) ) { |
|
1726 | - if ( empty( $wpinv_options['vat_offline_check'] ) && !self::offline_check( $vat_number ) ) { |
|
1725 | + if (!empty($wpinv_options['vat_vies_check'])) { |
|
1726 | + if (empty($wpinv_options['vat_offline_check']) && !self::offline_check($vat_number)) { |
|
1727 | 1727 | $vat_info['valid'] = false; |
1728 | - $wpi_session->set( 'user_vat_data', $vat_info ); |
|
1728 | + $wpi_session->set('user_vat_data', $vat_info); |
|
1729 | 1729 | |
1730 | - $response['error'] = wp_sprintf( __( '%s number not validated', 'invoicing' ), $vat_name ); |
|
1731 | - wp_send_json( $response ); |
|
1730 | + $response['error'] = wp_sprintf(__('%s number not validated', 'invoicing'), $vat_name); |
|
1731 | + wp_send_json($response); |
|
1732 | 1732 | } |
1733 | 1733 | |
1734 | 1734 | $response['success'] = true; |
1735 | - $response['message'] = wp_sprintf( __( '%s number validated', 'invoicing' ), $vat_name ); |
|
1735 | + $response['message'] = wp_sprintf(__('%s number validated', 'invoicing'), $vat_name); |
|
1736 | 1736 | } else { |
1737 | - $result = self::check_vat( $vat_number ); |
|
1737 | + $result = self::check_vat($vat_number); |
|
1738 | 1738 | |
1739 | - if ( empty( $result['valid'] ) ) { |
|
1739 | + if (empty($result['valid'])) { |
|
1740 | 1740 | $response['error'] = $result['message']; |
1741 | - wp_send_json( $response ); |
|
1741 | + wp_send_json($response); |
|
1742 | 1742 | } |
1743 | 1743 | |
1744 | - $vies_company = !empty( $result['company'] ) ? $result['company'] : ''; |
|
1745 | - $vies_company = apply_filters( 'wpinv_vies_company_name', $vies_company ); |
|
1744 | + $vies_company = !empty($result['company']) ? $result['company'] : ''; |
|
1745 | + $vies_company = apply_filters('wpinv_vies_company_name', $vies_company); |
|
1746 | 1746 | |
1747 | - $valid_company = $vies_company && $company && ( $vies_company == '---' || strcasecmp( trim( $vies_company ), trim( $company ) ) == 0 ) ? true : false; |
|
1747 | + $valid_company = $vies_company && $company && ($vies_company == '---' || strcasecmp(trim($vies_company), trim($company)) == 0) ? true : false; |
|
1748 | 1748 | |
1749 | - if ( !empty( $wpinv_options['vat_disable_company_name_check'] ) || $valid_company ) { |
|
1749 | + if (!empty($wpinv_options['vat_disable_company_name_check']) || $valid_company) { |
|
1750 | 1750 | $response['success'] = true; |
1751 | - $response['message'] = wp_sprintf( __( '%s number validated', 'invoicing' ), $vat_name ); |
|
1751 | + $response['message'] = wp_sprintf(__('%s number validated', 'invoicing'), $vat_name); |
|
1752 | 1752 | } else { |
1753 | 1753 | $vat_info['valid'] = false; |
1754 | - $wpi_session->set( 'user_vat_data', $vat_info ); |
|
1754 | + $wpi_session->set('user_vat_data', $vat_info); |
|
1755 | 1755 | |
1756 | 1756 | $response['success'] = false; |
1757 | - $response['message'] = wp_sprintf( __( 'The company name associated with the %s number provided is not the same as the company name provided.', 'invoicing' ), $vat_name ); |
|
1758 | - wp_send_json( $response ); |
|
1757 | + $response['message'] = wp_sprintf(__('The company name associated with the %s number provided is not the same as the company name provided.', 'invoicing'), $vat_name); |
|
1758 | + wp_send_json($response); |
|
1759 | 1759 | } |
1760 | 1760 | } |
1761 | 1761 | |
1762 | - if ( $is_checkout ) { |
|
1763 | - self::save_user_vat_details( $company, $vat_number ); |
|
1762 | + if ($is_checkout) { |
|
1763 | + self::save_user_vat_details($company, $vat_number); |
|
1764 | 1764 | |
1765 | - $vat_info = array('company' => $company, 'number' => $vat_number, 'valid' => true ); |
|
1766 | - $wpi_session->set( 'user_vat_data', $vat_info ); |
|
1765 | + $vat_info = array('company' => $company, 'number' => $vat_number, 'valid' => true); |
|
1766 | + $wpi_session->set('user_vat_data', $vat_info); |
|
1767 | 1767 | } |
1768 | 1768 | |
1769 | - wp_send_json( $response ); |
|
1769 | + wp_send_json($response); |
|
1770 | 1770 | } |
1771 | 1771 | |
1772 | 1772 | public static function ajax_vat_reset() { |
@@ -1775,161 +1775,161 @@ discard block |
||
1775 | 1775 | $company = is_user_logged_in() ? self::get_user_company() : ''; |
1776 | 1776 | $vat_number = self::get_user_vat_number(); |
1777 | 1777 | |
1778 | - $vat_info = array('company' => $company, 'number' => $vat_number, 'valid' => false ); |
|
1779 | - $wpi_session->set( 'user_vat_data', $vat_info ); |
|
1778 | + $vat_info = array('company' => $company, 'number' => $vat_number, 'valid' => false); |
|
1779 | + $wpi_session->set('user_vat_data', $vat_info); |
|
1780 | 1780 | |
1781 | 1781 | $response = array(); |
1782 | 1782 | $response['success'] = true; |
1783 | 1783 | $response['data']['company'] = $company; |
1784 | 1784 | $response['data']['number'] = $vat_number; |
1785 | 1785 | |
1786 | - wp_send_json( $response ); |
|
1786 | + wp_send_json($response); |
|
1787 | 1787 | } |
1788 | 1788 | |
1789 | - public static function checkout_vat_validate( $valid_data, $post ) { |
|
1789 | + public static function checkout_vat_validate($valid_data, $post) { |
|
1790 | 1790 | global $wpinv_options, $wpi_session; |
1791 | 1791 | |
1792 | - $vat_name = __( self::get_vat_name(), 'invoicing' ); |
|
1792 | + $vat_name = __(self::get_vat_name(), 'invoicing'); |
|
1793 | 1793 | |
1794 | - if ( !isset( $_POST['_wpi_nonce'] ) || !wp_verify_nonce( $_POST['_wpi_nonce'], 'vat_validation' ) ) { |
|
1795 | - wpinv_set_error( 'vat_validation', wp_sprintf( __( "Invalid %s validation request.", 'invoicing' ), $vat_name ) ); |
|
1794 | + if (!isset($_POST['_wpi_nonce']) || !wp_verify_nonce($_POST['_wpi_nonce'], 'vat_validation')) { |
|
1795 | + wpinv_set_error('vat_validation', wp_sprintf(__("Invalid %s validation request.", 'invoicing'), $vat_name)); |
|
1796 | 1796 | return; |
1797 | 1797 | } |
1798 | 1798 | |
1799 | - $vat_saved = $wpi_session->get( 'user_vat_data' ); |
|
1800 | - $wpi_session->set( 'user_vat_data', null ); |
|
1799 | + $vat_saved = $wpi_session->get('user_vat_data'); |
|
1800 | + $wpi_session->set('user_vat_data', null); |
|
1801 | 1801 | |
1802 | 1802 | $invoice = wpinv_get_invoice_cart(); |
1803 | 1803 | $amount = $invoice->get_total(); |
1804 | - $is_digital = self::invoice_has_digital_rule( $invoice ); |
|
1805 | - $no_vat = !self::requires_vat( 0, false, $is_digital ); |
|
1804 | + $is_digital = self::invoice_has_digital_rule($invoice); |
|
1805 | + $no_vat = !self::requires_vat(0, false, $is_digital); |
|
1806 | 1806 | |
1807 | - $company = !empty( $_POST['wpinv_company'] ) ? $_POST['wpinv_company'] : null; |
|
1808 | - $vat_number = !empty( $_POST['wpinv_vat_number'] ) ? $_POST['wpinv_vat_number'] : null; |
|
1809 | - $country = !empty( $_POST['wpinv_country'] ) ? $_POST['wpinv_country'] : $invoice->country; |
|
1810 | - if ( empty( $country ) ) { |
|
1807 | + $company = !empty($_POST['wpinv_company']) ? $_POST['wpinv_company'] : null; |
|
1808 | + $vat_number = !empty($_POST['wpinv_vat_number']) ? $_POST['wpinv_vat_number'] : null; |
|
1809 | + $country = !empty($_POST['wpinv_country']) ? $_POST['wpinv_country'] : $invoice->country; |
|
1810 | + if (empty($country)) { |
|
1811 | 1811 | $country = wpinv_default_billing_country(); |
1812 | 1812 | } |
1813 | 1813 | |
1814 | - if ( !$is_digital && $no_vat ) { |
|
1814 | + if (!$is_digital && $no_vat) { |
|
1815 | 1815 | return; |
1816 | 1816 | } |
1817 | 1817 | |
1818 | - $vat_data = array( 'company' => '', 'number' => '', 'valid' => false ); |
|
1818 | + $vat_data = array('company' => '', 'number' => '', 'valid' => false); |
|
1819 | 1819 | |
1820 | 1820 | $ip_country_code = self::get_country_by_ip(); |
1821 | - $is_eu_state = self::is_eu_state( $country ); |
|
1822 | - $is_eu_state_ip = self::is_eu_state( $ip_country_code ); |
|
1821 | + $is_eu_state = self::is_eu_state($country); |
|
1822 | + $is_eu_state_ip = self::is_eu_state($ip_country_code); |
|
1823 | 1823 | $is_non_eu_user = !$is_eu_state && !$is_eu_state_ip; |
1824 | 1824 | |
1825 | - if ( $is_digital && !$is_non_eu_user && empty( $vat_number ) && apply_filters( 'wpinv_checkout_requires_country', true, $amount ) ) { |
|
1825 | + if ($is_digital && !$is_non_eu_user && empty($vat_number) && apply_filters('wpinv_checkout_requires_country', true, $amount)) { |
|
1826 | 1826 | $vat_data['adddress_confirmed'] = false; |
1827 | 1827 | |
1828 | - if ( !isset( $_POST['wpinv_adddress_confirmed'] ) ) { |
|
1829 | - if ( $ip_country_code != $country ) { |
|
1830 | - wpinv_set_error( 'vat_validation', sprintf( __( 'The country of your current location must be the same as the country of your billing location or you must %s confirm %s the billing address is your home country.', 'invoicing' ), '<a href="#wpinv_adddress_confirm">', '</a>' ) ); |
|
1828 | + if (!isset($_POST['wpinv_adddress_confirmed'])) { |
|
1829 | + if ($ip_country_code != $country) { |
|
1830 | + wpinv_set_error('vat_validation', sprintf(__('The country of your current location must be the same as the country of your billing location or you must %s confirm %s the billing address is your home country.', 'invoicing'), '<a href="#wpinv_adddress_confirm">', '</a>')); |
|
1831 | 1831 | } |
1832 | 1832 | } else { |
1833 | 1833 | $vat_data['adddress_confirmed'] = true; |
1834 | 1834 | } |
1835 | 1835 | } |
1836 | 1836 | |
1837 | - if ( !empty( $wpinv_options['vat_prevent_b2c_purchase'] ) && !$is_non_eu_user && ( empty( $vat_number ) || $no_vat ) ) { |
|
1838 | - if ( $is_eu_state ) { |
|
1839 | - wpinv_set_error( 'vat_validation', wp_sprintf( __( 'Please enter and validate your %s number to verify your purchase is by an EU business.', 'invoicing' ), $vat_name ) ); |
|
1840 | - } else if ( $is_digital && $is_eu_state_ip ) { |
|
1841 | - wpinv_set_error( 'vat_validation', wp_sprintf( __( 'Sales to non-EU countries cannot be completed because %s must be applied.', 'invoicing' ), $vat_name ) ); |
|
1837 | + if (!empty($wpinv_options['vat_prevent_b2c_purchase']) && !$is_non_eu_user && (empty($vat_number) || $no_vat)) { |
|
1838 | + if ($is_eu_state) { |
|
1839 | + wpinv_set_error('vat_validation', wp_sprintf(__('Please enter and validate your %s number to verify your purchase is by an EU business.', 'invoicing'), $vat_name)); |
|
1840 | + } else if ($is_digital && $is_eu_state_ip) { |
|
1841 | + wpinv_set_error('vat_validation', wp_sprintf(__('Sales to non-EU countries cannot be completed because %s must be applied.', 'invoicing'), $vat_name)); |
|
1842 | 1842 | } |
1843 | 1843 | } |
1844 | 1844 | |
1845 | - if ( !$is_eu_state || $no_vat || empty( $vat_number ) ) { |
|
1845 | + if (!$is_eu_state || $no_vat || empty($vat_number)) { |
|
1846 | 1846 | return; |
1847 | 1847 | } |
1848 | 1848 | |
1849 | - if ( !empty( $vat_saved ) && isset( $vat_saved['valid'] ) ) { |
|
1850 | - $vat_data['valid'] = $vat_saved['valid']; |
|
1849 | + if (!empty($vat_saved) && isset($vat_saved['valid'])) { |
|
1850 | + $vat_data['valid'] = $vat_saved['valid']; |
|
1851 | 1851 | } |
1852 | 1852 | |
1853 | - if ( $company !== null ) { |
|
1853 | + if ($company !== null) { |
|
1854 | 1854 | $vat_data['company'] = $company; |
1855 | 1855 | } |
1856 | 1856 | |
1857 | 1857 | $message = ''; |
1858 | - if ( $vat_number !== null ) { |
|
1858 | + if ($vat_number !== null) { |
|
1859 | 1859 | $vat_data['number'] = $vat_number; |
1860 | 1860 | |
1861 | - if ( !$vat_data['valid'] || ( $vat_saved['number'] !== $vat_data['number'] ) || ( $vat_saved['company'] !== $vat_data['company'] ) ) { |
|
1862 | - if ( !empty( $wpinv_options['vat_vies_check'] ) ) { |
|
1863 | - if ( empty( $wpinv_options['vat_offline_check'] ) && !self::offline_check( $vat_number ) ) { |
|
1861 | + if (!$vat_data['valid'] || ($vat_saved['number'] !== $vat_data['number']) || ($vat_saved['company'] !== $vat_data['company'])) { |
|
1862 | + if (!empty($wpinv_options['vat_vies_check'])) { |
|
1863 | + if (empty($wpinv_options['vat_offline_check']) && !self::offline_check($vat_number)) { |
|
1864 | 1864 | $vat_data['valid'] = false; |
1865 | 1865 | } |
1866 | 1866 | } else { |
1867 | - $result = self::check_vat( $vat_number ); |
|
1867 | + $result = self::check_vat($vat_number); |
|
1868 | 1868 | |
1869 | - if ( !empty( $result['valid'] ) ) { |
|
1869 | + if (!empty($result['valid'])) { |
|
1870 | 1870 | $vat_data['valid'] = true; |
1871 | - $vies_company = !empty( $result['company'] ) ? $result['company'] : ''; |
|
1872 | - $vies_company = apply_filters( 'wpinv_vies_company_name', $vies_company ); |
|
1871 | + $vies_company = !empty($result['company']) ? $result['company'] : ''; |
|
1872 | + $vies_company = apply_filters('wpinv_vies_company_name', $vies_company); |
|
1873 | 1873 | |
1874 | - $valid_company = $vies_company && $company && ( $vies_company == '---' || strcasecmp( trim( $vies_company ), trim( $company ) ) == 0 ) ? true : false; |
|
1874 | + $valid_company = $vies_company && $company && ($vies_company == '---' || strcasecmp(trim($vies_company), trim($company)) == 0) ? true : false; |
|
1875 | 1875 | |
1876 | - if ( !( !empty( $wpinv_options['vat_disable_company_name_check'] ) || $valid_company ) ) { |
|
1876 | + if (!(!empty($wpinv_options['vat_disable_company_name_check']) || $valid_company)) { |
|
1877 | 1877 | $vat_data['valid'] = false; |
1878 | 1878 | |
1879 | - $message = wp_sprintf( __( 'The company name associated with the %s number provided is not the same as the company name provided.', 'invoicing' ), $vat_name ); |
|
1879 | + $message = wp_sprintf(__('The company name associated with the %s number provided is not the same as the company name provided.', 'invoicing'), $vat_name); |
|
1880 | 1880 | } |
1881 | 1881 | } else { |
1882 | - $message = wp_sprintf( __( 'Fail to validate the %s number: EU Commission VAT server (VIES) check fails.', 'invoicing' ), $vat_name ); |
|
1882 | + $message = wp_sprintf(__('Fail to validate the %s number: EU Commission VAT server (VIES) check fails.', 'invoicing'), $vat_name); |
|
1883 | 1883 | } |
1884 | 1884 | } |
1885 | 1885 | |
1886 | - if ( !$vat_data['valid'] ) { |
|
1887 | - $error = wp_sprintf( __( 'The %s %s number %s you have entered has not been validated', 'invoicing' ), '<a href="#wpi-vat-details">', $vat_name, '</a>' ) . ( $message ? ' ( ' . $message . ' )' : '' ); |
|
1888 | - wpinv_set_error( 'vat_validation', $error ); |
|
1886 | + if (!$vat_data['valid']) { |
|
1887 | + $error = wp_sprintf(__('The %s %s number %s you have entered has not been validated', 'invoicing'), '<a href="#wpi-vat-details">', $vat_name, '</a>') . ($message ? ' ( ' . $message . ' )' : ''); |
|
1888 | + wpinv_set_error('vat_validation', $error); |
|
1889 | 1889 | } |
1890 | 1890 | } |
1891 | 1891 | } |
1892 | 1892 | |
1893 | - $wpi_session->set( 'user_vat_data', $vat_data ); |
|
1893 | + $wpi_session->set('user_vat_data', $vat_data); |
|
1894 | 1894 | } |
1895 | 1895 | |
1896 | - public static function checkout_vat_fields( $billing_details ) { |
|
1896 | + public static function checkout_vat_fields($billing_details) { |
|
1897 | 1897 | global $wpi_session, $wpinv_options, $wpi_country, $wpi_requires_vat; |
1898 | 1898 | |
1899 | 1899 | $ip_address = wpinv_get_ip(); |
1900 | 1900 | $ip_country_code = self::get_country_by_ip(); |
1901 | 1901 | |
1902 | - $tax_label = __( self::get_vat_name(), 'invoicing' ); |
|
1902 | + $tax_label = __(self::get_vat_name(), 'invoicing'); |
|
1903 | 1903 | $invoice = wpinv_get_invoice_cart(); |
1904 | - $is_digital = self::invoice_has_digital_rule( $invoice ); |
|
1904 | + $is_digital = self::invoice_has_digital_rule($invoice); |
|
1905 | 1905 | $wpi_country = $invoice->country; |
1906 | 1906 | |
1907 | - $requires_vat = !self::hide_vat_fields() && !$invoice->is_free() && self::requires_vat( 0, false, $is_digital ); |
|
1907 | + $requires_vat = !self::hide_vat_fields() && !$invoice->is_free() && self::requires_vat(0, false, $is_digital); |
|
1908 | 1908 | $wpi_requires_vat = $requires_vat; |
1909 | 1909 | |
1910 | 1910 | $company = self::get_user_company(); |
1911 | 1911 | $vat_number = self::get_user_vat_number(); |
1912 | 1912 | |
1913 | - $validated = $vat_number ? self::get_user_vat_number( '', 0, true ) : 1; |
|
1914 | - $vat_info = $wpi_session->get( 'user_vat_data' ); |
|
1913 | + $validated = $vat_number ? self::get_user_vat_number('', 0, true) : 1; |
|
1914 | + $vat_info = $wpi_session->get('user_vat_data'); |
|
1915 | 1915 | |
1916 | - if ( is_array( $vat_info ) ) { |
|
1917 | - $company = isset( $vat_info['company'] ) ? $vat_info['company'] : ''; |
|
1918 | - $vat_number = isset( $vat_info['number'] ) ? $vat_info['number'] : ''; |
|
1919 | - $validated = isset( $vat_info['valid'] ) ? $vat_info['valid'] : false; |
|
1916 | + if (is_array($vat_info)) { |
|
1917 | + $company = isset($vat_info['company']) ? $vat_info['company'] : ''; |
|
1918 | + $vat_number = isset($vat_info['number']) ? $vat_info['number'] : ''; |
|
1919 | + $validated = isset($vat_info['valid']) ? $vat_info['valid'] : false; |
|
1920 | 1920 | } |
1921 | 1921 | |
1922 | 1922 | $selected_country = $invoice->country ? $invoice->country : wpinv_default_billing_country(); |
1923 | 1923 | |
1924 | - if ( $ip_country_code == 'UK' ) { |
|
1924 | + if ($ip_country_code == 'UK') { |
|
1925 | 1925 | $ip_country_code = 'GB'; |
1926 | 1926 | } |
1927 | 1927 | |
1928 | - if ( $selected_country == 'UK' ) { |
|
1928 | + if ($selected_country == 'UK') { |
|
1929 | 1929 | $selected_country = 'GB'; |
1930 | 1930 | } |
1931 | 1931 | |
1932 | - if ( $requires_vat && ( self::same_country_rule() == 'no' && wpinv_is_base_country( $selected_country ) || !self::allow_vat_rules() ) ) { |
|
1932 | + if ($requires_vat && (self::same_country_rule() == 'no' && wpinv_is_base_country($selected_country) || !self::allow_vat_rules())) { |
|
1933 | 1933 | $requires_vat = false; |
1934 | 1934 | } |
1935 | 1935 | |
@@ -1937,52 +1937,52 @@ discard block |
||
1937 | 1937 | $display_validate_btn = 'none'; |
1938 | 1938 | $display_reset_btn = 'none'; |
1939 | 1939 | |
1940 | - if ( !empty( $vat_number ) && $validated ) { |
|
1941 | - $vat_vailidated_text = wp_sprintf( __( '%s number validated', 'invoicing' ), $tax_label ); |
|
1940 | + if (!empty($vat_number) && $validated) { |
|
1941 | + $vat_vailidated_text = wp_sprintf(__('%s number validated', 'invoicing'), $tax_label); |
|
1942 | 1942 | $vat_vailidated_class = 'wpinv-vat-stat-1'; |
1943 | 1943 | $display_reset_btn = 'block'; |
1944 | 1944 | } else { |
1945 | - $vat_vailidated_text = empty( $vat_number ) ? '' : wp_sprintf( __( '%s number not validated', 'invoicing' ), $tax_label ); |
|
1946 | - $vat_vailidated_class = empty( $vat_number ) ? '' : 'wpinv-vat-stat-0'; |
|
1945 | + $vat_vailidated_text = empty($vat_number) ? '' : wp_sprintf(__('%s number not validated', 'invoicing'), $tax_label); |
|
1946 | + $vat_vailidated_class = empty($vat_number) ? '' : 'wpinv-vat-stat-0'; |
|
1947 | 1947 | $display_validate_btn = 'block'; |
1948 | 1948 | } |
1949 | 1949 | |
1950 | - $show_ip_country = $is_digital && ( empty( $vat_number ) || !$requires_vat ) && $ip_country_code != $selected_country ? 'block' : 'none'; |
|
1950 | + $show_ip_country = $is_digital && (empty($vat_number) || !$requires_vat) && $ip_country_code != $selected_country ? 'block' : 'none'; |
|
1951 | 1951 | ?> |
1952 | 1952 | <div id="wpi-vat-details" class="wpi-vat-details clearfix" style="display:<?php echo $display_vat_details; ?>"> |
1953 | 1953 | <div id="wpi_vat_info" class="clearfix panel panel-default"> |
1954 | - <div class="panel-heading"><h3 class="panel-title"><?php echo wp_sprintf( __( '%s Details', 'invoicing' ), $tax_label );?></h3></div> |
|
1954 | + <div class="panel-heading"><h3 class="panel-title"><?php echo wp_sprintf(__('%s Details', 'invoicing'), $tax_label); ?></h3></div> |
|
1955 | 1955 | <div id="wpinv-fields-box" class="panel-body"> |
1956 | 1956 | <p id="wpi_show_vat_note"> |
1957 | - <?php echo wp_sprintf( __( 'Validate your registered %s number to exclude tax.', 'invoicing' ), $tax_label ); ?> |
|
1957 | + <?php echo wp_sprintf(__('Validate your registered %s number to exclude tax.', 'invoicing'), $tax_label); ?> |
|
1958 | 1958 | </p> |
1959 | 1959 | <div id="wpi_vat_fields" class="wpi_vat_info"> |
1960 | 1960 | <p class="wpi-cart-field wpi-col2 wpi-colf"> |
1961 | - <label for="wpinv_company" class="wpi-label"><?php _e( 'Company Name', 'invoicing' );?></label> |
|
1961 | + <label for="wpinv_company" class="wpi-label"><?php _e('Company Name', 'invoicing'); ?></label> |
|
1962 | 1962 | <?php |
1963 | - echo wpinv_html_text( array( |
|
1963 | + echo wpinv_html_text(array( |
|
1964 | 1964 | 'id' => 'wpinv_company', |
1965 | 1965 | 'name' => 'wpinv_company', |
1966 | 1966 | 'value' => $company, |
1967 | 1967 | 'class' => 'wpi-input form-control', |
1968 | - 'placeholder' => __( 'Company name', 'invoicing' ), |
|
1969 | - ) ); |
|
1968 | + 'placeholder' => __('Company name', 'invoicing'), |
|
1969 | + )); |
|
1970 | 1970 | ?> |
1971 | 1971 | </p> |
1972 | 1972 | <p class="wpi-cart-field wpi-col2 wpi-coll wpi-cart-field-vat"> |
1973 | - <label for="wpinv_vat_number" class="wpi-label"><?php echo wp_sprintf( __( '%s Number', 'invoicing' ), $tax_label );?></label> |
|
1973 | + <label for="wpinv_vat_number" class="wpi-label"><?php echo wp_sprintf(__('%s Number', 'invoicing'), $tax_label); ?></label> |
|
1974 | 1974 | <span id="wpinv_vat_number-wrap"> |
1975 | 1975 | <label for="wpinv_vat_number" class="wpinv-label"></label> |
1976 | - <input type="text" class="wpi-input form-control" placeholder="<?php echo esc_attr( wp_sprintf( __( '%s number', 'invoicing' ), $tax_label ) );?>" value="<?php esc_attr_e( $vat_number );?>" id="wpinv_vat_number" name="wpinv_vat_number"> |
|
1977 | - <span class="wpinv-vat-stat <?php echo $vat_vailidated_class;?>"><i class="fa"></i> <font><?php echo $vat_vailidated_text;?></font></span> |
|
1976 | + <input type="text" class="wpi-input form-control" placeholder="<?php echo esc_attr(wp_sprintf(__('%s number', 'invoicing'), $tax_label)); ?>" value="<?php esc_attr_e($vat_number); ?>" id="wpinv_vat_number" name="wpinv_vat_number"> |
|
1977 | + <span class="wpinv-vat-stat <?php echo $vat_vailidated_class; ?>"><i class="fa"></i> <font><?php echo $vat_vailidated_text; ?></font></span> |
|
1978 | 1978 | </span> |
1979 | 1979 | </p> |
1980 | 1980 | <p class="wpi-cart-field wpi-col wpi-colf wpi-cart-field-actions"> |
1981 | - <button class="btn btn-success btn-sm wpinv-vat-validate" type="button" id="wpinv_vat_validate" style="display:<?php echo $display_validate_btn; ?>"><?php echo wp_sprintf( __("Validate %s Number", 'invoicing'), $tax_label ); ?></button> |
|
1982 | - <button class="btn btn-danger btn-sm wpinv-vat-reset" type="button" id="wpinv_vat_reset" style="display:<?php echo $display_reset_btn; ?>"><?php echo wp_sprintf( __("Reset %s", 'invoicing'), $tax_label ); ?></button> |
|
1981 | + <button class="btn btn-success btn-sm wpinv-vat-validate" type="button" id="wpinv_vat_validate" style="display:<?php echo $display_validate_btn; ?>"><?php echo wp_sprintf(__("Validate %s Number", 'invoicing'), $tax_label); ?></button> |
|
1982 | + <button class="btn btn-danger btn-sm wpinv-vat-reset" type="button" id="wpinv_vat_reset" style="display:<?php echo $display_reset_btn; ?>"><?php echo wp_sprintf(__("Reset %s", 'invoicing'), $tax_label); ?></button> |
|
1983 | 1983 | <span class="wpi-vat-box wpi-vat-box-info"><span id="text"></span></span> |
1984 | 1984 | <span class="wpi-vat-box wpi-vat-box-error"><span id="text"></span></span> |
1985 | - <input type="hidden" name="_wpi_nonce" value="<?php echo wp_create_nonce( 'vat_validation' ) ?>" /> |
|
1985 | + <input type="hidden" name="_wpi_nonce" value="<?php echo wp_create_nonce('vat_validation') ?>" /> |
|
1986 | 1986 | </p> |
1987 | 1987 | </div> |
1988 | 1988 | </div> |
@@ -1996,32 +1996,32 @@ discard block |
||
1996 | 1996 | </span> |
1997 | 1997 | </div> |
1998 | 1998 | </div> |
1999 | - <?php if ( empty( $wpinv_options['hide_ip_address'] ) ) { |
|
2000 | - $ip_link = '<a title="' . esc_attr( __( 'View more details on map', 'invoicing' ) ) . '" target="_blank" href="' . esc_url( admin_url( 'admin-ajax.php?action=wpinv_ip_geolocation&ip=' . $ip_address ) ) . '" class="wpi-ip-address-link">' . $ip_address . ' <i class="fa fa-external-link-square" aria-hidden="true"></i></a>'; |
|
1999 | + <?php if (empty($wpinv_options['hide_ip_address'])) { |
|
2000 | + $ip_link = '<a title="' . esc_attr(__('View more details on map', 'invoicing')) . '" target="_blank" href="' . esc_url(admin_url('admin-ajax.php?action=wpinv_ip_geolocation&ip=' . $ip_address)) . '" class="wpi-ip-address-link">' . $ip_address . ' <i class="fa fa-external-link-square" aria-hidden="true"></i></a>'; |
|
2001 | 2001 | ?> |
2002 | 2002 | <div class="wpi-ip-info clearfix panel panel-info"> |
2003 | 2003 | <div id="wpinv-fields-box" class="panel-body"> |
2004 | - <span><?php echo wp_sprintf( __( "Your IP address is: %s", 'invoicing' ), $ip_link ); ?></span> |
|
2004 | + <span><?php echo wp_sprintf(__("Your IP address is: %s", 'invoicing'), $ip_link); ?></span> |
|
2005 | 2005 | </div> |
2006 | 2006 | </div> |
2007 | 2007 | <?php } |
2008 | 2008 | } |
2009 | 2009 | |
2010 | - public static function show_vat_notice( $invoice ) { |
|
2011 | - if ( empty( $invoice ) ) { |
|
2010 | + public static function show_vat_notice($invoice) { |
|
2011 | + if (empty($invoice)) { |
|
2012 | 2012 | return NULL; |
2013 | 2013 | } |
2014 | 2014 | |
2015 | - $label = wpinv_get_option( 'vat_invoice_notice_label' ); |
|
2016 | - $notice = wpinv_get_option( 'vat_invoice_notice' ); |
|
2017 | - if ( $label || $notice ) { |
|
2015 | + $label = wpinv_get_option('vat_invoice_notice_label'); |
|
2016 | + $notice = wpinv_get_option('vat_invoice_notice'); |
|
2017 | + if ($label || $notice) { |
|
2018 | 2018 | ?> |
2019 | 2019 | <div class="row wpinv-vat-notice"> |
2020 | 2020 | <div class="col-sm-12"> |
2021 | - <?php if ( $label ) { ?> |
|
2022 | - <strong><?php _e( $label, 'invoicing' ); ?></strong> |
|
2023 | - <?php } if ( $notice ) { ?> |
|
2024 | - <?php echo wpautop( wptexturize( __( $notice, 'invoicing' ) ) ) ?> |
|
2021 | + <?php if ($label) { ?> |
|
2022 | + <strong><?php _e($label, 'invoicing'); ?></strong> |
|
2023 | + <?php } if ($notice) { ?> |
|
2024 | + <?php echo wpautop(wptexturize(__($notice, 'invoicing'))) ?> |
|
2025 | 2025 | <?php } ?> |
2026 | 2026 | </div> |
2027 | 2027 | </div> |
@@ -1,6 +1,8 @@ |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
3 | +if ( ! defined( 'ABSPATH' ) ) { |
|
4 | + exit; |
|
5 | +} |
|
4 | 6 | |
5 | 7 | class WPInv_Item { |
6 | 8 | public $ID = 0; |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
3 | +if (!defined('ABSPATH')) exit; |
|
4 | 4 | |
5 | 5 | class WPInv_Item { |
6 | 6 | public $ID = 0; |
@@ -49,26 +49,26 @@ discard block |
||
49 | 49 | public $filter; |
50 | 50 | |
51 | 51 | |
52 | - public function __construct( $_id = false, $_args = array() ) { |
|
53 | - $item = WP_Post::get_instance( $_id ); |
|
54 | - return $this->setup_item( $item ); |
|
52 | + public function __construct($_id = false, $_args = array()) { |
|
53 | + $item = WP_Post::get_instance($_id); |
|
54 | + return $this->setup_item($item); |
|
55 | 55 | } |
56 | 56 | |
57 | - private function setup_item( $item ) { |
|
58 | - if( ! is_object( $item ) ) { |
|
57 | + private function setup_item($item) { |
|
58 | + if (!is_object($item)) { |
|
59 | 59 | return false; |
60 | 60 | } |
61 | 61 | |
62 | - if( ! is_a( $item, 'WP_Post' ) ) { |
|
62 | + if (!is_a($item, 'WP_Post')) { |
|
63 | 63 | return false; |
64 | 64 | } |
65 | 65 | |
66 | - if( 'wpi_item' !== $item->post_type ) { |
|
66 | + if ('wpi_item' !== $item->post_type) { |
|
67 | 67 | return false; |
68 | 68 | } |
69 | 69 | |
70 | - foreach ( $item as $key => $value ) { |
|
71 | - switch ( $key ) { |
|
70 | + foreach ($item as $key => $value) { |
|
71 | + switch ($key) { |
|
72 | 72 | default: |
73 | 73 | $this->$key = $value; |
74 | 74 | break; |
@@ -78,38 +78,38 @@ discard block |
||
78 | 78 | return true; |
79 | 79 | } |
80 | 80 | |
81 | - public function __get( $key ) { |
|
82 | - if ( method_exists( $this, 'get_' . $key ) ) { |
|
83 | - return call_user_func( array( $this, 'get_' . $key ) ); |
|
81 | + public function __get($key) { |
|
82 | + if (method_exists($this, 'get_' . $key)) { |
|
83 | + return call_user_func(array($this, 'get_' . $key)); |
|
84 | 84 | } else { |
85 | - return new WP_Error( 'wpinv-item-invalid-property', sprintf( __( 'Can\'t get property %s', 'invoicing' ), $key ) ); |
|
85 | + return new WP_Error('wpinv-item-invalid-property', sprintf(__('Can\'t get property %s', 'invoicing'), $key)); |
|
86 | 86 | } |
87 | 87 | } |
88 | 88 | |
89 | - public function create( $data = array(), $wp_error = false ) { |
|
90 | - if ( $this->ID != 0 ) { |
|
89 | + public function create($data = array(), $wp_error = false) { |
|
90 | + if ($this->ID != 0) { |
|
91 | 91 | return false; |
92 | 92 | } |
93 | 93 | |
94 | 94 | $defaults = array( |
95 | 95 | 'post_type' => 'wpi_item', |
96 | 96 | 'post_status' => 'draft', |
97 | - 'post_title' => __( 'New Invoice Item', 'invoicing' ) |
|
97 | + 'post_title' => __('New Invoice Item', 'invoicing') |
|
98 | 98 | ); |
99 | 99 | |
100 | - $args = wp_parse_args( $data, $defaults ); |
|
100 | + $args = wp_parse_args($data, $defaults); |
|
101 | 101 | |
102 | - do_action( 'wpinv_item_pre_create', $args ); |
|
102 | + do_action('wpinv_item_pre_create', $args); |
|
103 | 103 | |
104 | - $id = wp_insert_post( $args, $wp_error ); |
|
104 | + $id = wp_insert_post($args, $wp_error); |
|
105 | 105 | if ($wp_error && is_wp_error($id)) { |
106 | 106 | return $id; |
107 | 107 | } |
108 | - if ( !$id ) { |
|
108 | + if (!$id) { |
|
109 | 109 | return false; |
110 | 110 | } |
111 | 111 | |
112 | - $item = WP_Post::get_instance( $id ); |
|
112 | + $item = WP_Post::get_instance($id); |
|
113 | 113 | |
114 | 114 | if (!empty($item) && !empty($data['meta'])) { |
115 | 115 | $this->ID = $item->ID; |
@@ -117,47 +117,47 @@ discard block |
||
117 | 117 | } |
118 | 118 | |
119 | 119 | // Set custom id if not set. |
120 | - if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
|
121 | - $this->save_metas( array( 'custom_id' => $id ) ); |
|
120 | + if (empty($data['meta']['custom_id']) && !$this->get_custom_id()) { |
|
121 | + $this->save_metas(array('custom_id' => $id)); |
|
122 | 122 | } |
123 | 123 | |
124 | - do_action( 'wpinv_item_create', $id, $args ); |
|
124 | + do_action('wpinv_item_create', $id, $args); |
|
125 | 125 | |
126 | - return $this->setup_item( $item ); |
|
126 | + return $this->setup_item($item); |
|
127 | 127 | } |
128 | 128 | |
129 | - public function update( $data = array(), $wp_error = false ) { |
|
130 | - if ( !$this->ID > 0 ) { |
|
129 | + public function update($data = array(), $wp_error = false) { |
|
130 | + if (!$this->ID > 0) { |
|
131 | 131 | return false; |
132 | 132 | } |
133 | 133 | |
134 | 134 | $data['ID'] = $this->ID; |
135 | 135 | |
136 | - do_action( 'wpinv_item_pre_update', $data ); |
|
136 | + do_action('wpinv_item_pre_update', $data); |
|
137 | 137 | |
138 | - $id = wp_update_post( $data, $wp_error ); |
|
138 | + $id = wp_update_post($data, $wp_error); |
|
139 | 139 | if ($wp_error && is_wp_error($id)) { |
140 | 140 | return $id; |
141 | 141 | } |
142 | 142 | |
143 | - if ( !$id ) { |
|
143 | + if (!$id) { |
|
144 | 144 | return false; |
145 | 145 | } |
146 | 146 | |
147 | - $item = WP_Post::get_instance( $id ); |
|
147 | + $item = WP_Post::get_instance($id); |
|
148 | 148 | if (!empty($item) && !empty($data['meta'])) { |
149 | 149 | $this->ID = $item->ID; |
150 | 150 | $this->save_metas($data['meta']); |
151 | 151 | } |
152 | 152 | |
153 | 153 | // Set custom id if not set. |
154 | - if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
|
155 | - $this->save_metas( array( 'custom_id' => $id ) ); |
|
154 | + if (empty($data['meta']['custom_id']) && !$this->get_custom_id()) { |
|
155 | + $this->save_metas(array('custom_id' => $id)); |
|
156 | 156 | } |
157 | 157 | |
158 | - do_action( 'wpinv_item_update', $id, $data ); |
|
158 | + do_action('wpinv_item_update', $id, $data); |
|
159 | 159 | |
160 | - return $this->setup_item( $item ); |
|
160 | + return $this->setup_item($item); |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | public function get_ID() { |
@@ -165,111 +165,111 @@ discard block |
||
165 | 165 | } |
166 | 166 | |
167 | 167 | public function get_name() { |
168 | - return get_the_title( $this->ID ); |
|
168 | + return get_the_title($this->ID); |
|
169 | 169 | } |
170 | 170 | |
171 | 171 | public function get_title() { |
172 | - return get_the_title( $this->ID ); |
|
172 | + return get_the_title($this->ID); |
|
173 | 173 | } |
174 | 174 | |
175 | 175 | public function get_status() { |
176 | - return get_post_status( $this->ID ); |
|
176 | + return get_post_status($this->ID); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | public function get_summary() { |
180 | - $post = get_post( $this->ID ); |
|
181 | - return !empty( $post->post_excerpt ) ? $post->post_excerpt : ''; |
|
180 | + $post = get_post($this->ID); |
|
181 | + return !empty($post->post_excerpt) ? $post->post_excerpt : ''; |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | public function get_price() { |
185 | - if ( ! isset( $this->price ) ) { |
|
186 | - $this->price = get_post_meta( $this->ID, '_wpinv_price', true ); |
|
185 | + if (!isset($this->price)) { |
|
186 | + $this->price = get_post_meta($this->ID, '_wpinv_price', true); |
|
187 | 187 | |
188 | - if ( $this->price ) { |
|
189 | - $this->price = wpinv_sanitize_amount( $this->price ); |
|
188 | + if ($this->price) { |
|
189 | + $this->price = wpinv_sanitize_amount($this->price); |
|
190 | 190 | } else { |
191 | 191 | $this->price = 0; |
192 | 192 | } |
193 | 193 | } |
194 | 194 | |
195 | - return apply_filters( 'wpinv_get_item_price', $this->price, $this->ID ); |
|
195 | + return apply_filters('wpinv_get_item_price', $this->price, $this->ID); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | public function get_vat_rule() { |
199 | 199 | global $wpinv_euvat; |
200 | 200 | |
201 | - if( !isset( $this->vat_rule ) ) { |
|
202 | - $this->vat_rule = get_post_meta( $this->ID, '_wpinv_vat_rule', true ); |
|
201 | + if (!isset($this->vat_rule)) { |
|
202 | + $this->vat_rule = get_post_meta($this->ID, '_wpinv_vat_rule', true); |
|
203 | 203 | |
204 | - if ( empty( $this->vat_rule ) ) { |
|
204 | + if (empty($this->vat_rule)) { |
|
205 | 205 | $this->vat_rule = $wpinv_euvat->allow_vat_rules() ? 'digital' : 'physical'; |
206 | 206 | } |
207 | 207 | } |
208 | 208 | |
209 | - return apply_filters( 'wpinv_get_item_vat_rule', $this->vat_rule, $this->ID ); |
|
209 | + return apply_filters('wpinv_get_item_vat_rule', $this->vat_rule, $this->ID); |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | public function get_vat_class() { |
213 | - if( !isset( $this->vat_class ) ) { |
|
214 | - $this->vat_class = get_post_meta( $this->ID, '_wpinv_vat_class', true ); |
|
213 | + if (!isset($this->vat_class)) { |
|
214 | + $this->vat_class = get_post_meta($this->ID, '_wpinv_vat_class', true); |
|
215 | 215 | |
216 | - if ( empty( $this->vat_class ) ) { |
|
216 | + if (empty($this->vat_class)) { |
|
217 | 217 | $this->vat_class = '_standard'; |
218 | 218 | } |
219 | 219 | } |
220 | 220 | |
221 | - return apply_filters( 'wpinv_get_item_vat_class', $this->vat_class, $this->ID ); |
|
221 | + return apply_filters('wpinv_get_item_vat_class', $this->vat_class, $this->ID); |
|
222 | 222 | } |
223 | 223 | |
224 | 224 | public function get_type() { |
225 | - if( ! isset( $this->type ) ) { |
|
226 | - $this->type = get_post_meta( $this->ID, '_wpinv_type', true ); |
|
225 | + if (!isset($this->type)) { |
|
226 | + $this->type = get_post_meta($this->ID, '_wpinv_type', true); |
|
227 | 227 | |
228 | - if ( empty( $this->type ) ) { |
|
228 | + if (empty($this->type)) { |
|
229 | 229 | $this->type = 'custom'; |
230 | 230 | } |
231 | 231 | } |
232 | 232 | |
233 | - return apply_filters( 'wpinv_get_item_type', $this->type, $this->ID ); |
|
233 | + return apply_filters('wpinv_get_item_type', $this->type, $this->ID); |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | public function get_custom_id() { |
237 | - $custom_id = get_post_meta( $this->ID, '_wpinv_custom_id', true ); |
|
237 | + $custom_id = get_post_meta($this->ID, '_wpinv_custom_id', true); |
|
238 | 238 | |
239 | - return apply_filters( 'wpinv_get_item_custom_id', $custom_id, $this->ID ); |
|
239 | + return apply_filters('wpinv_get_item_custom_id', $custom_id, $this->ID); |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | public function get_custom_name() { |
243 | - $custom_name = get_post_meta( $this->ID, '_wpinv_custom_name', true ); |
|
243 | + $custom_name = get_post_meta($this->ID, '_wpinv_custom_name', true); |
|
244 | 244 | |
245 | - return apply_filters( 'wpinv_get_item_custom_name', $custom_name, $this->ID ); |
|
245 | + return apply_filters('wpinv_get_item_custom_name', $custom_name, $this->ID); |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | public function get_custom_singular_name() { |
249 | - $custom_singular_name = get_post_meta( $this->ID, '_wpinv_custom_singular_name', true ); |
|
249 | + $custom_singular_name = get_post_meta($this->ID, '_wpinv_custom_singular_name', true); |
|
250 | 250 | |
251 | - return apply_filters( 'wpinv_get_item_custom_singular_name', $custom_singular_name, $this->ID ); |
|
251 | + return apply_filters('wpinv_get_item_custom_singular_name', $custom_singular_name, $this->ID); |
|
252 | 252 | } |
253 | 253 | |
254 | 254 | public function get_editable() { |
255 | - $editable = get_post_meta( $this->ID, '_wpinv_editable', true ); |
|
255 | + $editable = get_post_meta($this->ID, '_wpinv_editable', true); |
|
256 | 256 | |
257 | - return apply_filters( 'wpinv_item_get_editable', $editable, $this->ID ); |
|
257 | + return apply_filters('wpinv_item_get_editable', $editable, $this->ID); |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | public function get_excerpt() { |
261 | - $excerpt = get_the_excerpt( $this->ID ); |
|
261 | + $excerpt = get_the_excerpt($this->ID); |
|
262 | 262 | |
263 | - return apply_filters( 'wpinv_item_get_excerpt', $excerpt, $this->ID ); |
|
263 | + return apply_filters('wpinv_item_get_excerpt', $excerpt, $this->ID); |
|
264 | 264 | } |
265 | 265 | |
266 | 266 | /** |
267 | 267 | * Checks whether the item allows a user to set their own price |
268 | 268 | */ |
269 | 269 | public function get_is_dynamic_pricing() { |
270 | - $is_dynamic_pricing = get_post_meta( $this->ID, '_wpinv_dynamic_pricing', true ); |
|
270 | + $is_dynamic_pricing = get_post_meta($this->ID, '_wpinv_dynamic_pricing', true); |
|
271 | 271 | |
272 | - return (int) apply_filters( 'wpinv_item_get_is_dynamic_pricing', $is_dynamic_pricing, $this->ID ); |
|
272 | + return (int) apply_filters('wpinv_item_get_is_dynamic_pricing', $is_dynamic_pricing, $this->ID); |
|
273 | 273 | |
274 | 274 | } |
275 | 275 | |
@@ -279,32 +279,32 @@ discard block |
||
279 | 279 | public function get_minimum_price() { |
280 | 280 | |
281 | 281 | //Fetch the minimum price and cast it to a float |
282 | - $price = (float) get_post_meta( $this->ID, '_minimum_price', true ); |
|
282 | + $price = (float) get_post_meta($this->ID, '_minimum_price', true); |
|
283 | 283 | |
284 | 284 | //Sanitize it |
285 | - $price = wpinv_sanitize_amount( $price ); |
|
285 | + $price = wpinv_sanitize_amount($price); |
|
286 | 286 | |
287 | 287 | //Filter then return it |
288 | - return apply_filters( 'wpinv_item_get_minimum_price', $price, $this->ID ); |
|
288 | + return apply_filters('wpinv_item_get_minimum_price', $price, $this->ID); |
|
289 | 289 | |
290 | 290 | } |
291 | 291 | |
292 | 292 | public function get_is_recurring() { |
293 | - $is_recurring = get_post_meta( $this->ID, '_wpinv_is_recurring', true ); |
|
293 | + $is_recurring = get_post_meta($this->ID, '_wpinv_is_recurring', true); |
|
294 | 294 | |
295 | - return apply_filters( 'wpinv_item_get_is_recurring', $is_recurring, $this->ID ); |
|
295 | + return apply_filters('wpinv_item_get_is_recurring', $is_recurring, $this->ID); |
|
296 | 296 | |
297 | 297 | } |
298 | 298 | |
299 | - public function get_recurring_period( $full = false ) { |
|
300 | - $period = get_post_meta( $this->ID, '_wpinv_recurring_period', true ); |
|
299 | + public function get_recurring_period($full = false) { |
|
300 | + $period = get_post_meta($this->ID, '_wpinv_recurring_period', true); |
|
301 | 301 | |
302 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
302 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
303 | 303 | $period = 'D'; |
304 | 304 | } |
305 | 305 | |
306 | - if ( $full ) { |
|
307 | - switch( $period ) { |
|
306 | + if ($full) { |
|
307 | + switch ($period) { |
|
308 | 308 | case 'D': |
309 | 309 | $period = 'day'; |
310 | 310 | break; |
@@ -320,40 +320,40 @@ discard block |
||
320 | 320 | } |
321 | 321 | } |
322 | 322 | |
323 | - return apply_filters( 'wpinv_item_recurring_period', $period, $full, $this->ID ); |
|
323 | + return apply_filters('wpinv_item_recurring_period', $period, $full, $this->ID); |
|
324 | 324 | } |
325 | 325 | |
326 | 326 | public function get_recurring_interval() { |
327 | - $interval = (int)get_post_meta( $this->ID, '_wpinv_recurring_interval', true ); |
|
327 | + $interval = (int) get_post_meta($this->ID, '_wpinv_recurring_interval', true); |
|
328 | 328 | |
329 | - if ( !$interval > 0 ) { |
|
329 | + if (!$interval > 0) { |
|
330 | 330 | $interval = 1; |
331 | 331 | } |
332 | 332 | |
333 | - return apply_filters( 'wpinv_item_recurring_interval', $interval, $this->ID ); |
|
333 | + return apply_filters('wpinv_item_recurring_interval', $interval, $this->ID); |
|
334 | 334 | } |
335 | 335 | |
336 | 336 | public function get_recurring_limit() { |
337 | - $limit = get_post_meta( $this->ID, '_wpinv_recurring_limit', true ); |
|
337 | + $limit = get_post_meta($this->ID, '_wpinv_recurring_limit', true); |
|
338 | 338 | |
339 | - return (int)apply_filters( 'wpinv_item_recurring_limit', $limit, $this->ID ); |
|
339 | + return (int) apply_filters('wpinv_item_recurring_limit', $limit, $this->ID); |
|
340 | 340 | } |
341 | 341 | |
342 | 342 | public function get_free_trial() { |
343 | - $free_trial = get_post_meta( $this->ID, '_wpinv_free_trial', true ); |
|
343 | + $free_trial = get_post_meta($this->ID, '_wpinv_free_trial', true); |
|
344 | 344 | |
345 | - return apply_filters( 'wpinv_item_get_free_trial', $free_trial, $this->ID ); |
|
345 | + return apply_filters('wpinv_item_get_free_trial', $free_trial, $this->ID); |
|
346 | 346 | } |
347 | 347 | |
348 | - public function get_trial_period( $full = false ) { |
|
349 | - $period = get_post_meta( $this->ID, '_wpinv_trial_period', true ); |
|
348 | + public function get_trial_period($full = false) { |
|
349 | + $period = get_post_meta($this->ID, '_wpinv_trial_period', true); |
|
350 | 350 | |
351 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
351 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
352 | 352 | $period = 'D'; |
353 | 353 | } |
354 | 354 | |
355 | - if ( $full ) { |
|
356 | - switch( $period ) { |
|
355 | + if ($full) { |
|
356 | + switch ($period) { |
|
357 | 357 | case 'D': |
358 | 358 | $period = 'day'; |
359 | 359 | break; |
@@ -369,47 +369,47 @@ discard block |
||
369 | 369 | } |
370 | 370 | } |
371 | 371 | |
372 | - return apply_filters( 'wpinv_item_trial_period', $period, $full, $this->ID ); |
|
372 | + return apply_filters('wpinv_item_trial_period', $period, $full, $this->ID); |
|
373 | 373 | } |
374 | 374 | |
375 | 375 | public function get_trial_interval() { |
376 | - $interval = absint( get_post_meta( $this->ID, '_wpinv_trial_interval', true ) ); |
|
376 | + $interval = absint(get_post_meta($this->ID, '_wpinv_trial_interval', true)); |
|
377 | 377 | |
378 | - if ( !$interval > 0 ) { |
|
378 | + if (!$interval > 0) { |
|
379 | 379 | $interval = 1; |
380 | 380 | } |
381 | 381 | |
382 | - return apply_filters( 'wpinv_item_trial_interval', $interval, $this->ID ); |
|
382 | + return apply_filters('wpinv_item_trial_interval', $interval, $this->ID); |
|
383 | 383 | } |
384 | 384 | |
385 | 385 | public function get_the_price() { |
386 | - $item_price = wpinv_price( wpinv_format_amount( $this->get_price() ) ); |
|
386 | + $item_price = wpinv_price(wpinv_format_amount($this->get_price())); |
|
387 | 387 | |
388 | - return apply_filters( 'wpinv_get_the_item_price', $item_price, $this->ID ); |
|
388 | + return apply_filters('wpinv_get_the_item_price', $item_price, $this->ID); |
|
389 | 389 | } |
390 | 390 | |
391 | 391 | public function is_recurring() { |
392 | 392 | $is_recurring = $this->get_is_recurring(); |
393 | 393 | |
394 | - return (bool)apply_filters( 'wpinv_is_recurring_item', $is_recurring, $this->ID ); |
|
394 | + return (bool) apply_filters('wpinv_is_recurring_item', $is_recurring, $this->ID); |
|
395 | 395 | } |
396 | 396 | |
397 | 397 | public function has_free_trial() { |
398 | 398 | $free_trial = $this->is_recurring() && $this->get_free_trial() ? true : false; |
399 | 399 | |
400 | - return (bool)apply_filters( 'wpinv_item_has_free_trial', $free_trial, $this->ID ); |
|
400 | + return (bool) apply_filters('wpinv_item_has_free_trial', $free_trial, $this->ID); |
|
401 | 401 | } |
402 | 402 | |
403 | 403 | public function is_free() { |
404 | 404 | $is_free = false; |
405 | 405 | |
406 | - $price = get_post_meta( $this->ID, '_wpinv_price', true ); |
|
406 | + $price = get_post_meta($this->ID, '_wpinv_price', true); |
|
407 | 407 | |
408 | - if ( (float)$price == 0 ) { |
|
408 | + if ((float) $price == 0) { |
|
409 | 409 | $is_free = true; |
410 | 410 | } |
411 | 411 | |
412 | - return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID ); |
|
412 | + return (bool) apply_filters('wpinv_is_free_item', $is_free, $this->ID); |
|
413 | 413 | |
414 | 414 | } |
415 | 415 | |
@@ -418,15 +418,15 @@ discard block |
||
418 | 418 | |
419 | 419 | $is_editable = $editable === 0 || $editable === '0' ? false : true; |
420 | 420 | |
421 | - return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID ); |
|
421 | + return (bool) apply_filters('wpinv_item_is_editable', $is_editable, $this->ID); |
|
422 | 422 | } |
423 | 423 | |
424 | - public function save_metas( $metas = array() ) { |
|
425 | - if ( empty( $metas ) ) { |
|
424 | + public function save_metas($metas = array()) { |
|
425 | + if (empty($metas)) { |
|
426 | 426 | return false; |
427 | 427 | } |
428 | 428 | |
429 | - foreach ( $metas as $meta_key => $meta_value ) { |
|
429 | + foreach ($metas as $meta_key => $meta_value) { |
|
430 | 430 | $meta_key = strpos($meta_key, '_wpinv_') !== 0 ? '_wpinv_' . $meta_key : $meta_key; |
431 | 431 | |
432 | 432 | $this->update_meta($meta_key, $meta_value); |
@@ -435,77 +435,77 @@ discard block |
||
435 | 435 | return true; |
436 | 436 | } |
437 | 437 | |
438 | - public function update_meta( $meta_key = '', $meta_value = '', $prev_value = '' ) { |
|
439 | - if ( empty( $meta_key ) ) { |
|
438 | + public function update_meta($meta_key = '', $meta_value = '', $prev_value = '') { |
|
439 | + if (empty($meta_key)) { |
|
440 | 440 | return false; |
441 | 441 | } |
442 | 442 | |
443 | - if( '_wpinv_minimum_price' === $meta_key ) { |
|
443 | + if ('_wpinv_minimum_price' === $meta_key) { |
|
444 | 444 | $meta_key = '_minimum_price'; |
445 | 445 | } |
446 | 446 | |
447 | - $meta_value = apply_filters( 'wpinv_update_item_meta_' . $meta_key, $meta_value, $this->ID ); |
|
447 | + $meta_value = apply_filters('wpinv_update_item_meta_' . $meta_key, $meta_value, $this->ID); |
|
448 | 448 | |
449 | - return update_post_meta( $this->ID, $meta_key, $meta_value, $prev_value ); |
|
449 | + return update_post_meta($this->ID, $meta_key, $meta_value, $prev_value); |
|
450 | 450 | } |
451 | 451 | |
452 | - public function get_fees( $type = 'fee', $item_id = 0 ) { |
|
452 | + public function get_fees($type = 'fee', $item_id = 0) { |
|
453 | 453 | global $wpi_session; |
454 | 454 | |
455 | - $fees = $wpi_session->get( 'wpi_cart_fees' ); |
|
455 | + $fees = $wpi_session->get('wpi_cart_fees'); |
|
456 | 456 | |
457 | - if ( ! wpinv_get_cart_contents() ) { |
|
457 | + if (!wpinv_get_cart_contents()) { |
|
458 | 458 | // We can only get item type fees when the cart is empty |
459 | 459 | $type = 'custom'; |
460 | 460 | } |
461 | 461 | |
462 | - if ( ! empty( $fees ) && ! empty( $type ) && 'all' !== $type ) { |
|
463 | - foreach( $fees as $key => $fee ) { |
|
464 | - if( ! empty( $fee['type'] ) && $type != $fee['type'] ) { |
|
465 | - unset( $fees[ $key ] ); |
|
462 | + if (!empty($fees) && !empty($type) && 'all' !== $type) { |
|
463 | + foreach ($fees as $key => $fee) { |
|
464 | + if (!empty($fee['type']) && $type != $fee['type']) { |
|
465 | + unset($fees[$key]); |
|
466 | 466 | } |
467 | 467 | } |
468 | 468 | } |
469 | 469 | |
470 | - if ( ! empty( $fees ) && ! empty( $item_id ) ) { |
|
470 | + if (!empty($fees) && !empty($item_id)) { |
|
471 | 471 | // Remove fees that don't belong to the specified Item |
472 | - foreach ( $fees as $key => $fee ) { |
|
473 | - if ( (int) $item_id !== (int)$fee['custom_id'] ) { |
|
474 | - unset( $fees[ $key ] ); |
|
472 | + foreach ($fees as $key => $fee) { |
|
473 | + if ((int) $item_id !== (int) $fee['custom_id']) { |
|
474 | + unset($fees[$key]); |
|
475 | 475 | } |
476 | 476 | } |
477 | 477 | } |
478 | 478 | |
479 | - if ( ! empty( $fees ) ) { |
|
479 | + if (!empty($fees)) { |
|
480 | 480 | // Remove fees that belong to a specific item but are not in the cart |
481 | - foreach( $fees as $key => $fee ) { |
|
482 | - if( empty( $fee['custom_id'] ) ) { |
|
481 | + foreach ($fees as $key => $fee) { |
|
482 | + if (empty($fee['custom_id'])) { |
|
483 | 483 | continue; |
484 | 484 | } |
485 | 485 | |
486 | - if ( !wpinv_item_in_cart( $fee['custom_id'] ) ) { |
|
487 | - unset( $fees[ $key ] ); |
|
486 | + if (!wpinv_item_in_cart($fee['custom_id'])) { |
|
487 | + unset($fees[$key]); |
|
488 | 488 | } |
489 | 489 | } |
490 | 490 | } |
491 | 491 | |
492 | - return ! empty( $fees ) ? $fees : array(); |
|
492 | + return !empty($fees) ? $fees : array(); |
|
493 | 493 | } |
494 | 494 | |
495 | 495 | public function can_purchase() { |
496 | 496 | $can_purchase = true; |
497 | 497 | |
498 | - if ( !current_user_can( 'edit_post', $this->ID ) && $this->post_status != 'publish' ) { |
|
498 | + if (!current_user_can('edit_post', $this->ID) && $this->post_status != 'publish') { |
|
499 | 499 | $can_purchase = false; |
500 | 500 | } |
501 | 501 | |
502 | - return (bool)apply_filters( 'wpinv_can_purchase_item', $can_purchase, $this ); |
|
502 | + return (bool) apply_filters('wpinv_can_purchase_item', $can_purchase, $this); |
|
503 | 503 | } |
504 | 504 | |
505 | 505 | /** |
506 | 506 | * Checks whether this item supports dynamic pricing or not |
507 | 507 | */ |
508 | 508 | public function supports_dynamic_pricing() { |
509 | - return (bool) apply_filters( 'wpinv_item_supports_dynamic_pricing', true, $this ); |
|
509 | + return (bool) apply_filters('wpinv_item_supports_dynamic_pricing', true, $this); |
|
510 | 510 | } |
511 | 511 | } |
@@ -1,6 +1,8 @@ |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
3 | +if ( ! defined( 'ABSPATH' ) ) { |
|
4 | + exit; |
|
5 | +} |
|
4 | 6 | |
5 | 7 | add_action( 'wpinv_worldpay_cc_form', '__return_false' ); |
6 | 8 |
@@ -1,12 +1,12 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
3 | +if (!defined('ABSPATH')) exit; |
|
4 | 4 | |
5 | -add_action( 'wpinv_worldpay_cc_form', '__return_false' ); |
|
5 | +add_action('wpinv_worldpay_cc_form', '__return_false'); |
|
6 | 6 | |
7 | -function wpinv_process_worldpay_payment( $purchase_data ) { |
|
8 | - if( ! wp_verify_nonce( $purchase_data['gateway_nonce'], 'wpi-gateway' ) ) { |
|
9 | - wp_die( __( 'Nonce verification has failed', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
7 | +function wpinv_process_worldpay_payment($purchase_data) { |
|
8 | + if (!wp_verify_nonce($purchase_data['gateway_nonce'], 'wpi-gateway')) { |
|
9 | + wp_die(__('Nonce verification has failed', 'invoicing'), __('Error', 'invoicing'), array('response' => 403)); |
|
10 | 10 | } |
11 | 11 | |
12 | 12 | // Collect payment data |
@@ -24,132 +24,132 @@ discard block |
||
24 | 24 | ); |
25 | 25 | |
26 | 26 | // Record the pending payment |
27 | - $invoice = wpinv_get_invoice( $purchase_data['invoice_id'] ); |
|
27 | + $invoice = wpinv_get_invoice($purchase_data['invoice_id']); |
|
28 | 28 | |
29 | - if ( !empty( $invoice ) ) { |
|
29 | + if (!empty($invoice)) { |
|
30 | 30 | $quantities_enabled = wpinv_item_quantities_enabled(); |
31 | 31 | |
32 | - $instId = wpinv_get_option( 'worldpay_instId', false ); |
|
32 | + $instId = wpinv_get_option('worldpay_instId', false); |
|
33 | 33 | $cartId = $invoice->get_number(); |
34 | - $testMode = wpinv_is_test_mode( 'worldpay' ) ? 100 : 0; |
|
34 | + $testMode = wpinv_is_test_mode('worldpay') ? 100 : 0; |
|
35 | 35 | $name = $invoice->get_user_full_name(); |
36 | - $address = wp_strip_all_tags( $invoice->get_address(), true ); |
|
36 | + $address = wp_strip_all_tags($invoice->get_address(), true); |
|
37 | 37 | $postcode = $invoice->zip; |
38 | 38 | $tel = $invoice->phone; |
39 | 39 | $email = $invoice->get_email(); |
40 | 40 | $country = $invoice->country; |
41 | - $amount = wpinv_sanitize_amount( $invoice->get_total() ); |
|
41 | + $amount = wpinv_sanitize_amount($invoice->get_total()); |
|
42 | 42 | $currency = wpinv_get_currency(); |
43 | 43 | |
44 | 44 | $items = array(); |
45 | - foreach ( $invoice->get_cart_details() as $item ) { |
|
45 | + foreach ($invoice->get_cart_details() as $item) { |
|
46 | 46 | $item_desc = $item['name']; |
47 | - $quantity = !empty( $item['quantity'] ) && $item['quantity'] > 0 ? $item['quantity'] : 1; |
|
48 | - $item_desc .= ' (' . ( $quantities_enabled ? $quantity . 'x ' : '' ) . wpinv_price( wpinv_format_amount( $item['item_price'] ) ) . ')'; |
|
47 | + $quantity = !empty($item['quantity']) && $item['quantity'] > 0 ? $item['quantity'] : 1; |
|
48 | + $item_desc .= ' (' . ($quantities_enabled ? $quantity . 'x ' : '') . wpinv_price(wpinv_format_amount($item['item_price'])) . ')'; |
|
49 | 49 | |
50 | 50 | $items[] = $item_desc; |
51 | 51 | } |
52 | 52 | |
53 | - $desc = implode( ', ', $items ); |
|
54 | - if ( wpinv_use_taxes() && $invoice->get_tax() > 0 ) { |
|
55 | - $desc .= ', ' . wp_sprintf( __( 'Tax: %s', 'invoicing' ), $invoice->get_tax( true ) ); |
|
53 | + $desc = implode(', ', $items); |
|
54 | + if (wpinv_use_taxes() && $invoice->get_tax() > 0) { |
|
55 | + $desc .= ', ' . wp_sprintf(__('Tax: %s', 'invoicing'), $invoice->get_tax(true)); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | $extra_params = array(); |
59 | 59 | $extra_params['MC_description'] = $desc; |
60 | - $extra_params['MC_callback'] = wpinv_get_ipn_url( 'worldpay' ); |
|
60 | + $extra_params['MC_callback'] = wpinv_get_ipn_url('worldpay'); |
|
61 | 61 | $extra_params['MC_key'] = $invoice->get_key(); |
62 | 62 | $extra_params['MC_invoice_id'] = $invoice->ID; |
63 | 63 | $extra_params['address1'] = $address; |
64 | 64 | $extra_params['town'] = $invoice->city; |
65 | 65 | $extra_params['region'] = $invoice->state; |
66 | - $extra_params['amountString'] = $invoice->get_total( true ); |
|
67 | - $extra_params['countryString'] = wpinv_country_name( $invoice->country ); |
|
66 | + $extra_params['amountString'] = $invoice->get_total(true); |
|
67 | + $extra_params['countryString'] = wpinv_country_name($invoice->country); |
|
68 | 68 | $extra_params['compName'] = $invoice->company; |
69 | 69 | |
70 | - $extra_params = apply_filters( 'wpinv_worldpay_form_extra_parameters', $extra_params, $invoice ); |
|
70 | + $extra_params = apply_filters('wpinv_worldpay_form_extra_parameters', $extra_params, $invoice); |
|
71 | 71 | |
72 | - $redirect_text = __( 'Redirecting to Worldpay site, click on button if not redirected.', 'invoicing' ); |
|
73 | - $redirect_text = apply_filters( 'wpinv_worldpay_redirect_text', $redirect_text, $invoice ); |
|
72 | + $redirect_text = __('Redirecting to Worldpay site, click on button if not redirected.', 'invoicing'); |
|
73 | + $redirect_text = apply_filters('wpinv_worldpay_redirect_text', $redirect_text, $invoice); |
|
74 | 74 | |
75 | 75 | // Empty the shopping cart |
76 | 76 | wpinv_empty_cart(); |
77 | 77 | ?> |
78 | 78 | <div class="wpi-worldpay-form" style="padding:20px;font-family:arial,sans-serif;text-align:center;color:#555"> |
79 | -<?php do_action( 'wpinv_worldpay_form_before', $invoice ); ?> |
|
80 | -<h3><?php echo $redirect_text ;?></h3> |
|
79 | +<?php do_action('wpinv_worldpay_form_before', $invoice); ?> |
|
80 | +<h3><?php echo $redirect_text; ?></h3> |
|
81 | 81 | <form action="<?php echo wpinv_get_worldpay_redirect(); ?>" name="wpi_worldpay_form" method="POST"> |
82 | - <input type="hidden" value="<?php echo $amount;?>" name="amount"> |
|
83 | - <input type="hidden" value="<?php echo esc_attr( $cartId );?>" name="cartId"> |
|
84 | - <input type="hidden" value="<?php echo $currency;?>" name="currency"> |
|
85 | - <input type="hidden" value="<?php echo $instId;?>" name="instId"> |
|
86 | - <input type="hidden" value="<?php echo $testMode;?>" name="testMode"> |
|
87 | - <input type="hidden" value="<?php echo esc_attr( $name );?>" name="name"> |
|
88 | - <input type="hidden" value="<?php echo esc_attr( $address );?>" name="address"> |
|
89 | - <input type="hidden" value="<?php echo esc_attr( $postcode );?>" name="postcode"> |
|
90 | - <input type="hidden" value="<?php echo esc_attr( $tel );?>" name="tel"> |
|
91 | - <input type="hidden" value="<?php echo esc_attr( $email );?>" name="email"> |
|
92 | - <input type="hidden" value="<?php echo esc_attr( $country );?>" name="country"> |
|
93 | - <input type="hidden" value="<?php echo esc_attr( $desc );?>" name="desc"> |
|
94 | - <?php foreach ( $extra_params as $param => $value ) { ?> |
|
95 | - <?php if ( !empty( $value !== false ) ) { ?> |
|
96 | - <input type="hidden" value="<?php echo esc_attr( $value );?>" name="<?php echo esc_attr( $param );?>"> |
|
82 | + <input type="hidden" value="<?php echo $amount; ?>" name="amount"> |
|
83 | + <input type="hidden" value="<?php echo esc_attr($cartId); ?>" name="cartId"> |
|
84 | + <input type="hidden" value="<?php echo $currency; ?>" name="currency"> |
|
85 | + <input type="hidden" value="<?php echo $instId; ?>" name="instId"> |
|
86 | + <input type="hidden" value="<?php echo $testMode; ?>" name="testMode"> |
|
87 | + <input type="hidden" value="<?php echo esc_attr($name); ?>" name="name"> |
|
88 | + <input type="hidden" value="<?php echo esc_attr($address); ?>" name="address"> |
|
89 | + <input type="hidden" value="<?php echo esc_attr($postcode); ?>" name="postcode"> |
|
90 | + <input type="hidden" value="<?php echo esc_attr($tel); ?>" name="tel"> |
|
91 | + <input type="hidden" value="<?php echo esc_attr($email); ?>" name="email"> |
|
92 | + <input type="hidden" value="<?php echo esc_attr($country); ?>" name="country"> |
|
93 | + <input type="hidden" value="<?php echo esc_attr($desc); ?>" name="desc"> |
|
94 | + <?php foreach ($extra_params as $param => $value) { ?> |
|
95 | + <?php if (!empty($value !== false)) { ?> |
|
96 | + <input type="hidden" value="<?php echo esc_attr($value); ?>" name="<?php echo esc_attr($param); ?>"> |
|
97 | 97 | <?php } ?> |
98 | 98 | <?php } ?> |
99 | - <?php do_action( 'wpinv_worldpay_form_parameters', $invoice ); ?> |
|
100 | - <input type="submit" name="wpi_worldpay_submit" value="<?php esc_attr_e( 'Pay by Credit Card / Debit Card (WorldPay)', 'invoicing' ) ;?>"> |
|
99 | + <?php do_action('wpinv_worldpay_form_parameters', $invoice); ?> |
|
100 | + <input type="submit" name="wpi_worldpay_submit" value="<?php esc_attr_e('Pay by Credit Card / Debit Card (WorldPay)', 'invoicing'); ?>"> |
|
101 | 101 | </form> |
102 | 102 | <script type="text/javascript">document.wpi_worldpay_form.submit();</script> |
103 | -<?php do_action( 'wpinv_worldpay_form_after', $invoice ); ?> |
|
103 | +<?php do_action('wpinv_worldpay_form_after', $invoice); ?> |
|
104 | 104 | </div> |
105 | 105 | <?php |
106 | 106 | } else { |
107 | - wpinv_record_gateway_error( __( 'Payment Error', 'invoicing' ), sprintf( __( 'Payment creation failed while processing a worldpay payment. Payment data: %s', 'invoicing' ), json_encode( $payment_data ) ), $invoice ); |
|
107 | + wpinv_record_gateway_error(__('Payment Error', 'invoicing'), sprintf(__('Payment creation failed while processing a worldpay payment. Payment data: %s', 'invoicing'), json_encode($payment_data)), $invoice); |
|
108 | 108 | // If errors are present, send the user back to the purchase page so they can be corrected |
109 | - wpinv_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['wpi-gateway'] ); |
|
109 | + wpinv_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['wpi-gateway']); |
|
110 | 110 | } |
111 | 111 | } |
112 | -add_action( 'wpinv_gateway_worldpay', 'wpinv_process_worldpay_payment' ); |
|
112 | +add_action('wpinv_gateway_worldpay', 'wpinv_process_worldpay_payment'); |
|
113 | 113 | |
114 | 114 | function wpinv_get_worldpay_redirect() { |
115 | - $redirect = wpinv_is_test_mode( 'worldpay' ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
115 | + $redirect = wpinv_is_test_mode('worldpay') ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
116 | 116 | |
117 | - return apply_filters( 'wpinv_worldpay_redirect', $redirect ); |
|
117 | + return apply_filters('wpinv_worldpay_redirect', $redirect); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | function wpinv_process_worldpay_ipn() { |
121 | - $request = wpinv_get_post_data( 'post' ); |
|
121 | + $request = wpinv_get_post_data('post'); |
|
122 | 122 | |
123 | - if ( !empty( $request['cartId'] ) && !empty( $request['transStatus'] ) && !empty( $request['installation'] ) && isset( $request['testMode'] ) && isset( $request['MC_invoice_id'] ) && isset( $request['MC_key'] ) ) { |
|
123 | + if (!empty($request['cartId']) && !empty($request['transStatus']) && !empty($request['installation']) && isset($request['testMode']) && isset($request['MC_invoice_id']) && isset($request['MC_key'])) { |
|
124 | 124 | $invoice_id = $request['MC_invoice_id']; |
125 | 125 | |
126 | - if ( $invoice_id == wpinv_get_invoice_id_by_key( $request['MC_key'] ) && $invoice = wpinv_get_invoice( $invoice_id ) ) { |
|
127 | - if ( $request['transStatus'] == 'Y' ) { |
|
128 | - wpinv_update_payment_status( $invoice_id, 'publish' ); |
|
129 | - wpinv_set_payment_transaction_id( $invoice_id, $request['transId'] ); |
|
130 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'Worldpay Transaction ID: %s', 'invoicing' ), $request['transId'] ) ); |
|
126 | + if ($invoice_id == wpinv_get_invoice_id_by_key($request['MC_key']) && $invoice = wpinv_get_invoice($invoice_id)) { |
|
127 | + if ($request['transStatus'] == 'Y') { |
|
128 | + wpinv_update_payment_status($invoice_id, 'publish'); |
|
129 | + wpinv_set_payment_transaction_id($invoice_id, $request['transId']); |
|
130 | + wpinv_insert_payment_note($invoice_id, sprintf(__('Worldpay Transaction ID: %s', 'invoicing'), $request['transId'])); |
|
131 | 131 | return; |
132 | - } else if ( $request['transStatus'] == 'C' ) { |
|
133 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
134 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment transaction failed while processing Worldpay payment, kindly check IPN log.', 'invoicing' ) ); |
|
132 | + } else if ($request['transStatus'] == 'C') { |
|
133 | + wpinv_update_payment_status($invoice_id, 'wpi-failed'); |
|
134 | + wpinv_insert_payment_note($invoice_id, __('Payment transaction failed while processing Worldpay payment, kindly check IPN log.', 'invoicing')); |
|
135 | 135 | |
136 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Payment transaction failed while processing Worldpay payment. IPN data: %s', 'invoicing' ), json_encode( $request ) ), $invoice_id ); |
|
136 | + wpinv_record_gateway_error(__('IPN Error', 'invoicing'), sprintf(__('Payment transaction failed while processing Worldpay payment. IPN data: %s', 'invoicing'), json_encode($request)), $invoice_id); |
|
137 | 137 | return; |
138 | 138 | } |
139 | 139 | } |
140 | 140 | } |
141 | 141 | return; |
142 | 142 | } |
143 | -add_action( 'wpinv_verify_worldpay_ipn', 'wpinv_process_worldpay_ipn' ); |
|
143 | +add_action('wpinv_verify_worldpay_ipn', 'wpinv_process_worldpay_ipn'); |
|
144 | 144 | |
145 | 145 | function wpinv_is_worldpay_valid_for_use() { |
146 | - return in_array( wpinv_get_currency(), apply_filters( 'wpinv_worldpay_supported_currencies', array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' ))); |
|
146 | + return in_array(wpinv_get_currency(), apply_filters('wpinv_worldpay_supported_currencies', array('AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR'))); |
|
147 | 147 | } |
148 | 148 | |
149 | -function wpinv_check_worldpay_currency_support( $gateway_list ) { |
|
150 | - if ( isset( $gateway_list['worldpay'] ) && ! wpinv_is_worldpay_valid_for_use() ) { |
|
151 | - unset( $gateway_list['worldpay'] ); |
|
149 | +function wpinv_check_worldpay_currency_support($gateway_list) { |
|
150 | + if (isset($gateway_list['worldpay']) && !wpinv_is_worldpay_valid_for_use()) { |
|
151 | + unset($gateway_list['worldpay']); |
|
152 | 152 | } |
153 | 153 | return $gateway_list; |
154 | 154 | } |
155 | -add_filter( 'wpinv_enabled_payment_gateways', 'wpinv_check_worldpay_currency_support', 10, 1 ); |
|
156 | 155 | \ No newline at end of file |
156 | +add_filter('wpinv_enabled_payment_gateways', 'wpinv_check_worldpay_currency_support', 10, 1); |
|
157 | 157 | \ No newline at end of file |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | const EXCEPTION_CURL = 10; |
41 | 41 | |
42 | 42 | private $ch; |
43 | - private $login; |
|
43 | + private $login; |
|
44 | 44 | private $response; |
45 | 45 | private $response_xml; |
46 | 46 | private $results; |
@@ -48,9 +48,9 @@ discard block |
||
48 | 48 | private $url; |
49 | 49 | private $xml; |
50 | 50 | |
51 | - public function __construct($login, $transkey, $test = self::USE_PRODUCTION_SERVER) |
|
52 | - { |
|
53 | - $login = trim($login); |
|
51 | + public function __construct($login, $transkey, $test = self::USE_PRODUCTION_SERVER) |
|
52 | + { |
|
53 | + $login = trim($login); |
|
54 | 54 | $transkey = trim($transkey); |
55 | 55 | if (empty($login) || empty($transkey)) |
56 | 56 | { |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | |
64 | 64 | $subdomain = ($test) ? 'apitest' : 'api'; |
65 | 65 | $this->url = 'https://' . $subdomain . '.authorize.net/xml/v1/request.api'; |
66 | - } |
|
66 | + } |
|
67 | 67 | |
68 | 68 | /** |
69 | 69 | * remove XML response namespaces |
@@ -78,9 +78,9 @@ discard block |
||
78 | 78 | return str_replace(' xmlns:xsd="http://www.w3.org/2001/XMLSchema"','',$input); |
79 | 79 | } |
80 | 80 | |
81 | - public function __toString() |
|
82 | - { |
|
83 | - $output = ''; |
|
81 | + public function __toString() |
|
82 | + { |
|
83 | + $output = ''; |
|
84 | 84 | $output .= '<table summary="Authorize.Net Results" id="authnet">' . "\n"; |
85 | 85 | $output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Class Parameters</b></th>' . "\n" . '</tr>' . "\n"; |
86 | 86 | $output .= '<tr>' . "\n\t\t" . '<td><b>API Login ID</b></td><td>' . $this->login . '</td>' . "\n" . '</tr>' . "\n"; |
@@ -117,9 +117,9 @@ discard block |
||
117 | 117 | $output .= '</table>'; |
118 | 118 | |
119 | 119 | return $output; |
120 | - } |
|
120 | + } |
|
121 | 121 | |
122 | - public function __destruct() |
|
122 | + public function __destruct() |
|
123 | 123 | { |
124 | 124 | if (isset($this->ch)) |
125 | 125 | { |
@@ -128,31 +128,31 @@ discard block |
||
128 | 128 | } |
129 | 129 | |
130 | 130 | public function __get($var) |
131 | - { |
|
132 | - return $this->response_xml->$var; |
|
133 | - } |
|
134 | - |
|
135 | - public function __set($key, $value) |
|
136 | - { |
|
137 | - trigger_error('You cannot set parameters directly in ' . __CLASS__ . '.', E_USER_WARNING); |
|
138 | - return false; |
|
139 | - } |
|
140 | - |
|
141 | - public function __call($api_call, $args) |
|
142 | - { |
|
143 | - $this->xml = new SimpleXMLElement('<' . $api_call . '></' . $api_call . '>'); |
|
131 | + { |
|
132 | + return $this->response_xml->$var; |
|
133 | + } |
|
134 | + |
|
135 | + public function __set($key, $value) |
|
136 | + { |
|
137 | + trigger_error('You cannot set parameters directly in ' . __CLASS__ . '.', E_USER_WARNING); |
|
138 | + return false; |
|
139 | + } |
|
140 | + |
|
141 | + public function __call($api_call, $args) |
|
142 | + { |
|
143 | + $this->xml = new SimpleXMLElement('<' . $api_call . '></' . $api_call . '>'); |
|
144 | 144 | $this->xml->addAttribute('xmlns', 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'); |
145 | - $merch_auth = $this->xml->addChild('merchantAuthentication'); |
|
145 | + $merch_auth = $this->xml->addChild('merchantAuthentication'); |
|
146 | 146 | $merch_auth->addChild('name', $this->login); |
147 | - $merch_auth->addChild('transactionKey', $this->transkey); |
|
147 | + $merch_auth->addChild('transactionKey', $this->transkey); |
|
148 | 148 | |
149 | - $this->setParameters($this->xml, $args[0]); |
|
150 | - $this->process(); |
|
151 | - } |
|
149 | + $this->setParameters($this->xml, $args[0]); |
|
150 | + $this->process(); |
|
151 | + } |
|
152 | 152 | |
153 | - private function setParameters($xml, $array) |
|
154 | - { |
|
155 | - if (is_array($array)) |
|
153 | + private function setParameters($xml, $array) |
|
154 | + { |
|
155 | + if (is_array($array)) |
|
156 | 156 | { |
157 | 157 | $first = true; |
158 | 158 | foreach ($array as $key => $value) |
@@ -184,34 +184,34 @@ discard block |
||
184 | 184 | } |
185 | 185 | } |
186 | 186 | } |
187 | - } |
|
187 | + } |
|
188 | 188 | |
189 | - private function process() |
|
190 | - { |
|
191 | - $this->xml = $this->xml->asXML(); |
|
189 | + private function process() |
|
190 | + { |
|
191 | + $this->xml = $this->xml->asXML(); |
|
192 | 192 | |
193 | - $this->ch = curl_init(); |
|
193 | + $this->ch = curl_init(); |
|
194 | 194 | curl_setopt($this->ch, CURLOPT_URL, $this->url); |
195 | - curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); |
|
196 | - curl_setopt($this->ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); |
|
197 | - curl_setopt($this->ch, CURLOPT_HEADER, 0); |
|
198 | - curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->xml); |
|
199 | - curl_setopt($this->ch, CURLOPT_POST, 1); |
|
200 | - curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 2); |
|
201 | - curl_setopt($this->ch, CURLOPT_CAINFO, dirname(__FILE__) . '/ssl/cert.pem'); |
|
195 | + curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); |
|
196 | + curl_setopt($this->ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); |
|
197 | + curl_setopt($this->ch, CURLOPT_HEADER, 0); |
|
198 | + curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->xml); |
|
199 | + curl_setopt($this->ch, CURLOPT_POST, 1); |
|
200 | + curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 2); |
|
201 | + curl_setopt($this->ch, CURLOPT_CAINFO, dirname(__FILE__) . '/ssl/cert.pem'); |
|
202 | 202 | |
203 | 203 | if(($this->response = curl_exec($this->ch)) !== false) |
204 | 204 | { |
205 | 205 | $this->response_xml = @new SimpleXMLElement($this->response); |
206 | 206 | |
207 | - curl_close($this->ch); |
|
207 | + curl_close($this->ch); |
|
208 | 208 | unset($this->ch); |
209 | 209 | return; |
210 | - } |
|
210 | + } |
|
211 | 211 | throw new AuthnetXMLException('Connection error: ' . curl_error($this->ch) . ' (' . curl_errno($this->ch) . ')', self::EXCEPTION_CURL); |
212 | - } |
|
212 | + } |
|
213 | 213 | |
214 | - public function isSuccessful() |
|
214 | + public function isSuccessful() |
|
215 | 215 | { |
216 | 216 | return $this->response_xml->messages->resultCode == 'Ok'; |
217 | 217 | } |
@@ -165,20 +165,17 @@ |
||
165 | 165 | { |
166 | 166 | $xmlx = $xml; |
167 | 167 | $first = false; |
168 | - } |
|
169 | - else |
|
168 | + } else |
|
170 | 169 | { |
171 | 170 | $parent = $xml->xpath('parent::*'); |
172 | 171 | $xmlx = $parent[0]->addChild($xml->getName()); |
173 | 172 | } |
174 | - } |
|
175 | - else |
|
173 | + } else |
|
176 | 174 | { |
177 | 175 | $xmlx = $xml->addChild($key); |
178 | 176 | } |
179 | 177 | $this->setParameters($xmlx, $value); |
180 | - } |
|
181 | - else |
|
178 | + } else |
|
182 | 179 | { |
183 | 180 | $xml->$key = $value; |
184 | 181 | } |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | |
51 | 51 | public function __construct($login, $transkey, $test = self::USE_PRODUCTION_SERVER) |
52 | 52 | { |
53 | - $login = trim($login); |
|
53 | + $login = trim($login); |
|
54 | 54 | $transkey = trim($transkey); |
55 | 55 | if (empty($login) || empty($transkey)) |
56 | 56 | { |
@@ -73,14 +73,14 @@ discard block |
||
73 | 73 | private function removeResponseXMLNS($input) |
74 | 74 | { |
75 | 75 | // why remove them one at a time? all three aren't consistantly used in the response |
76 | - $input = str_replace(' xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"','',$input); |
|
77 | - $input = str_replace(' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"','',$input); |
|
78 | - return str_replace(' xmlns:xsd="http://www.w3.org/2001/XMLSchema"','',$input); |
|
76 | + $input = str_replace(' xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"', '', $input); |
|
77 | + $input = str_replace(' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"', '', $input); |
|
78 | + return str_replace(' xmlns:xsd="http://www.w3.org/2001/XMLSchema"', '', $input); |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | public function __toString() |
82 | 82 | { |
83 | - $output = ''; |
|
83 | + $output = ''; |
|
84 | 84 | $output .= '<table summary="Authorize.Net Results" id="authnet">' . "\n"; |
85 | 85 | $output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Class Parameters</b></th>' . "\n" . '</tr>' . "\n"; |
86 | 86 | $output .= '<tr>' . "\n\t\t" . '<td><b>API Login ID</b></td><td>' . $this->login . '</td>' . "\n" . '</tr>' . "\n"; |
@@ -159,9 +159,9 @@ discard block |
||
159 | 159 | { |
160 | 160 | if (is_array($value)) |
161 | 161 | { |
162 | - if(is_numeric($key)) |
|
162 | + if (is_numeric($key)) |
|
163 | 163 | { |
164 | - if($first === true) |
|
164 | + if ($first === true) |
|
165 | 165 | { |
166 | 166 | $xmlx = $xml; |
167 | 167 | $first = false; |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 2); |
201 | 201 | curl_setopt($this->ch, CURLOPT_CAINFO, dirname(__FILE__) . '/ssl/cert.pem'); |
202 | 202 | |
203 | - if(($this->response = curl_exec($this->ch)) !== false) |
|
203 | + if (($this->response = curl_exec($this->ch)) !== false) |
|
204 | 204 | { |
205 | 205 | $this->response_xml = @new SimpleXMLElement($this->response); |
206 | 206 |
@@ -96,7 +96,7 @@ |
||
96 | 96 | curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, false); |
97 | 97 | } |
98 | 98 | |
99 | - if (preg_match('/xml/',$post_url)) { |
|
99 | + if (preg_match('/xml/', $post_url)) { |
|
100 | 100 | curl_setopt($curl_request, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); |
101 | 101 | } |
102 | 102 |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | $this->xml = @simplexml_load_string($response); |
30 | 30 | |
31 | 31 | // Remove namespaces for use with XPath. |
32 | - $this->xpath_xml = @simplexml_load_string(preg_replace('/ xmlns:xsi[^>]+/','',$response)); |
|
32 | + $this->xpath_xml = @simplexml_load_string(preg_replace('/ xmlns:xsi[^>]+/', '', $response)); |
|
33 | 33 | } |
34 | 34 | } |
35 | 35 | |
@@ -116,12 +116,12 @@ discard block |
||
116 | 116 | { |
117 | 117 | $start = "<$elementName>"; |
118 | 118 | $end = "</$elementName>"; |
119 | - if (strpos($this->response,$start) === false || strpos($this->response,$end) === false) { |
|
119 | + if (strpos($this->response, $start) === false || strpos($this->response, $end) === false) { |
|
120 | 120 | return false; |
121 | 121 | } else { |
122 | - $start_position = strpos($this->response, $start)+strlen($start); |
|
122 | + $start_position = strpos($this->response, $start) + strlen($start); |
|
123 | 123 | $end_position = strpos($this->response, $end); |
124 | - return substr($this->response, $start_position, $end_position-$start_position); |
|
124 | + return substr($this->response, $start_position, $end_position - $start_position); |
|
125 | 125 | } |
126 | 126 | } |
127 | 127 |