getContactClient(true); } catch (Auth\ConfigError $e) { $ret = $this->displayAuthErrors($e->getErrors(), true); if ($ret) return $ret; } $products = get_option('onecrm_enabled_products'); $symbol = get_option('onecrm_currency_symbol'); if (!$symbol) $symbol = '$'; $symbol_after = get_option('onecrm_currency_after'); if (!is_array($products)) $products = []; $uniq = uniqid(''); list ($req, $signature) = onecrm_signed_request(['uniq' => $uniq]); $init_data = $this->getProductsMeta($products); if (!empty($attr['edit']) && is_array($attr['edit'])) { $edit = $attr['edit']; $plan_id = $attr['edit']['plan']; $prod_id = null; foreach ($init_data['prod_meta']['products'] as $pid => $pdata) { foreach ($pdata['plans'] as $plid => $_) { if ($plid === $plan_id) { $prod_id = $pid; break 2; } } } $edit['product'] = $prod_id; $init_data['edit'] = $edit; } $init_data['settings'] = compact('symbol', 'symbol_after'); $init_data['signature'] = [ 'request' => $req, 'token' => $signature, ]; $init_data['compare_1crm_editions_url'] = self::COMPARE_1CRM_EDITIONS_LINK; $return_page = (int)get_option( 'onecrm_subscriptions_page' ); if ($return_page) { $return_url = get_the_permalink($return_page); $param = empty($attr['edit']['subscription_id']) ? 'created' : 'updated'; $init_data['return_url'] = add_query_arg([$param => 1], $return_url); } if (!empty($_GET['token'])) { if (!empty($_GET['popup'])) { $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; if ( strpos( $current_url, '?' ) !== false ) { list( $current_url, $unused ) = explode( '?', $current_url, 2 ); } $current_url = add_query_arg(['token' => sanitize_key($_GET['token'])], $current_url); echo sprintf( '', json_encode($current_url) ); exit; } $user_id = get_current_user_id(); $the_data = get_user_meta($user_id, 'onecrm_p_signup_data', true); if (is_array($the_data)) { if (!empty($the_data['continue_on_login'])) { unset($the_data['continue_on_login']); update_user_meta($user_id, 'onecrm_p_signup_data', $the_data); } if ($the_data['token'] === sanitize_key($_GET['token'])) { $init_data['continue_subscription'] = $the_data['data']; } } } add_action('wp_footer',function() { wp_enqueue_script('onecrm-subscription-scripts', ONECRMP_PLUGIN_URL . '/js/subscriptions.js', ['jquery', 'jquery-ui-tooltip']); wp_enqueue_style('onecrm-subscriptions-css', ONECRMP_PLUGIN_URL . '/css/subscriptions.css'); }); add_action('wp_print_footer_scripts',function () use ($init_data) { echo ''; }); $ret = '
'; return $ret; } public function getProductsMeta($ids) { $meta = $this->fetchProductsMeta($ids); $user_id = get_current_user_id(); $meta['customer_data']['user_id'] = $user_id; if ($user_id) { $user = wp_get_current_user(); $meta['customer_data']['email'] = $user->get('user_email'); $meta['customer_data']['first_name'] = get_user_meta($user_id, 'first_name', true); $meta['customer_data']['last_name'] = get_user_meta($user_id, 'last_name', true); $meta['customer_data']['account_name'] = get_user_meta($user_id, 'onecrm_p_company_name', true); } if ($user_id && empty($meta['customer_data']['customer_id'])) { $client = Auth\OneCrm::instance()->getAdminClient(); if ($client) { $cust_data = [ 'customer' => [ 'account_name' => $meta['customer_data']['account_name'], 'first_name' => $meta['customer_data']['first_name'], 'last_name' => $meta['customer_data']['last_name'], 'email' => $meta['customer_data']['email'], ], 'account_id' => get_user_meta($user_id, 'onecrm_p_account_id', true), 'contact_id' => get_user_meta($user_id, 'onecrm_p_contact_id', true), ]; try { $result = $client->post('/subscription/create_customer', $cust_data); $meta['customer_data']['customer_id'] = $result['id']; } catch (\Exception $e) { } } } $init_data = [ 'prod_meta' => $meta, ]; return $init_data; } protected function fetchProductsMeta($ids) { $client = Auth\OneCrm::instance()->getAdminClient(); if (!$client) return []; $params = ['products' => $ids]; $user_id = get_current_user_id(); if ($user_id) { $contact_id = get_user_meta( $user_id, 'onecrm_p_contact_id', true); if ($contact_id) $params['contact_id'] = $contact_id; } try { $meta = $client->get('/subscription/meta/signup', $params); return $meta; } catch (\Exception $e) { return []; } } public function subscriptions_shortcode($attr) { $user_id = get_current_user_id(); if (!empty($_GET['edit'])) { return $this->editSubscription($attr, sanitize_key($_GET['edit'])); } if (!empty($_GET['cancel'])) { return $this->cancelSubscription($attr, sanitize_key($_GET['cancel'])); } try { $client = Auth\OneCrm::instance()->getContactClient(true); if (!$client) return false; } catch (Auth\ConfigError $e) { return $this->displayAuthErrors($e->getErrors()); } if (!$client) return false; $fields_meta = $client->get("/meta/fields/Subscription"); $fields_meta['fields']['_edit'] = [ 'name' => '_edit', 'type' => '_custom', 'vname' => ' ', ]; $fields_meta['fields']['name']['type'] = '_custom'; $fields_meta['fields']['name']['vname'] = __('Details', ONECRM_P_TEXTDOMAIN); $fields_meta['fields']['state']['type'] = '_custom'; //Paginate attributes $page = (int)sanitize_key($_GET['page_number']); if ($page < 1) $page = 1; $perPage = self::PER_PAGE; $start = ($page - 1) * $perPage; if ($start < 0) $start = 0; $curPage = (int)($start / $perPage) + 1; $fields = ['name', 'plan', 'state', '_edit']; //Add filters dat from user's input $list_filters = ['filter_text', 'plan', 'view_closed']; $filters = $this->get_input_filters($list_filters, $fields_meta['filters']); $model = $client->model('Subscription'); $list = $model->getList([ 'fields' => ['name', '_edit', 'has_unpaid_invoices', 'state', 'native', 'plan'], 'filters' => $filters, ], $start, $perPage); $rows = $list->getRecords(); $rows = array_map(function($row) { $row['_edit'] = ''; return $row; }, $rows); $pages = new Renderer\Pagination($curPage, $perPage, $list->totalResults(), '?page_number=%s'); $ListView = new Renderer\ListView([ "detail_available" => false, "model" => 'Susbcription', "link_template" => $link_template, "link_template_data" => $link_template_data, 'custom_render' => [$this, 'renderCustomField'], ]); if (!empty($_GET['created']) || !empty($_GET['updated']) || !empty($_GET['cancelled'])) { if (!empty($_GET['created'])) { $text = __( 'Your subscription was created. It can take up to a minute for the subscription to appear in the subscriptions list. Please click here in a while to refresh the list' /* translators: %s will breplaced with an URL */ , ONECRM_P_TEXTDOMAIN ); } elseif (!empty($_GET['cancelled'])) { $text = __( 'Your subscription was cancelled. It can take up to a minute for the subscription status to update. Please click here in a while to refresh the list' /* translators: %s will breplaced with an URL */ , ONECRM_P_TEXTDOMAIN ); } else { $text = __( 'It can take up to a minute for the subscription to be updated. Please click here in a while to refresh the list' /* translators: %s will breplaced with an URL */ , ONECRM_P_TEXTDOMAIN ); } $url = get_the_permalink(); echo '

'; echo sprintf($text, $url); echo '

'; } echo '
'; echo '

' . __('Subscriptions', ONECRM_P_TEXTDOMAIN). '

'; $signup_page = (int)get_option( 'onecrm_signup_page' ); $create_button = null; if ($signup_page) { $signup_url = get_the_permalink($signup_page); $create_button = ''; } $FilterFormView = new FilterFormView([]); echo $FilterFormView->render( [ 'filters' => $list_filters, 'input' => onecrm_p_sanitize_postdata_multiline($_POST), 'def' => $fields_meta['filters'], 'model' => ['Susbcription'], 'create_button' => $create_button, 'page_url' => $_SERVER['REQUEST_URI'] ] ); echo $pages->render(); echo $ListView->render(compact("rows", "fields", "fields_meta")); echo $pages->render(); echo '
'; } protected function cancelSubscription($attr, $sid) { try { $client = Auth\OneCrm::instance()->getContactClient(true); if (!$client) return false; } catch (Auth\ConfigError $e) { return $this->displayAuthErrors($e->getErrors()); } $client = Auth\OneCrm::instance()->getAdminClient(); if (!$client) return ''; if (!empty($_POST['cancel_at'])) { $data = [ 'subscription_id' => $sid, 'end_of_term' => $_POST['cancel_at'] == 'end_of_term', 'credits' => 'prorate', ]; try { $result = $client->post('/subscription/cancel', $data); if (empty($result['error'])) { $url = add_query_arg(['cancelled' => 1], get_the_permalink()); wp_redirect($url); exit; } echo $result['error']; } catch (\Exception $e) { echo $e->getMessage(); } } $subsc_data = $client->get('/subscription/details/' . $sid); echo '

'; echo '

' . __('You are about to cancel a subscription', ONECRM_P_TEXTDOMAIN) . '

'; echo '

' . esc_html($subsc_data['name']) . '

'; echo '

'; echo ''; echo '
'; echo '

'; echo '' . __('Return to subscriptions list', ONECRM_P_TEXTDOMAIN) . ' '; echo ""; echo '

'; } protected function editSubscription($attr, $sid) { try { $client = Auth\OneCrm::instance()->getContactClient(true); if (!$client) return false; } catch (Auth\ConfigError $e) { return $this->displayAuthErrors($e->getErrors()); } $client = Auth\OneCrm::instance()->getAdminClient(); if (!$client) return []; $user_id = get_current_user_id(); $account_id = get_user_meta( $user_id, 'onecrm_p_account_id', true); $subsc_data = $client->get('/subscription/details/' . $sid); if ($subsc_data['account_id'] !== $account_id) return; if (!is_array($attr)) { $attr = []; } $attr['edit'] = [ 'addons' => $subsc_data['addons'], 'options' => $subsc_data['options'], 'plan' => $subsc_data['plan_id'], 'qty' => $subsc_data['quantity'], 'subscription_id' => $subsc_data['pp_subs_id'], 'invoice_notes' => isset($subsc_data['invoice_notes']) ? $subsc_data['invoice_notes'] : '', ]; return $this->signup_shortcode($attr); } public function renderCustomField($field, $model, $meta) { switch ($field) { case 'name': $ret = htmlspecialchars($model[$field]); if (empty($model['_formatted'])) return $ret; $f = $model['_formatted']; $ret .= '
'; if (!empty($f['qty_label'])) $ret .= $f['qty_label']; else $ret .= __('Quantity', ONECRM_P_TEXTDOMAIN); $ret .= ': ' . $f['quantity']; if (!empty($f['addons'])) { foreach ($f['addons'] as $a) { $ret .= '
' . __('Addon', ONECRM_P_TEXTDOMAIN) . ': '; $ret .= htmlspecialchars($a['name']); if ($a['quantity'] > 1) { $ret .= " (x{$a['quantity']})"; } } } if (!empty($f['current_term_start'])) { $ret .= '
'; $ret .= __('Current term start', ONECRM_P_TEXTDOMAIN) . ': '; $ret .= $f['current_term_start']; } if (!empty($f['current_term_end'])) { $ret .= '
'; $ret .= __('Current term end', ONECRM_P_TEXTDOMAIN) . ': '; $ret .= $f['current_term_end']; } if (!empty($f['due_invoices_count'])) { $ret .= '
'; $ret .= __('Unpaid invoices', ONECRM_P_TEXTDOMAIN) . ': '; $ret .= $f['due_invoices_count']; } return $ret; case '_edit': if ($model['state'] != 'active') return ''; $url = add_query_arg(['edit' => $model['id']], get_the_permalink()); $ret = "" . __('Edit', ONECRM_P_TEXTDOMAIN) . ""; $url = add_query_arg(['cancel' => $model['id']], get_the_permalink()); $ret .= "
" . __('Cancel', ONECRM_P_TEXTDOMAIN) . ""; return $ret; case 'state': $value = array_filter($meta['fields'][$field]['options'], function($value) use ($model, $field) { return $value['value'] == $model[$field]; }); $value = current($value); $value = $value ? $value['label'] : $model[$field]; if (!empty($mode['has_unpaid_invoices'])) { $value = __('Payment Failed', ONECRM_P_TEXTDOMAIN); $cls = 'failed'; } else { $cls = $model[$field] == 'active' ? 'active' : 'inactive'; } return '' . htmlspecialchars($value) . ''; case 'plan': return htmlspecialchars($model[$field]); } } function displayAuthErrors($errors, $no_login = false) { if (in_array('login', $errors) && !$no_login) { $ret = '

'; $ret .= __('You need to login in order to access this area', ONECRM_P_TEXTDOMAIN); $ret .= '

'; $ret .= ''; $ret .= '