2017-06-13 09:45:37 +02:00
< ? php
/**
2018-12-04 14:01:14 +01:00
* WPSEO plugin file .
*
2017-06-13 09:45:37 +02:00
* @ package WPSEO\Admin\Export
*/
/**
* Class WPSEO_Export
*
* Class with functionality to export the WP SEO settings
*/
class WPSEO_Export {
const NONCE_ACTION = 'wpseo_export' ;
/**
* @ var string
*/
private $export = '' ;
/**
* @ var string
*/
private $error = '' ;
/**
* @ var boolean
*/
public $success ;
/**
2018-12-04 14:01:14 +01:00
* Handles the export request .
2017-06-13 09:45:37 +02:00
*/
2018-12-04 14:01:14 +01:00
public function export () {
check_admin_referer ( self :: NONCE_ACTION );
$this -> export_settings ();
$this -> output ();
}
2017-06-13 09:45:37 +02:00
/**
2018-12-04 14:01:14 +01:00
* Outputs the export .
2017-06-13 09:45:37 +02:00
*/
2018-12-04 14:01:14 +01:00
public function output () {
if ( ! WPSEO_Capability_Utils :: current_user_can ( 'wpseo_manage_options' ) ) {
esc_html_e ( 'You do not have the required rights to export settings.' , 'wordpress-seo' );
return ;
}
2017-06-13 09:45:37 +02:00
2018-12-04 14:01:14 +01:00
echo '<p>' ;
/* translators: %1$s expands to Import settings */
printf ( esc_html__ ( 'Copy all these settings to another site\'s %1$s tab and click "%1$s" there.' , 'wordpress-seo' ), __ ( 'Import settings' , 'wordpress-seo' ) );
echo '</p>' ;
echo '<textarea id="wpseo-export" rows="20" cols="100">' . $this -> export . '</textarea>' ;
2017-06-13 09:45:37 +02:00
}
/**
* Returns true when the property error has a value .
*
* @ return bool
*/
public function has_error () {
return ( $this -> error !== '' );
}
/**
* Sets the error hook , to display the error to the user .
*/
public function set_error_hook () {
2017-11-15 16:23:10 +01:00
/* translators: %1$s expands to Yoast SEO */
2017-06-13 09:45:37 +02:00
$message = sprintf ( __ ( 'Error creating %1$s export: ' , 'wordpress-seo' ), 'Yoast SEO' ) . $this -> error ;
2018-12-04 14:01:14 +01:00
printf (
'<div class="notice notice-error"><p>%1$s</p></div>' ,
$message
);
2017-06-13 09:45:37 +02:00
}
/**
* Exports the current site ' s WP SEO settings .
*/
private function export_settings () {
$this -> export_header ();
foreach ( WPSEO_Options :: get_option_names () as $opt_group ) {
$this -> write_opt_group ( $opt_group );
}
}
/**
2018-12-04 14:01:14 +01:00
* Writes the header of the export .
2017-06-13 09:45:37 +02:00
*/
private function export_header () {
2018-12-04 14:01:14 +01:00
$header = sprintf (
/* translators: %1$s expands to Yoast SEO, %2$s expands to Yoast.com */
esc_html__ ( 'These are settings for the %1$s plugin by %2$s' , 'wordpress-seo' ),
'Yoast SEO' ,
'Yoast.com'
);
$this -> write_line ( '; ' . $header );
2017-06-13 09:45:37 +02:00
}
/**
* Writes a line to the export
*
* @ param string $line Line string .
* @ param boolean $newline_first Boolean flag whether to prepend with new line .
*/
private function write_line ( $line , $newline_first = false ) {
if ( $newline_first ) {
$this -> export .= PHP_EOL ;
}
$this -> export .= $line . PHP_EOL ;
}
/**
* Writes an entire option group to the export
*
* @ param string $opt_group Option group name .
*/
private function write_opt_group ( $opt_group ) {
$this -> write_line ( '[' . $opt_group . ']' , true );
$options = get_option ( $opt_group );
if ( ! is_array ( $options ) ) {
return ;
}
foreach ( $options as $key => $elem ) {
if ( is_array ( $elem ) ) {
2017-11-15 16:23:10 +01:00
$count = count ( $elem );
for ( $i = 0 ; $i < $count ; $i ++ ) {
2017-06-13 09:45:37 +02:00
$this -> write_setting ( $key . '[]' , $elem [ $i ] );
}
}
else {
$this -> write_setting ( $key , $elem );
}
}
}
/**
* Writes a settings line to the export
*
* @ param string $key Key string .
* @ param string $val Value string .
*/
private function write_setting ( $key , $val ) {
if ( is_string ( $val ) ) {
$val = '"' . $val . '"' ;
}
$this -> write_line ( $key . ' = ' . $val );
}
}