コード例 #1
0
 /**
  * Check whether an option key is valid
  *
  * @param string $key
  * @return bool
  */
 public function is_option_key_valid($key)
 {
     $options = $this->bridge->get_option($key);
     if ($options && is_array($options)) {
         return true;
     }
     return false;
 }
コード例 #2
0
 /**
  * Generate HTML field
  */
 protected function generate_html_field($type = '', $data = array(), $name = '', $in_section = false)
 {
     $pre_html_field = '';
     $post_html_field = '';
     $checked = 'checked="checked" ';
     $selected = 'selected="selected" ';
     $value = isset($this->form_options[$name]) ? $this->form_options[$name] : '';
     $value = isset($data['value']) ? $data['value'] : $value;
     if ('checkbox' == $type) {
         $value = current(array_values($data));
         $value = $value ? $value : 'yes';
     }
     $value = !empty($this->domain) && ('textarea' == $type || 'input' == $type) ? $this->bridge->t($value, $this->domain) : $value;
     if (is_array($value)) {
         foreach ($value as &$v) {
             $v = is_array($v) ? array_map('esc_attr', $v) : esc_attr($v);
         }
     } else {
         $value = 'textarea' == $type ? esc_html($value) : esc_attr($value);
     }
     $array_replace = array();
     $array_search = array('type', 'text', 'size', 'name', 'value', 'cols', 'rows', 'label', 'disabled', 'pre', 'post', 'attributes', 'label_attributes');
     $return_html = '';
     $attributes = $this->generate_field_attributes($name);
     $label_attributes = '';
     $br = $this->is_compound_field($name) || $type == 'textarea' ? '' : "<br />\n";
     $pre = !empty($data['pre']) ? $data['pre'] : '';
     $post = !empty($data['post']) ? $data['post'] : '';
     $param = empty($this->form['params'][$name]) ? false : $this->form['params'][$name];
     $name_attr = esc_attr($name);
     switch ($type) {
         case 'heading':
         case 'heading4':
             $html_field = '%s';
             break;
         case 'input':
             $data['label'] = !empty($data['label']) ? ' <em>' . $data['label'] . '</em>' : '';
             $html_field = !$in_section ? '%pre%<input %attributes%%disabled% size="%size%" type="text" ' . 'id="' . $name_attr . '" ' . 'name="' . $name_attr . '" ' . 'value="' . $value . '" />%label%' : '<label for="' . $name_attr . '">%pre%<input %attributes%%disabled% size="%size%" type="text" ' . 'id="' . $name_attr . '" ' . 'name="' . $name_attr . '" ' . 'value="' . $value . '" />%label%</label>';
             $post_html_field = '%post%';
             break;
         case 'select':
         case 'select_multi':
             $pre_html_field = 'select_multi' == $type ? '%pre%<select %attributes%id="' . $name_attr . '" name="' . $name_attr . '[]" multiple>' . "\n" : '%pre%<select %attributes%id="' . $name_attr . '" name="' . $name_attr . '">' . "\n";
             $html_field = '<option %selected%value="%value%">%option%</option>';
             $post_html_field = '</select>%post%' . $br;
             break;
         case 'checkbox':
             $html_field = '<label %label_attributes%for="%name%">' . '<input %attributes%%checked%type="checkbox" id="%name%" name="%name%" value="yes" /> %label%</label>';
             $post_html_field = '%post%';
             break;
         case 'checkbox_multi':
             $html_field = '<label %label_attributes%for="%name%-%value%">' . '<input %attributes%%checked%type="checkbox" id="%name%-%value%" name="%name%[]" value="%value%" /> %label%</label>';
             $post_html_field = '%post%';
             break;
         case 'radio':
             $html_field = '<label %label_attributes%>' . '<input %attributes%%checked%type="radio" ' . 'name="' . $name_attr . '" value="%value%" /> %label%</label>';
             $post_html_field = '%post%';
             break;
         case 'textarea':
             $html_field = '%pre%<textarea %attributes%%disabled% ' . 'id="' . $name_attr . '" ' . 'name="' . $name_attr . '" cols="%cols%" rows="%rows%">' . $value . '</textarea>';
             $post_html_field = '%post%';
             break;
             // @since rev 161 add a help field
         // @since rev 161 add a help field
         case 'help':
             $html_field_class = 'bwp-field-help';
             $html_field_inner = '&nbsp;<span %attributes%>(?)</span>';
             // use nice font icon for WP 3.8+
             if ($this->plugin->get_current_wp_version('3.8')) {
                 $html_field_class = 'dashicons dashicons-editor-help bwp-field-help';
                 $html_field_inner = '&nbsp;<span %attributes%></span>';
             }
             // use explicitly set attributes for this field when needed
             if (empty($data['url'])) {
                 $attributes = isset($data['attributes']) && is_array($data['attributes']) ? $data['attributes'] : array('class' => '');
                 $attributes['class'] .= ' ' . $html_field_class;
                 $attributes = $this->generate_attribute_string($attributes);
                 $html_field = $html_field_inner;
             } else {
                 $attributes = $this->generate_attribute_string(array('class' => $html_field_class));
                 $html_field = '<a class="bwp-field-help-link" target="_blank" ' . 'title="' . __('View more info in a separate tab', $this->domain) . '" ' . 'href="' . esc_url($data['url']) . '">' . $html_field_inner . '</a>';
             }
             break;
             // @since rev 165 add button field
         // @since rev 165 add button field
         case 'button':
             $data['type'] = empty($data['type']) ? 'button' : $data['type'];
             $data['text'] = empty($data['text']) ? '' : $data['text'];
             // set default button classes
             $btn_class = !empty($data['is_primary']) ? 'button-primary' : 'button-secondary';
             $attributes = $this->get_field_attributes($name);
             $attributes['class'] = $btn_class . ' ' . $attributes['class'];
             $attributes = $this->generate_attribute_string($attributes);
             $html_field = '%pre%<button %attributes%%disabled% ' . 'id="' . $name_attr . '" ' . 'name="' . $name_attr . '" type="%type%">' . '%text%' . '</button>';
             $post_html_field = '%post%';
             break;
     }
     if (!isset($data)) {
         return;
     }
     if (strpos($type, 'heading') === 0 && !is_array($data)) {
         $return_html .= sprintf($html_field, $data);
     } elseif ($type == 'radio' || $type == 'checkbox' || $type == 'checkbox_multi' || $type == 'select' || $type == 'select_multi') {
         // generate label attributes for checkbox/radiobox if any
         if (strpos($type, 'select') === false) {
             $label_attributes = array();
             $this->generate_field_help_attributes($name, $label_attributes);
             $label_attributes = $this->generate_attribute_string($label_attributes);
             // generating label attributes might add some post HTML, so we
             // need to reassign br here
             $br = $this->is_compound_field($name) ? '' : "<br />\n";
         }
         foreach ($data as $key => $value) {
             if ($type == 'checkbox') {
                 // handle checkbox a little bit differently
                 if ($this->form_options[$name] == 'yes') {
                     $return_html .= str_replace(array('%value%', '%name%', '%label%', '%checked%', '%attributes%', '%label_attributes%'), array($value, $name_attr, $key, $checked, $attributes, $label_attributes), $html_field);
                     $return_html .= apply_filters('bwp_option_after_' . $type . '_' . $name . '_checked', '', $value, $param);
                     $return_html .= $br;
                 } else {
                     $return_html .= str_replace(array('%value%', '%name%', '%label%', '%checked%', '%attributes%', '%label_attributes%'), array($value, $name_attr, $key, '', $attributes, $label_attributes), $html_field);
                     $return_html .= apply_filters('bwp_option_after_' . $type . '_' . $name, '', $value, $param);
                     $return_html .= $br;
                 }
             } elseif ($type == 'checkbox_multi') {
                 // handle a multi checkbox differently
                 if (isset($this->form_options[$name]) && is_array($this->form_options[$name]) && in_array($value, $this->form_options[$name])) {
                     $return_html .= str_replace(array('%value%', '%name%', '%label%', '%checked%', '%attributes%', '%label_attributes%'), array($value, $name_attr, $key, $checked, $attributes, $label_attributes), $html_field);
                     $return_html .= apply_filters('bwp_option_after_' . $type . '_' . $name . '_checked', '', $value, $param);
                     $return_html .= $br;
                 } else {
                     $return_html .= str_replace(array('%value%', '%name%', '%label%', '%checked%', '%attributes%', '%label_attributes%'), array($value, $name_attr, $key, '', $attributes, $label_attributes), $html_field);
                     $return_html .= apply_filters('bwp_option_after_' . $type . '_' . $name, '', $value, $param);
                     $return_html .= $br;
                 }
             } elseif (isset($this->form_options[$name]) && ($this->form_options[$name] == $value || is_array($this->form_options[$name]) && in_array($value, $this->form_options[$name]))) {
                 $item_br = $type == 'select' || $type == 'select_multi' ? "\n" : $br;
                 $return_html .= str_replace(array('%value%', '%name%', '%label%', '%option%', '%checked%', '%selected%', '%pre%', '%post%'), array($value, $name_attr, $key, $key, $checked, $selected, $pre, $post), $html_field) . $item_br;
             } else {
                 $item_br = $type == 'select' || $type == 'select_multi' ? "\n" : $br;
                 $return_html .= str_replace(array('%value%', '%name%', '%label%', '%option%', '%checked%', '%selected%', '%pre%', '%post%'), array($value, $name_attr, $key, $key, '', '', $pre, $post), $html_field) . $item_br;
             }
         }
     } else {
         foreach ($array_search as &$keyword) {
             $array_replace[$keyword] = '';
             if ($keyword == 'attributes') {
                 $array_replace[$keyword] = $attributes;
             } elseif (!empty($data[$keyword])) {
                 $array_replace[$keyword] = $data[$keyword];
             }
             $keyword = '%' . $keyword . '%';
         }
         $return_html = str_replace($array_search, $array_replace, $html_field) . $br;
     }
     // inline fields
     $inline_html = '';
     if (isset($this->form['inline_fields'][$name]) && is_array($this->form['inline_fields'][$name])) {
         foreach ($this->form['inline_fields'][$name] as $field => $field_type) {
             if (isset($this->form[$field_type][$field])) {
                 $inline_html = ' ' . $this->generate_html_field($field_type, $this->form[$field_type][$field], $field, $in_section);
             }
         }
     }
     // html before field
     $pre = !empty($this->form['pre'][$name]) ? ' ' . $this->form['pre'][$name] : $pre;
     // html after field
     $post = !empty($this->form['post'][$name]) ? ' ' . $this->form['post'][$name] : $post;
     // support for custom html attributes
     $pre_html_field = str_replace('%attributes%', $attributes, $pre_html_field);
     return str_replace('%pre%', $pre, $pre_html_field) . $return_html . str_replace('%post%', $post, $post_html_field) . $inline_html;
 }