Feeds::add_sections()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Feeds Settings Page
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Admin\Pages;
8
9
use SimpleCalendar\Abstracts\Feed;
10
use SimpleCalendar\Abstracts\Admin_Page;
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
/**
17
 * Feeds settings.
18
 *
19
 * Handles calendar feeds settings and outputs the settings page markup.
20
 *
21
 * @since 3.0.0
22
 */
23
class Feeds extends Admin_Page {
24
25
	/**
26
	 * Feed types.
27
	 *
28
	 * @access private
29
	 * @var array
30
	 */
31
	private $feed_types = array();
32
33
	/**
34
	 * Constructor.
35
	 *
36
	 * @since 3.0.0
37
	 */
38 View Code Duplication
	public function __construct() {
0 ignored issues
show
Duplication introduced by
This method 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...
39
40
		$this->id           = 'feeds';
41
		$this->option_group = 'settings';
42
		$this->label        = __( 'Event Sources', 'google-calendar-events' );
43
		//$this->description  = __( 'Manage calendar event sources settings.', 'google-calendar-events' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
45
		$feeds_settings = array();
46
		$feeds = simcal_get_feed_types();
47
		if ( ! empty( $feeds ) && is_array( $feeds ) ) {
48
			foreach ( $feeds as $feed ) {
49
50
				$feed_type = simcal_get_feed( $feed );
51
52
				if ( $feed_type instanceof Feed ) {
53
					$settings = $feed_type->settings_fields();
54
					if ( ! empty( $settings ) ) {
55
						$feeds_settings[ $feed ] = $settings;
56
					}
57
				}
58
			}
59
		}
60
61
		$this->feed_types = $feeds_settings;
62
		$this->sections   = $this->add_sections();
63
		$this->fields     = $this->add_fields();
64
	}
65
66
	/**
67
	 * Add sections.
68
	 *
69
	 * @since  3.0.0
70
	 *
71
	 * @return array
72
	 */
73
	public function add_sections() {
74
75
		$sections = array();
76
77
		foreach ( $this->feed_types as $feed_type => $type ) {
78
79
			$sections[ $feed_type ] = array(
80
				'title'       => $type['name'],
81
				'description' => $type['description'],
82
			);
83
84
		}
85
86
		arsort( $sections );
87
88
		return apply_filters( 'simcal_add_' . $this->option_group . '_' . $this->id .'_sections', $sections );
89
	}
90
91
	/**
92
	 * Add fields.
93
	 *
94
	 * @since  3.0.0
95
	 *
96
	 * @return array
97
	 */
98
	public function add_fields() {
99
100
		$fields       = array();
101
		$feed_types   = $this->feed_types;
102
		$this->values = get_option( 'simple-calendar_' . $this->option_group . '_' . $this->id );
103
104
		foreach ( $this->sections as $type => $contents ) :
105
106 View Code Duplication
			if ( isset( $feed_types[ $type ]['fields'] ) ) {
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...
107
				foreach ( $feed_types[ $type ]['fields'] as $key => $args ) {
108
109
					$fields[ $type ][] = array_merge( $args, array(
110
						'name'  => 'simple-calendar_' . $this->option_group . '_' . $this->id . '[' . $type . '][' . $key . ']',
111
						'id'    => 'simple-calendar-' . $this->option_group . '-' . $this->id . '-' . $type . '-' . $key,
112
						'value' => $this->get_option_value( $type, $key )
113
					) );
114
115
				}
116
			}
117
118
		endforeach;
119
120
		return apply_filters( 'simcal_add_' . $this->option_group . '_' . $this->id . '_fields', $fields );
121
	}
122
123
}
124