Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Element Description: Featured Products
- */
- if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly.
- }
- /*******************************/
- /* Fetch All Products */
- /*******************************/
- function fetch_all_products() {
- $products = get_posts(array(
- 'post_type' => 'products',
- 'posts_per_page' => -1,
- 'post_status' => 'publish',
- ));
- $product_list = array();
- if (!empty($products)) {
- foreach ($products as $product) {
- $product_list[$product->post_title] = $product->ID;
- }
- }
- return $product_list;
- }
- function fetch_all_featured_pages() {
- $pages = get_posts(array(
- 'post_type' => 'page',
- 'posts_per_page' => -1,
- 'post_status' => 'publish',
- ));
- $page_list = array();
- if ( !empty($pages) ) {
- foreach ( $pages as $page ) {
- $page_list[$page->post_title] = $page->ID;
- }
- }
- return $page_list;
- }
- /*******************************/
- /* Featured Products Container */
- /*******************************/
- vc_map(array(
- 'name' => __('Featured Products', 'text-domain'),
- 'base' => 'featured_products',
- 'icon' => 'icon-wpb-application-icon-large',
- 'description' => __('Block used to display featured products.', 'text-domain'),
- 'category' => __('CM Custom Modules', 'text-domain'),
- 'as_parent' => array(
- 'only' => 'featured_products_item,featured_product,featured_custom_box'
- ),
- 'content_element' => true,
- 'show_settings_on_create' => false,
- 'is_container' => false,
- 'params' => array(),
- 'js_view' => 'VcColumnView',
- ));
- /*******************************/
- /* Product Item Mapping */
- /*******************************/
- function map_featured_products_item() {
- vc_map(array(
- 'name' => __('Featured Product Page', 'text-domain'),
- 'base' => 'featured_products_item',
- 'description' => __( 'Displays a dropdown where you can select Pages.', 'text-domain' ),
- 'content_element' => true,
- 'icon' => 'icon-wpb-application-icon-large',
- 'as_child' => array('only' => 'featured_products'), // Child of 'featured_products'
- 'params' => array(
- array(
- 'type' => 'dropdown',
- 'heading' => __('Select Page', 'js_composer'),
- 'param_name' => 'select_featured_product',
- 'holder' => 'heading',
- 'value' => fetch_all_featured_pages(), // Use the same fetch function
- 'description' => __('Select a page to display, This will include pulling the page title, excerpt for description and page link. If you wanted custom details, you can fill-up the fields below to override', 'js_composer'),
- 'admin_label' => true,
- ),
- array(
- 'type' => 'textfield',
- 'class' => '',
- 'heading' => __( 'Custom Title', 'text-domain' ),
- 'param_name' => 'custom_title',
- 'value' => __( '', 'text-domain' ),
- 'description' => __( 'Enter Custom Title', 'text-domain' ),
- 'admin_label' => false,
- 'weight' => 0 ,
- ),
- array(
- 'type' => 'textarea',
- 'class' => '',
- 'heading' => __( 'Custom Description', 'text-domain' ),
- 'param_name' => 'custom_description',
- 'value' => __( '', 'text-domain' ),
- 'description' => __( 'Enter Custom Description', 'text-domain' ),
- 'admin_label' => false,
- 'weight' => 0 ,
- ),
- array(
- 'type' => 'attach_image',
- 'class' => '',
- 'heading' => __('Custom Image', 'text-domain'),
- 'param_name' => 'custom_image',
- 'description' => __('Add a custom image (optional).', 'text-domain'),
- ),
- array(
- 'type' => 'vc_link',
- 'heading' => __( 'Custom URL', TCM_VC ),
- 'param_name' => 'custom_button',
- 'description' => __( 'Add Link and Title. Leave blank if you do not want this to appear', TCM_VC ),
- ),
- ),
- ));
- }
- add_action('vc_before_init', 'map_featured_products_item');
- /*******************************/
- /* Product Item Mapping */
- /*******************************/
- function map_featured_product() {
- vc_map(array(
- 'name' => __(' Featured Product', 'text-domain'),
- 'base' => 'featured_product',
- 'description' => __( 'Displays a dropdown where you can select Product.', 'text-domain' ),
- 'content_element' => true,
- 'icon' => 'icon-wpb-application-icon-large',
- 'as_child' => array('only' => 'featured_products'), // Still a child of 'featured_products'
- 'params' => array(
- array(
- 'type' => 'dropdown',
- 'heading' => __('Select Product', 'js_composer'),
- 'param_name' => 'select_product_item',
- 'holder' => 'heading',
- 'value' => fetch_all_products(),
- 'description' => __('Select a product to display, This will include pulling the product title, excerpt for description and product link. If you wanted custom details, you can fill-up the fields below to override', 'js_composer'),
- 'admin_label' => true,
- ),
- array(
- 'type' => 'textfield',
- 'class' => '',
- 'heading' => __( 'Custom Title', 'text-domain' ),
- 'param_name' => 'custom_title',
- 'value' => __( '', 'text-domain' ),
- 'description' => __( 'Enter Custom Title', 'text-domain' ),
- 'admin_label' => false,
- 'weight' => 0 ,
- ),
- array(
- 'type' => 'textarea',
- 'class' => '',
- 'heading' => __( 'Custom Description', 'text-domain' ),
- 'param_name' => 'custom_description',
- 'value' => __( '', 'text-domain' ),
- 'description' => __( 'Enter Custom Description', 'text-domain' ),
- 'admin_label' => false,
- 'weight' => 0 ,
- ),
- array(
- 'type' => 'attach_image',
- 'class' => '',
- 'heading' => __('Custom Image', 'text-domain'),
- 'param_name' => 'custom_image',
- 'description' => __('Add a custom image (optional).', 'text-domain'),
- ),
- array(
- 'type' => 'vc_link',
- 'heading' => __( 'Custom URL', TCM_VC ),
- 'param_name' => 'custom_button',
- 'description' => __( 'Add Link and Title. Leave blank if you do not want this to appear', TCM_VC ),
- ),
- ),
- ));
- }
- add_action('vc_before_init', 'map_featured_product');
- /*******************************/
- /* Product Item Mapping */
- /*******************************/
- function map_featured_custom_box() {
- vc_map(array(
- 'name' => __('Custom Box', 'text-domain'),
- 'base' => 'featured_custom_box',
- 'description' => __( 'Displays a custom box that you can enter data manually.', 'text-domain' ),
- 'content_element' => true,
- 'icon' => 'icon-wpb-application-icon-large',
- 'as_child' => array('only' => 'featured_products'), // Child of 'featured_products'
- 'params' => array(
- array(
- 'type' => 'textfield',
- 'class' => '',
- 'heading' => __( 'Custom Title', 'text-domain' ),
- 'param_name' => 'box_custom_title',
- 'value' => __( '', 'text-domain' ),
- 'description' => __( 'Enter Custom Title', 'text-domain' ),
- 'admin_label' => false,
- 'weight' => 0 ,
- 'admin_label' => true, // This ensures the title displays in the backend
- ),
- array(
- 'type' => 'textarea',
- 'class' => '',
- 'heading' => __( 'Custom Description', 'text-domain' ),
- 'param_name' => 'box_custom_description',
- 'value' => __( '', 'text-domain' ),
- 'description' => __( 'Enter Custom Description', 'text-domain' ),
- 'admin_label' => false,
- 'weight' => 0 ,
- 'admin_label' => true, // This ensures the title displays in the backend
- ),
- array(
- 'type' => 'attach_image',
- 'class' => '',
- 'heading' => __('Custom Image', 'text-domain'),
- 'param_name' => 'box_custom_image',
- 'description' => __('Add a custom image (optional).', 'text-domain'),
- ),
- array(
- 'type' => 'vc_link',
- 'heading' => __( 'Box Button', TCM_VC ),
- 'param_name' => 'box_button',
- 'description' => __( 'Add Link and Title. Leave blank if you do not want this to appear', TCM_VC ),
- ),
- ),
- ));
- }
- add_action('vc_before_init', 'map_featured_custom_box');
- /*******************************/
- /* Render Featured Products Container Shortcode */
- /*******************************/
- function render_featured_products_shortcode($atts, $content) {
- ob_start();
- ?>
- <div class="featured_products_wrapper">
- <div class="featured_products_slider">
- <?php echo do_shortcode($content); ?>
- </div>
- <div class="featured_products_arrows">
- <button class="slide-prev" aria-label="Previous" type="button">
- <svg xmlns="http://www.w3.org/2000/svg" width="25" height="24" viewBox="0 0 25 24" fill="none">
- <path d="M12.5 4L13.91 5.41L8.33 11H20.5V13H8.33L13.91 18.59L12.5 20L4.5 12L12.5 4Z" fill="white"/>
- </svg>
- </button>
- <button class="slide-next" aria-label="Next" type="button">
- <svg xmlns="http://www.w3.org/2000/svg" width="25" height="24" viewBox="0 0 25 24" fill="none">
- <path d="M12.5 4L11.09 5.41L16.67 11H4.5V13H16.67L11.09 18.59L12.5 20L20.5 12L12.5 4Z" fill="white"/>
- </svg>
- </button>
- </div>
- </div>
- <?php
- return ob_get_clean();
- }
- add_shortcode('featured_products', 'render_featured_products_shortcode');
- /*******************************/
- /* Render Featured Products Item Shortcode */
- /*******************************/
- function render_featured_products_item_shortcode($atts, $content) {
- $atts = shortcode_atts(
- array(
- 'select_featured_product' => '',
- 'custom_image' => '',
- 'custom_title' => '',
- 'custom_description' => '',
- 'custom_button' => ''
- ),
- $atts
- );
- ob_start();
- // Get the selected page
- $page_id = $atts['select_featured_product'];
- $page = get_post($page_id);
- if ( $page && !is_wp_error($page) ) {
- // Get custom image or featured image for the page
- $featured_image = get_the_post_thumbnail_url($page_id, 'full');
- $bg_image = !empty($featured_image) ? $featured_image : (!empty($atts['custom_image']) ? wp_get_attachment_image_src( $atts['custom_image'], 'full' )[0] : '');
- // Get the excerpt, fallback to trimming content if no excerpt is available
- $excerpt = !empty($page->post_excerpt) ? $page->post_excerpt : '';
- // Use custom_description if available, fallback to $excerpt
- $description = !empty($atts['custom_description']) ? $atts['custom_description'] : $excerpt;
- // Use custom_title if available, fallback to page title
- $title = !empty($atts['custom_title']) ? $atts['custom_title'] : $page->post_title;
- if(isset($atts['custom_button'])){
- $custom_button = $atts['custom_button'];
- $custom_button = vc_build_link( $custom_button );
- $custom_button_url = esc_url($custom_button['url']);
- $custom_button_title = $custom_button['title'];
- $custom_button_target = $custom_button['target'];
- $custom_button_rel = $custom_button['rel'];
- }
- ?>
- <div class="category_item">
- <div class="category_image" style="background: url('<?php echo esc_url($bg_image); ?>') no-repeat; background-size:cover;">
- <div class="category_details">
- <h5><?php echo esc_html($title); ?></h5>
- <p><?php echo esc_html($description); ?></p>
- <?php
- if(!empty($custom_button_url)) {
- echo sfe_button_link_arrow($custom_button_url, $custom_button_title);
- }else {
- $page_link = esc_url(get_permalink($page_id));
- echo sfe_button_link_arrow($page_link, "View All Products");
- }
- ?>
- </div>
- </div>
- </div>
- <?php
- } else {
- echo '<p>No page selected or page not found.</p>';
- }
- return ob_get_clean();
- }
- add_shortcode('featured_products_item', 'render_featured_products_item_shortcode');
- /*******************************/
- /* Render Alternative Products Item Shortcode */
- /*******************************/
- function render_featured_product_shortcode($atts, $content) {
- $atts = shortcode_atts(
- array(
- 'select_product_item' => '',
- 'custom_title' => '',
- 'custom_description' => '',
- 'custom_image' => '',
- 'custom_button' => ''
- ),
- $atts
- );
- ob_start();
- // Get the selected page
- $page_id = $atts['select_product_item'];
- $page = get_post($page_id);
- if ($page && !is_wp_error($page)) {
- // Get custom image or featured image for the page
- $featured_image = get_the_post_thumbnail_url($page_id, 'full');
- $bg_image = !empty($featured_image) ? $featured_image : (!empty($atts['custom_image']) ? wp_get_attachment_image_src($atts['custom_image'], 'full')[0] : '');
- // Get the excerpt, but do NOT fall back to post_content
- $excerpt = isset($page->post_excerpt) && !empty($page->post_excerpt) ? $page->post_excerpt : null;
- // Use custom_description if available, fallback to $excerpt, otherwise fallback to null
- $description = isset($atts['custom_description']) && !empty($atts['custom_description']) ? $atts['custom_description'] : $excerpt;
- // Use custom_title if available, fallback to page title
- $title = !empty($atts['custom_title']) ? $atts['custom_title'] : $page->post_title;
- if(isset($atts['custom_button'])){
- $custom_button = $atts['custom_button'];
- $custom_button = vc_build_link( $custom_button );
- $custom_button_url = esc_url($custom_button['url']);
- $custom_button_title = $custom_button['title'];
- $custom_button_target = $custom_button['target'];
- $custom_button_rel = $custom_button['rel'];
- }
- ?>
- <div class="category_item">
- <div class="category_image" style="background: url('<?php echo esc_url($bg_image); ?>') no-repeat; background-size: cover;">
- <div class="category_details">
- <h5><?php echo esc_html($title); ?></h5>
- <?php if ($description !== null) : ?>
- <p><?php echo esc_html($description); ?></p>
- <?php endif; ?>
- <?php
- if(!empty($custom_button_url)) {
- echo sfe_button_link_arrow($custom_button_url, $custom_button_title);
- }else {
- $page_link = esc_url(get_permalink($page_id));
- echo sfe_button_link_arrow($page_link, "View All Products");
- }
- ?>
- </div>
- </div>
- </div>
- <?php
- } else {
- echo '<p>No page selected or page not found.</p>';
- }
- return ob_get_clean();
- }
- add_shortcode('featured_product', 'render_featured_product_shortcode');
- /*******************************/
- /* Render Alternative Products Item Shortcode */
- /*******************************/
- function render_featured_custom_box_shortcode($atts, $content) {
- $atts = shortcode_atts(
- array(
- 'box_custom_title' => '',
- 'box_custom_description' => '',
- 'box_custom_image' => '',
- 'box_button' => ''
- ),
- $atts
- );
- ob_start();
- // Resolve box_custom image
- $bg_image = !empty($atts['box_custom_image']) ? wp_get_attachment_image_src($atts['box_custom_image'], 'full')[0] : '';
- // Resolve title and description
- $title = !empty($atts['box_custom_title']) ? $atts['box_custom_title'] : 'Default Title';
- $description = !empty($atts['box_custom_description']) ? $atts['box_custom_description'] : 'Default Description';
- if(isset($atts['box_button'])){
- $box_button = $atts['box_button'];
- $box_button = vc_build_link( $box_button );
- $box_button_url = esc_url($box_button['url']);
- $box_button_title = $box_button['title'];
- $box_button_target = $box_button['target'];
- $box_button_rel = $box_button['rel'];
- }
- ?>
- <div class="category_item">
- <div class="category_image" style="background: url('<?php echo esc_url($bg_image); ?>') no-repeat; background-size: cover;">
- <div class="category_details">
- <h5><?php echo esc_html($title); ?></h5>
- <p><?php echo esc_html($description); ?></p>
- <?php
- if(!empty($box_button_url)) {
- echo sfe_button_link_arrow($box_button_url, $box_button_title);
- }
- ?>
- </div>
- </div>
- </div>
- <?php
- return ob_get_clean();
- }
- add_shortcode('featured_custom_box', 'render_featured_custom_box_shortcode');
- /*******************************/
- /* WPBakery ShortCodes */
- /*******************************/
- if (class_exists('WPBakeryShortCodesContainer')) {
- class WPBakeryShortCode_featured_products extends WPBakeryShortCodesContainer {}
- }
- if (class_exists('WPBakeryShortCode')) {
- class WPBakeryShortCode_featured_products_item extends WPBakeryShortCode {}
- class WPBakeryShortCode_featured_product extends WPBakeryShortCode {}
- class WPBakeryShortCode_featured_custom_box extends WPBakeryShortCode {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement