| 1 |
<?php |
| 2 |
namespace LWS\WOOREWARDS\Ui\Blocks; |
| 3 |
if (!defined('ABSPATH')) exit(); |
| 4 |
|
| 5 |
/** |
| 6 |
* Class for integrating with WooCommerce Blocks |
| 7 |
* @see https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce-blocks/docs/third-party-developers/extensibility/checkout-block/available-slot-fills.md#experimentaldiscountsmeta |
| 8 |
*/ |
| 9 |
class Integration implements \Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface { |
| 10 |
const API = 'lws_woorewards'; |
| 11 |
protected $editionScriptHandlers = []; |
| 12 |
protected $scriptHandlers = []; |
| 13 |
|
| 14 |
/** An array of key, value pairs of data made available to the block on the client side. |
| 15 |
* @return array */ |
| 16 |
public function get_script_data() { |
| 17 |
$systems = []; |
| 18 |
foreach (\LWS\WOOREWARDS\Ui\Blocks\Extension::getSystems() as $pool) { |
| 19 |
/** @var \LWS\WOOREWARDS\PRO\Core\Pool $pool */ |
| 20 |
$symbol = $pool->getSymbol(2); |
| 21 |
$systems[$pool->getName()] = [ |
| 22 |
'show' => true, |
| 23 |
'name' => $pool->getName(), |
| 24 |
//'label' => \wp_kses($pool->getOption('display_title'), array()), |
| 25 |
/* translators: %s: points symbol */ |
| 26 |
'field' => sprintf(__("Use your %s", 'woorewards-lite'), $symbol), |
| 27 |
'title' => __("You own", 'woorewards-lite'), |
| 28 |
'input' => __("Enter a value", 'woorewards-lite'), |
| 29 |
'apply' => __("Apply", 'woorewards-lite'), |
| 30 |
'details'=> $this->getDetails($pool), |
| 31 |
]; |
| 32 |
} |
| 33 |
$data = [ |
| 34 |
'enable' => true, |
| 35 |
'logged-user' => (bool)\get_current_user_id(), |
| 36 |
'systems' => $systems, |
| 37 |
]; |
| 38 |
return \apply_filters('lws-woorewards_blocks_script_data', $data); |
| 39 |
} |
| 40 |
|
| 41 |
/** @return array of string */ |
| 42 |
protected function getDetails($system) |
| 43 |
{ |
| 44 |
$details = []; |
| 45 |
$info = array_merge([ |
| 46 |
'name' => $system->getName(), |
| 47 |
'symbols' => $system->getSymbol('2'), |
| 48 |
], $system->getOptions([ |
| 49 |
'direct_reward_point_rate', |
| 50 |
'direct_reward_max_percent_of_cart', |
| 51 |
'direct_reward_min_points_on_cart', |
| 52 |
'direct_reward_max_points_on_cart', |
| 53 |
'direct_reward_total_floor', |
| 54 |
'direct_reward_min_subtotal' |
| 55 |
])); |
| 56 |
|
| 57 |
if (\trim($info['direct_reward_point_rate']) != '') { |
| 58 |
$details['rate'] = sprintf( |
| 59 |
/* translators: %1$s: points amount, %2$s: currency value */ |
| 60 |
__('%1$s → %2$s', 'woorewards-lite'), |
| 61 |
$system->formatPoints(1, true), |
| 62 |
\LWS\Adminpanel\Tools\Conveniences::getCurrencyPrice( |
| 63 |
$info['direct_reward_point_rate'], |
| 64 |
\apply_filters('lws_woorewards_point_rate_displays_real_decimals', false), |
| 65 |
true |
| 66 |
) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
if (\intval($info['direct_reward_min_points_on_cart']) > 0) { |
| 71 |
$details['min_points'] = sprintf( |
| 72 |
/* translators: %s: minimum points */ |
| 73 |
__('Minimum: %s', 'woorewards-lite'), |
| 74 |
$system->formatPoints($info['direct_reward_min_points_on_cart'], true) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
if (\intval($info['direct_reward_max_points_on_cart']) > 0) { |
| 79 |
$details['max_points'] = sprintf( |
| 80 |
/* translators: %s: maximum points */ |
| 81 |
__('Maximum: %s', 'woorewards-lite'), |
| 82 |
$system->formatPoints($info['direct_reward_max_points_on_cart'], true) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
if ($info['direct_reward_max_percent_of_cart'] != '' && $info['direct_reward_max_percent_of_cart'] < 100.0) { |
| 87 |
$details['max_perc'] = sprintf( |
| 88 |
/* translators: %s: maximum percentage */ |
| 89 |
__('Max. discount: %s%%', 'woorewards-lite'), |
| 90 |
$info['direct_reward_max_percent_of_cart'] |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
if ($info['direct_reward_total_floor'] != '' && $info['direct_reward_total_floor'] > 0.0) { |
| 95 |
$details['floor'] = sprintf( |
| 96 |
/* translators: %s: minimum cart total */ |
| 97 |
__('Min. total: %s', 'woorewards-lite'), |
| 98 |
\LWS\Adminpanel\Tools\Conveniences::getCurrencyPrice($info['direct_reward_total_floor']) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
if ($info['direct_reward_min_subtotal'] != '' && $info['direct_reward_min_subtotal'] > 0.0) { |
| 103 |
$details['min'] = sprintf( |
| 104 |
/* translators: %s: minimum subtotal amount */ |
| 105 |
__('Requires at least a %s subtotal', 'woorewards-lite'), |
| 106 |
\LWS\Adminpanel\Tools\Conveniences::getCurrencyPrice($info['direct_reward_min_subtotal']) |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
return \apply_filters('lws_woorewards_blocks_pointsoncart_details_text', $details, $system, $info); |
| 111 |
} |
| 112 |
|
| 113 |
/** The name of the integration. |
| 114 |
* @return string */ |
| 115 |
public function get_name() { |
| 116 |
return 'lws-wr-blocks'; |
| 117 |
} |
| 118 |
|
| 119 |
/** When called invokes any initialization/setup for the integration. */ |
| 120 |
public function initialize() { |
| 121 |
$this->registerBloc(array()); |
| 122 |
} |
| 123 |
|
| 124 |
/** Registers the main JS file required to add filters and Slot/Fills. */ |
| 125 |
public function registerBloc(array $options) { |
| 126 |
$options = \array_merge(array( |
| 127 |
'script' => 'index.js', |
| 128 |
'style' => 'style-index.css', |
| 129 |
'asset' => 'index.asset.php', |
| 130 |
'handle' => LWS_WOOREWARDS_PAGE . '-blocks', |
| 131 |
'style-handle' => true, |
| 132 |
), $options); |
| 133 |
|
| 134 |
if (true === $options['style-handle']) $options['style-handle'] = $options['handle']; |
| 135 |
|
| 136 |
$options['script'] = '/build/' . $options['script']; |
| 137 |
$options['style'] = '/build/' . $options['style']; |
| 138 |
$options['asset'] = LWS_WOOREWARDS_PATH . '/build/' . $options['asset']; |
| 139 |
|
| 140 |
$script_asset = array( |
| 141 |
'dependencies' => [], |
| 142 |
'version' => self::version($options['script']), |
| 143 |
); |
| 144 |
if (\file_exists($options['asset'])) { |
| 145 |
$script_asset = \array_merge($script_asset, require($options['asset'])); |
| 146 |
} |
| 147 |
|
| 148 |
if ($options['style-handle']) { |
| 149 |
\wp_enqueue_style( |
| 150 |
'lws-wr-blocks-blocks-integration', |
| 151 |
\plugins_url($options['style'], LWS_WOOREWARDS_FILE), |
| 152 |
[], |
| 153 |
self::version($options['style']) |
| 154 |
); |
| 155 |
$this->editionScriptHandlers[$options['style-handle']] = $options['style-handle']; |
| 156 |
} |
| 157 |
|
| 158 |
if ($options['handle']) { |
| 159 |
\wp_register_script( |
| 160 |
$options['handle'], |
| 161 |
\plugins_url($options['script'], LWS_WOOREWARDS_FILE), |
| 162 |
$script_asset['dependencies'], |
| 163 |
$script_asset['version'], |
| 164 |
true |
| 165 |
); |
| 166 |
\wp_set_script_translations( |
| 167 |
$options['handle'], |
| 168 |
'woorewards-lite', |
| 169 |
LWS_WOOREWARDS_PATH . '/languages' |
| 170 |
); |
| 171 |
$this->scriptHandlers[$options['handle']] = $options['handle']; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** Returns an array of script handles to enqueue in the frontend context. |
| 176 |
* @return string[] */ |
| 177 |
public function get_script_handles() { |
| 178 |
return $this->scriptHandlers; |
| 179 |
} |
| 180 |
|
| 181 |
/** Returns an array of script handles to enqueue in the editor context. |
| 182 |
* @return string[] */ |
| 183 |
public function get_editor_script_handles() { |
| 184 |
return $this->editionScriptHandlers; |
| 185 |
} |
| 186 |
|
| 187 |
/** Get the file modified time as a cache buster if we're in dev mode. |
| 188 |
* @param string $file Local path to the file. |
| 189 |
* @return string The cache buster value to use for the given file. */ |
| 190 |
protected static function version($file) { |
| 191 |
if (\defined('SCRIPT_DEBUG') && SCRIPT_DEBUG && \file_exists(LWS_WOOREWARDS_PATH . $file)) { |
| 192 |
return \filemtime(LWS_WOOREWARDS_PATH . $file); |
| 193 |
} else { |
| 194 |
return LWS_WOOREWARDS_VERSION; |
| 195 |
} |
| 196 |
} |
| 197 |
} |