/functions-admin.php

  1. <?php 
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) { exit; } 
  4.  
  5. /** PROFILES ****/ 
  6.  
  7. /** 
  8. * Write profile from form data 
  9. * 
  10. * @since 2.5.0.1 
  11. * @return bool Whether or not action successful 
  12. */ 
  13. function gde_form_to_profile( $pid, $data ) { 
  14.  
  15. // get current profile data 
  16. $profile = gde_get_profiles( $pid, false ); 
  17.  
  18. // initialize checkbox values (values if options unchecked) 
  19. $profile['tb_flags'] = "przn"; 
  20. $profile['tb_fullwin'] = "same"; 
  21. $profile['tb_fulluser'] = "no"; 
  22. $profile['tb_print'] = "no"; 
  23. $profile['vw_flags'] = ""; 
  24. $profile['link_force'] = "no"; 
  25. $profile['link_mask'] = "no"; 
  26. $profile['link_block'] = "no"; 
  27.  
  28. // enforce trailing slash on base_url 
  29. $data['base_url'] = trailingslashit( $data['base_url'] ); 
  30.  
  31. // sanitize width/height 
  32. $data['default_width'] = gde_sanitize_dims( $data['default_width'] ); 
  33. $data['default_height'] = gde_sanitize_dims( $data['default_height'] ); 
  34. if ( ! $data['default_width'] ) { 
  35. $data['default_width'] = $profile['default_width']; 
  36. if ( ! $data['default_height'] ) { 
  37. $data['default_height'] = $profile['default_height']; 
  38.  
  39. foreach ( $data as $k => $v ) { 
  40. if ( array_key_exists( $k, $profile ) ) { 
  41. // all fields where name == profile key 
  42. $profile[$k] = stripslashes( $v ); 
  43. } elseif ( strstr( $k, 'gdet_' ) && ( strstr( $v, 'gdet_' ) ) ) { 
  44. // toolbar checkboxes 
  45. if ( $k == 'gdet_h' ) { 
  46. $profile['tb_flags'] .= "h"; 
  47. } else { 
  48. $profile['tb_flags'] = str_replace( str_replace( "gdet_", "", $v ), "", $profile['tb_flags'] ); 
  49. } elseif ( $k == "fs_win" ) { 
  50. $profile['tb_fullwin'] = "new"; 
  51. } elseif ( $k == "fs_user" ) { 
  52. $profile['tb_fulluser'] = "yes"; 
  53. } elseif ( $k == "fs_print" ) { 
  54. $profile['tb_print'] = "yes"; 
  55. } elseif ( strstr( $k, 'gdev_' ) && ( strstr( $v, 'gdev_' ) ) ) { 
  56. $profile['vw_flags'] .= str_replace( "gdev_", "", $v ); 
  57. } elseif ( $k == "force" ) { 
  58. $profile['link_force'] = "yes"; 
  59. } elseif ( $k == "mask" ) { 
  60. $profile['link_mask'] = "yes"; 
  61. } elseif ( $k == "block" && gde_is_blockable( $profile ) ) { 
  62. $profile['link_block'] = "yes"; 
  63.  
  64. $newprofile = array( '', '', serialize( $profile ) ); 
  65. if ( gde_write_profile( $newprofile, $pid, true ) > 0 ) { 
  66. // update successful 
  67. return true; 
  68. } else { 
  69. return false; 
  70.  
  71. /** 
  72. * Make new profile (from existing) 
  73. * 
  74. * @since 2.5.0.1 
  75. * @return bool Whether or not action successful 
  76. */ 
  77. function gde_profile_to_profile( $sourceid, $name, $desc = '' ) { 
  78. global $wpdb; 
  79. $table = $wpdb->prefix . 'gde_profiles'; 
  80.  
  81. if ( $sourcedata = $wpdb->get_row( $wpdb->prepare( "SELECT profile_data FROM $table WHERE profile_id = %d", $sourceid ), ARRAY_A ) ) { 
  82. $newprofile = array( $name, $desc, $sourcedata['profile_data'] ); 
  83. if ( gde_write_profile( $newprofile ) > 0 ) { 
  84. return true; 
  85. } else { 
  86. return false; 
  87. } else { 
  88. return false; 
  89.  
  90. /** 
  91. * Create/update profile 
  92. * 
  93. * @since 2.5.0.1 
  94. * @return int 0 = fail, 1 = created, 2 = updated, 3 = nothing to do 
  95. * @note data array expected: [0] name, [1] desc, [2] serialized data 
  96. */ 
  97. function gde_write_profile( $data, $id = null, $overwrite = false ) { 
  98. global $wpdb; 
  99. $table = $wpdb->prefix . 'gde_profiles'; 
  100.  
  101. if ( empty( $id ) ) { 
  102. // get profile name 
  103. $pname = strtolower( $data[0] ); 
  104.  
  105. // new (non-default) profile 
  106. if ( ! $wpdb->insert( 
  107. $table,  
  108. array( 
  109. 'profile_name' => $pname,  
  110. 'profile_desc' => $data[1],  
  111. 'profile_data' => $data[2] 
  112. ) ) { 
  113. gde_dx_log("Failed to create profile '$pname'"); 
  114. return 0; 
  115. } else { 
  116. gde_dx_log("New profile '$pname' created"); 
  117. return 1; 
  118. } else { 
  119. // new (default) or updated profile 
  120. if ( is_null( $wpdb->get_row( "SELECT * FROM $table WHERE profile_id = $id" ) ) ) { 
  121. // new default profile 
  122. //gde_dx_log("Profile ID $id doesn't exist - creating"); 
  123.  
  124. if ( ! $wpdb->insert( 
  125. $table,  
  126. array( 
  127. 'profile_id' => $id,  
  128. 'profile_name' => strtolower( $data[0] ),  
  129. 'profile_desc' => $data[1],  
  130. 'profile_data' => $data[2] 
  131. ),  
  132. array( 
  133. '%d', '%s', '%s', '%s' 
  134. ) ) { 
  135. gde_dx_log("Profile $id creation failed"); 
  136. return 0; 
  137. } else { 
  138. gde_dx_log("Profile $id created"); 
  139. return 1; 
  140. } elseif ( $overwrite ) { 
  141. // get old data 
  142. $olddata = gde_get_profiles( $id, false, true ); 
  143. $olddesc = $olddata['profile_desc']; 
  144. unset( $olddata['profile_desc'] ); 
  145.  
  146. // update profile 
  147. gde_dx_log("Profile ID $id exists - updating"); 
  148.  
  149. if ( ! empty( $data[0] ) ) { 
  150. // overwrite name 
  151. $newdata['profile_name'] = strtolower( $data[0] ); 
  152. if ( ! empty( $data[1] ) && ( $data[1] !== $olddesc ) ) { 
  153. // overwrite description 
  154. $newdata['profile_desc'] = $data[1]; 
  155.  
  156. if ( ! empty( $data[2] ) && ( $data[2] !== serialize( $olddata ) ) ) { 
  157. // overwrite data 
  158. $newdata['profile_data'] = $data[2]; 
  159.  
  160. if ( isset( $newdata ) ) { 
  161. if ( ! $wpdb->update( 
  162. $table,  
  163. $newdata,  
  164. array( 'profile_id' => $id ),  
  165. array( 
  166. '%s', '%s', '%s' 
  167. ) ) { 
  168. $info = print_r($newdata, true); 
  169. gde_dx_log("Profile $id update failed writing: \n\n $info"); 
  170. return 0; 
  171. } else { 
  172. gde_dx_log("Profile $id updated"); 
  173. return 2; 
  174. } else { 
  175. gde_dx_log("Overwrite requested but no changes found"); 
  176. return 3; 
  177. } else { 
  178. gde_dx_log("Profile $id exists, overwrite not specified - nothing changed"); 
  179. return 3; 
  180.  
  181. /** 
  182. * Delete profile 
  183. * 
  184. * @since 2.5.0.1 
  185. * @return bool Whether or not action successful 
  186. */ 
  187. function gde_delete_profile( $id ) { 
  188. global $wpdb; 
  189. $table = $wpdb->prefix . 'gde_profiles'; 
  190.  
  191. if ( $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE profile_id = %d", $id ) ) > 0 ) { 
  192. return true; 
  193. } else { 
  194. return false; 
  195.  
  196. /** 
  197. * Check for duplicate profile name 
  198. * 
  199. * @since 2.5.0.2 
  200. * @return int Profile id of name or -1 if no match 
  201. */ 
  202. function gde_profile_name_exists( $name ) { 
  203. global $wpdb; 
  204. $table = $wpdb->prefix . 'gde_profiles'; 
  205.  
  206. if ( $id = $wpdb->get_row( $wpdb->prepare( "SELECT profile_id FROM $table WHERE profile_name = %s", $name ), ARRAY_A ) ) { 
  207. return (int) $id['profile_id']; 
  208. } else { 
  209. return -1; 
  210.  
  211. /** 
  212. * Make existing profile data default (overwrite current default) 
  213. * 
  214. * @since 2.5.0.1 
  215. * @return bool Whether or not action successful 
  216. */ 
  217. function gde_overwrite_profile( $sourceid ) { 
  218. global $wpdb; 
  219. $table = $wpdb->prefix . 'gde_profiles'; 
  220.  
  221. if ( $data = $wpdb->get_row( $wpdb->prepare( "SELECT profile_data FROM $table WHERE profile_id = %d", $sourceid ), ARRAY_A ) ) { 
  222. if ( $wpdb->update ( $table, $data, array( 'profile_id' => 1 ), array( '%s' ) ) ) { 
  223. return true; 
  224. } else { 
  225. return false; 
  226. } else { 
  227. return false; 
  228.  
  229. /** 
  230. * Process profile/settings import file 
  231. * 
  232. * @since 2.5.0.3 
  233. * @return void 
  234. */ 
  235. function gde_import( $data ) { 
  236. $label = __('Import', 'gde'); 
  237. $status = array(); 
  238.  
  239. echo ' 
  240. <div class="wrap"> 
  241. <div class="icon32" id="icon-plugins"></div> 
  242. <h2>Google Doc Embedder ' . $label . '</h2> 
  243. '; 
  244.  
  245. if ( isset( $data['profiles'] ) || isset( $data['profiles'] ) ) { 
  246. // full import 
  247. echo "<p>" . __('Performing full import...', 'gde') . "</p>\n"; 
  248.  
  249. // profiles import 
  250. if ( isset( $data['profiles'] ) ) { 
  251. echo "<p>" . __('Importing profiles', 'gde'); 
  252.  
  253. $success = gde_import_profiles( $data['profiles'] ); 
  254. $total = count( $data['profiles'] ); 
  255. echo " ($success/$total)... "; 
  256. if ( $success == $total ) { 
  257. echo __('done', 'gde') . ".</p>\n"; 
  258. } else { 
  259. $status[] = "fail"; 
  260. echo "<strong>" . __('failed', 'gde') . ".</strong></p>\n"; 
  261.  
  262. // settings import 
  263. if ( isset( $data['settings'] ) ) { 
  264. echo "<p>" . __('Importing settings', 'gde') . "... "; 
  265. if ( ! gde_import_settings( $data['settings'] ) ) { 
  266. $status[] = "fail"; 
  267. echo "<strong>" . __('failed', 'gde') . ".</strong></p>\n"; 
  268. } else { 
  269. echo __('done', 'gde') . ".</p>\n"; 
  270. } elseif ( isset( $data[0]['profile_id'] ) ) { 
  271. // profile import 
  272. echo "<p>" . __('Importing profiles', 'gde'); 
  273.  
  274. $success = gde_import_profiles( $data ); 
  275. $total = count( $data ); 
  276. echo " ($success/$total)... "; 
  277. if ( $success == $total ) { 
  278. echo __('done', 'gde') . ".</p>\n"; 
  279. } else { 
  280. $status[] = "fail"; 
  281. echo "<strong>" . __('failed', 'gde') . ".</strong></p>\n"; 
  282. } elseif ( isset( $data['ed_disable'] ) ) { 
  283. // settings import 
  284. echo "<p>" . __('Importing settings... ', 'gde'); 
  285.  
  286. if ( ! gde_import_settings( $data ) ) { 
  287. $status[] = "fail"; 
  288. echo "<strong>" . __('failed', 'gde') . ".</strong></p>\n"; 
  289. } else { 
  290. echo __('done', 'gde') . ".</p>\n"; 
  291. } else { 
  292. echo "<p>" . __('Please select a valid export file to import.', 'gde') . "</p>\n"; 
  293.  
  294. if ( in_array( 'fail', $status ) ) { 
  295. echo "<p>" . __('All or part of the import failed. See above for information.', 'gde') . "</p>\n"; 
  296. } else { 
  297. echo "<p>" . __('Import completed successfully.', 'gde') . "</p>\n"; 
  298.  
  299. echo "<p><a href=''>" . __('Return to GDE Settings', 'gde') . "</a></p>\n"; 
  300. echo "</div>\n"; 
  301.  
  302. /** 
  303. * Process settings import data 
  304. * 
  305. * @since 2.5.0.3 
  306. * @return bool Whether or not settings import succeeded 
  307. */ 
  308. function gde_import_settings( $data ) { 
  309. global $gdeoptions; 
  310.  
  311. $current = $gdeoptions; 
  312. unset( $current['api_key'] ); 
  313.  
  314. if ( $current == $data ) { 
  315. // nothing to do 
  316. return true; 
  317. } else { 
  318. foreach ( $data as $k => $v ) { 
  319. $gdeoptions[$k] = $v; 
  320.  
  321. if ( update_option( 'gde_options', $gdeoptions ) ) { 
  322. return true; 
  323. } else { 
  324. return false; 
  325.  
  326. function gde_import_profiles( $data ) { 
  327. $success = 0; 
  328.  
  329. foreach ( $data as $v ) { 
  330. $pid = gde_profile_name_exists( $v['profile_name'] ); 
  331. if ( $pid !== -1 ) { 
  332. // overwrite existing profile 
  333. $prodata = array( '', $v['profile_desc'], $v['profile_data'] ); 
  334. if ( gde_write_profile( $prodata, $pid, true ) > 0 ) { 
  335. $success++; 
  336. } else { 
  337. gde_dx_log("failure importing to overwrite profile $pid"); 
  338. } else { 
  339. // write as new profile 
  340. $prodata = array( $v['profile_name'], $v['profile_desc'], $v['profile_data'] ); 
  341. if ( gde_write_profile( $prodata ) > 0 ) { 
  342. $success++; 
  343. } else { 
  344. gde_dx_log("failure importing to new profile"); 
  345.  
  346. return $success; 
  347.  
  348. /** SETTINGS ****/ 
  349.  
  350. /** 
  351. * Get locale 
  352. * 
  353. * @since 2.4.1.1 
  354. * @return string Google viewer lang code based on WP_LANG setting, or en_US if not defined 
  355. */ 
  356. function gde_get_locale() { 
  357. $locale = get_locale(); 
  358.  
  359. require_once( GDE_PLUGIN_DIR . 'libs/lib-langs.php' ); 
  360. return gde_mapped_langs( $locale ); 
  361.  
  362. function gde_option_page() { 
  363. global $gde_settings_page, $gdeoptions; 
  364.  
  365. $gde_settings_page = add_options_page( 'GDE '.__('Settings', 'gde'), 'GDE '.__('Settings', 'gde'), 'manage_options', 'gde-settings', 'gde_options' ); 
  366.  
  367. // enable custom styles and settings jQuery 
  368. add_action( 'admin_print_styles', 'gde_admin_custom_css' ); 
  369. add_action( 'admin_enqueue_scripts', 'gde_admin_custom_js' ); 
  370.  
  371. function gde_options() { 
  372. if (! current_user_can('manage_options') ) wp_die('You don\'t have access to this page.'); 
  373. if (! user_can_access_admin_page()) wp_die( __('You do not have sufficient permissions to access this page', 'gde') ); 
  374.  
  375. require( GDE_PLUGIN_DIR . 'options.php' ); 
  376. add_action('in_admin_footer', 'gde_admin_footer'); 
  377.  
  378. /** 
  379. function gde_site_option_page() { 
  380. global $gde_global_page; 
  381.  
  382. $gde_global_page = add_submenu_page( 'settings.php', 'GDE '.__('Settings', 'gde'), 'GDE '.__('Settings', 'gde'), 'manage_network_options', basename(__FILE__), 'gde_site_options' ); 
  383.  
  384. // enable custom styles and settings jQuery 
  385. //add_action( 'admin_print_styles', 'gde_admin_custom_css' ); 
  386. //add_action( 'admin_enqueue_scripts', 'gde_admin_custom_js' ); 
  387.  
  388. function gde_site_options() { 
  389. //if ( function_exists('current_user_can') && !current_user_can('manage_options') ) wp_die('You don\'t have access to this page.'); 
  390. //if (! user_can_access_admin_page()) wp_die( __('You do not have sufficient permissions to access this page', 'gde') ); 
  391.  
  392. require( GDE_PLUGIN_DIR . 'site-options.php' ); 
  393. add_action( 'in_admin_footer', 'gde_admin_footer' ); 
  394. */ 
  395.  
  396. /** 
  397. * Get Default Base URL 
  398. * 
  399. * @since 2.5.0.1 
  400. * @return string Default base URL based on WP settings 
  401. */ 
  402. function gde_base_url() { 
  403. if ( ! $baseurl = get_option( 'upload_url_path' ) ) { 
  404. $uploads = wp_upload_dir(); 
  405. $baseurl = $uploads['baseurl']; 
  406.  
  407. return trailingslashit( $baseurl ); 
  408.  
  409. /** 
  410. * Display tabs 
  411. * 
  412. * @since 2.5.0.1 
  413. * @return void 
  414. */ 
  415. function gde_show_tab( $name ) { 
  416. $tabfile = GDE_PLUGIN_DIR . "libs/tab-$name.php"; 
  417.  
  418. if ( file_exists( $tabfile ) ) { 
  419. include_once( $tabfile ); 
  420.  
  421. /** 
  422. * Reset global (multisite) options 
  423. * 
  424. * @since 2.5.0.1 
  425. * @return void 
  426. */ 
  427. /** 
  428. function gde_global_reset() { 
  429. global $gdeglobals; 
  430.  
  431. // by default, global settings are empty 
  432. if ($gdeglobals) { 
  433. delete_site_option('gde_globals'); 
  434. } 
  435. } 
  436. */ 
  437.  
  438. /** 
  439. * Delete autoexpire secure docs 
  440. * 
  441. * @since 2.5.0.1 
  442. * @note Runs via wp-cron according to schedule defined in lib-setup 
  443. * @return void 
  444. */ 
  445. /** 
  446. function gde_sec_cleanup() { 
  447. global $wpdb; 
  448.  
  449. $table = $wpdb->prefix . 'gde_secure'; 
  450. $wpdb->query( "DELETE FROM $table WHERE autoexpire = 'Y'" ); 
  451. gde_dx_log("Cleanup ran"); 
  452. } 
  453. */ 
  454.  
  455. /** 
  456. * Include custom css for settings pages 
  457. * 
  458. * @since 2.5.0.1 
  459. * @return void 
  460. */ 
  461. function gde_admin_custom_css( $hook ) { 
  462. if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'gde-settings' ) ) { 
  463. $css = GDE_PLUGIN_URL . 'css/admin-styles.css'; 
  464. wp_enqueue_style( 'gde_css', $css ); 
  465.  
  466. function gde_admin_footer() { 
  467. global $pdata; 
  468.  
  469. $plugin_str = __('plugin', 'gde'); 
  470. $version_str = __('Version', 'gde'); 
  471. printf( '%1$s %2$s | %3$s %4$s<br />', $pdata['Title'], $plugin_str, $version_str, $pdata['Version'] ); 
  472.  
  473. function gde_show_msg( $message, $error = false ) { 
  474. if ( $error ) { $class = "error"; } else { $class = "updated"; } 
  475. echo '<div id="message" class="'.$class.'"><p>' . $message . '</p></div>'; 
  476.  
  477. // add additional links, for convenience 
  478. function gde_actlinks( $links ) {  
  479. $settings_link = '<a href="options-general.php?page=gde-settings">' . __('Settings', 'gde') . '</a>';  
  480. array_unshift( $links, $settings_link );  
  481. return $links;  
  482.  
  483. function gde_admin_print_scripts( $arg ) { 
  484. global $pagenow; 
  485. if (is_admin() && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ) { 
  486. $js = GDE_PLUGIN_URL . 'js/gde-quicktags.js'; 
  487. wp_enqueue_script( 'gde_qts', $js, array('quicktags') ); 
  488.  
  489. function gde_admin_custom_js( $hook ) { 
  490. global $gde_settings_page, $gde_global_page, $pagenow; 
  491.  
  492. if ( $gde_settings_page == $hook || $gde_global_page == $hook ) { 
  493. $js['gde_clr'] = GDE_PLUGIN_URL . 'js/jscolor/jscolor.js'; 
  494. $js['gde_jqs'] = GDE_PLUGIN_URL . 'js/gde-jquery.js'; 
  495. foreach ( $js as $key => $script ) { 
  496. wp_enqueue_script( $key, $script ); 
  497. // localize 
  498. wp_localize_script( 'gde_jqs', 'jqs_vars', array ( 
  499. // internal use 
  500. 'gde_url' => GDE_PLUGIN_URL,  
  501. // profiles tab 
  502. 'delete' => __('This profile will be permanently deleted.', 'gde') . "\n\n" . __('Are you sure?', 'gde'),  
  503. 'default' => __('Settings for this profile will overwrite the default profile.', 'gde') . "\n\n" . __('Are you sure?', 'gde'),  
  504. 'reset' => __('Your profile list will be reset to its original state. All changes will be lost.', 'gde') . "\n\n" . __('Are you sure?', 'gde'),  
  505. // advanced tab 
  506. 'badimport' => __('Please select a valid export file to import.', 'gde'),  
  507. 'warnimport' => __('Any settings or duplicate profile names in this import will overwrite the current values.', 'gde') . "\n\n" . __('Are you sure?', 'gde'),  
  508. // support tab 
  509. 'baddebug' => __('Please include a shortcode or message to request support.', 'gde') 
  510. ); 
  511.  
  512. /** MEDIA LIBRARY & EDITOR INTEGRATION ****/ 
  513.  
  514. /** 
  515. * Select embed behavior and profile from Media Library 
  516. * 
  517. * @since 2.5.0.1 
  518. * @note Doesn't work in WP 3.5+ (not called in those versions) 
  519. * @return array Form fields for attachment form 
  520. */ 
  521. function gde_attachment_fields_to_edit( $form_fields, $post ) { 
  522. global $gdeoptions, $pagenow; 
  523.  
  524. if ( $pagenow == "media.php" ) { 
  525. // attachment info page, added fields not relevant 
  526. return $form_fields; 
  527.  
  528. $supported_exts = gde_supported_types(); 
  529.  
  530. if ( in_array( $post->post_mime_type, $supported_exts ) ) { 
  531. // file is supported, show fields 
  532.  
  533. if ( $gdeoptions['ed_embed_sc'] == "yes" ) { 
  534. $use_sc = true; 
  535. } else { 
  536. $use_sc = false; 
  537.  
  538. $checked = ( $use_sc ) ? 'checked' : ''; 
  539. $form_fields['gde_use_sc'] = array( 
  540. 'label' => 'Google Doc Embedder',  
  541. 'input' => 'html',  
  542. 'html' => "<input type='checkbox' {$checked} name='attachments[{$post->ID}][gde_use_sc]' id='attachments[{$post->ID}][gde_use_sc]' /> " . __('Embed file using Google Doc Embedder', 'gde'),  
  543. 'value' => $use_sc 
  544. ); 
  545.  
  546. // get profiles 
  547. $profiles = gde_get_profiles(); 
  548.  
  549. $html = "<select name='attachments[{$post->ID}][gde_profile]' id='attachments[{$post->ID}][gde_profile]' style='height:2em;'>"; 
  550. foreach ( $profiles as $p ) { 
  551. $html .= "\t<option value='".$p['profile_id']."'>".$p['profile_name']."</option>\n"; 
  552. $html .= "</select>"; 
  553.  
  554. $form_fields['gde_profile'] = array( 
  555. 'label' => '',  
  556. 'input' => 'html',  
  557. 'html' => $html,  
  558. 'helps' => __('Select the GDE viewer profile to use', 'gde') 
  559. ); 
  560.  
  561. return $form_fields; 
  562.  
  563. /** 
  564. * Modify the file insertion from Media Library if requested 
  565. * 
  566. * @since 2.4.0.1 
  567. * @note Doesn't work in WP 3.5+ (not called in those versions) 
  568. * @return string HTML to insert into editor 
  569. */ 
  570. function gde_media_insert( $html, $id, $attachment ) { 
  571. if ( isset( $attachment['gde_use_sc'] ) && $attachment['gde_use_sc'] == "on" ) { 
  572. $output = '[gview file="' . $attachment['url'] . '"'; 
  573. if ( isset( $attachment['gde_profile'] ) && $attachment['gde_profile'] !== "1" ) { 
  574. $output .= ' profile="' . $attachment['gde_profile'] . '"]'; 
  575. } else { 
  576. $output .= ']'; 
  577. return $output; 
  578. } else { 
  579. return $html; 
  580.  
  581. function gde_media_insert_35( $html, $id, $attachment ) { 
  582. global $gdeoptions; 
  583.  
  584. if ( gde_valid_type( $attachment['url'] ) && $gdeoptions['ed_embed_sc'] == "yes" ) { 
  585. return '[gview file="' . $attachment['url'] . '"]'; 
  586. } else { 
  587. return $html; 
  588.  
  589. /** 
  590. * Add upload support for natively unsupported mimetypes used by this plugin 
  591. * 
  592. * @since 2.4.0.1 
  593. * @return array Updated array of allowed upload types 
  594. */ 
  595. function gde_upload_mimes( $existing_mimes = array() ) { 
  596. $supported_exts = gde_supported_types(); 
  597.  
  598. foreach ( $supported_exts as $ext => $mimetype ) { 
  599. if ( ! array_key_exists( $ext, gde_mimes_expanded( $existing_mimes ) ) ) { 
  600. $existing_mimes[$ext] = $mimetype; 
  601. return gde_mimes_collapsed( $existing_mimes ); 
  602.  
  603. function gde_mimes_expanded( array $types ) { 
  604. // expand the supported mime types so that every ext is its own key 
  605. foreach ( $types as $k => $v ) { 
  606. if ( strpos( "|", $k ) ) { 
  607. $subtypes = explode( "|", $k ); 
  608. foreach ( $subtypes as $type ) { 
  609. $newtypes[$type] = $v; 
  610. unset( $types[$k] ); 
  611. $types = array_merge( $types, $newtypes ); 
  612. return $types; 
  613.  
  614. function gde_mimes_collapsed( $types ) { 
  615. // collapes the supported mime types so that each mime is listed once with combined key (default) 
  616. $newtypes = array(); 
  617.  
  618. foreach ( $types as $k => $v ) { 
  619. if ( isset( $newtypes[$v] ) ) { 
  620. $newtypes[$v] .= '|' . $k; 
  621. } else { 
  622. $newtypes[$v] = $k; 
  623. return array_flip( $newtypes ); 
  624.  
  625. /** 
  626. * Add TinyMCE button 
  627. * 
  628. * @since 2.0.0.1 
  629. * @return void 
  630. */ 
  631. function gde_mce_addbuttons() { 
  632. // don't bother doing this stuff if the current user lacks permissions 
  633. if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) 
  634. return; 
  635.  
  636. // add only in Rich Editor mode 
  637. if ( get_user_option('rich_editing') == 'true') { 
  638. add_filter("mce_external_plugins", "gde_add_tinymce_plugin"); 
  639. add_filter('mce_buttons', 'gde_register_mce_button'); 
  640.  
  641. function gde_add_tinymce_plugin( $plugin_array ) { 
  642. // load the TinyMCE plugin 
  643. $plugin_array['gde'] = GDE_PLUGIN_URL . 'js/editor_plugin.js'; 
  644. return $plugin_array; 
  645.  
  646. function gde_register_mce_button( $buttons ) { 
  647. array_push( $buttons, "separator", "gde" ); 
  648. return $buttons; 
  649.  
  650. /** BETA CHECKING ****/ 
  651.  
  652. /** 
  653. * Check current beta status 
  654. * 
  655. * @since 2.5.0.1 
  656. * @return bool Whether or not the currently running version is a beta 
  657. */ 
  658. function gde_is_beta() { 
  659. global $pdata; 
  660.  
  661. // check for currently running beta version (contains any letter or hyphen) 
  662. if ( preg_match( '/[a-z-]/i', $pdata['Version'] ) ) { 
  663. // running a beta 
  664. return true; 
  665. } else { 
  666. return false; 
  667.  
  668. /** 
  669. * Display beta status 
  670. * 
  671. * @since 2.5.0.1 
  672. * @return void 
  673. */ 
  674. function gde_warn_on_plugin_page( $plugin_file ) { 
  675. global $pdata; 
  676.  
  677. if ( strstr( $plugin_file, $pdata['mainfile'] ) ) { 
  678.  
  679. // see if there's a release waiting first (prevent double messages) 
  680. $updates = (array) get_site_option( '_site_transient_update_plugins' ); 
  681. if ( isset( $updates['response'] ) && array_key_exists( $pdata['basename'], $updates['response'] ) ) { 
  682. return; 
  683.  
  684. if ( gde_is_beta() ) { 
  685. $message[] = __('You are running a pre-release version of Google Doc Embedder. Please watch this space for important updates.', 'gde'); 
  686. } else { 
  687. $message = array(); 
  688.  
  689. // print message if any 
  690. $message = rtrim( implode( " ", $message ) ); 
  691. if ( ! empty( $message ) ) { 
  692. // style improvements?? 
  693. //add_action( 'admin_enqueue_scripts', 'gde_admin_beta_js' ); 
  694.  
  695. print(' 
  696. <tr class="plugin-update-tr"> 
  697. <td colspan="3" class="plugin-update colspanchange"> 
  698. <div class="update-message" style="background:#e3e3e3;"> 
  699. '.$message.' 
  700. </div> 
  701. </td> 
  702. </tr> 
  703. '); 
  704.  
  705. /** 
  706. * Run beta checking process 
  707. * 
  708. * @since 2.5.0.1 
  709. * @return bool True or false, there is a (newer) beta available 
  710. */ 
  711. function gde_check_for_beta( $plugin_file ) { 
  712. global $gdeoptions, $pdata; 
  713.  
  714. // beta checking is enabled 
  715. if ( $gdeoptions['beta_check'] == "yes" ) { 
  716.  
  717. if ( gde_beta_available() ) { 
  718.  
  719. require GDE_PLUGIN_DIR . 'libs/lib-betacheck.php'; 
  720. $betacheck = new PluginUpdateChecker( 
  721. GDE_BETA_API . 'beta-info/gde',  
  722. $plugin_file,  
  723. $pdata['slug'] 
  724. ); 
  725.  
  726. if ( ! $state = get_option('external_updates-' . $pdata['slug']) ) { 
  727. // get beta info if not cached 
  728. $betacheck->checkForUpdates(); 
  729. if ( ! $state = get_option('external_updates-' . $pdata['slug']) ) { 
  730. // something's wrong with the process - skip 
  731. gde_dx_log("Can't fetch beta info - skipping"); 
  732. return false; 
  733. } else { 
  734. if ( version_compare( $state->update->version, $pdata['Version'], '>' ) ) { 
  735. return true; 
  736. } elseif ( version_compare( $state->update->version, $pdata['Version'], '>' ) ) { 
  737. return true; 
  738.  
  739. // otherwise... 
  740. return false; 
  741.  
  742. /** 
  743. * Check to see if a beta is available (generally or to this install's API key) 
  744. * 
  745. * @since 2.5.0.1 
  746. * @return bool Whether or not a new beta is available 
  747. */ 
  748. function gde_beta_available() { 
  749. global $gdeoptions, $pdata; 
  750.  
  751. $key = 'gde_beta_version'; 
  752.  
  753. if ( $avail_version = get_site_transient( $key ) ) { 
  754. // transient already set - compare versions 
  755. if ( version_compare( $pdata['Version'], $avail_version, '>=' ) ) { 
  756. // installed version is same or newer, don't do anything 
  757. return false; 
  758. } else { 
  759. // transient is newer, get beta info (no version check necessary) 
  760. return true; 
  761. } else { 
  762. // beta status unknown - attempt to fetch 
  763. $api_url = GDE_BETA_API . "versions/gde"; 
  764.  
  765. if ( ! empty ( $gdeoptions['api_key'] ) ) { 
  766. $api_url .= '?api_key=' . $gdeoptions['api_key']; 
  767.  
  768. gde_dx_log("Performing remote beta check"); 
  769. $response = wp_remote_get( $api_url ); 
  770.  
  771. // set checking interval lower if currently running a beta 
  772. if ( gde_is_beta() ) { 
  773. $hours = 3; 
  774. } else { 
  775. $hours = 24; 
  776.  
  777. if ( ! is_wp_error( $response ) ) { 
  778. if ( $json = json_decode( wp_remote_retrieve_body( $response ) ) ) { 
  779. if ( isset( $json->beta->version ) ) { 
  780. $ver = $json->beta->version; 
  781. gde_dx_log("Beta detected: ".$ver); 
  782. if ( ! empty( $ver ) ) { 
  783. gde_dx_log("Beta detected, don't check again for $hours hours"); 
  784. set_site_transient( $key, $ver, 60*60*$hours ); 
  785.  
  786. // there is a beta available, let the checker decide if it's relevant 
  787. return true; 
  788. } else { 
  789. // no beta available - don't check again for x hours 
  790. gde_dx_log("No beta detected, check again in $hours hours"); 
  791. set_site_transient( $key, $pdata['Version'], 60*60*24 ); 
  792. return false; 
  793.  
  794. // otherwise (in case of retrieve failure) 
  795. return false; 
  796. }  
  797.  
  798. /** 
  799. * Include custom js for plugin page (beta notification) 
  800. * 
  801. * @since 2.5.0.1 
  802. * @return void 
  803. */ 
  804. function gde_admin_beta_js_update() { 
  805. global $pagenow; 
  806.  
  807. if ( current_user_can('activate_plugins' && $pagenow == 'plugins.php' ) ) { 
  808. $js = GDE_PLUGIN_URL . 'js/gde-betanotify.js'; 
  809. wp_enqueue_script( 'gde_betanotify', $js ); 
  810.  
  811. /** 
  812. * Check for existence and valid content of dx log 
  813. * 
  814. * @since 2.5.2.1 
  815. * @return bool 
  816. */ 
  817. function gde_log_available() { 
  818. global $wpdb; 
  819.  
  820. $table = $wpdb->base_prefix . 'gde_dx_log'; 
  821. $blogid = get_current_blog_id(); 
  822. $log = false; 
  823.  
  824. if ( $wpdb->get_var( "SHOW TABLES LIKE '$table'" ) == $table ) { 
  825. $c = $wpdb->get_var( "SELECT COUNT(*) FROM $table WHERE blogid = $blogid" ); 
  826. if ( $c > 0 ) { 
  827. $log = true; 
  828.  
  829. return $log; 
  830.  
  831. ?> 
.