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() { |
|
|
|
|
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' ); |
|
|
|
|
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'] ) ) { |
|
|
|
|
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
|
|
|
|
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.