Skip to content

Commit

Permalink
no more singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
otrok7 committed Nov 20, 2024
1 parent 9ef916c commit ed32414
Show file tree
Hide file tree
Showing 23 changed files with 802 additions and 762 deletions.
97 changes: 30 additions & 67 deletions admin/class-bread-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ class Bread_Admin
var $maxSetting = 1;
var $loaded_setting = 1;
var $bmltEnabled_admin;
public function __construct($plugin_name, $version, $bmltEnabled_admin)
private Bread $bread;
public function __construct($plugin_name, $version, $bmltEnabled_admin, $bread)
{
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->bmltEnabled_admin = $bmltEnabled_admin;
$this->bread = $bread;
}
public function get_bread_instance(): Bread
{
return $this->bread;
}

/**
* Register the stylesheets for the admin area.
*
Expand Down Expand Up @@ -168,14 +173,14 @@ function is_root_server_missing()
global $my_admin_page;
$screen = get_current_screen();
if ($screen->id == $my_admin_page) {
$root_server = Bread::getOption('root_server');
$root_server = $this->bread->getOption('root_server');
if ($root_server == '') {
echo '<div id="message" class="error"><p>Missing BMLT Server in settings for bread.</p>';
$url = admin_url('options-general.php?page=class-bread-admin.php');
echo "<p><a href='$url'>Settings</a></p>";
echo '</div>';
} else if (!Bread::temp_dir()) {
echo '<div id="message" class="error"><p>' . Bread::temp_dir() . ' temporary directory is not writable.</p>';
} else if (!$this->bread->temp_dir()) {
echo '<div id="message" class="error"><p>' . $this->bread->temp_dir() . ' temporary directory is not writable.</p>';
$url = admin_url('options-general.php?page=class-bread-admin.php');
echo "<p><a href='$url'>Settings</a></p>";
echo '</div>';
Expand All @@ -185,7 +190,6 @@ function is_root_server_missing()

function pwsix_process_rename_settings()
{
Bread::getMLOptions(Bread::getRequestedSetting());
if (isset($_POST['bmltmeetinglistsave']) && $_POST['bmltmeetinglistsave'] == 'Save Changes') {
return;
}
Expand All @@ -198,15 +202,14 @@ function pwsix_process_rename_settings()
if (! $this->current_user_can_modify()) {
return;
}

Bread::renameSetting($this->loaded_setting, sanitize_text_field($_POST['setting_descr']));
$this->bread->getMLOptions($this->bread->getRequestedSetting());
$this->bread->renameSetting($this->loaded_setting, sanitize_text_field($_POST['setting_descr']));
}
/**
* Process a settings export that generates a .json file of the shop settings
*/
function pwsix_process_settings_export()
{
Bread::getMLOptions(Bread::getRequestedSetting());
if (isset($_POST['bmltmeetinglistsave']) && $_POST['bmltmeetinglistsave'] == 'Save Changes') {
return;
}
Expand All @@ -219,13 +222,13 @@ function pwsix_process_settings_export()
if (! current_user_can('manage_bread')) { // TODO: Is this necessary? Why not let the user make a copy
return;
}

$blogname = str_replace(" - ", " ", get_option('blogname').'-'.Bread::getSettingName($this->loaded_setting));
$this->bread->getMLOptions($this->bread->getRequestedSetting());
$blogname = str_replace(" - ", " ", get_option('blogname').'-'.$this->bread->getSettingName($this->loaded_setting));
$blogname = str_replace(" ", "-", $blogname);
$date = date("m-d-Y");
$blogname = trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($blogname)), '-');
$json_name = $blogname.$date.".json"; // Naming the filename will be generated.
$settings = get_option(Bread::getOptionsName());
$settings = get_option($this->bread->getOptionsName());
foreach ($settings as $key => $value) {
$value = maybe_unserialize($value);
$need_options[$key] = $value;
Expand All @@ -247,7 +250,7 @@ function current_user_can_modify()
if (in_array('administrator', $user->roles)) {
return true;
}
$authors_safe = Bread::getOption('authors');
$authors_safe = $this->bread->getOption('authors');
if (!is_array($authors_safe) || empty($authors_safe)) {
return true;
}
Expand All @@ -268,7 +271,6 @@ function current_user_can_create()
*/
function pwsix_process_settings_import()
{
Bread::getMLOptions(Bread::getRequestedSetting());
if (isset($_POST['bmltmeetinglistsave']) && $_POST['bmltmeetinglistsave'] == 'Save Changes') {
return;
}
Expand All @@ -281,6 +283,7 @@ function pwsix_process_settings_import()
if (! current_user_can('manage_bread')) {
return;
}
$this->bread->getMLOptions($this->bread->getRequestedSetting());
$file_name = $_FILES['import_file']['name'];
$tmp = explode('.', $file_name);
$extension = end($tmp);
Expand All @@ -301,51 +304,11 @@ function pwsix_process_settings_import()
}
$settings = json_decode($encode_options, true);
$settings['authors'] = array(wp_get_current_user()->ID);
update_option(Bread::getOptionsName(), $settings);
update_option($this->bread->getOptionsName(), $settings);
setcookie('pwsix_action', "import_settings", time()+10);
setcookie('current-meeting-list', $this->loaded_setting, time()+10);
wp_safe_redirect(admin_url('?page=class-bread-admin.php'));
}

