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:
Complex classes like Core often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Core, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Core |
||
| 13 | { |
||
| 14 | private $plugin_dir; |
||
| 15 | private $last_author = false; |
||
| 16 | private $timeshift_cached_meta; |
||
| 17 | |||
| 18 | 12 | public function __construct($i18n) |
|
| 28 | |||
| 29 | 2 | public function hasTimeshifts($post_id) |
|
| 30 | { |
||
| 31 | 2 | $post_type = get_post_type($post_id); |
|
| 32 | 2 | $table_name = $this->wpdb->prefix . 'timeshift_' . $post_type; |
|
| 33 | 2 | $this->checkTable($post_type); |
|
| 34 | 2 | $sql = "select count(1) as amount from $table_name where post_id=" . $post_id; |
|
| 35 | 2 | $r = $this->wpdb->get_results($sql); |
|
| 36 | |||
| 37 | 2 | if ($r && count($r) == 1) { |
|
| 38 | 1 | if (intval($r[0]->amount) > 0) { |
|
| 39 | 1 | return true; |
|
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | 1 | return false; |
|
| 44 | } |
||
| 45 | |||
| 46 | 12 | public function timeshiftVisible() |
|
| 52 | |||
| 53 | 12 | public function add_metabox() |
|
| 66 | |||
| 67 | 2 | public function timeshift_metabox() |
|
| 68 | { |
||
| 69 | global $post; |
||
| 70 | 2 | if (! isset($_GET['post'])) { |
|
| 71 | 1 | return; |
|
| 72 | } |
||
| 73 | 1 | $prod_post = get_post($_GET['post']); |
|
| 74 | 1 | $table_name = $this->wpdb->prefix . 'timeshift_' . $post->post_type; |
|
| 75 | 1 | $sql = "select * from $table_name where post_id=" . $post->ID . ' order by create_date desc'; |
|
| 76 | |||
| 77 | 1 | $last_editor = get_post_meta($prod_post->ID, '_edit_last', true); |
|
| 78 | 1 | $row = $this->wpdb->get_results($sql); |
|
| 79 | 1 | echo '<table class="widefat fixed">'; |
|
| 80 | 1 | echo '<thead>'; |
|
| 81 | 1 | echo '<tr>'; |
|
| 82 | 1 | echo '<th width=30></th>'; |
|
| 83 | 1 | echo '<th width="40%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Title', 'kmm-timeshift') . '</th>'; |
|
| 84 | 1 | echo '<th width="30%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Snapshot Date', 'kmm-timeshift') . '</th>'; |
|
| 85 | 1 | echo '<th width="10%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Author', 'kmm-timeshift') . '</th>'; |
|
| 86 | 1 | echo '<th width="10%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Actions', 'kmm-timeshift') . '</th>'; |
|
| 87 | 1 | echo '</tr>'; |
|
| 88 | 1 | echo ' </thead>'; |
|
| 89 | 1 | echo '<tbody>'; |
|
| 90 | 1 | echo '<tr style="font-weight: 800;">'; |
|
| 91 | 1 | echo '<td>' . get_avatar($last_editor, 30) . '</td>'; |
|
| 92 | 1 | echo '<td>' . $prod_post->post_title . '</td>'; |
|
| 93 | 1 | echo '<td>' . $prod_post->post_date . '</td>'; |
|
| 94 | 1 | echo '<td>' . get_the_author_meta('display_name', $last_editor) . '</td>'; |
|
| 95 | 1 | echo "<td><a href='post.php?post=" . $_GET['post'] . "&action=edit'><span class='dashicons dashicons-admin-site'></span></A></td>"; |
|
| 96 | 1 | echo '</tr>'; |
|
| 97 | |||
| 98 | 1 | foreach ($row as $rev) { |
|
| 99 | $timeshift = unserialize($rev->post_payload); |
||
| 100 | $style = ''; |
||
| 101 | if (isset($_GET['timeshift']) && $_GET['timeshift'] == $rev->id) { |
||
| 102 | $style = 'style="font-style:italic;background-color: lightblue;"'; |
||
| 103 | } |
||
| 104 | echo '<tr ' . $style . '>'; |
||
| 105 | echo '<td>' . get_avatar($timeshift->meta['_edit_last'][0], 30) . '</td>'; |
||
| 106 | echo '<td>' . $timeshift->post->post_title . '</td>'; |
||
| 107 | echo '<td>' . $rev->create_date . '</td>'; |
||
| 108 | echo '<td>' . get_the_author_meta('display_name', $timeshift->meta['_edit_last'][0]) . '</td>'; |
||
| 109 | echo "<td><a href='post.php?post=" . $_GET['post'] . '&action=edit×hift=' . $rev->id . "'><span class='dashicons dashicons-backup'></span></a></td>"; |
||
| 110 | echo '</tr>'; |
||
| 111 | } |
||
| 112 | 1 | echo '</tbody>'; |
|
| 113 | 1 | echo '</table>'; |
|
| 114 | } |
||
| 115 | |||
| 116 | 12 | public function add_filters() |
|
| 124 | |||
| 125 | 1 | public function update_post_metadata($check, int $object_id, string $meta_key, $meta_value, $prev_value) |
|
| 126 | { |
||
| 127 | 1 | if ($meta_key == '_edit_last') { |
|
| 128 | 1 | $lo = get_post_meta($object_id, '_edit_last', true); |
|
| 129 | 1 | $this->last_author = $lo; |
|
| 130 | } |
||
| 131 | |||
| 132 | 1 | return null; |
|
| 133 | } |
||
| 134 | |||
| 135 | 3 | public function inject_metadata_timeshift($value, $post_id, $key, $single) |
|
| 136 | { |
||
| 137 | 3 | if (! isset($_GET['timeshift'])) { |
|
| 138 | 3 | return; |
|
| 139 | } |
||
| 140 | //Load timeshift |
||
| 141 | if (! $this->timeshift_cached_meta) { |
||
| 142 | $post_type = get_post_type($post_id); |
||
| 143 | $table_name = $this->wpdb->prefix . 'timeshift_' . $post_type; |
||
| 144 | $sql = "select * from $table_name where id=" . intval($_GET['timeshift']); |
||
| 145 | $r = $this->wpdb->get_results($sql); |
||
| 146 | View Code Duplication | if ($r && count($r) == 1) { |
|
| 147 | $payload = unserialize($r[0]->post_payload); |
||
| 148 | $this->timeshift_cached_meta = $payload->meta; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | if ($this->timeshift_cached_meta && isset($this->timeshift_cached_meta[$key])) { |
||
| 152 | return $this->timeshift_cached_meta[$key]; |
||
| 153 | } |
||
| 154 | if ($single) { |
||
| 155 | return null; |
||
| 156 | } |
||
| 157 | |||
| 158 | return []; |
||
| 159 | } |
||
| 160 | |||
| 161 | 2 | public function inject_timeshift($p) |
|
| 162 | { |
||
| 163 | global $post; |
||
| 164 | 2 | if (! isset($_GET['timeshift'])) { |
|
| 165 | 1 | return; |
|
| 166 | } |
||
| 167 | //Load timeshift |
||
| 168 | 1 | $table_name = $this->wpdb->prefix . 'timeshift_' . $post->post_type; |
|
| 169 | 1 | $sql = "select * from $table_name where id=" . intval($_GET['timeshift']); |
|
| 170 | 1 | $r = $this->wpdb->get_results($sql); |
|
| 171 | 1 | View Code Duplication | if ($r && count($r) == 1) { |
| 172 | $payload = unserialize($r[0]->post_payload); |
||
| 173 | $post = $payload->post; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | 12 | public function add_actions() |
|
| 190 | |||
| 191 | 4 | public function checkTable($postType) |
|
| 192 | { |
||
| 193 | 4 | $table_name = $this->wpdb->prefix . 'timeshift_' . $postType; |
|
| 194 | |||
| 195 | 4 | $charset_collate = $this->wpdb->get_charset_collate(); |
|
| 196 | |||
| 197 | 4 | $sql = "CREATE TABLE IF NOT EXISTS $table_name ( |
|
| 198 | id int(12) NOT NULL AUTO_INCREMENT, |
||
| 199 | post_id int(12) NOT NULL, |
||
| 200 | create_date datetime default CURRENT_TIMESTAMP, |
||
| 201 | post_payload TEXT |
||
| 202 | ,PRIMARY KEY (id) |
||
| 203 | 4 | ) $charset_collate;"; |
|
| 204 | |||
| 205 | 4 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
|
| 206 | 4 | $a = dbDelta($sql); |
|
| 207 | |||
| 208 | 4 | return true; |
|
| 209 | } |
||
| 210 | |||
| 211 | 2 | public function storeTimeshift($timeshift) |
|
| 212 | { |
||
| 213 | 2 | $table_name = $this->wpdb->prefix . 'timeshift_' . $timeshift->post->post_type; |
|
| 214 | 2 | $sql = "insert into $table_name (post_id, post_payload) VALUES(%d, '%s')"; |
|
| 215 | 2 | $query = $this->wpdb->prepare($sql, $timeshift->post->ID, serialize($timeshift)); |
|
| 216 | 2 | $this->wpdb->query($query); |
|
| 217 | } |
||
| 218 | |||
| 219 | public function create_snapshot($postID) |
||
| 223 | |||
| 224 | 1 | public function pre_post_update(int $post_ID, array $data = null) |
|
| 225 | { |
||
| 226 | 1 | if (wp_is_post_autosave($post_ID)) { |
|
| 227 | return; |
||
| 228 | } |
||
| 229 | 1 | if (get_post_status($post_ID) == 'auto-draft') { |
|
| 230 | return; |
||
| 246 | } |
||
| 247 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state