Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
otrok7 committed Dec 15, 2024
1 parent f91ae6d commit ce7ccb0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
27 changes: 10 additions & 17 deletions admin/class-bread-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ function pwsix_process_settings_import()
$settings['authors'] = array(wp_get_current_user()->ID);
$this->bread->setOptions($settings);
update_option($this->bread->getOptionsName(), $this->bread->getOptions());
setcookie('pwsix_action', "import_settings", time() + 10);
setcookie('current-meeting-list', $this->bread->getRequestedSetting(), time() + 10);
setcookie('bread_import_file', $import_file, time() + 10);
wp_safe_redirect(admin_url('?page=class-bread-admin.php'));
}
function my_theme_add_editor_styles()
Expand Down Expand Up @@ -442,6 +442,9 @@ function admin_options_page()
break;
}
}
if (empty($this->bread->getOptions())) {
$this->bread->getConfigurationForSettingId($this->bread->getRequestedSetting());
}
include_once plugin_dir_path(__FILE__) . 'partials/bread-admin-display.php';
(new Bread_AdminDisplay($this))->admin_options_page();
}
Expand All @@ -460,7 +463,8 @@ function pwsix_process_wizard()
}
$settings = json_decode($encode_options, true);
$ncols = substr_count($settings['meeting_template_content'], '<td');
$id = $this->bread->getInitialSetting() ? 1 : ((is_numeric($_POST['wizard_setting_id'])) ? intval($_POST['wizard_setting_id']) : $this->bread->getMaxSetting() + 1);
$id = $this->bread->loadAllSettings([]);
$id = $this->bread->isInitialSetting() ? 1 : ((is_numeric($_POST['wizard_setting_id'])) ? intval($_POST['wizard_setting_id']) : $this->bread->getMaxSetting() + 1);
$optionsName = $this->bread->generateOptionName($id);
$settings['page_size'] = $layoutInfos[1];
$settings['authors'] = array();
Expand Down Expand Up @@ -504,7 +508,6 @@ function pwsix_process_wizard()
$setting_name = $setting_name == '' ? 'Setting ' . $id : $setting_name;
$this->bread->setAndSaveSetting($id, $setting_name);
$this->bread->getConfigurationForSettingId($id);
$this->bread->setRequestedSetting($id);
ignore_user_abort(true);
ob_clean();
header('Content-Type: application/json; charset=utf-8');
Expand All @@ -529,7 +532,6 @@ function pwsix_process_settings_admin()
}
$this->bread->deleteSetting($this->bread->getRequestedSetting());
$this->bread->getConfigurationForSettingId(1);
$this->bread->setRequestedSetting(1);
} elseif (isset($_POST['duplicate'])) {
if (!$this->current_user_can_create()) {
return;
Expand All @@ -544,17 +546,14 @@ function pwsix_process_settings_admin()
}
function process_customize_form()
{
$this->bread->getConfigurationForSettingId($this->bread->getRequestedSetting());
if (!isset($_POST['bmltmeetinglistsave'])) {
$_POST['bmltmeetinglistsave'] = false;
}
if (!isset($_POST['bmltmeetinglistpreview'])) {
$_POST['bmltmeetinglistpreview'] = false;
}
$_POST['bmltmeetinglistsave'] = isset($_POST['bmltmeetinglistsave']);
$_POST['bmltmeetinglistpreview'] = isset($_POST['bmltmeetinglistpreview']);

if ($_POST['bmltmeetinglistsave'] || $_POST['bmltmeetinglistpreview']) {
if (!wp_verify_nonce($_POST['_wpnonce'], 'bmltmeetinglistupdate-options')) {
die('Whoops! There was a problem with the data you posted. Please go back and try again.');
}
$this->bread->getConfigurationForSettingId($this->bread->getRequestedSetting());
$this->bread->setOption('bread_version', sanitize_text_field($_POST['bread_version']));
$this->bread->setOption('front_page_content', wp_kses_post($_POST['front_page_content']));
$this->bread->setOption('front_page_line_height', $_POST['front_page_line_height']);
Expand Down Expand Up @@ -691,12 +690,6 @@ function process_customize_form()
wp_redirect(home_url() . "?preview-meeting-list=1");
exit();
}
} elseif (isset($_REQUEST['pwsix_action']) && $_REQUEST['pwsix_action'] == "import_settings") {
echo '<div class="updated"><p style="color: #F00;">Your file was successfully imported!</p></div>';
$num = delete_transient($this->bread->get_TransientKey($this->bread->getRequestedSetting()));
} elseif (isset($_REQUEST['pwsix_action']) && $_REQUEST['pwsix_action'] == "default_settings_success") {
echo '<div class="updated"><p style="color: #F00;">Your default settings were successfully updated!</p></div>';
$num = delete_transient($this->bread->get_TransientKey($this->bread->getRequestedSetting()));
}
}
}
5 changes: 4 additions & 1 deletion admin/partials/bread-admin-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ function admin_options_page()
set_transient('admin_notice', 'Please put down your weapon. You have 20 seconds to comply.');
if (!$this->admin->current_user_can_modify()) {
echo '<div class="updated"><p style="color: #F00;">You do not have permission to save this configuation!</p>';
} elseif (isset($_COOKIE['bread_import_file'])) {
echo '<div class="updated"><p style="color: #F00;">File loaded.</p>';
delete_transient($this->bread->get_TransientKey($this->bread->getRequestedSetting()));
} else {
$this->admin->save_admin_options();
echo '<div class="updated"><p style="color: #F00;">Your changes were successfully saved!</p>';
Expand All @@ -99,7 +102,7 @@ function admin_options_page()
}
}
echo '</div>';

