Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change import settings logic #11502

Merged
merged 13 commits into from Nov 6, 2018
Next
Change import settings logic
  • Loading branch information
Joost de Valk committed Nov 5, 2018
commit 3bfa70a143f5ea3ee1934f3a1703bb5caf139ffa
34 changes: 0 additions & 34 deletions admin/class-config.php
Expand Up @@ -41,24 +41,10 @@ public function init() {
wp_redirect( admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ) );
}

add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'config_page_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'config_page_styles' ) );
}

/**
* Run admin-specific actions.
*/
public function admin_init() {

$page = filter_input( INPUT_GET, 'page' );
$tool = filter_input( INPUT_GET, 'tool' );
$export_nonce = filter_input( INPUT_POST, WPSEO_Export::NONCE_NAME );

if ( 'wpseo_tools' === $page && 'import-export' === $tool && $export_nonce !== null ) {
$this->do_yoast_export();
}
}

/**
* Loads the required styles for the config page.
Expand Down Expand Up @@ -158,24 +144,4 @@ private function enqueue_tools_scripts() {
$this->asset_manager->enqueue_script( 'bulk-editor' );
}
}

/**
* Runs the yoast exporter class to possibly init the file download.
*/
private function do_yoast_export() {
check_admin_referer( WPSEO_Export::NONCE_ACTION, WPSEO_Export::NONCE_NAME );

if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
return;
}

$wpseo_post = filter_input( INPUT_POST, 'wpseo' );
$include_taxonomy = ! empty( $wpseo_post['include_taxonomy'] );
$export = new WPSEO_Export( $include_taxonomy );

if ( $export->has_error() ) {
add_action( 'admin_notices', array( $export, 'set_error_hook' ) );

}
}
} /* End of class */
163 changes: 13 additions & 150 deletions admin/class-export.php
Expand Up @@ -11,12 +11,7 @@
* Class with functionality to export the WP SEO settings
*/
class WPSEO_Export {

const ZIP_FILENAME = 'yoast-seo-settings-export.zip';
const INI_FILENAME = 'settings.ini';

const NONCE_ACTION = 'wpseo_export';
const NONCE_NAME = 'wpseo_export_nonce';

/**
* @var string
Expand All @@ -28,38 +23,28 @@ class WPSEO_Export {
*/
private $error = '';

/**
* @var string
*/
public $export_zip_url = '';

/**
* @var boolean
*/
public $success;

/**
* Whether or not the export will include taxonomy metadata
*
* @var boolean
*/
private $include_taxonomy;

/**
* @var array
* Class constructor
*/
private $dir = array();
public function __construct() {
check_admin_referer( WPSEO_Export::NONCE_ACTION );
jdevalk marked this conversation as resolved.
Show resolved Hide resolved
$this->export_settings();
$this->output();
}

/**
* Class constructor
*
* @param boolean $include_taxonomy Whether to include the taxonomy metadata the plugin creates.
* Outputs the export.
*/
public function __construct( $include_taxonomy = false ) {
$this->include_taxonomy = $include_taxonomy;
$this->dir = wp_upload_dir();

$this->export_settings();
public function output() {
/* 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 '<br/><br/>';
echo '<textarea id="wpseo-export" rows="20" cols="100">' . $this->export . '</textarea>';
}

/**
Expand Down Expand Up @@ -88,25 +73,11 @@ public function set_error_hook() {
* 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 );
}

$this->taxonomy_metadata();

if ( ! $this->write_settings_file() ) {
$this->error = __( 'Could not write settings to file.', 'wordpress-seo' );

return;
}

if ( $this->zip_file() ) {
// Just exit, because there is a download being served.
exit;
}
}

/**
Expand All @@ -119,10 +90,7 @@ private function export_header() {
'Yoast SEO',
'Yoast.com'
);
$this->write_line( '; ' . $header . ' - ' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/1yd' ) ) );
if ( $this->include_taxonomy ) {
$this->write_line( '; ' . __( 'This export includes taxonomy metadata', 'wordpress-seo' ) );
}
$this->write_line( '; ' . $header );
}

/**
Expand Down Expand Up @@ -178,109 +146,4 @@ private function write_setting( $key, $val ) {
}
$this->write_line( $key . ' = ' . $val );
}

/**
* Adds the taxonomy meta data if there is any
*/
private function taxonomy_metadata() {
if ( $this->include_taxonomy ) {
$taxonomy_meta = get_option( 'wpseo_taxonomy_meta' );
if ( is_array( $taxonomy_meta ) ) {
$this->write_line( '[wpseo_taxonomy_meta]', true );
$this->write_setting( 'wpseo_taxonomy_meta', urlencode( wp_json_encode( $taxonomy_meta ) ) );
}
else {
$this->write_line( '; ' . __( 'No taxonomy metadata found', 'wordpress-seo' ), true );
}
}
}

/**
* Writes the settings to our temporary settings.ini file
*
* @return boolean unsigned
*/
private function write_settings_file() {
$handle = fopen( $this->dir['path'] . '/' . self::INI_FILENAME, 'w' );
if ( ! $handle ) {
return false;
}

$res = fwrite( $handle, $this->export );
if ( ! $res ) {
return false;
}

fclose( $handle );

return true;
}

/**
* Zips the settings ini file
*
* @return bool|null
*/
private function zip_file() {
$is_zip_created = $this->create_zip();

// The settings.ini isn't needed, because it's in the zipfile.
$this->remove_settings_ini();

if ( ! $is_zip_created ) {
$this->error = __( 'Could not zip settings-file.', 'wordpress-seo' );

return false;
}

$this->serve_settings_export();
$this->remove_zip();

return true;
}

/**
* Creates the zipfile and returns true if it created successful.
*
* @return bool
*/
private function create_zip() {
chdir( $this->dir['path'] );
$zip = new PclZip( './' . self::ZIP_FILENAME );
if ( 0 === $zip->create( './' . self::INI_FILENAME ) ) {
return false;
}

return file_exists( self::ZIP_FILENAME );
}

/**
* Downloads the zip file.
*/
private function serve_settings_export() {
// Clean any content that has been already output. For example by other plugins or faulty PHP files.
if ( ob_get_contents() ) {
ob_clean();
}
header( 'Content-Type: application/octet-stream; charset=utf-8' );
header( 'Content-Transfer-Encoding: Binary' );
header( 'Content-Disposition: attachment; filename=' . self::ZIP_FILENAME );
header( 'Content-Length: ' . filesize( self::ZIP_FILENAME ) );

readfile( self::ZIP_FILENAME );
}

/**
* Removes the settings ini file.
*/
private function remove_settings_ini() {
unlink( './' . self::INI_FILENAME );
}

/**
* Removes the files because they are already downloaded.
*/
private function remove_zip() {
unlink( './' . self::ZIP_FILENAME );
}
}