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 |
||
| 32 | class ShortcodesManager |
||
| 33 | { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var LegacyShortcodesManager $legacy_shortcodes_manager |
||
| 37 | */ |
||
| 38 | private $legacy_shortcodes_manager; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ShortcodeInterface[] $shortcodes |
||
| 42 | */ |
||
| 43 | private $shortcodes; |
||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * ShortcodesManager constructor |
||
| 49 | * |
||
| 50 | * @param LegacyShortcodesManager $legacy_shortcodes_manager |
||
| 51 | */ |
||
| 52 | public function __construct(LegacyShortcodesManager $legacy_shortcodes_manager) { |
||
| 53 | $this->legacy_shortcodes_manager = $legacy_shortcodes_manager; |
||
| 54 | // assemble a list of installed and active shortcodes |
||
| 55 | add_action( |
||
| 56 | 'AHEE__EE_System__register_shortcodes_modules_and_widgets', |
||
| 57 | array($this, 'registerShortcodes'), |
||
| 58 | 999 |
||
| 59 | ); |
||
| 60 | // call add_shortcode() for all installed shortcodes |
||
| 61 | add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'addShortcodes')); |
||
| 62 | // check content for shortcodes the old way |
||
| 63 | add_action('parse_query', array($this->legacy_shortcodes_manager, 'initializeShortcodes'), 5); |
||
| 64 | // check content for shortcodes the NEW more efficient way |
||
| 65 | add_action('template_redirect', array($this, 'templateRedirect'), 999); |
||
| 66 | } |
||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * @return CollectionInterface|ShortcodeInterface[] |
||
| 72 | * @throws InvalidIdentifierException |
||
| 73 | * @throws InvalidInterfaceException |
||
| 74 | * @throws InvalidFilePathException |
||
| 75 | * @throws InvalidEntityException |
||
| 76 | * @throws InvalidDataTypeException |
||
| 77 | * @throws InvalidClassException |
||
| 78 | */ |
||
| 79 | public function getShortcodes() |
||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * @return CollectionInterface|ShortcodeInterface[] |
||
| 91 | * @throws InvalidIdentifierException |
||
| 92 | * @throws InvalidInterfaceException |
||
| 93 | * @throws InvalidFilePathException |
||
| 94 | * @throws InvalidEntityException |
||
| 95 | * @throws InvalidDataTypeException |
||
| 96 | * @throws InvalidClassException |
||
| 97 | */ |
||
| 98 | protected function loadShortcodesCollection() |
||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @return void |
||
| 124 | * @throws DomainException |
||
| 125 | * @throws InvalidInterfaceException |
||
| 126 | * @throws InvalidIdentifierException |
||
| 127 | * @throws InvalidFilePathException |
||
| 128 | * @throws InvalidEntityException |
||
| 129 | * @throws InvalidDataTypeException |
||
| 130 | * @throws InvalidClassException |
||
| 131 | */ |
||
| 132 | public function registerShortcodes() |
||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | public function addShortcodes() |
||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * callback for the "template_redirect" hook point |
||
| 188 | * checks posts for EE shortcodes, and initializes them, |
||
| 189 | * then toggles filter switch that loads core default assets |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function templateRedirect() |
||
| 194 | { |
||
| 195 | global $wp_query; |
||
| 196 | if (empty($wp_query->posts)) { |
||
| 197 | return; |
||
| 198 | } |
||
| 199 | $load_assets = false; |
||
| 200 | // array of posts displayed in current request |
||
| 201 | $posts = is_array($wp_query->posts) ? $wp_query->posts : array($wp_query->posts); |
||
| 202 | foreach ($posts as $post) { |
||
| 203 | // now check post content and excerpt for EE shortcodes |
||
| 204 | $load_assets = $this->parseContentForShortcodes($post->post_content) |
||
| 205 | ? true |
||
| 206 | : $load_assets; |
||
| 207 | } |
||
| 208 | View Code Duplication | if ($load_assets) { |
|
| 209 | $this->legacy_shortcodes_manager->registry()->REQ->set_espresso_page(true); |
||
| 210 | add_filter('FHEE_load_css', '__return_true'); |
||
| 211 | add_filter('FHEE_load_js', '__return_true'); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * checks supplied content against list of shortcodes, |
||
| 219 | * then initializes any found shortcodes, and returns true. |
||
| 220 | * returns false if no shortcodes found. |
||
| 221 | * |
||
| 222 | * @param string $content |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function parseContentForShortcodes($content) |
||
| 240 | |||
| 241 | } |
||
| 242 | // End of file ShortcodesManager.php |
||
| 244 |