gde_do_shortcode

The Google Doc Embedder gde do shortcode function.

Description

gde_do_shortcode( $atts ); 

Parameters (1)

0. $atts
The atts.

Usage

  1. if ( !function_exists( 'gde_do_shortcode' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-doc-embedder/gviewer.php'; 
  3.  
  4. // The atts. 
  5. $atts = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = gde_do_shortcode($atts); 
  9.  

Defined (1)

The function is defined in the following location(s).

/gviewer.php  
  1. function gde_do_shortcode( $atts ) { 
  2. global $healthy, $gdeoptions; //$gdeglobals 
  3.  
  4. // check profile table health 
  5. if ( ! $healthy ) { 
  6. delete_option('gde_db_version'); 
  7. return gde_show_error( __('Unable to load profile settings', 'google-document-embedder') ); 
  8.  
  9. // handle global setting overrides - not active in this release 
  10. /** 
  11. if ($gdeglobals['enforce_viewer'] == "std") { 
  12. $gdeoptions['disable_proxy'] = "yes"; 
  13. if ($gdeglobals['enforce_lang']) { 
  14. $gdeoptions['default_lang'] = $gdeglobals['enforce_lang']; 
  15. */ 
  16.  
  17. extract( shortcode_atts( array ( 
  18. 'file' => '',  
  19. 'profile' => 1, // default profile is always ID 1 
  20. 'save' => '',  
  21. 'width' => '',  
  22. 'height' => '',  
  23. 'cache' => '' 
  24. //'title' => '', // not yet implemented 
  25. //'page' => '', // support broken in Google Viewer 
  26. ), $atts ) ); 
  27.  
  28. // get requested profile data (or default if doesn't exist) 
  29. $term = $profile; 
  30. if ( is_numeric( $term ) ) { 
  31. // id-based lookup 
  32. if ( ! $profile = gde_get_profiles( $term ) ) { 
  33. gde_dx_log("Loading default profile instead"); 
  34. if ( ! $profile = gde_get_profiles( 1 ) ) { 
  35. return gde_show_error( __('Unable to load requested profile.', 'google-document-embedder') ); 
  36. } else { 
  37. $pid = 1; 
  38. } else { 
  39. $pid = $term; 
  40. } else { 
  41. // name-based lookup 
  42. if ( ! $profile = gde_get_profiles( strtolower( $term ) ) ) { 
  43. gde_dx_log("Loading default profile instead"); 
  44. if ( ! $profile = gde_get_profiles( 1 ) ) { 
  45. return gde_show_error( __('Unable to load requested profile.', 'google-document-embedder') ); 
  46. } else { 
  47. $pid = 1; 
  48. } else { 
  49. $pid = $profile['profile_id']; 
  50.  
  51. // use profile defaults if shortcode override not defined 
  52. if ( $save !== "0" ) { 
  53. if ( empty( $save ) ) { 
  54. $save = $profile['link_show']; 
  55. if ( empty( $width ) ) { 
  56. $width = $profile['default_width']; 
  57. if ( empty( $height ) ) { 
  58. $height = $profile['default_height']; 
  59. if ( $cache !== "0" ) { 
  60. if ( empty( $cache ) ) { 
  61. $cache = $profile['cache']; 
  62. //if ( $profile['language'] !== "en_US" ) { 
  63. $lang = $profile['language']; 
  64. //} 
  65.  
  66. // tweak the dimensions if necessary 
  67. $width = gde_sanitize_dims( $width ); 
  68. $height = gde_sanitize_dims( $height ); 
  69.  
  70. // add base url if needed 
  71. if ( ! preg_match( "/^http/i", $file ) ) { 
  72. if ( substr( $file, 0, 2 ) == "//" ) { 
  73. // append dynamic protocol 
  74. if ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ) { 
  75. $file = "https:" . $file; 
  76. } else { 
  77. $file = "http:" . $file; 
  78. } elseif ( isset( $profile['base_url'] ) ) { 
  79. // not a full link, add base URL if available 
  80. if ( substr( $file, 0, 1 ) == "/" ) { 
  81. // remove any preceding slash from doc (base URL adds it) 
  82. $file = ltrim( $file, '/' ); 
  83. $file = $profile['base_url'] . $file; 
  84.  
  85. // capture file details 
  86. $fn = basename( $file ); 
  87. $fnp = gde_split_filename( $fn ); 
  88.  
  89. // check for missing required field 
  90. if ( ! $file ) { 
  91. return gde_show_error( __('File not specified, check shortcode syntax', 'google-document-embedder') ); 
  92.  
  93. // file validation 
  94. if ( $gdeoptions['error_check'] == "no" ) { 
  95. $force = true; 
  96. } else { 
  97. $force = false; 
  98. $status = gde_validate_file( str_replace( " ", "%20", $file ), $force ); 
  99.  
  100. if ( ! isset( $code ) && ! is_array( $status ) && $status !== -1 ) { 
  101. // validation failed 
  102. $code = gde_show_error( $status ); 
  103. } elseif ( ! isset( $code ) ) { 
  104. // validation passed or was skipped 
  105.  
  106. // check for max filesize 
  107. $viewer = true; 
  108. if ( $gdeoptions['file_maxsize'] > 0 && isset( $status['fsize'] ) ) { 
  109. $maxbytes = (int) $gdeoptions['file_maxsize'] * 1024 * 1024; 
  110. if ( $status['fsize'] > $maxbytes ) { 
  111. $viewer = false; 
  112.  
  113. // generate links (embed, download) 
  114. $links = array( $file, $file ); 
  115. if ( $profile['link_block'] == "yes" && gde_is_blockable( $profile ) ) { 
  116. if ( $secure = gde_get_secure_url( $file ) ) { 
  117. $links[0] = $secure; 
  118. } else { 
  119. $links[0] = ''; 
  120. $links[1] = ''; 
  121. } elseif ( $profile['link_show'] !== "none" ) { 
  122. if ( $profile['link_force'] == "yes" && $profile['link_mask'] == "no" ) { 
  123. $links[1] = GDE_PLUGIN_URL . "load.php?d=" . urlencode( $links[1] ); 
  124. } elseif ( $profile['link_force'] == "no" && $profile['link_mask'] == "yes" ) { 
  125. $short = gde_get_short_url( $links[0] ); 
  126. $links[0] = $short; 
  127. $links[1] = $short; 
  128. } elseif ( $profile['link_force'] == "yes" && $profile['link_mask'] == "yes" ) { 
  129. $short = gde_get_short_url( GDE_PLUGIN_URL . "load.php?d=" . urlencode( $links[0] ) ); 
  130. $links[0] = $short; 
  131. $links[1] = $short; 
  132.  
  133. // obfuscate filename if cache disabled (globally or via shortcode) 
  134. // note that this is ignored if the document is secure to prevent each hit from generating a new db row 
  135. if ( ! empty( $links[1] ) && ( $cache == "off" || $cache == "0" ) ) { 
  136. $links[0] .= "?" . time(); 
  137.  
  138. // check for failed secure doc 
  139. if ( empty( $links[0] ) && empty( $links[1] ) ) { 
  140. $code = gde_show_error( __('Unable to secure document', 'google-document-embedder') ); 
  141. } else { 
  142.  
  143. // which viewer? 
  144. //if ( $profile['viewer'] == "enhanced" ) { 
  145. // $lnk = GDE_PLUGIN_URL . "view.php?url=" . urlencode( $links[0] ) . "&hl=" . $lang . "&gpid=" . $pid; 
  146. // make protocol-agnostic 
  147. // $lnk = preg_replace( '/^https?:/i', '', $lnk ); 
  148. //} else { 
  149. $lnk = "//docs.google.com/viewer?url=" . urlencode( $links[0] ) . "&hl=" . $lang; 
  150. //} 
  151.  
  152. // what mode? 
  153. //if ( $profile['tb_mobile'] == "always" ) { 
  154. // $lnk .= "&mobile=true"; 
  155. //} else { 
  156. $lnk .= "&embedded=true"; 
  157. //} 
  158.  
  159. // build viewer 
  160. if ( $viewer == false ) { 
  161. // exceeds max filesize 
  162. $vwr = ''; 
  163. } else { 
  164. $vwr = '<iframe src="%U%" class="gde-frame" style="width:%W%; height:%H%; border: none;"%ATTRS%></iframe>'; 
  165. $vwr = str_replace("%U%", $lnk, $vwr); 
  166. $vwr = str_replace("%W%", $width, $vwr); 
  167. $vwr = str_replace("%H%", $height, $vwr); 
  168.  
  169. // frame attributes 
  170. $vattr[] = ' scrolling="no"'; // iphone scrolling bug 
  171. //if ( ! empty( $page ) && is_numeric( $page ) ) { // selected starting page 
  172. // $page = (int) $page - 1; 
  173. // $vattr[] = ' onload="javascript:this.contentWindow.location.hash=\':0.page.' . $page . '\';"'; 
  174. //} 
  175. $vwr = str_replace( "%ATTRS%", implode( '', $vattr ), $vwr ); 
  176.  
  177. // show download link? 
  178. $allow_save = false; 
  179. if ( ! empty( $links[1] ) ) { // link empty = secure document; ignore any other save attribute 
  180. if ( $save == "all" || $save == "1" ) { 
  181. $allow_save = true; 
  182. } elseif ( $save == "users" && is_user_logged_in() ) { 
  183. $allow_save = true; 
  184.  
  185. if ( $allow_save ) { 
  186. // build download link 
  187. $linkcode = '<p class="gde-text"><a href="%LINK%" class="gde-link"%ATTRS%>%TXT%</a></p>'; 
  188. $linkcode = str_replace( "%LINK%", $links[1], $linkcode ); 
  189.  
  190. // fix type 
  191. $ftype = strtoupper( $fnp[1] ); 
  192. if ( $ftype == "TIF" ) {  
  193. $ftype = "TIFF"; 
  194.  
  195. // link attributes 
  196. if ( $profile['link_mask'] == "yes" ) { 
  197. $attr[] = ' rel="nofollow"'; 
  198. $attr[] = gde_ga_event( $file ); // GA integration 
  199. $linkcode = str_replace("%ATTRS%", implode( '', $attr ), $linkcode); 
  200.  
  201. // link text 
  202. if ( empty( $profile['link_text'] ) ) { 
  203. $profile['link_text'] = __('Download', 'google-document-embedder'); 
  204.  
  205. $dltext = str_replace( "%FILE", $fn, $profile['link_text'] ); 
  206. $dltext = str_replace( "%TYPE", $ftype, $dltext ); 
  207. $dltext = str_replace( "%SIZE", gde_format_bytes( $status['fsize'] ), $dltext ); 
  208.  
  209. $linkcode = str_replace( "%TXT%", $dltext, $linkcode ); 
  210. } else { 
  211. $linkcode = ''; 
  212.  
  213. // link position 
  214. if ( $profile['link_pos'] == "above" ) { 
  215. $code = $linkcode . "\n" . $vwr; 
  216. } else { 
  217. $code = $vwr . "\n" . $linkcode; 
  218.  
  219. return $code;