| 1 |
<?php |
| 2 |
namespace Barn2\PTS_Lib; |
| 3 |
|
| 4 |
if ( ! \class_exists( __NAMESPACE__ . '\\Util' ) ) { |
| 5 |
|
| 6 |
/** |
| 7 |
* Utility functions for Barn2 plugins. |
| 8 |
* |
| 9 |
* @package Barn2\barn2-lib |
| 10 |
* @author Barn2 Plugins <support@barn2.co.uk> |
| 11 |
* @license GPL-3.0 |
| 12 |
* @copyright Barn2 Media Ltd |
| 13 |
* @version 1.4.8 |
| 14 |
*/ |
| 15 |
class Util { |
| 16 |
|
| 17 |
const EDD_STORE_URL = 'https://barn2.co.uk'; |
| 18 |
const KNOWLEDGE_BASE_URL = 'https://barn2.co.uk'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Formats a HTML link to a path on the Barn2 site. |
| 22 |
* |
| 23 |
* @param string $relative_path The path relative to https://barn2.co.uk. |
| 24 |
* @param string $link_text The link text. |
| 25 |
* @param boolean $new_tab Whether to open the link in a new tab. |
| 26 |
* @return string The hyperlink. |
| 27 |
*/ |
| 28 |
public static function barn2_link( $relative_path, $link_text = '', $new_tab = false ) { |
| 29 |
if ( empty( $link_text ) ) { |
| 30 |
$link_text = __( 'Read more', 'posts-data-table' ); |
| 31 |
} |
| 32 |
return self::format_link( self::barn2_url( $relative_path ), esc_html( $link_text ), $new_tab ); |
| 33 |
} |
| 34 |
|
| 35 |
public static function barn2_url( $relative_path ) { |
| 36 |
return esc_url( 'https://barn2.co.uk/' . ltrim( $relative_path, '/' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
public static function format_link( $url, $link_text, $new_tab = false ) { |
| 40 |
return sprintf( '%1$s%2$s</a>', self::format_link_open( $url, $new_tab ), $link_text ); |
| 41 |
} |
| 42 |
|
| 43 |
public static function format_link_open( $url, $new_tab = false ) { |
| 44 |
$target = $new_tab ? ' target="_blank"' : ''; |
| 45 |
return sprintf( '<a href="%1$s"%2$s>', esc_url( $url ), $target ); |
| 46 |
} |
| 47 |
|
| 48 |
public static function format_store_url( $path = '' ) { |
| 49 |
return self::EDD_STORE_URL . '/' . ltrim( $path, ' /' ); |
| 50 |
} |
| 51 |
|
| 52 |
public static function format_store_link( $path, $link_text, $new_tab = true ) { |
| 53 |
return self::format_link( self::format_store_url( $path ), $link_text, $new_tab ); |
| 54 |
} |
| 55 |
|
| 56 |
public static function format_store_link_open( $path, $new_tab = true ) { |
| 57 |
return self::format_link_open( self::format_store_url( $path ), $new_tab ); |
| 58 |
} |
| 59 |
|
| 60 |
public static function get_add_to_cart_url( $download_id, $price_id = 0, $discount_code = '' ) { |
| 61 |
$args = array( |
| 62 |
'edd_action' => 'add_to_cart', |
| 63 |
'download_id' => (int) $download_id |
| 64 |
); |
| 65 |
if ( $price_id ) { |
| 66 |
$args['edd_options[price_id]'] = (int) $price_id; |
| 67 |
} |
| 68 |
if ( $discount_code ) { |
| 69 |
$args['discount'] = $discount_code; |
| 70 |
} |
| 71 |
|
| 72 |
return self::format_store_url( '?' . http_build_query( $args ) ); |
| 73 |
} |
| 74 |
|
| 75 |
public static function is_admin() { |
| 76 |
return is_admin(); |
| 77 |
} |
| 78 |
|
| 79 |
public static function is_front_end() { |
| 80 |
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ); |
| 81 |
} |
| 82 |
|
| 83 |
public static function is_woocommerce_active() { |
| 84 |
return class_exists( '\WooCommerce' ); |
| 85 |
} |
| 86 |
|
| 87 |
public static function is_product_addons_active() { |
| 88 |
return class_exists( '\WC_Product_Addons' ); |
| 89 |
} |
| 90 |
|
| 91 |
public static function is_edd_active() { |
| 92 |
return class_exists( '\Easy_Digital_Downloads' ); |
| 93 |
} |
| 94 |
|
| 95 |
public static function is_protected_categories_active() { |
| 96 |
if ( function_exists( '\Barn2\Plugin\WC_Protected_Categories\wpc' ) ) { |
| 97 |
return \Barn2\Plugin\WC_Protected_Categories\wpc()->has_valid_license(); |
| 98 |
} else { |
| 99 |
if ( class_exists( '\WC_Protected_Categories_Plugin' ) ) { |
| 100 |
if ( method_exists( \WC_Protected_Categories_Plugin::instance(), 'has_valid_license' ) ) { |
| 101 |
return \WC_Protected_Categories_Plugin::instance()->has_valid_license(); |
| 102 |
} |
| 103 |
return true; |
| 104 |
} |
| 105 |
} |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
public static function is_product_table_active() { |
| 110 |
if ( function_exists( '\Barn2\Plugin\WC_Product_Table\wpt' ) ) { |
| 111 |
return \Barn2\Plugin\WC_Product_Table\wpt()->has_valid_license(); |
| 112 |
} elseif ( class_exists( '\WC_Product_Table_Plugin' ) ) { |
| 113 |
if ( method_exists( \WC_Product_Table_Plugin::instance(), 'has_valid_license' ) ) { |
| 114 |
return \WC_Product_Table_Plugin::instance()->has_valid_license(); |
| 115 |
} |
| 116 |
return true; |
| 117 |
} |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
public static function is_quick_view_pro_active() { |
| 122 |
if ( function_exists( '\Barn2\Plugin\WC_Quick_View_Pro\wqv' ) ) { |
| 123 |
return \Barn2\Plugin\WC_Quick_View_Pro\wqv()->has_valid_license(); |
| 124 |
} else { |
| 125 |
if ( class_exists( '\Barn2\Plugin\WC_Quick_View_Pro\Quick_View_Plugin' ) ) { |
| 126 |
return \Barn2\Plugin\WC_Quick_View_Pro\Quick_View_Plugin::instance()->has_valid_license(); |
| 127 |
} |
| 128 |
return false; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public static function get_script_suffix() { |
| 133 |
return defined( 'SCRIPT_DEBUG' ) && \SCRIPT_DEBUG ? '' : '.min'; |
| 134 |
} |
| 135 |
|
| 136 |
public static function register_services( $services ) { |
| 137 |
array_map( function( $service ) { |
| 138 |
if ( ( $service instanceof Conditional ) && ! $service->is_required() ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
if ( $service instanceof Registerable ) { |
| 142 |
$service->register(); |
| 143 |
} |
| 144 |
if ( $service instanceof Schedulable ) { |
| 145 |
$service->schedule(); |
| 146 |
} |
| 147 |
}, $services ); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
} |