VariableProductTitle
4 years ago
.gitkeep
4 years ago
WpaeInvalidPhpException.php
4 years ago
WpaeInvalidStringException.php
4 years ago
WpaeMethodNotFoundException.php
4 years ago
WpaePhpInterpreterErrorHandler.php
4 years ago
WpaeString.php
4 years ago
WpaeTooMuchRecursionException.php
4 years ago
WpaeXmlProcessor.php
4 years ago
XmlCsvExport.php
4 years ago
XmlExportACF.php
4 years ago
XmlExportComment.php
4 years ago
XmlExportCpt.php
4 years ago
XmlExportEngine.php
4 years ago
XmlExportFiltering.php
4 years ago
XmlExportMediaGallery.php
4 years ago
XmlExportTaxonomy.php
4 years ago
XmlExportUser.php
4 years ago
XmlExportWooCommerce.php
4 years ago
XmlExportWooCommerceCoupon.php
4 years ago
XmlExportWooCommerceOrder.php
4 years ago
XmlGoogleMerchants.php
4 years ago
XmlSpec.php
4 years ago
WpaeString.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | class WpaeString |
| 5 | { |
| 6 | const MIDDLE_COMMA = "*middlecomma*"; |
| 7 | |
| 8 | public function isBetween($haystack, $search, $start, $end) |
| 9 | { |
| 10 | if($haystack == $search) { |
| 11 | return false; |
| 12 | } |
| 13 | if (strpos($haystack, $start) === false) { |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | $haystack = str_replace($search, "#SNIPPET#", $haystack); |
| 18 | $search = "#SNIPPET#"; |
| 19 | |
| 20 | $searchPosition = strpos($haystack, $search); |
| 21 | |
| 22 | $firstString = substr($haystack, 0, $searchPosition); |
| 23 | $lastString = substr($haystack, $searchPosition + strlen($search), strlen($haystack)); |
| 24 | |
| 25 | $isInFirstString = false; |
| 26 | $isInLastString = false; |
| 27 | |
| 28 | // Make sure the number of strings in the part before and after is not equal |
| 29 | // to exclude cases like [a]b[c] we want only cases like a[b]c |
| 30 | $numberOfStartInFirstString = substr_count($firstString, $start); |
| 31 | $numberOfEndInFirstString = substr_count($firstString, $end); |
| 32 | |
| 33 | $numberOfStartInLastString = substr_count($lastString, $start); |
| 34 | $numberOfEndInLastString = substr_count($lastString, $end); |
| 35 | |
| 36 | if(strpos($firstString, $start) !== false && $numberOfStartInFirstString - $numberOfEndInFirstString) { |
| 37 | $isInFirstString = true; |
| 38 | } |
| 39 | |
| 40 | if(strpos($lastString, $end) !== false && $numberOfStartInLastString - $numberOfEndInLastString) { |
| 41 | $isInLastString = true; |
| 42 | } |
| 43 | |
| 44 | return $isInFirstString && $isInLastString; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param $sanitizedSnippet |
| 49 | * @return mixed |
| 50 | */ |
| 51 | public function quoteParams($sanitizedSnippet) |
| 52 | { |
| 53 | if(strpos($sanitizedSnippet, 'array') !== false) { |
| 54 | return $sanitizedSnippet; |
| 55 | } |
| 56 | |
| 57 | if(strpos($sanitizedSnippet, WpaeXmlProcessor::SNIPPET_DELIMITER) === false && strpos($sanitizedSnippet, '"') === false && strpos($sanitizedSnippet, "'") === false ) { |
| 58 | $sanitizedSnippet = str_replace("(","(\"",$sanitizedSnippet); |
| 59 | $sanitizedSnippet = str_replace(")","\")",$sanitizedSnippet); |
| 60 | |
| 61 | return $sanitizedSnippet; |
| 62 | } |
| 63 | |
| 64 | $sanitizedSnippet = str_replace(WpaeXmlProcessor::SNIPPET_DELIMITER, '"', $sanitizedSnippet); |
| 65 | |
| 66 | $sanitizedString = ""; |
| 67 | |
| 68 | $isInString = false; |
| 69 | |
| 70 | for($i=0; $i< strlen($sanitizedSnippet); $i++) { |
| 71 | if($sanitizedSnippet[$i] == "\"") { |
| 72 | if($isInString) { |
| 73 | $isInString = false; |
| 74 | } else { |
| 75 | $isInString = true; |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | |
| 80 | if($sanitizedSnippet[$i] === "," && $isInString) { |
| 81 | $sanitizedString.= self::MIDDLE_COMMA; |
| 82 | } else { |
| 83 | $sanitizedString .= $sanitizedSnippet[$i]; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $sanitizedSnippet = $sanitizedString; |
| 88 | |
| 89 | if(strpos($sanitizedSnippet,"(") !== false && strpos($sanitizedSnippet, ")") !== false) { |
| 90 | |
| 91 | $sanitizedSnippet = str_replace("()", '("")', $sanitizedSnippet); |
| 92 | $parts = explode("(", $sanitizedSnippet); |
| 93 | if (!isset($parts[1])) { |
| 94 | //TODO: Can this happen? |
| 95 | } |
| 96 | $params = $parts[1]; |
| 97 | $parameterPart = $parts[1]; |
| 98 | $originalParameterPart = $parts[1]; |
| 99 | |
| 100 | $params = explode(")", $params); |
| 101 | $params = $params[0]; |
| 102 | |
| 103 | if (strpos($params, ",") !== false) { |
| 104 | $params = explode(",", $params); |
| 105 | } else { |
| 106 | $params = array($params); |
| 107 | } |
| 108 | |
| 109 | foreach ($params as $param) { |
| 110 | if (!preg_match('/".*"/', $param)) { |
| 111 | $parameterPart = str_replace(','.$param, ',"' .trim($param) . '"', $parameterPart); |
| 112 | $parameterPart = str_replace('('.$param, '("' .trim($param) . '"', $parameterPart); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | $sanitizedSnippet = str_replace($originalParameterPart, $parameterPart, $sanitizedSnippet); |
| 117 | } |
| 118 | |
| 119 | $sanitizedSnippet = str_replace(self::MIDDLE_COMMA, ',', $sanitizedSnippet); |
| 120 | |
| 121 | return $sanitizedSnippet; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @param $sanitizedSnippet |
| 126 | * @return string |
| 127 | */ |
| 128 | private function quoteStringWithTokenizer($sanitizedSnippet) |
| 129 | { |
| 130 | $sanitizedSnippet = explode("(", $sanitizedSnippet); |
| 131 | $functionName = $sanitizedSnippet[0]; |
| 132 | $sanitizedSnippet = $sanitizedSnippet[1]; |
| 133 | $sanitizedSnippet = str_replace(')', '', $sanitizedSnippet); |
| 134 | |
| 135 | $tokens = token_get_all('<?php ' . $sanitizedSnippet . ' ?>'); |
| 136 | |
| 137 | $sanitizedString = ""; |
| 138 | |
| 139 | foreach ($tokens as $token) { |
| 140 | if ($token[0] == 319) { |
| 141 | $sanitizedString .= '"' . $token[1] . '",'; |
| 142 | } |
| 143 | if ($token[0] == 323) { |
| 144 | $sanitizedString .= $token[1] . ','; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | $sanitizedString = substr($sanitizedString, 0, -1); |
| 149 | $sanitizedString = $functionName . '(' . $sanitizedSnippet . ')'; |
| 150 | |
| 151 | return $sanitizedString; |
| 152 | } |
| 153 | } |