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