-
Notifications
You must be signed in to change notification settings - Fork 0
/
lm-widgets.php
179 lines (149 loc) · 6.09 KB
/
lm-widgets.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Registers zeen101's LiveMarket Widgets
*
* @package zeen101's LiveMarket
* @since 1.0.0
*/
/**
* Register our widgets classes with WP
*
* @since 1.0.0
*/
function register_livemarket_widgets() {
register_widget( 'LiveMarket_Promotions' );
}
add_action( 'widgets_init', 'register_livemarket_widgets' );
/**
* This class registers and returns the Cover Image Widget
*
* @since 1.0.0
*/
class LiveMarket_Promotions extends WP_Widget {
/**
* Set's widget name and description
*
* @since 1.0.0
*/
function __construct() {
$widget_ops = array( 'classname' => 'livemarket_list_widget', 'description' => __( 'Displays a list of Promotions associated with your LiveMarket publication', 'livemarket' ) );
parent::__construct( 'LiveMarket_Promotions', __( 'LiveMarket Promotions', 'livemarket' ), $widget_ops );
}
/**
* Displays the widget on the front end
*
* @since 1.0.0
*
* @param array $args
* @param array $instance
*/
function widget( $args, $instance ) {
extract( $args );
extract( $instance );
if ( empty( $instance['signup_text'] ) ) {
$instance['signup_text'] = __( 'Post Your Promotion Here For Free', 'livemarket' );
}
$settings = get_livemarket_settings();
if ( empty( $settings['api_key'] ) ) {
return '<h1 class="error">' . __( 'You Must Enter a Valid LiveMarket API Token in the Live Market Plugin', 'livemarket' ) . '</h1>';
}
$out = '';
if ( !empty( $instance['subtext'] ) ) {
$out .= '<p class="livemarket_subtext">' . $instance['subtext'] . '</p>';
}
if ( !empty( $instance['show_signup'] ) ) {
$out .= widget_formatted_livemarket_advertisement_signup_link( $instance['signup_text'] );
}
$out .= widget_formatted_livemarket_advertisements( 0, $instance['limit'], $instance['category'] ); //Page, Limit
if ( ! empty( $out ) ) {
echo $before_widget;
if ( $title) {
echo $before_title . $title . $after_title;
}
echo '<div class="livemarket_widget_list">';
echo $out;
echo '</div>';
echo $after_widget;
}
}
/**
* Saves the widgets options on submit
*
* @since 1.0.0
*
* @param array $new_instance
* @param array $old_isntance
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['subtext'] = $new_instance['subtext'];
if ( 1 > $new_instance['limit'] ) {
$new_instance['limit'] = 1;
} else if ( 50 < $new_instance['limit'] ) {
$new_instance['limit'] = 50;
}
$instance['limit'] = $new_instance['limit'];
$instance['category'] = $new_instance['category'];
$instance['show_signup'] = (bool)$new_instance['show_signup'];
$instance['signup_text'] = $new_instance['signup_text'];
return $instance;
}
/**
* Displays the widget options in the dashboard
*
* @since 1.0.0
*
* @param array $instance
*/
function form( $instance ) {
$settings = get_livemarket_settings();
$categories = get_livemarket_advertisement_categories();
if ( empty( $settings['api_key'] ) ) {
echo '<h1 class="error">' . __( 'You Must Enter a Valid LiveMarket API Token in the Live Market Plugin', 'livemarket' ) . '</h1>';
}
//Defaults
$defaults = array(
'title' => 'LiveMarket',
'subtext' => '',
'category' => 0,
'limit' => 10,
'show_signup' => true,
'signup_text' => __( 'Post Your Promotion Here For Free', 'livemarket' ),
);
$instance = wp_parse_args( $instance, $defaults );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'livemarket' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( strip_tags( $instance['title'] ) ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('subtext'); ?>"><?php _e( 'Description:', 'livemarket' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('subtext'); ?>" name="<?php echo $this->get_field_name('subtext'); ?>" type="text" value="<?php echo esc_attr( strip_tags( $instance['subtext'] ) ); ?>" />
</p>
<?php if ( !empty( $categories->success ) && !empty( $categories->data ) ) { ?>
<p>
<label for="<?php echo $this->get_field_id('category'); ?>"><?php _e( 'Category:', 'livemarket' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
<option value="" <?php selected( $instance['category'], 0, true ) ?>><?php _e( 'All Categories', 'livemarket' ); ?></option>
<?php foreach( $categories->data as $category ) { ?>
<option value="<?php echo $category->slug; ?>" <?php selected( $instance['category'], $category->slug, true ) ?>><?php echo $category->name; ?></option>
<?php } ?>
</select>
</p>
<?php } ?>
<p>
<label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e( 'Limit (50 max):', 'livemarket' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="number" value="<?php echo esc_attr( strip_tags( $instance['limit'] ) ); ?>" min="1" max="50" />
</p>
<p>
<label for="<?php echo $this->get_field_id('show_signup'); ?>"><?php _e( 'Show Signup Link:', 'livemarket' ); ?></label>
<input id="<?php echo $this->get_field_id('show_signup'); ?>" name="<?php echo $this->get_field_name('show_signup'); ?>" type="checkbox" <?php checked( $instance['show_signup'] ); ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('signup_text'); ?>"><?php _e( 'Signup Text:', 'livemarket' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('signup_text'); ?>" name="<?php echo $this->get_field_name('signup_text'); ?>" type="text" value="<?php echo esc_attr( strip_tags( $instance['signup_text'] ) ); ?>" />
</p>
<?php
}
}