parent_id ) {
wp_enqueue_script( 'top-ten-admin-js' );
wp_enqueue_style( 'top-ten-admin-css' );
}
}
/**
* Admin Menu.
*
* @since 3.3.0
*/
public function admin_menu() {
$this->parent_id = add_submenu_page(
'tptn_dashboard',
esc_html__( 'Top 10 Import Export Tables', 'top-10' ),
esc_html__( 'Import/Export', 'top-10' ),
'manage_options',
'tptn_exim_page',
array( $this, 'render_page' )
);
}
/**
* Admin Menu.
*
* @since 3.3.0
*/
public function network_admin_menu() {
$this->parent_id = add_submenu_page(
'tptn_dashboard',
esc_html__( 'Top 10 Import Export Tables', 'top-10' ),
esc_html__( 'Import/Export', 'top-10' ),
'manage_network_options',
'tptn_exim_page',
array( $this, 'render_page' )
);
}
/**
* Render the tools settings page.
*
* @since 2.7.0
*
* @return void
*/
public function render_page() {
/* Message for successful file import */
if ( isset( $_GET['file_import'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( 'success' === $_GET['file_import'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
add_settings_error( 'tptn-notices', '', esc_html__( 'Data has been imported into the table', 'top-10' ), 'success' );
} elseif ( 'fail' === $_GET['file_import'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
add_settings_error( 'tptn-notices', '', esc_html__( 'Data import failure. Check the number of columns for the file being imported, if you are uploading it in the right section below and that the data is correct', 'top-10' ), 'error' );
}
}
/* Message for successful file import */
if ( isset( $_GET['settings_import'] ) && 'success' === $_GET['settings_import'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
add_settings_error( 'tptn-notices', '', esc_html__( 'Settings have been imported successfully', 'top-10' ), 'success' );
}
ob_start();
?>
'tptn_exim_page',
'file_import' => 'fail',
),
is_network_admin() ? network_admin_url( 'admin.php' ) : admin_url( 'admin.php' )
)
);
exit;
}
$prepared = self::build_import_data( $parsed['rows'], false, $use_urls, $network_wide );
if ( ! empty( $prepared ) ) {
$results[] = Database::bulk_upsert( $prepared, false );
}
}
if ( $daily_upload ) {
$import_file = wp_unslash( $_FILES['daily_table_file']['tmp_name'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$parsed = Csv_Helper::parse_import_file( $import_file );
if ( 0 === $parsed['total'] ) {
wp_safe_redirect(
add_query_arg(
array(
'page' => 'tptn_exim_page',
'file_import' => 'fail',
),
is_network_admin() ? network_admin_url( 'admin.php' ) : admin_url( 'admin.php' )
)
);
exit;
}
$prepared = self::build_import_data( $parsed['rows'], true, $use_urls, $network_wide );
if ( ! empty( $prepared ) ) {
$results[] = Database::bulk_upsert( $prepared, true );
}
}
$all_success = ! empty( $results ) && ! in_array( false, $results, true );
$file_import = $all_success ? 'success' : 'fail';
wp_safe_redirect(
add_query_arg(
array(
'page' => 'tptn_exim_page',
'file_import' => $file_import,
),
is_network_admin() ? network_admin_url( 'admin.php' ) : admin_url( 'admin.php' )
)
);
exit;
}
/**
* Build structured import data from parsed CSV rows.
*
* @since 4.3.0
*
* @param array $rows Parsed rows from Csv_Helper::parse_import_file().
* @param bool $daily Whether these are daily-table rows.
* @param bool $use_urls Whether to resolve URL column to post IDs.
* @param bool $network_wide Whether this is a network-wide import.
* @return array Prepared data rows for Database::bulk_upsert().
*/
private static function build_import_data( array $rows, bool $daily, bool $use_urls, bool $network_wide ): array {
$prepared = array();
$url_list = array();
foreach ( $rows as $row ) {
$post_id = absint( $row['postnumber'] );
$blog_id = isset( $row['blog_id'] ) ? absint( $row['blog_id'] ) : get_current_blog_id();
if ( $use_urls && ! empty( $row['url'] ) ) {
$url = esc_url_raw( trim( $row['url'], " \t\n\r\0\x0B" ) );
if ( ! isset( $url_list[ $url ] ) ) {
$url_list[ $url ] = url_to_postid( $url );
}
$post_id = absint( $url_list[ $url ] );
}
if ( 0 === $post_id ) {
continue;
}
if ( ! $network_wide && ! is_network_admin() && (int) get_current_blog_id() !== $blog_id ) {
continue;
}
if ( $daily ) {
$raw_date = isset( $row['dp_date'] ) ? trim( $row['dp_date'], " \t\n\r\0\x0B" ) : '';
try {
$dp_date = ( new \DateTime( $raw_date ) )->format( 'Y-m-d H' );
} catch ( \Exception $e ) {
$dp_date = $raw_date;
}
$prepared[] = array(
'postnumber' => $post_id,
'cntaccess' => absint( $row['cntaccess'] ),
'dp_date' => $dp_date,
'blog_id' => $blog_id,
);
} else {
$prepared[] = array(
'postnumber' => $post_id,
'cntaccess' => absint( $row['cntaccess'] ),
'blog_id' => $blog_id,
);
}
}
return $prepared;
}
/**
* Process a settings export that generates a .json file of the Top 10 settings
*
* @since 2.7.0
*/
public static function process_settings_export() {
if ( empty( $_POST['tptn_action'] ) || 'export_settings' !== $_POST['tptn_action'] ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_POST['tptn_export_settings_nonce'] ), 'tptn_export_settings_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$settings = get_option( 'tptn_settings' );
ignore_user_abort( true );
nocache_headers();
header( 'Content-Type: application/json; charset=utf-8' );
header( 'Content-Disposition: attachment; filename=tptn-settings-export-' . gmdate( 'm-d-Y' ) . '.json' );
header( 'Expires: 0' );
echo wp_json_encode( $settings );
exit;
}
/**
* Process a settings import from a json file
*
* @since 2.7.0
*/
public static function process_settings_import() {
if ( empty( $_POST['tptn_action'] ) || 'import_settings' !== $_POST['tptn_action'] ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_POST['tptn_import_settings_nonce'] ), 'tptn_import_settings_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$filename = 'import_settings_file';
$extension = isset( $_FILES[ $filename ]['name'] ) ? pathinfo( sanitize_file_name( wp_unslash( $_FILES[ $filename ]['name'] ) ), PATHINFO_EXTENSION ) : '';
if ( 'json' !== $extension ) {
wp_die( esc_html__( 'Please upload a valid .json file', 'top-10' ) );
}
$import_file = isset( $_FILES[ $filename ]['tmp_name'] ) ? ( wp_unslash( $_FILES[ $filename ]['tmp_name'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( empty( $import_file ) ) {
wp_die( esc_html__( 'Please upload a file to import', 'top-10' ) );
}
// Retrieve the settings from the file and convert the json object to an array.
$settings = (array) json_decode( file_get_contents( $import_file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
update_option( 'tptn_settings', $settings );
wp_safe_redirect(
add_query_arg(
array(
'page' => 'tptn_exim_page',
'settings_import' => 'success',
),
admin_url( 'admin.php' )
)
);
exit;
}
}