/** * Shortcode to output a number box - [stripe_radio] * * @since 2.0.0 */ function stripe_radio($attr) { static $counter = 1; global $sc_script_options; $attr = shortcode_atts(array('id' => '', 'label' => '', 'default' => '', 'options' => '', 'is_quantity' => 'false', 'amounts' => '', 'is_amount' => 'false'), $attr, 'stripe_radio'); $id = $attr['id']; $label = $attr['label']; $default = $attr['default']; $options = $attr['options']; $is_quantity = $attr['is_quantity']; $amounts = $attr['amounts']; $is_amount = $attr['is_amount']; Shortcode_Tracker::add_new_shortcode('stripe_radio_' . $counter, 'stripe_radio', $attr, false); // Check for ID and if it doesn't exist then we will make our own if ($id == '') { $id = 'sc_cf_radio_' . $counter; } $options = explode(',', $options); if (!empty($amounts)) { $amounts = explode(',', str_replace(' ', '', $amounts)); if (count($options) != count($amounts)) { Shortcode_Tracker::update_error_count(); if (current_user_can('manage_options')) { Shortcode_Tracker::add_error_message('<h6>' . __('Admin note: Your number of options and amounts are not equal.', 'stripe') . '</h6>'); } else { return ''; } } } if ($is_amount == 'true') { if (current_user_can('manage_options')) { echo '<h6>' . sprintf(__('Admin note: The "is_amount" attribute is deprecated and will be removed in an upcoming release. Please use the new "amounts" attribute instead. %s', 'stripe'), '<a href="' . SC_WEBSITE_BASE_URL . 'docs/shortcodes/stripe-custom-fields/" target="_blank">' . __('See Documentation', 'stripe') . '</a>') . '</h6>'; } } $quantity_html = 'true' == $is_quantity ? 'data-sc-quantity="true" ' : ''; $quantity_class = 'true' == $is_quantity ? ' sc-cf-quantity' : ''; $amount_class = !empty($amounts) || $is_amount == 'true' ? ' sc-cf-amount' : ''; $html = !empty($label) ? '<label>' . $label . '</label>' : ''; $html .= '<div class="sc-radio-group">'; $i = 1; foreach ($options as $option) { $option = trim($option); $value = $option; if (empty($default)) { $default = $option; } if ($is_amount == 'true') { $currency = strtoupper($sc_script_options['script']['currency']); $amount = Stripe_Checkout_Misc::to_formatted_amount($option, $currency); if ($currency == 'USD') { $option_name = '$' . $amount; } else { $option_name = $amount . ' ' . $currency; } } else { if (!empty($amounts)) { $value = $amounts[$i - 1]; } } if ($default == $option && $is_quantity != 'true' && !empty($amounts)) { $sc_script_options['script']['amount'] = $value; } // Don't use built-in checked() function here for now since we need "checked" in double quotes. $html .= '<label title="' . esc_attr($option) . '">'; $html .= '<input type="radio" name="sc_form_field[' . esc_attr($id) . ']" value="' . (isset($option_name) ? $option_name : $option) . '" ' . 'data-sc-price="' . esc_attr($value) . '" ' . ($default == $option ? 'checked="checked"' : '') . ' class="' . esc_attr($id) . '_' . $i . $quantity_class . $amount_class . '" data-parsley-errors-container=".' . apply_filters('sc_form_group_class', 'sc-form-group') . '" ' . $quantity_html . '>'; $html .= '<span>' . (isset($option_name) ? $option_name : $option) . '</span>'; $html .= '</label>'; $i++; } $html .= '</div>'; //sc-radio-group $attr['currency'] = strtoupper($sc_script_options['script']['currency']); $args = $this->get_args($id, $attr, $counter); // Incrememnt static counter $counter++; $this->total_fields(); return '<div class="' . apply_filters('sc_form_group_class', 'sc-form-group') . '">' . apply_filters('sc_stripe_radio', $html, $args) . '</div>'; }
public function validate_subscription($html) { $sub = Shortcode_Tracker::shortcode_exists_current('stripe_subscription'); $uea = Shortcode_Tracker::shortcode_exists_current('stripe_amount'); //$html = ''; // Neither exist so we can just exit now if ($sub === false && $uea === false) { return $html; } $sub_id = isset($sub['attr']['id']) ? true : false; $sub_children = isset($sub['children']) ? true : false; $use_amount = isset($sub['attr']['use_amount']) && $sub['attr']['use_amount'] == 'true' ? true : false; // Can't have both an ID and UEA if (($sub_id || $sub_children) && $uea) { Shortcode_Tracker::update_error_count(); if (current_user_can('manage_options')) { Shortcode_Tracker::add_error_message('<h6>' . __('Subscriptions must specify a plan ID or include a user-entered amount field. You cannot include both or omit both.', 'sc_sub') . '</h6>'); } } if (empty($sub_id) && ($uea || $use_amount) && $sub != false) { $interval = isset($sub['attr']['interval']) ? $sub['attr']['interval'] : 'month'; $interval_count = isset($sub['attr']['interval_count']) ? $sub['attr']['interval_count'] : 1; $statement_description = isset($sub['attr']['statement_description']) ? $sub['attr']['statement_description'] : ''; $html .= '<input type="hidden" name="sc_sub_id" class="sc_sub_id" value="" />'; $html .= '<input type="hidden" name="sc_sub_interval" class="sc_sub_interval" value="' . $interval . '" />'; $html .= '<input type="hidden" name="sc_sub_interval_count" class="sc_sub_interval_count" value="' . $interval_count . '" />'; $html .= '<input type="hidden" name="sc_sub_statement_description" class="sc_sub_statement_description" value="' . $statement_description . '" />'; } if (empty($sub_id) && !$uea && empty($sub_children) && $use_amount === false) { Shortcode_Tracker::update_error_count(); if (current_user_can('manage_options')) { Shortcode_Tracker::add_error_message('<h6>' . __('Subscriptions must specify a plan ID or include a user-entered amount field. You cannot include both or omit both.', 'sc_sub') . '</h6>'); } } return $html; }
public function main_shortcode($attr, $content = null) { // Our unique identifier in case multiple forms exist static $uid = 1; extract(shortcode_atts(array('label' => '', 'type' => 'radio', 'default' => '', 'show_details' => 'true', 'id' => '', 'interval' => 'month', 'interval_count' => 1, 'statement_description' => '', 'use_amount' => 'false', 'setup_fee' => 0), $attr, 'stripe_subscription')); Shortcode_Tracker::add_new_shortcode('stripe_subscription_' . $uid, 'stripe_subscription', $attr, false); $sub_amount = 0; $sub_interval = 0; $sub_interval_count = 0; $error_count = 0; $error_html = ''; $html = !empty($label) ? '<p class="sc-radio-group-label">' . $label . '</p>' : ''; // If the type is set to single then we just want to make sure the button points to the right plan // and that the button will be assigned the correct price for the subscripti // We use sanitize_text_field here to account for empty values that may exist $content_check = sanitize_text_field($content); // Limit to 15 $statement_description = substr($statement_description, 0, 22); if ($type != 'radio') { Shortcode_Tracker::update_error_count(); if (current_user_can('manage_options')) { Shortcode_Tracker::add_error_message('<h6>' . __('You have entered an invalid type.', 'sc_sub') . '</h6>'); } return; } if (empty($content_check)) { if (empty($id)) { return; } $sub = Stripe_Subscriptions_Functions::get_subscription_by_id($id); if (!is_object($sub)) { if (current_user_can('manage_options')) { //$error_html .= _x( 'Invalid subscription ID entered - ', 'Shown when the admin has entered an invalid subscription ID', 'sc_sub' ) . $id . '<br>'; $error_html .= $sub; $error_count++; } } else { $default = $sub->id; $sub_amount = $sub->amount; $sub_interval = $sub->interval; $sub_interval_count = $sub->interval_count; $sub_currency = $sub->currency; } $uid++; } else { $content = trim(substr($this->parse_shortcode_content($content), 0, -1)); $ids = explode(';', $content); static $id_num = 1; $html .= '<div class="sc-radio-group">'; foreach ($ids as $id) { $data = explode('|', $id); $data[0] = trim($data[0]); // Get the Subscription if (empty($data[0])) { Shortcode_Tracker::update_error_count(); if (current_user_can('manage_options')) { $error_html .= _x('You have not entered any subscription plan IDs', 'Shown when the admin has not entered any subscription IDs', 'sc_sub') . $data[0] . '<br>'; } $error_count++; continue; } $sub = Stripe_Subscriptions_Functions::get_subscription_by_id($data[0]); if (!is_object($sub)) { if (current_user_can('manage_options')) { //$error_html .= _x( 'Invalid subscription ID entered - ', 'Shown when the admin has entered an invalid subscription ID', 'sc_sub' ) . $id . '<br>'; $error_html .= $sub; } $error_count++; continue; } if (empty($default)) { $default = $sub->id; $sub_interval = $sub->interval; $sub_interval_count = $sub->interval_count; } if ($default == $sub->id) { $sub_amount = $sub->amount; } $currency = $sub->currency; $formatted_amount = Stripe_Checkout_Misc::to_formatted_amount($sub->amount, $sub->currency); $details_html = ' - '; $details_html .= ($currency == 'usd' ? '$' : '') . $formatted_amount . ($currency == 'usd' ? '' : ' ' . strtoupper($currency)); // For interval count of 1, use $1.00/month format. // For a count > 1, use $1.00 every 3 months format. if ($sub->interval_count == 1) { $details_html .= '/' . $sub->interval; } else { $details_html .= ' ' . __('every', 'sc_sub') . ' ' . $sub->interval_count . ' ' . $sub->interval . 's'; } $details_html = ($data[1] != 'null' ? $data[1] : $sub->name) . ($show_details == 'true' ? ' ' . $details_html : ''); $details_html = apply_filters('sc_subscription_details', $details_html, $sub); if ($type == 'radio') { $html .= '<label title="' . esc_attr($sub->name) . '">'; $html .= '<input type="radio" value="' . esc_attr($sub->name) . '" name="sc_radio_' . $uid . '" id="sc_radio_' . $id_num . '" data-sub-amount="' . $sub->amount . '" ' . 'data-sub-id="' . $sub->id . '" ' . (!empty($default) && $sub->id == $default ? 'checked' : '') . ' data-parsley-errors-container=".sc-radio-group" ' . ' data-sub-interval="' . $sub->interval . '" data-sub-interval-count="' . $sub->interval_count . '" data-sub-currency="' . $currency . '" ' . 'data-sub-setup-fee="' . (!empty($data[2]) ? $data[2] : 0) . '">'; $html .= '<span>' . $details_html . '</span>'; $html .= '</label>'; } if (!empty($default) && $sub->id == $default) { $sub_interval = $sub->interval; } $id_num++; } $html .= '</div>'; } if ($setup_fee > 0) { global $sc_script_options; $sc_script_options['script']['setupFee'] = $setup_fee; //add_filter( 'sc_stripe_total', array( 'Stripe_Subscriptions_Functions', 'setup_fee_stripe_total' ), 10, 2 ); } if ($error_count < 1) { $html .= '<input type="hidden" name="sc_sub_id" class="sc_sub_id" value="' . $default . '" />'; $html .= '<input type="hidden" name="sc_sub_amount" class="sc_sub_amount" value="' . $sub_amount . '" />'; $html .= '<input type="hidden" name="sc_sub_interval" class="sc_sub_interval" value="' . $sub_interval . '" />'; $html .= '<input type="hidden" name="sc_sub_interval_count" class="sc_sub_interval_count" value="' . $sub_interval_count . '" />'; $html .= '<input type="hidden" name="sc_sub_setup_fee" class="sc_sub_setup_fee" value="' . esc_attr($setup_fee) . '" />'; if (isset($sub_currency)) { $html .= '<input type="hidden" name="sc_sub_currency" class="sc_sub_currency" value="' . $sub_currency . '" />'; } return '<div class="sc_sub_wrapper sc-form-group" id="sc_sub_wrapper_' . $uid . '">' . $html . '</div>'; } else { Shortcode_Tracker::update_error_count(); if (current_user_can('manage_options')) { Shortcode_Tracker::add_error_message('<h6>An error has occurred. Please check your shortcode syntax.</h6>'); } return '<h6>' . __('An error has occurred. If the problem persists, please contact the site administrator.', 'sc_sub') . '</h6>'; } }