always_load_this_plugin_first(); // reload plugin settings $this->reload_settings(); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_plugin_action_links' ) ); add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'admin_menu', array( $this, 'add_menu_page' ) ); // Save/Upate settings form add_action( "wp_ajax_geoipr_form_submit", array( $this, 'save_settings_fields' ) ); // reset all redirect rules or generate json file for export... also if uploaded import settings too add_action( 'admin_init', array( $this, 'form_submit_actions' ) ); add_action( 'admin_footer_text', array( $this, 'admin_footer_text' ) ); } /** * We need to load this plugin first to work properely * * @access public * @return array */ public function always_load_this_plugin_first() { $geoipr_plugin = plugin_basename( trim( __FILE__ ) ); $active_plugins = get_option( 'active_plugins' ); if ( $geoipr_plugin_name = array_search( $geoipr_plugin, $active_plugins ) ) { // remove the plugin from any order it is now array_splice( $active_plugins, $geoipr_plugin_name, 1 ); // add this plugin to the beginning to load first array_unshift( $active_plugins, $geoipr_plugin ); // update now the option update_option( 'active_plugins', $active_plugins ); } } /** * Add Plugin Settings Page Link To Plugin List Table.. * * @access public * @return array */ public function add_plugin_action_links( $links ) { $links[] = sprintf( '%s', admin_url( 'options-general.php?page=geoip-redirect.php' ), __( 'Settings', 'adv-geoip-redirect' ) ); return $links; } /** * Refresh plugin settings value.. * * @access public * @return null */ public function reload_settings() { // check if settings is available or set empty values... if ( ! $this->redirect_settings = GEOIPR_UTIL::get_plugin_settings() ) { GEOIPR_UTIL::reset_plugin_settings(); $this->redirect_settings = GEOIPR_UTIL::get_plugin_settings(); } } /** * load language translations * * @access public * @return null */ public function load_plugin_textdomain() { load_plugin_textdomain( 'adv-geoip-redirect', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } /** * Load Plugin Scripts & Styles * * @access public * @return null */ public function enqueue_scripts() { $current_scrn = get_current_screen(); if ( $current_scrn->id !== 'settings_page_geoip-redirect' ) return; // reload plugin settings $this->reload_settings(); wp_enqueue_style ( 'geoipr_bootstrap_css', GEOIPR_PLUGIN_URL . 'assets/css/bootstrap.css', false ); wp_enqueue_style ( 'geoipr_select2_css', GEOIPR_PLUGIN_URL . 'assets/css/select2.min.css', false ); wp_enqueue_style ( 'geoipr_plugin_css', GEOIPR_PLUGIN_URL . 'assets/css/style.css', false ); wp_enqueue_script( 'geoipr_select2_script', GEOIPR_PLUGIN_URL . 'assets/js/select2.min.js', array(), '', true ); wp_enqueue_script( 'geoipr_plugin_script', GEOIPR_PLUGIN_URL . 'assets/js/script.js', array( 'geoipr_select2_script', 'jquery', 'wp-util', 'jquery-ui-sortable' ), '', true ); wp_localize_script( 'geoipr_plugin_script', 'geoipr', array( 'btnSavingText' => __( 'Saving... Please Wait', 'adv-geoip-redirect' ), 'confirnDeleteMsg' => __( 'Do You Really Want To Delete This Redirect Rule?', 'adv-geoip-redirect' ), 'confirnResetMsg' => __( 'Do You Really Want To Reset All Redirect Rules? Please Make a backup using the Export Tool below to restore again!', 'adv-geoip-redirect' ), 'redirectRules' => json_encode( $this->redirect_settings['redirect_rules'] ) )); } /** * Add Plugin Settings page to dashboard * * @access public * @return null */ public function add_menu_page() { add_options_page( 'GeoIP Redirect', 'GeoIP Redirect', 'manage_options' , 'geoip-redirect.php', array( $this, 'render_menu_page' ) ); } /** * Add Plugin Settings page to dashboard * * @access public * @return null */ public function save_settings_fields() { if ( ! isset( $_POST['_wpnonce_geoipr_settings_form'] ) || ! wp_verify_nonce( $_POST['_wpnonce_geoipr_settings_form'], 'geoipr_settings_form' ) ) { $response = array( 'status' => 'error', 'message' => __( 'Sorry, your nonce did not verify.', 'adv-geoip-redirect') ); // nonce is fine so process form fields }else{ // delete unnecessary post fields unset( $_POST['_wpnonce_geoipr_settings_form'] ); unset( $_POST['action'] ); // sanitize post fields recursively $form_values = GEOIPR_UTIL::sanitize_array_recursively( $_POST ); if ( ! isset( $form_values['redirect_rules'] ) ) $form_values['redirect_rules'] = array(); update_option( "geoipr_redirect_options", $form_values ); $response = array( 'status' => 'success', 'message' => __( 'Settings Updated Successfully!', 'adv-geoip-redirect') ); } wp_send_json( $response ); exit(); } /** * reset all redirect rules or generate json file for export... also if uploaded import settings too * * @access public * @return null */ public function form_submit_actions() { $nonce_error = array( 'class' => 'notice notice-warning', 'message' => __( 'Sorry, your nonce did not verify.', 'adv-geoip-redirect' ) ); if ( isset( $_POST['geoipr_export_action'] ) && current_user_can( 'manage_options' ) ) { if ( ! isset( $_POST['_wpnonce_geoipr_settings_export_form'] ) || ! wp_verify_nonce( $_POST['_wpnonce_geoipr_settings_export_form'], 'geoipr_settings_export_form' ) ) { $this->notices[] = $nonce_error; } else { GEOIPR_UTIL::export_settings(); } } if ( isset( $_POST['geoipr_import_action'] ) && current_user_can( 'manage_options' ) ) { if ( ! isset( $_POST['_wpnonce_geoipr_settings_import_form'] ) || ! wp_verify_nonce( $_POST['_wpnonce_geoipr_settings_import_form'], 'geoipr_settings_import_form' ) ) { $this->notices[] = $nonce_error; } else { $import_file = $_FILES['import_file']['tmp_name']; if( empty( $import_file ) ) { $this->notices[] = array( 'class' => 'notice notice-warning', 'message' => __( 'Please upload a file to import.', 'adv-geoip-redirect' ) ); return; } $file_name = $_FILES['import_file']['name']; // get file extension $ext = explode( '.', $file_name ); $extension = end( $ext ); if( $extension != 'json' ) { $this->notices[] = array( 'class' => 'notice notice-warning', 'message' => __( 'Please upload a valid .json file.', 'adv-geoip-redirect' ) ); return; } if ( GEOIPR_UTIL::import_settings( $import_file ) ) { $this->notices[] = array( 'class' => 'notice notice-success', 'message' => __( 'Settings Imported Successfully', 'adv-geoip-redirect' ) ); }else{ $this->notices[] = array( 'class' => 'notice notice-warning', 'message' => __( 'Something Went Wrong! Please Try Again!', 'adv-geoip-redirect' ) ); } } } if ( isset( $_POST['geoipr_reset_btn'] ) && $_POST['geoipr_reset_btn'] == '1' ) { if ( ! isset( $_POST['_wpnonce_geoipr_settings_form'] ) || ! wp_verify_nonce( $_POST['_wpnonce_geoipr_settings_form'], 'geoipr_settings_form' ) ) { $this->notices = $nonce_error; } else { if( current_user_can('administrator' )){ GEOIPR_UTIL::reset_plugin_settings(); $this->notices[] = array( 'class' => 'notice notice-success', 'message' => __( 'Filters Reset Successfully', 'adv-geoip-redirect' ) ); } } } $this->show_admin_notices(); } /** * show form submit status messages * * @access public * @return null */ public function show_admin_notices() { if ( !empty( $this->notices ) ) { foreach ( $this->notices as $notice ) { add_action( 'admin_notices', function() use ( $notice ) { printf( '
%2$s