Skip to content

Commit 42b28fb

Browse files
committed
added debug option to settings, default is to disable debug messages
1 parent 995c4c1 commit 42b28fb

File tree

3 files changed

+60
-40
lines changed

3 files changed

+60
-40
lines changed

awpp.css

+8-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
color: black !important;
1414
}
1515

16-
.awpp_placemark h3 {}
17-
margin: 0 !important;
18-
padding: 5px 0;
19-
font-size: medium !important;
20-
line-height: normal !important;
21-
font-weight: normal !important;
22-
}
16+
.awpp_placemark h3 {
17+
margin: 0 !important;
18+
padding: 5px 0;
19+
font-size: medium !important;
20+
line-height: normal !important;
21+
font-weight: normal !important;
22+
}
2323

2424
.awpp_placemark div {
2525
margin: 0 !important;
@@ -57,6 +57,7 @@
5757
.awpp_address {
5858
float:left;
5959
padding-left:1em;
60+
width: 15em;
6061
}
6162

6263
.awpp_photo_block {

awpp.php

+25-18
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ class AWPP_Init {
2525

2626

2727
public function __construct(){
28-
// Admin menu
29-
add_action('admin_init', array( 'AWPP_Settings', 'admin_init'));
30-
add_action('admin_menu', array( 'AWPP_Settings', 'add_menu'));
28+
// Admin menu id built and populated in AWPP_Settings
29+
$awpps = new AWPP_Settings();
30+
add_action('admin_init', array( $awpps, 'admin_init'));
31+
add_action('admin_menu', array( $awpps, 'add_menu'));
3132

3233
// Always? Cf. callback function comment
3334
//add_action('wp_enqueue_scripts', array( &$this, 'loadResources'), 11); // that action doesn't seem to work
@@ -94,15 +95,18 @@ public function loadResources() {
9495
class AWPP_Shortcode {
9596

9697
const PREFIX = 'awpp';
98+
private $_options;
99+
private $_debug;
97100

98101
public function __construct(){
102+
$this->_options = get_option('awpp_options');
103+
$this->_debug = $this->_options['debug_enable'];
99104

100105
}
101106
/*
102107
* [annuaire-map] shortcode with region, type and content parameters
103108
*/
104109
public function create_annuaire_map( $attributes ){
105-
$options = get_option('awpp_options');
106110
$geoData = null;
107111

108112
extract(
@@ -111,9 +115,9 @@ public function create_annuaire_map( $attributes ){
111115
'region' => 'north',
112116
'type' => 1,
113117
'content' => 'structure',
114-
'width' => $options['map_width'],
115-
'height' => $options['map_height'],
116-
'center' => $options['map_center']
118+
'width' => $this->_options['map_width'],
119+
'height' => $this->_options['map_height'],
120+
'center' => $this->_options['map_center']
117121
),
118122
$attributes )
119123
);
@@ -134,7 +138,6 @@ public function create_annuaire_map( $attributes ){
134138
* [annuaire] shortcode with region, type and content parameters
135139
*/
136140
public function create_annuaire_list( $attributes ){
137-
$options = get_option('awpp_options');
138141
$geoData = null;
139142

140143
extract(
@@ -144,9 +147,9 @@ public function create_annuaire_list( $attributes ){
144147
'type' => 1,
145148
'content' => 'structure',
146149
'map' => false,
147-
'width' => $options['map_width'],
148-
'height' => $options['map_height'],
149-
'center' => $options['map_center'],
150+
'width' => $this->_options['map_width'],
151+
'height' => $this->_options['map_height'],
152+
'center' => $this->_options['map_center'],
150153
'photos' => true,
151154
'limit' => 100
152155
),
@@ -242,7 +245,7 @@ private function _encodeAddress( $name, $data ) {
242245
* Embedd the DOM element that will be replaced by the actual map
243246
*/
244247
private function _displayMap( $geoData, $center, $width, $height ){
245-
if ( empty( $geoData ) ) {
248+
if ( $this->_debug && empty( $geoData ) ) {
246249
return sprintf('Something went incredibly wrong: No geodata received');
247250
}
248251

@@ -306,10 +309,10 @@ private function _googleGeocode( $address ) {
306309
str_replace( ' ', '+', $address )
307310
)
308311
);
309-
if ( is_wp_error( $geocodeResponse )) {
312+
if ( $this->_debug && is_wp_error( $geocodeResponse )) {
310313
print('Something went wrong:' . $geocodeResponse->get_error_message());
311314
}
312-
if( $geocodeResponse[ 'response' ][ 'code' ] != 200 ) {
315+
if( $this->_debug && $geocodeResponse[ 'response' ][ 'code' ] != 200 ) {
313316
printf(
314317
__( '<p>%s geocode error: %d %s</p> <p>Response: %s</p>', self::PREFIX ),
315318
self::PREFIX,
@@ -322,17 +325,21 @@ private function _googleGeocode( $address ) {
322325

323326
// Else decode response and handle geocoding related errors
324327
$coordinates = json_decode( $geocodeResponse['body'] );
325-
if( json_last_error() != JSON_ERROR_NONE ) {
328+
if( $this->_debug && json_last_error() != JSON_ERROR_NONE ) {
326329
print('Did not get valid json response');
327330
}
328331

329-
if( isset( $coordinates->status ) && $coordinates->status == 'REQUEST_DENIED' ) {
332+
if( $this->_debug && isset( $coordinates->status ) && $coordinates->status == 'REQUEST_DENIED' ) {
330333
printf( __( '%s geocode error: Request Denied.', self::PREFIX), self::PREFIX );
331334
return false;
332335
}
333336

334-
if( !isset( $coordinates->results ) || empty( $coordinates->results ) ) {
335-
print( __( "That address couldn't be geocoded, please make sure that it's correct.", self::PREFIX ) );
337+
if( ( $this->_debug && !isset( $coordinates->results ) ) || ($this->_debug && empty( $coordinates->results ) ) ) {
338+
printf(
339+
__( "%s geocode error: The address (%s) couldn't be geocoded, please make sure that it's correct." ),
340+
self::PREFIX,
341+
$address
342+
);
336343
return false;
337344
}
338345

settings.php

+27-15
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
class AWPP_Settings {
1717

18+
private $_options;
19+
1820
public function __construct(){
19-
// nothing yet
21+
$this->_options = get_option('awpp_options');
2022
}
2123

2224
public function add_menu(){
@@ -25,13 +27,16 @@ public function add_menu(){
2527

2628
public function admin_init(){
2729
register_setting('awpp-group', 'awpp_options');
28-
add_settings_section('awpp_main', 'Main Settings', array(&$this, 'awpp_section_text'), 'awpp');
29-
add_settings_field('awpp_photo_width', 'Default maximum photo width (px)', array( 'AWPP_Settings', 'awpp_photo_width_input'), 'awpp', 'awpp_main');
30+
add_settings_section('awpp_main', 'Main Settings', array( $this, 'awpp_section_text'), 'awpp');
31+
add_settings_field('awpp_photo_width', 'Default maximum photo width (px)', array( $this, 'awpp_photo_width_input'), 'awpp', 'awpp_main');
3032

31-
add_settings_section('awpp_map', 'Map Settings', array(&$this, 'awpp_section_map_text'), 'awpp');
32-
add_settings_field('awpp_map_width', 'Default map width (px)', array( 'AWPP_Settings', 'awpp_map_width_input'), 'awpp', 'awpp_map');
33-
add_settings_field('awpp_map_height', 'Default map height (px)', array( 'AWPP_Settings', 'awpp_map_height_input'), 'awpp', 'awpp_map');
34-
add_settings_field('awpp_map_center', 'Center map here', array( 'AWPP_Settings', 'awpp_map_center_input'), 'awpp', 'awpp_map');
33+
add_settings_section('awpp_map', 'Map Settings', array($this, 'awpp_section_map_text'), 'awpp');
34+
add_settings_field('awpp_map_width', 'Default map width (px)', array( $this, 'awpp_map_width_input'), 'awpp', 'awpp_map');
35+
add_settings_field('awpp_map_height', 'Default map height (px)', array( $this, 'awpp_map_height_input'), 'awpp', 'awpp_map');
36+
add_settings_field('awpp_map_center', 'Center map here', array( $this, 'awpp_map_center_input'), 'awpp', 'awpp_map');
37+
38+
add_settings_section('awpp_debug', 'Debugging', array( $this, 'awpp_section_debug_text'), 'awpp');
39+
add_settings_field('awpp_debug_enable', 'Enable debugging', array( $this, 'awpp_debug_checkbox'), 'awpp', 'awpp_debug');
3540
}
3641

3742
public function awpp_section_text(){
@@ -42,32 +47,39 @@ public function awpp_section_map_text(){
4247
print('<p>Define your default map settings here.</p>');
4348
}
4449

50+
public function awpp_section_debug_text() {
51+
print('<p>If you enable debugging, AWPP will output debugging information on the pages where you use shortcodes or widgets.</p>');
52+
}
53+
54+
public function awpp_debug_checkbox(){
55+
$checked = ( $this->_options['debug_enable'] ) ? 'checked="checked"' : '';
56+
print('<input type="checkbox" name="awpp_options[debug_enable]"'
57+
. 'id="awpp_debug_enable_checkbox"'
58+
. 'value="1" ' . $checked . '/>');
59+
}
60+
4561
public function awpp_photo_width_input(){
46-
$options = get_option('awpp_options');
4762
print('<input type="number" step="10" min="0" id="awpp_photo_width_input"'
4863
. 'name="awpp_options[photo_width]" size="40" class="small-text"'
49-
. 'value="' . $options['photo_width'] . '"/>');
64+
. 'value="' . $this->_options['photo_width'] . '"/>');
5065
}
5166

5267
public function awpp_map_width_input(){
53-
$options = get_option('awpp_options');
5468
print('<input type="number" step="10" min="0" id="awpp_map_width_input"'
5569
. 'name="awpp_options[map_width]" size="40" class="small-text"'
56-
. 'value="' . $options['map_width'] . '"/>');
70+
. 'value="' . $this->_options['map_width'] . '"/>');
5771
}
5872

5973
public function awpp_map_height_input(){
60-
$options = get_option('awpp_options');
6174
print('<input type="number" step="10" min="0" id="awpp_map_height_input"'
6275
. 'name="awpp_options[map_height]" size="40" class="small-text"'
63-
. 'value="' . $options['map_height'] . '"/>');
76+
. 'value="' . $this->_options['map_height'] . '"/>');
6477
}
6578

6679
public function awpp_map_center_input(){
67-
$options = get_option('awpp_options');
6880
print('<input type="text" step="10" min="0" id="awpp_map_center_input"'
6981
. 'name="awpp_options[map_center]" size="40" class="medium-text"'
70-
. 'value="' . $options['map_center'] . '"/>');
82+
. 'value="' . $this->_options['map_center'] . '"/>');
7183
}
7284

7385
/*

0 commit comments

Comments
 (0)