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 |
||
| 21 | abstract class EspressoShortcode implements ShortcodeInterface |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * transient prefix |
||
| 26 | * |
||
| 27 | * @type string |
||
| 28 | */ |
||
| 29 | const CACHE_TRANSIENT_PREFIX = 'ee_sc_'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var PostRelatedCacheManager $cache_manager |
||
| 33 | */ |
||
| 34 | private $cache_manager; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * true if ShortcodeInterface::initializeShortcode() has been called |
||
| 38 | * if false, then that will get called before processing |
||
| 39 | * |
||
| 40 | * @var boolean $initialized |
||
| 41 | */ |
||
| 42 | private $initialized = false; |
||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * EspressoShortcode constructor |
||
| 48 | * |
||
| 49 | * @param PostRelatedCacheManager $cache_manager |
||
| 50 | */ |
||
| 51 | public function __construct(PostRelatedCacheManager $cache_manager) |
||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | public function shortcodeHasBeenInitialized() |
||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * enqueues scripts then processes the shortcode |
||
| 70 | * |
||
| 71 | * @param array $attributes |
||
| 72 | * @return string |
||
| 73 | * @throws EE_Error |
||
| 74 | */ |
||
| 75 | final public function processShortcodeCallback($attributes = array()) |
||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * If shortcode caching is enabled for the shortcode, |
||
| 93 | * and cached results exist, then that will be returned |
||
| 94 | * else new content will be generated. |
||
| 95 | * If caching is enabled, then the new content will be cached for later. |
||
| 96 | * |
||
| 97 | * @param array $attributes |
||
| 98 | * @return mixed|string |
||
| 99 | * @throws EE_Error |
||
| 100 | */ |
||
| 101 | private function shortcodeContent(array $attributes) |
||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * @return int |
||
| 133 | * @throws EE_Error |
||
| 134 | */ |
||
| 135 | private function currentPostID() |
||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * @param int $post_ID |
||
| 153 | * @return string |
||
| 154 | * @throws EE_Error |
||
| 155 | */ |
||
| 156 | private function shortcodeCacheID($post_ID) |
||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * array for defining custom attribute sanitization callbacks, |
||
| 166 | * where keys match keys in your attributes array, |
||
| 167 | * and values represent the sanitization function you wish to be applied to that attribute. |
||
| 168 | * So for example, if you had an integer attribute named "event_id" |
||
| 169 | * that you wanted to be sanitized using absint(), |
||
| 170 | * then you would return the following: |
||
| 171 | * array('event_id' => 'absint') |
||
| 172 | * Entering 'skip_sanitization' for the callback value |
||
| 173 | * means that no sanitization will be applied |
||
| 174 | * on the assumption that the attribute |
||
| 175 | * will be sanitized at some point... right? |
||
| 176 | * You wouldn't pass around unsanitized attributes would you? |
||
| 177 | * That would be very Tom Foolery of you!!! |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | protected function customAttributeSanitizationMap() |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * Performs basic sanitization on shortcode attributes |
||
| 190 | * Since incoming attributes from the shortcode usage in the WP editor will all be strings, |
||
| 191 | * most attributes will by default be sanitized using the sanitize_text_field() function. |
||
| 192 | * This can be overridden using the customAttributeSanitizationMap() method (see above), |
||
| 193 | * all other attributes would be sanitized using the defaults in the switch statement below |
||
| 194 | * |
||
| 195 | * @param array $attributes |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | private function sanitizeAttributes(array $attributes) |
||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | } |
||
| 241 | // End of file EspressoShortcode.php |
||
| 242 | // Location: EventEspresso\core\services\shortcodes/EspressoShortcode.php |