UserSettings   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 79
ccs 0
cts 34
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B show_user_profile() 0 36 3
A edit_user_profile_update() 0 9 2
1
<?php
2
3
/**
4
 * The dashboard-specific functionality of the plugin.
5
 *
6
 * @link       https://log.pt
7
 * @since      1.0.0
8
 *
9
 * @package    RedmineEmbed
10
 * @subpackage RedmineEmbed/Admin
11
 */
12
13
namespace logoscon\WP\RedmineEmbed\Admin;
14
15
use logoscon\WP\RedmineEmbed\Plugin;
16
17
/**
18
 * The dashboard-specific functionality of the plugin.
19
 *
20
 * Defines the plugin name, version, and two examples hooks for how to
21
 * enqueue the dashboard-specific stylesheet and JavaScript.
22
 *
23
 * @package    RedmineEmbed
24
 * @subpackage RedmineEmbed/Admin
25
 * @author     log.OSCON, Lda. <[email protected]>
26
 */
27
class UserSettings {
28
29
	/**
30
	 * The plugin's instance.
31
	 *
32
	 * @since  1.0.0
33
	 * @access private
34
	 * @var    Plugin $plugin This plugin's instance.
35
	 */
36
	private $plugin;
37
38
	/**
39
	 * Initialize the class and set its properties.
40
	 *
41
	 * @since 1.0.0
42
	 *
43
	 * @param Plugin $plugin This plugin's instance.
44
	 */
45
	public function __construct( Plugin $plugin ) {
46
		$this->plugin = $plugin;
47
	}
48
49
	/**
50
	 * Display the user profile form field.
51
	 *
52
	 * @param  \WP_User $user User instance.
53
	 */
54
	public function show_user_profile( \WP_User $user ) {
55
		$url = $this->plugin->get_option( 'root_url', '' );
56
57
		if ( empty( $url ) ) {
58
			return;
59
		}
60
61
		if ( ! \current_user_can( 'edit_user', $user->ID ) ) {
62
			return;
63
		}
64
65
		$api_key = \get_user_option( 'redmine_embed_api_key', $user->ID );
66
67
		?>
68
		<h3><?php \_e( 'Redmine', 'redmine-embed' ); ?></h3>
69
		<table class="form-table">
70
			<tr>
71
				<th><label for="redmine_embed_api_key"><?php
72
					\_e( 'API Key', 'redmine-embed' );
73
				?></label></th>
74
				<td>
75
					<input type="text" name="redmine_embed_api_key"
76
						id="redmine_embed_api_key" class="regular-text"
77
						value="<?php echo \esc_attr( $api_key ); ?>">
78
					<br>
79
					<span class="description"><?php
80
						printf(
81
							\__( 'Enter your API key for the Redmine install at %s. It may be found on the right-hand pane of your account page.', 'redmine-embed' ),
82
							\esc_url( $url )
83
						);
84
					?></span>
85
				</td>
86
			</tr>
87
		</table>
88
		<?php
89
	}
90
91
	/**
92
	 * Update saved fields on the edited user profile.
93
	 * @param int $user_id Edited user ID.
94
	 */
95
	public function edit_user_profile_update( $user_id ) {
0 ignored issues
show
Coding Style introduced by
edit_user_profile_update uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
96
		if ( ! \current_user_can( 'edit_user', $user_id ) ) {
97
			return;
98
		}
99
100
		$api_key = \sanitize_key( $_POST['redmine_embed_api_key'] );
101
102
		\update_user_option( $user_id, 'redmine_embed_api_key', $api_key );
103
	}
104
105
}
106