/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. /** SETTINGS ****/ 
  231.  
  232. /** 
  233. * Get locale 
  234. * 
  235. * @since 2.4.1.1 
  236. * @return string Google viewer lang code based on WP_LANG setting, or en_US if not defined 
  237. */ 
  238. function gde_get_locale() { 
  239. $locale = get_locale(); 
  240.  
  241. require_once( GDE_PLUGIN_DIR . 'libs/lib-langs.php' ); 
  242. return gde_mapped_langs( $locale ); 
  243.  
  244. function gde_option_page() { 
  245. global $gde_settings_page, $gdeoptions; 
  246.  
  247. $gde_settings_page = add_options_page( 'GDE '.__('Settings', 'google-document-embedder'), 'GDE '.__('Settings', 'google-document-embedder'), 'manage_options', 'gde-settings', 'gde_options' ); 
  248.  
  249. // enable custom styles and settings jQuery 
  250. add_action( 'admin_print_styles', 'gde_admin_custom_css' ); 
  251. add_action( 'admin_enqueue_scripts', 'gde_admin_custom_js' ); 
  252.  
  253. function gde_options() { 
  254. if (! current_user_can('manage_options') ) wp_die('You don\'t have access to this page.'); 
  255. if (! user_can_access_admin_page()) wp_die( __('You do not have sufficient permissions to access this page', 'google-document-embedder') ); 
  256.  
  257. require( GDE_PLUGIN_DIR . 'options.php' ); 
  258. add_action('in_admin_footer', 'gde_admin_footer'); 
  259.  
  260. /** 
  261. function gde_site_option_page() { 
  262. global $gde_global_page; 
  263.  
  264. $gde_global_page = add_submenu_page( 'settings.php', 'GDE '.__('Settings', 'google-document-embedder'), 'GDE '.__('Settings', 'google-document-embedder'), 'manage_network_options', basename(__FILE__), 'gde_site_options' ); 
  265.  
  266. // enable custom styles and settings jQuery 
  267. //add_action( 'admin_print_styles', 'gde_admin_custom_css' ); 
  268. //add_action( 'admin_enqueue_scripts', 'gde_admin_custom_js' ); 
  269.  
  270. function gde_site_options() { 
  271. //if ( function_exists('current_user_can') && !current_user_can('manage_options') ) wp_die('You don\'t have access to this page.'); 
  272. //if (! user_can_access_admin_page()) wp_die( __('You do not have sufficient permissions to access this page', 'google-document-embedder') ); 
  273.  
  274. require( GDE_PLUGIN_DIR . 'site-options.php' ); 
  275. add_action( 'in_admin_footer', 'gde_admin_footer' ); 
  276. */ 
  277.  
  278. /** 
  279. * Get Default Base URL 
  280. * 
  281. * @since 2.5.0.1 
  282. * @return string Default base URL based on WP settings 
  283. */ 
  284. function gde_base_url() { 
  285. if ( ! $baseurl = get_option( 'upload_url_path' ) ) { 
  286. $uploads = wp_upload_dir(); 
  287. $baseurl = $uploads['baseurl']; 
  288.  
  289. return trailingslashit( $baseurl ); 
  290.  
  291. /** 
  292. * Display tabs 
  293. * 
  294. * @since 2.5.0.1 
  295. * @return void 
  296. */ 
  297. function gde_show_tab( $name ) { 
  298. $tabfile = GDE_PLUGIN_DIR . "libs/tab-$name.php"; 
  299.  
  300. if ( file_exists( $tabfile ) ) { 
  301. include_once( $tabfile ); 
  302.  
  303. /** 
  304. * Reset global (multisite) options 
  305. * 
  306. * @since 2.5.0.1 
  307. * @return void 
  308. */ 
  309. /** 
  310. function gde_global_reset() { 
  311. global $gdeglobals; 
  312.  
  313. // by default, global settings are empty 
  314. if ($gdeglobals) { 
  315. delete_site_option('gde_globals'); 
  316. } 
  317. } 
  318. */ 
  319.  
  320. /** 
  321. * Delete autoexpire secure docs 
  322. * 
  323. * @since 2.5.0.1 
  324. * @note Runs via wp-cron according to schedule defined in lib-setup 
  325. * @return void 
  326. */ 
  327. /** 
  328. function gde_sec_cleanup() { 
  329. global $wpdb; 
  330.  
  331. $table = $wpdb->prefix . 'gde_secure'; 
  332. $wpdb->query( "DELETE FROM $table WHERE autoexpire = 'Y'" ); 
  333. gde_dx_log("Cleanup ran"); 
  334. } 
  335. */ 
  336.  
  337. /** 
  338. * Include custom css for settings pages 
  339. * 
  340. * @since 2.5.0.1 
  341. * @return void 
  342. */ 
  343. function gde_admin_custom_css( $hook ) { 
  344. global $wp_version; 
  345.  
  346. if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'gde-settings' ) ) { 
  347. if ( version_compare( $wp_version, '3.8-RC1', '>=' ) ) { 
  348. $css = GDE_PLUGIN_URL . 'css/admin-styles38.css'; 
  349. } else { 
  350. $css = GDE_PLUGIN_URL . 'css/admin-styles.css'; 
  351. wp_enqueue_style( 'gde_css', $css ); 
  352.  
  353. // native color picker 
  354. wp_enqueue_style( 'wp-color-picker' ); 
  355.  
  356. function gde_admin_footer() { 
  357. global $pdata; 
  358.  
  359. $plugin_str = __('plugin', 'google-document-embedder'); 
  360. $version_str = __('Version', 'google-document-embedder'); 
  361. printf( '%1$s %2$s | %3$s %4$s<br />', $pdata['Title'], $plugin_str, $version_str, $pdata['Version'] ); 
  362.  
  363. function gde_show_msg( $message, $error = false ) { 
  364. if ( $error ) { $class = "error"; } else { $class = "updated"; } 
  365. echo '<div id="message" class="'.$class.'"><p>' . $message . '</p></div>'; 
  366.  
  367. // add additional links, for convenience 
  368. function gde_actlinks( $links ) {  
  369. $settings_link = '<a href="options-general.php?page=gde-settings">' . __('Settings', 'google-document-embedder') . '</a>';  
  370. array_unshift( $links, $settings_link );  
  371. return $links;  
  372.  
  373. function gde_admin_print_scripts( $arg ) { 
  374. global $pagenow; 
  375. if (is_admin() && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ) { 
  376. $js = GDE_PLUGIN_URL . 'js/gde-quicktags.js'; 
  377. wp_enqueue_script( 'gde_qts', $js, array('quicktags') ); 
  378.  
  379. function gde_admin_custom_js( $hook ) { 
  380. global $gde_settings_page, $gde_global_page, $pagenow; 
  381.  
  382. if ( $gde_settings_page == $hook || $gde_global_page == $hook ) { 
  383. wp_enqueue_script( 'gde_jqs', GDE_PLUGIN_URL . 'js/gde-jquery.js', array( 'wp-color-picker' ), false, true ); 
  384.  
  385. // localize 
  386. wp_localize_script( 'gde_jqs', 'jqs_vars', array ( 
  387. // internal use 
  388. 'gde_url' => GDE_PLUGIN_URL,  
  389. // profiles tab 
  390. 'delete' => __('This profile will be permanently deleted.', 'google-document-embedder') . "\n\n" . __('Are you sure?', 'google-document-embedder'),  
  391. 'default' => __('Settings for this profile will overwrite the default profile.', 'google-document-embedder') . "\n\n" . __('Are you sure?', 'google-document-embedder'),  
  392. 'reset' => __('Your profile list will be reset to its original state. All changes will be lost.', 'google-document-embedder') . "\n\n" . __('Are you sure?', 'google-document-embedder'),  
  393. // advanced tab 
  394. 'badimport' => __('Please select a valid export file to import.', 'google-document-embedder'),  
  395. 'warnimport' => __('Any settings or duplicate profile names in this import will overwrite the current values.', 'google-document-embedder') . "\n\n" . __('Are you sure?', 'google-document-embedder'),  
  396. // support tab 
  397. 'baddebug' => __('Please include a shortcode or message to request support.', 'google-document-embedder') 
  398. ); 
  399.  
  400. /** MEDIA LIBRARY & EDITOR INTEGRATION ****/ 
  401.  
  402. /** 
  403. * Modify the file insertion from Media Library if requested 
  404. * 
  405. * @since 2.4.0.1 
  406. * @note Requires WP 3.5+ 
  407. * @return string HTML to insert into editor 
  408. */ 
  409. function gde_media_insert( $html, $id, $attachment ) { 
  410. global $gdeoptions; 
  411.  
  412. $gdoc_url = ''; 
  413. if (isset($attachment['url'])) { 
  414. $gdoc_url = $attachment['url']; 
  415. elseif ($id > 0) { 
  416. $post = get_post($id); 
  417. if ($post) { 
  418. $gdoc_url = wp_get_attachment_url($id); 
  419.  
  420. if ($gdoc_url != '' && gde_valid_type( $gdoc_url ) && $gdeoptions['ed_embed_sc'] == "yes" ) { 
  421. return '[gview file="' . $gdoc_url . '"]'; 
  422. } else { 
  423. return $html; 
  424.  
  425. /** 
  426. * Add upload support for natively unsupported mimetypes used by this plugin 
  427. * 
  428. * @since 2.4.0.1 
  429. * @return array Updated array of allowed upload types 
  430. */ 
  431. function gde_upload_mimes( $existing_mimes = array() ) { 
  432. $supported_exts = gde_supported_types(); 
  433.  
  434. foreach ( $supported_exts as $ext => $mimetype ) { 
  435. if ( ! array_key_exists( $ext, gde_mimes_expanded( $existing_mimes ) ) ) { 
  436. $existing_mimes[$ext] = $mimetype; 
  437. return gde_mimes_collapsed( $existing_mimes ); 
  438.  
  439. function gde_mimes_expanded( array $types ) { 
  440. // expand the supported mime types so that every ext is its own key 
  441. foreach ( $types as $k => $v ) { 
  442. if ( strpos( "|", $k ) ) { 
  443. $subtypes = explode( "|", $k ); 
  444. foreach ( $subtypes as $type ) { 
  445. $newtypes[$type] = $v; 
  446. unset( $types[$k] ); 
  447. $types = array_merge( $types, $newtypes ); 
  448. return $types; 
  449.  
  450. function gde_mimes_collapsed( $types ) { 
  451. // collapes the supported mime types so that each mime is listed once with combined key (default) 
  452. $newtypes = array(); 
  453.  
  454. foreach ( $types as $k => $v ) { 
  455. if ( isset( $newtypes[$v] ) ) { 
  456. $newtypes[$v] .= '|' . $k; 
  457. } else { 
  458. $newtypes[$v] = $k; 
  459. return array_flip( $newtypes ); 
  460.  
  461. /** 
  462. * Add TinyMCE button 
  463. * 
  464. * @since 2.0.0.1 
  465. * @return void 
  466. */ 
  467. function gde_mce_addbuttons() { 
  468. // don't bother doing this stuff if the current user lacks permissions 
  469. if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) 
  470. return; 
  471.  
  472. // add only in Rich Editor mode 
  473. if ( get_user_option('rich_editing') == 'true') { 
  474. add_filter("mce_external_plugins", "gde_add_tinymce_plugin"); 
  475. add_filter('mce_buttons', 'gde_register_mce_button'); 
  476.  
  477. function gde_add_tinymce_plugin( $plugin_array ) { 
  478. // load the TinyMCE plugin 
  479. $plugin_array['gde'] = GDE_PLUGIN_URL . 'js/editor_plugin.js'; 
  480. return $plugin_array; 
  481.  
  482. function gde_register_mce_button( $buttons ) { 
  483. array_push( $buttons, "separator", "gde" ); 
  484. return $buttons; 
  485.  
  486. /** 
  487. * Check current beta status 
  488. * 
  489. * @since 2.5.0.1 
  490. * @return bool Whether or not the currently running version is a beta 
  491. */ 
  492. function gde_is_beta() { 
  493. global $pdata; 
  494.  
  495. // check for currently running beta version (contains any letter or hyphen) 
  496. if ( preg_match( '/[a-z-]/i', $pdata['Version'] ) ) { 
  497. // running a beta 
  498. return true; 
  499. } else { 
  500. return false; 
  501.  
  502. /** 
  503. * Display beta status 
  504. * 
  505. * @since 2.5.0.1 
  506. * @return void 
  507. */ 
  508. function gde_warn_on_plugin_page( $plugin_file ) { 
  509. global $pdata; 
  510.  
  511. if ( strstr( $plugin_file, $pdata['mainfile'] ) ) { 
  512.  
  513. // see if there's a release waiting first (prevent double messages) 
  514. $updates = (array) get_site_option( '_site_transient_update_plugins' ); 
  515. if ( isset( $updates['response'] ) && array_key_exists( $pdata['basename'], $updates['response'] ) ) { 
  516. return; 
  517.  
  518. if ( gde_is_beta() ) { 
  519. $message[] = __('You are running a pre-release version of Google Doc Embedder. Please watch this space for important updates.', 'google-document-embedder'); 
  520. } else { 
  521. $message = array(); 
  522.  
  523. // print message if any 
  524. $message = rtrim( implode( " ", $message ) ); 
  525. if ( ! empty( $message ) ) { 
  526. // style improvements?? 
  527. //add_action( 'admin_enqueue_scripts', 'gde_admin_beta_js' ); 
  528.  
  529. print(' 
  530. <tr class="plugin-update-tr"> 
  531. <td colspan="3" class="plugin-update colspanchange"> 
  532. <div class="update-message" style="background:#e3e3e3;"> 
  533. '.$message.' 
  534. </div> 
  535. </td> 
  536. </tr> 
  537. '); 
  538.  
  539. /** 
  540. * Check for existence and valid content of dx log 
  541. * 
  542. * @since 2.5.2.1 
  543. * @return bool 
  544. */ 
  545. function gde_log_available() { 
  546. global $wpdb; 
  547.  
  548. $table = $wpdb->base_prefix . 'gde_dx_log'; 
  549. $blogid = get_current_blog_id(); 
  550. $log = false; 
  551.  
  552. if ( $wpdb->get_var( "SHOW TABLES LIKE '$table'" ) == $table ) { 
  553. $c = $wpdb->get_var( "SELECT COUNT(*) FROM $table WHERE blogid = $blogid" ); 
  554. if ( $c > 0 ) { 
  555. $log = true; 
  556.  
  557. return $log; 
  558.  
  559. ?> 
.