'clean', ]; } /** * List of keys of the default fields we want to display on the builder. * * @since 2.0.0 * @return array */ public function default_fields() { return [ // 'name', ]; } /** * If you want to force a particular attribute to a value, declare it here. * * @since 2.0.0 * @return array */ public function force_attributes() { return [ 'id' => 'period_selection', 'name' => __('Plan Duration Switch', 'wp-multisite-waas'), 'required' => true, ]; } /** * Returns the list of available pricing table templates. * * @since 2.0.0 * @return array */ public function get_template_options() { $available_templates = Field_Templates_Manager::get_instance()->get_templates_as_options('period_selection'); return $available_templates; } /** * Returns the list of additional fields specific to this type. * * @since 2.0.0 * @return array */ public function get_fields() { $editor_fields = []; $editor_fields['period_selection_template'] = [ 'type' => 'group', 'order' => 98.4, 'desc' => Field_Templates_Manager::get_instance()->render_preview_block('period_selection'), 'fields' => [ 'period_selection_template' => [ 'type' => 'select', 'title' => __('Period Selector Template', 'wp-multisite-waas'), 'placeholder' => __('Select your Template', 'wp-multisite-waas'), 'options' => [$this, 'get_template_options'], 'wrapper_classes' => 'wu-flex-grow', 'html_attr' => [ 'v-model' => 'period_selection_template', ], ], ], ]; $editor_fields['period_options_header'] = [ 'type' => 'small-header', 'title' => __('Options', 'wp-multisite-waas'), 'desc' => __('Add different options below. These need to match your product price variations.', 'wp-multisite-waas'), 'order' => 90, ]; $editor_fields['period_options_empty'] = [ 'type' => 'note', 'desc' => __('Add the first option using the button below.', 'wp-multisite-waas'), 'classes' => 'wu-text-gray-600 wu-text-xs wu-text-center wu-w-full', 'wrapper_classes' => 'wu-bg-gray-100 wu-items-end', 'order' => 90.5, 'wrapper_html_attr' => [ 'v-if' => 'period_options.length === 0', 'v-cloak' => '1', ], ]; $editor_fields['period_options'] = [ 'type' => 'group', 'tooltip' => '', 'order' => 91, 'wrapper_classes' => 'wu-relative wu-bg-gray-100 wu-pb-2', 'wrapper_html_attr' => [ 'v-if' => 'period_options.length', 'v-for' => '(period_option, index) in period_options', 'v-cloak' => '1', ], 'fields' => [ 'period_options_remove' => [ 'type' => 'note', 'desc' => sprintf('', __('Remove', 'wp-multisite-waas')), 'wrapper_classes' => 'wu-absolute wu-top-0 wu-right-0', ], 'period_options_duration' => [ 'type' => 'number', 'title' => __('Duration', 'wp-multisite-waas'), 'placeholder' => '', 'wrapper_classes' => 'wu-w-2/12', 'min' => 1, 'html_attr' => [ 'v-model' => 'period_option.duration', 'steps' => 1, 'v-bind:name' => '"period_options[" + index + "][duration]"', ], ], 'period_options_duration_unit' => [ 'type' => 'select', 'title' => ' ', 'placeholder' => '', 'wrapper_classes' => 'wu-w-5/12 wu-mx-2', 'html_attr' => [ 'v-model' => 'period_option.duration_unit', 'v-bind:name' => '"period_options[" + index + "][duration_unit]"', ], 'options' => [ 'day' => __('Days', 'wp-multisite-waas'), 'week' => __('Weeks', 'wp-multisite-waas'), 'month' => __('Months', 'wp-multisite-waas'), 'year' => __('Years', 'wp-multisite-waas'), ], ], 'period_options_label' => [ 'type' => 'text', 'title' => __('Label', 'wp-multisite-waas'), 'placeholder' => __('e.g. Monthly', 'wp-multisite-waas'), 'wrapper_classes' => 'wu-w-5/12', 'html_attr' => [ 'v-model' => 'period_option.label', 'v-bind:name' => '"period_options[" + index + "][label]"', ], ], ], ]; $editor_fields['repeat'] = [ 'order' => 92, 'type' => 'submit', 'title' => __('+ Add option', 'wp-multisite-waas'), 'classes' => 'wu-uppercase wu-text-2xs wu-text-blue-700 wu-border-none wu-bg-transparent wu-font-bold wu-text-right wu-w-full wu-cursor-pointer', 'wrapper_classes' => 'wu-bg-gray-100 wu-items-end', 'wrapper_html_attr' => [ 'v-cloak' => '1', ], 'html_attr' => [ 'v-on:click.prevent' => '() => period_options.push({ duration: 1, duration_unit: "month", label: "", })', ], ]; return $editor_fields; } /** * Returns the field/element actual field array to be used on the checkout form. * * @since 2.0.0 * * @param array $attributes Attributes saved on the editor form. * @return array An array of fields, not the field itself. */ public function to_fields_array($attributes) { if ('legacy' === wu_get_isset($attributes, 'period_selection_template')) { wp_register_script('wu-legacy-signup', wu_get_asset('legacy-signup.js', 'js'), ['wu-functions'], wu_get_version()); wp_enqueue_script('wu-legacy-signup'); wp_enqueue_style('legacy-shortcodes', wu_get_asset('legacy-shortcodes.css', 'css'), ['dashicons'], wu_get_version()); } $template_class = Field_Templates_Manager::get_instance()->get_template_class('period_selection', $attributes['period_selection_template']); $content = $template_class ? $template_class->render_container($attributes) : __('Template does not exist.', 'wp-multisite-waas'); $checkout_fields = []; $checkout_fields[ $attributes['id'] ] = [ 'type' => 'note', 'id' => $attributes['id'], 'wrapper_classes' => $attributes['element_classes'], 'desc' => $content, ]; $checkout_fields['duration'] = [ 'type' => 'hidden', 'html_attr' => [ 'v-model' => 'duration', ], ]; $checkout_fields['duration_unit'] = [ 'type' => 'hidden', 'html_attr' => [ 'v-model' => 'duration_unit', ], ]; return $checkout_fields; } }