/** * Return an instance of this class * * @return object */ public static function instance() { if (self::$instance == null) { self::$instance = new self(); } return self::$instance; }
/** * Action * * @return void */ public function action() { // Create custom nonce $this->create_nonce('db_update'); // Load views if action is empty if (empty($this->action)) { $this->content($this->view()); } // Load views if action is not empty (handle postdata) if (!empty($this->action) && check_admin_referer($this->nonce, '_nonce')) { // Do the update if ($this->action == 'db_update') { $version_info = get_option(self::$plugin_prefix . '_version'); $result = GW_GoPricing_Data::db_update(); if ($result === false) { GW_GoPricing_AdminNotices::add('db_update', 'error', __('Oops, something went wrong!', 'go_pricing_textdomain')); } else { GW_GoPricing_AdminNotices::add('db_update', 'success', sprintf(__('%1$s pricing table(s) has been successfully updated.', 'go_pricing_textdomain'), $result)); $version_info['db'] = self::$db_version; update_option(self::$plugin_prefix . '_version', $version_info); delete_option(self::$plugin_prefix . '_tables'); wp_redirect(admin_url('admin.php?page=go-pricing')); exit; } } else { $this->content($this->view()); } } }
/** * Process postdata * * @return void */ public function process_postdata($postdata) { // Clean postdata $postdata = GW_GoPricing_Helper::clean_input($postdata, 'filtered', '', array('thousand-sep')); $postdata = GW_GoPricing_Helper::remove_input($postdata, 'action'); $settings = get_option(self::$plugin_prefix . '_table_settings', $postdata); // Veriify and save data if (!empty($postdata)) { if (empty($settings) || !empty($settings) && $settings != $postdata) { $result = update_option(self::$plugin_prefix . '_table_settings', $postdata); if ($result === false) { GW_GoPricing_AdminNotices::add('general_settings', 'error', __('Oops, something went wrong!', 'go_pricing_textdomain')); } else { GW_GoPricing_AdminNotices::add('general_settings', 'success', __('General Settings has been successfully updated!', 'go_pricing_textdomain')); } } else { GW_GoPricing_AdminNotices::add('general_settings', 'success', __('General Settings has been successfully updated!', 'go_pricing_textdomain')); } } else { GW_GoPricing_AdminNotices::add('general_settings', 'error', __('Oops, something went wrong!', 'go_pricing_textdomain')); } }
/** * Do import * * @return bool */ public function save_table($data) { if (empty($data['name'])) { GW_GoPricing_AdminNotices::add('main', 'error', __('Pricing "Table Name" is empty!', 'go_pricing_textdomain'), false); } if (empty($data['id'])) { GW_GoPricing_AdminNotices::add('main', 'error', __('Pricing "Table ID" is empty!', 'go_pricing_textdomain'), false); } elseif (strlen($data['id']) >= 1 && preg_match('/([^a-z0-9\\-_])+/', $data['id']) == 1) { GW_GoPricing_AdminNotices::add('main', 'error', __('Pricing "Table ID" can contain only lowercase letters, numbers, hyphens and underscores!', 'go_pricing_textdomain'), false); } else { $tables = GW_GoPricing_Data::get_tables(); foreach ((array) $tables as $table_key => $table) { $table_ids[$table_key] = $table['id']; } if (!empty($data['postid']) && !empty($table_ids[$data['postid']])) { unset($table_ids[$data['postid']]); } if (in_array($data['id'], $table_ids)) { GW_GoPricing_AdminNotices::add('main', 'error', sprintf(__('Pricing table with an ID of "%s" already exists! It must be uniqe.', 'go_pricing_textdomain'), $data['id']), false); } } $notices = GW_GoPricing_AdminNotices::get('main', 'error'); if (isset($data['col-data'])) { $col_data = GW_GoPricing_Helper::clean_input($data['col-data'], 'html'); } $data = GW_GoPricing_Helper::clean_input($data); if (isset($col_data)) { $data['col-data'] = $col_data; } if (empty($notices)) { $data = GW_GoPricing_Helper::remove_input($data, array($_POST['action'])); $postid = GW_GoPricing_Data::update_table($data); if ($postid !== false) { // Create table message if (empty($data['postid'])) { GW_GoPricing_AdminNotices::add('main', 'success', sprintf(__('Pricing table has been successfully created!<br>Shortcode: </strong>[go_pricing id="%s"]<strong>', 'go_pricing_textdomain'), $data['id'])); } else { GW_GoPricing_AdminNotices::add('main', 'success', sprintf(__('Pricing table has been successfully updated!<br>Shortcode: </strong>[go_pricing id="%s"]<strong>', 'go_pricing_textdomain'), $data['id'])); } return $postid; } else { GW_GoPricing_AdminNotices::add('main', 'error', __('Oops, something went wrong!', 'go_pricing_textdomain')); } } else { $this->set_temp_postdata($data); } return false; }
/** * Ajax action router (handle callbacks) * * @return void */ public static function ajax_action_router() { if (empty($_POST['action']) || empty($_POST['_action'])) { GW_GoPricing_AdminNotices::add('ajax', 'error', __('No AJAX action has been found!', 'go_pricing_textdomain')); GW_GoPricing_AdminNotices::show(); exit; } $actions = get_option(self::$plugin_prefix . '_ajax_actions'); if (empty($actions['data']) || empty($actions['token']) || sha1(@serialize($actions['data'])) != $actions['token']) { GW_GoPricing_AdminNotices::add('ajax', 'error', __('Invalid AJAX callback has been detected! Please, refresh the page!', 'go_pricing_textdomain')); GW_GoPricing_AdminNotices::show(); exit; } if (!empty($actions['data'][$_POST['_action']])) { call_user_func($actions['data'][$_POST['_action']]); } else { GW_GoPricing_AdminNotices::add('ajax', 'error', __('No AJAX callback has been registered for this action! Please, refresh the page!', 'go_pricing_textdomain')); GW_GoPricing_AdminNotices::show(); exit; } exit; }
/** * Validate & return import data * * @return string | bool */ public function validate_import_data($import_data) { if (empty($import_data)) { GW_GoPricing_AdminNotices::add('impex', 'error', __('There is nothing to import!', 'go_pricing_textdomain')); return false; } $result = @unserialize(base64_decode($import_data)); if ($result === false) { GW_GoPricing_AdminNotices::add('impex', 'error', __('Invalid import data!', 'go_pricing_textdomain')); return false; } if (empty($result['_info']['db_version']) || version_compare($result['_info']['db_version'], self::$db_version, "<")) { GW_GoPricing_AdminNotices::add('impex', 'error', __('Import data is not compatible with the current version!', 'go_pricing_textdomain')); // return false; } unset($result['_info']); return $result; }
/** * Import * * @return bool */ public function import($file, $override, $ids) { $file_content = @file_get_contents($file); if ($file_content === false) { GW_GoPricing_AdminNotices::add('impex', 'error', __('Invalid import data!', 'go_pricing_textdomain')); return false; } $data = @unserialize(base64_decode($file_content)); if ($data === false) { GW_GoPricing_AdminNotices::add('impex', 'error', __('Invalid import data!', 'go_pricing_textdomain')); unlink($file['file']); return false; } if (empty($data['_info']['db_version']) || version_compare($data['_info']['db_version'], self::$db_version, "<")) { GW_GoPricing_AdminNotices::add('impex', 'error', __('Import data is not compatible with the current version!', 'go_pricing_textdomain')); unlink($file['file']); return false; } $data = $file_content; $ids = isset($ids[0]) && $ids[0] == 'all' ? array() : $ids; $data = GW_GoPricing_Helper::clean_input($data); $result = GW_GoPricing_Data::import($data, (bool) $override, $ids); if ($result === false) { GW_GoPricing_AdminNotices::add('main', 'error', __('Oops, something went wrong!', 'go_pricing_textdomain')); unlink($file['file']); return false; } else { GW_GoPricing_AdminNotices::add('main', 'success', sprintf(__('%1$s pricing table(s) has been successfully imported.', 'go_pricing_textdomain'), $result)); } delete_transient(self::$plugin_prefix . '_uploads'); return true; }