lass' => '',
'icon' => PWB_PLUGIN_URL . '/assets/backend/img/logo.jpg',
'category' => 'WooCommerce',
'params' => array(
array(
'type' => 'textfield',
'holder' => 'div',
'heading' => __( 'Brands per page', 'perfect-woocommerce-brands' ),
'param_name' => 'per_page',
'value' => '10',
'description' => __( 'Show x brands per page', 'perfect-woocommerce-brands' ),
),
array(
'type' => 'dropdown',
'heading' => __( 'Brand logo size', 'perfect-woocommerce-brands' ),
'param_name' => 'image_size',
'admin_label' => true,
'value' => $available_image_sizes_adapted,
),
array(
'type' => 'dropdown',
'heading' => __( 'Order by', 'perfect-woocommerce-brands' ),
'param_name' => 'order_by',
'admin_label' => true,
'value' => array(
'name' => 'name',
'slug' => 'slug',
'term_id' => 'term_id',
'id' => 'id',
'description' => 'description',
'rand' => 'rand',
),
),
array(
'type' => 'dropdown',
'heading' => __( 'Order', 'perfect-woocommerce-brands' ),
'param_name' => 'order',
'admin_label' => true,
'value' => array(
'ASC' => 'ASC',
'DSC' => 'DSC',
),
),
array(
'type' => 'dropdown',
'heading' => __( 'Title position', 'perfect-woocommerce-brands' ),
'param_name' => 'title_position',
'admin_label' => true,
'value' => array(
__( 'Before image', 'perfect-woocommerce-brands' ) => 'before',
__( 'After image', 'perfect-woocommerce-brands' ) => 'after',
__( 'Hide', 'perfect-woocommerce-brands' ) => 'none',
),
),
array(
'type' => 'checkbox',
'holder' => 'div',
'heading' => __( 'Hide empty', 'perfect-woocommerce-brands' ),
'param_name' => 'hide_empty',
'description' => __( 'Hide brands that have not been assigned to any product', 'perfect-woocommerce-brands' ),
),
),
)
);
vc_map(
array(
'name' => __( 'PWB AZ Listing', 'perfect-woocommerce-brands' ),
'description' => __( 'AZ Listing for brands', 'perfect-woocommerce-brands' ),
'base' => 'pwb-az-listing',
'class' => '',
'icon' => PWB_PLUGIN_URL . '/assets/backend/img/logo.jpg',
'category' => 'WooCommerce',
'params' => array(
array(
'type' => 'dropdown',
'heading' => __( 'Only parent brands', 'perfect-woocommerce-brands' ),
'param_name' => 'only_parents',
'admin_label' => true,
'value' => array(
esc_html__( 'No' ) => 'no',
esc_html__( 'Yes' ) => 'yes',
),
),
),
)
);
vc_map(
array(
'name' => __( 'PWB brand', 'perfect-woocommerce-brands' ),
'description' => __( 'Show brand for a specific product', 'perfect-woocommerce-brands' ),
'base' => 'pwb-brand',
'class' => '',
'icon' => PWB_PLUGIN_URL . '/assets/backend/img/logo.jpg',
'category' => 'WooCommerce',
'params' => array(
array(
'type' => 'textfield',
'holder' => 'div',
'heading' => __( 'Product id', 'perfect-woocommerce-brands' ),
'param_name' => 'product_id',
'value' => null,
'description' => __( 'Product id (post id)', 'perfect-woocommerce-brands' ),
),
array(
'type' => 'dropdown',
'heading' => __( 'Brand logo size', 'perfect-woocommerce-brands' ),
'param_name' => 'image_size',
'admin_label' => true,
'value' => $available_image_sizes_adapted,
),
),
)
);
}
public function action_woocommerce_single_product_summary() {
$brands = wp_get_post_terms( get_the_ID(), 'pwb-brand' );
if ( ! is_wp_error( $brands ) ) {
if ( count( $brands ) > 0 ) {
$show_as = get_option( 'wc_pwb_admin_tab_brands_in_single' );
if ( 'no' != $show_as ) {
do_action( 'pwb_before_single_product_brands', $brands );
echo '
';
if ( 'brand_link' == $show_as ) {
$label = apply_filters( 'pwb_text_before_brands_links', esc_html( _n( 'Brand', 'Brands', count( $brands ), 'perfect-woocommerce-brands' ) ), count( $brands ) );
if ( $label ) {
$before_brands_links = '
';
$before_brands_links .= $label;
$before_brands_links .= ':';
echo wp_kses_post( apply_filters( 'pwb_html_before_brands_links', $before_brands_links ) );
}
}
foreach ( $brands as $brand ) {
$brand_link = get_term_link( $brand->term_id, 'pwb-brand' );
$attachment_id = get_term_meta( $brand->term_id, 'pwb_brand_image', 1 );
$image_size = 'thumbnail';
$image_size_selected = get_option( 'wc_pwb_admin_tab_brand_logo_size', 'thumbnail' );
if ( false != $image_size_selected ) {
$image_size = $image_size_selected;
}
$attachment_html = wp_get_attachment_image( $attachment_id, $image_size );
if ( ! empty( $attachment_html ) && 'brand_image' == $show_as || ! empty( $attachment_html ) && ! $show_as ) {
echo '
' . $attachment_html . '';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
if ( $brand !== $brands[0] ) {
echo wp_kses_post( get_option( 'wc_pwb_admin_tab_brands_in_loop_separator', '' ) );
}
echo '
' . esc_html( $brand->name ) . '';
}
}
echo '
';
do_action( 'pwb_after_single_product_brands', $brands );
}
}
}
}
public function enqueue_scripts() {
wp_register_script(
'pwb-lib-slick',
PWB_PLUGIN_URL . '/assets/lib/slick/slick.min.js',
array( 'jquery' ),
'1.8.0',
false
);
wp_register_style(
'pwb-lib-slick',
PWB_PLUGIN_URL . '/assets/lib/slick/slick.css',
array(),
'1.8.0',
'all'
);
$frontend = include PWB_PLUGIN_DIR . 'build/frontend/js/index.asset.php';
wp_enqueue_style(
'pwb-styles-frontend',
plugins_url( '/build/frontend/css/style.css', PWB_PLUGIN_FILE ),
array(),
PWB_PLUGIN_VERSION,
'all'
);
wp_register_script(
'pwb-functions-frontend',
plugins_url( '/build/frontend/js/index.js', PWB_PLUGIN_FILE ),
$frontend['dependencies'],
$frontend['version'],
true
);
wp_localize_script(
'pwb-functions-frontend',
'pwb_ajax_object',
array(
'carousel_prev' => apply_filters( 'pwb_carousel_prev', '<' ),
'carousel_next' => apply_filters( 'pwb_carousel_next', '>' ),
)
);
wp_enqueue_script( 'pwb-functions-frontend' );
}
public function admin_enqueue_scripts( $hook ) {
$screen = get_current_screen();
if ( 'edit-tags.php' == $hook && 'pwb-brand' == $screen->taxonomy || 'term.php' == $hook && 'pwb-brand' == $screen->taxonomy ) {
wp_enqueue_media();
}
$backend = include PWB_PLUGIN_DIR . 'build/backend/js/index.asset.php';
wp_enqueue_style(
'pwb-styles-admin',
plugins_url( '/build/backend/css/style.css', PWB_PLUGIN_FILE ),
array(),
PWB_PLUGIN_VERSION
);
wp_enqueue_script(
'pwb-functions-admin',
plugins_url( '/build/backend/js/index.js', PWB_PLUGIN_FILE ),
$backend['dependencies'],
$backend['version'],
true
);
wp_localize_script(
'pwb-functions-admin',
'pwb_ajax_object_admin',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'site_url' => site_url(),
'brands_url' => admin_url( 'edit-tags.php?taxonomy=pwb-brand&post_type=product' ),
'translations' => array(
'migrate_notice' => esc_html__( '¿Start migration?', 'perfect-woocommerce-brands' ),
'migrating' => esc_html__( 'We are migrating the product brands. ¡Don´t close this window until the process is finished!', 'perfect-woocommerce-brands' ),
'dummy_data_notice' => esc_html__( '¿Start loading dummy data?', 'perfect-woocommerce-brands' ),
'dummy_data' => esc_html__( 'We are importing the dummy data. ¡Don´t close this window until the process is finished!', 'perfect-woocommerce-brands' ),
),
'nonce' => array(
'pwb_brands_export' => wp_create_nonce( 'pwb_brands_export' ),
'pwb_brands_import' => wp_create_nonce( 'pwb_brands_import' ),
'pwb_admin_set_featured_brand' => wp_create_nonce( 'pwb_admin_set_featured_brand' ),
'pwb_admin_save_screen_settings' => wp_create_nonce( 'pwb_admin_save_screen_settings' ),
'pwb_admin_dummy_data' => wp_create_nonce( 'pwb_admin_dummy_data' ),
'pwb_admin_migrate_brands' => wp_create_nonce( 'pwb_admin_migrate_brands' ),
'pwb_system_status' => wp_create_nonce( 'pwb_system_status' ),
),
)
);
wp_enqueue_script( 'pwb-functions-admin' );
}
public function register_brands_taxonomy() {
$labels = array(
'name' => esc_html__( 'Brands', 'perfect-woocommerce-brands' ),
'singular_name' => esc_html__( 'Brand', 'perfect-woocommerce-brands' ),
'menu_name' => esc_html__( 'Brands', 'perfect-woocommerce-brands' ),
'all_items' => esc_html__( 'All Brands', 'perfect-woocommerce-brands' ),
'edit_item' => esc_html__( 'Edit Brand', 'perfect-woocommerce-brands' ),
'view_item' => esc_html__( 'View Brand', 'perfect-woocommerce-brands' ),
'update_item' => esc_html__( 'Update Brand', 'perfect-woocommerce-brands' ),
'add_new_item' => esc_html__( 'Add New Brand', 'perfect-woocommerce-brands' ),
'new_item_name' => esc_html__( 'New Brand Name', 'perfect-woocommerce-brands' ),
'parent_item' => esc_html__( 'Parent Brand', 'perfect-woocommerce-brands' ),
'parent_item_colon' => esc_html__( 'Parent Brand:', 'perfect-woocommerce-brands' ),
'search_items' => esc_html__( 'Search Brands', 'perfect-woocommerce-brands' ),
'popular_items' => esc_html__( 'Popular Brands', 'perfect-woocommerce-brands' ),
'separate_items_with_commas' => esc_html__( 'Separate brands with commas', 'perfect-woocommerce-brands' ),
'add_or_remove_items' => esc_html__( 'Add or remove brands', 'perfect-woocommerce-brands' ),
'choose_from_most_used' => esc_html__( 'Choose from the most used brands', 'perfect-woocommerce-brands' ),
'not_found' => esc_html__( 'No brands found', 'perfect-woocommerce-brands' ),
);
$new_slug = get_option( 'wc_pwb_admin_tab_slug' );
$old_slug = get_option( 'old_wc_pwb_admin_tab_slug' );
$new_slug = ( false != $new_slug ) ? $new_slug : 'brand';
$old_slug = ( false != $old_slug ) ? $old_slug : 'null';
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'public' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'rewrite' => array(
'slug' => apply_filters( 'pwb_taxonomy_rewrite', $new_slug ),
'hierarchical' => true,
'with_front' => apply_filters( 'pwb_taxonomy_with_front', true ),
'ep_mask' => EP_PERMALINK,
),
);
register_taxonomy( 'pwb-brand', array( 'product' ), $args );
if ( false != $new_slug && false != $old_slug && $new_slug != $old_slug ) {
flush_rewrite_rules();
update_option( 'old_wc_pwb_admin_tab_slug', $new_slug );
}
}
public function add_brands_metafields() {
register_meta( 'term', 'pwb_brand_image', array( $this, 'add_brands_metafields_sanitize' ) );
}
public function add_brands_metafields_sanitize( $brand_img ) {
return $brand_img;
}
public static function get_brands( $hide_empty = false, $order_by = 'name', $order = 'ASC', $only_featured = false, $pwb_term = false, $only_parents = false ) {
$result = array();
$brands_args = array(
'hide_empty' => $hide_empty,
'orderby' => $order_by,
'order' => $order,
);
if ( $only_featured ) {
$brands_args['meta_query'] = array(
array(
'key' => 'pwb_featured_brand',
'value' => true,
),
);
}
if ( $only_parents ) {
$brands_args['parent'] = 0;
}
$brands = get_terms( 'pwb-brand', $brands_args );
foreach ( $brands as $key => $brand ) {
if ( $pwb_term ) {
$brands[ $key ] = new Term( $brand );
} else {
$brand_image_id = get_term_meta( $brand->term_id, 'pwb_brand_image', true );
$brand_banner_id = get_term_meta( $brand->term_id, 'pwb_brand_banner', true );
$brand->brand_image = wp_get_attachment_image_src( $brand_image_id );
$brand->brand_banner = wp_get_attachment_image_src( $brand_banner_id );
}
}
if ( is_array( $brands ) && count( $brands ) > 0 ) {
$result = $brands;
}
return $result;
}
public static function get_brands_array( $is_select = false ) {
$result = array();
/**
* If is for select input adds default value
*/
if ( $is_select ) {
$result[0] = esc_html__( 'All', 'perfect-woocommerce-brands' );
}
$brands = get_terms(
'pwb-brand',
array(
'hide_empty' => false,
)
);
foreach ( $brands as $brand ) {
$result[ $brand->term_id ] = $brand->slug;
}
return $result;
}
public function print_brand_banner() {
$queried_object = get_queried_object();
$brand_banner = get_term_meta( $queried_object->term_id, 'pwb_brand_banner', true );
$brand_banner_link = get_term_meta( $queried_object->term_id, 'pwb_brand_banner_link', true );
$show_banner = get_option( 'wc_pwb_admin_tab_brand_banner' );
$show_banner = get_option( 'wc_pwb_admin_tab_brand_banner' );
$show_banner_class = ( ! $show_banner || 'yes' == $show_banner ) ? 'pwb-before-loop' : 'pwb-after-loop';
if ( '' != $brand_banner ) {
echo '';
}
}
public function print_brand_desc() {
$queried_object = get_queried_object();
$show_desc = get_option( 'wc_pwb_admin_tab_brand_desc' );
$show_desc = get_option( 'wc_pwb_admin_tab_brand_desc' );
$show_desc_class = ( ! $show_desc || 'yes' == $show_desc ) ? 'pwb-before-loop' : 'pwb-after-loop';
if ( '' != $queried_object->description && 'no' !== $show_desc ) {
echo '';
echo do_shortcode( apply_filters( 'the_content', $queried_object->description ) );
echo '
';
}
}
public function print_brand_banner_and_desc() {
$queried_object = get_queried_object();
$show_desc = get_option( 'wc_pwb_admin_tab_brand_desc' );
$show_desc_class = ( ! $show_desc || 'yes' == $show_desc ) ? 'pwb-before-loop' : 'pwb-after-loop';
$brand_banner = get_term_meta( $queried_object->term_id, 'pwb_brand_banner', true );
$brand_banner_link = get_term_meta( $queried_object->term_id, 'pwb_brand_banner_link', true );
if ( '' != $brand_banner || '' != $queried_object->description && 'no' !== $show_desc ) {
echo '';
$this->print_brand_banner();
$this->print_brand_desc();
echo '
';
}
}
public static function render_template( $name = '', $folder = '', $data = array(), $private = true ) {
/**
* Default template
*/
if ( $folder ) {
$folder = $folder . '/';
}
$template_file = dirname( __DIR__ ) . '/templates/' . $folder . $name . '.php';
/**
* Theme overrides
*/
if ( ! $private ) {
$theme_template_path = get_stylesheet_directory() . '/perfect-woocommerce-brands/';
if ( file_exists( $theme_template_path . $folder . $name . '.php' ) ) {
$template_file = $theme_template_path . $folder . $name . '.php';
}
}
extract( $data );
ob_start();
include $template_file;
return ob_get_clean();
}
public function product_duplicate_save( $duplicate, $product ) {
$product_brands = wp_get_object_terms( $product->get_id(), 'pwb-brand', array( 'fields' => 'ids' ) );
wp_set_object_terms( $duplicate->get_id(), $product_brands, 'pwb-brand' );
}
public function breadcrumbs( $crumbs ) {
if ( is_tax( 'pwb-brand' ) ) {
$brands_page_id = get_option( 'wc_pwb_admin_tab_brands_page_id' );
if ( ! empty( $brands_page_id ) && '-' != $brands_page_id ) {
$cur_brand = get_queried_object();
$brand_ancestors = get_ancestors( $cur_brand->term_id, 'pwb-brand', 'taxonomy' );
$brand_page_pos = count( $crumbs ) - ( count( $brand_ancestors ) + 2 );
if ( is_paged() ) {
--$brand_page_pos;
}
if ( isset( $crumbs[ $brand_page_pos ][1] ) ) {
$crumbs[ $brand_page_pos ][1] = get_page_link( $brands_page_id );
}
}
}
return $crumbs;
}
}