Last active
August 20, 2016 13:46
-
-
Save ontiuk/bc6feee30a6860103303 to your computer and use it in GitHub Desktop.
Woocommerce & WCVendors Integration: Product post-type labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Product Post Type Labels. Wrap around admin labels for product post type. | |
| * Integrated with check for vendor, so applies to vendor only in WP Admin UI | |
| */ | |
| final class Theme_Product_Labels { | |
| /** | |
| * Value assigned to products in WP global $menu | |
| * | |
| * @var integer $menu default 26 | |
| * @access public | |
| */ | |
| public $menu_id = 26; | |
| /** | |
| * Current user | |
| * | |
| * @var boolean $is_vendor | |
| * @access private | |
| */ | |
| private $is_vendor; | |
| /** | |
| * Singular label | |
| * | |
| * @var string $single | |
| * @access private | |
| */ | |
| private $single = ''; | |
| /** | |
| * Plural label | |
| * | |
| * @var string $plural | |
| * @access private | |
| */ | |
| private $plural = ''; | |
| /** | |
| * Constructor | |
| * | |
| * @param string $single | |
| * @param string $plural | |
| * @acess public | |
| */ | |
| public function __construct( $single, $plural='' ) { | |
| // validate label | |
| if ( empty( $single ) ) { | |
| add_action( 'admin_notices', array( $this, 'admin_notice' ) ); | |
| return; | |
| } else { | |
| $this->single = sanitize_text_field( $single ); | |
| $this->plural = ( empty( $plural ) ) ? $this->single : sanitize_text_field( $plural ); | |
| } | |
| // set up user state | |
| $this->is_vendor = $this->is_vendor(); | |
| // admin menu post type labels | |
| add_action( 'admin_menu', array( $this, 'admin_menu_labels' ) ); | |
| add_action( 'init', array( $this, 'product_labels' ) ); | |
| add_filter( 'post_updated_messages', array( $this, 'product_messages') ); | |
| } | |
| /** | |
| * Admin menu label | |
| * | |
| * @access public | |
| */ | |
| public function admin_menu_labels() { | |
| global $menu, $submenu; | |
| // user is vendor | |
| if ( !$this->is_vendor ) { return; } | |
| // ok change labels if set | |
| if ( isset( $this->menu_id, $submenu['edit.php?post_type=product'] ) ) { | |
| $menu[$this->menu_id][0] = ucwords( $this->plural ); | |
| $submenu['edit.php?post_type=product'][5][0] = ucwords( $this->plural ); | |
| $submenu['edit.php?post_type=product'][10][0] = 'Add ' . ucwords( $this->plural ); | |
| } | |
| } | |
| /** | |
| * Change product post type labels | |
| * | |
| * @access public | |
| */ | |
| public function product_labels() { | |
| global $wp_post_types; | |
| // user is vendor | |
| if ( !$this->is_vendor ) { return; } | |
| // set labels | |
| $labels = &$wp_post_types['product']->labels; | |
| $labels->name = ucwords( $this->plural ); | |
| $labels->singular_name = ucwords( $this->plural ); | |
| $labels->add_new = 'Add new '. $this->single; | |
| $labels->add_new_item = 'Add new ' . $this->single; | |
| $labels->edit_item = 'Edit ' . $this->single; | |
| $labels->new_item = ucwords( $this->plural ); | |
| $labels->view_item = 'View ' . $this->single; | |
| $labels->search_items = 'Search ' . $this->plural; | |
| $labels->not_found = 'No ' . $this->plural.' found'; | |
| $labels->not_found_in_trash = 'No ' . $this->plural . ' found in Trash'; | |
| $labels->all_items = 'All ' . $this->plural; | |
| $labels->menu_name = ucwords( $this->plural ); | |
| $labels->name_admin_bar = ucwords( $this->plural ); | |
| } | |
| /** | |
| * Product post type messages | |
| */ | |
| public function product_messages( $messages ) { | |
| global $post, $post_ID; | |
| // user is vendor | |
| if ( !$this->is_vendor ) { return; } | |
| // set messages | |
| $messages['product'] = array( | |
| 0 => '', | |
| 1 => sprintf( $this->single . ' updated. <a href="%s">View ' . $this->single . '</a>', esc_url( get_permalink( $post_ID ) ) ), | |
| 2 => 'Custom field updated.', | |
| 3 => 'Custom field deleted.', | |
| 4 => ucwords( $this->single ) . ' updated.', | |
| 5 => isset( $_GET['revision'] ) ? sprintf( ucwords( $this->single ) . ' restored to revision from %s', wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, | |
| 6 => sprintf( ucwords( $this->single ) . ' published. <a href="%s">View ' . $this->single . '</a>', esc_url( get_permalink( $post_ID ) ) ), | |
| 7 => ucwords( $this->single ) . ' saved.', | |
| 8 => sprintf( ucwords( $this->single ) . ' submitted. <a target="_blank" href="%s">Preview ' . $this->single . '</a>', esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ) ), | |
| 9 => sprintf( ucwords( $this->single ) . ' scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview ' . $this->single . '</a>', | |
| date_i18n( 'M j, Y @ G:i', strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ) ), | |
| 10 => sprintf( ucwords( $this->single ) . ' draft updated. <a target="_blank" href="%s">Preview ' . $this->single . '</a>', esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ) ), | |
| ); | |
| return $messages; | |
| } | |
| /** | |
| * Test current user is a vendor | |
| */ | |
| protected function is_vendor() { | |
| global $current_user; | |
| // get the user | |
| if ( !$current_user instanceof WP_User ) { | |
| $current_user = wp_get_current_user(); | |
| if ( !$current_user instanceof WP_User ) { return FALSE; } | |
| } | |
| // vendor only | |
| if ( class_exists( 'WCV_Vendors' ) && WCV_Vendors::is_vendor( $current_user->ID ) ) { return TRUE; } | |
| // default | |
| return FALSE; | |
| } | |
| /** | |
| * Admin notice | |
| * | |
| * @access public | |
| */ | |
| public function admin_notice() { | |
| ?> | |
| <div class="error"> <p>Product Single Label Is Required</p></div> | |
| <?php | |
| } | |
| } | |
| $theme_product_labels = new Theme_Product_Labels; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment