-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHlidacWidget.php
60 lines (50 loc) · 1.41 KB
/
HlidacWidget.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
<?php
namespace Hlidac\Wp;
/**
* Hlidac widget shortcode
*
* @author Petr Stastny <[email protected]>
*/
class HlidacWidget
{
/**
* Get shortcode content
*
* @param array $atts shortcode attributes
* @return string
*/
public static function getContent(array $atts)
{
if (empty($atts['page'])) {
return '';
}
$width = '500';
if (!empty($atts['width'])) {
$width=$atts['width'];
}
$randomSource = 'abcdefghijklmnopqrstuvwxyz01234567890';
$id = 'hlidac'.self::randomStringFrom($randomSource, 5);
$scriptUrl = 'https://www.hlidacstatu.cz/widget/'.$id.'?width='.$width;
wp_enqueue_script('hlidacstatu-widget', $scriptUrl, array(), '1.0');
$html = '';
$html .= '<div id="'.$id.'" style="width:'.$width.'px" widget-page="'.$atts['page'].'"></div>';
return $html;
}
/**
* Generate random string from explicit source characters
*
* @param string $source source characters
* @param int $length string length
* @return string
*/
public static function randomStringFrom($source, $length)
{
$shuffled = str_shuffle($source);
$sourceLen = strlen($source);
$result = '';
for ($i = 1; $i <= $length; $i++) {
$result .= $shuffled[random_int(0, $sourceLen - 1)];
}
return $result;
}
}