Skip to content

Commit 995c4c1

Browse files
committed
sorted out some code, split of the initialization and widget classes, started implementing widget functionality
1 parent bd4136e commit 995c4c1

File tree

3 files changed

+182
-112
lines changed

3 files changed

+182
-112
lines changed

awpp.php

+75-107
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,34 @@
1313
if( $_SERVER['SCRIPT_FILENAME'] == __FILE__ )
1414
die( 'Access denied.' );
1515

16-
17-
define('WP_DEBUG', true);
18-
1916
require_once( dirname(__FILE__) . '/settings.php' );
17+
require_once( dirname(__FILE__) . '/widget.php' );
2018

2119
/**
2220
* Description of awpp
2321
*
2422
* @author kwisatz
2523
*/
26-
class Awpp {
27-
28-
const PREFIX = 'awpp';
29-
24+
class AWPP_Init {
25+
26+
3027
public function __construct(){
3128
// Admin menu
3229
add_action('admin_init', array( 'AWPP_Settings', 'admin_init'));
33-
add_action('admin_menu', array( &$this, 'add_menu'));
30+
add_action('admin_menu', array( 'AWPP_Settings', 'add_menu'));
3431

3532
// Always? Cf. callback function comment
3633
//add_action('wp_enqueue_scripts', array( &$this, 'loadResources'), 11); // that action doesn't seem to work
3734
add_action('wp', array( &$this, 'loadResources'), 11);
3835
add_action('wp_head', array( &$this, 'outputHead' ) );
3936

4037
// Widget
41-
add_action('widgets_init', array(&$this, 'register_awpp_widget'));
38+
add_action('widgets_init', array('AWPP_Widget', 'register_awpp_widget'));
4239

4340
// Shortcodes http://codex.wordpress.org/Shortcode_API
44-
add_shortcode( 'annuaire', array(&$this, 'create_annuaire_list') );
45-
add_shortcode( 'annuaire-map', array( &$this, 'create_annuaire_map') );
41+
$awpp_sc = new AWPP_Shortcode;
42+
add_shortcode( 'annuaire', array( $awpp_sc, 'create_annuaire_list') );
43+
add_shortcode( 'annuaire-map', array( $awpp_sc, 'create_annuaire_map') );
4644
}
4745

4846
public function activate(){
@@ -57,13 +55,56 @@ public function outputHead(){
5755
print('<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />');
5856
}
5957

58+
/**
59+
* Register and load javascript and stylesheet information
60+
* @link http://codex.wordpress.org/Function_Reference/wp_register_script
61+
*/
62+
public function loadResources() {
63+
wp_register_script(
64+
'googleMapsAPI', // handle
65+
'http'. ( is_ssl() ? 's' : '' ) .'://maps.google.com/maps/api/js?key=AIzaSyD2s8HfAbcc2uaoZ1Nuf8zSwOV9sg5kONI&sensor=false', // src
66+
array(), // deps
67+
false, // ver
68+
true // in_footer
69+
);
70+
wp_register_script(
71+
'awppScript', // handle
72+
plugins_url('awpp.js', __FILE__), // src
73+
array( 'googleMapsAPI', 'jquery' ), // deps
74+
false, // ver
75+
true // in_footer
76+
);
77+
wp_register_style(
78+
'awppStyle', // handle
79+
plugins_url('awpp.css', __FILE__), // src
80+
array(), // deps
81+
false, // ver
82+
'all' // media
83+
);
84+
85+
// TODO only load when shortcode has been parsed
86+
wp_enqueue_script( 'googleMapsAPI' );
87+
wp_enqueue_script( 'awppScript' );
88+
89+
wp_enqueue_style( 'awppStyle' );
90+
}
91+
92+
}
93+
94+
class AWPP_Shortcode {
95+
96+
const PREFIX = 'awpp';
97+
98+
public function __construct(){
99+
100+
}
60101
/*
61102
* [annuaire-map] shortcode with region, type and content parameters
62103
*/
63104
public function create_annuaire_map( $attributes ){
64105
$options = get_option('awpp_options');
65106
$geoData = null;
66-
107+
67108
extract(
68109
shortcode_atts(
69110
array(
@@ -105,15 +146,22 @@ public function create_annuaire_list( $attributes ){
105146
'map' => false,
106147
'width' => $options['map_width'],
107148
'height' => $options['map_height'],
108-
'center' => $options['map_center']
149+
'center' => $options['map_center'],
150+
'photos' => true,
151+
'limit' => 100
109152
),
110153
$attributes )
111154
);
112155

113156
$data = $this->_getDataFromServer( $region, $type, $content );
114157
$output = '<div class="wrap"><div id="awpp_addresses">';
115-
158+
$count = 0;
159+
116160
foreach( $data as $entry ) {
161+
162+
if( ++$count > $limit ) {
163+
continue;
164+
}
117165

118166
$title = strip_tags( $entry->titre );
119167

@@ -128,8 +176,14 @@ public function create_annuaire_list( $attributes ){
128176
$output .= ( $entry->fax ) ? '<br/>Fax: ' . $entry->fax : '';
129177
$output .= ( $entry->link ) ? '<br/><a href="' . $entry->link . '" target="_blank">Homepage</a>' : '';
130178
$output .= '</div>';
131-
132-
if( is_array( $entry->photo ) && !empty( $entry->photo[0] ) ) {
179+
180+
/*
181+
* Only display photos if
182+
* 1) they have been requested,
183+
* 2) there is an array of photos and
184+
* 3) the first element isn't empty
185+
*/
186+
if( $photos && is_array( $entry->photo ) && !empty( $entry->photo[0] ) ) {
133187
$options = get_option('awpp_options');
134188
$photo = trim( strstr( $entry->photo[0], 'http') );
135189
$output .= '<div class="awpp_photo_block">'
@@ -246,41 +300,7 @@ private function _getMapPlacemarks( $geoData ){
246300
return $placemarks;
247301
}
248302

249-
/**
250-
* Register and load javascript and stylesheet information
251-
* @link http://codex.wordpress.org/Function_Reference/wp_register_script
252-
*/
253-
public function loadResources() {
254-
wp_register_script(
255-
'googleMapsAPI', // handle
256-
'http'. ( is_ssl() ? 's' : '' ) .'://maps.google.com/maps/api/js?key=AIzaSyD2s8HfAbcc2uaoZ1Nuf8zSwOV9sg5kONI&sensor=false', // src
257-
array(), // deps
258-
false, // ver
259-
true // in_footer
260-
);
261-
wp_register_script(
262-
'awppScript', // handle
263-
plugins_url('awpp.js', __FILE__), // src
264-
array( 'googleMapsAPI', 'jquery' ), // deps
265-
false, // ver
266-
true // in_footer
267-
);
268-
wp_register_style(
269-
'awppStyle', // handle
270-
plugins_url('awpp.css', __FILE__), // src
271-
array(), // deps
272-
false, // ver
273-
'all' // media
274-
);
275-
276-
// TODO only load when shortcode has been parsed
277-
wp_enqueue_script( 'googleMapsAPI' );
278-
wp_enqueue_script( 'awppScript' );
279-
280-
wp_enqueue_style( 'awppStyle' );
281-
}
282-
283-
private function _googleGeocode( $address ) {
303+
private function _googleGeocode( $address ) {
284304
$geocodeResponse = wp_remote_get(
285305
sprintf( 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false',
286306
str_replace( ' ', '+', $address )
@@ -320,65 +340,13 @@ private function _googleGeocode( $address ) {
320340
return array( 'latitude' => $coordinates->results[ 0 ]->geometry->location->lat,
321341
'longitude' => $coordinates->results[ 0 ]->geometry->location->lng );
322342
}
323-
324-
/**
325-
* Register the widget class
326-
*/
327-
public function register_awpp_widget(){
328-
register_widget('Awpp_Widget');
329-
}
330-
331-
public function add_menu(){
332-
add_options_page('Annuaire Client Settings', 'Annuaire Client', 'manage_options', 'awpp', array('AWPP_Settings', 'plugin_settings_page'));
333-
}
334-
}
335-
336-
class Awpp_Widget extends WP_Widget {
337-
338-
public function __construct() {
339-
parent::__construct(
340-
'awpp_widget', // Base ID
341-
'Awpp Widget', // Name
342-
array('description' => __('Awpp widget') ) // Args
343-
);
344-
}
345-
346-
public function form( $instance ){
347-
if ( isset( $instance[ 'title' ] ) ) {
348-
$title = $instance[ 'title' ];
349-
} else {
350-
$title = __( 'New title', 'text_domain' );
351-
}
352-
print('<p><label for="' . $this->get_field_id('title') . '"' . _e( "Title") . '</label>');
353-
print('<input class="widefat" id="' . $this->get_field_id( 'title' )
354-
. '" name="' . $this->get_field_name( 'title' )
355-
. '" type="text" value="' . esc_attr( $title ) . '"/></p>');
356-
}
357-
358-
public function update( $new_instance, $old_instance ){
359-
$instance = array();
360-
$instance['title'] = strip_tags( $new_instance['title'] );
361-
return $instance;
362-
}
363-
364-
public function widget( $args, $instance ){
365-
extract( $args );
366-
$title = apply_filters( 'widget_title', $instance['title'] );
367-
echo $before_widget;
368-
if ( ! empty( $title ) )
369-
echo $before_title . $title . $after_title;
370-
echo __( 'Hello, World!', 'text_domain' );
371-
echo $after_widget;
372-
}
373-
374-
375343
}
376344

377-
if(class_exists( 'awpp' ) ){
378-
register_activation_hook(__FILE__, array('Awpp', 'activate') );
379-
register_deactivation_hook(__FILE__, array('Awpp', 'deactivate') );
345+
if( class_exists( 'AWPP_Init' ) ){
346+
register_activation_hook(__FILE__, array('AWPP_Init', 'activate') );
347+
register_deactivation_hook(__FILE__, array('AWPP_Init', 'deactivate') );
380348

381-
$awpp = new Awpp;
349+
$awpp = new AWPP_Init;
382350

383351
}
384352

settings.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
<?php
22

3-
/*
4-
* To change this template, choose Tools | Templates
5-
* and open the template in the editor.
6-
*/
7-
83
/**
94
* Description of AWPP_Settings
105
*
116
* @author kwisatz
7+
* Plugin Name: Annuaire Client
8+
* Plugin URI:
9+
* Description: Retrieves configurable entries from annuaire.youth.lu and displays them in various formats
10+
* Version: 0.1.0
11+
* Author: David Raison
12+
* Author URI: http://david.raison.lu
13+
* License: GPL3
1214
*/
15+
1316
class AWPP_Settings {
1417

18+
public function __construct(){
19+
// nothing yet
20+
}
21+
22+
public function add_menu(){
23+
add_options_page('Annuaire Client Settings', 'Annuaire Client', 'manage_options', 'awpp', array('AWPP_Settings', 'plugin_settings_page'));
24+
}
1525

1626
public function admin_init(){
1727
register_setting('awpp-group', 'awpp_options');

widget.php

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* Description of widget
5+
*
6+
* @author kwisatz
7+
*/
8+
class Awpp_Widget extends WP_Widget {
9+
10+
public function __construct() {
11+
parent::__construct(
12+
'awpp_widget', // Base ID
13+
'Awpp Widget', // Name
14+
array('description' => __('Awpp widget') ) // Args
15+
);
16+
}
17+
18+
/**
19+
* Register the widget class
20+
*/
21+
public function register_awpp_widget(){
22+
register_widget('AWPP_Widget');
23+
}
24+
25+
public function form( $instance ){
26+
if ( isset( $instance[ 'title' ] ) ) {
27+
$title = $instance[ 'title' ];
28+
} else {
29+
$title = __( 'New title', 'text_domain' );
30+
}
31+
32+
if ( isset( $instance[ 'region' ] ) ) {
33+
$region = $instance[ 'region' ];
34+
} else {
35+
$region = 'center';
36+
}
37+
38+
if ( isset( $instance[ 'limit' ] ) ) {
39+
$limit = $instance[ 'limit' ];
40+
} else {
41+
$limit = 5;
42+
}
43+
44+
print('<p><label for="' . $this->get_field_id('title') . '"' . _e( "Title") . '</label>');
45+
print('<input class="widefat" id="' . $this->get_field_id( 'title' )
46+
. '" name="' . $this->get_field_name( 'title' )
47+
. '" type="text" value="' . esc_attr( $title ) . '"/></p>');
48+
print('<p><label for="' . $this->get_field_id('region') . '"' . _e( "Region") . '</label>');
49+
print('<input class="widefat" id="' . $this->get_field_id( 'region' )
50+
. '" name="' . $this->get_field_name( 'region' )
51+
. '" type="text" value="' . esc_attr( $region ) . '"/></p>');
52+
print('<p><label for="' . $this->get_field_id('type') . '"' . _e( "Type") . '</label>');
53+
print('<input class="widefat" id="' . $this->get_field_id( 'type' )
54+
. '" name="' . $this->get_field_name( 'type' )
55+
. '" type="text" value="' . esc_attr( $title ) . '"/></p>');
56+
print('<p><label for="' . $this->get_field_id('limit') . '"' . _e( "Limit") . '</label>');
57+
print('<input class="widefat small-text" id="' . $this->get_field_id( 'limit' )
58+
. '" name="' . $this->get_field_name( 'limit' )
59+
. '" type="number" value="' . esc_attr( $limit ) . '"/></p>');
60+
}
61+
62+
public function update( $new_instance, $old_instance ){
63+
$instance = array();
64+
$instance['title'] = strip_tags( $new_instance['title'] );
65+
$instance['region'] = strip_tags( $new_instance['region'] );
66+
$instance['limit'] = strip_tags( $new_instance['limit'] );
67+
return $instance;
68+
}
69+
70+
public function widget( $args, $instance ){
71+
extract( $args );
72+
$title = apply_filters( 'widget_title', $instance['title'] );
73+
echo $before_widget;
74+
if ( ! empty( $title ) )
75+
echo $before_title . $title . $after_title;
76+
77+
//echo __( 'Hello, World!', 'text_domain' );
78+
$sc = new AWPP_Shortcode();
79+
print( $sc->create_annuaire_list( array(
80+
'region' => $instance['region'],
81+
'photos' => false,
82+
'limit' => $instance['limit']
83+
)
84+
) );
85+
86+
echo $after_widget;
87+
}
88+
89+
90+
}
91+
92+
?>

0 commit comments

Comments
 (0)