/**
  * Save "Custom CSS" to a file, or return HTML for the credentials form
  *
  * @since 1.0.0
  *
  * @return string (if necessary) HTML for the credentials form for the WP_Filesystem API
  */
 public function save_custom_css_to_file()
 {
     // Set current screen to get Screen Icon to have a custom HTML ID, so that we can hide it with CSS
     set_current_screen('tablepress_options_invisible');
     // Start capturing the output, to get HTML of the credentials form (if needed)
     ob_start();
     $url = '';
     // same page
     $credentials = request_filesystem_credentials($url, '', false, false, null);
     // do we have credentials already? (Otherwise the form will have been rendered already.)
     if (false === $credentials) {
         $form_data = ob_get_contents();
         ob_end_clean();
         $form_data = str_replace('name="upgrade" id="upgrade" class="button"', 'name="upgrade" id="upgrade" class="button button-primary button-large"', $form_data);
         return $form_data;
     }
     // we have received credentials, but don't know if they are valid yet
     if (!WP_Filesystem($credentials)) {
         // credentials failed, so ask again (with $error flag true)
         request_filesystem_credentials($url, '', true, false, null);
         $form_data = ob_get_contents();
         ob_end_clean();
         $form_data = str_replace('name="upgrade" id="upgrade" class="button"', 'name="upgrade" id="upgrade" class="button button-primary button-large"', $form_data);
         return $form_data;
     }
     // we have valid access to the filesystem now -> try to save the file
     $filename = WP_CONTENT_DIR . '/tablepress-custom.css';
     $filename = apply_filters('tablepress_custom_css_file_name', $filename);
     $filename_min = WP_CONTENT_DIR . '/tablepress-custom.min.css';
     $filename_min = apply_filters('tablepress_custom_css_minified_file_name', $filename_min);
     // Check if file name is valid (0 means yes)
     if (0 !== validate_file($filename) || 0 !== validate_file($filename_min)) {
         TablePress::redirect(array('action' => 'options', 'message' => 'success_save_error_custom_css'));
     }
     global $wp_filesystem;
     // WP_CONTENT_DIR and (FTP-)Content-Dir can be different (e.g. if FTP working dir is /)
     // We need to account for that by replacing the path difference in the filename
     $path_difference = str_replace($wp_filesystem->wp_content_dir(), '', trailingslashit(WP_CONTENT_DIR));
     if ('' != $path_difference) {
         $filename = str_replace($path_difference, '', $filename);
         $filename_min = str_replace($path_difference, '', $filename_min);
     }
     $custom_css = $this->get('custom_css');
     $custom_css_minified = $this->get('custom_css_minified');
     $result = $wp_filesystem->put_contents($filename, $custom_css, FS_CHMOD_FILE);
     $result_min = $wp_filesystem->put_contents($filename_min, $custom_css_minified, FS_CHMOD_FILE);
     if (!$result || !$result_min) {
         TablePress::redirect(array('action' => 'options', 'message' => 'success_save_error_custom_css'));
     }
     // at this point, saving was successful, so enable the checkbox again
     // (if it was not enabled before, we would never have tried to save)
     // and also increase the "Custom CSS" version number (for cache busting)
     $this->update(array('use_custom_css_file' => true, 'custom_css_version' => $this->get('custom_css_version') + 1));
     TablePress::redirect(array('action' => 'options', 'message' => 'success_save'));
 }
 /**
  * Copy a table.
  *
  * @since 1.0.0
  */
 public function handle_get_action_copy_table()
 {
     $table_id = !empty($_GET['item']) ? $_GET['item'] : false;
     TablePress::check_nonce('copy_table', $table_id);
     $return = !empty($_GET['return']) ? $_GET['return'] : 'list';
     $return_item = !empty($_GET['return_item']) ? $_GET['return_item'] : false;
     // Nonce check should actually catch this already.
     if (false === $table_id) {
         TablePress::redirect(array('action' => $return, 'message' => 'error_copy', 'table_id' => $return_item));
     }
     if (!current_user_can('tablepress_copy_table', $table_id)) {
         wp_die(__('You do not have sufficient permissions to access this page.', 'default'), 403);
     }
     $this->init_i18n_support();
     // for the translation of "Copy of".
     $copy_table_id = TablePress::$model_table->copy($table_id);
     if (is_wp_error($copy_table_id)) {
         TablePress::redirect(array('action' => $return, 'message' => 'error_copy', 'table_id' => $return_item));
     } else {
         $return_item = $copy_table_id;
     }
     /*
      * Slightly more complex redirect method, to account for sort, search, and pagination in the WP_List_Table on the List View,
      * but only if this action succeeds, to have everything fresh in the event of an error.
      */
     $sendback = wp_get_referer();
     if (!$sendback) {
         $sendback = TablePress::url(array('action' => $return, 'message' => 'success_copy', 'table_id' => $return_item));
     } else {
         $sendback = remove_query_arg(array('action', 'message', 'table_id'), $sendback);
         $sendback = add_query_arg(array('action' => $return, 'message' => 'success_copy', 'table_id' => $return_item), $sendback);
     }
     wp_redirect($sendback);
     exit;
 }
 /**
  * Save changes on the Stockflock screen.
  *
  */
 public function handle_post_action_stockflock()
 {
     if (!isset($_POST['stockflock'])) {
         return;
     }
     TablePress::check_nonce('stockflock');
     if (!current_user_can('manage_options')) {
         wp_die(__('You do not have sufficient permissions to access this page.'));
     }
     if (empty($_POST['stockflock']) || !is_array($_POST['stockflock'])) {
         TablePress::redirect(array('action' => 'stockflock', 'message' => 'error_save'));
     } else {
         $stockflock = stripslashes_deep($_POST['stockflock']);
     }
     $params = array('option_name' => 'tablepress_stockflock_config', 'default_value' => array());
     $stockflock_config = TablePress::load_class('TablePress_WP_Option', 'class-wp_option.php', 'classes', $params);
     //trim and restrict array length to be not more than 20
     $raw_companies = explode(',', $stockflock['companies']);
     $valid_companies = array_slice(array_map('trim', $raw_companies), 0, $this->company_limit);
     $stockflock['companies'] = strtoupper(implode(',', $valid_companies));
     //give default value to data_points
     $data_point_list = ['short_name', 'price_value', 'industry_name', 'dividend_yield', 'earning_value', 'dividend_per_share', 'book_value', 'debt_assets', 'net_asset_value', 'gearing_ratio', 'property_yield_ratio'];
     if (is_array($stockflock['data_points'])) {
         foreach ($data_point_list as $key => $value) {
             if (!array_key_exists($value, $stockflock['data_points'])) {
                 $stockflock['data_points'][$value] = "false";
             }
         }
     }
     //store to wp_options
     $stockflock_config->update($stockflock);
     TablePress::redirect(array('action' => 'stockflock', 'message' => 'success_save'));
 }