-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundcloud-random-track.php
177 lines (141 loc) · 4.55 KB
/
soundcloud-random-track.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
<?php
/**
* Plugin Name: Soundcloud Random Track
* Plugin URI: http://mesmerized.info
* Description: A Plugin that enables [soundcloud_random] tag to be used for displaying a random widget from a pre-defined list
* Version: 0.1.0
* Author: Gleb Goloborodko
* Author URI: http://www.goloborodko.com
* Text Domain: Optional. Plugin's text domain for localization. Example: mytextdomain
* Domain Path: Optional. Plugin's relative directory path to .mo files. Example: /locale/
* Network: Optional. Whether the plugin can only be activated network wide. Example: true
* License: GPL2
*/
register_activation_hook(__FILE__, 'wstr_install');
function wstr_install()
{
// installs plugin
// create array in options
add_option('wstr_options_array',array());
add_option('wstr_params',array());
}
register_deactivation_hook(__FILE__, 'wstr_deactivate');
function wstr_deactivate()
{
// deactivates plugin
// deletes array in options
delete_option('wstr_options_array');
delete_option('wstr_params');
}
function load_style_js()
{
wp_register_style( 'wstr', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('wstr');
wp_register_script( 'wstr', plugins_url('main.js',__FILE__ ));
wp_enqueue_script('wstr');
}
add_filter("the_content", 'wstr_filter');
function wstr_filter($content)
{
$replace = '[soundcloud_random]';
$wstr_options = get_option('wstr_options_array');
$wstr_params = get_option('wstr_params');
$object = $wstr_options[rand(0,count($wstr_options)-1)];
$object = stripcslashes($object);
$content = str_replace($replace, $object, $content);
// regular expression for width & height
$pattern_width = '/width=\".*?\"/';
if($wstr_params['width'])
$content = preg_replace($pattern_width,'width="'.$wstr_params['width'].'"',$content);
$pattern_height = '/height=\".*?\"/';
if($wstr_params['height'])
$content = preg_replace($pattern_height,'height="'.$wstr_params['height'].'"',$content);
return $content;
}
// admdin part ------------------------
function save_data()
{
// saving data to datbase using wp_option
}
function load_data()
{
// loading data from datbase using wp_option
}
add_action('admin_menu', 'wstr_create_menu');
function wstr_create_menu()
{
add_menu_page('SoundCloud Plugin Page',
'SC Plugin',
'manage_options',
'wstr_main_menu_page',
'wstr_settings_page');
add_action('admin_init', 'wstr_register_settings');
}
function wstr_register_settings()
{
register_setting('wstr-settings-group','wstr_options','wstr_sanitize_options');
}
function wstr_settings_page()
{
load_style_js();
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// removing empty values
$_POST['wstr_options'] = array_filter($_POST['wstr_options']);
if(isset($_POST['wstr_options']))
update_option('wstr_options_array',$_POST['wstr_options']);
$params = array();
if(isset($_POST['wstr_width']))
$params['width'] = $_POST['wstr_width'];
if(isset($_POST['wstr_height']))
$params['height'] = $_POST['wstr_height'];
update_option('wstr_params',$params);
}
$params = get_option('wstr_params');
settings_fields('wstr-settings-group');
$wstr_options = get_option('wstr_options_array');
foreach($wstr_options as &$option_temp)
$option_temp = stripcslashes($option_temp);
?>
<div id="wstr_wrap" class=wrap>
<h2>SoundCloud Random Track settings page</h2>
<form method="post" actions='options.php' id="wstr_form">
<p>
<button type="button" id="wstr_addnew" onclick="wstrAddNewTrack(this);">Add new</button>
</p>
<?php $index = 0; ?>
<?php foreach($wstr_options as $option): ?>
<div id="wstr_track">
<label>Widget code:
<textarea class="wstr_value" name="wstr_options[]"><?php echo esc_attr($option)?></textarea>
</label>
<button class="wstr_remove" type="button" onclick="wstrRemoveTrack(this);">X</button>
<!-- <br /> -->
</div>
<?php endforeach;?>
<label>
Width:
<input name="wstr_width" class="wstr_options" value="<?php echo $params['width']?>"/>
</label>
<label>
Height:
<input name="wstr_height" class="wstr_options" value="<?php echo $params['height']?>"/>
</label>
<p class=submit>
<input type="submit" class="button-primary" value="Save changes" />
</p>
</form>
</div>
<?
}
function wstr_sanitize_options($input)
{
// echo "sanitizing"; die;
// $input['option1'] = esc_url($input['option1']);
// $input['option2'] = esc_url($input['option2']);
// $input['option3'] = esc_url($input['option3']);
// $input['option4'] = esc_url($input['option4']);
return $input;
}
// enf of admin part ------------------
?>