Instantly share code, notes, and snippets.

Embed
What would you like to do?
FoxyShop Wholesale Support Plugin
<?php /*
**************************************************************************
Plugin Name: FoxyShop Wholesale User Add-on
Plugin URI: http://www.foxytools.com/
Description: Adds Wholesale User Support, Advanced Custom Fields required
Author: SparkWeb Interactive, Inc.
Version: 1.0
Author URI: http://www.sparkweb.net/
**************************************************************************/
//Create User Role on Activation
register_activation_hook(__FILE__, 'add_roles_on_plugin_activation');
function add_roles_on_plugin_activation() {
add_role('wholesale', 'Wholesale', array('read' => true, 'level_0' => true));
}
//Check User Role
function check_user_role($roles,$user_id=NULL) {
if ($user_id)
$user = get_userdata($user_id);
else
$user = wp_get_current_user();
// No user found, return
if (empty($user))
return FALSE;
// Append administrator to roles, if necessary
if (!is_array($roles)) $roles = array($roles);
// Loop through user roles
foreach ($user->roles as $role) {
// Does user have role
if (in_array($role,$roles)) {
return TRUE;
}
}
// User not in roles
return FALSE;
}
//Notice about ACF Requirement
function foxyshop_role_install_acf_prompt() {
if (!function_exists("register_field_group")) {
?>
<div class="updated">
<p>In order to allow the FoxyShop Wholesale Role functionality, please install <a href="plugin-install.php?tab=search&amp;s=advanced+custom+fields">Advanced Custom Fields</a>.</p>
</div>
<?php
}
}
add_action( 'admin_notices', 'foxyshop_role_install_acf_prompt' );
//Check Products
add_filter('foxyshop_setup_product_info', 'foxyshop_role_check_products', 10, 2);
function foxyshop_role_check_products($product, $product_id) {
global $post;
//Check Access
if (check_user_role(array("wholesale", "administrator")) || !function_exists("get_field")) {
return $product;
}
if (get_field("access_restriction") == "wholesale" && !check_user_role(array("wholesale", "administrator"))) {
$product['hide_product'] = 1;
}
$product_categories = wp_get_post_terms($product['id'], 'foxyshop_categories', array("fields" => "all"));
if ($product_categories && !is_user_logged_in()) {
foreach ($product_categories as $term) {
$access_restriction = get_field("access", $term->taxonomy . "_" . $term->term_id);
if ($access_restriction == "wholesale") {
$product['hide_product'] = 1;
}
}
}
return $product;
}
//Custom Category Code
add_filter("foxyshop_category_children_write", "my_foxyshop_category_children_write", 10, 2);
function my_foxyshop_category_children_write($liwrite, $term) {
if (!is_object($term) || !function_exists("get_field")) {
return $liwrite;
}
//Check For Wholesale
$access_restriction = get_field("access_restriction", $term->taxonomy . "_" . $term->term_id);
if ($access_restriction == "wholesale" && !check_user_role(array("wholesale", "administrator"))) {
return "";
}
return $liwrite;
}
//Don't Allow
add_action('admin_init', 'foxyshop_role_remove_dashboard');
function foxyshop_role_remove_dashboard() {
if (!current_user_can('manage_options') && $_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') {
wp_redirect(home_url());
exit;
}
}
add_action('after_setup_theme', 'foxyshop_role_remove_admin_bar');
function foxyshop_role_remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
add_action('admin_init', 'foxyshop_role_register_field_group');
function foxyshop_role_register_field_group() {
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_product-category',
'title' => 'Product Category',
'fields' => array (
array (
'key' => 'field_532a0be23246b',
'label' => 'Access Restriction',
'name' => 'access_restriction',
'type' => 'select',
'choices' => array (
'none' => 'None',
'wholesale' => 'Wholesale Only',
),
'default_value' => '',
'allow_null' => 0,
'multiple' => 0,
),
),
'location' => array (
array (
array (
'param' => 'ef_taxonomy',
'operator' => '==',
'value' => 'foxyshop_categories',
'order_no' => '0',
'group_no' => '0',
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
register_field_group(array (
'id' => 'acf_product-details',
'title' => 'Product Details',
'fields' => array (
array (
'key' => 'field_532a0b8fed02f',
'label' => 'Access Restriction',
'name' => 'access_restriction',
'type' => 'select',
'choices' => array (
'none' => 'None',
'wholesale' => 'Wholesale Only',
),
'default_value' => '',
'allow_null' => 0,
'multiple' => 0,
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'foxyshop_product',
'order_no' => '0',
'group_no' => '0',
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment