Advertisement
borkolivic

pehapeac za tabove

Apr 4th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.56 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: MX WooCommerce custom tabs
  4. Plugin URI: http://media-x.hr
  5. Description: Plugin za dodavanje custom tabova
  6. Version: 1.0
  7. Author: Media X
  8. Author URI: http://media-x.hr
  9. License: GPLv3 or later License
  10. URI: http://www.gnu.org/licenses/gpl-3.0.html
  11. WC requires at least: 2.2
  12. WC tested up to: 2.4.7
  13. */
  14.  
  15. if (!class_exists('mx_custom_tabs')) {
  16.  
  17.     class mx_custom_tabs {
  18.  
  19.         public $paths = array();
  20.  
  21.         public function path($name, $file = '') {
  22.             return $this->paths[$name] . (strlen($file) > 0 ? '/' . preg_replace('/^\//', '', $file) : '');
  23.         }
  24.  
  25.         public function assetUrl($file)  {
  26.             return $this->paths['BASE_URI'] . $this->path('ASSETS_DIR_NAME', $file);
  27.         }
  28.        
  29.  
  30.         function __construct() {
  31.  
  32.             $this->paths = array(
  33.                 'BASE_URI' => $dir = plugin_dir_path( __FILE__ ),
  34.                 'ASSETS_DIR_NAME' => 'assets'
  35.             );
  36.  
  37.             if (is_admin()) {
  38.                 add_action('add_meta_boxes', array(&$this, 'dynamic_add_custom_tab'));
  39.  
  40.                 add_action('load-post.php', array($this, 'add_assets') , 4);
  41.                 add_action('load-post-new.php', array($this, 'add_assets') , 4 );
  42.  
  43.                 /* Do something with the data entered */
  44.                 add_action('save_post', array(&$this, 'dynamic_save_postdata') );
  45.             } else {
  46.                 add_action('init', array(&$this, 'init'));
  47.             }
  48.  
  49.         }
  50.        
  51.  
  52.         function init() {
  53.             add_filter('woocommerce_product_tabs', array(&$this, 'product_custom_tabs'));
  54.         }
  55.        
  56.  
  57.         function product_custom_tabs ($tabs) {
  58.             global $post;
  59.  
  60.             $custom_tabs = get_post_meta($post->ID, 'mx_custom_tabs', true);
  61.             $priority = 50;
  62.  
  63.  
  64.             if (isset($custom_tabs) && !empty($custom_tabs) && count($custom_tabs) > 0) {
  65.                 foreach(@$custom_tabs as $id => $tab) {
  66.                     if (isset($tab['mx_title_product_tab']) && $tab['mx_title_product_tab'] != '' && isset($tab['mx_content_product_tab'])) {
  67.                         $tabs[$id] = array(
  68.                             'title' => $tab['mx_title_product_tab'],
  69.                             'priority' => $priority,
  70.                             'callback' => 'mx_woocommerce_product_custom_tab'
  71.                         );
  72.                     }
  73.                     $priority = $priority + 1;
  74.                 }
  75.             }
  76.             return $tabs;
  77.         }
  78.        
  79.  
  80.         function add_assets() {
  81.             add_action('print_media_templates', array(&$this, 'add_tmpl') );
  82.             wp_enqueue_script('custom_tab_js', plugins_url('assets/js/custom_tab.js', __FILE__), array( 'jquery' ), '1.0', true);
  83.             wp_enqueue_style('custom_tab_css', plugins_url('assets/css/custom_tab.css', __FILE__));
  84.         }
  85.  
  86.         public function add_tmpl() {
  87.  
  88.             $settings = array(
  89.                 'textarea_name' => 'mx_custom_tabs[__REPLACE_SSS__][mx_content_product_tab]',
  90.                 'textarea_rows' => 3,
  91.                 'quicktags' => true,
  92.                 'tinymce' => true
  93.             );
  94.  
  95.             ob_start(); ?>
  96.  
  97.             <script type="text/html" id="tmpl-add-custom-tab">
  98.                 <li>
  99.                     <div class="handle-area"></div>
  100.                     <div class="item">
  101.                         <h3><?php esc_html_e('Custom Tab Title', 'mx'); ?></h3>
  102.                         <input type="text" name="mx_custom_tabs[__REPLACE_SSS__][mx_title_product_tab]" value=""/>
  103.                         <p class="desc"><?php esc_html_e('Enter a title for the tab (required)', 'mx'); ?></p>
  104.                     </div>
  105.                     <div class="item wp-editor">
  106.                         <h3><?php esc_html_e('Custom Tab Content', 'mx'); ?></h3>
  107.                         <?php wp_editor( '', '__REPLACE_SSS__', $settings ); ?>
  108.                     </div>
  109.                     <div class="item">
  110.                         <a href="javascript:void(0)" class="button button-secondary remove-custom-tab"><?php _e('Remove Custom Tab', 'mx'); ?></a>
  111.                     </div>
  112.                 </li>
  113.             </script>
  114.  
  115.             <?php echo ob_get_clean();
  116.         }
  117.  
  118.         function dynamic_add_custom_tab() {
  119.             add_meta_box('mx_dynamic_custom_tab', esc_html__( 'Custom Product Tabs', 'mx' ), array(&$this, 'dynamic_inner_custom_tab'), 'product', 'advanced', 'high');
  120.         }
  121.  
  122.         /* Prints the box content */
  123.         function dynamic_inner_custom_tab() {
  124.             global $post;
  125.  
  126.             // Use nonce for verification
  127.             wp_nonce_field( 'mx-custom-tab', 'dynamicMeta_noncename' );
  128.             ?>
  129.  
  130.             <div id="meta_custom_tabs">
  131.  
  132.                 <?php $custom_tabs = get_post_meta($post->ID, 'mx_custom_tabs', true); ?>
  133.  
  134.                 <ul class="custom-box-holder">
  135.  
  136.                     <?php if (isset($custom_tabs) && !empty($custom_tabs) && count($custom_tabs) > 0): ?>
  137.  
  138.                         <?php foreach($custom_tabs as $id => $tab): ?>
  139.  
  140.                             <?php if (isset($tab['mx_title_product_tab']) || isset($tab['mx_content_product_tab'])): ?>
  141.  
  142.                                 <li>
  143.                                     <div class="handle-area"></div>
  144.                                     <div class="item">
  145.                                         <h3><?php esc_html_e('Custom Tab Title', 'mx'); ?></h3>
  146.                                         <input type="text" name="mx_custom_tabs[<?php echo esc_attr($id); ?>][mx_title_product_tab]" value="<?php echo esc_attr($tab['mx_title_product_tab']); ?>" />
  147.                                         <p class="desc"><?php esc_html_e('Enter a title for the tab (required)', 'mx'); ?></p>
  148.                                     </div>
  149.                                     <div class="item wp-editor">
  150.                                         <h3><?php esc_html_e('Custom Tab Content', 'mx'); ?></h3>
  151.                                         <?php wp_editor( $tab['mx_content_product_tab'], esc_attr($id), array('textarea_name' => 'mx_custom_tabs['. $id .'][mx_content_product_tab]', 'textarea_rows' => 3, 'tinymce' => true, ) ); ?>
  152.                                     </div>
  153.                                     <div class="item">
  154.                                         <a href="javascript:void(0)" class="button button-secondary remove-custom-tab"><?php esc_html_e('Remove Custom Tab', 'mx'); ?></a>
  155.                                     </div>
  156.                                 </li>
  157.  
  158.                             <?php endif; ?>
  159.  
  160.                         <?php endforeach; ?>
  161.  
  162.                     <?php endif; ?>
  163.  
  164.                 </ul><!--/ .custom-tabs-->
  165.  
  166.                 <a href="javascript:void(0);" class="button button-primary add-custom-tab"><?php esc_html_e('Add Custom Tab', 'mx'); ?></a>
  167.  
  168.             </div><?php
  169.  
  170.         }
  171.  
  172.         /* When the post is saved, saves our custom data */
  173.         function dynamic_save_postdata ($post_id) {
  174.  
  175.             // verify if this is an auto save routine.
  176.             // If it is our form has not been submitted, so we dont want to do anything
  177.             if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  178.                 return;
  179.  
  180.             // verify this came from the our screen and with proper authorization,
  181.             // because save_post can be triggered at other times
  182.             if ( !isset( $_POST['dynamicMeta_noncename'] ) )
  183.                 return;
  184.  
  185.             if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], 'mx-custom-tab' ) )
  186.                 return;
  187.  
  188.             if ( !isset( $_POST['mx_custom_tabs'] ) ) {
  189.                 $_POST['mx_custom_tabs'] = '';
  190.             }
  191.  
  192.             $tabs = $_POST['mx_custom_tabs'];
  193.             update_post_meta($post_id, 'mx_custom_tabs', $tabs);
  194.         }
  195.  
  196.     }
  197.  
  198. function mx_woocommerce_product_custom_tab($key)
  199. {
  200.     global $post;
  201.  
  202.     $mx_title_product_tab = $mx_content_product_tab = '';
  203.     $custom_tabs_array = get_post_meta($post->ID, 'mx_custom_tabs', true);
  204.     $custom_tab = $custom_tabs_array[$key];
  205.  
  206.     extract($custom_tab);
  207.  
  208.     if ($mx_title_product_tab != '') {
  209.         echo do_shortcode($mx_content_product_tab);
  210.     }
  211. }
  212.  
  213.     new mx_CUSTOM_TABS();
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement