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 |
||
12 | class Sensei_Learner_Profiles { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $profile_url_base; |
||
17 | |||
18 | /** |
||
19 | * Constructor. |
||
20 | * @since 1.4.0 |
||
21 | */ |
||
22 | public function __construct () { |
||
23 | |||
24 | // Setup learner profile URL base |
||
25 | $this->profile_url_base = apply_filters( 'sensei_learner_profiles_url_base', __( 'learner', 'woothemes-sensei') ); |
||
26 | |||
27 | // Setup permalink structure for learner profiles |
||
28 | add_action( 'init', array( $this, 'setup_permastruct' ) ); |
||
29 | add_filter( 'wp_title', array( $this, 'page_title' ), 10, 2 ); |
||
30 | |||
31 | // Set heading for courses section of learner profiles |
||
32 | add_action( 'sensei_learner_profile_info', array( $this, 'learner_profile_courses_heading' ), 30, 1 ); |
||
33 | |||
34 | // Add class to body tag |
||
35 | add_filter( 'body_class', array( $this, 'learner_profile_body_class' ), 10, 1 ); |
||
36 | } // End __construct() |
||
37 | |||
38 | /** |
||
39 | * Setup permalink structure for learner profiles |
||
40 | * @since 1.4.0 |
||
41 | * @return void |
||
42 | */ |
||
43 | public function setup_permastruct() { |
||
44 | |||
45 | if( isset( Sensei()->settings->settings[ 'learner_profile_enable' ] ) |
||
46 | && Sensei()->settings->settings[ 'learner_profile_enable' ] ) { |
||
47 | |||
48 | add_rewrite_rule( '^' . $this->profile_url_base . '/([^/]*)/?', 'index.php?learner_profile=$matches[1]', 'top' ); |
||
49 | add_rewrite_tag( '%learner_profile%', '([^&]+)' ); |
||
50 | |||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Adding page title for course results page |
||
56 | * @param string $title Original title |
||
57 | * @param string $sep Seeparator string |
||
58 | * @return string Modified title |
||
59 | */ |
||
60 | public function page_title( $title, $sep = null ) { |
||
61 | global $wp_query; |
||
62 | if( isset( $wp_query->query_vars['learner_profile'] ) ) { |
||
63 | $learner_user = get_user_by( 'login', $wp_query->query_vars['learner_profile'] ); |
||
64 | |||
65 | $name = Sensei_Learner::get_full_name( $learner_user->ID ); |
||
66 | |||
67 | $title = apply_filters( 'sensei_learner_profile_courses_heading', sprintf( __( 'Courses %s is taking', 'woothemes-sensei' ), $name ) ) . ' ' . $sep . ' '; |
||
68 | } |
||
69 | return $title; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Get permalink for learner profile |
||
74 | * @since 1.4.0 |
||
75 | * @param integer $user_id ID of user |
||
76 | * @return string The learner profile permalink |
||
77 | */ |
||
78 | public function get_permalink( $user_id = 0 ) { |
||
79 | $user = false; |
||
80 | if( $user_id == 0 ) { |
||
81 | global $current_user; |
||
82 | wp_get_current_user(); |
||
83 | $user = $current_user; |
||
84 | } else { |
||
85 | $user = get_userdata( $user_id ); |
||
86 | } |
||
87 | |||
88 | $permalink = ''; |
||
89 | |||
90 | View Code Duplication | if( $user ) { |
|
91 | if ( get_option('permalink_structure') ) { |
||
92 | $permalink = trailingslashit( get_site_url() ) . $this->profile_url_base . '/' . $user->user_nicename; |
||
93 | } else { |
||
94 | $permalink = trailingslashit( get_site_url() ) . '?learner_profile=' . $user->user_nicename; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $permalink; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Load content for learner profiles |
||
103 | * @since 1.4.0 |
||
104 | * @return void |
||
105 | */ |
||
106 | public function content() { |
||
118 | |||
119 | /** |
||
120 | * Set heading for courses section of learner profiles |
||
121 | * @since 1.4.0 |
||
122 | * @param object $user Queried user object |
||
123 | * @return void |
||
124 | */ |
||
125 | public function learner_profile_courses_heading( $user ) { |
||
134 | |||
135 | /** |
||
136 | * Load user info for learner profiles |
||
137 | * @since 1.4.0 |
||
138 | * @param object $user Queried user object |
||
139 | * @return void |
||
140 | */ |
||
141 | public static function user_info( $user ) { |
||
199 | |||
200 | /** |
||
201 | * Adding class to body tag |
||
202 | * @param array $classes Existing classes |
||
203 | * @return array Modified classes |
||
204 | */ |
||
205 | public function learner_profile_body_class( $classes ) { |
||
212 | |||
213 | /** |
||
214 | * Deprecate the deprecate_sensei_learner_profile_content hook |
||
215 | * |
||
216 | * @since 1.9.0 |
||
217 | */ |
||
218 | public static function deprecate_sensei_learner_profile_content_hook(){ |
||
223 | |||
224 | |||
225 | } // End Class |
||
226 | |||
233 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.