/**
 * Locates a meta option if it exists
 *
 * @param string $key the key of the option
 */
function plmeta($key, $args)
{
    $d = array('subkey' => null, 'post_id' => null, 'setting' => null, 'clone_id' => null);
    $o = wp_parse_args($args, $d);
    // Deal with cloning options
    if (isset($args['clone_id']) && $args['clone_id'] != 1) {
        $id_key = $key . '_' . $args['clone_id'];
    } else {
        $id_key = $key;
    }
    // Deal w/ default checkbox/boolean stuff
    // If default is set, return if reversed
    if (isset($o['post_id']) && !empty($o['post_id'])) {
        $default_value = get_post_meta($o['post_id'], $id_key, true);
        $reverse = pldefault($key, $args, 'val') ? get_post_meta($o['post_id'], $key . '_reverse', true) : false;
        if ((bool) $default_value && (bool) $reverse) {
            return false;
        } else {
            return $default_value;
        }
    } else {
        return false;
    }
}
/**
 * Locates a meta option if it exists
 *
 * @param string $key the key of the option
 */
function plmeta($key, $args)
{
    $d = array('subkey' => null, 'post_id' => null, 'setting' => null, 'clone_id' => null);
    $o = wp_parse_args($args, $d);
    $pid = $o['post_id'];
    if (!$pid) {
        return false;
    }
    $meta_global = "pl_meta_{$pid}";
    global ${$meta_global};
    $meta_opts = ${$meta_global};
    if (!is_array($meta_opts)) {
        $meta_opts = get_post_meta($pid);
        ${$meta_global} = $meta_opts;
    }
    // Deal with cloning options
    if (isset($args['clone_id']) && $args['clone_id'] != 1 && $args['clone_id'] != 0) {
        $id_key = $key . '_' . $args['clone_id'];
    } else {
        $id_key = $key;
    }
    // Deal w/ default checkbox/boolean stuff
    // If default is set, return if reversed
    if (isset($o['post_id']) && !empty($o['post_id'])) {
        $default_value = isset($meta_opts[$id_key][0]) ? $meta_opts[$id_key][0] : false;
        $reverse = pldefault($key, $args, 'val') && isset($meta_opts[$key . '_reverse'][0]) ? $meta_opts[$key . '_reverse'][0] : false;
        if ((bool) $default_value && (bool) $reverse) {
            return false;
        } else {
            return $default_value;
        }
    } else {
        return false;
    }
}
 /**
  * Option generation engine
  *
  * Flag needed for post id/profile id -- settings for special handling.
  */
 function option_engine($oid, $o, $flag = null, $setting = null)
 {
     $o = wp_parse_args($o, $this->defaults);
     $o = $this->make_adjustments($o);
     if ($o['disabled']) {
         return;
     }
     $setting = isset($this->settings_field) ? $this->settings_field : PAGELINES_SETTINGS;
     $oset = array('setting' => $setting);
     if ($o['type'] == 'select_same') {
         $new = array_flip($o['selectvalues']);
         foreach ($new as $key => $val) {
             $new[$key] = array('name' => $key);
         }
         $o['selectvalues'] = $new;
     }
     if ($this->settings_field == 'meta') {
         $oset['post_id'] = $flag;
         $o['pid'] = $flag;
         $o['input_id'] = get_pagelines_option_id($oid);
         if ($o['type'] == 'check' && (bool) pldefault($oid)) {
             $o['val'] = plmeta($oid . '_reverse', $oset);
             $o['input_name'] = $oid . '_reverse';
             $o['inputlabel'] = '(Turn Off) ' . $o['inputlabel'];
         } else {
             $o['val'] = plmeta($oid, $oset);
             $o['input_name'] = $oid;
         }
         // Check is difficult w/ defaults got to compensate
         $o['placeholder'] = pldefault($oid, $oset);
         // Parse through multi-selects
         if (!empty($o['selectvalues'])) {
             foreach ($o['selectvalues'] as $sid => $s) {
                 $o['selectvalues'][$sid]['val'] = plmeta($sid, $oset);
                 $o['selectvalues'][$sid]['input_id'] = get_pagelines_option_id($oid, $sid);
                 $o['selectvalues'][$sid]['input_name'] = $sid;
                 $o['selectvalues'][$sid]['placeholder'] = pldefault($sid, $oset);
             }
         }
     } elseif ($this->settings_field == 'profile') {
         $user = $flag;
         $o['val'] = pl_um($oid, $user->ID);
         $o['input_name'] = $oid;
         $o['input_id'] = get_pagelines_option_id($oid);
         if (!empty($o['selectvalues'])) {
             foreach ($o['selectvalues'] as $sid => $s) {
                 $o['selectvalues'][$sid]['val'] = pl_um($oid, $user->ID);
                 $o['selectvalues'][$sid]['input_id'] = get_pagelines_option_id($sid);
                 $o['selectvalues'][$sid]['input_name'] = $sid;
             }
         }
     } elseif ($this->settings_field == PAGELINES_SPECIAL) {
         if ($o['special'] != 'default' && $o['type'] == 'check' && (bool) pldefault($oid)) {
             $oset['subkey'] = $oid . '_reverse';
             $o['inputlabel'] = '(Turn Off) ' . $o['inputlabel'];
         } else {
             $oset['subkey'] = $oid;
         }
         $o['val'] = ploption($o['special'], $oset);
         $o['input_name'] = plname($o['special'], $oset);
         $o['input_id'] = plid($o['special'], $oset);
         $o['placeholder'] = pldefault($oid, $oset);
         // What a hassle.
         // Allow global option for text content (no sub key)
         // If 'hidden' then option will be nuked on save, so in class.sections.php
         // there is an 'upop' that updates to global settings
         if ($o['type'] == 'text_content' || $o['type'] == 'text_content_reverse') {
             $oset['subkey'] = null;
             $o['val'] = ploption($oid, $oset);
             $o['input_name'] = plname($oid, $oset);
         }
         if (!empty($o['selectvalues'])) {
             foreach ($o['selectvalues'] as $sid => $s) {
                 $oset['subkey'] = $sid;
                 $oset['clone_id'] = $o['clone_id'];
                 $o['selectvalues'][$sid]['val'] = ploption($o['special'], $oset);
                 $o['selectvalues'][$sid]['input_id'] = plid($o['special'], $oset);
                 $o['selectvalues'][$sid]['input_name'] = plname($o['special'], $oset);
                 $o['selectvalues'][$sid]['placeholder'] = pldefault($sid, $oset);
             }
         }
     } else {
         $o['val'] = ploption($oid, $oset);
         $o['input_name'] = get_pagelines_option_name($oid, null, null, $setting);
         $o['input_id'] = get_pagelines_option_id($oid, null, null, $setting);
         if (!empty($o['selectvalues'])) {
             foreach ($o['selectvalues'] as $sid => $s) {
                 $o['selectvalues'][$sid]['val'] = ploption($sid, $oset);
                 $o['selectvalues'][$sid]['input_id'] = get_pagelines_option_id($sid);
                 $o['selectvalues'][$sid]['input_name'] = get_pagelines_option_name($sid, null, null, $setting);
             }
         }
     }
     $o['placeholder'] = pl_html($o['placeholder']);
     if ($this->_do_the_option($oid, $o)) {
         printf('<div class="optionrow fix %s">', $this->_layout_class($o));
         $this->get_option_title($oid, $o);
         printf('<div class="optin fix"><div class="oinputs"><div class="oinputs-pad">');
         $this->option_breaker($oid, $o);
         printf('</div></div>');
         echo $this->_get_explanation($oid, $o);
         echo '<div class="clear"></div></div></div>';
     }
 }
 /**
  * Save Meta Options
  *
  * Use tabs array to save options...
  * Need to identify if the option is being set to empty or has never been set
  * *Section Control* gets its own saving schema
  */
 function save_meta_options($postID)
 {
     // Make sure we are saving on the correct post type...
     // Current post type is passed in $_POST
     $current_post_type = isset($_POST['post_type']) ? $_POST['post_type'] : false;
     $post_type_save = in_array($current_post_type, $this->settings['posttype']) ? true : false;
     if ((isset($_POST['update']) || isset($_POST['save']) || isset($_POST['publish'])) && $post_type_save) {
         $page_template = isset($_POST['page_template']) ? $_POST['page_template'] : null;
         $save_template = $this->get_save_template_type($_POST['post_type'], $page_template);
         $template_type = new PageLinesTemplate($save_template);
         $template_type->load_section_optionator();
         // Loop through tabs
         foreach ($this->tabs as $tab => $t) {
             // Loop through tab options
             foreach ($t->options as $oid => $o) {
                 if ($oid == 'section_control') {
                     $this->save_sc($postID);
                 } elseif ($oid == 'page_background_image') {
                     $this->save_bg($oid, $postID);
                 } elseif ($o['type'] == 'text_content' || $o['type'] == 'text_content_reverse') {
                     $option_value = isset($_POST[$oid]) ? $_POST[$oid] : null;
                     plupop($oid, $option_value);
                     plupop($oid, $option_value, array('setting' => PAGELINES_SPECIAL));
                 } elseif ($o['type'] == 'check' && (bool) pldefault($oid)) {
                     $reverse = $oid . "_reverse";
                     $option_value = isset($_POST[$reverse]) ? $_POST[$reverse] : null;
                     if (!empty($option_value) || get_post_meta($postID, $reverse)) {
                         update_post_meta($postID, $reverse, $option_value);
                     }
                 } else {
                     // Note: If the value is null, then test to see if the option is already set to something
                     // create and overwrite the option to null in that case (i.e. it is being set to empty)
                     if (isset($o['selectvalues']) && pagelines_is_multi_option($oid, $o)) {
                         foreach ($o['selectvalues'] as $sid => $s) {
                             $option_value = isset($_POST[$sid]) ? $_POST[$sid] : null;
                             if (!empty($option_value) || get_post_meta($postID, $sid)) {
                                 update_post_meta($postID, $sid, $option_value);
                             }
                         }
                     } else {
                         $option_value = isset($_POST[$oid]) ? $_POST[$oid] : null;
                         if (!empty($option_value) || get_post_meta($postID, $oid)) {
                             update_post_meta($postID, $oid, $option_value);
                         }
                     }
                 }
             }
         }
     }
 }