1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* RE Pro Settings |
4
|
|
|
* |
5
|
|
|
* @package re-pro |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* REProSettings. |
10
|
|
|
*/ |
11
|
|
|
class REProSettings { |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* General settings. |
15
|
|
|
* |
16
|
|
|
* @var [Array] |
17
|
|
|
*/ |
18
|
|
|
private $general_settings; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Settings constructor. |
22
|
|
|
*/ |
23
|
|
|
public function __construct() { |
24
|
|
|
if ( is_admin() ) { |
25
|
|
|
$this->general_settings = get_option( 'repro_settings' ); |
26
|
|
|
|
27
|
|
|
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
28
|
|
|
add_action( 'admin_init', array( $this, 'settings_init' ) ); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add admin menu. |
34
|
|
|
*/ |
35
|
|
|
public function add_admin_menu() { |
36
|
|
|
add_options_page( __( 'Real Estate Pro' ), __( 'Real Estate Pro' ), 'manage_options', 're-pro-settings', array( $this, 'render_settings' ) ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Initialize settings fields and sections. |
41
|
|
|
*/ |
42
|
|
|
public function settings_init() { |
43
|
|
|
register_setting( 'repro_settings', 'repro_settings' ); |
44
|
|
|
|
45
|
|
|
// General Settings. |
46
|
|
|
add_settings_section( |
47
|
|
|
'repro_settings', |
48
|
|
|
__( 'General Settings', 're-pro' ), |
49
|
|
|
array( $this, 'general_callback' ), |
50
|
|
|
'repro_general_settings' |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
add_settings_field( |
54
|
|
|
'google_maps', |
55
|
|
|
__( 'Google Maps Module', 're-pro' ), |
56
|
|
|
array( $this, 'google_maps' ), |
57
|
|
|
'repro_general_settings', |
58
|
|
|
'repro_settings' |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
add_settings_field( |
62
|
|
|
'greatschools_apikey', |
63
|
|
|
__( 'Great Schools API Key', 're-pro' ), |
64
|
|
|
array( $this, 'greatschools_module' ), |
65
|
|
|
'repro_general_settings', |
66
|
|
|
'repro_settings' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
add_settings_field( |
70
|
|
|
'zillow_apikey', |
71
|
|
|
__( 'Zillow API Key', 're-pro' ), |
72
|
|
|
array( $this, 'zillow_module' ), |
73
|
|
|
'repro_general_settings', |
74
|
|
|
'repro_settings' |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
add_settings_field( |
78
|
|
|
'sa_apikey', |
79
|
|
|
__( 'Street Advisor API Key', 're-pro' ), |
80
|
|
|
array( $this, 'street_advisor' ), |
81
|
|
|
'repro_general_settings', |
82
|
|
|
'repro_settings' |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Feed section callback. |
89
|
|
|
*/ |
90
|
|
|
public function general_callback() { |
91
|
|
|
echo esc_attr( 'Activate the modules you would like to use.', 're-pro' ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Render Christies field. |
96
|
|
|
*/ |
97
|
|
|
public function google_maps() { |
98
|
|
|
$gmaps_active = isset( $this->general_settings['gmaps_active'] ) ? $this->general_settings['gmaps_active'] : '0'; |
99
|
|
|
$gmaps_key = isset( $this->general_settings['gmaps_key'] ) ? $this->general_settings['gmaps_key'] : ''; |
100
|
|
|
$gmaps_style = isset( $this->general_settings['gmaps_style'] ) ? $this->general_settings['gmaps_style'] : ''; |
101
|
|
|
$gmaps_zoom = isset( $this->general_settings['gmaps_zoom'] ) ? $this->general_settings['gmaps_zoom'] : ''; |
102
|
|
|
|
103
|
|
|
$checked = checked( '1', $gmaps_active, false ); |
104
|
|
|
$is_active = ( '1' === $gmaps_active ) ? 'Deactivate Google maps module?' : 'Activate Google maps module?'; |
105
|
|
|
$disable_zoom = checked( '1', $gmaps_zoom, false ); |
106
|
|
|
|
107
|
|
|
echo '<input style="vertical-align: top;" type="checkbox" name="repro_settings[gmaps_active]"' . esc_attr( $checked ) . ' value="1"> '; |
108
|
|
|
echo '<div style="display: inline-block;">'; |
109
|
|
|
esc_attr_e( $is_active ); |
110
|
|
|
echo '</div><br><br>'; |
111
|
|
|
|
112
|
|
|
echo '<input class="widefat" type="password" name="repro_settings[gmaps_key]" placeholder="Google maps API key" value="' . $gmaps_key . '">'; |
113
|
|
|
echo '<span class="description"> Enter your google maps javascript api key</span><br><br>'; |
114
|
|
|
|
115
|
|
|
echo '<textarea style="width:500px;" rows="10" cols="50" name="repro_settings[gmaps_style]">'; |
116
|
|
|
esc_attr_e( $gmaps_style ); |
117
|
|
|
echo '</textarea>'; |
118
|
|
|
echo '<br /><span class="description">Insert valid json to add custom style to maps. Json styles can be generated using <a target="_blank" href="https://snazzymaps.com/">Snazzy Maps</a> or the <a target="_blank" href="https://mapstyle.withgoogle.com/">Google Maps Styling Wizard</a></span><br><br>'; |
119
|
|
|
|
120
|
|
|
echo '<input style="vertical-align: top;" type="checkbox" name="repro_settings[gmaps_zoom]"' . esc_attr( $disable_zoom ) . ' value="1"> '; |
121
|
|
|
echo '<div style="display: inline-block;">'; |
122
|
|
|
echo 'Disable scroll zoom in Google Maps'; |
123
|
|
|
echo '</div>'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
public function zillow_module() { |
128
|
|
|
|
129
|
|
|
$zillow_apikey = isset( $this->general_settings['zillow_apikey'] ) ? $this->general_settings['zillow_apikey'] : ''; |
130
|
|
|
|
131
|
|
|
echo '<input class="widefat" type="text" name="repro_settings[zillow_apikey]" placeholder="Zillow API Key" value="' . $zillow_apikey . '">'; |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function street_advisor() { |
136
|
|
|
|
137
|
|
|
$sa_apikey = isset( $this->general_settings['sa_apikey'] ) ? $this->general_settings['sa_apikey'] : ''; |
138
|
|
|
|
139
|
|
|
echo '<input class="widefat" type="text" name="repro_settings[sa_apikey]" placeholder="Street Advisor API Key" value="' . $sa_apikey . '">'; |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* greatschools_module function. |
145
|
|
|
* |
146
|
|
|
* @access public |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
public function greatschools_module() { |
150
|
|
|
|
151
|
|
|
$greatschools_apikey = isset( $this->general_settings['greatschools_apikey'] ) ? $this->general_settings['greatschools_apikey'] : ''; |
152
|
|
|
|
153
|
|
|
echo '<input class="widefat" type="text" name="repro_settings[greatschools_apikey]" placeholder="Great Schools API Key" value="' . $greatschools_apikey . '">'; |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Render full settings page. |
159
|
|
|
*/ |
160
|
|
|
public function render_settings() { |
161
|
|
|
if ( ! current_user_can( 'manage_options' ) ) { |
162
|
|
|
wp_die( esc_attr( "You don't have access to this page", 're-pro' ) ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
echo '<div class="wrap">'; |
166
|
|
|
echo '<form method="post" action="options.php">'; |
167
|
|
|
echo '<h1>' . esc_attr( 'Real Estate Pro', 're-pro' ) . '</h1>'; |
168
|
|
|
|
169
|
|
|
settings_fields( 'repro_settings' ); |
170
|
|
|
do_settings_sections( 'repro_general_settings' ); |
171
|
|
|
submit_button(); |
172
|
|
|
|
173
|
|
|
echo '</form>'; |
174
|
|
|
echo '</div>'; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
new REProSettings(); |
179
|
|
|
|
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.