-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr-shortcode.php
executable file
·139 lines (120 loc) · 3.83 KB
/
qr-shortcode.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
<?php
/*
Plugin Name: QR shortcode
Plugin URI: http://www.blackcrystal.net/project/qr-shortcode/
Description: Plugin adds [qr] shortcode and shows QR image in admin pages of all post types.
Version: 1.0
Author: Sergei Miami <miami@blackcrystal.net>
Author URI: http://www.blackcrystal.net
Copyright 2016 Sergei Miami <miami@blackcrystal.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( !defined( 'ABSPATH' ) ) exit;
class qrshortcode
{
private static $instance = null;
public static function init()
{
if( self::$instance == null )
self::$instance = new self;
return self::$instance;
}
/**
* qrshortcode constructor forbidden
* @use qrshortcode::init()
*/
private function __construct()
{
add_shortcode( 'qr', array($this,'add_shortcode'));
add_action( 'add_meta_boxes', array($this,'add_metaboxes'));
}
/**
* Adds metaboxes
*/
public function add_metaboxes()
{
add_meta_box('qr-shortcode', 'QR code', array($this,'render_metaboxes'), null, 'side', 'low');
}
/**
* Render QR metabox min admin page
* @param $post
*/
public function render_metaboxes( $post )
{
if (get_post_status($post) != 'publish')
{
echo "Clik Publish to get QR code";
return;
}
echo do_shortcode('[qr style="width: 100%; height: auto;"]');
}
/**
* Adds shortcode
* @param $atts
* @return string
*/
public function add_shortcode( $atts )
{
$atts = shortcode_atts( array(
'link'=>get_post_permalink(),
'size'=>'300',
'style'=>'',
), $atts );
//$atts['link'] = $this->shorten_link($atts['link']);
$url = 'http://chart.apis.google.com/chart?cht=qr'.
'&chs='.$atts['size'].'x'.$atts['size'] .
'&chl='.urlencode($atts['link']).
'&chld=H|0';
return '<img src="'.$this->get_cached_qr_url($url).'" style="'.$atts['style'].'">';
}
/**
* Unused method because need to cache url somehow
* (too many requests to google API)
* @param $url
* @return bool|mixed
*/
private function shorten_link($url)
{
if ($result = wp_cache_get($url, 'qr-shortcode')) return $result;
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => array('Content-Type: application/json'),
'content' => json_encode(array('longUrl'=>$url)),
),
));
$json = file_get_contents('https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyAZUFTmJLUHJJsmaXr3mHr6Z9kGbhhxGzA', false, $context);
$obj = json_decode($json);
$result = isset($obj->id) ? $obj->id : $url;
wp_cache_set($url, $result, 'qr-shortcode');
return $result;
}
/**
* Cache QR code into uplads dir
* @param string $url google chart api url
* @return string uri to image
*/
private function get_cached_qr_url( $url )
{
$dir = wp_get_upload_dir();
$file = md5($url).'.png';
$subdir = substr($file,0,2);
if (!file_exists($path = "$dir[basedir]/qr-shortcode/$subdir"))
wp_mkdir_p($path);
if (!file_exists($path = "$dir[basedir]/qr-shortcode/$subdir/$file"))
file_put_contents($path, file_get_contents($url)) && chmod($path, 0666);
return "$dir[baseurl]/qr-shortcode/$subdir/$file";
}
}
// let's rock!
add_action( 'plugins_loaded', array('qrshortcode', 'init') );