Advertisement
MikkoDC

Multi Option

Feb 13th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.91 KB | None | 0 0
  1. <?php
  2. /*
  3. Element Description: Featured Products
  4. */
  5.  
  6. if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly.
  7. }
  8. /*******************************/
  9. /* Fetch All Products */
  10. /*******************************/
  11. function fetch_all_products() {
  12. $products = get_posts(array(
  13. 'post_type' => 'products',
  14. 'posts_per_page' => -1,
  15. 'post_status' => 'publish',
  16. ));
  17.  
  18. $product_list = array();
  19.  
  20. if (!empty($products)) {
  21. foreach ($products as $product) {
  22. $product_list[$product->post_title] = $product->ID;
  23. }
  24. }
  25.  
  26. return $product_list;
  27. }
  28.  
  29. function fetch_all_featured_pages() {
  30. $pages = get_posts(array(
  31. 'post_type' => 'page',
  32. 'posts_per_page' => -1,
  33. 'post_status' => 'publish',
  34. ));
  35.  
  36. $page_list = array();
  37.  
  38. if ( !empty($pages) ) {
  39. foreach ( $pages as $page ) {
  40. $page_list[$page->post_title] = $page->ID;
  41. }
  42. }
  43.  
  44. return $page_list;
  45. }
  46. /*******************************/
  47. /* Featured Products Container */
  48. /*******************************/
  49. vc_map(array(
  50. 'name' => __('Featured Products', 'text-domain'),
  51. 'base' => 'featured_products',
  52. 'icon' => 'icon-wpb-application-icon-large',
  53. 'description' => __('Block used to display featured products.', 'text-domain'),
  54. 'category' => __('CM Custom Modules', 'text-domain'),
  55. 'as_parent' => array(
  56. 'only' => 'featured_products_item,featured_product,featured_custom_box'
  57. ),
  58. 'content_element' => true,
  59. 'show_settings_on_create' => false,
  60. 'is_container' => false,
  61. 'params' => array(),
  62. 'js_view' => 'VcColumnView',
  63. ));
  64.  
  65.  
  66. /*******************************/
  67. /* Product Item Mapping */
  68. /*******************************/
  69. function map_featured_products_item() {
  70. vc_map(array(
  71. 'name' => __('Featured Product Page', 'text-domain'),
  72. 'base' => 'featured_products_item',
  73. 'description' => __( 'Displays a dropdown where you can select Pages.', 'text-domain' ),
  74. 'content_element' => true,
  75. 'icon' => 'icon-wpb-application-icon-large',
  76. 'as_child' => array('only' => 'featured_products'), // Child of 'featured_products'
  77. 'params' => array(
  78. array(
  79. 'type' => 'dropdown',
  80. 'heading' => __('Select Page', 'js_composer'),
  81. 'param_name' => 'select_featured_product',
  82. 'holder' => 'heading',
  83. 'value' => fetch_all_featured_pages(), // Use the same fetch function
  84. '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'),
  85. 'admin_label' => true,
  86. ),
  87. array(
  88. 'type' => 'textfield',
  89. 'class' => '',
  90. 'heading' => __( 'Custom Title', 'text-domain' ),
  91. 'param_name' => 'custom_title',
  92. 'value' => __( '', 'text-domain' ),
  93. 'description' => __( 'Enter Custom Title', 'text-domain' ),
  94. 'admin_label' => false,
  95. 'weight' => 0 ,
  96. ),
  97. array(
  98. 'type' => 'textarea',
  99. 'class' => '',
  100. 'heading' => __( 'Custom Description', 'text-domain' ),
  101. 'param_name' => 'custom_description',
  102. 'value' => __( '', 'text-domain' ),
  103. 'description' => __( 'Enter Custom Description', 'text-domain' ),
  104. 'admin_label' => false,
  105. 'weight' => 0 ,
  106. ),
  107.  
  108. array(
  109. 'type' => 'attach_image',
  110. 'class' => '',
  111. 'heading' => __('Custom Image', 'text-domain'),
  112. 'param_name' => 'custom_image',
  113. 'description' => __('Add a custom image (optional).', 'text-domain'),
  114. ),
  115. array(
  116. 'type' => 'vc_link',
  117. 'heading' => __( 'Custom URL', TCM_VC ),
  118. 'param_name' => 'custom_button',
  119. 'description' => __( 'Add Link and Title. Leave blank if you do not want this to appear', TCM_VC ),
  120. ),
  121. ),
  122. ));
  123. }
  124.  
  125. add_action('vc_before_init', 'map_featured_products_item');
  126.  
  127. /*******************************/
  128. /* Product Item Mapping */
  129. /*******************************/
  130. function map_featured_product() {
  131. vc_map(array(
  132. 'name' => __(' Featured Product', 'text-domain'),
  133. 'base' => 'featured_product',
  134. 'description' => __( 'Displays a dropdown where you can select Product.', 'text-domain' ),
  135. 'content_element' => true,
  136. 'icon' => 'icon-wpb-application-icon-large',
  137. 'as_child' => array('only' => 'featured_products'), // Still a child of 'featured_products'
  138. 'params' => array(
  139. array(
  140. 'type' => 'dropdown',
  141. 'heading' => __('Select Product', 'js_composer'),
  142. 'param_name' => 'select_product_item',
  143. 'holder' => 'heading',
  144. 'value' => fetch_all_products(),
  145. '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'),
  146. 'admin_label' => true,
  147. ),
  148. array(
  149. 'type' => 'textfield',
  150. 'class' => '',
  151. 'heading' => __( 'Custom Title', 'text-domain' ),
  152. 'param_name' => 'custom_title',
  153. 'value' => __( '', 'text-domain' ),
  154. 'description' => __( 'Enter Custom Title', 'text-domain' ),
  155. 'admin_label' => false,
  156. 'weight' => 0 ,
  157. ),
  158. array(
  159. 'type' => 'textarea',
  160. 'class' => '',
  161. 'heading' => __( 'Custom Description', 'text-domain' ),
  162. 'param_name' => 'custom_description',
  163. 'value' => __( '', 'text-domain' ),
  164. 'description' => __( 'Enter Custom Description', 'text-domain' ),
  165. 'admin_label' => false,
  166. 'weight' => 0 ,
  167. ),
  168. array(
  169. 'type' => 'attach_image',
  170. 'class' => '',
  171. 'heading' => __('Custom Image', 'text-domain'),
  172. 'param_name' => 'custom_image',
  173. 'description' => __('Add a custom image (optional).', 'text-domain'),
  174. ),
  175. array(
  176. 'type' => 'vc_link',
  177. 'heading' => __( 'Custom URL', TCM_VC ),
  178. 'param_name' => 'custom_button',
  179. 'description' => __( 'Add Link and Title. Leave blank if you do not want this to appear', TCM_VC ),
  180. ),
  181. ),
  182. ));
  183. }
  184.  
  185. add_action('vc_before_init', 'map_featured_product');
  186.  
  187. /*******************************/
  188. /* Product Item Mapping */
  189. /*******************************/
  190. function map_featured_custom_box() {
  191. vc_map(array(
  192. 'name' => __('Custom Box', 'text-domain'),
  193. 'base' => 'featured_custom_box',
  194. 'description' => __( 'Displays a custom box that you can enter data manually.', 'text-domain' ),
  195. 'content_element' => true,
  196. 'icon' => 'icon-wpb-application-icon-large',
  197. 'as_child' => array('only' => 'featured_products'), // Child of 'featured_products'
  198. 'params' => array(
  199. array(
  200. 'type' => 'textfield',
  201. 'class' => '',
  202. 'heading' => __( 'Custom Title', 'text-domain' ),
  203. 'param_name' => 'box_custom_title',
  204. 'value' => __( '', 'text-domain' ),
  205. 'description' => __( 'Enter Custom Title', 'text-domain' ),
  206. 'admin_label' => false,
  207. 'weight' => 0 ,
  208. 'admin_label' => true, // This ensures the title displays in the backend
  209. ),
  210. array(
  211. 'type' => 'textarea',
  212. 'class' => '',
  213. 'heading' => __( 'Custom Description', 'text-domain' ),
  214. 'param_name' => 'box_custom_description',
  215. 'value' => __( '', 'text-domain' ),
  216. 'description' => __( 'Enter Custom Description', 'text-domain' ),
  217. 'admin_label' => false,
  218. 'weight' => 0 ,
  219. 'admin_label' => true, // This ensures the title displays in the backend
  220. ),
  221.  
  222. array(
  223. 'type' => 'attach_image',
  224. 'class' => '',
  225. 'heading' => __('Custom Image', 'text-domain'),
  226. 'param_name' => 'box_custom_image',
  227. 'description' => __('Add a custom image (optional).', 'text-domain'),
  228. ),
  229. array(
  230. 'type' => 'vc_link',
  231. 'heading' => __( 'Box Button', TCM_VC ),
  232. 'param_name' => 'box_button',
  233. 'description' => __( 'Add Link and Title. Leave blank if you do not want this to appear', TCM_VC ),
  234. ),
  235. ),
  236. ));
  237. }
  238. add_action('vc_before_init', 'map_featured_custom_box');
  239.  
  240. /*******************************/
  241. /* Render Featured Products Container Shortcode */
  242. /*******************************/
  243. function render_featured_products_shortcode($atts, $content) {
  244. ob_start();
  245. ?>
  246. <div class="featured_products_wrapper">
  247. <div class="featured_products_slider">
  248. <?php echo do_shortcode($content); ?>
  249. </div>
  250. <div class="featured_products_arrows">
  251. <button class="slide-prev" aria-label="Previous" type="button">
  252. <svg xmlns="http://www.w3.org/2000/svg" width="25" height="24" viewBox="0 0 25 24" fill="none">
  253. <path d="M12.5 4L13.91 5.41L8.33 11H20.5V13H8.33L13.91 18.59L12.5 20L4.5 12L12.5 4Z" fill="white"/>
  254. </svg>
  255. </button>
  256. <button class="slide-next" aria-label="Next" type="button">
  257. <svg xmlns="http://www.w3.org/2000/svg" width="25" height="24" viewBox="0 0 25 24" fill="none">
  258. <path d="M12.5 4L11.09 5.41L16.67 11H4.5V13H16.67L11.09 18.59L12.5 20L20.5 12L12.5 4Z" fill="white"/>
  259. </svg>
  260. </button>
  261. </div>
  262. </div>
  263. <?php
  264. return ob_get_clean();
  265. }
  266.  
  267. add_shortcode('featured_products', 'render_featured_products_shortcode');
  268.  
  269. /*******************************/
  270. /* Render Featured Products Item Shortcode */
  271. /*******************************/
  272. function render_featured_products_item_shortcode($atts, $content) {
  273. $atts = shortcode_atts(
  274. array(
  275. 'select_featured_product' => '',
  276. 'custom_image' => '',
  277. 'custom_title' => '',
  278. 'custom_description' => '',
  279. 'custom_button' => ''
  280. ),
  281. $atts
  282. );
  283.  
  284. ob_start();
  285.  
  286. // Get the selected page
  287. $page_id = $atts['select_featured_product'];
  288. $page = get_post($page_id);
  289.  
  290. if ( $page && !is_wp_error($page) ) {
  291. // Get custom image or featured image for the page
  292. $featured_image = get_the_post_thumbnail_url($page_id, 'full');
  293. $bg_image = !empty($featured_image) ? $featured_image : (!empty($atts['custom_image']) ? wp_get_attachment_image_src( $atts['custom_image'], 'full' )[0] : '');
  294.  
  295. // Get the excerpt, fallback to trimming content if no excerpt is available
  296. $excerpt = !empty($page->post_excerpt) ? $page->post_excerpt : '';
  297.  
  298. // Use custom_description if available, fallback to $excerpt
  299. $description = !empty($atts['custom_description']) ? $atts['custom_description'] : $excerpt;
  300.  
  301. // Use custom_title if available, fallback to page title
  302. $title = !empty($atts['custom_title']) ? $atts['custom_title'] : $page->post_title;
  303.  
  304. if(isset($atts['custom_button'])){
  305. $custom_button = $atts['custom_button'];
  306. $custom_button = vc_build_link( $custom_button );
  307. $custom_button_url = esc_url($custom_button['url']);
  308. $custom_button_title = $custom_button['title'];
  309. $custom_button_target = $custom_button['target'];
  310. $custom_button_rel = $custom_button['rel'];
  311. }
  312.  
  313. ?>
  314. <div class="category_item">
  315.  
  316. <div class="category_image" style="background: url('<?php echo esc_url($bg_image); ?>') no-repeat; background-size:cover;">
  317. <div class="category_details">
  318. <h5><?php echo esc_html($title); ?></h5>
  319. <p><?php echo esc_html($description); ?></p>
  320. <?php
  321. if(!empty($custom_button_url)) {
  322. echo sfe_button_link_arrow($custom_button_url, $custom_button_title);
  323. }else {
  324. $page_link = esc_url(get_permalink($page_id));
  325. echo sfe_button_link_arrow($page_link, "View All Products");
  326. }
  327. ?>
  328. </div>
  329. </div>
  330. </div>
  331. <?php
  332. } else {
  333. echo '<p>No page selected or page not found.</p>';
  334. }
  335.  
  336. return ob_get_clean();
  337. }
  338.  
  339. add_shortcode('featured_products_item', 'render_featured_products_item_shortcode');
  340.  
  341. /*******************************/
  342. /* Render Alternative Products Item Shortcode */
  343. /*******************************/
  344. function render_featured_product_shortcode($atts, $content) {
  345. $atts = shortcode_atts(
  346. array(
  347. 'select_product_item' => '',
  348. 'custom_title' => '',
  349. 'custom_description' => '',
  350. 'custom_image' => '',
  351. 'custom_button' => ''
  352. ),
  353. $atts
  354. );
  355.  
  356. ob_start();
  357.  
  358. // Get the selected page
  359. $page_id = $atts['select_product_item'];
  360. $page = get_post($page_id);
  361.  
  362. if ($page && !is_wp_error($page)) {
  363. // Get custom image or featured image for the page
  364. $featured_image = get_the_post_thumbnail_url($page_id, 'full');
  365. $bg_image = !empty($featured_image) ? $featured_image : (!empty($atts['custom_image']) ? wp_get_attachment_image_src($atts['custom_image'], 'full')[0] : '');
  366.  
  367. // Get the excerpt, but do NOT fall back to post_content
  368. $excerpt = isset($page->post_excerpt) && !empty($page->post_excerpt) ? $page->post_excerpt : null;
  369.  
  370. // Use custom_description if available, fallback to $excerpt, otherwise fallback to null
  371. $description = isset($atts['custom_description']) && !empty($atts['custom_description']) ? $atts['custom_description'] : $excerpt;
  372.  
  373. // Use custom_title if available, fallback to page title
  374. $title = !empty($atts['custom_title']) ? $atts['custom_title'] : $page->post_title;
  375.  
  376. if(isset($atts['custom_button'])){
  377. $custom_button = $atts['custom_button'];
  378. $custom_button = vc_build_link( $custom_button );
  379. $custom_button_url = esc_url($custom_button['url']);
  380. $custom_button_title = $custom_button['title'];
  381. $custom_button_target = $custom_button['target'];
  382. $custom_button_rel = $custom_button['rel'];
  383. }
  384.  
  385. ?>
  386. <div class="category_item">
  387. <div class="category_image" style="background: url('<?php echo esc_url($bg_image); ?>') no-repeat; background-size: cover;">
  388. <div class="category_details">
  389. <h5><?php echo esc_html($title); ?></h5>
  390. <?php if ($description !== null) : ?>
  391. <p><?php echo esc_html($description); ?></p>
  392. <?php endif; ?>
  393. <?php
  394. if(!empty($custom_button_url)) {
  395. echo sfe_button_link_arrow($custom_button_url, $custom_button_title);
  396. }else {
  397. $page_link = esc_url(get_permalink($page_id));
  398. echo sfe_button_link_arrow($page_link, "View All Products");
  399. }
  400. ?>
  401. </div>
  402. </div>
  403. </div>
  404. <?php
  405. } else {
  406. echo '<p>No page selected or page not found.</p>';
  407. }
  408.  
  409.  
  410.  
  411. return ob_get_clean();
  412. }
  413. add_shortcode('featured_product', 'render_featured_product_shortcode');
  414.  
  415.  
  416. /*******************************/
  417. /* Render Alternative Products Item Shortcode */
  418. /*******************************/
  419. function render_featured_custom_box_shortcode($atts, $content) {
  420. $atts = shortcode_atts(
  421. array(
  422. 'box_custom_title' => '',
  423. 'box_custom_description' => '',
  424. 'box_custom_image' => '',
  425. 'box_button' => ''
  426. ),
  427. $atts
  428. );
  429.  
  430. ob_start();
  431.  
  432. // Resolve box_custom image
  433. $bg_image = !empty($atts['box_custom_image']) ? wp_get_attachment_image_src($atts['box_custom_image'], 'full')[0] : '';
  434.  
  435. // Resolve title and description
  436. $title = !empty($atts['box_custom_title']) ? $atts['box_custom_title'] : 'Default Title';
  437. $description = !empty($atts['box_custom_description']) ? $atts['box_custom_description'] : 'Default Description';
  438.  
  439. if(isset($atts['box_button'])){
  440. $box_button = $atts['box_button'];
  441. $box_button = vc_build_link( $box_button );
  442. $box_button_url = esc_url($box_button['url']);
  443. $box_button_title = $box_button['title'];
  444. $box_button_target = $box_button['target'];
  445. $box_button_rel = $box_button['rel'];
  446. }
  447.  
  448. ?>
  449. <div class="category_item">
  450. <div class="category_image" style="background: url('<?php echo esc_url($bg_image); ?>') no-repeat; background-size: cover;">
  451. <div class="category_details">
  452. <h5><?php echo esc_html($title); ?></h5>
  453. <p><?php echo esc_html($description); ?></p>
  454. <?php
  455. if(!empty($box_button_url)) {
  456. echo sfe_button_link_arrow($box_button_url, $box_button_title);
  457. }
  458. ?>
  459. </div>
  460. </div>
  461. </div>
  462. <?php
  463.  
  464. return ob_get_clean();
  465. }
  466. add_shortcode('featured_custom_box', 'render_featured_custom_box_shortcode');
  467.  
  468.  
  469. /*******************************/
  470. /* WPBakery ShortCodes */
  471. /*******************************/
  472. if (class_exists('WPBakeryShortCodesContainer')) {
  473. class WPBakeryShortCode_featured_products extends WPBakeryShortCodesContainer {}
  474. }
  475.  
  476. if (class_exists('WPBakeryShortCode')) {
  477. class WPBakeryShortCode_featured_products_item extends WPBakeryShortCode {}
  478. class WPBakeryShortCode_featured_product extends WPBakeryShortCode {}
  479. class WPBakeryShortCode_featured_custom_box extends WPBakeryShortCode {}
  480. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement