Skip to content

Commit

Permalink
The configuration export ability has been added. [#9](#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
iworks committed Dec 3, 2024
1 parent e106e60 commit 34d9c2c
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 41 deletions.
36 changes: 36 additions & 0 deletions etc/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,42 @@ function orphans_indicator_options() {
'classes' => array( 'large-text' ),
'rows' => 10,
),
array(
'type' => 'heading',
'label' => __( 'Export/Import', 'sierotki' ),
'since' => '3.3.0',
),
array(
'type' => 'subheading',
'label' => __( 'Export', 'sierotki' ),
'since' => '3.3.0',
),
array(
'name' => 'export_extra',
'th' => __( 'Add extra information', 'sierotki' ),
'type' => 'checkbox',
'description' => __( 'Add site configuration data like language, url. Nothing sensitive.', 'sierotki' ),
'sanitize_callback' => 'absint',
'classes' => array( 'switch-button' ),
'default' => 1,
),
array(
'name' => 'export',
'value' => __( 'Export JSON', 'sierotki' ),
'type' => 'button',
'since' => '3.3.0',
),
array(
'type' => 'subheading',
'label' => __( 'Import', 'sierotki' ),
'since' => '3.3.0',
),
array(
'name' => 'import',
'th' => __( 'Import JSON', 'sierotki' ),
'type' => 'file',
'since' => '3.3.0',
),
),
'metaboxes' => array(
'assistance' => array(
Expand Down
79 changes: 66 additions & 13 deletions includes/iworks/class-iworks-orphan.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
*/

defined( 'ABSPATH' ) || exit; // Exit if accessed directly

/**
* Main Orphans Class.
*
* @since 1.0.0
*/
class iworks_orphan {

private $options;
Expand Down Expand Up @@ -94,10 +101,6 @@ public function __construct() {
*/
$file = dirname( dirname( dirname( __FILE__ ) ) ) . '/sierotki.php';
$this->root = dirname( $file );
/**
* options
*/
$this->options = get_orphan_options();
/**
* plugin ID
*/
Expand All @@ -106,9 +109,11 @@ public function __construct() {
* actions
*/
add_action( 'init', array( $this, 'init' ) );
add_action( 'init', array( $this, 'action_load_plugin_textdomain' ) );
add_action( 'init', array( $this, 'action_load_plugin_textdomain' ), 0 );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'load-appearance_page_iworks_orphan_index', array( $this, 'clear_terms_cache' ) );
add_action( 'load-appearance_page_iworks_orphan_index', array( $this, 'add_admin_scripts' ) );
add_action( 'plugins_loaded', array( $this, 'send_json' ) );
/**
* clear cache terms after site langage was changed
*
Expand All @@ -123,14 +128,6 @@ public function __construct() {
* iWorks Rate Class
*/
add_filter( 'iworks_rate_notice_logo_style', array( $this, 'filter_plugin_logo' ), 10, 2 );
/**
* Replace in Translations functions.
*
* Since 3.1.0
*/
if ( $this->options->get_option( 'gettext' ) ) {
add_filter( 'gettext', array( $this, 'filter_gettext' ), 10, 3 );
}
/**
* get terms filter
*
Expand Down Expand Up @@ -483,6 +480,18 @@ public function admin_init() {
* Initialize, but not for admin
*/
public function init() {
/**
* options
*/
$this->options = get_orphan_options();
/**
* Replace in Translations functions.
*
* Since 3.1.0
*/
if ( $this->options->get_option( 'gettext' ) ) {
add_filter( 'gettext', array( $this, 'filter_gettext' ), 10, 3 );
}
/**
* Turn off all replacements for admin area - we do not need it!
*/
Expand Down Expand Up @@ -1054,12 +1063,56 @@ public function filter_index_iworks_orphan_post_type_data( $options, $option_nam
* @since 3.2.9
*/
public function action_load_plugin_textdomain() {
return;
load_plugin_textdomain(
'sierotki',
false,
plugin_basename( $this->root ) . '/languages'
);
}

/**
* add admin scripts
*
* @since 3.3.0
*/
public function add_admin_scripts() {
add_action( 'admin_print_scripts', array( $this, 'action_admin_print_scripts' ), PHP_INT_MAX );
}

/**
* add admin scripts
*
* @since 3.3.0
*/
public function action_admin_print_scripts() {
?>
<script id="sierotki">
jQuery(document).ready(function($) {
$('input[name=iworks_orphan_export]').on( 'click', function(e) {
$form = $('<form method="post"></form>');
$form.append('<input type="hidden" name="nonce" value="'+$(this).data('nonce') +'">');
$form.append('<input type="hidden" name="extra" value="'+$('input[name=iworks_orphan_export_extra]').is(':checked') +'">');
$('body').append($form);
$form.submit();
});
});
</script>
<?php
}

public function send_json() {
$nonce_value = filter_input( INPUT_POST, 'nonce' );
if ( empty( $nonce_value ) ) {
return;
}
if ( ! wp_verify_nonce( $nonce_value, 'iworks_orphan_export' ) ) {
return;
}
include_once __DIR__ . '/orphans/class-iworks-orphans-export.php';
$export = new iWorks_Orphans_Export;
$export->send_json();
wp_send_json_error();
}
}

129 changes: 129 additions & 0 deletions includes/iworks/orphans/class-iworks-orphans-export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/*
Copyright 2024-PLUGIN_TILL_YEAR Marcin Pietrzak ([email protected])
this program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

defined( 'ABSPATH' ) || exit; // Exit if accessed directly
/**
* Export Orphans Configuration.
*
* The class allows to export plugin configuration to JSON file.
*
* @since 3.3.0
*/
class iWorks_Orphans_Export {
private $options;

/**
* Prepare and send JSON file
*
* @since 3.3.0
*/
public function send_json() {
$this->options = get_orphan_options();
$options = $this->options->get_group();
if ( ! is_array( $options ) ) {
return;
}
if ( ! isset( $options['options'] ) ) {
return;
}
$add_wordpress_data = 'true' === filter_input( INPUT_POST, 'extra' );
/**
* data
*/
$data = array(
'Meta' => array(
'date' => date( 'c' ),
'plugin' => array(
'name' => 'Orphans',
'version' => 'PLUGIN_VERSION',
),
'url' => array(
'GitHub' => 'https://github.com/iworks/sierotki',
'WordPress' => 'https://wordpress.org/plugins/sierotki/',
),
),
'Orphans' => $this->get_settings_plugin(),
'WordPress' => $add_wordpress_data ? $this->get_wordpress_settings() : array(),
);
/**
* filename
*/
$filename = sanitize_file_name(
sprintf(
'%s-%s.json',
get_option( 'blogname' ),
date( 'c' )
)
);
/**
* export file
*/
header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/json' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Content-Transfer-Encoding: binary' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: public' );
echo json_encode( $data, JSON_PRETTY_PRINT );
exit;
}

/**
* get plugin settings
*
* @since 3.3.0
*/
private function get_settings_plugin() {
$data = array();
foreach ( $options['options'] as $one ) {
if ( ! isset( $one['name'] ) ) {
continue;
}
$option_name = $this->options->get_option_name( $one['name'] );
$data[] = array(
'name' => $one['name'],
'option_name' => $this->options->get_option_name( $one['name'] ),
'option_value' => $this->options->get_option( $one['name'] ),
);
}
return $data;
}

/**
* get WordPress settings
*
* @since 3.3.0
*/
private function get_wordpress_settings() {
$fields = array(
'siteurl',
'blogname',
'blog_charset',
'active_plugins',
'WPLANG',
);
$data = array();
foreach ( $fields as $option_name ) {
$data[ $option_name ] = get_option( $option_name );
}
return $data;
}
}

2 changes: 2 additions & 0 deletions includes/iworks/orphans/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
// Silence is golden.
Loading

0 comments on commit 34d9c2c

Please sign in to comment.