WPBO_Provider_WordPress   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 0
cbo 0
dl 0
loc 68
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A submit() 0 20 2
B register() 0 25 5
1
<?php
2
/**
3
 * BetterOptin Provider WordPress
4
 *
5
 * @package   BetterOptin/Provider/WordPress
6
 * @author    ThemeAvenue <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://themeavenue.net
9
 * @copyright 2015 ThemeAvenue
10
 */
11
12
// If this file is called directly, abort.
13
if ( ! defined( 'WPINC' ) ) {
14
	die;
15
}
16
17
class WPBO_Provider_WordPress {
18
19
	/**
20
	 * Trigger form submission.
21
	 *
22
	 * @since  1.0.0
23
	 *
24
	 * @param array $data Sanitized form data
25
	 *
26
	 * @return null
27
	 */
28
	public static function submit( $data ) {
29
30
		$result = false;
31
32
		/* Subscribe the new user */
33
		$user = self::register( $data );
34
35
		/* Insertion is successful */
36
		if ( ! is_wp_error( $user ) ) {
37
38
			$result = true;
39
40
			/* Add a marker in user meta */
41
			add_user_meta( $user, 'wpbo_subscription', 'yes', true );
42
43
		}
44
45
		return $result;
46
47
	}
48
49
	/**
50
	 * Subscribe the visitor to a list.
51
	 *
52
	 * @since  1.0.0
53
	 *
54
	 * @param array $data Form data
55
	 *
56
	 * @return int|WP_Error Result
57
	 */
58
	protected static function register( $data ) {
59
60
		$password = wp_generate_password();
61
		$role     = isset( $options['wp_default_role'] ) ? $options['wp_default_role'] : 'betteroptin';
0 ignored issues
show
Bug introduced by
The variable $options seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
62
		$email    = $data['email'];
63
64
		/* Extra verification to avoid giving admin cap to subscribers. */
65
		if ( 'administrator' == $role ) {
66
			$role = 'subscriber';
67
		}
68
69
		$args = array(
70
			'user_email'   => $email,
71
			'user_login'   => $email,
72
			'first_name'   => isset( $data['first_name'] ) ? $data['first_name'] : $data['name'],
73
			'display_name' => isset( $data['first_name'] ) ? $data['first_name'] : $email,
74
			'user_pass'    => md5( $password ),
75
			'role'         => $role
76
		);
77
78
		$user = wp_insert_user( $args );
79
80
		return $user;
81
82
	}
83
84
}