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 |
||
| 10 | class WordPress_GitHub_Sync_Admin { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Hook into GitHub API |
||
| 14 | */ |
||
| 15 | public function __construct() { |
||
| 16 | add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
||
| 17 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
||
| 18 | add_action( 'current_screen', array( $this, 'trigger_cron' ) ); |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Callback to render the settings page view |
||
| 23 | */ |
||
| 24 | public function settings_page() { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Callback to register the plugin's options |
||
| 30 | */ |
||
| 31 | public function register_settings() { |
||
| 32 | add_settings_section( |
||
| 33 | 'general', |
||
| 34 | 'General Settings', |
||
| 35 | array( $this, 'section_callback' ), |
||
| 36 | WordPress_GitHub_Sync::$text_domain |
||
| 37 | ); |
||
| 38 | |||
| 39 | register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_host' ); |
||
| 40 | add_settings_field( 'wpghs_host', __( 'GitHub hostname', 'wp-github-sync' ), array( $this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array( |
||
| 41 | 'default' => 'https://api.github.com', |
||
| 42 | 'name' => 'wpghs_host', |
||
| 43 | 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'wp-github-sync' ), |
||
| 44 | ) |
||
| 45 | ); |
||
| 46 | |||
| 47 | register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_repository' ); |
||
| 48 | add_settings_field( 'wpghs_repository', __( 'Repository', 'wp-github-sync' ), array( $this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array( |
||
| 49 | 'default' => '', |
||
| 50 | 'name' => 'wpghs_repository', |
||
| 51 | 'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'wp-github-sync' ), |
||
| 52 | ) |
||
| 53 | ); |
||
| 54 | |||
| 55 | register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_oauth_token' ); |
||
| 56 | add_settings_field( 'wpghs_oauth_token', __( 'Oauth Token', 'wp-github-sync' ), array( $this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array( |
||
| 57 | 'default' => '', |
||
| 58 | 'name' => 'wpghs_oauth_token', |
||
| 59 | 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'wp-github-sync' ), |
||
| 60 | ) |
||
| 61 | ); |
||
| 62 | |||
| 63 | register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_secret' ); |
||
| 64 | add_settings_field( 'wpghs_secret', __( 'Webhook Secret', 'wp-github-sync' ), array( $this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array( |
||
| 65 | 'default' => '', |
||
| 66 | 'name' => 'wpghs_secret', |
||
| 67 | 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'wp-github-sync' ), |
||
| 68 | ) |
||
| 69 | ); |
||
| 70 | |||
| 71 | register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_default_user' ); |
||
| 72 | add_settings_field( 'wpghs_default_user', __( 'Default Import User', 'wp-github-sync' ), array( &$this, 'user_field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array( |
||
| 73 | 'default' => '', |
||
| 74 | 'name' => 'wpghs_default_user', |
||
| 75 | 'help_text' => __( 'The fallback user for import, in case WordPress <--> GitHub Sync cannot find the committer in the database.', 'wp-github-sync' ), |
||
| 76 | ) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Callback to render an individual options field |
||
| 82 | * |
||
| 83 | * @param array $args Field arguments. |
||
| 84 | */ |
||
| 85 | public function field_callback( $args ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Callback to render the default import user field. |
||
| 91 | * |
||
| 92 | * @param array $args Field arguments. |
||
| 93 | */ |
||
| 94 | public function user_field_callback( $args ) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Displays settings messages from background processes |
||
| 100 | */ |
||
| 101 | public function section_callback() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Add options menu to admin navbar |
||
| 151 | */ |
||
| 152 | public function add_admin_menu() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Admin callback to trigger import/export because WordPress admin routing lol |
||
| 164 | */ |
||
| 165 | public function trigger_cron() { |
||
| 189 | } |
||
| 190 |