options-interface.php 15 KB

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