Advertisement
Guest User

Untitled

a guest
Sep 28th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. /* Copyright 2011 jacobbrett
  2.    License: GPLv2
  3. */
  4.  
  5. <?php
  6. class bandcamp_integration {
  7.     function bandcamp_integration() {
  8.         // Add admin options page
  9.         add_action( 'admin_menu', '$this->admin_menu' );
  10.         // Add admin settings
  11.         add_action( 'admin_init', '$this->admin_init' );
  12.     }
  13.  
  14.     // Add admin options page
  15.     function admin_menu() {
  16.         add_options_page( 'BandCamp Integration', 'BandCamp', 'manage_options', 'bandcamp-integration', 'options_page' );
  17.     }
  18.  
  19.     // Display admin options page
  20.     function options_page() {
  21. ?>
  22. <div class="wrap">
  23.     <h2>BandCamp Integration</h2>
  24.     <p>Options relating to the Custom Plugin.</p>
  25.     <form method="post" action="options.php">
  26.         <?php settings_fields('options'); ?>
  27.         <?php do_settings_sections('bandcamp_integration'); ?>
  28.         <p class="submit">
  29.             <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
  30.         </p>
  31.     </form>
  32. </div>
  33. <?php
  34.     }
  35.  
  36.     // Add admin settings
  37.     function admin_init() {
  38.         register_setting( 'options', 'options', 'options_validate' );
  39.         add_settings_section( 'main', 'Main Settings', 'main_callback', 'bandcamp_integration' );
  40.         add_settings_field( 'api_key', 'BandCamp API Key', 'api_key_callback', 'bandcamp_integration', 'main' );
  41.         add_settings_field( 'band_id', 'BandCamp Band ID', 'band_id_callback', 'bandcamp_integration', 'main' );
  42.         add_settings_field( 'sync', 'Synchronise Data', 'sync_callback', 'bandcamp_integration', 'main' );
  43.     }
  44.  
  45.     // Main Settings description
  46.     function main_callback() {
  47.         echo '<p>Main description of this section here.</p>';
  48.     }
  49.  
  50.     // BandCamp API Key description
  51.     function api_key_callback() {
  52.         $options = get_option( 'options' );
  53.         echo "<input id='bandcamp_api_key' name='options[api_key]' size='40' type='text' value='{$options['api_key']}' />";
  54.         echo "<span class='description'>You may <a href='http://bandcamp.com/developer#key_request' target='_blank'>request an API key</a> if you do not have one.</span>";
  55.     }
  56.  
  57.     // BandCamp Band ID description
  58.     function band_id_callback() {
  59.         $options = get_option( 'options' );
  60.         echo "<input id='bandcamp_band_id' name='options[band_id]' size='40' type='text' value='{$options['band_id']}' />";
  61.         echo '<span class="description">You may find your Band ID by <a href="http://api.bandcamp.com/api/band/3/search?key=<key>&name=band%20name&debug" target="_blank">querying BandCamp</a>.</span>';
  62.     }
  63.  
  64.     // Synchronise Data description
  65.     function sync_callback() {
  66.         $options = get_option( 'options' );
  67.         echo '<button type="button">Synchronise Data</button>';
  68.         echo '<span class="description">Update local discography with data from BandCamp (unimplemented).</span>';
  69.     }
  70.  
  71.     // Validate options
  72.     function options_validate( $input ) {
  73.         $options = get_option( 'options' );
  74.         $options['api_key'] = trim( $input['api_key'] );
  75.         if ( ! preg_match( '/^[a-z]{29}$/i', $options['api_key'] ) )
  76.             $options['api_key'] = '';
  77.  
  78.         $options['band_id'] = trim( $input['band_id'] );
  79.         if ( ! preg_match( '/^[0-9]{10}$/i', $options['band_id'] ) )
  80.             $options['band_id'] = '';
  81.  
  82.         return $options;
  83.     }
  84. }
  85. ?>
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement