-
Notifications
You must be signed in to change notification settings - Fork 0
/
logoroulette.php
153 lines (114 loc) · 4.23 KB
/
logoroulette.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
<?php
/*
Plugin name: Logo Roulette
Description: Show sponsor logos on sidebar widget
Version: 1.0
Author: heidiwik
License: GPLv2 or later
*/
/* Admin page */
function logoroulette_setup_menu(){
add_menu_page( 'Logo Roulette', 'Logo Roulette', 'manage_options', 'logoroulette', 'logoroulette_init' );
}
add_action('admin_menu', 'logoroulette_setup_menu');
function logoroulette_init() {
logoroulette_handle_image();
$options = get_option('logoroulette_options', false);
?>
<div class="wrap">
<h1>Logo Roulette</h1>
<h2>Add sponsor logo</h2>
<p>Logos are shown in widget on random order</p>
<input class="upload_image_button button" type="button" value="Add new logo" />
<div id="image_to_add"></div>
<div id="logoroulette">
<h2>Logos</h2>
<div id="logot">
<?php if ($options) {
foreach ($options as $logo) {
$logo_url = wp_get_attachment_image_src($logo[0], 'medium');
echo '<div class="logo">';
echo '<img width="150" src="' . $logo_url[0] . '"><br>';
echo '<p>' . $logo[1] . '</p>';
echo '<p><a href="?page=logoroulette&poista=' . $logo[0] . '">Delete</a></p>';
echo '</div>';
}
}
?>
</div>
</div>
</div>
<?php
}
/* Upload image */
function logoroulette_handle_image() {
if(isset($_POST['image_to_add_id'])){
$image_to_add = filter_input(INPUT_POST, 'image_to_add_id', FILTER_SANITIZE_SPECIAL_CHARS);
$link_to_add = filter_input(INPUT_POST, 'link_to_add', FILTER_SANITIZE_URL);
if (!$link_to_add) {
echo '<div id="notice" class="error notice is-dismissible"><p>Link error, image not loaded </p></div>';
} else {
$uusi_logo = array($image_to_add, $link_to_add);
if (get_option('logoroulette_options', false)) {
$options = get_option('logoroulette_options', false);
$options[] = $uusi_logo;
} else {
$options = array($uusi_logo);
}
if (update_option('logoroulette_options', $options)) {
echo '<div id="notice" class="updated notice is-dismissible"><p>Logo added</p></div>';
} else {
echo '<div id="notice" class="error notice is-dismissible"><p>Error</p></div>';
}
}
} elseif (isset($_GET['poista'])) {
$poistettava_logo_id = filter_input(INPUT_GET, 'poista', FILTER_SANITIZE_SPECIAL_CHARS);
$current_options = get_option('logoroulette_options');
foreach ($current_options as $logo_key => $logo) {
if ($logo[0] == $poistettava_logo_id) {
unset($current_options[$logo_key]);
continue;
}
}
if (update_option('logoroulette_options', $current_options)) {
echo '<div id="notice" class="updated notice is-dismissible"><p>Logo deleted</p></div>';
} else {
echo '<div id="notice" class="error notice is-dismissible"><p>Error</p></div>';
}
}
}
/* Logo Roulette widget */
function logoroulette_display($args) {
echo $args['before_widget'];
$logot = get_option('logoroulette_options');
$random_logo_key = array_rand($logot, 1);
$logo = $logot[$random_logo_key];
$logo_data = wp_get_attachment_image_src($logo[0], 'medium');
echo "<br><a target='_blank' href='http://" . $logo[1] . " '>";
echo '<img width="180" src="' . $logo_data[0] . '"></a>';
echo $args['after_widget'];
}
wp_register_sidebar_widget(
'logoroulette_widget_1',
'Logo Roulette',
'logoroulette_display',
array(
'description' => 'Logo Roulette widget'
)
);
function logoroulette_admin_scripts() {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_script('jquery');
wp_enqueue_script('logoroulette-media-library', plugins_url('js/logoroulette.js', __FILE__), array(), '1.0', true);
}
function logoroulette_admin_styles() {
wp_enqueue_style('thickbox');
wp_enqueue_style('logoroulette', plugins_url('css/logoroulette.css', __FILE__), '1.0');
}
add_action('admin_print_scripts', 'logoroulette_admin_scripts');
add_action('admin_print_styles', 'logoroulette_admin_styles');
function logoroulette_load_wp_media_files() {
wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts', 'logoroulette_load_wp_media_files' );