class All_In_One_Redirection_Admin_Screen {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* The table name for this plugin
*
* @since 1.0.0
* @access protected
* @var string $db_table_name The table name for this plugin.
*/
protected $db_table_name;
/**
* The default site url for this plugin
*
* @since 1.0.0
* @access protected
* @var string $def_site_url The default site url for this plugin.
*/
protected $def_site_url;
/**
* The default setting site url for this plugin
*
* @since 1.0.0
* @access protected
* @var string $def_setting_site_url The default site url for this plugin.
*/
protected $def_setting_site_url;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version, $db_table_name, $def_site_url, $def_setting_site_url ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->db_table_name = $db_table_name;
$this->def_site_url = $def_site_url;
$this->def_setting_site_url = $def_setting_site_url;
}
/**
* Sanitize a value for CSV export to mitigate CSV injection.
* Prefix values that start with =, +, -, or @ with a single quote.
*/
private function csv_sanitize_value( $value ) {
$value = (string) $value;
if ( preg_match('/^[=+\-@]/', $value) ) {
return "'" . $value;
}
return $value;
}
// set the menu in admin section
public function vsz_add_admin_menu(){
add_menu_page(esc_html__( 'Redirection', 'all-in-one-redirection' ), esc_html__( 'Redirection', 'all-in-one-redirection' ), 'manage_options', 'all-in-one-redirection', array($this,'vsz_admin_redirection_screen'), 'dashicons-randomize', 40 );
add_submenu_page('all-in-one-redirection',esc_html__( 'Redirection Setting', 'all-in-one-redirection' ), esc_html__( 'Redirection Setting', 'all-in-one-redirection' ), 'manage_options', 'all-in-one-redirection-setting', array($this,'vsz_admin_redirection_setting_screen'),'', 41);
add_submenu_page('all-in-one-redirection',esc_html__( '404 Pages List', 'all-in-one-redirection' ), esc_html__( '404 Pages List', 'all-in-one-redirection' ), 'manage_options', 'all-in-one-redirection-404-pages-list', array($this,'vsz_admin_404_redirection_list_screen'),'', 42);
add_submenu_page('all-in-one-redirection',esc_html__( 'Tool', 'all-in-one-redirection' ), esc_html__( 'Tool', 'all-in-one-redirection' ), 'manage_options', 'all-in-one-redirection-tool', array($this,'vsz_admin_page_redirection_tool_screen'),'', 43);
}
// Redirection setting
public function vsz_admin_redirection_setting_screen(){
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Insufficient permissions.', 'all-in-one-redirection' ) );
}
echo '
';
echo '
'.esc_html__( 'Redirection Setting', 'all-in-one-redirection' ).'
';
echo '
';
if(isset($_POST['redirection_setting_btn']) && !empty($_POST['redirection_setting_btn'])){
if(wp_verify_nonce( sanitize_text_field($_POST['setting-nonce']), 'setting-page-redirection' )){
$redirection_setting = array();
$redirection_setting['host']=sanitize_text_field($_POST['host']);
$redirection_setting['www']=sanitize_text_field($_POST['www']);
//enable-disable host setting
if(sanitize_text_field($_POST['additional_setting'])=='CUSTOM-SETTING'){
$redirection_setting['setting_host_www']='TRUE';
}
else{
$redirection_setting['setting_host_www']='FALSE';
}
//default seeting based on site url
if(sanitize_text_field($_POST['additional_setting'])=='DEFAULT-SETTING'){
$redirection_setting['setting_default_siteurl']='TRUE';
$wp_vsz_site_url = $this->def_site_url;
if(strpos($wp_vsz_site_url,'https://') !== false ){
$redirection_setting['host']='HTTPS';
}
else{
$redirection_setting['host']='HTTP';
}
if(strpos($wp_vsz_site_url,'www.')){
$redirection_setting['www']='WWW';
}
else{
$redirection_setting['www']='NON-WWW';
}
}
else{
$redirection_setting['setting_default_siteurl']='FALSE';
}
//setting to remove all data during plugin deactive
if(sanitize_text_field($_POST['remove-redirection'])){
$redirection_setting['remove_redirection']='TRUE';
}
else{
$redirection_setting['remove_redirection']='FALSE';
}
//setting to save redirect 404 pages to the Home page value
if(sanitize_text_field($_POST['redirect-404-to-home'])){
$redirection_setting['redirect_404_to_home']='YES';
}
else{
$redirection_setting['redirect_404_to_home']='NO';
}
update_option('all-in-one-redirection-setting',json_encode($redirection_setting));
echo '
'.esc_html__( 'Setting saved successfully.', 'all-in-one-redirection' ).'
';
}
else{
exit;
}
}
echo '
';
echo '
';
echo '
';
}
/*
* Add new redirection record
* List of all the redirection records
* Update optino for all records
* Delete Particular single records
* Delete all records
*/
public function vsz_admin_redirection_screen(){
global $wpdb;
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Insufficient permissions.', 'all-in-one-redirection' ) );
}
$post_per_page = 20;
$no_of_page = 0;
$current_page = 0;
$record_start = 0;
$table_name = $this->db_table_name;
echo '';
echo '';
echo '
'.esc_html__( 'Redirection List', 'all-in-one-redirection' ).'
';
// set the filter variables
if(isset($_REQUEST['record-filter']) && !empty($_REQUEST['record-filter'])){
$filter_key = trim( sanitize_text_field( wp_unslash( $_REQUEST['keyword'] ) ) );
$filter_type = sanitize_text_field( wp_unslash( $_REQUEST['type'] ) );
$filter_hide_record = sanitize_text_field( wp_unslash( $_REQUEST['hide'] ) );
$filter_reg_record = sanitize_text_field( wp_unslash( $_REQUEST['regexp'] ) );
}
// filter record form
echo '
';
// Delete all records
if(isset($_POST['delete_all_btn']) && !empty($_POST['delete_all_btn'])){
if(wp_verify_nonce( sanitize_text_field($_POST['delete-all-nonce']), 'delete-all-page-redirection' )){
//Changed dated 15-05-2023
$del_all = $wpdb->query( "DELETE FROM `" . esc_sql( $table_name ) . "` WHERE rtype != 404" );
if($del_all){
echo '
'.esc_html__( 'All Record deleted successfully.', 'all-in-one-redirection' ).'
';
}
}
else{
exit;
}
}
// Reset Hits Records
if(isset($_POST['reset_hits_btn']) && !empty($_POST['reset_hits_btn'])){
if(wp_verify_nonce( sanitize_text_field($_POST['reset-all-hits-nonce']), 'reset-all-hits-redirection' )){
//Changed dated 15-05-2023
$reset_hits = $wpdb->query($wpdb->prepare("update $table_name set hits=0 where rtype!=404"));
if($reset_hits){
echo '
'.esc_html__( 'Hits records are reset successfully.', 'all-in-one-redirection' ).'
';
}
}
else{
exit;
}
}
// Update all the records
if(isset($_POST['update_list'])){
if(wp_verify_nonce( sanitize_text_field($_POST['redirection-list-nonce']), 'list-page-redirection' )){
$c_list = 0;
while($c_list
def_setting_site_url,'',trim($sourceurl));
if(empty($sourceurl)){
$sourceurl = '/';
}
if(!isset($_POST['destination_url'][$c_list]) || empty($_POST['destination_url'][$c_list])){
$destinationurl = $this->def_setting_site_url.'/';
}
else{
$destinationurl = esc_url_raw( wp_unslash( trim( $_POST['destination_url'][$c_list] ) ) );
}
if(isset($_POST['check_reg_exp_btn'][sanitize_text_field($_POST['redirection_id'][$c_list])])){
$regexpression = sanitize_text_field($_POST['check_reg_exp_btn'][sanitize_text_field($_POST['redirection_id'][$c_list])]);
if($regexpression!=1){
$regexpression=0;
}
}
else{
$regexpression=0;
}
if(isset($_POST['check_hide_btn'][sanitize_text_field($_POST['redirection_id'][$c_list])])){
$redirectionhide = sanitize_text_field($_POST['check_hide_btn'][sanitize_text_field($_POST['redirection_id'][$c_list])]);
if($redirectionhide!=1){
$redirectionhide=0;
}
}
else{
$redirectionhide=0;
}
$defsource = trim($_POST['source_url'][$c_list]);
if(isset($defsource) && !empty($defsource) && substr($sourceurl,0,1) == '/'){
$sourceurl = str_ireplace(' ', '%20', trim($sourceurl));
$wpdb->update(
$table_name,
array(
'rtype' => trim(sanitize_text_field($_POST['redirection_type'][$c_list])),
'source_url' => stripslashes(trim($sourceurl)),
'destination_url' => trim($destinationurl),
'reg_expression' => trim($regexpression),
'hide_url' => trim($redirectionhide)
),
array( 'id' => sanitize_text_field($_POST['redirection_id'][$c_list]) )
);
}
$c_list++;
}
echo ''.esc_html__( 'Record updated successfully.', 'all-in-one-redirection' ).'
';
}
else{
exit;
}
}
elseif(isset($_POST['delete_selected_list'])){
if(wp_verify_nonce(sanitize_text_field( wp_unslash( $_POST['redirection-list-nonce'] ) ),'list-page-redirection')){
if ( ! empty( $_POST['check_delete_btns'] ) && is_array( $_POST['check_delete_btns'] ) ) {
$page_ids = array_map( 'absint', wp_unslash( $_POST['check_delete_btns'] ) );
$page_ids = array_filter( $page_ids );
if ( ! empty( $page_ids ) ) {
$placeholders = implode( ',', array_fill( 0, count( $page_ids ), '%d' ) );
$sql = $wpdb->prepare(
"DELETE FROM {$table_name} WHERE id IN ($placeholders)",
$page_ids
);
$del_record = $wpdb->query( $sql );
if ( false !== $del_record ) {
echo '' .
esc_html__( 'Record deleted successfully.', 'all-in-one-redirection' ) .
'
';
}
}
}
}
else{
exit;
}
}
// filter record fetch — build prepared WHERE clauses and parameters
$safe_table = esc_sql( $table_name );
$where_clauses = array( 'rtype != %d' );
$where_params = array( 404 );
if ( isset( $_REQUEST['record-filter'] ) && ! empty( $_REQUEST['record-filter'] ) ) {
if ( isset( $filter_key ) && $filter_key !== '' ) {
$like = '%' . $wpdb->esc_like( $filter_key ) . '%';
$where_clauses[] = '( source_url LIKE %s OR destination_url LIKE %s )';
$where_params[] = $like;
$where_params[] = $like;
}
if ( isset( $filter_type ) && $filter_type !== '' ) {
$where_clauses[] = 'rtype = %d';
$where_params[] = intval( $filter_type );
}
if ( isset( $filter_hide_record ) && $filter_hide_record !== '' ) {
if ( 'true' === $filter_hide_record ) {
$where_clauses[] = 'hide_url = %d';
$where_params[] = 1;
} elseif ( 'false' === $filter_hide_record ) {
$where_clauses[] = 'hide_url = %d';
$where_params[] = 0;
}
}
if ( isset( $filter_reg_record ) && $filter_reg_record !== '' ) {
if ( 'true' === $filter_reg_record ) {
$where_clauses[] = 'reg_expression = %d';
$where_params[] = 1;
} elseif ( 'false' === $filter_reg_record ) {
$where_clauses[] = 'reg_expression = %d';
$where_params[] = 0;
}
}
}
$where_sql = ' WHERE ' . implode( ' AND ', $where_clauses );
// Pagination — ensure integer values
$per_page = absint( $post_per_page );
$current_page = isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
$current_page = max( 1, $current_page );
$record_start = ( $current_page - 1 ) * $per_page;
// Total count using prepared query
if ( ! empty( $where_params ) ) {
$count_query = call_user_func_array(
array( $wpdb, 'prepare' ),
array_merge( array( "SELECT COUNT(id) FROM {$safe_table} {$where_sql}" ), $where_params )
);
}
else {
$count_query = "SELECT COUNT(id) FROM {$safe_table}";
}
$no_of_total_page = (int) $wpdb->get_var( $count_query );
$no_of_page = ( $per_page > 0 ) ? ceil( $no_of_total_page / $per_page ) : 0;
// Build select query with LIMIT placeholders
$select_sql = "SELECT * FROM {$safe_table} {$where_sql} ORDER BY id DESC LIMIT %d, %d";
$select_params = $where_params;
$select_params[] = $record_start;
$select_params[] = $per_page;
$select_query = call_user_func_array(
array( $wpdb, 'prepare' ),
array_merge( array( $select_sql ), $select_params )
);
$redirection_list = $wpdb->get_results($select_query);
if($redirection_list){
echo '';
echo '';
echo '';
// custom pagination
$pagination_info = array();
$pagination_info['total_pages'] = (int) $no_of_page;
$pagination_info['curr_page'] = (int) $current_page;
if($no_of_page>1){
$filter_pagination_condition = '';
if(isset($_REQUEST['record-filter']) && !empty($_REQUEST['record-filter'])){
$filter_pagination_condition .= '&keyword=' . urlencode( stripslashes( $filter_key ) );
$filter_pagination_condition .= '&type='.$filter_type;
$filter_pagination_condition .= '&hide='.$filter_hide_record;
$filter_pagination_condition .= '®exp='.$filter_reg_record;
$filter_pagination_condition .= '&record-filter=yes';
}
echo '';
} // END custom pagination
}
else{
if(!isset($del_all)){
echo ''.esc_html__( 'No record found.', 'all-in-one-redirection' ).'
';
}
}
echo ' ';
}
/*
* 404 pages list
* Add 404 page in redirection
*/
public function vsz_admin_404_redirection_list_screen(){
global $wpdb;
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Insufficient permissions.', 'all-in-one-redirection' ) );
}
$post_per_page = 20;
$no_of_page = 0;
$current_page = 0;
$record_start = 0;
$table_name = $this->db_table_name;
echo '';
echo '
'.esc_html__( '404 Pages List', 'all-in-one-redirection' ).'
';
// set the filter variables
if(isset($_REQUEST['record-404-filter']) && !empty($_REQUEST['record-404-filter'])){
$filter_404_key = trim( sanitize_text_field( wp_unslash( $_REQUEST['keyword'] ) ) );
}
// filter 404 record form
echo '
';
// delete selected all 404 page records
if(isset($_POST['delete-all-404-pages']) && !empty($_POST['delete-all-404-pages'])){
if(wp_verify_nonce( sanitize_text_field($_POST['delete-404-page-nonce']), 'delete-404-page-list' )){
//Changed dated 15-05-2023
$del_404_page_all = $wpdb->query($wpdb->prepare("DELETE FROM $table_name where rtype=404"));
if($del_404_page_all){
echo '
'.esc_html__( 'All Record deleted successfully.', 'all-in-one-redirection' ).'
';
}
}
else{
exit;
}
}
// delete selected 404 page records
if(isset($_POST['delete-404-pages']) && !empty($_POST['delete-404-pages'])){
if(wp_verify_nonce( sanitize_text_field($_POST['delete-404-page-nonce']), 'delete-404-page-list' )){
if ( ! empty( $_POST['check_404_page_delete_btns'] ) && is_array( $_POST['check_404_page_delete_btns'] ) ) {
$page_404_ids = array_map( 'absint', wp_unslash( $_POST['check_404_page_delete_btns'] ) );
$page_404_ids = array_filter( $page_404_ids );
if( ! empty( $page_404_ids ) ) {
$placeholders = implode( ',', array_fill( 0, count( $page_404_ids ), '%d' ) );
$sql = $wpdb->prepare( "DELETE FROM {$table_name} WHERE id IN ($placeholders)", $page_404_ids );
$del_404_page = $wpdb->query( $sql );
if ( $del_404_page ) {
echo '
'.esc_html__( 'Record deleted successfully.', 'all-in-one-redirection' ).'
';
}
}
}
}
else{
exit;
}
}
// Save new 404 page record
if(isset($_POST['insert_404_page_redirection_btn'])){
$source_url = rtrim(trim($_POST['source_url_insert']),'/');
$source_url = str_ireplace($this->def_setting_site_url,'',trim($source_url));
if(empty($source_url)){
$source_url = '/';
}
if(!isset($_POST['destination_url_insert']) || empty($_POST['destination_url_insert'])){
$destination_url = $this->def_setting_site_url.'/';
}
else{
$destination_url = esc_url_raw( wp_unslash( trim( $_POST['destination_url_insert'] ) ) );
}
$regexpression_check = sanitize_text_field($_POST['reg_expression_check']);
if($regexpression_check!=1){
$regexpression_check=0;
}
if(wp_verify_nonce( sanitize_text_field($_POST['insert-404-page-nonce']), 'insert-404-page-redirection' )){
$def_source = trim($_POST['source_url_insert']);
if(isset($def_source) && !empty($def_source)){
if(substr($source_url,0,1) == '/'){
$source_url = str_ireplace(' ', '%20', trim($source_url));
$chk_exist_rule = 0;
//Changed dated 15-05-2023
$source_url = stripslashes(trim($source_url));
$chk_exist_rule_data = $wpdb->get_row($wpdb->prepare("select * from {$table_name} where source_url=%s and reg_expression=%s and hide_url=0 and rtype!=404 order by id DESC",$source_url, $regexpression_check));
if($chk_exist_rule_data){
if($chk_exist_rule_data->source_url==stripslashes(trim($source_url))){
$chk_exist_rule = 1;
}
}
if(!$chk_exist_rule){
$r_insert = $wpdb->insert(
$table_name,
array(
'rtype' => trim(sanitize_text_field($_POST['redirection_type'])),
'source_url' => stripslashes(trim($source_url)),
'destination_url' => trim($destination_url),
'reg_expression' => trim($regexpression_check),
'time' => current_time( 'mysql' )
)
);
if($r_insert){
//Changed dated 15-05-2023
$del_404_page_all = $wpdb->query($wpdb->prepare("DELETE FROM $table_name where id=%d", sanitize_text_field($_POST['page_404_id'])));
echo '
'.esc_html__( 'Record added successfully.', 'all-in-one-redirection' ).'
';
}
}
else{
echo '
'.esc_html__( 'Please insert valid source url because of duplicate entry.', 'all-in-one-redirection' ).'
';
}
}
else{
echo '
'.esc_html__( 'Please insert valid source url with slash(/).', 'all-in-one-redirection' ).'
';
}
}
else{
echo '
'.esc_html__( 'Please insert the source url.', 'all-in-one-redirection' ).'
';
}
}
else{
exit;
}
}
//Changed dated 15-05-2023
// filter record fetch for 404 list — build prepared WHERE clauses and parameters
$safe_table = esc_sql( $table_name );
$where_clauses_404 = array( 'rtype = %d' );
$where_params_404 = array( 404 );
if ( isset( $_REQUEST['record-404-filter'] ) && ! empty( $_REQUEST['record-404-filter'] ) ) {
if ( isset( $filter_404_key ) && $filter_404_key !== '' ) {
$like_404 = '%' . $wpdb->esc_like( $filter_404_key ) . '%';
$where_clauses_404[] = 'source_url LIKE %s';
$where_params_404[] = $like_404;
}
}
$where_sql_404 = ' WHERE ' . implode( ' AND ', $where_clauses_404 );
// Pagination — ensure integer values
$per_page_404 = absint( $post_per_page );
$current_page_404 = isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
$current_page_404 = max( 1, $current_page_404 );
$record_start_404 = ( $current_page_404 - 1 ) * $per_page_404;
// Total count using prepared query
if ( ! empty( $where_params_404 ) ) {
$count_sql_404 = "SELECT COUNT(id) as no_of_total_record FROM {$safe_table} {$where_sql_404}";
$count_args_404 = array_merge( array( $count_sql_404 ), $where_params_404 );
$count_query_404 = call_user_func_array( array( $wpdb, 'prepare' ), $count_args_404 );
} else {
$count_query_404 = "SELECT COUNT(id) as no_of_total_record FROM {$safe_table}";
}
$no_of_total_page = $wpdb->get_var( $count_query_404 );
$no_of_page = ( $per_page_404 > 0 ) ? ceil( $no_of_total_page / $per_page_404 ) : 0;
// expose pagination variables to the template below
$current_page = $current_page_404;
$record_start = $record_start_404;
// Build select query with LIMIT placeholders
$select_sql_404 = "SELECT * FROM {$safe_table} {$where_sql_404} ORDER BY last_access DESC LIMIT %d, %d";
$select_params_404 = $where_params_404;
$select_params_404[] = $record_start_404;
$select_params_404[] = $per_page_404;
$select_args_404 = array_merge( array( $select_sql_404 ), $select_params_404 );
$select_404_page_query = call_user_func_array( array( $wpdb, 'prepare' ), $select_args_404 );
$page_404_data = $wpdb->get_results( $select_404_page_query );
if($page_404_data){
echo '
';
// custom pagination
$pagination_info = array();
$pagination_info['total_pages'] = $no_of_page;
$pagination_info['curr_page'] = $current_page;
if($no_of_page>1){
$filter_404_pagination_condition = '';
if(isset($_REQUEST['record-404-filter']) && !empty($_REQUEST['record-404-filter'])){
$filter_404_pagination_condition .= '&keyword=' . urlencode( stripslashes( $filter_404_key ) );
$filter_404_pagination_condition .= '&record-404-filter=yes';
}
echo '';
} // END custom pagination
echo '
';
}
else{
if(!isset($del_404_page_all)){
echo '
'.esc_html__( 'No record found.', 'all-in-one-redirection' ).'
';
}
}
echo '
';
}
/*
* Tool - Import/Export Functionality
*/
public function vsz_admin_page_redirection_tool_screen(){
global $wpdb;
$table_name = esc_sql($this->db_table_name);
echo '';
echo '
';
echo '
';
echo '
'.esc_html__( 'Redirection Bulk Import', 'all-in-one-redirection' ).'
';
if(isset($_POST['import_file_redirection_btn']) && !empty($_POST['import_file_redirection_btn'])){
if(wp_verify_nonce( sanitize_text_field($_POST['file-import-nonce']), 'import-file-page-redirection' )){
$flag=1;
$error_msg="";
$import_file_name = $_FILES['redirection-file']['name'];
$file_path = plugin_dir_path(dirname(dirname( __FILE__ ))).'upload/import/';
$file_basename = substr($import_file_name, 0, strripos($import_file_name, '.')); // get file name
$file_ext = substr($import_file_name, strripos($import_file_name, '.')); // get file extention
$allowed_file_types = array('.csv');
if($import_file_name){
if(in_array($file_ext,$allowed_file_types)){
$newfilename = $file_path.'all-in-one-redirection-import-'.date('Ymdhis').$file_ext;
// use WordPress Filesystem API for safer file handling
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$uploaded = wp_handle_upload( $_FILES['redirection-file'], array( 'test_form' => false ) );
if ( isset( $uploaded['error'] ) ) {
$flag = 0;
$error_msg = esc_html__( 'File upload failed: ', 'all-in-one-redirection' ) . esc_html( $uploaded['error'] );
}
else {
$import_csv_file = $uploaded['file'];
if(( $handle = fopen( $import_csv_file, 'r' ) ) !== false ){
$header = array("Codes","Status");
//Seperating the values by semicolon in csv
$row_count=0;
$skip_record_list=0;
$skip_record_flag=true;
while($csv_data = fgetcsv($handle)){
$row_count++;
if($row_count!=1){
$num_field = count($csv_data);
if($num_field == 5){
if(!empty($csv_data[0])){
$flag=1;
$file_source_url = rtrim($csv_data[1],'/');
$def_source = trim($csv_data[1]);
$file_source_url = str_ireplace($this->def_setting_site_url,'',trim($file_source_url));
if(empty($file_source_url)){
$file_source_url = '/';
}
if(!isset($csv_data[2]) || empty($csv_data[2])){
$file_destination_url = $this->def_setting_site_url.'/';
}
else{
$file_destination_url = esc_url($csv_data[2]);
}
$redirection_type = trim($csv_data[0]);
if($redirection_type!=301 && $redirection_type!=302){
$redirection_type=301;
}
$redirection_reg_expression = trim($csv_data[3]);
if($redirection_reg_expression!=1){
$redirection_reg_expression=0;
}
$redirection_hide = trim($csv_data[4]);
if($redirection_hide!=1){
$redirection_hide=0;
}
$skip_record_list++;
$chk_exist_rule = 0;
//changed dated 15-05-2023
$file_source_url = stripslashes(trim($file_source_url));
$chk_exist_rule_data = $wpdb->get_row($wpdb->prepare("select * from {$table_name} where source_url=%s and reg_expression=%s and hide_url=%s and rtype!=404 order by id DESC",$file_source_url, $redirection_reg_expression, $redirection_hide));
if($chk_exist_rule_data){
if($chk_exist_rule_data->source_url==stripslashes(trim($file_source_url))){
$chk_exist_rule = 1;
}
}
if(isset($def_source) && !empty($def_source) && substr($file_source_url,0,1) == '/' && !$chk_exist_rule){
$wpdb->insert(
$table_name,
array(
'rtype' => $redirection_type,
'source_url' => stripslashes(trim($file_source_url)),
'destination_url' => trim($file_destination_url),
'reg_expression' => $redirection_reg_expression,
'hide_url' => $redirection_hide,
'time' => current_time( 'mysql' )
)
);
}
else{
if($skip_record_flag){
echo '
'.esc_html__( 'Below records are skipped because duplicate entry or source url is not valid. So, please check it.', 'all-in-one-redirection' ).'
';
$skip_record_flag=false;
}
echo '
' . esc_html__( 'Index ', 'all-in-one-redirection' ) . esc_html( $skip_record_list + 1 ) . ': ' . esc_html( $csv_data[1] ) . '
';
}
}
}
else{
$flag=0;
$error_msg = esc_html__( 'Number of fields in csv file does not matched and file must be separated with comma.', 'all-in-one-redirection' );
}
}
}
}
else{
$flag=0;
$error_msg = esc_html__( 'File permission not valid.', 'all-in-one-redirection' );
}
}
}
else{
$flag=0;
$error_msg = esc_html__( 'Invalid file type. Please upload the csv file.', 'all-in-one-redirection' );
}
}
else{
$flag=0;
$error_msg = esc_html__( 'Please upload the file.', 'all-in-one-redirection' );
}
if($flag==0){
echo '
'.esc_html($error_msg).'
';
}
else{
echo '
'.esc_html__( 'All records are added successfully.', 'all-in-one-redirection' ).'
';
//import csv delete from file path var import_csv_file
if(isset($import_csv_file) && !empty($import_csv_file)){
unlink($import_csv_file);
}
}
}
else{
echo '
'.esc_html__( 'There is some technical problem. Please try again.', 'all-in-one-redirection' ).'
';
}
}
echo '
';
echo '
';
echo '
';
echo '
'.esc_html__( 'Redirection Bulk Export', 'all-in-one-redirection' ).'
';
if(isset($_POST['export_file_redirection_btn']) && !empty($_POST['export_file_redirection_btn'])){
if(wp_verify_nonce( sanitize_text_field($_POST['file-export-nonce']), 'export-file-page-redirection' )){
$file_path = plugin_dir_path(dirname(dirname( __FILE__ ))).'upload/export/';
$file_field_header = array('Type','Source-URL','Destination-URL','Regular-Expression','Hide');
$newFileName = 'all-in-one-redirection-export-'.date('Ymdhis').'.csv';
$exportedFileName = $file_path.$newFileName;
$myfile = @fopen( $exportedFileName, 'w' );
if ( false === $myfile ) {
echo '
' . esc_html__( 'Unable to open file for writing. Check folder permissions.', 'all-in-one-redirection' ) . '
';
}
else {
$arrInfo = array_values( $file_field_header );
// sanitize headers and write
$sanitized = array_map( array( $this, 'csv_sanitize_value' ), $arrInfo );
fputcsv( $myfile, $sanitized, ',' );
$redirection_list_select = $wpdb->get_results( "SELECT * FROM $table_name where rtype!=404 order by id DESC" );
foreach ( $redirection_list_select as $key => $redirection_page ) {
$file_field_value = array(
$this->csv_sanitize_value( $redirection_page->rtype ),
$this->csv_sanitize_value( $redirection_page->source_url ),
$this->csv_sanitize_value( $redirection_page->destination_url ),
$this->csv_sanitize_value( $redirection_page->reg_expression ),
$this->csv_sanitize_value( $redirection_page->hide_url ),
);
fputcsv( $myfile, $file_field_value, ',' );
}
fclose( $myfile );
$file_export_path = plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'upload/export/' . $newFileName;
echo '
';
}
}
else{
echo '
'.esc_html__( 'There is some technical problem. Please try again.', 'all-in-one-redirection' ).'
';
}
}
elseif(isset($_POST['export_file_404_pages_list_btn']) && !empty($_POST['export_file_404_pages_list_btn'])){
if(wp_verify_nonce( sanitize_text_field($_POST['file-export-nonce']), 'export-file-page-redirection' )){
$file_path = plugin_dir_path(dirname(dirname( __FILE__ ))).'upload/export/';
$file_field_header = array('Access-Date','Source-URL','Hits');
$newFileName = 'all-in-one-404-page-list-export-'.date('Ymdhis').'.csv';
$exportedFileName = $file_path.$newFileName;
$myfile = fopen($exportedFileName, 'w') or
die("
");
if($myfile){
$arrInfo = array();
$arrInfo = array_values($file_field_header);
fputcsv($myfile, $arrInfo,',');
unset($arrInfo);
//Changed dated 15-05-2023
$redirection_list_select = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table_name where rtype=404 order by last_access DESC") );
foreach($redirection_list_select as $key => $redirection_page){
$file_field_value = array(date('M d, Y h:i A',strtotime($redirection_page->last_access)),$redirection_page->source_url,$redirection_page->hits);
$arrInfo = array();
$arrInfo = array_values($file_field_value);
fputcsv($myfile, $arrInfo,',');
}
fclose($myfile);
$file_export_path = plugin_dir_url(dirname(dirname( __FILE__))).'upload/export/'.$newFileName;
echo '
';
}
else{
echo '
'.esc_html__( 'There is some folder permission issue. Please set the proper file permission.', 'all-in-one-redirection' ).'
';
}
}
else{
echo '
'.esc_html__( 'There is some technical problem. Please try again.', 'all-in-one-redirection' ).'
';
}
}
echo '
';
echo '
';
echo '
';
echo '
';
}
}//close class