-
Notifications
You must be signed in to change notification settings - Fork 0
/
sponsors.php
211 lines (193 loc) · 7.2 KB
/
sponsors.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/*
@link http://tim.gremalm.se/
@since 1.0.0
@package Sponsorswidget
@wordpress-plugin
Plugin Name: SponsorsWidget
Plugin URI: https://github.com/ChalmersRobotics/SponsorsWidget
Description: A widget for Wordpress where you can list sponsors.
Version: 1.0.0
Author: Tim Gremalm
Author URI: http://tim.gremalm.se/
License: GPL-3.0+
License URI: http://www.gnu.org/licenses/gpl-3.0.txt
Text Domain: sponsorswidget
Domain Path: /languages
*/
//Widget
class Sponsors_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'sponsors_widget', // Base ID
esc_html__( 'Sponsors Widget', 'text_domain' ), // Name
array( 'description' => esc_html__( 'Adds sponsors in a widget', 'text_domain' ), )
);
}
//Front-end display of widget
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
//Get sponsors's
$sponsors = get_posts(array('post_type' => 'sponsor',
'posts_per_page'=>-1,
'orderby' => 'rand',
'order' => 'ASC'
));
//var_dump($sponsors);
foreach($sponsors as $sponsor) {
$sponsorurl = get_post_meta( $sponsor->ID, 'sponsorurl', true );
$sponsorimage = get_post_meta( $sponsor->ID, 'sponsorimage', true );
//var_dump($sponsorimage);
?>
<p><a href="<?php echo $sponsorurl; ?>" title="<?php echo $sponsor->post_title; ?>" target="_blank"><img src="<?php echo $sponsorimage; ?>" title="<?php echo $sponsor->post_title; ?>" alt="<?php echo $sponsor->post_title; ?>" /></a></p><br />
<?php
}
echo $args['after_widget'];
}
//Back-end widget form
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
//Sanitize widget form values as they are saved
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
add_action('widgets_init', create_function('', 'return register_widget("Sponsors_Widget");'));
//Meta box for post type sponsor
function wpt_add_sponsor_metaboxes( $post ) {
//add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args );
add_meta_box(
'wpt_sponsor',
'sponsor parameters',
'wpt_sponsor',
'sponsor',
'normal',
'default'
);
}
function wpt_sponsor() {
global $post;
wp_enqueue_script('jquery');
wp_enqueue_script('thickbox');
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('wptuts-upload');
//Nonce field to validate form request came from current site
wp_nonce_field( basename( __FILE__ ), 'sponsor_fields' );
//Parameter sponsor url
$sponsorurl = get_post_meta( $post->ID, 'sponsorurl', true );
echo '<label>' . esc_attr_e( 'URL to sponsor:', 'text_domain' ) . '</label>';
echo '<input type="text" name="sponsorurl" id="sponsorurl" value="' . esc_url( $sponsorurl ) . '" class="widefat" />';
//Parameter for movement url
$sponsorimage = get_post_meta( $post->ID, 'sponsorimage', true );
echo '<label>' . esc_attr_e( 'URL to sponsor image:', 'text_domain' ) . '</label>';
echo '<input type="text" name="sponsorimage" id="sponsorimage" value="' . esc_url( $sponsorimage ) . '" class="widefat" />';
echo '<input id="upload_logo_button" type="button" class="button" value="Upload Logo" />';
echo '<span class="description">Upload an image for the logotype</span>';
echo '<div id="logopreview"></div>';
MediaUploadScript();
}
function MediaUploadScript() {
?>
<script type="text/javascript" language="javascript">// <![CDATA[
jQuery(document).ready(function($) {
$('#upload_logo_button').click(function() {
//Thickbox open Media Uploader
tb_show('Upload a logo', 'media-upload.php?type=image&TB_iframe=true&post_id=0', false);
return false;
});
window.send_to_editor = function(html) {
imgurl = jQuery('img', html).attr('src');
jQuery('#sponsorimage').val(imgurl);
jQuery('#logopreview').html(html);
jQuery('#logopreview img').css("max-width", "100%");
tb_remove();
};
if (jQuery('#sponsorimage').val()) {
jQuery('#logopreview').prepend('<img class="alignnone" src="' + jQuery('#sponsorimage').val() + '" style="max-width: 100%" />');
}
});
// ]]>
</script>
<?php
}
function save_meta_box_sponsor( $post_id, $post ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
//Verify this came from the our screen and with proper authorization, because save_post can be triggered at other times.
if ( ! wp_verify_nonce( $_POST['sponsor_fields'], basename(__FILE__) ) ) {
return $post_id;
}
if ( ! isset( $_POST['sponsorurl'] ) || ! isset( $_POST['sponsorimage'] ) ) {
return $post_id;
}
//This sanitizes the data from the field and saves it into an array $sponsor_meta
$sponsor_meta['sponsorurl'] = esc_textarea( $_POST['sponsorurl'] );
$sponsor_meta['sponsorimage'] = esc_textarea( $_POST['sponsorimage'] );
foreach ( $sponsor_meta as $key => $value ) :
//Don't store custom data twice
if ( 'revision' === $post->post_type ) {
return;
}
if ( get_post_meta( $post_id, $key, false ) ) {
update_post_meta( $post_id, $key, $value );
} else {
add_post_meta( $post_id, $key, $value);
}
if ( ! $value ) {
delete_post_meta( $post_id, $key );
}
endforeach;
}
add_action('save_post', 'save_meta_box_sponsor', 1, 2);
//Post type sponsors
function register_cpt_sponsors() {
$labels = array(
'name' => __( 'Sponsors', 'sponsor' ),
'singular_name' => __( 'Sponsor', 'sponsor' ),
'add_new' => __( 'Add New', 'sponsor' ),
'add_new_item' => __( 'Add New Sponsor', 'sponsor' ),
'edit_item' => __( 'Edit Sponsor', 'sponsor' ),
'new_item' => __( 'New Sponsor', 'sponsor' ),
'view_item' => __( 'View Sponsor', 'sponsor' ),
'search_items' => __( 'Search Sponsors', 'sponsor' ),
'not_found' => __( 'No sponsors found', 'sponsor' ),
'not_found_in_trash' => __( 'No sponsors found in Trash', 'sponsor' ),
'parent_item_colon' => __( 'Parent sponsor:', 'sponsor' ),
'menu_name' => __( 'Sponsors', 'sponsor' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'List of sponsors',
'supports' => array( 'title' ),
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 71,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'register_meta_box_cb' => 'wpt_add_sponsor_metaboxes',
);
register_post_type( 'sponsor', $args );
}
add_action( 'init', 'register_cpt_sponsors' );