Skip to content

Commit d35f3fa

Browse files
committed
Merge branch 'main' into release
2 parents 542eec7 + d9b9eb6 commit d35f3fa

File tree

17 files changed

+371
-20
lines changed

17 files changed

+371
-20
lines changed

phpcs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@
1616
<exclude name="PSR12.Properties.ConstantVisibility.NotFound"/>
1717
<exclude name="Squiz.Classes.ValidClassName"/>
1818
</rule>
19+
20+
<!--
21+
This file extends from a main wordpress class and they don't use the camelcase convention for method names.
22+
-->
23+
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
24+
<exclude-pattern>src/server/Api.php</exclude-pattern>
25+
</rule>
1926
</ruleset>

src/assets/css/beans-storefront.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ div: .beans-section
266266

267267

268268
/* Woocommerce */
269-
.woocommerce-remove-coupon[data-coupon="redeem_points"] {
269+
.woocommerce-remove-coupon[data-coupon="redeem_points"],
270+
.woocommerce-remove-coupon[data-coupon="redeem_tiers"] {
270271
display: none;
271272
}
272273

src/beans-woocommerce.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* Plugin Name: Beans
55
* Plugin URI: https://www.trybeans.com/
66
* Description: Marketing Apps for WooCommerce.
7-
* Version: 3.3.4
7+
* Version: 3.3.5
88
* Author: Beans
99
* Author URI: https://www.trybeans.com/
1010
* Text Domain: beans-woo
1111
* Domain Path: /languages
1212
* Requires PHP: 7.1
1313
* Requires at least: 5.2
1414
* WC requires at least: 4.1
15-
* WC tested up to: 5.9.*
15+
* WC tested up to: 6.1.*
1616
* @author Beans
1717
*/
1818

@@ -36,7 +36,7 @@
3636
}
3737

3838
if (!defined('BEANS_PLUGIN_VERSION')) {
39-
define('BEANS_PLUGIN_VERSION', '3.3.4');
39+
define('BEANS_PLUGIN_VERSION', '3.3.5');
4040
}
4141

4242

src/includes/Beans.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Beans
3333
{
3434
public $endpoint = 'https://api.trybeans.com/v3/';
3535

36-
const VERSION = '3.3.4'; // private
36+
const VERSION = '3.3.5'; // private
3737

3838
private $_secret = '';
3939
private $_next_page = '';

src/includes/Helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function getDomain($sub)
2020
'CONNECT' => 'connect.trybeans.com',
2121
'WWW' => 'www.trybeans.com',
2222
'CDN' => 'cdn.trybeans.com',
23-
'HOOK' => 'api.radix.trybeans.com',
23+
'RADIX' => 'api.radix.trybeans.com',
2424
);
2525
$val = getenv($key);
2626

