Skip to content

Commit ce7ccb0

Browse files
committed
Performance improvements
1 parent f91ae6d commit ce7ccb0

File tree

3 files changed

+31
-46
lines changed

3 files changed

+31
-46
lines changed

admin/class-bread-admin.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ function pwsix_process_settings_import()
335335
$settings['authors'] = array(wp_get_current_user()->ID);
336336
$this->bread->setOptions($settings);
337337
update_option($this->bread->getOptionsName(), $this->bread->getOptions());
338-
setcookie('pwsix_action', "import_settings", time() + 10);
339338
setcookie('current-meeting-list', $this->bread->getRequestedSetting(), time() + 10);
339+
setcookie('bread_import_file', $import_file, time() + 10);
340340
wp_safe_redirect(admin_url('?page=class-bread-admin.php'));
341341
}
342342
function my_theme_add_editor_styles()
@@ -442,6 +442,9 @@ function admin_options_page()
442442
break;
443443
}
444444
}
445+
if (empty($this->bread->getOptions())) {
446+
$this->bread->getConfigurationForSettingId($this->bread->getRequestedSetting());
447+
}
445448
include_once plugin_dir_path(__FILE__) . 'partials/bread-admin-display.php';
446449
(new Bread_AdminDisplay($this))->admin_options_page();
447450
}
@@ -460,7 +463,8 @@ function pwsix_process_wizard()
460463
}
461464
$settings = json_decode($encode_options, true);
462465
$ncols = substr_count($settings['meeting_template_content'], '<td');
463-
$id = $this->bread->getInitialSetting() ? 1 : ((is_numeric($_POST['wizard_setting_id'])) ? intval($_POST['wizard_setting_id']) : $this->bread->getMaxSetting() + 1);
466+
$id = $this->bread->loadAllSettings([]);
467+
$id = $this->bread->isInitialSetting() ? 1 : ((is_numeric($_POST['wizard_setting_id'])) ? intval($_POST['wizard_setting_id']) : $this->bread->getMaxSetting() + 1);
464468
$optionsName = $this->bread->generateOptionName($id);
465469
$settings['page_size'] = $layoutInfos[1];
466470
$settings['authors'] = array();
@@ -504,7 +508,6 @@ function pwsix_process_wizard()
504508
$setting_name = $setting_name == '' ? 'Setting ' . $id : $setting_name;
505509
$this->bread->setAndSaveSetting($id, $setting_name);
506510
$this->bread->getConfigurationForSettingId($id);
507-
$this->bread->setRequestedSetting($id);
508511
ignore_user_abort(true);
509512
ob_clean();
510513
header('Content-Type: application/json; charset=utf-8');
@@ -529,7 +532,6 @@ function pwsix_process_settings_admin()
529532
}
530533
$this->bread->deleteSetting($this->bread->getRequestedSetting());
531534
$this->bread->getConfigurationForSettingId(1);
532-
$this->bread->setRequestedSetting(1);
533535
} elseif (isset($_POST['duplicate'])) {
534536
if (!$this->current_user_can_create()) {
535537
return;
@@ -544,17 +546,14 @@ function pwsix_process_settings_admin()
544546
}
545547
function process_customize_form()
546548
{
547-
$this->bread->getConfigurationForSettingId($this->bread->getRequestedSetting());
548-
if (!isset($_POST['bmltmeetinglistsave'])) {
549-
$_POST['bmltmeetinglistsave'] = false;
550-
}
551-
if (!isset($_POST['bmltmeetinglistpreview'])) {
552-
$_POST['bmltmeetinglistpreview'] = false;
553-
}
549+
$_POST['bmltmeetinglistsave'] = isset($_POST['bmltmeetinglistsave']);
550+
$_POST['bmltmeetinglistpreview'] = isset($_POST['bmltmeetinglistpreview']);
551+
554552
if ($_POST['bmltmeetinglistsave'] || $_POST['bmltmeetinglistpreview']) {
555553
if (!wp_verify_nonce($_POST['_wpnonce'], 'bmltmeetinglistupdate-options')) {
556554
die('Whoops! There was a problem with the data you posted. Please go back and try again.');
557555
}
556+
$this->bread->getConfigurationForSettingId($this->bread->getRequestedSetting());
558557
$this->bread->setOption('bread_version', sanitize_text_field($_POST['bread_version']));
559558
$this->bread->setOption('front_page_content', wp_kses_post($_POST['front_page_content']));
560559
$this->bread->setOption('front_page_line_height', $_POST['front_page_line_height']);
@@ -691,12 +690,6 @@ function process_customize_form()
691690
wp_redirect(home_url() . "?preview-meeting-list=1");
692691
exit();
693692
}
694-
} elseif (isset($_REQUEST['pwsix_action']) && $_REQUEST['pwsix_action'] == "import_settings") {
695-
echo '<div class="updated"><p style="color: #F00;">Your file was successfully imported!</p></div>';
696-
$num = delete_transient($this->bread->get_TransientKey($this->bread->getRequestedSetting()));
697-
} elseif (isset($_REQUEST['pwsix_action']) && $_REQUEST['pwsix_action'] == "default_settings_success") {
698-
echo '<div class="updated"><p style="color: #F00;">Your default settings were successfully updated!</p></div>';
699-
$num = delete_transient($this->bread->get_TransientKey($this->bread->getRequestedSetting()));
700693
}
701694
}
702695
}

