forked from cuny-academic-commons/google-docs-shortcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdocs-shortcode.php
160 lines (120 loc) · 4.44 KB
/
gdocs-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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*
Plugin Name: Google Docs Shortcode
Plugin URI: https://github.com/cuny-academic-commons/google-docs-shortcode
Description: Easily embed a Google Doc into your blog posts
Author: r-a-y
Author URI: http://buddypress.org/community/members/r-a-y/
Version: 0.2
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Shortcode to embed a Google Doc.
*
* eg. [gdoc link="https://docs.google.com/document/pub?id=XXX"]
*/
function ray_google_docs_shortcode( $atts ) {
global $content_width;
extract( shortcode_atts( array(
'link' => false,
// dimensions
'width' => ! empty( $content_width ) ? $content_width : '100%',
'height' => 300, // default height is set to 300
// only for documents
'seamless' => 1, // if set to 'true', this will not show the Google Docs header / footer.
// if set to 'false', this will show the Google Docs header / footer.
// only for presentations
'size' => false, // preset presentation size, either 'small', 'medium' or 'large';
// preset dimensions are as follows: small (480x389), medium (960x749), large (1440x1109)
// to set custom size, set the 'width' and 'height' params instead
), $atts ) );
// if no link or link is not from Google Docs, stop now!
if ( ! $link || strpos( $link, '://docs.google.com' ) === false )
return;
$type = $extra = false;
// set the doc type by looking at the URL
// document
if ( strpos( $link, '/document/' ) !== false ) {
$type = 'doc';
// presentation
} elseif ( strpos( $link, '/presentation/' ) !== false || strpos( $link, '/present/' ) !== false ) {
$type = 'presentation';
// form
} elseif ( strpos( $link, '/forms/' ) !== false || strpos( $link, 'form?formkey' ) !== false ) {
$type = 'form';
// spreadsheet
} elseif ( strpos( $link, '/spreadsheet/' ) !== false ) {
$type = 'spreadsheet';
// nada!
} else {
return;
}
// add query args depending on doc type
switch ( $type ) {
case 'doc' :
if ( (int) $seamless === 1 )
$link = add_query_arg( 'embedded', 'true', $link );
break;
case 'presentation' :
// alter the link so we're in embed mode
// (older docs)
$link = str_replace( '/view', '/embed', $link );
// alter the link so we're in embed mode
$link = str_replace( 'pub?', 'embed?', $link );
// dimensions
switch ( $size ) {
case 'medium' :
$width = 960;
$height = 749;
break;
case 'large' :
$width = 1440;
$height = 1109;
break;
case 'small' :
default :
$width = 480;
$height = 389;
break;
}
// extra iframe args
// i'm aware that these are non-standard attributes in XHTML / HTML5,
// but these are the attributes given by Google's embed code!
// don't like this? use the 'ray_google_docs_shortcode_output' filter to remove it!
$extra = ' frameborder="0" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"';
break;
case 'form' :
// new form format
if ( strpos( $link, '/forms/' ) !== false ) {
$link = str_replace( 'viewform', 'viewform?embedded=true', $link );
// older form format
} else {
$link = str_replace( 'viewform?', 'embeddedform?', $link );
}
// extra iframe args
// i'm aware that these are non-standard attributes in XHTML / HTML5,
// but these are the attributes given by Google's embed code!
// don't like this? use the 'ray_google_docs_shortcode_output' filter to remove it!
$extra = ' frameborder="0" marginheight="0" marginwidth="0"';
break;
case 'spreadsheet' :
$link = add_query_arg( 'widget', 'true', $link );
// extra iframe args
// i'm aware that these are non-standard attributes in XHTML / HTML5,
// but these are the attributes given by Google's embed code!
// don't like this? use the 'ray_google_docs_shortcode_output' filter to remove it!
$extra = ' frameborder="0"';
break;
}
// set width
$width = ' width="' . esc_attr( $width ) . '"';
// set height
$height = ' height="' . esc_attr( $height ) . '"';
// finally, embed the google doc!
$output = '<iframe id="gdoc-' . md5( $link ) . '" class="gdocs_shortcode gdocs_' . esc_attr( $type ) . '" src="' . esc_url( $link ) . '"' . $width . $height . $extra . '></iframe>';
return apply_filters( 'ray_google_docs_shortcode_output', $output, $type );
}
add_shortcode( 'gdoc', 'ray_google_docs_shortcode' );