This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsurname_widget.php
125 lines (102 loc) · 3.98 KB
/
surname_widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Add function to widgets_init that'll load our widget.
* @since 0.1
*/
add_action( 'widgets_init', 'load_widgets' );
/**
* Register our widget.
* 'Example_Widget' is the widget class used below.
*
* @since 0.1
*/
function load_widgets() {
register_widget( 'Surname_Widget' );
}
/**
* Example Widget class.
* This class handles everything that needs to be handled with the widget:
* the settings, form, display, and update. Nice!
*
* @since 0.1
*/
class Surname_Widget extends WP_Widget {
/**
* Widget setup.
*/
function Surname_Widget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'rpSurnameWidget', 'description' => __('An widget that displays the top X surnames in rootspersona.', 'rootspersona') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'rp_surname_widget' );
/* Create the widget. */
$this->WP_Widget( 'rp_surname_widget', __('Surname Widget', 'rootspersona'), $widget_ops, $control_ops );
}
/**
* How to display the widget on the screen.
*/
function widget( $args, $instance ) {
global $wpdb;
extract( $args );
/* Our variables from the widget settings. */
$title = apply_filters('widget_title', $instance['title'] );
$cnt = $instance['cnt'];
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo $before_title . $title . $after_title;
/* Display name from widget settings if one was input. */
if ( $cnt > 0 ) {
$creds = new RP_Credentials();
$creds->set_prefix( $wpdb->prefix );
$transaction = new RP_Transaction( $creds, false );
$rows = RP_Dao_Factory::get_rp_persona_dao( $wpdb->prefix )
->get_top_x_surnames( $cnt );
$rCnt = count($rows);
if($rCnt > 0 ) {
for($idx = 0; $idx < $rCnt; $idx++ ) {
//echo '<div style="margin-left:10px;">' . $rows[$idx]['surname'] . ' (' . $rows[$idx]['cnt'] . ')</div>';
echo '<div style="margin-left:10px;"><a href="'
. get_home_url(null,'/?s=' . $rows[$idx]['surname'] . '&posttype=page&widget=advanced-search-widget-3')
. '" rel="nofollow">' . $rows[$idx]['surname'] . '</a> (' . $rows[$idx]['cnt'] . ')</div>';
}
}
$transaction->close();
}
/* After widget (defined by themes). */
echo $after_widget;
}
/**
* Update the widget settings.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['cnt'] = strip_tags( $new_instance['cnt'] );
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
* Make use of the get_field_id() and get_field_name() function
* when creating your form elements. This handles the confusing stuff.
*/
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array( 'title' => __('Top Surnames', 'rootspersona'), 'cnt' => '10' );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<!-- Your Name: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'cnt' ); ?>"><?php _e('How Many:', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'cnt' ); ?>" name="<?php echo $this->get_field_name( 'cnt' ); ?>" value="<?php echo $instance['cnt']; ?>" style="width:100%;" />
</p>
<?php
}
}
?>