@@ -158,7 +158,7 @@ public static function getCurrentPage()
158158
{
159159

160160
// This function is used in `LianaProductObserver.updateProductPrice` to update the currency of the price in
161-
// point when the Buy with point feature is active. We do not update the price
161+
// point when the Buy with point feature is active. We do not update the price
162162
// on the `shop` and` product` pages. Be careful when updating this function.
163163
if (is_front_page()) {
164164
return 'home';

src/readme.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
=== Beans · Marketing Apps for Woocommerce ===
22

3-
Contributors: nas2lor, yan_dah, corentinalcoy
3+
Contributors: nas2lor, yan_dah, corentinal
44
Tags: loyalty, referral, email, popup, social, rewards, automated emails, referral program, marketing email, widget, message, coupon, reward, retention, voucher, points, loyalty program, incentive, incentivize, woocommerce, marketing, advertising, referring, discount, promoting, coupon, trybeans, beans, affiliate, e-commerce, ecommerce, engage, customers, woocommerce plugin, woocommerce extension, facebook, like, social network, credit, free, twitter, tweet, google, google plus, PInterest
55
Requires at least: 5.2
66
Requires PHP: 7.1
7-
Tested up to: 5.8.2
8-
Stable tag: 3.3.4
7+
Tested up to: 5.9
8+
Stable tag: 3.3.5
99
License: Apache License, Version 2.0
1010
License URI: http://www.apache.org/licenses/LICENSE-2.0
1111

@@ -116,6 +116,13 @@ The WordPress codex contains instructions on
116116

117117
== Changelog ==
118118

119+
== 3.3.5 ==
120+
- Introduce riper LTS/EDGE version
121+
- Introduce lifetime discount on the tier system
122+
- Implement Beans REST API v1
123+
- Test compatibility with WordPress 5.9
124+
- Test compatibility with WooCommerce 6.1.1
125+
119126
== 3.3.4 ==
120127
- Fix buy with points product price on product page
121128

src/server/Api.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
namespace BeansWoo\Server;
4+
5+
use Beans\BeansError;
6+
use BeansWoo\Helper;
7+
8+
class ConnectorRESTController extends \WP_REST_Controller
9+
{
10+
// The `wc-` prefix helps to use the WooCommerce Rest authentication on our routes.
11+
protected $namespace = "wc-beans/v1";
12+
protected $rest_base = 'connector';
13+
14+
public function register_routes()
15+
{
16+
register_rest_route(
17+
$this->namespace,
18+
'/' . $this->rest_base . '/current',
19+
array(
20+
array(
21+
'methods' => \WP_REST_Server::READABLE,
22+
'callback' => array($this, 'get_item'),
23+
'permission_callback' => array($this, 'get_item_permissions_check'),
24+
'args' => $this->get_args(\WP_REST_Server::READABLE)
25+
),
26+
array(
27+
'methods' => \WP_REST_Server::EDITABLE,
28+
'callback' => array($this, 'update_item'),
29+
'permission_callback' => array($this, 'update_item_permissions_check'),
30+
'args' => $this->get_args(\WP_REST_Server::EDITABLE)
31+
)
32+
)
33+
);
34+
35+
register_rest_route(
36+
$this->namespace,
37+
'/' . $this->rest_base . '/install',
38+
array(
39+
array(
40+
'methods' => \WP_REST_Server::CREATABLE,
41+
'callback' => array($this, 'install'),
42+
'permission_callback' => array($this, 'install_item_permissions_check'),
43+
'args' => $this->get_args(\WP_REST_Server::CREATABLE)
44+
),
45+
)
46+
);
47+
}
48+
49+
public function get_item($request)
50+
{
51+
$response = new \WP_REST_Response(self::get_item_data());
52+
$response->set_status(200);
53+
return $response;
54+
}
55+
public function install($request)
56+
{
57+
if (isset($request['card']) && isset($request['token'])) {
58+
$card_id = $request['card'];
59+
$token = $request['token'];
60+
// Using `Connector::processSetup()` doesn't work. I had an error about the
61+
// `Class BeansWoo\Admin\Connector` doesn't exist. I tried to investigate but I am not able to find out
62+
// what is the bug.
63+
// todo; Use `Connector::processSetup()` instead of duplicating the logic;
64+
Helper::$key = $card_id;
65+
66+
try {
67+
$integration_key = Helper::API()->get('core/auth/integration_key/' . $token);
68+
} catch (BeansError $e) {
69+
Helper::log('Connecting failed: ' . $e->getMessage());
70+
return new \WP_Error(
71+
"beans_rest_cannot_setup",
72+
__("Unable to setup Beans plugin", 'beans'),
73+
array('status' => 400)
74+
);
75+
}
76+
77+
Helper::setConfig('key', $integration_key['id']);
78+
Helper::setConfig('card', $integration_key['card']['id']);
79+
Helper::setConfig('secret', $integration_key['secret']);
80+
Helper::clearTransients();
81+
}
82+
83+
$response = new \WP_REST_Response(self::get_item_data());
84+
$response->set_status(201);
85+
return $response;
86+
}
87+
88+
public function update_item($request)
89+
{
90+
if (isset($request['riper_version'])) {
91+
Helper::setConfig('riper_version', $request['riper_version']);
92+
}
93+
94+
$response = new \WP_REST_Response(self::get_item_data());
95+
$response->set_status(202);
96+
return $response;
97+
}
98+
99+
public function get_item_permissions_check($request)
100+
{
101+
return $this->check_permissions($request, 'view');
102+
}
103+
104+
public function update_item_permissions_check($request)
105+
{
106+
return $this->check_permissions($request, 'edit');
107+
}
108+
109+
public function install_item_permissions_check($request)
110+
{
111+
return $this->check_permissions($request, 'install');
112+
}
113+
114+
public function get_args($action)
115+
{
116+
if ($action === \WP_REST_Server::EDITABLE) {
117+
return array(
118+
'riper_version' => array(
119+
'required' => false,
120+
'sanitize_callback' => array(__CLASS__, 'sanitize_value'),
121+
'validate_callback' => function ($param, $request, $key) {
122+
return is_string($param) and in_array($param, array('lts', 'edge'));
123+
},
124+
),
125+
);
126+
} elseif ($action == \WP_REST_Server::CREATABLE) {
127+
return array(
128+
'token' => array(
129+
'required' => true,
130+
'sanitize_callback' => array(__CLASS__, 'sanitize_value'),
131+
'validate_callback' => function ($param, $request, $key) {
132+
return is_string($param);
133+
},
134+
),
135+
'card' => array(
136+
'required' => true,
137+
'sanitize_callback' => array(__CLASS__, 'sanitize_value'),
138+
'validate_callback' => function ($param, $request, $key) {
139+
return is_string($param) and substr($param, 0, strlen($key)) === $key;
140+
},
141+
),
142+
);
143+
}
144+
return array();
145+
}
146+
147+
public static function sanitize_value($param, $request, $key)
148+
{
149+
return htmlspecialchars($param);
150+
}
151+
152+
private function check_permissions($request, $action)
153+
{
154+
if (!current_user_can('manage_options')) {
155+
return new \WP_Error(
156+
"beans_rest_cannot_$action",
157+
__("Sorry, you are not allowed to $action this resource.", 'woocommerce'),
158+
array('status' => rest_authorization_required_code())
159+
);
160+
}
161+
return true;
162+
}
163+
164+
public static function get_item_data()
165+
{
166+
return array(
167+
'card' => Helper::getConfig('card'),
168+
'is_setup' => Helper::isSetup(),
169+
'riper_version' => Helper::getConfig('riper_version'),
170+
'pages' => Helper::getBeansPages(),
171+
);
172+
}
173+
}

src/server/Hooks.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,9 @@ private static function getBeansPages()
8888

8989
foreach (Helper::getBeansPages() as $app_name => $values) {
9090
$page_id = $values['page_id'];
91-
$page_exists = false;
9291
$page_visible = false;
9392

9493
$page_set = true;
95-
if (get_post($page_id)) {
96-
$page_exists = true;
97-
}
9894
if ('publish' === get_post_status($page_id)) {
9995
$page_visible = true;
10096
}
@@ -105,7 +101,7 @@ private static function getBeansPages()
105101
'type' => $values['type'],
106102
'shortcode_present' => true,
107103
'shortcode_required' => true,
108-
'page_exists' => $page_exists,
104+
'page_exists' => $values['page_exists'],
109105
'page_visible' => $page_visible,
110106
'page_name' => $values['page_name'],
111107
'shortcode' => $values['shortcode'],
@@ -126,7 +122,7 @@ public static function postWebhookStatus($status)
126122
);
127123

128124
try {
129-
Helper::API('HOOK')->post('hook/radix/woocommerce/shop/plugin_status', $args, $headers);
125+
Helper::API('RADIX')->post('hook/radix/woocommerce/shop/plugin_status', $args, $headers);
130126
} catch (BeansError $e) {
131127
}
132128
}

src/server/init.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BeansWoo\Helper;
66

7+
require_once 'Api.php';
78
require_once 'Hooks.php';
89

910
defined('ABSPATH') or die;
@@ -14,6 +15,13 @@ class Main
1415
public static function init()
1516
{
1617
Hooks::init();
18+
add_filter('woocommerce_rest_api_get_rest_namespaces', array(__CLASS__, 'registerRESTRoutes'));
19+
}
20+
21+
public static function registerRESTRoutes($controllers)
22+
{
23+
$controllers['wc-beans/v1']['connector'] = 'BeansWoo\Server\ConnectorRESTController';
24+
return $controllers;
1725
}
1826

1927
public static function registerPluginActivationHooks()

src/storefront/bamboo/Page/BambooPage.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ public static function renderPage()
2121

2222
public static function getPageReferences()
2323
{
24+
$page_id = Helper::getConfig('bamboo_page');
25+
2426
return array(
2527
'shortcode' => '[' . self::PAGE_SHORTCODE . ']',
26-
'page_id' => Helper::getConfig('bamboo_page'),
28+
'page_id' => $page_id,
2729
'page_name' => 'Referral Program',
2830
'option' => 'beans_referral_page_id',
2931
'slug' => 'referral-program',
3032
'type' => 'referral',
33+
'page_exists' => get_post($page_id) ? true : false,
3134
);
3235
}
3336
}

0 commit comments

Comments
 (0)