Completed
Pull Request — staging (#840)
by
unknown
17:14
created

OptinForm::import_forms()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 2
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * YIKES Inc. Easy Forms.
4
 *
5
 * @package   YIKES\EasyForms
6
 * @author    Freddie Mixell
7
 * @license   GPL2
8
 */
9
10
namespace YIKES\EasyForms\Model;
11
12 View Code Duplication
class OptinForm extends OptinFormBase {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
13
14
	/**
15
	 * The base name of our option in the options table.
16
	 *
17
	 * @var string
18
	 */
19
	protected $option = 'yikes_easy_mailchimp_extender_forms';
20
21
	/**
22
	 * Get a form with the given ID.
23
	 *
24
	 * @author Freddie Mixell
25
	 *
26
	 * @param int $form_id The ID of the form to retrieve.
27
	 *
28
	 * @return array The array of form data.
29
	 */
30
	public function find( $form_id ) {
31
		$all_forms = $this->get_all_forms();
32
33
		return isset( $all_forms[ $form_id ] ) ? $all_forms[ $form_id ] : array();
34
	}
35
36
	/**
37
	 * Get the IDs of all registered forms.
38
	 *
39
	 * @author Jeremy Pry
40
	 * @return array All form IDs.
41
	 */
42
	public function get_form_ids() {
43
		return array_keys( $this->get_all_forms() );
44
	}
45
46
	/**
47
	 * Update the data for a particular form.
48
	 *
49
	 * @author Jeremy Pry
50
	 *
51
	 * @param int   $form_id The form ID to update.
52
	 * @param array $data    The form data to update.
53
	 *
54
	 * @return bool Whether the form was successfully updated.
55
	 */
56
	public function update_form( $form_id, $data ) {
57
		$data['id'] = $form_id;
58
		$all_forms  = $this->get_all_forms();
59
60
		if ( ! isset( $all_forms[ $form_id ] ) ) {
61
			return false;
62
		}
63
64
		$new_data = array_merge( $all_forms[ $form_id ], $data );
65
		ksort( $new_data );
66
		$all_forms[ $form_id ] = $new_data;
67
68
		return update_option( $this->option, $all_forms );
69
	}
70
71
	/**
72
	 * Create a new form.
73
	 *
74
	 * @author Jeremy Pry
75
	 *
76
	 * @param array $form_data Data to apply to the new form.
77
	 *
78
	 * @return int|bool The new form ID, or false on failure.
79
	 */
80
	public function create_form( $form_data ) {
81
		// Include default form data
82
		$form_data = yikes_deep_parse_args( $form_data, $this->get_form_defaults() );
83
84
		// Grab our existing IDs and determine what the next one should be.
85
		$all_ids         = $this->get_form_ids();
86
		$last_id         = end( $all_ids );
87
		$new_id          = false === $last_id ? 1 : $last_id + 1;
0 ignored issues
show
Bug Compatibility introduced by
The expression false === $last_id ? 1 : $last_id + 1; of type integer|double adds the type double to the return on line 103 which is incompatible with the return type declared by the interface YIKES\EasyForms\Model\OptinInterface::create_form of type integer|boolean.
Loading history...
88
		$form_data['id'] = $new_id;
89
90
		// Ensure our data is consistently sorted
91
		ksort( $form_data );
92
93
		// Store the new form in our array of forms.
94
		$all_forms            = $this->get_all_forms();
95
		$all_forms[ $new_id ] = $form_data;
96
97
		// Update our option with the new form.
98
		$result = update_option( $this->option, $all_forms );
99
		if ( false === $result ) {
100
			return $result;
101
		}
102
103
		return $new_id;
104
	}
105
106
	/**
107
	 * Delete a form.
108
	 *
109
	 * @author Jeremy Pry
110
	 *
111
	 * @param int $form_id The form ID to delete.
112
	 *
113
	 * @return bool Whether the form was successfully deleted.
114
	 */
115
	public function delete_form( $form_id ) {
116
		$all_forms = $this->get_all_forms();
117
118
		if ( ! isset( $all_forms[ $form_id ] ) ) {
119
			return false;
120
		}
121
122
		unset( $all_forms[ $form_id ] );
123
124
		return update_option( $this->option, $all_forms );
125
	}
126
127
	/**
128
	 * Get all data for all forms.
129
	 *
130
	 * @author Jeremy Pry
131
	 * @return array All form data, indexed by form ID.
132
	 */
133
	public function get_all_forms() {
134
		return $this->get_option();
135
	}
136
137
	/**
138
	 * Get the name of the option used for saving the forms.
139
	 *
140
	 * @author Jeremy Pry
141
	 * @return string
142
	 */
143
	public function get_option_name() {
144
		return $this->option;
145
	}
146
147
	/**
148
	 * Import forms in bulk.
149
	 *
150
	 * @author Jeremy Pry
151
	 *
152
	 * @param array $form_data        Array of form data, indexed by form ID.
153
	 * @param bool  $replace_existing Whether to replace existing forms.
154
	 */
155
	public function import_forms( $form_data, $replace_existing ) {
156
		$existing = $replace_existing ? array() : $this->get_option();
157
		$new_data = array();
158
159
		foreach ( $form_data as $id => $data ) {
160
			$new_data[ $id ] = isset( $existing[ $id ] ) ? $existing[ $id ] : $data;
161
			ksort( $new_data[ $id ] );
162
		}
163
164
		update_option( $this->option, $new_data );
165
	}
166
167
	/**
168
	 * Add our option to the database.
169
	 *
170
	 * @author Jeremy Pry
171
	 * @return bool
172
	 */
173
	public function create_option() {
174
		return add_option( $this->get_option_name(), array() );
175
	}
176
177
	/**
178
	 * Get our forms option from the database.
179
	 *
180
	 * @author Jeremy Pry
181
	 * @return array The array of form data.
182
	 */
183
	protected function get_option() {
184
		return get_option( $this->get_option_name(), array() );
185
	}
186
}
187