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 ''; $ret .= __('You need to login in order to access this area', ONECRM_P_TEXTDOMAIN); $ret .= '
'; $ret .= ''; $ret .= '