PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/options-interface.php

https://github.com/mattwiebe/options-framework-plugin
PHP | 344 lines | 241 code | 65 blank | 38 comment | 51 complexity | 4ac58141eef27e1358de70227fbe5a2c MD5 | raw file
  1. <?php
  2. /**
  3. * Generates the tabs that are used in the options menu
  4. */
  5. function optionsframework_tabs() {
  6. $optionsframework_settings = get_option('optionsframework');
  7. $options =& _optionsframework_options();
  8. $menu = '';
  9. foreach ($options as $value) {
  10. // Heading for Navigation
  11. if ($value['type'] == "heading") {
  12. $jquery_click_hook = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($value['name']) );
  13. $jquery_click_hook = "of-option-" . $jquery_click_hook;
  14. $menu .= '<a id="'. esc_attr( $jquery_click_hook ) . '-tab" class="nav-tab" title="' . esc_attr( $value['name'] ) . '" href="' . esc_attr( '#'. $jquery_click_hook ) . '">' . esc_html( $value['name'] ) . '</a>';
  15. }
  16. }
  17. return $menu;
  18. }
  19. /**
  20. * Generates the options fields that are used in the form.
  21. */
  22. function optionsframework_fields() {
  23. global $allowedtags;
  24. $optionsframework_settings = get_option('optionsframework');
  25. // Gets the unique option id
  26. if ( isset( $optionsframework_settings['id'] ) ) {
  27. $option_name = $optionsframework_settings['id'];
  28. }
  29. else {
  30. $option_name = 'optionsframework';
  31. };
  32. $settings = get_option($option_name);
  33. $options =& _optionsframework_options();
  34. $counter = 0;
  35. $menu = '';
  36. foreach ( $options as $value ) {
  37. $counter++;
  38. $val = '';
  39. $select_value = '';
  40. $checked = '';
  41. $output = '';
  42. // Wrap all options
  43. if ( ( $value['type'] != "heading" ) && ( $value['type'] != "info" ) ) {
  44. // Keep all ids lowercase with no spaces
  45. $value['id'] = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($value['id']) );
  46. $id = 'section-' . $value['id'];
  47. $class = 'section ';
  48. if ( isset( $value['type'] ) ) {
  49. $class .= ' section-' . $value['type'];
  50. }
  51. if ( isset( $value['class'] ) ) {
  52. $class .= ' ' . $value['class'];
  53. }
  54. $output .= '<div id="' . esc_attr( $id ) .'" class="' . esc_attr( $class ) . '">'."\n";
  55. if ( isset( $value['name'] ) ) {
  56. $output .= '<h4 class="heading">' . esc_html( $value['name'] ) . '</h4>' . "\n";
  57. }
  58. if ( $value['type'] != 'editor' ) {
  59. $output .= '<div class="option">' . "\n" . '<div class="controls">' . "\n";
  60. }
  61. else {
  62. $output .= '<div class="option">' . "\n" . '<div>' . "\n";
  63. }
  64. }
  65. // Set default value to $val
  66. if ( isset( $value['std'] ) ) {
  67. $val = $value['std'];
  68. }
  69. // If the option is already saved, ovveride $val
  70. if ( ( $value['type'] != 'heading' ) && ( $value['type'] != 'info') ) {
  71. if ( isset( $settings[($value['id'])]) ) {
  72. $val = $settings[($value['id'])];
  73. // Striping slashes of non-array options
  74. if ( !is_array($val) ) {
  75. $val = stripslashes( $val );
  76. }
  77. }
  78. }
  79. // If there is a description save it for labels
  80. $explain_value = '';
  81. if ( isset( $value['desc'] ) ) {
  82. $explain_value = $value['desc'];
  83. }
  84. switch ( $value['type'] ) {
  85. // Basic text input
  86. case 'text':
  87. $output .= '<input id="' . esc_attr( $value['id'] ) . '" class="of-input" name="' . esc_attr( $option_name . '[' . $value['id'] . ']' ) . '" type="text" value="' . esc_attr( $val ) . '" />';
  88. break;
  89. // Textarea
  90. case 'textarea':
  91. $rows = '8';
  92. if ( isset( $value['settings']['rows'] ) ) {
  93. $custom_rows = $value['settings']['rows'];
  94. if ( is_numeric( $custom_rows ) ) {
  95. $rows = $custom_rows;
  96. }
  97. }
  98. $val = stripslashes( $val );
  99. $output .= '<textarea id="' . esc_attr( $value['id'] ) . '" class="of-input" name="' . esc_attr( $option_name . '[' . $value['id'] . ']' ) . '" rows="' . $rows . '">' . esc_textarea( $val ) . '</textarea>';
  100. break;
  101. // Select Box
  102. case ($value['type'] == 'select'):
  103. $output .= '<select class="of-input" name="' . esc_attr( $option_name . '[' . $value['id'] . ']' ) . '" id="' . esc_attr( $value['id'] ) . '">';
  104. foreach ($value['options'] as $key => $option ) {
  105. $selected = '';
  106. if ( $val != '' ) {
  107. if ( $val == $key) { $selected = ' selected="selected"';}
  108. }
  109. $output .= '<option'. $selected .' value="' . esc_attr( $key ) . '">' . esc_html( $option ) . '</option>';
  110. }
  111. $output .= '</select>';
  112. break;
  113. // Radio Box
  114. case "radio":
  115. $name = $option_name .'['. $value['id'] .']';
  116. foreach ($value['options'] as $key => $option) {
  117. $id = $option_name . '-' . $value['id'] .'-'. $key;
  118. $output .= '<input class="of-input of-radio" type="radio" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="'. esc_attr( $key ) . '" '. checked( $val, $key, false) .' /><label for="' . esc_attr( $id ) . '">' . esc_html( $option ) . '</label>';
  119. }
  120. break;
  121. // Image Selectors
  122. case "images":
  123. $name = $option_name .'['. $value['id'] .']';
  124. foreach ( $value['options'] as $key => $option ) {
  125. $selected = '';
  126. $checked = '';
  127. if ( $val != '' ) {
  128. if ( $val == $key ) {
  129. $selected = ' of-radio-img-selected';
  130. $checked = ' checked="checked"';
  131. }
  132. }
  133. $output .= '<input type="radio" id="' . esc_attr( $value['id'] .'_'. $key) . '" class="of-radio-img-radio" value="' . esc_attr( $key ) . '" name="' . esc_attr( $name ) . '" '. $checked .' />';
  134. $output .= '<div class="of-radio-img-label">' . esc_html( $key ) . '</div>';
  135. $output .= '<img src="' . esc_url( $option ) . '" alt="' . $option .'" class="of-radio-img-img' . $selected .'" onclick="document.getElementById(\''. esc_attr($value['id'] .'_'. $key) .'\').checked=true;" />';
  136. }
  137. break;
  138. // Checkbox
  139. case "checkbox":
  140. $output .= '<input id="' . esc_attr( $value['id'] ) . '" class="checkbox of-input" type="checkbox" name="' . esc_attr( $option_name . '[' . $value['id'] . ']' ) . '" '. checked( $val, 1, false) .' />';
  141. $output .= '<label class="explain" for="' . esc_attr( $value['id'] ) . '">' . wp_kses( $explain_value, $allowedtags) . '</label>';
  142. break;
  143. // Multicheck
  144. case "multicheck":
  145. foreach ($value['options'] as $key => $option) {
  146. $checked = '';
  147. $label = $option;
  148. $option = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($key));
  149. $id = $option_name . '-' . $value['id'] . '-'. $option;
  150. $name = $option_name . '[' . $value['id'] . '][' . $option .']';
  151. if ( isset($val[$option]) ) {
  152. $checked = checked($val[$option], 1, false);
  153. }
  154. $output .= '<input id="' . esc_attr( $id ) . '" class="checkbox of-input" type="checkbox" name="' . esc_attr( $name ) . '" ' . $checked . ' /><label for="' . esc_attr( $id ) . '">' . esc_html( $label ) . '</label>';
  155. }
  156. break;
  157. // Color picker
  158. case "color":
  159. $output .= '<div id="' . esc_attr( $value['id'] . '_picker' ) . '" class="colorSelector"><div style="' . esc_attr( 'background-color:' . $val ) . '"></div></div>';
  160. $output .= '<input class="of-color" name="' . esc_attr( $option_name . '[' . $value['id'] . ']' ) . '" id="' . esc_attr( $value['id'] ) . '" type="text" value="' . esc_attr( $val ) . '" />';
  161. break;
  162. // Uploader
  163. case "upload":
  164. $output .= optionsframework_medialibrary_uploader( $value['id'], $val, null );
  165. break;
  166. // Typography
  167. case 'typography':
  168. $typography_stored = $val;
  169. // Font Size
  170. $font_size = '<select class="of-typography of-typography-size" name="' . esc_attr( $option_name . '[' . $value['id'] . '][size]' ) . '" id="' . esc_attr( $value['id'] . '_size' ) . '">';
  171. $sizes = of_recognized_font_sizes();
  172. foreach ( $sizes as $i ) {
  173. $size = $i . 'px';
  174. $font_size .= '<option value="' . esc_attr( $size ) . '" ' . selected( $typography_stored['size'], $size, false ) . '>' . esc_html( $size ) . '</option>';
  175. }
  176. $font_size .= '</select>';
  177. // Font Face
  178. $font_face = '<select class="of-typography of-typography-face" name="' . esc_attr( $option_name . '[' . $value['id'] . '][face]' ) . '" id="' . esc_attr( $value['id'] . '_face' ) . '">';
  179. $faces = of_recognized_font_faces();
  180. foreach ( $faces as $key => $face ) {
  181. $font_face .= '<option value="' . esc_attr( $key ) . '" ' . selected( $typography_stored['face'], $key, false ) . '>' . esc_html( $face ) . '</option>';
  182. }
  183. $font_face .= '</select>';
  184. // Font Weight
  185. $font_style = '<select class="of-typography of-typography-style" name="'.$option_name.'['.$value['id'].'][style]" id="'. $value['id'].'_style">';
  186. // Font Style
  187. $styles = of_recognized_font_styles();
  188. foreach ( $styles as $key => $style ) {
  189. $font_style .= '<option value="' . esc_attr( $key ) . '" ' . selected( $typography_stored['style'], $key, false ) . '>'. $style .'</option>';
  190. }
  191. $font_style .= '</select>';
  192. // Font Color
  193. $font_color = '<div id="' . esc_attr( $value['id'] ) . '_color_picker" class="colorSelector"><div style="' . esc_attr( 'background-color:' . $typography_stored['color'] ) . '"></div></div>';
  194. $font_color .= '<input class="of-color of-typography of-typography-color" name="' . esc_attr( $option_name . '[' . $value['id'] . '][color]' ) . '" id="' . esc_attr( $value['id'] . '_color' ) . '" type="text" value="' . esc_attr( $typography_stored['color'] ) . '" />';
  195. // allow modification/injection of typography fields
  196. $typography_fields = compact( 'font_size', 'font_face', 'font_style', 'font_color' );
  197. $typography_fields = apply_filters( 'of_typography_fields', $typography_fields, $typography_stored, $value );
  198. $output .= implode( '', $typography_fields );
  199. break;
  200. // Background
  201. case 'background':
  202. $background = $val;
  203. // Background Color
  204. $output .= '<div id="' . esc_attr( $value['id'] ) . '_color_picker" class="colorSelector"><div style="' . esc_attr( 'background-color:' . $background['color'] ) . '"></div></div>';
  205. $output .= '<input class="of-color of-background of-background-color" name="' . esc_attr( $option_name . '[' . $value['id'] . '][color]' ) . '" id="' . esc_attr( $value['id'] . '_color' ) . '" type="text" value="' . esc_attr( $background['color'] ) . '" />';
  206. // Background Image - New AJAX Uploader using Media Library
  207. if (!isset($background['image'])) {
  208. $background['image'] = '';
  209. }
  210. $output .= optionsframework_medialibrary_uploader( $value['id'], $background['image'], null, '',0,'image');
  211. $class = 'of-background-properties';
  212. if ( '' == $background['image'] ) {
  213. $class .= ' hide';
  214. }
  215. $output .= '<div class="' . esc_attr( $class ) . '">';
  216. // Background Repeat
  217. $output .= '<select class="of-background of-background-repeat" name="' . esc_attr( $option_name . '[' . $value['id'] . '][repeat]' ) . '" id="' . esc_attr( $value['id'] . '_repeat' ) . '">';
  218. $repeats = of_recognized_background_repeat();
  219. foreach ($repeats as $key => $repeat) {
  220. $output .= '<option value="' . esc_attr( $key ) . '" ' . selected( $background['repeat'], $key, false ) . '>'. esc_html( $repeat ) . '</option>';
  221. }
  222. $output .= '</select>';
  223. // Background Position
  224. $output .= '<select class="of-background of-background-position" name="' . esc_attr( $option_name . '[' . $value['id'] . '][position]' ) . '" id="' . esc_attr( $value['id'] . '_position' ) . '">';
  225. $positions = of_recognized_background_position();
  226. foreach ($positions as $key=>$position) {
  227. $output .= '<option value="' . esc_attr( $key ) . '" ' . selected( $background['position'], $key, false ) . '>'. esc_html( $position ) . '</option>';
  228. }
  229. $output .= '</select>';
  230. // Background Attachment
  231. $output .= '<select class="of-background of-background-attachment" name="' . esc_attr( $option_name . '[' . $value['id'] . '][attachment]' ) . '" id="' . esc_attr( $value['id'] . '_attachment' ) . '">';
  232. $attachments = of_recognized_background_attachment();
  233. foreach ($attachments as $key => $attachment) {
  234. $output .= '<option value="' . esc_attr( $key ) . '" ' . selected( $background['attachment'], $key, false ) . '>' . esc_html( $attachment ) . '</option>';
  235. }
  236. $output .= '</select>';
  237. $output .= '</div>';
  238. break;
  239. // Info
  240. case "info":
  241. $class = 'section';
  242. if ( isset( $value['type'] ) ) {
  243. $class .= ' section-' . $value['type'];
  244. }
  245. if ( isset( $value['class'] ) ) {
  246. $class .= ' ' . $value['class'];
  247. }
  248. $output .= '<div class="' . esc_attr( $class ) . '">' . "\n";
  249. if ( isset($value['name']) ) {
  250. $output .= '<h4 class="heading">' . esc_html( $value['name'] ) . '</h4>' . "\n";
  251. }
  252. if ( $value['desc'] ) {
  253. $output .= apply_filters('of_sanitize_info', $value['desc'] ) . "\n";
  254. }
  255. $output .= '</div>' . "\n";
  256. break;
  257. // Heading for Navigation
  258. case "heading":
  259. if ($counter >= 2) {
  260. $output .= '</div>'."\n";
  261. }
  262. $jquery_click_hook = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($value['name']) );
  263. $jquery_click_hook = "of-option-" . $jquery_click_hook;
  264. $menu .= '<a id="'. esc_attr( $jquery_click_hook ) . '-tab" class="nav-tab" title="' . esc_attr( $value['name'] ) . '" href="' . esc_attr( '#'. $jquery_click_hook ) . '">' . esc_html( $value['name'] ) . '</a>';
  265. $output .= '<div class="group" id="' . esc_attr( $jquery_click_hook ) . '">';
  266. $output .= '<h3>' . esc_html( $value['name'] ) . '</h3>' . "\n";
  267. break;
  268. }
  269. if ( ( $value['type'] != "heading" ) && ( $value['type'] != "info" ) ) {
  270. $output .= '</div>';
  271. if ( ( $value['type'] != "checkbox" ) && ( $value['type'] != "editor" ) ) {
  272. $output .= '<div class="explain">' . wp_kses( $explain_value, $allowedtags) . '</div>'."\n";
  273. }
  274. $output .= '</div></div>'."\n";
  275. }
  276. echo $output;
  277. }
  278. echo '</div>';
  279. }