class-export.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Export
  6. */
  7. /**
  8. * Class WPSEO_Export.
  9. *
  10. * Class with functionality to export the WP SEO settings.
  11. */
  12. class WPSEO_Export {
  13. /**
  14. * Holds the nonce action.
  15. *
  16. * @var string
  17. */
  18. const NONCE_ACTION = 'wpseo_export';
  19. /**
  20. * Holds the export data.
  21. *
  22. * @var string
  23. */
  24. private $export = '';
  25. /**
  26. * Holds whether the export was a success.
  27. *
  28. * @var boolean
  29. */
  30. public $success;
  31. /**
  32. * Handles the export request.
  33. */
  34. public function export() {
  35. check_admin_referer( self::NONCE_ACTION );
  36. $this->export_settings();
  37. $this->output();
  38. }
  39. /**
  40. * Outputs the export.
  41. */
  42. public function output() {
  43. if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
  44. esc_html_e( 'You do not have the required rights to export settings.', 'wordpress-seo' );
  45. return;
  46. }
  47. echo '<p id="wpseo-settings-export-desc">';
  48. printf(
  49. /* translators: %1$s expands to Import settings */
  50. esc_html__(
  51. 'Copy all these settings to another site\'s %1$s tab and click "%1$s" there.',
  52. 'wordpress-seo'
  53. ),
  54. esc_html__(
  55. 'Import settings',
  56. 'wordpress-seo'
  57. )
  58. );
  59. echo '</p>';
  60. /* translators: %1$s expands to Yoast SEO */
  61. echo '<label for="wpseo-settings-export" class="yoast-inline-label">' . sprintf( __( 'Your %1$s settings:', 'wordpress-seo' ), 'Yoast SEO' ) . '</label><br />';
  62. echo '<textarea id="wpseo-settings-export" rows="20" cols="100" aria-describedby="wpseo-settings-export-desc">' . esc_textarea( $this->export ) . '</textarea>';
  63. }
  64. /**
  65. * Exports the current site's WP SEO settings.
  66. */
  67. private function export_settings() {
  68. $this->export_header();
  69. foreach ( WPSEO_Options::get_option_names() as $opt_group ) {
  70. $this->write_opt_group( $opt_group );
  71. }
  72. }
  73. /**
  74. * Writes the header of the export.
  75. */
  76. private function export_header() {
  77. $header = sprintf(
  78. /* translators: %1$s expands to Yoast SEO, %2$s expands to Yoast.com */
  79. esc_html__( 'These are settings for the %1$s plugin by %2$s', 'wordpress-seo' ),
  80. 'Yoast SEO',
  81. 'Yoast.com'
  82. );
  83. $this->write_line( '; ' . $header );
  84. }
  85. /**
  86. * Writes a line to the export.
  87. *
  88. * @param string $line Line string.
  89. * @param boolean $newline_first Boolean flag whether to prepend with new line.
  90. */
  91. private function write_line( $line, $newline_first = false ) {
  92. if ( $newline_first ) {
  93. $this->export .= PHP_EOL;
  94. }
  95. $this->export .= $line . PHP_EOL;
  96. }
  97. /**
  98. * Writes an entire option group to the export.
  99. *
  100. * @param string $opt_group Option group name.
  101. */
  102. private function write_opt_group( $opt_group ) {
  103. $this->write_line( '[' . $opt_group . ']', true );
  104. $options = get_option( $opt_group );
  105. if ( ! is_array( $options ) ) {
  106. return;
  107. }
  108. foreach ( $options as $key => $elem ) {
  109. if ( is_array( $elem ) ) {
  110. $count = count( $elem );
  111. for ( $i = 0; $i < $count; $i++ ) {
  112. $this->write_setting( $key . '[]', $elem[ $i ] );
  113. }
  114. }
  115. else {
  116. $this->write_setting( $key, $elem );
  117. }
  118. }
  119. }
  120. /**
  121. * Writes a settings line to the export.
  122. *
  123. * @param string $key Key string.
  124. * @param string $val Value string.
  125. */
  126. private function write_setting( $key, $val ) {
  127. if ( is_string( $val ) ) {
  128. $val = '"' . $val . '"';
  129. }
  130. $this->write_line( $key . ' = ' . $val );
  131. }
  132. /* ********************* DEPRECATED METHODS ********************* */
  133. /**
  134. * Returns true when the property error has a value.
  135. *
  136. * @deprecated 11.9 Obsolete since the export setting refactor in 9.2.
  137. *
  138. * @codeCoverageIgnore
  139. *
  140. * @return bool
  141. */
  142. public function has_error() {
  143. _deprecated_function( __METHOD__, 'WPSEO 11.9' );
  144. return false;
  145. }
  146. /**
  147. * Sets the error hook, to display the error to the user.
  148. *
  149. * @deprecated 11.9 Obsolete since the export setting refactor in 9.2.
  150. *
  151. * @codeCoverageIgnore
  152. */
  153. public function set_error_hook() {
  154. _deprecated_function( __METHOD__, 'WPSEO 11.9' );
  155. }
  156. }