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