PageRenderTime 87ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/yst_plugin_tools.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 492 lines | 372 code | 67 blank | 53 comment | 105 complexity | 965e02244f4e54f8be0b5e39ea03b36d MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /**
  3. * Backend Class for use in all Yoast plugins
  4. * Version 0.2.1
  5. */
  6. if ( !class_exists('Yoast_WPSEO_Plugin_Admin') ) {
  7. class Yoast_WPSEO_Plugin_Admin {
  8. var $hook = '';
  9. var $filename = '';
  10. var $longname = '';
  11. var $shortname = '';
  12. var $ozhicon = '';
  13. var $optionname = '';
  14. var $homepage = '';
  15. var $feed = 'http://yoast.com/feed/';
  16. var $accesslvl = 'manage_options';
  17. var $adminpages = array( 'wpseo_dashboard', 'wpseo_rss', 'wpseo_indexation', 'wpseo_files', 'wpseo_permalinks', 'wpseo_internal-links', 'wpseo_import', 'wpseo_titles', 'wpseo_xml');
  18. function __construct() {
  19. }
  20. function add_ozh_adminmenu_icon( $hook ) {
  21. if ($hook == $this->hook)
  22. return WPSEO_URL.$this->ozhicon;
  23. return $hook;
  24. }
  25. function config_page_styles() {
  26. global $pagenow;
  27. if ( $pagenow == 'admin.php' && isset($_GET['page']) && in_array($_GET['page'], $this->adminpages) ) {
  28. wp_enqueue_style('dashboard');
  29. wp_enqueue_style('thickbox');
  30. wp_enqueue_style('global');
  31. wp_enqueue_style('wp-admin');
  32. wp_enqueue_style('yoast-admin-css', WPSEO_URL . 'css/yst_plugin_tools.css', WPSEO_VERSION );
  33. }
  34. }
  35. function register_network_settings_page() {
  36. add_menu_page($this->longname, $this->shortname, 'delete_users', 'wpseo_dashboard', array(&$this,'network_config_page'), WPSEO_URL.'images/yoast-icon.png');
  37. }
  38. function register_settings_page() {
  39. add_menu_page($this->longname, $this->shortname, $this->accesslvl, 'wpseo_dashboard', array(&$this,'config_page'), WPSEO_URL.'images/yoast-icon.png');
  40. add_submenu_page('wpseo_dashboard','Titles','Titles',$this->accesslvl, 'wpseo_titles', array(&$this,'titles_page'));
  41. add_submenu_page('wpseo_dashboard','Indexation','Indexation',$this->accesslvl, 'wpseo_indexation', array(&$this,'indexation_page'));
  42. add_submenu_page('wpseo_dashboard','XML Sitemaps','XML Sitemaps',$this->accesslvl, 'wpseo_xml', array(&$this,'xml_sitemaps_page'));
  43. add_submenu_page('wpseo_dashboard','Permalinks','Permalinks',$this->accesslvl, 'wpseo_permalinks', array(&$this,'permalinks_page'));
  44. add_submenu_page('wpseo_dashboard','Internal Links','Internal Links',$this->accesslvl, 'wpseo_internal-links', array(&$this,'internallinks_page'));
  45. add_submenu_page('wpseo_dashboard','RSS','RSS',$this->accesslvl, 'wpseo_rss', array(&$this,'rss_page'));
  46. add_submenu_page('wpseo_dashboard','Import & Export','Import & Export',$this->accesslvl, 'wpseo_import', array(&$this,'import_page'));
  47. // Make sure on a multi site install only super admins can edit .htaccess and robots.txt
  48. if ( !function_exists('is_multisite') || !is_multisite() )
  49. add_submenu_page('wpseo_dashboard','Edit files','Edit files',$this->accesslvl, 'wpseo_files', array(&$this,'files_page'));
  50. else
  51. add_submenu_page('wpseo_dashboard','Edit files','Edit files','delete_users', 'wpseo_files', array(&$this,'files_page'));
  52. global $submenu;
  53. if ( isset($submenu['wpseo_dashboard']) )
  54. $submenu['wpseo_dashboard'][0][0] = 'Dashboard';
  55. }
  56. function plugin_options_url() {
  57. return admin_url( 'admin.php?page=wpseo_dashboard' );
  58. }
  59. /**
  60. * Add a link to the settings page to the plugins list
  61. */
  62. function add_action_link( $links, $file ) {
  63. static $this_plugin;
  64. if( empty($this_plugin) ) $this_plugin = $this->filename;
  65. if ( $file == $this_plugin ) {
  66. $settings_link = '<a href="' . $this->plugin_options_url() . '">' . __('Settings') . '</a>';
  67. array_unshift( $links, $settings_link );
  68. }
  69. return $links;
  70. }
  71. function config_page() {
  72. }
  73. function config_page_scripts() {
  74. global $pagenow;
  75. wp_enqueue_script( 'wpseo-admin-global-script', WPSEO_URL.'js/wp-seo-admin-global.js', array('jquery'), WPSEO_VERSION, true );
  76. if ( $pagenow == 'admin.php' && isset($_GET['page']) && in_array($_GET['page'], $this->adminpages) ) {
  77. wp_enqueue_script( 'wpseo-admin-script', WPSEO_URL.'js/wp-seo-admin.js', array('jquery'), WPSEO_VERSION, true );
  78. wp_enqueue_script( 'postbox' );
  79. wp_enqueue_script( 'dashboard' );
  80. wp_enqueue_script( 'thickbox' );
  81. }
  82. }
  83. /**
  84. * Create a Checkbox input field
  85. */
  86. function checkbox($id, $label, $label_left = false, $option = '') {
  87. if ( $option == '') {
  88. $options = get_wpseo_options();
  89. $option = !empty($option) ? $option : $this->currentoption;
  90. } else {
  91. if ( function_exists('is_network_admin') && is_network_admin() ) {
  92. $options = get_site_option($option);
  93. } else {
  94. $options = get_option($option);
  95. }
  96. }
  97. if (!isset($options[$id]))
  98. $options[$id] = false;
  99. $output_label = '<label for="'.$id.'">'.$label.'</label>';
  100. $output_input = '<input class="checkbox" type="checkbox" id="'.$id.'" name="'.$option.'['.$id.']"'. checked($options[$id],'on',false).'/> ';
  101. if( $label_left ) {
  102. $output = $output_label . $output_input;
  103. } else {
  104. $output = $output_input . $output_label;
  105. }
  106. return $output . '<br class="clear" />';
  107. }
  108. /**
  109. * Create a Text input field
  110. */
  111. function textinput($id, $label, $option = '') {
  112. if ( $option == '') {
  113. $options = get_wpseo_options();
  114. $option = !empty($option) ? $option : $this->currentoption;
  115. } else {
  116. if ( function_exists('is_network_admin') && is_network_admin() ) {
  117. $options = get_site_option($option);
  118. } else {
  119. $options = get_option($option);
  120. }
  121. }
  122. $val = '';
  123. if (isset($options[$id]))
  124. $val = htmlspecialchars($options[$id]);
  125. return '<label class="textinput" for="'.$id.'">'.$label.':</label><input class="textinput" type="text" id="'.$id.'" name="'.$option.'['.$id.']" value="'.$val.'"/>' . '<br class="clear" />';
  126. }
  127. /**
  128. * Create a small textarea
  129. */
  130. function textarea($id, $label, $option = '', $class = '') {
  131. if ( $option == '') {
  132. $options = get_wpseo_options();
  133. $option = !empty($option) ? $option : $this->currentoption;
  134. } else {
  135. if ( function_exists('is_network_admin') && is_network_admin() ) {
  136. $options = get_site_option($option);
  137. } else {
  138. $options = get_option($option);
  139. }
  140. }
  141. $val = '';
  142. if (isset($options[$id]))
  143. $val = esc_html($options[$id]);
  144. return '<label class="textinput" for="'.$id.'">'.$label.':</label><textarea class="textinput '.$class.'" id="'.$id.'" name="'.$option.'['.$id.']">' . $val . '</textarea>' . '<br class="clear" />';
  145. }
  146. /**
  147. * Create a Hidden input field
  148. */
  149. function hiddeninput($id, $option = '') {
  150. if ( $option == '') {
  151. $options = get_wpseo_options();
  152. $option = !empty($option) ? $option : $this->currentoption;
  153. } else {
  154. if ( function_exists('is_network_admin') && is_network_admin() ) {
  155. $options = get_site_option($option);
  156. } else {
  157. $options = get_option($option);
  158. }
  159. }
  160. $val = '';
  161. if (isset($options[$id]))
  162. $val = htmlspecialchars($options[$id]);
  163. return '<input class="hidden" type="hidden" id="'.$id.'" name="'.$option.'['.$id.']" value="'.$val.'"/>';
  164. }
  165. /**
  166. * Create a Select Box
  167. */
  168. function select($id, $label, $values, $option = '') {
  169. if ( $option == '') {
  170. $options = get_wpseo_options();
  171. $option = !empty($option) ? $option : $this->currentoption;
  172. } else {
  173. if ( function_exists('is_network_admin') && is_network_admin() ) {
  174. $options = get_site_option($option);
  175. } else {
  176. $options = get_option($option);
  177. }
  178. }
  179. $output = '<label class="select" for="'.$id.'">'.$label.':</label>';
  180. $output .= '<select class="select" name="'.$option.'['.$id.']" id="'.$id.'">';
  181. foreach($values as $value => $label) {
  182. $sel = '';
  183. if (isset($options[$id]) && $options[$id] == $value)
  184. $sel = 'selected="selected" ';
  185. if (!empty($label))
  186. $output .= '<option '.$sel.'value="'.$value.'">'.$label.'</option>';
  187. }
  188. $output .= '</select>';
  189. return $output . '<br class="clear"/>';
  190. }
  191. /**
  192. * Create a File upload
  193. */
  194. function file_upload($id, $label, $option = '') {
  195. $option = !empty($option) ? $option : $this->currentoption;
  196. $options = get_wpseo_options();
  197. $val = '';
  198. if (isset($options[$id]) && strtolower(gettype($options[$id])) == 'array') {
  199. $val = $options[$id]['url'];
  200. }
  201. $output = '<label class="select" for="'.$id.'">'.$label.':</label>';
  202. $output .= '<input type="file" value="' . $val . '" class="textinput" name="'.$option.'['.$id.']" id="'.$id.'"/>';
  203. // Need to save separate array items in hidden inputs, because empty file inputs type will be deleted by settings API.
  204. if(!empty($options[$id])) {
  205. $output .= '<input class="hidden" type="hidden" id="' . $id . '_file" name="wpseo_local[' . $id . '][file]" value="' . $options[$id]['file'] . '"/>';
  206. $output .= '<input class="hidden" type="hidden" id="' . $id . '_url" name="wpseo_local[' . $id . '][url]" value="' . $options[$id]['url'] . '"/>';
  207. $output .= '<input class="hidden" type="hidden" id="' . $id . '_type" name="wpseo_local[' . $id . '][type]" value="' . $options[$id]['type'] . '"/>';
  208. }
  209. $output .= '<br class="clear"/>';
  210. return $output;
  211. }
  212. /**
  213. * Create a Radio input field
  214. */
  215. function radio($id, $values, $label, $option = '') {
  216. if ( $option == '') {
  217. $options = get_wpseo_options();
  218. $option = !empty($option) ? $option : $this->currentoption;
  219. } else {
  220. if ( function_exists('is_network_admin') && is_network_admin() ) {
  221. $options = get_site_option($option);
  222. } else {
  223. $options = get_option($option);
  224. }
  225. }
  226. if (!isset($options[$id]))
  227. $options[$id] = false;
  228. $output = '<br/><label class="select">'.$label.':</label>';
  229. foreach($values as $key => $value) {
  230. $output .= '<input type="radio" class="radio" id="'.$id.'-' . $key . '" name="'.$option.'['.$id.']" value="'. $key.'" ' . ($options[$id] == $key ? ' checked="checked"' : '') . ' /> <label class="radio" for="'.$id.'-' . $key . '">'.$value.'</label>';
  231. }
  232. $output .= '<br/>';
  233. return $output;
  234. }
  235. /**
  236. * Create a hidden input field
  237. */
  238. function hidden($id, $option = '') {
  239. if ( $option == '') {
  240. $options = get_wpseo_options();
  241. $option = !empty($option) ? $option : $this->currentoption;
  242. } else {
  243. if ( function_exists('is_network_admin') && is_network_admin() ) {
  244. $options = get_site_option($option);
  245. } else {
  246. $options = get_option($option);
  247. }
  248. }
  249. if (!isset($options[$id]))
  250. $options[$id] = '';
  251. return '<input type="hidden" id="hidden_'.$id.'" name="'.$option.'['.$id.']" value="'.$options[$id].'"/>';
  252. }
  253. /**
  254. * Create a potbox widget
  255. */
  256. function postbox($id, $title, $content) {
  257. ?>
  258. <div id="<?php echo $id; ?>" class="postbox">
  259. <div class="handlediv" title="Click to toggle"><br /></div>
  260. <h3 class="hndle"><span><?php echo $title; ?></span></h3>
  261. <div class="inside">
  262. <?php echo $content; ?>
  263. </div>
  264. </div>
  265. <?php
  266. }
  267. /**
  268. * Create a form table from an array of rows
  269. */
  270. function form_table($rows) {
  271. $content = '<table class="form-table">';
  272. foreach ($rows as $row) {
  273. $content .= '<tr><th valign="top" scrope="row">';
  274. if (isset($row['id']) && $row['id'] != '')
  275. $content .= '<label for="'.$row['id'].'">'.$row['label'].':</label>';
  276. else
  277. $content .= $row['label'];
  278. if (isset($row['desc']) && $row['desc'] != '')
  279. $content .= '<br/><small>'.$row['desc'].'</small>';
  280. $content .= '</th><td valign="top">';
  281. $content .= $row['content'];
  282. $content .= '</td></tr>';
  283. }
  284. $content .= '</table>';
  285. return $content;
  286. }
  287. /**
  288. * Create a "plugin like" box.
  289. */
  290. function plugin_like() {
  291. $content = '<p>'.__('Why not do any or all of the following:','ystplugin').'</p>';
  292. $content .= '<ul>';
  293. $content .= '<li><a href="'.$this->homepage.'">'.__('Link to it so other folks can find out about it.','ystplugin').'</a></li>';
  294. $content .= '<li><a href="http://wordpress.org/extend/plugins/'.$this->hook.'/">'.__('Give it a 5 star rating on WordPress.org.','ystplugin').'</a></li>';
  295. $content .= '<li><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;hosted_button_id=2017947">'.__('Donate a token of your appreciation.','ystplugin').'</a></li>';
  296. $content .= '</ul>';
  297. $this->postbox($this->hook.'like', __('Like this plugin?'), $content);
  298. }
  299. /**
  300. * Info box with link to the support forums.
  301. */
  302. function plugin_support() {
  303. $content = '<p>'.__('If you are having problems with this plugin, please talk about them in the','ystplugin').' <a href="http://wordpress.org/tags/'.$this->hook.'">'.__("Support forums",'ystplugin').'</a>.</p>';
  304. $content .= '<p>'.__("If you're sure you've found a bug, or have a feature request, please submit it in the")." <a href='http://yoast.com/bugs/wordpress-seo/'>".__('bug tracker')."</a>.</p>";
  305. $this->postbox($this->hook.'support', __('Need support?'), $content);
  306. }
  307. function text_limit( $text, $limit, $finish = '&hellip;') {
  308. if( strlen( $text ) > $limit ) {
  309. $text = substr( $text, 0, $limit );
  310. $text = substr( $text, 0, - ( strlen( strrchr( $text,' ') ) ) );
  311. $text .= $finish;
  312. }
  313. return $text;
  314. }
  315. function fetch_rss_items( $num ) {
  316. include_once(ABSPATH . WPINC . '/feed.php');
  317. $rss = fetch_feed( $this->feed );
  318. // Bail if feed doesn't work
  319. if ( is_wp_error($rss) )
  320. return false;
  321. $rss_items = $rss->get_items( 0, $rss->get_item_quantity( $num ) );
  322. // If the feed was erroneously
  323. if ( !$rss_items ) {
  324. $md5 = md5( $this->feed );
  325. delete_transient( 'feed_' . $md5 );
  326. delete_transient( 'feed_mod_' . $md5 );
  327. $rss = fetch_feed( $this->feed );
  328. $rss_items = $rss->get_items( 0, $rss->get_item_quantity( $num ) );
  329. }
  330. return $rss_items;
  331. }
  332. /**
  333. * Box with latest news from Yoast.com for sidebar
  334. */
  335. function news() {
  336. $rss_items = $this->fetch_rss_items( 5 );
  337. $content = '<ul>';
  338. if ( !$rss_items ) {
  339. $content .= '<li class="yoast">no news items, feed might be broken...</li>';
  340. } else {
  341. foreach ( $rss_items as $item ) {
  342. $content .= '<li class="yoast">';
  343. $content .= '<a class="rsswidget" href="'.esc_url( $item->get_permalink(), $protocolls=null, 'display' ).'">'. esc_html( $item->get_title() ) .'</a> ';
  344. $content .= '</li>';
  345. }
  346. }
  347. $content .= '<li class="rss"><a href="'.$this->feed.'">Subscribe with RSS</a></li>';
  348. $content .= '<li class="email"><a href="http://yoast.com/wordpress-newsletter/">Subscribe by email</a></li>';
  349. $content .= '</ul>';
  350. $this->postbox('yoastlatest', 'Latest news from Yoast', $content);
  351. }
  352. /**
  353. * Widget with latest news from Yoast.com for dashbaord
  354. */
  355. function db_widget() {
  356. $options = get_option('wpseo_yoastdbwidget');
  357. $network = '';
  358. if ( function_exists('is_network_admin') && is_network_admin() )
  359. $network = '_network';
  360. if (isset($_POST['yoast_removedbwidget'])) {
  361. $options['removedbwidget'.$network] = true;
  362. update_option('wpseo_yoastdbwidget',$options);
  363. }
  364. if ( isset($options['removedbwidget'.$network]) && $options['removedbwidget'.$network] ) {
  365. echo "If you reload, this widget will be gone and never appear again, unless you decide to delete the database option 'wpseo_yoastdbwidget'.";
  366. return;
  367. }
  368. $rss_items = $this->fetch_rss_items( 3 );
  369. echo '<div class="rss-widget">';
  370. echo '<a href="http://yoast.com/" title="Go to Yoast.com"><img src="'.WPSEO_URL.'images/yoast-logo-rss.png" class="alignright" alt="Yoast"/></a>';
  371. echo '<ul>';
  372. if ( !$rss_items ) {
  373. echo '<li class="yoast">no news items, feed might be broken...</li>';
  374. } else {
  375. foreach ( $rss_items as $item ) {
  376. echo '<li class="yoast">';
  377. echo '<a class="rsswidget" href="'.esc_url( $item->get_permalink(), $protocolls=null, 'display' ).'">'. esc_html( $item->get_title() ) .'</a>';
  378. echo ' <span class="rss-date">'. $item->get_date('F j, Y') .'</span>';
  379. echo '<div class="rssSummary">'. esc_html( $this->text_limit( strip_tags( $item->get_description() ), 150 ) ).'</div>';
  380. echo '</li>';
  381. }
  382. }
  383. echo '</ul>';
  384. echo '<br class="clear"/><div style="margin-top:10px;border-top: 1px solid #ddd; padding-top: 10px; text-align:center;">';
  385. echo '<a href="'.$this->feed.'"><img src="'.get_bloginfo('wpurl').'/wp-includes/images/rss.png" alt=""/> Subscribe with RSS</a>';
  386. echo ' &nbsp; &nbsp; &nbsp; ';
  387. echo '<a href="http://yoast.com/wordpress-newsletter/"><img src="'.WPSEO_URL.'images/email_sub.png" alt=""/> Subscribe by email</a>';
  388. echo '<form class="alignright" method="post"><input type="hidden" name="yoast_removedbwidget" value="true"/><input title="Remove this widget from all users dashboards" class="button" type="submit" value="X"/></form>';
  389. echo '</div>';
  390. echo '</div>';
  391. }
  392. function widget_setup() {
  393. $network = '';
  394. if ( function_exists('is_network_admin') && is_network_admin() )
  395. $network = '_network';
  396. $options = get_option('wpseo_yoastdbwidget');
  397. if ( !isset($options['removedbwidget'.$network]) || !$options['removedbwidget'.$network] )
  398. wp_add_dashboard_widget( 'yoast_db_widget' , 'The Latest From Yoast' , array(&$this, 'db_widget') );
  399. }
  400. function widget_order( $arr ) {
  401. global $wp_meta_boxes;
  402. if ( function_exists('is_network_admin') && is_network_admin() ) {
  403. $plugins = $wp_meta_boxes['dashboard-network']['normal']['core']['dashboard_plugins'];
  404. unset($wp_meta_boxes['dashboard-network']['normal']['core']['dashboard_plugins']);
  405. $wp_meta_boxes['dashboard-network']['normal']['core'][] = $plugins;
  406. } else if ( is_admin() ) {
  407. if ( isset($wp_meta_boxes['dashboard']['normal']['core']['yoast_db_widget']) ) {
  408. $yoast_db_widget = $wp_meta_boxes['dashboard']['normal']['core']['yoast_db_widget'];
  409. unset($wp_meta_boxes['dashboard']['normal']['core']['yoast_db_widget']);
  410. if ( isset($wp_meta_boxes['dashboard']['side']['core']) ) {
  411. $begin = array_slice($wp_meta_boxes['dashboard']['side']['core'], 0, 1);
  412. $end = array_slice($wp_meta_boxes['dashboard']['side']['core'], 1, 5);
  413. $wp_meta_boxes['dashboard']['side']['core'] = $begin;
  414. $wp_meta_boxes['dashboard']['side']['core'][] = $yoast_db_widget;
  415. $wp_meta_boxes['dashboard']['side']['core'] += $end;
  416. } else {
  417. $wp_meta_boxes['dashboard']['side']['core'] = array();
  418. $wp_meta_boxes['dashboard']['side']['core'][] = $yoast_db_widget;
  419. }
  420. }
  421. }
  422. return $arr;
  423. }
  424. }
  425. }