Completed
Push — master ( 9a3784...c8ddca )
by Justin
22:39
created

register_email_group()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 1
b 0
f 1
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Take settings registered for WP-Admin and hooks them up to the REST API.
8
 *
9
 * @version  2.7.0
10
 * @since    2.7.0
11
 * @package  WooCommerce/Classes
12
 * @category Class
13
 */
14
class WC_Register_WP_Admin_Settings {
15
16
	/** @var class Contains the current class to pull settings from. Either a admin page object or WC_Email object. */
17
	protected $object;
18
19
	/**
20
	 * Hooks into the settings API and starts registering our settings.
21
	 *
22
	 * @since 2.7.0
23
	 * @param WC_Email|WC_Settings_Page $object The object that contains the settings to register.
24
	 * @param string                    $type   Type of settings to register (email or page).
25
	 */
26
	public function __construct( $object, $type ) {
27
		$this->object = $object;
0 ignored issues
show
Documentation Bug introduced by
It seems like $object of type object<WC_Email> or object<WC_Settings_Page> is incompatible with the declared type object<class> of property $object.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
		if ( 'page' === $type ) {
29
			add_filter( 'woocommerce_settings_groups', array( $this, 'register_page_group' ) );
30
			add_filter( 'woocommerce_settings-' . $this->object->get_id(),  array( $this, 'register_page_settings' ) );
0 ignored issues
show
Bug introduced by
The method get_id does only exist in WC_Settings_Page, but not in WC_Email.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
31
		} else if ( 'email' === $type ) {
32
			add_filter( 'woocommerce_settings_groups', array( $this, 'register_email_group' ) );
33
			add_filter( 'woocommerce_settings-email_' . $this->object->id,  array( $this, 'register_email_settings' ) );
34
		}
35
	}
36
37
	/**
38
	 * Register's all of our different notification emails as sub groups
39
	 * of email settings.
40
	 *
41
	 * @since  2.7.0
42
	 * @param  array $groups Existing registered groups.
43
	 * @return array
44
	 */
45
	public function register_email_group( $groups ) {
46
		$groups[] = array(
47
			'id'          => 'email_' . $this->object->id,
48
			'label'       => $this->object->title,
49
			'description' => $this->object->description,
50
			'parent_id'   => 'email',
51
		);
52
		return $groups;
53
	}
54
55
	/**
56
	 * Registers all of the setting form fields for emails to each email type's group.
57
	 *
58
	 * @since  2.7.0
59
	 * @param  array $settings Existing registered settings.
60
	 * @return array
61
	 */
62
	public function register_email_settings( $settings ) {
63
		foreach ( $this->object->form_fields as $id => $setting ) {
64
			$setting['id']         = $id;
65
			$setting['option_key'] = array( $this->object->get_option_key(), $id );
66
			$new_setting      = $this->register_setting( $setting );
67
			if ( $new_setting ) {
68
				$settings[] = $new_setting;
69
			}
70
		}
71
		return $settings;
72
	}
73
74
	/**
75
	 * Registers a setting group, based on admin page ID & label as parent group.
76
	 *
77
	 * @since  2.7.0
78
	 * @param  array $groups Array of previously registered groups.
79
	 * @return array
80
	 */
81
	public function register_page_group( $groups ) {
82
		$groups[] = array(
83
			'id'    => $this->object->get_id(),
84
			'label' => $this->object->get_label(),
85
		);
86
		return $groups;
87
	}
88
89
	/**
90
	 * Registers settings to a specific group.
91
	 *
92
	 * @since  2.7.0
93
	 * @param  array $settings Existing registered settings
94
	 * @return array
95
	 */
96
	public function register_page_settings( $settings ) {
97
		/**
98
		 * wp-admin settings can be broken down into separate sections from
99
		 * a UI standpoint. This will grab all the sections associated with
100
		 * a particular setting group (like 'products') and register them
101
		 * to the REST API.
102
		 */
103
		$sections = $this->object->get_sections();
104
		if ( empty( $sections ) ) {
105
			// Default section is just an empty string, per admin page classes
106
			$sections = array( '' );
107
		}
108
109
		foreach ( $sections as $section => $section_label ) {
110
			$settings_from_section = $this->object->get_settings( $section );
111
			foreach ( $settings_from_section as $setting ) {
112
				if ( ! isset( $setting['id'] ) ) {
113
					continue;
114
				}
115
				$setting['option_key'] = $setting['id'];
116
				$new_setting           = $this->register_setting( $setting );
117
				if ( $new_setting ) {
118
					$settings[] = $new_setting;
119
				}
120
			}
121
		}
122
		return $settings;
123
	}
124
125
	/**
126
	 * Register a setting into the format expected for the Settings REST API.
127
	 *
128
	 * @since 2.7.0
129
	 * @param  array $setting
130
	 * @return array|bool
131
	 */
132
	public function register_setting( $setting ) {
133
		if ( ! isset( $setting['id'] ) ) {
134
			return false;
135
		}
136
137
		$description = '';
138 View Code Duplication
		if ( ! empty ( $setting['desc'] ) ) {
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...
139
			$description = $setting['desc'];
140
		} else if ( ! empty( $setting['description'] ) ) {
141
			$description = $setting['description'];
142
		}
143
144
		$new_setting = array(
145
			'id'          => $setting['id'],
146
			'label'       => ( ! empty( $setting['title'] ) ? $setting['title'] : '' ),
147
			'description' => $description,
148
			'type'        => $setting['type'],
149
			'option_key'  => $setting['option_key'],
150
		);
151
152
		if ( isset( $setting['default'] ) ) {
153
			$new_setting['default'] = $setting['default'];
154
		}
155
		if ( isset( $setting['options'] ) ) {
156
			$new_setting['options'] = $setting['options'];
157
		}
158
		if ( isset( $setting['desc_tip'] ) ) {
159 View Code Duplication
			if ( true === $setting['desc_tip'] ) {
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...
160
				$new_setting['tip'] = $description;
161
			} else if ( ! empty( $setting['desc_tip'] ) ) {
162
				$new_setting['tip'] = $setting['desc_tip'];
163
			}
164
		}
165
166
		return $new_setting;
167
	}
168
169
}
170