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 |
||
| 30 | class Xcloner { |
||
|
|
|||
| 31 | |||
| 32 | /** |
||
| 33 | * The loader that's responsible for maintaining and registering all hooks that power |
||
| 34 | * the plugin. |
||
| 35 | * |
||
| 36 | * @since 1.0.0 |
||
| 37 | * @access protected |
||
| 38 | * @var Xcloner_Loader $loader Maintains and registers all hooks for the plugin. |
||
| 39 | */ |
||
| 40 | protected $loader; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The unique identifier of this plugin. |
||
| 44 | * |
||
| 45 | * @since 1.0.0 |
||
| 46 | * @access protected |
||
| 47 | * @var string $plugin_name The string used to uniquely identify this plugin. |
||
| 48 | */ |
||
| 49 | protected $plugin_name; |
||
| 50 | |||
| 51 | protected $plugin_admin; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The current version of the plugin. |
||
| 55 | * |
||
| 56 | * @since 1.0.0 |
||
| 57 | * @access protected |
||
| 58 | * @var string $version The current version of the plugin. |
||
| 59 | */ |
||
| 60 | protected $version; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Define the core functionality of the plugin. |
||
| 64 | * |
||
| 65 | * Set the plugin name and the plugin version that can be used throughout the plugin. |
||
| 66 | * Load the dependencies, define the locale, and set the hooks for the admin area and |
||
| 67 | * the public-facing side of the site. |
||
| 68 | * |
||
| 69 | * @since 1.0.0 |
||
| 70 | */ |
||
| 71 | public function init() |
||
| 90 | |||
| 91 | public function check_dependencies(){ |
||
| 117 | |||
| 118 | public function trigger_message($message, $status = "error", $message_param1 = "", $message_param2 = "", $message_param3 = "") |
||
| 124 | |||
| 125 | public function trigger_message_notice($message, $status = "success") |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Load the required dependencies for this plugin. |
||
| 136 | * |
||
| 137 | * Include the following files that make up the plugin: |
||
| 138 | * |
||
| 139 | * - Xcloner_Loader. Orchestrates the hooks of the plugin. |
||
| 140 | * - Xcloner_i18n. Defines internationalization functionality. |
||
| 141 | * - Xcloner_Admin. Defines all hooks for the admin area. |
||
| 142 | * - Xcloner_Public. Defines all hooks for the public side of the site. |
||
| 143 | * |
||
| 144 | * Create an instance of the loader which will be used to register the hooks |
||
| 145 | * with WordPress. |
||
| 146 | * |
||
| 147 | * @since 1.0.0 |
||
| 148 | * @access private |
||
| 149 | */ |
||
| 150 | private function load_dependencies() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Define the locale for this plugin for internationalization. |
||
| 236 | * |
||
| 237 | * Uses the Xcloner_i18n class in order to set the domain and to register the hook |
||
| 238 | * with WordPress. |
||
| 239 | * |
||
| 240 | * @since 1.0.0 |
||
| 241 | * @access private |
||
| 242 | */ |
||
| 243 | private function set_locale() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Register all of the hooks related to the admin area functionality |
||
| 256 | * of the plugin. |
||
| 257 | * |
||
| 258 | * @since 1.0.0 |
||
| 259 | * @access private |
||
| 260 | */ |
||
| 261 | private function define_admin_hooks() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Register the Admin Sidebar menu |
||
| 273 | * |
||
| 274 | * @access private |
||
| 275 | */ |
||
| 276 | private function define_admin_menu(){ |
||
| 281 | |||
| 282 | private function define_plugin_settings(){ |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Register all of the hooks related to the public-facing functionality |
||
| 292 | * of the plugin. |
||
| 293 | * |
||
| 294 | * @since 1.0.0 |
||
| 295 | * @access private |
||
| 296 | */ |
||
| 297 | private function define_public_hooks() { |
||
| 305 | |||
| 306 | public function exception_handler() { |
||
| 317 | |||
| 318 | function friendly_error_type($type) { |
||
| 329 | |||
| 330 | private function define_ajax_hooks() |
||
| 331 | { |
||
| 332 | //$plugin_public = new Xcloner_Public( $this->get_plugin_name(), $this->get_version() ); |
||
| 333 | //$this->loader->add_action( 'wp_ajax_get_database_tables_action', $plugin_public, array('Xcloner_Api','get_database_tables_action') ); |
||
| 334 | |||
| 335 | if(is_admin()) |
||
| 336 | { |
||
| 337 | $xcloner_api = new Xcloner_Api(); |
||
| 338 | |||
| 339 | add_action( 'wp_ajax_get_database_tables_action' , array($xcloner_api,'get_database_tables_action') ); |
||
| 340 | add_action( 'wp_ajax_get_file_system_action' , array($xcloner_api,'get_file_system_action') ); |
||
| 341 | add_action( 'wp_ajax_scan_filesystem' , array($xcloner_api,'scan_filesystem') ); |
||
| 342 | add_action( 'wp_ajax_backup_database' , array($xcloner_api,'backup_database') ); |
||
| 343 | add_action( 'wp_ajax_backup_files' , array($xcloner_api,'backup_files') ); |
||
| 344 | add_action( 'wp_ajax_save_schedule' , array($xcloner_api,'save_schedule') ); |
||
| 345 | add_action( 'wp_ajax_get_schedule_by_id' , array($xcloner_api,'get_schedule_by_id') ); |
||
| 346 | add_action( 'wp_ajax_get_scheduler_list' , array($xcloner_api,'get_scheduler_list') ); |
||
| 347 | add_action( 'wp_ajax_delete_schedule_by_id' , array($xcloner_api,'delete_schedule_by_id') ); |
||
| 348 | add_action( 'wp_ajax_delete_backup_by_name' , array($xcloner_api,'delete_backup_by_name') ); |
||
| 349 | add_action( 'wp_ajax_download_backup_by_name' , array($xcloner_api,'download_backup_by_name') ); |
||
| 350 | add_action( 'wp_ajax_remote_storage_save_status' , array($xcloner_api,'remote_storage_save_status') ); |
||
| 351 | add_action( 'wp_ajax_upload_backup_to_remote' , array($xcloner_api,'upload_backup_to_remote') ); |
||
| 352 | add_action( 'wp_ajax_list_backup_files' , array($xcloner_api,'list_backup_files') ); |
||
| 353 | add_action( 'wp_ajax_restore_upload_backup' , array($xcloner_api,'restore_upload_backup') ); |
||
| 354 | add_action( 'wp_ajax_download_restore_script' , array($xcloner_api,'download_restore_script') ); |
||
| 355 | add_action( 'admin_notices', array($this, 'xcloner_error_admin_notices' )); |
||
| 356 | |||
| 357 | //if (is_admin()) { |
||
| 358 | add_filter('plugin_action_links', array($this, 'add_plugin_action_links'), 10, 2); |
||
| 359 | } |
||
| 360 | |||
| 361 | } |
||
| 362 | |||
| 363 | function add_plugin_action_links($links, $file) { |
||
| 372 | |||
| 373 | public function xcloner_error_admin_notices() { |
||
| 376 | |||
| 377 | public function define_cron_hooks() |
||
| 387 | |||
| 388 | /*public function xcloner_scheduler_callback($schedule_id) |
||
| 389 | { |
||
| 390 | $cron = new Xcloner_Scheduler; |
||
| 391 | |||
| 392 | $cron->run_schedule($schedule_id); |
||
| 393 | }*/ |
||
| 394 | |||
| 395 | function add_new_intervals($schedules) |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * Run the loader to execute all of the hooks with WordPress. |
||
| 414 | * |
||
| 415 | * @since 1.0.0 |
||
| 416 | */ |
||
| 417 | public function run() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * The name of the plugin used to uniquely identify it within the context of |
||
| 423 | * WordPress and to define internationalization functionality. |
||
| 424 | * |
||
| 425 | * @since 1.0.0 |
||
| 426 | * @return string The name of the plugin. |
||
| 427 | */ |
||
| 428 | public function get_plugin_name() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * The reference to the class that orchestrates the hooks with the plugin. |
||
| 434 | * |
||
| 435 | * @since 1.0.0 |
||
| 436 | * @return Xcloner_Loader Orchestrates the hooks of the plugin. |
||
| 437 | */ |
||
| 438 | public function get_loader() { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Retrieve the version number of the plugin. |
||
| 444 | * |
||
| 445 | * @since 1.0.0 |
||
| 446 | * @return string The version number of the plugin. |
||
| 447 | */ |
||
| 448 | public function get_version() { |
||
| 451 | |||
| 452 | public function display($page) |
||
| 459 | } |
||
| 460 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.