%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/cwg/wp-content/themes/cwg/inc/
Upload File :
Create Path :
Current File : //var/www/cwg/wp-content/themes/cwg/inc/admin.php

<?php

add_theme_support('menus');
add_theme_support('custom-logo');

add_filter( 'allow_dev_auto_core_updates', '__return_false' );

function register_main_menu()
{
    register_nav_menus(
        [
            'main-menu' => 'Main Menu',
            'footer-menu' => 'Footer Menu'
        ]
    );
}
add_action('init', 'register_main_menu');

// Enable support for post formats
add_theme_support('post-formats', array('aside', 'image', 'video', 'quote', 'link'));

// Enable support for featured images
add_theme_support('post-thumbnails');

// Enable title tag
add_theme_support('title-tag');

add_action('acf/init', 'my_acf_op_init');
function my_acf_op_init()
{

    // Check function exists.
    if (function_exists('acf_add_options_page')) {

        // Add parent.
        $main_options_page = acf_add_options_page(array(
            'page_title'  => __('CWG - Settings'),
            'menu_title'  => __('CWG - Settings'),
            'menu_slug' => 'theme-options',
            'redirect'    => false,
        ));
    }
}

/** "New user" email to generic sales adress instead of admin. */
/*add_filter( 'wp_new_user_notification_email_admin', 'my_wp_new_user_notification_email_admin', 10, 3 );
function my_wp_new_user_notification_email_admin( $notification, $user, $blogname ) {
    $notification['to'] = 'clement.caira@gmail.com';
    return $notification;
}*/

add_filter('wp_new_user_notification_email', 'create_user_from_admin_notification_email', 10, 3);

function create_user_from_admin_notification_email($wp_new_user_notification_email, $user, $blogname) {    
    if (is_admin() && current_user_can('administrator')) {
        $roles = $user->roles;
        $allow_roles = ['customer_b2b', 'customer'];
        $result = array_intersect($roles, $allow_roles);
        $is_allow_roles = (!empty($result)) ? true : false;
        if ($is_allow_roles) {
            $current_user = wp_get_current_user();
            $email_admin  = $current_user->user_email;
            $wp_new_user_notification_email['to'] = $email_admin;
        }
        
    }
    return $wp_new_user_notification_email;
}

add_action( 'profile_update', 'custom_profile_update', 10, 2 );
 
function custom_profile_update( $user_id, $old_user_data ) {
    change_role_b2c_to_b2b($user_id);
}

// Email user when admin change role b2c to b2b
function user_role_update( $user_id, $new_role ) {
    change_role_b2c_to_b2b($user_id);
}

add_action( 'set_user_role', 'user_role_update', 10, 2);

function change_role_b2c_to_b2b($user_id) {
    if (is_admin() && current_user_can('administrator')) {
        $site_url = get_bloginfo('wpurl');
        $user_info = get_userdata( $user_id );

        if(in_array( 'customer_b2b', (array) $user_info->roles )){
            $new_role = ucfirst(str_replace(['_'], [' '], get_role('customer_b2b')->name));
            $current_user = wp_get_current_user();
            $to  = $current_user->user_email;
            $subject = "Role changed: ".$site_url."";
            $message = "Hello " . $user_info->display_name . " your role has changed on " . $site_url . ", congratulations you are now an " . $new_role;
           
            wp_mail($to, $subject, $message);
        }
    }
}

function user_enquire_product( $user_id, $new_role ) {
    $site_url = get_bloginfo('wpurl');
    $user_info = get_userdata( $user_id );
    $to = $user_info->user_email;
    $subject = "Role changed: ".$site_url."";
    $message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role;
    wp_mail($to, $subject, $message);
}


function add_customer_roles() {
    if ( get_option( 'custom_roles_version' ) < 1 ) {
        add_role( 'customer_b2b', 'Customer B2B', get_role( 'customer' )->capabilities );
        update_option( 'custom_roles_version', 1 );
    }
}
add_action( 'init', 'add_customer_roles' );

const URL_ARG = 'redirect_to_checkout';

function has_query_param () {
    return isset( $_GET[ URL_ARG ] );
}

function redirect_to_checkout ( ) {
    if ( has_query_param() ) {
        wp_safe_redirect( wc_get_checkout_url() );
        die;
    }
}

