Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Membership Block #9802

Merged
merged 63 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
ff86e4c
Initial code from wpcom
artpi Jun 19, 2018
e6a4187
fix blog id
artpi Jun 19, 2018
801ed05
Fix content
artpi Jun 20, 2018
94f6f20
Linter
artpi Jun 29, 2018
c1e86d0
again - lint
artpi Jun 29, 2018
b3126cb
Rework into a block part 1
artpi Mar 18, 2019
04843df
memberships changes
artpi Mar 19, 2019
8d1ff79
Memberships status endpoint
artpi Mar 20, 2019
2feeb2b
endpoint enhancements
artpi Mar 20, 2019
600bc93
Something memberships
artpi Mar 25, 2019
641bd51
whitelist the option
artpi Mar 25, 2019
5c8e88d
Thickbox is working
artpi Mar 26, 2019
7c9af41
Pass button color
artpi Mar 26, 2019
4b061e9
Endpoint changes because I have to rebase
artpi Mar 27, 2019
675be9c
Port work from calypso
artpi Mar 27, 2019
a20c69c
Make it work
artpi Mar 27, 2019
c310a99
Fixes to the UI
artpi Mar 27, 2019
6787afc
fix for price change
artpi Mar 27, 2019
63c2564
remove legacy code
artpi Mar 27, 2019
9dc7d46
Review feedbac
artpi Apr 1, 2019
976d7f1
repurpose format_price
artpi Apr 1, 2019
110e955
permissions check
artpi Apr 1, 2019
9087c2c
remove simple payments import
artpi Apr 1, 2019
2553672
make it pull a proper file
artpi Apr 1, 2019
236327f
review feedback - JS parts
artpi Apr 2, 2019
3d2c08a
Update modules/memberships/class-jetpack-memberships.php
sirreal Apr 2, 2019
8de9da5
Update extensions/blocks/membership-button/edit.jsx
sirreal Apr 2, 2019
920f1c3
Update extensions/blocks/membership-button/edit.jsx
sirreal Apr 2, 2019
be33ad6
textdomain
artpi Apr 2, 2019
3ddc1f0
remove unnecessary version
artpi Apr 2, 2019
046b806
Remove default button text from block default
sirreal Apr 2, 2019
94e6994
Use unwrapped i18n depenencies instaed of relative path
sirreal Apr 2, 2019
e68d671
Textdomain
sirreal Apr 2, 2019
810eef9
texdomain again
artpi Apr 2, 2019
7af5028
properly escape attributes
artpi Apr 2, 2019
64fe6e4
reuse currency functins
artpi Apr 2, 2019
d4586ec
Revert simple payments format_currency changes
artpi Apr 2, 2019
35397ba
solve cancel and a small UI issue
artpi Apr 2, 2019
25f880c
fix lint in class memberships
artpi Apr 2, 2019
28db6a4
lint api files
artpi Apr 2, 2019
0a6aab5
Add php files to linter
artpi Apr 2, 2019
cc0343a
jetpack-memberships class moved on wpcom
artpi Apr 2, 2019
579e738
make call to wpcom as a user
artpi Apr 2, 2019
7730f21
now really make it as a user, this is the php file
artpi Apr 2, 2019
ad22f80
Add missing textdomains
artpi Apr 2, 2019
f5d8d68
Handle message to pass the iframe modal
artpi Apr 4, 2019
6c3423c
Update color hex's to become Muriel variables
Apr 4, 2019
45682bb
Review feedback - some protection
artpi Apr 5, 2019
fc685fa
Rename meta name from mem_ to jetpack_memberships_
artpi Apr 5, 2019
682fdc5
change the var
artpi Apr 12, 2019
dfce75d
change CSS vars
artpi Apr 12, 2019
bd5f5e2
Fixes for changes in the upstream
artpi Apr 23, 2019
b436744
fix after implementing review feedback
artpi Apr 23, 2019
b421590
Change copy for item description
artpi Apr 24, 2019
f049f82
Code review suggested edits - mostly copy
jeherve Apr 25, 2019
a7761d6
Review feedback: remove unnecessary import
artpi Apr 26, 2019
4235ae9
Better feedback where my account is not connected
artpi Apr 26, 2019
9bae01c
Endpoints nly for wpcom/active jetpack
artpi Apr 26, 2019
4c3153d
remove unused function
artpi Apr 26, 2019
fea7f0b
Additional check if JP is active
artpi Apr 27, 2019
81c6d9c
Pass Lang to subscribe form
artpi Apr 27, 2019
7956f6a
error catching
artpi Apr 27, 2019
6a57671
PHPCS whitelist: add the whole new module folder to the pre-commit
jeherve Apr 29, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions _inc/lib/core-api/wpcom-endpoints/memberships.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName
/**
* Memberships: API to communicate with "product" database.
*
* @package Jetpack
* @since 7.3.0
*/

