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 Writing_On_GitHub_Admin { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * plugin file name rel plugin dir. |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $plugin_file; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Hook into GitHub API |
||
| 20 | */ |
||
| 21 | public function __construct( $plugin_file ) { |
||
| 22 | $this->plugin_file = $plugin_file; |
||
| 23 | |||
| 24 | add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
||
|
|
|||
| 25 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
||
| 26 | add_action( 'current_screen', array( $this, 'trigger_cron' ) ); |
||
| 27 | add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 ); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * settings link |
||
| 32 | * @param string[] $links |
||
| 33 | * @param string $file |
||
| 34 | * @return string[] |
||
| 35 | */ |
||
| 36 | public function settings_links( $links, $file ) { |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Callback to render the settings page view |
||
| 51 | */ |
||
| 52 | public function settings_page() { |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Callback to register the plugin's options |
||
| 58 | */ |
||
| 59 | public function register_settings() { |
||
| 60 | add_settings_section( |
||
| 61 | 'general', |
||
| 62 | 'General Settings', |
||
| 63 | array( $this, 'section_callback' ), |
||
| 64 | Writing_On_GitHub::$text_domain |
||
| 65 | ); |
||
| 66 | |||
| 67 | register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' ); |
||
| 68 | add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
||
| 69 | 'default' => 'https://api.github.com', |
||
| 70 | 'name' => 'wogh_host', |
||
| 71 | 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ), |
||
| 72 | ) |
||
| 73 | ); |
||
| 74 | |||
| 75 | register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' ); |
||
| 76 | add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
||
| 77 | 'default' => '', |
||
| 78 | 'name' => 'wogh_repository', |
||
| 79 | '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.', 'writing-on-github' ), |
||
| 80 | ) |
||
| 81 | ); |
||
| 82 | |||
| 83 | register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' ); |
||
| 84 | add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
||
| 85 | 'default' => 'master', |
||
| 86 | 'name' => 'wogh_branch', |
||
| 87 | 'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ), |
||
| 88 | ) |
||
| 89 | ); |
||
| 90 | |||
| 91 | register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' ); |
||
| 92 | add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
||
| 93 | 'default' => '', |
||
| 94 | 'name' => 'wogh_oauth_token', |
||
| 95 | 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ), |
||
| 96 | ) |
||
| 97 | ); |
||
| 98 | |||
| 99 | register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' ); |
||
| 100 | add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
||
| 101 | 'default' => '', |
||
| 102 | 'name' => 'wogh_secret', |
||
| 103 | 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ), |
||
| 104 | ) |
||
| 105 | ); |
||
| 106 | |||
| 107 | register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' ); |
||
| 108 | add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
||
| 109 | 'default' => '', |
||
| 110 | 'name' => 'wogh_default_user', |
||
| 111 | 'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ), |
||
| 112 | ) |
||
| 113 | ); |
||
| 114 | |||
| 115 | register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' ); |
||
| 116 | add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
||
| 117 | 'default' => '', |
||
| 118 | 'name' => 'wogh_ignore_author', |
||
| 119 | 'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ), |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | |||
| 123 | register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' ); |
||
| 124 | add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
||
| 125 | 'default' => '', |
||
| 126 | 'name' => 'wogh_dont_export_content', |
||
| 127 | 'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ), |
||
| 128 | ) |
||
| 129 | ); |
||
| 130 | |||
| 131 | // register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_metas' ); |
||
| 132 | // add_settings_field( 'wogh_ignore_metas', __( 'Ignore post metas', 'writing-on-github' ), array( &$this, 'textarea_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
||
| 133 | // 'default' => '', |
||
| 134 | // 'name' => 'wogh_ignore_metas', |
||
| 135 | // 'help_text' => __( 'These meta keys will be ignored and cannot be imported and exported. One meta key per line.', 'writing-on-github' ), |
||
| 136 | // ) |
||
| 137 | // ); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Callback to render an individual options field |
||
| 142 | * |
||
| 143 | * @param array $args Field arguments. |
||
| 144 | */ |
||
| 145 | public function field_callback( $args ) { |
||
| 146 | include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php'; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Callback to render the default import user field. |
||
| 151 | * |
||
| 152 | * @param array $args Field arguments. |
||
| 153 | */ |
||
| 154 | public function user_field_callback( $args ) { |
||
| 155 | include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php'; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Callback to render the textarea field. |
||
| 160 | * |
||
| 161 | * @param array $args Field arguments. |
||
| 162 | */ |
||
| 163 | public function textarea_field_callback( $args ) { |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Callback to render the checkbox field. |
||
| 169 | * |
||
| 170 | * @param array $args Field arguments. |
||
| 171 | */ |
||
| 172 | public function checkbox_field_callback( $args ) { |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Displays settings messages from background processes |
||
| 178 | */ |
||
| 179 | public function section_callback() { |
||
| 180 | if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
||
| 181 | return; |
||
| 182 | } |
||
| 183 | |||
| 184 | if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?> |
||
| 185 | <div class="updated"> |
||
| 186 | <p><?php esc_html_e( 'Export to GitHub started.', 'writing-on-github' ); ?></p> |
||
| 187 | </div><?php |
||
| 188 | delete_option( '_wogh_export_started' ); |
||
| 189 | } |
||
| 190 | |||
| 191 | if ( $message = get_option( '_wogh_export_error' ) ) { ?> |
||
| 192 | <div class="error"> |
||
| 193 | <p><?php esc_html_e( 'Export to GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> |
||
| 194 | </div><?php |
||
| 195 | delete_option( '_wogh_export_error' ); |
||
| 196 | } |
||
| 197 | |||
| 198 | if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?> |
||
| 199 | <div class="updated"> |
||
| 200 | <p><?php esc_html_e( 'Export to GitHub completed successfully.', 'writing-on-github' );?></p> |
||
| 201 | </div><?php |
||
| 202 | delete_option( '_wogh_export_complete' ); |
||
| 203 | } |
||
| 204 | |||
| 205 | if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?> |
||
| 206 | <div class="updated"> |
||
| 207 | <p><?php esc_html_e( 'Import from GitHub started.', 'writing-on-github' ); ?></p> |
||
| 208 | </div><?php |
||
| 209 | delete_option( '_wogh_import_started' ); |
||
| 210 | } |
||
| 211 | |||
| 212 | if ( $message = get_option( '_wogh_import_error' ) ) { ?> |
||
| 213 | <div class="error"> |
||
| 214 | <p><?php esc_html_e( 'Import from GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> |
||
| 215 | </div><?php |
||
| 216 | delete_option( '_wogh_import_error' ); |
||
| 217 | } |
||
| 218 | |||
| 219 | if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?> |
||
| 220 | <div class="updated"> |
||
| 221 | <p><?php esc_html_e( 'Import from GitHub completed successfully.', 'writing-on-github' );?></p> |
||
| 222 | </div><?php |
||
| 223 | delete_option( '_wogh_import_complete' ); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Add options menu to admin navbar |
||
| 229 | */ |
||
| 230 | public function add_admin_menu() { |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Admin callback to trigger import/export because WordPress admin routing lol |
||
| 242 | */ |
||
| 243 | public function trigger_cron() { |
||
| 271 | } |
||
| 272 | } |
||
| 273 |