inboundnow / retired-landing-pages

Create landing pages for your WordPress site. Monitor and improve conversion rates, run A/B split tests, customize your own templates and more.
http://www.inboundnow.com/landing-pages/
GNU General Public License v2.0
4 stars 2 forks source link

Merging all Metabox Creation into one function #15

Open DavidWells opened 11 years ago

DavidWells commented 11 years ago

We have lp_render_metabox and lp_render_global_settings functions running almost identical switch statements. https://github.com/inboundnow/landing-pages/blob/master/functions/functions.admin.php#L466

We should merge all metabox creation into one switch function so we can globally update all metabox styles if we need to.

The metabox functions are going to (eventually) live in /shared/ folder probably with 'inbound_render_metabox' as the function name. So when we need metaboxes for any situation we can use it.

Is it possible to hook into a switch statement for 1 off custom metabox rendering?

Like: do_action('custom_switch_hook_for_extra_metaboxes'); (down towards the bottom)

function lp_render_metabox($key,$custom_fields,$post)
{
    // Use nonce for verification
    echo "<input type='hidden' name='lp_{$key}_custom_fields_nonce' value='".wp_create_nonce('lp-nonce')."' />";

    // Begin the field table and loop
    echo '<table class="form-table" >';
    //print_r($custom_fields);exit;
    foreach ($custom_fields as $field) {
        $raw_option_id = str_replace($key . "-", "", $field['id']);
        $field_id = $key . "-" .$field['id'];
        $label_class = $raw_option_id . "-label";
        // get value of this field if it exists for this post
        $meta = get_post_meta($post->ID, $field_id, true);

        if ((!isset($meta)&&isset($field['default'])&&!is_numeric($meta))||isset($meta)&&empty($meta)&&isset($field['default'])&&!is_numeric($meta))
        {
            //echo $field['id'].":".$meta;
            //echo "<br>";
            $meta = $field['default'];
        }

        // begin a table row with
        echo '<tr class="'.$field_id.' '.$raw_option_id.' landing-page-option-row">
                <th class="landing-page-table-header '.$label_class.'"><label for="'.$field_id.'">'.$field['label'].'</label></th>
                <td class="landing-page-option-td">';
                switch($field['type']) {
                    // default content for the_content
                    case 'default-content':
                        echo '<span id="overwrite-content" class="button-secondary">Insert Default Content into main Content area</span><div style="display:none;"><textarea name="'.$field_id.'" id="'.$field_id.'" class="default-content" cols="106" rows="6" style="width: 75%; display:hidden;">'.$meta.'</textarea></div>';
                        break;
                    // text
                    case 'colorpicker':
                        if (!$meta)
                        {
                            $meta = $field['default'];
                        }
                        echo '<input type="text" class="jpicker" style="background-color:#'.$meta.'" name="'.$field_id.'" id="'.$field_id.'" value="'.$meta.'" size="5" /><span class="button-primary new-save-lp" id="'.$field_id.'" style="margin-left:10px; display:none;">Update</span>
                                <div class="lp_tooltip tool_color" title="'.$field['description'].'"></div>';
                        break;
                    case 'datepicker':
                        echo '<div class="jquery-date-picker" id="date-picking">    
                        <span class="datepair" data-language="javascript">  
                                    Date: <input type="text" id="date-picker-'.$key.'" class="date start" /></span>
                                    Time: <input id="time-picker-'.$key.'" type="text" class="time time-picker" />
                                    <input type="hidden" name="'.$field_id.'" id="'.$field_id.'" value="'.$meta.'" class="new-date" value="" >
                                    <p class="description">'.$field['description'].'</p>
                            </div>';        
                        break;                      
                    case 'text':
                        echo '<input type="text" name="'.$field_id.'" id="'.$field_id.'" value="'.$meta.'" size="30" />
                                <div class="lp_tooltip" title="'.$field['description'].'"></div>';
                        break;
                    // textarea
                    case 'textarea':
                        echo '<textarea name="'.$field_id.'" id="'.$field_id.'" cols="106" rows="6" style="width: 75%;">'.$meta.'</textarea>
                                <div class="lp_tooltip tool_textarea" title="'.$field['description'].'"></div>';
                        break;
                    // wysiwyg
                    case 'wysiwyg':
                        wp_editor( $meta, $field_id, $settings = array() );
                        echo    '<p class="description">'.$field['description'].'</p>';                         
                        break;
                    // media                    
                    case 'media':
                        //echo 1; exit;
                        echo '<label for="upload_image">';
                        echo '<input name="'.$field_id.'"  id="'.$field_id.'" type="text" size="36" name="upload_image" value="'.$meta.'" />';
                        echo '<input class="upload_image_button" id="uploader_'.$field_id.'" type="button" value="Upload Image" />';
                        echo '<p class="description">'.$field['description'].'</p>'; 
                        break;
                    // checkbox
                    case 'checkbox':
                        $i = 1;
                        echo "<table class='lp_check_box_table'>";                      
                        if (!isset($meta)){$meta=array();}
                        elseif (!is_array($meta)){
                            $meta = array($meta);
                        }
                        foreach ($field['options'] as $value=>$label) {
                            if ($i==5||$i==1)
                            {
                                echo "<tr>";
                                $i=1;
                            }
                                echo '<td><input type="checkbox" name="'.$field_id.'[]" id="'.$field_id.'" value="'.$value.'" ',in_array($value,$meta) ? ' checked="checked"' : '','/>';
                                echo '<label for="'.$value.'">&nbsp;&nbsp;'.$label.'</label></td>';                 
                            if ($i==4)
                            {
                                echo "</tr>";
                            }
                            $i++;
                        }
                        echo "</table>";
                        echo '<div class="lp_tooltip tool_checkbox" title="'.$field['description'].'"></div>';
                    break;
                    // radio
                    case 'radio':
                        foreach ($field['options'] as $value=>$label) {
                            //echo $meta.":".$field_id;
                            //echo "<br>";
                            echo '<input type="radio" name="'.$field_id.'" id="'.$field_id.'" value="'.$value.'" ',$meta==$value ? ' checked="checked"' : '','/>';
                            echo '<label for="'.$value.'">&nbsp;&nbsp;'.$label.'</label> &nbsp;&nbsp;&nbsp;&nbsp;';                             
                        }
                        echo '<div class="lp_tooltip" title="'.$field['description'].'"></div>';
                    break;
                    // select
                    case 'dropdown':
                        echo '<select name="'.$field_id.'" id="'.$field_id.'" class="'.$raw_option_id.'">';
                        foreach ($field['options'] as $value=>$label) {
                            echo '<option', $meta == $value ? ' selected="selected"' : '', ' value="'.$value.'">'.$label.'</option>';
                        }
                        echo '</select><div class="lp_tooltip" title="'.$field['description'].'"></div>';
                    break;

do_action('custom_switch_hook_for_extra_metaboxes');

                } //end switch
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table
}

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/2485725-merging-all-metabox-creation-into-one-function?utm_campaign=plugin&utm_content=tracker%2F260749&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F260749&utm_medium=issues&utm_source=github).
atwellpub commented 10 years ago

David did you say you were going to take on this project?

Right now we have the switch statements segregated into their respective modules.

I remember you mentioning that you had begun migrating a version of the global settings switch statement into the /shared/ folder.

DavidWells commented 10 years ago

Yea this has begun on CTA. Still working on it