Jump to content

Help with re-factoring repetitive code


rmc-dj

Recommended Posts

Hello there. I'm a web designer trying to learn more development skills. I've written this opening hours widget for wordpress. Its functionality is very simple which is all I really need and it does the job, however I'd love to know how to make the code smaller as there are lots of thing repeating. I think parts of it could be done using a for each loop and arrays, but I can't figure out what way to do it. If anyone can help or point me in the right direction for what topic specifically to study I would be very grateful. Please see code below. Sorry if its really badly written,

 

/**
* Creating and displaying an Opening Times Widget
*/
wp_register_sidebar_widget(
'opening_times_widget',          // your unique widget id
'Opening Times',                 // widget name
'opening_times_widget_display',  // callback function to display widget
array(                      // options
 'description' => 'This widget displays your opening hours in your website sidebar'
)
);


wp_register_widget_control(
'opening_times_widget',  // id
'opening_times_widget',  // name
'opening_times_widget_control' // callback function
);



function opening_times_widget_control($args=array(), $params=array()) {
//the form is submitted, save into database
if (isset($_POST['submitted'])) {
 update_option('opening_times_widget_title', $_POST['widgettitle']);
 update_option('opening_times_widget_email', $_POST['widgetemail']);
 update_option('opening_times_widget_description', $_POST['description']);
 update_option('opening_times_widget_monday', $_POST['mondayhours']);
 update_option('opening_times_widget_tuesday', $_POST['tuesdayhours']);
 update_option('opening_times_widget_wednesday', $_POST['wednesdayhours']);
 update_option('opening_times_widget_thursday', $_POST['thursdayhours']);
 update_option('opening_times_widget_friday', $_POST['fridayhours']);
 update_option('opening_times_widget_saturday', $_POST['saturdayhours']);
 update_option('opening_times_widget_sunday', $_POST['sundayhours']);
}
//load options
$widgettitle = get_option('opening_times_widget_title');
$widgetemail = get_option('opening_times_widget_email');
$description = get_option('opening_times_widget_description');
$mondayhours = get_option('opening_times_widget_monday');
$tuesdayhours = get_option('opening_times_widget_tuesday');
$wednesdayhours = get_option('opening_times_widget_wednesday');
$thursdayhours = get_option('opening_times_widget_thursday');
$fridayhours = get_option('opening_times_widget_friday');
$saturdayhours = get_option('opening_times_widget_saturday');
$sundayhours = get_option('opening_times_widget_sunday');
?>


Widget Title: ( eg: Opening Hours )<br />
<input type="text" class="widefat" name="widgettitle" value="<?php echo stripslashes($widgettitle); ?>" />
<br /><br />
Monday<br />
<input type="text" class="widefat" name="mondayhours" value="<?php echo stripslashes($mondayhours); ?>" />
<br /><br />
Tuesday<br />
<input type="text" class="widefat" name="tuesdayhours" value="<?php echo stripslashes($tuesdayhours); ?>" />
<br /><br />
Wednesday<br />
<input type="text" class="widefat" name="wednesdayhours" value="<?php echo stripslashes($wednesdayhours); ?>" />
<br /><br />
Thursday<br />
<input type="text" class="widefat" name="thursdayhours" value="<?php echo stripslashes($thursdayhours); ?>" />
<br /><br />
Friday<br />
<input type="text" class="widefat" name="fridayhours" value="<?php echo stripslashes($fridayhours); ?>" />
<br /><br />
Saturday<br />
<input type="text" class="widefat" name="saturdayhours" value="<?php echo stripslashes($saturdayhours); ?>" />
<br /><br />
Sunday<br />
<input type="text" class="widefat" name="sundayhours" value="<?php echo stripslashes($sundayhours); ?>" />
<br /><br />
Additional Information:<br />
<textarea class="widefat" rows="5" name="description"><?php echo stripslashes($description); ?></textarea>
<br /><br />
Email Address:<br />
<input type="text" class="widefat" name="widgetemail" value="<?php echo stripslashes($widgetemail); ?>" />
<br /><br />
<input type="hidden" name="submitted" value="1" />
<?php
}


function opening_times_widget_display($args=array(), $params=array()) {
//load options
$widgettitle = get_option('opening_times_widget_title');
$description = get_option('opening_times_widget_description');
$widgetemail = get_option('opening_times_widget_email');
$mondayhours = get_option('opening_times_widget_monday');
$tuesdayhours = get_option('opening_times_widget_tuesday');
$wednesdayhours = get_option('opening_times_widget_wednesday');
$thursdayhours = get_option('opening_times_widget_thursday');
$fridayhours = get_option('opening_times_widget_friday');
$saturdayhours = get_option('opening_times_widget_saturday');
$sundayhours = get_option('opening_times_widget_sunday');
//widget output
echo stripslashes($args['before_widget']);
echo stripslashes($args['before_title']);
echo stripslashes($widgettitle).'<span class="opening-hours-icon"> </span>';
echo stripslashes($args['after_title']);

echo '<div class="textwidget">';

if ($mondayhours != '') {
 echo '<div class="opening-hours-day-info">Monday: <span>'.stripslashes($mondayhours).'</span></div>';
}
if ($tuesdayhours != '') {
 echo '<div class="opening-hours-day-info">Tuesday: <span>'.stripslashes($tuesdayhours).'</span></div>';
}
if ($wednesdayhours != '') {
 echo '<div class="opening-hours-day-info">Wednesday: <span>'.stripslashes($wednesdayhours).'</span></div>';
}
if ($thursdayhours != '') {
 echo '<div class="opening-hours-day-info">Thursday: <span>'.stripslashes($thursdayhours).'</span></div>';
}
if ($fridayhours != '') {
 echo '<div class="opening-hours-day-info">Friday: <span>'.stripslashes($fridayhours).'</span></div>';
}
if ($saturdayhours != '') {
 echo '<div class="opening-hours-day-info">Saturday: <span>'.stripslashes($saturdayhours).'</span></div>';
}
if ($sundayhours != '') {
 echo '<div class="opening-hours-day-info">Sunday: <span>'.stripslashes($sundayhours).'</span></div>';
}

echo '<div class="opening-hours-description">'.stripslashes(nl2br($description)).'</div>';

if ($widgetemail != '') {
 echo '<div class="opening-hours-email"><a href="mailto:'.stripslashes($widgetemail).'" target="_blank">Email us</a> to arrange an appointment</div>';
}
echo '</div>';//close div.textwidget
echo stripslashes($args['after_widget']);
}

Link to comment
https://forums.phpfreaks.com/topic/274149-help-with-re-factoring-repetitive-code/
Share on other sites

The most obvious thing to do would be to consolidate your load options code into a function of its own. Something like:

 

function load_options() {
   $options = array();

   $options['title'] = stripslashes(get_option('opening_times_widget_title'));
   $options['email'] = stripslashes(get_option('opening_times_widget_email'));
   $options['description'] = stripslashes(get_option('opening_times_widget_description'));

   // etc.

   return $options;
}

 

You can then invoke the function like:

 

$options = load_options();

 

Once you have your $options variable, you can just access the options like:

 

echo $options['title']; // where you can replace 'title' with whatever it is you want to use

 

EDIT: For future reading:

 

http://php.net/manual/en/language.functions.php

http://php.net/manual/en/language.types.array.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.