PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/theme-options/inc/class-theme-options.php

https://github.com/kcssm/news
PHP | 941 lines | 591 code | 164 blank | 186 comment | 82 complexity | c9629a059b03c781a49618fb9eacaa8d MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * Theme Options class
  4. * Do not edit this file unless you know what you're doing
  5. *
  6. * @package _gpp
  7. * @since 1.0
  8. */
  9. /**
  10. * Includes
  11. *
  12. * @since 1.0
  13. */
  14. include_once( 'custom-fonts.php' );
  15. include_once( 'actions.php' );
  16. /**
  17. * Main Class
  18. *
  19. * @since 1.0
  20. */
  21. class GPPThemeOptions {
  22. public $sections;
  23. public $checkboxes;
  24. public $settings;
  25. /**
  26. * Construct
  27. *
  28. * @since 1.0
  29. */
  30. public function __construct() {
  31. // Get theme info
  32. if ( function_exists( 'wp_get_theme' ) ) {
  33. $theme = wp_get_theme();
  34. $this->theme[ 'authorURI' ] = $theme->{ 'Author URI' };
  35. } else {
  36. $theme = get_theme_data( get_stylesheet_directory() . '/style.css' );
  37. $this->theme[ 'authorURI' ] = $theme[ 'AuthorURI' ];
  38. }
  39. // Set constants for theme info
  40. $this->theme[ 'name' ] = $theme[ 'Name' ];
  41. $this->theme[ 'nicename' ] = strtolower( str_replace( " ", "-", $this->theme[ 'name' ] ) );
  42. $this->theme[ 'version' ] = $theme[ 'Version' ];
  43. $this->theme[ 'author' ] = $theme[ 'Author' ];
  44. $this->theme[ 'uri' ] = $this->theme[ 'authorURI' ] . "themes/" . $this->theme[ 'nicename' ];
  45. $this->theme[ 'support' ] = $this->theme[ 'authorURI' ] . 'support/';
  46. $this->theme[ 'readme' ] = get_template_directory_uri() . '/readme.txt';
  47. // This will keep track of the checkbox options for the formHandler function.
  48. $this->checkboxes = array();
  49. $this->settings = array();
  50. $this->getSettings();
  51. add_action( 'admin_menu', array( &$this, 'addPages' ) );
  52. add_action( 'admin_init', array( &$this, 'registerSettings' ) );
  53. add_action( 'admin_notices', array( &$this, 'updateNotice') );
  54. add_action( 'wp_ajax_readme', array( &$this, 'displayReadme' ) );
  55. /* if ( ! get_option( '_gpp_options' ) )
  56. $this->initializeSettings(); */
  57. }
  58. /**
  59. * Add options page
  60. *
  61. * @since 1.0
  62. */
  63. public function addPages() {
  64. $admin_page = add_theme_page( __( 'Theme Options', '_gpp' ), __( 'Theme Options', '_gpp' ), 'manage_options', 'gpp-options', array( &$this, 'displayPage' ) );
  65. add_action( 'admin_print_scripts-' . $admin_page, array( &$this, 'addScripts' ) );
  66. add_action( 'admin_print_styles-' . $admin_page, array( &$this, 'addStyles' ) );
  67. }
  68. /**
  69. * Create settings field
  70. *
  71. * @since 1.0
  72. */
  73. public function createSetting( $args = array() ) {
  74. $defaults = array(
  75. 'id' => 'default_field',
  76. 'title' => __( 'Default Field', '_gpp' ),
  77. 'desc' => __( 'This is a default description.', '_gpp' ),
  78. 'std' => '',
  79. 'type' => 'text',
  80. 'section' => 'general',
  81. 'choices' => array(),
  82. 'class' => '',
  83. 'html' => ''
  84. );
  85. extract( wp_parse_args( $args, $defaults ) );
  86. $field_args = array(
  87. 'type' => $type,
  88. 'id' => $id,
  89. 'desc' => $desc,
  90. 'std' => $std,
  91. 'choices' => $choices,
  92. 'label_for' => $id,
  93. 'class' => $class,
  94. 'html' => $html
  95. );
  96. if ( $type == 'checkbox' )
  97. $this->checkboxes[] = $id;
  98. add_settings_field( $id, $title, array( $this, 'buildOptions' ), 'gpp-options', $section, $field_args );
  99. }
  100. public function updateNotice( $notice = null, $message = null ) {
  101. if ( is_null( $message ) ) {
  102. $message = __( 'Setting updated', '_gpp' );
  103. }
  104. // our default notice
  105. if ( is_null( $notice ) && ! empty( $_GET['settings-updated'] ) ) {
  106. $notice = $_GET[ 'settings-updated' ];
  107. }
  108. // additional classes for styling and fading
  109. // i.e. 'updated other class more class'
  110. $classes = __( 'updated ', '_gpp' );
  111. if ( $notice ) {
  112. add_settings_error( 'gpp-notices', 'gpp-updated', $message, $classes );
  113. }
  114. settings_errors( 'gpp-notices' );
  115. }
  116. /**
  117. * Display options page
  118. *
  119. * @since 1.0
  120. */
  121. public function displayPage() {
  122. echo '<div id="gpp-wrap" class="wrap">';
  123. screen_icon( 'themes' );
  124. echo '<h2>' . __( 'Theme Options', '_gpp' ) . '</h2>';
  125. echo '<ul class="theme-info">';
  126. echo '<li><a href="' . $this->theme[ 'uri' ] . '" target="_blank">' . $this->theme[ 'name' ] . '</a></li>';
  127. echo '<li>' . __( 'Version: ', '_gpp' ) . $this->theme['version'] . '</li>';
  128. echo '<li><a href="' . $this->theme[ 'support' ] . '"" target="_blank">' . __( 'Support', '_gpp' ) . '</a></li>';
  129. echo '<li><a class="thickbox" href="' . get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php?action=readme&height=600&width=640" title="' . __( 'Theme Instructions', '_gpp' ) . '">' . __( 'Instructions', '_gpp' ) . '</a></li>';
  130. echo '</ul>';
  131. echo '<form action="options.php" method="post" id="gpp_form" enctype="multipart/form-data">';
  132. $this->updateNotice();
  133. settings_fields( '_gpp_options' );
  134. // show tabbed interface
  135. if ( $this->tabbed == true ) {
  136. $tab_html = null;
  137. foreach ( $this->sections as $section_slug => $section ) {
  138. $tab_html .= '<li><a href="#' . $section_slug . '">' . $section . '</a></li>';
  139. }
  140. print '<div id="gpp-tabs" class="ui-tabs">';
  141. print '<ul class="ui-tabs-nav">'.$tab_html.'</ul>';
  142. do_settings_sections( $_GET[ 'page' ] );
  143. print '</div>';
  144. } else { // show one page of options
  145. do_settings_sections( $_GET[ 'page' ] );
  146. }
  147. echo '<p class="submit"><input name="Submit" type="submit" class="button-primary" value="' . __( 'Save Changes', '_gpp' ) . '" /></p>
  148. <p class="reset"><input type="button" class="reset-button reset-handle button-secondary" name="reset" value="' . __( 'Restore Defaults', '_gpp' ) . '" /></p>
  149. </form>';
  150. echo '<script type="text/javascript">
  151. jQuery(document).ready(function($) {
  152. var sections = [];';
  153. foreach ( $this->sections as $section_slug => $section )
  154. echo "sections['$section'] = '$section_slug';";
  155. if ( $this->tabbed == true ) {
  156. echo 'var wrapped = $(".wrap h3").wrap("<div class=\"ui-tabs-panel\">");
  157. wrapped.each(function() {
  158. $(this).parent().append($(this).parent().nextUntil("div.ui-tabs-panel"));
  159. });
  160. $(".ui-tabs-panel").each(function(index) {
  161. $(this).attr("id", sections[$(this).children("h3").text()]);
  162. if (index > 0)
  163. $(this).addClass("ui-tabs-hide");
  164. });
  165. $(".ui-tabs").tabs({
  166. fx: { opacity: "toggle", duration: 40 }
  167. });';
  168. }
  169. echo '
  170. $("input[type=text], textarea").each(function() {
  171. if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "")
  172. $(this).css("color", "#999");
  173. });
  174. $("input[type=text], textarea").focus(function() {
  175. if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "") {
  176. $(this).val("");
  177. $(this).css("color", "#000");
  178. }
  179. }).blur(function() {
  180. if ($(this).val() == "" || $(this).val() == $(this).attr("placeholder")) {
  181. $(this).val($(this).attr("placeholder"));
  182. $(this).css("color", "#999");
  183. }
  184. });
  185. $(".wrap h3, .wrap table").show();
  186. // This will make the "warning" checkbox class really stand out when checked.
  187. // I use it here for the Reset checkbox.
  188. $(".warning").change(function() {
  189. if ($(this).is(":checked"))
  190. $(this).parent().css("background", "#c00").css("color", "#fff").css("fontWeight", "bold");
  191. else
  192. $(this).parent().css("background", "none").css("color", "inherit").css("fontWeight", "normal");
  193. });
  194. // Browser compatibility
  195. if ($.browser.mozilla)
  196. $("form").attr("autocomplete", "off");
  197. // Slider
  198. // calls appendo
  199. $("#slideshow_list").appendo({
  200. allowDelete: false,
  201. labelAdd: "Add Another Slide",
  202. subSelect: "li.slide:last",
  203. onAdd: clear_fields
  204. });
  205. // slide delete button
  206. $("#slideshow_list li.slide").ready(function() {
  207. if($("#slideshow_list li.slide").size() == 1) {
  208. $(".submitdelete").hide();
  209. }
  210. return false;
  211. });
  212. $("#slideshow_list li.slide .remove_slide").live("click", function() {
  213. if($("#slideshow_list li.slide").size() == 1) {
  214. alert("Sorry, you need at least one slide");
  215. }
  216. else {
  217. $(this).parent().slideUp(300, function() {
  218. $(this).remove();
  219. })
  220. }
  221. return false;
  222. });
  223. function clear_fields($row)
  224. {
  225. $row.find("input,textarea").val("");
  226. $row.find(".previewimg").html("");
  227. return true;
  228. };
  229. $("#slideshow_list").sortable();
  230. $("#slides-details-button").click(function(){
  231. $("#slideshow_list li").toggleClass("list-view");
  232. $("#slides-details-button").toggleClass("list");
  233. });
  234. });
  235. </script>
  236. </div>';
  237. }
  238. /**
  239. * Description for section
  240. *
  241. * @since 1.0
  242. */
  243. public function displaySection() {}
  244. /**
  245. * Parse the readme.txt. Shown in thickbox using wp_ajax_readme call.
  246. *
  247. * @uses parseFile()
  248. * @return html parsed from markdown in txt
  249. * @since 1.0
  250. */
  251. public function displayReadme() {
  252. print $this->parseFile( $this->theme[ 'readme' ] );
  253. exit();
  254. }
  255. /**
  256. * HTML output for text field
  257. *
  258. * @since 1.0
  259. */
  260. public function buildOptions( $args = array() ) {
  261. extract( $args );
  262. $theme_options = get_option( '_gpp_options' );
  263. if ( empty( $theme_options ) )
  264. $theme_options[ $id ] = $std;
  265. elseif ( ! isset( $theme_options[ $id ] ) )
  266. $theme_options[ $id ] = 0;
  267. $field_class = '';
  268. if ( $class != '' )
  269. $field_class = ' ' . $class;
  270. switch ( $type ) {
  271. case 'heading':
  272. echo '</td></tr><tr valign="top"><td colspan="2"><h4>' . $desc . '</h4>';
  273. break;
  274. case 'checkbox':
  275. echo '<input class="checkbox' . $field_class . '" type="checkbox" id="' . $id . '" name="_gpp_options[' . $id . ']" value="1" ' . checked( $theme_options[ $id ], 1, false ) . ' /> <label for="' . $id . '">' . $desc . '</label>';
  276. break;
  277. case 'select':
  278. echo '<select class="select' . $field_class . '" name="_gpp_options[' . $id . ']">';
  279. foreach ( $choices as $value => $label )
  280. echo '<option value="' . esc_attr( $value ) . '"' . selected( $theme_options[ $id ], $value, false ) . '>' . $label . '</option>';
  281. echo '</select>';
  282. if ( $desc != '' )
  283. echo '<span class="description">' . $desc . '</span>';
  284. break;
  285. case 'multiselect':
  286. echo '<select class="select' . $field_class . '" name="_gpp_options[' . $id . '][]" multiple="multiple">';
  287. foreach ( $choices as $value => $label ) {
  288. /**
  289. * If the user has saved options use them
  290. * or use what is set in the std value
  291. */
  292. if ( is_array( $theme_options[ $id ] ) && in_array( $value, $theme_options[ $id ] ) ) {
  293. $selected = 'selected="selected"';
  294. } else {
  295. $selected = null;
  296. }
  297. echo '<option value="' . esc_attr( $value ) . '"' . $selected . '>' . $label . '</option>';
  298. }
  299. echo '</select>';
  300. if ( $desc != '' )
  301. echo '<span class="description">' . $desc . '</span>';
  302. break;
  303. case 'multicheck':
  304. $i = 0;
  305. foreach ( $choices as $value => $label ) {
  306. /**
  307. * If the user has saved options use them
  308. * or use what is set in the std value
  309. */
  310. if ( is_array( $theme_options[ $id ] ) && in_array( $value, $theme_options[ $id ] ) ) {
  311. $selected = 'checked="checked"';
  312. } else {
  313. $selected = null;
  314. }
  315. echo '<input class="checkbox' . $field_class . '" type="checkbox" name="_gpp_options[' . $id . '][]" id="' . $id . $i . '" value="' . esc_attr( $value ) . '"' . $selected . '> <label for="' . $id . $i . '">' . $label . '</label>';
  316. echo '<br />';
  317. $i++;
  318. }
  319. if ( $desc != '' )
  320. echo '<span class="description">' . $desc . '</span>';
  321. break;
  322. case 'radio':
  323. $i = 0;
  324. foreach ( $choices as $value => $label ) {
  325. echo '<input class="radio' . $field_class . '" type="radio" name="_gpp_options[' . $id . ']" id="' . $id . $i . '" value="' . esc_attr( $value ) . '" ' . checked( $theme_options[ $id ], $value, false ) . '> <label for="' . $id . $i . '">' . $label . '</label>';
  326. //if ( $i < count( $theme_options ) - 1 )
  327. echo '<br />';
  328. $i++;
  329. }
  330. if ( $desc != '' )
  331. echo '<span class="description">' . $desc . '</span>';
  332. break;
  333. case 'radio_img':
  334. echo '<fieldset>';
  335. $i = 0;
  336. $i++;
  337. foreach( $choices as $k => $v ) {
  338. $selected = ( checked( $theme_options[ $id ], $k, false ) != '' )?' gpp-radio-img-selected':'';
  339. echo '<label class="gpp-radio-img'.$selected.' gpp-radio-img-'.$theme_options[ $id ].'" for="' . $id . '_' . $i . '">';
  340. echo '<input type="radio" id="' . $id . '_' . $i . '" name="_gpp_options[' . $id . ']" '.$field_class.' value="'.$k.'" '.checked( $theme_options[ $id ], $k, false ).'/>';
  341. echo '<img src="'.$v[ 'img' ].'" alt="'.$v[ 'title' ].'" onclick="jQuery:gpp_radio_img_select(\'' . $id . '_' . $i . '\', \''.$theme_options[ $id ].'\');" />';
  342. echo '<br/><span>'.$v[ 'title' ].'</span>';
  343. echo '</label>';
  344. $i++;
  345. }//foreach
  346. echo ( isset( $theme_options[ $desc ] ) && !empty( $theme_options[ $desc ] ) ) ? '<br/><span class="description">'.$theme_options[ $desc ].'</span>':'';
  347. echo '</fieldset>';
  348. break;
  349. case 'textarea':
  350. echo '<textarea class="' . $field_class . '" id="' . $id . '" name="_gpp_options[' . $id . ']" placeholder="' . $std . '" rows="5" cols="30">' . esc_textarea( $theme_options[ $id ] ) . '</textarea>';
  351. if ( $desc != '' )
  352. echo '<span class="description">' . $desc . '</span>';
  353. break;
  354. case 'password':
  355. echo '<input class="regular-text' . $field_class . '" type="password" id="' . $id . '" name="_gpp_options[' . $id . ']" value="' . esc_attr( $theme_options[ $id ] ) . '" autocomplete="off" />';
  356. if ( $desc != '' )
  357. echo '<span class="description">' . $desc . '</span>';
  358. break;
  359. case 'upload':
  360. echo '<input id="' . $id . '" class="upload-url' . $field_class . '" type="hidden" name="_gpp_options[' . $id . ']" value="' . esc_attr( $theme_options[ $id ] ) . '" /><div class="imgpreview">';
  361. if( $theme_options[ $id ] != '' ) {
  362. echo '<img class="gpp-screenshot" id="gpp-screenshot-'.$id.'" src="' . esc_attr( $theme_options[ $id ] ) . '" />';
  363. }
  364. echo "</div>";
  365. if ( $theme_options[ $id ] == '' ) {
  366. $remove = ' style="display:none;"';
  367. $upload = '';
  368. } else {
  369. $remove = '';
  370. $upload = ' style="display:none;"';
  371. }
  372. echo '<a href="javascript:void(0);" class="upload_button button-secondary"' . $upload . ' rel-id="' . $id . '">' . __('Browse', '_gpp') . '</a>';
  373. echo '<a href="javascript:void(0);" class="upload_button_remove"' . $remove . ' rel-id="' . $id . '">' . __('Remove Upload', '_gpp') . '</a>';
  374. if ( $desc != '' )
  375. echo '<span class="description">' . $desc . '</span>';
  376. break;
  377. case 'html':
  378. echo $html;
  379. break;
  380. case 'color':
  381. echo '<div class="farb-popup-wrapper">';
  382. echo '<input class="popup-colorpicker' . $field_class . '" type="text" id="' . $id . '" name="_gpp_options[' . $id . ']" placeholder="' . $std . '" value="' . esc_attr( $theme_options[ $id ] ) . '" />
  383. <div class="farb-popup"><div class="farb-popup-inside"><div id="' . $id . 'picker" class="color-picker"></div></div>';
  384. echo '</div>';
  385. break;
  386. case 'slide':
  387. if ( $desc != '' )
  388. echo '<span class="description slide-desc' . $field_class . '">' . $desc . '</span>';
  389. echo '<br /><span id="slides-details-button"></span>';
  390. echo '<ul id="slideshow_list">';
  391. if ( $theme_options[ $id ] <> '' ) {
  392. $slides = array();
  393. foreach ( $theme_options[ $id ][ 'title' ] as $k => $v ) {
  394. $slides[] = array(
  395. 'title' => $v,
  396. 'link' => $theme_options[ $id ][ 'link' ][ $k ],
  397. 'caption' => $theme_options[ $id ][ 'caption' ][ $k ],
  398. 'image' => $theme_options[ $id ][ 'image' ][ $k ]
  399. );
  400. }
  401. $i = 1;
  402. foreach ( $slides as $slide ) {
  403. echo '<li class="slide">';
  404. echo '<span class="description">' . __( 'Slide Title', '_gpp' ) . '</span>';
  405. echo '<input class="regular-text' . $field_class . '" name="_gpp_options[' . $id . '][title][]" placeholder="' . $std . '" id="' . $id . '_title_' . $i . '" value="' . $slide[ 'title' ] . '" type="text" />';
  406. echo '<span class="description">' . __( 'Slide Link', '_gpp' ) . '</span>';
  407. echo '<input class="regular-text' . $field_class . '" name="_gpp_options[' . $id . '][link][]" placeholder="' . $std . '" id="' . $id . '_title_' . $i . '" value="' . $slide[ 'link' ] . '" type="text" />';
  408. echo '<span class="description">' . __( 'Slide Caption', '_gpp' ) . '</span>';
  409. echo '<textarea class="' . $field_class . '" name="_gpp_options[' . $id . '][caption][]" id="' . $id . '_caption_' . $i . '" cols="40" rows="4">' . $slide[ 'caption' ] . '</textarea>';
  410. echo '<span class="description">' . __( 'Slide Image', '_gpp' ) . '</span>';
  411. echo '<input class="upload-input-text src" name="_gpp_options[' . $id . '][image][]" id="' . $id . '_image_' . $i . '" type="text" value="'. $slide['image'] . '" type="text" />
  412. <a href="' . get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php?action=choice&width=150&height=100" id="' . $id . '_button" class="button upbutton">' . __( 'Upload', '_gpp' ) . '</a>';
  413. echo '<div class="clear"></div><div class="previewimg">';
  414. if ( $slide[ 'image' ] != "" )
  415. {
  416. echo '<img class="uploadedimg" id="image_' . $id . '_image_' . $i . '" src="' . $slide[ 'image' ] . '" />';
  417. }
  418. echo '</div>';
  419. echo '<a class="remove_slide submitdelete">' . __( 'Delete Slide', '_gpp' ) . '</a>';
  420. echo '</li>';
  421. $i++;
  422. }
  423. } else {
  424. $i = 1;
  425. echo '<li class="slide">';
  426. echo '<span class="description">' . __( 'Slide Title', '_gpp' ) . '</span>';
  427. echo '<input class="regular-text' . $field_class . '" name="_gpp_options[' . $id . '][title][]" placeholder="' . $std . '" id="'. $id .'_title_' . $i . '" value="" type="text" />';
  428. echo '<span class="description">' . __( 'Slide Link', '_gpp' ) . '</span>';
  429. echo '<input class="regular-text' . $field_class . '" name="_gpp_options[' . $id . '][link][]" placeholder="' . $std . '" id="'. $id .'_title_' . $i . '" value="" type="text" />';
  430. echo '<span class="description">' . __( 'Slide Caption', '_gpp' ) . '</span>';
  431. echo '<textarea class="' . $field_class . '" name="_gpp_options[' . $id . '][caption][]" id="' . $id . '_caption_' . $i . '" cols="40" rows="4"></textarea>';
  432. echo '<span class="description">' . __( 'Slide Image', '_gpp' ) . '</span>';
  433. echo '<input class="upload-input-text src" name="_gpp_options[' . $id . '][image][]" id="' . $id . '_image_' . $i . '" type="text" value="" type="text" />
  434. <a href="' . get_option('siteurl') . '/wp-admin/admin-ajax.php?action=choice&width=150&height=100" id="' . $id . '_button" class="button upbutton">' . __( 'Upload', '_gpp' ) . '</a>';
  435. echo '<div class="clear"></div><div class="previewimg">';
  436. echo '</div>';
  437. echo '<a class="remove_slide submitdelete">' . __( 'Delete Slide', '_gpp' ) . '</a>';
  438. echo '</li>';
  439. }
  440. echo '</ul>';
  441. break;
  442. case 'text':
  443. default:
  444. echo '<input class="regular-text' . $field_class . '" type="text" id="' . $id . '" name="_gpp_options[' . $id . ']" placeholder="' . $std . '" value="' . esc_attr( $theme_options[ $id ] ) . '" autocomplete="off" />';
  445. if ( $desc != '' )
  446. echo '<span class="description">' . $desc . '</span>';
  447. break;
  448. case 'file':
  449. echo '<input type="file" name="_gpp_options_file" />';
  450. echo '<input type="submit" name="filesubmit" class="button" value="Upload">';
  451. echo '<br /><br />';
  452. if ( isset( $theme_options[ '_gpp_options_file' ][ 'attachment_id' ] ) ){
  453. echo wp_get_attachment_image( $theme_options[ '_gpp_options_file' ][ 'attachment_id' ], 'medium' );
  454. echo '<br /><br /><input type="submit" name="remove_file_upload" id="remove_file_upload" class="button" value="Remove Image">';
  455. }
  456. break;
  457. }
  458. }
  459. /**
  460. * Settings and defaults
  461. *
  462. * @since 1.0
  463. */
  464. public function getSettings() {
  465. // extend this class to create theme options. See config.php
  466. }
  467. /**
  468. * Initialize settings to their default values
  469. *
  470. * @since 1.0
  471. */
  472. public function initializeSettings( $settings=null ) {
  473. if ( is_null( $settings ) )
  474. $settings = $this->settings;
  475. $default_settings = array();
  476. foreach ( $settings as $id => $setting ) {
  477. if ( $setting[ 'type' ] != 'heading' )
  478. $default_settings[ $id ] = $setting[ 'std' ];
  479. }
  480. update_option( '_gpp_options', $default_settings );
  481. }
  482. /**
  483. * Register settings
  484. *
  485. * @since 1.0
  486. */
  487. public function registerSettings() {
  488. register_setting( '_gpp_options', '_gpp_options', array ( &$this, 'formHandler' ) );
  489. foreach ( $this->sections as $slug => $title ) {
  490. add_settings_section( $slug, $title, array( &$this, 'displaySection' ), 'gpp-options' );
  491. }
  492. $this->getSettings();
  493. foreach ( $this->settings as $id => $setting ) {
  494. $setting[ 'id' ] = $id;
  495. $this->createSetting( $setting );
  496. }
  497. }
  498. /**
  499. * jQuery Tabs
  500. *
  501. * @since 1.0
  502. */
  503. public function addScripts() {
  504. if ( $this->tabbed == true )
  505. wp_print_scripts( 'jquery-ui-tabs' );
  506. wp_enqueue_script( 'media-upload' );
  507. wp_enqueue_script( 'thickbox' );
  508. wp_enqueue_script( 'farbtastic' );
  509. wp_register_script( 'gpp-options-scripts', get_template_directory_uri() . '/lib/theme-options/js/options.js', array( 'jquery', 'media-upload','thickbox', 'farbtastic' ) );
  510. wp_enqueue_script( 'gpp-options-scripts' );
  511. wp_register_script( 'appendo', get_template_directory_uri() . '/lib/theme-options/js/jquery.appendo.js', array( 'jquery' ) );
  512. wp_enqueue_script( 'appendo' );
  513. wp_enqueue_script( 'jquery-ui-sortable' );
  514. }
  515. /**
  516. * Styling for the theme options page
  517. *
  518. * @since 1.0
  519. */
  520. public function addStyles() {
  521. wp_register_style( 'gpp-options-styles', get_template_directory_uri() . '/lib/theme-options/css/options.css' );
  522. if ( $this->tabbed == true )
  523. wp_enqueue_style( 'gpp-options-styles' );
  524. wp_enqueue_style( 'thickbox' );
  525. wp_enqueue_style( 'farbtastic' );
  526. }
  527. /**
  528. * Form Handler
  529. *
  530. * @since 1.0
  531. */
  532. public function formHandler( $input ) {
  533. /* if ( ! empty( $_POST['reset'] ) ) {
  534. //$input = $this->resetSettings( $input );
  535. $input = "";
  536. return $input;
  537. } */
  538. if ( ! empty( $_POST[ 'reset' ] ) ) {
  539. $defaults = array();
  540. foreach ( $this->settings as $id => $setting ) {
  541. if ( $setting[ 'type' ] != 'heading' ) {
  542. $defaults[ $id ] = $setting[ 'std' ];
  543. }
  544. }
  545. return $defaults;
  546. }
  547. if ( ! isset( $input[ 'reset_theme' ] ) ) {
  548. $theme_options = get_option( '_gpp_options' );
  549. if ( $_FILES[ '_gpp_options_file' ][ 'size' ] > 0 ) {
  550. // die('we have file');
  551. $media = new GPPMediaUpload;
  552. $input[ '_gpp_options_file' ] = $media->saveUpload( $field_name='_gpp_options_file' );
  553. } else {
  554. // die('we have NO file');
  555. $input[ '_gpp_options_file' ] = $theme_options[ '_gpp_options_file' ];
  556. }
  557. if ( isset( $_POST[ 'remove_file_upload' ] ) ) {
  558. unset( $input[ '_gpp_options_file' ] );
  559. }
  560. foreach ( $this->checkboxes as $id ) {
  561. if ( isset( $theme_options[ $id ] ) && ! isset( $input[ $id ] ) )
  562. unset( $theme_options[ $id ] );
  563. }
  564. return $input;
  565. }
  566. // Create our array for storing the validated options
  567. $output = array();
  568. // Loop through each of the incoming options
  569. foreach( $input as $key => $value ) {
  570. // Check to see if the current option has a value. If so, process it.
  571. if( isset( $input[ $key ] ) ) {
  572. // Strip all HTML and PHP tags and properly handle quoted strings
  573. $output[ $key ] = wp_filter_nohtml_kses( $input[ $key ] );
  574. } // end if
  575. } // end foreach
  576. // Return the array processing any additional functions filtered by this action
  577. return apply_filters( 'formHandler', $output, $input );
  578. }
  579. /**
  580. * Do reset setting stuff here
  581. */
  582. public function resetSettings( $inputs = array() ) {
  583. $defaults = array();
  584. foreach( $inputs as $key => $value ) {
  585. $defaults[ $key ] = '';
  586. }
  587. return $defaults;
  588. }
  589. /**
  590. * Format categories for $choices array.
  591. *
  592. * @uses get_terms();
  593. * @return $terms['value']['label']
  594. * @todo check if taxonomy exists
  595. */
  596. public function getCategories( $taxonomy = null, $firstblank = false ) {
  597. if ( is_null( $taxonomy ) )
  598. $taxonomy = 'category';
  599. $args = array(
  600. 'hide_empty' => 0
  601. );
  602. $terms_obj = get_terms( $taxonomy, $args );
  603. $items = array();
  604. if( $firstblank ) {
  605. $terms[''] = '-- Choose One --';
  606. }
  607. foreach ( $terms_obj as $tt ) {
  608. $terms[ $tt->term_id ] = $tt->name;
  609. }
  610. return $terms;
  611. }
  612. /**
  613. * Format Pages for $choices array.
  614. *
  615. * @uses get_pages
  616. * @param $value, the value to be used in the option value=""
  617. * default is ID, any index can be used as a value.
  618. * see codex: http://codex.wordpress.org/Function_Reference/get_pages
  619. */
  620. public function getPages( $value = null, $firstblank = false ) {
  621. if ( is_null( $value ) )
  622. $value = 'ID';
  623. $args = array( 'post_type' => 'page' );
  624. $obj = get_pages( $args );
  625. $items = array();
  626. if( $firstblank ) {
  627. $terms[''] = '-- Choose One --';
  628. }
  629. foreach ( $obj as $item ) {
  630. $items[ $item->$value ] = $item->post_title;
  631. }
  632. return $items;
  633. }
  634. /**
  635. * Parse Readme.txt
  636. *
  637. * @param string $url URL of textfile readme.txt
  638. * @return string readme code
  639. */
  640. public function parseFile( $url=null ) {
  641. // no/wrong url
  642. if ( empty( $url ) or basename( $url ) != 'readme.txt')
  643. return false;
  644. $response = wp_remote_get( $url );
  645. if( is_wp_error( $response ) ) {
  646. echo 'Unable to load the instructions.';
  647. } else {
  648. $readme = $response[ 'body' ];
  649. }
  650. // make links clickable
  651. $readme = make_clickable( nl2br( esc_html( $readme ) ) );
  652. // code, strong, em
  653. $readme = preg_replace( '/`(.*?)`/', '<code>\\1</code>', $readme );
  654. $readme = preg_replace( '/[\040]\*\*(.*?)\*\*/', ' <strong>\\1</strong>', $readme );
  655. $readme = preg_replace( '/[\040]\*(.*?)\*/', ' <em>\\1</em>', $readme );
  656. // headings
  657. $readme = preg_replace( '/=== (.*?) ===/', '<h2>\\1</h2>', $readme );
  658. $readme = preg_replace( '/== (.*?) ==/', '<h3>\\1</h3>', $readme );
  659. $readme = preg_replace( '/= (.*?) =/', '<h4>\\1</h4>', $readme );
  660. // links
  661. $readme = preg_replace( '#(^|[\[]{1}[\s]*)([^\n<>^\)]+)([\]]{1}[\(]{1}[\s]*)(http://|ftp://|mailto:|https://)([^\s<>]+)([\s]*[\)]|$)#', '<a href="$4$5">$2</a>', $readme );
  662. $readme = preg_replace( '#(^|[^\"=]{1})(http://|ftp://|mailto:|https://)([^\s<>]+)([\s\n<>]|$)#', '$1<a href="$2$3">$2$3</a>$4', $readme );
  663. return $readme;
  664. }
  665. } // end class
  666. function gpp_option( $option ) {
  667. $theme_options = get_option( '_gpp_options' );
  668. if ( isset( $theme_options[ $option ] ) )
  669. return $theme_options[ $option ];
  670. else
  671. return false;
  672. }
  673. /**
  674. * Permissions NOT handled here!
  675. *
  676. * A series of related methods for managing file uploads within
  677. * WordPress.
  678. *
  679. * @author Zane M. Kolnik zanematthew[at]gmail[dot]com
  680. */
  681. Class GPPMediaUpload {
  682. public $upload_dir;
  683. private $attachment_id;
  684. public function __construct() {
  685. $this->upload_dir = wp_upload_dir();
  686. }
  687. /**
  688. * Handles the saving, i.e. creates a post type of attachment.
  689. *
  690. * During form submission run the method:
  691. * $class->fileUpload( $field_name='form_field_name' );
  692. *
  693. * @return $final_file An array of array of f*cking cool stuff
  694. * I guess if you think arrays are cool i like (*)(*)s
  695. * $final_file['attachment_id'] = $this->attachment_id;
  696. * $final_file['file'] = $uploaded_file['file'];
  697. * $final_file['file_info'] = $file_info[];
  698. */
  699. public function saveUpload( $field_name = null, $user_id = null ) {
  700. if ( is_null( $field_name ) )
  701. die( 'Need field_name' );
  702. // Move the file to the uploads directory, returns an array
  703. // of information from $_FILES
  704. $uploaded_file = $this->handleUpload( $_FILES[ $field_name ] );
  705. if ( ! isset( $uploaded_file[ 'file' ] ) )
  706. return false;
  707. // If we were to have a unique user account for uploading
  708. if ( is_null( $user_id ) ) {
  709. $current_user = wp_get_current_user();
  710. $user_id = $current_user->ID;
  711. }
  712. // Build the Global Unique Identifier
  713. $guid = $this->buildGuid( $uploaded_file[ 'file' ] );
  714. // Build our array of data to be inserted as a post
  715. $attachment = array(
  716. 'post_mime_type' => $_FILES[ $field_name ][ 'type' ],
  717. 'guid' => $guid,
  718. 'post_title' => $this->mediaTitle( $uploaded_file[ 'file' ] ),
  719. 'post_content' => '',
  720. 'post_author' => $user_id,
  721. 'post_status' => 'inherit',
  722. 'post_date' => date( 'Y-m-d H:i:s' ),
  723. 'post_date_gmt' => date( 'Y-m-d H:i:s' )
  724. );
  725. // Add the file to the media library and generate thumbnail.
  726. $this->attachment_id = wp_insert_attachment( $attachment, $uploaded_file[ 'file' ] );
  727. // @todo bug, this does NOT work when used in a PLUGIN!, so you'll have to make
  728. // your OWN thumbnail sizes!
  729. require_once( ABSPATH . 'wp-admin' . '/includes/image.php' );
  730. $meta = wp_generate_attachment_metadata( $this->attachment_id, $uploaded_file[ 'file' ] );
  731. $file_info = pathinfo( $uploaded_file[ 'file' ] );
  732. // Set the feedback flag to false, since the upload was successful
  733. $upload_feedback = false;
  734. $final_file = array();
  735. $final_file[ 'attachment_id' ] = $this->attachment_id;
  736. $final_file[ 'file' ] = $uploaded_file[ 'file' ];
  737. $final_file[ 'file_info' ] = $file_info;
  738. return $final_file;
  739. }
  740. /**
  741. * Do some set-up before calling the wp_handle_upload function
  742. */
  743. public function handleUpload( $file = array() ) {
  744. require_once( ABSPATH . 'wp-admin' . '/includes/file.php' );
  745. return wp_handle_upload( $file, array( 'test_form' => false ), date( 'Y/m' ) );
  746. }
  747. /**
  748. * Builds the GUID for a given file from the media library
  749. * @param full/path/to/file.jpg
  750. * @return guid
  751. */
  752. public function buildGuid( $file = null ) {
  753. // $wp_upload_dir = wp_upload_dir();
  754. return $this->upload_dir[ 'baseurl' ] . '/' . _wp_relative_upload_path( $file );
  755. }
  756. /**
  757. * Parse the title of the media based on the file name
  758. * @return title
  759. */
  760. public function mediaTitle( $file ) {
  761. return addslashes( preg_replace('/\.[^.]+$/', '', basename( $file ) ) );
  762. }
  763. } // End 'MediaUpload'
  764. ?>