PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.4.4
Jetpack – WP Security, Backup, Speed, & Growth v6.4.4
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / googleapps.php

googleapps.php in Jetpack – WP Security, Backup, Speed, & Growth 6.4.4, at modules/shortcodes/googleapps.php

243 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Google Docs and Google Calendar Shortcode
5 *
6 * Presentation:
7 * <iframe src="https://docs.google.com/present/embed?id=dhfhrphh_123drp8s65c&interval=15&autoStart=true&loop=true&size=l" frameborder="0" width="700" height="559"></iframe>
8 * <iframe src="https://docs.google.com/presentation/embed?id=13ItX4jV0SOSdr-ZjHarcpTh9Lr4omfsHAp87jpxv8-0&start=false&loop=false&delayms=3000" frameborder="0" width="960" height="749" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>
9 *
10 * Document:
11 * <iframe src="https://docs.google.com/document/pub?id=1kDatklacdZ_tZUOpWtt_ONzY97Ldj2zFcuO9LBY2Ln4&amp;embedded=true"></iframe>
12 * <iframe src="https://docs.google.com/document/d/1kDatklacdZ_tZUOpWtt_ONzY97Ldj2zFcuO9LBY2Ln4/pub?embedded=true"></iframe>
13 * <iframe src="https://docs.google.com/document/d/e/2PACX-1vRkpIdasKL-eKXDjJgpEONduUspZTz0YmKaajfie0eJYnzikuyusuG1_V8X8T9XflN9l8A1oCM2sgEA/pub?embedded=true"></iframe>
14 *
15 * External document:
16 * <iframe width=100% height=560px frameborder=0 src=https://docs.google.com/a/pranab.in/viewer?a=v&pid=explorer&chrome=false&embedded=true&srcid=1VTMwdgGiDMt8MCr75-YkQP-4u9WmEp1Qvf6C26KYBgFilxU2qndpd-VHhBIn&hl=en></iframe>
17 *
18 * Spreadsheet Form:
19 * <iframe src="https://spreadsheets.google.com/embeddedform?formkey=dEVOYnMzZG5jMUpGbjFMYjFYNVB3NkE6MQ" width="760" height="710" frameborder="0" marginheight="0" marginwidth="0">Loading...</iframe>
20 *
21 * Spreadsheet Widget:
22 * <iframe width='500' height='300' frameborder='0' src='https://spreadsheets1.google.com/a/petedavies.com/pub?hl=en&hl=en&key=0AjSij7nlnXvKdHNsNjRSWG12YmVfOEFwdlMxQ3J1S1E&single=true&gid=0&output=html&widget=true'></iframe>
23 * <iframe width='500' height='300' frameborder='0' src='https://spreadsheets.google.com/spreadsheet/pub?hl=en&hl=en&key=0AhInIwfvYrIUdGJiTXhtUEhBSFVPUzdRZU5OMDlqdnc&output=html&widget=true'></iframe>
24 *
25 * Calendar:
26 * <iframe src="https://www.google.com/calendar/embed?src=serjant%40gmail.com&ctz=Europe/Sofia" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"></iframe>
27 * <iframe src="http://www.google.com/calendar/hosted/belcastro.com/embed?src=n8nr8sd6v9hnus3nmlk7ed1238%40group.calendar.google.com&ctz=Europe/Zurich" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"></iframe>
28 *
29 * Customized calendar:
30 * <iframe src="https://www.google.com/calendar/embed?title=asdf&amp;showTitle=0&amp;showNav=0&amp;showDate=0&amp;showPrint=0&amp;showTabs=0&amp;showCalendars=0&amp;
31 * showTz=0&amp;mode=AGENDA&amp;height=300&amp;wkst=2&amp;hl=fi&amp;bgcolor=%23ffcccc&amp;src=m52gdmbgelo3itf00u1v44g0ns%40group.calendar.google.com&amp;color=%234E5D6C&amp;
32 * src=serjant%40gmail.com&amp;color=%235229A3&amp;ctz=Europe%2FRiga" style=" border:solid 1px #777 " width="500" height="300" frameborder="0" scrolling="no"></iframe>
33 *
34 * Generic
35 * <iframe src="https://docs.google.com/file/d/0B0SIdZW7iu-zX1RWREJpMXVHZVU/preview" width="640" height="480"></iframe>
36 */
37
38 add_filter( 'pre_kses', 'googleapps_embed_to_shortcode' );
39 add_shortcode( 'googleapps', 'googleapps_shortcode' );
40
41 /**
42 * Reverse iframe embed to shortcode mapping HTML attributes to shortcode attributes.
43 *
44 * @since 4.5.0
45 *
46 * @param string $content
47 *
48 * @return mixed
49 */
50 function googleapps_embed_to_shortcode( $content ) {
51 if ( ! is_string( $content ) || false === stripos( $content, '<iframe' ) && false === stripos( $content, '.google.com' ) ) {
52 return $content;
53 }
54
55 $regexp = '#<iframe((?:\s+\w+="[^"]*")*?)\s*src="https?://(docs|drive|spreadsheets\d*|calendar|www)*\.google\.com/(?!maps)([-\w\./]+)(?:\?)?([^"]+)?"\s*((?:\s+\w+="[^"]*")*?)>.*?</iframe>#i';
56 $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
57 $regexp_squot = str_replace( '"', "'", $regexp );
58 $regexp_ent_squot = str_replace( '"', "'", $regexp_ent );
59 $regexp_noquot = '!<iframe(.*?)src=https://(docs|drive)\.google\.com/[-\.\w/]*?(viewer)\?(.*?)>(.*?)</iframe>!';
60 $regexp_ent_noquot = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp_noquot, ENT_NOQUOTES ) );
61
62 foreach ( array( 'regexp', 'regexp_ent', 'regexp_squot', 'regexp_ent_squot', 'regexp_noquot', 'regexp_ent_noquot' ) as $reg ) {
63 if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
64 continue;
65 }
66
67 foreach ( $matches as $match ) {
68 $params = $match[1] . $match[5];
69 if ( in_array( $reg, array( 'regexp_ent', 'regexp_ent_squot' ) ) ) {
70 $params = html_entity_decode( $params );
71 }
72
73 $params = wp_kses_hair( $params, array( 'http' ) );
74
75 $width = $height = 0;
76 if ( isset( $params['width'] ) ) {
77 $width = (int) $params['width']['value'];
78 }
79
80 if ( isset( $params['height'] ) ) {
81 $height = (int) $params['height']['value'];
82 }
83
84 // allow the user to specify width greater than 200 inside text widgets
85 if ( $width > 400 && isset( $_POST['widget-text'] ) ) {
86 $width = 200;
87 $height = 200;
88 }
89
90 $attributes = '';
91 if ( isset( $params['width'] ) && '100%' == $params['width']['value'] ) {
92 $width = '100%';
93 }
94
95 if ( $width ) {
96 $attributes = ' width="' . $width . '"';
97 }
98
99 if ( $height ) {
100 $attributes .= ' height="' . $height . '"';
101 }
102
103 $domain = 'spreadsheets';
104 if ( in_array( $match[2], array( 'docs', 'drive', 'www', 'calendar' ) ) ) {
105 $domain = $match[2];
106 }
107
108 // Make sure this is actually something that the shortcode supports. If it's not, leave the HTML alone.
109 if ( ! googleapps_validate_domain_and_dir( $domain, $match[3] ) ) {
110 continue;
111 }
112
113 /** This action is documented in modules/widgets/social-media-icons.php */
114 do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', googleapps_service_name( $domain, $match[3] ) );
115
116 $content = str_replace( $match[0], '[googleapps domain="' . $domain . '" dir="' . $match[3] . '" query="' . esc_attr( $match[4] ) . '"' . $attributes . ' /]', $content );
117 }
118 }
119
120 return $content;
121 }
122
123 /**
124 * Parse shortcode attributes and output a Google Docs embed.
125 *
126 * @since 4.5.0
127 *
128 * @param array $atts
129 *
130 * @return string
131 */
132 function googleapps_shortcode( $atts ) {
133 global $content_width;
134
135 $attr = shortcode_atts(
136 array(
137 'width' => '100%',
138 'height' => '560',
139 'domain' => 'docs',
140 'dir' => 'document',
141 'query' => '',
142 'src' => '',
143 ), $atts
144 );
145
146 if ( isset( $content_width ) && is_numeric( $attr['width'] ) && $attr['width'] > $content_width ) {
147 $attr['width'] = $content_width;
148 }
149
150 if ( isset( $content_width ) && '560' === $attr['height'] ) {
151 $attr['height'] = $content_height = floor( $content_width * 3 / 4 );
152 }
153
154 if ( isset( $atts[0] ) && $atts[0] ) {
155 $attr['src'] = $atts[0];
156 }
157
158 if ( $attr['src'] && preg_match( '!https?://(docs|drive|spreadsheets\d*|calendar|www)*\.google\.com/([-\w\./]+)\?([^"]+)!', $attr['src'], $matches ) ) {
159 $attr['domain'] = $matches[1];
160 $attr['dir'] = $matches[2];
161 parse_str( htmlspecialchars_decode( $matches[3] ), $query_ar );
162 $query_ar['chrome'] = 'false';
163 $query_ar['embedded'] = 'true';
164 $attr['query'] = http_build_query( $query_ar );
165 }
166
167 if ( ! googleapps_validate_domain_and_dir( $attr['domain'], $attr['dir'] ) ) {
168 return '<!-- Unsupported URL -->';
169 }
170
171 $attr['query'] = $attr['dir'] . '?' . $attr['query'];
172
173 /** This action is documented in modules/widgets/social-media-icons.php */
174 do_action( 'jetpack_bump_stats_extras', 'embeds', googleapps_service_name( $attr['domain'], $attr['dir'] ) );
175
176 return sprintf(
177 '<iframe src="%s" frameborder="0" width="%s" height="%s" marginheight="0" marginwidth="0"></iframe>',
178 esc_url( 'https://' . $attr['domain'] . '.google.com/' . $attr['query'] ),
179 esc_attr( $attr['width'] ),
180 esc_attr( $attr['height'] )
181 );
182 }
183
184 /**
185 * Check that the domain blogs to a Google Apps domain.
186 *
187 * @since 4.5.0
188 *
189 * @param string $domain
190 * @param string $dir
191 *
192 * @return bool
193 */
194 function googleapps_validate_domain_and_dir( $domain, $dir ) {
195 if ( ! in_array( $domain, array( 'docs', 'drive', 'www', 'spreadsheets', 'calendar' ) ) ) {
196 return false;
197 }
198
199 // Calendars
200 if ( ( 'www' === $domain || 'calendar' === $domain ) && 'calendar/' !== substr( $dir, 0, 9 ) ) {
201 return false;
202 }
203
204 // Docs
205 if ( in_array( $domain, array( 'docs', 'drive' ) ) && ! preg_match( '![-\.\w/]*(presentation/embed|presentation/d/(.*)|present/embed|document/pub|spreadsheets/d/(.*)|document/d/(e/)?[\w-]+/pub|file/d/[\w-]+/preview|viewer|forms/d/(.*)/viewform|spreadsheet/\w+)$!', $dir ) ) {
206 return false;
207 }
208
209 // Spreadsheets
210 if ( 'spreadsheets' == $domain && ! preg_match( '!^([-\.\w/]+/pub|[-\.\w/]*embeddedform)$!', $dir ) ) {
211 return false;
212 }
213
214 return true;
215 }
216
217 /**
218 * Get the name of the service we'll be embedding.
219 *
220 * @since 4.5.0
221 *
222 * @param string $domain
223 * @param string $dir
224 *
225 * @return string
226 */
227 function googleapps_service_name( $domain, $dir ) {
228 switch ( $domain ) {
229 case 'drive':
230 case 'docs':
231 $service_name = ( 'present/embed' == $dir ) ? 'googledocs_presentation' : 'googledocs_document';
232 break;
233 case 'spreadsheets':
234 $service_name = ( 'embeddedform' == $dir ) ? 'googledocs_form' : 'googledocs_spreadsheet';
235 break;
236 case 'calendar':
237 default:
238 $service_name = 'google_calendar';
239 }
240
241 return $service_name;
242 }
243