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 |
||
20 | abstract class EspressoShortcode implements ShortcodeInterface |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * transient prefix |
||
25 | * |
||
26 | * @type string |
||
27 | */ |
||
28 | const CACHE_TRANSIENT_PREFIX = 'ee_sc_'; |
||
29 | |||
30 | /** |
||
31 | * @var PostRelatedCacheManager $cache_manager |
||
32 | */ |
||
33 | private $cache_manager; |
||
34 | |||
35 | |||
36 | |||
37 | /** |
||
38 | * EspressoShortcode constructor |
||
39 | * |
||
40 | * @param PostRelatedCacheManager $cache_manager |
||
41 | */ |
||
42 | public function __construct(PostRelatedCacheManager $cache_manager) |
||
46 | |||
47 | |||
48 | |||
49 | /** |
||
50 | * enqueues scripts then processes the shortcode |
||
51 | * |
||
52 | * @param array $attributes |
||
53 | * @return string |
||
54 | * @throws \EE_Error |
||
55 | */ |
||
56 | final public function processShortcodeCallback($attributes = array()) |
||
69 | |||
70 | |||
71 | |||
72 | /** |
||
73 | * If shortcode caching is enabled for the shortcode, |
||
74 | * and cached results exist, then that will be returned |
||
75 | * else new content will be generated. |
||
76 | * If caching is enabled, then the new content will be cached for later. |
||
77 | * |
||
78 | * @param array $attributes |
||
79 | * @return mixed|string |
||
80 | * @throws \EE_Error |
||
81 | */ |
||
82 | private function shortcodeContent(array $attributes) |
||
106 | |||
107 | |||
108 | |||
109 | /** |
||
110 | * @return int |
||
111 | * @throws \EE_Error |
||
112 | */ |
||
113 | private function currentPostID() |
||
126 | |||
127 | |||
128 | |||
129 | /** |
||
130 | * @param int $post_ID |
||
131 | * @return string |
||
132 | * @throws \EE_Error |
||
133 | */ |
||
134 | private function shortcodeCacheID($post_ID) |
||
139 | |||
140 | |||
141 | |||
142 | /** |
||
143 | * array for defining custom attribute sanitization callbacks, |
||
144 | * where keys match keys in your attributes array, |
||
145 | * and values represent the sanitization function you wish to be applied to that attribute. |
||
146 | * So for example, if you had an integer attribute named "event_id" |
||
147 | * that you wanted to be sanitized using absint(), |
||
148 | * then you would return the following: |
||
149 | * array('event_id' => 'absint') |
||
150 | * Entering 'skip_sanitization' for the callback value |
||
151 | * means that no sanitization will be applied |
||
152 | * on the assumption that the attribute |
||
153 | * will be sanitized at some point... right? |
||
154 | * You wouldn't pass around unsanitized attributes would you? |
||
155 | * That would be very Tom Foolery of you!!! |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | protected function customAttributeSanitizationMap() |
||
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * Performs basic sanitization on shortcode attributes |
||
168 | * Since incoming attributes from the shortcode usage in the WP editor will all be strings, |
||
169 | * most attributes will by default be sanitized using the sanitize_text_field() function. |
||
170 | * This can be overridden using the customAttributeSanitizationMap() method (see above), |
||
171 | * all other attributes would be sanitized using the defaults in the switch statement below |
||
172 | * |
||
173 | * @param array $attributes |
||
174 | * @return array |
||
175 | */ |
||
176 | private function sanitizeAttributes(array $attributes) |
||
215 | |||
216 | |||
217 | |||
218 | } |
||
219 | // End of file EspressoShortcode.php |
||
220 | // Location: EventEspresso\core\services\shortcodes/EspressoShortcode.php |