1. <?php
  2.  
  3. /*
  4. Written by Chris Jean for iThemes.com
  5. Licensed under GPLv2
  6.  
  7. Version 1.0.5
  8.  
  9. */
  10.  
  11. if ( ! class_exists( 'ITTabber' ) ) {
  12.     class ITTabber extends ITCoreClass {
  13.         var $_var = 'tabber_widget';
  14.         var $_page_title = 'Manage Tabber Widgets';
  15.         var $_page_var = 'tabber-widgets';
  16.         var $_menu_title = 'Tabber Widgets';
  17.         var $_default_menu_function = 'add_management_page';
  18.         var $_menu_priority = '15';
  19.        
  20.         var $_global_storage = true;
  21.        
  22.        
  23.         function ITTabber() {
  24.             $this->ITCoreClass();
  25.            
  26.             $this->_file = __FILE__;
  27.            
  28.             add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_public_scripts' ) );
  29.         }
  30.        
  31.         function init() {
  32.             ITCoreClass::init();
  33.            
  34.             if ( ! isset( $this->_options['tabbers'] ) )
  35.                 $this->_options['tabbers'] = array();
  36.            
  37.             foreach ( (array) $this->_options['tabbers'] as $id => $tabber ) {
  38.                 for ( $counter = 1; $counter <= $tabber['num']; $counter++ ) {
  39.                     register_sidebar( array( 'name' => "Tabber - {$tabber['name']} - {$tabber["title_$counter"]}", 'before_widget' => '<div class="tabber-widget %2$s" id="%1$s">','after_widget' => '</div>', 'before_title' => '<h5 class="tabber-widget-title">', 'after_title' => '</h5>' ) );
  40.                 }
  41.             }
  42.            
  43.             $this->_templates_directory = dirname( __FILE__ ) . '/templates';
  44.            
  45.             if ( is_ssl() )
  46.                 $this->_plugin_url = preg_replace( '/^http:/', 'https:', $this->_plugin_url );
  47.         }
  48.        
  49.         function contextual_help( $text, $screen ) {
  50. /*          $text = "<h5>Get help with \"Layout Editor\"</h5>\n";
  51.             $text .= "The Layout Editor allows you to create an unlimited variety of layout options for your site.";*/
  52.            
  53.             return $text;
  54.         }
  55.        
  56.         function enqueue_public_scripts() {
  57.             $styles = array();
  58.            
  59.             foreach ( (array) $this->_options['tabbers'] as $tabber )
  60.                 $styles[$tabber['template_class']] = $tabber['template_file'];
  61.            
  62.             foreach ( (array) $styles as $class => $file )
  63.                 wp_enqueue_style( "{$this->_var}-$class", "{$this->_plugin_url}/templates/$file" );
  64.         }
  65.        
  66.         function add_admin_scripts() {
  67.             ITCoreClass::add_admin_scripts();
  68.            
  69.             wp_enqueue_script( 'jquery' );
  70.             wp_enqueue_script( "{$this->_var}-script", "{$this->_plugin_url}/js/editor.js" );
  71.             wp_enqueue_script( "{$this->_var}-idtabs", "{$this->_plugin_url}/js/jquery.idTabs.min.js" );
  72.         }
  73.        
  74.         function add_admin_styles() {
  75.             ITCoreClass::add_admin_styles();
  76.            
  77. /*          wp_enqueue_style( "{$this->_var}-theme-options", "{$this->_plugin_url}/css/layout-editor.css" );*/
  78.         }
  79.        
  80.        
  81.         // Pages //////////////////////////////////////
  82.        
  83.         function index() {
  84.             ITCoreClass::index();
  85.            
  86.             if ( ! empty( $_REQUEST['save'] ) )
  87.                 $this->_save_tabber();
  88.             else if ( isset( $_REQUEST['delete'] ) )
  89.                 $this->_delete_tabber();
  90.             else
  91.                 $this->_list_tabbers();
  92.         }
  93.        
  94.         function _save_tabber() {
  95.             $tabber = ITForm::get_post_data();
  96.            
  97.            
  98.             foreach ( (array) $this->_options['tabbers'] as $cur_id => $cur_tabber )
  99.                 if ( ( $tabber['name'] === $cur_tabber['name'] ) && ( $_POST['id'] != $cur_id ) )
  100.                     $this->_errors[] = 'A Tabber with that Name already exists. Please choose a unique Name.';
  101.            
  102.             if ( empty( $tabber['name'] ) )
  103.                 $this->_errors[] = "You must supply a Name";
  104.             if ( empty( $tabber['num'] ) )
  105.                 $this->_errors[] = "You must supply the Number of Widgets";
  106.             if ( ! is_file( "{$this->_templates_directory}/{$tabber['template_file']}" ) )
  107.                 $this->_errors[] = "The selected Template was unable to be read. Please choose another Template.";
  108.            
  109.             if ( is_numeric( $tabber['num'] ) ) {
  110.                 for ( $counter = 1; $counter <= 11; $counter++ )
  111.                     if ( ( $counter <= $tabber['num'] ) && ( empty( $tabber["title_$counter"] ) ) )
  112.                         $this->_errors[] = "You must supply a Tab $counter Title";
  113.             }
  114.            
  115.             if ( ! empty( $this->_errors ) ) {
  116.                 $this->_list_tabbers();
  117.                 return;
  118.             }
  119.            
  120.            
  121.             $templates = $this->_get_templates();
  122.            
  123.             $tabber['template_name'] = $templates[$tabber['template_file']]['name'];
  124.             $tabber['template_class'] = $templates[$tabber['template_file']]['class'];
  125.            
  126.            
  127.             if ( ! isset( $_POST['id'] ) ) {
  128.                 $this->_options['tabbers'][] = $tabber;
  129.                 ITUtility::show_status_message( 'Tabber added' );
  130.             }
  131.             else {
  132.                 $this->_options['tabbers'][$_POST['id']] = $tabber;
  133.                 ITUtility::show_status_message( 'Tabber updated' );
  134.             }
  135.            
  136.             $this->_save();
  137.            
  138.             unset( $_POST['id'] );
  139.             unset( $_REQUEST['id'] );
  140.            
  141.             $this->_list_tabbers();
  142.         }
  143.        
  144.         function _delete_tabber() {
  145.             $tabber = $this->_options['tabbers'][$_REQUEST['delete']];
  146.            
  147.             unset( $this->_options['tabbers'][$_REQUEST['delete']] );
  148.             $this->_save();
  149.            
  150.             ITUtility::show_status_message( "Deleted {$tabber['name']}" );
  151.            
  152.             $this->_list_tabbers();
  153.         }
  154.        
  155.         function _list_tabbers() {
  156.             ITUtility::require_file_once( 'it-array-sort.php' );
  157.            
  158. ?>
  159.     <?php if ( empty( $this->_errors ) && ! isset( $_REQUEST['id'] ) ) : ?>
  160.         <div class="wrap">
  161.             <h2>Tabber Widgets</h2>
  162.            
  163.             <?php if ( count( $this->_options['tabbers'] ) > 0 ) : ?>
  164.                 <table class="widefat">
  165.                     <thead>
  166.                         <tr class="thead">
  167.                             <th>Name</th>
  168.                             <th>Number of Widgets</th>
  169.                             <th>Template</th>
  170.                             <th>Tab Titles</th>
  171.                         </tr>
  172.                     </thead>
  173.                     <tfoot>
  174.                         <tr class="thead">
  175.                             <th>Name</th>
  176.                             <th>Number of Widgets</th>
  177.                             <th>Template</th>
  178.                             <th>Tab Titles</th>
  179.                         </tr>
  180.                     </tfoot>
  181.                     <tbody id="tabbers">
  182.                         <?php
  183.                             $class = '';
  184.                            
  185.                             $sort = new ITArraySort( $this->_options['tabbers'], 'name' );
  186.                             $tabbers = $sort->get_sorted_array();
  187.                         ?>
  188.                         <?php foreach ( (array) $tabbers as $id => $tabber ) : ?>
  189.                             <?php
  190.                                 $tab_titles = '';
  191.                                
  192.                                 for ( $counter = 1; $counter <= 11; $counter++ ) {
  193.                                     if ( $counter <= $tabber['num'] ) {
  194.                                         if ( ! empty( $tab_titles ) )
  195.                                             $tab_titles .= ', ';
  196.                                        
  197.                                         $tab_titles .= $tabber["title_$counter"];
  198.                                     }
  199.                                 }
  200.                             ?>
  201.                            
  202.                             <tr id="tabber-<?php echo $id; ?>"<?php echo $class; ?>>
  203.                                 <td>
  204.                                     <strong><a class="tabber_name" href="<?php echo $this->_self_link; ?>&id=<?php echo $id; ?>" title="Modify Tabber Settings"><?php echo $tabber['name']; ?></a></strong>
  205.                                     <div class="row-actions">
  206.                                         <span class="edit"><a href="<?php echo $this->_self_link; ?>&id=<?php echo $id; ?>">Edit</a> | </span>
  207.                                         <span class="delete"><a class="delete_tabber_link" href="<?php echo $this->_self_link; ?>&delete=<?php echo $id; ?>">Delete</a></span>
  208.                                     </div>
  209.                                 </td>
  210.                                 <td><?php echo $tabber['num']; ?></td>
  211.                                 <td><?php echo $tabber['template_name']; ?></td>
  212.                                 <td><?php echo $tab_titles; ?></td>
  213.                             </tr>
  214.                             <?php $class = ( '' === $class ) ? ' class="alternate"' : ''; ?>
  215.                         <?php endforeach; ?>
  216.                     </tbody>
  217.                 </table>
  218.             <?php endif; ?>
  219.         </div>
  220.     <?php endif; ?>
  221.    
  222.     <div class="wrap">
  223.         <h2><?php echo ( ! isset( $_REQUEST['id'] ) ) ? 'Add New Tabber' : 'Modify Tabber'; ?></h2>
  224.        
  225.         <?php
  226.             $number_widgets = array();
  227.            
  228.             for ( $num = 1; $num <= 11; $num++ )
  229.                 $number_widgets[$num] = $num;
  230.            
  231.            
  232.             if ( isset( $this->_errors ) )
  233.                 foreach ( (array) $this->_errors as $error )
  234.                     ITUtility::show_error_message( $error );
  235.            
  236.            
  237.             $tabber = array();
  238.            
  239.             if ( ! empty( $this->_errors ) ) {
  240.                 $tabber = ITForm::get_post_data();
  241.                
  242.                 if ( isset( $_POST['id'] ) )
  243.                     $tabber['id'] = $_POST['id'];
  244.             }
  245.             else if ( isset( $_REQUEST['id'] ) ) {
  246.                 $tabber = $this->_options['tabbers'][$_REQUEST['id']];
  247.                 $tabber['id'] = $_REQUEST['id'];
  248.             }
  249.             else
  250.                 $tabber['num'] = 3;
  251.            
  252.            
  253.             $templates = $this->_get_templates();
  254.             $template_options = array();
  255.            
  256.             foreach ( (array) $templates as $template )
  257.                 $template_options[$template['file']] = $template['name'];
  258.            
  259.             asort( $template_options );
  260.            
  261.            
  262.             $form =& new ITForm( $tabber, array( 'prefix' => $this->_var ) );
  263.         ?>
  264.        
  265.         <?php $form->start_form(); ?>
  266.             <table class="form-table">
  267.                 <tr><th scope="row">Name</th>
  268.                     <td><?php $form->add_text_box( 'name', array( 'size' => 20 ) ); ?></td>
  269.                 </tr>
  270.                 <tr><th scope="row">Number of Widgets</th>
  271.                     <td><?php $form->add_drop_down( 'num', $number_widgets ); ?></td>
  272.                 </tr>
  273.                 <tr><th scope="row">Template</th>
  274.                     <td><?php $form->add_drop_down( 'template_file', $template_options ); ?></td>
  275.                 </tr>
  276.                 <?php for ( $count = 1; $count <= 11; $count++ ) : ?>
  277.                     <tr id="title_input_row_<?php echo $count; ?>"><th scope="row">Tab <?php echo $count; ?> Title</th>
  278.                         <td><?php $form->add_text_box( "title_$count", array( 'size' => 20 ) ); ?></td>
  279.                     </tr>
  280.                 <?php endfor; ?>
  281.             </table>
  282.            
  283.             <p class="submit">
  284.                 <?php $form->add_submit( 'save', ( ! isset( $tabber['id'] ) ) ? 'Add Tabber' : 'Update Tabber' ); ?>
  285.             </p>
  286.            
  287.             <?php if ( isset( $tabber['id'] ) ) : ?>
  288.                 <?php $form->add_hidden_no_save( 'id', $tabber['id'] ); ?>
  289.             <?php endif; ?>
  290.         <?php $form->end_form(); ?>
  291.     </div>
  292. <?php
  293.            
  294.         }
  295.        
  296.        
  297.         // Utility Functions //////////////////////////
  298.        
  299.         function _get_templates() {
  300.             if ( isset( $this->_templates ) )
  301.                 return $this->_templates;
  302.            
  303.             $this->_templates = array();
  304.            
  305.             if ( false === ( $dir = opendir( $this->_templates_directory ) ) )
  306.                 return $this->_templates;
  307.            
  308.             while ( false !== ( $file = readdir( $dir ) ) ) {
  309.                 if ( is_dir( "{$this->_templates_directory}/$file" ) )
  310.                     continue;
  311.                 if ( ! preg_match( '|\.css$|', $file ) )
  312.                     continue;
  313.                
  314.                 $template = $this->_get_template_data( "{$this->_templates_directory}/$file" );
  315.                
  316.                 if ( is_array( $template ) )
  317.                     $this->_templates[$template['file']] = $template;
  318.             }
  319.            
  320.             return $this->_templates;
  321.         }
  322.        
  323.         function _get_template_data( $file ) {
  324.             $file_handle = fopen( $file, 'r' );
  325.             $data = fread( $file_handle, 8192 );
  326.             fclose( $file_handle );
  327.            
  328.             preg_match( '|Name:(.*)$|mi', $data, $name );
  329.             preg_match( '|Class:(.*)$|mi', $data, $class );
  330.            
  331.             $name = trim( $name[1] );
  332.             $class = trim( $class[1] );
  333.            
  334.             if ( empty( $name ) || empty( $class ) )
  335.                 return false;
  336.            
  337.             return array( 'file' => basename( $file ), 'name' => $name, 'class' => $class );
  338.         }
  339.     }
  340.    
  341.     new ITTabber();
  342. }
  343.  
  344. ?>