Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: MX WooCommerce custom tabs
- Plugin URI: http://media-x.hr
- Description: Plugin za dodavanje custom tabova
- Version: 1.0
- Author: Media X
- Author URI: http://media-x.hr
- License: GPLv3 or later License
- URI: http://www.gnu.org/licenses/gpl-3.0.html
- WC requires at least: 2.2
- WC tested up to: 2.4.7
- */
- if (!class_exists('mx_custom_tabs')) {
- class mx_custom_tabs {
- public $paths = array();
- public function path($name, $file = '') {
- return $this->paths[$name] . (strlen($file) > 0 ? '/' . preg_replace('/^\//', '', $file) : '');
- }
- public function assetUrl($file) {
- return $this->paths['BASE_URI'] . $this->path('ASSETS_DIR_NAME', $file);
- }
- function __construct() {
- $this->paths = array(
- 'BASE_URI' => $dir = plugin_dir_path( __FILE__ ),
- 'ASSETS_DIR_NAME' => 'assets'
- );
- if (is_admin()) {
- add_action('add_meta_boxes', array(&$this, 'dynamic_add_custom_tab'));
- add_action('load-post.php', array($this, 'add_assets') , 4);
- add_action('load-post-new.php', array($this, 'add_assets') , 4 );
- /* Do something with the data entered */
- add_action('save_post', array(&$this, 'dynamic_save_postdata') );
- } else {
- add_action('init', array(&$this, 'init'));
- }
- }
- function init() {
- add_filter('woocommerce_product_tabs', array(&$this, 'product_custom_tabs'));
- }
- function product_custom_tabs ($tabs) {
- global $post;
- $custom_tabs = get_post_meta($post->ID, 'mx_custom_tabs', true);
- $priority = 50;
- if (isset($custom_tabs) && !empty($custom_tabs) && count($custom_tabs) > 0) {
- foreach(@$custom_tabs as $id => $tab) {
- if (isset($tab['mx_title_product_tab']) && $tab['mx_title_product_tab'] != '' && isset($tab['mx_content_product_tab'])) {
- $tabs[$id] = array(
- 'title' => $tab['mx_title_product_tab'],
- 'priority' => $priority,
- 'callback' => 'mx_woocommerce_product_custom_tab'
- );
- }
- $priority = $priority + 1;
- }
- }
- return $tabs;
- }
- function add_assets() {
- add_action('print_media_templates', array(&$this, 'add_tmpl') );
- wp_enqueue_script('custom_tab_js', plugins_url('assets/js/custom_tab.js', __FILE__), array( 'jquery' ), '1.0', true);
- wp_enqueue_style('custom_tab_css', plugins_url('assets/css/custom_tab.css', __FILE__));
- }
- public function add_tmpl() {
- $settings = array(
- 'textarea_name' => 'mx_custom_tabs[__REPLACE_SSS__][mx_content_product_tab]',
- 'textarea_rows' => 3,
- 'quicktags' => true,
- 'tinymce' => true
- );
- ob_start(); ?>
- <script type="text/html" id="tmpl-add-custom-tab">
- <li>
- <div class="handle-area"></div>
- <div class="item">
- <h3><?php esc_html_e('Custom Tab Title', 'mx'); ?></h3>
- <input type="text" name="mx_custom_tabs[__REPLACE_SSS__][mx_title_product_tab]" value=""/>
- <p class="desc"><?php esc_html_e('Enter a title for the tab (required)', 'mx'); ?></p>
- </div>
- <div class="item wp-editor">
- <h3><?php esc_html_e('Custom Tab Content', 'mx'); ?></h3>
- <?php wp_editor( '', '__REPLACE_SSS__', $settings ); ?>
- </div>
- <div class="item">
- <a href="javascript:void(0)" class="button button-secondary remove-custom-tab"><?php _e('Remove Custom Tab', 'mx'); ?></a>
- </div>
- </li>
- </script>
- <?php echo ob_get_clean();
- }
- function dynamic_add_custom_tab() {
- add_meta_box('mx_dynamic_custom_tab', esc_html__( 'Custom Product Tabs', 'mx' ), array(&$this, 'dynamic_inner_custom_tab'), 'product', 'advanced', 'high');
- }
- /* Prints the box content */
- function dynamic_inner_custom_tab() {
- global $post;
- // Use nonce for verification
- wp_nonce_field( 'mx-custom-tab', 'dynamicMeta_noncename' );
- ?>
- <div id="meta_custom_tabs">
- <?php $custom_tabs = get_post_meta($post->ID, 'mx_custom_tabs', true); ?>
- <ul class="custom-box-holder">
- <?php if (isset($custom_tabs) && !empty($custom_tabs) && count($custom_tabs) > 0): ?>
- <?php foreach($custom_tabs as $id => $tab): ?>
- <?php if (isset($tab['mx_title_product_tab']) || isset($tab['mx_content_product_tab'])): ?>
- <li>
- <div class="handle-area"></div>
- <div class="item">
- <h3><?php esc_html_e('Custom Tab Title', 'mx'); ?></h3>
- <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']); ?>" />
- <p class="desc"><?php esc_html_e('Enter a title for the tab (required)', 'mx'); ?></p>
- </div>
- <div class="item wp-editor">
- <h3><?php esc_html_e('Custom Tab Content', 'mx'); ?></h3>
- <?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, ) ); ?>
- </div>
- <div class="item">
- <a href="javascript:void(0)" class="button button-secondary remove-custom-tab"><?php esc_html_e('Remove Custom Tab', 'mx'); ?></a>
- </div>
- </li>
- <?php endif; ?>
- <?php endforeach; ?>
- <?php endif; ?>
- </ul><!--/ .custom-tabs-->
- <a href="javascript:void(0);" class="button button-primary add-custom-tab"><?php esc_html_e('Add Custom Tab', 'mx'); ?></a>
- </div><?php
- }
- /* When the post is saved, saves our custom data */
- function dynamic_save_postdata ($post_id) {
- // verify if this is an auto save routine.
- // If it is our form has not been submitted, so we dont want to do anything
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
- return;
- // verify this came from the our screen and with proper authorization,
- // because save_post can be triggered at other times
- if ( !isset( $_POST['dynamicMeta_noncename'] ) )
- return;
- if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], 'mx-custom-tab' ) )
- return;
- if ( !isset( $_POST['mx_custom_tabs'] ) ) {
- $_POST['mx_custom_tabs'] = '';
- }
- $tabs = $_POST['mx_custom_tabs'];
- update_post_meta($post_id, 'mx_custom_tabs', $tabs);
- }
- }
- function mx_woocommerce_product_custom_tab($key)
- {
- global $post;
- $mx_title_product_tab = $mx_content_product_tab = '';
- $custom_tabs_array = get_post_meta($post->ID, 'mx_custom_tabs', true);
- $custom_tab = $custom_tabs_array[$key];
- extract($custom_tab);
- if ($mx_title_product_tab != '') {
- echo do_shortcode($mx_content_product_tab);
- }
- }
- new mx_CUSTOM_TABS();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement