1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
class greatschools_getschools extends WP_Widget { |
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* __construct function. |
8
|
|
|
* |
9
|
|
|
* @access public |
10
|
|
|
* @return void |
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
public function __construct() { |
13
|
|
|
|
14
|
|
|
parent::__construct( |
15
|
|
|
'getschools', |
16
|
|
|
__( 'GreatSchools - Get List of Schools', 're-pro' ), |
17
|
|
|
array( |
18
|
|
|
'description' => __( 'Get Schools', 're-pro' ), |
19
|
|
|
) |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* widget function. |
26
|
|
|
* |
27
|
|
|
* @access public |
28
|
|
|
* @param mixed $args |
29
|
|
|
* @param mixed $instance |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function widget( $args, $instance ) { |
33
|
|
|
|
34
|
|
|
$greatschools_title = !empty( $instance['greatschools_title'] ) ? $instance['greatschools_title'] : ''; |
35
|
|
|
$greatschools_state = !empty( $instance['greatschools_state'] ) ? $instance['greatschools_state'] : ''; |
36
|
|
|
$greatschools_city = !empty( $instance['greatschools_city'] ) ? $instance['greatschools_city'] : ''; |
37
|
|
|
|
38
|
|
|
// Call our API. |
39
|
|
|
$repro_settings = get_option( 'repro_settings' ); |
40
|
|
|
$greatschools_apikey = $repro_settings['greatschools_apikey']; |
41
|
|
|
|
42
|
|
|
$greatschools = new GreatSchoolsAPI( $greatschools_apikey ); |
43
|
|
|
$schools = $greatschools->get_schools( $greatschools_state,$greatschools_city ); |
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
echo $args['before_widget']; |
48
|
|
|
|
49
|
|
|
echo $args['before_title'] . esc_attr( $greatschools_title ) . $args['after_title']; |
50
|
|
|
|
51
|
|
|
// var_dump($schools); |
|
|
|
|
52
|
|
|
|
53
|
|
|
foreach($schools as $school) { |
|
|
|
|
54
|
|
|
|
55
|
|
|
foreach($school as $school_item) { |
56
|
|
|
|
57
|
|
|
$name = $school_item['name']; |
58
|
|
|
$type =$school_item['type']; |
|
|
|
|
59
|
|
|
$grade_range = $school_item['gradeRange']; |
|
|
|
|
60
|
|
|
$state = $school_item['state']; |
|
|
|
|
61
|
|
|
$city = $school_item['city']; |
|
|
|
|
62
|
|
|
$overview_link = $school_item['overviewLink']; |
63
|
|
|
|
64
|
|
|
echo '<li><a href="'.$overview_link.'" rel="nofollow">' . $name . '</a></li>'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
echo $args['after_widget']; |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function form( $instance ) { |
75
|
|
|
|
76
|
|
|
// Set default values |
77
|
|
|
$instance = wp_parse_args( (array) $instance, array( |
78
|
|
|
'greatschools_title' => '', |
79
|
|
|
'greatschools_state' => '', |
80
|
|
|
'greatschools_city' => '', |
81
|
|
|
) ); |
82
|
|
|
|
83
|
|
|
// Retrieve an existing value from the database |
84
|
|
|
$greatschools_title = !empty( $instance['greatschools_title'] ) ? $instance['greatschools_title'] : ''; |
85
|
|
|
$greatschools_state = !empty( $instance['greatschools_state'] ) ? $instance['greatschools_state'] : ''; |
86
|
|
|
$greatschools_city = !empty( $instance['greatschools_city'] ) ? $instance['greatschools_city'] : ''; |
87
|
|
|
|
88
|
|
|
// Form fields |
89
|
|
|
echo '<p>'; |
90
|
|
|
echo ' <label for="' . $this->get_field_id( 'greatschools_title' ) . '" class="greatschools_title_label">' . __( 'Title', 're-pro' ) . '</label>'; |
91
|
|
|
echo ' <input type="text" id="' . $this->get_field_id( 'greatschools_title' ) . '" name="' . $this->get_field_name( 'greatschools_title' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $greatschools_title ) . '">'; |
92
|
|
|
echo ' <span class="description">' . __( 'Title', 're-pro' ) . '</span>'; |
93
|
|
|
echo '</p>'; |
94
|
|
|
|
95
|
|
|
echo '<p>'; |
96
|
|
|
echo ' <label for="' . $this->get_field_id( 'greatschools_state' ) . '" class="greatschools_state_label">' . __( 'State', 're-pro' ) . '</label>'; |
97
|
|
|
echo ' <input type="text" id="' . $this->get_field_id( 'greatschools_state' ) . '" name="' . $this->get_field_name( 'greatschools_state' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $greatschools_state ) . '">'; |
98
|
|
|
echo ' <span class="description">' . __( 'State', 're-pro' ) . '</span>'; |
99
|
|
|
echo '</p>'; |
100
|
|
|
|
101
|
|
|
echo '<p>'; |
102
|
|
|
echo ' <label for="' . $this->get_field_id( 'greatschools_city' ) . '" class="greatschools_city_label">' . __( 'City', 're-pro' ) . '</label>'; |
103
|
|
|
echo ' <input type="text" id="' . $this->get_field_id( 'greatschools_city' ) . '" name="' . $this->get_field_name( 'greatschools_city' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $greatschools_city ) . '">'; |
104
|
|
|
echo ' <span class="description">' . __( 'City', 're-pro' ) . '</span>'; |
105
|
|
|
echo '</p>'; |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function update( $new_instance, $old_instance ) { |
110
|
|
|
|
111
|
|
|
$instance = $old_instance; |
112
|
|
|
|
113
|
|
|
$instance['greatschools_title'] = !empty( $new_instance['greatschools_title'] ) ? strip_tags( $new_instance['greatschools_title'] ) : ''; |
114
|
|
|
$instance['greatschools_state'] = !empty( $new_instance['greatschools_state'] ) ? strip_tags( $new_instance['greatschools_state'] ) : ''; |
115
|
|
|
$instance['greatschools_city'] = !empty( $new_instance['greatschools_city'] ) ? strip_tags( $new_instance['greatschools_city'] ) : ''; |
116
|
|
|
|
117
|
|
|
return $instance; |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
function greatschools_register_widgets() { |
124
|
|
|
|
125
|
|
|
$repro_settings = get_option( 'repro_settings' ); |
126
|
|
|
$greatschools_apikey = $repro_settings['greatschools_apikey']; |
127
|
|
|
|
128
|
|
|
if ( ! empty( $greatschools_apikey) ) { |
129
|
|
|
register_widget( 'greatschools_getschools' ); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
add_action( 'widgets_init', 'greatschools_register_widgets' ); |
133
|
|
|
|
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.