Grouped_Calendars_Admin::add_settings_meta_panel()   D
last analyzed

Complexity

Conditions 12
Paths 128

Size

Total Lines 90
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 53
nc 128
nop 1
dl 0
loc 90
rs 4.6933
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Grouped Calendars Feed - Admin
4
 *
5
 * @package SimpleCalendar/Feeds
6
 */
7
namespace SimpleCalendar\Feeds\Admin;
8
9
use SimpleCalendar\Feeds\Grouped_Calendars;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Grouped calendars feed admin.
17
 *
18
 * @since 3.0.0
19
 */
20
class Grouped_Calendars_Admin {
21
22
	/**
23
	 * Grouped Calendars feed object.
24
	 *
25
	 * @access private
26
	 * @var Grouped_Calendars
27
	 */
28
	private $feed = null;
29
30
	/**
31
	 * Hook in tabs.
32
	 *
33
	 * @since 3.0.0
34
	 *
35
	 * @param Grouped_Calendars $feed
36
	 */
37
	public function __construct( Grouped_Calendars $feed ) {
38
39
		$this->feed = $feed;
40
41 View Code Duplication
		if ( 'calendar' == simcal_is_admin_screen() ) {
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...
42
			add_filter( 'simcal_settings_meta_tabs_li', array( $this, 'add_settings_meta_tab_li' ), 10, 1 );
43
			add_action( 'simcal_settings_meta_panels', array( $this, 'add_settings_meta_panel' ), 10, 1 );
44
		}
45
		add_action( 'simcal_process_settings_meta', array( $this, 'process_meta' ), 10, 1 );
46
	}
47
48
	/**
49
	 * Add a tab to the settings meta box.
50
	 *
51
	 * @since  3.0.0
52
	 *
53
	 * @param  array $tabs
54
	 *
55
	 * @return array
56
	 */
57
	public function add_settings_meta_tab_li( $tabs ) {
58
		return array_merge( $tabs, array(
59
			'grouped-calendars' => array(
60
				'label'   => $this->feed->name,
61
				'target'  => 'grouped-calendars-settings-panel',
62
				'class'   => array(
63
					'simcal-feed-type',
64
					'simcal-feed-type-grouped-calendars',
65
				),
66
				'icon'    => 'simcal-icon-docs',
67
			),
68
		));
69
	}
70
71
	/**
72
	 * Add a panel to the settings meta box.
73
	 *
74
	 * @since 3.0.0
75
	 *
76
	 * @param int $post_id
77
	 */
78
	public function add_settings_meta_panel( $post_id ) {
79
80
		?>
81
		<div id="grouped-calendars-settings-panel" class="simcal-panel">
82
			<table>
83
				<thead>
84
				<tr><th colspan="2"><?php _e( 'Grouped Calendar Settings', 'google-calendar-events' ); ?></th></tr>
85
				</thead>
86
				<tbody class="simcal-panel-section">
87
				<tr class="simcal-panel-field">
88
					<th><label for="_grouped_calendars_source"><?php _e( 'Get Calendars From', 'google-calendar-events' ); ?></label></th>
89
					<td>
90
						<?php
91
92
						$source = esc_attr( get_post_meta( $post_id, '_grouped_calendars_source', true ) );
93
						$source = empty( $source ) ? 'ids' : $source;
94
95
						?>
96
						<select name="_grouped_calendars_source"
97
						        id="_grouped_calendars_source"
98
						        class="simcal-field simcal-field-select simcal-field-inline simcal-field-show-other"
99
						        data-show-field-on-choice="true">
100
							<option value="ids" data-show-field="_grouped_calendars_ids" <?php selected( 'ids', $source, true ); ?>><?php _e( 'Manual selection', 'google-calendar-events' ); ?></option>
101
							<option value="category" data-show-field="_grouped_calendars_category" <?php selected( 'category', $source, true ); ?>><?php _e( 'Category', 'google-calendar-events' ); ?></option>
102
						</select>
103
						<i class="simcal-icon-help simcal-help-tip" data-tip="<?php _e( 'Choose from which calendar feeds you want to get events from. Choose them individually or select all those belonging to calendar feed categories.', 'google-calendar-events' ); ?>"></i>
104
						<br><br>
105
						<?php
106
107
						$cals = simcal_get_calendars( $post_id );
108
						$meta = get_post_meta( $post_id, '_grouped_calendars_ids', true );
109
						$ids  = $meta && is_array( $meta ) ? implode( ',', array_map( 'absint', $meta ) ) : absint( $meta );
110
111
						simcal_print_field( array(
112
							'type'        => 'select',
113
							'multiselect' => 'multiselect',
114
							'name'        => '_grouped_calendars_ids',
115
							'id'          => '_grouped_calendars_ids',
116
							'value'       => $ids !== 0 ? $ids : '',
117
							'options'     => $cals,
118
							'enhanced'    => 'enhanced',
119
							'style'       => 'ids' == $source ? '' : array( 'display' => 'none' ),
120
							'attributes'  => array(
121
								'data-noresults' => __( 'No results found.', 'google-calendar-events' ),
122
							),
123
						));
124
125
						$meta = get_post_meta( $post_id, '_grouped_calendars_category', true );
126
						$category = $meta && is_array( $meta ) ? implode( ',', array_map( 'absint', $meta ) ): '';
127
128
						$terms = get_terms( 'calendar_category' );
129
130
						if ( ! empty( $terms ) ) {
131
132
							$categories = array();
133
							foreach ( $terms as $term ) {
134
								$categories[ $term->term_id ] = $term->name;
135
							}
136
137
							simcal_print_field( array(
138
								'type'        => 'select',
139
								'multiselect' => 'multiselect',
140
								'name'        => '_grouped_calendars_category',
141
								'id'          => '_grouped_calendars_category',
142
								'value'       => $category,
143
								'options'     => $categories,
144
								'enhanced'    => 'enhanced',
145
								'style'       => 'category' == $source ? '' : array( 'display' => 'none' ),
146
								'attributes'  => array(
147
									'data-noresults' => __( 'No results found.', 'google-calendar-events' ),
148
								),
149
							) );
150
151
						} else {
152
153
							$style = 'category' == $source ? '' : 'display: none;';
154
							$style .= ' width: 100%; max-width: 500px';
155
							echo '<input type="text" disabled="disabled" name="_grouped_calendars_category" id="_grouped_calendars_category" style="' . $style . '" placeholder="' . __( 'There are no calendar categories yet.', 'google-calendar-events' ) . '" />';
156
157
						}
158
159
						?>
160
					</td>
161
				</tr>
162
				</tbody>
163
			</table>
164
		</div>
165
		<?php
166
167
	}
168
169
	/**
170
	 * Process meta fields.
171
	 *
172
	 * @since 3.0.0
173
	 *
174
	 * @param int $post_id
175
	 */
176
	public function process_meta( $post_id ) {
0 ignored issues
show
Coding Style introduced by
process_meta 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...
177
178
		$source = isset( $_POST['_grouped_calendars_source'] ) ? sanitize_key( $_POST['_grouped_calendars_source'] ) : 'ids';
179
		update_post_meta( $post_id, '_grouped_calendars_source', $source );
180
181
		$ids = isset( $_POST['_grouped_calendars_ids'] ) ? array_map( 'absint', $_POST['_grouped_calendars_ids'] ) : '';
182
		update_post_meta( $post_id, '_grouped_calendars_ids', $ids );
183
184
		$category = isset( $_POST['_grouped_calendars_category'] ) ? array_map( 'absint', $_POST['_grouped_calendars_category'] ) : '';
185
		update_post_meta( $post_id, '_grouped_calendars_category', $category );
186
187
	}
188
189
}
190