function redirect_to_account_page () {
    if( is_checkout() && ! is_user_logged_in() ) {
        wp_safe_redirect( add_query_arg( URL_ARG, '', '/login' ) );
        die;
    }
}

add_action( 'template_redirect', 'redirect_to_account_page' );
add_action( 'wp_login', 'redirect_to_checkout' );

function add_to_wishlist_hook($wishlistId) {
    
    $actito = new Actito();
    if($_POST['action'] == "add_to_wishlist"){
        $itemId = intval($_POST['add_to_wishlist']);
        $email = wp_get_current_user()->user_email;
        
        $product = get_post($itemId);
        $artist = get_field('artist_label', $itemId);
        $category = get_the_terms( $itemId, 'product_cat' )[0];
        
        $actito->addWishlist($email, $wishlistId);
        $actito->addWishlistDetail($email, $wishlistId, $wishlistId.$itemId, $artist, $product->post_title, $category->name);
    }
    else {
        $itemId = intval($_POST['remove_from_wishlist']);
        
        $actito->addWishlist($email, $wishlistId);
        $actito->removeWishlistDetail($wishlistId.$itemId);
    }
}

add_action( 'yith_wcwl_update_wishlist', 'add_to_wishlist_hook', 100, 1 );

function new_wishlist_hook($wishlistId) {
    $email = wp_get_current_user()->user_email;

    $actito = new Actito();
    $actito->addWishlist($email, $wishlistId);
}

add_action( 'yith_wcwl_new_wishlist', 'new_wishlist_hook', 100, 1 );

function delete_wishlist_hook($wishlistId) {
    $items = YITH_WCWL_Wishlist_Factory::get_wishlist($wishlistId)->get_items();
    $actito = new Actito();
    foreach ($items as $itemId => $value) {
        $actito->removeWishlistDetail($wishlistId.$itemId);
    }
    $actito->removeWishlist($wishlistId);
}

add_action( 'yith_wcwl_before_delete_wishlist', 'delete_wishlist_hook', 100, 1 );

function enquire_hook() {
    $actito = new Actito();
    $arrayId = $_POST['data']['id'];
    foreach ($arrayId as $id) {
        $interactionID = $id.time();
        $email = wp_get_current_user()->user_email;

        $product = get_post($id);
        $artist = get_field('artist_label', $id);
        $category = get_the_terms( $id, 'product_cat' )[0];
        $actito->addEnquiry($interactionID, $email, $artist, $product->post_title, $category->name);
    }
}

add_action( 'enquire_mail', 'enquire_hook', 100, 0 );

function hook_order_paid( $order_ID ) {
	$order = wc_get_order( $order_ID );
    $email = wp_get_current_user()->user_email;
    $data = $order->get_data();
	$items = $data['line_items'];

    $actito = new Actito();
    
    $orderPaymentMethod = $data['payment_method'];
    $orderCouponCode = $data['coupon_lines'];
    $orderCurrency = $data['currency'];
    $orderTotalArticleAmount = $order->get_item_count();
    $orderTotalPrice = $data['total'];
    
    $orderStreet = $data['shipping']['address_1'];
    $orderPostalCode = $data['shipping']['postcode'];
    $orderCity = $data['shipping']['city'];
    $orderCountry = $data['shipping']['country'];
    $orderShippingNote = $data['customer_note'];
    
    $orderDate = $data['date_paid']->__toString();
    $orderStatus = $data['status'];

    $paramsOrder = [
        'orderPaymentMethod' => $orderPaymentMethod,
        'orderCouponCode' => $orderCouponCode,
        'orderCurrency' => $orderCurrency,
        'orderTotalArticleAmount' => $orderTotalArticleAmount,
        'orderTotalPrice' => $orderTotalPrice,
        'orderStreet' => $orderStreet,
        'orderPostalCode' => $orderPostalCode,
        'orderCity' => $orderCity,
        'orderCountry' => $orderCountry,
        'orderShippingNote' => $orderShippingNote,
        'orderDate' => $orderDate,
        'orderStatus' => $orderStatus
    ];

    $actito->addOrder($email, $order_ID, $paramsOrder);
        
    foreach ($items as $item) {
        $dataItems = $item->get_data();

        $productId = $dataItems['product_id'];
        
        $product = new WC_product($productId);
        $artist = get_field('artist_label', $productId);
        $category = get_the_terms( $productId, 'product_cat' )[0];

        $orderLineTotalPrice = $dataItems['total'];
        $unitQty = $dataItems['quantity'];
        $unitPriceVAT = $dataItems['total_tax'];
        $productImage = wp_get_attachment_url($product->get_image_id());

        $paramsOrderDetail = [
            'orderLineTotalPrice' => $orderLineTotalPrice,
            'unitQty' => $unitQty,
            'unitPriceVAT' => $unitPriceVAT,
            'productImage' => $productImage
        ];

        $actito->addOrderDetail($email, $order_ID, $order_ID.$productId, $artist, $product->name, $category->name, $paramsOrderDetail);
    }
}