/**
* Process a default settings
*/
function pwsix_process_default_settings()
{
Bread::getMLOptions(Bread::getRequestedSetting());
if (! current_user_can('manage_bread')
|| (isset($_POST['bmltmeetinglistsave']) && $_POST['bmltmeetinglistsave'] == 'Save Changes' )
) {
return;
} elseif (isset($_REQUEST['pwsix_action']) && 'three_column_default_settings' == $_REQUEST['pwsix_action']) {
if (! wp_verify_nonce($_POST['pwsix_submit_three_column'], 'pwsix_submit_three_column')) {
die('Whoops! There was a problem with the data you posted. Please go back and try again.');
}
$import_file = plugin_dir_path(__FILE__) . "includes/three_column_settings.json";
} elseif (isset($_REQUEST['pwsix_action']) && 'four_column_default_settings' == $_REQUEST['pwsix_action']) {
if (! wp_verify_nonce($_POST['pwsix_submit_four_column'], 'pwsix_submit_four_column')) {
die('Whoops! There was a problem with the data you posted. Please go back and try again.');
}
$import_file = plugin_dir_path(__FILE__) . "includes/four_column_settings.json";
} elseif (isset($_REQUEST['pwsix_action']) && 'booklet_default_settings' == $_REQUEST['pwsix_action']) {
if (! wp_verify_nonce($_POST['pwsix_submit_booklet'], 'pwsix_submit_booklet')) {
die('Whoops! There was a problem with the data you posted. Please go back and try again.');
}
$import_file = plugin_dir_path(__FILE__) . "includes/booklet_settings.json";
} else {
return;
}
if (empty($import_file)) {
wp_die(__('Error importing default settings file'));
}
$encode_options = file_get_contents($import_file);
$settings = json_decode($encode_options, true);
$settings['authors'] = Bread::getOption('authors');
update_option(Bread::getOptionsName(), $settings);
setcookie('pwsix_action', "default_settings_success", time()+10);
setcookie('current-meeting-list', $this->loaded_setting, time()+10);
wp_safe_redirect(admin_url('?page=class-bread-admin.php'));
}
function my_theme_add_editor_styles()
{
global $my_admin_page;
Expand All @@ -368,7 +331,7 @@ function filter_plugin_actions($links, $file)
*/
function save_admin_options()
{
Bread::updateOptions();
$this->bread->updateOptions();
}
public function getLatestRootVersion()
{
Expand All @@ -389,13 +352,13 @@ function get($url, $cookies = array())
'cookies' => $cookies,
);
if (isset($this->options['user_agent'])
&& Bread::getOption('user_agent') != 'None'
&& $this->bread->getOption('user_agent') != 'None'
) {
$args['headers'] = array(
'User-Agent' => Bread::getOption('user_agent')
'User-Agent' => $this->bread->getOption('user_agent')
);
}
if (Bread::getOption('sslverify') == '1') {
if ($this->bread->getOption('sslverify') == '1') {
$args['sslverify'] = false;
}
return wp_remote_get($url, $args);
Expand Down Expand Up @@ -433,7 +396,6 @@ function admin_options_page()
}
function pwsix_process_settings_admin()
{
Bread::getMLOptions(Bread::getRequestedSetting());
if (isset($_POST['bmltmeetinglistsave']) && $_POST['bmltmeetinglistsave'] == 'Save Changes') {
return;
}
Expand All @@ -443,28 +405,29 @@ function pwsix_process_settings_admin()
if (! wp_verify_nonce($_POST['pwsix_settings_admin_nonce'], 'pwsix_settings_admin_nonce')) {
return;
}
$this->bread->getMLOptions($this->bread->getRequestedSetting());
if (isset($_POST['delete'])) {
if (!$this->current_user_can_modify()) {
return;
}
if ($this->loaded_setting == 1) {
return;
}
Bread::deleteSetting($this->loaded_setting);
Bread::getMLOptions(1);
$this->bread->deleteSetting($this->loaded_setting);
$this->bread->getMLOptions(1);
$this->loaded_setting = 1;
Bread::setRequestedSetting(1);
$this->bread->setRequestedSetting(1);
} elseif (isset($_POST['duplicate'])) {
if (!$this->current_user_can_create()) {
return;
}
$id = $this->maxSetting + 1;
Bread::setOptionsName(Bread::generateOptionName($id));
Bread::setOption('authors', array());
$this->bread->setOptionsName($this->bread->generateOptionName($id));
$this->bread->setOption('authors', array());
$this->save_admin_options();
Bread::renameSetting($id, 'Setting '.$id);
$this->bread->renameSetting($id, 'Setting '.$id);
$this->maxSetting = $id;
Bread::getMLOptions($id);
$this->bread->getMLOptions($id);
}
}
}
1 change: 1 addition & 0 deletions admin/js/bmlt_meeting_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ jQuery(document).ready(
$ml('#landscape').prop("checked", true);
$ml('.booklet').show();
$ml('.single-page').hide();
$ml('#half').prop('checked') && $ml('.A6').hide();
}
function singlePageControlsShowHide() {
$ml('.booklet').hide();
Expand Down
61 changes: 32 additions & 29 deletions admin/partials/_backup_restore_setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,41 @@
<div id="postbox-container" class="postbox-container">
<div id="normal-sortables" class="meta-box-sortables ui-sortable">
<div id="exportdiv" class="postbox">
<h3 class="hndle">Configuration Manager</h3>
<h3 class="hndle">Configuration Manager</h3>
<div class="inside">
<p><?php _e('bread can support multiple meeting lists. Each meeting list has an integer ID and a text description that help the user to identify '); ?>
<?php _e('the configuration (or \'settings\') that will be used to generate the meeting list. The ID of the configuration is used in the link ');?>
<?php _e('that generates the meeting list (eg, ?current-meeting-list=2 generates the meeting list with ID 2.')?></p>
<?php _e('the configuration (or \'settings\') that will be used to generate the meeting list. The ID of the configuration is used in the link '); ?>
<?php _e('that generates the meeting list (eg, ?current-meeting-list=2 generates the meeting list with ID 2.') ?></p>
<h4>Current Meeting List</h4>
<form method="post">
<p>Meeting List ID: <?php echo $this->admin->loaded_setting?>
<input type="hidden" name="pwsix_action" value="rename_setting" /><input type="hidden" name="current-meeting-list" value="<?php echo $this->admin->loaded_setting?>" /></p>
<p>Meeting List ID: <?php echo $this->admin->loaded_setting ?>
<input type="hidden" name="pwsix_action" value="rename_setting" /><input type="hidden" name="current-meeting-list" value="<?php echo $this->admin->loaded_setting ?>" />
</p>
<?php wp_nonce_field('pwsix_rename_nonce', 'pwsix_rename_nonce'); ?>
<br>Configuration Name: <input type="text" name="setting_descr" value="<?php echo Bread::getSettingName($this->admin->loaded_setting)?>" />
<br>Configuration Name: <input type="text" name="setting_descr" value="<?php echo $this->bread->getSettingName($this->admin->loaded_setting) ?>" />
<?php submit_button(__('Save Configuration Name'), 'button-primary', 'submit_settings_name', false); ?>
</form></p>
<?php if (count(Bread::getSettingNames())>1) {?>
<h4>Configuration Selection</h4>
<form method="post">
<label for="current_setting">Select Configuration: </label>
<select class="setting_select" id="setting_select" name="current-meeting-list">
<?php foreach (Bread::getSettingNames() as $aKey => $aDescr) { ?>
<option <?php echo (($aKey==$this->admin->loaded_setting)?'selected':"") ?> value="<?php echo $aKey ?>"><?php echo $aKey.': '.$aDescr ?></option>
<?php } ?>
</select>
<?php wp_nonce_field('pwsix_load_nonce', 'pwsix_load_nonce'); ?>
<?php submit_button(__('Load Configuration'), 'button-primary', 'submit_change_settings', false); ?>
</form>
<?php }?>
<hr/>
</p>
<?php if (count($this->bread->getSettingNames()) > 1) { ?>
<h4>Configuration Selection</h4>
<form method="post">
<label for="current_setting">Select Configuration: </label>
<select class="setting_select" id="setting_select" name="current-meeting-list">
<?php foreach ($this->bread->getSettingNames() as $aKey => $aDescr) { ?>
<option <?php echo (($aKey == $this->admin->loaded_setting) ? 'selected' : "") ?> value="<?php echo $aKey ?>"><?php echo $aKey . ': ' . $aDescr ?></option>
<?php } ?>
</select>
<?php wp_nonce_field('pwsix_load_nonce', 'pwsix_load_nonce'); ?>
<?php submit_button(__('Load Configuration'), 'button-primary', 'submit_change_settings', false); ?>
</form>
<?php } ?>
<hr />
<form method="post">
<p><input type="hidden" name="pwsix_action" value="settings_admin" /><input type="hidden" name="current-meeting-list" value="<?php echo $this->admin->loaded_setting?>" /></p></p>
<p><input type="hidden" name="pwsix_action" value="settings_admin" /><input type="hidden" name="current-meeting-list" value="<?php echo $this->admin->loaded_setting ?>" /></p>
</p>
<?php wp_nonce_field('pwsix_settings_admin_nonce', 'pwsix_settings_admin_nonce'); ?>
<?php submit_button(__('Duplicate Current Configuration'), 'button-primary', 'duplicate', false); ?>
<?php if (1 < $this->admin->loaded_setting) {?>
<?php if (1 < $this->admin->loaded_setting) { ?>
<?php submit_button(__('Delete Current Configuration'), 'button-primary', 'delete_settings', false); ?>
<?php } ?>
</form>
Expand All @@ -52,7 +55,7 @@
<p><?php _e('This allows you to easily import meeting list settings into another site.'); ?></p>
<p><?php _e('Also useful for backing up before making significant changes to the meeting list settings.'); ?></p>
<form method="post">
<p><input type="hidden" name="pwsix_action" value="export_settings" /><input type="hidden" name="current-meeting-list" value="<?php echo $this->admin->loaded_setting?>" /></p>
<p><input type="hidden" name="pwsix_action" value="export_settings" /><input type="hidden" name="current-meeting-list" value="<?php echo $this->admin->loaded_setting ?>" /></p>
<p>
<?php wp_nonce_field('pwsix_export_nonce', 'pwsix_export_nonce'); ?>
<?php submit_button(__('Export'), 'button-primary', 'submit', false); ?>
Expand All @@ -65,11 +68,11 @@
<div class="inside">
<p><?php _e('Import meeting list settings from a previously exported meeting list.'); ?></p>
<form id="form_import_file" method="post" enctype="multipart/form-data">
<p><input type="file" required name="import_file"/></p>
<p><input type="file" required name="import_file" /></p>
<p>
<input type="hidden" name="pwsix_action" value="import_settings" /><input type="hidden" name="current-meeting-list" value="<?php echo $this->admin->loaded_setting?>" />
<?php wp_nonce_field('pwsix_import_nonce', 'pwsix_import_nonce'); ?>
<?php submit_button(__('Import'), 'button-primary', 'submit_import_file', false, array( 'id' => 'submit_import_file' )); ?>
<input type="hidden" name="pwsix_action" value="import_settings" /><input type="hidden" name="current-meeting-list" value="<?php echo $this->admin->loaded_setting ?>" />
<?php wp_nonce_field('pwsix_import_nonce', 'pwsix_import_nonce'); ?>
<?php submit_button(__('Import'), 'button-primary', 'submit_import_file', false, array('id' => 'submit_import_file')); ?>
</p>
<div id="basicModal">
<p style="color:#f00;">Your current meeting list settings will be replaced and lost forever.</p>
Expand All @@ -84,5 +87,5 @@
</div>
</div>
<br class="clear">
<p style="display: inline; margin-top:.5em;margin-bottom:1.0em;margin-left:.2em;"><a target="_blank" class="button-primary" href="<?php echo home_url();?>/?current-meeting-list=<?php echo $this->admin->loaded_setting;?>">Generate Meeting List</a></p>
</div>
<p style="display: inline; margin-top:.5em;margin-bottom:1.0em;margin-left:.2em;"><a target="_blank" class="button-primary" href="<?php echo home_url(); ?>/?current-meeting-list=<?php echo $this->admin->loaded_setting; ?>">Generate Meeting List</a></p>
</div>
Loading

0 comments on commit ed32414

Please sign in to comment.