$this->bread->fillUnsetOptions();
?>
<div class="hide wrap" id="meeting-list-tabs-wrapper">
Expand Down
45 changes: 17 additions & 28 deletions includes/class-bread.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Bread
*
* @var integer
*/
private int $requested_setting = 1;
private int $requested_setting = 0;
private $protocol;
private string $tmp_dir;
/**
Expand All @@ -74,7 +74,6 @@ class Bread
* @var array
*/
private array $translate = array();
private bool $generating_meeting_list = false;
private bool $exporting_meeting_list = false;
/**
* The wizard wants to know if we are generating the first meeting list for this site.
Expand Down Expand Up @@ -176,7 +175,7 @@ private static function rrmdir($dir)
@rmdir($dir);
}
}
private function loadAllSettings($holder): int
public function loadAllSettings($holder): int
{
if (isset($holder['bread_preview_settings'])) {
$this->allSettings = array();
Expand All @@ -188,6 +187,7 @@ private function loadAllSettings($holder): int
$this->allSettings = array();
$this->allSettings[1] = "Default Setting";
$this->maxSetting = 1;
$this->initial_setting = true;
} else {
foreach ($this->allSettings as $key => $value) {
if ($key > $this->maxSetting) {
Expand Down Expand Up @@ -241,29 +241,26 @@ private function getCurrentMeetingListHolder(): array
$ret['current-meeting-list'] = $_REQUEST['current-meeting-list'];
} elseif (isset($_REQUEST['export-meeting-list'])) {
$ret['current-meeting-list'] = $_REQUEST['export-meeting-list'];
$this->exporting_meeting_list = true;
} elseif (isset($_COOKIE['current-meeting-list'])) {
$ret['current-meeting-list'] = $_COOKIE['current-meeting-list'];
}
$this->generating_meeting_list = !empty($ret) && !is_admin() && !$this->exporting_meeting_list;
return $ret;
}
/**
* Undocumented function
*
* @return void
*/
public function generatingMeetingList(): bool
{
return $this->generating_meeting_list;
return (isset($_REQUEST['current-meeting-list']) || isset($_REQUEST['preview-meeting-list'])) && !is_admin();
}
public function exportingMeetingList(): bool
{
return $this->exporting_meeting_list;
return isset($_REQUEST['export-meeting-list']) && !is_admin();
}
public function generateOptionName($current_setting)
{
return Bread::OPTIONS_NAME . '_' . $current_setting;
if ($current_setting != 1) {
return Bread::OPTIONS_NAME . '_' . $current_setting;
} else {
return Bread::OPTIONS_NAME;
}
}
/**
* Retrieves the plugin options from the database.
Expand All @@ -278,19 +275,14 @@ public function &getConfigurationForSettingId($current_setting)
if (is_array($this->allSettings[$current_setting])) {
$this->options = $this->allSettings[$current_setting];
} else {
if ($current_setting != 1) {
$this->optionsName = $this->generateOptionName($current_setting);
} else {
$this->optionsName = Bread::OPTIONS_NAME;
}
$this->optionsName = $this->generateOptionName($current_setting);
//Don't forget to set up the default options
if (!$theOptions = get_option($this->optionsName)) {
if ($current_setting != 1) {
unset($this->allSettings[$current_setting]);
update_option(Bread::SETTINGS, $this->allSettings);
die('Undefined setting: ' . $current_setting);
}
$this->initial_setting = true;
$import_file = plugin_dir_path(__FILE__) . "../admin/templates/30/trifold-landscape-largefont.json";
$encode_options = file_get_contents($import_file);
$theOptions = json_decode($encode_options, true);
Expand Down Expand Up @@ -322,12 +314,12 @@ public function setOptionsName($name)
}
public function getRequestedSetting()
{
if ($this->requested_setting == 0) {
$holder = $this->getCurrentMeetingListHolder();
$this->requested_setting = $this->loadAllSettings($holder);
}
return $this->requested_setting;
}
public function setRequestedSetting($id)
{
$this->requested_setting = $id;
}
/**
* Define the core functionality of the plugin.
*
Expand All @@ -347,9 +339,6 @@ public function __construct()
$this->plugin_name = 'bread';
$this->tmp_dir = $this->setup_temp_dir();
$this->protocol = (strpos(strtolower(home_url()), "https") !== false ? "https" : "http") . "://";

$holder = $this->getCurrentMeetingListHolder();
$this->requested_setting = $this->loadAllSettings($holder);
$this->bread_bmlt = new Bread_Bmlt($this);

$this->load_dependencies();
Expand Down Expand Up @@ -383,7 +372,7 @@ public function getProtocol()
*
* @return bool true when no setting existed previously, ie, if we should create setting 1.
*/
public function getInitialSetting()
public function isInitialSetting()
{
return $this->initial_setting;
}
Expand Down Expand Up @@ -775,7 +764,7 @@ private function renamed_option(string $old, string $new)
*/
public function updateOptions()
{
update_option(Bread::getOptionsName(), $this->options);
update_option($this->getOptionsName(), $this->options);
}
public static function get_TransientKey($setting): string
{
Expand Down

0 comments on commit ce7ccb0

Please sign in to comment.