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 | class Xcloner_Admin { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The ID of this plugin. |
||
| 27 | * |
||
| 28 | * @since 1.0.0 |
||
| 29 | * @access private |
||
| 30 | * @var string $plugin_name The ID of this plugin. |
||
| 31 | */ |
||
| 32 | private $plugin_name; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The version of this plugin. |
||
| 36 | * |
||
| 37 | * @since 1.0.0 |
||
| 38 | * @access private |
||
| 39 | * @var string $version The current version of this plugin. |
||
| 40 | */ |
||
| 41 | private $version; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Xcloner |
||
| 45 | */ |
||
| 46 | private $xcloner_container; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Initialize the class and set its properties. |
||
| 50 | * |
||
| 51 | * Xcloner_Admin constructor. |
||
| 52 | * @param Xcloner $xcloner_container |
||
| 53 | */ |
||
| 54 | public function __construct(Xcloner $xcloner_container) { |
||
| 55 | |||
| 56 | $this->plugin_name = $xcloner_container->get_plugin_name(); |
||
| 57 | $this->version = $xcloner_container->get_version(); |
||
| 58 | $this->xcloner_container = $xcloner_container; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return Xcloner |
||
| 63 | */ |
||
| 64 | public function get_xcloner_container() { |
||
| 65 | return $this->xcloner_container; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Register the stylesheets for the admin area. |
||
| 70 | * |
||
| 71 | * @since 1.0.0 |
||
| 72 | */ |
||
| 73 | public function enqueue_styles($hook) { |
||
| 74 | |||
| 75 | if (!stristr($hook, "page_".$this->plugin_name) || (isset($_GET['option']) and $_GET['option'] == "com_cloner")) { |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * This function is provided for demonstration purposes only. |
||
| 81 | * |
||
| 82 | * An instance of this class should be passed to the run() function |
||
| 83 | * defined in Xcloner_Loader as all of the hooks are defined |
||
| 84 | * in that particular class. |
||
| 85 | * |
||
| 86 | * The Xcloner_Loader will then create the relationship |
||
| 87 | * between the defined hooks and the functions defined in this |
||
| 88 | * class. |
||
| 89 | */ |
||
| 90 | |||
| 91 | wp_enqueue_style($this->plugin_name."_materialize", plugin_dir_url(__FILE__).'css/materialize.min.css', array(), $this->version, 'all'); |
||
| 92 | wp_enqueue_style($this->plugin_name."_materialize.clockpicker", plugin_dir_url(__FILE__).'css/materialize.clockpicker.css', array(), $this->version, 'all'); |
||
| 93 | wp_enqueue_style($this->plugin_name."_materialize.icons", '//fonts.googleapis.com/icon?family=Material+Icons', array(), $this->version, 'all'); |
||
| 94 | wp_enqueue_style($this->plugin_name."_jquery.datatables", plugin_dir_url(__FILE__).'css/jquery.dataTables.min.css', array(), $this->version, 'all'); |
||
| 95 | wp_enqueue_style($this->plugin_name."_jquery.datatables.responsive", plugin_dir_url(__FILE__).'css/responsive.dataTables.css', array(), $this->version, 'all'); |
||
| 96 | wp_enqueue_style($this->plugin_name."_jstree", dirname(plugin_dir_url(__FILE__)).'/vendor/vakata/jstree/dist/themes/default/style.min.css', array(), '3.3', 'all'); |
||
| 97 | wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__).'css/xcloner-admin.css', array(), $this->version, 'all'); |
||
| 98 | |||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Register the JavaScript for the admin area. |
||
| 103 | * |
||
| 104 | * @since 1.0.0 |
||
| 105 | */ |
||
| 106 | public function enqueue_scripts($hook) { |
||
| 107 | |||
| 108 | if (!stristr($hook, "page_".$this->plugin_name)) { |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * This function is provided for demonstration purposes only. |
||
| 114 | * |
||
| 115 | * An instance of this class should be passed to the run() function |
||
| 116 | * defined in Xcloner_Loader as all of the hooks are defined |
||
| 117 | * in that particular class. |
||
| 118 | * |
||
| 119 | * The Xcloner_Loader will then create the relationship |
||
| 120 | * between the defined hooks and the functions defined in this |
||
| 121 | * class. |
||
| 122 | */ |
||
| 123 | |||
| 124 | add_thickbox(); |
||
| 125 | wp_enqueue_script('plugin-install'); |
||
| 126 | wp_enqueue_script('updates'); |
||
| 127 | wp_enqueue_script($this->plugin_name."_materialize", plugin_dir_url(__FILE__).'js/materialize.min.js', array('jquery'), $this->version, false); |
||
| 128 | wp_enqueue_script($this->plugin_name."_materialize.clockpicker", plugin_dir_url(__FILE__).'js/materialize.clockpicker.js', array('jquery'), $this->version, false); |
||
| 129 | wp_enqueue_script($this->plugin_name."_jquery.datatables", plugin_dir_url(__FILE__).'js/jquery.dataTables.min.js', array('jquery'), $this->version, false); |
||
| 130 | wp_enqueue_script($this->plugin_name."_jquery.datatables.respnsive", plugin_dir_url(__FILE__).'js/dataTables.responsive.js', array('jquery'), $this->version, false); |
||
| 131 | wp_enqueue_script($this->plugin_name."_vakata", dirname(plugin_dir_url(__FILE__)).'/vendor/vakata/jstree/dist/jstree.min.js', array('jquery'), '3.3', false); |
||
| 132 | wp_enqueue_script($this->plugin_name."_xcloner-backup-class", plugin_dir_url(__FILE__).'js/xcloner-backup-class.js', array('jquery'), $this->version, false); |
||
| 133 | wp_enqueue_script($this->plugin_name."_xcloner-scheduler-class", plugin_dir_url(__FILE__).'js/xcloner-scheduler-class.js', array('jquery'), $this->version, false); |
||
| 134 | wp_enqueue_script($this->plugin_name."_xcloner-restore-class", plugin_dir_url(__FILE__).'js/xcloner-restore-class.js', array('jquery'), $this->version, false); |
||
| 135 | wp_enqueue_script($this->plugin_name."_xcloner-manage-backups-class", plugin_dir_url(__FILE__).'js/xcloner-manage-backups-class.js', array('jquery'), $this->version, false); |
||
| 136 | wp_enqueue_script($this->plugin_name."_xcloner-remote-storage-class", plugin_dir_url(__FILE__).'js/xcloner-remote-storage-class.js', array('jquery'), $this->version, false); |
||
| 137 | wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__).'js/xcloner-admin.js', array('jquery'), $this->version, false); |
||
| 138 | |||
| 139 | |||
| 140 | } |
||
| 141 | |||
| 142 | public function xcloner_init_page() { |
||
| 143 | require_once("partials/xcloner_init_page.php"); |
||
| 144 | |||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the XCloner Storage Page |
||
| 149 | */ |
||
| 150 | public function xcloner_remote_storage_page() { |
||
| 151 | $xcloner_sanitization = $this->get_xcloner_container()->get_xcloner_sanitization(); |
||
| 152 | $remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage(); |
||
| 153 | |||
| 154 | |||
| 155 | if (isset($_POST['action'])) { |
||
| 156 | $_POST['action'] = $xcloner_sanitization->sanitize_input_as_string($_POST['action']); |
||
| 157 | $remote_storage->save($_POST['action']); |
||
| 158 | } |
||
| 159 | |||
| 160 | if (isset($_POST['authentification_code']) && $_POST['authentification_code'] != "") { |
||
| 161 | $_POST['authentification_code'] = $xcloner_sanitization->sanitize_input_as_string($_POST['authentification_code']); |
||
| 162 | |||
| 163 | $remote_storage->set_access_token($_POST['authentification_code']); |
||
| 164 | } |
||
| 165 | |||
| 166 | if (isset($_POST['connection_check']) && $_POST['connection_check']) { |
||
| 167 | $remote_storage->check($_POST['action']); |
||
| 168 | } |
||
| 169 | |||
| 170 | require_once("partials/xcloner_remote_storage_page.php"); |
||
| 171 | |||
| 172 | } |
||
| 173 | |||
| 174 | public function xcloner_scheduled_backups_page() { |
||
| 175 | $requirements = $this->xcloner_container->get_xcloner_requirements(); |
||
| 176 | |||
| 177 | if (!$requirements->check_backup_ready_status()) { |
||
| 178 | require_once("partials/xcloner_init_page.php"); |
||
| 179 | |||
| 180 | return false; |
||
| 181 | } |
||
| 182 | |||
| 183 | require_once("partials/xcloner_scheduled_backups_page.php"); |
||
| 184 | |||
| 185 | } |
||
| 186 | |||
| 187 | public function xcloner_manage_backups_page() { |
||
| 188 | require_once("partials/xcloner_manage_backups_page.php"); |
||
| 189 | |||
| 190 | } |
||
| 191 | |||
| 192 | public function xcloner_debugger_page() { |
||
| 194 | |||
| 195 | } |
||
| 196 | |||
| 197 | public function xcloner_restore_page() { |
||
| 198 | require_once("partials/xcloner_restore_page.php"); |
||
| 199 | |||
| 200 | } |
||
| 201 | |||
| 202 | public function xcloner_generate_backups_page() { |
||
| 214 | } |
||
| 215 | |||
| 216 | public function xcloner_settings_page() { |
||
| 217 | // check user capabilities |
||
| 291 | |||
| 292 | </div> |
||
| 293 | <?php |
||
| 294 | |||
| 295 | } |
||
| 298 |