@@ -22,249 +22,249 @@ |
||
| 22 | 22 | abstract class BlockAssetManager extends AssetManager implements BlockAssetManagerInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var string $editor_script_handle |
|
| 27 | - */ |
|
| 28 | - private $editor_script_handle; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var string $editor_style_handle |
|
| 32 | - */ |
|
| 33 | - private $editor_style_handle; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var string $script_handle |
|
| 37 | - */ |
|
| 38 | - private $script_handle; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var string $style_handle |
|
| 42 | - */ |
|
| 43 | - private $style_handle; |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - public function getEditorScriptHandle() |
|
| 50 | - { |
|
| 51 | - return $this->editor_script_handle; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param string $editor_script_handle |
|
| 57 | - */ |
|
| 58 | - public function setEditorScriptHandle($editor_script_handle) |
|
| 59 | - { |
|
| 60 | - if(strpos($editor_script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 61 | - $editor_script_handle = BlockInterface::NAME_SPACE . '-' . $editor_script_handle; |
|
| 62 | - } |
|
| 63 | - $this->editor_script_handle = $editor_script_handle; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return string |
|
| 69 | - */ |
|
| 70 | - public function getEditorStyleHandle() |
|
| 71 | - { |
|
| 72 | - return $this->editor_style_handle; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param string $editor_style_handle |
|
| 78 | - */ |
|
| 79 | - public function setEditorStyleHandle($editor_style_handle) |
|
| 80 | - { |
|
| 81 | - if (strpos($editor_style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 82 | - $editor_style_handle = BlockInterface::NAME_SPACE . '-' . $editor_style_handle; |
|
| 83 | - } |
|
| 84 | - $this->editor_style_handle = $editor_style_handle; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function getScriptHandle() |
|
| 92 | - { |
|
| 93 | - return $this->script_handle; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @param string $script_handle |
|
| 99 | - */ |
|
| 100 | - public function setScriptHandle($script_handle) |
|
| 101 | - { |
|
| 102 | - if (strpos($script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 103 | - $script_handle = BlockInterface::NAME_SPACE . '-' . $script_handle; |
|
| 104 | - } |
|
| 105 | - $this->script_handle = $script_handle; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return string |
|
| 111 | - */ |
|
| 112 | - public function getStyleHandle() |
|
| 113 | - { |
|
| 114 | - return $this->style_handle; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param string $style_handle |
|
| 120 | - */ |
|
| 121 | - public function setStyleHandle($style_handle) |
|
| 122 | - { |
|
| 123 | - if (strpos($style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 124 | - $style_handle = BlockInterface::NAME_SPACE . '-' . $style_handle; |
|
| 125 | - } |
|
| 126 | - $this->style_handle = $style_handle; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @since 4.9.71.p |
|
| 131 | - * @throws InvalidDataTypeException |
|
| 132 | - * @throws InvalidEntityException |
|
| 133 | - * @throws DuplicateCollectionIdentifierException |
|
| 134 | - */ |
|
| 135 | - public function addAssets() |
|
| 136 | - { |
|
| 137 | - $this->addEditorScript($this->getEditorScriptHandle()); |
|
| 138 | - $this->addEditorStyle($this->getEditorStyleHandle()); |
|
| 139 | - $this->addScript($this->getScriptHandle()); |
|
| 140 | - $this->addStyle($this->getStyleHandle()); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param $handle |
|
| 146 | - * @param array $dependencies |
|
| 147 | - * @since 4.9.71.p |
|
| 148 | - * @return JavascriptAsset |
|
| 149 | - * @throws InvalidDataTypeException |
|
| 150 | - * @throws InvalidEntityException |
|
| 151 | - * @throws DuplicateCollectionIdentifierException |
|
| 152 | - */ |
|
| 153 | - public function addEditorScript($handle, array $dependencies = array()) |
|
| 154 | - { |
|
| 155 | - if ($this->assets->hasJavascriptAsset($handle)){ |
|
| 156 | - return $this->assets->getJavascriptAsset($handle); |
|
| 157 | - } |
|
| 158 | - return $this->addJs($handle, $dependencies)->setRequiresTranslation(); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @param $handle |
|
| 164 | - * @param array $dependencies |
|
| 165 | - * @since 4.9.71.p |
|
| 166 | - * @return StylesheetAsset |
|
| 167 | - * @throws InvalidDataTypeException |
|
| 168 | - * @throws InvalidEntityException |
|
| 169 | - * @throws DuplicateCollectionIdentifierException |
|
| 170 | - */ |
|
| 171 | - public function addEditorStyle($handle, array $dependencies = array()) |
|
| 172 | - { |
|
| 173 | - if ($this->assets->hasStylesheetAsset($handle)) { |
|
| 174 | - return $this->assets->getStylesheetAsset($handle); |
|
| 175 | - } |
|
| 176 | - return $this->addCss($handle, $dependencies); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @param $handle |
|
| 182 | - * @param array $dependencies |
|
| 183 | - * @since 4.9.71.p |
|
| 184 | - * @return JavascriptAsset |
|
| 185 | - * @throws InvalidDataTypeException |
|
| 186 | - * @throws InvalidEntityException |
|
| 187 | - * @throws DuplicateCollectionIdentifierException |
|
| 188 | - */ |
|
| 189 | - public function addScript($handle, array $dependencies = array()) |
|
| 190 | - { |
|
| 191 | - if ($this->assets->hasJavascriptAsset($handle)) { |
|
| 192 | - return $this->assets->getJavascriptAsset($handle); |
|
| 193 | - } |
|
| 194 | - return $this->addJs($handle, $dependencies)->setRequiresTranslation(); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * @param $handle |
|
| 200 | - * @param array $dependencies |
|
| 201 | - * @since 4.9.71.p |
|
| 202 | - * @return StylesheetAsset |
|
| 203 | - * @throws InvalidDataTypeException |
|
| 204 | - * @throws InvalidEntityException |
|
| 205 | - * @throws DuplicateCollectionIdentifierException |
|
| 206 | - */ |
|
| 207 | - public function addStyle($handle, array $dependencies = array()) |
|
| 208 | - { |
|
| 209 | - if ($this->assets->hasStylesheetAsset($handle)) { |
|
| 210 | - return $this->assets->getStylesheetAsset($handle); |
|
| 211 | - } |
|
| 212 | - return $this->addCss($handle, $dependencies); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @return JavascriptAsset|null |
|
| 218 | - */ |
|
| 219 | - public function getEditorScript() |
|
| 220 | - { |
|
| 221 | - return $this->assets->getJavascriptAsset($this->editor_script_handle); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @return StylesheetAsset|null |
|
| 227 | - */ |
|
| 228 | - public function getEditorStyle() |
|
| 229 | - { |
|
| 230 | - return $this->assets->getStylesheetAsset($this->editor_style_handle); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * @return JavascriptAsset|null |
|
| 236 | - */ |
|
| 237 | - public function getScript() |
|
| 238 | - { |
|
| 239 | - return $this->assets->getJavascriptAsset($this->script_handle); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * @return StylesheetAsset|null |
|
| 245 | - */ |
|
| 246 | - public function getStyle() |
|
| 247 | - { |
|
| 248 | - return $this->assets->getStylesheetAsset($this->style_handle); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * @return void |
|
| 254 | - */ |
|
| 255 | - public function enqueueAssets() |
|
| 256 | - { |
|
| 257 | - $assets = array( |
|
| 258 | - $this->getEditorScript(), |
|
| 259 | - $this->getEditorStyle(), |
|
| 260 | - $this->getScript(), |
|
| 261 | - $this->getStyle(), |
|
| 262 | - ); |
|
| 263 | - foreach ($assets as $asset) { |
|
| 264 | - if ($asset instanceof BrowserAsset && $asset->isRegistered()) { |
|
| 265 | - $asset->enqueueAsset(); |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - } |
|
| 25 | + /** |
|
| 26 | + * @var string $editor_script_handle |
|
| 27 | + */ |
|
| 28 | + private $editor_script_handle; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var string $editor_style_handle |
|
| 32 | + */ |
|
| 33 | + private $editor_style_handle; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var string $script_handle |
|
| 37 | + */ |
|
| 38 | + private $script_handle; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var string $style_handle |
|
| 42 | + */ |
|
| 43 | + private $style_handle; |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + public function getEditorScriptHandle() |
|
| 50 | + { |
|
| 51 | + return $this->editor_script_handle; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param string $editor_script_handle |
|
| 57 | + */ |
|
| 58 | + public function setEditorScriptHandle($editor_script_handle) |
|
| 59 | + { |
|
| 60 | + if(strpos($editor_script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 61 | + $editor_script_handle = BlockInterface::NAME_SPACE . '-' . $editor_script_handle; |
|
| 62 | + } |
|
| 63 | + $this->editor_script_handle = $editor_script_handle; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return string |
|
| 69 | + */ |
|
| 70 | + public function getEditorStyleHandle() |
|
| 71 | + { |
|
| 72 | + return $this->editor_style_handle; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param string $editor_style_handle |
|
| 78 | + */ |
|
| 79 | + public function setEditorStyleHandle($editor_style_handle) |
|
| 80 | + { |
|
| 81 | + if (strpos($editor_style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 82 | + $editor_style_handle = BlockInterface::NAME_SPACE . '-' . $editor_style_handle; |
|
| 83 | + } |
|
| 84 | + $this->editor_style_handle = $editor_style_handle; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function getScriptHandle() |
|
| 92 | + { |
|
| 93 | + return $this->script_handle; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @param string $script_handle |
|
| 99 | + */ |
|
| 100 | + public function setScriptHandle($script_handle) |
|
| 101 | + { |
|
| 102 | + if (strpos($script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 103 | + $script_handle = BlockInterface::NAME_SPACE . '-' . $script_handle; |
|
| 104 | + } |
|
| 105 | + $this->script_handle = $script_handle; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return string |
|
| 111 | + */ |
|
| 112 | + public function getStyleHandle() |
|
| 113 | + { |
|
| 114 | + return $this->style_handle; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param string $style_handle |
|
| 120 | + */ |
|
| 121 | + public function setStyleHandle($style_handle) |
|
| 122 | + { |
|
| 123 | + if (strpos($style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 124 | + $style_handle = BlockInterface::NAME_SPACE . '-' . $style_handle; |
|
| 125 | + } |
|
| 126 | + $this->style_handle = $style_handle; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @since 4.9.71.p |
|
| 131 | + * @throws InvalidDataTypeException |
|
| 132 | + * @throws InvalidEntityException |
|
| 133 | + * @throws DuplicateCollectionIdentifierException |
|
| 134 | + */ |
|
| 135 | + public function addAssets() |
|
| 136 | + { |
|
| 137 | + $this->addEditorScript($this->getEditorScriptHandle()); |
|
| 138 | + $this->addEditorStyle($this->getEditorStyleHandle()); |
|
| 139 | + $this->addScript($this->getScriptHandle()); |
|
| 140 | + $this->addStyle($this->getStyleHandle()); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param $handle |
|
| 146 | + * @param array $dependencies |
|
| 147 | + * @since 4.9.71.p |
|
| 148 | + * @return JavascriptAsset |
|
| 149 | + * @throws InvalidDataTypeException |
|
| 150 | + * @throws InvalidEntityException |
|
| 151 | + * @throws DuplicateCollectionIdentifierException |
|
| 152 | + */ |
|
| 153 | + public function addEditorScript($handle, array $dependencies = array()) |
|
| 154 | + { |
|
| 155 | + if ($this->assets->hasJavascriptAsset($handle)){ |
|
| 156 | + return $this->assets->getJavascriptAsset($handle); |
|
| 157 | + } |
|
| 158 | + return $this->addJs($handle, $dependencies)->setRequiresTranslation(); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @param $handle |
|
| 164 | + * @param array $dependencies |
|
| 165 | + * @since 4.9.71.p |
|
| 166 | + * @return StylesheetAsset |
|
| 167 | + * @throws InvalidDataTypeException |
|
| 168 | + * @throws InvalidEntityException |
|
| 169 | + * @throws DuplicateCollectionIdentifierException |
|
| 170 | + */ |
|
| 171 | + public function addEditorStyle($handle, array $dependencies = array()) |
|
| 172 | + { |
|
| 173 | + if ($this->assets->hasStylesheetAsset($handle)) { |
|
| 174 | + return $this->assets->getStylesheetAsset($handle); |
|
| 175 | + } |
|
| 176 | + return $this->addCss($handle, $dependencies); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @param $handle |
|
| 182 | + * @param array $dependencies |
|
| 183 | + * @since 4.9.71.p |
|
| 184 | + * @return JavascriptAsset |
|
| 185 | + * @throws InvalidDataTypeException |
|
| 186 | + * @throws InvalidEntityException |
|
| 187 | + * @throws DuplicateCollectionIdentifierException |
|
| 188 | + */ |
|
| 189 | + public function addScript($handle, array $dependencies = array()) |
|
| 190 | + { |
|
| 191 | + if ($this->assets->hasJavascriptAsset($handle)) { |
|
| 192 | + return $this->assets->getJavascriptAsset($handle); |
|
| 193 | + } |
|
| 194 | + return $this->addJs($handle, $dependencies)->setRequiresTranslation(); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * @param $handle |
|
| 200 | + * @param array $dependencies |
|
| 201 | + * @since 4.9.71.p |
|
| 202 | + * @return StylesheetAsset |
|
| 203 | + * @throws InvalidDataTypeException |
|
| 204 | + * @throws InvalidEntityException |
|
| 205 | + * @throws DuplicateCollectionIdentifierException |
|
| 206 | + */ |
|
| 207 | + public function addStyle($handle, array $dependencies = array()) |
|
| 208 | + { |
|
| 209 | + if ($this->assets->hasStylesheetAsset($handle)) { |
|
| 210 | + return $this->assets->getStylesheetAsset($handle); |
|
| 211 | + } |
|
| 212 | + return $this->addCss($handle, $dependencies); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @return JavascriptAsset|null |
|
| 218 | + */ |
|
| 219 | + public function getEditorScript() |
|
| 220 | + { |
|
| 221 | + return $this->assets->getJavascriptAsset($this->editor_script_handle); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @return StylesheetAsset|null |
|
| 227 | + */ |
|
| 228 | + public function getEditorStyle() |
|
| 229 | + { |
|
| 230 | + return $this->assets->getStylesheetAsset($this->editor_style_handle); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * @return JavascriptAsset|null |
|
| 236 | + */ |
|
| 237 | + public function getScript() |
|
| 238 | + { |
|
| 239 | + return $this->assets->getJavascriptAsset($this->script_handle); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * @return StylesheetAsset|null |
|
| 245 | + */ |
|
| 246 | + public function getStyle() |
|
| 247 | + { |
|
| 248 | + return $this->assets->getStylesheetAsset($this->style_handle); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * @return void |
|
| 254 | + */ |
|
| 255 | + public function enqueueAssets() |
|
| 256 | + { |
|
| 257 | + $assets = array( |
|
| 258 | + $this->getEditorScript(), |
|
| 259 | + $this->getEditorStyle(), |
|
| 260 | + $this->getScript(), |
|
| 261 | + $this->getStyle(), |
|
| 262 | + ); |
|
| 263 | + foreach ($assets as $asset) { |
|
| 264 | + if ($asset instanceof BrowserAsset && $asset->isRegistered()) { |
|
| 265 | + $asset->enqueueAsset(); |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | 270 | } |
@@ -57,8 +57,8 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public function setEditorScriptHandle($editor_script_handle) |
| 59 | 59 | { |
| 60 | - if(strpos($editor_script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 61 | - $editor_script_handle = BlockInterface::NAME_SPACE . '-' . $editor_script_handle; |
|
| 60 | + if (strpos($editor_script_handle, BlockInterface::NAME_SPACE.'-') !== 0) { |
|
| 61 | + $editor_script_handle = BlockInterface::NAME_SPACE.'-'.$editor_script_handle; |
|
| 62 | 62 | } |
| 63 | 63 | $this->editor_script_handle = $editor_script_handle; |
| 64 | 64 | } |
@@ -78,8 +78,8 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | public function setEditorStyleHandle($editor_style_handle) |
| 80 | 80 | { |
| 81 | - if (strpos($editor_style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 82 | - $editor_style_handle = BlockInterface::NAME_SPACE . '-' . $editor_style_handle; |
|
| 81 | + if (strpos($editor_style_handle, BlockInterface::NAME_SPACE.'-') !== 0) { |
|
| 82 | + $editor_style_handle = BlockInterface::NAME_SPACE.'-'.$editor_style_handle; |
|
| 83 | 83 | } |
| 84 | 84 | $this->editor_style_handle = $editor_style_handle; |
| 85 | 85 | } |
@@ -99,8 +99,8 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | public function setScriptHandle($script_handle) |
| 101 | 101 | { |
| 102 | - if (strpos($script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 103 | - $script_handle = BlockInterface::NAME_SPACE . '-' . $script_handle; |
|
| 102 | + if (strpos($script_handle, BlockInterface::NAME_SPACE.'-') !== 0) { |
|
| 103 | + $script_handle = BlockInterface::NAME_SPACE.'-'.$script_handle; |
|
| 104 | 104 | } |
| 105 | 105 | $this->script_handle = $script_handle; |
| 106 | 106 | } |
@@ -120,8 +120,8 @@ discard block |
||
| 120 | 120 | */ |
| 121 | 121 | public function setStyleHandle($style_handle) |
| 122 | 122 | { |
| 123 | - if (strpos($style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 124 | - $style_handle = BlockInterface::NAME_SPACE . '-' . $style_handle; |
|
| 123 | + if (strpos($style_handle, BlockInterface::NAME_SPACE.'-') !== 0) { |
|
| 124 | + $style_handle = BlockInterface::NAME_SPACE.'-'.$style_handle; |
|
| 125 | 125 | } |
| 126 | 126 | $this->style_handle = $style_handle; |
| 127 | 127 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | public function addEditorScript($handle, array $dependencies = array()) |
| 154 | 154 | { |
| 155 | - if ($this->assets->hasJavascriptAsset($handle)){ |
|
| 155 | + if ($this->assets->hasJavascriptAsset($handle)) { |
|
| 156 | 156 | return $this->assets->getJavascriptAsset($handle); |
| 157 | 157 | } |
| 158 | 158 | return $this->addJs($handle, $dependencies)->setRequiresTranslation(); |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | $script->version(), |
| 185 | 185 | $script->loadInFooter() |
| 186 | 186 | ); |
| 187 | - if (! $registered && $this->debug()) { |
|
| 187 | + if ( ! $registered && $this->debug()) { |
|
| 188 | 188 | throw new AssetRegistrationException($script->handle()); |
| 189 | 189 | } |
| 190 | 190 | $script->setRegistered($registered); |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | $this->removeAlreadyRegisteredDataForScriptHandles(); |
| 245 | 245 | wp_add_inline_script( |
| 246 | 246 | 'eejs-core', |
| 247 | - 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 247 | + 'var eejsdata='.wp_json_encode(array('data' => $this->jsdata)), |
|
| 248 | 248 | 'before' |
| 249 | 249 | ); |
| 250 | 250 | $scripts = $this->assets->getJavascriptAssetsWithData(); |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | public function addData($key, $value) |
| 275 | 275 | { |
| 276 | 276 | if ($this->verifyDataNotExisting($key)) { |
| 277 | - $this->jsdata[ $key ] = $value; |
|
| 277 | + $this->jsdata[$key] = $value; |
|
| 278 | 278 | } |
| 279 | 279 | } |
| 280 | 280 | |
@@ -299,10 +299,10 @@ discard block |
||
| 299 | 299 | */ |
| 300 | 300 | public function pushData($key, $value) |
| 301 | 301 | { |
| 302 | - if (isset($this->jsdata[ $key ]) |
|
| 303 | - && ! is_array($this->jsdata[ $key ]) |
|
| 302 | + if (isset($this->jsdata[$key]) |
|
| 303 | + && ! is_array($this->jsdata[$key]) |
|
| 304 | 304 | ) { |
| 305 | - if (! $this->debug()) { |
|
| 305 | + if ( ! $this->debug()) { |
|
| 306 | 306 | return; |
| 307 | 307 | } |
| 308 | 308 | throw new InvalidArgumentException( |
@@ -317,10 +317,10 @@ discard block |
||
| 317 | 317 | ) |
| 318 | 318 | ); |
| 319 | 319 | } |
| 320 | - if ( ! isset( $this->jsdata[ $key ] ) ) { |
|
| 321 | - $this->jsdata[ $key ] = is_array($value) ? $value : [$value]; |
|
| 320 | + if ( ! isset($this->jsdata[$key])) { |
|
| 321 | + $this->jsdata[$key] = is_array($value) ? $value : [$value]; |
|
| 322 | 322 | } else { |
| 323 | - $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value); |
|
| 323 | + $this->jsdata[$key] = array_merge($this->jsdata[$key], (array) $value); |
|
| 324 | 324 | } |
| 325 | 325 | } |
| 326 | 326 | |
@@ -335,12 +335,12 @@ discard block |
||
| 335 | 335 | */ |
| 336 | 336 | public function addTemplate($template_reference, $template_content) |
| 337 | 337 | { |
| 338 | - if (! isset($this->jsdata['templates'])) { |
|
| 338 | + if ( ! isset($this->jsdata['templates'])) { |
|
| 339 | 339 | $this->jsdata['templates'] = array(); |
| 340 | 340 | } |
| 341 | 341 | //no overrides allowed. |
| 342 | - if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 343 | - if (! $this->debug()) { |
|
| 342 | + if (isset($this->jsdata['templates'][$template_reference])) { |
|
| 343 | + if ( ! $this->debug()) { |
|
| 344 | 344 | return; |
| 345 | 345 | } |
| 346 | 346 | throw new InvalidArgumentException( |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | ) |
| 354 | 354 | ); |
| 355 | 355 | } |
| 356 | - $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 356 | + $this->jsdata['templates'][$template_reference] = $template_content; |
|
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | |
@@ -365,8 +365,8 @@ discard block |
||
| 365 | 365 | */ |
| 366 | 366 | public function getTemplate($template_reference) |
| 367 | 367 | { |
| 368 | - return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 369 | - ? $this->jsdata['templates'][ $template_reference ] |
|
| 368 | + return isset($this->jsdata['templates'][$template_reference]) |
|
| 369 | + ? $this->jsdata['templates'][$template_reference] |
|
| 370 | 370 | : ''; |
| 371 | 371 | } |
| 372 | 372 | |
@@ -379,8 +379,8 @@ discard block |
||
| 379 | 379 | */ |
| 380 | 380 | public function getData($key) |
| 381 | 381 | { |
| 382 | - return isset($this->jsdata[ $key ]) |
|
| 383 | - ? $this->jsdata[ $key ] |
|
| 382 | + return isset($this->jsdata[$key]) |
|
| 383 | + ? $this->jsdata[$key] |
|
| 384 | 384 | : false; |
| 385 | 385 | } |
| 386 | 386 | |
@@ -395,11 +395,11 @@ discard block |
||
| 395 | 395 | */ |
| 396 | 396 | protected function verifyDataNotExisting($key) |
| 397 | 397 | { |
| 398 | - if (isset($this->jsdata[ $key ])) { |
|
| 399 | - if (! $this->debug()) { |
|
| 398 | + if (isset($this->jsdata[$key])) { |
|
| 399 | + if ( ! $this->debug()) { |
|
| 400 | 400 | return false; |
| 401 | 401 | } |
| 402 | - if (is_array($this->jsdata[ $key ])) { |
|
| 402 | + if (is_array($this->jsdata[$key])) { |
|
| 403 | 403 | throw new InvalidArgumentException( |
| 404 | 404 | sprintf( |
| 405 | 405 | __( |
@@ -442,11 +442,11 @@ discard block |
||
| 442 | 442 | public function getAssetUrl($namespace, $chunk_name, $asset_type) |
| 443 | 443 | { |
| 444 | 444 | $url = isset( |
| 445 | - $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
|
| 446 | - $this->manifest_data[ $namespace ]['url_base'] |
|
| 445 | + $this->manifest_data[$namespace][$chunk_name.'.'.$asset_type], |
|
| 446 | + $this->manifest_data[$namespace]['url_base'] |
|
| 447 | 447 | ) |
| 448 | - ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 449 | - . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
|
| 448 | + ? $this->manifest_data[$namespace]['url_base'] |
|
| 449 | + . $this->manifest_data[$namespace][$chunk_name.'.'.$asset_type] |
|
| 450 | 450 | : $chunk_name; |
| 451 | 451 | |
| 452 | 452 | return apply_filters( |
@@ -497,21 +497,21 @@ discard block |
||
| 497 | 497 | */ |
| 498 | 498 | private function getDependenciesForAsset($namespace, $chunk_name, $asset_type) |
| 499 | 499 | { |
| 500 | - $asset_index = $chunk_name . '.' . $asset_type; |
|
| 501 | - if (! isset( $this->dependencies_data[ $namespace ][ $asset_index ])) { |
|
| 502 | - $path = isset($this->manifest_data[ $namespace ]['path']) |
|
| 503 | - ? $this->manifest_data[ $namespace ]['path'] |
|
| 500 | + $asset_index = $chunk_name.'.'.$asset_type; |
|
| 501 | + if ( ! isset($this->dependencies_data[$namespace][$asset_index])) { |
|
| 502 | + $path = isset($this->manifest_data[$namespace]['path']) |
|
| 503 | + ? $this->manifest_data[$namespace]['path'] |
|
| 504 | 504 | : ''; |
| 505 | - $dependencies_index = $chunk_name . '.' . Asset::TYPE_JSON; |
|
| 506 | - $file_path = isset($this->manifest_data[ $namespace ][ $dependencies_index ]) |
|
| 507 | - ? $path . $this->manifest_data[ $namespace ][ $dependencies_index ] |
|
| 505 | + $dependencies_index = $chunk_name.'.'.Asset::TYPE_JSON; |
|
| 506 | + $file_path = isset($this->manifest_data[$namespace][$dependencies_index]) |
|
| 507 | + ? $path.$this->manifest_data[$namespace][$dependencies_index] |
|
| 508 | 508 | : |
| 509 | 509 | ''; |
| 510 | - $this->dependencies_data[ $namespace ][ $asset_index ] = $file_path !== '' && file_exists($file_path) |
|
| 510 | + $this->dependencies_data[$namespace][$asset_index] = $file_path !== '' && file_exists($file_path) |
|
| 511 | 511 | ? $this->getDependenciesForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
| 512 | 512 | : []; |
| 513 | 513 | } |
| 514 | - return $this->dependencies_data[ $namespace ][ $asset_index ]; |
|
| 514 | + return $this->dependencies_data[$namespace][$asset_index]; |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | |
@@ -533,32 +533,32 @@ discard block |
||
| 533 | 533 | if ($asset_type === Asset::TYPE_JS) { |
| 534 | 534 | return $chunk_name === 'eejs-core' ? $asset_dependencies : array_merge( |
| 535 | 535 | $asset_dependencies, |
| 536 | - [ CoreAssetManager::JS_HANDLE_JS_CORE ] |
|
| 536 | + [CoreAssetManager::JS_HANDLE_JS_CORE] |
|
| 537 | 537 | ); |
| 538 | 538 | } |
| 539 | 539 | // for css we need to make sure there is actually a css file related to this chunk. |
| 540 | - if (isset($this->manifest_data[ $namespace ])) { |
|
| 540 | + if (isset($this->manifest_data[$namespace])) { |
|
| 541 | 541 | // array of css chunk files for ee. |
| 542 | 542 | $css_chunks = array_map( |
| 543 | - function ($value) { |
|
| 543 | + function($value) { |
|
| 544 | 544 | return str_replace('.css', '', $value); |
| 545 | 545 | }, |
| 546 | 546 | array_filter( |
| 547 | - array_keys($this->manifest_data[ $namespace ]), |
|
| 548 | - function ($value) { |
|
| 547 | + array_keys($this->manifest_data[$namespace]), |
|
| 548 | + function($value) { |
|
| 549 | 549 | return strpos($value, '.css') !== false; |
| 550 | 550 | } |
| 551 | 551 | ) |
| 552 | 552 | ); |
| 553 | 553 | // add known wp chunks with css |
| 554 | - $css_chunks = array_merge( $css_chunks, $this->wp_css_handle_dependencies); |
|
| 554 | + $css_chunks = array_merge($css_chunks, $this->wp_css_handle_dependencies); |
|
| 555 | 555 | // flip for easier search |
| 556 | 556 | $css_chunks = array_flip($css_chunks); |
| 557 | 557 | // now let's filter the dependencies for the incoming chunk to actual chunks that have styles |
| 558 | 558 | return array_filter( |
| 559 | 559 | $asset_dependencies, |
| 560 | - function ($chunk_name) use ($css_chunks) { |
|
| 561 | - return isset($css_chunks[ $chunk_name ]); |
|
| 560 | + function($chunk_name) use ($css_chunks) { |
|
| 561 | + return isset($css_chunks[$chunk_name]); |
|
| 562 | 562 | } |
| 563 | 563 | ); |
| 564 | 564 | } |
@@ -606,7 +606,7 @@ discard block |
||
| 606 | 606 | $this->registerManifestFile( |
| 607 | 607 | $manifest_file->assetNamespace(), |
| 608 | 608 | $manifest_file->urlBase(), |
| 609 | - $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST, |
|
| 609 | + $manifest_file->filepath().Registry::FILE_NAME_BUILD_MANIFEST, |
|
| 610 | 610 | $manifest_file->filepath() |
| 611 | 611 | ); |
| 612 | 612 | } |
@@ -627,8 +627,8 @@ discard block |
||
| 627 | 627 | */ |
| 628 | 628 | public function registerManifestFile($namespace, $url_base, $manifest_file, $manifest_file_path = '') |
| 629 | 629 | { |
| 630 | - if (isset($this->manifest_data[ $namespace ])) { |
|
| 631 | - if (! $this->debug()) { |
|
| 630 | + if (isset($this->manifest_data[$namespace])) { |
|
| 631 | + if ( ! $this->debug()) { |
|
| 632 | 632 | return; |
| 633 | 633 | } |
| 634 | 634 | throw new InvalidArgumentException( |
@@ -661,12 +661,12 @@ discard block |
||
| 661 | 661 | } |
| 662 | 662 | return; |
| 663 | 663 | } |
| 664 | - $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 665 | - if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 666 | - $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 664 | + $this->manifest_data[$namespace] = $this->decodeManifestFile($manifest_file); |
|
| 665 | + if ( ! isset($this->manifest_data[$namespace]['url_base'])) { |
|
| 666 | + $this->manifest_data[$namespace]['url_base'] = trailingslashit($url_base); |
|
| 667 | 667 | } |
| 668 | - if (! isset($this->manifest_data[ $namespace ]['path'])) { |
|
| 669 | - $this->manifest_data[ $namespace ]['path'] = $manifest_file_path; |
|
| 668 | + if ( ! isset($this->manifest_data[$namespace]['path'])) { |
|
| 669 | + $this->manifest_data[$namespace]['path'] = $manifest_file_path; |
|
| 670 | 670 | } |
| 671 | 671 | } |
| 672 | 672 | |
@@ -681,7 +681,7 @@ discard block |
||
| 681 | 681 | */ |
| 682 | 682 | private function decodeManifestFile($manifest_file) |
| 683 | 683 | { |
| 684 | - if (! file_exists($manifest_file)) { |
|
| 684 | + if ( ! file_exists($manifest_file)) { |
|
| 685 | 685 | throw new InvalidFilePathException($manifest_file); |
| 686 | 686 | } |
| 687 | 687 | return json_decode(file_get_contents($manifest_file), true); |
@@ -695,7 +695,7 @@ discard block |
||
| 695 | 695 | */ |
| 696 | 696 | private function addRegisteredScriptHandlesWithData($script_handle) |
| 697 | 697 | { |
| 698 | - $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 698 | + $this->script_handles_with_data[$script_handle] = $script_handle; |
|
| 699 | 699 | } |
| 700 | 700 | |
| 701 | 701 | |
@@ -721,23 +721,23 @@ discard block |
||
| 721 | 721 | */ |
| 722 | 722 | private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
| 723 | 723 | { |
| 724 | - if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 724 | + if (isset($this->script_handles_with_data[$script_handle])) { |
|
| 725 | 725 | global $wp_scripts; |
| 726 | 726 | $unset_handle = false; |
| 727 | 727 | if ($wp_scripts->get_data($script_handle, 'data')) { |
| 728 | - unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 728 | + unset($wp_scripts->registered[$script_handle]->extra['data']); |
|
| 729 | 729 | $unset_handle = true; |
| 730 | 730 | } |
| 731 | 731 | //deal with inline_scripts |
| 732 | 732 | if ($wp_scripts->get_data($script_handle, 'before')) { |
| 733 | - unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 733 | + unset($wp_scripts->registered[$script_handle]->extra['before']); |
|
| 734 | 734 | $unset_handle = true; |
| 735 | 735 | } |
| 736 | 736 | if ($wp_scripts->get_data($script_handle, 'after')) { |
| 737 | - unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 737 | + unset($wp_scripts->registered[$script_handle]->extra['after']); |
|
| 738 | 738 | } |
| 739 | 739 | if ($unset_handle) { |
| 740 | - unset($this->script_handles_with_data[ $script_handle ]); |
|
| 740 | + unset($this->script_handles_with_data[$script_handle]); |
|
| 741 | 741 | } |
| 742 | 742 | } |
| 743 | 743 | } |
@@ -27,742 +27,742 @@ |
||
| 27 | 27 | class Registry |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var AssetCollection $assets |
|
| 34 | - */ |
|
| 35 | - protected $assets; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var I18nRegistry |
|
| 39 | - */ |
|
| 40 | - private $i18n_registry; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
| 44 | - * |
|
| 45 | - * @var array |
|
| 46 | - */ |
|
| 47 | - protected $jsdata = array(); |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
| 51 | - * page source. |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - private $script_handles_with_data = array(); |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Holds the manifest data obtained from registered manifest files. |
|
| 60 | - * Manifests are maps of asset chunk name to actual built asset file names. |
|
| 61 | - * Shape of this array is: |
|
| 62 | - * array( |
|
| 63 | - * 'some_namespace_slug' => array( |
|
| 64 | - * 'some_chunk_name' => array( |
|
| 65 | - * 'js' => 'filename.js' |
|
| 66 | - * 'css' => 'filename.js' |
|
| 67 | - * ), |
|
| 68 | - * 'url_base' => 'https://baseurl.com/to/assets |
|
| 69 | - * ) |
|
| 70 | - * ) |
|
| 71 | - * |
|
| 72 | - * @var array |
|
| 73 | - */ |
|
| 74 | - private $manifest_data = array(); |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Holds any dependency data obtained from registered dependency map json. |
|
| 79 | - * Dependency map json is generated via the @wordpress/dependency-extraction-webpack-plugin via the webpack config. |
|
| 80 | - * @see https://github.com/WordPress/gutenberg/tree/master/packages/dependency-extraction-webpack-plugin |
|
| 81 | - * |
|
| 82 | - * @var array |
|
| 83 | - */ |
|
| 84 | - private $dependencies_data = []; |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * This is a known array of possible wp css handles that correspond to what may be exposed as dependencies in our |
|
| 89 | - * build process. Currently the dependency export process in webpack does not consider css imports, so we derive |
|
| 90 | - * them via the js dependencies (WP uses the same handle for both js and css). This is a list of known handles that |
|
| 91 | - * are used for both js and css. |
|
| 92 | - * @var array |
|
| 93 | - */ |
|
| 94 | - private $wp_css_handle_dependencies = [ |
|
| 95 | - 'wp-components', |
|
| 96 | - 'wp-block-editor', |
|
| 97 | - 'wp-block-library', |
|
| 98 | - 'wp-edit-post', |
|
| 99 | - 'wp-edit-widgets', |
|
| 100 | - 'wp-editor', |
|
| 101 | - 'wp-format-library', |
|
| 102 | - 'wp-list-reusable-blocks', |
|
| 103 | - 'wp-nux', |
|
| 104 | - ]; |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Registry constructor. |
|
| 109 | - * Hooking into WP actions for script registry. |
|
| 110 | - * |
|
| 111 | - * @param AssetCollection $assets |
|
| 112 | - * @param I18nRegistry $i18n_registry |
|
| 113 | - * @throws InvalidArgumentException |
|
| 114 | - * @throws InvalidDataTypeException |
|
| 115 | - * @throws InvalidInterfaceException |
|
| 116 | - */ |
|
| 117 | - public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
|
| 118 | - { |
|
| 119 | - $this->assets = $assets; |
|
| 120 | - $this->i18n_registry = $i18n_registry; |
|
| 121 | - add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 122 | - add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 123 | - add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 124 | - add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 125 | - add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 126 | - add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 127 | - add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 128 | - add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
|
| 134 | - * translation handling. |
|
| 135 | - * |
|
| 136 | - * @return I18nRegistry |
|
| 137 | - */ |
|
| 138 | - public function getI18nRegistry() |
|
| 139 | - { |
|
| 140 | - return $this->i18n_registry; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Callback for the wp_enqueue_scripts actions used to register assets. |
|
| 146 | - * |
|
| 147 | - * @since 4.9.62.p |
|
| 148 | - * @throws Exception |
|
| 149 | - */ |
|
| 150 | - public function registerScriptsAndStyles() |
|
| 151 | - { |
|
| 152 | - try { |
|
| 153 | - $this->registerScripts($this->assets->getJavascriptAssets()); |
|
| 154 | - $this->registerStyles($this->assets->getStylesheetAssets()); |
|
| 155 | - } catch (Exception $exception) { |
|
| 156 | - new ExceptionStackTraceDisplay($exception); |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Registers JS assets with WP core |
|
| 163 | - * |
|
| 164 | - * @since 4.9.62.p |
|
| 165 | - * @param JavascriptAsset[] $scripts |
|
| 166 | - * @throws AssetRegistrationException |
|
| 167 | - * @throws InvalidDataTypeException |
|
| 168 | - */ |
|
| 169 | - public function registerScripts(array $scripts) |
|
| 170 | - { |
|
| 171 | - foreach ($scripts as $script) { |
|
| 172 | - // skip to next script if this has already been done |
|
| 173 | - if ($script->isRegistered()) { |
|
| 174 | - continue; |
|
| 175 | - } |
|
| 176 | - do_action( |
|
| 177 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 178 | - $script |
|
| 179 | - ); |
|
| 180 | - $registered = wp_register_script( |
|
| 181 | - $script->handle(), |
|
| 182 | - $script->source(), |
|
| 183 | - $script->dependencies(), |
|
| 184 | - $script->version(), |
|
| 185 | - $script->loadInFooter() |
|
| 186 | - ); |
|
| 187 | - if (! $registered && $this->debug()) { |
|
| 188 | - throw new AssetRegistrationException($script->handle()); |
|
| 189 | - } |
|
| 190 | - $script->setRegistered($registered); |
|
| 191 | - if ($script->requiresTranslation()) { |
|
| 192 | - $this->registerTranslation($script->handle()); |
|
| 193 | - } |
|
| 194 | - do_action( |
|
| 195 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
|
| 196 | - $script |
|
| 197 | - ); |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Registers CSS assets with WP core |
|
| 204 | - * |
|
| 205 | - * @since 4.9.62.p |
|
| 206 | - * @param StylesheetAsset[] $styles |
|
| 207 | - * @throws InvalidDataTypeException |
|
| 208 | - */ |
|
| 209 | - public function registerStyles(array $styles) |
|
| 210 | - { |
|
| 211 | - foreach ($styles as $style) { |
|
| 212 | - // skip to next style if this has already been done |
|
| 213 | - if ($style->isRegistered()) { |
|
| 214 | - continue; |
|
| 215 | - } |
|
| 216 | - do_action( |
|
| 217 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
|
| 218 | - $style |
|
| 219 | - ); |
|
| 220 | - wp_register_style( |
|
| 221 | - $style->handle(), |
|
| 222 | - $style->source(), |
|
| 223 | - $style->dependencies(), |
|
| 224 | - $style->version(), |
|
| 225 | - $style->media() |
|
| 226 | - ); |
|
| 227 | - $style->setRegistered(); |
|
| 228 | - do_action( |
|
| 229 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
|
| 230 | - $style |
|
| 231 | - ); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Call back for the script print in frontend and backend. |
|
| 238 | - * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
| 239 | - * |
|
| 240 | - * @since 4.9.31.rc.015 |
|
| 241 | - */ |
|
| 242 | - public function enqueueData() |
|
| 243 | - { |
|
| 244 | - $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
| 245 | - wp_add_inline_script( |
|
| 246 | - 'eejs-core', |
|
| 247 | - 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 248 | - 'before' |
|
| 249 | - ); |
|
| 250 | - $scripts = $this->assets->getJavascriptAssetsWithData(); |
|
| 251 | - foreach ($scripts as $script) { |
|
| 252 | - $this->addRegisteredScriptHandlesWithData($script->handle()); |
|
| 253 | - if ($script->hasInlineDataCallback()) { |
|
| 254 | - $localize = $script->inlineDataCallback(); |
|
| 255 | - $localize(); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Used to add data to eejs.data object. |
|
| 263 | - * Note: Overriding existing data is not allowed. |
|
| 264 | - * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
| 265 | - * If the data you add is something like this: |
|
| 266 | - * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
| 267 | - * It will be exposed in the page source as: |
|
| 268 | - * eejs.data.my_plugin_data.foo == gar |
|
| 269 | - * |
|
| 270 | - * @param string $key Key used to access your data |
|
| 271 | - * @param string|array $value Value to attach to key |
|
| 272 | - * @throws InvalidArgumentException |
|
| 273 | - */ |
|
| 274 | - public function addData($key, $value) |
|
| 275 | - { |
|
| 276 | - if ($this->verifyDataNotExisting($key)) { |
|
| 277 | - $this->jsdata[ $key ] = $value; |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
| 284 | - * elements in an array. |
|
| 285 | - * |
|
| 286 | - * When you use this method, the value you include will be merged with the array on $key. |
|
| 287 | - * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript |
|
| 288 | - * object like this, eejs.data.test = [ my_data, |
|
| 289 | - * ] |
|
| 290 | - * If there has already been a scalar value attached to the data object given key (via addData for instance), then |
|
| 291 | - * this will throw an exception. |
|
| 292 | - * |
|
| 293 | - * Caution: Only add data using this method if you are okay with the potential for additional data added on the same |
|
| 294 | - * key potentially overriding the existing data on merge (specifically with associative arrays). |
|
| 295 | - * |
|
| 296 | - * @param string $key Key to attach data to. |
|
| 297 | - * @param string|array $value Value being registered. |
|
| 298 | - * @throws InvalidArgumentException |
|
| 299 | - */ |
|
| 300 | - public function pushData($key, $value) |
|
| 301 | - { |
|
| 302 | - if (isset($this->jsdata[ $key ]) |
|
| 303 | - && ! is_array($this->jsdata[ $key ]) |
|
| 304 | - ) { |
|
| 305 | - if (! $this->debug()) { |
|
| 306 | - return; |
|
| 307 | - } |
|
| 308 | - throw new InvalidArgumentException( |
|
| 309 | - sprintf( |
|
| 310 | - __( |
|
| 311 | - 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
| 30 | + const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var AssetCollection $assets |
|
| 34 | + */ |
|
| 35 | + protected $assets; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var I18nRegistry |
|
| 39 | + */ |
|
| 40 | + private $i18n_registry; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
| 44 | + * |
|
| 45 | + * @var array |
|
| 46 | + */ |
|
| 47 | + protected $jsdata = array(); |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
| 51 | + * page source. |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + private $script_handles_with_data = array(); |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Holds the manifest data obtained from registered manifest files. |
|
| 60 | + * Manifests are maps of asset chunk name to actual built asset file names. |
|
| 61 | + * Shape of this array is: |
|
| 62 | + * array( |
|
| 63 | + * 'some_namespace_slug' => array( |
|
| 64 | + * 'some_chunk_name' => array( |
|
| 65 | + * 'js' => 'filename.js' |
|
| 66 | + * 'css' => 'filename.js' |
|
| 67 | + * ), |
|
| 68 | + * 'url_base' => 'https://baseurl.com/to/assets |
|
| 69 | + * ) |
|
| 70 | + * ) |
|
| 71 | + * |
|
| 72 | + * @var array |
|
| 73 | + */ |
|
| 74 | + private $manifest_data = array(); |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Holds any dependency data obtained from registered dependency map json. |
|
| 79 | + * Dependency map json is generated via the @wordpress/dependency-extraction-webpack-plugin via the webpack config. |
|
| 80 | + * @see https://github.com/WordPress/gutenberg/tree/master/packages/dependency-extraction-webpack-plugin |
|
| 81 | + * |
|
| 82 | + * @var array |
|
| 83 | + */ |
|
| 84 | + private $dependencies_data = []; |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * This is a known array of possible wp css handles that correspond to what may be exposed as dependencies in our |
|
| 89 | + * build process. Currently the dependency export process in webpack does not consider css imports, so we derive |
|
| 90 | + * them via the js dependencies (WP uses the same handle for both js and css). This is a list of known handles that |
|
| 91 | + * are used for both js and css. |
|
| 92 | + * @var array |
|
| 93 | + */ |
|
| 94 | + private $wp_css_handle_dependencies = [ |
|
| 95 | + 'wp-components', |
|
| 96 | + 'wp-block-editor', |
|
| 97 | + 'wp-block-library', |
|
| 98 | + 'wp-edit-post', |
|
| 99 | + 'wp-edit-widgets', |
|
| 100 | + 'wp-editor', |
|
| 101 | + 'wp-format-library', |
|
| 102 | + 'wp-list-reusable-blocks', |
|
| 103 | + 'wp-nux', |
|
| 104 | + ]; |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Registry constructor. |
|
| 109 | + * Hooking into WP actions for script registry. |
|
| 110 | + * |
|
| 111 | + * @param AssetCollection $assets |
|
| 112 | + * @param I18nRegistry $i18n_registry |
|
| 113 | + * @throws InvalidArgumentException |
|
| 114 | + * @throws InvalidDataTypeException |
|
| 115 | + * @throws InvalidInterfaceException |
|
| 116 | + */ |
|
| 117 | + public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
|
| 118 | + { |
|
| 119 | + $this->assets = $assets; |
|
| 120 | + $this->i18n_registry = $i18n_registry; |
|
| 121 | + add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 122 | + add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 123 | + add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 124 | + add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 125 | + add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 126 | + add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 127 | + add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 128 | + add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
|
| 134 | + * translation handling. |
|
| 135 | + * |
|
| 136 | + * @return I18nRegistry |
|
| 137 | + */ |
|
| 138 | + public function getI18nRegistry() |
|
| 139 | + { |
|
| 140 | + return $this->i18n_registry; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Callback for the wp_enqueue_scripts actions used to register assets. |
|
| 146 | + * |
|
| 147 | + * @since 4.9.62.p |
|
| 148 | + * @throws Exception |
|
| 149 | + */ |
|
| 150 | + public function registerScriptsAndStyles() |
|
| 151 | + { |
|
| 152 | + try { |
|
| 153 | + $this->registerScripts($this->assets->getJavascriptAssets()); |
|
| 154 | + $this->registerStyles($this->assets->getStylesheetAssets()); |
|
| 155 | + } catch (Exception $exception) { |
|
| 156 | + new ExceptionStackTraceDisplay($exception); |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Registers JS assets with WP core |
|
| 163 | + * |
|
| 164 | + * @since 4.9.62.p |
|
| 165 | + * @param JavascriptAsset[] $scripts |
|
| 166 | + * @throws AssetRegistrationException |
|
| 167 | + * @throws InvalidDataTypeException |
|
| 168 | + */ |
|
| 169 | + public function registerScripts(array $scripts) |
|
| 170 | + { |
|
| 171 | + foreach ($scripts as $script) { |
|
| 172 | + // skip to next script if this has already been done |
|
| 173 | + if ($script->isRegistered()) { |
|
| 174 | + continue; |
|
| 175 | + } |
|
| 176 | + do_action( |
|
| 177 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 178 | + $script |
|
| 179 | + ); |
|
| 180 | + $registered = wp_register_script( |
|
| 181 | + $script->handle(), |
|
| 182 | + $script->source(), |
|
| 183 | + $script->dependencies(), |
|
| 184 | + $script->version(), |
|
| 185 | + $script->loadInFooter() |
|
| 186 | + ); |
|
| 187 | + if (! $registered && $this->debug()) { |
|
| 188 | + throw new AssetRegistrationException($script->handle()); |
|
| 189 | + } |
|
| 190 | + $script->setRegistered($registered); |
|
| 191 | + if ($script->requiresTranslation()) { |
|
| 192 | + $this->registerTranslation($script->handle()); |
|
| 193 | + } |
|
| 194 | + do_action( |
|
| 195 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
|
| 196 | + $script |
|
| 197 | + ); |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Registers CSS assets with WP core |
|
| 204 | + * |
|
| 205 | + * @since 4.9.62.p |
|
| 206 | + * @param StylesheetAsset[] $styles |
|
| 207 | + * @throws InvalidDataTypeException |
|
| 208 | + */ |
|
| 209 | + public function registerStyles(array $styles) |
|
| 210 | + { |
|
| 211 | + foreach ($styles as $style) { |
|
| 212 | + // skip to next style if this has already been done |
|
| 213 | + if ($style->isRegistered()) { |
|
| 214 | + continue; |
|
| 215 | + } |
|
| 216 | + do_action( |
|
| 217 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
|
| 218 | + $style |
|
| 219 | + ); |
|
| 220 | + wp_register_style( |
|
| 221 | + $style->handle(), |
|
| 222 | + $style->source(), |
|
| 223 | + $style->dependencies(), |
|
| 224 | + $style->version(), |
|
| 225 | + $style->media() |
|
| 226 | + ); |
|
| 227 | + $style->setRegistered(); |
|
| 228 | + do_action( |
|
| 229 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
|
| 230 | + $style |
|
| 231 | + ); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Call back for the script print in frontend and backend. |
|
| 238 | + * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
| 239 | + * |
|
| 240 | + * @since 4.9.31.rc.015 |
|
| 241 | + */ |
|
| 242 | + public function enqueueData() |
|
| 243 | + { |
|
| 244 | + $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
| 245 | + wp_add_inline_script( |
|
| 246 | + 'eejs-core', |
|
| 247 | + 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 248 | + 'before' |
|
| 249 | + ); |
|
| 250 | + $scripts = $this->assets->getJavascriptAssetsWithData(); |
|
| 251 | + foreach ($scripts as $script) { |
|
| 252 | + $this->addRegisteredScriptHandlesWithData($script->handle()); |
|
| 253 | + if ($script->hasInlineDataCallback()) { |
|
| 254 | + $localize = $script->inlineDataCallback(); |
|
| 255 | + $localize(); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Used to add data to eejs.data object. |
|
| 263 | + * Note: Overriding existing data is not allowed. |
|
| 264 | + * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
| 265 | + * If the data you add is something like this: |
|
| 266 | + * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
| 267 | + * It will be exposed in the page source as: |
|
| 268 | + * eejs.data.my_plugin_data.foo == gar |
|
| 269 | + * |
|
| 270 | + * @param string $key Key used to access your data |
|
| 271 | + * @param string|array $value Value to attach to key |
|
| 272 | + * @throws InvalidArgumentException |
|
| 273 | + */ |
|
| 274 | + public function addData($key, $value) |
|
| 275 | + { |
|
| 276 | + if ($this->verifyDataNotExisting($key)) { |
|
| 277 | + $this->jsdata[ $key ] = $value; |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
| 284 | + * elements in an array. |
|
| 285 | + * |
|
| 286 | + * When you use this method, the value you include will be merged with the array on $key. |
|
| 287 | + * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript |
|
| 288 | + * object like this, eejs.data.test = [ my_data, |
|
| 289 | + * ] |
|
| 290 | + * If there has already been a scalar value attached to the data object given key (via addData for instance), then |
|
| 291 | + * this will throw an exception. |
|
| 292 | + * |
|
| 293 | + * Caution: Only add data using this method if you are okay with the potential for additional data added on the same |
|
| 294 | + * key potentially overriding the existing data on merge (specifically with associative arrays). |
|
| 295 | + * |
|
| 296 | + * @param string $key Key to attach data to. |
|
| 297 | + * @param string|array $value Value being registered. |
|
| 298 | + * @throws InvalidArgumentException |
|
| 299 | + */ |
|
| 300 | + public function pushData($key, $value) |
|
| 301 | + { |
|
| 302 | + if (isset($this->jsdata[ $key ]) |
|
| 303 | + && ! is_array($this->jsdata[ $key ]) |
|
| 304 | + ) { |
|
| 305 | + if (! $this->debug()) { |
|
| 306 | + return; |
|
| 307 | + } |
|
| 308 | + throw new InvalidArgumentException( |
|
| 309 | + sprintf( |
|
| 310 | + __( |
|
| 311 | + 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
| 312 | 312 | push values to this data element when it is an array.', |
| 313 | - 'event_espresso' |
|
| 314 | - ), |
|
| 315 | - $key, |
|
| 316 | - __METHOD__ |
|
| 317 | - ) |
|
| 318 | - ); |
|
| 319 | - } |
|
| 320 | - if ( ! isset( $this->jsdata[ $key ] ) ) { |
|
| 321 | - $this->jsdata[ $key ] = is_array($value) ? $value : [$value]; |
|
| 322 | - } else { |
|
| 323 | - $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value); |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Used to set content used by javascript for a template. |
|
| 330 | - * Note: Overrides of existing registered templates are not allowed. |
|
| 331 | - * |
|
| 332 | - * @param string $template_reference |
|
| 333 | - * @param string $template_content |
|
| 334 | - * @throws InvalidArgumentException |
|
| 335 | - */ |
|
| 336 | - public function addTemplate($template_reference, $template_content) |
|
| 337 | - { |
|
| 338 | - if (! isset($this->jsdata['templates'])) { |
|
| 339 | - $this->jsdata['templates'] = array(); |
|
| 340 | - } |
|
| 341 | - //no overrides allowed. |
|
| 342 | - if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 343 | - if (! $this->debug()) { |
|
| 344 | - return; |
|
| 345 | - } |
|
| 346 | - throw new InvalidArgumentException( |
|
| 347 | - sprintf( |
|
| 348 | - __( |
|
| 349 | - 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
| 350 | - 'event_espresso' |
|
| 351 | - ), |
|
| 352 | - $template_reference |
|
| 353 | - ) |
|
| 354 | - ); |
|
| 355 | - } |
|
| 356 | - $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Retrieve the template content already registered for the given reference. |
|
| 362 | - * |
|
| 363 | - * @param string $template_reference |
|
| 364 | - * @return string |
|
| 365 | - */ |
|
| 366 | - public function getTemplate($template_reference) |
|
| 367 | - { |
|
| 368 | - return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 369 | - ? $this->jsdata['templates'][ $template_reference ] |
|
| 370 | - : ''; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Retrieve registered data. |
|
| 376 | - * |
|
| 377 | - * @param string $key Name of key to attach data to. |
|
| 378 | - * @return mixed If there is no for the given key, then false is returned. |
|
| 379 | - */ |
|
| 380 | - public function getData($key) |
|
| 381 | - { |
|
| 382 | - return isset($this->jsdata[ $key ]) |
|
| 383 | - ? $this->jsdata[ $key ] |
|
| 384 | - : false; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Verifies whether the given data exists already on the jsdata array. |
|
| 390 | - * Overriding data is not allowed. |
|
| 391 | - * |
|
| 392 | - * @param string $key Index for data. |
|
| 393 | - * @return bool If valid then return true. |
|
| 394 | - * @throws InvalidArgumentException if data already exists. |
|
| 395 | - */ |
|
| 396 | - protected function verifyDataNotExisting($key) |
|
| 397 | - { |
|
| 398 | - if (isset($this->jsdata[ $key ])) { |
|
| 399 | - if (! $this->debug()) { |
|
| 400 | - return false; |
|
| 401 | - } |
|
| 402 | - if (is_array($this->jsdata[ $key ])) { |
|
| 403 | - throw new InvalidArgumentException( |
|
| 404 | - sprintf( |
|
| 405 | - __( |
|
| 406 | - 'The value for %1$s already exists in the Registry::eejs object. |
|
| 313 | + 'event_espresso' |
|
| 314 | + ), |
|
| 315 | + $key, |
|
| 316 | + __METHOD__ |
|
| 317 | + ) |
|
| 318 | + ); |
|
| 319 | + } |
|
| 320 | + if ( ! isset( $this->jsdata[ $key ] ) ) { |
|
| 321 | + $this->jsdata[ $key ] = is_array($value) ? $value : [$value]; |
|
| 322 | + } else { |
|
| 323 | + $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value); |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Used to set content used by javascript for a template. |
|
| 330 | + * Note: Overrides of existing registered templates are not allowed. |
|
| 331 | + * |
|
| 332 | + * @param string $template_reference |
|
| 333 | + * @param string $template_content |
|
| 334 | + * @throws InvalidArgumentException |
|
| 335 | + */ |
|
| 336 | + public function addTemplate($template_reference, $template_content) |
|
| 337 | + { |
|
| 338 | + if (! isset($this->jsdata['templates'])) { |
|
| 339 | + $this->jsdata['templates'] = array(); |
|
| 340 | + } |
|
| 341 | + //no overrides allowed. |
|
| 342 | + if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 343 | + if (! $this->debug()) { |
|
| 344 | + return; |
|
| 345 | + } |
|
| 346 | + throw new InvalidArgumentException( |
|
| 347 | + sprintf( |
|
| 348 | + __( |
|
| 349 | + 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
| 350 | + 'event_espresso' |
|
| 351 | + ), |
|
| 352 | + $template_reference |
|
| 353 | + ) |
|
| 354 | + ); |
|
| 355 | + } |
|
| 356 | + $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Retrieve the template content already registered for the given reference. |
|
| 362 | + * |
|
| 363 | + * @param string $template_reference |
|
| 364 | + * @return string |
|
| 365 | + */ |
|
| 366 | + public function getTemplate($template_reference) |
|
| 367 | + { |
|
| 368 | + return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 369 | + ? $this->jsdata['templates'][ $template_reference ] |
|
| 370 | + : ''; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Retrieve registered data. |
|
| 376 | + * |
|
| 377 | + * @param string $key Name of key to attach data to. |
|
| 378 | + * @return mixed If there is no for the given key, then false is returned. |
|
| 379 | + */ |
|
| 380 | + public function getData($key) |
|
| 381 | + { |
|
| 382 | + return isset($this->jsdata[ $key ]) |
|
| 383 | + ? $this->jsdata[ $key ] |
|
| 384 | + : false; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Verifies whether the given data exists already on the jsdata array. |
|
| 390 | + * Overriding data is not allowed. |
|
| 391 | + * |
|
| 392 | + * @param string $key Index for data. |
|
| 393 | + * @return bool If valid then return true. |
|
| 394 | + * @throws InvalidArgumentException if data already exists. |
|
| 395 | + */ |
|
| 396 | + protected function verifyDataNotExisting($key) |
|
| 397 | + { |
|
| 398 | + if (isset($this->jsdata[ $key ])) { |
|
| 399 | + if (! $this->debug()) { |
|
| 400 | + return false; |
|
| 401 | + } |
|
| 402 | + if (is_array($this->jsdata[ $key ])) { |
|
| 403 | + throw new InvalidArgumentException( |
|
| 404 | + sprintf( |
|
| 405 | + __( |
|
| 406 | + 'The value for %1$s already exists in the Registry::eejs object. |
|
| 407 | 407 | Overrides are not allowed. Since the value of this data is an array, you may want to use the |
| 408 | 408 | %2$s method to push your value to the array.', |
| 409 | - 'event_espresso' |
|
| 410 | - ), |
|
| 411 | - $key, |
|
| 412 | - 'pushData()' |
|
| 413 | - ) |
|
| 414 | - ); |
|
| 415 | - } |
|
| 416 | - throw new InvalidArgumentException( |
|
| 417 | - sprintf( |
|
| 418 | - __( |
|
| 419 | - 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
| 409 | + 'event_espresso' |
|
| 410 | + ), |
|
| 411 | + $key, |
|
| 412 | + 'pushData()' |
|
| 413 | + ) |
|
| 414 | + ); |
|
| 415 | + } |
|
| 416 | + throw new InvalidArgumentException( |
|
| 417 | + sprintf( |
|
| 418 | + __( |
|
| 419 | + 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
| 420 | 420 | allowed. Consider attaching your value to a different key', |
| 421 | - 'event_espresso' |
|
| 422 | - ), |
|
| 423 | - $key |
|
| 424 | - ) |
|
| 425 | - ); |
|
| 426 | - } |
|
| 427 | - return true; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Get the actual asset path for asset manifests. |
|
| 433 | - * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
|
| 434 | - * |
|
| 435 | - * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
|
| 436 | - * asset file location. |
|
| 437 | - * @param string $chunk_name |
|
| 438 | - * @param string $asset_type |
|
| 439 | - * @return string |
|
| 440 | - * @since 4.9.59.p |
|
| 441 | - */ |
|
| 442 | - public function getAssetUrl($namespace, $chunk_name, $asset_type) |
|
| 443 | - { |
|
| 444 | - $url = isset( |
|
| 445 | - $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
|
| 446 | - $this->manifest_data[ $namespace ]['url_base'] |
|
| 447 | - ) |
|
| 448 | - ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 449 | - . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
|
| 450 | - : $chunk_name; |
|
| 451 | - |
|
| 452 | - return apply_filters( |
|
| 453 | - 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
|
| 454 | - $url, |
|
| 455 | - $namespace, |
|
| 456 | - $chunk_name, |
|
| 457 | - $asset_type |
|
| 458 | - ); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Return the url to a js file for the given namespace and chunk name. |
|
| 465 | - * |
|
| 466 | - * @param string $namespace |
|
| 467 | - * @param string $chunk_name |
|
| 468 | - * @return string |
|
| 469 | - */ |
|
| 470 | - public function getJsUrl($namespace, $chunk_name) |
|
| 471 | - { |
|
| 472 | - return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Return the url to a css file for the given namespace and chunk name. |
|
| 478 | - * |
|
| 479 | - * @param string $namespace |
|
| 480 | - * @param string $chunk_name |
|
| 481 | - * @return string |
|
| 482 | - */ |
|
| 483 | - public function getCssUrl($namespace, $chunk_name) |
|
| 484 | - { |
|
| 485 | - return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Return the dependencies for a given asset $chunk_name |
|
| 491 | - * |
|
| 492 | - * @param string $namespace |
|
| 493 | - * @param string $chunk_name |
|
| 494 | - * @param string $asset_type |
|
| 495 | - * @return array |
|
| 496 | - * @since 4.9.82.p |
|
| 497 | - */ |
|
| 498 | - private function getDependenciesForAsset($namespace, $chunk_name, $asset_type) |
|
| 499 | - { |
|
| 500 | - $asset_index = $chunk_name . '.' . $asset_type; |
|
| 501 | - if (! isset( $this->dependencies_data[ $namespace ][ $asset_index ])) { |
|
| 502 | - $path = isset($this->manifest_data[ $namespace ]['path']) |
|
| 503 | - ? $this->manifest_data[ $namespace ]['path'] |
|
| 504 | - : ''; |
|
| 505 | - $dependencies_index = $chunk_name . '.' . Asset::TYPE_JSON; |
|
| 506 | - $file_path = isset($this->manifest_data[ $namespace ][ $dependencies_index ]) |
|
| 507 | - ? $path . $this->manifest_data[ $namespace ][ $dependencies_index ] |
|
| 508 | - : |
|
| 509 | - ''; |
|
| 510 | - $this->dependencies_data[ $namespace ][ $asset_index ] = $file_path !== '' && file_exists($file_path) |
|
| 511 | - ? $this->getDependenciesForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
|
| 512 | - : []; |
|
| 513 | - } |
|
| 514 | - return $this->dependencies_data[ $namespace ][ $asset_index ]; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Return dependencies according to asset type. |
|
| 520 | - * |
|
| 521 | - * For css assets, this filters the auto generated dependencies by css type. |
|
| 522 | - * |
|
| 523 | - * @param string $namespace |
|
| 524 | - * @param string $asset_type |
|
| 525 | - * @param string $file_path |
|
| 526 | - * @param string $chunk_name |
|
| 527 | - * @return array |
|
| 528 | - * @since 4.9.82.p |
|
| 529 | - */ |
|
| 530 | - private function getDependenciesForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
|
| 531 | - { |
|
| 532 | - $asset_dependencies = json_decode(file_get_contents($file_path), true); |
|
| 533 | - if ($asset_type === Asset::TYPE_JS) { |
|
| 534 | - return $chunk_name === 'eejs-core' ? $asset_dependencies : array_merge( |
|
| 535 | - $asset_dependencies, |
|
| 536 | - [ CoreAssetManager::JS_HANDLE_JS_CORE ] |
|
| 537 | - ); |
|
| 538 | - } |
|
| 539 | - // for css we need to make sure there is actually a css file related to this chunk. |
|
| 540 | - if (isset($this->manifest_data[ $namespace ])) { |
|
| 541 | - // array of css chunk files for ee. |
|
| 542 | - $css_chunks = array_map( |
|
| 543 | - function ($value) { |
|
| 544 | - return str_replace('.css', '', $value); |
|
| 545 | - }, |
|
| 546 | - array_filter( |
|
| 547 | - array_keys($this->manifest_data[ $namespace ]), |
|
| 548 | - function ($value) { |
|
| 549 | - return strpos($value, '.css') !== false; |
|
| 550 | - } |
|
| 551 | - ) |
|
| 552 | - ); |
|
| 553 | - // add known wp chunks with css |
|
| 554 | - $css_chunks = array_merge( $css_chunks, $this->wp_css_handle_dependencies); |
|
| 555 | - // flip for easier search |
|
| 556 | - $css_chunks = array_flip($css_chunks); |
|
| 557 | - // now let's filter the dependencies for the incoming chunk to actual chunks that have styles |
|
| 558 | - return array_filter( |
|
| 559 | - $asset_dependencies, |
|
| 560 | - function ($chunk_name) use ($css_chunks) { |
|
| 561 | - return isset($css_chunks[ $chunk_name ]); |
|
| 562 | - } |
|
| 563 | - ); |
|
| 564 | - } |
|
| 565 | - return []; |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Get the dependencies array for the given js asset chunk name |
|
| 571 | - * |
|
| 572 | - * @param string $namespace |
|
| 573 | - * @param string $chunk_name |
|
| 574 | - * @return array |
|
| 575 | - * @since 4.9.82.p |
|
| 576 | - */ |
|
| 577 | - public function getJsDependencies($namespace, $chunk_name) |
|
| 578 | - { |
|
| 579 | - return $this->getDependenciesForAsset($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Get the dependencies array for the given css asset chunk name |
|
| 585 | - * |
|
| 586 | - * @param string $namespace |
|
| 587 | - * @param string $chunk_name |
|
| 588 | - * @return array |
|
| 589 | - * @since 4.9.82.p |
|
| 590 | - */ |
|
| 591 | - public function getCssDependencies($namespace, $chunk_name) |
|
| 592 | - { |
|
| 593 | - return $this->getDependenciesForAsset($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * @since 4.9.62.p |
|
| 599 | - * @throws InvalidArgumentException |
|
| 600 | - * @throws InvalidFilePathException |
|
| 601 | - */ |
|
| 602 | - public function registerManifestFiles() |
|
| 603 | - { |
|
| 604 | - $manifest_files = $this->assets->getManifestFiles(); |
|
| 605 | - foreach ($manifest_files as $manifest_file) { |
|
| 606 | - $this->registerManifestFile( |
|
| 607 | - $manifest_file->assetNamespace(), |
|
| 608 | - $manifest_file->urlBase(), |
|
| 609 | - $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST, |
|
| 610 | - $manifest_file->filepath() |
|
| 611 | - ); |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * Used to register a js/css manifest file with the registered_manifest_files property. |
|
| 618 | - * |
|
| 619 | - * @param string $namespace Provided to associate the manifest file with a specific namespace. |
|
| 620 | - * @param string $url_base The url base for the manifest file location. |
|
| 621 | - * @param string $manifest_file The absolute path to the manifest file. |
|
| 622 | - * @param string $manifest_file_path The path to the folder containing the manifest file. If not provided will be |
|
| 623 | - * default to `plugin_root/assets/dist`. |
|
| 624 | - * @throws InvalidArgumentException |
|
| 625 | - * @throws InvalidFilePathException |
|
| 626 | - * @since 4.9.59.p |
|
| 627 | - */ |
|
| 628 | - public function registerManifestFile($namespace, $url_base, $manifest_file, $manifest_file_path = '') |
|
| 629 | - { |
|
| 630 | - if (isset($this->manifest_data[ $namespace ])) { |
|
| 631 | - if (! $this->debug()) { |
|
| 632 | - return; |
|
| 633 | - } |
|
| 634 | - throw new InvalidArgumentException( |
|
| 635 | - sprintf( |
|
| 636 | - esc_html__( |
|
| 637 | - 'The namespace for this manifest file has already been registered, choose a namespace other than %s', |
|
| 638 | - 'event_espresso' |
|
| 639 | - ), |
|
| 640 | - $namespace |
|
| 641 | - ) |
|
| 642 | - ); |
|
| 643 | - } |
|
| 644 | - if (filter_var($url_base, FILTER_VALIDATE_URL) === false) { |
|
| 645 | - if (is_admin()) { |
|
| 646 | - EE_Error::add_error( |
|
| 647 | - sprintf( |
|
| 648 | - esc_html__( |
|
| 649 | - 'The url given for %1$s assets is invalid. The url provided was: "%2$s". This usually happens when another plugin or theme on a site is using the "%3$s" filter or has an invalid url set for the "%4$s" constant', |
|
| 650 | - 'event_espresso' |
|
| 651 | - ), |
|
| 652 | - 'Event Espresso', |
|
| 653 | - $url_base, |
|
| 654 | - 'plugins_url', |
|
| 655 | - 'WP_PLUGIN_URL' |
|
| 656 | - ), |
|
| 657 | - __FILE__, |
|
| 658 | - __FUNCTION__, |
|
| 659 | - __LINE__ |
|
| 660 | - ); |
|
| 661 | - } |
|
| 662 | - return; |
|
| 663 | - } |
|
| 664 | - $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 665 | - if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 666 | - $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 667 | - } |
|
| 668 | - if (! isset($this->manifest_data[ $namespace ]['path'])) { |
|
| 669 | - $this->manifest_data[ $namespace ]['path'] = $manifest_file_path; |
|
| 670 | - } |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * Decodes json from the provided manifest file. |
|
| 676 | - * |
|
| 677 | - * @since 4.9.59.p |
|
| 678 | - * @param string $manifest_file Path to manifest file. |
|
| 679 | - * @return array |
|
| 680 | - * @throws InvalidFilePathException |
|
| 681 | - */ |
|
| 682 | - private function decodeManifestFile($manifest_file) |
|
| 683 | - { |
|
| 684 | - if (! file_exists($manifest_file)) { |
|
| 685 | - throw new InvalidFilePathException($manifest_file); |
|
| 686 | - } |
|
| 687 | - return json_decode(file_get_contents($manifest_file), true); |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - |
|
| 691 | - /** |
|
| 692 | - * This is used to set registered script handles that have data. |
|
| 693 | - * |
|
| 694 | - * @param string $script_handle |
|
| 695 | - */ |
|
| 696 | - private function addRegisteredScriptHandlesWithData($script_handle) |
|
| 697 | - { |
|
| 698 | - $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - |
|
| 702 | - /**i |
|
| 421 | + 'event_espresso' |
|
| 422 | + ), |
|
| 423 | + $key |
|
| 424 | + ) |
|
| 425 | + ); |
|
| 426 | + } |
|
| 427 | + return true; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Get the actual asset path for asset manifests. |
|
| 433 | + * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
|
| 434 | + * |
|
| 435 | + * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
|
| 436 | + * asset file location. |
|
| 437 | + * @param string $chunk_name |
|
| 438 | + * @param string $asset_type |
|
| 439 | + * @return string |
|
| 440 | + * @since 4.9.59.p |
|
| 441 | + */ |
|
| 442 | + public function getAssetUrl($namespace, $chunk_name, $asset_type) |
|
| 443 | + { |
|
| 444 | + $url = isset( |
|
| 445 | + $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
|
| 446 | + $this->manifest_data[ $namespace ]['url_base'] |
|
| 447 | + ) |
|
| 448 | + ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 449 | + . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
|
| 450 | + : $chunk_name; |
|
| 451 | + |
|
| 452 | + return apply_filters( |
|
| 453 | + 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
|
| 454 | + $url, |
|
| 455 | + $namespace, |
|
| 456 | + $chunk_name, |
|
| 457 | + $asset_type |
|
| 458 | + ); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Return the url to a js file for the given namespace and chunk name. |
|
| 465 | + * |
|
| 466 | + * @param string $namespace |
|
| 467 | + * @param string $chunk_name |
|
| 468 | + * @return string |
|
| 469 | + */ |
|
| 470 | + public function getJsUrl($namespace, $chunk_name) |
|
| 471 | + { |
|
| 472 | + return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Return the url to a css file for the given namespace and chunk name. |
|
| 478 | + * |
|
| 479 | + * @param string $namespace |
|
| 480 | + * @param string $chunk_name |
|
| 481 | + * @return string |
|
| 482 | + */ |
|
| 483 | + public function getCssUrl($namespace, $chunk_name) |
|
| 484 | + { |
|
| 485 | + return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Return the dependencies for a given asset $chunk_name |
|
| 491 | + * |
|
| 492 | + * @param string $namespace |
|
| 493 | + * @param string $chunk_name |
|
| 494 | + * @param string $asset_type |
|
| 495 | + * @return array |
|
| 496 | + * @since 4.9.82.p |
|
| 497 | + */ |
|
| 498 | + private function getDependenciesForAsset($namespace, $chunk_name, $asset_type) |
|
| 499 | + { |
|
| 500 | + $asset_index = $chunk_name . '.' . $asset_type; |
|
| 501 | + if (! isset( $this->dependencies_data[ $namespace ][ $asset_index ])) { |
|
| 502 | + $path = isset($this->manifest_data[ $namespace ]['path']) |
|
| 503 | + ? $this->manifest_data[ $namespace ]['path'] |
|
| 504 | + : ''; |
|
| 505 | + $dependencies_index = $chunk_name . '.' . Asset::TYPE_JSON; |
|
| 506 | + $file_path = isset($this->manifest_data[ $namespace ][ $dependencies_index ]) |
|
| 507 | + ? $path . $this->manifest_data[ $namespace ][ $dependencies_index ] |
|
| 508 | + : |
|
| 509 | + ''; |
|
| 510 | + $this->dependencies_data[ $namespace ][ $asset_index ] = $file_path !== '' && file_exists($file_path) |
|
| 511 | + ? $this->getDependenciesForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
|
| 512 | + : []; |
|
| 513 | + } |
|
| 514 | + return $this->dependencies_data[ $namespace ][ $asset_index ]; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Return dependencies according to asset type. |
|
| 520 | + * |
|
| 521 | + * For css assets, this filters the auto generated dependencies by css type. |
|
| 522 | + * |
|
| 523 | + * @param string $namespace |
|
| 524 | + * @param string $asset_type |
|
| 525 | + * @param string $file_path |
|
| 526 | + * @param string $chunk_name |
|
| 527 | + * @return array |
|
| 528 | + * @since 4.9.82.p |
|
| 529 | + */ |
|
| 530 | + private function getDependenciesForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
|
| 531 | + { |
|
| 532 | + $asset_dependencies = json_decode(file_get_contents($file_path), true); |
|
| 533 | + if ($asset_type === Asset::TYPE_JS) { |
|
| 534 | + return $chunk_name === 'eejs-core' ? $asset_dependencies : array_merge( |
|
| 535 | + $asset_dependencies, |
|
| 536 | + [ CoreAssetManager::JS_HANDLE_JS_CORE ] |
|
| 537 | + ); |
|
| 538 | + } |
|
| 539 | + // for css we need to make sure there is actually a css file related to this chunk. |
|
| 540 | + if (isset($this->manifest_data[ $namespace ])) { |
|
| 541 | + // array of css chunk files for ee. |
|
| 542 | + $css_chunks = array_map( |
|
| 543 | + function ($value) { |
|
| 544 | + return str_replace('.css', '', $value); |
|
| 545 | + }, |
|
| 546 | + array_filter( |
|
| 547 | + array_keys($this->manifest_data[ $namespace ]), |
|
| 548 | + function ($value) { |
|
| 549 | + return strpos($value, '.css') !== false; |
|
| 550 | + } |
|
| 551 | + ) |
|
| 552 | + ); |
|
| 553 | + // add known wp chunks with css |
|
| 554 | + $css_chunks = array_merge( $css_chunks, $this->wp_css_handle_dependencies); |
|
| 555 | + // flip for easier search |
|
| 556 | + $css_chunks = array_flip($css_chunks); |
|
| 557 | + // now let's filter the dependencies for the incoming chunk to actual chunks that have styles |
|
| 558 | + return array_filter( |
|
| 559 | + $asset_dependencies, |
|
| 560 | + function ($chunk_name) use ($css_chunks) { |
|
| 561 | + return isset($css_chunks[ $chunk_name ]); |
|
| 562 | + } |
|
| 563 | + ); |
|
| 564 | + } |
|
| 565 | + return []; |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Get the dependencies array for the given js asset chunk name |
|
| 571 | + * |
|
| 572 | + * @param string $namespace |
|
| 573 | + * @param string $chunk_name |
|
| 574 | + * @return array |
|
| 575 | + * @since 4.9.82.p |
|
| 576 | + */ |
|
| 577 | + public function getJsDependencies($namespace, $chunk_name) |
|
| 578 | + { |
|
| 579 | + return $this->getDependenciesForAsset($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Get the dependencies array for the given css asset chunk name |
|
| 585 | + * |
|
| 586 | + * @param string $namespace |
|
| 587 | + * @param string $chunk_name |
|
| 588 | + * @return array |
|
| 589 | + * @since 4.9.82.p |
|
| 590 | + */ |
|
| 591 | + public function getCssDependencies($namespace, $chunk_name) |
|
| 592 | + { |
|
| 593 | + return $this->getDependenciesForAsset($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * @since 4.9.62.p |
|
| 599 | + * @throws InvalidArgumentException |
|
| 600 | + * @throws InvalidFilePathException |
|
| 601 | + */ |
|
| 602 | + public function registerManifestFiles() |
|
| 603 | + { |
|
| 604 | + $manifest_files = $this->assets->getManifestFiles(); |
|
| 605 | + foreach ($manifest_files as $manifest_file) { |
|
| 606 | + $this->registerManifestFile( |
|
| 607 | + $manifest_file->assetNamespace(), |
|
| 608 | + $manifest_file->urlBase(), |
|
| 609 | + $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST, |
|
| 610 | + $manifest_file->filepath() |
|
| 611 | + ); |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * Used to register a js/css manifest file with the registered_manifest_files property. |
|
| 618 | + * |
|
| 619 | + * @param string $namespace Provided to associate the manifest file with a specific namespace. |
|
| 620 | + * @param string $url_base The url base for the manifest file location. |
|
| 621 | + * @param string $manifest_file The absolute path to the manifest file. |
|
| 622 | + * @param string $manifest_file_path The path to the folder containing the manifest file. If not provided will be |
|
| 623 | + * default to `plugin_root/assets/dist`. |
|
| 624 | + * @throws InvalidArgumentException |
|
| 625 | + * @throws InvalidFilePathException |
|
| 626 | + * @since 4.9.59.p |
|
| 627 | + */ |
|
| 628 | + public function registerManifestFile($namespace, $url_base, $manifest_file, $manifest_file_path = '') |
|
| 629 | + { |
|
| 630 | + if (isset($this->manifest_data[ $namespace ])) { |
|
| 631 | + if (! $this->debug()) { |
|
| 632 | + return; |
|
| 633 | + } |
|
| 634 | + throw new InvalidArgumentException( |
|
| 635 | + sprintf( |
|
| 636 | + esc_html__( |
|
| 637 | + 'The namespace for this manifest file has already been registered, choose a namespace other than %s', |
|
| 638 | + 'event_espresso' |
|
| 639 | + ), |
|
| 640 | + $namespace |
|
| 641 | + ) |
|
| 642 | + ); |
|
| 643 | + } |
|
| 644 | + if (filter_var($url_base, FILTER_VALIDATE_URL) === false) { |
|
| 645 | + if (is_admin()) { |
|
| 646 | + EE_Error::add_error( |
|
| 647 | + sprintf( |
|
| 648 | + esc_html__( |
|
| 649 | + 'The url given for %1$s assets is invalid. The url provided was: "%2$s". This usually happens when another plugin or theme on a site is using the "%3$s" filter or has an invalid url set for the "%4$s" constant', |
|
| 650 | + 'event_espresso' |
|
| 651 | + ), |
|
| 652 | + 'Event Espresso', |
|
| 653 | + $url_base, |
|
| 654 | + 'plugins_url', |
|
| 655 | + 'WP_PLUGIN_URL' |
|
| 656 | + ), |
|
| 657 | + __FILE__, |
|
| 658 | + __FUNCTION__, |
|
| 659 | + __LINE__ |
|
| 660 | + ); |
|
| 661 | + } |
|
| 662 | + return; |
|
| 663 | + } |
|
| 664 | + $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 665 | + if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 666 | + $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 667 | + } |
|
| 668 | + if (! isset($this->manifest_data[ $namespace ]['path'])) { |
|
| 669 | + $this->manifest_data[ $namespace ]['path'] = $manifest_file_path; |
|
| 670 | + } |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * Decodes json from the provided manifest file. |
|
| 676 | + * |
|
| 677 | + * @since 4.9.59.p |
|
| 678 | + * @param string $manifest_file Path to manifest file. |
|
| 679 | + * @return array |
|
| 680 | + * @throws InvalidFilePathException |
|
| 681 | + */ |
|
| 682 | + private function decodeManifestFile($manifest_file) |
|
| 683 | + { |
|
| 684 | + if (! file_exists($manifest_file)) { |
|
| 685 | + throw new InvalidFilePathException($manifest_file); |
|
| 686 | + } |
|
| 687 | + return json_decode(file_get_contents($manifest_file), true); |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + |
|
| 691 | + /** |
|
| 692 | + * This is used to set registered script handles that have data. |
|
| 693 | + * |
|
| 694 | + * @param string $script_handle |
|
| 695 | + */ |
|
| 696 | + private function addRegisteredScriptHandlesWithData($script_handle) |
|
| 697 | + { |
|
| 698 | + $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + |
|
| 702 | + /**i |
|
| 703 | 703 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
| 704 | 704 | * Dependency stored in WP_Scripts if its set. |
| 705 | 705 | */ |
| 706 | - private function removeAlreadyRegisteredDataForScriptHandles() |
|
| 707 | - { |
|
| 708 | - if (empty($this->script_handles_with_data)) { |
|
| 709 | - return; |
|
| 710 | - } |
|
| 711 | - foreach ($this->script_handles_with_data as $script_handle) { |
|
| 712 | - $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
| 713 | - } |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - |
|
| 717 | - /** |
|
| 718 | - * Removes any data dependency registered in WP_Scripts if its set. |
|
| 719 | - * |
|
| 720 | - * @param string $script_handle |
|
| 721 | - */ |
|
| 722 | - private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
| 723 | - { |
|
| 724 | - if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 725 | - global $wp_scripts; |
|
| 726 | - $unset_handle = false; |
|
| 727 | - if ($wp_scripts->get_data($script_handle, 'data')) { |
|
| 728 | - unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 729 | - $unset_handle = true; |
|
| 730 | - } |
|
| 731 | - //deal with inline_scripts |
|
| 732 | - if ($wp_scripts->get_data($script_handle, 'before')) { |
|
| 733 | - unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 734 | - $unset_handle = true; |
|
| 735 | - } |
|
| 736 | - if ($wp_scripts->get_data($script_handle, 'after')) { |
|
| 737 | - unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 738 | - } |
|
| 739 | - if ($unset_handle) { |
|
| 740 | - unset($this->script_handles_with_data[ $script_handle ]); |
|
| 741 | - } |
|
| 742 | - } |
|
| 743 | - } |
|
| 744 | - |
|
| 745 | - |
|
| 746 | - /** |
|
| 747 | - * register translations for a registered script |
|
| 748 | - * |
|
| 749 | - * @param string $handle |
|
| 750 | - */ |
|
| 751 | - public function registerTranslation($handle) |
|
| 752 | - { |
|
| 753 | - $this->i18n_registry->registerScriptI18n($handle); |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - |
|
| 757 | - /** |
|
| 758 | - * @since 4.9.63.p |
|
| 759 | - * @return bool |
|
| 760 | - */ |
|
| 761 | - private function debug() |
|
| 762 | - { |
|
| 763 | - return apply_filters( |
|
| 764 | - 'FHEE__EventEspresso_core_services_assets_Registry__debug', |
|
| 765 | - defined('EE_DEBUG') && EE_DEBUG |
|
| 766 | - ); |
|
| 767 | - } |
|
| 706 | + private function removeAlreadyRegisteredDataForScriptHandles() |
|
| 707 | + { |
|
| 708 | + if (empty($this->script_handles_with_data)) { |
|
| 709 | + return; |
|
| 710 | + } |
|
| 711 | + foreach ($this->script_handles_with_data as $script_handle) { |
|
| 712 | + $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
| 713 | + } |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + |
|
| 717 | + /** |
|
| 718 | + * Removes any data dependency registered in WP_Scripts if its set. |
|
| 719 | + * |
|
| 720 | + * @param string $script_handle |
|
| 721 | + */ |
|
| 722 | + private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
| 723 | + { |
|
| 724 | + if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 725 | + global $wp_scripts; |
|
| 726 | + $unset_handle = false; |
|
| 727 | + if ($wp_scripts->get_data($script_handle, 'data')) { |
|
| 728 | + unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 729 | + $unset_handle = true; |
|
| 730 | + } |
|
| 731 | + //deal with inline_scripts |
|
| 732 | + if ($wp_scripts->get_data($script_handle, 'before')) { |
|
| 733 | + unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 734 | + $unset_handle = true; |
|
| 735 | + } |
|
| 736 | + if ($wp_scripts->get_data($script_handle, 'after')) { |
|
| 737 | + unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 738 | + } |
|
| 739 | + if ($unset_handle) { |
|
| 740 | + unset($this->script_handles_with_data[ $script_handle ]); |
|
| 741 | + } |
|
| 742 | + } |
|
| 743 | + } |
|
| 744 | + |
|
| 745 | + |
|
| 746 | + /** |
|
| 747 | + * register translations for a registered script |
|
| 748 | + * |
|
| 749 | + * @param string $handle |
|
| 750 | + */ |
|
| 751 | + public function registerTranslation($handle) |
|
| 752 | + { |
|
| 753 | + $this->i18n_registry->registerScriptI18n($handle); |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + |
|
| 757 | + /** |
|
| 758 | + * @since 4.9.63.p |
|
| 759 | + * @return bool |
|
| 760 | + */ |
|
| 761 | + private function debug() |
|
| 762 | + { |
|
| 763 | + return apply_filters( |
|
| 764 | + 'FHEE__EventEspresso_core_services_assets_Registry__debug', |
|
| 765 | + defined('EE_DEBUG') && EE_DEBUG |
|
| 766 | + ); |
|
| 767 | + } |
|
| 768 | 768 | } |
@@ -7,39 +7,39 @@ discard block |
||
| 7 | 7 | if ($display_ticket_price) { ?> |
| 8 | 8 | <section class="tckt-slctr-tkt-price-sctn"> |
| 9 | 9 | <h5><?php |
| 10 | - echo apply_filters( |
|
| 11 | - 'FHEE__ticket_selector_chart_template__ticket_details_price_breakdown_heading', |
|
| 12 | - esc_html__('Price', 'event_espresso') |
|
| 13 | - ); ?></h5> |
|
| 10 | + echo apply_filters( |
|
| 11 | + 'FHEE__ticket_selector_chart_template__ticket_details_price_breakdown_heading', |
|
| 12 | + esc_html__('Price', 'event_espresso') |
|
| 13 | + ); ?></h5> |
|
| 14 | 14 | <div class="tckt-slctr-tkt-details-tbl-wrap-dv"> |
| 15 | 15 | <table class="tckt-slctr-tkt-details-tbl"> |
| 16 | 16 | <thead> |
| 17 | 17 | <tr> |
| 18 | 18 | <th class="ee-third-width"><span class="small-text"> |
| 19 | 19 | <?php |
| 20 | - esc_html_e( |
|
| 21 | - 'Name', |
|
| 22 | - 'event_espresso' |
|
| 23 | - ); ?></span> |
|
| 20 | + esc_html_e( |
|
| 21 | + 'Name', |
|
| 22 | + 'event_espresso' |
|
| 23 | + ); ?></span> |
|
| 24 | 24 | </th> |
| 25 | 25 | <th class="jst-cntr"><span class="small-text"> |
| 26 | 26 | <?php |
| 27 | - esc_html_e( |
|
| 28 | - 'Description', |
|
| 29 | - 'event_espresso' |
|
| 30 | - ); ?></span> |
|
| 27 | + esc_html_e( |
|
| 28 | + 'Description', |
|
| 29 | + 'event_espresso' |
|
| 30 | + ); ?></span> |
|
| 31 | 31 | </th> |
| 32 | 32 | <th class="ee-fourth-width jst-rght"><span class="small-text"> |
| 33 | 33 | <?php |
| 34 | - esc_html_e( |
|
| 35 | - 'Amount', |
|
| 36 | - 'event_espresso' |
|
| 37 | - ); ?></span></th> |
|
| 34 | + esc_html_e( |
|
| 35 | + 'Amount', |
|
| 36 | + 'event_espresso' |
|
| 37 | + ); ?></span></th> |
|
| 38 | 38 | </tr> |
| 39 | 39 | </thead> |
| 40 | 40 | <tbody> |
| 41 | 41 | <?php |
| 42 | - if ($ticket->base_price() instanceof EE_Price) { ?> |
|
| 42 | + if ($ticket->base_price() instanceof EE_Price) { ?> |
|
| 43 | 43 | <tr> |
| 44 | 44 | <td data-th="<?php esc_html_e('Name', 'event_espresso'); ?>" class="small-text" colspan="2"> |
| 45 | 45 | <b><?php echo $ticket->base_price()->name(); ?></b></td> |
@@ -47,12 +47,12 @@ discard block |
||
| 47 | 47 | class="jst-rght small-text"><?php echo $ticket->base_price()->pretty_price(); ?></td> |
| 48 | 48 | </tr> |
| 49 | 49 | <?php |
| 50 | - $running_total = $ticket->base_price()->amount(); |
|
| 51 | - } else { |
|
| 52 | - $running_total = 0; |
|
| 53 | - } |
|
| 54 | - // now add price modifiers |
|
| 55 | - foreach ($ticket->price_modifiers() as $price_mod) { ?> |
|
| 50 | + $running_total = $ticket->base_price()->amount(); |
|
| 51 | + } else { |
|
| 52 | + $running_total = 0; |
|
| 53 | + } |
|
| 54 | + // now add price modifiers |
|
| 55 | + foreach ($ticket->price_modifiers() as $price_mod) { ?> |
|
| 56 | 56 | <tr> |
| 57 | 57 | <td data-th="<?php esc_html_e('Name', 'event_espresso'); ?>" |
| 58 | 58 | class="jst-rght small-text"><?php echo $price_mod->name(); ?></td> |
@@ -62,23 +62,23 @@ discard block |
||
| 62 | 62 | % |
| 63 | 63 | </td> |
| 64 | 64 | <?php |
| 65 | - $new_sub_total = $running_total * ($price_mod->amount() / 100); |
|
| 66 | - $new_sub_total = $price_mod->is_discount() ? $new_sub_total * -1 : $new_sub_total; |
|
| 67 | - ?> |
|
| 65 | + $new_sub_total = $running_total * ($price_mod->amount() / 100); |
|
| 66 | + $new_sub_total = $price_mod->is_discount() ? $new_sub_total * -1 : $new_sub_total; |
|
| 67 | + ?> |
|
| 68 | 68 | <?php } else { ?> |
| 69 | 69 | <?php $new_sub_total = $price_mod->is_discount() ? $price_mod->amount() * -1 |
| 70 | - : $price_mod->amount(); ?> |
|
| 70 | + : $price_mod->amount(); ?> |
|
| 71 | 71 | <td data-th="<?php esc_html_e('Description', 'event_espresso'); ?>" |
| 72 | 72 | class="small-text"><?php echo $price_mod->desc(); ?></td> |
| 73 | 73 | <?php $new_sub_total = $price_mod->is_discount() ? $price_mod->amount() * -1 |
| 74 | - : $price_mod->amount(); ?> |
|
| 74 | + : $price_mod->amount(); ?> |
|
| 75 | 75 | <?php } ?> |
| 76 | 76 | <td data-th="<?php esc_html_e('Amount', 'event_espresso'); ?>" |
| 77 | 77 | class="jst-rght small-text"> |
| 78 | 78 | <?php |
| 79 | - echo EEH_Template::format_currency( |
|
| 80 | - $new_sub_total |
|
| 81 | - ); ?></td> |
|
| 79 | + echo EEH_Template::format_currency( |
|
| 80 | + $new_sub_total |
|
| 81 | + ); ?></td> |
|
| 82 | 82 | <?php $running_total += $new_sub_total; ?> |
| 83 | 83 | </tr> |
| 84 | 84 | <?php } ?> |
@@ -87,10 +87,10 @@ discard block |
||
| 87 | 87 | <tr> |
| 88 | 88 | <td colspan="2" class="jst-rght small-text sbttl"><b> |
| 89 | 89 | <?php |
| 90 | - esc_html_e( |
|
| 91 | - 'subtotal', |
|
| 92 | - 'event_espresso' |
|
| 93 | - ); ?></b></td> |
|
| 90 | + esc_html_e( |
|
| 91 | + 'subtotal', |
|
| 92 | + 'event_espresso' |
|
| 93 | + ); ?></b></td> |
|
| 94 | 94 | <td data-th="<?php esc_html_e('subtotal', 'event_espresso'); ?>" class="jst-rght small-text"> |
| 95 | 95 | <b><?php echo EEH_Template::format_currency($running_total); ?></b></td> |
| 96 | 96 | </tr> |
@@ -106,9 +106,9 @@ discard block |
||
| 106 | 106 | <td data-th="<?php esc_html_e('Amount', 'event_espresso'); ?>" |
| 107 | 107 | class="jst-rght small-text"> |
| 108 | 108 | <?php |
| 109 | - echo EEH_Template::format_currency( |
|
| 110 | - $tax_amount |
|
| 111 | - ); ?></td> |
|
| 109 | + echo EEH_Template::format_currency( |
|
| 110 | + $tax_amount |
|
| 111 | + ); ?></td> |
|
| 112 | 112 | <?php $running_total += $tax_amount; ?> |
| 113 | 113 | </tr> |
| 114 | 114 | <?php } ?> |
@@ -116,15 +116,15 @@ discard block |
||
| 116 | 116 | <tr> |
| 117 | 117 | <td colspan="2" class="jst-rght small-text ttl-lbl-td"> |
| 118 | 118 | <b><?php |
| 119 | - echo apply_filters( |
|
| 120 | - 'FHEE__ticket_selector_chart_template__ticket_details_total_price', |
|
| 121 | - esc_html__('Total', 'event_espresso') |
|
| 122 | - ); ?></b></td> |
|
| 119 | + echo apply_filters( |
|
| 120 | + 'FHEE__ticket_selector_chart_template__ticket_details_total_price', |
|
| 121 | + esc_html__('Total', 'event_espresso') |
|
| 122 | + ); ?></b></td> |
|
| 123 | 123 | <td data-th="<?php |
| 124 | - echo apply_filters( |
|
| 125 | - 'FHEE__ticket_selector_chart_template__ticket_details_total_price', |
|
| 126 | - esc_html__('Total', 'event_espresso') |
|
| 127 | - ); ?>" class="jst-rght small-text"> |
|
| 124 | + echo apply_filters( |
|
| 125 | + 'FHEE__ticket_selector_chart_template__ticket_details_total_price', |
|
| 126 | + esc_html__('Total', 'event_espresso') |
|
| 127 | + ); ?>" class="jst-rght small-text"> |
|
| 128 | 128 | <b><?php echo EEH_Template::format_currency($running_total); ?></b></td> |
| 129 | 129 | </tr> |
| 130 | 130 | </tbody> |
@@ -12,276 +12,276 @@ |
||
| 12 | 12 | { |
| 13 | 13 | |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * generates JSON-based linked data for an event |
|
| 17 | - * |
|
| 18 | - * @param EE_Event $event |
|
| 19 | - * @throws EE_Error |
|
| 20 | - */ |
|
| 21 | - public static function add_json_linked_data_for_event(EE_Event $event) |
|
| 22 | - { |
|
| 23 | - // Check we have a valid datetime for the event |
|
| 24 | - if (! $event->primary_datetime() instanceof EE_Datetime) { |
|
| 25 | - return; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - $template_args = array( |
|
| 29 | - 'event_permalink' => '', |
|
| 30 | - 'event_name' => '', |
|
| 31 | - 'event_description' => '', |
|
| 32 | - 'event_start' => '', |
|
| 33 | - 'event_end' => '', |
|
| 34 | - 'currency' => '', |
|
| 35 | - 'event_tickets' => array(), |
|
| 36 | - 'venue_name' => '', |
|
| 37 | - 'venue_url' => '', |
|
| 38 | - 'venue_locality' => '', |
|
| 39 | - 'venue_region' => '', |
|
| 40 | - 'venue_address' => '', |
|
| 41 | - 'event_image' => '', |
|
| 42 | - ); |
|
| 43 | - $template_args['event_permalink'] = $event->get_permalink(); |
|
| 44 | - $template_args['event_name'] = $event->name(); |
|
| 45 | - $template_args['event_description'] = wp_strip_all_tags($event->short_description(200)); |
|
| 46 | - // clone datetime so that date formats don't override those for the original datetime |
|
| 47 | - $primary_datetime = clone $event->primary_datetime(); |
|
| 48 | - $template_args['event_start'] = $primary_datetime->start_date(DateTime::ATOM); |
|
| 49 | - $template_args['event_end'] = $primary_datetime->end_date(DateTime::ATOM); |
|
| 50 | - unset($primary_datetime); |
|
| 51 | - $template_args['currency'] = EE_Registry::instance()->CFG->currency->code; |
|
| 52 | - foreach ($event->tickets() as $original_ticket) { |
|
| 53 | - // clone tickets so that date formats don't override those for the original ticket |
|
| 54 | - $ticket= clone $original_ticket; |
|
| 55 | - $ID = $ticket->ID(); |
|
| 56 | - $template_args['event_tickets'][ $ID ]['start_date'] = $ticket->start_date(DateTime::ATOM, null); |
|
| 57 | - $template_args['event_tickets'][ $ID ]['end_date'] = $ticket->end_date(DateTime::ATOM, null); |
|
| 58 | - $template_args['event_tickets'][ $ID ]['price'] = number_format( |
|
| 59 | - $ticket->price(), |
|
| 60 | - EE_Registry::instance()->CFG->currency->dec_plc, |
|
| 61 | - EE_Registry::instance()->CFG->currency->dec_mrk, |
|
| 62 | - EE_Registry::instance()->CFG->currency->thsnds |
|
| 63 | - ); |
|
| 64 | - switch ($ticket->ticket_status()) { |
|
| 65 | - case 'TKO': |
|
| 66 | - $availability = 'InStock'; |
|
| 67 | - break; |
|
| 68 | - case 'TKS': |
|
| 69 | - $availability = 'SoldOut'; |
|
| 70 | - break; |
|
| 71 | - default: |
|
| 72 | - $availability = null; |
|
| 73 | - break; |
|
| 74 | - } |
|
| 75 | - $template_args['event_tickets'][ $ID ]['availability'] = $availability; |
|
| 76 | - unset($ticket); |
|
| 77 | - } |
|
| 78 | - $VNU_ID = espresso_venue_id(); |
|
| 79 | - if (! empty($VNU_ID) && ! espresso_is_venue_private($VNU_ID)) { |
|
| 80 | - $venue = EEH_Venue_View::get_venue($VNU_ID); |
|
| 81 | - $template_args['venue_name'] = get_the_title($VNU_ID); |
|
| 82 | - $template_args['venue_url'] = get_permalink($VNU_ID); |
|
| 83 | - $template_args['venue_locality'] = $venue->city(); |
|
| 84 | - $template_args['venue_region'] = $venue->state_name(); |
|
| 85 | - $template_args['venue_address'] = $venue->address(); |
|
| 86 | - } |
|
| 87 | - $template_args['event_image'] = $event->feature_image_url(); |
|
| 88 | - $template_args = apply_filters( |
|
| 89 | - 'FHEE__EEH_Schema__add_json_linked_data_for_event__template_args', |
|
| 90 | - $template_args, |
|
| 91 | - $event, |
|
| 92 | - $VNU_ID |
|
| 93 | - ); |
|
| 94 | - extract($template_args, EXTR_OVERWRITE); |
|
| 95 | - include EE_TEMPLATES . 'json_linked_data_for_event.template.php'; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * location |
|
| 101 | - * The location of the event, organization or action. |
|
| 102 | - * Should include the Venue name AND schema formatted address info |
|
| 103 | - * |
|
| 104 | - * @access public |
|
| 105 | - * @param string $location |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public static function location($location = null) |
|
| 109 | - { |
|
| 110 | - return ! empty($location) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">' |
|
| 111 | - . $location |
|
| 112 | - . '</div>' : ''; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * name |
|
| 119 | - * The name of the Event or Venue. |
|
| 120 | - * |
|
| 121 | - * @access public |
|
| 122 | - * @param string $name |
|
| 123 | - * @return string |
|
| 124 | - */ |
|
| 125 | - public static function name($name = null) |
|
| 126 | - { |
|
| 127 | - return ! empty($name) ? '<span itemprop="name">' . $name . '</span>' : ''; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * streetAddress |
|
| 134 | - * The street address. For example, 1600 Amphitheatre Pkwy. |
|
| 135 | - * |
|
| 136 | - * @access public |
|
| 137 | - * @param EEI_Address $obj_with_address |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - public static function streetAddress(EEI_Address $obj_with_address = null) |
|
| 141 | - { |
|
| 142 | - return $obj_with_address->address() !== null && $obj_with_address->address() !== '' |
|
| 143 | - ? '<span itemprop="streetAddress">' . $obj_with_address->address() . '</span>' : ''; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * postOfficeBoxNumber |
|
| 150 | - * The post office box number for PO box addresses. |
|
| 151 | - * |
|
| 152 | - * @access public |
|
| 153 | - * @param EEI_Address $obj_with_address |
|
| 154 | - * @return string |
|
| 155 | - */ |
|
| 156 | - public static function postOfficeBoxNumber(EEI_Address $obj_with_address = null) |
|
| 157 | - { |
|
| 158 | - // regex check for some form of PO Box or P.O. Box, etc, etc, etc |
|
| 159 | - if (preg_match( |
|
| 160 | - "/^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i", |
|
| 161 | - $obj_with_address->address2() |
|
| 162 | - ) ) { |
|
| 163 | - return $obj_with_address->address2() !== null && $obj_with_address->address2() !== '' |
|
| 164 | - ? '<span itemprop="postOfficeBoxNumber">' . $obj_with_address->address2() . '</span>' : ''; |
|
| 165 | - } else { |
|
| 166 | - return $obj_with_address->address2(); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * addressLocality |
|
| 174 | - * The locality (city, town, etc). For example, Mountain View. |
|
| 175 | - * |
|
| 176 | - * @access public |
|
| 177 | - * @param EEI_Address $obj_with_address |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - public static function addressLocality(EEI_Address $obj_with_address = null) |
|
| 181 | - { |
|
| 182 | - return $obj_with_address->city() !== null && $obj_with_address->city() !== '' |
|
| 183 | - ? '<span itemprop="addressLocality">' . $obj_with_address->city() . '</span>' : ''; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * addressRegion |
|
| 190 | - * The region (state, province, etc). For example, CA. |
|
| 191 | - * |
|
| 192 | - * @access public |
|
| 193 | - * @param EEI_Address $obj_with_address |
|
| 194 | - * @return string |
|
| 195 | - */ |
|
| 196 | - public static function addressRegion(EEI_Address $obj_with_address = null) |
|
| 197 | - { |
|
| 198 | - $state = $obj_with_address->state_name(); |
|
| 199 | - if (! empty($state)) { |
|
| 200 | - return '<span itemprop="addressRegion">' . $state . '</span>'; |
|
| 201 | - } else { |
|
| 202 | - return ''; |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * addressCountry |
|
| 210 | - * The country. For example, USA. You can also provide the two-letter ISO 3166-1 alpha-2 country code. |
|
| 211 | - * |
|
| 212 | - * @access public |
|
| 213 | - * @param EEI_Address $obj_with_address |
|
| 214 | - * @return string |
|
| 215 | - */ |
|
| 216 | - public static function addressCountry(EEI_Address $obj_with_address = null) |
|
| 217 | - { |
|
| 218 | - $country = $obj_with_address->country_name(); |
|
| 219 | - if (! empty($country)) { |
|
| 220 | - return '<span itemprop="addressCountry">' . $country . '</span>'; |
|
| 221 | - } else { |
|
| 222 | - return ''; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * postalCode |
|
| 230 | - * The postal code. For example, 94043. |
|
| 231 | - * |
|
| 232 | - * @access public |
|
| 233 | - * @param EEI_Address $obj_with_address |
|
| 234 | - * @return string |
|
| 235 | - */ |
|
| 236 | - public static function postalCode(EEI_Address $obj_with_address = null) |
|
| 237 | - { |
|
| 238 | - return $obj_with_address->zip() !== null && $obj_with_address->zip() !== '' ? '<span itemprop="postalCode">' |
|
| 239 | - . $obj_with_address->zip() |
|
| 240 | - . '</span>' : ''; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * telephone |
|
| 247 | - * The telephone number. |
|
| 248 | - * |
|
| 249 | - * @access public |
|
| 250 | - * @param string $phone_nmbr |
|
| 251 | - * @return string |
|
| 252 | - */ |
|
| 253 | - public static function telephone($phone_nmbr = null) |
|
| 254 | - { |
|
| 255 | - return $phone_nmbr !== null && $phone_nmbr !== '' ? '<span itemprop="telephone">' . $phone_nmbr . '</span>' |
|
| 256 | - : ''; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * URL |
|
| 263 | - * URL of the item as a clickable link |
|
| 264 | - * |
|
| 265 | - * @access public |
|
| 266 | - * @param string $url - the URL that the link will resolve to |
|
| 267 | - * @param string $text - the text that will be used for the visible link |
|
| 268 | - * @param array $attributes - array of additional link attributes in attribute_name => value pairs. ie: array( 'title' => 'click here', 'class' => 'link-class' ) |
|
| 269 | - * @return string (link) |
|
| 270 | - */ |
|
| 271 | - public static function url($url = null, $text = null, $attributes = array()) |
|
| 272 | - { |
|
| 273 | - // Check the URL includes a scheme |
|
| 274 | - $parsed_url = parse_url($url); |
|
| 275 | - if (empty($parsed_url['scheme'])) { |
|
| 276 | - $url = 'http://' . ltrim($url, '/'); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - $atts = ''; |
|
| 280 | - foreach ($attributes as $attribute => $value) { |
|
| 281 | - $atts .= ' ' . $attribute . '="' . $value . '"'; |
|
| 282 | - } |
|
| 283 | - $text = $text !== null && $text !== '' ? $text : $url; |
|
| 284 | - return $url !== null && $url !== '' ? '<a itemprop="url" href="' . $url . '"' . $atts . '>' . $text . '</a>' |
|
| 285 | - : ''; |
|
| 286 | - } |
|
| 15 | + /** |
|
| 16 | + * generates JSON-based linked data for an event |
|
| 17 | + * |
|
| 18 | + * @param EE_Event $event |
|
| 19 | + * @throws EE_Error |
|
| 20 | + */ |
|
| 21 | + public static function add_json_linked_data_for_event(EE_Event $event) |
|
| 22 | + { |
|
| 23 | + // Check we have a valid datetime for the event |
|
| 24 | + if (! $event->primary_datetime() instanceof EE_Datetime) { |
|
| 25 | + return; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + $template_args = array( |
|
| 29 | + 'event_permalink' => '', |
|
| 30 | + 'event_name' => '', |
|
| 31 | + 'event_description' => '', |
|
| 32 | + 'event_start' => '', |
|
| 33 | + 'event_end' => '', |
|
| 34 | + 'currency' => '', |
|
| 35 | + 'event_tickets' => array(), |
|
| 36 | + 'venue_name' => '', |
|
| 37 | + 'venue_url' => '', |
|
| 38 | + 'venue_locality' => '', |
|
| 39 | + 'venue_region' => '', |
|
| 40 | + 'venue_address' => '', |
|
| 41 | + 'event_image' => '', |
|
| 42 | + ); |
|
| 43 | + $template_args['event_permalink'] = $event->get_permalink(); |
|
| 44 | + $template_args['event_name'] = $event->name(); |
|
| 45 | + $template_args['event_description'] = wp_strip_all_tags($event->short_description(200)); |
|
| 46 | + // clone datetime so that date formats don't override those for the original datetime |
|
| 47 | + $primary_datetime = clone $event->primary_datetime(); |
|
| 48 | + $template_args['event_start'] = $primary_datetime->start_date(DateTime::ATOM); |
|
| 49 | + $template_args['event_end'] = $primary_datetime->end_date(DateTime::ATOM); |
|
| 50 | + unset($primary_datetime); |
|
| 51 | + $template_args['currency'] = EE_Registry::instance()->CFG->currency->code; |
|
| 52 | + foreach ($event->tickets() as $original_ticket) { |
|
| 53 | + // clone tickets so that date formats don't override those for the original ticket |
|
| 54 | + $ticket= clone $original_ticket; |
|
| 55 | + $ID = $ticket->ID(); |
|
| 56 | + $template_args['event_tickets'][ $ID ]['start_date'] = $ticket->start_date(DateTime::ATOM, null); |
|
| 57 | + $template_args['event_tickets'][ $ID ]['end_date'] = $ticket->end_date(DateTime::ATOM, null); |
|
| 58 | + $template_args['event_tickets'][ $ID ]['price'] = number_format( |
|
| 59 | + $ticket->price(), |
|
| 60 | + EE_Registry::instance()->CFG->currency->dec_plc, |
|
| 61 | + EE_Registry::instance()->CFG->currency->dec_mrk, |
|
| 62 | + EE_Registry::instance()->CFG->currency->thsnds |
|
| 63 | + ); |
|
| 64 | + switch ($ticket->ticket_status()) { |
|
| 65 | + case 'TKO': |
|
| 66 | + $availability = 'InStock'; |
|
| 67 | + break; |
|
| 68 | + case 'TKS': |
|
| 69 | + $availability = 'SoldOut'; |
|
| 70 | + break; |
|
| 71 | + default: |
|
| 72 | + $availability = null; |
|
| 73 | + break; |
|
| 74 | + } |
|
| 75 | + $template_args['event_tickets'][ $ID ]['availability'] = $availability; |
|
| 76 | + unset($ticket); |
|
| 77 | + } |
|
| 78 | + $VNU_ID = espresso_venue_id(); |
|
| 79 | + if (! empty($VNU_ID) && ! espresso_is_venue_private($VNU_ID)) { |
|
| 80 | + $venue = EEH_Venue_View::get_venue($VNU_ID); |
|
| 81 | + $template_args['venue_name'] = get_the_title($VNU_ID); |
|
| 82 | + $template_args['venue_url'] = get_permalink($VNU_ID); |
|
| 83 | + $template_args['venue_locality'] = $venue->city(); |
|
| 84 | + $template_args['venue_region'] = $venue->state_name(); |
|
| 85 | + $template_args['venue_address'] = $venue->address(); |
|
| 86 | + } |
|
| 87 | + $template_args['event_image'] = $event->feature_image_url(); |
|
| 88 | + $template_args = apply_filters( |
|
| 89 | + 'FHEE__EEH_Schema__add_json_linked_data_for_event__template_args', |
|
| 90 | + $template_args, |
|
| 91 | + $event, |
|
| 92 | + $VNU_ID |
|
| 93 | + ); |
|
| 94 | + extract($template_args, EXTR_OVERWRITE); |
|
| 95 | + include EE_TEMPLATES . 'json_linked_data_for_event.template.php'; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * location |
|
| 101 | + * The location of the event, organization or action. |
|
| 102 | + * Should include the Venue name AND schema formatted address info |
|
| 103 | + * |
|
| 104 | + * @access public |
|
| 105 | + * @param string $location |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public static function location($location = null) |
|
| 109 | + { |
|
| 110 | + return ! empty($location) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">' |
|
| 111 | + . $location |
|
| 112 | + . '</div>' : ''; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * name |
|
| 119 | + * The name of the Event or Venue. |
|
| 120 | + * |
|
| 121 | + * @access public |
|
| 122 | + * @param string $name |
|
| 123 | + * @return string |
|
| 124 | + */ |
|
| 125 | + public static function name($name = null) |
|
| 126 | + { |
|
| 127 | + return ! empty($name) ? '<span itemprop="name">' . $name . '</span>' : ''; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * streetAddress |
|
| 134 | + * The street address. For example, 1600 Amphitheatre Pkwy. |
|
| 135 | + * |
|
| 136 | + * @access public |
|
| 137 | + * @param EEI_Address $obj_with_address |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + public static function streetAddress(EEI_Address $obj_with_address = null) |
|
| 141 | + { |
|
| 142 | + return $obj_with_address->address() !== null && $obj_with_address->address() !== '' |
|
| 143 | + ? '<span itemprop="streetAddress">' . $obj_with_address->address() . '</span>' : ''; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * postOfficeBoxNumber |
|
| 150 | + * The post office box number for PO box addresses. |
|
| 151 | + * |
|
| 152 | + * @access public |
|
| 153 | + * @param EEI_Address $obj_with_address |
|
| 154 | + * @return string |
|
| 155 | + */ |
|
| 156 | + public static function postOfficeBoxNumber(EEI_Address $obj_with_address = null) |
|
| 157 | + { |
|
| 158 | + // regex check for some form of PO Box or P.O. Box, etc, etc, etc |
|
| 159 | + if (preg_match( |
|
| 160 | + "/^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i", |
|
| 161 | + $obj_with_address->address2() |
|
| 162 | + ) ) { |
|
| 163 | + return $obj_with_address->address2() !== null && $obj_with_address->address2() !== '' |
|
| 164 | + ? '<span itemprop="postOfficeBoxNumber">' . $obj_with_address->address2() . '</span>' : ''; |
|
| 165 | + } else { |
|
| 166 | + return $obj_with_address->address2(); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * addressLocality |
|
| 174 | + * The locality (city, town, etc). For example, Mountain View. |
|
| 175 | + * |
|
| 176 | + * @access public |
|
| 177 | + * @param EEI_Address $obj_with_address |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + public static function addressLocality(EEI_Address $obj_with_address = null) |
|
| 181 | + { |
|
| 182 | + return $obj_with_address->city() !== null && $obj_with_address->city() !== '' |
|
| 183 | + ? '<span itemprop="addressLocality">' . $obj_with_address->city() . '</span>' : ''; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * addressRegion |
|
| 190 | + * The region (state, province, etc). For example, CA. |
|
| 191 | + * |
|
| 192 | + * @access public |
|
| 193 | + * @param EEI_Address $obj_with_address |
|
| 194 | + * @return string |
|
| 195 | + */ |
|
| 196 | + public static function addressRegion(EEI_Address $obj_with_address = null) |
|
| 197 | + { |
|
| 198 | + $state = $obj_with_address->state_name(); |
|
| 199 | + if (! empty($state)) { |
|
| 200 | + return '<span itemprop="addressRegion">' . $state . '</span>'; |
|
| 201 | + } else { |
|
| 202 | + return ''; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * addressCountry |
|
| 210 | + * The country. For example, USA. You can also provide the two-letter ISO 3166-1 alpha-2 country code. |
|
| 211 | + * |
|
| 212 | + * @access public |
|
| 213 | + * @param EEI_Address $obj_with_address |
|
| 214 | + * @return string |
|
| 215 | + */ |
|
| 216 | + public static function addressCountry(EEI_Address $obj_with_address = null) |
|
| 217 | + { |
|
| 218 | + $country = $obj_with_address->country_name(); |
|
| 219 | + if (! empty($country)) { |
|
| 220 | + return '<span itemprop="addressCountry">' . $country . '</span>'; |
|
| 221 | + } else { |
|
| 222 | + return ''; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * postalCode |
|
| 230 | + * The postal code. For example, 94043. |
|
| 231 | + * |
|
| 232 | + * @access public |
|
| 233 | + * @param EEI_Address $obj_with_address |
|
| 234 | + * @return string |
|
| 235 | + */ |
|
| 236 | + public static function postalCode(EEI_Address $obj_with_address = null) |
|
| 237 | + { |
|
| 238 | + return $obj_with_address->zip() !== null && $obj_with_address->zip() !== '' ? '<span itemprop="postalCode">' |
|
| 239 | + . $obj_with_address->zip() |
|
| 240 | + . '</span>' : ''; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * telephone |
|
| 247 | + * The telephone number. |
|
| 248 | + * |
|
| 249 | + * @access public |
|
| 250 | + * @param string $phone_nmbr |
|
| 251 | + * @return string |
|
| 252 | + */ |
|
| 253 | + public static function telephone($phone_nmbr = null) |
|
| 254 | + { |
|
| 255 | + return $phone_nmbr !== null && $phone_nmbr !== '' ? '<span itemprop="telephone">' . $phone_nmbr . '</span>' |
|
| 256 | + : ''; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * URL |
|
| 263 | + * URL of the item as a clickable link |
|
| 264 | + * |
|
| 265 | + * @access public |
|
| 266 | + * @param string $url - the URL that the link will resolve to |
|
| 267 | + * @param string $text - the text that will be used for the visible link |
|
| 268 | + * @param array $attributes - array of additional link attributes in attribute_name => value pairs. ie: array( 'title' => 'click here', 'class' => 'link-class' ) |
|
| 269 | + * @return string (link) |
|
| 270 | + */ |
|
| 271 | + public static function url($url = null, $text = null, $attributes = array()) |
|
| 272 | + { |
|
| 273 | + // Check the URL includes a scheme |
|
| 274 | + $parsed_url = parse_url($url); |
|
| 275 | + if (empty($parsed_url['scheme'])) { |
|
| 276 | + $url = 'http://' . ltrim($url, '/'); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + $atts = ''; |
|
| 280 | + foreach ($attributes as $attribute => $value) { |
|
| 281 | + $atts .= ' ' . $attribute . '="' . $value . '"'; |
|
| 282 | + } |
|
| 283 | + $text = $text !== null && $text !== '' ? $text : $url; |
|
| 284 | + return $url !== null && $url !== '' ? '<a itemprop="url" href="' . $url . '"' . $atts . '>' . $text . '</a>' |
|
| 285 | + : ''; |
|
| 286 | + } |
|
| 287 | 287 | } |
@@ -24,8 +24,8 @@ discard block |
||
| 24 | 24 | "url": "<?php echo $event_permalink; ?>", |
| 25 | 25 | "offers": [ |
| 26 | 26 | <?php |
| 27 | - $i = 0; |
|
| 28 | - foreach ($event_tickets as $ticket) {?> |
|
| 27 | + $i = 0; |
|
| 28 | + foreach ($event_tickets as $ticket) {?> |
|
| 29 | 29 | { |
| 30 | 30 | "@type": "Offer", |
| 31 | 31 | "url": "<?php echo $event_permalink; ?>", |
@@ -34,18 +34,18 @@ discard block |
||
| 34 | 34 | "price": "<?php echo $ticket['price']; ?>", |
| 35 | 35 | "priceCurrency": "<?php echo $currency; ?>" |
| 36 | 36 | <?php if (isset($ticket['availability'])) { |
| 37 | - ?>,"availability": "http://schema.org/<?php echo $ticket['availability']; ?>" |
|
| 37 | + ?>,"availability": "http://schema.org/<?php echo $ticket['availability']; ?>" |
|
| 38 | 38 | <?php } ?> |
| 39 | 39 | }<?php |
| 40 | - $i++; |
|
| 41 | - if ($i < count($event_tickets)) { |
|
| 42 | - echo ','; |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - ?> |
|
| 40 | + $i++; |
|
| 41 | + if ($i < count($event_tickets)) { |
|
| 42 | + echo ','; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + ?> |
|
| 46 | 46 | ]<?php |
| 47 | - if ($venue_name) { |
|
| 48 | - ?>, |
|
| 47 | + if ($venue_name) { |
|
| 48 | + ?>, |
|
| 49 | 49 | "location": { |
| 50 | 50 | "@type": "Place", |
| 51 | 51 | "name": <?php echo wp_json_encode($venue_name); ?>, |
@@ -58,13 +58,13 @@ discard block |
||
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | <?php |
| 61 | - } ?> |
|
| 61 | + } ?> |
|
| 62 | 62 | <?php |
| 63 | - if ($event_image) { |
|
| 64 | - ?>, |
|
| 63 | + if ($event_image) { |
|
| 64 | + ?>, |
|
| 65 | 65 | "image": "<?php echo $event_image; ?>" |
| 66 | 66 | <?php |
| 67 | - } ?> |
|
| 67 | + } ?> |
|
| 68 | 68 | <?php do_action('AHEE__json_linked_data_for_event__template'); ?> |
| 69 | 69 | } |
| 70 | 70 | |
@@ -1,6 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use EventEspresso\core\services\formatters\AsciiOnly; |
|
| 4 | 3 | use EventEspresso\core\services\loaders\LoaderFactory; |
| 5 | 4 | |
| 6 | 5 | /** |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | $line_item->unit_price(), |
| 217 | 217 | 'N' |
| 218 | 218 | ); |
| 219 | - $order_description .= $this->prepareStringForAuthnet($line_item->desc()) . ', '; |
|
| 219 | + $order_description .= $this->prepareStringForAuthnet($line_item->desc()).', '; |
|
| 220 | 220 | } |
| 221 | 221 | foreach ($total_line_item->tax_descendants() as $tax_line_item) { |
| 222 | 222 | $this->addLineItem( |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | // invoice_num would be nice to have it be unique per SPCO page-load, that way if users |
| 255 | 255 | // press back, they don't submit a duplicate. However, we may be keeping the user on teh same spco page |
| 256 | 256 | // in which case, we need to generate teh invoice num per request right here... |
| 257 | - $this->setField('invoice_num', wp_generate_password(12, false));// $billing_info['_reg-page-billing-invoice-'.$this->_gateway_name]['value']); |
|
| 257 | + $this->setField('invoice_num', wp_generate_password(12, false)); // $billing_info['_reg-page-billing-invoice-'.$this->_gateway_name]['value']); |
|
| 258 | 258 | // tell AIM that any duplicates sent in the next 5 minutes are to be ignored |
| 259 | 259 | $this->setField('duplicate_window', 5 * MINUTE_IN_SECONDS); |
| 260 | 260 | |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | // Capture response |
| 266 | 266 | $this->type = "AUTH_CAPTURE"; |
| 267 | 267 | $response = $this->_sendRequest($payment); |
| 268 | - if (! empty($response)) { |
|
| 268 | + if ( ! empty($response)) { |
|
| 269 | 269 | if ($response->error_message) { |
| 270 | 270 | $payment->set_status($this->_pay_model->failed_status()); |
| 271 | 271 | $payment->set_gateway_response($response->error_message); |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | protected function _set_sensitive_billing_data($billing_info) |
| 312 | 312 | { |
| 313 | 313 | $this->setField('card_num', $billing_info['credit_card']); |
| 314 | - $this->setField('exp_date', $billing_info['exp_month'] . $billing_info['exp_year']); |
|
| 314 | + $this->setField('exp_date', $billing_info['exp_month'].$billing_info['exp_year']); |
|
| 315 | 315 | $this->setField('card_code', $billing_info['cvv']); |
| 316 | 316 | } |
| 317 | 317 | |
@@ -351,7 +351,7 @@ discard block |
||
| 351 | 351 | protected function setField($name, $value) |
| 352 | 352 | { |
| 353 | 353 | if (in_array($name, $this->_all_aim_fields)) { |
| 354 | - $this->_x_post_fields[ $name ] = $value; |
|
| 354 | + $this->_x_post_fields[$name] = $value; |
|
| 355 | 355 | } else { |
| 356 | 356 | throw new AuthorizeNetException("Error: no field $name exists in the AIM API. |
| 357 | 357 | To set a custom field use setCustomField('field','value') instead."); |
@@ -371,11 +371,11 @@ discard block |
||
| 371 | 371 | $this->_x_post_fields['tran_key'] = $this->_transaction_key; |
| 372 | 372 | $x_keys = array(); |
| 373 | 373 | foreach ($this->_x_post_fields as $key => $value) { |
| 374 | - $x_keys[] = "x_$key=" . urlencode($this->_get_unsupported_character_remover()->format($value)); |
|
| 374 | + $x_keys[] = "x_$key=".urlencode($this->_get_unsupported_character_remover()->format($value)); |
|
| 375 | 375 | } |
| 376 | 376 | // Add line items |
| 377 | 377 | foreach ($this->_additional_line_items as $key => $value) { |
| 378 | - $x_keys[] = "x_line_item=" . urlencode($this->_get_unsupported_character_remover()->format($value)); |
|
| 378 | + $x_keys[] = "x_line_item=".urlencode($this->_get_unsupported_character_remover()->format($value)); |
|
| 379 | 379 | } |
| 380 | 380 | $this->_log_clean_request($x_keys, $payment); |
| 381 | 381 | $post_url = $this->_get_server_url(); |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1); |
| 388 | 388 | curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 2); |
| 389 | 389 | if ($this->VERIFY_PEER) { |
| 390 | - curl_setopt($curl_request, CURLOPT_CAINFO, dirname(__DIR__) . '/ssl/cert.pem'); |
|
| 390 | + curl_setopt($curl_request, CURLOPT_CAINFO, dirname(__DIR__).'/ssl/cert.pem'); |
|
| 391 | 391 | } else { |
| 392 | 392 | curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, false); |
| 393 | 393 | } |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | $response = curl_exec($curl_request); |
| 400 | 400 | |
| 401 | 401 | curl_close($curl_request); |
| 402 | - $response_obj = new EE_AuthorizeNetAIM_Response($response); |
|
| 402 | + $response_obj = new EE_AuthorizeNetAIM_Response($response); |
|
| 403 | 403 | |
| 404 | 404 | return $this->_log_and_clean_response($response_obj, $payment); |
| 405 | 405 | } |
@@ -419,7 +419,7 @@ discard block |
||
| 419 | 419 | if (strpos($keyvaltogether, $key) === 0) { |
| 420 | 420 | // found it at the first character |
| 421 | 421 | // so its one of them |
| 422 | - unset($request_array[ $index ]); |
|
| 422 | + unset($request_array[$index]); |
|
| 423 | 423 | } |
| 424 | 424 | } |
| 425 | 425 | } |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | // Split Array |
| 561 | 561 | $this->response = $response; |
| 562 | 562 | if ($encap_char) { |
| 563 | - $this->_response_array = explode($encap_char . $delimiter . $encap_char, substr($response, 1, -1)); |
|
| 563 | + $this->_response_array = explode($encap_char.$delimiter.$encap_char, substr($response, 1, -1)); |
|
| 564 | 564 | } else { |
| 565 | 565 | $this->_response_array = explode($delimiter, $response); |
| 566 | 566 | } |
@@ -642,7 +642,7 @@ discard block |
||
| 642 | 642 | } |
| 643 | 643 | } |
| 644 | 644 | |
| 645 | -if (! class_exists('AuthorizeNetException')) { |
|
| 645 | +if ( ! class_exists('AuthorizeNetException')) { |
|
| 646 | 646 | /** |
| 647 | 647 | * Class AuthorizeNetException |
| 648 | 648 | * |
@@ -362,7 +362,7 @@ discard block |
||
| 362 | 362 | /** |
| 363 | 363 | * Posts the request to AuthorizeNet & returns response. |
| 364 | 364 | * |
| 365 | - * @param $payment |
|
| 365 | + * @param EEI_Payment $payment |
|
| 366 | 366 | * @return \EE_AuthorizeNetAIM_Response |
| 367 | 367 | */ |
| 368 | 368 | private function _sendRequest($payment) |
@@ -451,7 +451,7 @@ discard block |
||
| 451 | 451 | /** |
| 452 | 452 | * Removes characters Authorize.net doesn't handle well. |
| 453 | 453 | * @since 4.9.82.p |
| 454 | - * @param $text |
|
| 454 | + * @param string $text |
|
| 455 | 455 | * @return string |
| 456 | 456 | */ |
| 457 | 457 | private function prepareStringForAuthnet($text) |
@@ -26,442 +26,442 @@ discard block |
||
| 26 | 26 | class EEG_Aim extends EE_Onsite_Gateway |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - const LIVE_URL = 'https://secure2.authorize.net/gateway/transact.dll'; // Authnet URL |
|
| 30 | - |
|
| 31 | - const SANDBOX_URL = 'https://test.authorize.net/gateway/transact.dll'; |
|
| 32 | - |
|
| 33 | - protected $_login_id; |
|
| 34 | - |
|
| 35 | - protected $_transaction_key; |
|
| 36 | - |
|
| 37 | - protected $_server; |
|
| 38 | - |
|
| 39 | - protected $_currencies_supported = array( |
|
| 40 | - 'AUD', |
|
| 41 | - 'USD', |
|
| 42 | - 'CAD', |
|
| 43 | - 'EUR', |
|
| 44 | - 'GBP', |
|
| 45 | - 'NZD', |
|
| 46 | - ); |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Whether to send test transactions (even to live site) |
|
| 50 | - * |
|
| 51 | - * @var boolean |
|
| 52 | - */ |
|
| 53 | - protected $_test_transactions; |
|
| 54 | - |
|
| 55 | - private $VERIFY_PEER = false; |
|
| 56 | - |
|
| 57 | - private $_x_post_fields = array( |
|
| 58 | - "version" => "3.1", |
|
| 59 | - "delim_char" => ",", |
|
| 60 | - "delim_data" => "TRUE", |
|
| 61 | - "relay_response" => "FALSE", |
|
| 62 | - "encap_char" => "|", |
|
| 63 | - ); |
|
| 64 | - |
|
| 65 | - private $_additional_line_items = array(); |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * A list of all fields in the AIM API. |
|
| 69 | - * Used to warn user if they try to set a field not offered in the API. |
|
| 70 | - */ |
|
| 71 | - private $_all_aim_fields = array( |
|
| 72 | - "address", |
|
| 73 | - "allow_partial_auth", |
|
| 74 | - "amount", |
|
| 75 | - "auth_code", |
|
| 76 | - "authentication_indicator", |
|
| 77 | - "bank_aba_code", |
|
| 78 | - "bank_acct_name", |
|
| 79 | - "bank_acct_num", |
|
| 80 | - "bank_acct_type", |
|
| 81 | - "bank_check_number", |
|
| 82 | - "bank_name", |
|
| 83 | - "card_code", |
|
| 84 | - "card_num", |
|
| 85 | - "cardholder_authentication_value", |
|
| 86 | - "city", |
|
| 87 | - "company", |
|
| 88 | - "country", |
|
| 89 | - "cust_id", |
|
| 90 | - "customer_ip", |
|
| 91 | - "delim_char", |
|
| 92 | - "delim_data", |
|
| 93 | - "description", |
|
| 94 | - "duplicate_window", |
|
| 95 | - "duty", |
|
| 96 | - "echeck_type", |
|
| 97 | - "email", |
|
| 98 | - "email_customer", |
|
| 99 | - "encap_char", |
|
| 100 | - "exp_date", |
|
| 101 | - "fax", |
|
| 102 | - "first_name", |
|
| 103 | - "footer_email_receipt", |
|
| 104 | - "freight", |
|
| 105 | - "header_email_receipt", |
|
| 106 | - "invoice_num", |
|
| 107 | - "last_name", |
|
| 108 | - "line_item", |
|
| 109 | - "login", |
|
| 110 | - "method", |
|
| 111 | - "phone", |
|
| 112 | - "po_num", |
|
| 113 | - "recurring_billing", |
|
| 114 | - "relay_response", |
|
| 115 | - "ship_to_address", |
|
| 116 | - "ship_to_city", |
|
| 117 | - "ship_to_company", |
|
| 118 | - "ship_to_country", |
|
| 119 | - "ship_to_first_name", |
|
| 120 | - "ship_to_last_name", |
|
| 121 | - "ship_to_state", |
|
| 122 | - "ship_to_zip", |
|
| 123 | - "split_tender_id", |
|
| 124 | - "state", |
|
| 125 | - "tax", |
|
| 126 | - "tax_exempt", |
|
| 127 | - "test_request", |
|
| 128 | - "tran_key", |
|
| 129 | - "trans_id", |
|
| 130 | - "type", |
|
| 131 | - "version", |
|
| 132 | - "zip", |
|
| 133 | - "solution_id", |
|
| 134 | - "currency_code" |
|
| 135 | - ); |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Gets the URL where the request should go. This is filterable |
|
| 140 | - * |
|
| 141 | - * @return string |
|
| 142 | - */ |
|
| 143 | - protected function _get_server_url() |
|
| 144 | - { |
|
| 145 | - return apply_filters( |
|
| 146 | - 'FHEE__EEG_Aim___get_server_url', |
|
| 147 | - $this->_debug_mode ? self::SANDBOX_URL : self::LIVE_URL, |
|
| 148 | - $this |
|
| 149 | - ); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * TEMPORARY CALLBACK! Do not use |
|
| 155 | - * Callback which filters the server url. This is added so site admins can revert to using |
|
| 156 | - * the old AIM server in case Akamai service breaks their integration. |
|
| 157 | - * Using Akamai will, however, be mandatory on June 30th 2016 Authorize.net |
|
| 158 | - * (see http://www.authorize.net/support/akamaifaqs/#firewall?utm_campaign=April%202016%20Technical%20Updates%20for%20Merchants.html&utm_medium=email&utm_source=Eloqua&elqTrackId=46103bdc375c411a979c2f658fc99074&elq=7026706360154fee9b6d588b27d8eb6a&elqaid=506&elqat=1&elqCampaignId=343) |
|
| 159 | - * Once that happens, this will be obsolete and WILL BE REMOVED. |
|
| 160 | - * |
|
| 161 | - * @param string $url |
|
| 162 | - * @param EEG_Aim $gateway_object |
|
| 163 | - * @return string |
|
| 164 | - */ |
|
| 165 | - public function possibly_use_deprecated_aim_server($url, EEG_Aim $gateway_object) |
|
| 166 | - { |
|
| 167 | - if ($gateway_object->_server === 'authorize.net' && ! $gateway_object->_debug_mode) { |
|
| 168 | - return 'https://secure.authorize.net/gateway/transact.dll'; |
|
| 169 | - } else { |
|
| 170 | - return $url; |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Asks the gateway to do whatever it does to process the payment. Onsite gateways will |
|
| 177 | - * usually send a request directly to the payment provider and update the payment's status based on that; |
|
| 178 | - * whereas offsite gateways will usually just update the payment with the URL and query parameters to use |
|
| 179 | - * for sending the request via http_remote_request() |
|
| 180 | - * |
|
| 181 | - * @param EEI_Payment $payment |
|
| 182 | - * @param array $billing_info { |
|
| 183 | - * @type $credit_card string |
|
| 184 | - * @type $cvv string |
|
| 185 | - * @type $exp_month string |
|
| 186 | - * @type $exp_year string |
|
| 187 | - * @see parent::do_direct_payment |
|
| 188 | - * } |
|
| 189 | - * @return EEI_Payment updated |
|
| 190 | - */ |
|
| 191 | - public function do_direct_payment($payment, $billing_info = null) |
|
| 192 | - { |
|
| 193 | - add_filter('FHEE__EEG_Aim___get_server_url', array($this, 'possibly_use_deprecated_aim_server'), 10, 2); |
|
| 194 | - // Enable test mode if needed |
|
| 195 | - // 4007000000027 <-- test successful visa |
|
| 196 | - // 4222222222222 <-- test failure card number |
|
| 197 | - |
|
| 198 | - $item_num = 1; |
|
| 199 | - $transaction = $payment->transaction(); |
|
| 200 | - $gateway_formatter = $this->_get_gateway_formatter(); |
|
| 201 | - $order_description = $this->prepareStringForAuthnet($gateway_formatter->formatOrderDescription($payment)); |
|
| 202 | - $primary_registrant = $transaction->primary_registration(); |
|
| 203 | - // if we're are charging for the full amount, show the normal line items |
|
| 204 | - // and the itemized total adds up properly |
|
| 205 | - if ($this->_can_easily_itemize_transaction_for($payment)) { |
|
| 206 | - $total_line_item = $transaction->total_line_item(); |
|
| 207 | - foreach ($total_line_item->get_items() as $line_item) { |
|
| 208 | - if ($line_item->quantity() == 0) { |
|
| 209 | - continue; |
|
| 210 | - } |
|
| 211 | - $this->addLineItem( |
|
| 212 | - $item_num++, |
|
| 213 | - $gateway_formatter->formatLineItemName($line_item, $payment), |
|
| 214 | - $gateway_formatter->formatLineItemDesc($line_item, $payment), |
|
| 215 | - $line_item->quantity(), |
|
| 216 | - $line_item->unit_price(), |
|
| 217 | - 'N' |
|
| 218 | - ); |
|
| 219 | - $order_description .= $this->prepareStringForAuthnet($line_item->desc()) . ', '; |
|
| 220 | - } |
|
| 221 | - foreach ($total_line_item->tax_descendants() as $tax_line_item) { |
|
| 222 | - $this->addLineItem( |
|
| 223 | - $item_num++, |
|
| 224 | - $tax_line_item->name(), |
|
| 225 | - $tax_line_item->desc(), |
|
| 226 | - 1, |
|
| 227 | - $tax_line_item->total(), |
|
| 228 | - 'N' |
|
| 229 | - ); |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - // start transaction |
|
| 234 | - // if in debug mode, use authorize.net's sandbox id; otherwise use the Event Espresso partner id |
|
| 235 | - $partner_id = $this->_debug_mode ? 'AAA100302' : 'AAA105363'; |
|
| 236 | - $this->setField('solution_id', $partner_id); |
|
| 237 | - $this->setField('amount', $gateway_formatter->formatCurrency($payment->amount())); |
|
| 238 | - $this->setField('description', substr(rtrim($order_description, ', '), 0, 255)); |
|
| 239 | - $this->_set_sensitive_billing_data($billing_info); |
|
| 240 | - $this->setField('first_name', $billing_info['first_name']); |
|
| 241 | - $this->setField('last_name', $billing_info['last_name']); |
|
| 242 | - $this->setField('email', $billing_info['email']); |
|
| 243 | - $this->setField('company', $billing_info['company']); |
|
| 244 | - $this->setField('address', $billing_info['address'].' '.$billing_info['address2']); |
|
| 245 | - $this->setField('city', $billing_info['city']); |
|
| 246 | - $this->setField('state', $billing_info['state']); |
|
| 247 | - $this->setField('country', $billing_info['country']); |
|
| 248 | - $this->setField('zip', $billing_info['zip']); |
|
| 249 | - $this->setField('fax', $billing_info['fax']); |
|
| 250 | - $this->setField('cust_id', $primary_registrant->ID()); |
|
| 251 | - $this->setField('phone', $billing_info['phone']); |
|
| 252 | - $currency_config = LoaderFactory::getLoader()->load('EE_Currency_Config'); |
|
| 253 | - $this->setField('currency_code', $currency_config->code); |
|
| 254 | - // invoice_num would be nice to have it be unique per SPCO page-load, that way if users |
|
| 255 | - // press back, they don't submit a duplicate. However, we may be keeping the user on teh same spco page |
|
| 256 | - // in which case, we need to generate teh invoice num per request right here... |
|
| 257 | - $this->setField('invoice_num', wp_generate_password(12, false));// $billing_info['_reg-page-billing-invoice-'.$this->_gateway_name]['value']); |
|
| 258 | - // tell AIM that any duplicates sent in the next 5 minutes are to be ignored |
|
| 259 | - $this->setField('duplicate_window', 5 * MINUTE_IN_SECONDS); |
|
| 260 | - |
|
| 261 | - if ($this->_test_transactions) { |
|
| 262 | - $this->test_request = "true"; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - // Capture response |
|
| 266 | - $this->type = "AUTH_CAPTURE"; |
|
| 267 | - $response = $this->_sendRequest($payment); |
|
| 268 | - if (! empty($response)) { |
|
| 269 | - if ($response->error_message) { |
|
| 270 | - $payment->set_status($this->_pay_model->failed_status()); |
|
| 271 | - $payment->set_gateway_response($response->error_message); |
|
| 272 | - } else { |
|
| 273 | - $payment_status = $response->approved |
|
| 274 | - ? $this->_pay_model->approved_status() |
|
| 275 | - : $this->_pay_model->declined_status(); |
|
| 276 | - $payment->set_status($payment_status); |
|
| 277 | - // make sure we interpret the AMT as a float, not an international string (where periods are thousand separators) |
|
| 278 | - $payment->set_amount((float) $response->amount); |
|
| 279 | - $payment->set_gateway_response( |
|
| 280 | - sprintf( |
|
| 281 | - esc_html__('%1$s (Reason Code: %2$s)', 'event_espresso'), |
|
| 282 | - $response->response_reason_text, |
|
| 283 | - $response->response_reason_code |
|
| 284 | - ) |
|
| 285 | - ); |
|
| 286 | - if ($this->_debug_mode) { |
|
| 287 | - $txn_id = $response->invoice_number; |
|
| 288 | - } else { |
|
| 289 | - $txn_id = $response->transaction_id; |
|
| 290 | - } |
|
| 291 | - $payment->set_txn_id_chq_nmbr($txn_id); |
|
| 292 | - } |
|
| 293 | - $payment->set_extra_accntng($primary_registrant->reg_code()); |
|
| 294 | - $payment->set_details(print_r($response, true)); |
|
| 295 | - } else { |
|
| 296 | - $payment->set_status($this->_pay_model->failed_status()); |
|
| 297 | - $payment->set_gateway_response(__("There was no response from Authorize.net", 'event_espresso')); |
|
| 298 | - $payment->set_details(print_r($response, true)); |
|
| 299 | - } |
|
| 300 | - return $payment; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Sets billing data for the upcoming request to AIM that is considered sensitive; |
|
| 306 | - * also this method can be overridden by children classes to easily change |
|
| 307 | - * what billing data gets sent |
|
| 308 | - * |
|
| 309 | - * @param array $billing_info |
|
| 310 | - */ |
|
| 311 | - protected function _set_sensitive_billing_data($billing_info) |
|
| 312 | - { |
|
| 313 | - $this->setField('card_num', $billing_info['credit_card']); |
|
| 314 | - $this->setField('exp_date', $billing_info['exp_month'] . $billing_info['exp_year']); |
|
| 315 | - $this->setField('card_code', $billing_info['cvv']); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Add a line item. |
|
| 321 | - * |
|
| 322 | - * @param string $item_id |
|
| 323 | - * @param string $item_name |
|
| 324 | - * @param string $item_description |
|
| 325 | - * @param string $item_quantity |
|
| 326 | - * @param string $item_unit_price |
|
| 327 | - * @param string $item_taxable |
|
| 328 | - */ |
|
| 329 | - public function addLineItem($item_id, $item_name, $item_description, $item_quantity, $item_unit_price, $item_taxable) |
|
| 330 | - { |
|
| 331 | - $args = array( |
|
| 332 | - substr($item_id, 0, 31), |
|
| 333 | - substr($this->prepareStringForAuthnet($item_name), 0, 31), |
|
| 334 | - substr($this->prepareStringForAuthnet($item_description), 0, 255), |
|
| 335 | - number_format(abs($item_quantity), 2, '.', ''), |
|
| 336 | - number_format(abs($item_unit_price), 2, '.', ''), |
|
| 337 | - $item_taxable === 'N' ? 'N' : 'Y' |
|
| 338 | - ); |
|
| 339 | - $this->_additional_line_items[] = implode('<|>', $args); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Set an individual name/value pair. This will append x_ to the name |
|
| 345 | - * before posting. |
|
| 346 | - * |
|
| 347 | - * @param string $name |
|
| 348 | - * @param string $value |
|
| 349 | - * @throws AuthorizeNetException |
|
| 350 | - */ |
|
| 351 | - protected function setField($name, $value) |
|
| 352 | - { |
|
| 353 | - if (in_array($name, $this->_all_aim_fields)) { |
|
| 354 | - $this->_x_post_fields[ $name ] = $value; |
|
| 355 | - } else { |
|
| 356 | - throw new AuthorizeNetException("Error: no field $name exists in the AIM API. |
|
| 29 | + const LIVE_URL = 'https://secure2.authorize.net/gateway/transact.dll'; // Authnet URL |
|
| 30 | + |
|
| 31 | + const SANDBOX_URL = 'https://test.authorize.net/gateway/transact.dll'; |
|
| 32 | + |
|
| 33 | + protected $_login_id; |
|
| 34 | + |
|
| 35 | + protected $_transaction_key; |
|
| 36 | + |
|
| 37 | + protected $_server; |
|
| 38 | + |
|
| 39 | + protected $_currencies_supported = array( |
|
| 40 | + 'AUD', |
|
| 41 | + 'USD', |
|
| 42 | + 'CAD', |
|
| 43 | + 'EUR', |
|
| 44 | + 'GBP', |
|
| 45 | + 'NZD', |
|
| 46 | + ); |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Whether to send test transactions (even to live site) |
|
| 50 | + * |
|
| 51 | + * @var boolean |
|
| 52 | + */ |
|
| 53 | + protected $_test_transactions; |
|
| 54 | + |
|
| 55 | + private $VERIFY_PEER = false; |
|
| 56 | + |
|
| 57 | + private $_x_post_fields = array( |
|
| 58 | + "version" => "3.1", |
|
| 59 | + "delim_char" => ",", |
|
| 60 | + "delim_data" => "TRUE", |
|
| 61 | + "relay_response" => "FALSE", |
|
| 62 | + "encap_char" => "|", |
|
| 63 | + ); |
|
| 64 | + |
|
| 65 | + private $_additional_line_items = array(); |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * A list of all fields in the AIM API. |
|
| 69 | + * Used to warn user if they try to set a field not offered in the API. |
|
| 70 | + */ |
|
| 71 | + private $_all_aim_fields = array( |
|
| 72 | + "address", |
|
| 73 | + "allow_partial_auth", |
|
| 74 | + "amount", |
|
| 75 | + "auth_code", |
|
| 76 | + "authentication_indicator", |
|
| 77 | + "bank_aba_code", |
|
| 78 | + "bank_acct_name", |
|
| 79 | + "bank_acct_num", |
|
| 80 | + "bank_acct_type", |
|
| 81 | + "bank_check_number", |
|
| 82 | + "bank_name", |
|
| 83 | + "card_code", |
|
| 84 | + "card_num", |
|
| 85 | + "cardholder_authentication_value", |
|
| 86 | + "city", |
|
| 87 | + "company", |
|
| 88 | + "country", |
|
| 89 | + "cust_id", |
|
| 90 | + "customer_ip", |
|
| 91 | + "delim_char", |
|
| 92 | + "delim_data", |
|
| 93 | + "description", |
|
| 94 | + "duplicate_window", |
|
| 95 | + "duty", |
|
| 96 | + "echeck_type", |
|
| 97 | + "email", |
|
| 98 | + "email_customer", |
|
| 99 | + "encap_char", |
|
| 100 | + "exp_date", |
|
| 101 | + "fax", |
|
| 102 | + "first_name", |
|
| 103 | + "footer_email_receipt", |
|
| 104 | + "freight", |
|
| 105 | + "header_email_receipt", |
|
| 106 | + "invoice_num", |
|
| 107 | + "last_name", |
|
| 108 | + "line_item", |
|
| 109 | + "login", |
|
| 110 | + "method", |
|
| 111 | + "phone", |
|
| 112 | + "po_num", |
|
| 113 | + "recurring_billing", |
|
| 114 | + "relay_response", |
|
| 115 | + "ship_to_address", |
|
| 116 | + "ship_to_city", |
|
| 117 | + "ship_to_company", |
|
| 118 | + "ship_to_country", |
|
| 119 | + "ship_to_first_name", |
|
| 120 | + "ship_to_last_name", |
|
| 121 | + "ship_to_state", |
|
| 122 | + "ship_to_zip", |
|
| 123 | + "split_tender_id", |
|
| 124 | + "state", |
|
| 125 | + "tax", |
|
| 126 | + "tax_exempt", |
|
| 127 | + "test_request", |
|
| 128 | + "tran_key", |
|
| 129 | + "trans_id", |
|
| 130 | + "type", |
|
| 131 | + "version", |
|
| 132 | + "zip", |
|
| 133 | + "solution_id", |
|
| 134 | + "currency_code" |
|
| 135 | + ); |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Gets the URL where the request should go. This is filterable |
|
| 140 | + * |
|
| 141 | + * @return string |
|
| 142 | + */ |
|
| 143 | + protected function _get_server_url() |
|
| 144 | + { |
|
| 145 | + return apply_filters( |
|
| 146 | + 'FHEE__EEG_Aim___get_server_url', |
|
| 147 | + $this->_debug_mode ? self::SANDBOX_URL : self::LIVE_URL, |
|
| 148 | + $this |
|
| 149 | + ); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * TEMPORARY CALLBACK! Do not use |
|
| 155 | + * Callback which filters the server url. This is added so site admins can revert to using |
|
| 156 | + * the old AIM server in case Akamai service breaks their integration. |
|
| 157 | + * Using Akamai will, however, be mandatory on June 30th 2016 Authorize.net |
|
| 158 | + * (see http://www.authorize.net/support/akamaifaqs/#firewall?utm_campaign=April%202016%20Technical%20Updates%20for%20Merchants.html&utm_medium=email&utm_source=Eloqua&elqTrackId=46103bdc375c411a979c2f658fc99074&elq=7026706360154fee9b6d588b27d8eb6a&elqaid=506&elqat=1&elqCampaignId=343) |
|
| 159 | + * Once that happens, this will be obsolete and WILL BE REMOVED. |
|
| 160 | + * |
|
| 161 | + * @param string $url |
|
| 162 | + * @param EEG_Aim $gateway_object |
|
| 163 | + * @return string |
|
| 164 | + */ |
|
| 165 | + public function possibly_use_deprecated_aim_server($url, EEG_Aim $gateway_object) |
|
| 166 | + { |
|
| 167 | + if ($gateway_object->_server === 'authorize.net' && ! $gateway_object->_debug_mode) { |
|
| 168 | + return 'https://secure.authorize.net/gateway/transact.dll'; |
|
| 169 | + } else { |
|
| 170 | + return $url; |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Asks the gateway to do whatever it does to process the payment. Onsite gateways will |
|
| 177 | + * usually send a request directly to the payment provider and update the payment's status based on that; |
|
| 178 | + * whereas offsite gateways will usually just update the payment with the URL and query parameters to use |
|
| 179 | + * for sending the request via http_remote_request() |
|
| 180 | + * |
|
| 181 | + * @param EEI_Payment $payment |
|
| 182 | + * @param array $billing_info { |
|
| 183 | + * @type $credit_card string |
|
| 184 | + * @type $cvv string |
|
| 185 | + * @type $exp_month string |
|
| 186 | + * @type $exp_year string |
|
| 187 | + * @see parent::do_direct_payment |
|
| 188 | + * } |
|
| 189 | + * @return EEI_Payment updated |
|
| 190 | + */ |
|
| 191 | + public function do_direct_payment($payment, $billing_info = null) |
|
| 192 | + { |
|
| 193 | + add_filter('FHEE__EEG_Aim___get_server_url', array($this, 'possibly_use_deprecated_aim_server'), 10, 2); |
|
| 194 | + // Enable test mode if needed |
|
| 195 | + // 4007000000027 <-- test successful visa |
|
| 196 | + // 4222222222222 <-- test failure card number |
|
| 197 | + |
|
| 198 | + $item_num = 1; |
|
| 199 | + $transaction = $payment->transaction(); |
|
| 200 | + $gateway_formatter = $this->_get_gateway_formatter(); |
|
| 201 | + $order_description = $this->prepareStringForAuthnet($gateway_formatter->formatOrderDescription($payment)); |
|
| 202 | + $primary_registrant = $transaction->primary_registration(); |
|
| 203 | + // if we're are charging for the full amount, show the normal line items |
|
| 204 | + // and the itemized total adds up properly |
|
| 205 | + if ($this->_can_easily_itemize_transaction_for($payment)) { |
|
| 206 | + $total_line_item = $transaction->total_line_item(); |
|
| 207 | + foreach ($total_line_item->get_items() as $line_item) { |
|
| 208 | + if ($line_item->quantity() == 0) { |
|
| 209 | + continue; |
|
| 210 | + } |
|
| 211 | + $this->addLineItem( |
|
| 212 | + $item_num++, |
|
| 213 | + $gateway_formatter->formatLineItemName($line_item, $payment), |
|
| 214 | + $gateway_formatter->formatLineItemDesc($line_item, $payment), |
|
| 215 | + $line_item->quantity(), |
|
| 216 | + $line_item->unit_price(), |
|
| 217 | + 'N' |
|
| 218 | + ); |
|
| 219 | + $order_description .= $this->prepareStringForAuthnet($line_item->desc()) . ', '; |
|
| 220 | + } |
|
| 221 | + foreach ($total_line_item->tax_descendants() as $tax_line_item) { |
|
| 222 | + $this->addLineItem( |
|
| 223 | + $item_num++, |
|
| 224 | + $tax_line_item->name(), |
|
| 225 | + $tax_line_item->desc(), |
|
| 226 | + 1, |
|
| 227 | + $tax_line_item->total(), |
|
| 228 | + 'N' |
|
| 229 | + ); |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + // start transaction |
|
| 234 | + // if in debug mode, use authorize.net's sandbox id; otherwise use the Event Espresso partner id |
|
| 235 | + $partner_id = $this->_debug_mode ? 'AAA100302' : 'AAA105363'; |
|
| 236 | + $this->setField('solution_id', $partner_id); |
|
| 237 | + $this->setField('amount', $gateway_formatter->formatCurrency($payment->amount())); |
|
| 238 | + $this->setField('description', substr(rtrim($order_description, ', '), 0, 255)); |
|
| 239 | + $this->_set_sensitive_billing_data($billing_info); |
|
| 240 | + $this->setField('first_name', $billing_info['first_name']); |
|
| 241 | + $this->setField('last_name', $billing_info['last_name']); |
|
| 242 | + $this->setField('email', $billing_info['email']); |
|
| 243 | + $this->setField('company', $billing_info['company']); |
|
| 244 | + $this->setField('address', $billing_info['address'].' '.$billing_info['address2']); |
|
| 245 | + $this->setField('city', $billing_info['city']); |
|
| 246 | + $this->setField('state', $billing_info['state']); |
|
| 247 | + $this->setField('country', $billing_info['country']); |
|
| 248 | + $this->setField('zip', $billing_info['zip']); |
|
| 249 | + $this->setField('fax', $billing_info['fax']); |
|
| 250 | + $this->setField('cust_id', $primary_registrant->ID()); |
|
| 251 | + $this->setField('phone', $billing_info['phone']); |
|
| 252 | + $currency_config = LoaderFactory::getLoader()->load('EE_Currency_Config'); |
|
| 253 | + $this->setField('currency_code', $currency_config->code); |
|
| 254 | + // invoice_num would be nice to have it be unique per SPCO page-load, that way if users |
|
| 255 | + // press back, they don't submit a duplicate. However, we may be keeping the user on teh same spco page |
|
| 256 | + // in which case, we need to generate teh invoice num per request right here... |
|
| 257 | + $this->setField('invoice_num', wp_generate_password(12, false));// $billing_info['_reg-page-billing-invoice-'.$this->_gateway_name]['value']); |
|
| 258 | + // tell AIM that any duplicates sent in the next 5 minutes are to be ignored |
|
| 259 | + $this->setField('duplicate_window', 5 * MINUTE_IN_SECONDS); |
|
| 260 | + |
|
| 261 | + if ($this->_test_transactions) { |
|
| 262 | + $this->test_request = "true"; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + // Capture response |
|
| 266 | + $this->type = "AUTH_CAPTURE"; |
|
| 267 | + $response = $this->_sendRequest($payment); |
|
| 268 | + if (! empty($response)) { |
|
| 269 | + if ($response->error_message) { |
|
| 270 | + $payment->set_status($this->_pay_model->failed_status()); |
|
| 271 | + $payment->set_gateway_response($response->error_message); |
|
| 272 | + } else { |
|
| 273 | + $payment_status = $response->approved |
|
| 274 | + ? $this->_pay_model->approved_status() |
|
| 275 | + : $this->_pay_model->declined_status(); |
|
| 276 | + $payment->set_status($payment_status); |
|
| 277 | + // make sure we interpret the AMT as a float, not an international string (where periods are thousand separators) |
|
| 278 | + $payment->set_amount((float) $response->amount); |
|
| 279 | + $payment->set_gateway_response( |
|
| 280 | + sprintf( |
|
| 281 | + esc_html__('%1$s (Reason Code: %2$s)', 'event_espresso'), |
|
| 282 | + $response->response_reason_text, |
|
| 283 | + $response->response_reason_code |
|
| 284 | + ) |
|
| 285 | + ); |
|
| 286 | + if ($this->_debug_mode) { |
|
| 287 | + $txn_id = $response->invoice_number; |
|
| 288 | + } else { |
|
| 289 | + $txn_id = $response->transaction_id; |
|
| 290 | + } |
|
| 291 | + $payment->set_txn_id_chq_nmbr($txn_id); |
|
| 292 | + } |
|
| 293 | + $payment->set_extra_accntng($primary_registrant->reg_code()); |
|
| 294 | + $payment->set_details(print_r($response, true)); |
|
| 295 | + } else { |
|
| 296 | + $payment->set_status($this->_pay_model->failed_status()); |
|
| 297 | + $payment->set_gateway_response(__("There was no response from Authorize.net", 'event_espresso')); |
|
| 298 | + $payment->set_details(print_r($response, true)); |
|
| 299 | + } |
|
| 300 | + return $payment; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Sets billing data for the upcoming request to AIM that is considered sensitive; |
|
| 306 | + * also this method can be overridden by children classes to easily change |
|
| 307 | + * what billing data gets sent |
|
| 308 | + * |
|
| 309 | + * @param array $billing_info |
|
| 310 | + */ |
|
| 311 | + protected function _set_sensitive_billing_data($billing_info) |
|
| 312 | + { |
|
| 313 | + $this->setField('card_num', $billing_info['credit_card']); |
|
| 314 | + $this->setField('exp_date', $billing_info['exp_month'] . $billing_info['exp_year']); |
|
| 315 | + $this->setField('card_code', $billing_info['cvv']); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Add a line item. |
|
| 321 | + * |
|
| 322 | + * @param string $item_id |
|
| 323 | + * @param string $item_name |
|
| 324 | + * @param string $item_description |
|
| 325 | + * @param string $item_quantity |
|
| 326 | + * @param string $item_unit_price |
|
| 327 | + * @param string $item_taxable |
|
| 328 | + */ |
|
| 329 | + public function addLineItem($item_id, $item_name, $item_description, $item_quantity, $item_unit_price, $item_taxable) |
|
| 330 | + { |
|
| 331 | + $args = array( |
|
| 332 | + substr($item_id, 0, 31), |
|
| 333 | + substr($this->prepareStringForAuthnet($item_name), 0, 31), |
|
| 334 | + substr($this->prepareStringForAuthnet($item_description), 0, 255), |
|
| 335 | + number_format(abs($item_quantity), 2, '.', ''), |
|
| 336 | + number_format(abs($item_unit_price), 2, '.', ''), |
|
| 337 | + $item_taxable === 'N' ? 'N' : 'Y' |
|
| 338 | + ); |
|
| 339 | + $this->_additional_line_items[] = implode('<|>', $args); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Set an individual name/value pair. This will append x_ to the name |
|
| 345 | + * before posting. |
|
| 346 | + * |
|
| 347 | + * @param string $name |
|
| 348 | + * @param string $value |
|
| 349 | + * @throws AuthorizeNetException |
|
| 350 | + */ |
|
| 351 | + protected function setField($name, $value) |
|
| 352 | + { |
|
| 353 | + if (in_array($name, $this->_all_aim_fields)) { |
|
| 354 | + $this->_x_post_fields[ $name ] = $value; |
|
| 355 | + } else { |
|
| 356 | + throw new AuthorizeNetException("Error: no field $name exists in the AIM API. |
|
| 357 | 357 | To set a custom field use setCustomField('field','value') instead."); |
| 358 | - } |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Posts the request to AuthorizeNet & returns response. |
|
| 364 | - * |
|
| 365 | - * @param $payment |
|
| 366 | - * @return \EE_AuthorizeNetAIM_Response |
|
| 367 | - */ |
|
| 368 | - private function _sendRequest($payment) |
|
| 369 | - { |
|
| 370 | - $this->_x_post_fields['login'] = $this->_login_id; |
|
| 371 | - $this->_x_post_fields['tran_key'] = $this->_transaction_key; |
|
| 372 | - $x_keys = array(); |
|
| 373 | - foreach ($this->_x_post_fields as $key => $value) { |
|
| 374 | - $x_keys[] = "x_$key=" . urlencode($this->_get_unsupported_character_remover()->format($value)); |
|
| 375 | - } |
|
| 376 | - // Add line items |
|
| 377 | - foreach ($this->_additional_line_items as $key => $value) { |
|
| 378 | - $x_keys[] = "x_line_item=" . urlencode($this->_get_unsupported_character_remover()->format($value)); |
|
| 379 | - } |
|
| 380 | - $this->_log_clean_request($x_keys, $payment); |
|
| 381 | - $post_url = $this->_get_server_url(); |
|
| 382 | - $curl_request = curl_init($post_url); |
|
| 383 | - $post_body = implode("&", $x_keys); |
|
| 384 | - curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post_body); |
|
| 385 | - curl_setopt($curl_request, CURLOPT_HEADER, 0); |
|
| 386 | - curl_setopt($curl_request, CURLOPT_TIMEOUT, 45); |
|
| 387 | - curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1); |
|
| 388 | - curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 2); |
|
| 389 | - if ($this->VERIFY_PEER) { |
|
| 390 | - curl_setopt($curl_request, CURLOPT_CAINFO, dirname(__DIR__) . '/ssl/cert.pem'); |
|
| 391 | - } else { |
|
| 392 | - curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, false); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - if (preg_match('/xml/', $post_url)) { |
|
| 396 | - curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Content-Type: text/xml")); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - $response = curl_exec($curl_request); |
|
| 400 | - |
|
| 401 | - curl_close($curl_request); |
|
| 402 | - $response_obj = new EE_AuthorizeNetAIM_Response($response); |
|
| 403 | - |
|
| 404 | - return $this->_log_and_clean_response($response_obj, $payment); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Logs the clean data only |
|
| 410 | - * |
|
| 411 | - * @param array $request_array |
|
| 412 | - * @param EEI_Payment $payment |
|
| 413 | - */ |
|
| 414 | - protected function _log_clean_request($request_array, $payment) |
|
| 415 | - { |
|
| 416 | - $keys_to_filter_out = array('x_card_num', 'x_card_code', 'x_exp_date'); |
|
| 417 | - foreach ($request_array as $index => $keyvaltogether) { |
|
| 418 | - foreach ($keys_to_filter_out as $key) { |
|
| 419 | - if (strpos($keyvaltogether, $key) === 0) { |
|
| 420 | - // found it at the first character |
|
| 421 | - // so its one of them |
|
| 422 | - unset($request_array[ $index ]); |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - $this->log( |
|
| 427 | - array( |
|
| 428 | - 'AIM Request sent:' => $request_array, |
|
| 429 | - 'Server URL' => $this->_get_server_url() |
|
| 430 | - ), |
|
| 431 | - $payment |
|
| 432 | - ); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Logs the response and cleans it |
|
| 439 | - * |
|
| 440 | - * @param EE_AuthorizeNetAIM_Response $response_obj |
|
| 441 | - * @param EE_Payment $payment |
|
| 442 | - * @return \EE_AuthorizeNetAIM_Response |
|
| 443 | - */ |
|
| 444 | - private function _log_and_clean_response($response_obj, $payment) |
|
| 445 | - { |
|
| 446 | - $response_obj->account_number = ''; |
|
| 447 | - $this->log(array('AIM Response received:' => (array) $response_obj), $payment); |
|
| 448 | - return $response_obj; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Removes characters Authorize.net doesn't handle well. |
|
| 453 | - * @since 4.9.82.p |
|
| 454 | - * @param $text |
|
| 455 | - * @return string |
|
| 456 | - */ |
|
| 457 | - private function prepareStringForAuthnet($text) |
|
| 458 | - { |
|
| 459 | - return str_replace( |
|
| 460 | - '\'', |
|
| 461 | - '', |
|
| 462 | - $text |
|
| 463 | - ); |
|
| 464 | - } |
|
| 358 | + } |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Posts the request to AuthorizeNet & returns response. |
|
| 364 | + * |
|
| 365 | + * @param $payment |
|
| 366 | + * @return \EE_AuthorizeNetAIM_Response |
|
| 367 | + */ |
|
| 368 | + private function _sendRequest($payment) |
|
| 369 | + { |
|
| 370 | + $this->_x_post_fields['login'] = $this->_login_id; |
|
| 371 | + $this->_x_post_fields['tran_key'] = $this->_transaction_key; |
|
| 372 | + $x_keys = array(); |
|
| 373 | + foreach ($this->_x_post_fields as $key => $value) { |
|
| 374 | + $x_keys[] = "x_$key=" . urlencode($this->_get_unsupported_character_remover()->format($value)); |
|
| 375 | + } |
|
| 376 | + // Add line items |
|
| 377 | + foreach ($this->_additional_line_items as $key => $value) { |
|
| 378 | + $x_keys[] = "x_line_item=" . urlencode($this->_get_unsupported_character_remover()->format($value)); |
|
| 379 | + } |
|
| 380 | + $this->_log_clean_request($x_keys, $payment); |
|
| 381 | + $post_url = $this->_get_server_url(); |
|
| 382 | + $curl_request = curl_init($post_url); |
|
| 383 | + $post_body = implode("&", $x_keys); |
|
| 384 | + curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post_body); |
|
| 385 | + curl_setopt($curl_request, CURLOPT_HEADER, 0); |
|
| 386 | + curl_setopt($curl_request, CURLOPT_TIMEOUT, 45); |
|
| 387 | + curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1); |
|
| 388 | + curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 2); |
|
| 389 | + if ($this->VERIFY_PEER) { |
|
| 390 | + curl_setopt($curl_request, CURLOPT_CAINFO, dirname(__DIR__) . '/ssl/cert.pem'); |
|
| 391 | + } else { |
|
| 392 | + curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, false); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + if (preg_match('/xml/', $post_url)) { |
|
| 396 | + curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Content-Type: text/xml")); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + $response = curl_exec($curl_request); |
|
| 400 | + |
|
| 401 | + curl_close($curl_request); |
|
| 402 | + $response_obj = new EE_AuthorizeNetAIM_Response($response); |
|
| 403 | + |
|
| 404 | + return $this->_log_and_clean_response($response_obj, $payment); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Logs the clean data only |
|
| 410 | + * |
|
| 411 | + * @param array $request_array |
|
| 412 | + * @param EEI_Payment $payment |
|
| 413 | + */ |
|
| 414 | + protected function _log_clean_request($request_array, $payment) |
|
| 415 | + { |
|
| 416 | + $keys_to_filter_out = array('x_card_num', 'x_card_code', 'x_exp_date'); |
|
| 417 | + foreach ($request_array as $index => $keyvaltogether) { |
|
| 418 | + foreach ($keys_to_filter_out as $key) { |
|
| 419 | + if (strpos($keyvaltogether, $key) === 0) { |
|
| 420 | + // found it at the first character |
|
| 421 | + // so its one of them |
|
| 422 | + unset($request_array[ $index ]); |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + $this->log( |
|
| 427 | + array( |
|
| 428 | + 'AIM Request sent:' => $request_array, |
|
| 429 | + 'Server URL' => $this->_get_server_url() |
|
| 430 | + ), |
|
| 431 | + $payment |
|
| 432 | + ); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Logs the response and cleans it |
|
| 439 | + * |
|
| 440 | + * @param EE_AuthorizeNetAIM_Response $response_obj |
|
| 441 | + * @param EE_Payment $payment |
|
| 442 | + * @return \EE_AuthorizeNetAIM_Response |
|
| 443 | + */ |
|
| 444 | + private function _log_and_clean_response($response_obj, $payment) |
|
| 445 | + { |
|
| 446 | + $response_obj->account_number = ''; |
|
| 447 | + $this->log(array('AIM Response received:' => (array) $response_obj), $payment); |
|
| 448 | + return $response_obj; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Removes characters Authorize.net doesn't handle well. |
|
| 453 | + * @since 4.9.82.p |
|
| 454 | + * @param $text |
|
| 455 | + * @return string |
|
| 456 | + */ |
|
| 457 | + private function prepareStringForAuthnet($text) |
|
| 458 | + { |
|
| 459 | + return str_replace( |
|
| 460 | + '\'', |
|
| 461 | + '', |
|
| 462 | + $text |
|
| 463 | + ); |
|
| 464 | + } |
|
| 465 | 465 | } |
| 466 | 466 | |
| 467 | 467 | |
@@ -477,192 +477,192 @@ discard block |
||
| 477 | 477 | class EE_AuthorizeNetAIM_Response |
| 478 | 478 | { |
| 479 | 479 | |
| 480 | - const APPROVED = '1'; |
|
| 481 | - const DECLINED = '2'; |
|
| 482 | - const ERROR = '3'; |
|
| 483 | - const HELD = '4'; |
|
| 484 | - |
|
| 485 | - protected $_x_post_fields = array( |
|
| 486 | - "version" => "3.1", |
|
| 487 | - "delim_char" => ",", |
|
| 488 | - "delim_data" => "TRUE", |
|
| 489 | - "relay_response" => "FALSE", |
|
| 490 | - "encap_char" => "|", |
|
| 491 | - ); |
|
| 492 | - public $approved; |
|
| 493 | - public $declined; |
|
| 494 | - public $error; |
|
| 495 | - public $held; |
|
| 496 | - public $response_code; |
|
| 497 | - public $response_subcode; |
|
| 498 | - public $response_reason_code; |
|
| 499 | - public $response_reason_text; |
|
| 500 | - public $authorization_code; |
|
| 501 | - public $avs_response; |
|
| 502 | - public $transaction_id; |
|
| 503 | - public $invoice_number; |
|
| 504 | - public $description; |
|
| 505 | - public $amount; |
|
| 506 | - public $method; |
|
| 507 | - public $transaction_type; |
|
| 508 | - public $customer_id; |
|
| 509 | - public $first_name; |
|
| 510 | - public $last_name; |
|
| 511 | - public $company; |
|
| 512 | - public $address; |
|
| 513 | - public $city; |
|
| 514 | - public $state; |
|
| 515 | - public $zip_code; |
|
| 516 | - public $country; |
|
| 517 | - public $phone; |
|
| 518 | - public $fax; |
|
| 519 | - public $email_address; |
|
| 520 | - public $ship_to_first_name; |
|
| 521 | - public $ship_to_last_name; |
|
| 522 | - public $ship_to_company; |
|
| 523 | - public $ship_to_address; |
|
| 524 | - public $ship_to_city; |
|
| 525 | - public $ship_to_state; |
|
| 526 | - public $ship_to_zip_code; |
|
| 527 | - public $ship_to_country; |
|
| 528 | - public $tax; |
|
| 529 | - public $duty; |
|
| 530 | - public $freight; |
|
| 531 | - public $tax_exempt; |
|
| 532 | - public $purchase_order_number; |
|
| 533 | - public $md5_hash; |
|
| 534 | - public $card_code_response; |
|
| 535 | - public $cavv_response; // cardholder_authentication_verification_response |
|
| 536 | - public $account_number; |
|
| 537 | - public $card_type; |
|
| 538 | - public $split_tender_id; |
|
| 539 | - public $requested_amount; |
|
| 540 | - public $balance_on_card; |
|
| 541 | - public $response; // The response string from AuthorizeNet. |
|
| 542 | - public $error_message; |
|
| 543 | - private $_response_array = array(); // An array with the split response. |
|
| 544 | - |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Constructor. Parses the AuthorizeNet response string |
|
| 548 | - * |
|
| 549 | - * @param string $response The response from the AuthNet server. |
|
| 550 | - * @var string $delimiter The delimiter used (default is ",") |
|
| 551 | - * @var string $encap_char The encap_char used (default is "|") |
|
| 552 | - * @var array $custom_fields Any custom fields set in the request. |
|
| 553 | - */ |
|
| 554 | - |
|
| 555 | - public function __construct($response) |
|
| 556 | - { |
|
| 557 | - $encap_char = $this->_x_post_fields['encap_char']; |
|
| 558 | - $delimiter = $this->_x_post_fields['delim_char']; |
|
| 559 | - if ($response) { |
|
| 560 | - // Split Array |
|
| 561 | - $this->response = $response; |
|
| 562 | - if ($encap_char) { |
|
| 563 | - $this->_response_array = explode($encap_char . $delimiter . $encap_char, substr($response, 1, -1)); |
|
| 564 | - } else { |
|
| 565 | - $this->_response_array = explode($delimiter, $response); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * If AuthorizeNet doesn't return a delimited response. |
|
| 570 | - */ |
|
| 571 | - if (count($this->_response_array) < 10) { |
|
| 572 | - $this->approved = false; |
|
| 573 | - $this->error = true; |
|
| 574 | - $this->error_message = sprintf( |
|
| 575 | - esc_html__('Unrecognized response from Authorize.net: %1$s', 'event_espresso'), |
|
| 576 | - esc_html($response) |
|
| 577 | - ); |
|
| 578 | - return; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - |
|
| 582 | - |
|
| 583 | - // Set all fields |
|
| 584 | - $this->response_code = $this->_response_array[0]; |
|
| 585 | - $this->response_subcode = $this->_response_array[1]; |
|
| 586 | - $this->response_reason_code = $this->_response_array[2]; |
|
| 587 | - $this->response_reason_text = $this->_response_array[3]; |
|
| 588 | - $this->authorization_code = $this->_response_array[4]; |
|
| 589 | - $this->avs_response = $this->_response_array[5]; |
|
| 590 | - $this->transaction_id = $this->_response_array[6]; |
|
| 591 | - $this->invoice_number = $this->_response_array[7]; |
|
| 592 | - $this->description = $this->_response_array[8]; |
|
| 593 | - $this->amount = $this->_response_array[9]; |
|
| 594 | - $this->method = $this->_response_array[10]; |
|
| 595 | - $this->transaction_type = $this->_response_array[11]; |
|
| 596 | - $this->customer_id = $this->_response_array[12]; |
|
| 597 | - $this->first_name = $this->_response_array[13]; |
|
| 598 | - $this->last_name = $this->_response_array[14]; |
|
| 599 | - $this->company = $this->_response_array[15]; |
|
| 600 | - $this->address = $this->_response_array[16]; |
|
| 601 | - $this->city = $this->_response_array[17]; |
|
| 602 | - $this->state = $this->_response_array[18]; |
|
| 603 | - $this->zip_code = $this->_response_array[19]; |
|
| 604 | - $this->country = $this->_response_array[20]; |
|
| 605 | - $this->phone = $this->_response_array[21]; |
|
| 606 | - $this->fax = $this->_response_array[22]; |
|
| 607 | - $this->email_address = $this->_response_array[23]; |
|
| 608 | - $this->ship_to_first_name = $this->_response_array[24]; |
|
| 609 | - $this->ship_to_last_name = $this->_response_array[25]; |
|
| 610 | - $this->ship_to_company = $this->_response_array[26]; |
|
| 611 | - $this->ship_to_address = $this->_response_array[27]; |
|
| 612 | - $this->ship_to_city = $this->_response_array[28]; |
|
| 613 | - $this->ship_to_state = $this->_response_array[29]; |
|
| 614 | - $this->ship_to_zip_code = $this->_response_array[30]; |
|
| 615 | - $this->ship_to_country = $this->_response_array[31]; |
|
| 616 | - $this->tax = $this->_response_array[32]; |
|
| 617 | - $this->duty = $this->_response_array[33]; |
|
| 618 | - $this->freight = $this->_response_array[34]; |
|
| 619 | - $this->tax_exempt = $this->_response_array[35]; |
|
| 620 | - $this->purchase_order_number = $this->_response_array[36]; |
|
| 621 | - $this->md5_hash = $this->_response_array[37]; |
|
| 622 | - $this->card_code_response = $this->_response_array[38]; |
|
| 623 | - $this->cavv_response = $this->_response_array[39]; |
|
| 624 | - $this->account_number = $this->_response_array[50]; |
|
| 625 | - $this->card_type = $this->_response_array[51]; |
|
| 626 | - $this->split_tender_id = $this->_response_array[52]; |
|
| 627 | - $this->requested_amount = $this->_response_array[53]; |
|
| 628 | - $this->balance_on_card = $this->_response_array[54]; |
|
| 629 | - |
|
| 630 | - $this->approved = ($this->response_code === self::APPROVED); |
|
| 631 | - $this->declined = ($this->response_code === self::DECLINED); |
|
| 632 | - $this->error = ($this->response_code === self::ERROR); |
|
| 633 | - $this->held = ($this->response_code === self::HELD); |
|
| 634 | - } else { |
|
| 635 | - $this->approved = false; |
|
| 636 | - $this->error = true; |
|
| 637 | - $this->error_message = esc_html__( |
|
| 638 | - 'Error connecting to Authorize.net', |
|
| 639 | - 'event_espresso' |
|
| 640 | - ); |
|
| 641 | - } |
|
| 642 | - } |
|
| 480 | + const APPROVED = '1'; |
|
| 481 | + const DECLINED = '2'; |
|
| 482 | + const ERROR = '3'; |
|
| 483 | + const HELD = '4'; |
|
| 484 | + |
|
| 485 | + protected $_x_post_fields = array( |
|
| 486 | + "version" => "3.1", |
|
| 487 | + "delim_char" => ",", |
|
| 488 | + "delim_data" => "TRUE", |
|
| 489 | + "relay_response" => "FALSE", |
|
| 490 | + "encap_char" => "|", |
|
| 491 | + ); |
|
| 492 | + public $approved; |
|
| 493 | + public $declined; |
|
| 494 | + public $error; |
|
| 495 | + public $held; |
|
| 496 | + public $response_code; |
|
| 497 | + public $response_subcode; |
|
| 498 | + public $response_reason_code; |
|
| 499 | + public $response_reason_text; |
|
| 500 | + public $authorization_code; |
|
| 501 | + public $avs_response; |
|
| 502 | + public $transaction_id; |
|
| 503 | + public $invoice_number; |
|
| 504 | + public $description; |
|
| 505 | + public $amount; |
|
| 506 | + public $method; |
|
| 507 | + public $transaction_type; |
|
| 508 | + public $customer_id; |
|
| 509 | + public $first_name; |
|
| 510 | + public $last_name; |
|
| 511 | + public $company; |
|
| 512 | + public $address; |
|
| 513 | + public $city; |
|
| 514 | + public $state; |
|
| 515 | + public $zip_code; |
|
| 516 | + public $country; |
|
| 517 | + public $phone; |
|
| 518 | + public $fax; |
|
| 519 | + public $email_address; |
|
| 520 | + public $ship_to_first_name; |
|
| 521 | + public $ship_to_last_name; |
|
| 522 | + public $ship_to_company; |
|
| 523 | + public $ship_to_address; |
|
| 524 | + public $ship_to_city; |
|
| 525 | + public $ship_to_state; |
|
| 526 | + public $ship_to_zip_code; |
|
| 527 | + public $ship_to_country; |
|
| 528 | + public $tax; |
|
| 529 | + public $duty; |
|
| 530 | + public $freight; |
|
| 531 | + public $tax_exempt; |
|
| 532 | + public $purchase_order_number; |
|
| 533 | + public $md5_hash; |
|
| 534 | + public $card_code_response; |
|
| 535 | + public $cavv_response; // cardholder_authentication_verification_response |
|
| 536 | + public $account_number; |
|
| 537 | + public $card_type; |
|
| 538 | + public $split_tender_id; |
|
| 539 | + public $requested_amount; |
|
| 540 | + public $balance_on_card; |
|
| 541 | + public $response; // The response string from AuthorizeNet. |
|
| 542 | + public $error_message; |
|
| 543 | + private $_response_array = array(); // An array with the split response. |
|
| 544 | + |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Constructor. Parses the AuthorizeNet response string |
|
| 548 | + * |
|
| 549 | + * @param string $response The response from the AuthNet server. |
|
| 550 | + * @var string $delimiter The delimiter used (default is ",") |
|
| 551 | + * @var string $encap_char The encap_char used (default is "|") |
|
| 552 | + * @var array $custom_fields Any custom fields set in the request. |
|
| 553 | + */ |
|
| 554 | + |
|
| 555 | + public function __construct($response) |
|
| 556 | + { |
|
| 557 | + $encap_char = $this->_x_post_fields['encap_char']; |
|
| 558 | + $delimiter = $this->_x_post_fields['delim_char']; |
|
| 559 | + if ($response) { |
|
| 560 | + // Split Array |
|
| 561 | + $this->response = $response; |
|
| 562 | + if ($encap_char) { |
|
| 563 | + $this->_response_array = explode($encap_char . $delimiter . $encap_char, substr($response, 1, -1)); |
|
| 564 | + } else { |
|
| 565 | + $this->_response_array = explode($delimiter, $response); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * If AuthorizeNet doesn't return a delimited response. |
|
| 570 | + */ |
|
| 571 | + if (count($this->_response_array) < 10) { |
|
| 572 | + $this->approved = false; |
|
| 573 | + $this->error = true; |
|
| 574 | + $this->error_message = sprintf( |
|
| 575 | + esc_html__('Unrecognized response from Authorize.net: %1$s', 'event_espresso'), |
|
| 576 | + esc_html($response) |
|
| 577 | + ); |
|
| 578 | + return; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + |
|
| 582 | + |
|
| 583 | + // Set all fields |
|
| 584 | + $this->response_code = $this->_response_array[0]; |
|
| 585 | + $this->response_subcode = $this->_response_array[1]; |
|
| 586 | + $this->response_reason_code = $this->_response_array[2]; |
|
| 587 | + $this->response_reason_text = $this->_response_array[3]; |
|
| 588 | + $this->authorization_code = $this->_response_array[4]; |
|
| 589 | + $this->avs_response = $this->_response_array[5]; |
|
| 590 | + $this->transaction_id = $this->_response_array[6]; |
|
| 591 | + $this->invoice_number = $this->_response_array[7]; |
|
| 592 | + $this->description = $this->_response_array[8]; |
|
| 593 | + $this->amount = $this->_response_array[9]; |
|
| 594 | + $this->method = $this->_response_array[10]; |
|
| 595 | + $this->transaction_type = $this->_response_array[11]; |
|
| 596 | + $this->customer_id = $this->_response_array[12]; |
|
| 597 | + $this->first_name = $this->_response_array[13]; |
|
| 598 | + $this->last_name = $this->_response_array[14]; |
|
| 599 | + $this->company = $this->_response_array[15]; |
|
| 600 | + $this->address = $this->_response_array[16]; |
|
| 601 | + $this->city = $this->_response_array[17]; |
|
| 602 | + $this->state = $this->_response_array[18]; |
|
| 603 | + $this->zip_code = $this->_response_array[19]; |
|
| 604 | + $this->country = $this->_response_array[20]; |
|
| 605 | + $this->phone = $this->_response_array[21]; |
|
| 606 | + $this->fax = $this->_response_array[22]; |
|
| 607 | + $this->email_address = $this->_response_array[23]; |
|
| 608 | + $this->ship_to_first_name = $this->_response_array[24]; |
|
| 609 | + $this->ship_to_last_name = $this->_response_array[25]; |
|
| 610 | + $this->ship_to_company = $this->_response_array[26]; |
|
| 611 | + $this->ship_to_address = $this->_response_array[27]; |
|
| 612 | + $this->ship_to_city = $this->_response_array[28]; |
|
| 613 | + $this->ship_to_state = $this->_response_array[29]; |
|
| 614 | + $this->ship_to_zip_code = $this->_response_array[30]; |
|
| 615 | + $this->ship_to_country = $this->_response_array[31]; |
|
| 616 | + $this->tax = $this->_response_array[32]; |
|
| 617 | + $this->duty = $this->_response_array[33]; |
|
| 618 | + $this->freight = $this->_response_array[34]; |
|
| 619 | + $this->tax_exempt = $this->_response_array[35]; |
|
| 620 | + $this->purchase_order_number = $this->_response_array[36]; |
|
| 621 | + $this->md5_hash = $this->_response_array[37]; |
|
| 622 | + $this->card_code_response = $this->_response_array[38]; |
|
| 623 | + $this->cavv_response = $this->_response_array[39]; |
|
| 624 | + $this->account_number = $this->_response_array[50]; |
|
| 625 | + $this->card_type = $this->_response_array[51]; |
|
| 626 | + $this->split_tender_id = $this->_response_array[52]; |
|
| 627 | + $this->requested_amount = $this->_response_array[53]; |
|
| 628 | + $this->balance_on_card = $this->_response_array[54]; |
|
| 629 | + |
|
| 630 | + $this->approved = ($this->response_code === self::APPROVED); |
|
| 631 | + $this->declined = ($this->response_code === self::DECLINED); |
|
| 632 | + $this->error = ($this->response_code === self::ERROR); |
|
| 633 | + $this->held = ($this->response_code === self::HELD); |
|
| 634 | + } else { |
|
| 635 | + $this->approved = false; |
|
| 636 | + $this->error = true; |
|
| 637 | + $this->error_message = esc_html__( |
|
| 638 | + 'Error connecting to Authorize.net', |
|
| 639 | + 'event_espresso' |
|
| 640 | + ); |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | 643 | } |
| 644 | 644 | |
| 645 | 645 | if (! class_exists('AuthorizeNetException')) { |
| 646 | - /** |
|
| 647 | - * Class AuthorizeNetException |
|
| 648 | - * |
|
| 649 | - * @package AuthorizeNet |
|
| 650 | - */ |
|
| 651 | - class AuthorizeNetException extends Exception |
|
| 652 | - { |
|
| 653 | - |
|
| 654 | - /** |
|
| 655 | - * Construct the exception. Note: The message is NOT binary safe. |
|
| 656 | - * |
|
| 657 | - * @link http://php.net/manual/en/exception.construct.php |
|
| 658 | - * @param string $message [optional] The Exception message to throw. |
|
| 659 | - * @param int $code [optional] The Exception code. |
|
| 660 | - * @param Exception $previous [optional] The previous exception used for the exception chaining. Since 5.3.0 |
|
| 661 | - * @since 5.1.0 |
|
| 662 | - */ |
|
| 663 | - public function __construct($message = "", $code = 0, Exception $previous = null) |
|
| 664 | - { |
|
| 665 | - parent::__construct($message, $code, $previous); |
|
| 666 | - } |
|
| 667 | - } |
|
| 646 | + /** |
|
| 647 | + * Class AuthorizeNetException |
|
| 648 | + * |
|
| 649 | + * @package AuthorizeNet |
|
| 650 | + */ |
|
| 651 | + class AuthorizeNetException extends Exception |
|
| 652 | + { |
|
| 653 | + |
|
| 654 | + /** |
|
| 655 | + * Construct the exception. Note: The message is NOT binary safe. |
|
| 656 | + * |
|
| 657 | + * @link http://php.net/manual/en/exception.construct.php |
|
| 658 | + * @param string $message [optional] The Exception message to throw. |
|
| 659 | + * @param int $code [optional] The Exception code. |
|
| 660 | + * @param Exception $previous [optional] The previous exception used for the exception chaining. Since 5.3.0 |
|
| 661 | + * @since 5.1.0 |
|
| 662 | + */ |
|
| 663 | + public function __construct($message = "", $code = 0, Exception $previous = null) |
|
| 664 | + { |
|
| 665 | + parent::__construct($message, $code, $previous); |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | 668 | } |
@@ -21,139 +21,139 @@ |
||
| 21 | 21 | class EE_Event_List_Shortcodes extends EE_Shortcodes |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - public function __construct() |
|
| 25 | - { |
|
| 26 | - parent::__construct(); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - protected function _init_props() |
|
| 31 | - { |
|
| 32 | - $this->label = __('Event List Shortcodes', 'event_espresso'); |
|
| 33 | - $this->description = __('All shortcodes specific to event lists', 'event_espresso'); |
|
| 34 | - $this->_shortcodes = array( |
|
| 35 | - '[EVENT_LIST]' => __('Will output a list of events', 'event_espresso'), |
|
| 36 | - ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - protected function _parser($shortcode) |
|
| 41 | - { |
|
| 42 | - switch ($shortcode) { |
|
| 43 | - case '[EVENT_LIST]': |
|
| 44 | - return $this->_get_event_list(); |
|
| 45 | - break; |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * figure out what the incoming data is and then return the appropriate parsed value. |
|
| 52 | - * |
|
| 53 | - * @return string |
|
| 54 | - */ |
|
| 55 | - private function _get_event_list() |
|
| 56 | - { |
|
| 57 | - $this->_validate_list_requirements(); |
|
| 58 | - |
|
| 59 | - if ($this->_data['data'] instanceof EE_Messages_Addressee) { |
|
| 60 | - return $this->_get_event_list_for_main(); |
|
| 61 | - } elseif ($this->_data['data'] instanceof EE_Registration) { |
|
| 62 | - return $this->_get_event_list_for_registration(); |
|
| 63 | - } // prevent recursive loop |
|
| 64 | - else { |
|
| 65 | - return ''; |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * This returns the parsed event list for main template |
|
| 72 | - * |
|
| 73 | - * @return string |
|
| 74 | - */ |
|
| 75 | - private function _get_event_list_for_main() |
|
| 76 | - { |
|
| 77 | - |
|
| 78 | - $valid_shortcodes = array( |
|
| 79 | - 'event', |
|
| 80 | - 'attendee_list', |
|
| 81 | - 'ticket_list', |
|
| 82 | - 'datetime_list', |
|
| 83 | - 'venue', |
|
| 84 | - 'attendee', |
|
| 85 | - 'recipient_list', |
|
| 86 | - 'recipient_details', |
|
| 87 | - 'primary_registration_list', |
|
| 88 | - 'primary_registration_details', |
|
| 89 | - 'event_author', |
|
| 90 | - 'organization', |
|
| 91 | - ); |
|
| 92 | - $template = $this->_data['template']; |
|
| 93 | - $data = $this->_data['data']; |
|
| 94 | - $events = ''; |
|
| 95 | - |
|
| 96 | - // now we need to loop through the events array in EE_Messages_Addressee and send data to the EE_Parser helper. |
|
| 97 | - foreach ($data->events as $event) { |
|
| 98 | - $events .= $this->_shortcode_helper->parse_event_list_template( |
|
| 99 | - $template, |
|
| 100 | - $event['event'], |
|
| 101 | - $valid_shortcodes, |
|
| 102 | - $this->_extra_data |
|
| 103 | - ); |
|
| 104 | - } |
|
| 105 | - return $events; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * This returns the parsed event list for an attendee |
|
| 111 | - * |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - private function _get_event_list_for_registration() |
|
| 115 | - { |
|
| 116 | - $valid_shortcodes = array( |
|
| 117 | - 'event', |
|
| 118 | - 'ticket_list', |
|
| 119 | - 'datetime_list', |
|
| 120 | - 'attendee', |
|
| 121 | - 'event_author', |
|
| 122 | - 'recipient_details', |
|
| 123 | - 'recipient_list', |
|
| 124 | - 'venue', |
|
| 125 | - 'organization', |
|
| 126 | - ); |
|
| 127 | - $template = is_array($this->_data['template']) && isset($this->_data['template']['event_list']) |
|
| 128 | - ? $this->_data['template']['event_list'] : $this->_extra_data['template']['event_list']; |
|
| 129 | - $registration = $this->_data['data']; |
|
| 130 | - |
|
| 131 | - // let's remove any existing [ATTENDEE_LIST] shortcode from the event list template so that we don't get recursion. |
|
| 132 | - $template = str_replace('[ATTENDEE_LIST]', '', $template); |
|
| 133 | - |
|
| 134 | - // here we're setting up the events for the event_list template for THIS registration. |
|
| 135 | - $evt_result = ''; |
|
| 136 | - $all_events = $this->_get_events_from_registration($registration); |
|
| 137 | - |
|
| 138 | - // we're NOT going to prepare a list of attendees this time around |
|
| 139 | - $events = ''; |
|
| 140 | - |
|
| 141 | - foreach ((array) $all_events as $event) { |
|
| 142 | - $events .= $this->_shortcode_helper->parse_event_list_template( |
|
| 143 | - $template, |
|
| 144 | - $event, |
|
| 145 | - $valid_shortcodes, |
|
| 146 | - $this->_extra_data |
|
| 147 | - ); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return $events; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - private function _get_events_from_registration(EE_Registration $registration) |
|
| 155 | - { |
|
| 156 | - return isset($this->_extra_data['data']->registrations) |
|
| 157 | - ? array($this->_extra_data['data']->registrations[ $registration->ID() ]['evt_obj']) : array(); |
|
| 158 | - } |
|
| 24 | + public function __construct() |
|
| 25 | + { |
|
| 26 | + parent::__construct(); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + protected function _init_props() |
|
| 31 | + { |
|
| 32 | + $this->label = __('Event List Shortcodes', 'event_espresso'); |
|
| 33 | + $this->description = __('All shortcodes specific to event lists', 'event_espresso'); |
|
| 34 | + $this->_shortcodes = array( |
|
| 35 | + '[EVENT_LIST]' => __('Will output a list of events', 'event_espresso'), |
|
| 36 | + ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + protected function _parser($shortcode) |
|
| 41 | + { |
|
| 42 | + switch ($shortcode) { |
|
| 43 | + case '[EVENT_LIST]': |
|
| 44 | + return $this->_get_event_list(); |
|
| 45 | + break; |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * figure out what the incoming data is and then return the appropriate parsed value. |
|
| 52 | + * |
|
| 53 | + * @return string |
|
| 54 | + */ |
|
| 55 | + private function _get_event_list() |
|
| 56 | + { |
|
| 57 | + $this->_validate_list_requirements(); |
|
| 58 | + |
|
| 59 | + if ($this->_data['data'] instanceof EE_Messages_Addressee) { |
|
| 60 | + return $this->_get_event_list_for_main(); |
|
| 61 | + } elseif ($this->_data['data'] instanceof EE_Registration) { |
|
| 62 | + return $this->_get_event_list_for_registration(); |
|
| 63 | + } // prevent recursive loop |
|
| 64 | + else { |
|
| 65 | + return ''; |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * This returns the parsed event list for main template |
|
| 72 | + * |
|
| 73 | + * @return string |
|
| 74 | + */ |
|
| 75 | + private function _get_event_list_for_main() |
|
| 76 | + { |
|
| 77 | + |
|
| 78 | + $valid_shortcodes = array( |
|
| 79 | + 'event', |
|
| 80 | + 'attendee_list', |
|
| 81 | + 'ticket_list', |
|
| 82 | + 'datetime_list', |
|
| 83 | + 'venue', |
|
| 84 | + 'attendee', |
|
| 85 | + 'recipient_list', |
|
| 86 | + 'recipient_details', |
|
| 87 | + 'primary_registration_list', |
|
| 88 | + 'primary_registration_details', |
|
| 89 | + 'event_author', |
|
| 90 | + 'organization', |
|
| 91 | + ); |
|
| 92 | + $template = $this->_data['template']; |
|
| 93 | + $data = $this->_data['data']; |
|
| 94 | + $events = ''; |
|
| 95 | + |
|
| 96 | + // now we need to loop through the events array in EE_Messages_Addressee and send data to the EE_Parser helper. |
|
| 97 | + foreach ($data->events as $event) { |
|
| 98 | + $events .= $this->_shortcode_helper->parse_event_list_template( |
|
| 99 | + $template, |
|
| 100 | + $event['event'], |
|
| 101 | + $valid_shortcodes, |
|
| 102 | + $this->_extra_data |
|
| 103 | + ); |
|
| 104 | + } |
|
| 105 | + return $events; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * This returns the parsed event list for an attendee |
|
| 111 | + * |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + private function _get_event_list_for_registration() |
|
| 115 | + { |
|
| 116 | + $valid_shortcodes = array( |
|
| 117 | + 'event', |
|
| 118 | + 'ticket_list', |
|
| 119 | + 'datetime_list', |
|
| 120 | + 'attendee', |
|
| 121 | + 'event_author', |
|
| 122 | + 'recipient_details', |
|
| 123 | + 'recipient_list', |
|
| 124 | + 'venue', |
|
| 125 | + 'organization', |
|
| 126 | + ); |
|
| 127 | + $template = is_array($this->_data['template']) && isset($this->_data['template']['event_list']) |
|
| 128 | + ? $this->_data['template']['event_list'] : $this->_extra_data['template']['event_list']; |
|
| 129 | + $registration = $this->_data['data']; |
|
| 130 | + |
|
| 131 | + // let's remove any existing [ATTENDEE_LIST] shortcode from the event list template so that we don't get recursion. |
|
| 132 | + $template = str_replace('[ATTENDEE_LIST]', '', $template); |
|
| 133 | + |
|
| 134 | + // here we're setting up the events for the event_list template for THIS registration. |
|
| 135 | + $evt_result = ''; |
|
| 136 | + $all_events = $this->_get_events_from_registration($registration); |
|
| 137 | + |
|
| 138 | + // we're NOT going to prepare a list of attendees this time around |
|
| 139 | + $events = ''; |
|
| 140 | + |
|
| 141 | + foreach ((array) $all_events as $event) { |
|
| 142 | + $events .= $this->_shortcode_helper->parse_event_list_template( |
|
| 143 | + $template, |
|
| 144 | + $event, |
|
| 145 | + $valid_shortcodes, |
|
| 146 | + $this->_extra_data |
|
| 147 | + ); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return $events; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + private function _get_events_from_registration(EE_Registration $registration) |
|
| 155 | + { |
|
| 156 | + return isset($this->_extra_data['data']->registrations) |
|
| 157 | + ? array($this->_extra_data['data']->registrations[ $registration->ID() ]['evt_obj']) : array(); |
|
| 158 | + } |
|
| 159 | 159 | } |
@@ -18,181 +18,181 @@ |
||
| 18 | 18 | class RequestTypeContextDetector |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var RequestTypeContextFactoryInterface $factory |
|
| 23 | - */ |
|
| 24 | - private $factory; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var RequestInterface $request |
|
| 28 | - */ |
|
| 29 | - private $request; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var array $globalRouteConditions |
|
| 33 | - */ |
|
| 34 | - private $globalRouteConditions; |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * RequestTypeContextDetector constructor. |
|
| 39 | - * |
|
| 40 | - * @param RequestInterface $request |
|
| 41 | - * @param RequestTypeContextFactoryInterface $factory |
|
| 42 | - * @param array $globalRouteConditions an array for injecting values that would |
|
| 43 | - * otherwise be defined as global constants |
|
| 44 | - * or other global variables for the current |
|
| 45 | - * request route such as DOING_AJAX |
|
| 46 | - */ |
|
| 47 | - public function __construct( |
|
| 48 | - RequestInterface $request, |
|
| 49 | - RequestTypeContextFactoryInterface $factory, |
|
| 50 | - array $globalRouteConditions = array() |
|
| 51 | - ) { |
|
| 52 | - $this->request = $request; |
|
| 53 | - $this->factory = $factory; |
|
| 54 | - $this->globalRouteConditions = $globalRouteConditions; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @return mixed |
|
| 60 | - */ |
|
| 61 | - private function getGlobalRouteCondition($globalRouteCondition, $default) |
|
| 62 | - { |
|
| 63 | - return isset($this->globalRouteConditions[ $globalRouteCondition ]) |
|
| 64 | - ? $this->globalRouteConditions[ $globalRouteCondition ] |
|
| 65 | - : $default; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return RequestTypeContext |
|
| 71 | - * @throws InvalidArgumentException |
|
| 72 | - */ |
|
| 73 | - public function detectRequestTypeContext() |
|
| 74 | - { |
|
| 75 | - // Detect error scrapes |
|
| 76 | - if ($this->request->getRequestParam('wp_scrape_key') !== null |
|
| 77 | - && $this->request->getRequestParam('wp_scrape_nonce') !== null |
|
| 78 | - ) { |
|
| 79 | - return $this->factory->create(RequestTypeContext::WP_SCRAPE); |
|
| 80 | - } |
|
| 81 | - // Detect EE REST API |
|
| 82 | - if ($this->isEspressoRestApiRequest()) { |
|
| 83 | - return $this->factory->create(RequestTypeContext::API); |
|
| 84 | - } |
|
| 85 | - // Detect WP REST API |
|
| 86 | - if ($this->isWordPressRestApiRequest()) { |
|
| 87 | - return $this->factory->create(RequestTypeContext::WP_API); |
|
| 88 | - } |
|
| 89 | - // Detect AJAX |
|
| 90 | - if ($this->getGlobalRouteCondition('DOING_AJAX', false)) { |
|
| 91 | - if (filter_var($this->request->getRequestParam('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
| 92 | - return $this->factory->create(RequestTypeContext::AJAX_FRONT); |
|
| 93 | - } |
|
| 94 | - if (filter_var($this->request->getRequestParam('ee_admin_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
| 95 | - return $this->factory->create(RequestTypeContext::AJAX_ADMIN); |
|
| 96 | - } |
|
| 97 | - if ($this->request->getRequestParam('action') === 'heartbeat') { |
|
| 98 | - return $this->factory->create(RequestTypeContext::AJAX_HEARTBEAT); |
|
| 99 | - } |
|
| 100 | - return $this->factory->create(RequestTypeContext::AJAX_OTHER); |
|
| 101 | - } |
|
| 102 | - // Detect WP_Cron |
|
| 103 | - if ($this->isCronRequest()) { |
|
| 104 | - return $this->factory->create(RequestTypeContext::CRON); |
|
| 105 | - } |
|
| 106 | - // Detect command line requests |
|
| 107 | - if ($this->getGlobalRouteCondition('WP_CLI', false)) { |
|
| 108 | - return $this->factory->create(RequestTypeContext::CLI); |
|
| 109 | - } |
|
| 110 | - // detect WordPress admin (ie: "Dashboard") |
|
| 111 | - if ($this->getGlobalRouteCondition('is_admin', false)) { |
|
| 112 | - return $this->factory->create(RequestTypeContext::ADMIN); |
|
| 113 | - } |
|
| 114 | - // Detect iFrames |
|
| 115 | - if ($this->isIframeRoute()) { |
|
| 116 | - return $this->factory->create(RequestTypeContext::IFRAME); |
|
| 117 | - } |
|
| 118 | - // Detect Feeds |
|
| 119 | - if ($this->isFeedRequest()) { |
|
| 120 | - return $this->factory->create(RequestTypeContext::FEED); |
|
| 121 | - } |
|
| 122 | - // and by process of elimination... |
|
| 123 | - return $this->factory->create(RequestTypeContext::FRONTEND); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @return bool |
|
| 129 | - */ |
|
| 130 | - private function isEspressoRestApiRequest() |
|
| 131 | - { |
|
| 132 | - // Check for URLs like http://mysite.com/?rest_route=/ee... and http://mysite.com/wp-json/ee/... |
|
| 133 | - return strpos( |
|
| 134 | - $this->request->getRequestParam('rest_route', false), |
|
| 135 | - '/' . Domain::API_NAMESPACE |
|
| 136 | - ) === 0 |
|
| 137 | - || $this->uriPathMatches(trim(rest_get_url_prefix(), '/') . '/' . Domain::API_NAMESPACE); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @return bool |
|
| 144 | - */ |
|
| 145 | - private function isWordPressRestApiRequest() |
|
| 146 | - { |
|
| 147 | - // Check for URLs like http://mysite.com/?rest_route=/.. and http://mysite.com/wp-json/... |
|
| 148 | - return $this->request->getRequestParam('rest_route', false) |
|
| 149 | - || $this->uriPathMatches(trim(rest_get_url_prefix(), '/')); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @return bool |
|
| 155 | - */ |
|
| 156 | - private function isCronRequest() |
|
| 157 | - { |
|
| 158 | - return $this->uriPathMatches('wp-cron.php'); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @return bool |
|
| 164 | - */ |
|
| 165 | - private function isFeedRequest() |
|
| 166 | - { |
|
| 167 | - return $this->uriPathMatches('feed'); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @param string $component |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - private function uriPathMatches($component) |
|
| 176 | - { |
|
| 177 | - $request_uri = $this->request->requestUri(true); |
|
| 178 | - $parts = explode('?', $request_uri); |
|
| 179 | - $path = trim(reset($parts), '/'); |
|
| 180 | - return strpos($path, $component) === 0; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @return bool |
|
| 186 | - */ |
|
| 187 | - private function isIframeRoute() |
|
| 188 | - { |
|
| 189 | - $is_iframe_route = apply_filters( |
|
| 190 | - 'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute', |
|
| 191 | - $this->request->getRequestParam('event_list', '') === 'iframe' |
|
| 192 | - || $this->request->getRequestParam('ticket_selector', '') === 'iframe' |
|
| 193 | - || $this->request->getRequestParam('calendar', '') === 'iframe', |
|
| 194 | - $this |
|
| 195 | - ); |
|
| 196 | - return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN); |
|
| 197 | - } |
|
| 21 | + /** |
|
| 22 | + * @var RequestTypeContextFactoryInterface $factory |
|
| 23 | + */ |
|
| 24 | + private $factory; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var RequestInterface $request |
|
| 28 | + */ |
|
| 29 | + private $request; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var array $globalRouteConditions |
|
| 33 | + */ |
|
| 34 | + private $globalRouteConditions; |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * RequestTypeContextDetector constructor. |
|
| 39 | + * |
|
| 40 | + * @param RequestInterface $request |
|
| 41 | + * @param RequestTypeContextFactoryInterface $factory |
|
| 42 | + * @param array $globalRouteConditions an array for injecting values that would |
|
| 43 | + * otherwise be defined as global constants |
|
| 44 | + * or other global variables for the current |
|
| 45 | + * request route such as DOING_AJAX |
|
| 46 | + */ |
|
| 47 | + public function __construct( |
|
| 48 | + RequestInterface $request, |
|
| 49 | + RequestTypeContextFactoryInterface $factory, |
|
| 50 | + array $globalRouteConditions = array() |
|
| 51 | + ) { |
|
| 52 | + $this->request = $request; |
|
| 53 | + $this->factory = $factory; |
|
| 54 | + $this->globalRouteConditions = $globalRouteConditions; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @return mixed |
|
| 60 | + */ |
|
| 61 | + private function getGlobalRouteCondition($globalRouteCondition, $default) |
|
| 62 | + { |
|
| 63 | + return isset($this->globalRouteConditions[ $globalRouteCondition ]) |
|
| 64 | + ? $this->globalRouteConditions[ $globalRouteCondition ] |
|
| 65 | + : $default; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return RequestTypeContext |
|
| 71 | + * @throws InvalidArgumentException |
|
| 72 | + */ |
|
| 73 | + public function detectRequestTypeContext() |
|
| 74 | + { |
|
| 75 | + // Detect error scrapes |
|
| 76 | + if ($this->request->getRequestParam('wp_scrape_key') !== null |
|
| 77 | + && $this->request->getRequestParam('wp_scrape_nonce') !== null |
|
| 78 | + ) { |
|
| 79 | + return $this->factory->create(RequestTypeContext::WP_SCRAPE); |
|
| 80 | + } |
|
| 81 | + // Detect EE REST API |
|
| 82 | + if ($this->isEspressoRestApiRequest()) { |
|
| 83 | + return $this->factory->create(RequestTypeContext::API); |
|
| 84 | + } |
|
| 85 | + // Detect WP REST API |
|
| 86 | + if ($this->isWordPressRestApiRequest()) { |
|
| 87 | + return $this->factory->create(RequestTypeContext::WP_API); |
|
| 88 | + } |
|
| 89 | + // Detect AJAX |
|
| 90 | + if ($this->getGlobalRouteCondition('DOING_AJAX', false)) { |
|
| 91 | + if (filter_var($this->request->getRequestParam('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
| 92 | + return $this->factory->create(RequestTypeContext::AJAX_FRONT); |
|
| 93 | + } |
|
| 94 | + if (filter_var($this->request->getRequestParam('ee_admin_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
| 95 | + return $this->factory->create(RequestTypeContext::AJAX_ADMIN); |
|
| 96 | + } |
|
| 97 | + if ($this->request->getRequestParam('action') === 'heartbeat') { |
|
| 98 | + return $this->factory->create(RequestTypeContext::AJAX_HEARTBEAT); |
|
| 99 | + } |
|
| 100 | + return $this->factory->create(RequestTypeContext::AJAX_OTHER); |
|
| 101 | + } |
|
| 102 | + // Detect WP_Cron |
|
| 103 | + if ($this->isCronRequest()) { |
|
| 104 | + return $this->factory->create(RequestTypeContext::CRON); |
|
| 105 | + } |
|
| 106 | + // Detect command line requests |
|
| 107 | + if ($this->getGlobalRouteCondition('WP_CLI', false)) { |
|
| 108 | + return $this->factory->create(RequestTypeContext::CLI); |
|
| 109 | + } |
|
| 110 | + // detect WordPress admin (ie: "Dashboard") |
|
| 111 | + if ($this->getGlobalRouteCondition('is_admin', false)) { |
|
| 112 | + return $this->factory->create(RequestTypeContext::ADMIN); |
|
| 113 | + } |
|
| 114 | + // Detect iFrames |
|
| 115 | + if ($this->isIframeRoute()) { |
|
| 116 | + return $this->factory->create(RequestTypeContext::IFRAME); |
|
| 117 | + } |
|
| 118 | + // Detect Feeds |
|
| 119 | + if ($this->isFeedRequest()) { |
|
| 120 | + return $this->factory->create(RequestTypeContext::FEED); |
|
| 121 | + } |
|
| 122 | + // and by process of elimination... |
|
| 123 | + return $this->factory->create(RequestTypeContext::FRONTEND); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @return bool |
|
| 129 | + */ |
|
| 130 | + private function isEspressoRestApiRequest() |
|
| 131 | + { |
|
| 132 | + // Check for URLs like http://mysite.com/?rest_route=/ee... and http://mysite.com/wp-json/ee/... |
|
| 133 | + return strpos( |
|
| 134 | + $this->request->getRequestParam('rest_route', false), |
|
| 135 | + '/' . Domain::API_NAMESPACE |
|
| 136 | + ) === 0 |
|
| 137 | + || $this->uriPathMatches(trim(rest_get_url_prefix(), '/') . '/' . Domain::API_NAMESPACE); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @return bool |
|
| 144 | + */ |
|
| 145 | + private function isWordPressRestApiRequest() |
|
| 146 | + { |
|
| 147 | + // Check for URLs like http://mysite.com/?rest_route=/.. and http://mysite.com/wp-json/... |
|
| 148 | + return $this->request->getRequestParam('rest_route', false) |
|
| 149 | + || $this->uriPathMatches(trim(rest_get_url_prefix(), '/')); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @return bool |
|
| 155 | + */ |
|
| 156 | + private function isCronRequest() |
|
| 157 | + { |
|
| 158 | + return $this->uriPathMatches('wp-cron.php'); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @return bool |
|
| 164 | + */ |
|
| 165 | + private function isFeedRequest() |
|
| 166 | + { |
|
| 167 | + return $this->uriPathMatches('feed'); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @param string $component |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + private function uriPathMatches($component) |
|
| 176 | + { |
|
| 177 | + $request_uri = $this->request->requestUri(true); |
|
| 178 | + $parts = explode('?', $request_uri); |
|
| 179 | + $path = trim(reset($parts), '/'); |
|
| 180 | + return strpos($path, $component) === 0; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @return bool |
|
| 186 | + */ |
|
| 187 | + private function isIframeRoute() |
|
| 188 | + { |
|
| 189 | + $is_iframe_route = apply_filters( |
|
| 190 | + 'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute', |
|
| 191 | + $this->request->getRequestParam('event_list', '') === 'iframe' |
|
| 192 | + || $this->request->getRequestParam('ticket_selector', '') === 'iframe' |
|
| 193 | + || $this->request->getRequestParam('calendar', '') === 'iframe', |
|
| 194 | + $this |
|
| 195 | + ); |
|
| 196 | + return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN); |
|
| 197 | + } |
|
| 198 | 198 | } |
@@ -17,640 +17,640 @@ |
||
| 17 | 17 | class Request implements InterminableInterface, RequestInterface, ReservedInstanceInterface |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * $_GET parameters |
|
| 22 | - * |
|
| 23 | - * @var array $get |
|
| 24 | - */ |
|
| 25 | - private $get; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * $_POST parameters |
|
| 29 | - * |
|
| 30 | - * @var array $post |
|
| 31 | - */ |
|
| 32 | - private $post; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * $_COOKIE parameters |
|
| 36 | - * |
|
| 37 | - * @var array $cookie |
|
| 38 | - */ |
|
| 39 | - private $cookie; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * $_SERVER parameters |
|
| 43 | - * |
|
| 44 | - * @var array $server |
|
| 45 | - */ |
|
| 46 | - private $server; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * $_FILES parameters |
|
| 50 | - * |
|
| 51 | - * @var array $files |
|
| 52 | - */ |
|
| 53 | - private $files; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * $_REQUEST parameters |
|
| 57 | - * |
|
| 58 | - * @var array $request |
|
| 59 | - */ |
|
| 60 | - private $request; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var RequestTypeContextCheckerInterface |
|
| 64 | - */ |
|
| 65 | - private $request_type; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * IP address for request |
|
| 69 | - * |
|
| 70 | - * @var string $ip_address |
|
| 71 | - */ |
|
| 72 | - private $ip_address; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @var string $user_agent |
|
| 76 | - */ |
|
| 77 | - private $user_agent; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * true if current user appears to be some kind of bot |
|
| 81 | - * |
|
| 82 | - * @var bool $is_bot |
|
| 83 | - */ |
|
| 84 | - private $is_bot; |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @param array $get |
|
| 89 | - * @param array $post |
|
| 90 | - * @param array $cookie |
|
| 91 | - * @param array $server |
|
| 92 | - * @param array $files |
|
| 93 | - */ |
|
| 94 | - public function __construct(array $get, array $post, array $cookie, array $server, array $files = array()) |
|
| 95 | - { |
|
| 96 | - // grab request vars |
|
| 97 | - $this->get = $get; |
|
| 98 | - $this->post = $post; |
|
| 99 | - $this->cookie = $cookie; |
|
| 100 | - $this->server = $server; |
|
| 101 | - $this->files = $files; |
|
| 102 | - $this->request = array_merge($this->get, $this->post); |
|
| 103 | - $this->ip_address = $this->visitorIp(); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param RequestTypeContextCheckerInterface $type |
|
| 109 | - */ |
|
| 110 | - public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
|
| 111 | - { |
|
| 112 | - $this->request_type = $type; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @return array |
|
| 118 | - */ |
|
| 119 | - public function getParams() |
|
| 120 | - { |
|
| 121 | - return $this->get; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - public function postParams() |
|
| 129 | - { |
|
| 130 | - return $this->post; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @return array |
|
| 136 | - */ |
|
| 137 | - public function cookieParams() |
|
| 138 | - { |
|
| 139 | - return $this->cookie; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @return array |
|
| 145 | - */ |
|
| 146 | - public function serverParams() |
|
| 147 | - { |
|
| 148 | - return $this->server; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @return array |
|
| 154 | - */ |
|
| 155 | - public function filesParams() |
|
| 156 | - { |
|
| 157 | - return $this->files; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * returns contents of $_REQUEST |
|
| 163 | - * |
|
| 164 | - * @return array |
|
| 165 | - */ |
|
| 166 | - public function requestParams() |
|
| 167 | - { |
|
| 168 | - return $this->request; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @param $key |
|
| 174 | - * @param $value |
|
| 175 | - * @param bool $override_ee |
|
| 176 | - * @return void |
|
| 177 | - */ |
|
| 178 | - public function setRequestParam($key, $value, $override_ee = false) |
|
| 179 | - { |
|
| 180 | - // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 181 | - if ($key !== 'ee' |
|
| 182 | - || ($key === 'ee' && empty($this->request['ee'])) |
|
| 183 | - || ($key === 'ee' && ! empty($this->request['ee']) && $override_ee) |
|
| 184 | - ) { |
|
| 185 | - $this->request[ $key ] = $value; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * returns the value for a request param if the given key exists |
|
| 192 | - * |
|
| 193 | - * @param $key |
|
| 194 | - * @param null $default |
|
| 195 | - * @return mixed |
|
| 196 | - */ |
|
| 197 | - public function getRequestParam($key, $default = null) |
|
| 198 | - { |
|
| 199 | - return $this->requestParameterDrillDown($key, $default, 'get'); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * check if param exists |
|
| 205 | - * |
|
| 206 | - * @param $key |
|
| 207 | - * @return bool |
|
| 208 | - */ |
|
| 209 | - public function requestParamIsSet($key) |
|
| 210 | - { |
|
| 211 | - return $this->requestParameterDrillDown($key); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * check if a request parameter exists whose key that matches the supplied wildcard pattern |
|
| 217 | - * and return the value for the first match found |
|
| 218 | - * wildcards can be either of the following: |
|
| 219 | - * ? to represent a single character of any type |
|
| 220 | - * * to represent one or more characters of any type |
|
| 221 | - * |
|
| 222 | - * @param string $pattern |
|
| 223 | - * @param null|mixed $default |
|
| 224 | - * @return mixed |
|
| 225 | - */ |
|
| 226 | - public function getMatch($pattern, $default = null) |
|
| 227 | - { |
|
| 228 | - return $this->requestParameterDrillDown($pattern, $default, 'match'); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * check if a request parameter exists whose key matches the supplied wildcard pattern |
|
| 234 | - * wildcards can be either of the following: |
|
| 235 | - * ? to represent a single character of any type |
|
| 236 | - * * to represent one or more characters of any type |
|
| 237 | - * returns true if a match is found or false if not |
|
| 238 | - * |
|
| 239 | - * @param string $pattern |
|
| 240 | - * @return bool |
|
| 241 | - */ |
|
| 242 | - public function matches($pattern) |
|
| 243 | - { |
|
| 244 | - return $this->requestParameterDrillDown($pattern, null, 'match') !== null; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
|
| 250 | - * @param string $pattern A string including wildcards to be converted to a regex pattern |
|
| 251 | - * and used to search through the current request's parameter keys |
|
| 252 | - * @param array $request_params The array of request parameters to search through |
|
| 253 | - * @param mixed $default [optional] The value to be returned if no match is found. |
|
| 254 | - * Default is null |
|
| 255 | - * @param string $return [optional] Controls what kind of value is returned. |
|
| 256 | - * Options are: |
|
| 257 | - * 'bool' will return true or false if match is found or not |
|
| 258 | - * 'key' will return the first key found that matches the supplied pattern |
|
| 259 | - * 'value' will return the value for the first request parameter |
|
| 260 | - * whose key matches the supplied pattern |
|
| 261 | - * Default is 'value' |
|
| 262 | - * @return boolean|string |
|
| 263 | - */ |
|
| 264 | - private function match($pattern, array $request_params, $default = null, $return = 'value') |
|
| 265 | - { |
|
| 266 | - $return = in_array($return, array('bool', 'key', 'value'), true) |
|
| 267 | - ? $return |
|
| 268 | - : 'is_set'; |
|
| 269 | - // replace wildcard chars with regex chars |
|
| 270 | - $pattern = str_replace( |
|
| 271 | - array("\*", "\?"), |
|
| 272 | - array('.*', '.'), |
|
| 273 | - preg_quote($pattern, '/') |
|
| 274 | - ); |
|
| 275 | - foreach ($request_params as $key => $request_param) { |
|
| 276 | - if (preg_match('/^' . $pattern . '$/is', $key)) { |
|
| 277 | - // return value for request param |
|
| 278 | - if ($return === 'value') { |
|
| 279 | - return $request_params[ $key ]; |
|
| 280 | - } |
|
| 281 | - // or actual key or true just to indicate it was found |
|
| 282 | - return $return === 'key' ? $key : true; |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - // match not found so return default value or false |
|
| 286 | - return $return === 'value' ? $default : false; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 292 | - * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 293 | - * by using square brackets to surround keys for deeper array elements. |
|
| 294 | - * For example : |
|
| 295 | - * if the supplied $key was: "first[second][third]" |
|
| 296 | - * then this will attempt to drill down into the request parameter array to find a value. |
|
| 297 | - * Given the following request parameters: |
|
| 298 | - * array( |
|
| 299 | - * 'first' => array( |
|
| 300 | - * 'second' => array( |
|
| 301 | - * 'third' => 'has a value' |
|
| 302 | - * ) |
|
| 303 | - * ) |
|
| 304 | - * ) |
|
| 305 | - * would return true if default parameters were set |
|
| 306 | - * |
|
| 307 | - * @param string $callback |
|
| 308 | - * @param $key |
|
| 309 | - * @param null $default |
|
| 310 | - * @param array $request_params |
|
| 311 | - * @return bool|mixed|null |
|
| 312 | - */ |
|
| 313 | - private function requestParameterDrillDown( |
|
| 314 | - $key, |
|
| 315 | - $default = null, |
|
| 316 | - $callback = 'is_set', |
|
| 317 | - array $request_params = array() |
|
| 318 | - ) { |
|
| 319 | - $callback = in_array($callback, array('is_set', 'get', 'match'), true) |
|
| 320 | - ? $callback |
|
| 321 | - : 'is_set'; |
|
| 322 | - $request_params = ! empty($request_params) |
|
| 323 | - ? $request_params |
|
| 324 | - : $this->request; |
|
| 325 | - // does incoming key represent an array like 'first[second][third]' ? |
|
| 326 | - if (strpos($key, '[') !== false) { |
|
| 327 | - // turn it into an actual array |
|
| 328 | - $key = str_replace(']', '', $key); |
|
| 329 | - $keys = explode('[', $key); |
|
| 330 | - $key = array_shift($keys); |
|
| 331 | - if ($callback === 'match') { |
|
| 332 | - $real_key = $this->match($key, $request_params, $default, 'key'); |
|
| 333 | - $key = $real_key ? $real_key : $key; |
|
| 334 | - } |
|
| 335 | - // check if top level key exists |
|
| 336 | - if (isset($request_params[ $key ])) { |
|
| 337 | - // build a new key to pass along like: 'second[third]' |
|
| 338 | - // or just 'second' depending on depth of keys |
|
| 339 | - $key_string = array_shift($keys); |
|
| 340 | - if (! empty($keys)) { |
|
| 341 | - $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 342 | - } |
|
| 343 | - return $this->requestParameterDrillDown( |
|
| 344 | - $key_string, |
|
| 345 | - $default, |
|
| 346 | - $callback, |
|
| 347 | - $request_params[ $key ] |
|
| 348 | - ); |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - if ($callback === 'is_set') { |
|
| 352 | - return isset($request_params[ $key ]); |
|
| 353 | - } |
|
| 354 | - if ($callback === 'match') { |
|
| 355 | - return $this->match($key, $request_params, $default); |
|
| 356 | - } |
|
| 357 | - return isset($request_params[ $key ]) |
|
| 358 | - ? $request_params[ $key ] |
|
| 359 | - : $default; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * remove param |
|
| 365 | - * |
|
| 366 | - * @param $key |
|
| 367 | - * @param bool $unset_from_global_too |
|
| 368 | - */ |
|
| 369 | - public function unSetRequestParam($key, $unset_from_global_too = false) |
|
| 370 | - { |
|
| 371 | - unset($this->request[ $key ]); |
|
| 372 | - if ($unset_from_global_too) { |
|
| 373 | - unset($_REQUEST[ $key ]); |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * @return string |
|
| 380 | - */ |
|
| 381 | - public function ipAddress() |
|
| 382 | - { |
|
| 383 | - return $this->ip_address; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * attempt to get IP address of current visitor from server |
|
| 389 | - * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 390 | - * |
|
| 391 | - * @access public |
|
| 392 | - * @return string |
|
| 393 | - */ |
|
| 394 | - private function visitorIp() |
|
| 395 | - { |
|
| 396 | - $visitor_ip = '0.0.0.0'; |
|
| 397 | - $server_keys = array( |
|
| 398 | - 'HTTP_CLIENT_IP', |
|
| 399 | - 'HTTP_X_FORWARDED_FOR', |
|
| 400 | - 'HTTP_X_FORWARDED', |
|
| 401 | - 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 402 | - 'HTTP_FORWARDED_FOR', |
|
| 403 | - 'HTTP_FORWARDED', |
|
| 404 | - 'REMOTE_ADDR', |
|
| 405 | - ); |
|
| 406 | - foreach ($server_keys as $key) { |
|
| 407 | - if (isset($this->server[ $key ])) { |
|
| 408 | - foreach (array_map('trim', explode(',', $this->server[ $key ])) as $ip) { |
|
| 409 | - if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 410 | - $visitor_ip = $ip; |
|
| 411 | - } |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - return $visitor_ip; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Gets the request's literal URI. Related to `requestUriAfterSiteHomeUri`, see its description for a comparison. |
|
| 421 | - * @param boolean $relativeToWpRoot If home_url() is "http://mysite.com/wp/", and a request comes to |
|
| 422 | - * "http://mysite.com/wp/wp-json", setting $relativeToWpRoot=true will return |
|
| 423 | - * "/wp-json", whereas $relativeToWpRoot=false will return "/wp/wp-json/". |
|
| 424 | - * @return string |
|
| 425 | - */ |
|
| 426 | - public function requestUri($relativeToWpRoot = false) |
|
| 427 | - { |
|
| 428 | - $request_uri = filter_input( |
|
| 429 | - INPUT_SERVER, |
|
| 430 | - 'REQUEST_URI', |
|
| 431 | - FILTER_SANITIZE_URL, |
|
| 432 | - FILTER_NULL_ON_FAILURE |
|
| 433 | - ); |
|
| 434 | - if (empty($request_uri)) { |
|
| 435 | - // fallback sanitization if the above fails |
|
| 436 | - $request_uri = wp_sanitize_redirect($this->server['REQUEST_URI']); |
|
| 437 | - } |
|
| 438 | - if ($relativeToWpRoot) { |
|
| 439 | - $home_path = untrailingslashit( |
|
| 440 | - parse_url( |
|
| 441 | - home_url(), |
|
| 442 | - PHP_URL_PATH |
|
| 443 | - ) |
|
| 444 | - ); |
|
| 445 | - $request_uri = str_replace( |
|
| 446 | - $home_path, |
|
| 447 | - '', |
|
| 448 | - $request_uri |
|
| 449 | - ); |
|
| 450 | - } |
|
| 451 | - return $request_uri; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * @return string |
|
| 456 | - */ |
|
| 457 | - public function userAgent() |
|
| 458 | - { |
|
| 459 | - return $this->user_agent; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * @param string $user_agent |
|
| 465 | - */ |
|
| 466 | - public function setUserAgent($user_agent = '') |
|
| 467 | - { |
|
| 468 | - if ($user_agent === '' || ! is_string($user_agent)) { |
|
| 469 | - $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string) esc_attr($_SERVER['HTTP_USER_AGENT']) : ''; |
|
| 470 | - } |
|
| 471 | - $this->user_agent = $user_agent; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * @return bool |
|
| 477 | - */ |
|
| 478 | - public function isBot() |
|
| 479 | - { |
|
| 480 | - return $this->is_bot; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * @param bool $is_bot |
|
| 486 | - */ |
|
| 487 | - public function setIsBot($is_bot) |
|
| 488 | - { |
|
| 489 | - $this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN); |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * @return bool |
|
| 495 | - */ |
|
| 496 | - public function isActivation() |
|
| 497 | - { |
|
| 498 | - return $this->request_type->isActivation(); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * @param $is_activation |
|
| 504 | - * @return bool |
|
| 505 | - */ |
|
| 506 | - public function setIsActivation($is_activation) |
|
| 507 | - { |
|
| 508 | - return $this->request_type->setIsActivation($is_activation); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * @return bool |
|
| 514 | - */ |
|
| 515 | - public function isAdmin() |
|
| 516 | - { |
|
| 517 | - return $this->request_type->isAdmin(); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - |
|
| 521 | - /** |
|
| 522 | - * @return bool |
|
| 523 | - */ |
|
| 524 | - public function isAdminAjax() |
|
| 525 | - { |
|
| 526 | - return $this->request_type->isAdminAjax(); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * @return bool |
|
| 532 | - */ |
|
| 533 | - public function isAjax() |
|
| 534 | - { |
|
| 535 | - return $this->request_type->isAjax(); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - |
|
| 539 | - /** |
|
| 540 | - * @return bool |
|
| 541 | - */ |
|
| 542 | - public function isEeAjax() |
|
| 543 | - { |
|
| 544 | - return $this->request_type->isEeAjax(); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * @return bool |
|
| 550 | - */ |
|
| 551 | - public function isOtherAjax() |
|
| 552 | - { |
|
| 553 | - return $this->request_type->isOtherAjax(); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * @return bool |
|
| 559 | - */ |
|
| 560 | - public function isApi() |
|
| 561 | - { |
|
| 562 | - return $this->request_type->isApi(); |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * @return bool |
|
| 568 | - */ |
|
| 569 | - public function isCli() |
|
| 570 | - { |
|
| 571 | - return $this->request_type->isCli(); |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * @return bool |
|
| 577 | - */ |
|
| 578 | - public function isCron() |
|
| 579 | - { |
|
| 580 | - return $this->request_type->isCron(); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * @return bool |
|
| 586 | - */ |
|
| 587 | - public function isFeed() |
|
| 588 | - { |
|
| 589 | - return $this->request_type->isFeed(); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * @return bool |
|
| 595 | - */ |
|
| 596 | - public function isFrontend() |
|
| 597 | - { |
|
| 598 | - return $this->request_type->isFrontend(); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * @return bool |
|
| 604 | - */ |
|
| 605 | - public function isFrontAjax() |
|
| 606 | - { |
|
| 607 | - return $this->request_type->isFrontAjax(); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * @return bool |
|
| 613 | - */ |
|
| 614 | - public function isIframe() |
|
| 615 | - { |
|
| 616 | - return $this->request_type->isIframe(); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * @return bool |
|
| 622 | - */ |
|
| 623 | - public function isWordPressApi() |
|
| 624 | - { |
|
| 625 | - return $this->request_type->isWordPressApi(); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * @return bool |
|
| 632 | - */ |
|
| 633 | - public function isWordPressHeartbeat() |
|
| 634 | - { |
|
| 635 | - return $this->request_type->isWordPressHeartbeat(); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * @return bool |
|
| 642 | - */ |
|
| 643 | - public function isWordPressScrape() |
|
| 644 | - { |
|
| 645 | - return $this->request_type->isWordPressScrape(); |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * @return string |
|
| 651 | - */ |
|
| 652 | - public function slug() |
|
| 653 | - { |
|
| 654 | - return $this->request_type->slug(); |
|
| 655 | - } |
|
| 20 | + /** |
|
| 21 | + * $_GET parameters |
|
| 22 | + * |
|
| 23 | + * @var array $get |
|
| 24 | + */ |
|
| 25 | + private $get; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * $_POST parameters |
|
| 29 | + * |
|
| 30 | + * @var array $post |
|
| 31 | + */ |
|
| 32 | + private $post; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * $_COOKIE parameters |
|
| 36 | + * |
|
| 37 | + * @var array $cookie |
|
| 38 | + */ |
|
| 39 | + private $cookie; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * $_SERVER parameters |
|
| 43 | + * |
|
| 44 | + * @var array $server |
|
| 45 | + */ |
|
| 46 | + private $server; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * $_FILES parameters |
|
| 50 | + * |
|
| 51 | + * @var array $files |
|
| 52 | + */ |
|
| 53 | + private $files; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * $_REQUEST parameters |
|
| 57 | + * |
|
| 58 | + * @var array $request |
|
| 59 | + */ |
|
| 60 | + private $request; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var RequestTypeContextCheckerInterface |
|
| 64 | + */ |
|
| 65 | + private $request_type; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * IP address for request |
|
| 69 | + * |
|
| 70 | + * @var string $ip_address |
|
| 71 | + */ |
|
| 72 | + private $ip_address; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @var string $user_agent |
|
| 76 | + */ |
|
| 77 | + private $user_agent; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * true if current user appears to be some kind of bot |
|
| 81 | + * |
|
| 82 | + * @var bool $is_bot |
|
| 83 | + */ |
|
| 84 | + private $is_bot; |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @param array $get |
|
| 89 | + * @param array $post |
|
| 90 | + * @param array $cookie |
|
| 91 | + * @param array $server |
|
| 92 | + * @param array $files |
|
| 93 | + */ |
|
| 94 | + public function __construct(array $get, array $post, array $cookie, array $server, array $files = array()) |
|
| 95 | + { |
|
| 96 | + // grab request vars |
|
| 97 | + $this->get = $get; |
|
| 98 | + $this->post = $post; |
|
| 99 | + $this->cookie = $cookie; |
|
| 100 | + $this->server = $server; |
|
| 101 | + $this->files = $files; |
|
| 102 | + $this->request = array_merge($this->get, $this->post); |
|
| 103 | + $this->ip_address = $this->visitorIp(); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param RequestTypeContextCheckerInterface $type |
|
| 109 | + */ |
|
| 110 | + public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
|
| 111 | + { |
|
| 112 | + $this->request_type = $type; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @return array |
|
| 118 | + */ |
|
| 119 | + public function getParams() |
|
| 120 | + { |
|
| 121 | + return $this->get; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + public function postParams() |
|
| 129 | + { |
|
| 130 | + return $this->post; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @return array |
|
| 136 | + */ |
|
| 137 | + public function cookieParams() |
|
| 138 | + { |
|
| 139 | + return $this->cookie; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @return array |
|
| 145 | + */ |
|
| 146 | + public function serverParams() |
|
| 147 | + { |
|
| 148 | + return $this->server; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @return array |
|
| 154 | + */ |
|
| 155 | + public function filesParams() |
|
| 156 | + { |
|
| 157 | + return $this->files; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * returns contents of $_REQUEST |
|
| 163 | + * |
|
| 164 | + * @return array |
|
| 165 | + */ |
|
| 166 | + public function requestParams() |
|
| 167 | + { |
|
| 168 | + return $this->request; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @param $key |
|
| 174 | + * @param $value |
|
| 175 | + * @param bool $override_ee |
|
| 176 | + * @return void |
|
| 177 | + */ |
|
| 178 | + public function setRequestParam($key, $value, $override_ee = false) |
|
| 179 | + { |
|
| 180 | + // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 181 | + if ($key !== 'ee' |
|
| 182 | + || ($key === 'ee' && empty($this->request['ee'])) |
|
| 183 | + || ($key === 'ee' && ! empty($this->request['ee']) && $override_ee) |
|
| 184 | + ) { |
|
| 185 | + $this->request[ $key ] = $value; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * returns the value for a request param if the given key exists |
|
| 192 | + * |
|
| 193 | + * @param $key |
|
| 194 | + * @param null $default |
|
| 195 | + * @return mixed |
|
| 196 | + */ |
|
| 197 | + public function getRequestParam($key, $default = null) |
|
| 198 | + { |
|
| 199 | + return $this->requestParameterDrillDown($key, $default, 'get'); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * check if param exists |
|
| 205 | + * |
|
| 206 | + * @param $key |
|
| 207 | + * @return bool |
|
| 208 | + */ |
|
| 209 | + public function requestParamIsSet($key) |
|
| 210 | + { |
|
| 211 | + return $this->requestParameterDrillDown($key); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * check if a request parameter exists whose key that matches the supplied wildcard pattern |
|
| 217 | + * and return the value for the first match found |
|
| 218 | + * wildcards can be either of the following: |
|
| 219 | + * ? to represent a single character of any type |
|
| 220 | + * * to represent one or more characters of any type |
|
| 221 | + * |
|
| 222 | + * @param string $pattern |
|
| 223 | + * @param null|mixed $default |
|
| 224 | + * @return mixed |
|
| 225 | + */ |
|
| 226 | + public function getMatch($pattern, $default = null) |
|
| 227 | + { |
|
| 228 | + return $this->requestParameterDrillDown($pattern, $default, 'match'); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * check if a request parameter exists whose key matches the supplied wildcard pattern |
|
| 234 | + * wildcards can be either of the following: |
|
| 235 | + * ? to represent a single character of any type |
|
| 236 | + * * to represent one or more characters of any type |
|
| 237 | + * returns true if a match is found or false if not |
|
| 238 | + * |
|
| 239 | + * @param string $pattern |
|
| 240 | + * @return bool |
|
| 241 | + */ |
|
| 242 | + public function matches($pattern) |
|
| 243 | + { |
|
| 244 | + return $this->requestParameterDrillDown($pattern, null, 'match') !== null; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
|
| 250 | + * @param string $pattern A string including wildcards to be converted to a regex pattern |
|
| 251 | + * and used to search through the current request's parameter keys |
|
| 252 | + * @param array $request_params The array of request parameters to search through |
|
| 253 | + * @param mixed $default [optional] The value to be returned if no match is found. |
|
| 254 | + * Default is null |
|
| 255 | + * @param string $return [optional] Controls what kind of value is returned. |
|
| 256 | + * Options are: |
|
| 257 | + * 'bool' will return true or false if match is found or not |
|
| 258 | + * 'key' will return the first key found that matches the supplied pattern |
|
| 259 | + * 'value' will return the value for the first request parameter |
|
| 260 | + * whose key matches the supplied pattern |
|
| 261 | + * Default is 'value' |
|
| 262 | + * @return boolean|string |
|
| 263 | + */ |
|
| 264 | + private function match($pattern, array $request_params, $default = null, $return = 'value') |
|
| 265 | + { |
|
| 266 | + $return = in_array($return, array('bool', 'key', 'value'), true) |
|
| 267 | + ? $return |
|
| 268 | + : 'is_set'; |
|
| 269 | + // replace wildcard chars with regex chars |
|
| 270 | + $pattern = str_replace( |
|
| 271 | + array("\*", "\?"), |
|
| 272 | + array('.*', '.'), |
|
| 273 | + preg_quote($pattern, '/') |
|
| 274 | + ); |
|
| 275 | + foreach ($request_params as $key => $request_param) { |
|
| 276 | + if (preg_match('/^' . $pattern . '$/is', $key)) { |
|
| 277 | + // return value for request param |
|
| 278 | + if ($return === 'value') { |
|
| 279 | + return $request_params[ $key ]; |
|
| 280 | + } |
|
| 281 | + // or actual key or true just to indicate it was found |
|
| 282 | + return $return === 'key' ? $key : true; |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + // match not found so return default value or false |
|
| 286 | + return $return === 'value' ? $default : false; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 292 | + * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 293 | + * by using square brackets to surround keys for deeper array elements. |
|
| 294 | + * For example : |
|
| 295 | + * if the supplied $key was: "first[second][third]" |
|
| 296 | + * then this will attempt to drill down into the request parameter array to find a value. |
|
| 297 | + * Given the following request parameters: |
|
| 298 | + * array( |
|
| 299 | + * 'first' => array( |
|
| 300 | + * 'second' => array( |
|
| 301 | + * 'third' => 'has a value' |
|
| 302 | + * ) |
|
| 303 | + * ) |
|
| 304 | + * ) |
|
| 305 | + * would return true if default parameters were set |
|
| 306 | + * |
|
| 307 | + * @param string $callback |
|
| 308 | + * @param $key |
|
| 309 | + * @param null $default |
|
| 310 | + * @param array $request_params |
|
| 311 | + * @return bool|mixed|null |
|
| 312 | + */ |
|
| 313 | + private function requestParameterDrillDown( |
|
| 314 | + $key, |
|
| 315 | + $default = null, |
|
| 316 | + $callback = 'is_set', |
|
| 317 | + array $request_params = array() |
|
| 318 | + ) { |
|
| 319 | + $callback = in_array($callback, array('is_set', 'get', 'match'), true) |
|
| 320 | + ? $callback |
|
| 321 | + : 'is_set'; |
|
| 322 | + $request_params = ! empty($request_params) |
|
| 323 | + ? $request_params |
|
| 324 | + : $this->request; |
|
| 325 | + // does incoming key represent an array like 'first[second][third]' ? |
|
| 326 | + if (strpos($key, '[') !== false) { |
|
| 327 | + // turn it into an actual array |
|
| 328 | + $key = str_replace(']', '', $key); |
|
| 329 | + $keys = explode('[', $key); |
|
| 330 | + $key = array_shift($keys); |
|
| 331 | + if ($callback === 'match') { |
|
| 332 | + $real_key = $this->match($key, $request_params, $default, 'key'); |
|
| 333 | + $key = $real_key ? $real_key : $key; |
|
| 334 | + } |
|
| 335 | + // check if top level key exists |
|
| 336 | + if (isset($request_params[ $key ])) { |
|
| 337 | + // build a new key to pass along like: 'second[third]' |
|
| 338 | + // or just 'second' depending on depth of keys |
|
| 339 | + $key_string = array_shift($keys); |
|
| 340 | + if (! empty($keys)) { |
|
| 341 | + $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 342 | + } |
|
| 343 | + return $this->requestParameterDrillDown( |
|
| 344 | + $key_string, |
|
| 345 | + $default, |
|
| 346 | + $callback, |
|
| 347 | + $request_params[ $key ] |
|
| 348 | + ); |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + if ($callback === 'is_set') { |
|
| 352 | + return isset($request_params[ $key ]); |
|
| 353 | + } |
|
| 354 | + if ($callback === 'match') { |
|
| 355 | + return $this->match($key, $request_params, $default); |
|
| 356 | + } |
|
| 357 | + return isset($request_params[ $key ]) |
|
| 358 | + ? $request_params[ $key ] |
|
| 359 | + : $default; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * remove param |
|
| 365 | + * |
|
| 366 | + * @param $key |
|
| 367 | + * @param bool $unset_from_global_too |
|
| 368 | + */ |
|
| 369 | + public function unSetRequestParam($key, $unset_from_global_too = false) |
|
| 370 | + { |
|
| 371 | + unset($this->request[ $key ]); |
|
| 372 | + if ($unset_from_global_too) { |
|
| 373 | + unset($_REQUEST[ $key ]); |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * @return string |
|
| 380 | + */ |
|
| 381 | + public function ipAddress() |
|
| 382 | + { |
|
| 383 | + return $this->ip_address; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * attempt to get IP address of current visitor from server |
|
| 389 | + * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 390 | + * |
|
| 391 | + * @access public |
|
| 392 | + * @return string |
|
| 393 | + */ |
|
| 394 | + private function visitorIp() |
|
| 395 | + { |
|
| 396 | + $visitor_ip = '0.0.0.0'; |
|
| 397 | + $server_keys = array( |
|
| 398 | + 'HTTP_CLIENT_IP', |
|
| 399 | + 'HTTP_X_FORWARDED_FOR', |
|
| 400 | + 'HTTP_X_FORWARDED', |
|
| 401 | + 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 402 | + 'HTTP_FORWARDED_FOR', |
|
| 403 | + 'HTTP_FORWARDED', |
|
| 404 | + 'REMOTE_ADDR', |
|
| 405 | + ); |
|
| 406 | + foreach ($server_keys as $key) { |
|
| 407 | + if (isset($this->server[ $key ])) { |
|
| 408 | + foreach (array_map('trim', explode(',', $this->server[ $key ])) as $ip) { |
|
| 409 | + if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 410 | + $visitor_ip = $ip; |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + return $visitor_ip; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Gets the request's literal URI. Related to `requestUriAfterSiteHomeUri`, see its description for a comparison. |
|
| 421 | + * @param boolean $relativeToWpRoot If home_url() is "http://mysite.com/wp/", and a request comes to |
|
| 422 | + * "http://mysite.com/wp/wp-json", setting $relativeToWpRoot=true will return |
|
| 423 | + * "/wp-json", whereas $relativeToWpRoot=false will return "/wp/wp-json/". |
|
| 424 | + * @return string |
|
| 425 | + */ |
|
| 426 | + public function requestUri($relativeToWpRoot = false) |
|
| 427 | + { |
|
| 428 | + $request_uri = filter_input( |
|
| 429 | + INPUT_SERVER, |
|
| 430 | + 'REQUEST_URI', |
|
| 431 | + FILTER_SANITIZE_URL, |
|
| 432 | + FILTER_NULL_ON_FAILURE |
|
| 433 | + ); |
|
| 434 | + if (empty($request_uri)) { |
|
| 435 | + // fallback sanitization if the above fails |
|
| 436 | + $request_uri = wp_sanitize_redirect($this->server['REQUEST_URI']); |
|
| 437 | + } |
|
| 438 | + if ($relativeToWpRoot) { |
|
| 439 | + $home_path = untrailingslashit( |
|
| 440 | + parse_url( |
|
| 441 | + home_url(), |
|
| 442 | + PHP_URL_PATH |
|
| 443 | + ) |
|
| 444 | + ); |
|
| 445 | + $request_uri = str_replace( |
|
| 446 | + $home_path, |
|
| 447 | + '', |
|
| 448 | + $request_uri |
|
| 449 | + ); |
|
| 450 | + } |
|
| 451 | + return $request_uri; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * @return string |
|
| 456 | + */ |
|
| 457 | + public function userAgent() |
|
| 458 | + { |
|
| 459 | + return $this->user_agent; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * @param string $user_agent |
|
| 465 | + */ |
|
| 466 | + public function setUserAgent($user_agent = '') |
|
| 467 | + { |
|
| 468 | + if ($user_agent === '' || ! is_string($user_agent)) { |
|
| 469 | + $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string) esc_attr($_SERVER['HTTP_USER_AGENT']) : ''; |
|
| 470 | + } |
|
| 471 | + $this->user_agent = $user_agent; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * @return bool |
|
| 477 | + */ |
|
| 478 | + public function isBot() |
|
| 479 | + { |
|
| 480 | + return $this->is_bot; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * @param bool $is_bot |
|
| 486 | + */ |
|
| 487 | + public function setIsBot($is_bot) |
|
| 488 | + { |
|
| 489 | + $this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN); |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * @return bool |
|
| 495 | + */ |
|
| 496 | + public function isActivation() |
|
| 497 | + { |
|
| 498 | + return $this->request_type->isActivation(); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * @param $is_activation |
|
| 504 | + * @return bool |
|
| 505 | + */ |
|
| 506 | + public function setIsActivation($is_activation) |
|
| 507 | + { |
|
| 508 | + return $this->request_type->setIsActivation($is_activation); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * @return bool |
|
| 514 | + */ |
|
| 515 | + public function isAdmin() |
|
| 516 | + { |
|
| 517 | + return $this->request_type->isAdmin(); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + |
|
| 521 | + /** |
|
| 522 | + * @return bool |
|
| 523 | + */ |
|
| 524 | + public function isAdminAjax() |
|
| 525 | + { |
|
| 526 | + return $this->request_type->isAdminAjax(); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * @return bool |
|
| 532 | + */ |
|
| 533 | + public function isAjax() |
|
| 534 | + { |
|
| 535 | + return $this->request_type->isAjax(); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * @return bool |
|
| 541 | + */ |
|
| 542 | + public function isEeAjax() |
|
| 543 | + { |
|
| 544 | + return $this->request_type->isEeAjax(); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * @return bool |
|
| 550 | + */ |
|
| 551 | + public function isOtherAjax() |
|
| 552 | + { |
|
| 553 | + return $this->request_type->isOtherAjax(); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * @return bool |
|
| 559 | + */ |
|
| 560 | + public function isApi() |
|
| 561 | + { |
|
| 562 | + return $this->request_type->isApi(); |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * @return bool |
|
| 568 | + */ |
|
| 569 | + public function isCli() |
|
| 570 | + { |
|
| 571 | + return $this->request_type->isCli(); |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * @return bool |
|
| 577 | + */ |
|
| 578 | + public function isCron() |
|
| 579 | + { |
|
| 580 | + return $this->request_type->isCron(); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * @return bool |
|
| 586 | + */ |
|
| 587 | + public function isFeed() |
|
| 588 | + { |
|
| 589 | + return $this->request_type->isFeed(); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * @return bool |
|
| 595 | + */ |
|
| 596 | + public function isFrontend() |
|
| 597 | + { |
|
| 598 | + return $this->request_type->isFrontend(); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * @return bool |
|
| 604 | + */ |
|
| 605 | + public function isFrontAjax() |
|
| 606 | + { |
|
| 607 | + return $this->request_type->isFrontAjax(); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * @return bool |
|
| 613 | + */ |
|
| 614 | + public function isIframe() |
|
| 615 | + { |
|
| 616 | + return $this->request_type->isIframe(); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * @return bool |
|
| 622 | + */ |
|
| 623 | + public function isWordPressApi() |
|
| 624 | + { |
|
| 625 | + return $this->request_type->isWordPressApi(); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + |
|
| 629 | + |
|
| 630 | + /** |
|
| 631 | + * @return bool |
|
| 632 | + */ |
|
| 633 | + public function isWordPressHeartbeat() |
|
| 634 | + { |
|
| 635 | + return $this->request_type->isWordPressHeartbeat(); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * @return bool |
|
| 642 | + */ |
|
| 643 | + public function isWordPressScrape() |
|
| 644 | + { |
|
| 645 | + return $this->request_type->isWordPressScrape(); |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * @return string |
|
| 651 | + */ |
|
| 652 | + public function slug() |
|
| 653 | + { |
|
| 654 | + return $this->request_type->slug(); |
|
| 655 | + } |
|
| 656 | 656 | } |