Ejemplo n.º 1
0
/**
 * Invoked when a product is duplicated and duplicates any product tabs
 *
 * @since 1.0.6
 */
function wc_tab_manager_duplicate_product($new_id, $original_post)
{
    $tabs = get_post_meta($new_id, '_product_tabs', true);
    if (is_array($tabs)) {
        $is_updated = false;
        foreach ($tabs as $key => $tab) {
            if ('product' === $tab['type']) {
                $tab_post = get_post($tab['id']);
                $new_tab_data = array('post_title' => $tab_post->post_title, 'post_content' => $tab_post->post_content, 'post_status' => 'publish', 'ping_status' => 'closed', 'post_author' => get_current_user_id(), 'post_type' => 'wc_product_tab', 'post_parent' => $new_id, 'post_password' => uniqid('tab_'));
                $removed_hooks = wc_tab_manager_remove_conflicting_hooks(array('delete_post' => 'detect_post_delete'));
                // link up the product to its new tab
                $tabs[$key]['id'] = wp_insert_post($new_tab_data);
                $is_updated = true;
                wc_tab_manager_restore_conflicting_hooks($removed_hooks);
            }
        }
        if ($is_updated) {
            update_post_meta($new_id, '_product_tabs', $tabs);
        }
    }
}
/**
 * Process and return the tab data POSTed by the sortable product tabs
 * function.
 *
 * @access public
 * @param int $post_id optional post identifier.  A value of 0 indicates product-level
 *        tabs will not be processed
 * @param object $post the optional post object.
 *
 * @return array tab data, ready for persistance
 */
function wc_tab_manager_process_tabs($post_id = 0, $post = null)
{
    $tab_active = isset($_POST['product_tab_active']) ? $_POST['product_tab_active'] : array();
    $tab_positions = isset($_POST['product_tab_position']) ? $_POST['product_tab_position'] : array();
    $tab_types = isset($_POST['product_tab_type']) ? $_POST['product_tab_type'] : array();
    $tab_headings = isset($_POST['product_tab_heading']) ? $_POST['product_tab_heading'] : array();
    // available only for the core description/additional_information tabs
    $tab_titles = isset($_POST['product_tab_title']) ? $_POST['product_tab_title'] : array();
    $tab_content = isset($_POST['product_tab_content']) ? $_POST['product_tab_content'] : array();
    // available only for product tab type
    $tab_ids = isset($_POST['product_tab_id']) ? $_POST['product_tab_id'] : array();
    $tabs = array();
    // create the new set of active tabs (if any)
    for ($i = 0; $i < count($tab_positions); $i++) {
        if ($tab_active[$i]) {
            $tab = array('position' => $tab_positions[$i], 'type' => $tab_types[$i], 'id' => $tab_ids[$i]);
            if (isset($tab_titles[$i])) {
                $tab['title'] = $tab_titles[$i];
            }
            if ('product' == $tab['type']) {
                $removed_hooks = wc_tab_manager_remove_conflicting_hooks(array('post_updated' => 'detect_slug_change', 'pre_post_update' => 'set_pre_update_stock_status'));
                if (!$tab['id']) {
                    // new custom product tab
                    $new_tab_data = array('post_title' => $tab_titles[$i], 'post_content' => $tab_content[$i], 'post_status' => 'publish', 'ping_status' => 'closed', 'post_author' => get_current_user_id(), 'post_type' => 'wc_product_tab', 'post_parent' => $post_id, 'post_password' => uniqid('tab_'));
                    $tab['id'] = wp_insert_post($new_tab_data);
                } else {
                    // update existing custom product tab
                    $tab_data = array('ID' => $tab['id'], 'post_title' => $tab_titles[$i], 'post_content' => $tab_content[$i]);
                    wp_update_post($tab_data);
                }
                wc_tab_manager_restore_conflicting_hooks($removed_hooks);
            }
            // only the core description and additional information tabs have a heading
            if (isset($tab_headings[$i])) {
                $tab['heading'] = $tab_headings[$i];
            }
            $tabs[$tab['type'] . '_tab_' . $tab['id']] = $tab;
        } else {
            // tab removed
            if ('product' == $tab_types[$i]) {
                // for product custom tabs, remove the tab post record
                wp_delete_post($tab_ids[$i]);
            }
        }
    }
    // sort the tabs according to position
    if (!function_exists('product_tabs_cmp')) {
        function product_tabs_cmp($a, $b)
        {
            if ($a['position'] == $b['position']) {
                return 0;
            }
            return $a['position'] < $b['position'] ? -1 : 1;
        }
    }
    uasort($tabs, 'product_tabs_cmp');
    // make sure the position values are 0, 1, 2 ...
    $i = 0;
    foreach ($tabs as &$tab) {
        $tab['position'] = $i++;
    }
    // it's important to generate unique names to use for the tab/tab panel css ids, so that
    //  clicking a tab brings up the correct tab panel (since we can't change their names)
    //  We'll generate names like 'description', 'description-1', 'description-2', etc
    $found_names = array();
    $tab_names = array();
    // first off, the core tabs get priority on naming (which for them is their id)
    foreach ($tabs as &$tab) {
        if ('core' == $tab['type']) {
            $tab_name = $tab['id'];
            if (!isset($found_names[$tab_name])) {
                $found_names[$tab_name] = 0;
            }
            $found_names[$tab_name]++;
        }
    }
    // next up: the 3rd party tabs; we don't want to clash with their keys
    foreach ($tabs as &$tab) {
        if ('third_party' == $tab['type']) {
            $tab_name = $tab['id'];
            if (!isset($found_names[$tab_name])) {
                $found_names[$tab_name] = 0;
            }
            $found_names[$tab_name]++;
        }
    }
    // next up are the global tabs
    foreach ($tabs as &$tab) {
        if ('global' == $tab['type']) {
            // see product tab comment below for naming discussion
            if (strlen($tab['title']) != strlen(utf8_encode($tab['title']))) {
                $tab_name = "global-tab";
            } else {
                $tab_name = sanitize_title($tab['title']);
            }
            if (!isset($found_names[$tab_name])) {
                $found_names[$tab_name] = 0;
            }
            $found_names[$tab_name]++;
            if ($found_names[$tab_name] > 1) {
                $tab_name .= '-' . ($found_names[$tab_name] - 1);
            }
            $tab['name'] = $tab_name;
            // once the title is used to generate the unique name, it is no longer needed as it will be pulled from the tab post
            unset($tab['title']);
        }
    }
    // finally the custom product tabs
    foreach ($tabs as &$tab) {
        if ('product' == $tab['type']) {
            // we try to generate a clean unique tab name based off of the tab title,
            //  however the page javascript (jquery) that controls the tab switching can not
            //  handle unicode class id's, escaped or otherwise.  The compromise is to
            //  use the "pretty" name for non-unicode strings, and just use a safe "product-tab"
            //  identifier for tab titles containing unicode
            if (strlen($tab['title']) != strlen(utf8_encode($tab['title']))) {
                $tab_name = "product-tab";
            } else {
                $tab_name = sanitize_title($tab['title']);
            }
            if (!isset($found_names[$tab_name])) {
                $found_names[$tab_name] = 0;
            }
            $found_names[$tab_name]++;
            if ($found_names[$tab_name] > 1) {
                $tab_name .= '-' . ($found_names[$tab_name] - 1);
            }
            $tab['name'] = $tab_name;
            // once the title is used to generate the unique name, it is no longer needed as it will be pulled from the tab post
            unset($tab['title']);
        }
    }
    return $tabs;
}