/wp-content/plugins/wordpress-seo/admin/views/tool-file-editor.php

https://bitbucket.org/carloskikea/helpet · PHP · 241 lines · 194 code · 26 blank · 21 comment · 29 complexity · 1b85a0724887b2162a2b23954e19a70d MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. if ( ! defined( 'WPSEO_VERSION' ) ) {
  8. header( 'Status: 403 Forbidden' );
  9. header( 'HTTP/1.1 403 Forbidden' );
  10. exit();
  11. }
  12. $robots_file = get_home_path() . 'robots.txt';
  13. $ht_access_file = get_home_path() . '.htaccess';
  14. if ( isset( $_POST['create_robots'] ) ) {
  15. if ( ! current_user_can( 'edit_files' ) ) {
  16. $die_msg = sprintf(
  17. /* translators: %s expands to robots.txt. */
  18. __( 'You cannot create a %s file.', 'wordpress-seo' ),
  19. 'robots.txt'
  20. );
  21. die( esc_html( $die_msg ) );
  22. }
  23. check_admin_referer( 'wpseo_create_robots' );
  24. ob_start();
  25. error_reporting( 0 );
  26. do_robots();
  27. $robots_content = ob_get_clean();
  28. $f = fopen( $robots_file, 'x' );
  29. fwrite( $f, $robots_content );
  30. }
  31. if ( isset( $_POST['submitrobots'] ) ) {
  32. if ( ! current_user_can( 'edit_files' ) ) {
  33. $die_msg = sprintf(
  34. /* translators: %s expands to robots.txt. */
  35. __( 'You cannot edit the %s file.', 'wordpress-seo' ),
  36. 'robots.txt'
  37. );
  38. die( esc_html( $die_msg ) );
  39. }
  40. check_admin_referer( 'wpseo-robotstxt' );
  41. if ( file_exists( $robots_file ) ) {
  42. $robotsnew = stripslashes( $_POST['robotsnew'] );
  43. if ( is_writable( $robots_file ) ) {
  44. $f = fopen( $robots_file, 'w+' );
  45. fwrite( $f, $robotsnew );
  46. fclose( $f );
  47. $msg = sprintf(
  48. /* translators: %s expands to robots.txt. */
  49. __( 'Updated %s', 'wordpress-seo' ),
  50. 'robots.txt'
  51. );
  52. }
  53. }
  54. }
  55. if ( isset( $_POST['submithtaccess'] ) ) {
  56. if ( ! current_user_can( 'edit_files' ) ) {
  57. $die_msg = sprintf(
  58. /* translators: %s expands to ".htaccess". */
  59. __( 'You cannot edit the %s file.', 'wordpress-seo' ),
  60. '.htaccess'
  61. );
  62. die( esc_html( $die_msg ) );
  63. }
  64. check_admin_referer( 'wpseo-htaccess' );
  65. if ( file_exists( $ht_access_file ) ) {
  66. $ht_access_new = stripslashes( $_POST['htaccessnew'] );
  67. if ( is_writeable( $ht_access_file ) ) {
  68. $f = fopen( $ht_access_file, 'w+' );
  69. fwrite( $f, $ht_access_new );
  70. fclose( $f );
  71. }
  72. }
  73. }
  74. if ( isset( $msg ) && ! empty( $msg ) ) {
  75. echo '<div id="message" class="updated fade"><p>', esc_html( $msg ), '</p></div>';
  76. }
  77. if ( is_multisite() ) {
  78. $action_url = network_admin_url( 'admin.php?page=wpseo_files' );
  79. }
  80. else {
  81. $action_url = admin_url( 'admin.php?page=wpseo_tools&tool=file-editor' );
  82. }
  83. echo '<br><br>';
  84. $helpcenter_tab = new WPSEO_Option_Tab( 'bulk-editor', __( 'Bulk editor', 'wordpress-seo' ),
  85. array( 'video_url' => WPSEO_Shortlinker::get( 'https://yoa.st/screencast-tools-file-editor' ) ) );
  86. $helpcenter = new WPSEO_Help_Center( 'bulk-editor', $helpcenter_tab, WPSEO_Utils::is_yoast_seo_premium() );
  87. $helpcenter->localize_data();
  88. $helpcenter->mount();
  89. // N.B.: "robots.txt" is a fixed file name and should not be translatable.
  90. echo '<h2>robots.txt</h2>';
  91. if ( ! file_exists( $robots_file ) ) {
  92. if ( is_writable( get_home_path() ) ) {
  93. echo '<form action="', esc_url( $action_url ), '" method="post" id="robotstxtcreateform">';
  94. wp_nonce_field( 'wpseo_create_robots', '_wpnonce', true, true );
  95. echo '<p>';
  96. printf(
  97. /* translators: %s expands to robots.txt. */
  98. esc_html__( 'You don\'t have a %s file, create one here:', 'wordpress-seo' ),
  99. 'robots.txt'
  100. );
  101. echo '</p>';
  102. printf(
  103. '<input type="submit" class="button" name="create_robots" value="%s">',
  104. sprintf(
  105. /* translators: %s expands to robots.txt. */
  106. esc_attr__( 'Create %s file', 'wordpress-seo' ),
  107. 'robots.txt'
  108. )
  109. );
  110. echo '</form>';
  111. }
  112. else {
  113. echo '<p>';
  114. printf(
  115. /* translators: %s expands to robots.txt. */
  116. esc_html__( 'If you had a %s file and it was editable, you could edit it from here.', 'wordpress-seo' ),
  117. 'robots.txt'
  118. );
  119. echo '</p>';
  120. }
  121. }
  122. else {
  123. $f = fopen( $robots_file, 'r' );
  124. $content = '';
  125. if ( filesize( $robots_file ) > 0 ) {
  126. $content = fread( $f, filesize( $robots_file ) );
  127. }
  128. if ( ! is_writable( $robots_file ) ) {
  129. echo '<p><em>';
  130. printf(
  131. /* translators: %s expands to robots.txt. */
  132. esc_html__( 'If your %s were writable, you could edit it from here.', 'wordpress-seo' ),
  133. 'robots.txt'
  134. );
  135. echo '</em></p>';
  136. echo '<textarea class="large-text code" disabled="disabled" rows="15" name="robotsnew">', esc_textarea( $content ), '</textarea><br/>';
  137. }
  138. else {
  139. echo '<form action="', esc_url( $action_url ), '" method="post" id="robotstxtform">';
  140. wp_nonce_field( 'wpseo-robotstxt', '_wpnonce', true, true );
  141. echo '<p><label for="robotsnew" class="yoast-inline-label">';
  142. printf(
  143. /* translators: %s expands to robots.txt. */
  144. esc_html__( 'Edit the content of your %s:', 'wordpress-seo' ),
  145. 'robots.txt'
  146. );
  147. echo '</label></p>';
  148. echo '<textarea class="large-text code" rows="15" name="robotsnew" id="robotsnew">', esc_textarea( $content ), '</textarea><br/>';
  149. printf(
  150. '<div class="submit"><input class="button" type="submit" name="submitrobots" value="%s" /></div>',
  151. sprintf(
  152. /* translators: %s expands to robots.txt. */
  153. esc_attr__( 'Save changes to %s', 'wordpress-seo' ),
  154. 'robots.txt'
  155. )
  156. );
  157. echo '</form>';
  158. }
  159. }
  160. if ( ( isset( $_SERVER['SERVER_SOFTWARE'] ) && stristr( $_SERVER['SERVER_SOFTWARE'], 'nginx' ) === false ) ) {
  161. echo '<h2>';
  162. printf(
  163. /* translators: %s expands to ".htaccess". */
  164. esc_html__( '%s file', 'wordpress-seo' ),
  165. '.htaccess'
  166. );
  167. echo '</h2>';
  168. if ( file_exists( $ht_access_file ) ) {
  169. $f = fopen( $ht_access_file, 'r' );
  170. $contentht = '';
  171. if ( filesize( $ht_access_file ) > 0 ) {
  172. $contentht = fread( $f, filesize( $ht_access_file ) );
  173. }
  174. if ( ! is_writable( $ht_access_file ) ) {
  175. echo '<p><em>';
  176. printf(
  177. /* translators: %s expands to ".htaccess". */
  178. esc_html__( 'If your %s were writable, you could edit it from here.', 'wordpress-seo' ),
  179. '.htaccess'
  180. );
  181. echo '</em></p>';
  182. echo '<textarea class="large-text code" disabled="disabled" rows="15" name="robotsnew">', esc_textarea( $contentht ), '</textarea><br/>';
  183. }
  184. else {
  185. echo '<form action="', esc_url( $action_url ), '" method="post" id="htaccessform">';
  186. wp_nonce_field( 'wpseo-htaccess', '_wpnonce', true, true );
  187. echo '<p><label for="htaccessnew" class="yoast-inline-label">';
  188. printf(
  189. /* translators: %s expands to ".htaccess". */
  190. esc_html__( 'Edit the content of your %s:', 'wordpress-seo' ),
  191. '.htaccess'
  192. );
  193. echo '</label></p>';
  194. echo '<textarea class="large-text code" rows="15" name="htaccessnew" id="htaccessnew">', esc_textarea( $contentht ), '</textarea><br/>';
  195. printf(
  196. '<div class="submit"><input class="button" type="submit" name="submithtaccess" value="%s" /></div>',
  197. sprintf(
  198. /* translators: %s expands to ".htaccess". */
  199. esc_attr__( 'Save changes to %s', 'wordpress-seo' ),
  200. '.htaccess'
  201. )
  202. );
  203. echo '</form>';
  204. }
  205. }
  206. else {
  207. echo '<p>';
  208. printf(
  209. /* translators: %s expands to ".htaccess". */
  210. esc_html__( 'If you had a %s file and it was editable, you could edit it from here.', 'wordpress-seo' ),
  211. '.htaccess'
  212. );
  213. echo '</p>';
  214. }
  215. }