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