Sensei_Learner_Profiles   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 214
Duplicated Lines 3.27 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 20
lcom 1
cbo 4
dl 7
loc 214
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A content() 0 12 4
A setup_permastruct() 0 10 3
A page_title() 0 11 2
B get_permalink() 7 22 4
A learner_profile_courses_heading() 0 9 2
A user_info() 0 58 1
A learner_profile_body_class() 0 7 2
A deprecate_sensei_learner_profile_content_hook() 0 5 1
A __construct() 0 15 1

How to fix   Duplicated Code   

Duplicated Code

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 2.

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.

Loading history...
2
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4
/**
5
 * All functionality pertaining to the learner profiles in Sensei.
6
 *
7
 * @package Views
8
 * @author Automattic
9
 *
10
 * @since 1.4.0
11
 */
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;
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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() {
107
		global $wp_query,  $learner_user, $current_user;
108
109
		if( isset( Sensei()->settings->settings[ 'learner_profile_enable' ] ) && Sensei()->settings->settings[ 'learner_profile_enable' ] ) {
110
111
			if( isset( $wp_query->query_vars['learner_profile'] ) ) {
112
113
                Sensei_Templates::get_template( 'learner-profile/learner-info.php' );
114
115
			}
116
		}
117
	}
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 ) {
126
		if( strlen( $user->first_name ) > 0 ) {
127
			$name = $user->first_name;
128
		} else {
129
			$name = $user->display_name;
130
		}
131
		$name = apply_filters( 'sensei_learner_profile_courses_heading_name', $name );
132
		echo '<h2>' . apply_filters( 'sensei_learner_profile_courses_heading', sprintf( __( 'Courses %s is taking', 'woothemes-sensei' ), $name ) ) . '</h2>';
133
	}
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 ) {
142
143
        /**
144
         * This hooke fires inside the Sensei_Learner_Profiles::user_info function.
145
         * just before the htmls is generated.
146
         * @since 1.0.0
147
         */
148
        do_action( 'sensei_learner_profile_info', $user );
149
150
        /**
151
         * This filter runs inside the Sensei_Learner_Profiles::user_info function.
152
         * Here you can change the user avatar.
153
         *
154
         * @since 1.0.0
155
         *
156
         * @param false|string `<img>` $user_avatar
157
         */
158
		$learner_avatar = apply_filters( 'sensei_learner_profile_info_avatar', get_avatar( $user->ID, 120 ), $user->ID );
159
160
        /**
161
         * This filter runs inside the Sensei_Learner_Profiles::user_info function.
162
         * Here you can change the learner profile user display name.
163
         * @since 1.0.0
164
         *
165
         * @param string $user_display_name
166
         * @param string $user_id
167
         */
168
		$learner_name = apply_filters( 'sensei_learner_profile_info_name', $user->display_name, $user->ID );
169
170
        /**
171
         * This filter runs inside the Sensei_Learner_Profiles::user_info function.
172
         * With this filter can change the users description on the learner user info
173
         * output.
174
         *
175
         * @since 1.0.0
176
         *
177
         * @param string $user_description
178
         * @param string $user_id
179
         */
180
		$learner_bio = apply_filters( 'sensei_learner_profile_info_bio', $user->description, $user->ID );
181
		?>
182
183
		<div id="learner-info">
184
185
			<div class="learner-avatar"><?php echo $learner_avatar; ?></div>
186
187
			<div class="learner-content">
188
189
				<h2><?php echo $learner_name; ?></h2>
190
191
				<div class="description"><?php echo wpautop( $learner_bio ); ?></div>
192
193
			</div>
194
195
		</div>
196
197
        <?php
198
	}
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 ) {
206
		global $wp_query;
207
		if( isset( $wp_query->query_vars['learner_profile'] ) ) {
208
			$classes[] = 'learner-profile';
209
		}
210
		return $classes;
211
	}
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(){
219
220
        sensei_do_deprecated_action( 'sensei_learner_profile_content', '1.9.0', 'sensei_learner_profile_content_before' );
221
222
    }
223
224
225
} // End Class
226
227
/**
228
 * Class WooThemes_Sensei_Learner_Profiles
229
 * @ignore only for backward compatibility
230
 * @since 1.9.0
231
 */
232
class WooThemes_Sensei_Learner_Profiles extends Sensei_Learner_Profiles {}
233