class-wc-admin-customize.php 2.48 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
<?php
/**
 * Setup customize items.
 *
 * @author   WooCommerce
 * @category Admin
 * @package  WooCommerce/Admin/Customize
 * @version  3.1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'WC_Admin_Customize', false ) ) :

/**
 * WC_Admin_Customize Class.
 */
class WC_Admin_Customize {

	/**
	 * Initialize customize actions.
	 */
	public function __construct() {
		// Include custom items to customizer nav menu settings.
		add_filter( 'customize_nav_menu_available_item_types', array( $this, 'register_customize_nav_menu_item_types' ) );
		add_filter( 'customize_nav_menu_available_items', array( $this, 'register_customize_nav_menu_items' ), 10, 4 );
	}

	/**
	 * Register customize new nav menu item types.
	 * This will register WooCommerce account endpoints as a nav menu item type.
	 *
	 * @since  3.1.0
	 * @param  array $item_types Menu item types.
	 * @return array
	 */
	public function register_customize_nav_menu_item_types( $item_types  ) {
		$item_types[] = array(
			'title'      => __( 'WooCommerce endpoints', 'woocommerce' ),
			'type_label' => __( 'WooCommerce endpoint', 'woocommerce' ),
			'type'       => 'woocommerce_nav',
			'object'     => 'woocommerce_endpoint',
		);

		return $item_types;
	}

	/**
	 * Register account endpoints to customize nav menu items.
	 *
	 * @since  3.1.0
	 * @param  array   $items  List of nav menu items.
	 * @param  string  $type   Nav menu type.
	 * @param  string  $object Nav menu object.
	 * @param  integer $page   Page number.
	 * @return array
	 */
	public function register_customize_nav_menu_items( $items = array(), $type = '', $object = '', $page = 0 ) {
		if ( 'woocommerce_endpoint' !== $object ) {
			return $items;
		}

		// Don't allow pagination since all items are loaded at once.
		if ( 0 < $page ) {
			return $items;
		}

		// Get items from account menu.
		$endpoints = wc_get_account_menu_items();

		// Remove dashboard item.
		if ( isset( $endpoints['dashboard'] ) ) {
			unset( $endpoints['dashboard'] );
		}

		// Include missing lost password.
		$endpoints['lost-password'] = __( 'Lost password', 'woocommerce' );

		$endpoints = apply_filters( 'woocommerce_custom_nav_menu_items', $endpoints );

		foreach ( $endpoints as $endpoint => $title ) {
			$items[] = array(
				'id'         => $endpoint,
				'title'      => $title,
				'type_label' => __( 'Custom Link', 'woocommerce' ),
				'url'        => esc_url_raw( wc_get_account_endpoint_url( $endpoint ) ),
			);
		}

		return $items;
	}
}

endif;

return new WC_Admin_Customize();