Passed
Branch master (df5e39)
by Christian
02:53
created

Buttons::form_template()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 113

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 113
rs 8
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PodloveSubscribeButton\Settings;
4
5
class Buttons {
6
7
	public static function page() {
8
9
		$action = null !== filter_input(INPUT_GET, 'action') ? filter_input(INPUT_GET, 'action') : NULL;
10
		$is_network = is_network_admin();
11
12
		if ( $action == 'confirm_delete' && null !== filter_input(INPUT_GET, 'button') ) {
13
			$button = ( $is_network === TRUE ? \PodloveSubscribeButton\Model\NetworkButton::find_by_id( (int) filter_input(INPUT_GET, 'button') ) : \PodloveSubscribeButton\Model\Button::find_by_id( (int) filter_input(INPUT_GET, 'button') ) );
14
			?>
15
			<div class="updated">
16
				<p>
17
					<strong>
18
						<?php echo sprintf( __( 'You selected to delete the button "%s". Please confirm this action.', 'podlove-subscribe-button' ), $button->title ) ?>
19
					</strong>
20
				</p>
21
				<p>
22
					<?php echo self::get_action_link( $button, __( 'Delete button permanently', 'podlove-subscribe-button' ), 'delete', 'button' ) ?>
23
					<?php echo self::get_action_link( $button, __( 'Don\'t change anything', 'podlove-subscribe-button' ), 'keep', 'button-primary' ) ?>
24
				</p>
25
			</div>
26
			<?php
27
		}
28
		?>
29
		<div class="wrap">
30
			<h2><?php echo __( 'Podcast Subscribe Button', 'podlove-subscribe-button' ); ?> <a href="?page=<?php echo filter_input(INPUT_GET, 'page'); ?>&amp;action=new&amp;network=<?php echo $is_network; ?>" class="add-new-h2"><?php echo __( 'Add New', 'podlove-subscribe-button' ); ?></a></h2>
31
			<?php
32
33
			switch ( $action ) {
34
				case 'new':   self::new_template();  break;
35
				case 'edit':  self::edit_template(); break;
36
				case 'index': self::view_template(); break;
37
				default:      self::view_template(); break;
38
			}
39
			?>
40
		</div>
41
		<?php
42
	}
43
44
	/**
45
	 * Process form: save/update a format
46
	 */
47
	public static function save() {
48
		if ( null == filter_input(INPUT_GET, 'button') )
49
			return;
50
51
		$post = filter_input_array(INPUT_POST);
52
53
		$button = ( filter_input(INPUT_GET, 'network') === '1' ? \PodloveSubscribeButton\Model\NetworkButton::find_by_id( filter_input(INPUT_GET, 'button') ) : \PodloveSubscribeButton\Model\Button::find_by_id( filter_input(INPUT_GET, 'button') ) );
54
		$button->update_attributes( $post['podlove_button'] );
55
56 View Code Duplication
		if ( isset($post['submit_and_stay']) ) {
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...
57
			self::redirect( 'edit', $button->id, array( 'network' => filter_input(INPUT_GET, 'network') ), ( filter_input(INPUT_GET, 'network') === '1' ? TRUE : FALSE ) );
58
		} else {
59
			self::redirect( 'index', $button->id, array(), ( filter_input(INPUT_GET, 'network') === '1' ? TRUE : FALSE ) );
60
		}
61
	}
62
	/**
63
	 * Process form: create a format
64
	 */
65
	public static function create() {
66
		global $wpdb;
67
68
		$post = filter_input_array(INPUT_POST);
69
70
		$button = ( filter_input(INPUT_GET, 'network') === '1' ? new \PodloveSubscribeButton\Model\NetworkButton : new \PodloveSubscribeButton\Model\Button );
71
		$button->update_attributes( $post['podlove_button'] );
72
73 View Code Duplication
		if ( isset($post['submit_and_stay']) ) {
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...
74
			self::redirect( 'edit', $button->id, array( 'network' => filter_input(INPUT_GET, 'network') ), ( filter_input(INPUT_GET, 'network') === '1' ? TRUE : FALSE ) );
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<PodloveSubscribeButton\Model\Button>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
		} else {
76
			self::redirect( 'index', $button->id, array(), ( filter_input(INPUT_GET, 'network') === '1' ? TRUE : FALSE ) );
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<PodloveSubscribeButton\Model\Button>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
77
		}
78
	}
79
80
	/**
81
	 * Process form: delete a format
82
	 */
83
	public static function delete() {
84
		if ( null ==  filter_input(INPUT_GET, 'button') )
85
			return;
86
87
		$button = ( filter_input(INPUT_GET, 'network') === '1' ? \PodloveSubscribeButton\Model\NetworkButton::find_by_id( filter_input(INPUT_GET, 'button') ) : \PodloveSubscribeButton\Model\Button::find_by_id( filter_input(INPUT_GET, 'button') ) );
88
		$button->delete();
89
90
		self::redirect( 'index', NULL, array(), ( filter_input(INPUT_GET, 'network') === '1' ? TRUE : FALSE ) );
91
	}
92
93
	/**
94
	 * Helper method: redirect to a certain page.
95
	 */
96
	public static function redirect( $action, $button_id = NULL, $params = array(), $network = FALSE ) {
97
		$page    = ( $network ? '/network/settings' : 'options-general' ) . '.php?page=' . filter_input(INPUT_GET, 'page');
98
		$show    = ( $button_id ) ? '&button=' . $button_id : '';
99
		$action  = '&action=' . $action;
100
101
		array_walk( $params, function(&$value, $key) { $value = "&$key=$value"; } );
102
103
		wp_redirect( admin_url( $page . $show . $action . implode( '', $params ) ) );
104
	}
105
106
	public static function process_form() {
107
		if ( null === filter_input(INPUT_GET, 'button') )
108
			return;
109
110
		$action = ( null !== filter_input(INPUT_GET, 'action') ? filter_input(INPUT_GET, 'action') : NULL );
111
112
		if ( $action === 'save' ) {
113
			self::save();
114
		} elseif ( $action === 'create' ) {
115
			self::create();
116
		} elseif ( $action === 'delete' ) {
117
			self::delete();
118
		}
119
	}
120
121
	public static function new_template() {
122
		if ( filter_input(INPUT_GET, 'network') == '1' ) {
123
			$button = new \PodloveSubscribeButton\Model\NetworkButton;
124
		} else {
125
			$button = new \PodloveSubscribeButton\Model\Button;
126
		}
127
128
		echo '<h3>' . __( 'New Subscribe button', 'podlove-subscribe-button' ) . '</h3>'.
129
				__( 'Please fill in your Podcast metadata to create a Podlove Subscription button', 'podlove-subscribe-button' );
130
		self::form_template( $button, 'create' );
131
	}
132
133
	public static function edit_template() {
134
		if ( filter_input(INPUT_GET, 'network') == '1' ) {
135
			$button = \PodloveSubscribeButton\Model\NetworkButton::find_by_id( filter_input(INPUT_GET, 'button') );
136
		} else {
137
			$button = \PodloveSubscribeButton\Model\Button::find_by_id( filter_input(INPUT_GET, 'button') );
138
		}
139
140
		echo '<h3>' . sprintf( __( 'Edit Subscribe button: %s', 'podlove-subscribe-button' ), $button->title ) . '</h3>';
141
		self::form_template( $button, 'save' );
142
	}
143
144
	public static function view_template() {
145
		$is_network = is_network_admin();
146
		?>
147
		<p><?php _e('This plugin allows easy inclusion of the Podlove Subscribe Button. Put it in your sidebar with a simple widget or include the button in pages and/or posts with a simple shortcode.', 'podlove'); ?></p>
148
		<p><?php _e('Start by adding a button for each of your podcasts here. You can then add the button to your sidebar by adding the <a href="widgets.php">Podlove Subscribe Button widget</a>.', 'podlove'); ?></p>
149
		<p><?php _e('If you want to display the button inside a page or article, you can also use the <code>[podlove-subscribe-button]</code> shortcode anywhere.', 'podlove'); ?></p>
150
		<?php
151
		$table = new \PodloveSubscribeButton\Button_List_Table;
152
		$table->prepare_items();
153
		$table->display();
154
155
		// Get the global button settings (with fallback to default values)
156
		$settings = \PodloveSubscribeButton\Model\Button::get_global_setting_with_fallback();
157
158
		if ( ! $is_network ) :
159
		?>
160
		<h3><?php _e('Default Settings', 'podlove'); ?></h3>
161
		<form method="post" action="options.php">
162
			<?php settings_fields( 'podlove-subscribe-button' ); ?>
163
			<?php do_settings_sections( 'podlove-subscribe-button' ); ?>
164
			<table class="form-table">
165
				<tr valign="top">
166
				<th scope="row"><label for="podlove_subscribe_button_default_size"><?php _e('Size', 'podlove'); ?></label></th>
167
				<td>
168
					<select name="podlove_subscribe_button_default_size" id="podlove_subscribe_button_default_size">
169 View Code Duplication
						<?php foreach (\PodloveSubscribeButton\Model\Button::$size as $value => $description) : ?>
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...
170
							<option value="<?php echo $value; ?>" <?php echo ( $settings['size'] == $value ? "selected" : '' ); ?>><?php echo $description; ?></option>
171
						<?php endforeach; ?>
172
					</select>
173
				</td>
174
				</tr>
175
				<tr valign="top">
176
				<th scope="row"><label for="podlove_subscribe_button_default_autowidth"><?php _e('Autowidth', 'podlove'); ?></label></th>
177
				<td>
178
					<input type="checkbox" name="podlove_subscribe_button_default_autowidth" id="podlove_subscribe_button_default_autowidth" <?php echo ( $settings['autowidth'] == 'on' ? 'checked' : '' ) ?> />
179
				</td>
180
				</tr>
181
				<tr valign="top">
182
				<th scope="row"><label for="podlove_subscribe_button_default_color"><?php _e('Color', 'podlove'); ?></label></th>
183
				<td>
184
					<input id="podlove_subscribe_button_default_color" name="podlove_subscribe_button_default_color" class="podlove_subscribe_button_color" value="<?php echo $settings['color'] ?>" />
185
				</td>
186
				</tr>
187
				<tr valign="top">
188
				<th scope="row"><label for="podlove_subscribe_button_default_style"><?php _e('Style', 'podlove'); ?></label></th>
189
				<td>
190
					<select name="podlove_subscribe_button_default_style" id="podlove_subscribe_button_default_style">
191 View Code Duplication
						<?php foreach (\PodloveSubscribeButton\Model\Button::$style as $value => $description) : ?>
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...
192
							<option value="<?php echo $value; ?>" <?php echo ( $settings['style'] == $value ? "selected" : '' ); ?>><?php echo $description; ?></option>
193
						<?php endforeach; ?>
194
					</select>
195
				</td>
196
				</tr>
197
				<tr valign="top">
198
				<th scope="row"><label for="podlove_subscribe_button_default_format"><?php _e('Format', 'podlove'); ?></label></th>
199
				<td>
200
					<select name="podlove_subscribe_button_default_format" id="podlove_subscribe_button_default_format">
201 View Code Duplication
						<?php foreach (\PodloveSubscribeButton\Model\Button::$format as $value => $description) : ?>
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...
202
							<option value="<?php echo $value; ?>" <?php echo ( $settings['format'] == $value ? "selected" : '' ); ?>><?php echo $description; ?></option>
203
						<?php endforeach; ?>
204
					</select>
205
				</td>
206
				</tr>
207
			</table>
208
			<?php submit_button(); ?>
209
		</form>
210
		<?php
211
		endif;
212
	}
213
214
	private static function form_template( $button, $action ) {
215
		// Enqueue Scripts for Media Manager
216
		wp_enqueue_media();
217
		// Adjust if is_network
218
		$is_network = is_network_admin();
219
		?>
220
		<form method="post" action="<?php echo ( $is_network === TRUE ? '/wp-admin/network/settings' : 'options-general' ) ?>.php?page=podlove-subscribe-button&button=<?php echo $button->id; ?>&action=<?php echo $action; ?>&network=<?php echo $is_network; ?>">
221
			<input type="hidden" value="<?php echo $button->id; ?>" name="podlove_button[id]" />
222
			<table class="form-table" border="0" cellspacing="0">
223
					<tbody>
224
					<tr>
225
						<td scope="row">
226
							<label for="podlove_button_name"><?php _e('Button ID', 'podlove'); ?></label>
227
						</td>
228
						<td>
229
							<input type="text" class="regular-text" id="podlove_button_name" name="podlove_button[name]" value="<?php echo $button->name; ?>" />
230
							<br /><span class="description"><?php _e('The ID will be used as in internal identifier for shortcodes.', 'podlove'); ?></span>
231
						</td>
232
					</tr>
233
					<tr>
234
						<td scope="row">
235
							<label for="podlove_button_title"><?php _e('Podcast Title', 'podlove'); ?></label>
236
						</td>
237
						<td>
238
							<input type="text" class="regular-text" id="podlove_button_title" name="podlove_button[title]" value="<?php echo $button->title; ?>" />
239
						</td>
240
					</tr>
241
					<tr>
242
						<td scope="row">
243
							<label for="podlove_button_subtitle"><?php _e('Podcast Subtitle', 'podlove'); ?></label>
244
						</td>
245
						<td>
246
							<input type="text" class="regular-text" id="podlove_button_subtitle" name="podlove_button[subtitle]" value="<?php echo $button->subtitle; ?>" />
247
						</td>
248
					</tr>
249
					<tr>
250
						<td scope="row">
251
							<label for="podlove_button_description"><?php _e('Podcast Description', 'podlove'); ?></label>
252
						</td>
253
						<td>
254
							<textarea class="autogrow" cols="40" rows="3" id="podlove_button_description" name="podlove_button[description]"><?php echo $button->description; ?></textarea>
255
						</td>
256
					</tr>
257
					<tr>
258
						<td scope="row">
259
							<label for="podlove-button-cover"><?php _e('Podcast Image URL', 'podlove'); ?></label>
260
						</td>
261
						<td>
262
							<input type="text" class="regular-text" id="podlove-button-cover" name="podlove_button[cover]" value="<?php echo $button->cover; ?>" />
263
							<a id="Podlove_cover_image_select" class="button" href="#">Select</a>
264
							<br /><img src="<?php echo $button->cover; ?>" alt="" style="width: 200px" />
265
							<script type="text/javascript">
266
								(function($) {
267
									$("#podlove-button-cover").on( 'change', function() {
268
										url = $(this).val();
269
										$(this).parent().find("img").attr("src", url);
270
									} );
271
								})(jQuery);
272
							</script>
273
						</td>
274
					</tr>
275
					<tr>
276
						<td scope="row">
277
							<label for="feeds_table"><?php _e('Podcast Feeds', 'podlove'); ?></label>
278
						</td>
279
						<td>
280
							<table id="feeds_table" class="podlove_alternating" border="0" cellspacing="0">
281
								<thead>
282
									<tr>
283
										<th><?php _e('URL', 'podlove'); ?></th>
284
										<th><?php _e('iTunes feed ID', 'podlove'); ?></th>
285
										<th><?php _e('Media format', 'podlove'); ?></th>
286
										<th><?php _e('Actions', 'podlove'); ?></th>
287
									</tr>
288
								</thead>
289
								<tbody id="feeds_table_body">
290
								</tbody>
291
							</table>
292
							<input type="button" class="button add_feed" value="+" />
293
							<p><span class="description"><?php _e('Provide all Feeds with their corresponding Media File Type. The Subscribe Button will then automatically provide the most suitable feed to the subscriber with respect to their Podcast Client.', 'podlove'); ?></span></p>
294
						</td>
295
					</tr>
296
					</tbody>
297
				</table>
298
				<input name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'podlove'); ?>" type="submit" />
299
				<input type="submit" name="submit_and_stay" id="submit_and_stay" class="button" value="<?php _e('Save Changes and Continue Editing', 'podlove'); ?>"  />
300
301
				<script type="text/template" id="feed_line_template">
302
					<tr>
303
						<td>
304
							<input type="text" class="regular-text" name="podlove_button[feeds][{{id}}][url]" value="{{url}}" />
305
						</td>
306
						<td>
307
						<input type="text" class="regular-text" name="podlove_button[feeds][{{id}}][itunesfeedid]" value="{{itunesfeedid}}" />
308
						</td>
309
						<td>
310
							<select class="regular-text podlove-media-format" name="podlove_button[feeds][{{id}}][format]">
311
								<?php
312
									foreach (\PodloveSubscribeButton\MediaTypes::$audio as $id => $audio) {
313
										echo "<option value='".$id."'>".$audio['title']."</option>\n";
314
									}
315
								?>
316
							</select>
317
						</td>
318
						<td><i class="clickable podlove-icon-remove"></i></td>
319
					</tr>
320
				</script>
321
				<script type="text/javascript">
322
					var feeds = <?php echo json_encode($button->feeds); ?>;
323
				</script>
324
		</form>
325
		<?php
326
	}
327
328
	public static function get_action_link( $button, $title, $action = 'edit', $type = 'link' ) {
329
		return sprintf(
330
			'<a href="?page=%s&action=%s&button=%s&network='.is_network_admin().'"%s>' . $title . '</a>',
331
			filter_input(INPUT_GET, 'page'),
332
			$action,
333
			$button->id,
334
			$type == 'button' ? ' class="button"' : ''
335
		);
336
	}
337
338
}