/**
* Class WPCOM_REST_API_V2_Endpoint_Memberships
* This introduces V2 endpoints.
*/
class WPCOM_REST_API_V2_Endpoint_Memberships extends WP_REST_Controller {

/**
* WPCOM_REST_API_V2_Endpoint_Memberships constructor.
*/
public function __construct() {
$this->namespace = 'wpcom/v2';
$this->rest_base = 'memberships';
$this->wpcom_is_wpcom_only_endpoint = true;
$this->wpcom_is_site_specific_endpoint = true;
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}

/**
* Called automatically on `rest_api_init()`.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
$this->rest_base . '/status',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_status' ),
'permission_callback' => array( $this, 'get_status_permission_check' ),
),
)
);
register_rest_route(
$this->namespace,
$this->rest_base . '/product',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_product' ),
'permission_callback' => array( $this, 'get_status_permission_check' ),
'args' => array(
'title' => array(
'type' => 'string',
'required' => true,
),
'price' => array(
'type' => 'float',
'required' => true,
),
'currency' => array(
'type' => 'string',
'required' => true,
),
'interval' => array(
'type' => 'string',
'required' => true,
),
),
),
)
);
}

/**
* Ensure the user has proper permissions
*
* @return boolean
*/
public function get_status_permission_check() {
return current_user_can( 'edit_posts' );
}

/**
* Do create a product based on data, or pass request to wpcom.
*
* @param object $request - request passed from WP.
*
* @return array|WP_Error
*/
public function create_product( $request ) {
if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
require_lib( 'memberships' );
$connected_destination_account_id = Jetpack_Memberships::get_connected_account_id();
if ( ! $connected_destination_account_id ) {
return new WP_Error( 'no-destination-account', __( 'Please set up a Stripe account for this site first', 'jetpack' ) );
}
$product = Memberships_Product::create(
get_current_blog_id(),
array(
'title' => $request['title'],
'price' => $request['price'],
'currency' => $request['currency'],
'interval' => $request['interval'],
'connected_destination_account_id' => $connected_destination_account_id,
)
);
return $product->to_array();
} else {
$blog_id = Jetpack_Options::get_option( 'id' );
$response = Jetpack_Client::wpcom_json_api_request_as_user(
artpi marked this conversation as resolved.
Show resolved Hide resolved
"/sites/$blog_id/{$this->rest_base}/product",
'v2',
array(
'method' => 'POST',
),
array(
'title' => $request['title'],
'price' => $request['price'],
'currency' => $request['currency'],
'interval' => $request['interval'],
)
);
if ( is_wp_error( $response ) ) {
if ( $response->get_error_code() === 'missing_token' ) {
return new WP_Error( 'missing_token', __( 'Please connect your user account to WordPress.com', 'jetpack' ), 404 );
}
return new WP_Error( 'wpcom_connection_error', __( 'Could not connect to WordPress.com', 'jetpack' ), 404 );
}
$data = isset( $response['body'] ) ? json_decode( $response['body'], true ) : null;
return $data;
}

return $request;
}

/**
* Get a status of connection for the site. If this is Jetpack, pass the request to wpcom.
*
* @return array|WP_Error
*/
public function get_status() {
$connected_account_id = Jetpack_Memberships::get_connected_account_id();
$connect_url = '';
if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
require_lib( 'memberships' );
$blog_id = get_current_blog_id();
if ( ! $connected_account_id ) {
$connect_url = get_memberships_connected_account_redirect( get_current_user_id(), $blog_id );
}
$products = get_memberships_plans( $blog_id );
} else {
$blog_id = Jetpack_Options::get_option( 'id' );
$response = Jetpack_Client::wpcom_json_api_request_as_user(
"/sites/$blog_id/{$this->rest_base}/status",
'v2',
array(),
null
);
if ( is_wp_error( $response ) ) {
if ( $response->get_error_code() === 'missing_token' ) {
return new WP_Error( 'missing_token', __( 'Please connect your user account to WordPress.com', 'jetpack' ), 404 );
}
return new WP_Error( 'wpcom_connection_error', __( 'Could not connect to WordPress.com', 'jetpack' ), 404 );
}
$data = isset( $response['body'] ) ? json_decode( $response['body'], true ) : null;
if ( ! $connected_account_id ) {
$connect_url = empty( $data['connect_url'] ) ? '' : $data['connect_url'];
}
$products = empty( $data['products'] ) ? array() : $data['products'];
}
return array(
'connected_account_id' => $connected_account_id,
'connect_url' => $connect_url,
'products' => $products,
);
}
}

if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_active() ) {
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Memberships' );
}

2 changes: 2 additions & 0 deletions bin/phpcs-whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ module.exports = [
'functions.global.php',
'functions.opengraph.php',
'_inc/lib/debugger/',
'_inc/lib/core-api/wpcom-endpoints/memberships.php',
'_inc/lib/class.jetpack-password-checker.php',
'modules/masterbar/',
'modules/memberships/',
'modules/module-extras.php',
'modules/module-info.php',
'modules/theme-tools/social-menu/',
Expand Down
Loading