add_action( 'woocommerce_order_status_processing', 'hook_order_paid' );


function custom_signin() {
    if(isset($_POST["log"]) && isset($_POST["pwd"])) {
        $log=$_POST['log'];
        $pwd=$_POST['pwd'];
        wp_signon(
            array(
                'user_login'    =>   $log,
                'user_password'     =>   $pwd
            )
        );
        $location = $_SERVER['HTTP_REFERER'];
        wp_safe_redirect($location);
        exit();
    }
}

function custom_register() {
    if (isset($_POST['user_registeration']))
    {
        global $signUpError;
        $reg_errors = new WP_Error;
        $useremail=$_POST['useremail'];
        $password=$_POST['password'];
        $userfirstname = $_POST['userfirstname'];
        $userlastname = $_POST['userlastname'];

        $uppercase = preg_match('@[A-Z]@', $password);
        //$lowercase = preg_match('@[a-z]@', $password);
        $number    = preg_match('@[0-9]@', $password);
        $specialChars = preg_match('@[^\w]@', $password);

        if(!isset($_POST['coocheck'])) {
            $reg_errors->add('field', 'Please confirm that you have read ans agree to our Terms & Conditions');
        }
        if(empty( $useremail ) || empty($password) || empty($userfirstname) || empty($userlastname))
        {
            $reg_errors->add('field', 'Required form field is missing');
        }
        if ( !is_email( $useremail ) )
        {
            $reg_errors->add( 'email_invalid', 'Email id is not valid!' );
        }
        if ( email_exists( $useremail ) )
        {
            $reg_errors->add( 'email', 'Email Already exist!' );
        }
        if ( !$uppercase || !$number || !$specialChars || strlen($password) < 8 ) {
            $reg_errors->add( 'password', 'Password length must be greater than 8 and should include at least one upper case letter, one number, and one special character.' );
        }
        if (count( $reg_errors->get_error_messages() ) != 0)
        {
            foreach ( $reg_errors->get_error_messages() as $error )
            {
                $signUpError .= '<p style="color:#e88931; text-aling:left; font-size: 12px; font-weight: 300;">'.$error . '<br/></p>';
            }
        } else {
            $useremail  =   sanitize_email( $_POST['useremail'] );
            $communicationpreferences  =  ( $_POST['mailcheck'] ?? false );
            $birthdate = $_POST['birthdate'];
            $country = $_POST['country'];
            $password   =   esc_attr( $_POST['password'] );
            $userdata = array(
                'user_login'    =>   $useremail,
                'user_email'    =>   $useremail,
                'first_name'    =>   $userfirstname,
                'last_name'     =>   $userlastname,
                'user_pass'     =>   $password
                );

            $actito = new Actito();
            $actitoUser = json_decode($actito->createOrUpdateUser($useremail, $country, $userfirstname, $userlastname, $birthdate), true);

            if($communicationpreferences) {
                $actito->optInUser($actitoUser['profileId']);
            }

            $user = wp_insert_user( $userdata );

            update_field( 'newsletter_carpenters', $communicationpreferences , 'user_'.$user );
            update_field( 'newsletter_design', $communicationpreferences , 'user_'.$user );
            update_field( 'contact_email', $communicationpreferences , 'user_'.$user );
            update_field( 'communication_preferences', $communicationpreferences , 'user_'.$user );

            update_field( 'birthdate', $birthdate , 'user_'.$user );
            update_field( 'country', $country , 'user_'.$user );
            wp_signon(
                array(
                    'user_login'    =>   $useremail,
                    'user_password'     =>   $password
                )
            );
            wp_redirect(home_url());
        }
    }
}
add_image_size( 'collectibleCat', 600 );

