WC_PD_Helpers

Helper functions.

Defined (1)

The class is defined in the following location(s).

/class-wc-pd-helpers.php  
  1. class WC_PD_Helpers { 
  2.  
  3. public static function merge_product_titles( $products ) { 
  4.  
  5. $parts_to_merge = array(); 
  6.  
  7. if ( ! empty( $products ) ) { 
  8.  
  9. $loop = 0; 
  10.  
  11. foreach ( $products as $product_id => $product ) { 
  12.  
  13. if ( $loop === 0 ) { 
  14. $part_to_merge = __( '%s', 'woocommerce-product-dependencies' ); 
  15. } elseif ( count( $products ) - 1 === $loop ) { 
  16. $part_to_merge = __( ' or %s', 'woocommerce-product-dependencies' ); 
  17. } else { 
  18. $part_to_merge = __( ', %s', 'woocommerce-product-dependencies' ); 
  19.  
  20. $product_permalink = $product->is_visible() ? $product->get_permalink() : ''; 
  21. $product_title = WC_PD_Core_Compatibility::is_wc_version_gte_2_7() ? $product->get_name() : $product->get_title(); 
  22.  
  23. if ( $product_permalink ) { 
  24. $part_to_merge = sprintf( $part_to_merge, sprintf( '"<a href="%1$s">%2$s</a>"', esc_url( $product_permalink ), $product_title ) ); 
  25. } else { 
  26. $part_to_merge = sprintf( $part_to_merge, sprintf( '"%s"', $product_title ) ); 
  27.  
  28. $parts_to_merge[] = $part_to_merge; 
  29.  
  30. $loop++; 
  31.  
  32. if ( is_rtl() ) { 
  33. $parts_to_merge = array_reverse( $parts_to_merge ); 
  34.  
  35. return implode( '', $parts_to_merge ); 
  36.  
  37. /** 
  38. * Return a formatted product title. 
  39. * @param WC_Product|int $product 
  40. * @param string $title 
  41. * @return string 
  42. */ 
  43. public static function get_product_title( $product, $title = '' ) { 
  44.  
  45. if ( ! is_object( $product ) ) { 
  46. $product = wc_get_product( $product ); 
  47.  
  48. if ( ! $product ) { 
  49. return false; 
  50.  
  51. if ( WC_PD_Core_Compatibility::is_wc_version_gte_2_7() ) { 
  52. return $product->get_formatted_name(); 
  53.  
  54. $title = $title ? $title : $product->get_title(); 
  55. $sku = $product->get_sku(); 
  56. $id = WC_PD_Core_Compatibility::get_id( $product ); 
  57.  
  58. if ( $sku ) { 
  59. $identifier = $sku; 
  60. } else { 
  61. $identifier = '#' . $id; 
  62.  
  63. return self::format_product_title( $title, $identifier, '', WC_PD_Core_Compatibility::is_wc_version_gte_2_7() ); 
  64.  
  65. /** 
  66. * Format a product title. 
  67. * @param string $title 
  68. * @param string $identifier 
  69. * @param string $meta 
  70. * @param string $paren 
  71. * @return string 
  72. */ 
  73. public static function format_product_title( $title, $identifier = '', $meta = '', $paren = false ) { 
  74.  
  75. if ( $identifier && $meta ) { 
  76. if ( $paren ) { 
  77. $title = sprintf( _x( '%1$s – %2$s (%3$s)', 'product title followed by meta and sku in parenthesis', 'woocommerce-product-dependencies' ), $title, $meta, $identifier ); 
  78. } else { 
  79. $title = sprintf( _x( '%1$s – %2$s – %3$s', 'sku followed by product title and meta', 'woocommerce-product-dependencies' ), $identifier, $title, $meta ); 
  80. } elseif ( $identifier ) { 
  81. if ( $paren ) { 
  82. $title = sprintf( _x( '%1$s (%2$s)', 'product title followed by sku in parenthesis', 'woocommerce-product-dependencies' ), $title, $identifier ); 
  83. } else { 
  84. $title = sprintf( _x( '%1$s – %2$s', 'sku followed by product title', 'woocommerce-product-dependencies' ), $identifier, $title ); 
  85. } elseif ( $meta ) { 
  86. if ( $paren ) { 
  87. $title = sprintf( _x( '%1$s (%2$s)', 'product title followed by meta in parenthesis', 'woocommerce-product-dependencies' ), $title, $meta ); 
  88. } else { 
  89. $title = sprintf( _x( '%1$s – %2$s', 'product title followed by meta', 'woocommerce-product-dependencies' ), $title, $meta ); 
  90.  
  91. return $title;