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 |
||
| 4 | abstract class Jetpack_Admin_Page { |
||
| 5 | // Add page specific actions given the page hook |
||
| 6 | abstract function add_page_actions( $hook ); |
||
| 7 | |||
| 8 | // Create a menu item for the page and returns the hook |
||
| 9 | abstract function get_page_hook(); |
||
| 10 | |||
| 11 | // Enqueue and localize page specific scripts |
||
| 12 | abstract function page_admin_scripts(); |
||
| 13 | |||
| 14 | // Render page specific HTML |
||
| 15 | abstract function page_render(); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Function called after admin_styles to load any additional needed styles. |
||
| 19 | * |
||
| 20 | * @since 4.3 |
||
| 21 | */ |
||
| 22 | function additional_styles() {} |
||
| 23 | |||
| 24 | function __construct() { |
||
| 27 | |||
| 28 | function add_actions() { |
||
| 29 | /** |
||
| 30 | * Don't add in the modules page unless modules are available! |
||
| 31 | */ |
||
| 32 | if ( $this->dont_show_if_not_active && ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
| 33 | return; |
||
| 34 | } |
||
| 35 | |||
| 36 | // Initialize menu item for the page in the admin |
||
| 37 | $hook = $this->get_page_hook(); |
||
| 38 | |||
| 39 | // Attach hooks common to all Jetpack admin pages based on the created |
||
| 40 | // hook |
||
| 41 | add_action( "load-$hook", array( $this, 'admin_help' ) ); |
||
| 42 | add_action( "load-$hook", array( $this, 'admin_page_load' ) ); |
||
| 43 | add_action( "admin_head-$hook", array( $this, 'admin_head' ) ); |
||
| 44 | |||
| 45 | add_action( "admin_print_styles-$hook", array( $this, 'admin_styles' ) ); |
||
| 46 | add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts' ) ); |
||
| 47 | |||
| 48 | // Attach page specific actions in addition to the above |
||
| 49 | $this->add_page_actions( $hook ); |
||
| 50 | } |
||
| 51 | |||
| 52 | function admin_head() { |
||
| 67 | |||
| 68 | // Render the page with a common top and bottom part, and page specific content |
||
| 69 | function render() { |
||
| 70 | $this->page_render(); |
||
| 71 | } |
||
| 72 | |||
| 73 | function admin_help() { |
||
| 74 | $this->jetpack->admin_help(); |
||
| 75 | } |
||
| 76 | |||
| 77 | function admin_page_load() { |
||
| 81 | |||
| 82 | function admin_page_top() { |
||
| 85 | |||
| 86 | function admin_page_bottom() { |
||
| 87 | include_once( JETPACK__PLUGIN_DIR . '_inc/footer.php' ); |
||
| 88 | } |
||
| 89 | |||
| 90 | // Add page specific scripts and jetpack stats for all menu pages |
||
| 91 | function admin_scripts() { |
||
| 92 | $this->page_admin_scripts(); // Delegate to inheriting class |
||
| 93 | add_action( 'admin_footer', array( $this->jetpack, 'do_stats' ) ); |
||
| 94 | } |
||
| 95 | |||
| 96 | // Enqueue the Jetpack admin stylesheet |
||
| 97 | View Code Duplication | function admin_styles() { |
|
| 98 | $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
||
| 99 | |||
| 100 | wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION . '-20121016' ); |
||
| 101 | wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' ); |
||
| 102 | wp_style_add_data( 'jetpack-admin', 'suffix', $min ); |
||
| 103 | |||
| 104 | $this->additional_styles(); |
||
| 105 | } |
||
| 106 | |||
| 107 | function is_wp_version_too_old() { |
||
| 111 | } |
||
| 112 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: