/lib/modules/genesis-custom-backgrounds/lib/admin/gcb-settings.php

https://github.com/Genesis-Simple/Genesis-Simple-Plugin · PHP · 194 lines · 117 code · 27 blank · 50 comment · 3 complexity · d20431a66c247a6b00c610fb6b07d275 MD5 · raw file

  1. <?php
  2. /**
  3. * This returns an array of directory names.
  4. *
  5. * @author Travis Smith
  6. */
  7. function gcb_dir_list( $d ){
  8. foreach( array_diff( scandir( $d ) , array( '.' , '..' , '.DS_Store' ) ) as $f ) {
  9. if( is_dir( $d . '/' . $f ) ) {
  10. $l[]=$f;
  11. }
  12. }
  13. return $l;
  14. }
  15. /**
  16. * This returns an array of file names.
  17. *
  18. * @author Travis Smith
  19. */
  20. function gcb_file_list( $d ){
  21. foreach( array_diff( scandir( $d ) , array( '.' , '..' , '.DS_Store' ) ) as $f ) {
  22. $l[]=$f;
  23. }
  24. return $l;
  25. }
  26. /**
  27. * This is a necessary go-between to get our scripts and boxes loaded
  28. * on the theme settings page only, and not the rest of the admin.
  29. *
  30. * @author Travis Smith
  31. * @global string $_gcb_settings_pagehook value set in this function
  32. */
  33. add_action('admin_menu', 'gcb_settings_init');
  34. function gcb_settings_init() {
  35. global $_gcb_settings_pagehook;
  36. // Add "Genesis Backgrounds" submenu
  37. $_gcb_settings_pagehook = add_submenu_page( 'genesis' , __( 'Genesis Backgrounds' , GCB_DOMAIN ), __( 'Genesis Backgrounds' , GCB_DOMAIN ) , 'manage_options' , 'gcb-settings' , 'gcb_settings_admin' );
  38. add_action('load-'.$_gcb_settings_pagehook, 'gcb_settings_boxes' );
  39. }
  40. /**
  41. * This function is what actually gets output to the page. It handles the markup,
  42. * builds the form, fires do_meta_boxes().
  43. *
  44. * @author Travis Smith
  45. * @global string $_gcb_settings_pagehook
  46. * @global integer $screen_layout_columns
  47. * @version 1.0
  48. */
  49. function gcb_settings_admin() {
  50. global $_gcb_settings_pagehook, $screen_layout_columns;
  51. $screen_layout_columns = 1;
  52. $width = "width: 99%;";
  53. $hide2 = $hide3 = " display: none;";
  54. ?>
  55. <div id="gcb-settings" class="wrap gcb-metaboxes">
  56. <form method="post" action="options.php">
  57. <?php wp_nonce_field( 'closedpostboxes' , 'closedpostboxesnonce' , false ); ?>
  58. <?php wp_nonce_field( 'meta-box-order' , 'meta-box-order-nonce' , false ); ?>
  59. <?php settings_fields( GCB_SETTINGS_FIELD ); // important! ?>
  60. <?php screen_icon( 'plugins' ); ?>
  61. <h2 id="top-buttons">
  62. <?php _e('Genesis Background Settings', GCB_DOMAIN ); ?>
  63. <input type="submit" class="button-primary" value="<?php _e('Save Settings', GCB_DOMAIN ) ?>" />
  64. </h2>
  65. <div class="metabox-holder">
  66. <div class="postbox-container" style="<?php echo $width; ?>">
  67. <?php do_meta_boxes( $_gcb_settings_pagehook , 'column1' , null ); ?>
  68. </div>
  69. </div>
  70. <div class="bottom-buttons">
  71. <input type="submit" class="button-primary" value="<?php _e( 'Save Settings' , GCB_DOMAIN ) ?>" />
  72. </div>
  73. <div class="clear" style="margin-top:50px;"></div>
  74. </form>
  75. </div>
  76. <?php
  77. }
  78. /**
  79. * Tell WordPress that we want only 1 column available for our meta-boxes.
  80. *
  81. * @author Travis Smith
  82. * @global string $_gcb_settings_pagehook
  83. * @param array $columns WordPress array containing column settings for each page.
  84. * @param string $screen Name of the design settings page.
  85. * @return array
  86. */
  87. add_filter( 'screen_layout_columns', 'gcb_settings_layout_columns', 10, 2 );
  88. function gcb_settings_layout_columns($columns, $screen) {
  89. global $_gcb_settings_pagehook;
  90. if ( $screen == $_gcb_settings_pagehook ) {
  91. $columns[$_gcb_settings_pagehook] = 1;
  92. }
  93. return $columns;
  94. }
  95. /**
  96. * Adds all the settngs boxes to the design settings page.
  97. *
  98. * @author Travis Smith
  99. * @global string $_gcb_settings_pagehook
  100. * @version 1.0
  101. */
  102. function gcb_settings_boxes() {
  103. global $_gcb_settings_pagehook;
  104. add_meta_box( 'gcb-settings' , __( 'Genesis Backgrounds' , GCB_DOMAIN ) , 'gcb_settings' , $_gcb_settings_pagehook , 'column1' );
  105. }
  106. /**
  107. * Add settings to the Genesis Backgrounds metabox.
  108. *
  109. * @author Travis Smith
  110. * @version 1.0
  111. */
  112. function gcb_settings() {
  113. $setting = genesis_get_option( 'cb_default' , GCB_SETTINGS_FIELD );
  114. $bg_styles = ' background-image: url(\'' . gcb_setting_url( $setting ) . '\');'
  115. . ' background-repeat: repeat;'
  116. . ' background-position: top left;'
  117. . ' width: 100%;'
  118. . ' float: left;'
  119. . ' margin: 0 10px 10px 0;';
  120. ?>
  121. <h4><?php _e( 'Selected Default Background' , GCB_DOMAIN ); ?></h4>
  122. <div id="custom-background-image" style="<?php echo $bg_styles; ?>"><?php // must be double quote, see above ?>
  123. <p style="visibility:hidden;"><?php _e( 'default' , GCB_DOMAIN ); ?></p>
  124. <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" />
  125. </div>
  126. <div class="clear"></div>
  127. <?php
  128. $imagefolders = gcb_dir_list( GCB_BG_DIR );
  129. $lights = gcb_dir_list( GCB_LIGHT_URL );
  130. foreach( $lights as $key => $folder ) {
  131. $light[$folder] = gcb_file_list( GCB_BG_DIR . 'light/' . $folder );
  132. }
  133. $darks = gcb_dir_list( GCB_DARK_URL );
  134. foreach( $darks as $folder ) {
  135. $dark[$folder] = gcb_file_list( GCB_BG_DIR . 'dark/' . $folder );
  136. }
  137. foreach ( $imagefolders as $key => $folder) : ?>
  138. <h2><?php _e( ucfirst( $folder ) . ' Backgrounds' , GCB_DOMAIN ); ?></h2>
  139. <?php
  140. foreach ( ${$folder} as $fkey => $types ) : ?>
  141. <h4><?php _e( ucfirst( $fkey ) . ' Backgrounds' , GCB_DOMAIN ); ?></h4>
  142. <?php
  143. $i = 0;
  144. foreach ( ${$folder}[$fkey] as $ikey => $img ) :
  145. $i++;
  146. $bg_styles = '';
  147. // background-image URL must be single quote, see below
  148. $bg_styles .= ' background-image: url(\'' . GCB_BG_URL . $folder . '/' . $fkey . '/' . $img . '\');'
  149. . ' background-repeat: repeat;'
  150. . ' background-position: top left;'
  151. . ' width: 48%;'
  152. . ' float: left;'
  153. . ' margin: 0 10px 10px 0;';
  154. ?>
  155. <div id="custom-background-image" style="<?php echo $bg_styles; ?>"><?php // must be double quote, see above ?>
  156. <input type="radio" class="cbi-option" id="<?php echo GCB_SETTINGS_FIELD; ?>[cb_default]" name="<?php echo GCB_SETTINGS_FIELD; ?>[cb_default]"
  157. value="<?php echo $folder .'-' . $fkey . '-' . $i; ?>" <?php checked( $folder .'-' . $fkey . '-' . $i , $setting ); ?>>
  158. <p style="visibility:hidden;"><?php _e( ucfirst( $folder ) . ' ' . ucfirst( $fkey ) , GCB_DOMAIN ); ?></p>
  159. </input>
  160. <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" />
  161. </div>
  162. <?php
  163. endforeach;
  164. ?><div class="clear"></div><?php
  165. endforeach;
  166. endforeach;
  167. }