function update_communication_preferences() {
    $user = wp_get_current_user();
    $userId = $user->ID;
    $userEmail = $user->user_email;
    
    if (isset($_POST['update_communication_preferences'])) {
        $actito = new Actito();
        $actitoId = json_decode($actito->createOrUpdateUser($userEmail), true)['profileId'];

        if (isset($_POST['newscarcheck'])) {
            $newslettercarpenters  =  $_POST['newscarcheck'];
            $actito->optInCarpentersNewsletterUser($actitoId);
        } elseif (!isset($_POST['newscarcheck'])) {
            $newslettercarpenters  = false;
            $actito->optOutCarpentersNewsletterUser($actitoId);
        }
        if (isset($_POST['newsdescheck'])) {
            $newsletterdesign  =  $_POST['newsdescheck'];
            $actito->optInTheDesignEditNewsletterUser($actitoId);
        } elseif (!isset($_POST['newsdescheck'])) {
            $newsletterdesign  = false;
            $actito->optOutTheDesignEditNewsletterUser($actitoId);
        }
        if (isset($_POST['emailcheck'])) {
            $contactmail  =  $_POST['emailcheck'];
            $actito->optInMarketingUser($actitoId);
        } elseif (!isset($_POST['emailcheck'])) {
            $contactmail  = false;
            $actito->optOutMarketingUser($actitoId);
        }

        update_field( 'newsletter_carpenters', $newslettercarpenters , 'user_'.$userId );
        update_field( 'newsletter_design', $newsletterdesign , 'user_'.$userId );
        update_field( 'contact_email', $contactmail , 'user_'.$userId );
    }
}

// Changed wording of cta in checkout woocommerce 
function edit_woocomerce_label( $translated, $text, $domain ) {
    switch ($translated) {
        case 'Update basket':
            return 'Update bag';
            break;
        case 'Basket totals':
            return 'Bag totals';
            break;
        default:
            return $translated;
            break;
    }
}
add_filter( 'gettext', 'edit_woocomerce_label', 20, 3 );

// Add custom checkout field: woocommerce_review_order_before_submit
add_action( 'woocommerce_review_order_before_submit', 'my_custom_checkout_field' );
function my_custom_checkout_field() {
    echo '<div id="my_custom_checkout_field">';

    woocommerce_form_field( 'my_field_name', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('Suscribe to mailing list'),
    ),  WC()->checkout->get_value( 'my_field_name' ) );
    echo '</div>';
}

// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 1 );
function custom_checkout_field_update_order_meta( $order_id ) {

    if ( ! empty( $_POST['my_field_name'] ) )
        update_post_meta( $order_id, 'my_field_name', $_POST['my_field_name'] );
}

// Display the custom field result on the order edit page (backend) when checkbox has been checked
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
function display_custom_field_on_order_edit_pages( $order ){
    $my_field_name = get_post_meta( $order->get_id(), 'my_field_name', true );
    if( $my_field_name == 1 )
        echo '<p><strong>My custom field: </strong> <span style="color:red;">Is enabled</span></p>';
}



add_filter( 'intermediate_image_sizes_advanced', 'prefix_remove_default_images' );

function prefix_remove_default_images( $sizes ) {
    // unset( $sizes['small']); // 150px
    unset( $sizes['medium']); // 300px
    unset( $sizes['large']); // 1024px
    unset( $sizes['medium_large']); // 768px
    unset( $sizes[ 'shop_thumbnail' ]);  // Remove Shop thumbnail (180 x 180 hard cropped)
    unset( $sizes[ 'shop_catalog' ]);    // Remove Shop catalog (300 x 300 hard cropped)
    // unset( $sizes[ 'shop_single' ]);     // Shop single (600 x 600 hard cropped)
    return $sizes;
}


add_action('woocommerce_add_to_cart', 'update_actito_cart');
function update_actito_cart()
{
    $actito = new Actito();
    $id = $_POST['product_id'];

    $interactionID = $id . time();
    $email = wp_get_current_user()->user_email;

    $product = get_post($id);
    $artist = get_field('artist_label', $id);
    $category = get_the_terms($id, 'product_cat')[0];
    $actito->addToCart($interactionID, $email, $artist, $product->post_title, $category->name);
}

Zerion Mini Shell 1.0