Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 23 | class Registry |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var EE_Template_Config $template_config |
||
| 28 | */ |
||
| 29 | protected $template_config; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var EE_Currency_Config $currency_config |
||
| 33 | */ |
||
| 34 | protected $currency_config; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $jsdata = array(); |
||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Registry constructor. |
||
| 47 | * Hooking into WP actions for script registry. |
||
| 48 | * |
||
| 49 | * @param EE_Template_Config $template_config |
||
| 50 | * @param EE_Currency_Config $currency_config |
||
| 51 | */ |
||
| 52 | public function __construct(EE_Template_Config $template_config, EE_Currency_Config $currency_config) |
||
| 53 | { |
||
| 54 | $this->template_config = $template_config; |
||
| 55 | $this->currency_config = $currency_config; |
||
| 56 | add_action('wp_enqueue_scripts', array($this, 'scripts'), 1); |
||
| 57 | add_action('admin_enqueue_scripts', array($this, 'scripts'), 1); |
||
| 58 | add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
| 59 | add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * Callback for the WP script actions. |
||
| 66 | * Used to register globally accessible core scripts. |
||
| 67 | * Also used to add the eejs.data object to the source for any js having eejs-core as a dependency. |
||
| 68 | */ |
||
| 69 | public function scripts() |
||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Call back for the script print in frontend and backend. |
||
| 105 | * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
||
| 106 | * |
||
| 107 | * @since 4.9.31.rc.015 |
||
| 108 | */ |
||
| 109 | public function enqueueData() |
||
| 110 | { |
||
| 111 | wp_localize_script('eejs-core', 'eejs', array('data' => $this->jsdata)); |
||
| 112 | wp_localize_script('espresso_core', 'eei18n', EE_Registry::$i18n_js_strings); |
||
| 113 | $this->localizeAccountingJs(); |
||
| 114 | } |
||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Used to add data to eejs.data object. |
||
| 120 | * Note: Overriding existing data is not allowed. |
||
| 121 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
| 122 | * If the data you add is something like this: |
||
| 123 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
| 124 | * It will be exposed in the page source as: |
||
| 125 | * eejs.data.my_plugin_data.foo == gar |
||
| 126 | * |
||
| 127 | * @param string $key Key used to access your data |
||
| 128 | * @param string|array $value Value to attach to key |
||
| 129 | * @throws InvalidArgumentException |
||
| 130 | */ |
||
| 131 | public function addData($key, $value) |
||
| 132 | { |
||
| 133 | if ($this->verifyDataNotExisting($key)) { |
||
| 134 | $this->jsdata[$key] = $value; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
| 142 | * elements in an array. |
||
| 143 | * When you use this method, the value you include will be appended to the end of an array on $key. |
||
| 144 | * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
||
| 145 | * object like this, eejs.data.test = [ my_data, |
||
| 146 | * ] |
||
| 147 | * If there has already been a scalar value attached to the data object given key, then |
||
| 148 | * this will throw an exception. |
||
| 149 | * |
||
| 150 | * @param string $key Key to attach data to. |
||
| 151 | * @param string|array $value Value being registered. |
||
| 152 | * @throws InvalidArgumentException |
||
| 153 | */ |
||
| 154 | public function pushData($key, $value) |
||
| 155 | { |
||
| 156 | View Code Duplication | if (isset($this->jsdata[$key]) |
|
| 157 | && ! is_array($this->jsdata[$key]) |
||
| 158 | ) { |
||
| 159 | throw new invalidArgumentException( |
||
| 160 | sprintf( |
||
| 161 | __( |
||
| 162 | 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
||
| 163 | push values to this data element when it is an array.', |
||
| 164 | 'event_espresso' |
||
| 165 | ), |
||
| 166 | $key, |
||
| 167 | __METHOD__ |
||
| 168 | ) |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | $this->jsdata[$key][] = $value; |
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * Used to set content used by javascript for a template. |
||
| 178 | * Note: Overrides of existing registered templates are not allowed. |
||
| 179 | * |
||
| 180 | * @param string $template_reference |
||
| 181 | * @param string $template_content |
||
| 182 | * @throws InvalidArgumentException |
||
| 183 | */ |
||
| 184 | public function addTemplate($template_reference, $template_content) |
||
| 185 | { |
||
| 186 | if (! isset($this->jsdata['templates'])) { |
||
| 187 | $this->jsdata['templates'] = array(); |
||
| 188 | } |
||
| 189 | //no overrides allowed. |
||
| 190 | if (isset($this->jsdata['templates'][$template_reference])) { |
||
| 191 | throw new invalidArgumentException( |
||
| 192 | sprintf( |
||
| 193 | __( |
||
| 194 | 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
||
| 195 | 'event_espresso' |
||
| 196 | ), |
||
| 197 | $template_reference |
||
| 198 | ) |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | $this->jsdata['templates'][$template_reference] = $template_content; |
||
| 202 | } |
||
| 203 | |||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * Retrieve the template content already registered for the given reference. |
||
| 208 | * |
||
| 209 | * @param string $template_reference |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getTemplate($template_reference) |
||
| 213 | { |
||
| 214 | return isset($this->jsdata['templates'], $this->jsdata['templates'][$template_reference]) |
||
| 215 | ? $this->jsdata['templates'][$template_reference] |
||
| 216 | : ''; |
||
| 217 | } |
||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Retrieve registered data. |
||
| 223 | * |
||
| 224 | * @param string $key Name of key to attach data to. |
||
| 225 | * @return mixed If there is no for the given key, then false is returned. |
||
| 226 | */ |
||
| 227 | public function getData($key) |
||
| 228 | { |
||
| 229 | return isset($this->jsdata[$key]) |
||
| 230 | ? $this->jsdata[$key] |
||
| 231 | : false; |
||
| 232 | } |
||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Verifies whether the given data exists already on the jsdata array. |
||
| 238 | * Overriding data is not allowed. |
||
| 239 | * |
||
| 240 | * @param string $key Index for data. |
||
| 241 | * @return bool If valid then return true. |
||
| 242 | * @throws InvalidArgumentException if data already exists. |
||
| 243 | */ |
||
| 244 | protected function verifyDataNotExisting($key) |
||
| 245 | { |
||
| 246 | if (isset($this->jsdata[$key])) { |
||
| 247 | View Code Duplication | if (is_array($this->jsdata[$key])) { |
|
| 248 | throw new InvalidArgumentException( |
||
| 249 | sprintf( |
||
| 250 | __( |
||
| 251 | 'The value for %1$s already exists in the Registry::eejs object. |
||
| 252 | Overrides are not allowed. Since the value of this data is an array, you may want to use the |
||
| 253 | %2$s method to push your value to the array.', |
||
| 254 | 'event_espresso' |
||
| 255 | ), |
||
| 256 | $key, |
||
| 257 | 'pushData()' |
||
| 258 | ) |
||
| 259 | ); |
||
| 260 | } |
||
| 261 | throw new InvalidArgumentException( |
||
| 262 | sprintf( |
||
| 263 | __( |
||
| 264 | 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
||
| 265 | allowed. Consider attaching your value to a different key', |
||
| 266 | 'event_espresso' |
||
| 267 | ), |
||
| 268 | $key |
||
| 269 | ) |
||
| 270 | ); |
||
| 271 | } |
||
| 272 | return true; |
||
| 273 | } |
||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * registers core default stylesheets |
||
| 279 | */ |
||
| 280 | private function loadCoreCss() |
||
| 281 | { |
||
| 282 | if ($this->template_config->enable_default_style) { |
||
| 283 | $default_stylesheet_path = is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css') |
||
| 284 | ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css' |
||
| 285 | : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css'; |
||
| 286 | wp_register_style( |
||
| 287 | 'espresso_default', |
||
| 288 | $default_stylesheet_path, |
||
| 289 | array('dashicons'), |
||
| 290 | EVENT_ESPRESSO_VERSION |
||
| 291 | ); |
||
| 292 | //Load custom style sheet if available |
||
| 293 | if ($this->template_config->custom_style_sheet !== null) { |
||
| 294 | wp_register_style( |
||
| 295 | 'espresso_custom_css', |
||
| 296 | EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet, |
||
| 297 | array('espresso_default'), |
||
| 298 | EVENT_ESPRESSO_VERSION |
||
| 299 | ); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * registers core default javascript |
||
| 308 | */ |
||
| 309 | private function loadCoreJs() |
||
| 310 | { |
||
| 311 | // load core js |
||
| 312 | wp_register_script( |
||
| 313 | 'espresso_core', |
||
| 314 | EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
||
| 315 | array('jquery'), |
||
| 316 | EVENT_ESPRESSO_VERSION, |
||
| 317 | true |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * registers jQuery Validate for form validation |
||
| 325 | */ |
||
| 326 | private function loadJqueryValidate() |
||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * registers accounting.js for performing client-side calculations |
||
| 349 | */ |
||
| 350 | private function loadAccountingJs() |
||
| 369 | |||
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * registers accounting.js for performing client-side calculations |
||
| 374 | */ |
||
| 375 | private function localizeAccountingJs() |
||
| 400 | |||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * registers assets for cleaning your ears |
||
| 405 | */ |
||
| 406 | private function loadQtipJs() |
||
| 414 | |||
| 415 | |||
| 416 | |||
| 417 | |||
| 418 | } |
||
| 419 |