File: /home/zeusxp5/chuair.org/wp-content/themes/lifttruck/functions.php
<?php
/**
*----------------- include ------------------------------------------
*/
include_once(get_template_directory() . '/inc/tools.php');
include_once(get_template_directory() . '/inc/blocks.php');
include_once(get_template_directory() . '/inc/block-styles.php');
include_once(get_template_directory() . '/inc/meta-box.php');
include_once(get_template_directory() . '/inc/classes/wdevia_base_class.php' );
function lifttruck_init () {
return theme_base_class::instance();
}
lifttruck_init();
require_once(get_template_directory() . '/inc/base-functions.php');
include_once(get_template_directory() . '/inc/panel/PanelClass.php');
include_once(get_template_directory() . '/inc/installer.php');
include_once(get_template_directory() . '/inc/theme-support.php' );
require_once(get_template_directory() . '/inc/aq_resizer.php');
include_once(get_template_directory() . '/inc/enqueue-scripts.php' );
require_once( get_template_directory() . '/inc/image-presets.php' );
//** Add sticky header class */
function lifttruck_body_classes( $classes ) {
$post_id = get_the_ID();
$is_custom_header = get_post_meta( $post_id, 'wd_custom_header_options', true ) == 'custom';
$sticky_header_meta = get_post_meta( $post_id, 'wd_sticky_header', true );
$transparent_header_meta = get_post_meta( $post_id, 'wd_transparent_header', true );
$header_cap_meta = get_post_meta( $post_id, 'wd_header_cap', true );
$sticky_header_option = lifttruck_get_option("sticky_header");
$transparent_header_option = lifttruck_get_option('transparent_header');
if( ($sticky_header_option and !$is_custom_header) or ($is_custom_header and $sticky_header_meta) ) {
$classes[] = 'has-sticky-header';
}
if( ($transparent_header_option and !$is_custom_header) or ($is_custom_header and $transparent_header_meta)) {
if( ($is_custom_header & !$header_cap_meta) || get_post_type() == 'services' ) {
$classes[] = 'has-transparent-header not-has-header-cap';
}else{
$classes[] = 'has-transparent-header has-header-cap';
}
}
$classes[] = 'has-dark-sticky-header';
return $classes;
}
add_filter( 'body_class','lifttruck_body_classes' );
add_filter( 'body_class','lifttruck_body_classes' );
update_option('has_services_post_type', true);
if ( ! isset( $content_width ) ) $content_width = 900;
add_theme_support( 'automatic-feed-links' );
add_theme_support( "title-tag" );
function lifttruck_widgets_init(){
//--------------- Widget for Right Sidebar
register_sidebar(array(
'name' => esc_html__('Sidebar Right', 'lifttruck'),
'id' => 'sidebar-1',
'description' => esc_html__('Add widgets here to appear in your right sidebar.', 'lifttruck'),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h4 class="block-title">',
'after_title' => '</h4>',
));
}
add_action('widgets_init', 'lifttruck_widgets_init');
add_action( 'init', 'lifttruck_register_block_bindings' );
function lifttruck_register_block_bindings() {
register_block_bindings_source( 'lifttruck/header-data', array(
'label' => __( 'User Data', 'lifttruck' ),
'get_value_callback' => 'lifttruck_user_data_bindings'
) );
}
function lifttruck_user_data_bindings( $source_args ) {
// If no key or user ID argument is set, bail early.
if ( ! isset( $source_args['key'] ) ) {
return null;
}
// Return the data based on the key argument.
switch ( $source_args['key'] ) {
case 'address':
return "125 Av. Green Mount, NYC";
case 'address-icon':
return get_stylesheet_directory_uri() . "/assets/images/address.svg";
case 'tell':
return "(06) 458 968 365";
case 'tell-icon':
return get_stylesheet_directory_uri() . "/assets/images/phone.svg";
case 'working-hours':
return "9AM - 5PM Mon-Fri";
case 'working-hours-icon':
return get_stylesheet_directory_uri() . "/assets/images/working-hours.svg";
case 'description':
return '';
default:
return null;
}
}
// function add_attribute( $attribute, $att_value, $product_id ) {
// //Get product object from the ID
// $_product = wc_get_product($product_id);
// $attributes = $_product->get_attributes();
// $add_option = wp_set_object_terms( $product_id, $att_value, $attribute, true );
// $curr_options = $attributes[ $attribute ]['options'];
// echo "_____________________________";
// wd_var_dump($curr_options);
// wd_var_dump($add_option);
// $updated_options = array_push($curr_options,$add_option);
// wd_var_dump($updated_options);
// if (!in_array($add_option, $curr_options)) {
// $curr_options[] = $add_option;
// }
// wd_var_dump($curr_options);
// //Check if we have this attribute set already
// if(!in_array($add_option,$curr_options)){
// /* $updated_options = array_push($curr_options,$add_option);
// $data = array(
// 'pa_width' => array(
// 'name'=>'pa_width',
// 'options'=> $updated_options,
// 'is_visible' => '1',
// 'is_variation' => '0',
// 'is_taxonomy' => '1'
// )
// );
// update_post_meta($product_id, '_product_attributes', $data ); */
// }
// }
// /**
// * Add attribute to all WooCommerce products programmatically
// */
// function add_attribute_to_all_products() {
// // Get all products
// $args = array(
// 'post_type' => 'product',
// 'posts_per_page' => -1,
// );
// $products = new WP_Query( $args );
// // Check if there are any products
// if ( $products->have_posts() ) {
// for ( $i = 0; $i < $products->post_count; $i++ ) {
// $products->the_post();
// // Get product ID
// $product_id = get_the_ID();
// //Get product object from the ID
// $_product = wc_get_product($product_id);
// $attributes = $_product->get_attributes();
// wd_var_dump($attributes);
// foreach( $attributes as $key => $attribute ) {
// // wd_var_dump($key);
// // wd_var_dump($attribute);
// // wd_var_dump($attributes[$key]['options']);
// add_attribute( $key, '245', $product_id );
// }
// // $add_option = wp_set_object_terms( $product_id, '245', 'pa_width', true );
// // $curr_options = $attributes['pa_width']['options'];
// // //Check if we have this attribute set already
// // if(!in_array($add_option,$curr_options)){
// // $updated_options = array_push($curr_options,$add_option);
// // $data = array(
// // 'pa_width' => array(
// // 'name'=>'pa_width',
// // 'options'=> $updated_options,
// // 'is_visible' => '1',
// // 'is_variation' => '0',
// // 'is_taxonomy' => '1'
// // )
// // );
// // update_post_meta($product_id, '_product_attributes', $data );
// // }
// }
// }
// wp_reset_postdata();
// }
// // Hook into WordPress init action to run the function
// add_action( 'init', 'add_attribute_to_all_products' );
function create_product_with_red_color() {
$product_data = array(
'name' => 'Red Product',
'type' => 'simple',
'regular_price' => '10.00',
'description' => 'This is a red product',
'short_description' => 'Red color',
'categories' => array('red'),
'attributes' => array(
array(
'name' => 'pa_color',
'options' => array('Red'),
'position' => 0,
'visible' => true,
'variation' => true,
),
),
);
$product_id = wp_insert_post(array(
'post_title' => $product_data['name'],
'post_content' => $product_data['description'],
'post_excerpt' => $product_data['short_description'],
'post_status' => 'publish',
'post_type' => 'product',
'post_author' => 1,
));
update_post_meta($product_id, '_regular_price', $product_data['regular_price']);
update_post_meta($product_id, '_price', $product_data['regular_price']);
update_post_meta($product_id, '_visibility', 'visible');
update_post_meta($product_id, '_stock_status', 'instock');
foreach ($product_data['categories'] as $category) {
wp_set_object_terms($product_id, $category, 'product_cat');
}
$attributes = array();
foreach ($product_data['attributes'] as $attribute) {
$attribute_id = wc_attribute_taxonomy_id_by_name($attribute['name']);
$attribute_values = array_map('sanitize_title', $attribute['options']);
$attribute['value'] = implode('|', $attribute_values);
$attributes[$attribute_id] = $attribute;
}
update_post_meta($product_id, '_product_attributes', $attributes);
}
function pro_auto_create_product_with_attributes(){
$attributes_values = array(
array(
'name' => 'pa_color',
'values' => array('Red', 'Blue', 'Green'),
'position' => 0,
'visible' => true,
'variation' => true,
),
array(
'name' => 'pa_size',
'values' => array('Small', 'Medium', 'Large'),
'position' => 0,
'visible' => true,
'variation' => true,
),
);
create_product_with_red_color($attributes_values);
}
//add_action( 'init', 'pro_auto_create_product_with_attributes' );
// // Get the term ID of the attribute term
// $term = get_term_by('name', '235', 'pa_width');
// // Check if the term was found
// if ($term && !is_wp_error($term)) {
// // Output the term description
// echo $term->description;
// } else {
// echo 'Term not found.';
// }
// Function to create WooCommerce products
function createWooProducts($productsData) {
$createdProductIds = array();
foreach ($productsData as $productData) {
// Create product post
$newProduct = array(
'post_title' => $productData['name'],
'post_content' => $productData['description'],
'post_excerpt' => $productData['short_description'],
'post_status' => 'publish',
'post_type' => 'product'
);
$productId = wp_insert_post($newProduct);
if (!is_wp_error($productId)) {
// Set product regular price
update_post_meta($productId, '_regular_price', $productData['regular_price']);
update_post_meta($productId, '_price', $productData['regular_price']);
// Set product attributes
foreach ($productData['attributes'] as $attribute) {
wp_set_object_terms($productId, $attribute['options'], 'pa_' . $attribute['name'], true);
}
// Set product categories
foreach ($productData['categories'] as $categoryId) {
wp_set_post_terms($productId, array($categoryId), 'product_cat', true);
}
$createdProductIds[] = $productId;
} else {
echo 'Failed to create product: ' . $productId->get_error_message();
}
}
return $createdProductIds;
}
// Example usage
$productsData = array(
array(
'name' => 'Product 1',
'regular_price' => '10.00',
'description' => 'Description for Product 1',
'short_description' => 'Short description for Product 1',
'categories' => array(1), // Assuming category ID
'attributes' => array(
array(
'name' => 'width',
'options' => array('Small', 'Medium', 'Large')
),
array(
'name' => 'ratio',
'options' => array('4:3', '16:9')
)
)
),
array(
'name' => 'Product 2',
'regular_price' => '15.00',
'description' => 'Description for Product 2',
'short_description' => 'Short description for Product 2',
'categories' => array(2), // Assuming category ID
'attributes' => array(
array(
'name' => 'width',
'options' => array('Small', 'Medium', 'Large')
),
array(
'name' => 'ratio',
'options' => array('4:3', '16:9')
)
)
)
);
/* $createdProductIds = createWooProducts($productsData);
print_r($createdProductIds); */
/*
$target_products = array(
'post_type' => 'product',
'post_status' => 'publish',
//'product_cat' => 'Living Room Light Balls',
'posts_per_page'=>-1
);
$my_query = new WP_Query( $target_products );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$term_taxonomy_ids = wp_set_object_terms( get_the_ID(), '21', 'pa_width', true );
$thedata = Array('pa_width'=>Array(
'name'=>'pa_width',
'value'=>'20',
'is_visible' => '1',
'is_taxonomy' => '1'
));
update_post_meta( get_the_ID(),'_product_attributes',$thedata);
endwhile;
}
wp_reset_query(); */
// // Create Product attributes if not exist
// function create_product_attribute( $label_name ){
// global $wpdb;
// $slug = sanitize_title( $label_name );
// if ( strlen( $slug ) >= 28 ) {
// return new WP_Error( 'invalid_product_attribute_slug_too_long', sprintf( __( 'Name "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
// } elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
// return new WP_Error( 'invalid_product_attribute_slug_reserved_name', sprintf( __( 'Name "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
// } elseif ( taxonomy_exists( wc_attribute_taxonomy_name( $label_name ) ) ) {
// return new WP_Error( 'invalid_product_attribute_slug_already_exists', sprintf( __( 'Name "%s" is already in use. Change it, please.', 'woocommerce' ), $label_name ), array( 'status' => 400 ) );
// }
// $data = array(
// 'attribute_label' => $label_name,
// 'attribute_name' => $slug,
// 'attribute_type' => 'select',
// 'attribute_orderby' => 'menu_order',
// 'attribute_public' => 0, // Enable archives ==> true (or 1)
// );
// $results = $wpdb->insert( "{$wpdb->prefix}woocommerce_attribute_taxonomies", $data );
// if ( is_wp_error( $results ) ) {
// return new WP_Error( 'cannot_create_attribute', $results->get_error_message(), array( 'status' => 400 ) );
// }
// $id = $wpdb->insert_id;
// do_action('woocommerce_attribute_added', $id, $data);
// wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
// delete_transient('wc_attribute_taxonomies');
// }
// //take attributes array with values and attached with the products
// function pswc_attach_terms_value_with_product($product_id, $attributes){
// $_the_att_data = array();
// foreach($attributes as $attribute_k => $attribute_v){
// if($attribute_v !==''){
// // *** Sanitize the text lower and no spaces
// $_att_name = 'pa_'.wc_sanitize_taxonomy_name($attribute_k);
// //check and Create attribute if not exist by providing attribute name
// create_product_attribute( wc_sanitize_taxonomy_name($attribute_k));
// $term_taxonomy_ids = wp_set_object_terms( $product_id, $attribute_v, $_att_name, true );
// //build array of attribute with name and values
// $_the_att_data[$_att_name] = array(
// 'name'=>$_att_name,
// 'value'=>$attribute_v,
// 'is_visible' => '1',
// 'is_taxonomy' => '1'
// );
// }
// }
// //Update Product attributes array with the product
// update_post_meta( $product_id,'_product_attributes',$_the_att_data);
// }
// //create product
// // Get the product ID from the Created Woo Product
// //set product attribute Names with Terms
// $attributes = array('color'=>'red',
// 'size'=>'small',
// 'delivery'=>'express',
// 'design'=>'yes',);
// $product_id = 57;
// // Provide Product_id and attributes array with name as key and option value as value to save with product
// pswc_attach_terms_value_with_product($product_id, $attributes);
function lifttruck_register_patterns() {
if ( WP_Block_Type_Registry::get_instance()->is_registered( 'core/paragraph' ) ) {
register_block_pattern( 'lifttruck/hero', array(
'title' => __( 'Hero', 'lifttruck' ),
'categories' => array( 'featured' ),
'source' => 'theme',
'content' => '<!-- wp:group {"style":{"spacing":{"padding":{"bottom":"1rem"},"margin":{"bottom":"0"},"blockGap":"0.5rem"}},"className":"section-title","layout":{"type":"flex","orientation":"vertical","justifyContent":"left"}} -->
<div class="wp-block-group section-title" style="margin-bottom:0;padding-bottom:1rem"><!-- wp:paragraph {"style":{"typography":{"fontStyle":"normal","fontWeight":"500"},"elements":{"link":{"color":{"text":"var:preset|color|primary"}}}},"textColor":"primary","fontSize":"small","fontFamily":"dm-sans"} -->
<p class="has-primary-color has-text-color has-link-color has-dm-sans-font-family has-small-font-size" style="font-style:normal;font-weight:500">Detailing Service</p>
<!-- /wp:paragraph -->
<!-- wp:heading {"style":{"elements":{"link":{"color":{"text":"var:preset|color|base"}}}},"textColor":"base","className":"is-style-balance-wrap"} -->
<h2 class="wp-block-heading is-style-balance-wrap has-base-color has-text-color has-link-color">Quality Car Services <br>You Can Trust</h2>
<!-- /wp:heading --></div>
<!-- /wp:group -->'
) );
}
}
add_action( 'init', 'lifttruck_register_patterns', 999 );