@@ -23,560 +23,560 @@ |
||
| 23 | 23 | class Registry |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var AssetCollection $assets |
|
| 30 | - */ |
|
| 31 | - protected $assets; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var I18nRegistry |
|
| 35 | - */ |
|
| 36 | - private $i18n_registry; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $jsdata = array(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
| 47 | - * page source. |
|
| 48 | - * |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - private $script_handles_with_data = array(); |
|
| 26 | + const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var AssetCollection $assets |
|
| 30 | + */ |
|
| 31 | + protected $assets; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var I18nRegistry |
|
| 35 | + */ |
|
| 36 | + private $i18n_registry; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $jsdata = array(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
| 47 | + * page source. |
|
| 48 | + * |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + private $script_handles_with_data = array(); |
|
| 52 | 52 | |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Holds the manifest data obtained from registered manifest files. |
|
| 56 | - * Manifests are maps of asset chunk name to actual built asset file names. |
|
| 57 | - * Shape of this array is: |
|
| 58 | - * array( |
|
| 59 | - * 'some_namespace_slug' => array( |
|
| 60 | - * 'some_chunk_name' => array( |
|
| 61 | - * 'js' => 'filename.js' |
|
| 62 | - * 'css' => 'filename.js' |
|
| 63 | - * ), |
|
| 64 | - * 'url_base' => 'https://baseurl.com/to/assets |
|
| 65 | - * ) |
|
| 66 | - * ) |
|
| 67 | - * |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - private $manifest_data = array(); |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Registry constructor. |
|
| 75 | - * Hooking into WP actions for script registry. |
|
| 76 | - * |
|
| 77 | - * @param AssetCollection $assets |
|
| 78 | - * @param I18nRegistry $i18n_registry |
|
| 79 | - */ |
|
| 80 | - public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
|
| 81 | - { |
|
| 82 | - $this->assets = $assets; |
|
| 83 | - $this->i18n_registry = $i18n_registry; |
|
| 84 | - add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 85 | - add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 86 | - add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 87 | - add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 88 | - add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 89 | - add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 90 | - add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 91 | - add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
|
| 97 | - * translation handling. |
|
| 98 | - * |
|
| 99 | - * @return I18nRegistry |
|
| 100 | - */ |
|
| 101 | - public function getI18nRegistry() |
|
| 102 | - { |
|
| 103 | - return $this->i18n_registry; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Callback for the wp_enqueue_scripts actions used to register assets. |
|
| 109 | - * |
|
| 110 | - * @since $VID:$ |
|
| 111 | - * @throws Exception |
|
| 112 | - */ |
|
| 113 | - public function registerScriptsAndStyles() |
|
| 114 | - { |
|
| 115 | - try { |
|
| 116 | - $this->registerScripts($this->assets->getJavascriptAssets()); |
|
| 117 | - $this->registerStyles($this->assets->getStylesheetAssets()); |
|
| 118 | - } catch (Exception $exception) { |
|
| 119 | - new ExceptionStackTraceDisplay($exception); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Registers JS assets with WP core |
|
| 126 | - * |
|
| 127 | - * @since $VID:$ |
|
| 128 | - * @param JavascriptAsset[] $scripts |
|
| 129 | - * @throws AssetRegistrationException |
|
| 130 | - * @throws InvalidDataTypeException |
|
| 131 | - */ |
|
| 132 | - public function registerScripts(array $scripts) |
|
| 133 | - { |
|
| 134 | - foreach ($scripts as $script) { |
|
| 135 | - // skip to next script if this has already been done |
|
| 136 | - if ($script->isRegistered()) { |
|
| 137 | - continue; |
|
| 138 | - } |
|
| 139 | - do_action( |
|
| 140 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 141 | - $script |
|
| 142 | - ); |
|
| 143 | - $registered = wp_register_script( |
|
| 144 | - $script->handle(), |
|
| 145 | - $script->source(), |
|
| 146 | - $script->dependencies(), |
|
| 147 | - $script->version(), |
|
| 148 | - $script->loadInFooter() |
|
| 149 | - ); |
|
| 150 | - if (defined('EE_DEBUG') && EE_DEBUG && ! $registered) { |
|
| 151 | - throw new AssetRegistrationException($script->handle()); |
|
| 152 | - } |
|
| 153 | - $script->setRegistered($registered); |
|
| 154 | - if ($script->requiresTranslation()) { |
|
| 155 | - $this->registerTranslation($script->handle()); |
|
| 156 | - } |
|
| 157 | - do_action( |
|
| 158 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
|
| 159 | - $script |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Registers CSS assets with WP core |
|
| 167 | - * |
|
| 168 | - * @since $VID:$ |
|
| 169 | - * @param StylesheetAsset[] $styles |
|
| 170 | - * @throws InvalidDataTypeException |
|
| 171 | - */ |
|
| 172 | - public function registerStyles(array $styles) |
|
| 173 | - { |
|
| 174 | - foreach ($styles as $style) { |
|
| 175 | - // skip to next style if this has already been done |
|
| 176 | - if ($style->isRegistered()) { |
|
| 177 | - continue; |
|
| 178 | - } |
|
| 179 | - do_action( |
|
| 180 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
|
| 181 | - $style |
|
| 182 | - ); |
|
| 183 | - wp_enqueue_style( |
|
| 184 | - $style->handle(), |
|
| 185 | - $style->source(), |
|
| 186 | - $style->dependencies(), |
|
| 187 | - $style->version(), |
|
| 188 | - $style->media() |
|
| 189 | - ); |
|
| 190 | - $style->setRegistered(); |
|
| 191 | - do_action( |
|
| 192 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
|
| 193 | - $style |
|
| 194 | - ); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Call back for the script print in frontend and backend. |
|
| 201 | - * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
| 202 | - * |
|
| 203 | - * @since 4.9.31.rc.015 |
|
| 204 | - */ |
|
| 205 | - public function enqueueData() |
|
| 206 | - { |
|
| 207 | - $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
| 208 | - wp_add_inline_script( |
|
| 209 | - 'eejs-core', |
|
| 210 | - 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 211 | - 'before' |
|
| 212 | - ); |
|
| 213 | - $scripts = $this->assets->getJavascriptAssetsWithData(); |
|
| 214 | - foreach ($scripts as $script) { |
|
| 215 | - $this->addRegisteredScriptHandlesWithData($script->handle()); |
|
| 216 | - if ($script->hasLocalizationCallback()) { |
|
| 217 | - $localize = $script->localizationCallback(); |
|
| 218 | - $localize(); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Used to add data to eejs.data object. |
|
| 226 | - * Note: Overriding existing data is not allowed. |
|
| 227 | - * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
| 228 | - * If the data you add is something like this: |
|
| 229 | - * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
| 230 | - * It will be exposed in the page source as: |
|
| 231 | - * eejs.data.my_plugin_data.foo == gar |
|
| 232 | - * |
|
| 233 | - * @param string $key Key used to access your data |
|
| 234 | - * @param string|array $value Value to attach to key |
|
| 235 | - * @throws InvalidArgumentException |
|
| 236 | - */ |
|
| 237 | - public function addData($key, $value) |
|
| 238 | - { |
|
| 239 | - if ($this->verifyDataNotExisting($key)) { |
|
| 240 | - $this->jsdata[ $key ] = $value; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
| 247 | - * elements in an array. |
|
| 248 | - * When you use this method, the value you include will be appended to the end of an array on $key. |
|
| 249 | - * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
|
| 250 | - * object like this, eejs.data.test = [ my_data, |
|
| 251 | - * ] |
|
| 252 | - * If there has already been a scalar value attached to the data object given key, then |
|
| 253 | - * this will throw an exception. |
|
| 254 | - * |
|
| 255 | - * @param string $key Key to attach data to. |
|
| 256 | - * @param string|array $value Value being registered. |
|
| 257 | - * @throws InvalidArgumentException |
|
| 258 | - */ |
|
| 259 | - public function pushData($key, $value) |
|
| 260 | - { |
|
| 261 | - if (isset($this->jsdata[ $key ]) |
|
| 262 | - && ! is_array($this->jsdata[ $key ]) |
|
| 263 | - ) { |
|
| 264 | - throw new InvalidArgumentException( |
|
| 265 | - sprintf( |
|
| 266 | - __( |
|
| 267 | - 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
| 54 | + /** |
|
| 55 | + * Holds the manifest data obtained from registered manifest files. |
|
| 56 | + * Manifests are maps of asset chunk name to actual built asset file names. |
|
| 57 | + * Shape of this array is: |
|
| 58 | + * array( |
|
| 59 | + * 'some_namespace_slug' => array( |
|
| 60 | + * 'some_chunk_name' => array( |
|
| 61 | + * 'js' => 'filename.js' |
|
| 62 | + * 'css' => 'filename.js' |
|
| 63 | + * ), |
|
| 64 | + * 'url_base' => 'https://baseurl.com/to/assets |
|
| 65 | + * ) |
|
| 66 | + * ) |
|
| 67 | + * |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + private $manifest_data = array(); |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Registry constructor. |
|
| 75 | + * Hooking into WP actions for script registry. |
|
| 76 | + * |
|
| 77 | + * @param AssetCollection $assets |
|
| 78 | + * @param I18nRegistry $i18n_registry |
|
| 79 | + */ |
|
| 80 | + public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
|
| 81 | + { |
|
| 82 | + $this->assets = $assets; |
|
| 83 | + $this->i18n_registry = $i18n_registry; |
|
| 84 | + add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 85 | + add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 86 | + add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 87 | + add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 88 | + add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 89 | + add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 90 | + add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 91 | + add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
|
| 97 | + * translation handling. |
|
| 98 | + * |
|
| 99 | + * @return I18nRegistry |
|
| 100 | + */ |
|
| 101 | + public function getI18nRegistry() |
|
| 102 | + { |
|
| 103 | + return $this->i18n_registry; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Callback for the wp_enqueue_scripts actions used to register assets. |
|
| 109 | + * |
|
| 110 | + * @since $VID:$ |
|
| 111 | + * @throws Exception |
|
| 112 | + */ |
|
| 113 | + public function registerScriptsAndStyles() |
|
| 114 | + { |
|
| 115 | + try { |
|
| 116 | + $this->registerScripts($this->assets->getJavascriptAssets()); |
|
| 117 | + $this->registerStyles($this->assets->getStylesheetAssets()); |
|
| 118 | + } catch (Exception $exception) { |
|
| 119 | + new ExceptionStackTraceDisplay($exception); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Registers JS assets with WP core |
|
| 126 | + * |
|
| 127 | + * @since $VID:$ |
|
| 128 | + * @param JavascriptAsset[] $scripts |
|
| 129 | + * @throws AssetRegistrationException |
|
| 130 | + * @throws InvalidDataTypeException |
|
| 131 | + */ |
|
| 132 | + public function registerScripts(array $scripts) |
|
| 133 | + { |
|
| 134 | + foreach ($scripts as $script) { |
|
| 135 | + // skip to next script if this has already been done |
|
| 136 | + if ($script->isRegistered()) { |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 139 | + do_action( |
|
| 140 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 141 | + $script |
|
| 142 | + ); |
|
| 143 | + $registered = wp_register_script( |
|
| 144 | + $script->handle(), |
|
| 145 | + $script->source(), |
|
| 146 | + $script->dependencies(), |
|
| 147 | + $script->version(), |
|
| 148 | + $script->loadInFooter() |
|
| 149 | + ); |
|
| 150 | + if (defined('EE_DEBUG') && EE_DEBUG && ! $registered) { |
|
| 151 | + throw new AssetRegistrationException($script->handle()); |
|
| 152 | + } |
|
| 153 | + $script->setRegistered($registered); |
|
| 154 | + if ($script->requiresTranslation()) { |
|
| 155 | + $this->registerTranslation($script->handle()); |
|
| 156 | + } |
|
| 157 | + do_action( |
|
| 158 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
|
| 159 | + $script |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Registers CSS assets with WP core |
|
| 167 | + * |
|
| 168 | + * @since $VID:$ |
|
| 169 | + * @param StylesheetAsset[] $styles |
|
| 170 | + * @throws InvalidDataTypeException |
|
| 171 | + */ |
|
| 172 | + public function registerStyles(array $styles) |
|
| 173 | + { |
|
| 174 | + foreach ($styles as $style) { |
|
| 175 | + // skip to next style if this has already been done |
|
| 176 | + if ($style->isRegistered()) { |
|
| 177 | + continue; |
|
| 178 | + } |
|
| 179 | + do_action( |
|
| 180 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
|
| 181 | + $style |
|
| 182 | + ); |
|
| 183 | + wp_enqueue_style( |
|
| 184 | + $style->handle(), |
|
| 185 | + $style->source(), |
|
| 186 | + $style->dependencies(), |
|
| 187 | + $style->version(), |
|
| 188 | + $style->media() |
|
| 189 | + ); |
|
| 190 | + $style->setRegistered(); |
|
| 191 | + do_action( |
|
| 192 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
|
| 193 | + $style |
|
| 194 | + ); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Call back for the script print in frontend and backend. |
|
| 201 | + * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
| 202 | + * |
|
| 203 | + * @since 4.9.31.rc.015 |
|
| 204 | + */ |
|
| 205 | + public function enqueueData() |
|
| 206 | + { |
|
| 207 | + $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
| 208 | + wp_add_inline_script( |
|
| 209 | + 'eejs-core', |
|
| 210 | + 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 211 | + 'before' |
|
| 212 | + ); |
|
| 213 | + $scripts = $this->assets->getJavascriptAssetsWithData(); |
|
| 214 | + foreach ($scripts as $script) { |
|
| 215 | + $this->addRegisteredScriptHandlesWithData($script->handle()); |
|
| 216 | + if ($script->hasLocalizationCallback()) { |
|
| 217 | + $localize = $script->localizationCallback(); |
|
| 218 | + $localize(); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Used to add data to eejs.data object. |
|
| 226 | + * Note: Overriding existing data is not allowed. |
|
| 227 | + * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
| 228 | + * If the data you add is something like this: |
|
| 229 | + * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
| 230 | + * It will be exposed in the page source as: |
|
| 231 | + * eejs.data.my_plugin_data.foo == gar |
|
| 232 | + * |
|
| 233 | + * @param string $key Key used to access your data |
|
| 234 | + * @param string|array $value Value to attach to key |
|
| 235 | + * @throws InvalidArgumentException |
|
| 236 | + */ |
|
| 237 | + public function addData($key, $value) |
|
| 238 | + { |
|
| 239 | + if ($this->verifyDataNotExisting($key)) { |
|
| 240 | + $this->jsdata[ $key ] = $value; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
| 247 | + * elements in an array. |
|
| 248 | + * When you use this method, the value you include will be appended to the end of an array on $key. |
|
| 249 | + * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
|
| 250 | + * object like this, eejs.data.test = [ my_data, |
|
| 251 | + * ] |
|
| 252 | + * If there has already been a scalar value attached to the data object given key, then |
|
| 253 | + * this will throw an exception. |
|
| 254 | + * |
|
| 255 | + * @param string $key Key to attach data to. |
|
| 256 | + * @param string|array $value Value being registered. |
|
| 257 | + * @throws InvalidArgumentException |
|
| 258 | + */ |
|
| 259 | + public function pushData($key, $value) |
|
| 260 | + { |
|
| 261 | + if (isset($this->jsdata[ $key ]) |
|
| 262 | + && ! is_array($this->jsdata[ $key ]) |
|
| 263 | + ) { |
|
| 264 | + throw new InvalidArgumentException( |
|
| 265 | + sprintf( |
|
| 266 | + __( |
|
| 267 | + 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
| 268 | 268 | push values to this data element when it is an array.', |
| 269 | - 'event_espresso' |
|
| 270 | - ), |
|
| 271 | - $key, |
|
| 272 | - __METHOD__ |
|
| 273 | - ) |
|
| 274 | - ); |
|
| 275 | - } |
|
| 276 | - $this->jsdata[ $key ][] = $value; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Used to set content used by javascript for a template. |
|
| 282 | - * Note: Overrides of existing registered templates are not allowed. |
|
| 283 | - * |
|
| 284 | - * @param string $template_reference |
|
| 285 | - * @param string $template_content |
|
| 286 | - * @throws InvalidArgumentException |
|
| 287 | - */ |
|
| 288 | - public function addTemplate($template_reference, $template_content) |
|
| 289 | - { |
|
| 290 | - if (! isset($this->jsdata['templates'])) { |
|
| 291 | - $this->jsdata['templates'] = array(); |
|
| 292 | - } |
|
| 293 | - //no overrides allowed. |
|
| 294 | - if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 295 | - throw new InvalidArgumentException( |
|
| 296 | - sprintf( |
|
| 297 | - __( |
|
| 298 | - 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
| 299 | - 'event_espresso' |
|
| 300 | - ), |
|
| 301 | - $template_reference |
|
| 302 | - ) |
|
| 303 | - ); |
|
| 304 | - } |
|
| 305 | - $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Retrieve the template content already registered for the given reference. |
|
| 311 | - * |
|
| 312 | - * @param string $template_reference |
|
| 313 | - * @return string |
|
| 314 | - */ |
|
| 315 | - public function getTemplate($template_reference) |
|
| 316 | - { |
|
| 317 | - return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 318 | - ? $this->jsdata['templates'][ $template_reference ] |
|
| 319 | - : ''; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Retrieve registered data. |
|
| 325 | - * |
|
| 326 | - * @param string $key Name of key to attach data to. |
|
| 327 | - * @return mixed If there is no for the given key, then false is returned. |
|
| 328 | - */ |
|
| 329 | - public function getData($key) |
|
| 330 | - { |
|
| 331 | - return isset($this->jsdata[ $key ]) |
|
| 332 | - ? $this->jsdata[ $key ] |
|
| 333 | - : false; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Verifies whether the given data exists already on the jsdata array. |
|
| 339 | - * Overriding data is not allowed. |
|
| 340 | - * |
|
| 341 | - * @param string $key Index for data. |
|
| 342 | - * @return bool If valid then return true. |
|
| 343 | - * @throws InvalidArgumentException if data already exists. |
|
| 344 | - */ |
|
| 345 | - protected function verifyDataNotExisting($key) |
|
| 346 | - { |
|
| 347 | - if (isset($this->jsdata[ $key ])) { |
|
| 348 | - if (is_array($this->jsdata[ $key ])) { |
|
| 349 | - throw new InvalidArgumentException( |
|
| 350 | - sprintf( |
|
| 351 | - __( |
|
| 352 | - 'The value for %1$s already exists in the Registry::eejs object. |
|
| 269 | + 'event_espresso' |
|
| 270 | + ), |
|
| 271 | + $key, |
|
| 272 | + __METHOD__ |
|
| 273 | + ) |
|
| 274 | + ); |
|
| 275 | + } |
|
| 276 | + $this->jsdata[ $key ][] = $value; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Used to set content used by javascript for a template. |
|
| 282 | + * Note: Overrides of existing registered templates are not allowed. |
|
| 283 | + * |
|
| 284 | + * @param string $template_reference |
|
| 285 | + * @param string $template_content |
|
| 286 | + * @throws InvalidArgumentException |
|
| 287 | + */ |
|
| 288 | + public function addTemplate($template_reference, $template_content) |
|
| 289 | + { |
|
| 290 | + if (! isset($this->jsdata['templates'])) { |
|
| 291 | + $this->jsdata['templates'] = array(); |
|
| 292 | + } |
|
| 293 | + //no overrides allowed. |
|
| 294 | + if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 295 | + throw new InvalidArgumentException( |
|
| 296 | + sprintf( |
|
| 297 | + __( |
|
| 298 | + 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
| 299 | + 'event_espresso' |
|
| 300 | + ), |
|
| 301 | + $template_reference |
|
| 302 | + ) |
|
| 303 | + ); |
|
| 304 | + } |
|
| 305 | + $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Retrieve the template content already registered for the given reference. |
|
| 311 | + * |
|
| 312 | + * @param string $template_reference |
|
| 313 | + * @return string |
|
| 314 | + */ |
|
| 315 | + public function getTemplate($template_reference) |
|
| 316 | + { |
|
| 317 | + return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 318 | + ? $this->jsdata['templates'][ $template_reference ] |
|
| 319 | + : ''; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Retrieve registered data. |
|
| 325 | + * |
|
| 326 | + * @param string $key Name of key to attach data to. |
|
| 327 | + * @return mixed If there is no for the given key, then false is returned. |
|
| 328 | + */ |
|
| 329 | + public function getData($key) |
|
| 330 | + { |
|
| 331 | + return isset($this->jsdata[ $key ]) |
|
| 332 | + ? $this->jsdata[ $key ] |
|
| 333 | + : false; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Verifies whether the given data exists already on the jsdata array. |
|
| 339 | + * Overriding data is not allowed. |
|
| 340 | + * |
|
| 341 | + * @param string $key Index for data. |
|
| 342 | + * @return bool If valid then return true. |
|
| 343 | + * @throws InvalidArgumentException if data already exists. |
|
| 344 | + */ |
|
| 345 | + protected function verifyDataNotExisting($key) |
|
| 346 | + { |
|
| 347 | + if (isset($this->jsdata[ $key ])) { |
|
| 348 | + if (is_array($this->jsdata[ $key ])) { |
|
| 349 | + throw new InvalidArgumentException( |
|
| 350 | + sprintf( |
|
| 351 | + __( |
|
| 352 | + 'The value for %1$s already exists in the Registry::eejs object. |
|
| 353 | 353 | Overrides are not allowed. Since the value of this data is an array, you may want to use the |
| 354 | 354 | %2$s method to push your value to the array.', |
| 355 | - 'event_espresso' |
|
| 356 | - ), |
|
| 357 | - $key, |
|
| 358 | - 'pushData()' |
|
| 359 | - ) |
|
| 360 | - ); |
|
| 361 | - } |
|
| 362 | - throw new InvalidArgumentException( |
|
| 363 | - sprintf( |
|
| 364 | - __( |
|
| 365 | - 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
| 355 | + 'event_espresso' |
|
| 356 | + ), |
|
| 357 | + $key, |
|
| 358 | + 'pushData()' |
|
| 359 | + ) |
|
| 360 | + ); |
|
| 361 | + } |
|
| 362 | + throw new InvalidArgumentException( |
|
| 363 | + sprintf( |
|
| 364 | + __( |
|
| 365 | + 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
| 366 | 366 | allowed. Consider attaching your value to a different key', |
| 367 | - 'event_espresso' |
|
| 368 | - ), |
|
| 369 | - $key |
|
| 370 | - ) |
|
| 371 | - ); |
|
| 372 | - } |
|
| 373 | - return true; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Get the actual asset path for asset manifests. |
|
| 379 | - * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
|
| 380 | - * |
|
| 381 | - * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
|
| 382 | - * asset file location. |
|
| 383 | - * @param string $chunk_name |
|
| 384 | - * @param string $asset_type |
|
| 385 | - * @return string |
|
| 386 | - * @since 4.9.59.p |
|
| 387 | - */ |
|
| 388 | - public function getAssetUrl($namespace, $chunk_name, $asset_type) |
|
| 389 | - { |
|
| 390 | - $url = isset( |
|
| 391 | - $this->manifest_data[ $namespace ][ $chunk_name ][ $asset_type ], |
|
| 392 | - $this->manifest_data[ $namespace ]['url_base'] |
|
| 393 | - ) |
|
| 394 | - ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 395 | - . $this->manifest_data[ $namespace ][ $chunk_name ][ $asset_type ] |
|
| 396 | - : $chunk_name; |
|
| 397 | - return apply_filters( |
|
| 398 | - 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
|
| 399 | - $url, |
|
| 400 | - $namespace, |
|
| 401 | - $chunk_name, |
|
| 402 | - $asset_type |
|
| 403 | - ); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Return the url to a js file for the given namespace and chunk name. |
|
| 409 | - * |
|
| 410 | - * @param string $namespace |
|
| 411 | - * @param string $chunk_name |
|
| 412 | - * @return string |
|
| 413 | - */ |
|
| 414 | - public function getJsUrl($namespace, $chunk_name) |
|
| 415 | - { |
|
| 416 | - return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Return the url to a css file for the given namespace and chunk name. |
|
| 422 | - * |
|
| 423 | - * @param string $namespace |
|
| 424 | - * @param string $chunk_name |
|
| 425 | - * @return string |
|
| 426 | - */ |
|
| 427 | - public function getCssUrl($namespace, $chunk_name) |
|
| 428 | - { |
|
| 429 | - return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * @since $VID:$ |
|
| 435 | - * @throws InvalidArgumentException |
|
| 436 | - * @throws InvalidFilePathException |
|
| 437 | - */ |
|
| 438 | - public function registerManifestFiles() |
|
| 439 | - { |
|
| 440 | - $manifest_files = $this->assets->getManifestFiles(); |
|
| 441 | - foreach ($manifest_files as $manifest_file) { |
|
| 442 | - $this->registerManifestFile( |
|
| 443 | - $manifest_file->assetNamespace(), |
|
| 444 | - $manifest_file->urlBase(), |
|
| 445 | - $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST |
|
| 446 | - ); |
|
| 447 | - } |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Used to register a js/css manifest file with the registered_manifest_files property. |
|
| 453 | - * |
|
| 454 | - * @param string $namespace Provided to associate the manifest file with a specific namespace. |
|
| 455 | - * @param string $url_base The url base for the manifest file location. |
|
| 456 | - * @param string $manifest_file The absolute path to the manifest file. |
|
| 457 | - * @throws InvalidArgumentException |
|
| 458 | - * @throws InvalidFilePathException |
|
| 459 | - * @since 4.9.59.p |
|
| 460 | - */ |
|
| 461 | - public function registerManifestFile($namespace, $url_base, $manifest_file) |
|
| 462 | - { |
|
| 463 | - if (isset($this->manifest_data[ $namespace ])) { |
|
| 464 | - throw new InvalidArgumentException( |
|
| 465 | - sprintf( |
|
| 466 | - esc_html__( |
|
| 467 | - 'The namespace for this manifest file has already been registered, choose a namespace other than %s', |
|
| 468 | - 'event_espresso' |
|
| 469 | - ), |
|
| 470 | - $namespace |
|
| 471 | - ) |
|
| 472 | - ); |
|
| 473 | - } |
|
| 474 | - if (filter_var($url_base, FILTER_VALIDATE_URL) === false) { |
|
| 475 | - if (is_admin()) { |
|
| 476 | - EE_Error::add_error( |
|
| 477 | - sprintf( |
|
| 478 | - esc_html__( |
|
| 479 | - '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', |
|
| 480 | - 'event_espresso' |
|
| 481 | - ), |
|
| 482 | - 'Event Espresso', |
|
| 483 | - $url_base, |
|
| 484 | - 'plugins_url', |
|
| 485 | - 'WP_PLUGIN_URL' |
|
| 486 | - ), |
|
| 487 | - __FILE__, |
|
| 488 | - __FUNCTION__, |
|
| 489 | - __LINE__ |
|
| 490 | - ); |
|
| 491 | - } |
|
| 492 | - return; |
|
| 493 | - } |
|
| 494 | - $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 495 | - if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 496 | - $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 497 | - } |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * Decodes json from the provided manifest file. |
|
| 503 | - * |
|
| 504 | - * @since 4.9.59.p |
|
| 505 | - * @param string $manifest_file Path to manifest file. |
|
| 506 | - * @return array |
|
| 507 | - * @throws InvalidFilePathException |
|
| 508 | - */ |
|
| 509 | - private function decodeManifestFile($manifest_file) |
|
| 510 | - { |
|
| 511 | - if (! file_exists($manifest_file)) { |
|
| 512 | - throw new InvalidFilePathException($manifest_file); |
|
| 513 | - } |
|
| 514 | - return json_decode(file_get_contents($manifest_file), true); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * This is used to set registered script handles that have data. |
|
| 520 | - * |
|
| 521 | - * @param string $script_handle |
|
| 522 | - */ |
|
| 523 | - private function addRegisteredScriptHandlesWithData($script_handle) |
|
| 524 | - { |
|
| 525 | - $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - |
|
| 529 | - /**i |
|
| 367 | + 'event_espresso' |
|
| 368 | + ), |
|
| 369 | + $key |
|
| 370 | + ) |
|
| 371 | + ); |
|
| 372 | + } |
|
| 373 | + return true; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Get the actual asset path for asset manifests. |
|
| 379 | + * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
|
| 380 | + * |
|
| 381 | + * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
|
| 382 | + * asset file location. |
|
| 383 | + * @param string $chunk_name |
|
| 384 | + * @param string $asset_type |
|
| 385 | + * @return string |
|
| 386 | + * @since 4.9.59.p |
|
| 387 | + */ |
|
| 388 | + public function getAssetUrl($namespace, $chunk_name, $asset_type) |
|
| 389 | + { |
|
| 390 | + $url = isset( |
|
| 391 | + $this->manifest_data[ $namespace ][ $chunk_name ][ $asset_type ], |
|
| 392 | + $this->manifest_data[ $namespace ]['url_base'] |
|
| 393 | + ) |
|
| 394 | + ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 395 | + . $this->manifest_data[ $namespace ][ $chunk_name ][ $asset_type ] |
|
| 396 | + : $chunk_name; |
|
| 397 | + return apply_filters( |
|
| 398 | + 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
|
| 399 | + $url, |
|
| 400 | + $namespace, |
|
| 401 | + $chunk_name, |
|
| 402 | + $asset_type |
|
| 403 | + ); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Return the url to a js file for the given namespace and chunk name. |
|
| 409 | + * |
|
| 410 | + * @param string $namespace |
|
| 411 | + * @param string $chunk_name |
|
| 412 | + * @return string |
|
| 413 | + */ |
|
| 414 | + public function getJsUrl($namespace, $chunk_name) |
|
| 415 | + { |
|
| 416 | + return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Return the url to a css file for the given namespace and chunk name. |
|
| 422 | + * |
|
| 423 | + * @param string $namespace |
|
| 424 | + * @param string $chunk_name |
|
| 425 | + * @return string |
|
| 426 | + */ |
|
| 427 | + public function getCssUrl($namespace, $chunk_name) |
|
| 428 | + { |
|
| 429 | + return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * @since $VID:$ |
|
| 435 | + * @throws InvalidArgumentException |
|
| 436 | + * @throws InvalidFilePathException |
|
| 437 | + */ |
|
| 438 | + public function registerManifestFiles() |
|
| 439 | + { |
|
| 440 | + $manifest_files = $this->assets->getManifestFiles(); |
|
| 441 | + foreach ($manifest_files as $manifest_file) { |
|
| 442 | + $this->registerManifestFile( |
|
| 443 | + $manifest_file->assetNamespace(), |
|
| 444 | + $manifest_file->urlBase(), |
|
| 445 | + $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST |
|
| 446 | + ); |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Used to register a js/css manifest file with the registered_manifest_files property. |
|
| 453 | + * |
|
| 454 | + * @param string $namespace Provided to associate the manifest file with a specific namespace. |
|
| 455 | + * @param string $url_base The url base for the manifest file location. |
|
| 456 | + * @param string $manifest_file The absolute path to the manifest file. |
|
| 457 | + * @throws InvalidArgumentException |
|
| 458 | + * @throws InvalidFilePathException |
|
| 459 | + * @since 4.9.59.p |
|
| 460 | + */ |
|
| 461 | + public function registerManifestFile($namespace, $url_base, $manifest_file) |
|
| 462 | + { |
|
| 463 | + if (isset($this->manifest_data[ $namespace ])) { |
|
| 464 | + throw new InvalidArgumentException( |
|
| 465 | + sprintf( |
|
| 466 | + esc_html__( |
|
| 467 | + 'The namespace for this manifest file has already been registered, choose a namespace other than %s', |
|
| 468 | + 'event_espresso' |
|
| 469 | + ), |
|
| 470 | + $namespace |
|
| 471 | + ) |
|
| 472 | + ); |
|
| 473 | + } |
|
| 474 | + if (filter_var($url_base, FILTER_VALIDATE_URL) === false) { |
|
| 475 | + if (is_admin()) { |
|
| 476 | + EE_Error::add_error( |
|
| 477 | + sprintf( |
|
| 478 | + esc_html__( |
|
| 479 | + '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', |
|
| 480 | + 'event_espresso' |
|
| 481 | + ), |
|
| 482 | + 'Event Espresso', |
|
| 483 | + $url_base, |
|
| 484 | + 'plugins_url', |
|
| 485 | + 'WP_PLUGIN_URL' |
|
| 486 | + ), |
|
| 487 | + __FILE__, |
|
| 488 | + __FUNCTION__, |
|
| 489 | + __LINE__ |
|
| 490 | + ); |
|
| 491 | + } |
|
| 492 | + return; |
|
| 493 | + } |
|
| 494 | + $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 495 | + if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 496 | + $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 497 | + } |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * Decodes json from the provided manifest file. |
|
| 503 | + * |
|
| 504 | + * @since 4.9.59.p |
|
| 505 | + * @param string $manifest_file Path to manifest file. |
|
| 506 | + * @return array |
|
| 507 | + * @throws InvalidFilePathException |
|
| 508 | + */ |
|
| 509 | + private function decodeManifestFile($manifest_file) |
|
| 510 | + { |
|
| 511 | + if (! file_exists($manifest_file)) { |
|
| 512 | + throw new InvalidFilePathException($manifest_file); |
|
| 513 | + } |
|
| 514 | + return json_decode(file_get_contents($manifest_file), true); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * This is used to set registered script handles that have data. |
|
| 520 | + * |
|
| 521 | + * @param string $script_handle |
|
| 522 | + */ |
|
| 523 | + private function addRegisteredScriptHandlesWithData($script_handle) |
|
| 524 | + { |
|
| 525 | + $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + |
|
| 529 | + /**i |
|
| 530 | 530 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
| 531 | 531 | * Dependency stored in WP_Scripts if its set. |
| 532 | 532 | */ |
| 533 | - private function removeAlreadyRegisteredDataForScriptHandles() |
|
| 534 | - { |
|
| 535 | - if (empty($this->script_handles_with_data)) { |
|
| 536 | - return; |
|
| 537 | - } |
|
| 538 | - foreach ($this->script_handles_with_data as $script_handle) { |
|
| 539 | - $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - |
|
| 544 | - /** |
|
| 545 | - * Removes any data dependency registered in WP_Scripts if its set. |
|
| 546 | - * |
|
| 547 | - * @param string $script_handle |
|
| 548 | - */ |
|
| 549 | - private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
| 550 | - { |
|
| 551 | - if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 552 | - global $wp_scripts; |
|
| 553 | - $unset_handle = false; |
|
| 554 | - if ($wp_scripts->get_data($script_handle, 'data')) { |
|
| 555 | - unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 556 | - $unset_handle = true; |
|
| 557 | - } |
|
| 558 | - //deal with inline_scripts |
|
| 559 | - if ($wp_scripts->get_data($script_handle, 'before')) { |
|
| 560 | - unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 561 | - $unset_handle = true; |
|
| 562 | - } |
|
| 563 | - if ($wp_scripts->get_data($script_handle, 'after')) { |
|
| 564 | - unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 565 | - } |
|
| 566 | - if ($unset_handle) { |
|
| 567 | - unset($this->script_handles_with_data[ $script_handle ]); |
|
| 568 | - } |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - |
|
| 573 | - /** |
|
| 574 | - * register translations for a registered script |
|
| 575 | - * |
|
| 576 | - * @param string $handle |
|
| 577 | - */ |
|
| 578 | - public function registerTranslation($handle) |
|
| 579 | - { |
|
| 580 | - $this->i18n_registry->registerScriptI18n($handle); |
|
| 581 | - } |
|
| 533 | + private function removeAlreadyRegisteredDataForScriptHandles() |
|
| 534 | + { |
|
| 535 | + if (empty($this->script_handles_with_data)) { |
|
| 536 | + return; |
|
| 537 | + } |
|
| 538 | + foreach ($this->script_handles_with_data as $script_handle) { |
|
| 539 | + $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + |
|
| 544 | + /** |
|
| 545 | + * Removes any data dependency registered in WP_Scripts if its set. |
|
| 546 | + * |
|
| 547 | + * @param string $script_handle |
|
| 548 | + */ |
|
| 549 | + private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
| 550 | + { |
|
| 551 | + if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 552 | + global $wp_scripts; |
|
| 553 | + $unset_handle = false; |
|
| 554 | + if ($wp_scripts->get_data($script_handle, 'data')) { |
|
| 555 | + unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 556 | + $unset_handle = true; |
|
| 557 | + } |
|
| 558 | + //deal with inline_scripts |
|
| 559 | + if ($wp_scripts->get_data($script_handle, 'before')) { |
|
| 560 | + unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 561 | + $unset_handle = true; |
|
| 562 | + } |
|
| 563 | + if ($wp_scripts->get_data($script_handle, 'after')) { |
|
| 564 | + unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 565 | + } |
|
| 566 | + if ($unset_handle) { |
|
| 567 | + unset($this->script_handles_with_data[ $script_handle ]); |
|
| 568 | + } |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + |
|
| 573 | + /** |
|
| 574 | + * register translations for a registered script |
|
| 575 | + * |
|
| 576 | + * @param string $handle |
|
| 577 | + */ |
|
| 578 | + public function registerTranslation($handle) |
|
| 579 | + { |
|
| 580 | + $this->i18n_registry->registerScriptI18n($handle); |
|
| 581 | + } |
|
| 582 | 582 | } |
@@ -28,337 +28,337 @@ |
||
| 28 | 28 | class CoreAssetManager extends AssetManager |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - // WordPress core / Third party JS asset handles |
|
| 32 | - const JS_HANDLE_JQUERY = 'jquery'; |
|
| 33 | - |
|
| 34 | - const JS_HANDLE_JQUERY_VALIDATE = 'jquery-validate'; |
|
| 35 | - |
|
| 36 | - const JS_HANDLE_JQUERY_VALIDATE_EXTRA = 'jquery-validate-extra-methods'; |
|
| 37 | - |
|
| 38 | - const JS_HANDLE_UNDERSCORE = 'underscore'; |
|
| 39 | - |
|
| 40 | - const JS_HANDLE_ACCOUNTING_CORE = 'ee-accounting-core'; |
|
| 41 | - |
|
| 42 | - // EE JS assets handles |
|
| 43 | - const JS_HANDLE_EE_MANIFEST = 'ee-manifest'; |
|
| 44 | - |
|
| 45 | - const JS_HANDLE_EE_JS_CORE = 'eejs-core'; |
|
| 46 | - |
|
| 47 | - const JS_HANDLE_EE_VENDOR_REACT = 'ee-vendor-react'; |
|
| 48 | - |
|
| 49 | - const JS_HANDLE_EE_JS_API = 'eejs-api'; |
|
| 50 | - |
|
| 51 | - const JS_HANDLE_EE_CORE = 'espresso_core'; |
|
| 52 | - |
|
| 53 | - const JS_HANDLE_EE_I18N = 'eei18n'; |
|
| 54 | - |
|
| 55 | - const JS_HANDLE_EE_ACCOUNTING = 'ee-accounting'; |
|
| 56 | - |
|
| 57 | - const JS_HANDLE_EE_WP_PLUGINS_PAGE = 'ee-wp-plugins-page'; |
|
| 58 | - |
|
| 59 | - // EE CSS assets handles |
|
| 60 | - const CSS_HANDLE_EE_DEFAULT = 'espresso_default'; |
|
| 61 | - |
|
| 62 | - const CSS_HANDLE_EE_CUSTOM = 'espresso_custom_css'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var EE_Currency_Config $currency_config |
|
| 66 | - */ |
|
| 67 | - protected $currency_config; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var EE_Template_Config $template_config |
|
| 71 | - */ |
|
| 72 | - protected $template_config; |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * CoreAssetRegister constructor. |
|
| 77 | - * |
|
| 78 | - * @param AssetCollection $assets |
|
| 79 | - * @param EE_Currency_Config $currency_config |
|
| 80 | - * @param EE_Template_Config $template_config |
|
| 81 | - * @param DomainInterface $domain |
|
| 82 | - * @param Registry $registry |
|
| 83 | - */ |
|
| 84 | - public function __construct( |
|
| 85 | - AssetCollection $assets, |
|
| 86 | - EE_Currency_Config $currency_config, |
|
| 87 | - EE_Template_Config $template_config, |
|
| 88 | - DomainInterface $domain, |
|
| 89 | - Registry $registry |
|
| 90 | - ) { |
|
| 91 | - $this->currency_config = $currency_config; |
|
| 92 | - $this->template_config = $template_config; |
|
| 93 | - parent::__construct($domain, $assets, $registry); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @since $VID:$ |
|
| 99 | - * @throws DuplicateCollectionIdentifierException |
|
| 100 | - * @throws InvalidArgumentException |
|
| 101 | - * @throws InvalidDataTypeException |
|
| 102 | - * @throws InvalidEntityException |
|
| 103 | - */ |
|
| 104 | - public function addAssets() |
|
| 105 | - { |
|
| 106 | - $this->addJavascriptFiles(); |
|
| 107 | - $this->addStylesheetFiles(); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @since $VID:$ |
|
| 113 | - * @throws DuplicateCollectionIdentifierException |
|
| 114 | - * @throws InvalidArgumentException |
|
| 115 | - * @throws InvalidDataTypeException |
|
| 116 | - * @throws InvalidEntityException |
|
| 117 | - */ |
|
| 118 | - public function addJavascriptFiles() |
|
| 119 | - { |
|
| 120 | - $this->loadCoreJs(); |
|
| 121 | - $this->loadJqueryValidate(); |
|
| 122 | - $this->loadAccountingJs(); |
|
| 123 | - add_action( |
|
| 124 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 125 | - array($this, 'loadQtipJs') |
|
| 126 | - ); |
|
| 127 | - $this->registerAdminAssets(); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @since $VID:$ |
|
| 133 | - * @throws DuplicateCollectionIdentifierException |
|
| 134 | - * @throws InvalidDataTypeException |
|
| 135 | - * @throws InvalidEntityException |
|
| 136 | - */ |
|
| 137 | - public function addStylesheetFiles() |
|
| 138 | - { |
|
| 139 | - $this->loadCoreCss(); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * core default javascript |
|
| 145 | - * |
|
| 146 | - * @since $VID:$ |
|
| 147 | - * @throws DuplicateCollectionIdentifierException |
|
| 148 | - * @throws InvalidArgumentException |
|
| 149 | - * @throws InvalidDataTypeException |
|
| 150 | - * @throws InvalidEntityException |
|
| 151 | - */ |
|
| 152 | - private function loadCoreJs() |
|
| 153 | - { |
|
| 154 | - $this->addJavascript( |
|
| 155 | - CoreAssetManager::JS_HANDLE_EE_MANIFEST, |
|
| 156 | - $this->registry->getJsUrl($this->domain->assetNamespace(), 'manifest') |
|
| 157 | - ); |
|
| 158 | - |
|
| 159 | - $this->addJavascript( |
|
| 160 | - CoreAssetManager::JS_HANDLE_EE_JS_CORE, |
|
| 161 | - $this->registry->getJsUrl($this->domain->assetNamespace(), 'eejs'), |
|
| 162 | - array(CoreAssetManager::JS_HANDLE_EE_MANIFEST) |
|
| 163 | - ) |
|
| 164 | - ->setRequiresTranslation() |
|
| 165 | - ->setHasLocalizedData(); |
|
| 166 | - |
|
| 167 | - $this->addJavascript( |
|
| 168 | - CoreAssetManager::JS_HANDLE_EE_VENDOR_REACT, |
|
| 169 | - $this->registry->getJsUrl($this->domain->assetNamespace(), 'reactVendor'), |
|
| 170 | - array(CoreAssetManager::JS_HANDLE_EE_JS_CORE) |
|
| 171 | - ); |
|
| 172 | - |
|
| 173 | - global $wp_version; |
|
| 174 | - if (version_compare($wp_version, '4.4.0', '>')) { |
|
| 175 | - //js.api |
|
| 176 | - $this->addJavascript( |
|
| 177 | - CoreAssetManager::JS_HANDLE_EE_JS_API, |
|
| 178 | - EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js', |
|
| 179 | - array( |
|
| 180 | - CoreAssetManager::JS_HANDLE_UNDERSCORE, |
|
| 181 | - CoreAssetManager::JS_HANDLE_EE_JS_CORE |
|
| 182 | - ) |
|
| 183 | - ); |
|
| 184 | - $this->registry->addData('eejs_api_nonce', wp_create_nonce('wp_rest')); |
|
| 185 | - $this->registry->addData( |
|
| 186 | - 'paths', |
|
| 187 | - array( |
|
| 188 | - 'rest_route' => rest_url('ee/v4.8.36/'), |
|
| 189 | - 'collection_endpoints' => EED_Core_Rest_Api::getCollectionRoutesIndexedByModelName(), |
|
| 190 | - 'primary_keys' => EED_Core_Rest_Api::getPrimaryKeyNamesIndexedByModelName() |
|
| 191 | - ) |
|
| 192 | - ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - $this->addJavascript( |
|
| 196 | - CoreAssetManager::JS_HANDLE_EE_CORE, |
|
| 197 | - EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 198 | - array(CoreAssetManager::JS_HANDLE_JQUERY) |
|
| 199 | - ) |
|
| 200 | - ->setLocalizationCallback( |
|
| 201 | - function () { |
|
| 202 | - wp_localize_script( |
|
| 203 | - CoreAssetManager::JS_HANDLE_EE_CORE, |
|
| 204 | - CoreAssetManager::JS_HANDLE_EE_I18N, |
|
| 205 | - EE_Registry::$i18n_js_strings |
|
| 206 | - ); |
|
| 207 | - } |
|
| 208 | - ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @since $VID:$ |
|
| 214 | - * @throws DuplicateCollectionIdentifierException |
|
| 215 | - * @throws InvalidDataTypeException |
|
| 216 | - * @throws InvalidEntityException |
|
| 217 | - */ |
|
| 218 | - private function loadCoreCss() |
|
| 219 | - { |
|
| 220 | - if ($this->template_config->enable_default_style && ! is_admin()) { |
|
| 221 | - $this->addStylesheet( |
|
| 222 | - CoreAssetManager::CSS_HANDLE_EE_DEFAULT, |
|
| 223 | - is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css') |
|
| 224 | - ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css' |
|
| 225 | - : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css', |
|
| 226 | - array('dashicons') |
|
| 227 | - ); |
|
| 228 | - //Load custom style sheet if available |
|
| 229 | - if ($this->template_config->custom_style_sheet !== null) { |
|
| 230 | - $this->addStylesheet( |
|
| 231 | - CoreAssetManager::CSS_HANDLE_EE_CUSTOM, |
|
| 232 | - EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet, |
|
| 233 | - array(CoreAssetManager::CSS_HANDLE_EE_DEFAULT) |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * jQuery Validate for form validation |
|
| 242 | - * |
|
| 243 | - * @since $VID:$ |
|
| 244 | - * @throws DuplicateCollectionIdentifierException |
|
| 245 | - * @throws InvalidDataTypeException |
|
| 246 | - * @throws InvalidEntityException |
|
| 247 | - */ |
|
| 248 | - private function loadJqueryValidate() |
|
| 249 | - { |
|
| 250 | - $this->addJavascript( |
|
| 251 | - CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE, |
|
| 252 | - EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js', |
|
| 253 | - array(CoreAssetManager::JS_HANDLE_JQUERY) |
|
| 254 | - ) |
|
| 255 | - ->setVersion('1.15.0'); |
|
| 256 | - |
|
| 257 | - $this->addJavascript( |
|
| 258 | - CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE_EXTRA, |
|
| 259 | - EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js', |
|
| 260 | - array(CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE) |
|
| 261 | - ) |
|
| 262 | - ->setVersion('1.15.0'); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * accounting.js for performing client-side calculations |
|
| 268 | - * |
|
| 269 | - * @since $VID:$ |
|
| 270 | - * @throws DuplicateCollectionIdentifierException |
|
| 271 | - * @throws InvalidDataTypeException |
|
| 272 | - * @throws InvalidEntityException |
|
| 273 | - */ |
|
| 274 | - private function loadAccountingJs() |
|
| 275 | - { |
|
| 276 | - //accounting.js library |
|
| 277 | - // @link http://josscrowcroft.github.io/accounting.js/ |
|
| 278 | - $this->addJavascript( |
|
| 279 | - CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE, |
|
| 280 | - EE_THIRD_PARTY_URL . 'accounting/accounting.js', |
|
| 281 | - array(CoreAssetManager::JS_HANDLE_UNDERSCORE) |
|
| 282 | - ) |
|
| 283 | - ->setVersion('0.3.2'); |
|
| 284 | - |
|
| 285 | - $currency_config = $this->currency_config; |
|
| 286 | - $this->addJavascript( |
|
| 287 | - CoreAssetManager::JS_HANDLE_EE_ACCOUNTING, |
|
| 288 | - EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js', |
|
| 289 | - array(CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE) |
|
| 290 | - ) |
|
| 291 | - ->setLocalizationCallback( |
|
| 292 | - function () use ($currency_config) { |
|
| 293 | - wp_localize_script( |
|
| 294 | - CoreAssetManager::JS_HANDLE_EE_ACCOUNTING, |
|
| 295 | - 'EE_ACCOUNTING_CFG', |
|
| 296 | - array( |
|
| 297 | - 'currency' => array( |
|
| 298 | - 'symbol' => $currency_config->sign, |
|
| 299 | - 'format' => array( |
|
| 300 | - 'pos' => $currency_config->sign_b4 ? '%s%v' : '%v%s', |
|
| 301 | - 'neg' => $currency_config->sign_b4 ? '- %s%v' : '- %v%s', |
|
| 302 | - 'zero' => $currency_config->sign_b4 ? '%s--' : '--%s', |
|
| 303 | - ), |
|
| 304 | - 'decimal' => $currency_config->dec_mrk, |
|
| 305 | - 'thousand' => $currency_config->thsnds, |
|
| 306 | - 'precision' => $currency_config->dec_plc, |
|
| 307 | - ), |
|
| 308 | - 'number' => array( |
|
| 309 | - 'precision' => $currency_config->dec_plc, |
|
| 310 | - 'thousand' => $currency_config->thsnds, |
|
| 311 | - 'decimal' => $currency_config->dec_mrk, |
|
| 312 | - ), |
|
| 313 | - ) |
|
| 314 | - ); |
|
| 315 | - } |
|
| 316 | - ) |
|
| 317 | - ->setVersion(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * registers assets for cleaning your ears |
|
| 323 | - * |
|
| 324 | - * @param JavascriptAsset $script |
|
| 325 | - */ |
|
| 326 | - public function loadQtipJs(JavascriptAsset $script) |
|
| 327 | - { |
|
| 328 | - // qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook, |
|
| 329 | - // can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' ); |
|
| 330 | - if ( |
|
| 331 | - $script->handle() === CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE |
|
| 332 | - && apply_filters('FHEE_load_qtip', false) |
|
| 333 | - ) { |
|
| 334 | - EEH_Qtip_Loader::instance()->register_and_enqueue(); |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * assets that are used in the WordPress admin |
|
| 341 | - * |
|
| 342 | - * @since $VID:$ |
|
| 343 | - * @throws DuplicateCollectionIdentifierException |
|
| 344 | - * @throws InvalidDataTypeException |
|
| 345 | - * @throws InvalidEntityException |
|
| 346 | - */ |
|
| 347 | - private function registerAdminAssets() |
|
| 348 | - { |
|
| 349 | - $this->addJavascript( |
|
| 350 | - CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE, |
|
| 351 | - $this->registry->getJsUrl($this->domain->assetNamespace(), 'wp-plugins-page'), |
|
| 352 | - array( |
|
| 353 | - CoreAssetManager::JS_HANDLE_JQUERY, |
|
| 354 | - CoreAssetManager::JS_HANDLE_EE_VENDOR_REACT, |
|
| 355 | - ) |
|
| 356 | - ) |
|
| 357 | - ->setRequiresTranslation(); |
|
| 358 | - |
|
| 359 | - $this->addStylesheet( |
|
| 360 | - CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE, |
|
| 361 | - $this->registry->getCssUrl($this->domain->assetNamespace(), 'wp-plugins-page') |
|
| 362 | - ); |
|
| 363 | - } |
|
| 31 | + // WordPress core / Third party JS asset handles |
|
| 32 | + const JS_HANDLE_JQUERY = 'jquery'; |
|
| 33 | + |
|
| 34 | + const JS_HANDLE_JQUERY_VALIDATE = 'jquery-validate'; |
|
| 35 | + |
|
| 36 | + const JS_HANDLE_JQUERY_VALIDATE_EXTRA = 'jquery-validate-extra-methods'; |
|
| 37 | + |
|
| 38 | + const JS_HANDLE_UNDERSCORE = 'underscore'; |
|
| 39 | + |
|
| 40 | + const JS_HANDLE_ACCOUNTING_CORE = 'ee-accounting-core'; |
|
| 41 | + |
|
| 42 | + // EE JS assets handles |
|
| 43 | + const JS_HANDLE_EE_MANIFEST = 'ee-manifest'; |
|
| 44 | + |
|
| 45 | + const JS_HANDLE_EE_JS_CORE = 'eejs-core'; |
|
| 46 | + |
|
| 47 | + const JS_HANDLE_EE_VENDOR_REACT = 'ee-vendor-react'; |
|
| 48 | + |
|
| 49 | + const JS_HANDLE_EE_JS_API = 'eejs-api'; |
|
| 50 | + |
|
| 51 | + const JS_HANDLE_EE_CORE = 'espresso_core'; |
|
| 52 | + |
|
| 53 | + const JS_HANDLE_EE_I18N = 'eei18n'; |
|
| 54 | + |
|
| 55 | + const JS_HANDLE_EE_ACCOUNTING = 'ee-accounting'; |
|
| 56 | + |
|
| 57 | + const JS_HANDLE_EE_WP_PLUGINS_PAGE = 'ee-wp-plugins-page'; |
|
| 58 | + |
|
| 59 | + // EE CSS assets handles |
|
| 60 | + const CSS_HANDLE_EE_DEFAULT = 'espresso_default'; |
|
| 61 | + |
|
| 62 | + const CSS_HANDLE_EE_CUSTOM = 'espresso_custom_css'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var EE_Currency_Config $currency_config |
|
| 66 | + */ |
|
| 67 | + protected $currency_config; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var EE_Template_Config $template_config |
|
| 71 | + */ |
|
| 72 | + protected $template_config; |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * CoreAssetRegister constructor. |
|
| 77 | + * |
|
| 78 | + * @param AssetCollection $assets |
|
| 79 | + * @param EE_Currency_Config $currency_config |
|
| 80 | + * @param EE_Template_Config $template_config |
|
| 81 | + * @param DomainInterface $domain |
|
| 82 | + * @param Registry $registry |
|
| 83 | + */ |
|
| 84 | + public function __construct( |
|
| 85 | + AssetCollection $assets, |
|
| 86 | + EE_Currency_Config $currency_config, |
|
| 87 | + EE_Template_Config $template_config, |
|
| 88 | + DomainInterface $domain, |
|
| 89 | + Registry $registry |
|
| 90 | + ) { |
|
| 91 | + $this->currency_config = $currency_config; |
|
| 92 | + $this->template_config = $template_config; |
|
| 93 | + parent::__construct($domain, $assets, $registry); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @since $VID:$ |
|
| 99 | + * @throws DuplicateCollectionIdentifierException |
|
| 100 | + * @throws InvalidArgumentException |
|
| 101 | + * @throws InvalidDataTypeException |
|
| 102 | + * @throws InvalidEntityException |
|
| 103 | + */ |
|
| 104 | + public function addAssets() |
|
| 105 | + { |
|
| 106 | + $this->addJavascriptFiles(); |
|
| 107 | + $this->addStylesheetFiles(); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @since $VID:$ |
|
| 113 | + * @throws DuplicateCollectionIdentifierException |
|
| 114 | + * @throws InvalidArgumentException |
|
| 115 | + * @throws InvalidDataTypeException |
|
| 116 | + * @throws InvalidEntityException |
|
| 117 | + */ |
|
| 118 | + public function addJavascriptFiles() |
|
| 119 | + { |
|
| 120 | + $this->loadCoreJs(); |
|
| 121 | + $this->loadJqueryValidate(); |
|
| 122 | + $this->loadAccountingJs(); |
|
| 123 | + add_action( |
|
| 124 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 125 | + array($this, 'loadQtipJs') |
|
| 126 | + ); |
|
| 127 | + $this->registerAdminAssets(); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @since $VID:$ |
|
| 133 | + * @throws DuplicateCollectionIdentifierException |
|
| 134 | + * @throws InvalidDataTypeException |
|
| 135 | + * @throws InvalidEntityException |
|
| 136 | + */ |
|
| 137 | + public function addStylesheetFiles() |
|
| 138 | + { |
|
| 139 | + $this->loadCoreCss(); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * core default javascript |
|
| 145 | + * |
|
| 146 | + * @since $VID:$ |
|
| 147 | + * @throws DuplicateCollectionIdentifierException |
|
| 148 | + * @throws InvalidArgumentException |
|
| 149 | + * @throws InvalidDataTypeException |
|
| 150 | + * @throws InvalidEntityException |
|
| 151 | + */ |
|
| 152 | + private function loadCoreJs() |
|
| 153 | + { |
|
| 154 | + $this->addJavascript( |
|
| 155 | + CoreAssetManager::JS_HANDLE_EE_MANIFEST, |
|
| 156 | + $this->registry->getJsUrl($this->domain->assetNamespace(), 'manifest') |
|
| 157 | + ); |
|
| 158 | + |
|
| 159 | + $this->addJavascript( |
|
| 160 | + CoreAssetManager::JS_HANDLE_EE_JS_CORE, |
|
| 161 | + $this->registry->getJsUrl($this->domain->assetNamespace(), 'eejs'), |
|
| 162 | + array(CoreAssetManager::JS_HANDLE_EE_MANIFEST) |
|
| 163 | + ) |
|
| 164 | + ->setRequiresTranslation() |
|
| 165 | + ->setHasLocalizedData(); |
|
| 166 | + |
|
| 167 | + $this->addJavascript( |
|
| 168 | + CoreAssetManager::JS_HANDLE_EE_VENDOR_REACT, |
|
| 169 | + $this->registry->getJsUrl($this->domain->assetNamespace(), 'reactVendor'), |
|
| 170 | + array(CoreAssetManager::JS_HANDLE_EE_JS_CORE) |
|
| 171 | + ); |
|
| 172 | + |
|
| 173 | + global $wp_version; |
|
| 174 | + if (version_compare($wp_version, '4.4.0', '>')) { |
|
| 175 | + //js.api |
|
| 176 | + $this->addJavascript( |
|
| 177 | + CoreAssetManager::JS_HANDLE_EE_JS_API, |
|
| 178 | + EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js', |
|
| 179 | + array( |
|
| 180 | + CoreAssetManager::JS_HANDLE_UNDERSCORE, |
|
| 181 | + CoreAssetManager::JS_HANDLE_EE_JS_CORE |
|
| 182 | + ) |
|
| 183 | + ); |
|
| 184 | + $this->registry->addData('eejs_api_nonce', wp_create_nonce('wp_rest')); |
|
| 185 | + $this->registry->addData( |
|
| 186 | + 'paths', |
|
| 187 | + array( |
|
| 188 | + 'rest_route' => rest_url('ee/v4.8.36/'), |
|
| 189 | + 'collection_endpoints' => EED_Core_Rest_Api::getCollectionRoutesIndexedByModelName(), |
|
| 190 | + 'primary_keys' => EED_Core_Rest_Api::getPrimaryKeyNamesIndexedByModelName() |
|
| 191 | + ) |
|
| 192 | + ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + $this->addJavascript( |
|
| 196 | + CoreAssetManager::JS_HANDLE_EE_CORE, |
|
| 197 | + EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 198 | + array(CoreAssetManager::JS_HANDLE_JQUERY) |
|
| 199 | + ) |
|
| 200 | + ->setLocalizationCallback( |
|
| 201 | + function () { |
|
| 202 | + wp_localize_script( |
|
| 203 | + CoreAssetManager::JS_HANDLE_EE_CORE, |
|
| 204 | + CoreAssetManager::JS_HANDLE_EE_I18N, |
|
| 205 | + EE_Registry::$i18n_js_strings |
|
| 206 | + ); |
|
| 207 | + } |
|
| 208 | + ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @since $VID:$ |
|
| 214 | + * @throws DuplicateCollectionIdentifierException |
|
| 215 | + * @throws InvalidDataTypeException |
|
| 216 | + * @throws InvalidEntityException |
|
| 217 | + */ |
|
| 218 | + private function loadCoreCss() |
|
| 219 | + { |
|
| 220 | + if ($this->template_config->enable_default_style && ! is_admin()) { |
|
| 221 | + $this->addStylesheet( |
|
| 222 | + CoreAssetManager::CSS_HANDLE_EE_DEFAULT, |
|
| 223 | + is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css') |
|
| 224 | + ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css' |
|
| 225 | + : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css', |
|
| 226 | + array('dashicons') |
|
| 227 | + ); |
|
| 228 | + //Load custom style sheet if available |
|
| 229 | + if ($this->template_config->custom_style_sheet !== null) { |
|
| 230 | + $this->addStylesheet( |
|
| 231 | + CoreAssetManager::CSS_HANDLE_EE_CUSTOM, |
|
| 232 | + EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet, |
|
| 233 | + array(CoreAssetManager::CSS_HANDLE_EE_DEFAULT) |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * jQuery Validate for form validation |
|
| 242 | + * |
|
| 243 | + * @since $VID:$ |
|
| 244 | + * @throws DuplicateCollectionIdentifierException |
|
| 245 | + * @throws InvalidDataTypeException |
|
| 246 | + * @throws InvalidEntityException |
|
| 247 | + */ |
|
| 248 | + private function loadJqueryValidate() |
|
| 249 | + { |
|
| 250 | + $this->addJavascript( |
|
| 251 | + CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE, |
|
| 252 | + EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js', |
|
| 253 | + array(CoreAssetManager::JS_HANDLE_JQUERY) |
|
| 254 | + ) |
|
| 255 | + ->setVersion('1.15.0'); |
|
| 256 | + |
|
| 257 | + $this->addJavascript( |
|
| 258 | + CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE_EXTRA, |
|
| 259 | + EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js', |
|
| 260 | + array(CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE) |
|
| 261 | + ) |
|
| 262 | + ->setVersion('1.15.0'); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * accounting.js for performing client-side calculations |
|
| 268 | + * |
|
| 269 | + * @since $VID:$ |
|
| 270 | + * @throws DuplicateCollectionIdentifierException |
|
| 271 | + * @throws InvalidDataTypeException |
|
| 272 | + * @throws InvalidEntityException |
|
| 273 | + */ |
|
| 274 | + private function loadAccountingJs() |
|
| 275 | + { |
|
| 276 | + //accounting.js library |
|
| 277 | + // @link http://josscrowcroft.github.io/accounting.js/ |
|
| 278 | + $this->addJavascript( |
|
| 279 | + CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE, |
|
| 280 | + EE_THIRD_PARTY_URL . 'accounting/accounting.js', |
|
| 281 | + array(CoreAssetManager::JS_HANDLE_UNDERSCORE) |
|
| 282 | + ) |
|
| 283 | + ->setVersion('0.3.2'); |
|
| 284 | + |
|
| 285 | + $currency_config = $this->currency_config; |
|
| 286 | + $this->addJavascript( |
|
| 287 | + CoreAssetManager::JS_HANDLE_EE_ACCOUNTING, |
|
| 288 | + EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js', |
|
| 289 | + array(CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE) |
|
| 290 | + ) |
|
| 291 | + ->setLocalizationCallback( |
|
| 292 | + function () use ($currency_config) { |
|
| 293 | + wp_localize_script( |
|
| 294 | + CoreAssetManager::JS_HANDLE_EE_ACCOUNTING, |
|
| 295 | + 'EE_ACCOUNTING_CFG', |
|
| 296 | + array( |
|
| 297 | + 'currency' => array( |
|
| 298 | + 'symbol' => $currency_config->sign, |
|
| 299 | + 'format' => array( |
|
| 300 | + 'pos' => $currency_config->sign_b4 ? '%s%v' : '%v%s', |
|
| 301 | + 'neg' => $currency_config->sign_b4 ? '- %s%v' : '- %v%s', |
|
| 302 | + 'zero' => $currency_config->sign_b4 ? '%s--' : '--%s', |
|
| 303 | + ), |
|
| 304 | + 'decimal' => $currency_config->dec_mrk, |
|
| 305 | + 'thousand' => $currency_config->thsnds, |
|
| 306 | + 'precision' => $currency_config->dec_plc, |
|
| 307 | + ), |
|
| 308 | + 'number' => array( |
|
| 309 | + 'precision' => $currency_config->dec_plc, |
|
| 310 | + 'thousand' => $currency_config->thsnds, |
|
| 311 | + 'decimal' => $currency_config->dec_mrk, |
|
| 312 | + ), |
|
| 313 | + ) |
|
| 314 | + ); |
|
| 315 | + } |
|
| 316 | + ) |
|
| 317 | + ->setVersion(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * registers assets for cleaning your ears |
|
| 323 | + * |
|
| 324 | + * @param JavascriptAsset $script |
|
| 325 | + */ |
|
| 326 | + public function loadQtipJs(JavascriptAsset $script) |
|
| 327 | + { |
|
| 328 | + // qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook, |
|
| 329 | + // can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' ); |
|
| 330 | + if ( |
|
| 331 | + $script->handle() === CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE |
|
| 332 | + && apply_filters('FHEE_load_qtip', false) |
|
| 333 | + ) { |
|
| 334 | + EEH_Qtip_Loader::instance()->register_and_enqueue(); |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * assets that are used in the WordPress admin |
|
| 341 | + * |
|
| 342 | + * @since $VID:$ |
|
| 343 | + * @throws DuplicateCollectionIdentifierException |
|
| 344 | + * @throws InvalidDataTypeException |
|
| 345 | + * @throws InvalidEntityException |
|
| 346 | + */ |
|
| 347 | + private function registerAdminAssets() |
|
| 348 | + { |
|
| 349 | + $this->addJavascript( |
|
| 350 | + CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE, |
|
| 351 | + $this->registry->getJsUrl($this->domain->assetNamespace(), 'wp-plugins-page'), |
|
| 352 | + array( |
|
| 353 | + CoreAssetManager::JS_HANDLE_JQUERY, |
|
| 354 | + CoreAssetManager::JS_HANDLE_EE_VENDOR_REACT, |
|
| 355 | + ) |
|
| 356 | + ) |
|
| 357 | + ->setRequiresTranslation(); |
|
| 358 | + |
|
| 359 | + $this->addStylesheet( |
|
| 360 | + CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE, |
|
| 361 | + $this->registry->getCssUrl($this->domain->assetNamespace(), 'wp-plugins-page') |
|
| 362 | + ); |
|
| 363 | + } |
|
| 364 | 364 | } |
@@ -22,1268 +22,1268 @@ |
||
| 22 | 22 | class EED_Core_Rest_Api extends \EED_Module |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - const ee_api_namespace = Domain::API_NAMESPACE; |
|
| 26 | - |
|
| 27 | - const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
| 28 | - |
|
| 29 | - const saved_routes_option_names = 'ee_core_routes'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * string used in _links response bodies to make them globally unique. |
|
| 33 | - * |
|
| 34 | - * @see http://v2.wp-api.org/extending/linking/ |
|
| 35 | - */ |
|
| 36 | - const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var CalculatedModelFields |
|
| 40 | - */ |
|
| 41 | - protected static $_field_calculator; |
|
| 42 | - |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @return EED_Core_Rest_Api|EED_Module |
|
| 46 | - */ |
|
| 47 | - public static function instance() |
|
| 48 | - { |
|
| 49 | - self::$_field_calculator = new CalculatedModelFields(); |
|
| 50 | - return parent::get_instance(__CLASS__); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
| 56 | - * |
|
| 57 | - * @access public |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public static function set_hooks() |
|
| 61 | - { |
|
| 62 | - self::set_hooks_both(); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 68 | - * |
|
| 69 | - * @access public |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public static function set_hooks_admin() |
|
| 73 | - { |
|
| 74 | - self::set_hooks_both(); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - public static function set_hooks_both() |
|
| 79 | - { |
|
| 80 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
| 81 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
| 82 | - add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
| 83 | - add_filter( |
|
| 84 | - 'rest_index', |
|
| 85 | - array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex') |
|
| 86 | - ); |
|
| 87 | - EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * sets up hooks which only need to be included as part of REST API requests; |
|
| 93 | - * other requests like to the frontend or admin etc don't need them |
|
| 94 | - * |
|
| 95 | - * @throws \EE_Error |
|
| 96 | - */ |
|
| 97 | - public static function set_hooks_rest_api() |
|
| 98 | - { |
|
| 99 | - // set hooks which account for changes made to the API |
|
| 100 | - EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * public wrapper of _set_hooks_for_changes. |
|
| 106 | - * Loads all the hooks which make requests to old versions of the API |
|
| 107 | - * appear the same as they always did |
|
| 108 | - * |
|
| 109 | - * @throws EE_Error |
|
| 110 | - */ |
|
| 111 | - public static function set_hooks_for_changes() |
|
| 112 | - { |
|
| 113 | - self::_set_hooks_for_changes(); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Loads all the hooks which make requests to old versions of the API |
|
| 119 | - * appear the same as they always did |
|
| 120 | - * |
|
| 121 | - * @throws EE_Error |
|
| 122 | - */ |
|
| 123 | - protected static function _set_hooks_for_changes() |
|
| 124 | - { |
|
| 125 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
| 126 | - foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
| 127 | - // ignore the base parent class |
|
| 128 | - // and legacy named classes |
|
| 129 | - if ($classname_in_namespace === 'ChangesInBase' |
|
| 130 | - || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
| 131 | - ) { |
|
| 132 | - continue; |
|
| 133 | - } |
|
| 134 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 135 | - if (class_exists($full_classname)) { |
|
| 136 | - $instance_of_class = new $full_classname; |
|
| 137 | - if ($instance_of_class instanceof ChangesInBase) { |
|
| 138 | - $instance_of_class->setHooks(); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
| 147 | - * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
| 148 | - * |
|
| 149 | - * @throws \EE_Error |
|
| 150 | - */ |
|
| 151 | - public static function register_routes() |
|
| 152 | - { |
|
| 153 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
| 154 | - foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
| 155 | - /** |
|
| 156 | - * @var array $data_for_multiple_endpoints numerically indexed array |
|
| 157 | - * but can also contain route options like { |
|
| 158 | - * @type array $schema { |
|
| 159 | - * @type callable $schema_callback |
|
| 160 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 161 | - * WP_REST_Request of course |
|
| 162 | - * } |
|
| 163 | - * } |
|
| 164 | - */ |
|
| 165 | - // when registering routes, register all the endpoints' data at the same time |
|
| 166 | - $multiple_endpoint_args = array(); |
|
| 167 | - foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
| 168 | - /** |
|
| 169 | - * @var array $data_for_single_endpoint { |
|
| 170 | - * @type callable $callback |
|
| 171 | - * @type string methods |
|
| 172 | - * @type array args |
|
| 173 | - * @type array _links |
|
| 174 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 175 | - * WP_REST_Request of course |
|
| 176 | - * } |
|
| 177 | - */ |
|
| 178 | - // skip route options |
|
| 179 | - if (! is_numeric($endpoint_key)) { |
|
| 180 | - continue; |
|
| 181 | - } |
|
| 182 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 183 | - throw new EE_Error( |
|
| 184 | - esc_html__( |
|
| 185 | - // @codingStandardsIgnoreStart |
|
| 186 | - 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
| 187 | - // @codingStandardsIgnoreEnd |
|
| 188 | - 'event_espresso' |
|
| 189 | - ) |
|
| 190 | - ); |
|
| 191 | - } |
|
| 192 | - $callback = $data_for_single_endpoint['callback']; |
|
| 193 | - $single_endpoint_args = array( |
|
| 194 | - 'methods' => $data_for_single_endpoint['methods'], |
|
| 195 | - 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
| 196 | - : array(), |
|
| 197 | - ); |
|
| 198 | - if (isset($data_for_single_endpoint['_links'])) { |
|
| 199 | - $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
| 200 | - } |
|
| 201 | - if (isset($data_for_single_endpoint['callback_args'])) { |
|
| 202 | - $callback_args = $data_for_single_endpoint['callback_args']; |
|
| 203 | - $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
| 204 | - $callback, |
|
| 205 | - $callback_args |
|
| 206 | - ) { |
|
| 207 | - array_unshift($callback_args, $request); |
|
| 208 | - return call_user_func_array( |
|
| 209 | - $callback, |
|
| 210 | - $callback_args |
|
| 211 | - ); |
|
| 212 | - }; |
|
| 213 | - } else { |
|
| 214 | - $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
| 215 | - } |
|
| 216 | - $multiple_endpoint_args[] = $single_endpoint_args; |
|
| 217 | - } |
|
| 218 | - if (isset($data_for_multiple_endpoints['schema'])) { |
|
| 219 | - $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
| 220 | - $schema_callback = $schema_route_data['schema_callback']; |
|
| 221 | - $callback_args = $schema_route_data['callback_args']; |
|
| 222 | - $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
| 223 | - return call_user_func_array( |
|
| 224 | - $schema_callback, |
|
| 225 | - $callback_args |
|
| 226 | - ); |
|
| 227 | - }; |
|
| 228 | - } |
|
| 229 | - register_rest_route( |
|
| 230 | - $namespace, |
|
| 231 | - $relative_route, |
|
| 232 | - $multiple_endpoint_args |
|
| 233 | - ); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Checks if there was a version change or something that merits invalidating the cached |
|
| 241 | - * route data. If so, invalidates the cached route data so that it gets refreshed |
|
| 242 | - * next time the WP API is used |
|
| 243 | - */ |
|
| 244 | - public static function invalidate_cached_route_data_on_version_change() |
|
| 245 | - { |
|
| 246 | - if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
| 247 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 248 | - } |
|
| 249 | - foreach (EE_Registry::instance()->addons as $addon) { |
|
| 250 | - if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
| 251 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Removes the cached route data so it will get refreshed next time the WP API is used |
|
| 259 | - */ |
|
| 260 | - public static function invalidate_cached_route_data() |
|
| 261 | - { |
|
| 262 | - // delete the saved EE REST API routes |
|
| 263 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
| 264 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Gets the EE route data |
|
| 271 | - * |
|
| 272 | - * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
| 273 | - * @throws \EE_Error |
|
| 274 | - * @type string|array $callback |
|
| 275 | - * @type string $methods |
|
| 276 | - * @type boolean $hidden_endpoint |
|
| 277 | - * } |
|
| 278 | - */ |
|
| 279 | - public static function get_ee_route_data() |
|
| 280 | - { |
|
| 281 | - $ee_routes = array(); |
|
| 282 | - foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
| 283 | - $ee_routes[ self::ee_api_namespace . $version ] = self::_get_ee_route_data_for_version( |
|
| 284 | - $version, |
|
| 285 | - $hidden_endpoints |
|
| 286 | - ); |
|
| 287 | - } |
|
| 288 | - return $ee_routes; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Gets the EE route data from the wp options if it exists already, |
|
| 294 | - * otherwise re-generates it and saves it to the option |
|
| 295 | - * |
|
| 296 | - * @param string $version |
|
| 297 | - * @param boolean $hidden_endpoints |
|
| 298 | - * @return array |
|
| 299 | - * @throws \EE_Error |
|
| 300 | - */ |
|
| 301 | - protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 302 | - { |
|
| 303 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
| 304 | - if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
| 305 | - $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
| 306 | - } |
|
| 307 | - return $ee_routes; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Saves the EE REST API route data to a wp option and returns it |
|
| 313 | - * |
|
| 314 | - * @param string $version |
|
| 315 | - * @param boolean $hidden_endpoints |
|
| 316 | - * @return mixed|null |
|
| 317 | - * @throws \EE_Error |
|
| 318 | - */ |
|
| 319 | - protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 320 | - { |
|
| 321 | - $instance = self::instance(); |
|
| 322 | - $routes = apply_filters( |
|
| 323 | - 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
| 324 | - array_replace_recursive( |
|
| 325 | - $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
| 326 | - $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
| 327 | - $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
| 328 | - $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
| 329 | - ) |
|
| 330 | - ); |
|
| 331 | - $option_name = self::saved_routes_option_names . $version; |
|
| 332 | - if (get_option($option_name)) { |
|
| 333 | - update_option($option_name, $routes, true); |
|
| 334 | - } else { |
|
| 335 | - add_option($option_name, $routes, null, 'no'); |
|
| 336 | - } |
|
| 337 | - return $routes; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
| 343 | - * need to calculate it on every request |
|
| 344 | - * |
|
| 345 | - * @deprecated since version 4.9.1 |
|
| 346 | - * @return void |
|
| 347 | - */ |
|
| 348 | - public static function save_ee_routes() |
|
| 349 | - { |
|
| 350 | - if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 351 | - $instance = self::instance(); |
|
| 352 | - $routes = apply_filters( |
|
| 353 | - 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
| 354 | - array_replace_recursive( |
|
| 355 | - $instance->_register_config_routes(), |
|
| 356 | - $instance->_register_meta_routes(), |
|
| 357 | - $instance->_register_model_routes(), |
|
| 358 | - $instance->_register_rpc_routes() |
|
| 359 | - ) |
|
| 360 | - ); |
|
| 361 | - update_option(self::saved_routes_option_names, $routes, true); |
|
| 362 | - } |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Gets all the route information relating to EE models |
|
| 368 | - * |
|
| 369 | - * @return array @see get_ee_route_data |
|
| 370 | - * @deprecated since version 4.9.1 |
|
| 371 | - */ |
|
| 372 | - protected function _register_model_routes() |
|
| 373 | - { |
|
| 374 | - $model_routes = array(); |
|
| 375 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 376 | - $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 377 | - . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 378 | - } |
|
| 379 | - return $model_routes; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Decides whether or not to add write endpoints for this model. |
|
| 385 | - * |
|
| 386 | - * Currently, this defaults to exclude all global tables and models |
|
| 387 | - * which would allow inserting WP core data (we don't want to duplicate |
|
| 388 | - * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
| 389 | - * |
|
| 390 | - * @param EEM_Base $model |
|
| 391 | - * @return bool |
|
| 392 | - */ |
|
| 393 | - public static function should_have_write_endpoints(EEM_Base $model) |
|
| 394 | - { |
|
| 395 | - if ($model->is_wp_core_model()) { |
|
| 396 | - return false; |
|
| 397 | - } |
|
| 398 | - foreach ($model->get_tables() as $table) { |
|
| 399 | - if ($table->is_global()) { |
|
| 400 | - return false; |
|
| 401 | - } |
|
| 402 | - } |
|
| 403 | - return true; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
| 409 | - * in this versioned namespace of EE4 |
|
| 410 | - * |
|
| 411 | - * @param $version |
|
| 412 | - * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
| 413 | - */ |
|
| 414 | - public static function model_names_with_plural_routes($version) |
|
| 415 | - { |
|
| 416 | - $model_version_info = new ModelVersionInfo($version); |
|
| 417 | - $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
| 418 | - // let's not bother having endpoints for extra metas |
|
| 419 | - unset( |
|
| 420 | - $models_to_register['Extra_Meta'], |
|
| 421 | - $models_to_register['Extra_Join'], |
|
| 422 | - $models_to_register['Post_Meta'] |
|
| 423 | - ); |
|
| 424 | - return apply_filters( |
|
| 425 | - 'FHEE__EED_Core_REST_API___register_model_routes', |
|
| 426 | - $models_to_register |
|
| 427 | - ); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Gets the route data for EE models in the specified version |
|
| 433 | - * |
|
| 434 | - * @param string $version |
|
| 435 | - * @param boolean $hidden_endpoint |
|
| 436 | - * @return array |
|
| 437 | - * @throws EE_Error |
|
| 438 | - */ |
|
| 439 | - protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
| 440 | - { |
|
| 441 | - $model_routes = array(); |
|
| 442 | - $model_version_info = new ModelVersionInfo($version); |
|
| 443 | - foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
| 444 | - $model = \EE_Registry::instance()->load_model($model_name); |
|
| 445 | - // if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
| 446 | - if (! $model instanceof EEM_Base) { |
|
| 447 | - continue; |
|
| 448 | - } |
|
| 449 | - // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
| 450 | - $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
| 451 | - $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
| 452 | - $model_routes[ $plural_model_route ] = array( |
|
| 453 | - array( |
|
| 454 | - 'callback' => array( |
|
| 455 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 456 | - 'handleRequestGetAll', |
|
| 457 | - ), |
|
| 458 | - 'callback_args' => array($version, $model_name), |
|
| 459 | - 'methods' => WP_REST_Server::READABLE, |
|
| 460 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 461 | - 'args' => $this->_get_read_query_params($model, $version), |
|
| 462 | - '_links' => array( |
|
| 463 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 464 | - ), |
|
| 465 | - ), |
|
| 466 | - 'schema' => array( |
|
| 467 | - 'schema_callback' => array( |
|
| 468 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 469 | - 'handleSchemaRequest', |
|
| 470 | - ), |
|
| 471 | - 'callback_args' => array($version, $model_name), |
|
| 472 | - ), |
|
| 473 | - ); |
|
| 474 | - $model_routes[ $singular_model_route ] = array( |
|
| 475 | - array( |
|
| 476 | - 'callback' => array( |
|
| 477 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 478 | - 'handleRequestGetOne', |
|
| 479 | - ), |
|
| 480 | - 'callback_args' => array($version, $model_name), |
|
| 481 | - 'methods' => WP_REST_Server::READABLE, |
|
| 482 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 483 | - 'args' => $this->_get_response_selection_query_params($model, $version), |
|
| 484 | - ), |
|
| 485 | - ); |
|
| 486 | - if (apply_filters( |
|
| 487 | - 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
| 488 | - EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
| 489 | - $model |
|
| 490 | - )) { |
|
| 491 | - $model_routes[ $plural_model_route ][] = array( |
|
| 492 | - 'callback' => array( |
|
| 493 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 494 | - 'handleRequestInsert', |
|
| 495 | - ), |
|
| 496 | - 'callback_args' => array($version, $model_name), |
|
| 497 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 498 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 499 | - 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
| 500 | - ); |
|
| 501 | - $model_routes[ $singular_model_route ] = array_merge( |
|
| 502 | - $model_routes[ $singular_model_route ], |
|
| 503 | - array( |
|
| 504 | - array( |
|
| 505 | - 'callback' => array( |
|
| 506 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 507 | - 'handleRequestUpdate', |
|
| 508 | - ), |
|
| 509 | - 'callback_args' => array($version, $model_name), |
|
| 510 | - 'methods' => WP_REST_Server::EDITABLE, |
|
| 511 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 512 | - 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
| 513 | - ), |
|
| 514 | - array( |
|
| 515 | - 'callback' => array( |
|
| 516 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 517 | - 'handleRequestDelete', |
|
| 518 | - ), |
|
| 519 | - 'callback_args' => array($version, $model_name), |
|
| 520 | - 'methods' => WP_REST_Server::DELETABLE, |
|
| 521 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 522 | - 'args' => $this->_get_delete_query_params($model, $version), |
|
| 523 | - ), |
|
| 524 | - ) |
|
| 525 | - ); |
|
| 526 | - } |
|
| 527 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 528 | - $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
| 529 | - $model, |
|
| 530 | - '(?P<id>[^\/]+)', |
|
| 531 | - $relation_obj |
|
| 532 | - ); |
|
| 533 | - $endpoints = array( |
|
| 534 | - array( |
|
| 535 | - 'callback' => array( |
|
| 536 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 537 | - 'handleRequestGetRelated', |
|
| 538 | - ), |
|
| 539 | - 'callback_args' => array($version, $model_name, $relation_name), |
|
| 540 | - 'methods' => WP_REST_Server::READABLE, |
|
| 541 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 542 | - 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
| 543 | - ), |
|
| 544 | - ); |
|
| 545 | - $model_routes[ $related_route ] = $endpoints; |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - return $model_routes; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
| 554 | - * excluding the preceding slash. |
|
| 555 | - * Eg you pass get_plural_route_to('Event') = 'events' |
|
| 556 | - * |
|
| 557 | - * @param EEM_Base $model |
|
| 558 | - * @return string |
|
| 559 | - */ |
|
| 560 | - public static function get_collection_route(EEM_Base $model) |
|
| 561 | - { |
|
| 562 | - return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 568 | - * excluding the preceding slash. |
|
| 569 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 570 | - * |
|
| 571 | - * @param EEM_Base $model eg Event or Venue |
|
| 572 | - * @param string $id |
|
| 573 | - * @return string |
|
| 574 | - */ |
|
| 575 | - public static function get_entity_route($model, $id) |
|
| 576 | - { |
|
| 577 | - return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 583 | - * excluding the preceding slash. |
|
| 584 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 585 | - * |
|
| 586 | - * @param EEM_Base $model eg Event or Venue |
|
| 587 | - * @param string $id |
|
| 588 | - * @param EE_Model_Relation_Base $relation_obj |
|
| 589 | - * @return string |
|
| 590 | - */ |
|
| 591 | - public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
| 592 | - { |
|
| 593 | - $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
| 594 | - $relation_obj->get_other_model()->get_this_model_name(), |
|
| 595 | - $relation_obj |
|
| 596 | - ); |
|
| 597 | - return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
| 603 | - * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
| 604 | - * |
|
| 605 | - * @param string $relative_route |
|
| 606 | - * @param string $version |
|
| 607 | - * @return string |
|
| 608 | - */ |
|
| 609 | - public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
| 610 | - { |
|
| 611 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
| 617 | - * routes that don't conform to the traditional REST CRUD-style). |
|
| 618 | - * |
|
| 619 | - * @deprecated since 4.9.1 |
|
| 620 | - */ |
|
| 621 | - protected function _register_rpc_routes() |
|
| 622 | - { |
|
| 623 | - $routes = array(); |
|
| 624 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 625 | - $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 626 | - $version, |
|
| 627 | - $hidden_endpoint |
|
| 628 | - ); |
|
| 629 | - } |
|
| 630 | - return $routes; |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * @param string $version |
|
| 636 | - * @param boolean $hidden_endpoint |
|
| 637 | - * @return array |
|
| 638 | - */ |
|
| 639 | - protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
| 640 | - { |
|
| 641 | - $this_versions_routes = array(); |
|
| 642 | - // checkin endpoint |
|
| 643 | - $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
| 644 | - array( |
|
| 645 | - 'callback' => array( |
|
| 646 | - 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
| 647 | - 'handleRequestToggleCheckin', |
|
| 648 | - ), |
|
| 649 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 650 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 651 | - 'args' => array( |
|
| 652 | - 'force' => array( |
|
| 653 | - 'required' => false, |
|
| 654 | - 'default' => false, |
|
| 655 | - 'description' => __( |
|
| 656 | - // @codingStandardsIgnoreStart |
|
| 657 | - 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
| 658 | - // @codingStandardsIgnoreEnd |
|
| 659 | - 'event_espresso' |
|
| 660 | - ), |
|
| 661 | - ), |
|
| 662 | - ), |
|
| 663 | - 'callback_args' => array($version), |
|
| 664 | - ), |
|
| 665 | - ); |
|
| 666 | - return apply_filters( |
|
| 667 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
| 668 | - $this_versions_routes, |
|
| 669 | - $version, |
|
| 670 | - $hidden_endpoint |
|
| 671 | - ); |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - |
|
| 675 | - /** |
|
| 676 | - * Gets the query params that can be used when request one or many |
|
| 677 | - * |
|
| 678 | - * @param EEM_Base $model |
|
| 679 | - * @param string $version |
|
| 680 | - * @return array |
|
| 681 | - */ |
|
| 682 | - protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
| 683 | - { |
|
| 684 | - return apply_filters( |
|
| 685 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
| 686 | - array( |
|
| 687 | - 'include' => array( |
|
| 688 | - 'required' => false, |
|
| 689 | - 'default' => '*', |
|
| 690 | - 'type' => 'string', |
|
| 691 | - ), |
|
| 692 | - 'calculate' => array( |
|
| 693 | - 'required' => false, |
|
| 694 | - 'default' => '', |
|
| 695 | - 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
| 696 | - 'type' => 'string', |
|
| 697 | - // because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
| 698 | - // freaks out. We'll just validate this argument while handling the request |
|
| 699 | - 'validate_callback' => null, |
|
| 700 | - 'sanitize_callback' => null, |
|
| 701 | - ), |
|
| 702 | - ), |
|
| 703 | - $model, |
|
| 704 | - $version |
|
| 705 | - ); |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * Gets the parameters acceptable for delete requests |
|
| 711 | - * |
|
| 712 | - * @param \EEM_Base $model |
|
| 713 | - * @param string $version |
|
| 714 | - * @return array |
|
| 715 | - */ |
|
| 716 | - protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
| 717 | - { |
|
| 718 | - $params_for_delete = array( |
|
| 719 | - 'allow_blocking' => array( |
|
| 720 | - 'required' => false, |
|
| 721 | - 'default' => true, |
|
| 722 | - 'type' => 'boolean', |
|
| 723 | - ), |
|
| 724 | - ); |
|
| 725 | - $params_for_delete['force'] = array( |
|
| 726 | - 'required' => false, |
|
| 727 | - 'default' => false, |
|
| 728 | - 'type' => 'boolean', |
|
| 729 | - ); |
|
| 730 | - return apply_filters( |
|
| 731 | - 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
| 732 | - $params_for_delete, |
|
| 733 | - $model, |
|
| 734 | - $version |
|
| 735 | - ); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Gets info about reading query params that are acceptable |
|
| 741 | - * |
|
| 742 | - * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
| 743 | - * @param string $version |
|
| 744 | - * @return array describing the args acceptable when querying this model |
|
| 745 | - * @throws EE_Error |
|
| 746 | - */ |
|
| 747 | - protected function _get_read_query_params(\EEM_Base $model, $version) |
|
| 748 | - { |
|
| 749 | - $default_orderby = array(); |
|
| 750 | - foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
| 751 | - $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 752 | - } |
|
| 753 | - return array_merge( |
|
| 754 | - $this->_get_response_selection_query_params($model, $version), |
|
| 755 | - array( |
|
| 756 | - 'where' => array( |
|
| 757 | - 'required' => false, |
|
| 758 | - 'default' => array(), |
|
| 759 | - 'type' => 'object', |
|
| 760 | - // because we accept an almost infinite list of possible where conditions, WP |
|
| 761 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
| 762 | - // while handling the request |
|
| 763 | - 'validate_callback' => null, |
|
| 764 | - 'sanitize_callback' => null, |
|
| 765 | - ), |
|
| 766 | - 'limit' => array( |
|
| 767 | - 'required' => false, |
|
| 768 | - 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
| 769 | - 'type' => array( |
|
| 770 | - 'array', |
|
| 771 | - 'string', |
|
| 772 | - 'integer', |
|
| 773 | - ), |
|
| 774 | - // because we accept a variety of types, WP core validation and sanitization |
|
| 775 | - // freaks out. We'll just validate this argument while handling the request |
|
| 776 | - 'validate_callback' => null, |
|
| 777 | - 'sanitize_callback' => null, |
|
| 778 | - ), |
|
| 779 | - 'order_by' => array( |
|
| 780 | - 'required' => false, |
|
| 781 | - 'default' => $default_orderby, |
|
| 782 | - 'type' => array( |
|
| 783 | - 'object', |
|
| 784 | - 'string', |
|
| 785 | - ),// because we accept a variety of types, WP core validation and sanitization |
|
| 786 | - // freaks out. We'll just validate this argument while handling the request |
|
| 787 | - 'validate_callback' => null, |
|
| 788 | - 'sanitize_callback' => null, |
|
| 789 | - ), |
|
| 790 | - 'group_by' => array( |
|
| 791 | - 'required' => false, |
|
| 792 | - 'default' => null, |
|
| 793 | - 'type' => array( |
|
| 794 | - 'object', |
|
| 795 | - 'string', |
|
| 796 | - ), |
|
| 797 | - // because we accept an almost infinite list of possible groupings, |
|
| 798 | - // WP core validation and sanitization |
|
| 799 | - // freaks out. We'll just validate this argument while handling the request |
|
| 800 | - 'validate_callback' => null, |
|
| 801 | - 'sanitize_callback' => null, |
|
| 802 | - ), |
|
| 803 | - 'having' => array( |
|
| 804 | - 'required' => false, |
|
| 805 | - 'default' => null, |
|
| 806 | - 'type' => 'object', |
|
| 807 | - // because we accept an almost infinite list of possible where conditions, WP |
|
| 808 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
| 809 | - // while handling the request |
|
| 810 | - 'validate_callback' => null, |
|
| 811 | - 'sanitize_callback' => null, |
|
| 812 | - ), |
|
| 813 | - 'caps' => array( |
|
| 814 | - 'required' => false, |
|
| 815 | - 'default' => EEM_Base::caps_read, |
|
| 816 | - 'type' => 'string', |
|
| 817 | - 'enum' => array( |
|
| 818 | - EEM_Base::caps_read, |
|
| 819 | - EEM_Base::caps_read_admin, |
|
| 820 | - EEM_Base::caps_edit, |
|
| 821 | - EEM_Base::caps_delete, |
|
| 822 | - ), |
|
| 823 | - ), |
|
| 824 | - ) |
|
| 825 | - ); |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - |
|
| 829 | - /** |
|
| 830 | - * Gets parameter information for a model regarding writing data |
|
| 831 | - * |
|
| 832 | - * @param string $model_name |
|
| 833 | - * @param ModelVersionInfo $model_version_info |
|
| 834 | - * @param boolean $create whether this is for request to create (in |
|
| 835 | - * which case we need all required params) or |
|
| 836 | - * just to update (in which case we don't |
|
| 837 | - * need those on every request) |
|
| 838 | - * @return array |
|
| 839 | - */ |
|
| 840 | - protected function _get_write_params( |
|
| 841 | - $model_name, |
|
| 842 | - ModelVersionInfo $model_version_info, |
|
| 843 | - $create = false |
|
| 844 | - ) { |
|
| 845 | - $model = EE_Registry::instance()->load_model($model_name); |
|
| 846 | - $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
| 847 | - $args_info = array(); |
|
| 848 | - foreach ($fields as $field_name => $field_obj) { |
|
| 849 | - if ($field_obj->is_auto_increment()) { |
|
| 850 | - // totally ignore auto increment IDs |
|
| 851 | - continue; |
|
| 852 | - } |
|
| 853 | - $arg_info = $field_obj->getSchema(); |
|
| 854 | - $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
| 855 | - $arg_info['required'] = $required; |
|
| 856 | - // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
| 857 | - unset($arg_info['readonly']); |
|
| 858 | - $schema_properties = $field_obj->getSchemaProperties(); |
|
| 859 | - if (isset($schema_properties['raw']) |
|
| 860 | - && $field_obj->getSchemaType() === 'object' |
|
| 861 | - ) { |
|
| 862 | - // if there's a "raw" form of this argument, use those properties instead |
|
| 863 | - $arg_info = array_replace( |
|
| 864 | - $arg_info, |
|
| 865 | - $schema_properties['raw'] |
|
| 866 | - ); |
|
| 867 | - } |
|
| 868 | - $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
| 869 | - $field_obj, |
|
| 870 | - $field_obj->get_default_value(), |
|
| 871 | - $model_version_info->requestedVersion() |
|
| 872 | - ); |
|
| 873 | - // we do our own validation and sanitization within the controller |
|
| 874 | - if (function_exists('rest_validate_value_from_schema')) { |
|
| 875 | - $sanitize_callback = array( |
|
| 876 | - 'EED_Core_Rest_Api', |
|
| 877 | - 'default_sanitize_callback', |
|
| 878 | - ); |
|
| 879 | - } else { |
|
| 880 | - $sanitize_callback = null; |
|
| 881 | - } |
|
| 882 | - $arg_info['sanitize_callback'] = $sanitize_callback; |
|
| 883 | - $args_info[ $field_name ] = $arg_info; |
|
| 884 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 885 | - $gmt_arg_info = $arg_info; |
|
| 886 | - $gmt_arg_info['description'] = sprintf( |
|
| 887 | - esc_html__( |
|
| 888 | - '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
| 889 | - 'event_espresso' |
|
| 890 | - ), |
|
| 891 | - $field_obj->get_nicename(), |
|
| 892 | - $field_name |
|
| 893 | - ); |
|
| 894 | - $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 895 | - } |
|
| 896 | - } |
|
| 897 | - return $args_info; |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - |
|
| 901 | - /** |
|
| 902 | - * Replacement for WP API's 'rest_parse_request_arg'. |
|
| 903 | - * If the value is blank but not required, don't bother validating it. |
|
| 904 | - * Also, it uses our email validation instead of WP API's default. |
|
| 905 | - * |
|
| 906 | - * @param $value |
|
| 907 | - * @param WP_REST_Request $request |
|
| 908 | - * @param $param |
|
| 909 | - * @return bool|true|WP_Error |
|
| 910 | - * @throws InvalidArgumentException |
|
| 911 | - * @throws InvalidInterfaceException |
|
| 912 | - * @throws InvalidDataTypeException |
|
| 913 | - */ |
|
| 914 | - public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
| 915 | - { |
|
| 916 | - $attributes = $request->get_attributes(); |
|
| 917 | - if (! isset($attributes['args'][ $param ]) |
|
| 918 | - || ! is_array($attributes['args'][ $param ])) { |
|
| 919 | - $validation_result = true; |
|
| 920 | - } else { |
|
| 921 | - $args = $attributes['args'][ $param ]; |
|
| 922 | - if (( |
|
| 923 | - $value === '' |
|
| 924 | - || $value === null |
|
| 925 | - ) |
|
| 926 | - && (! isset($args['required']) |
|
| 927 | - || $args['required'] === false |
|
| 928 | - ) |
|
| 929 | - ) { |
|
| 930 | - // not required and not provided? that's cool |
|
| 931 | - $validation_result = true; |
|
| 932 | - } elseif (isset($args['format']) |
|
| 933 | - && $args['format'] === 'email' |
|
| 934 | - ) { |
|
| 935 | - $validation_result = true; |
|
| 936 | - if (! self::_validate_email($value)) { |
|
| 937 | - $validation_result = new WP_Error( |
|
| 938 | - 'rest_invalid_param', |
|
| 939 | - esc_html__( |
|
| 940 | - 'The email address is not valid or does not exist.', |
|
| 941 | - 'event_espresso' |
|
| 942 | - ) |
|
| 943 | - ); |
|
| 944 | - } |
|
| 945 | - } else { |
|
| 946 | - $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
| 947 | - } |
|
| 948 | - } |
|
| 949 | - if (is_wp_error($validation_result)) { |
|
| 950 | - return $validation_result; |
|
| 951 | - } |
|
| 952 | - return rest_sanitize_request_arg($value, $request, $param); |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - |
|
| 956 | - /** |
|
| 957 | - * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
| 958 | - * |
|
| 959 | - * @param $email |
|
| 960 | - * @return bool |
|
| 961 | - * @throws InvalidArgumentException |
|
| 962 | - * @throws InvalidInterfaceException |
|
| 963 | - * @throws InvalidDataTypeException |
|
| 964 | - */ |
|
| 965 | - protected static function _validate_email($email) |
|
| 966 | - { |
|
| 967 | - try { |
|
| 968 | - EmailAddressFactory::create($email); |
|
| 969 | - return true; |
|
| 970 | - } catch (EmailValidationException $e) { |
|
| 971 | - return false; |
|
| 972 | - } |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - |
|
| 976 | - /** |
|
| 977 | - * Gets routes for the config |
|
| 978 | - * |
|
| 979 | - * @return array @see _register_model_routes |
|
| 980 | - * @deprecated since version 4.9.1 |
|
| 981 | - */ |
|
| 982 | - protected function _register_config_routes() |
|
| 983 | - { |
|
| 984 | - $config_routes = array(); |
|
| 985 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 986 | - $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 987 | - $version, |
|
| 988 | - $hidden_endpoint |
|
| 989 | - ); |
|
| 990 | - } |
|
| 991 | - return $config_routes; |
|
| 992 | - } |
|
| 993 | - |
|
| 994 | - |
|
| 995 | - /** |
|
| 996 | - * Gets routes for the config for the specified version |
|
| 997 | - * |
|
| 998 | - * @param string $version |
|
| 999 | - * @param boolean $hidden_endpoint |
|
| 1000 | - * @return array |
|
| 1001 | - */ |
|
| 1002 | - protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
| 1003 | - { |
|
| 1004 | - return array( |
|
| 1005 | - 'config' => array( |
|
| 1006 | - array( |
|
| 1007 | - 'callback' => array( |
|
| 1008 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1009 | - 'handleRequest', |
|
| 1010 | - ), |
|
| 1011 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1012 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1013 | - 'callback_args' => array($version), |
|
| 1014 | - ), |
|
| 1015 | - ), |
|
| 1016 | - 'site_info' => array( |
|
| 1017 | - array( |
|
| 1018 | - 'callback' => array( |
|
| 1019 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1020 | - 'handleRequestSiteInfo', |
|
| 1021 | - ), |
|
| 1022 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1023 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1024 | - 'callback_args' => array($version), |
|
| 1025 | - ), |
|
| 1026 | - ), |
|
| 1027 | - ); |
|
| 1028 | - } |
|
| 1029 | - |
|
| 1030 | - |
|
| 1031 | - /** |
|
| 1032 | - * Gets the meta info routes |
|
| 1033 | - * |
|
| 1034 | - * @return array @see _register_model_routes |
|
| 1035 | - * @deprecated since version 4.9.1 |
|
| 1036 | - */ |
|
| 1037 | - protected function _register_meta_routes() |
|
| 1038 | - { |
|
| 1039 | - $meta_routes = array(); |
|
| 1040 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 1041 | - $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1042 | - $version, |
|
| 1043 | - $hidden_endpoint |
|
| 1044 | - ); |
|
| 1045 | - } |
|
| 1046 | - return $meta_routes; |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - |
|
| 1050 | - /** |
|
| 1051 | - * @param string $version |
|
| 1052 | - * @param boolean $hidden_endpoint |
|
| 1053 | - * @return array |
|
| 1054 | - */ |
|
| 1055 | - protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
| 1056 | - { |
|
| 1057 | - return array( |
|
| 1058 | - 'resources' => array( |
|
| 1059 | - array( |
|
| 1060 | - 'callback' => array( |
|
| 1061 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
| 1062 | - 'handleRequestModelsMeta', |
|
| 1063 | - ), |
|
| 1064 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1065 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1066 | - 'callback_args' => array($version), |
|
| 1067 | - ), |
|
| 1068 | - ), |
|
| 1069 | - ); |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - |
|
| 1073 | - /** |
|
| 1074 | - * Tries to hide old 4.6 endpoints from the |
|
| 1075 | - * |
|
| 1076 | - * @param array $route_data |
|
| 1077 | - * @return array |
|
| 1078 | - * @throws \EE_Error |
|
| 1079 | - */ |
|
| 1080 | - public static function hide_old_endpoints($route_data) |
|
| 1081 | - { |
|
| 1082 | - // allow API clients to override which endpoints get hidden, in case |
|
| 1083 | - // they want to discover particular endpoints |
|
| 1084 | - // also, we don't have access to the request so we have to just grab it from the superglobal |
|
| 1085 | - $force_show_ee_namespace = ltrim( |
|
| 1086 | - EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
| 1087 | - '/' |
|
| 1088 | - ); |
|
| 1089 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
| 1090 | - foreach ($relative_urls as $resource_name => $endpoints) { |
|
| 1091 | - foreach ($endpoints as $key => $endpoint) { |
|
| 1092 | - // skip schema and other route options |
|
| 1093 | - if (! is_numeric($key)) { |
|
| 1094 | - continue; |
|
| 1095 | - } |
|
| 1096 | - // by default, hide "hidden_endpoint"s, unless the request indicates |
|
| 1097 | - // to $force_show_ee_namespace, in which case only show that one |
|
| 1098 | - // namespace's endpoints (and hide all others) |
|
| 1099 | - if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
| 1100 | - || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
| 1101 | - ) { |
|
| 1102 | - $full_route = '/' . ltrim($namespace, '/'); |
|
| 1103 | - $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1104 | - unset($route_data[ $full_route ]); |
|
| 1105 | - } |
|
| 1106 | - } |
|
| 1107 | - } |
|
| 1108 | - } |
|
| 1109 | - return $route_data; |
|
| 1110 | - } |
|
| 1111 | - |
|
| 1112 | - |
|
| 1113 | - /** |
|
| 1114 | - * Returns an array describing which versions of core support serving requests for. |
|
| 1115 | - * Keys are core versions' major and minor version, and values are the |
|
| 1116 | - * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
| 1117 | - * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
| 1118 | - * the answers table entirely, in which case it would be very difficult for |
|
| 1119 | - * it to serve 4.6-style responses. |
|
| 1120 | - * Versions of core that are missing from this array are unknowns. |
|
| 1121 | - * previous ver |
|
| 1122 | - * |
|
| 1123 | - * @return array |
|
| 1124 | - */ |
|
| 1125 | - public static function version_compatibilities() |
|
| 1126 | - { |
|
| 1127 | - return apply_filters( |
|
| 1128 | - 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
| 1129 | - array( |
|
| 1130 | - '4.8.29' => '4.8.29', |
|
| 1131 | - '4.8.33' => '4.8.29', |
|
| 1132 | - '4.8.34' => '4.8.29', |
|
| 1133 | - '4.8.36' => '4.8.29', |
|
| 1134 | - ) |
|
| 1135 | - ); |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - |
|
| 1139 | - /** |
|
| 1140 | - * Gets the latest API version served. Eg if there |
|
| 1141 | - * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
| 1142 | - * we are on core version 4.8.34, it will return the string "4.8.32" |
|
| 1143 | - * |
|
| 1144 | - * @return string |
|
| 1145 | - */ |
|
| 1146 | - public static function latest_rest_api_version() |
|
| 1147 | - { |
|
| 1148 | - $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
| 1149 | - $versions_served_keys = array_keys($versions_served); |
|
| 1150 | - return end($versions_served_keys); |
|
| 1151 | - } |
|
| 1152 | - |
|
| 1153 | - |
|
| 1154 | - /** |
|
| 1155 | - * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
| 1156 | - * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
| 1157 | - * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
| 1158 | - * We also indicate whether or not this version should be put in the index or not |
|
| 1159 | - * |
|
| 1160 | - * @return array keys are API version numbers (just major and minor numbers), and values |
|
| 1161 | - * are whether or not they should be hidden |
|
| 1162 | - */ |
|
| 1163 | - public static function versions_served() |
|
| 1164 | - { |
|
| 1165 | - $versions_served = array(); |
|
| 1166 | - $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
| 1167 | - $lowest_compatible_version = end($possibly_served_versions); |
|
| 1168 | - reset($possibly_served_versions); |
|
| 1169 | - $versions_served_historically = array_keys($possibly_served_versions); |
|
| 1170 | - $latest_version = end($versions_served_historically); |
|
| 1171 | - reset($versions_served_historically); |
|
| 1172 | - // for each version of core we have ever served: |
|
| 1173 | - foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
| 1174 | - // if it's not above the current core version, and it's compatible with the current version of core |
|
| 1175 | - if ($key_versioned_endpoint === $latest_version) { |
|
| 1176 | - // don't hide the latest version in the index |
|
| 1177 | - $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1178 | - } elseif ($key_versioned_endpoint >= $lowest_compatible_version |
|
| 1179 | - && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
| 1180 | - ) { |
|
| 1181 | - // include, but hide, previous versions which are still supported |
|
| 1182 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1183 | - } elseif (apply_filters( |
|
| 1184 | - 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
| 1185 | - false, |
|
| 1186 | - $possibly_served_versions |
|
| 1187 | - )) { |
|
| 1188 | - // if a version is no longer supported, don't include it in index or list of versions served |
|
| 1189 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1190 | - } |
|
| 1191 | - } |
|
| 1192 | - return $versions_served; |
|
| 1193 | - } |
|
| 1194 | - |
|
| 1195 | - |
|
| 1196 | - /** |
|
| 1197 | - * Gets the major and minor version of EE core's version string |
|
| 1198 | - * |
|
| 1199 | - * @return string |
|
| 1200 | - */ |
|
| 1201 | - public static function core_version() |
|
| 1202 | - { |
|
| 1203 | - return apply_filters( |
|
| 1204 | - 'FHEE__EED_Core_REST_API__core_version', |
|
| 1205 | - implode( |
|
| 1206 | - '.', |
|
| 1207 | - array_slice( |
|
| 1208 | - explode( |
|
| 1209 | - '.', |
|
| 1210 | - espresso_version() |
|
| 1211 | - ), |
|
| 1212 | - 0, |
|
| 1213 | - 3 |
|
| 1214 | - ) |
|
| 1215 | - ) |
|
| 1216 | - ); |
|
| 1217 | - } |
|
| 1218 | - |
|
| 1219 | - |
|
| 1220 | - /** |
|
| 1221 | - * Gets the default limit that should be used when querying for resources |
|
| 1222 | - * |
|
| 1223 | - * @return int |
|
| 1224 | - */ |
|
| 1225 | - public static function get_default_query_limit() |
|
| 1226 | - { |
|
| 1227 | - // we actually don't use a const because we want folks to always use |
|
| 1228 | - // this method, not the const directly |
|
| 1229 | - return apply_filters( |
|
| 1230 | - 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
| 1231 | - 50 |
|
| 1232 | - ); |
|
| 1233 | - } |
|
| 1234 | - |
|
| 1235 | - |
|
| 1236 | - /** |
|
| 1237 | - * |
|
| 1238 | - * @param string $version api version string (i.e. '4.8.36') |
|
| 1239 | - * @return array |
|
| 1240 | - */ |
|
| 1241 | - public static function getCollectionRoutesIndexedByModelName($version = '') |
|
| 1242 | - { |
|
| 1243 | - $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
| 1244 | - $model_names = self::model_names_with_plural_routes($version); |
|
| 1245 | - $collection_routes = array(); |
|
| 1246 | - foreach ($model_names as $model_name => $model_class_name) { |
|
| 1247 | - $collection_routes[ strtolower($model_name) ] = '/' . self::ee_api_namespace . $version . '/' |
|
| 1248 | - . EEH_Inflector::pluralize_and_lower($model_name); |
|
| 1249 | - } |
|
| 1250 | - return $collection_routes; |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - |
|
| 1254 | - /** |
|
| 1255 | - * Returns an array of primary key names indexed by model names. |
|
| 1256 | - * @param string $version |
|
| 1257 | - * @return array |
|
| 1258 | - */ |
|
| 1259 | - public static function getPrimaryKeyNamesIndexedByModelName($version = '') |
|
| 1260 | - { |
|
| 1261 | - $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
| 1262 | - $model_names = self::model_names_with_plural_routes($version); |
|
| 1263 | - $primary_key_items = array(); |
|
| 1264 | - foreach ($model_names as $model_name => $model_class_name) { |
|
| 1265 | - $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
|
| 1266 | - foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
|
| 1267 | - if (count($primary_keys) > 1) { |
|
| 1268 | - $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
| 1269 | - } else { |
|
| 1270 | - $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
| 1271 | - } |
|
| 1272 | - } |
|
| 1273 | - } |
|
| 1274 | - return $primary_key_items; |
|
| 1275 | - } |
|
| 1276 | - |
|
| 1277 | - |
|
| 1278 | - |
|
| 1279 | - /** |
|
| 1280 | - * run - initial module setup |
|
| 1281 | - * |
|
| 1282 | - * @access public |
|
| 1283 | - * @param WP $WP |
|
| 1284 | - * @return void |
|
| 1285 | - */ |
|
| 1286 | - public function run($WP) |
|
| 1287 | - { |
|
| 1288 | - } |
|
| 25 | + const ee_api_namespace = Domain::API_NAMESPACE; |
|
| 26 | + |
|
| 27 | + const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
| 28 | + |
|
| 29 | + const saved_routes_option_names = 'ee_core_routes'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * string used in _links response bodies to make them globally unique. |
|
| 33 | + * |
|
| 34 | + * @see http://v2.wp-api.org/extending/linking/ |
|
| 35 | + */ |
|
| 36 | + const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var CalculatedModelFields |
|
| 40 | + */ |
|
| 41 | + protected static $_field_calculator; |
|
| 42 | + |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @return EED_Core_Rest_Api|EED_Module |
|
| 46 | + */ |
|
| 47 | + public static function instance() |
|
| 48 | + { |
|
| 49 | + self::$_field_calculator = new CalculatedModelFields(); |
|
| 50 | + return parent::get_instance(__CLASS__); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
| 56 | + * |
|
| 57 | + * @access public |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public static function set_hooks() |
|
| 61 | + { |
|
| 62 | + self::set_hooks_both(); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 68 | + * |
|
| 69 | + * @access public |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public static function set_hooks_admin() |
|
| 73 | + { |
|
| 74 | + self::set_hooks_both(); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + public static function set_hooks_both() |
|
| 79 | + { |
|
| 80 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
| 81 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
| 82 | + add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
| 83 | + add_filter( |
|
| 84 | + 'rest_index', |
|
| 85 | + array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex') |
|
| 86 | + ); |
|
| 87 | + EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * sets up hooks which only need to be included as part of REST API requests; |
|
| 93 | + * other requests like to the frontend or admin etc don't need them |
|
| 94 | + * |
|
| 95 | + * @throws \EE_Error |
|
| 96 | + */ |
|
| 97 | + public static function set_hooks_rest_api() |
|
| 98 | + { |
|
| 99 | + // set hooks which account for changes made to the API |
|
| 100 | + EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * public wrapper of _set_hooks_for_changes. |
|
| 106 | + * Loads all the hooks which make requests to old versions of the API |
|
| 107 | + * appear the same as they always did |
|
| 108 | + * |
|
| 109 | + * @throws EE_Error |
|
| 110 | + */ |
|
| 111 | + public static function set_hooks_for_changes() |
|
| 112 | + { |
|
| 113 | + self::_set_hooks_for_changes(); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Loads all the hooks which make requests to old versions of the API |
|
| 119 | + * appear the same as they always did |
|
| 120 | + * |
|
| 121 | + * @throws EE_Error |
|
| 122 | + */ |
|
| 123 | + protected static function _set_hooks_for_changes() |
|
| 124 | + { |
|
| 125 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
| 126 | + foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
| 127 | + // ignore the base parent class |
|
| 128 | + // and legacy named classes |
|
| 129 | + if ($classname_in_namespace === 'ChangesInBase' |
|
| 130 | + || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
| 131 | + ) { |
|
| 132 | + continue; |
|
| 133 | + } |
|
| 134 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 135 | + if (class_exists($full_classname)) { |
|
| 136 | + $instance_of_class = new $full_classname; |
|
| 137 | + if ($instance_of_class instanceof ChangesInBase) { |
|
| 138 | + $instance_of_class->setHooks(); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
| 147 | + * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
| 148 | + * |
|
| 149 | + * @throws \EE_Error |
|
| 150 | + */ |
|
| 151 | + public static function register_routes() |
|
| 152 | + { |
|
| 153 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
| 154 | + foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
| 155 | + /** |
|
| 156 | + * @var array $data_for_multiple_endpoints numerically indexed array |
|
| 157 | + * but can also contain route options like { |
|
| 158 | + * @type array $schema { |
|
| 159 | + * @type callable $schema_callback |
|
| 160 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 161 | + * WP_REST_Request of course |
|
| 162 | + * } |
|
| 163 | + * } |
|
| 164 | + */ |
|
| 165 | + // when registering routes, register all the endpoints' data at the same time |
|
| 166 | + $multiple_endpoint_args = array(); |
|
| 167 | + foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
| 168 | + /** |
|
| 169 | + * @var array $data_for_single_endpoint { |
|
| 170 | + * @type callable $callback |
|
| 171 | + * @type string methods |
|
| 172 | + * @type array args |
|
| 173 | + * @type array _links |
|
| 174 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 175 | + * WP_REST_Request of course |
|
| 176 | + * } |
|
| 177 | + */ |
|
| 178 | + // skip route options |
|
| 179 | + if (! is_numeric($endpoint_key)) { |
|
| 180 | + continue; |
|
| 181 | + } |
|
| 182 | + if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 183 | + throw new EE_Error( |
|
| 184 | + esc_html__( |
|
| 185 | + // @codingStandardsIgnoreStart |
|
| 186 | + 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
| 187 | + // @codingStandardsIgnoreEnd |
|
| 188 | + 'event_espresso' |
|
| 189 | + ) |
|
| 190 | + ); |
|
| 191 | + } |
|
| 192 | + $callback = $data_for_single_endpoint['callback']; |
|
| 193 | + $single_endpoint_args = array( |
|
| 194 | + 'methods' => $data_for_single_endpoint['methods'], |
|
| 195 | + 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
| 196 | + : array(), |
|
| 197 | + ); |
|
| 198 | + if (isset($data_for_single_endpoint['_links'])) { |
|
| 199 | + $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
| 200 | + } |
|
| 201 | + if (isset($data_for_single_endpoint['callback_args'])) { |
|
| 202 | + $callback_args = $data_for_single_endpoint['callback_args']; |
|
| 203 | + $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
| 204 | + $callback, |
|
| 205 | + $callback_args |
|
| 206 | + ) { |
|
| 207 | + array_unshift($callback_args, $request); |
|
| 208 | + return call_user_func_array( |
|
| 209 | + $callback, |
|
| 210 | + $callback_args |
|
| 211 | + ); |
|
| 212 | + }; |
|
| 213 | + } else { |
|
| 214 | + $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
| 215 | + } |
|
| 216 | + $multiple_endpoint_args[] = $single_endpoint_args; |
|
| 217 | + } |
|
| 218 | + if (isset($data_for_multiple_endpoints['schema'])) { |
|
| 219 | + $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
| 220 | + $schema_callback = $schema_route_data['schema_callback']; |
|
| 221 | + $callback_args = $schema_route_data['callback_args']; |
|
| 222 | + $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
| 223 | + return call_user_func_array( |
|
| 224 | + $schema_callback, |
|
| 225 | + $callback_args |
|
| 226 | + ); |
|
| 227 | + }; |
|
| 228 | + } |
|
| 229 | + register_rest_route( |
|
| 230 | + $namespace, |
|
| 231 | + $relative_route, |
|
| 232 | + $multiple_endpoint_args |
|
| 233 | + ); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Checks if there was a version change or something that merits invalidating the cached |
|
| 241 | + * route data. If so, invalidates the cached route data so that it gets refreshed |
|
| 242 | + * next time the WP API is used |
|
| 243 | + */ |
|
| 244 | + public static function invalidate_cached_route_data_on_version_change() |
|
| 245 | + { |
|
| 246 | + if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
| 247 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 248 | + } |
|
| 249 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
| 250 | + if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
| 251 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Removes the cached route data so it will get refreshed next time the WP API is used |
|
| 259 | + */ |
|
| 260 | + public static function invalidate_cached_route_data() |
|
| 261 | + { |
|
| 262 | + // delete the saved EE REST API routes |
|
| 263 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
| 264 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Gets the EE route data |
|
| 271 | + * |
|
| 272 | + * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
| 273 | + * @throws \EE_Error |
|
| 274 | + * @type string|array $callback |
|
| 275 | + * @type string $methods |
|
| 276 | + * @type boolean $hidden_endpoint |
|
| 277 | + * } |
|
| 278 | + */ |
|
| 279 | + public static function get_ee_route_data() |
|
| 280 | + { |
|
| 281 | + $ee_routes = array(); |
|
| 282 | + foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
| 283 | + $ee_routes[ self::ee_api_namespace . $version ] = self::_get_ee_route_data_for_version( |
|
| 284 | + $version, |
|
| 285 | + $hidden_endpoints |
|
| 286 | + ); |
|
| 287 | + } |
|
| 288 | + return $ee_routes; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Gets the EE route data from the wp options if it exists already, |
|
| 294 | + * otherwise re-generates it and saves it to the option |
|
| 295 | + * |
|
| 296 | + * @param string $version |
|
| 297 | + * @param boolean $hidden_endpoints |
|
| 298 | + * @return array |
|
| 299 | + * @throws \EE_Error |
|
| 300 | + */ |
|
| 301 | + protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 302 | + { |
|
| 303 | + $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
| 304 | + if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
| 305 | + $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
| 306 | + } |
|
| 307 | + return $ee_routes; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Saves the EE REST API route data to a wp option and returns it |
|
| 313 | + * |
|
| 314 | + * @param string $version |
|
| 315 | + * @param boolean $hidden_endpoints |
|
| 316 | + * @return mixed|null |
|
| 317 | + * @throws \EE_Error |
|
| 318 | + */ |
|
| 319 | + protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 320 | + { |
|
| 321 | + $instance = self::instance(); |
|
| 322 | + $routes = apply_filters( |
|
| 323 | + 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
| 324 | + array_replace_recursive( |
|
| 325 | + $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
| 326 | + $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
| 327 | + $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
| 328 | + $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
| 329 | + ) |
|
| 330 | + ); |
|
| 331 | + $option_name = self::saved_routes_option_names . $version; |
|
| 332 | + if (get_option($option_name)) { |
|
| 333 | + update_option($option_name, $routes, true); |
|
| 334 | + } else { |
|
| 335 | + add_option($option_name, $routes, null, 'no'); |
|
| 336 | + } |
|
| 337 | + return $routes; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
| 343 | + * need to calculate it on every request |
|
| 344 | + * |
|
| 345 | + * @deprecated since version 4.9.1 |
|
| 346 | + * @return void |
|
| 347 | + */ |
|
| 348 | + public static function save_ee_routes() |
|
| 349 | + { |
|
| 350 | + if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 351 | + $instance = self::instance(); |
|
| 352 | + $routes = apply_filters( |
|
| 353 | + 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
| 354 | + array_replace_recursive( |
|
| 355 | + $instance->_register_config_routes(), |
|
| 356 | + $instance->_register_meta_routes(), |
|
| 357 | + $instance->_register_model_routes(), |
|
| 358 | + $instance->_register_rpc_routes() |
|
| 359 | + ) |
|
| 360 | + ); |
|
| 361 | + update_option(self::saved_routes_option_names, $routes, true); |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Gets all the route information relating to EE models |
|
| 368 | + * |
|
| 369 | + * @return array @see get_ee_route_data |
|
| 370 | + * @deprecated since version 4.9.1 |
|
| 371 | + */ |
|
| 372 | + protected function _register_model_routes() |
|
| 373 | + { |
|
| 374 | + $model_routes = array(); |
|
| 375 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 376 | + $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 377 | + . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 378 | + } |
|
| 379 | + return $model_routes; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Decides whether or not to add write endpoints for this model. |
|
| 385 | + * |
|
| 386 | + * Currently, this defaults to exclude all global tables and models |
|
| 387 | + * which would allow inserting WP core data (we don't want to duplicate |
|
| 388 | + * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
| 389 | + * |
|
| 390 | + * @param EEM_Base $model |
|
| 391 | + * @return bool |
|
| 392 | + */ |
|
| 393 | + public static function should_have_write_endpoints(EEM_Base $model) |
|
| 394 | + { |
|
| 395 | + if ($model->is_wp_core_model()) { |
|
| 396 | + return false; |
|
| 397 | + } |
|
| 398 | + foreach ($model->get_tables() as $table) { |
|
| 399 | + if ($table->is_global()) { |
|
| 400 | + return false; |
|
| 401 | + } |
|
| 402 | + } |
|
| 403 | + return true; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
| 409 | + * in this versioned namespace of EE4 |
|
| 410 | + * |
|
| 411 | + * @param $version |
|
| 412 | + * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
| 413 | + */ |
|
| 414 | + public static function model_names_with_plural_routes($version) |
|
| 415 | + { |
|
| 416 | + $model_version_info = new ModelVersionInfo($version); |
|
| 417 | + $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
| 418 | + // let's not bother having endpoints for extra metas |
|
| 419 | + unset( |
|
| 420 | + $models_to_register['Extra_Meta'], |
|
| 421 | + $models_to_register['Extra_Join'], |
|
| 422 | + $models_to_register['Post_Meta'] |
|
| 423 | + ); |
|
| 424 | + return apply_filters( |
|
| 425 | + 'FHEE__EED_Core_REST_API___register_model_routes', |
|
| 426 | + $models_to_register |
|
| 427 | + ); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Gets the route data for EE models in the specified version |
|
| 433 | + * |
|
| 434 | + * @param string $version |
|
| 435 | + * @param boolean $hidden_endpoint |
|
| 436 | + * @return array |
|
| 437 | + * @throws EE_Error |
|
| 438 | + */ |
|
| 439 | + protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
| 440 | + { |
|
| 441 | + $model_routes = array(); |
|
| 442 | + $model_version_info = new ModelVersionInfo($version); |
|
| 443 | + foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
| 444 | + $model = \EE_Registry::instance()->load_model($model_name); |
|
| 445 | + // if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
| 446 | + if (! $model instanceof EEM_Base) { |
|
| 447 | + continue; |
|
| 448 | + } |
|
| 449 | + // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
| 450 | + $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
| 451 | + $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
| 452 | + $model_routes[ $plural_model_route ] = array( |
|
| 453 | + array( |
|
| 454 | + 'callback' => array( |
|
| 455 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 456 | + 'handleRequestGetAll', |
|
| 457 | + ), |
|
| 458 | + 'callback_args' => array($version, $model_name), |
|
| 459 | + 'methods' => WP_REST_Server::READABLE, |
|
| 460 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 461 | + 'args' => $this->_get_read_query_params($model, $version), |
|
| 462 | + '_links' => array( |
|
| 463 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 464 | + ), |
|
| 465 | + ), |
|
| 466 | + 'schema' => array( |
|
| 467 | + 'schema_callback' => array( |
|
| 468 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 469 | + 'handleSchemaRequest', |
|
| 470 | + ), |
|
| 471 | + 'callback_args' => array($version, $model_name), |
|
| 472 | + ), |
|
| 473 | + ); |
|
| 474 | + $model_routes[ $singular_model_route ] = array( |
|
| 475 | + array( |
|
| 476 | + 'callback' => array( |
|
| 477 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 478 | + 'handleRequestGetOne', |
|
| 479 | + ), |
|
| 480 | + 'callback_args' => array($version, $model_name), |
|
| 481 | + 'methods' => WP_REST_Server::READABLE, |
|
| 482 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 483 | + 'args' => $this->_get_response_selection_query_params($model, $version), |
|
| 484 | + ), |
|
| 485 | + ); |
|
| 486 | + if (apply_filters( |
|
| 487 | + 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
| 488 | + EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
| 489 | + $model |
|
| 490 | + )) { |
|
| 491 | + $model_routes[ $plural_model_route ][] = array( |
|
| 492 | + 'callback' => array( |
|
| 493 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 494 | + 'handleRequestInsert', |
|
| 495 | + ), |
|
| 496 | + 'callback_args' => array($version, $model_name), |
|
| 497 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 498 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 499 | + 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
| 500 | + ); |
|
| 501 | + $model_routes[ $singular_model_route ] = array_merge( |
|
| 502 | + $model_routes[ $singular_model_route ], |
|
| 503 | + array( |
|
| 504 | + array( |
|
| 505 | + 'callback' => array( |
|
| 506 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 507 | + 'handleRequestUpdate', |
|
| 508 | + ), |
|
| 509 | + 'callback_args' => array($version, $model_name), |
|
| 510 | + 'methods' => WP_REST_Server::EDITABLE, |
|
| 511 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 512 | + 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
| 513 | + ), |
|
| 514 | + array( |
|
| 515 | + 'callback' => array( |
|
| 516 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 517 | + 'handleRequestDelete', |
|
| 518 | + ), |
|
| 519 | + 'callback_args' => array($version, $model_name), |
|
| 520 | + 'methods' => WP_REST_Server::DELETABLE, |
|
| 521 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 522 | + 'args' => $this->_get_delete_query_params($model, $version), |
|
| 523 | + ), |
|
| 524 | + ) |
|
| 525 | + ); |
|
| 526 | + } |
|
| 527 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 528 | + $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
| 529 | + $model, |
|
| 530 | + '(?P<id>[^\/]+)', |
|
| 531 | + $relation_obj |
|
| 532 | + ); |
|
| 533 | + $endpoints = array( |
|
| 534 | + array( |
|
| 535 | + 'callback' => array( |
|
| 536 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 537 | + 'handleRequestGetRelated', |
|
| 538 | + ), |
|
| 539 | + 'callback_args' => array($version, $model_name, $relation_name), |
|
| 540 | + 'methods' => WP_REST_Server::READABLE, |
|
| 541 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 542 | + 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
| 543 | + ), |
|
| 544 | + ); |
|
| 545 | + $model_routes[ $related_route ] = $endpoints; |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + return $model_routes; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
| 554 | + * excluding the preceding slash. |
|
| 555 | + * Eg you pass get_plural_route_to('Event') = 'events' |
|
| 556 | + * |
|
| 557 | + * @param EEM_Base $model |
|
| 558 | + * @return string |
|
| 559 | + */ |
|
| 560 | + public static function get_collection_route(EEM_Base $model) |
|
| 561 | + { |
|
| 562 | + return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 568 | + * excluding the preceding slash. |
|
| 569 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 570 | + * |
|
| 571 | + * @param EEM_Base $model eg Event or Venue |
|
| 572 | + * @param string $id |
|
| 573 | + * @return string |
|
| 574 | + */ |
|
| 575 | + public static function get_entity_route($model, $id) |
|
| 576 | + { |
|
| 577 | + return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 583 | + * excluding the preceding slash. |
|
| 584 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 585 | + * |
|
| 586 | + * @param EEM_Base $model eg Event or Venue |
|
| 587 | + * @param string $id |
|
| 588 | + * @param EE_Model_Relation_Base $relation_obj |
|
| 589 | + * @return string |
|
| 590 | + */ |
|
| 591 | + public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
| 592 | + { |
|
| 593 | + $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
| 594 | + $relation_obj->get_other_model()->get_this_model_name(), |
|
| 595 | + $relation_obj |
|
| 596 | + ); |
|
| 597 | + return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
| 603 | + * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
| 604 | + * |
|
| 605 | + * @param string $relative_route |
|
| 606 | + * @param string $version |
|
| 607 | + * @return string |
|
| 608 | + */ |
|
| 609 | + public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
| 610 | + { |
|
| 611 | + return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
| 617 | + * routes that don't conform to the traditional REST CRUD-style). |
|
| 618 | + * |
|
| 619 | + * @deprecated since 4.9.1 |
|
| 620 | + */ |
|
| 621 | + protected function _register_rpc_routes() |
|
| 622 | + { |
|
| 623 | + $routes = array(); |
|
| 624 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 625 | + $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 626 | + $version, |
|
| 627 | + $hidden_endpoint |
|
| 628 | + ); |
|
| 629 | + } |
|
| 630 | + return $routes; |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * @param string $version |
|
| 636 | + * @param boolean $hidden_endpoint |
|
| 637 | + * @return array |
|
| 638 | + */ |
|
| 639 | + protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
| 640 | + { |
|
| 641 | + $this_versions_routes = array(); |
|
| 642 | + // checkin endpoint |
|
| 643 | + $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
| 644 | + array( |
|
| 645 | + 'callback' => array( |
|
| 646 | + 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
| 647 | + 'handleRequestToggleCheckin', |
|
| 648 | + ), |
|
| 649 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 650 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 651 | + 'args' => array( |
|
| 652 | + 'force' => array( |
|
| 653 | + 'required' => false, |
|
| 654 | + 'default' => false, |
|
| 655 | + 'description' => __( |
|
| 656 | + // @codingStandardsIgnoreStart |
|
| 657 | + 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
| 658 | + // @codingStandardsIgnoreEnd |
|
| 659 | + 'event_espresso' |
|
| 660 | + ), |
|
| 661 | + ), |
|
| 662 | + ), |
|
| 663 | + 'callback_args' => array($version), |
|
| 664 | + ), |
|
| 665 | + ); |
|
| 666 | + return apply_filters( |
|
| 667 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
| 668 | + $this_versions_routes, |
|
| 669 | + $version, |
|
| 670 | + $hidden_endpoint |
|
| 671 | + ); |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + |
|
| 675 | + /** |
|
| 676 | + * Gets the query params that can be used when request one or many |
|
| 677 | + * |
|
| 678 | + * @param EEM_Base $model |
|
| 679 | + * @param string $version |
|
| 680 | + * @return array |
|
| 681 | + */ |
|
| 682 | + protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
| 683 | + { |
|
| 684 | + return apply_filters( |
|
| 685 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
| 686 | + array( |
|
| 687 | + 'include' => array( |
|
| 688 | + 'required' => false, |
|
| 689 | + 'default' => '*', |
|
| 690 | + 'type' => 'string', |
|
| 691 | + ), |
|
| 692 | + 'calculate' => array( |
|
| 693 | + 'required' => false, |
|
| 694 | + 'default' => '', |
|
| 695 | + 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
| 696 | + 'type' => 'string', |
|
| 697 | + // because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
| 698 | + // freaks out. We'll just validate this argument while handling the request |
|
| 699 | + 'validate_callback' => null, |
|
| 700 | + 'sanitize_callback' => null, |
|
| 701 | + ), |
|
| 702 | + ), |
|
| 703 | + $model, |
|
| 704 | + $version |
|
| 705 | + ); |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * Gets the parameters acceptable for delete requests |
|
| 711 | + * |
|
| 712 | + * @param \EEM_Base $model |
|
| 713 | + * @param string $version |
|
| 714 | + * @return array |
|
| 715 | + */ |
|
| 716 | + protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
| 717 | + { |
|
| 718 | + $params_for_delete = array( |
|
| 719 | + 'allow_blocking' => array( |
|
| 720 | + 'required' => false, |
|
| 721 | + 'default' => true, |
|
| 722 | + 'type' => 'boolean', |
|
| 723 | + ), |
|
| 724 | + ); |
|
| 725 | + $params_for_delete['force'] = array( |
|
| 726 | + 'required' => false, |
|
| 727 | + 'default' => false, |
|
| 728 | + 'type' => 'boolean', |
|
| 729 | + ); |
|
| 730 | + return apply_filters( |
|
| 731 | + 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
| 732 | + $params_for_delete, |
|
| 733 | + $model, |
|
| 734 | + $version |
|
| 735 | + ); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Gets info about reading query params that are acceptable |
|
| 741 | + * |
|
| 742 | + * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
| 743 | + * @param string $version |
|
| 744 | + * @return array describing the args acceptable when querying this model |
|
| 745 | + * @throws EE_Error |
|
| 746 | + */ |
|
| 747 | + protected function _get_read_query_params(\EEM_Base $model, $version) |
|
| 748 | + { |
|
| 749 | + $default_orderby = array(); |
|
| 750 | + foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
| 751 | + $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 752 | + } |
|
| 753 | + return array_merge( |
|
| 754 | + $this->_get_response_selection_query_params($model, $version), |
|
| 755 | + array( |
|
| 756 | + 'where' => array( |
|
| 757 | + 'required' => false, |
|
| 758 | + 'default' => array(), |
|
| 759 | + 'type' => 'object', |
|
| 760 | + // because we accept an almost infinite list of possible where conditions, WP |
|
| 761 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
| 762 | + // while handling the request |
|
| 763 | + 'validate_callback' => null, |
|
| 764 | + 'sanitize_callback' => null, |
|
| 765 | + ), |
|
| 766 | + 'limit' => array( |
|
| 767 | + 'required' => false, |
|
| 768 | + 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
| 769 | + 'type' => array( |
|
| 770 | + 'array', |
|
| 771 | + 'string', |
|
| 772 | + 'integer', |
|
| 773 | + ), |
|
| 774 | + // because we accept a variety of types, WP core validation and sanitization |
|
| 775 | + // freaks out. We'll just validate this argument while handling the request |
|
| 776 | + 'validate_callback' => null, |
|
| 777 | + 'sanitize_callback' => null, |
|
| 778 | + ), |
|
| 779 | + 'order_by' => array( |
|
| 780 | + 'required' => false, |
|
| 781 | + 'default' => $default_orderby, |
|
| 782 | + 'type' => array( |
|
| 783 | + 'object', |
|
| 784 | + 'string', |
|
| 785 | + ),// because we accept a variety of types, WP core validation and sanitization |
|
| 786 | + // freaks out. We'll just validate this argument while handling the request |
|
| 787 | + 'validate_callback' => null, |
|
| 788 | + 'sanitize_callback' => null, |
|
| 789 | + ), |
|
| 790 | + 'group_by' => array( |
|
| 791 | + 'required' => false, |
|
| 792 | + 'default' => null, |
|
| 793 | + 'type' => array( |
|
| 794 | + 'object', |
|
| 795 | + 'string', |
|
| 796 | + ), |
|
| 797 | + // because we accept an almost infinite list of possible groupings, |
|
| 798 | + // WP core validation and sanitization |
|
| 799 | + // freaks out. We'll just validate this argument while handling the request |
|
| 800 | + 'validate_callback' => null, |
|
| 801 | + 'sanitize_callback' => null, |
|
| 802 | + ), |
|
| 803 | + 'having' => array( |
|
| 804 | + 'required' => false, |
|
| 805 | + 'default' => null, |
|
| 806 | + 'type' => 'object', |
|
| 807 | + // because we accept an almost infinite list of possible where conditions, WP |
|
| 808 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
| 809 | + // while handling the request |
|
| 810 | + 'validate_callback' => null, |
|
| 811 | + 'sanitize_callback' => null, |
|
| 812 | + ), |
|
| 813 | + 'caps' => array( |
|
| 814 | + 'required' => false, |
|
| 815 | + 'default' => EEM_Base::caps_read, |
|
| 816 | + 'type' => 'string', |
|
| 817 | + 'enum' => array( |
|
| 818 | + EEM_Base::caps_read, |
|
| 819 | + EEM_Base::caps_read_admin, |
|
| 820 | + EEM_Base::caps_edit, |
|
| 821 | + EEM_Base::caps_delete, |
|
| 822 | + ), |
|
| 823 | + ), |
|
| 824 | + ) |
|
| 825 | + ); |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + |
|
| 829 | + /** |
|
| 830 | + * Gets parameter information for a model regarding writing data |
|
| 831 | + * |
|
| 832 | + * @param string $model_name |
|
| 833 | + * @param ModelVersionInfo $model_version_info |
|
| 834 | + * @param boolean $create whether this is for request to create (in |
|
| 835 | + * which case we need all required params) or |
|
| 836 | + * just to update (in which case we don't |
|
| 837 | + * need those on every request) |
|
| 838 | + * @return array |
|
| 839 | + */ |
|
| 840 | + protected function _get_write_params( |
|
| 841 | + $model_name, |
|
| 842 | + ModelVersionInfo $model_version_info, |
|
| 843 | + $create = false |
|
| 844 | + ) { |
|
| 845 | + $model = EE_Registry::instance()->load_model($model_name); |
|
| 846 | + $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
| 847 | + $args_info = array(); |
|
| 848 | + foreach ($fields as $field_name => $field_obj) { |
|
| 849 | + if ($field_obj->is_auto_increment()) { |
|
| 850 | + // totally ignore auto increment IDs |
|
| 851 | + continue; |
|
| 852 | + } |
|
| 853 | + $arg_info = $field_obj->getSchema(); |
|
| 854 | + $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
| 855 | + $arg_info['required'] = $required; |
|
| 856 | + // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
| 857 | + unset($arg_info['readonly']); |
|
| 858 | + $schema_properties = $field_obj->getSchemaProperties(); |
|
| 859 | + if (isset($schema_properties['raw']) |
|
| 860 | + && $field_obj->getSchemaType() === 'object' |
|
| 861 | + ) { |
|
| 862 | + // if there's a "raw" form of this argument, use those properties instead |
|
| 863 | + $arg_info = array_replace( |
|
| 864 | + $arg_info, |
|
| 865 | + $schema_properties['raw'] |
|
| 866 | + ); |
|
| 867 | + } |
|
| 868 | + $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
| 869 | + $field_obj, |
|
| 870 | + $field_obj->get_default_value(), |
|
| 871 | + $model_version_info->requestedVersion() |
|
| 872 | + ); |
|
| 873 | + // we do our own validation and sanitization within the controller |
|
| 874 | + if (function_exists('rest_validate_value_from_schema')) { |
|
| 875 | + $sanitize_callback = array( |
|
| 876 | + 'EED_Core_Rest_Api', |
|
| 877 | + 'default_sanitize_callback', |
|
| 878 | + ); |
|
| 879 | + } else { |
|
| 880 | + $sanitize_callback = null; |
|
| 881 | + } |
|
| 882 | + $arg_info['sanitize_callback'] = $sanitize_callback; |
|
| 883 | + $args_info[ $field_name ] = $arg_info; |
|
| 884 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 885 | + $gmt_arg_info = $arg_info; |
|
| 886 | + $gmt_arg_info['description'] = sprintf( |
|
| 887 | + esc_html__( |
|
| 888 | + '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
| 889 | + 'event_espresso' |
|
| 890 | + ), |
|
| 891 | + $field_obj->get_nicename(), |
|
| 892 | + $field_name |
|
| 893 | + ); |
|
| 894 | + $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 895 | + } |
|
| 896 | + } |
|
| 897 | + return $args_info; |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + |
|
| 901 | + /** |
|
| 902 | + * Replacement for WP API's 'rest_parse_request_arg'. |
|
| 903 | + * If the value is blank but not required, don't bother validating it. |
|
| 904 | + * Also, it uses our email validation instead of WP API's default. |
|
| 905 | + * |
|
| 906 | + * @param $value |
|
| 907 | + * @param WP_REST_Request $request |
|
| 908 | + * @param $param |
|
| 909 | + * @return bool|true|WP_Error |
|
| 910 | + * @throws InvalidArgumentException |
|
| 911 | + * @throws InvalidInterfaceException |
|
| 912 | + * @throws InvalidDataTypeException |
|
| 913 | + */ |
|
| 914 | + public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
| 915 | + { |
|
| 916 | + $attributes = $request->get_attributes(); |
|
| 917 | + if (! isset($attributes['args'][ $param ]) |
|
| 918 | + || ! is_array($attributes['args'][ $param ])) { |
|
| 919 | + $validation_result = true; |
|
| 920 | + } else { |
|
| 921 | + $args = $attributes['args'][ $param ]; |
|
| 922 | + if (( |
|
| 923 | + $value === '' |
|
| 924 | + || $value === null |
|
| 925 | + ) |
|
| 926 | + && (! isset($args['required']) |
|
| 927 | + || $args['required'] === false |
|
| 928 | + ) |
|
| 929 | + ) { |
|
| 930 | + // not required and not provided? that's cool |
|
| 931 | + $validation_result = true; |
|
| 932 | + } elseif (isset($args['format']) |
|
| 933 | + && $args['format'] === 'email' |
|
| 934 | + ) { |
|
| 935 | + $validation_result = true; |
|
| 936 | + if (! self::_validate_email($value)) { |
|
| 937 | + $validation_result = new WP_Error( |
|
| 938 | + 'rest_invalid_param', |
|
| 939 | + esc_html__( |
|
| 940 | + 'The email address is not valid or does not exist.', |
|
| 941 | + 'event_espresso' |
|
| 942 | + ) |
|
| 943 | + ); |
|
| 944 | + } |
|
| 945 | + } else { |
|
| 946 | + $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
| 947 | + } |
|
| 948 | + } |
|
| 949 | + if (is_wp_error($validation_result)) { |
|
| 950 | + return $validation_result; |
|
| 951 | + } |
|
| 952 | + return rest_sanitize_request_arg($value, $request, $param); |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + |
|
| 956 | + /** |
|
| 957 | + * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
| 958 | + * |
|
| 959 | + * @param $email |
|
| 960 | + * @return bool |
|
| 961 | + * @throws InvalidArgumentException |
|
| 962 | + * @throws InvalidInterfaceException |
|
| 963 | + * @throws InvalidDataTypeException |
|
| 964 | + */ |
|
| 965 | + protected static function _validate_email($email) |
|
| 966 | + { |
|
| 967 | + try { |
|
| 968 | + EmailAddressFactory::create($email); |
|
| 969 | + return true; |
|
| 970 | + } catch (EmailValidationException $e) { |
|
| 971 | + return false; |
|
| 972 | + } |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + |
|
| 976 | + /** |
|
| 977 | + * Gets routes for the config |
|
| 978 | + * |
|
| 979 | + * @return array @see _register_model_routes |
|
| 980 | + * @deprecated since version 4.9.1 |
|
| 981 | + */ |
|
| 982 | + protected function _register_config_routes() |
|
| 983 | + { |
|
| 984 | + $config_routes = array(); |
|
| 985 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 986 | + $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 987 | + $version, |
|
| 988 | + $hidden_endpoint |
|
| 989 | + ); |
|
| 990 | + } |
|
| 991 | + return $config_routes; |
|
| 992 | + } |
|
| 993 | + |
|
| 994 | + |
|
| 995 | + /** |
|
| 996 | + * Gets routes for the config for the specified version |
|
| 997 | + * |
|
| 998 | + * @param string $version |
|
| 999 | + * @param boolean $hidden_endpoint |
|
| 1000 | + * @return array |
|
| 1001 | + */ |
|
| 1002 | + protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
| 1003 | + { |
|
| 1004 | + return array( |
|
| 1005 | + 'config' => array( |
|
| 1006 | + array( |
|
| 1007 | + 'callback' => array( |
|
| 1008 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1009 | + 'handleRequest', |
|
| 1010 | + ), |
|
| 1011 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1012 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1013 | + 'callback_args' => array($version), |
|
| 1014 | + ), |
|
| 1015 | + ), |
|
| 1016 | + 'site_info' => array( |
|
| 1017 | + array( |
|
| 1018 | + 'callback' => array( |
|
| 1019 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1020 | + 'handleRequestSiteInfo', |
|
| 1021 | + ), |
|
| 1022 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1023 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1024 | + 'callback_args' => array($version), |
|
| 1025 | + ), |
|
| 1026 | + ), |
|
| 1027 | + ); |
|
| 1028 | + } |
|
| 1029 | + |
|
| 1030 | + |
|
| 1031 | + /** |
|
| 1032 | + * Gets the meta info routes |
|
| 1033 | + * |
|
| 1034 | + * @return array @see _register_model_routes |
|
| 1035 | + * @deprecated since version 4.9.1 |
|
| 1036 | + */ |
|
| 1037 | + protected function _register_meta_routes() |
|
| 1038 | + { |
|
| 1039 | + $meta_routes = array(); |
|
| 1040 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 1041 | + $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1042 | + $version, |
|
| 1043 | + $hidden_endpoint |
|
| 1044 | + ); |
|
| 1045 | + } |
|
| 1046 | + return $meta_routes; |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + |
|
| 1050 | + /** |
|
| 1051 | + * @param string $version |
|
| 1052 | + * @param boolean $hidden_endpoint |
|
| 1053 | + * @return array |
|
| 1054 | + */ |
|
| 1055 | + protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
| 1056 | + { |
|
| 1057 | + return array( |
|
| 1058 | + 'resources' => array( |
|
| 1059 | + array( |
|
| 1060 | + 'callback' => array( |
|
| 1061 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
| 1062 | + 'handleRequestModelsMeta', |
|
| 1063 | + ), |
|
| 1064 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1065 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1066 | + 'callback_args' => array($version), |
|
| 1067 | + ), |
|
| 1068 | + ), |
|
| 1069 | + ); |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + |
|
| 1073 | + /** |
|
| 1074 | + * Tries to hide old 4.6 endpoints from the |
|
| 1075 | + * |
|
| 1076 | + * @param array $route_data |
|
| 1077 | + * @return array |
|
| 1078 | + * @throws \EE_Error |
|
| 1079 | + */ |
|
| 1080 | + public static function hide_old_endpoints($route_data) |
|
| 1081 | + { |
|
| 1082 | + // allow API clients to override which endpoints get hidden, in case |
|
| 1083 | + // they want to discover particular endpoints |
|
| 1084 | + // also, we don't have access to the request so we have to just grab it from the superglobal |
|
| 1085 | + $force_show_ee_namespace = ltrim( |
|
| 1086 | + EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
| 1087 | + '/' |
|
| 1088 | + ); |
|
| 1089 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
| 1090 | + foreach ($relative_urls as $resource_name => $endpoints) { |
|
| 1091 | + foreach ($endpoints as $key => $endpoint) { |
|
| 1092 | + // skip schema and other route options |
|
| 1093 | + if (! is_numeric($key)) { |
|
| 1094 | + continue; |
|
| 1095 | + } |
|
| 1096 | + // by default, hide "hidden_endpoint"s, unless the request indicates |
|
| 1097 | + // to $force_show_ee_namespace, in which case only show that one |
|
| 1098 | + // namespace's endpoints (and hide all others) |
|
| 1099 | + if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
| 1100 | + || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
| 1101 | + ) { |
|
| 1102 | + $full_route = '/' . ltrim($namespace, '/'); |
|
| 1103 | + $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1104 | + unset($route_data[ $full_route ]); |
|
| 1105 | + } |
|
| 1106 | + } |
|
| 1107 | + } |
|
| 1108 | + } |
|
| 1109 | + return $route_data; |
|
| 1110 | + } |
|
| 1111 | + |
|
| 1112 | + |
|
| 1113 | + /** |
|
| 1114 | + * Returns an array describing which versions of core support serving requests for. |
|
| 1115 | + * Keys are core versions' major and minor version, and values are the |
|
| 1116 | + * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
| 1117 | + * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
| 1118 | + * the answers table entirely, in which case it would be very difficult for |
|
| 1119 | + * it to serve 4.6-style responses. |
|
| 1120 | + * Versions of core that are missing from this array are unknowns. |
|
| 1121 | + * previous ver |
|
| 1122 | + * |
|
| 1123 | + * @return array |
|
| 1124 | + */ |
|
| 1125 | + public static function version_compatibilities() |
|
| 1126 | + { |
|
| 1127 | + return apply_filters( |
|
| 1128 | + 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
| 1129 | + array( |
|
| 1130 | + '4.8.29' => '4.8.29', |
|
| 1131 | + '4.8.33' => '4.8.29', |
|
| 1132 | + '4.8.34' => '4.8.29', |
|
| 1133 | + '4.8.36' => '4.8.29', |
|
| 1134 | + ) |
|
| 1135 | + ); |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + |
|
| 1139 | + /** |
|
| 1140 | + * Gets the latest API version served. Eg if there |
|
| 1141 | + * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
| 1142 | + * we are on core version 4.8.34, it will return the string "4.8.32" |
|
| 1143 | + * |
|
| 1144 | + * @return string |
|
| 1145 | + */ |
|
| 1146 | + public static function latest_rest_api_version() |
|
| 1147 | + { |
|
| 1148 | + $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
| 1149 | + $versions_served_keys = array_keys($versions_served); |
|
| 1150 | + return end($versions_served_keys); |
|
| 1151 | + } |
|
| 1152 | + |
|
| 1153 | + |
|
| 1154 | + /** |
|
| 1155 | + * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
| 1156 | + * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
| 1157 | + * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
| 1158 | + * We also indicate whether or not this version should be put in the index or not |
|
| 1159 | + * |
|
| 1160 | + * @return array keys are API version numbers (just major and minor numbers), and values |
|
| 1161 | + * are whether or not they should be hidden |
|
| 1162 | + */ |
|
| 1163 | + public static function versions_served() |
|
| 1164 | + { |
|
| 1165 | + $versions_served = array(); |
|
| 1166 | + $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
| 1167 | + $lowest_compatible_version = end($possibly_served_versions); |
|
| 1168 | + reset($possibly_served_versions); |
|
| 1169 | + $versions_served_historically = array_keys($possibly_served_versions); |
|
| 1170 | + $latest_version = end($versions_served_historically); |
|
| 1171 | + reset($versions_served_historically); |
|
| 1172 | + // for each version of core we have ever served: |
|
| 1173 | + foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
| 1174 | + // if it's not above the current core version, and it's compatible with the current version of core |
|
| 1175 | + if ($key_versioned_endpoint === $latest_version) { |
|
| 1176 | + // don't hide the latest version in the index |
|
| 1177 | + $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1178 | + } elseif ($key_versioned_endpoint >= $lowest_compatible_version |
|
| 1179 | + && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
| 1180 | + ) { |
|
| 1181 | + // include, but hide, previous versions which are still supported |
|
| 1182 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1183 | + } elseif (apply_filters( |
|
| 1184 | + 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
| 1185 | + false, |
|
| 1186 | + $possibly_served_versions |
|
| 1187 | + )) { |
|
| 1188 | + // if a version is no longer supported, don't include it in index or list of versions served |
|
| 1189 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1190 | + } |
|
| 1191 | + } |
|
| 1192 | + return $versions_served; |
|
| 1193 | + } |
|
| 1194 | + |
|
| 1195 | + |
|
| 1196 | + /** |
|
| 1197 | + * Gets the major and minor version of EE core's version string |
|
| 1198 | + * |
|
| 1199 | + * @return string |
|
| 1200 | + */ |
|
| 1201 | + public static function core_version() |
|
| 1202 | + { |
|
| 1203 | + return apply_filters( |
|
| 1204 | + 'FHEE__EED_Core_REST_API__core_version', |
|
| 1205 | + implode( |
|
| 1206 | + '.', |
|
| 1207 | + array_slice( |
|
| 1208 | + explode( |
|
| 1209 | + '.', |
|
| 1210 | + espresso_version() |
|
| 1211 | + ), |
|
| 1212 | + 0, |
|
| 1213 | + 3 |
|
| 1214 | + ) |
|
| 1215 | + ) |
|
| 1216 | + ); |
|
| 1217 | + } |
|
| 1218 | + |
|
| 1219 | + |
|
| 1220 | + /** |
|
| 1221 | + * Gets the default limit that should be used when querying for resources |
|
| 1222 | + * |
|
| 1223 | + * @return int |
|
| 1224 | + */ |
|
| 1225 | + public static function get_default_query_limit() |
|
| 1226 | + { |
|
| 1227 | + // we actually don't use a const because we want folks to always use |
|
| 1228 | + // this method, not the const directly |
|
| 1229 | + return apply_filters( |
|
| 1230 | + 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
| 1231 | + 50 |
|
| 1232 | + ); |
|
| 1233 | + } |
|
| 1234 | + |
|
| 1235 | + |
|
| 1236 | + /** |
|
| 1237 | + * |
|
| 1238 | + * @param string $version api version string (i.e. '4.8.36') |
|
| 1239 | + * @return array |
|
| 1240 | + */ |
|
| 1241 | + public static function getCollectionRoutesIndexedByModelName($version = '') |
|
| 1242 | + { |
|
| 1243 | + $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
| 1244 | + $model_names = self::model_names_with_plural_routes($version); |
|
| 1245 | + $collection_routes = array(); |
|
| 1246 | + foreach ($model_names as $model_name => $model_class_name) { |
|
| 1247 | + $collection_routes[ strtolower($model_name) ] = '/' . self::ee_api_namespace . $version . '/' |
|
| 1248 | + . EEH_Inflector::pluralize_and_lower($model_name); |
|
| 1249 | + } |
|
| 1250 | + return $collection_routes; |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + |
|
| 1254 | + /** |
|
| 1255 | + * Returns an array of primary key names indexed by model names. |
|
| 1256 | + * @param string $version |
|
| 1257 | + * @return array |
|
| 1258 | + */ |
|
| 1259 | + public static function getPrimaryKeyNamesIndexedByModelName($version = '') |
|
| 1260 | + { |
|
| 1261 | + $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
| 1262 | + $model_names = self::model_names_with_plural_routes($version); |
|
| 1263 | + $primary_key_items = array(); |
|
| 1264 | + foreach ($model_names as $model_name => $model_class_name) { |
|
| 1265 | + $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
|
| 1266 | + foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
|
| 1267 | + if (count($primary_keys) > 1) { |
|
| 1268 | + $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
| 1269 | + } else { |
|
| 1270 | + $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
| 1271 | + } |
|
| 1272 | + } |
|
| 1273 | + } |
|
| 1274 | + return $primary_key_items; |
|
| 1275 | + } |
|
| 1276 | + |
|
| 1277 | + |
|
| 1278 | + |
|
| 1279 | + /** |
|
| 1280 | + * run - initial module setup |
|
| 1281 | + * |
|
| 1282 | + * @access public |
|
| 1283 | + * @param WP $WP |
|
| 1284 | + * @return void |
|
| 1285 | + */ |
|
| 1286 | + public function run($WP) |
|
| 1287 | + { |
|
| 1288 | + } |
|
| 1289 | 1289 | } |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | protected static function _set_hooks_for_changes() |
| 124 | 124 | { |
| 125 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
| 125 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES.'rest_api'.DS.'changes'), false); |
|
| 126 | 126 | foreach ($folder_contents as $classname_in_namespace => $filepath) { |
| 127 | 127 | // ignore the base parent class |
| 128 | 128 | // and legacy named classes |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | ) { |
| 132 | 132 | continue; |
| 133 | 133 | } |
| 134 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 134 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\'.$classname_in_namespace; |
|
| 135 | 135 | if (class_exists($full_classname)) { |
| 136 | 136 | $instance_of_class = new $full_classname; |
| 137 | 137 | if ($instance_of_class instanceof ChangesInBase) { |
@@ -176,10 +176,10 @@ discard block |
||
| 176 | 176 | * } |
| 177 | 177 | */ |
| 178 | 178 | // skip route options |
| 179 | - if (! is_numeric($endpoint_key)) { |
|
| 179 | + if ( ! is_numeric($endpoint_key)) { |
|
| 180 | 180 | continue; |
| 181 | 181 | } |
| 182 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 182 | + if ( ! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 183 | 183 | throw new EE_Error( |
| 184 | 184 | esc_html__( |
| 185 | 185 | // @codingStandardsIgnoreStart |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | } |
| 201 | 201 | if (isset($data_for_single_endpoint['callback_args'])) { |
| 202 | 202 | $callback_args = $data_for_single_endpoint['callback_args']; |
| 203 | - $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
| 203 | + $single_endpoint_args['callback'] = function(\WP_REST_Request $request) use ( |
|
| 204 | 204 | $callback, |
| 205 | 205 | $callback_args |
| 206 | 206 | ) { |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | $schema_route_data = $data_for_multiple_endpoints['schema']; |
| 220 | 220 | $schema_callback = $schema_route_data['schema_callback']; |
| 221 | 221 | $callback_args = $schema_route_data['callback_args']; |
| 222 | - $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
| 222 | + $multiple_endpoint_args['schema'] = function() use ($schema_callback, $callback_args) { |
|
| 223 | 223 | return call_user_func_array( |
| 224 | 224 | $schema_callback, |
| 225 | 225 | $callback_args |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | { |
| 262 | 262 | // delete the saved EE REST API routes |
| 263 | 263 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
| 264 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 264 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names.$version); |
|
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | { |
| 281 | 281 | $ee_routes = array(); |
| 282 | 282 | foreach (self::versions_served() as $version => $hidden_endpoints) { |
| 283 | - $ee_routes[ self::ee_api_namespace . $version ] = self::_get_ee_route_data_for_version( |
|
| 283 | + $ee_routes[self::ee_api_namespace.$version] = self::_get_ee_route_data_for_version( |
|
| 284 | 284 | $version, |
| 285 | 285 | $hidden_endpoints |
| 286 | 286 | ); |
@@ -300,8 +300,8 @@ discard block |
||
| 300 | 300 | */ |
| 301 | 301 | protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
| 302 | 302 | { |
| 303 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
| 304 | - if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
| 303 | + $ee_routes = get_option(self::saved_routes_option_names.$version, null); |
|
| 304 | + if ( ! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
| 305 | 305 | $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
| 306 | 306 | } |
| 307 | 307 | return $ee_routes; |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
| 329 | 329 | ) |
| 330 | 330 | ); |
| 331 | - $option_name = self::saved_routes_option_names . $version; |
|
| 331 | + $option_name = self::saved_routes_option_names.$version; |
|
| 332 | 332 | if (get_option($option_name)) { |
| 333 | 333 | update_option($option_name, $routes, true); |
| 334 | 334 | } else { |
@@ -373,8 +373,8 @@ discard block |
||
| 373 | 373 | { |
| 374 | 374 | $model_routes = array(); |
| 375 | 375 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
| 376 | - $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 377 | - . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 376 | + $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
| 377 | + . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 378 | 378 | } |
| 379 | 379 | return $model_routes; |
| 380 | 380 | } |
@@ -443,13 +443,13 @@ discard block |
||
| 443 | 443 | foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
| 444 | 444 | $model = \EE_Registry::instance()->load_model($model_name); |
| 445 | 445 | // if this isn't a valid model then let's skip iterate to the next item in the loop. |
| 446 | - if (! $model instanceof EEM_Base) { |
|
| 446 | + if ( ! $model instanceof EEM_Base) { |
|
| 447 | 447 | continue; |
| 448 | 448 | } |
| 449 | 449 | // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
| 450 | 450 | $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
| 451 | 451 | $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
| 452 | - $model_routes[ $plural_model_route ] = array( |
|
| 452 | + $model_routes[$plural_model_route] = array( |
|
| 453 | 453 | array( |
| 454 | 454 | 'callback' => array( |
| 455 | 455 | 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
@@ -460,7 +460,7 @@ discard block |
||
| 460 | 460 | 'hidden_endpoint' => $hidden_endpoint, |
| 461 | 461 | 'args' => $this->_get_read_query_params($model, $version), |
| 462 | 462 | '_links' => array( |
| 463 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 463 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace.$version.$singular_model_route), |
|
| 464 | 464 | ), |
| 465 | 465 | ), |
| 466 | 466 | 'schema' => array( |
@@ -471,7 +471,7 @@ discard block |
||
| 471 | 471 | 'callback_args' => array($version, $model_name), |
| 472 | 472 | ), |
| 473 | 473 | ); |
| 474 | - $model_routes[ $singular_model_route ] = array( |
|
| 474 | + $model_routes[$singular_model_route] = array( |
|
| 475 | 475 | array( |
| 476 | 476 | 'callback' => array( |
| 477 | 477 | 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
@@ -488,7 +488,7 @@ discard block |
||
| 488 | 488 | EED_Core_Rest_Api::should_have_write_endpoints($model), |
| 489 | 489 | $model |
| 490 | 490 | )) { |
| 491 | - $model_routes[ $plural_model_route ][] = array( |
|
| 491 | + $model_routes[$plural_model_route][] = array( |
|
| 492 | 492 | 'callback' => array( |
| 493 | 493 | 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
| 494 | 494 | 'handleRequestInsert', |
@@ -498,8 +498,8 @@ discard block |
||
| 498 | 498 | 'hidden_endpoint' => $hidden_endpoint, |
| 499 | 499 | 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
| 500 | 500 | ); |
| 501 | - $model_routes[ $singular_model_route ] = array_merge( |
|
| 502 | - $model_routes[ $singular_model_route ], |
|
| 501 | + $model_routes[$singular_model_route] = array_merge( |
|
| 502 | + $model_routes[$singular_model_route], |
|
| 503 | 503 | array( |
| 504 | 504 | array( |
| 505 | 505 | 'callback' => array( |
@@ -542,7 +542,7 @@ discard block |
||
| 542 | 542 | 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
| 543 | 543 | ), |
| 544 | 544 | ); |
| 545 | - $model_routes[ $related_route ] = $endpoints; |
|
| 545 | + $model_routes[$related_route] = $endpoints; |
|
| 546 | 546 | } |
| 547 | 547 | } |
| 548 | 548 | return $model_routes; |
@@ -574,7 +574,7 @@ discard block |
||
| 574 | 574 | */ |
| 575 | 575 | public static function get_entity_route($model, $id) |
| 576 | 576 | { |
| 577 | - return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 577 | + return EED_Core_Rest_Api::get_collection_route($model).'/'.$id; |
|
| 578 | 578 | } |
| 579 | 579 | |
| 580 | 580 | |
@@ -594,7 +594,7 @@ discard block |
||
| 594 | 594 | $relation_obj->get_other_model()->get_this_model_name(), |
| 595 | 595 | $relation_obj |
| 596 | 596 | ); |
| 597 | - return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 597 | + return EED_Core_Rest_Api::get_entity_route($model, $id).'/'.$related_model_name_endpoint_part; |
|
| 598 | 598 | } |
| 599 | 599 | |
| 600 | 600 | |
@@ -608,7 +608,7 @@ discard block |
||
| 608 | 608 | */ |
| 609 | 609 | public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
| 610 | 610 | { |
| 611 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 611 | + return '/'.EED_Core_Rest_Api::ee_api_namespace.$version.'/'.$relative_route; |
|
| 612 | 612 | } |
| 613 | 613 | |
| 614 | 614 | |
@@ -622,7 +622,7 @@ discard block |
||
| 622 | 622 | { |
| 623 | 623 | $routes = array(); |
| 624 | 624 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
| 625 | - $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 625 | + $routes[self::ee_api_namespace.$version] = $this->_get_rpc_route_data_for_version( |
|
| 626 | 626 | $version, |
| 627 | 627 | $hidden_endpoint |
| 628 | 628 | ); |
@@ -748,7 +748,7 @@ discard block |
||
| 748 | 748 | { |
| 749 | 749 | $default_orderby = array(); |
| 750 | 750 | foreach ($model->get_combined_primary_key_fields() as $key_field) { |
| 751 | - $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 751 | + $default_orderby[$key_field->get_name()] = 'ASC'; |
|
| 752 | 752 | } |
| 753 | 753 | return array_merge( |
| 754 | 754 | $this->_get_response_selection_query_params($model, $version), |
@@ -782,7 +782,7 @@ discard block |
||
| 782 | 782 | 'type' => array( |
| 783 | 783 | 'object', |
| 784 | 784 | 'string', |
| 785 | - ),// because we accept a variety of types, WP core validation and sanitization |
|
| 785 | + ), // because we accept a variety of types, WP core validation and sanitization |
|
| 786 | 786 | // freaks out. We'll just validate this argument while handling the request |
| 787 | 787 | 'validate_callback' => null, |
| 788 | 788 | 'sanitize_callback' => null, |
@@ -880,7 +880,7 @@ discard block |
||
| 880 | 880 | $sanitize_callback = null; |
| 881 | 881 | } |
| 882 | 882 | $arg_info['sanitize_callback'] = $sanitize_callback; |
| 883 | - $args_info[ $field_name ] = $arg_info; |
|
| 883 | + $args_info[$field_name] = $arg_info; |
|
| 884 | 884 | if ($field_obj instanceof EE_Datetime_Field) { |
| 885 | 885 | $gmt_arg_info = $arg_info; |
| 886 | 886 | $gmt_arg_info['description'] = sprintf( |
@@ -891,7 +891,7 @@ discard block |
||
| 891 | 891 | $field_obj->get_nicename(), |
| 892 | 892 | $field_name |
| 893 | 893 | ); |
| 894 | - $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 894 | + $args_info[$field_name.'_gmt'] = $gmt_arg_info; |
|
| 895 | 895 | } |
| 896 | 896 | } |
| 897 | 897 | return $args_info; |
@@ -914,16 +914,16 @@ discard block |
||
| 914 | 914 | public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
| 915 | 915 | { |
| 916 | 916 | $attributes = $request->get_attributes(); |
| 917 | - if (! isset($attributes['args'][ $param ]) |
|
| 918 | - || ! is_array($attributes['args'][ $param ])) { |
|
| 917 | + if ( ! isset($attributes['args'][$param]) |
|
| 918 | + || ! is_array($attributes['args'][$param])) { |
|
| 919 | 919 | $validation_result = true; |
| 920 | 920 | } else { |
| 921 | - $args = $attributes['args'][ $param ]; |
|
| 921 | + $args = $attributes['args'][$param]; |
|
| 922 | 922 | if (( |
| 923 | 923 | $value === '' |
| 924 | 924 | || $value === null |
| 925 | 925 | ) |
| 926 | - && (! isset($args['required']) |
|
| 926 | + && ( ! isset($args['required']) |
|
| 927 | 927 | || $args['required'] === false |
| 928 | 928 | ) |
| 929 | 929 | ) { |
@@ -933,7 +933,7 @@ discard block |
||
| 933 | 933 | && $args['format'] === 'email' |
| 934 | 934 | ) { |
| 935 | 935 | $validation_result = true; |
| 936 | - if (! self::_validate_email($value)) { |
|
| 936 | + if ( ! self::_validate_email($value)) { |
|
| 937 | 937 | $validation_result = new WP_Error( |
| 938 | 938 | 'rest_invalid_param', |
| 939 | 939 | esc_html__( |
@@ -983,7 +983,7 @@ discard block |
||
| 983 | 983 | { |
| 984 | 984 | $config_routes = array(); |
| 985 | 985 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
| 986 | - $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 986 | + $config_routes[self::ee_api_namespace.$version] = $this->_get_config_route_data_for_version( |
|
| 987 | 987 | $version, |
| 988 | 988 | $hidden_endpoint |
| 989 | 989 | ); |
@@ -1038,7 +1038,7 @@ discard block |
||
| 1038 | 1038 | { |
| 1039 | 1039 | $meta_routes = array(); |
| 1040 | 1040 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
| 1041 | - $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1041 | + $meta_routes[self::ee_api_namespace.$version] = $this->_get_meta_route_data_for_version( |
|
| 1042 | 1042 | $version, |
| 1043 | 1043 | $hidden_endpoint |
| 1044 | 1044 | ); |
@@ -1090,7 +1090,7 @@ discard block |
||
| 1090 | 1090 | foreach ($relative_urls as $resource_name => $endpoints) { |
| 1091 | 1091 | foreach ($endpoints as $key => $endpoint) { |
| 1092 | 1092 | // skip schema and other route options |
| 1093 | - if (! is_numeric($key)) { |
|
| 1093 | + if ( ! is_numeric($key)) { |
|
| 1094 | 1094 | continue; |
| 1095 | 1095 | } |
| 1096 | 1096 | // by default, hide "hidden_endpoint"s, unless the request indicates |
@@ -1099,9 +1099,9 @@ discard block |
||
| 1099 | 1099 | if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
| 1100 | 1100 | || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
| 1101 | 1101 | ) { |
| 1102 | - $full_route = '/' . ltrim($namespace, '/'); |
|
| 1103 | - $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1104 | - unset($route_data[ $full_route ]); |
|
| 1102 | + $full_route = '/'.ltrim($namespace, '/'); |
|
| 1103 | + $full_route .= '/'.ltrim($resource_name, '/'); |
|
| 1104 | + unset($route_data[$full_route]); |
|
| 1105 | 1105 | } |
| 1106 | 1106 | } |
| 1107 | 1107 | } |
@@ -1174,19 +1174,19 @@ discard block |
||
| 1174 | 1174 | // if it's not above the current core version, and it's compatible with the current version of core |
| 1175 | 1175 | if ($key_versioned_endpoint === $latest_version) { |
| 1176 | 1176 | // don't hide the latest version in the index |
| 1177 | - $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1177 | + $versions_served[$key_versioned_endpoint] = false; |
|
| 1178 | 1178 | } elseif ($key_versioned_endpoint >= $lowest_compatible_version |
| 1179 | 1179 | && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
| 1180 | 1180 | ) { |
| 1181 | 1181 | // include, but hide, previous versions which are still supported |
| 1182 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1182 | + $versions_served[$key_versioned_endpoint] = true; |
|
| 1183 | 1183 | } elseif (apply_filters( |
| 1184 | 1184 | 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
| 1185 | 1185 | false, |
| 1186 | 1186 | $possibly_served_versions |
| 1187 | 1187 | )) { |
| 1188 | 1188 | // if a version is no longer supported, don't include it in index or list of versions served |
| 1189 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1189 | + $versions_served[$key_versioned_endpoint] = true; |
|
| 1190 | 1190 | } |
| 1191 | 1191 | } |
| 1192 | 1192 | return $versions_served; |
@@ -1244,7 +1244,7 @@ discard block |
||
| 1244 | 1244 | $model_names = self::model_names_with_plural_routes($version); |
| 1245 | 1245 | $collection_routes = array(); |
| 1246 | 1246 | foreach ($model_names as $model_name => $model_class_name) { |
| 1247 | - $collection_routes[ strtolower($model_name) ] = '/' . self::ee_api_namespace . $version . '/' |
|
| 1247 | + $collection_routes[strtolower($model_name)] = '/'.self::ee_api_namespace.$version.'/' |
|
| 1248 | 1248 | . EEH_Inflector::pluralize_and_lower($model_name); |
| 1249 | 1249 | } |
| 1250 | 1250 | return $collection_routes; |
@@ -1265,9 +1265,9 @@ discard block |
||
| 1265 | 1265 | $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
| 1266 | 1266 | foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
| 1267 | 1267 | if (count($primary_keys) > 1) { |
| 1268 | - $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
| 1268 | + $primary_key_items[strtolower($model_name)][] = $primary_key_name; |
|
| 1269 | 1269 | } else { |
| 1270 | - $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
| 1270 | + $primary_key_items[strtolower($model_name)] = $primary_key_name; |
|
| 1271 | 1271 | } |
| 1272 | 1272 | } |
| 1273 | 1273 | } |