admin/partials/bread-admin-display.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ function admin_options_page()
9090
set_transient('admin_notice', 'Please put down your weapon. You have 20 seconds to comply.');
9191
if (!$this->admin->current_user_can_modify()) {
9292
echo '<div class="updated"><p style="color: #F00;">You do not have permission to save this configuation!</p>';
93+
} elseif (isset($_COOKIE['bread_import_file'])) {
94+
echo '<div class="updated"><p style="color: #F00;">File loaded.</p>';
95+
delete_transient($this->bread->get_TransientKey($this->bread->getRequestedSetting()));
9396
} else {
9497
$this->admin->save_admin_options();
9598
echo '<div class="updated"><p style="color: #F00;">Your changes were successfully saved!</p>';
@@ -99,7 +102,7 @@ function admin_options_page()
99102
}
100103
}
101104
echo '</div>';
102-
105+
103106
$this->bread->fillUnsetOptions();
104107
?>
105108
<div class="hide wrap" id="meeting-list-tabs-wrapper">

includes/class-bread.php

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Bread
5959
*
6060
* @var integer
6161
*/
62-
private int $requested_setting = 1;
62+
private int $requested_setting = 0;
6363
private $protocol;
6464
private string $tmp_dir;
6565
/**
@@ -74,7 +74,6 @@ class Bread
7474
* @var array
7575
*/
7676
private array $translate = array();
77-
private bool $generating_meeting_list = false;
7877
private bool $exporting_meeting_list = false;
7978
/**
8079
* The wizard wants to know if we are generating the first meeting list for this site.
@@ -176,7 +175,7 @@ private static function rrmdir($dir)
176175
@rmdir($dir);
177176
}
178177
}
179-
private function loadAllSettings($holder): int
178+
public function loadAllSettings($holder): int
180179
{
181180
if (isset($holder['bread_preview_settings'])) {
182181
$this->allSettings = array();
@@ -188,6 +187,7 @@ private function loadAllSettings($holder): int
188187
$this->allSettings = array();
189188
$this->allSettings[1] = "Default Setting";
190189
$this->maxSetting = 1;
190+
$this->initial_setting = true;
191191
} else {
192192
foreach ($this->allSettings as $key => $value) {
193193
if ($key > $this->maxSetting) {
@@ -241,29 +241,26 @@ private function getCurrentMeetingListHolder(): array
241241
$ret['current-meeting-list'] = $_REQUEST['current-meeting-list'];
242242
} elseif (isset($_REQUEST['export-meeting-list'])) {
243243
$ret['current-meeting-list'] = $_REQUEST['export-meeting-list'];
244-
$this->exporting_meeting_list = true;
245244
} elseif (isset($_COOKIE['current-meeting-list'])) {
246245
$ret['current-meeting-list'] = $_COOKIE['current-meeting-list'];
247246
}
248-
$this->generating_meeting_list = !empty($ret) && !is_admin() && !$this->exporting_meeting_list;
249247
return $ret;
250248
}
251-
/**
252-
* Undocumented function
253-
*
254-
* @return void
255-
*/
256249
public function generatingMeetingList(): bool
257250
{
258-
return $this->generating_meeting_list;
251+
return (isset($_REQUEST['current-meeting-list']) || isset($_REQUEST['preview-meeting-list'])) && !is_admin();
259252
}
260253
public function exportingMeetingList(): bool
261254
{
262-
return $this->exporting_meeting_list;
255+
return isset($_REQUEST['export-meeting-list']) && !is_admin();
263256
}
264257
public function generateOptionName($current_setting)
265258
{
266-
return Bread::OPTIONS_NAME . '_' . $current_setting;
259+
if ($current_setting != 1) {
260+
return Bread::OPTIONS_NAME . '_' . $current_setting;
261+
} else {
262+
return Bread::OPTIONS_NAME;
263+
}
267264
}
268265
/**
269266
* Retrieves the plugin options from the database.
@@ -278,19 +275,14 @@ public function &getConfigurationForSettingId($current_setting)
278275
if (is_array($this->allSettings[$current_setting])) {
279276
$this->options = $this->allSettings[$current_setting];
280277
} else {
281-
if ($current_setting != 1) {
282-
$this->optionsName = $this->generateOptionName($current_setting);
283-
} else {
284-
$this->optionsName = Bread::OPTIONS_NAME;
285-
}
278+
$this->optionsName = $this->generateOptionName($current_setting);
286279
//Don't forget to set up the default options
287280
if (!$theOptions = get_option($this->optionsName)) {
288281
if ($current_setting != 1) {
289282
unset($this->allSettings[$current_setting]);
290283
update_option(Bread::SETTINGS, $this->allSettings);
291284
die('Undefined setting: ' . $current_setting);
292285
}
293-
$this->initial_setting = true;
294286
$import_file = plugin_dir_path(__FILE__) . "../admin/templates/30/trifold-landscape-largefont.json";
295287
$encode_options = file_get_contents($import_file);
296288
$theOptions = json_decode($encode_options, true);
@@ -322,12 +314,12 @@ public function setOptionsName($name)
322314
}
323315
public function getRequestedSetting()
324316
{
317+
if ($this->requested_setting == 0) {
318+
$holder = $this->getCurrentMeetingListHolder();
319+
$this->requested_setting = $this->loadAllSettings($holder);
320+
}
325321
return $this->requested_setting;
326322
}
327-
public function setRequestedSetting($id)
328-
{
329-
$this->requested_setting = $id;
330-
}
331323
/**
332324
* Define the core functionality of the plugin.
333325
*
@@ -347,9 +339,6 @@ public function __construct()
347339
$this->plugin_name = 'bread';
348340
$this->tmp_dir = $this->setup_temp_dir();
349341
$this->protocol = (strpos(strtolower(home_url()), "https") !== false ? "https" : "http") . "://";
350-
351-
$holder = $this->getCurrentMeetingListHolder();
352-
$this->requested_setting = $this->loadAllSettings($holder);
353342
$this->bread_bmlt = new Bread_Bmlt($this);
354343

355344
$this->load_dependencies();
@@ -383,7 +372,7 @@ public function getProtocol()
383372
*
384373
* @return bool true when no setting existed previously, ie, if we should create setting 1.
385374
*/
386-
public function getInitialSetting()
375+
public function isInitialSetting()
387376
{
388377
return $this->initial_setting;
389378
}
@@ -775,7 +764,7 @@ private function renamed_option(string $old, string $new)
775764
*/
776765
public function updateOptions()
777766
{
778-
update_option(Bread::getOptionsName(), $this->options);
767+
update_option($this->getOptionsName(), $this->options);
779768
}
780769
public static function get_TransientKey($setting): string
781770
{

0 commit comments

Comments
 (0)