Completed
Push — issues/1409 ( f73807...576502 )
by Ravinder
19:00
created

Give_Settings_Page::output_sections()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 6
nop 0
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Give Settings Page/Tab
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Settings_Page
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.8
10
 */
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit; // Exit if accessed directly
14
}
15
16
if ( ! class_exists( 'Give_Settings_Page' ) ) :
17
18
	/**
19
	 * Give_Settings_Page.
20
	 *
21
	 * @sine 1.8
22
	 */
23
	class Give_Settings_Page {
24
25
		/**
26
		 * Setting page id.
27
		 *
28
		 * @since 1.8
29
		 * @var   string
30
		 */
31
		protected $id = '';
32
33
		/**
34
		 * Setting page label.
35
		 *
36
		 * @since 1.8
37
		 * @var   string
38
		 */
39
		protected $label = '';
40
41
42
		/**
43
		 * Default tab.
44
		 *
45
		 * @since 1.8
46
		 * @var   string
47
		 */
48
		protected $default_tab = '';
49
50
		/**
51
		 * Current setting page.
52
		 *
53
		 * @since 1.8
54
		 * @var   string|null
55
		 */
56
		private $current_setting_page = null;
57
58
		/**
59
		 * Constructor.
60
		 */
61
		public function __construct() {
62
			// Get current setting page.
63
			$this->current_setting_page = give_get_current_setting_page();
64
65
			add_filter( "give_default_setting_tab_section_{$this->id}", array( $this, 'set_default_setting_tab' ), 10 );
66
			add_filter( "{$this->current_setting_page}_tabs_array", array( $this, 'add_settings_page' ), 20 );
67
			add_action( "{$this->current_setting_page}_sections_{$this->id}_page", array( $this, 'output_sections' ) );
68
			add_action( "{$this->current_setting_page}_settings_{$this->id}_page", array( $this, 'output' ) );
69
			add_action( "{$this->current_setting_page}_save_{$this->id}", array( $this, 'save' ) );
70
		}
71
72
		/**
73
		 * Default setting tab.
74
		 *
75
		 * @since  1.8
76
		 * @param  $setting_tab
77
		 * @return string
78
		 */
79
		function set_default_setting_tab( $setting_tab ) {
0 ignored issues
show
Unused Code introduced by
The parameter $setting_tab is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
			return $this->default_tab;
81
		}
82
83
		/**
84
		 * Add this page to settings.
85
		 *
86
		 * @since  1.8
87
		 * @param  array $pages Lst of pages.
88
		 * @return array
89
		 */
90
		public function add_settings_page( $pages ) {
91
			$pages[ $this->id ] = $this->label;
92
93
			return $pages;
94
		}
95
96
		/**
97
		 * Get settings array.
98
		 *
99
		 * @since  1.8
100
		 * @return array
101
		 */
102
		public function get_settings() {
103
			/**
104
			 * Filter the settings.
105
			 *
106
			 * @since  1.8
107
			 * @param  array $settings
108
			 */
109
			$settings = apply_filters( 'give_get_settings_' . $this->id, array() );
110
111
			// Output.
112
			return $settings;
113
		}
114
115
		/**
116
		 * Get sections.
117
		 *
118
		 * @since 1.8
119
		 * @return array
120
		 */
121
		public function get_sections() {
122
			return apply_filters( 'give_get_sections_' . $this->id, array() );
123
		}
124
125
		/**
126
		 * Output sections.
127
		 *
128
		 * @since  1.8
129
		 * @return void
130
		 */
131
		public function output_sections() {
132
			//Get current section.
133
			$current_section = give_get_current_setting_section();
134
135
			//Get all sections.
136
			$sections = $this->get_sections();
137
138
			//Show section settings only if setting section exist.
139
			if( ! in_array( $current_section , array_keys( $sections ) ) ) {
140
				echo '<div class="error"><p>' . __( 'Oops, this settings page does not exist.', 'give' ) . '</p></div>';
141
				return;
142
			}
143
144
			//Bailout.
145
			if ( empty( $sections ) ) {
146
				return;
147
			}
148
149
			if( is_null( $this->current_setting_page ) ) {
150
				$this->current_setting_page = give_get_current_setting_page();
151
			}
152
153
			echo '<ul class="subsubsub">';
154
155
			// Get section keys.
156
			$array_keys = array_keys( $sections );
157
158
			foreach ( $sections as $id => $label ) {
159
				echo '<li><a href="' . admin_url( 'edit.php?post_type=give_forms&page=' . $this->current_setting_page . '&tab=' . $this->id . '&section=' . sanitize_title( $id ) ) . '" class="' . ( $current_section == $id ? 'current' : '' ) . '">' . $label . '</a> ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
160
			}
161
162
			echo '</ul><br class="clear" /><hr>';
163
		}
164
165
		/**
166
		 * Output the settings.
167
		 *
168
		 * @since  1.8
169
		 * @return void
170
		 */
171
		public function output() {
172
			$settings = $this->get_settings();
173
174
			Give_Admin_Settings::output_fields( $settings, 'give_settings' );
175
		}
176
177
		/**
178
		 * Save settings.
179
		 *
180
		 * @since  1.8
181
		 * @return void
182
		 */
183
		public function save() {
184
			$settings = $this->get_settings();
185
			$current_section = give_get_current_setting_section();
186
187
			Give_Admin_Settings::save_fields( $settings, 'give_settings' );
188
189
			/**
190
			 * Trigger Action
191
			 *
192
			 * @since 1.8
193
			 */
194
			do_action( 'give_update_options_' . $this->id . '_' . $current_section );
195
		}
196
	}
197
198
endif;
199